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 /* make sure gr_name isn't taken, make sure gid is kosher
15 * return 1 on failure */
16 static int group_study(struct group *g)
22 const int max = 65000;
24 etc_group = xfopen(bb_path_group_file, "r");
26 /* make sure gr_name isn't taken, make sure gid is kosher */
28 while ((grp = fgetgrent(etc_group))) {
29 if ((strcmp(grp->gr_name, g->gr_name)) == 0) {
30 bb_error_msg_and_die("%s: group already in use", g->gr_name);
32 if ((desired) && grp->gr_gid == desired) {
33 bb_error_msg_and_die("%d: gid already in use",
36 if ((grp->gr_gid > g->gr_gid) && (grp->gr_gid < max)) {
37 g->gr_gid = grp->gr_gid;
52 /* append a new user to the passwd file */
53 static int addgroup(char *group, gid_t gid, const char *user)
58 /* make sure gid and group haven't already been allocated */
64 /* add entry to group */
65 file = xfopen(bb_path_group_file, "a");
66 /* group:passwd:gid:userlist */
67 fprintf(file, "%s:%s:%d:%s\n", group, "x", gr.gr_gid, user);
70 #if ENABLE_FEATURE_SHADOWPASSWDS
71 file = xfopen(bb_path_gshadow_file, "a");
72 fprintf(file, "%s:!::\n", group);
81 * addgroup will take a login_name as its first parameter.
85 * can be customized via command-line parameters.
86 * ________________________________________________________________________ */
87 int addgroup_main(int argc, char **argv)
92 /* check for min, max and missing args and exit on error */
93 opt_complementary = "-1:?2:?";
94 if (getopt32(argc, argv, "g:", &group)) {
95 gid = xatoul_range(group, 0, (gid_t)ULONG_MAX);
97 /* move past the commandline options */
100 /* need to be root */
102 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
106 return addgroup(argv[0], gid, (argv[1]) ? argv[1] : "");