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