xref: /titanic_41/usr/src/cmd/oamuser/group/groupadd.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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.4 */
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 extern int errmsg();
44 extern gid_t findnextgid();
45 extern int valid_gid(), add_group();
46 
47 /*
48  *  groupadd [-g gid [-o]] group
49  *
50  *	This command adds new groups to the system.  Arguments are:
51  *
52  *	gid - a gid_t less than MAXUID
53  *	group - a string of printable characters excluding colon(:) and less
54  *		than MAXGLEN characters long.
55  */
56 
57 char *cmdname = "groupadd";
58 
59 main(argc, argv)
60 int argc;
61 char *argv[];
62 {
63 	int ch;				/* return from getopt */
64 	gid_t gid;			/* group id */
65 	int oflag = 0;	/* flags */
66 	register rc;
67 	char *gidstr = NULL;	/* gid from command line */
68 	char *grpname;			/* group name from command line */
69 	int warning;
70 
71 	while ((ch = getopt(argc, argv, "g:o")) != EOF)
72 		switch (ch) {
73 			case 'g':
74 				gidstr = optarg;
75 				break;
76 			case 'o':
77 				oflag++;
78 				break;
79 			case '?':
80 				errmsg(M_AUSAGE);
81 				exit(EX_SYNTAX);
82 		}
83 
84 	if ((oflag && !gidstr) || optind != argc - 1) {
85 		errmsg(M_AUSAGE);
86 		exit(EX_SYNTAX);
87 	}
88 
89 	grpname = argv[optind];
90 
91 	switch (valid_gname(grpname, NULL, &warning)) {
92 	case INVALID:
93 		errmsg(M_GRP_INVALID, grpname);
94 		exit(EX_BADARG);
95 		/*NOTREACHED*/
96 	case NOTUNIQUE:
97 		errmsg(M_GRP_USED, grpname);
98 		exit(EX_NAME_EXISTS);
99 		/*NOTREACHED*/
100 	}
101 	if (warning)
102 		warningmsg(warning, grpname);
103 
104 	if (gidstr) {
105 		/* Given a gid string - validate it */
106 		char *ptr;
107 
108 		errno = 0;
109 		gid = (gid_t)strtol(gidstr, &ptr, 10);
110 
111 		if (*ptr || errno == ERANGE) {
112 			errmsg(M_GID_INVALID, gidstr);
113 			exit(EX_BADARG);
114 		}
115 
116 		switch (valid_gid(gid, NULL)) {
117 		case RESERVED:
118 			errmsg(M_RESERVED, gid);
119 			break;
120 
121 		case NOTUNIQUE:
122 			if (!oflag) {
123 				errmsg(M_GRP_USED, gidstr);
124 				exit(EX_ID_EXISTS);
125 			}
126 			break;
127 
128 		case INVALID:
129 			errmsg(M_GID_INVALID, gidstr);
130 			exit(EX_BADARG);
131 
132 		case TOOBIG:
133 			errmsg(M_TOOBIG, gid);
134 			exit(EX_BADARG);
135 
136 		}
137 
138 	} else {
139 
140 		if ((gid = findnextgid()) < 0) {
141 			errmsg(M_GID_INVALID, "default id");
142 			exit(EX_ID_EXISTS);
143 		}
144 
145 	}
146 
147 	if ((rc = add_group(grpname, gid)) != EX_SUCCESS)
148 		errmsg(M_UPDATE, "created");
149 
150 	return (rc);
151 }
152