2 * Copyright 1991 - 1994, Julianne Frances Haugh
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 RCSID(PKG_VER "$Id: groupmod.c,v 1.14 2000/09/02 18:40:44 marekm Exp $")
35 #include <sys/types.h>
41 #include "prototypes.h"
50 static int is_shadow_grp;
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 */
65 static char *group_name;
66 static char *group_newname;
67 static gid_t group_id;
68 static gid_t group_newid;
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 */
78 extern int gr_dbm_mode;
79 extern int sg_dbm_mode;
85 /* local function prototypes */
86 static void usage(void);
87 static void new_grent(struct group *);
89 static void new_sgent(struct sgrp *);
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);
99 * usage - display usage message and exit
105 fprintf(stderr, _("usage: groupmod [-g gid [-o]] [-n name] group\n"));
110 * new_grent - updates the values in a group file entry
112 * new_grent() takes all of the values that have been entered and
113 * fills in a (struct group) with them.
117 new_grent(struct group *grent)
120 grent->gr_name = xstrdup (group_newname);
123 grent->gr_gid = group_newid;
128 * new_sgent - updates the values in a shadow group file entry
130 * new_sgent() takes all of the values that have been entered and
131 * fills in a (struct sgrp) with them.
135 new_sgent(struct sgrp *sgent)
138 sgent->sg_name = xstrdup (group_newname);
140 #endif /* SHADOWGRP */
143 * grp_update - update group file entries
145 * grp_update() writes the new records to the group files.
152 const struct group *ogrp;
155 const struct sgrp *osgrp = NULL;
156 #endif /* SHADOWGRP */
159 * Get the current settings for this group.
162 ogrp = gr_locate(group_name);
165 _("%s: %s not found in /etc/group\n"),
172 if (is_shadow_grp && (osgrp = sgr_locate(group_name))) {
176 #endif /* SHADOWGRP */
179 * Write out the new group file entry.
182 if (!gr_update(&grp)) {
183 fprintf(stderr, _("%s: error adding new group entry\n"), Prog);
186 if (nflg && !gr_remove(group_name)) {
187 fprintf(stderr, _("%s: error removing group entry\n"), Prog);
193 * Update the DBM group file with the new entry as well.
196 if (gr_dbm_present()) {
197 if (!gr_dbm_update(&grp)) {
199 _("%s: cannot add new dbm group entry\n"),
203 if (nflg && (ogrp = getgrnam(group_name)) &&
204 !gr_dbm_remove(ogrp)) {
206 _("%s: error removing group dbm entry\n"),
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.
226 * Write out the new shadow group entries as well.
229 if (!sgr_update(&sgrp)) {
230 fprintf(stderr, _("%s: error adding new group entry\n"), Prog);
233 if (nflg && !sgr_remove(group_name)) {
234 fprintf(stderr, _("%s: error removing group entry\n"), Prog);
240 * Update the DBM shadow group file with the new entry as well.
243 if (sg_dbm_present()) {
244 if (!sg_dbm_update(&sgrp)) {
246 _("%s: cannot add new dbm shadow group entry\n"),
250 if (nflg && ! sg_dbm_remove (group_name)) {
252 _("%s: error removing shadow group dbm entry\n"),
260 #endif /* SHADOWGRP */
263 SYSLOG((LOG_INFO, "change group `%s' to `%s'\n",
264 group_name, group_newname));
267 SYSLOG((LOG_INFO, "change gid for `%s' to %d\n",
268 nflg ? group_newname:group_name, group_newid));
272 * check_new_gid - check the new GID value for uniqueness
274 * check_new_gid() insures that the new GID value is unique.
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.
286 if (group_id == group_newid) {
291 if (oflg || ! getgrgid (group_newid))
295 * Tell the user what they did wrong.
299 _("%s: %ld is not a unique gid\n"),
300 Prog, (long) group_newid);
305 * check_new_name - check the new name for uniqueness
307 * check_new_name() insures that the new name does not exist
308 * already. You can't have the same name twice, period.
315 * Make sure they are actually changing the name.
318 if (strcmp(group_name, group_newname) == 0) {
323 if (check_group_name(group_newname)) {
326 * If the entry is found, too bad.
329 if (getgrnam(group_newname)) {
330 fprintf(stderr, _("%s: %s is not a unique name\n"),
331 Prog, group_newname);
338 * All invalid group names land here.
341 fprintf(stderr, _("%s: %s is a not a valid group name\n"),
342 Prog, group_newname);
347 * process_flags - perform command line argument setting
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.
355 process_flags(int argc, char **argv)
360 while ((arg = getopt (argc, argv, "og:n:")) != EOF) {
364 group_newid = strtol(optarg, &end, 10);
367 _("%s: invalid group %s\n"),
374 group_newname = optarg;
386 if (optind != argc - 1)
389 group_name = argv[argc - 1];
393 * close_files - close all of the files that were opened
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.
403 fprintf(stderr, _("%s: cannot rewrite group file\n"), Prog);
408 if (is_shadow_grp && !sgr_close()) {
409 fprintf(stderr, _("%s: cannot rewrite shadow group file\n"),
415 #endif /* SHADOWGRP */
419 * open_files - lock and open the group files
421 * open_files() opens the two group files.
428 fprintf(stderr, _("%s: unable to lock group file\n"), Prog);
431 if (!gr_open(O_RDWR)) {
432 fprintf(stderr, _("%s: unable to open group file\n"), Prog);
436 if (is_shadow_grp && !sgr_lock()) {
437 fprintf(stderr, _("%s: unable to lock shadow group file\n"),
441 if (is_shadow_grp && !sgr_open(O_RDWR)) {
442 fprintf(stderr, _("%s: unable to open shadow group file\n"),
446 #endif /* SHADOWGRP */
450 * main - groupmod command
452 * The syntax of the groupmod command is
454 * groupmod [ -g gid [ -o ]] [ -n name ] group
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
463 main(int argc, char **argv)
468 * Get my name so that I can use it to report errors.
471 Prog = Basename(argv[0]);
473 setlocale(LC_ALL, "");
474 bindtextdomain(PACKAGE, LOCALEDIR);
480 is_shadow_grp = sgr_file_present();
484 * The open routines for the DBM files don't use read-write
485 * as the mode, so we have to clue them in.
489 gr_dbm_mode = O_RDWR;
491 sg_dbm_mode = O_RDWR;
492 #endif /* SHADOWGRP */
494 process_flags (argc, argv);
497 * Start with a quick check to see if the group exists.
500 if (!(grp = getgrnam(group_name))) {
501 fprintf(stderr, _("%s: group %s does not exist\n"),
505 group_id = grp->gr_gid;
510 * Now make sure it isn't an NIS group.
517 fprintf(stderr, _("%s: group %s is a NIS group\n"),
520 if (! yp_get_default_domain (&nis_domain) &&
521 ! yp_master (nis_domain, "group.byname",
523 fprintf(stderr, _("%s: %s is the NIS master\n"),
537 * Do the hard stuff - open the files, create the group entries,
538 * then close and update the files.