xref: /illumos-gate/usr/src/cmd/oamuser/group/groupmod.c (revision 5146325846cd457c912118089437f46b9e7475f6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include <sys/types.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34 #include <limits.h>
35 #include <userdefs.h>
36 #include <users.h>
37 #include <errno.h>
38 #include "messages.h"
39 
40 /*
41  *  groupmod -g gid [-o] | -n name group
42  *
43  *	This command modifies groups on the system.  Arguments are:
44  *
45  *	gid - a gid_t less than UID_MAX
46  *	name - a string of printable characters excluding colon (:) and less
47  *		than MAXGLEN characters long.
48  *	group - a string of printable characters excluding colon(:) and less
49  *		than MAXGLEN characters long.
50  */
51 
52 extern int valid_gid(), mod_group();
53 extern void errmsg();
54 
55 char *cmdname = "groupmod";
56 
57 int
58 main(int argc, char *argv[])
59 {
60 	int ch;				/* return from getopt */
61 	gid_t gid;			/* group id */
62 	int oflag = 0;			/* flags */
63 	int valret;			/* return from valid_gid() */
64 	char *gidstr = NULL;		/* gid from command line */
65 	char *newname = NULL;		/* new group name with -n option */
66 	char *grpname;			/* group name from command line */
67 	int warning;
68 
69 	oflag = 0;	/* flags */
70 
71 	while ((ch = getopt(argc, argv, "g:on:")) != EOF)  {
72 		switch (ch) {
73 			case 'g':
74 				gidstr = optarg;
75 				break;
76 			case 'o':
77 				oflag++;
78 				break;
79 			case 'n':
80 				newname = optarg;
81 				break;
82 			case '?':
83 				errmsg(M_MUSAGE);
84 				exit(EX_SYNTAX);
85 		}
86 	}
87 
88 	if ((oflag && !gidstr) || optind != argc - 1) {
89 		errmsg(M_MUSAGE);
90 		exit(EX_SYNTAX);
91 	}
92 
93 	grpname = argv[optind];
94 
95 	if (gidstr) {
96 		/* convert gidstr to integer */
97 		char *ptr;
98 
99 		errno = 0;
100 		gid = (gid_t)strtol(gidstr, &ptr, 10);
101 
102 		if (*ptr || errno == ERANGE) {
103 			errmsg(M_GID_INVALID, gidstr);
104 			exit(EX_BADARG);
105 		}
106 
107 		switch (valid_gid(gid, NULL)) {
108 		case RESERVED:
109 			errmsg(M_RESERVED, gid);
110 			break;
111 
112 		case NOTUNIQUE:
113 			if (!oflag) {
114 				errmsg(M_GRP_USED, gidstr);
115 				exit(EX_ID_EXISTS);
116 			}
117 			break;
118 
119 		case INVALID:
120 			errmsg(M_GID_INVALID, gidstr);
121 			exit(EX_BADARG);
122 			/*NOTREACHED*/
123 
124 		case TOOBIG:
125 			errmsg(M_TOOBIG, gid);
126 			exit(EX_BADARG);
127 			/*NOTREACHED*/
128 
129 		}
130 
131 	} else gid = -1;
132 
133 	if (newname) {
134 		switch (valid_gname(newname, NULL, &warning)) {
135 		case INVALID:
136 			errmsg(M_GRP_INVALID, newname);
137 			exit(EX_BADARG);
138 		case NOTUNIQUE:
139 			errmsg(M_GRP_USED, newname);
140 			exit(EX_NAME_EXISTS);
141 		}
142 		if (warning)
143 			warningmsg(warning, newname);
144 	}
145 
146 	if ((valret = mod_group(grpname, gid, newname)) != EX_SUCCESS) {
147 		if (valret == EX_NAME_NOT_EXIST)
148 			errmsg(M_NO_GROUP, grpname);
149 		else
150 			errmsg(M_UPDATE, "modified");
151 	}
152 
153 	return (valret);
154 }
155