1 /* vi: set sw=4 ts=4: */
3 * addgroup - add users to /etc/passwd and /etc/shadow
5 * Copyright (C) 1999 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
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.
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.
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
30 #include <sys/param.h>
32 #include <sys/types.h>
39 /* structs __________________________ */
41 /* data _____________________________ */
43 /* defaults : should this be in an external file? */
44 static const char default_passwd[] = "x";
47 /* make sure gr_name isn't taken, make sure gid is kosher
48 * return 1 on failure */
49 static int group_study(const char *filename, struct group *g)
55 const int max = 65000;
57 etc_group = bb_xfopen(filename, "r");
59 /* make sure gr_name isn't taken, make sure gid is kosher */
61 while ((grp = fgetgrent(etc_group))) {
62 if ((strcmp(grp->gr_name, g->gr_name)) == 0) {
63 bb_error_msg_and_die("%s: group already in use\n", g->gr_name);
65 if ((desired) && grp->gr_gid == desired) {
66 bb_error_msg_and_die("%d: gid has already been allocated\n",
69 if ((grp->gr_gid > g->gr_gid) && (grp->gr_gid < max)) {
70 g->gr_gid = grp->gr_gid;
85 /* append a new user to the passwd file */
86 static int addgroup(const char *filename, char *group, gid_t gid, const char *user)
90 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
96 /* group:passwd:gid:userlist */
97 static const char entryfmt[] = "%s:%s:%d:%s\n";
99 /* make sure gid and group haven't already been allocated */
102 if (group_study(filename, &gr))
105 /* add entry to group */
106 etc_group = bb_xfopen(filename, "a");
108 fprintf(etc_group, entryfmt, group, default_passwd, gr.gr_gid, user);
112 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
113 /* add entry to gshadow if necessary */
114 if (access(bb_path_gshadow_file, F_OK|W_OK) == 0) {
115 etc_gshadow = bb_xfopen(bb_path_gshadow_file, "a");
116 fprintf(etc_gshadow, "%s:!::\n", group);
125 #ifndef CONFIG_ADDUSER
126 static inline void if_i_am_not_root(void)
129 bb_error_msg_and_die( "Only root may add a user or group to the system.");
133 extern void if_i_am_not_root(void);
137 * addgroup will take a login_name as its first parameter.
141 * can be customized via command-line parameters.
142 * ________________________________________________________________________ */
143 int addgroup_main(int argc, char **argv)
149 /* get remaining args */
150 if(bb_getopt_ulflags(argc, argv, "g:", &group)) {
151 gid = bb_xgetlarg(group, 10, 0, LONG_MAX);
155 group = argv[optind];
170 return addgroup(bb_path_group_file, group, gid, user);