2 * Copyright 1990 - 1994, Julianne Frances Haugh
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/types.h>
42 #include "exitcodes.h"
45 #include "prototypes.h"
52 /* The name of this command, as it is invoked */
56 /* Indicate if shadow groups are enabled on the system
57 * (/etc/gshadow present) */
58 static int is_shadowgrp;
61 /* Flags set by options */
63 aflg = 0, Aflg = 0, dflg = 0, Mflg = 0, rflg = 0, Rflg = 0;
64 /* The name of the group that is being affected */
65 static char *group = NULL;
66 /* The name of the user being added (-a) or removed (-d) from group */
67 static char *user = NULL;
68 /* The new list of members set with -M */
69 static char *members = NULL;
71 /* The new list of group administrators set with -A */
72 static char *admins = NULL;
74 /* The name of the caller */
75 static char *myname = NULL;
76 /* The UID of the caller */
77 static unsigned long bywho = -1;
78 /* Indicate if gpasswd was called by root */
79 #define amroot (0 == bywho)
81 /* The number of retries for th user to provide and repeat a new password */
86 /* local function prototypes */
87 static void usage (void);
88 static RETSIGTYPE catch_signals (int killed);
89 static int check_list (const char *users);
90 static void process_flags (int argc, char **argv);
91 static void check_flags (int argc, int opt_index);
92 static void open_files (void);
93 static void close_files (void);
95 static void get_group (struct group *gr, struct sgrp *sg);
96 static void check_perms (const struct sgrp *sg);
97 static void update_group (struct group *gr, struct sgrp *sg);
98 static void change_passwd (struct group *gr, struct sgrp *sg);
100 static void get_group (struct group *gr);
101 static void check_perms (const struct group *gr);
102 static void update_group (struct group *gr);
103 static void change_passwd (struct group *gr);
107 * usage - display usage message
109 static void usage (void)
111 fprintf (stderr, _("Usage: %s [-r|-R] group\n"), Prog);
112 fprintf (stderr, _(" %s [-a user] group\n"), Prog);
113 fprintf (stderr, _(" %s [-d user] group\n"), Prog);
116 _(" %s [-A user,...] [-M user,...] group\n"), Prog);
118 fprintf (stderr, _(" %s [-M user,...] group\n"), Prog);
124 * catch_signals - set or reset termio modes.
126 * catch_signals() is called before processing begins. signal() is then
127 * called with catch_signals() as the signal handler. If signal later
128 * calls catch_signals() with a signal number, the terminal modes are
131 static RETSIGTYPE catch_signals (int killed)
149 * check_list - check a comma-separated list of user names for validity
151 * check_list scans a comma-separated list of user names and checks
152 * that each listed name exists.
154 static int check_list (const char *users)
156 const char *start, *end;
161 for (start = users; start && *start; start = end) {
162 if ((end = strchr (start, ','))) {
166 len = strlen (start);
169 if (len > sizeof (username) - 1) {
170 len = sizeof (username) - 1;
172 strncpy (username, start, len);
173 username[len] = '\0';
176 * This user must exist.
179 if (!getpwnam (username)) { /* local, no need for xgetpwnam */
180 fprintf (stderr, _("%s: unknown user %s\n"),
188 static void failure (void)
190 fprintf (stderr, _("%s: Permission denied.\n"), Prog);
195 * process_flags - process the command line options and arguments
197 static void process_flags (int argc, char **argv)
201 while ((flag = getopt (argc, argv, "a:A:d:gM:rR")) != EOF) {
203 case 'a': /* add a user */
205 /* local, no need for xgetpwnam */
206 if (!getpwnam (user)) {
208 _("%s: unknown user %s\n"), Prog,
211 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
212 "adding to group", user, -1, 0);
222 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
223 "Listing administrators", NULL,
231 ("%s: shadow group passwords required for -A\n"),
236 if (check_list (admins)) {
242 case 'd': /* delete a user */
246 case 'g': /* no-op from normal password */
251 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
252 "listing members", NULL, bywho,
258 if (check_list (members)) {
263 case 'r': /* remove group password */
266 case 'R': /* restrict group password */
274 /* Get the name of the group that is being affected. */
275 group = argv[optind];
277 check_flags (argc, optind);
281 * check_flags - check the validity of options
283 static void check_flags (int argc, int opt_index)
286 * Make sure exclusive flags are exclusive
288 if (aflg + dflg + rflg + Rflg + (Aflg || Mflg) > 1) {
293 * Make sure one (and only one) group was provided
295 if ((argc != (opt_index+1)) || (NULL == group)) {
301 * open_files - lock and open the group databases
303 * It will call exit in case of error.
305 static void open_files (void)
307 if (gr_lock () == 0) {
308 fprintf (stderr, _("%s: can't get lock\n"), Prog);
309 SYSLOG ((LOG_WARN, "failed to get lock for /etc/group"));
311 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
312 "locking /etc/group", group, -1, 0);
317 if (is_shadowgrp && (sgr_lock () == 0)) {
318 fprintf (stderr, _("%s: can't get shadow lock\n"), Prog);
319 SYSLOG ((LOG_WARN, "failed to get lock for /etc/gshadow"));
321 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
322 "locking /etc/gshadow", group, -1, 0);
327 if (gr_open (O_RDWR) == 0) {
328 fprintf (stderr, _("%s: can't open file\n"), Prog);
329 SYSLOG ((LOG_WARN, "cannot open /etc/group"));
331 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
332 "opening /etc/group", group, -1, 0);
337 if (is_shadowgrp && (sgr_open (O_RDWR) == 0)) {
338 fprintf (stderr, _("%s: can't open shadow file\n"), Prog);
339 SYSLOG ((LOG_WARN, "cannot open /etc/gshadow"));
341 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
342 "opening /etc/gshadow", group, -1, 0);
350 * close_files - close and unlock the group databases
352 * This cause any changes in the databases to be committed.
354 * It will call exit in case of error.
356 static void close_files (void)
358 if (gr_close () == 0) {
359 fprintf (stderr, _("%s: can't re-write file\n"), Prog);
360 SYSLOG ((LOG_WARN, "cannot re-write /etc/group"));
362 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
363 "rewriting /etc/group", group, -1, 0);
368 if (is_shadowgrp && (sgr_close () == 0)) {
369 fprintf (stderr, _("%s: can't re-write shadow file\n"), Prog);
370 SYSLOG ((LOG_WARN, "cannot re-write /etc/gshadow"));
372 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
373 "rewriting /etc/gshadow", group, -1, 0);
378 /* TODO: same logging as in open_files & for /etc/group */
382 if (gr_unlock () == 0) {
383 fprintf (stderr, _("%s: can't unlock file\n"), Prog);
385 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
386 "unlocking group file", group, -1, 0);
393 * check_perms - check if the user is allowed to change the password of
394 * the specified group.
396 * It only returns if the user is allowed.
399 static void check_perms (const struct sgrp *sg)
401 static void check_perms (const struct group *gr)
406 * The policy here for changing a group is that 1) you must be root
407 * or 2). you must be listed as an administrative member.
408 * Administrative members can do anything to a group that the root
411 if (!amroot && !is_on_list (sg->sg_adm, myname)) {
413 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
414 "modify group", group, -1, 0);
418 #else /* ! SHADOWGRP */
420 #ifdef FIRST_MEMBER_IS_ADMIN
422 * The policy here for changing a group is that 1) you must bes root
423 * or 2) you must be the first listed member of the group. The
424 * first listed member of a group can do anything to that group that
425 * the root user can. The rationale for this hack is that the FIRST
426 * user is probably the most important user in this entire group.
429 if (gr->gr_mem[0] == (char *) 0) {
431 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
432 "modifying group", group, -1, 0);
437 if (strcmp (gr->gr_mem[0], myname) != 0) {
439 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
440 "modifying group", myname, -1, 0);
447 * This feature enabled by default could be a security problem when
448 * installed on existing systems where the first group member might
449 * be just a normal user. --marekm
453 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
454 "modifying group", group, -1, 0);
459 #endif /* SHADOWGRP */
463 * update_group - Update the group information in the databases
466 static void update_group (struct group *gr, struct sgrp *sg)
468 static void update_group (struct group *gr)
471 if (!gr_update (gr)) {
472 fprintf (stderr, _("%s: can't update entry\n"), Prog);
473 SYSLOG ((LOG_WARN, "cannot update /etc/group"));
475 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
476 "updating /etc/group", group, -1, 0);
481 if (is_shadowgrp && !sgr_update (sg)) {
482 fprintf (stderr, _("%s: can't update shadow entry\n"), Prog);
483 SYSLOG ((LOG_WARN, "cannot update /etc/gshadow"));
485 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
486 "updating /etc/gshadow", group, -1, 0);
494 * get_group - get the current information for the group
496 * The information are copied in group structure(s) so that they can be
500 static void get_group (struct group *gr, struct sgrp *sg)
502 static void get_group (struct group *gr)
505 struct group const*tmpgr = NULL;
506 struct sgrp const*tmpsg = NULL;
508 if (!gr_open (O_RDONLY)) {
509 fprintf (stderr, _("%s: can't open file\n"), Prog);
510 SYSLOG ((LOG_WARN, "cannot open /etc/group"));
512 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
513 "opening /etc/group", group, -1, 0);
518 if (!(tmpgr = gr_locate (group))) {
519 fprintf (stderr, _("unknown group: %s\n"), group);
521 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
522 "group lookup", group, -1, 0);
528 gr->gr_name = xstrdup (tmpgr->gr_name);
529 gr->gr_passwd = xstrdup (tmpgr->gr_passwd);
530 gr->gr_mem = dup_list (tmpgr->gr_mem);
533 fprintf (stderr, _("%s: can't close file\n"), Prog);
534 SYSLOG ((LOG_WARN, "cannot close /etc/group"));
536 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
537 "closing /etc/group", group, -1, 0);
543 if (!sgr_open (O_RDONLY)) {
544 fprintf (stderr, _("%s: can't open shadow file\n"), Prog);
545 SYSLOG ((LOG_WARN, "cannot open /etc/gshadow"));
547 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
548 "opening /etc/gshadow", group, -1, 0);
552 if ((tmpsg = sgr_locate (group))) {
554 sg->sg_name = xstrdup (tmpsg->sg_name);
555 sg->sg_passwd = xstrdup (tmpsg->sg_passwd);
557 sg->sg_mem = dup_list (tmpsg->sg_mem);
558 sg->sg_adm = dup_list (tmpsg->sg_adm);
560 sg->sg_name = xstrdup (group);
561 sg->sg_passwd = gr->gr_passwd;
562 gr->gr_passwd = "!"; /* XXX warning: const */
564 sg->sg_mem = dup_list (gr->gr_mem);
566 sg->sg_adm = (char **) xmalloc (sizeof (char *) * 2);
567 #ifdef FIRST_MEMBER_IS_ADMIN
569 sg->sg_adm[0] = xstrdup (sg->sg_mem[0]);
579 fprintf (stderr, _("%s: can't close shadow file\n"), Prog);
580 SYSLOG ((LOG_WARN, "cannot close /etc/gshadow"));
582 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
583 "closing /etc/gshadow", group, -1, 0);
587 #endif /* SHADOWGRP */
591 * change_passwd - change the group's password
593 * Get the new password from the user and update the password in the
596 * It will call exit in case of error.
599 static void change_passwd (struct group *gr, struct sgrp *sg)
601 static void change_passwd (struct group *gr)
605 static char pass[BUFSIZ];
609 * A new password is to be entered and it must be encrypted, etc.
610 * The password will be prompted for twice, and both entries must be
611 * identical. There is no need to validate the old password since
612 * the invoker is either the group owner, or root.
614 printf (_("Changing the password for group %s\n"), group);
616 for (retries = 0; retries < RETRIES; retries++) {
617 if (!(cp = getpass (_("New Password: ")))) {
623 if (!(cp = getpass (_("Re-enter new password: ")))) {
627 if (strcmp (pass, cp) == 0) {
633 memzero (pass, sizeof pass);
635 if (retries + 1 < RETRIES) {
636 puts (_("They don't match; try again"));
638 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
639 "changing password", group, -1, 0);
644 if (retries == RETRIES) {
645 fprintf (stderr, _("%s: Try again later\n"), Prog);
649 cp = pw_encrypt (pass, crypt_make_salt (NULL, NULL));
650 memzero (pass, sizeof pass);
660 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
661 "changing password", group, -1, 1);
663 SYSLOG ((LOG_INFO, "change the password for group %s by %s", group,
668 * gpasswd - administer the /etc/group file
670 * -a user add user to the named group
671 * -d user remove user from the named group
672 * -r remove password from the named group
673 * -R restrict access to the named group
674 * -A user,... make list of users the administrative users
675 * -M user,... make list of users the group members
677 int main (int argc, char **argv)
683 struct passwd *pw = NULL;
690 setlocale (LC_ALL, "");
691 bindtextdomain (PACKAGE, LOCALEDIR);
692 textdomain (PACKAGE);
695 * Make a note of whether or not this command was invoked by root.
696 * This will be used to bypass certain checks later on. Also, set
697 * the real user ID to match the effective user ID. This will
698 * prevent the invoker from issuing signals which would interfer
702 Prog = Basename (argv[0]);
705 setbuf (stdout, NULL);
706 setbuf (stderr, NULL);
709 is_shadowgrp = sgr_file_present ();
712 /* Parse the options */
713 process_flags (argc, argv);
716 * Determine the name of the user that invoked this command. This
717 * is really hit or miss because there are so many ways that command
718 * can be executed and so many ways to trip up the routines that
719 * report the user name.
722 pw = get_my_pwent ();
724 fprintf (stderr, _("Who are you?\n"));
726 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "user lookup", NULL,
731 myname = xstrdup (pw->pw_name);
734 * Replicate the group so it can be modified later on.
737 get_group (&grent, &sgent);
743 * Check if the user is allowed to change the password of this group.
746 check_perms (&sgent);
748 check_perms (&grent);
752 * Removing a password is straight forward. Just set the password
756 grent.gr_passwd = ""; /* XXX warning: const */
758 sgent.sg_passwd = ""; /* XXX warning: const */
761 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
762 "deleting group password", group, -1, 1);
764 SYSLOG ((LOG_INFO, "remove password from group %s by %s",
769 * Same thing for restricting the group. Set the password
772 grent.gr_passwd = "!"; /* XXX warning: const */
774 sgent.sg_passwd = "!"; /* XXX warning: const */
777 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
778 "restrict access to group", group, -1, 1);
780 SYSLOG ((LOG_INFO, "restrict access to group %s by %s",
786 * Adding a member to a member list is pretty straightforward as
787 * well. Call the appropriate routine and split.
790 printf (_("Adding user %s to group %s\n"), user, group);
791 grent.gr_mem = add_list (grent.gr_mem, user);
793 sgent.sg_mem = add_list (sgent.sg_mem, user);
796 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "adding group member",
799 SYSLOG ((LOG_INFO, "add member %s to group %s by %s", user,
805 * Removing a member from the member list is the same deal as adding
806 * one, except the routine is different.
811 printf (_("Removing user %s from group %s\n"), user, group);
813 if (is_on_list (grent.gr_mem, user)) {
815 grent.gr_mem = del_list (grent.gr_mem, user);
818 if (is_on_list (sgent.sg_mem, user)) {
820 sgent.sg_mem = del_list (sgent.sg_mem, user);
824 fprintf (stderr, _("%s: unknown member %s\n"),
827 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
828 "deleting member", user, -1, 0);
833 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "deleting member",
836 SYSLOG ((LOG_INFO, "remove member %s from group %s by %s",
837 user, group, myname));
842 * Replacing the entire list of administators is simple. Check the
843 * list to make sure everyone is a real user. Then slap the new list
848 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "setting group admin",
851 SYSLOG ((LOG_INFO, "set administrators of %s to %s",
853 sgent.sg_adm = comma_to_list (admins);
861 * Replacing the entire list of members is simple. Check the list to
862 * make sure everyone is a real user. Then slap the new list in
867 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
868 "setting group members", group, -1, 1);
870 SYSLOG ((LOG_INFO, "set members of %s to %s", group, members));
872 sgent.sg_mem = comma_to_list (members);
874 grent.gr_mem = comma_to_list (members);
879 * If the password is being changed, the input and output must both
880 * be a tty. The typical keyboard signals are caught so the termio
881 * modes can be restored.
883 if (!isatty (0) || !isatty (1)) {
884 fprintf (stderr, _("%s: Not a tty\n"), Prog);
886 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing password",
892 catch_signals (0); /* save tty modes */
894 signal (SIGHUP, catch_signals);
895 signal (SIGINT, catch_signals);
896 signal (SIGQUIT, catch_signals);
897 signal (SIGTERM, catch_signals);
899 signal (SIGTSTP, catch_signals);
902 /* Prompt for the new password */
904 change_passwd (&grent, &sgent);
906 change_passwd (&grent);
910 * This is the common arrival point to output the new group file.
911 * The freshly crafted entry is in allocated space. The group file
912 * will be locked and opened for writing. The new entry will be
917 fprintf (stderr, _("Cannot change ID to root.\n"));
918 SYSLOG ((LOG_ERR, "can't setuid(0)"));
920 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing id to root",
931 update_group (&grent, &sgent);
933 update_group (&grent);
938 nscd_flush_cache ("group");