xref: /titanic_44/usr/src/cmd/oamuser/group/groupmod.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 2004 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 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.3 */
32 
33 #include <sys/types.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <ctype.h>
37 #include <limits.h>
38 #include <userdefs.h>
39 #include <users.h>
40 #include <errno.h>
41 #include "messages.h"
42 
43 /*
44  *  groupmod -g gid [-o] | -n name group
45  *
46  *	This command modifies groups on the system.  Arguments are:
47  *
48  *	gid - a gid_t less than UID_MAX
49  *	name - a string of printable characters excluding colon (:) and less
50  *		than MAXGLEN characters long.
51  *	group - a string of printable characters excluding colon(:) and less
52  *		than MAXGLEN characters long.
53  */
54 
55 extern int valid_gid(), mod_group();
56 extern void errmsg();
57 
58 char *cmdname = "groupmod";
59 
60 main(argc, argv)
61 int argc;
62 char *argv[];
63 {
64 	int ch;				/* return from getopt */
65 	gid_t gid;			/* group id */
66 	int oflag = 0;			/* flags */
67 	int valret;			/* return from valid_gid() */
68 	char *gidstr = NULL;		/* gid from command line */
69 	char *newname = NULL;		/* new group name with -n option */
70 	char *grpname;			/* group name from command line */
71 	int warning;
72 
73 	oflag = 0;	/* flags */
74 
75 	while ((ch = getopt(argc, argv, "g:on:")) != EOF)  {
76 		switch (ch) {
77 			case 'g':
78 				gidstr = optarg;
79 				break;
80 			case 'o':
81 				oflag++;
82 				break;
83 			case 'n':
84 				newname = optarg;
85 				break;
86 			case '?':
87 				errmsg(M_MUSAGE);
88 				exit(EX_SYNTAX);
89 		}
90 	}
91 
92 	if ((oflag && !gidstr) || optind != argc - 1) {
93 		errmsg(M_MUSAGE);
94 		exit(EX_SYNTAX);
95 	}
96 
97 	grpname = argv[optind];
98 
99 	if (gidstr) {
100 		/* convert gidstr to integer */
101 		char *ptr;
102 
103 		errno = 0;
104 		gid = (gid_t)strtol(gidstr, &ptr, 10);
105 
106 		if (*ptr || errno == ERANGE) {
107 			errmsg(M_GID_INVALID, gidstr);
108 			exit(EX_BADARG);
109 		}
110 
111 		switch (valid_gid(gid, NULL)) {
112 		case RESERVED:
113 			errmsg(M_RESERVED, gid);
114 			break;
115 
116 		case NOTUNIQUE:
117 			if (!oflag) {
118 				errmsg(M_GRP_USED, gidstr);
119 				exit(EX_ID_EXISTS);
120 			}
121 			break;
122 
123 		case INVALID:
124 			errmsg(M_GID_INVALID, gidstr);
125 			exit(EX_BADARG);
126 			/*NOTREACHED*/
127 
128 		case TOOBIG:
129 			errmsg(M_TOOBIG, gid);
130 			exit(EX_BADARG);
131 			/*NOTREACHED*/
132 
133 		}
134 
135 	} else gid = -1;
136 
137 	if (newname) {
138 		switch (valid_gname(newname, NULL, &warning)) {
139 		case INVALID:
140 			errmsg(M_GRP_INVALID, newname);
141 			exit(EX_BADARG);
142 		case NOTUNIQUE:
143 			errmsg(M_GRP_USED, newname);
144 			exit(EX_NAME_EXISTS);
145 		}
146 		if (warning)
147 			warningmsg(warning, newname);
148 	}
149 
150 	if ((valret = mod_group(grpname, gid, newname)) != EX_SUCCESS) {
151 		if (valret == EX_NAME_NOT_EXIST)
152 			errmsg(M_NO_GROUP, grpname);
153 		else
154 			errmsg(M_UPDATE, "modified");
155 	}
156 
157 	return (valret);
158 }
159