2 * Copyright (c) 1991 - 1993, Julianne Frances Haugh
3 * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4 * Copyright (c) 2000 - 2006, Tomasz Kłoczko
5 * Copyright (c) 2007 - 2009, Nicolas François
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the copyright holders or contributors may not be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ident "$Id: groupadd.c 3015 2009-06-05 22:16:56Z nekral-guest $"
42 #include <sys/types.h>
43 #ifdef ACCT_TOOLS_SETUID
48 #endif /* ACCT_TOOLS_SETUID */
54 #include "prototypes.h"
63 #define E_SUCCESS 0 /* success */
64 #define E_USAGE 2 /* invalid command syntax */
65 #define E_BAD_ARG 3 /* invalid argument to option */
66 #define E_GID_IN_USE 4 /* gid not unique (when -o not used) */
67 #define E_NAME_IN_USE 9 /* group name not unique */
68 #define E_GRP_UPDATE 10 /* can't update group file */
75 static /*@null@*/char *group_name;
76 static gid_t group_id;
77 static /*@null@*/char *group_passwd;
78 static /*@null@*/char *empty_list = NULL;
80 static bool oflg = false; /* permit non-unique group ID to be specified with -g */
81 static bool gflg = false; /* ID value for the new group */
82 static bool fflg = false; /* if group already exists, do nothing and exit(0) */
83 static bool rflg = false; /* create a system account */
84 static bool pflg = false; /* new encrypted password */
87 static bool is_shadow_grp;
90 /* local function prototypes */
91 static void usage (void);
92 static void new_grent (struct group *grent);
95 static void new_sgent (struct sgrp *sgent);
97 static void grp_update (void);
98 static void check_new_name (void);
99 static void close_files (void);
100 static void open_files (void);
101 static void process_flags (int argc, char **argv);
102 static void check_flags (void);
103 static void check_perms (void);
106 * usage - display usage message and exit
108 static void usage (void)
110 (void) fprintf (stderr,
111 _("Usage: %s [options] GROUP\n"
115 (void) fputs (_(" -f, --force exit successfully if the group already exists,\n"
116 " and cancel -g if the GID is already used\n"), stderr);
117 (void) fputs (_(" -g, --gid GID use GID for the new group\n"), stderr);
118 (void) fputs (_(" -h, --help display this help message and exit\n"), stderr);
119 (void) fputs (_(" -K, --key KEY=VALUE override /etc/login.defs defaults\n"), stderr);
120 (void) fputs (_(" -o, --non-unique allow to create groups with duplicate\n"
121 " (non-unique) GID\n"), stderr);
122 (void) fputs (_(" -p, --password PASSWORD use this encrypted password for the new group\n"), stderr);
123 (void) fputs (_(" -r, --system create a system account\n"), stderr);
124 (void) fputs ("\n", stderr);
129 * new_grent - initialize the values in a group file entry
131 * new_grent() takes all of the values that have been entered and fills
132 * in a (struct group) with them.
134 static void new_grent (struct group *grent)
136 memzero (grent, sizeof *grent);
137 grent->gr_name = group_name;
139 grent->gr_passwd = group_passwd;
141 grent->gr_passwd = SHADOW_PASSWD_STRING; /* XXX warning: const */
143 grent->gr_gid = group_id;
144 grent->gr_mem = &empty_list;
149 * new_sgent - initialize the values in a shadow group file entry
151 * new_sgent() takes all of the values that have been entered and fills
152 * in a (struct sgrp) with them.
154 static void new_sgent (struct sgrp *sgent)
156 memzero (sgent, sizeof *sgent);
157 sgent->sg_name = group_name;
159 sgent->sg_passwd = group_passwd;
161 sgent->sg_passwd = "!"; /* XXX warning: const */
163 sgent->sg_adm = &empty_list;
164 sgent->sg_mem = &empty_list;
166 #endif /* SHADOWGRP */
169 * grp_update - add new group file entries
171 * grp_update() writes the new records to the group files.
173 static void grp_update (void)
179 #endif /* SHADOWGRP */
182 * To add the group, we need to update /etc/group.
183 * Make sure failures will be reported.
185 add_cleanup (cleanup_report_add_group_group, group_name);
188 /* We also need to update /etc/gshadow */
189 add_cleanup (cleanup_report_add_group_gshadow, group_name);
194 * Create the initial entries for this new group.
199 if (is_shadow_grp && pflg) {
200 grp.gr_passwd = SHADOW_PASSWD_STRING; /* XXX warning: const */
202 #endif /* SHADOWGRP */
205 * Write out the new group file entry.
207 if (gr_update (&grp) == 0) {
209 _("%s: failed to prepare the new %s entry '%s'\n"),
210 Prog, gr_dbname (), grp.gr_name);
215 * Write out the new shadow group entries as well.
217 if (is_shadow_grp && (sgr_update (&sgrp) == 0)) {
219 _("%s: failed to prepare the new %s entry '%s'\n"),
220 Prog, sgr_dbname (), sgrp.sg_name);
223 #endif /* SHADOWGRP */
227 * check_new_name - check the new name for validity
229 * check_new_name() insures that the new name doesn't contain any
230 * illegal characters.
232 static void check_new_name (void)
234 if (is_valid_group_name (group_name)) {
239 * All invalid group names land here.
242 fprintf (stderr, _("%s: '%s' is not a valid group name\n"),
249 * close_files - close all of the files that were opened
251 * close_files() closes all of the files that were opened for this new
252 * group. This causes any modified entries to be written out.
254 static void close_files (void)
256 /* First, write the changes in the regular group database */
257 if (gr_close () == 0) {
259 _("%s: failure while writing changes to %s\n"),
264 audit_logger (AUDIT_ADD_GROUP, Prog,
265 "adding group to /etc/group",
266 group_name, (unsigned int) group_id,
267 SHADOW_AUDIT_SUCCESS);
269 SYSLOG ((LOG_INFO, "group added to %s: name=%s, GID=%u",
270 gr_dbname (), group_name, (unsigned int) group_id));
271 del_cleanup (cleanup_report_add_group_group);
273 cleanup_unlock_group (NULL);
274 del_cleanup (cleanup_unlock_group);
276 /* Now, write the changes in the shadow database */
279 if (sgr_close () == 0) {
281 _("%s: failure while writing changes to %s\n"),
282 Prog, sgr_dbname ());
286 audit_logger (AUDIT_ADD_GROUP, Prog,
287 "adding group to /etc/gshadow",
288 group_name, (unsigned int) group_id,
289 SHADOW_AUDIT_SUCCESS);
291 SYSLOG ((LOG_INFO, "group added to %s: name=%s",
292 sgr_dbname (), group_name));
293 del_cleanup (cleanup_report_add_group_gshadow);
295 cleanup_unlock_gshadow (NULL);
296 del_cleanup (cleanup_unlock_gshadow);
298 #endif /* SHADOWGRP */
300 /* Report success at the system level */
302 audit_logger (AUDIT_ADD_GROUP, Prog,
304 group_name, (unsigned int) group_id,
305 SHADOW_AUDIT_SUCCESS);
307 SYSLOG ((LOG_INFO, "new group: name=%s, GID=%u",
308 group_name, (unsigned int) group_id));
309 del_cleanup (cleanup_report_add_group);
313 * open_files - lock and open the group files
315 * open_files() opens the two group files.
317 static void open_files (void)
319 /* First, lock the databases */
320 if (gr_lock () == 0) {
322 _("%s: cannot lock %s; try again later.\n"),
326 add_cleanup (cleanup_unlock_group, NULL);
330 if (sgr_lock () == 0) {
332 _("%s: cannot lock %s; try again later.\n"),
333 Prog, sgr_dbname ());
336 add_cleanup (cleanup_unlock_gshadow, NULL);
338 #endif /* SHADOWGRP */
341 * Now if the group is not added, it's our fault.
342 * Make sure failures will be reported.
344 add_cleanup (cleanup_report_add_group, group_name);
346 /* An now open the databases */
347 if (gr_open (O_RDWR) == 0) {
348 fprintf (stderr, _("%s: cannot open %s\n"), Prog, gr_dbname ());
349 SYSLOG ((LOG_WARN, "cannot open %s", gr_dbname ()));
355 if (sgr_open (O_RDWR) == 0) {
357 _("%s: cannot open %s\n"),
358 Prog, sgr_dbname ());
359 SYSLOG ((LOG_WARN, "cannot open %s", sgr_dbname ()));
363 #endif /* SHADOWGRP */
367 * process_flags - parse the command line options
369 * It will not return if an error is encountered.
371 static void process_flags (int argc, char **argv)
374 * Parse the command line options.
377 int option_index = 0;
379 static struct option long_options[] = {
380 {"force", no_argument, NULL, 'f'},
381 {"gid", required_argument, NULL, 'g'},
382 {"help", no_argument, NULL, 'h'},
383 {"key", required_argument, NULL, 'K'},
384 {"non-unique", no_argument, NULL, 'o'},
385 {"password", required_argument, NULL, 'p'},
386 {"system", no_argument, NULL, 'r'},
387 {NULL, 0, NULL, '\0'}
391 getopt_long (argc, argv, "fg:hK:op:r", long_options,
392 &option_index)) != -1) {
396 * "force" - do nothing, just exit(0), if the
397 * specified group already exists. With -g, if
398 * specified gid already exists, choose another
399 * (unique) gid (turn off -g). Based on the RedHat's
400 * patch from shadow-utils-970616-9.
406 if ( (get_gid (optarg, &group_id) == 0)
407 || (group_id == (gid_t)-1)) {
409 _("%s: invalid group ID '%s'\n"),
419 * override login.defs defaults (-K name=value)
420 * example: -K GID_MIN=100 -K GID_MAX=499
421 * note: -K GID_MIN=10,GID_MAX=499 doesn't work yet
423 cp = strchr (optarg, '=');
426 _("%s: -K requires KEY=VALUE\n"),
430 /* terminate name, point to value */
432 if (putdef_str (optarg, cp) < 0) {
441 group_passwd = optarg;
452 * Check the flags consistency
454 if (optind != argc - 1) {
457 group_name = argv[optind];
463 * check_flags - check flags and parameters consistency
465 * It will not return if an error is encountered.
467 static void check_flags (void)
469 /* -o does not make sense without -g */
477 * Check if the group already exist.
479 /* local, no need for xgetgrnam */
480 if (getgrnam (group_name) != NULL) {
481 /* The group already exist */
483 /* OK, no need to do anything */
487 _("%s: group '%s' already exists\n"),
489 exit (E_NAME_IN_USE);
492 if (gflg && (getgrgid (group_id) != NULL)) {
493 /* A GID was specified, and a group already exist with that GID
494 * - either we will use this GID anyway (-o)
495 * - either we ignore the specified GID and
496 * we will use another one (-f)
497 * - either it is a failure
500 /* Continue with this GID */
502 /* Turn off -g, we can use any GID */
506 _("%s: GID '%lu' already exists\n"),
507 Prog, (unsigned long int) group_id);
514 * check_perms - check if the caller is allowed to add a group
516 * With PAM support, the setuid bit can be set on groupadd to allow
517 * non-root users to groups.
518 * Without PAM support, only users who can write in the group databases
521 * It will not return if the user is not allowed.
523 static void check_perms (void)
525 #ifdef ACCT_TOOLS_SETUID
527 pam_handle_t *pamh = NULL;
529 struct passwd *pampw;
531 pampw = getpwuid (getuid ()); /* local, no need for xgetpwuid */
534 _("%s: Cannot determine your user name.\n"),
539 retval = pam_start ("groupadd", pampw->pw_name, &conv, &pamh);
541 if (PAM_SUCCESS == retval) {
542 retval = pam_authenticate (pamh, 0);
545 if (PAM_SUCCESS == retval) {
546 retval = pam_acct_mgmt (pamh, 0);
550 (void) pam_end (pamh, retval);
552 if (PAM_SUCCESS != retval) {
553 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
557 #endif /* ACCT_TOOLS_SETUID */
561 * main - groupadd command
563 int main (int argc, char **argv)
568 atexit (do_cleanups);
571 * Get my name so that I can use it to report errors.
573 Prog = Basename (argv[0]);
575 (void) setlocale (LC_ALL, "");
576 (void) bindtextdomain (PACKAGE, LOCALEDIR);
577 (void) textdomain (PACKAGE);
579 OPENLOG ("groupadd");
582 * Parse the command line options.
584 process_flags (argc, argv);
589 is_shadow_grp = sgr_file_present ();
593 * Do the hard stuff - open the files, create the group entries,
594 * then close and update the files.
599 if (find_new_gid (rflg, &group_id, NULL) < 0) {
607 nscd_flush_cache ("group");