usage.c: remove reference to busybox.h
[platform/upstream/busybox.git] / loginutils / addgroup.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * addgroup - add groups to /etc/group and /etc/gshadow
4  *
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>
8  *
9  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10  *
11  */
12
13 #include "libbb.h"
14
15 static void xgroup_study(struct group *g)
16 {
17         enum { max = 65000 };
18         /* Use a particular gid. */
19         int desired = (g->gr_gid > 0);
20
21         /* Make sure gr_name is unused */
22         if (getgrnam(g->gr_name)) {
23                 goto error;
24         }
25
26         /* Check if the desired gid is free or 
27            find the first free one */
28         do {
29                 if (g->gr_gid == max) { /* out of bounds: exit */
30                         bb_error_msg_and_die("no gids left");
31                 }
32                 if (!getgrgid(g->gr_gid)) {
33                         return; /* ok */
34                 }
35                 if (desired) { /* the desired gid is already in use: exit */
36                         g->gr_name = itoa(g->gr_gid);
37                         goto error;
38                 }
39                 g->gr_gid++;
40         } while (1);
41
42 error:
43         /* exit */
44         bb_error_msg_and_die("%s: already in use", g->gr_name);
45 }
46
47 /* append a new user to the passwd file */
48 static void new_group(char *group, gid_t gid)
49 {
50         FILE *file;
51         struct group gr;
52
53         /* make sure gid and group haven't already been allocated */
54         gr.gr_gid = gid;
55         gr.gr_name = group;
56         xgroup_study( &gr);
57
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)
63                 fclose(file);
64 #if ENABLE_FEATURE_SHADOWPASSWDS
65         file = fopen_or_warn(bb_path_gshadow_file, "a");
66         if (file) {
67                 fprintf(file, "%s:!::\n", group);
68                 if (ENABLE_FEATURE_CLEAN_UP)
69                         fclose(file);
70         }
71 #endif
72 }
73
74 #if ENABLE_FEATURE_ADDUSER_TO_GROUP
75 static void add_user_to_group(char **args,
76                 const char *path,
77                 FILE *(*fopen_func)(const char *fileName, const char *mode))
78 {
79         char *line;
80         int len = strlen(args[1]);
81         llist_t *plist = NULL;
82         FILE *group_file;
83
84         group_file = fopen_func(path, "r");
85
86         if (!group_file) return;
87
88         while ((line = xmalloc_getline(group_file))) {
89                 /* Find the group */
90                 if (!strncmp(line, args[1], len)
91                 && line[len] == ':'
92                 ) {
93                         /* Add the new user */
94                         line = xasprintf("%s%s%s", line,
95                                                 last_char_is(line, ':') ? "" : ",",
96                                                 args[0]);
97                 }
98                 llist_add_to_end(&plist, line);
99         }
100
101         if (ENABLE_FEATURE_CLEAN_UP) {
102                 fclose(group_file);
103                 group_file = fopen_func(path, "w");
104                 while ((line = llist_pop(&plist))) {
105                         if (group_file)
106                                 fprintf(group_file, "%s\n", line);
107                         free(line);
108                 }
109                 if (group_file)
110                         fclose(group_file);
111         } else {
112                 group_file = fopen_func(path, "w");
113                 if (group_file)
114                         while ((line = llist_pop(&plist)))
115                                 fprintf(group_file, "%s\n", line);
116         }
117 }
118 #endif
119
120 /*
121  * addgroup will take a login_name as its first parameter.
122  *
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.
126  */
127 int addgroup_main(int argc, char **argv);
128 int addgroup_main(int argc, char **argv)
129 {
130         char *group;
131         gid_t gid = 0;
132
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);
137         }
138         /* move past the commandline options */
139         argv += optind;
140         argc -= optind;
141
142         /* need to be root */
143         if (geteuid()) {
144                 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
145         }
146
147 #if ENABLE_FEATURE_ADDUSER_TO_GROUP
148         if (argc == 2) {
149                 struct group *gr;
150                 
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 */
159                                 return EXIT_SUCCESS;
160                         }
161                 }
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 */
166         } else
167 #endif /* ENABLE_FEATURE_ADDUSER_TO_GROUP */
168                 new_group(argv[0], gid);
169
170         /* Reached only on success */
171         return EXIT_SUCCESS;
172 }