e04a8d784087ff8e1363e13c98e98d78ae1bffd2
[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  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/param.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 #include "busybox.h"
35 #include "pwd.h"
36 #include "grp.h"
37
38 #define GROUP_FILE      "/etc/group"
39 #define SHADOW_FILE             "/etc/gshadow"
40
41
42 /* structs __________________________ */
43
44 /* data _____________________________ */
45
46 /* defaults : should this be in an external file? */
47 static const char default_passwd[] = "x";
48
49
50 /* make sure gr_name isn't taken, make sure gid is kosher
51  * return 1 on failure */
52 static int group_study(const char *filename, struct group *g)
53 {
54         FILE *etc_group;
55         gid_t desired;
56
57         struct group *grp;
58         const int max = 65000;
59
60         etc_group = xfopen(filename, "r");
61
62         /* make sure gr_name isn't taken, make sure gid is kosher */
63         desired = g->gr_gid;
64         while ((grp = fgetgrent(etc_group))) {
65                 if ((strcmp(grp->gr_name, g->gr_name)) == 0) {
66                         error_msg_and_die("%s: group already in use\n", g->gr_name);
67                 }
68                 if ((desired) && grp->gr_gid == desired) {
69                         error_msg_and_die("%d: gid has already been allocated\n",
70                                                           desired);
71                 }
72                 if ((grp->gr_gid > g->gr_gid) && (grp->gr_gid < max)) {
73                         g->gr_gid = grp->gr_gid;
74                 }
75         }
76         fclose(etc_group);
77
78         /* gid */
79         if (desired) {
80                 g->gr_gid = desired;
81         } else {
82                 g->gr_gid++;
83         }
84         /* return 1; */
85         return 0;
86 }
87
88 /* append a new user to the passwd file */
89 static int addgroup(const char *filename, char *group, gid_t gid)
90 {
91         FILE *etc_group;
92
93 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
94         FILE *etc_gshadow;
95         char *gshadow = SHADOW_FILE;
96 #endif
97
98         struct group gr;
99
100         /* group:passwd:gid:userlist */
101         static const char entryfmt[] = "%s:%s:%d:%s\n";
102
103         /* make sure gid and group haven't already been allocated */
104         gr.gr_gid = gid;
105         gr.gr_name = group;
106         if (group_study(filename, &gr))
107                 return 1;
108
109         /* add entry to group */
110         etc_group = xfopen(filename, "a");
111
112         fprintf(etc_group, entryfmt, group, default_passwd, gr.gr_gid, "");
113         fclose(etc_group);
114
115
116 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
117         /* add entry to gshadow if necessary */
118         if (access(gshadow, F_OK|W_OK) == 0) {
119                 etc_gshadow = xfopen(gshadow, "a");
120                 fprintf(etc_gshadow, "%s:!::\n", group);
121                 fclose(etc_gshadow);
122         }
123 #endif
124
125         /* return 1; */
126         return 0;
127 }
128
129 /*
130  * addgroup will take a login_name as its first parameter.
131  *
132  * gid 
133  *
134  * can be customized via command-line parameters.
135  * ________________________________________________________________________ */
136 int addgroup_main(int argc, char **argv)
137 {
138         int opt;
139         char *group;
140         gid_t gid = 0;
141
142         /* get remaining args */
143         while ((opt = getopt (argc, argv, "g:")) != -1)
144                 switch (opt) {
145                         case 'g':
146                                 gid = strtol(optarg, NULL, 10);
147                                 break;
148                         default:
149                                 show_usage();
150                                 break;
151                 }
152
153         if (optind >= argc) {
154                 show_usage();
155         } else {
156                 group = argv[optind];
157         }
158
159         if (geteuid() != 0) {
160                 error_msg_and_die
161                         ("Only root may add a group to the system.");
162         }
163
164         /* werk */
165         return addgroup(GROUP_FILE, group, gid);
166 }
167
168 /* $Id: addgroup.c,v 1.1 2002/06/04 20:45:05 sandman Exp $ */