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.
13 //usage:#define addgroup_trivial_usage
14 //usage: "[-g GID] " IF_FEATURE_ADDUSER_TO_GROUP("[USER] ") "GROUP"
15 //usage:#define addgroup_full_usage "\n\n"
16 //usage: "Add a group " IF_FEATURE_ADDUSER_TO_GROUP("or add a user to a group") "\n"
18 //usage: "\n -g GID Group id"
19 //usage: "\n -S Create a system group"
23 #if CONFIG_LAST_SYSTEM_ID < CONFIG_FIRST_SYSTEM_ID
24 #error Bad LAST_SYSTEM_ID or FIRST_SYSTEM_ID in .config
27 #define OPT_GID (1 << 0)
28 #define OPT_SYSTEM_ACCOUNT (1 << 1)
30 /* We assume GID_T_MAX == INT_MAX */
31 static void xgroup_study(struct group *g)
33 unsigned max = INT_MAX;
35 /* Make sure gr_name is unused */
36 if (getgrnam(g->gr_name)) {
37 bb_error_msg_and_die("%s '%s' in use", "group", g->gr_name);
38 /* these format strings are reused in adduser and addgroup */
41 /* if a specific gid is requested, the --system switch and */
42 /* min and max values are overridden, and the range of valid */
43 /* gid values is set to [0, INT_MAX] */
44 if (!(option_mask32 & OPT_GID)) {
45 if (option_mask32 & OPT_SYSTEM_ACCOUNT) {
46 g->gr_gid = CONFIG_FIRST_SYSTEM_ID;
47 max = CONFIG_LAST_SYSTEM_ID;
49 g->gr_gid = CONFIG_LAST_SYSTEM_ID + 1;
53 /* Check if the desired gid is free
54 * or find the first free one */
56 if (!getgrgid(g->gr_gid)) {
57 return; /* found free group: return */
59 if (option_mask32 & OPT_GID) {
60 /* -g N, cannot pick gid other than N: error */
61 bb_error_msg_and_die("%s '%s' in use", "gid", itoa(g->gr_gid));
62 /* this format strings is reused in adduser and addgroup */
64 if (g->gr_gid == max) {
65 /* overflowed: error */
66 bb_error_msg_and_die("no %cids left", 'g');
67 /* this format string is reused in adduser and addgroup */
73 /* append a new user to the passwd file */
74 static void new_group(char *group, gid_t gid)
79 /* make sure gid and group haven't already been allocated */
84 /* add entry to group */
85 p = xasprintf("x:%u:", (unsigned) gr.gr_gid);
86 if (update_passwd(bb_path_group_file, group, p, NULL) < 0)
88 if (ENABLE_FEATURE_CLEAN_UP)
90 #if ENABLE_FEATURE_SHADOWPASSWDS
91 /* /etc/gshadow fields:
93 * 2. Encrypted password.
94 * If set, non-members of the group can join the group
95 * by typing the password for that group using the newgrp command.
96 * If the value is of this field ! then no user is allowed
97 * to access the group using the newgrp command. A value of !!
98 * is treated the same as a value of ! only it indicates
99 * that a password has never been set before. If the value is null,
100 * only group members can log into the group.
101 * 3. Group administrators (comma delimited list).
102 * Group members listed here can add or remove group members
103 * using the gpasswd command.
104 * 4. Group members (comma delimited list).
106 /* Ignore errors: if file is missing we assume admin doesn't want it */
107 update_passwd(bb_path_gshadow_file, group, "!::", NULL);
111 #if ENABLE_FEATURE_ADDGROUP_LONG_OPTIONS
112 static const char addgroup_longopts[] ALIGN1 =
113 "gid\0" Required_argument "g"
114 "system\0" No_argument "S"
119 * addgroup will take a login_name as its first parameter.
121 * gid can be customized via command-line parameters.
122 * If called with two non-option arguments, addgroup
123 * will add an existing user to an existing group.
125 int addgroup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
126 int addgroup_main(int argc UNUSED_PARAM, char **argv)
131 /* need to be root */
133 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
135 #if ENABLE_FEATURE_ADDGROUP_LONG_OPTIONS
136 applet_long_options = addgroup_longopts;
140 * addgroup -g num group
141 * addgroup user group
142 * Check for min, max and missing args */
143 opt_complementary = "-1:?2:g+";
144 opts = getopt32(argv, "g:S", &gid);
145 /* move past the commandline options */
149 #if ENABLE_FEATURE_ADDUSER_TO_GROUP
153 if (opts & OPT_GID) {
154 /* -g was there, but "addgroup -g num user group"
159 /* check if group and user exist */
160 xuname2uid(argv[0]); /* unknown user: exit */
161 gr = xgetgrnam(argv[1]); /* unknown group: exit */
162 /* check if user is already in this group */
163 for (; *(gr->gr_mem) != NULL; (gr->gr_mem)++) {
164 if (!strcmp(argv[0], *(gr->gr_mem))) {
165 /* user is already in group: do nothing */
169 if (update_passwd(bb_path_group_file, argv[1], NULL, argv[0]) < 0) {
172 # if ENABLE_FEATURE_SHADOWPASSWDS
173 update_passwd(bb_path_gshadow_file, argv[1], NULL, argv[0]);
176 #endif /* ENABLE_FEATURE_ADDUSER_TO_GROUP */
178 die_if_bad_username(argv[0]);
179 new_group(argv[0], gid);
181 /* Reached only on success */