xref: /onnv-gate/usr/src/cmd/oamuser/group/groupmod.c (revision 108:55ad989fc90b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*108Sbasabi  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
31*108Sbasabi #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate #include <ctype.h>
370Sstevel@tonic-gate #include <limits.h>
380Sstevel@tonic-gate #include <userdefs.h>
390Sstevel@tonic-gate #include <users.h>
400Sstevel@tonic-gate #include <errno.h>
410Sstevel@tonic-gate #include "messages.h"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate  *  groupmod -g gid [-o] | -n name group
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  *	This command modifies groups on the system.  Arguments are:
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  *	gid - a gid_t less than UID_MAX
490Sstevel@tonic-gate  *	name - a string of printable characters excluding colon (:) and less
500Sstevel@tonic-gate  *		than MAXGLEN characters long.
510Sstevel@tonic-gate  *	group - a string of printable characters excluding colon(:) and less
520Sstevel@tonic-gate  *		than MAXGLEN characters long.
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate extern int valid_gid(), mod_group();
560Sstevel@tonic-gate extern void errmsg();
570Sstevel@tonic-gate 
580Sstevel@tonic-gate char *cmdname = "groupmod";
590Sstevel@tonic-gate 
60*108Sbasabi int
main(int argc,char * argv[])61*108Sbasabi main(int argc, char *argv[])
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	int ch;				/* return from getopt */
640Sstevel@tonic-gate 	gid_t gid;			/* group id */
650Sstevel@tonic-gate 	int oflag = 0;			/* flags */
660Sstevel@tonic-gate 	int valret;			/* return from valid_gid() */
670Sstevel@tonic-gate 	char *gidstr = NULL;		/* gid from command line */
680Sstevel@tonic-gate 	char *newname = NULL;		/* new group name with -n option */
690Sstevel@tonic-gate 	char *grpname;			/* group name from command line */
700Sstevel@tonic-gate 	int warning;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	oflag = 0;	/* flags */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, "g:on:")) != EOF)  {
750Sstevel@tonic-gate 		switch (ch) {
760Sstevel@tonic-gate 			case 'g':
770Sstevel@tonic-gate 				gidstr = optarg;
780Sstevel@tonic-gate 				break;
790Sstevel@tonic-gate 			case 'o':
800Sstevel@tonic-gate 				oflag++;
810Sstevel@tonic-gate 				break;
820Sstevel@tonic-gate 			case 'n':
830Sstevel@tonic-gate 				newname = optarg;
840Sstevel@tonic-gate 				break;
850Sstevel@tonic-gate 			case '?':
860Sstevel@tonic-gate 				errmsg(M_MUSAGE);
870Sstevel@tonic-gate 				exit(EX_SYNTAX);
880Sstevel@tonic-gate 		}
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	if ((oflag && !gidstr) || optind != argc - 1) {
920Sstevel@tonic-gate 		errmsg(M_MUSAGE);
930Sstevel@tonic-gate 		exit(EX_SYNTAX);
940Sstevel@tonic-gate 	}
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	grpname = argv[optind];
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if (gidstr) {
990Sstevel@tonic-gate 		/* convert gidstr to integer */
1000Sstevel@tonic-gate 		char *ptr;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 		errno = 0;
1030Sstevel@tonic-gate 		gid = (gid_t)strtol(gidstr, &ptr, 10);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 		if (*ptr || errno == ERANGE) {
1060Sstevel@tonic-gate 			errmsg(M_GID_INVALID, gidstr);
1070Sstevel@tonic-gate 			exit(EX_BADARG);
1080Sstevel@tonic-gate 		}
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 		switch (valid_gid(gid, NULL)) {
1110Sstevel@tonic-gate 		case RESERVED:
1120Sstevel@tonic-gate 			errmsg(M_RESERVED, gid);
1130Sstevel@tonic-gate 			break;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 		case NOTUNIQUE:
1160Sstevel@tonic-gate 			if (!oflag) {
1170Sstevel@tonic-gate 				errmsg(M_GRP_USED, gidstr);
1180Sstevel@tonic-gate 				exit(EX_ID_EXISTS);
1190Sstevel@tonic-gate 			}
1200Sstevel@tonic-gate 			break;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 		case INVALID:
1230Sstevel@tonic-gate 			errmsg(M_GID_INVALID, gidstr);
1240Sstevel@tonic-gate 			exit(EX_BADARG);
1250Sstevel@tonic-gate 			/*NOTREACHED*/
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 		case TOOBIG:
1280Sstevel@tonic-gate 			errmsg(M_TOOBIG, gid);
1290Sstevel@tonic-gate 			exit(EX_BADARG);
1300Sstevel@tonic-gate 			/*NOTREACHED*/
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		}
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	} else gid = -1;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	if (newname) {
1370Sstevel@tonic-gate 		switch (valid_gname(newname, NULL, &warning)) {
1380Sstevel@tonic-gate 		case INVALID:
1390Sstevel@tonic-gate 			errmsg(M_GRP_INVALID, newname);
1400Sstevel@tonic-gate 			exit(EX_BADARG);
1410Sstevel@tonic-gate 		case NOTUNIQUE:
1420Sstevel@tonic-gate 			errmsg(M_GRP_USED, newname);
1430Sstevel@tonic-gate 			exit(EX_NAME_EXISTS);
1440Sstevel@tonic-gate 		}
1450Sstevel@tonic-gate 		if (warning)
1460Sstevel@tonic-gate 			warningmsg(warning, newname);
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	if ((valret = mod_group(grpname, gid, newname)) != EX_SUCCESS) {
1500Sstevel@tonic-gate 		if (valret == EX_NAME_NOT_EXIST)
1510Sstevel@tonic-gate 			errmsg(M_NO_GROUP, grpname);
1520Sstevel@tonic-gate 		else
1530Sstevel@tonic-gate 			errmsg(M_UPDATE, "modified");
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	return (valret);
1570Sstevel@tonic-gate }
158