1 /* vi: set sw=4 ts=4: */
3 * addgroup - add users to /etc/passwd and /etc/shadow
6 * Copyright (C) 1999 by Lineo, inc.
7 * Written by John Beppu <beppu@lineo.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <sys/param.h>
33 #include <sys/types.h>
36 #include "pwd_grp/pwd.h"
37 #include "pwd_grp/grp.h"
39 #define GROUP_FILE "/etc/group"
40 #define SHADOW_FILE "/etc/gshadow"
43 /* structs __________________________ */
45 /* data _____________________________ */
47 /* defaults : should this be in an external file? */
48 static char *default_passwd = "x";
51 /* make sure gr_name isn't taken, make sure gid is kosher
52 * return 1 on failure */
53 static int group_study(const char *filename, struct group *g)
59 const int max = 65000;
61 /* FIXME : make an fopen_wrapper */
62 etc_group = fopen(filename, "r");
64 perror_msg_and_die("%s", filename);
67 /* make sure gr_name isn't taken, make sure gid is kosher */
69 while ((grp = fgetgrent(etc_group))) {
70 if ((strcmp(grp->gr_name, g->gr_name)) == 0) {
71 error_msg_and_die("%s: group already in use\n", g->gr_name);
73 if ((desired) && grp->gr_gid == desired) {
74 error_msg_and_die("%d: gid has already been allocated\n",
77 if ((grp->gr_gid > g->gr_gid) && (grp->gr_gid < max)) {
78 g->gr_gid = grp->gr_gid;
93 /* append a new user to the passwd file */
94 static int addgroup(const char *filename, char *group, gid_t gid)
98 char *gshadow = SHADOW_FILE;
102 /* group:passwd:gid:userlist */
103 const char *entryfmt = "%s:%s:%d:%s\n";
105 /* make sure gid and group haven't already been allocated */
108 if (group_study(filename, &gr))
111 /* add entry to group */
112 etc_group = fopen(filename, "a");
114 perror_msg_and_die("%s", filename);
116 fprintf(etc_group, entryfmt, group, default_passwd, gr.gr_gid, "");
119 /* add entry to gshadow if necessary */
120 if (access(gshadow, F_OK|W_OK) == 0) {
121 etc_gshadow = xfopen(gshadow, "a");
122 fprintf(etc_gshadow, "%s:!::\n", group);
131 * addgroup will take a login_name as its first parameter.
135 * can be customized via command-line parameters.
136 * ________________________________________________________________________ */
137 int addgroup_main(int argc, char **argv)
144 /* get remaining args */
145 for (i = 1; i < argc; i++) {
146 if (argv[i][0] == '-') {
153 gid = strtol(argv[++i], NULL, 10);
156 error_msg_and_die("addgroup: invalid option -- %c\n", opt);
169 if (geteuid() != 0) {
171 ("addgroup: Only root may add a group to the system.\n");
175 return addgroup(GROUP_FILE, group, gid);
178 /* $Id: addgroup.c,v 1.1 2001/08/21 16:18:59 andersen Exp $ */