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 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14 #include <sys/types.h>
19 /* make sure gr_name isn't taken, make sure gid is kosher
20 * return 1 on failure */
21 static int group_study(struct group *g)
27 const int max = 65000;
29 etc_group = bb_xfopen(bb_path_group_file, "r");
31 /* make sure gr_name isn't taken, make sure gid is kosher */
33 while ((grp = fgetgrent(etc_group))) {
34 if ((strcmp(grp->gr_name, g->gr_name)) == 0) {
35 bb_error_msg_and_die("%s: group already in use", g->gr_name);
37 if ((desired) && grp->gr_gid == desired) {
38 bb_error_msg_and_die("%d: gid already in use",
41 if ((grp->gr_gid > g->gr_gid) && (grp->gr_gid < max)) {
42 g->gr_gid = grp->gr_gid;
57 /* append a new user to the passwd file */
58 static int addgroup(char *group, gid_t gid, const char *user)
63 /* make sure gid and group haven't already been allocated */
69 /* add entry to group */
70 file = bb_xfopen(bb_path_group_file, "a");
71 /* group:passwd:gid:userlist */
72 fprintf(file, "%s:%s:%d:%s\n", group, "x", gr.gr_gid, user);
75 #if ENABLE_FEATURE_SHADOWPASSWDS
76 file = bb_xfopen(bb_path_gshadow_file, "a");
77 fprintf(file, "%s:!::\n", group);
86 * addgroup will take a login_name as its first parameter.
90 * can be customized via command-line parameters.
91 * ________________________________________________________________________ */
92 int addgroup_main(int argc, char **argv)
97 /* check for min, max and missing args and exit on error */
98 bb_opt_complementally = "-1:?2:?";
100 if (bb_getopt_ulflags(argc, argv, "g:", &group)) {
101 gid = bb_xgetlarg(group, 10, 0, LONG_MAX);
103 /* move past the commandline options */
106 /* need to be root */
108 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
112 return addgroup(argv[0], gid, (argv[1]) ? argv[1] : "");