hdparm: remove stray static (-200 bytes bss)
[oweals/busybox.git] / loginutils / addgroup.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * addgroup - add users to /etc/passwd and /etc/shadow
4  *
5  * Copyright (C) 1999 by Lineo, inc. and John Beppu
6  * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9  *
10  */
11
12 #include "busybox.h"
13
14 /* make sure gr_name isn't taken, make sure gid is kosher
15  * return 1 on failure */
16 static int group_study(struct group *g)
17 {
18         enum { max = 65000 };
19         FILE *etc_group;
20         gid_t desired;
21         /* Using _r function to avoid static buffers pulled in */
22         char buffer[256];
23         struct group grp;
24         struct group *result;
25
26         etc_group = xfopen(bb_path_group_file, "r");
27
28         /* make sure gr_name isn't taken, make sure gid is kosher */
29         desired = g->gr_gid;
30         while (!fgetgrent_r(etc_group, &grp, buffer, sizeof(buffer), &result)) {
31                 if ((strcmp(grp.gr_name, g->gr_name)) == 0) {
32                         bb_error_msg_and_die("%s: group already in use", g->gr_name);
33                 }
34                 if ((desired) && grp.gr_gid == desired) {
35                         bb_error_msg_and_die("%d: gid already in use",
36                                                           desired);
37                 }
38                 if ((grp.gr_gid > g->gr_gid) && (grp.gr_gid < max)) {
39                         g->gr_gid = grp.gr_gid;
40                 }
41         }
42         if (ENABLE_FEATURE_CLEAN_UP)
43                 fclose(etc_group);
44
45         /* gid */
46         g->gr_gid++;
47         if (desired) {
48                 g->gr_gid = desired;
49         }
50         /* return 1; */
51         return 0;
52 }
53
54 /* append a new user to the passwd file */
55 static int addgroup(char *group, gid_t gid, const char *user)
56 {
57         FILE *file;
58         struct group gr;
59
60         /* make sure gid and group haven't already been allocated */
61         gr.gr_gid = gid;
62         gr.gr_name = group;
63         if (group_study(&gr))
64                 return 1;
65
66         /* add entry to group */
67         file = xfopen(bb_path_group_file, "a");
68         /* group:passwd:gid:userlist */
69         fprintf(file, "%s:%s:%d:%s\n", group, "x", gr.gr_gid, user);
70         if (ENABLE_FEATURE_CLEAN_UP)
71                 fclose(file);
72
73 #if ENABLE_FEATURE_SHADOWPASSWDS
74         file = fopen_or_warn(bb_path_gshadow_file, "a");
75         if (file) {
76                 fprintf(file, "%s:!::\n", group);
77                 if (ENABLE_FEATURE_CLEAN_UP)
78                         fclose(file);
79         }
80 #endif
81
82         /* return 1; */
83         return 0;
84 }
85
86 /*
87  * addgroup will take a login_name as its first parameter.
88  *
89  * gid can be customized via command-line parameters.
90  */
91 int addgroup_main(int argc, char **argv);
92 int addgroup_main(int argc, char **argv)
93 {
94         char *group;
95         gid_t gid = 0;
96
97         /* check for min, max and missing args and exit on error */
98         opt_complementary = "-1:?2:?";
99         if (getopt32(argc, argv, "g:", &group)) {
100                 gid = xatoul_range(group, 0, (gid_t)ULONG_MAX);
101         }
102         /* move past the commandline options */
103         argv += optind;
104
105         /* need to be root */
106         if (geteuid()) {
107                 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
108         }
109
110         return addgroup(argv[0], gid, argv[1] ? argv[1] : "");
111 }