1 /* vi: set sw=4 ts=4: */
3 * addgroup - add groups to /etc/group and /etc/gshadow
5 * Copyright (C) 1999 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
7 * Copyright (C) 2007 by Tito Ragusa <farmatito@tiscali.it>
9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
12 //config:config ADDGROUP
13 //config: bool "addgroup"
16 //config: Utility for creating a new group account.
18 //config:config FEATURE_ADDGROUP_LONG_OPTIONS
19 //config: bool "Enable long options"
21 //config: depends on ADDGROUP && LONG_OPTS
23 //config:config FEATURE_ADDUSER_TO_GROUP
24 //config: bool "Support adding users to groups"
26 //config: depends on ADDGROUP
28 //config: If called with two non-option arguments,
29 //config: addgroup will add an existing user to an
30 //config: existing group.
32 //applet:IF_ADDGROUP(APPLET(addgroup, BB_DIR_USR_SBIN, BB_SUID_DROP))
34 //kbuild:lib-$(CONFIG_ADDGROUP) += addgroup.o
36 //usage:#define addgroup_trivial_usage
37 //usage: "[-g GID] [-S] " IF_FEATURE_ADDUSER_TO_GROUP("[USER] ") "GROUP"
38 //usage:#define addgroup_full_usage "\n\n"
39 //usage: "Add a group" IF_FEATURE_ADDUSER_TO_GROUP(" or add a user to a group") "\n"
40 //usage: "\n -g GID Group id"
41 //usage: "\n -S Create a system group"
45 #if CONFIG_LAST_SYSTEM_ID < CONFIG_FIRST_SYSTEM_ID
46 #error Bad LAST_SYSTEM_ID or FIRST_SYSTEM_ID in .config
48 #if CONFIG_LAST_ID < CONFIG_LAST_SYSTEM_ID
49 #error Bad LAST_ID or LAST_SYSTEM_ID in .config
52 #define OPT_GID (1 << 0)
53 #define OPT_SYSTEM_ACCOUNT (1 << 1)
55 static void xgroup_study(struct group *g)
57 unsigned max = CONFIG_LAST_ID;
59 /* Make sure gr_name is unused */
60 if (getgrnam(g->gr_name)) {
61 bb_error_msg_and_die("%s '%s' in use", "group", g->gr_name);
62 /* these format strings are reused in adduser and addgroup */
65 /* if a specific gid is requested, the --system switch and */
66 /* min and max values are overridden, and the range of valid */
67 /* gid values is set to [0, INT_MAX] */
68 if (!(option_mask32 & OPT_GID)) {
69 if (option_mask32 & OPT_SYSTEM_ACCOUNT) {
70 g->gr_gid = CONFIG_FIRST_SYSTEM_ID;
71 max = CONFIG_LAST_SYSTEM_ID;
73 g->gr_gid = CONFIG_LAST_SYSTEM_ID + 1;
76 /* Check if the desired gid is free
77 * or find the first free one */
79 if (!getgrgid(g->gr_gid)) {
80 return; /* found free group: return */
82 if (option_mask32 & OPT_GID) {
83 /* -g N, cannot pick gid other than N: error */
84 bb_error_msg_and_die("%s '%s' in use", "gid", itoa(g->gr_gid));
85 /* this format strings is reused in adduser and addgroup */
87 if (g->gr_gid == max) {
88 /* overflowed: error */
89 bb_error_msg_and_die("no %cids left", 'g');
90 /* this format string is reused in adduser and addgroup */
96 /* append a new user to the passwd file */
97 static void new_group(char *group, gid_t gid)
102 /* make sure gid and group haven't already been allocated */
107 /* add entry to group */
108 p = xasprintf("x:%u:", (unsigned) gr.gr_gid);
109 if (update_passwd(bb_path_group_file, group, p, NULL) < 0)
111 if (ENABLE_FEATURE_CLEAN_UP)
113 #if ENABLE_FEATURE_SHADOWPASSWDS
114 /* /etc/gshadow fields:
116 * 2. Encrypted password.
117 * If set, non-members of the group can join the group
118 * by typing the password for that group using the newgrp command.
119 * If the value is of this field ! then no user is allowed
120 * to access the group using the newgrp command. A value of !!
121 * is treated the same as a value of ! only it indicates
122 * that a password has never been set before. If the value is null,
123 * only group members can log into the group.
124 * 3. Group administrators (comma delimited list).
125 * Group members listed here can add or remove group members
126 * using the gpasswd command.
127 * 4. Group members (comma delimited list).
129 /* Ignore errors: if file is missing we assume admin doesn't want it */
130 update_passwd(bb_path_gshadow_file, group, "!::", NULL);
134 #if ENABLE_FEATURE_ADDGROUP_LONG_OPTIONS
135 static const char addgroup_longopts[] ALIGN1 =
136 "gid\0" Required_argument "g"
137 "system\0" No_argument "S"
142 * addgroup will take a login_name as its first parameter.
144 * gid can be customized via command-line parameters.
145 * If called with two non-option arguments, addgroup
146 * will add an existing user to an existing group.
148 int addgroup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
149 int addgroup_main(int argc UNUSED_PARAM, char **argv)
152 const char *gid = "0";
154 /* need to be root */
156 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
158 #if ENABLE_FEATURE_ADDGROUP_LONG_OPTIONS
159 applet_long_options = addgroup_longopts;
163 * addgroup -g num group
164 * addgroup user group
165 * Check for min, max and missing args */
166 opt_complementary = "-1:?2";
167 opts = getopt32(argv, "g:S", &gid);
168 /* move past the commandline options */
172 #if ENABLE_FEATURE_ADDUSER_TO_GROUP
176 if (opts & OPT_GID) {
177 /* -g was there, but "addgroup -g num user group"
182 /* check if group and user exist */
183 xuname2uid(argv[0]); /* unknown user: exit */
184 gr = xgetgrnam(argv[1]); /* unknown group: exit */
185 /* check if user is already in this group */
186 for (; *(gr->gr_mem) != NULL; (gr->gr_mem)++) {
187 if (strcmp(argv[0], *(gr->gr_mem)) == 0) {
188 /* user is already in group: do nothing */
192 if (update_passwd(bb_path_group_file, argv[1], NULL, argv[0]) < 0) {
195 # if ENABLE_FEATURE_SHADOWPASSWDS
196 update_passwd(bb_path_gshadow_file, argv[1], NULL, argv[0]);
199 #endif /* ENABLE_FEATURE_ADDUSER_TO_GROUP */
201 die_if_bad_username(argv[0]);
202 new_group(argv[0], xatou_range(gid, 0, CONFIG_LAST_ID));
204 /* Reached only on success */