blob: f4c024516a8f3d2f6d7b7be60be9a19805ca4953 [file] [log] [blame]
Robert Griebl1fca5582002-06-04 20:45:46 +00001/* 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"
Eric Andersen887ca792002-07-03 23:19:26 +000035#include "pwd_.h"
36#include "grp_.h"
Robert Griebl1fca5582002-06-04 20:45:46 +000037
Robert Griebl1fca5582002-06-04 20:45:46 +000038
39/* structs __________________________ */
40
41/* data _____________________________ */
42
43/* defaults : should this be in an external file? */
44static const char default_passwd[] = "x";
45
46
47/* make sure gr_name isn't taken, make sure gid is kosher
48 * return 1 on failure */
49static int group_study(const char *filename, struct group *g)
50{
51 FILE *etc_group;
52 gid_t desired;
53
54 struct group *grp;
55 const int max = 65000;
56
57 etc_group = xfopen(filename, "r");
58
59 /* make sure gr_name isn't taken, make sure gid is kosher */
60 desired = g->gr_gid;
61 while ((grp = fgetgrent(etc_group))) {
62 if ((strcmp(grp->gr_name, g->gr_name)) == 0) {
63 error_msg_and_die("%s: group already in use\n", g->gr_name);
64 }
65 if ((desired) && grp->gr_gid == desired) {
66 error_msg_and_die("%d: gid has already been allocated\n",
67 desired);
68 }
69 if ((grp->gr_gid > g->gr_gid) && (grp->gr_gid < max)) {
70 g->gr_gid = grp->gr_gid;
71 }
72 }
73 fclose(etc_group);
74
75 /* gid */
76 if (desired) {
77 g->gr_gid = desired;
78 } else {
79 g->gr_gid++;
80 }
81 /* return 1; */
82 return 0;
83}
84
85/* append a new user to the passwd file */
86static int addgroup(const char *filename, char *group, gid_t gid)
87{
88 FILE *etc_group;
89
90#ifdef CONFIG_FEATURE_SHADOWPASSWDS
91 FILE *etc_gshadow;
Manuel Novoa III edcac552002-08-06 20:39:23 +000092 const char *gshadow = gshadow_file;
Robert Griebl1fca5582002-06-04 20:45:46 +000093#endif
94
95 struct group gr;
96
97 /* group:passwd:gid:userlist */
98 static const char entryfmt[] = "%s:%s:%d:%s\n";
99
100 /* make sure gid and group haven't already been allocated */
101 gr.gr_gid = gid;
102 gr.gr_name = group;
103 if (group_study(filename, &gr))
104 return 1;
105
106 /* add entry to group */
107 etc_group = xfopen(filename, "a");
108
109 fprintf(etc_group, entryfmt, group, default_passwd, gr.gr_gid, "");
110 fclose(etc_group);
111
112
113#ifdef CONFIG_FEATURE_SHADOWPASSWDS
114 /* add entry to gshadow if necessary */
115 if (access(gshadow, F_OK|W_OK) == 0) {
116 etc_gshadow = xfopen(gshadow, "a");
117 fprintf(etc_gshadow, "%s:!::\n", group);
118 fclose(etc_gshadow);
119 }
120#endif
121
122 /* return 1; */
123 return 0;
124}
125
126/*
127 * addgroup will take a login_name as its first parameter.
128 *
129 * gid
130 *
131 * can be customized via command-line parameters.
132 * ________________________________________________________________________ */
133int addgroup_main(int argc, char **argv)
134{
Robert Griebl1fca5582002-06-04 20:45:46 +0000135 char *group;
136 gid_t gid = 0;
137
Eric Andersen09eb0002002-11-14 11:10:14 +0000138 if (argc < 2) {
Robert Griebl1fca5582002-06-04 20:45:46 +0000139 show_usage();
Robert Griebl1fca5582002-06-04 20:45:46 +0000140 }
141
Eric Andersen09eb0002002-11-14 11:10:14 +0000142 if (strncmp(argv[1], "-g", 2) == 0) {
143 gid = strtol(argv[2], NULL, 10);
144 group = argv[2];
145 } else {
146 show_usage();
147 }
148
Robert Griebl1fca5582002-06-04 20:45:46 +0000149 if (geteuid() != 0) {
150 error_msg_and_die
151 ("Only root may add a group to the system.");
152 }
153
154 /* werk */
Eric Andersen27f64e12002-06-23 04:24:25 +0000155 return addgroup(group_file, group, gid);
Robert Griebl1fca5582002-06-04 20:45:46 +0000156}
157
Eric Andersen09eb0002002-11-14 11:10:14 +0000158/* $Id: addgroup.c,v 1.6 2002/11/14 11:10:14 andersen Exp $ */