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 (8.2 kb)"
15 //config: select LONG_OPTS
17 //config: Utility for creating a new group account.
19 //config:config FEATURE_ADDUSER_TO_GROUP
20 //config: bool "Support adding users to groups"
22 //config: depends on ADDGROUP
24 //config: If called with two non-option arguments,
25 //config: addgroup will add an existing user to an
26 //config: existing group.
28 //applet:IF_ADDGROUP(APPLET_NOEXEC(addgroup, addgroup, BB_DIR_USR_SBIN, BB_SUID_DROP, addgroup))
30 //kbuild:lib-$(CONFIG_ADDGROUP) += addgroup.o
32 //usage:#define addgroup_trivial_usage
33 //usage: "[-g GID] [-S] " IF_FEATURE_ADDUSER_TO_GROUP("[USER] ") "GROUP"
34 //usage:#define addgroup_full_usage "\n\n"
35 //usage: "Add a group" IF_FEATURE_ADDUSER_TO_GROUP(" or add a user to a group") "\n"
36 //usage: "\n -g GID Group id"
37 //usage: "\n -S Create a system group"
41 #if CONFIG_LAST_SYSTEM_ID < CONFIG_FIRST_SYSTEM_ID
42 #error Bad LAST_SYSTEM_ID or FIRST_SYSTEM_ID in .config
44 #if CONFIG_LAST_ID < CONFIG_LAST_SYSTEM_ID
45 #error Bad LAST_ID or LAST_SYSTEM_ID in .config
48 #define OPT_GID (1 << 0)
49 #define OPT_SYSTEM_ACCOUNT (1 << 1)
51 static void xgroup_study(struct group *g)
53 unsigned max = CONFIG_LAST_ID;
55 /* Make sure gr_name is unused */
56 if (getgrnam(g->gr_name)) {
57 bb_error_msg_and_die("%s '%s' in use", "group", g->gr_name);
58 /* these format strings are reused in adduser and addgroup */
61 /* if a specific gid is requested, the --system switch and */
62 /* min and max values are overridden, and the range of valid */
63 /* gid values is set to [0, INT_MAX] */
64 if (!(option_mask32 & OPT_GID)) {
65 if (option_mask32 & OPT_SYSTEM_ACCOUNT) {
66 g->gr_gid = CONFIG_FIRST_SYSTEM_ID;
67 max = CONFIG_LAST_SYSTEM_ID;
69 g->gr_gid = CONFIG_LAST_SYSTEM_ID + 1;
72 /* Check if the desired gid is free
73 * or find the first free one */
75 if (!getgrgid(g->gr_gid)) {
76 return; /* found free group: return */
78 if (option_mask32 & OPT_GID) {
79 /* -g N, cannot pick gid other than N: error */
80 bb_error_msg_and_die("%s '%s' in use", "gid", itoa(g->gr_gid));
81 /* this format strings is reused in adduser and addgroup */
83 if (g->gr_gid == max) {
84 /* overflowed: error */
85 bb_error_msg_and_die("no %cids left", 'g');
86 /* this format string is reused in adduser and addgroup */
92 /* append a new user to the passwd file */
93 static void new_group(char *group, gid_t gid)
98 /* make sure gid and group haven't already been allocated */
103 /* add entry to group */
104 p = xasprintf("x:%u:", (unsigned) gr.gr_gid);
105 if (update_passwd(bb_path_group_file, group, p, NULL) < 0)
107 if (ENABLE_FEATURE_CLEAN_UP)
109 #if ENABLE_FEATURE_SHADOWPASSWDS
110 /* /etc/gshadow fields:
112 * 2. Encrypted password.
113 * If set, non-members of the group can join the group
114 * by typing the password for that group using the newgrp command.
115 * If the value is of this field ! then no user is allowed
116 * to access the group using the newgrp command. A value of !!
117 * is treated the same as a value of ! only it indicates
118 * that a password has never been set before. If the value is null,
119 * only group members can log into the group.
120 * 3. Group administrators (comma delimited list).
121 * Group members listed here can add or remove group members
122 * using the gpasswd command.
123 * 4. Group members (comma delimited list).
125 /* Ignore errors: if file is missing we assume admin doesn't want it */
126 update_passwd(bb_path_gshadow_file, group, "!::", NULL);
130 //FIXME: upstream addgroup has no short options! NOT COMPATIBLE!
131 static const char addgroup_longopts[] ALIGN1 =
132 "gid\0" Required_argument "g"
133 "system\0" No_argument "S"
137 * addgroup will take a login_name as its first parameter.
139 * gid can be customized via command-line parameters.
140 * If called with two non-option arguments, addgroup
141 * will add an existing user to an existing group.
143 int addgroup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
144 int addgroup_main(int argc UNUSED_PARAM, char **argv)
147 const char *gid = "0";
149 /* need to be root */
151 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
155 * addgroup --gid num group
156 * addgroup user group
157 * Check for min, max and missing args */
158 opts = getopt32long(argv, "^" "g:S" "\0" "-1:?2", addgroup_longopts,
161 /* move past the commandline options */
165 #if ENABLE_FEATURE_ADDUSER_TO_GROUP
169 if (opts & OPT_GID) {
170 /* -g was there, but "addgroup -g num user group"
175 /* check if group and user exist */
176 xuname2uid(argv[0]); /* unknown user: exit */
177 gr = xgetgrnam(argv[1]); /* unknown group: exit */
178 /* check if user is already in this group */
179 for (; *(gr->gr_mem) != NULL; (gr->gr_mem)++) {
180 if (strcmp(argv[0], *(gr->gr_mem)) == 0) {
181 /* user is already in group: do nothing */
185 if (update_passwd(bb_path_group_file, argv[1], NULL, argv[0]) < 0) {
188 # if ENABLE_FEATURE_SHADOWPASSWDS
189 update_passwd(bb_path_gshadow_file, argv[1], NULL, argv[0]);
192 #endif /* ENABLE_FEATURE_ADDUSER_TO_GROUP */
194 die_if_bad_username(argv[0]);
195 new_group(argv[0], xatou_range(gid, 0, CONFIG_LAST_ID));
197 /* Reached only on success */