1 /* vi: set sw=4 ts=4: */
3 * addgroup - add users to /etc/passwd and /etc/shadow
5 * Copyright (C) 1999 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14 /* make sure gr_name isn't taken, make sure gid is kosher
15 * return 1 on failure */
16 static int group_study(struct group *g)
21 /* Using _r function to avoid static buffers pulled in */
26 etc_group = xfopen(bb_path_group_file, "r");
28 /* make sure gr_name isn't taken, make sure gid is kosher */
30 while (!fgetgrent_r(etc_group, &grp, buffer, sizeof(buffer), &result)) {
31 if ((strcmp(grp.gr_name, g->gr_name)) == 0) {
32 bb_error_msg_and_die("%s: group already in use", g->gr_name);
34 if ((desired) && grp.gr_gid == desired) {
35 bb_error_msg_and_die("%d: gid already in use",
38 if ((grp.gr_gid > g->gr_gid) && (grp.gr_gid < max)) {
39 g->gr_gid = grp.gr_gid;
42 if (ENABLE_FEATURE_CLEAN_UP)
54 /* append a new user to the passwd file */
55 static int addgroup(char *group, gid_t gid, const char *user)
60 /* make sure gid and group haven't already been allocated */
66 /* add entry to group */
67 file = xfopen(bb_path_group_file, "a");
68 /* group:passwd:gid:userlist */
69 fprintf(file, "%s:%s:%d:%s\n", group, "x", gr.gr_gid, user);
70 if (ENABLE_FEATURE_CLEAN_UP)
73 #if ENABLE_FEATURE_SHADOWPASSWDS
74 file = fopen_or_warn(bb_path_gshadow_file, "a");
76 fprintf(file, "%s:!::\n", group);
77 if (ENABLE_FEATURE_CLEAN_UP)
87 * addgroup will take a login_name as its first parameter.
89 * gid can be customized via command-line parameters.
91 int addgroup_main(int argc, char **argv);
92 int addgroup_main(int argc, char **argv)
97 /* check for min, max and missing args and exit on error */
98 opt_complementary = "-1:?2:?";
99 if (getopt32(argc, argv, "g:", &group)) {
100 gid = xatoul_range(group, 0, (gid_t)ULONG_MAX);
102 /* move past the commandline options */
105 /* need to be root */
107 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
110 return addgroup(argv[0], gid, argv[1] ? argv[1] : "");