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 tarball for details.
15 static void xgroup_study(struct group *g)
18 /* Use a particular gid. */
19 int desired = (g->gr_gid > 0);
21 /* Make sure gr_name is unused */
22 if (getgrnam(g->gr_name)) {
26 /* Check if the desired gid is free or
27 find the first free one */
29 if (g->gr_gid == max) { /* out of bounds: exit */
30 bb_error_msg_and_die("no gids left");
32 if (!getgrgid(g->gr_gid)) {
35 if (desired) { /* the desired gid is already in use: exit */
36 g->gr_name = itoa(g->gr_gid);
44 bb_error_msg_and_die("%s: already in use", g->gr_name);
47 /* append a new user to the passwd file */
48 static void new_group(char *group, gid_t gid)
53 /* make sure gid and group haven't already been allocated */
58 /* add entry to group */
59 file = xfopen(bb_path_group_file, "a");
60 /* group:passwd:gid:userlist */
61 fprintf(file, "%s:x:%d:\n", group, gr.gr_gid);
62 if (ENABLE_FEATURE_CLEAN_UP)
64 #if ENABLE_FEATURE_SHADOWPASSWDS
65 file = fopen_or_warn(bb_path_gshadow_file, "a");
67 fprintf(file, "%s:!::\n", group);
68 if (ENABLE_FEATURE_CLEAN_UP)
74 #if ENABLE_FEATURE_ADDUSER_TO_GROUP
75 static void add_user_to_group(char **args,
77 FILE *(*fopen_func)(const char *fileName, const char *mode))
80 int len = strlen(args[1]);
81 llist_t *plist = NULL;
84 group_file = fopen_func(path, "r");
86 if (!group_file) return;
88 while ((line = xmalloc_getline(group_file))) {
90 if (!strncmp(line, args[1], len)
93 /* Add the new user */
94 line = xasprintf("%s%s%s", line,
95 last_char_is(line, ':') ? "" : ",",
98 llist_add_to_end(&plist, line);
101 if (ENABLE_FEATURE_CLEAN_UP) {
103 group_file = fopen_func(path, "w");
104 while ((line = llist_pop(&plist))) {
106 fprintf(group_file, "%s\n", line);
112 group_file = fopen_func(path, "w");
114 while ((line = llist_pop(&plist)))
115 fprintf(group_file, "%s\n", line);
121 * addgroup will take a login_name as its first parameter.
123 * gid can be customized via command-line parameters.
124 * If called with two non-option arguments, addgroup
125 * will add an existing user to an existing group.
127 int addgroup_main(int argc, char **argv);
128 int addgroup_main(int argc, char **argv)
133 /* check for min, max and missing args and exit on error */
134 opt_complementary = "-1:?2:?";
135 if (getopt32(argc, argv, "g:", &group)) {
136 gid = xatoul_range(group, 0, (gid_t)ULONG_MAX);
138 /* move past the commandline options */
142 /* need to be root */
144 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
147 #if ENABLE_FEATURE_ADDUSER_TO_GROUP
151 /* check if group and user exist */
152 xuname2uid(argv[0]); /* unknown user: exit */
153 xgroup2gid(argv[1]); /* unknown group: exit */
154 /* check if user is already in this group */
155 gr = getgrnam(argv[1]);
156 for (; *(gr->gr_mem) != NULL; (gr->gr_mem)++) {
157 if (!strcmp(argv[0], *(gr->gr_mem))) {
158 /* user is already in group: do nothing */
162 add_user_to_group(argv, bb_path_group_file, xfopen);
163 #if ENABLE_FEATURE_SHADOWPASSWDS
164 add_user_to_group(argv, bb_path_gshadow_file, fopen_or_warn);
165 #endif /* ENABLE_FEATURE_SHADOWPASSWDS */
167 #endif /* ENABLE_FEATURE_ADDUSER_TO_GROUP */
168 new_group(argv[0], gid);
170 /* Reached only on success */