]> granicus.if.org Git - shadow/blob - src/gpasswd.c
* src/gpasswd.c: log_gpasswd_success_gshadow only exists ifdef
[shadow] / src / gpasswd.c
1 /*
2  * Copyright (c) 1990 - 1994, Julianne Frances Haugh
3  * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4  * Copyright (c) 2001 - 2006, Tomasz Kłoczko
5  * Copyright (c) 2007 - 2009, Nicolas François
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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.
31  */
32
33 #include <config.h>
34
35 #ident "$Id$"
36
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <getopt.h>
40 #include <grp.h>
41 #include <pwd.h>
42 #include <signal.h>
43 #include <stdio.h>
44 #include <sys/types.h>
45 #include "defines.h"
46 #include "exitcodes.h"
47 #include "groupio.h"
48 #include "nscd.h"
49 #include "prototypes.h"
50 #ifdef SHADOWGRP
51 #include "sgroupio.h"
52 #endif
53 /*
54  * Global variables
55  */
56 /* The name of this command, as it is invoked */
57 char *Prog;
58
59 #ifdef SHADOWGRP
60 /* Indicate if shadow groups are enabled on the system
61  * (/etc/gshadow present) */
62 static bool is_shadowgrp;
63 #endif
64
65 /* Flags set by options */
66 static bool aflg = false;
67 static bool Aflg = false;
68 static bool dflg = false;
69 static bool Mflg = false;
70 static bool rflg = false;
71 static bool Rflg = false;
72 /* The name of the group that is being affected */
73 static char *group = NULL;
74 /* The name of the user being added (-a) or removed (-d) from group */
75 static char *user = NULL;
76 /* The new list of members set with -M */
77 static char *members = NULL;
78 #ifdef SHADOWGRP
79 /* The new list of group administrators set with -A */
80 static char *admins = NULL;
81 #endif
82 /* The name of the caller */
83 static char *myname = NULL;
84 /* The UID of the caller */
85 static uid_t bywho;
86 /* Indicate if gpasswd was called by root */
87 #define amroot  (0 == bywho)
88
89 /* The number of retries for th user to provide and repeat a new password */
90 #ifndef RETRIES
91 #define RETRIES 3
92 #endif
93
94 /* local function prototypes */
95 static void usage (void);
96 static RETSIGTYPE catch_signals (int killed);
97 static bool is_valid_user_list (const char *users);
98 static void process_flags (int argc, char **argv);
99 static void check_flags (int argc, int opt_index);
100 static void open_files (void);
101 static void close_files (void);
102 #ifdef SHADOWGRP
103 static void get_group (struct group *gr, struct sgrp *sg);
104 static void check_perms (const struct group *gr, const struct sgrp *sg);
105 static void update_group (struct group *gr, struct sgrp *sg);
106 static void change_passwd (struct group *gr, struct sgrp *sg);
107 #else
108 static void get_group (struct group *gr);
109 static void check_perms (const struct group *gr);
110 static void update_group (struct group *gr);
111 static void change_passwd (struct group *gr);
112 #endif
113 static void log_gpasswd_failure (const char *suffix);
114 static void log_gpasswd_failure_system (void *unused(arg));
115 static void log_gpasswd_failure_group (void *unused(arg));
116 #ifdef SHADOWGRP
117 static void log_gpasswd_failure_gshadow (void *unused(arg));
118 #endif
119 static void log_gpasswd_success (const char *suffix);
120 static void log_gpasswd_success_system (void *unused(arg));
121 static void log_gpasswd_success_group (void *unused(arg));
122 #ifdef SHADOWGRP
123 static void log_gpasswd_success_gshadow (void *unused(arg));
124 #endif
125
126 /*
127  * usage - display usage message
128  */
129 static void usage (void)
130 {
131         fprintf (stderr,
132                  _("Usage: %s [option] GROUP\n"
133                    "\n"
134                    "Options:\n"
135                    "  -a, --add USER                add USER to GROUP\n"
136                    "  -d, --delete USER             remove USER from GROUP\n"
137                    "  -r, --remove-password         remove the GROUP's password\n"
138                    "  -R, --restrict                restrict access to GROUP to its members\n"
139                    "  -M, --members USER,...        set the list of members of GROUP\n"
140                    "%s\n"
141                    "\n"),
142                  Prog,
143 #ifdef SHADOWGRP
144                  _("  -A, --administrators ADMIN,...\n"
145                    "                                set the list of administrators for GROUP\n"
146                    "Except for the -A and -M options, the options cannot be combined.\n")
147 #else
148                  _("The options cannot be combined.\n")
149 #endif
150                 );
151         exit (E_USAGE);
152 }
153
154 /*
155  * catch_signals - set or reset termio modes.
156  *
157  *      catch_signals() is called before processing begins. signal() is then
158  *      called with catch_signals() as the signal handler. If signal later
159  *      calls catch_signals() with a signal number, the terminal modes are
160  *      then reset.
161  */
162 static RETSIGTYPE catch_signals (int killed)
163 {
164         static TERMIO sgtty;
165
166         if (0 != killed) {
167                 STTY (0, &sgtty);
168         } else {
169                 GTTY (0, &sgtty);
170         }
171
172         if (0 != killed) {
173                 (void) putchar ('\n');
174                 (void) fflush (stdout);
175                 exit (killed);
176         }
177 }
178
179 /*
180  * is_valid_user_list - check a comma-separated list of user names for validity
181  *
182  *      is_valid_user_list scans a comma-separated list of user names and
183  *      checks that each listed name exists is the user database.
184  *
185  *      It returns true if the list of users is valid.
186  */
187 static bool is_valid_user_list (const char *users)
188 {
189         char *username, *end;
190         bool is_valid = true;
191         char *tmpusers = xstrdup (users);
192
193         for (username = tmpusers;
194              (NULL != username) && ('\0' != *username);
195              username = end) {
196                 end = strchr (username, ',');
197                 if (NULL != end) {
198                         *end = '\0';
199                         end++;
200                 }
201
202                 /*
203                  * This user must exist.
204                  */
205
206                 /* local, no need for xgetpwnam */
207                 if (getpwnam (username) == NULL) {
208                         fprintf (stderr, _("%s: user '%s' does not exist\n"),
209                                  Prog, username);
210                         is_valid = false;
211                 }
212         }
213
214         free (tmpusers);
215
216         return is_valid;
217 }
218
219 static void failure (void)
220 {
221         fprintf (stderr, _("%s: Permission denied.\n"), Prog);
222         log_gpasswd_failure (": Permission denied");
223         exit (1);
224 }
225
226 /*
227  * process_flags - process the command line options and arguments
228  */
229 static void process_flags (int argc, char **argv)
230 {
231         int flag;
232         int option_index = 0;
233         static struct option long_options[] = {
234                 {"add", required_argument, NULL, 'a'},
235                 {"delete", required_argument, NULL, 'd'},
236                 {"remove-password", no_argument, NULL, 'r'},
237                 {"restrict", no_argument, NULL, 'R'},
238                 {"administrators", required_argument, NULL, 'A'},
239                 {"members", required_argument, NULL, 'M'},
240                 {NULL, 0, NULL, '\0'}
241                 };
242
243         while ((flag = getopt_long (argc, argv, "a:A:d:gM:rR", long_options, &option_index)) != -1) {
244                 switch (flag) {
245                 case 'a':       /* add a user */
246                         aflg = true;
247                         user = optarg;
248                         /* local, no need for xgetpwnam */
249                         if (getpwnam (user) == NULL) {
250                                 fprintf (stderr,
251                                          _("%s: user '%s' does not exist\n"), Prog,
252                                          user);
253                                 exit (1);
254                         }
255                         break;
256 #ifdef SHADOWGRP
257                 case 'A':       /* set the list of administrators */
258                         if (!is_shadowgrp) {
259                                 fprintf (stderr,
260                                          _("%s: shadow group passwords required for -A\n"),
261                                          Prog);
262                                 exit (2);
263                         }
264                         admins = optarg;
265                         if (!is_valid_user_list (admins)) {
266                                 exit (1);
267                         }
268                         Aflg = true;
269                         break;
270 #endif                          /* SHADOWGRP */
271                 case 'd':       /* delete a user */
272                         dflg = true;
273                         user = optarg;
274                         break;
275                 case 'g':       /* no-op from normal password */
276                         break;
277                 case 'M':       /* set the list of members */
278                         members = optarg;
279                         if (!is_valid_user_list (members)) {
280                                 exit (1);
281                         }
282                         Mflg = true;
283                         break;
284                 case 'r':       /* remove group password */
285                         rflg = true;
286                         break;
287                 case 'R':       /* restrict group password */
288                         Rflg = true;
289                         break;
290                 default:
291                         usage ();
292                 }
293         }
294
295         /* Get the name of the group that is being affected. */
296         group = argv[optind];
297
298         check_flags (argc, optind);
299 }
300
301 /*
302  * check_flags - check the validity of options
303  */
304 static void check_flags (int argc, int opt_index)
305 {
306         int exclusive = 0;
307         /*
308          * Make sure exclusive flags are exclusive
309          */
310         if (aflg) {
311                 exclusive++;
312         }
313         if (dflg) {
314                 exclusive++;
315         }
316         if (rflg) {
317                 exclusive++;
318         }
319         if (Rflg) {
320                 exclusive++;
321         }
322         if (Aflg || Mflg) {
323                 exclusive++;
324         }
325         if (exclusive > 1) {
326                 usage ();
327         }
328
329         /*
330          * Make sure one (and only one) group was provided
331          */
332         if ((argc != (opt_index+1)) || (NULL == group)) {
333                 usage ();
334         }
335 }
336
337 /*
338  * open_files - lock and open the group databases
339  *
340  *      It will call exit in case of error.
341  */
342 static void open_files (void)
343 {
344         if (gr_lock () == 0) {
345                 fprintf (stderr,
346                          _("%s: cannot lock %s; try again later.\n"),
347                          Prog, gr_dbname ());
348                 exit (1);
349         }
350         add_cleanup (cleanup_unlock_group, NULL);
351
352 #ifdef SHADOWGRP
353         if (is_shadowgrp) {
354                 if (sgr_lock () == 0) {
355                         fprintf (stderr,
356                                  _("%s: cannot lock %s; try again later.\n"),
357                                  Prog, sgr_dbname ());
358                         exit (1);
359                 }
360                 add_cleanup (cleanup_unlock_gshadow, NULL);
361         }
362 #endif                          /* SHADOWGRP */
363
364         add_cleanup (log_gpasswd_failure_system, NULL);
365
366         if (gr_open (O_RDWR) == 0) {
367                 fprintf (stderr,
368                          _("%s: cannot open %s\n"),
369                          Prog, gr_dbname ());
370                 SYSLOG ((LOG_WARN, "cannot open %s", gr_dbname ()));
371                 exit (1);
372         }
373
374 #ifdef SHADOWGRP
375         if (is_shadowgrp) {
376                 if (sgr_open (O_RDWR) == 0) {
377                         fprintf (stderr,
378                                  _("%s: cannot open %s\n"),
379                                  Prog, sgr_dbname ());
380                         SYSLOG ((LOG_WARN, "cannot open %s", sgr_dbname ()));
381                         exit (1);
382                 }
383                 add_cleanup (log_gpasswd_failure_gshadow, NULL);
384         }
385 #endif                          /* SHADOWGRP */
386
387         add_cleanup (log_gpasswd_failure_group, NULL);
388         del_cleanup (log_gpasswd_failure_system);
389 }
390
391 static void log_gpasswd_failure (const char *suffix)
392 {
393 #ifdef WITH_AUDIT
394         char buf[1024];
395 #endif
396         if (aflg) {
397                 SYSLOG ((LOG_ERR,
398                          "%s failed to add user %s to group %s%s",
399                          myname, user, group, suffix));
400 #ifdef WITH_AUDIT
401                 snprintf (buf, 1023,
402                           "%s failed to add user %s to group %s%s",
403                           myname, user, group, suffix);
404                 buf[1023] = '\0';
405                 audit_logger (AUDIT_USER_ACCT, Prog,
406                               buf,
407                               group, AUDIT_NO_ID,
408                               SHADOW_AUDIT_FAILURE);
409 #endif
410         } else if (dflg) {
411                 SYSLOG ((LOG_ERR,
412                          "%s failed to remove user %s from group %s%s",
413                          myname, user, group, suffix));
414 #ifdef WITH_AUDIT
415                 snprintf (buf, 1023,
416                           "%s failed to remove user %s from group %s%s",
417                           myname, user, group, suffix);
418                 buf[1023] = '\0';
419                 audit_logger (AUDIT_USER_ACCT, Prog,
420                               buf,
421                               group, AUDIT_NO_ID,
422                               SHADOW_AUDIT_FAILURE);
423 #endif
424         } else if (rflg) {
425                 SYSLOG ((LOG_ERR,
426                          "%s failed to remove password of group %s%s",
427                          myname, group, suffix));
428 #ifdef WITH_AUDIT
429                 snprintf (buf, 1023,
430                           "%s failed to remove password of group %s%s",
431                           myname, group, suffix);
432                 buf[1023] = '\0';
433                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
434                               buf,
435                               group, AUDIT_NO_ID,
436                               SHADOW_AUDIT_FAILURE);
437 #endif
438         } else if (Rflg) {
439                 SYSLOG ((LOG_ERR,
440                          "%s failed to restrict access to group %s%s",
441                          myname, group, suffix));
442 #ifdef WITH_AUDIT
443                 snprintf (buf, 1023,
444                           "%s failed to restrict access to group %s%s",
445                           myname, group, suffix);
446                 buf[1023] = '\0';
447                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
448                               buf,
449                               group, AUDIT_NO_ID,
450                               SHADOW_AUDIT_FAILURE);
451 #endif
452         } else if (Aflg || Mflg) {
453 #ifdef SHADOWGRP
454                 if (Aflg) {
455                         SYSLOG ((LOG_ERR,
456                                  "%s failed to set the administrators of group %s to %s%s",
457                                  myname, group, admins, suffix));
458 #ifdef WITH_AUDIT
459                         snprintf (buf, 1023,
460                                   "%s failed to set the administrators of group %s to %s%s",
461                                   myname, group, admins, suffix);
462                         buf[1023] = '\0';
463                         audit_logger (AUDIT_USER_ACCT, Prog,
464                                       buf,
465                                       group, AUDIT_NO_ID,
466                                       SHADOW_AUDIT_FAILURE);
467 #endif
468                 }
469 #endif                          /* SHADOWGRP */
470                 if (Mflg) {
471                         SYSLOG ((LOG_ERR,
472                                  "%s failed to set the members of group %s to %s%s",
473                                  myname, group, members, suffix));
474 #ifdef WITH_AUDIT
475                         snprintf (buf, 1023,
476                                   "%s failed to set the members of group %s to %s%s",
477                                   myname, group, members, suffix);
478                         buf[1023] = '\0';
479                         audit_logger (AUDIT_USER_ACCT, Prog,
480                                       buf,
481                                       group, AUDIT_NO_ID,
482                                       SHADOW_AUDIT_FAILURE);
483 #endif
484                 }
485         } else {
486                 SYSLOG ((LOG_ERR,
487                          "%s failed to change password of group %s%s",
488                          myname, group, suffix));
489 #ifdef WITH_AUDIT
490                 snprintf (buf, 1023,
491                           "%s failed to change password of group %s%s",
492                           myname, group, suffix);
493                 buf[1023] = '\0';
494                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
495                               buf,
496                               group, AUDIT_NO_ID,
497                               SHADOW_AUDIT_FAILURE);
498 #endif
499         }
500 }
501
502 static void log_gpasswd_failure_system (void *unused(arg))
503 {
504         log_gpasswd_failure ("");
505 }
506
507 static void log_gpasswd_failure_group (void *unused(arg))
508 {
509         char buf[1024];
510         snprintf (buf, 1023, " in %s", gr_dbname ());
511         buf[1023] = '\0';
512         log_gpasswd_failure (buf);
513 }
514
515 #ifdef SHADOWGRP
516 static void log_gpasswd_failure_gshadow (void *unused(arg))
517 {
518         char buf[1024];
519         snprintf (buf, 1023, " in %s", sgr_dbname ());
520         buf[1023] = '\0';
521         log_gpasswd_failure (buf);
522 }
523 #endif                          /* SHADOWGRP */
524
525 static void log_gpasswd_success (const char *suffix)
526 {
527 #ifdef WITH_AUDIT
528         char buf[1024];
529 #endif
530         if (aflg) {
531                 SYSLOG ((LOG_INFO,
532                          "user %s added by %s to group %s%s",
533                          user, myname, group, suffix));
534 #ifdef WITH_AUDIT
535                 snprintf (buf, 1023,
536                           "user %s added by %s to group %s%s",
537                           user, myname, group, suffix);
538                 buf[1023] = '\0';
539                 audit_logger (AUDIT_USER_ACCT, Prog,
540                               buf,
541                               group, AUDIT_NO_ID,
542                               SHADOW_AUDIT_SUCCESS);
543 #endif
544         } else if (dflg) {
545                 SYSLOG ((LOG_INFO,
546                          "user %s removed by %s from group %s%s",
547                          user, myname, group, suffix));
548 #ifdef WITH_AUDIT
549                 snprintf (buf, 1023,
550                           "user %s removed by %s from group %s%s",
551                           user, myname, group, suffix);
552                 buf[1023] = '\0';
553                 audit_logger (AUDIT_USER_ACCT, Prog,
554                               buf,
555                               group, AUDIT_NO_ID,
556                               SHADOW_AUDIT_SUCCESS);
557 #endif
558         } else if (rflg) {
559                 SYSLOG ((LOG_INFO,
560                          "password of group %s removed by %s%s",
561                          group, myname, suffix));
562 #ifdef WITH_AUDIT
563                 snprintf (buf, 1023,
564                           "password of group %s removed by %s%s",
565                           group, myname, suffix);
566                 buf[1023] = '\0';
567                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
568                               buf,
569                               group, AUDIT_NO_ID,
570                               SHADOW_AUDIT_SUCCESS);
571 #endif
572         } else if (Rflg) {
573                 SYSLOG ((LOG_INFO,
574                          "access to group %s restricted by %s%s",
575                          group, myname, suffix));
576 #ifdef WITH_AUDIT
577                 snprintf (buf, 1023,
578                           "access to group %s restricted by %s%s",
579                           group, myname, suffix);
580                 buf[1023] = '\0';
581                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
582                               buf,
583                               group, AUDIT_NO_ID,
584                               SHADOW_AUDIT_SUCCESS);
585 #endif
586         } else if (Aflg || Mflg) {
587 #ifdef SHADOWGRP
588                 if (Aflg) {
589                         SYSLOG ((LOG_INFO,
590                                  "administrators of group %s set by %s to %s%s",
591                                  group, myname, admins, suffix));
592 #ifdef WITH_AUDIT
593                         snprintf (buf, 1023,
594                                   "administrators of group %s set by %s to %s%s",
595                                   group, myname, admins, suffix);
596                         buf[1023] = '\0';
597                         audit_logger (AUDIT_USER_ACCT, Prog,
598                                       buf,
599                                       group, AUDIT_NO_ID,
600                                       SHADOW_AUDIT_SUCCESS);
601 #endif
602                 }
603 #endif                          /* SHADOWGRP */
604                 if (Mflg) {
605                         SYSLOG ((LOG_INFO,
606                                  "members of group %s set by %s to %s%s",
607                                  group, myname, members, suffix));
608 #ifdef WITH_AUDIT
609                         snprintf (buf, 1023,
610                                   "members of group %s set by %s to %s%s",
611                                   group, myname, members, suffix);
612                         buf[1023] = '\0';
613                         audit_logger (AUDIT_USER_ACCT, Prog,
614                                       buf,
615                                       group, AUDIT_NO_ID,
616                                       SHADOW_AUDIT_SUCCESS);
617 #endif
618                 }
619         } else {
620                 SYSLOG ((LOG_INFO,
621                          "password of group %s changed by %s%s",
622                          group, myname, suffix));
623 #ifdef WITH_AUDIT
624                 snprintf (buf, 1023,
625                           "password of group %s changed by %s%s",
626                           group, myname, suffix);
627                 buf[1023] = '\0';
628                 audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
629                               buf,
630                               group, AUDIT_NO_ID,
631                               SHADOW_AUDIT_SUCCESS);
632 #endif
633         }
634 }
635
636 static void log_gpasswd_success_system (void *unused(arg))
637 {
638         log_gpasswd_success ("");
639 }
640
641 static void log_gpasswd_success_group (void *unused(arg))
642 {
643         char buf[1024];
644         snprintf (buf, 1023, " in %s", gr_dbname ());
645         buf[1023] = '\0';
646         log_gpasswd_success (buf);
647 }
648
649 #ifdef SHADOWGRP
650 static void log_gpasswd_success_gshadow (void *unused(arg))
651 {
652         char buf[1024];
653         snprintf (buf, 1023, " in %s", sgr_dbname ());
654         buf[1023] = '\0';
655         log_gpasswd_success (buf);
656 }
657 #endif                          /* SHADOWGRP */
658
659 /*
660  * close_files - close and unlock the group databases
661  *
662  *      This cause any changes in the databases to be committed.
663  *
664  *      It will call exit in case of error.
665  */
666 static void close_files (void)
667 {
668         if (gr_close () == 0) {
669                 fprintf (stderr,
670                          _("%s: failure while writing changes to %s\n"),
671                          Prog, gr_dbname ());
672                 exit (1);
673         }
674         add_cleanup (log_gpasswd_success_group, NULL);
675         del_cleanup (log_gpasswd_failure_group);
676
677         cleanup_unlock_group (NULL);
678         del_cleanup (cleanup_unlock_group);
679
680 #ifdef SHADOWGRP
681         if (is_shadowgrp) {
682                 if (sgr_close () == 0) {
683                         fprintf (stderr,
684                                  _("%s: failure while writing changes to %s\n"),
685                                  Prog, sgr_dbname ());
686                         exit (1);
687                 }
688                 add_cleanup (log_gpasswd_success_gshadow, NULL);
689                 del_cleanup (log_gpasswd_failure_gshadow);
690
691                 cleanup_unlock_gshadow (NULL);
692                 del_cleanup (cleanup_unlock_gshadow);
693         }
694 #endif                          /* SHADOWGRP */
695
696         log_gpasswd_success_system (NULL);
697         del_cleanup (log_gpasswd_success_group);
698 #ifdef SHADOWGRP
699         del_cleanup (log_gpasswd_success_gshadow);
700 #endif
701 }
702
703 /*
704  * check_perms - check if the user is allowed to change the password of
705  *               the specified group.
706  *
707  *      It only returns if the user is allowed.
708  */
709 #ifdef SHADOWGRP
710 static void check_perms (const struct group *gr, const struct sgrp *sg)
711 #else
712 static void check_perms (const struct group *gr)
713 #endif
714 {
715         /*
716          * Only root can use the -M and -A options.
717          */
718         if (!amroot && (Aflg || Mflg)) {
719                 failure ();
720         }
721
722 #ifdef SHADOWGRP
723         if (is_shadowgrp) {
724                 /*
725                  * The policy here for changing a group is that
726                  * 1) you must be root or
727                  * 2) you must be listed as an administrative member.
728                  * Administrative members can do anything to a group that
729                  * the root user can.
730                  */
731                 if (!amroot && !is_on_list (sg->sg_adm, myname)) {
732                         failure ();
733                 }
734         } else
735 #endif                          /* SHADOWGRP */
736         {
737 #ifdef FIRST_MEMBER_IS_ADMIN
738                 /*
739                  * The policy here for changing a group is that
740                  * 1) you must be root or
741                  * 2) you must be the first listed member of the group.
742                  * The first listed member of a group can do anything to
743                  * that group that the root user can. The rationale for
744                  * this hack is that the FIRST user is probably the most
745                  * important user in this entire group.
746                  *
747                  * This feature enabled by default could be a security
748                  * problem when installed on existing systems where the
749                  * first group member might be just a normal user.
750                  * --marekm
751                  */
752                 if (!amroot) {
753                         if (gr->gr_mem[0] == (char *) 0) {
754                                 failure ();
755                         }
756
757                         if (strcmp (gr->gr_mem[0], myname) != 0) {
758                                 failure ();
759                         }
760                 }
761 #else                           /* ! FIRST_MEMBER_IS_ADMIN */
762                 if (!amroot) {
763                         failure ();
764                 }
765 #endif
766         }
767 }
768
769 /*
770  * update_group - Update the group information in the databases
771  */
772 #ifdef SHADOWGRP
773 static void update_group (struct group *gr, struct sgrp *sg)
774 #else
775 static void update_group (struct group *gr)
776 #endif
777 {
778         if (gr_update (gr) == 0) {
779                 fprintf (stderr,
780                          _("%s: failed to prepare the new %s entry '%s'\n"),
781                          Prog, gr_dbname (), gr->gr_name);
782                 exit (1);
783         }
784 #ifdef SHADOWGRP
785         if (is_shadowgrp && (sgr_update (sg) == 0)) {
786                 fprintf (stderr,
787                          _("%s: failed to prepare the new %s entry '%s'\n"),
788                          Prog, sgr_dbname (), sg->sg_name);
789                 exit (1);
790         }
791 #endif                          /* SHADOWGRP */
792 }
793
794 /*
795  * get_group - get the current information for the group
796  *
797  *      The information are copied in group structure(s) so that they can be
798  *      modified later.
799  *
800  *      Note: If !is_shadowgrp, *sg will not be initialized.
801  */
802 #ifdef SHADOWGRP
803 static void get_group (struct group *gr, struct sgrp *sg)
804 #else
805 static void get_group (struct group *gr)
806 #endif
807 {
808         struct group const*tmpgr = NULL;
809 #ifdef SHADOWGRP
810         struct sgrp const*tmpsg = NULL;
811 #endif
812
813         if (gr_open (O_RDONLY) == 0) {
814                 fprintf (stderr, _("%s: cannot open %s\n"), Prog, gr_dbname ());
815                 SYSLOG ((LOG_WARN, "cannot open %s", gr_dbname ()));
816                 exit (1);
817         }
818
819         tmpgr = gr_locate (group);
820         if (NULL == tmpgr) {
821                 fprintf (stderr,
822                          _("%s: group '%s' does not exist in %s\n"),
823                          Prog, group, gr_dbname ());
824                 exit (1);
825         }
826
827         *gr = *tmpgr;
828         gr->gr_name = xstrdup (tmpgr->gr_name);
829         gr->gr_passwd = xstrdup (tmpgr->gr_passwd);
830         gr->gr_mem = dup_list (tmpgr->gr_mem);
831
832         if (gr_close () == 0) {
833                 fprintf (stderr,
834                          _("%s: failure while closing read-only %s\n"),
835                          Prog, gr_dbname ());
836                 SYSLOG ((LOG_ERR,
837                          "failure while closing read-only %s",
838                          gr_dbname ()));
839                 exit (1);
840         }
841
842 #ifdef SHADOWGRP
843         if (is_shadowgrp) {
844                 if (sgr_open (O_RDONLY) == 0) {
845                         fprintf (stderr,
846                                  _("%s: cannot open %s\n"),
847                                  Prog, sgr_dbname ());
848                         SYSLOG ((LOG_WARN, "cannot open %s", sgr_dbname ()));
849                         exit (1);
850                 }
851                 tmpsg = sgr_locate (group);
852                 if (NULL != tmpsg) {
853                         *sg = *tmpsg;
854                         sg->sg_name = xstrdup (tmpsg->sg_name);
855                         sg->sg_passwd = xstrdup (tmpsg->sg_passwd);
856
857                         sg->sg_mem = dup_list (tmpsg->sg_mem);
858                         sg->sg_adm = dup_list (tmpsg->sg_adm);
859                 } else {
860                         sg->sg_name = xstrdup (group);
861                         sg->sg_passwd = gr->gr_passwd;
862                         gr->gr_passwd = SHADOW_PASSWD_STRING;   /* XXX warning: const */
863
864                         sg->sg_mem = dup_list (gr->gr_mem);
865
866                         sg->sg_adm = (char **) xmalloc (sizeof (char *) * 2);
867 #ifdef FIRST_MEMBER_IS_ADMIN
868                         if (sg->sg_mem[0]) {
869                                 sg->sg_adm[0] = xstrdup (sg->sg_mem[0]);
870                                 sg->sg_adm[1] = NULL;
871                         } else
872 #endif
873                         {
874                                 sg->sg_adm[0] = NULL;
875                         }
876
877                 }
878                 if (sgr_close () == 0) {
879                         fprintf (stderr,
880                                  _("%s: failure while closing read-only %s\n"),
881                                  Prog, sgr_dbname ());
882                         SYSLOG ((LOG_ERR,
883                                  "failure while closing read-only %s",
884                                  sgr_dbname ()));
885                         exit (1);
886                 }
887         }
888 #endif                          /* SHADOWGRP */
889 }
890
891 /*
892  * change_passwd - change the group's password
893  *
894  *      Get the new password from the user and update the password in the
895  *      group's structure.
896  *
897  *      It will call exit in case of error.
898  */
899 #ifdef SHADOWGRP
900 static void change_passwd (struct group *gr, struct sgrp *sg)
901 #else
902 static void change_passwd (struct group *gr)
903 #endif
904 {
905         char *cp;
906         static char pass[BUFSIZ];
907         int retries;
908
909         /*
910          * A new password is to be entered and it must be encrypted, etc.
911          * The password will be prompted for twice, and both entries must be
912          * identical. There is no need to validate the old password since
913          * the invoker is either the group owner, or root.
914          */
915         printf (_("Changing the password for group %s\n"), group);
916
917         for (retries = 0; retries < RETRIES; retries++) {
918                 cp = getpass (_("New Password: "));
919                 if (NULL == cp) {
920                         exit (1);
921                 }
922
923                 STRFCPY (pass, cp);
924                 strzero (cp);
925                 cp = getpass (_("Re-enter new password: "));
926                 if (NULL == cp) {
927                         exit (1);
928                 }
929
930                 if (strcmp (pass, cp) == 0) {
931                         strzero (cp);
932                         break;
933                 }
934
935                 strzero (cp);
936                 memzero (pass, sizeof pass);
937
938                 if (retries + 1 < RETRIES) {
939                         puts (_("They don't match; try again"));
940                 }
941         }
942
943         if (retries == RETRIES) {
944                 fprintf (stderr, _("%s: Try again later\n"), Prog);
945                 exit (1);
946         }
947
948         cp = pw_encrypt (pass, crypt_make_salt (NULL, NULL));
949         memzero (pass, sizeof pass);
950 #ifdef SHADOWGRP
951         if (is_shadowgrp) {
952                 sg->sg_passwd = cp;
953         } else
954 #endif
955         {
956                 gr->gr_passwd = cp;
957         }
958 }
959
960 /*
961  * gpasswd - administer the /etc/group file
962  *
963  *      -a user         add user to the named group
964  *      -d user         remove user from the named group
965  *      -r              remove password from the named group
966  *      -R              restrict access to the named group
967  *      -A user,...     make list of users the administrative users
968  *      -M user,...     make list of users the group members
969  */
970 int main (int argc, char **argv)
971 {
972         struct group grent;
973 #ifdef SHADOWGRP
974         struct sgrp sgent;
975 #endif
976         struct passwd *pw = NULL;
977
978 #ifdef WITH_AUDIT
979         audit_help_open ();
980 #endif
981
982         sanitize_env ();
983         (void) setlocale (LC_ALL, "");
984         (void) bindtextdomain (PACKAGE, LOCALEDIR);
985         (void) textdomain (PACKAGE);
986
987         /*
988          * Make a note of whether or not this command was invoked by root.
989          * This will be used to bypass certain checks later on. Also, set
990          * the real user ID to match the effective user ID. This will
991          * prevent the invoker from issuing signals which would interfere
992          * with this command.
993          */
994         bywho = getuid ();
995         Prog = Basename (argv[0]);
996
997         OPENLOG ("gpasswd");
998         setbuf (stdout, NULL);
999         setbuf (stderr, NULL);
1000
1001 #ifdef SHADOWGRP
1002         is_shadowgrp = sgr_file_present ();
1003 #endif
1004
1005         /*
1006          * Determine the name of the user that invoked this command. This
1007          * is really hit or miss because there are so many ways that command
1008          * can be executed and so many ways to trip up the routines that
1009          * report the user name.
1010          */
1011         pw = get_my_pwent ();
1012         if (NULL == pw) {
1013                 fprintf (stderr, _("%s: Cannot determine your user name.\n"),
1014                          Prog);
1015                 SYSLOG ((LOG_WARN,
1016                          "Cannot determine the user name of the caller (UID %lu)",
1017                          (unsigned long) getuid ()));
1018                 exit (1);
1019         }
1020         myname = xstrdup (pw->pw_name);
1021
1022         /*
1023          * Register an exit function to warn for any inconsistency that we
1024          * could create.
1025          */
1026         if (atexit (do_cleanups) != 0) {
1027                 fprintf(stderr, "%s: cannot set exit function\n", Prog);
1028                 exit(EXIT_FAILURE);
1029         }
1030
1031         /* Parse the options */
1032         process_flags (argc, argv);
1033
1034         /*
1035          * Replicate the group so it can be modified later on.
1036          */
1037 #ifdef SHADOWGRP
1038         get_group (&grent, &sgent);
1039 #else
1040         get_group (&grent);
1041 #endif
1042
1043         /*
1044          * Check if the user is allowed to change the password of this group.
1045          */
1046 #ifdef SHADOWGRP
1047         check_perms (&grent, &sgent);
1048 #else
1049         check_perms (&grent);
1050 #endif
1051
1052         /*
1053          * Removing a password is straight forward. Just set the password
1054          * field to a "".
1055          */
1056         if (rflg) {
1057                 grent.gr_passwd = "";   /* XXX warning: const */
1058 #ifdef SHADOWGRP
1059                 sgent.sg_passwd = "";   /* XXX warning: const */
1060 #endif
1061                 goto output;
1062         } else if (Rflg) {
1063                 /*
1064                  * Same thing for restricting the group. Set the password
1065                  * field to "!".
1066                  */
1067                 grent.gr_passwd = "!";  /* XXX warning: const */
1068 #ifdef SHADOWGRP
1069                 sgent.sg_passwd = "!";  /* XXX warning: const */
1070 #endif
1071                 goto output;
1072         }
1073
1074         /*
1075          * Adding a member to a member list is pretty straightforward as
1076          * well. Call the appropriate routine and split.
1077          */
1078         if (aflg) {
1079                 printf (_("Adding user %s to group %s\n"), user, group);
1080                 grent.gr_mem = add_list (grent.gr_mem, user);
1081 #ifdef SHADOWGRP
1082                 if (is_shadowgrp) {
1083                         sgent.sg_mem = add_list (sgent.sg_mem, user);
1084                 }
1085 #endif
1086                 goto output;
1087         }
1088
1089         /*
1090          * Removing a member from the member list is the same deal as adding
1091          * one, except the routine is different.
1092          */
1093         if (dflg) {
1094                 bool removed = false;
1095
1096                 printf (_("Removing user %s from group %s\n"), user, group);
1097
1098                 if (is_on_list (grent.gr_mem, user)) {
1099                         removed = true;
1100                         grent.gr_mem = del_list (grent.gr_mem, user);
1101                 }
1102 #ifdef SHADOWGRP
1103                 if (is_shadowgrp) {
1104                         if (is_on_list (sgent.sg_mem, user)) {
1105                                 removed = true;
1106                                 sgent.sg_mem = del_list (sgent.sg_mem, user);
1107                         }
1108                 }
1109 #endif
1110                 if (!removed) {
1111                         fprintf (stderr,
1112                                  _("%s: user '%s' is not a member of '%s'\n"),
1113                                  Prog, user, group);
1114                         exit (1);
1115                 }
1116                 goto output;
1117         }
1118 #ifdef SHADOWGRP
1119         /*
1120          * Replacing the entire list of administrators is simple. Check the
1121          * list to make sure everyone is a real user. Then slap the new list
1122          * in place.
1123          */
1124         if (Aflg) {
1125                 sgent.sg_adm = comma_to_list (admins);
1126                 if (!Mflg) {
1127                         goto output;
1128                 }
1129         }
1130 #endif                          /* SHADOWGRP */
1131
1132         /*
1133          * Replacing the entire list of members is simple. Check the list to
1134          * make sure everyone is a real user. Then slap the new list in
1135          * place.
1136          */
1137         if (Mflg) {
1138 #ifdef SHADOWGRP
1139                 sgent.sg_mem = comma_to_list (members);
1140 #endif
1141                 grent.gr_mem = comma_to_list (members);
1142                 goto output;
1143         }
1144
1145         /*
1146          * If the password is being changed, the input and output must both
1147          * be a tty. The typical keyboard signals are caught so the termio
1148          * modes can be restored.
1149          */
1150         if ((isatty (0) == 0) || (isatty (1) == 0)) {
1151                 fprintf (stderr, _("%s: Not a tty\n"), Prog);
1152                 exit (1);
1153         }
1154
1155         catch_signals (0);      /* save tty modes */
1156
1157         (void) signal (SIGHUP, catch_signals);
1158         (void) signal (SIGINT, catch_signals);
1159         (void) signal (SIGQUIT, catch_signals);
1160         (void) signal (SIGTERM, catch_signals);
1161 #ifdef SIGTSTP
1162         (void) signal (SIGTSTP, catch_signals);
1163 #endif
1164
1165         /* Prompt for the new password */
1166 #ifdef SHADOWGRP
1167         change_passwd (&grent, &sgent);
1168 #else
1169         change_passwd (&grent);
1170 #endif
1171
1172         /*
1173          * This is the common arrival point to output the new group file.
1174          * The freshly crafted entry is in allocated space. The group file
1175          * will be locked and opened for writing. The new entry will be
1176          * output, etc.
1177          */
1178       output:
1179         if (setuid (0) != 0) {
1180                 fputs (_("Cannot change ID to root.\n"), stderr);
1181                 SYSLOG ((LOG_ERR, "can't setuid(0)"));
1182                 closelog ();
1183                 exit (1);
1184         }
1185         pwd_init ();
1186
1187         open_files ();
1188
1189 #ifdef SHADOWGRP
1190         update_group (&grent, &sgent);
1191 #else
1192         update_group (&grent);
1193 #endif
1194
1195         close_files ();
1196
1197         nscd_flush_cache ("group");
1198
1199         exit (E_SUCCESS);
1200 }
1201