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
33 RCSID(PKG_VER "$Id: gpasswd.c,v 1.14 1999/06/07 16:40:45 marekm Exp $")
35 #include <sys/types.h>
43 #include "prototypes.h"
53 static int is_shadowgrp;
68 extern char *crypt_make_salt P_((void));
73 extern int sg_dbm_mode;
75 extern int gr_dbm_mode;
78 /* local function prototypes */
79 static void usage P_((void));
80 static RETSIGTYPE die P_((int));
81 static int check_list P_((const char *));
82 int main P_((int, char **));
85 * usage - display usage message
91 fprintf(stderr, _("usage: %s [-r|-R] group\n"), Prog);
92 fprintf(stderr, _(" %s [-a user] group\n"), Prog);
93 fprintf(stderr, _(" %s [-d user] group\n"), Prog);
95 fprintf(stderr, _(" %s [-A user,...] [-M user,...] group\n"),
98 fprintf(stderr, _(" %s [-M user,...] group\n"), Prog);
104 * die - set or reset termio modes.
106 * die() is called before processing begins. signal() is then
107 * called with die() as the signal handler. If signal later
108 * calls die() with a signal number, the terminal modes are
130 * check_list - check a comma-separated list of user names for validity
132 * check_list scans a comma-separated list of user names and checks
133 * that each listed name exists.
137 check_list(const char *users)
139 const char *start, *end;
144 for (start = users; start && *start; start = end) {
145 if ((end = strchr (start, ','))) {
152 if (len > sizeof(username) - 1)
153 len = sizeof(username) - 1;
154 strncpy(username, start, len);
155 username[len] = '\0';
158 * This user must exist.
161 if (!getpwnam(username)) {
162 fprintf(stderr, _("%s: unknown user %s\n"),
174 fprintf(stderr, _("Permission denied.\n"));
181 * gpasswd - administer the /etc/group file
183 * -a user add user to the named group
184 * -d user remove user from the named group
185 * -r remove password from the named group
186 * -R restrict access to the named group
187 * -A user,... make list of users the administrative users
188 * -M user,... make list of users the group members
192 main(int argc, char **argv)
198 struct group *gr = NULL;
200 static char pass[BUFSIZ];
202 struct sgrp *sg = NULL;
206 struct passwd *pw = NULL;
210 char *members = NULL;
213 setlocale(LC_ALL, "");
214 bindtextdomain(PACKAGE, LOCALEDIR);
218 * Make a note of whether or not this command was invoked
219 * by root. This will be used to bypass certain checks
220 * later on. Also, set the real user ID to match the
221 * effective user ID. This will prevent the invoker from
222 * issuing signals which would interfer with this command.
225 amroot = getuid () == 0;
228 sg_dbm_mode = O_RDWR;
230 gr_dbm_mode = O_RDWR;
233 Prog = Basename(argv[0]);
235 openlog("gpasswd", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_AUTH);
236 setbuf (stdout, (char *) 0);
237 setbuf (stderr, (char *) 0);
240 is_shadowgrp = sgr_file_present();
242 while ((flag = getopt (argc, argv, "a:d:grRA:M:")) != EOF) {
244 case 'a': /* add a user */
246 if (!getpwnam(user)) {
247 fprintf(stderr, _("%s: unknown user %s\n"),
259 _("%s: shadow group passwords required for -A\n"),
264 if (check_list(admins))
269 case 'd': /* delete a user */
273 case 'g': /* no-op from normal password */
279 if (check_list(members))
283 case 'r': /* remove group password */
286 case 'R': /* restrict group password */
295 * Make sure exclusive flags are exclusive
298 if (aflg + dflg + rflg + Rflg + (Aflg || Mflg) > 1)
302 * Determine the name of the user that invoked this command.
303 * This is really hit or miss because there are so many ways
304 * that command can be executed and so many ways to trip up
305 * the routines that report the user name.
310 fprintf(stderr, _("Who are you?\n"));
313 myname = xstrdup(pw->pw_name);
316 * Get the name of the group that is being affected. The group
317 * entry will be completely replicated so it may be modified
322 * XXX - should get the entry using gr_locate() and modify
323 * that, getgrnam() could give us a NIS group. --marekm
326 if (! (group = argv[optind]))
329 if (! (gr = getgrnam (group))) {
330 fprintf (stderr, _("unknown group: %s\n"), group);
334 grent.gr_name = xstrdup(gr->gr_name);
335 grent.gr_passwd = xstrdup(gr->gr_passwd);
337 grent.gr_mem = dup_list(gr->gr_mem);
339 if ((sg = getsgnam (group))) {
341 sgent.sg_name = xstrdup(sg->sg_name);
342 sgent.sg_passwd = xstrdup(sg->sg_passwd);
344 sgent.sg_mem = dup_list(sg->sg_mem);
345 sgent.sg_adm = dup_list(sg->sg_adm);
347 sgent.sg_name = xstrdup(group);
348 sgent.sg_passwd = grent.gr_passwd;
349 grent.gr_passwd = "!"; /* XXX warning: const */
351 sgent.sg_mem = dup_list(grent.gr_mem);
353 sgent.sg_adm = (char **) xmalloc(sizeof(char *) * 2);
354 #ifdef FIRST_MEMBER_IS_ADMIN
355 if (sgent.sg_mem[0]) {
356 sgent.sg_adm[0] = xstrdup(sgent.sg_mem[0]);
366 * The policy here for changing a group is that 1) you must be
367 * root or 2). you must be listed as an administrative member.
368 * Administrative members can do anything to a group that the
372 if (!amroot && !is_on_list(sgent.sg_adm, myname))
374 #else /* ! SHADOWGRP */
376 #ifdef FIRST_MEMBER_IS_ADMIN
378 * The policy here for changing a group is that 1) you must bes
379 * root or 2) you must be the first listed member of the group.
380 * The first listed member of a group can do anything to that
381 * group that the root user can. The rationale for this hack is
382 * that the FIRST user is probably the most important user in
387 if (grent.gr_mem[0] == (char *) 0)
390 if (strcmp(grent.gr_mem[0], myname) != 0)
395 * This feature enabled by default could be a security problem
396 * when installed on existing systems where the first group
397 * member might be just a normal user... --marekm
404 #endif /* SHADOWGRP */
407 * Removing a password is straight forward. Just set the
408 * password field to a "".
412 grent.gr_passwd = ""; /* XXX warning: const */
414 sgent.sg_passwd = ""; /* XXX warning: const */
416 SYSLOG((LOG_INFO, "remove password from group %s by %s\n", group, myname));
420 * Same thing for restricting the group. Set the password
424 grent.gr_passwd = "!"; /* XXX warning: const */
426 sgent.sg_passwd = "!"; /* XXX warning: const */
428 SYSLOG((LOG_INFO, "restrict access to group %s by %s\n", group, myname));
433 * Adding a member to a member list is pretty straightforward
434 * as well. Call the appropriate routine and split.
438 printf(_("Adding user %s to group %s\n"), user, group);
439 grent.gr_mem = add_list (grent.gr_mem, user);
441 sgent.sg_mem = add_list (sgent.sg_mem, user);
443 SYSLOG((LOG_INFO, "add member %s to group %s by %s\n", user, group, myname));
448 * Removing a member from the member list is the same deal
449 * as adding one, except the routine is different.
455 printf(_("Removing user %s from group %s\n"), user, group);
457 if (is_on_list(grent.gr_mem, user)) {
459 grent.gr_mem = del_list (grent.gr_mem, user);
462 if (is_on_list(sgent.sg_mem, user)) {
464 sgent.sg_mem = del_list (sgent.sg_mem, user);
468 fprintf(stderr, _("%s: unknown member %s\n"),
472 SYSLOG((LOG_INFO, "remove member %s from group %s by %s\n",
473 user, group, myname));
479 * Replacing the entire list of administators is simple. Check the
480 * list to make sure everyone is a real user. Then slap the new
485 SYSLOG((LOG_INFO, "set administrators of %s to %s\n",
487 sgent.sg_adm = comma_to_list(admins);
494 * Replacing the entire list of members is simple. Check the list
495 * to make sure everyone is a real user. Then slap the new list
500 SYSLOG((LOG_INFO,"set members of %s to %s\n",group,members));
502 sgent.sg_mem = comma_to_list(members);
504 grent.gr_mem = comma_to_list(members);
509 * If the password is being changed, the input and output must
510 * both be a tty. The typical keyboard signals are caught
511 * so the termio modes can be restored.
514 if (! isatty (0) || ! isatty (1)) {
515 fprintf(stderr, _("%s: Not a tty\n"), Prog);
519 die (0); /* save tty modes */
521 signal (SIGHUP, die);
522 signal (SIGINT, die);
523 signal (SIGQUIT, die);
524 signal (SIGTERM, die);
526 signal (SIGTSTP, die);
530 * A new password is to be entered and it must be encrypted,
531 * etc. The password will be prompted for twice, and both
532 * entries must be identical. There is no need to validate
533 * the old password since the invoker is either the group
537 printf(_("Changing the password for group %s\n"), group);
539 for (retries = 0; retries < RETRIES; retries++) {
540 if (! (cp = getpass(_("New Password:"))))
545 if (! (cp = getpass (_("Re-enter new password:"))))
548 if (strcmp(pass, cp) == 0) {
554 memzero(pass, sizeof pass);
556 if (retries + 1 < RETRIES)
557 puts(_("They don't match; try again"));
560 if (retries == RETRIES) {
561 fprintf(stderr, _("%s: Try again later\n"), Prog);
565 cp = pw_encrypt(pass, crypt_make_salt());
566 memzero(pass, sizeof pass);
568 sgent.sg_passwd = cp;
570 grent.gr_passwd = cp;
572 SYSLOG((LOG_INFO, "change the password for group %s by %s\n", group, myname));
575 * This is the common arrival point to output the new group
576 * file. The freshly crafted entry is in allocated space.
577 * The group file will be locked and opened for writing. The
578 * new entry will be output, etc.
583 fprintf(stderr, _("Cannot change ID to root.\n"));
584 SYSLOG((LOG_ERR, "can't setuid(0)"));
591 fprintf(stderr, _("%s: can't get lock\n"), Prog);
592 SYSLOG((LOG_WARN, "failed to get lock for /etc/group\n"));
596 if (is_shadowgrp && ! sgr_lock ()) {
597 fprintf(stderr, _("%s: can't get shadow lock\n"), Prog);
598 SYSLOG((LOG_WARN, "failed to get lock for /etc/gshadow\n"));
602 if (! gr_open (O_RDWR)) {
603 fprintf(stderr, _("%s: can't open file\n"), Prog);
604 SYSLOG((LOG_WARN, "cannot open /etc/group\n"));
608 if (is_shadowgrp && ! sgr_open (O_RDWR)) {
609 fprintf(stderr, _("%s: can't open shadow file\n"), Prog);
610 SYSLOG((LOG_WARN, "cannot open /etc/gshadow\n"));
614 if (! gr_update (&grent)) {
615 fprintf(stderr, _("%s: can't update entry\n"), Prog);
616 SYSLOG((LOG_WARN, "cannot update /etc/group\n"));
620 if (is_shadowgrp && ! sgr_update (&sgent)) {
621 fprintf(stderr, _("%s: can't update shadow entry\n"), Prog);
622 SYSLOG((LOG_WARN, "cannot update /etc/gshadow\n"));
627 fprintf(stderr, _("%s: can't re-write file\n"), Prog);
628 SYSLOG((LOG_WARN, "cannot re-write /etc/group\n"));
632 if (is_shadowgrp && ! sgr_close ()) {
633 fprintf(stderr, _("%s: can't re-write shadow file\n"), Prog);
634 SYSLOG((LOG_WARN, "cannot re-write /etc/gshadow\n"));
640 if (! gr_unlock ()) {
641 fprintf(stderr, _("%s: can't unlock file\n"), Prog);
645 if (gr_dbm_present() && ! gr_dbm_update (&grent)) {
646 fprintf(stderr, _("%s: can't update DBM files\n"), Prog);
647 SYSLOG((LOG_WARN, "cannot update /etc/group DBM files\n"));
652 if (is_shadowgrp && sg_dbm_present() && ! sg_dbm_update (&sgent)) {
653 fprintf(stderr, _("%s: can't update DBM shadow files\n"), Prog);
654 SYSLOG((LOG_WARN, "cannot update /etc/gshadow DBM files\n"));