]> granicus.if.org Git - shadow/blob - src/groupadd.c
[svn-upgrade] Integrating new upstream version, shadow (4.0.11)
[shadow] / src / groupadd.c
1 /*
2  * Copyright 1991 - 1993, Julianne Frances Haugh
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 #include <config.h>
31
32 #include "rcsid.h"
33 RCSID (PKG_VER "$Id: groupadd.c,v 1.37 2005/07/18 13:17:45 kloczek Exp $")
34 #include <sys/types.h>
35 #include <stdio.h>
36 #include <grp.h>
37 #include <ctype.h>
38 #include <fcntl.h>
39 #ifdef USE_PAM
40 #include <security/pam_appl.h>
41 #include <security/pam_misc.h>
42 #include <pwd.h>
43 #endif                          /* USE_PAM */
44 #include "defines.h"
45 #include "prototypes.h"
46 #include "chkname.h"
47 #include "getdef.h"
48 #include "groupio.h"
49 #include "nscd.h"
50 #ifdef  SHADOWGRP
51 #include "sgroupio.h"
52 static int is_shadow_grp;
53 #endif
54
55 /*
56  * exit status values
57  */
58
59 #define E_SUCCESS       0       /* success */
60 #define E_USAGE         2       /* invalid command syntax */
61 #define E_BAD_ARG       3       /* invalid argument to option */
62 #define E_GID_IN_USE    4       /* gid not unique (when -o not used) */
63 #define E_NAME_IN_USE   9       /* group name not unique */
64 #define E_GRP_UPDATE    10      /* can't update group file */
65
66 static char *group_name;
67 static gid_t group_id;
68 static char *empty_list = NULL;
69
70 static char *Prog;
71
72 static int oflg = 0;            /* permit non-unique group ID to be specified with -g */
73 static int gflg = 0;            /* ID value for the new group */
74 static int fflg = 0;            /* if group already exists, do nothing and exit(0) */
75
76 /* local function prototypes */
77 static void usage (void);
78 static void new_grent (struct group *);
79
80 #ifdef SHADOWGRP
81 static void new_sgent (struct sgrp *);
82 #endif
83 static void grp_update (void);
84 static void find_new_gid (void);
85 static void check_new_name (void);
86 static void process_flags (int, char **);
87 static void close_files (void);
88 static void open_files (void);
89 static void fail_exit (int);
90
91 /*
92  * usage - display usage message and exit
93  */
94
95 static void usage (void)
96 {
97         fprintf (stderr, _("Usage: groupadd [-g gid [-o]] [-f] group\n"));
98         exit (E_USAGE);
99 }
100
101 /*
102  * new_grent - initialize the values in a group file entry
103  *
104  *      new_grent() takes all of the values that have been entered and fills
105  *      in a (struct group) with them.
106  */
107
108 static void new_grent (struct group *grent)
109 {
110         memzero (grent, sizeof *grent);
111         grent->gr_name = group_name;
112         grent->gr_passwd = SHADOW_PASSWD_STRING;        /* XXX warning: const */
113         grent->gr_gid = group_id;
114         grent->gr_mem = &empty_list;
115 }
116
117 #ifdef  SHADOWGRP
118 /*
119  * new_sgent - initialize the values in a shadow group file entry
120  *
121  *      new_sgent() takes all of the values that have been entered and fills
122  *      in a (struct sgrp) with them.
123  */
124
125 static void new_sgent (struct sgrp *sgent)
126 {
127         memzero (sgent, sizeof *sgent);
128         sgent->sg_name = group_name;
129         sgent->sg_passwd = "!"; /* XXX warning: const */
130         sgent->sg_adm = &empty_list;
131         sgent->sg_mem = &empty_list;
132 }
133 #endif                          /* SHADOWGRP */
134
135 /*
136  * grp_update - add new group file entries
137  *
138  *      grp_update() writes the new records to the group files.
139  */
140
141 static void grp_update (void)
142 {
143         struct group grp;
144
145 #ifdef  SHADOWGRP
146         struct sgrp sgrp;
147 #endif                          /* SHADOWGRP */
148
149         /*
150          * Create the initial entries for this new group.
151          */
152         new_grent (&grp);
153 #ifdef  SHADOWGRP
154         new_sgent (&sgrp);
155 #endif                          /* SHADOWGRP */
156
157         /*
158          * Write out the new group file entry.
159          */
160         if (!gr_update (&grp)) {
161                 fprintf (stderr, _("%s: error adding new group entry\n"), Prog);
162                 fail_exit (E_GRP_UPDATE);
163         }
164 #ifdef  SHADOWGRP
165         /*
166          * Write out the new shadow group entries as well.
167          */
168         if (is_shadow_grp && !sgr_update (&sgrp)) {
169                 fprintf (stderr, _("%s: error adding new group entry\n"), Prog);
170                 fail_exit (E_GRP_UPDATE);
171         }
172 #endif                          /* SHADOWGRP */
173         SYSLOG ((LOG_INFO, "new group: name=%s, GID=%u",
174                  group_name, (unsigned int) group_id));
175 }
176
177 /*
178  * find_new_gid - find the next available GID
179  *
180  *      find_new_gid() locates the next highest unused GID in the group
181  *      file, or checks the given group ID against the existing ones for
182  *      uniqueness.
183  */
184
185 static void find_new_gid (void)
186 {
187         const struct group *grp;
188         gid_t gid_min, gid_max;
189
190         gid_min = getdef_unum ("GID_MIN", 100);
191         gid_max = getdef_unum ("GID_MAX", 60000);
192
193         /*
194          * Start with some GID value if the user didn't provide us with
195          * one already.
196          */
197
198         if (!gflg)
199                 group_id = gid_min;
200
201         /*
202          * Search the entire group file, either looking for this GID (if the
203          * user specified one with -g) or looking for the largest unused
204          * value.
205          */
206
207 #ifdef NO_GETGRENT
208         gr_rewind ();
209         while ((grp = gr_next ())) {
210 #else
211         setgrent ();
212         while ((grp = getgrent ())) {
213 #endif
214                 if (strcmp (group_name, grp->gr_name) == 0) {
215                         if (fflg) {
216                                 fail_exit (E_SUCCESS);
217                         }
218                         fprintf (stderr, _("%s: name %s is not unique\n"),
219                                  Prog, group_name);
220                         fail_exit (E_NAME_IN_USE);
221                 }
222                 if (gflg && group_id == grp->gr_gid) {
223                         if (fflg) {
224                                 /* turn off -g and search again */
225                                 gflg = 0;
226 #ifdef NO_GETGRENT
227                                 gr_rewind ();
228 #else
229                                 setgrent ();
230 #endif
231                                 continue;
232                         }
233                         fprintf (stderr, _("%s: GID %u is not unique\n"),
234                                  Prog, (unsigned int) group_id);
235                         fail_exit (E_GID_IN_USE);
236                 }
237                 if (!gflg && grp->gr_gid >= group_id) {
238                         if (grp->gr_gid > gid_max)
239                                 continue;
240                         group_id = grp->gr_gid + 1;
241                 }
242         }
243         if (!gflg && group_id == gid_max + 1) {
244                 for (group_id = gid_min; group_id < gid_max; group_id++) {
245 #ifdef NO_GETGRENT
246                         gr_rewind ();
247                         while ((grp = gr_next ())
248                                && grp->gr_gid != group_id);
249                         if (!grp)
250                                 break;
251 #else
252                         if (!getgrgid (group_id))
253                                 break;
254 #endif
255                 }
256                 if (group_id == gid_max) {
257                         fprintf (stderr, _("%s: can't get unique GID\n"), Prog);
258                         fail_exit (E_GID_IN_USE);
259                 }
260         }
261 }
262
263 /*
264  * check_new_name - check the new name for validity
265  *
266  *      check_new_name() insures that the new name doesn't contain any
267  *      illegal characters.
268  */
269
270 static void check_new_name (void)
271 {
272         if (check_group_name (group_name))
273                 return;
274
275         /*
276          * All invalid group names land here.
277          */
278
279         fprintf (stderr, _("%s: %s is not a valid group name\n"),
280                  Prog, group_name);
281
282         exit (E_BAD_ARG);
283 }
284
285 /*
286  * process_flags - perform command line argument setting
287  *
288  *      process_flags() interprets the command line arguments and sets the
289  *      values that the user will be created with accordingly. The values
290  *      are checked for sanity.
291  */
292
293 static void process_flags (int argc, char **argv)
294 {
295         char *cp;
296         int arg;
297
298         while ((arg = getopt (argc, argv, "fg:K:o")) != EOF) {
299                 switch (arg) {
300                 case 'f':
301                         /*
302                          * "force" - do nothing, just exit(0), if the
303                          * specified group already exists. With -g, if
304                          * specified gid already exists, choose another
305                          * (unique) gid (turn off -g). Based on the RedHat's
306                          * patch from shadow-utils-970616-9.
307                          */
308                         fflg++;
309                         break;
310                 case 'g':
311                         gflg++;
312                         if (!isdigit (optarg[0]))
313                                 usage ();
314
315                         group_id = strtoul (optarg, &cp, 10);
316                         if (*cp != '\0') {
317                                 fprintf (stderr,
318                                          _("%s: invalid group %s\n"), Prog,
319                                          optarg);
320                                 fail_exit (E_BAD_ARG);
321                         }
322                         break;
323                 case 'K':
324                         /*
325                          * override login.defs defaults (-K name=value)
326                          * example: -K GID_MIN=100 -K GID_MAX=499
327                          * note: -K GID_MIN=10,GID_MAX=499 doesn't work yet
328                          */
329                         cp = strchr (optarg, '=');
330                         if (!cp) {
331                                 fprintf (stderr,
332                                          _("%s: -K requires KEY=VALUE\n"),
333                                          Prog);
334                                 exit (E_BAD_ARG);
335                         }
336                         /* terminate name, point to value */
337                         *cp++ = '\0';
338                         if (putdef_str (optarg, cp) < 0)
339                                 exit (E_BAD_ARG);
340                         break;
341                 case 'o':
342                         oflg++;
343                         break;
344                 default:
345                         usage ();
346                 }
347         }
348
349         if (oflg && !gflg)
350                 usage ();
351
352         if (optind != argc - 1)
353                 usage ();
354
355         group_name = argv[argc - 1];
356         check_new_name ();
357 }
358
359 /*
360  * close_files - close all of the files that were opened
361  *
362  *      close_files() closes all of the files that were opened for this new
363  *      group. This causes any modified entries to be written out.
364  */
365
366 static void close_files (void)
367 {
368         if (!gr_close ()) {
369                 fprintf (stderr, _("%s: cannot rewrite group file\n"), Prog);
370                 fail_exit (E_GRP_UPDATE);
371         }
372         gr_unlock ();
373 #ifdef  SHADOWGRP
374         if (is_shadow_grp && !sgr_close ()) {
375                 fprintf (stderr,
376                          _("%s: cannot rewrite shadow group file\n"), Prog);
377                 fail_exit (E_GRP_UPDATE);
378         }
379         if (is_shadow_grp)
380                 sgr_unlock ();
381 #endif                          /* SHADOWGRP */
382 }
383
384 /*
385  * open_files - lock and open the group files
386  *
387  *      open_files() opens the two group files.
388  */
389
390 static void open_files (void)
391 {
392         if (!gr_lock ()) {
393                 fprintf (stderr, _("%s: unable to lock group file\n"), Prog);
394                 exit (E_GRP_UPDATE);
395         }
396         if (!gr_open (O_RDWR)) {
397                 fprintf (stderr, _("%s: unable to open group file\n"), Prog);
398                 fail_exit (E_GRP_UPDATE);
399         }
400 #ifdef  SHADOWGRP
401         if (is_shadow_grp && !sgr_lock ()) {
402                 fprintf (stderr,
403                          _("%s: unable to lock shadow group file\n"), Prog);
404                 fail_exit (E_GRP_UPDATE);
405         }
406         if (is_shadow_grp && !sgr_open (O_RDWR)) {
407                 fprintf (stderr,
408                          _("%s: unable to open shadow group file\n"), Prog);
409                 fail_exit (E_GRP_UPDATE);
410         }
411 #endif                          /* SHADOWGRP */
412 }
413
414 /*
415  * fail_exit - exit with an error code after unlocking files
416  */
417
418 static void fail_exit (int code)
419 {
420         (void) gr_unlock ();
421 #ifdef  SHADOWGRP
422         if (is_shadow_grp)
423                 sgr_unlock ();
424 #endif
425         exit (code);
426 }
427
428 #ifdef USE_PAM
429 static struct pam_conv conv = {
430         misc_conv,
431         NULL
432 };
433 #endif                          /* USE_PAM */
434
435 /*
436  * main - groupadd command
437  */
438
439 int main (int argc, char **argv)
440 {
441 #ifdef USE_PAM
442         pam_handle_t *pamh = NULL;
443         struct passwd *pampw;
444         int retval;
445 #endif
446
447         /*
448          * Get my name so that I can use it to report errors.
449          */
450
451         Prog = Basename (argv[0]);
452
453         setlocale (LC_ALL, "");
454         bindtextdomain (PACKAGE, LOCALEDIR);
455         textdomain (PACKAGE);
456
457         OPENLOG ("groupadd");
458
459         process_flags (argc, argv);
460
461 #ifdef USE_PAM
462         retval = PAM_SUCCESS;
463
464         pampw = getpwuid (getuid ());
465         if (pampw == NULL) {
466                 retval = PAM_USER_UNKNOWN;
467         }
468
469         if (retval == PAM_SUCCESS) {
470                 retval = pam_start ("groupadd", pampw->pw_name, &conv, &pamh);
471         }
472
473         if (retval == PAM_SUCCESS) {
474                 retval = pam_authenticate (pamh, 0);
475                 if (retval != PAM_SUCCESS) {
476                         pam_end (pamh, retval);
477                 }
478         }
479
480         if (retval == PAM_SUCCESS) {
481                 retval = pam_acct_mgmt (pamh, 0);
482                 if (retval != PAM_SUCCESS) {
483                         pam_end (pamh, retval);
484                 }
485         }
486
487         if (retval != PAM_SUCCESS) {
488                 fprintf (stderr, _("%s: PAM authentication failed\n"), Prog);
489                 exit (1);
490         }
491
492         OPENLOG ("groupadd");
493 #endif                          /* USE_PAM */
494
495 #ifdef SHADOWGRP
496         is_shadow_grp = sgr_file_present ();
497 #endif
498
499         /*
500          * Start with a quick check to see if the group exists.
501          */
502
503         if (getgrnam (group_name)) {
504                 if (fflg) {
505                         exit (E_SUCCESS);
506                 }
507                 fprintf (stderr, _("%s: group %s exists\n"), Prog, group_name);
508                 exit (E_NAME_IN_USE);
509         }
510
511         /*
512          * Do the hard stuff - open the files, create the group entries,
513          * then close and update the files.
514          */
515         open_files ();
516
517         if (!gflg || !oflg)
518                 find_new_gid ();
519
520         grp_update ();
521         nscd_flush_cache ("group");
522
523         close_files ();
524
525 #ifdef USE_PAM
526         if (retval == PAM_SUCCESS) {
527                 retval = pam_chauthtok (pamh, 0);
528                 if (retval != PAM_SUCCESS) {
529                         pam_end (pamh, retval);
530                 }
531         }
532
533         if (retval != PAM_SUCCESS) {
534                 fprintf (stderr, _("%s: PAM chauthtok failed\n"), Prog);
535                 exit (1);
536         }
537
538         if (retval == PAM_SUCCESS)
539                 pam_end (pamh, PAM_SUCCESS);
540 #endif                          /* USE_PAM */
541         exit (E_SUCCESS);
542         /* NOT REACHED */
543 }