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 * It returns 0 on success.
156 static int check_list (const char *users)
158 const char *start, *end;
163 for (start = users; start && *start; start = end) {
164 end = strchr (start, ',');
169 len = strlen (start);
172 if (len > sizeof (username) - 1) {
173 len = sizeof (username) - 1;
175 strncpy (username, start, len);
176 username[len] = '\0';
179 * This user must exist.
182 if (!getpwnam (username)) { /* local, no need for xgetpwnam */
183 fprintf (stderr, _("%s: unknown user %s\n"),
191 static void failure (void)
193 fprintf (stderr, _("%s: Permission denied.\n"), Prog);
198 * process_flags - process the command line options and arguments
200 static void process_flags (int argc, char **argv)
204 while ((flag = getopt (argc, argv, "a:A:d:gM:rR")) != EOF) {
206 case 'a': /* add a user */
208 /* local, no need for xgetpwnam */
209 if (!getpwnam (user)) {
211 _("%s: unknown user %s\n"), Prog,
214 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
215 "adding to group", user, -1, 0);
225 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
226 "Listing administrators", NULL,
234 ("%s: shadow group passwords required for -A\n"),
239 if (check_list (admins)) {
245 case 'd': /* delete a user */
249 case 'g': /* no-op from normal password */
254 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
255 "listing members", NULL, bywho,
261 if (check_list (members)) {
266 case 'r': /* remove group password */
269 case 'R': /* restrict group password */
277 /* Get the name of the group that is being affected. */
278 group = argv[optind];
280 check_flags (argc, optind);
284 * check_flags - check the validity of options
286 static void check_flags (int argc, int opt_index)
289 * Make sure exclusive flags are exclusive
291 if (aflg + dflg + rflg + Rflg + (Aflg || Mflg) > 1) {
296 * Make sure one (and only one) group was provided
298 if ((argc != (opt_index+1)) || (NULL == group)) {
304 * open_files - lock and open the group databases
306 * It will call exit in case of error.
308 static void open_files (void)
310 if (gr_lock () == 0) {
311 fprintf (stderr, _("%s: can't get lock\n"), Prog);
312 SYSLOG ((LOG_WARN, "failed to get lock for /etc/group"));
314 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
315 "locking /etc/group", group, -1, 0);
320 if (is_shadowgrp && (sgr_lock () == 0)) {
321 fprintf (stderr, _("%s: can't get shadow lock\n"), Prog);
322 SYSLOG ((LOG_WARN, "failed to get lock for /etc/gshadow"));
324 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
325 "locking /etc/gshadow", group, -1, 0);
330 if (gr_open (O_RDWR) == 0) {
331 fprintf (stderr, _("%s: can't open file\n"), Prog);
332 SYSLOG ((LOG_WARN, "cannot open /etc/group"));
334 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
335 "opening /etc/group", group, -1, 0);
340 if (is_shadowgrp && (sgr_open (O_RDWR) == 0)) {
341 fprintf (stderr, _("%s: can't open shadow file\n"), Prog);
342 SYSLOG ((LOG_WARN, "cannot open /etc/gshadow"));
344 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
345 "opening /etc/gshadow", group, -1, 0);
353 * close_files - close and unlock the group databases
355 * This cause any changes in the databases to be committed.
357 * It will call exit in case of error.
359 static void close_files (void)
361 if (gr_close () == 0) {
362 fprintf (stderr, _("%s: can't re-write file\n"), Prog);
363 SYSLOG ((LOG_WARN, "cannot re-write /etc/group"));
365 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
366 "rewriting /etc/group", group, -1, 0);
371 if (is_shadowgrp && (sgr_close () == 0)) {
372 fprintf (stderr, _("%s: can't re-write shadow file\n"), Prog);
373 SYSLOG ((LOG_WARN, "cannot re-write /etc/gshadow"));
375 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
376 "rewriting /etc/gshadow", group, -1, 0);
381 /* TODO: same logging as in open_files & for /etc/group */
385 if (gr_unlock () == 0) {
386 fprintf (stderr, _("%s: can't unlock file\n"), Prog);
388 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
389 "unlocking group file", group, -1, 0);
396 * check_perms - check if the user is allowed to change the password of
397 * the specified group.
399 * It only returns if the user is allowed.
402 static void check_perms (const struct sgrp *sg)
404 static void check_perms (const struct group *gr)
409 * The policy here for changing a group is that 1) you must be root
410 * or 2). you must be listed as an administrative member.
411 * Administrative members can do anything to a group that the root
414 if (!amroot && !is_on_list (sg->sg_adm, myname)) {
416 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
417 "modify group", group, -1, 0);
421 #else /* ! SHADOWGRP */
423 #ifdef FIRST_MEMBER_IS_ADMIN
425 * The policy here for changing a group is that 1) you must bes root
426 * or 2) you must be the first listed member of the group. The
427 * first listed member of a group can do anything to that group that
428 * the root user can. The rationale for this hack is that the FIRST
429 * user is probably the most important user in this entire group.
432 if (gr->gr_mem[0] == (char *) 0) {
434 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
435 "modifying group", group, -1, 0);
440 if (strcmp (gr->gr_mem[0], myname) != 0) {
442 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
443 "modifying group", myname, -1, 0);
450 * This feature enabled by default could be a security problem when
451 * installed on existing systems where the first group member might
452 * be just a normal user. --marekm
456 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
457 "modifying group", group, -1, 0);
462 #endif /* SHADOWGRP */
466 * update_group - Update the group information in the databases
469 static void update_group (struct group *gr, struct sgrp *sg)
471 static void update_group (struct group *gr)
474 if (!gr_update (gr)) {
475 fprintf (stderr, _("%s: can't update entry\n"), Prog);
476 SYSLOG ((LOG_WARN, "cannot update /etc/group"));
478 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
479 "updating /etc/group", group, -1, 0);
484 if (is_shadowgrp && !sgr_update (sg)) {
485 fprintf (stderr, _("%s: can't update shadow entry\n"), Prog);
486 SYSLOG ((LOG_WARN, "cannot update /etc/gshadow"));
488 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
489 "updating /etc/gshadow", group, -1, 0);
497 * get_group - get the current information for the group
499 * The information are copied in group structure(s) so that they can be
503 static void get_group (struct group *gr, struct sgrp *sg)
505 static void get_group (struct group *gr)
508 struct group const*tmpgr = NULL;
509 struct sgrp const*tmpsg = NULL;
511 if (!gr_open (O_RDONLY)) {
512 fprintf (stderr, _("%s: can't open file\n"), Prog);
513 SYSLOG ((LOG_WARN, "cannot open /etc/group"));
515 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
516 "opening /etc/group", group, -1, 0);
521 tmpgr = gr_locate (group);
523 fprintf (stderr, _("unknown group: %s\n"), group);
525 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
526 "group lookup", group, -1, 0);
532 gr->gr_name = xstrdup (tmpgr->gr_name);
533 gr->gr_passwd = xstrdup (tmpgr->gr_passwd);
534 gr->gr_mem = dup_list (tmpgr->gr_mem);
537 fprintf (stderr, _("%s: can't close file\n"), Prog);
538 SYSLOG ((LOG_WARN, "cannot close /etc/group"));
540 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
541 "closing /etc/group", group, -1, 0);
547 if (!sgr_open (O_RDONLY)) {
548 fprintf (stderr, _("%s: can't open shadow file\n"), Prog);
549 SYSLOG ((LOG_WARN, "cannot open /etc/gshadow"));
551 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
552 "opening /etc/gshadow", group, -1, 0);
556 tmpsg = sgr_locate (group);
559 sg->sg_name = xstrdup (tmpsg->sg_name);
560 sg->sg_passwd = xstrdup (tmpsg->sg_passwd);
562 sg->sg_mem = dup_list (tmpsg->sg_mem);
563 sg->sg_adm = dup_list (tmpsg->sg_adm);
565 sg->sg_name = xstrdup (group);
566 sg->sg_passwd = gr->gr_passwd;
567 gr->gr_passwd = "!"; /* XXX warning: const */
569 sg->sg_mem = dup_list (gr->gr_mem);
571 sg->sg_adm = (char **) xmalloc (sizeof (char *) * 2);
572 #ifdef FIRST_MEMBER_IS_ADMIN
574 sg->sg_adm[0] = xstrdup (sg->sg_mem[0]);
584 fprintf (stderr, _("%s: can't close shadow file\n"), Prog);
585 SYSLOG ((LOG_WARN, "cannot close /etc/gshadow"));
587 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
588 "closing /etc/gshadow", group, -1, 0);
592 #endif /* SHADOWGRP */
596 * change_passwd - change the group's password
598 * Get the new password from the user and update the password in the
601 * It will call exit in case of error.
604 static void change_passwd (struct group *gr, struct sgrp *sg)
606 static void change_passwd (struct group *gr)
610 static char pass[BUFSIZ];
614 * A new password is to be entered and it must be encrypted, etc.
615 * The password will be prompted for twice, and both entries must be
616 * identical. There is no need to validate the old password since
617 * the invoker is either the group owner, or root.
619 printf (_("Changing the password for group %s\n"), group);
621 for (retries = 0; retries < RETRIES; retries++) {
622 cp = getpass (_("New Password: "));
629 cp = getpass (_("Re-enter new password: "));
634 if (strcmp (pass, cp) == 0) {
640 memzero (pass, sizeof pass);
642 if (retries + 1 < RETRIES) {
643 puts (_("They don't match; try again"));
645 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
646 "changing password", group, -1, 0);
651 if (retries == RETRIES) {
652 fprintf (stderr, _("%s: Try again later\n"), Prog);
656 cp = pw_encrypt (pass, crypt_make_salt (NULL, NULL));
657 memzero (pass, sizeof pass);
667 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
668 "changing password", group, -1, 1);
670 SYSLOG ((LOG_INFO, "change the password for group %s by %s", group,
675 * gpasswd - administer the /etc/group file
677 * -a user add user to the named group
678 * -d user remove user from the named group
679 * -r remove password from the named group
680 * -R restrict access to the named group
681 * -A user,... make list of users the administrative users
682 * -M user,... make list of users the group members
684 int main (int argc, char **argv)
690 struct passwd *pw = NULL;
697 setlocale (LC_ALL, "");
698 bindtextdomain (PACKAGE, LOCALEDIR);
699 textdomain (PACKAGE);
702 * Make a note of whether or not this command was invoked by root.
703 * This will be used to bypass certain checks later on. Also, set
704 * the real user ID to match the effective user ID. This will
705 * prevent the invoker from issuing signals which would interfer
709 Prog = Basename (argv[0]);
712 setbuf (stdout, NULL);
713 setbuf (stderr, NULL);
716 is_shadowgrp = sgr_file_present ();
719 /* Parse the options */
720 process_flags (argc, argv);
723 * Determine the name of the user that invoked this command. This
724 * is really hit or miss because there are so many ways that command
725 * can be executed and so many ways to trip up the routines that
726 * report the user name.
729 pw = get_my_pwent ();
731 fprintf (stderr, _("Who are you?\n"));
733 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "user lookup", NULL,
738 myname = xstrdup (pw->pw_name);
741 * Replicate the group so it can be modified later on.
744 get_group (&grent, &sgent);
750 * Check if the user is allowed to change the password of this group.
753 check_perms (&sgent);
755 check_perms (&grent);
759 * Removing a password is straight forward. Just set the password
763 grent.gr_passwd = ""; /* XXX warning: const */
765 sgent.sg_passwd = ""; /* XXX warning: const */
768 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
769 "deleting group password", group, -1, 1);
771 SYSLOG ((LOG_INFO, "remove password from group %s by %s",
776 * Same thing for restricting the group. Set the password
779 grent.gr_passwd = "!"; /* XXX warning: const */
781 sgent.sg_passwd = "!"; /* XXX warning: const */
784 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
785 "restrict access to group", group, -1, 1);
787 SYSLOG ((LOG_INFO, "restrict access to group %s by %s",
793 * Adding a member to a member list is pretty straightforward as
794 * well. Call the appropriate routine and split.
797 printf (_("Adding user %s to group %s\n"), user, group);
798 grent.gr_mem = add_list (grent.gr_mem, user);
800 sgent.sg_mem = add_list (sgent.sg_mem, user);
803 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "adding group member",
806 SYSLOG ((LOG_INFO, "add member %s to group %s by %s", user,
812 * Removing a member from the member list is the same deal as adding
813 * one, except the routine is different.
818 printf (_("Removing user %s from group %s\n"), user, group);
820 if (is_on_list (grent.gr_mem, user)) {
822 grent.gr_mem = del_list (grent.gr_mem, user);
825 if (is_on_list (sgent.sg_mem, user)) {
827 sgent.sg_mem = del_list (sgent.sg_mem, user);
831 fprintf (stderr, _("%s: unknown member %s\n"),
834 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
835 "deleting member", user, -1, 0);
840 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "deleting member",
843 SYSLOG ((LOG_INFO, "remove member %s from group %s by %s",
844 user, group, myname));
849 * Replacing the entire list of administators is simple. Check the
850 * list to make sure everyone is a real user. Then slap the new list
855 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "setting group admin",
858 SYSLOG ((LOG_INFO, "set administrators of %s to %s",
860 sgent.sg_adm = comma_to_list (admins);
868 * Replacing the entire list of members is simple. Check the list to
869 * make sure everyone is a real user. Then slap the new list in
874 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
875 "setting group members", group, -1, 1);
877 SYSLOG ((LOG_INFO, "set members of %s to %s", group, members));
879 sgent.sg_mem = comma_to_list (members);
881 grent.gr_mem = comma_to_list (members);
886 * If the password is being changed, the input and output must both
887 * be a tty. The typical keyboard signals are caught so the termio
888 * modes can be restored.
890 if (!isatty (0) || !isatty (1)) {
891 fprintf (stderr, _("%s: Not a tty\n"), Prog);
893 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing password",
899 catch_signals (0); /* save tty modes */
901 signal (SIGHUP, catch_signals);
902 signal (SIGINT, catch_signals);
903 signal (SIGQUIT, catch_signals);
904 signal (SIGTERM, catch_signals);
906 signal (SIGTSTP, catch_signals);
909 /* Prompt for the new password */
911 change_passwd (&grent, &sgent);
913 change_passwd (&grent);
917 * This is the common arrival point to output the new group file.
918 * The freshly crafted entry is in allocated space. The group file
919 * will be locked and opened for writing. The new entry will be
924 fprintf (stderr, _("Cannot change ID to root.\n"));
925 SYSLOG ((LOG_ERR, "can't setuid(0)"));
927 audit_logger (AUDIT_USER_CHAUTHTOK, Prog, "changing id to root",
938 update_group (&grent, &sgent);
940 update_group (&grent);
945 nscd_flush_cache ("group");