]> granicus.if.org Git - shadow/blob - src/groupmod.c
[svn-upgrade] Integrating new upstream version, shadow (20000826)
[shadow] / src / groupmod.c
1 /*
2  * Copyright 1991 - 1994, 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: groupmod.c,v 1.13 2000/08/26 18:27:18 marekm Exp $")
34
35 #include <sys/types.h>
36 #include <stdio.h>
37 #include <grp.h>
38 #include <ctype.h>
39 #include <fcntl.h>
40
41 #include "prototypes.h"
42 #include "chkname.h"
43 #include "defines.h"
44
45 #include "groupio.h"
46
47 #ifdef  SHADOWGRP
48 #include "sgroupio.h"
49
50 static int is_shadow_grp;
51 #endif
52
53 /*
54  * exit status values
55  */
56
57 #define E_SUCCESS       0       /* success */
58 #define E_USAGE         2       /* invalid command syntax */
59 #define E_BAD_ARG       3       /* invalid argument to option */
60 #define E_GID_IN_USE    4       /* gid already in use (and no -o) */
61 #define E_NOTFOUND      6       /* specified group doesn't exist */
62 #define E_NAME_IN_USE   9       /* group name already in use */
63 #define E_GRP_UPDATE    10      /* can't update group file */
64
65 static char     *group_name;
66 static char     *group_newname;
67 static gid_t    group_id;
68 static gid_t    group_newid;
69
70 static char     *Prog;
71
72 static int
73         oflg = 0, /* permit non-unique group ID to be specified with -g */
74         gflg = 0, /* new ID value for the group */
75         nflg = 0; /* a new name has been specified for the group */
76
77 #ifdef  NDBM
78 extern  int     gr_dbm_mode;
79 extern  int     sg_dbm_mode;
80 #endif
81
82 extern int optind;
83 extern char *optarg;
84
85 /* local function prototypes */
86 static void usage(void);
87 static void new_grent(struct group *);
88 #ifdef SHADOWGRP
89 static void new_sgent(struct sgrp *);
90 #endif
91 static void grp_update(void);
92 static void check_new_gid(void);
93 static void check_new_name(void);
94 static void process_flags(int, char **);
95 static void close_files(void);
96 static void open_files(void);
97
98 /*
99  * usage - display usage message and exit
100  */
101
102 static void
103 usage(void)
104 {
105         fprintf(stderr, _("usage: groupmod [-g gid [-o]] [-n name] group\n"));
106         exit(E_USAGE);
107 }
108
109 /*
110  * new_grent - updates the values in a group file entry
111  *
112  *      new_grent() takes all of the values that have been entered and
113  *      fills in a (struct group) with them.
114  */
115
116 static void
117 new_grent(struct group *grent)
118 {
119         if (nflg)
120                 grent->gr_name = xstrdup (group_newname);
121
122         if (gflg)
123                 grent->gr_gid = group_newid;
124 }
125
126 #ifdef  SHADOWGRP
127 /*
128  * new_sgent - updates the values in a shadow group file entry
129  *
130  *      new_sgent() takes all of the values that have been entered and
131  *      fills in a (struct sgrp) with them.
132  */
133
134 static void
135 new_sgent(struct sgrp *sgent)
136 {
137         if (nflg)
138                 sgent->sg_name = xstrdup (group_newname);
139 }
140 #endif  /* SHADOWGRP */
141
142 /*
143  * grp_update - update group file entries
144  *
145  *      grp_update() writes the new records to the group files.
146  */
147
148 static void
149 grp_update(void)
150 {
151         struct group grp;
152         const struct group *ogrp;
153 #ifdef  SHADOWGRP
154         struct sgrp sgrp;
155         const struct sgrp *osgrp = NULL;
156 #endif  /* SHADOWGRP */
157
158         /*
159          * Get the current settings for this group.
160          */
161
162         ogrp = gr_locate(group_name);
163         if (!ogrp) {
164                 fprintf(stderr,
165                         _("%s: %s not found in /etc/group\n"),
166                         Prog, group_name);
167                 exit(E_GRP_UPDATE);
168         }
169         grp = *ogrp;
170         new_grent (&grp);
171 #ifdef  SHADOWGRP
172         if (is_shadow_grp && (osgrp = sgr_locate(group_name))) {
173                 sgrp = *osgrp;
174                 new_sgent (&sgrp);
175         }
176 #endif  /* SHADOWGRP */
177
178         /*
179          * Write out the new group file entry.
180          */
181
182         if (!gr_update(&grp)) {
183                 fprintf(stderr, _("%s: error adding new group entry\n"), Prog);
184                 exit(E_GRP_UPDATE);
185         }
186         if (nflg && !gr_remove(group_name)) {
187                 fprintf(stderr, _("%s: error removing group entry\n"), Prog);
188                 exit(E_GRP_UPDATE);
189         }
190 #ifdef  NDBM
191
192         /*
193          * Update the DBM group file with the new entry as well.
194          */
195
196         if (gr_dbm_present()) {
197                 if (!gr_dbm_update(&grp)) {
198                         fprintf(stderr,
199                                 _("%s: cannot add new dbm group entry\n"),
200                                 Prog);
201                         exit(E_GRP_UPDATE);
202                 }
203                 if (nflg && (ogrp = getgrnam(group_name)) &&
204                                 !gr_dbm_remove(ogrp)) {
205                         fprintf(stderr,
206                                 _("%s: error removing group dbm entry\n"),
207                                 Prog);
208                         exit(E_GRP_UPDATE);
209                 }
210                 endgrent ();
211         }
212 #endif  /* NDBM */
213
214 #ifdef  SHADOWGRP
215
216         /*
217          * Make sure there was a shadow entry to begin with.  Skip
218          * down to "out" if there wasn't.  Can't just return because
219          * there might be some syslogging to do.
220          */
221
222         if (! osgrp)
223                 goto out;
224
225         /*
226          * Write out the new shadow group entries as well.
227          */
228
229         if (!sgr_update(&sgrp)) {
230                 fprintf(stderr, _("%s: error adding new group entry\n"), Prog);
231                 exit(E_GRP_UPDATE);
232         }
233         if (nflg && !sgr_remove(group_name)) {
234                 fprintf(stderr, _("%s: error removing group entry\n"), Prog);
235                 exit(E_GRP_UPDATE);
236         }
237 #ifdef  NDBM
238
239         /*
240          * Update the DBM shadow group file with the new entry as well.
241          */
242
243         if (sg_dbm_present()) {
244                 if (!sg_dbm_update(&sgrp)) {
245                         fprintf(stderr,
246                                 _("%s: cannot add new dbm shadow group entry\n"),
247                                 Prog);
248                         exit(E_GRP_UPDATE);
249                 }
250                 if (nflg && ! sg_dbm_remove (group_name)) {
251                         fprintf (stderr,
252                                 _("%s: error removing shadow group dbm entry\n"),
253                                 Prog);
254                         exit(E_GRP_UPDATE);
255                 }
256                 endsgent ();
257         }
258 #endif  /* NDBM */
259 out:
260 #endif  /* SHADOWGRP */
261
262         if (nflg)
263                 SYSLOG((LOG_INFO, "change group `%s' to `%s'\n",
264                         group_name, group_newname));
265
266         if (gflg)
267                 SYSLOG((LOG_INFO, "change gid for `%s' to %d\n",
268                         nflg ? group_newname:group_name, group_newid));
269 }
270
271 /*
272  * check_new_gid - check the new GID value for uniqueness
273  *
274  *      check_new_gid() insures that the new GID value is unique.
275  */
276
277 static void
278 check_new_gid(void)
279 {
280         /*
281          * First, the easy stuff.  If the ID can be duplicated, or if
282          * the ID didn't really change, just return.  If the ID didn't
283          * change, turn off those flags.  No sense doing needless work.
284          */
285
286         if (group_id == group_newid) {
287                 gflg = 0;
288                 return;
289         }
290
291         if (oflg || ! getgrgid (group_newid))
292                 return;
293
294         /*
295          * Tell the user what they did wrong.
296          */
297
298         fprintf(stderr,
299                 _("%s: %ld is not a unique gid\n"),
300                 Prog, (long) group_newid);
301         exit(E_GID_IN_USE);
302 }
303
304 /*
305  * check_new_name - check the new name for uniqueness
306  *
307  *      check_new_name() insures that the new name does not exist
308  *      already.  You can't have the same name twice, period.
309  */
310
311 static void
312 check_new_name(void)
313 {
314         /*
315          * Make sure they are actually changing the name.
316          */
317
318         if (strcmp(group_name, group_newname) == 0) {
319                 nflg = 0;
320                 return;
321         }
322
323         if (check_group_name(group_newname)) {
324
325                 /*
326                  * If the entry is found, too bad.
327                  */
328
329                 if (getgrnam(group_newname)) {
330                         fprintf(stderr, _("%s: %s is not a unique name\n"),
331                                 Prog, group_newname);
332                         exit(E_NAME_IN_USE);
333                 }
334                 return;
335         }
336
337         /*
338          * All invalid group names land here.
339          */
340
341         fprintf(stderr, _("%s: %s is a not a valid group name\n"),
342                 Prog, group_newname);
343         exit(E_BAD_ARG);
344 }
345
346 /*
347  * process_flags - perform command line argument setting
348  *
349  *      process_flags() interprets the command line arguments and sets
350  *      the values that the user will be created with accordingly.  The
351  *      values are checked for sanity.
352  */
353
354 static void
355 process_flags(int argc, char **argv)
356 {
357         char    *end;
358         int     arg;
359
360         while ((arg = getopt (argc, argv, "og:n:")) != EOF) {
361                 switch (arg) {
362                         case 'g':
363                                 gflg++;
364                                 group_newid = strtol(optarg, &end, 10);
365                                 if (*end != '\0') {
366                                         fprintf(stderr,
367                                                 _("%s: invalid group %s\n"),
368                                                 Prog, optarg);
369                                         exit(E_BAD_ARG);
370                                 }
371                                 break;
372                         case 'n':
373                                 nflg++;
374                                 group_newname = optarg;
375                                 break;
376                         case 'o':
377                                 oflg++;
378                                 break;
379                         default:
380                                 usage ();
381                 }
382         }
383         if (oflg && !gflg)
384                 usage();
385
386         if (optind != argc - 1)
387                 usage();
388
389         group_name = argv[argc - 1];
390 }
391
392 /*
393  * close_files - close all of the files that were opened
394  *
395  *      close_files() closes all of the files that were opened for this
396  *      new group.  This causes any modified entries to be written out.
397  */
398
399 static void
400 close_files(void)
401 {
402         if (!gr_close()) {
403                 fprintf(stderr, _("%s: cannot rewrite group file\n"), Prog);
404                 exit(E_GRP_UPDATE);
405         }
406         gr_unlock();
407 #ifdef  SHADOWGRP
408         if (is_shadow_grp && !sgr_close()) {
409                 fprintf(stderr, _("%s: cannot rewrite shadow group file\n"),
410                         Prog);
411                 exit(E_GRP_UPDATE);
412         }
413         if (is_shadow_grp)
414                 sgr_unlock ();
415 #endif  /* SHADOWGRP */
416 }
417
418 /*
419  * open_files - lock and open the group files
420  *
421  *      open_files() opens the two group files.
422  */
423
424 static void
425 open_files(void)
426 {
427         if (!gr_lock()) {
428                 fprintf(stderr, _("%s: unable to lock group file\n"), Prog);
429                 exit(E_GRP_UPDATE);
430         }
431         if (!gr_open(O_RDWR)) {
432                 fprintf(stderr, _("%s: unable to open group file\n"), Prog);
433                 exit(E_GRP_UPDATE);
434         }
435 #ifdef  SHADOWGRP
436         if (is_shadow_grp && !sgr_lock()) {
437                 fprintf(stderr, _("%s: unable to lock shadow group file\n"),
438                         Prog);
439                 exit(E_GRP_UPDATE);
440         }
441         if (is_shadow_grp && !sgr_open(O_RDWR)) {
442                 fprintf(stderr, _("%s: unable to open shadow group file\n"),
443                         Prog);
444                 exit(E_GRP_UPDATE);
445         }
446 #endif  /* SHADOWGRP */
447 }
448
449 /*
450  * main - groupmod command
451  *
452  *      The syntax of the groupmod command is
453  *      
454  *      groupmod [ -g gid [ -o ]] [ -n name ] group
455  *
456  *      The flags are
457  *              -g - specify a new group ID value
458  *              -o - permit the group ID value to be non-unique
459  *              -n - specify a new group name
460  */
461
462 int
463 main(int argc, char **argv)
464 {
465         struct  group   *grp;
466
467         /*
468          * Get my name so that I can use it to report errors.
469          */
470
471         Prog = Basename(argv[0]);
472
473         setlocale(LC_ALL, "");
474         bindtextdomain(PACKAGE, LOCALEDIR);
475         textdomain(PACKAGE);
476
477         openlog(Prog, LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_AUTH);
478
479 #ifdef SHADOWGRP
480         is_shadow_grp = sgr_file_present();
481 #endif
482
483         /*
484          * The open routines for the DBM files don't use read-write
485          * as the mode, so we have to clue them in.
486          */
487
488 #ifdef  NDBM
489         gr_dbm_mode = O_RDWR;
490 #ifdef  SHADOWGRP
491         sg_dbm_mode = O_RDWR;
492 #endif  /* SHADOWGRP */
493 #endif  /* NDBM */
494         process_flags (argc, argv);
495
496         /*
497          * Start with a quick check to see if the group exists.
498          */
499
500         if (!(grp = getgrnam(group_name))) {
501                 fprintf(stderr, _("%s: group %s does not exist\n"),
502                         Prog, group_name);
503                 exit(E_NOTFOUND);
504         } else
505                 group_id = grp->gr_gid;
506
507 #ifdef  USE_NIS
508
509         /*
510          * Now make sure it isn't an NIS group.
511          */
512
513         if (__isgrNIS ()) {
514                 char    *nis_domain;
515                 char    *nis_master;
516
517                 fprintf(stderr, _("%s: group %s is a NIS group\n"),
518                         Prog, group_name);
519
520                 if (! yp_get_default_domain (&nis_domain) &&
521                                 ! yp_master (nis_domain, "group.byname",
522                                 &nis_master)) {
523                         fprintf(stderr, _("%s: %s is the NIS master\n"),
524                                 Prog, nis_master);
525                 }
526                 exit(E_NOTFOUND);
527         }
528 #endif
529
530         if (gflg)
531                 check_new_gid ();
532
533         if (nflg)
534                 check_new_name ();
535
536         /*
537          * Do the hard stuff - open the files, create the group entries,
538          * then close and update the files.
539          */
540
541         open_files ();
542
543         grp_update ();
544
545         close_files ();
546         exit(E_SUCCESS);
547         /*NOTREACHED*/
548 }