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 extern int errmsg();
440Sstevel@tonic-gate extern gid_t findnextgid();
450Sstevel@tonic-gate extern int valid_gid(), add_group();
460Sstevel@tonic-gate
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate * groupadd [-g gid [-o]] group
490Sstevel@tonic-gate *
500Sstevel@tonic-gate * This command adds new groups to the system. Arguments are:
510Sstevel@tonic-gate *
520Sstevel@tonic-gate * gid - a gid_t less than MAXUID
530Sstevel@tonic-gate * group - a string of printable characters excluding colon(:) and less
540Sstevel@tonic-gate * than MAXGLEN characters long.
550Sstevel@tonic-gate */
560Sstevel@tonic-gate
570Sstevel@tonic-gate char *cmdname = "groupadd";
580Sstevel@tonic-gate
59*108Sbasabi int
main(int argc,char * argv[])60*108Sbasabi main(int argc, char *argv[])
610Sstevel@tonic-gate {
620Sstevel@tonic-gate int ch; /* return from getopt */
630Sstevel@tonic-gate gid_t gid; /* group id */
640Sstevel@tonic-gate int oflag = 0; /* flags */
65*108Sbasabi int rc;
660Sstevel@tonic-gate char *gidstr = NULL; /* gid from command line */
670Sstevel@tonic-gate char *grpname; /* group name from command line */
680Sstevel@tonic-gate int warning;
690Sstevel@tonic-gate
700Sstevel@tonic-gate while ((ch = getopt(argc, argv, "g:o")) != EOF)
710Sstevel@tonic-gate switch (ch) {
720Sstevel@tonic-gate case 'g':
730Sstevel@tonic-gate gidstr = optarg;
740Sstevel@tonic-gate break;
750Sstevel@tonic-gate case 'o':
760Sstevel@tonic-gate oflag++;
770Sstevel@tonic-gate break;
780Sstevel@tonic-gate case '?':
790Sstevel@tonic-gate errmsg(M_AUSAGE);
800Sstevel@tonic-gate exit(EX_SYNTAX);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate
830Sstevel@tonic-gate if ((oflag && !gidstr) || optind != argc - 1) {
840Sstevel@tonic-gate errmsg(M_AUSAGE);
850Sstevel@tonic-gate exit(EX_SYNTAX);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate
880Sstevel@tonic-gate grpname = argv[optind];
890Sstevel@tonic-gate
900Sstevel@tonic-gate switch (valid_gname(grpname, NULL, &warning)) {
910Sstevel@tonic-gate case INVALID:
920Sstevel@tonic-gate errmsg(M_GRP_INVALID, grpname);
930Sstevel@tonic-gate exit(EX_BADARG);
940Sstevel@tonic-gate /*NOTREACHED*/
950Sstevel@tonic-gate case NOTUNIQUE:
960Sstevel@tonic-gate errmsg(M_GRP_USED, grpname);
970Sstevel@tonic-gate exit(EX_NAME_EXISTS);
980Sstevel@tonic-gate /*NOTREACHED*/
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate if (warning)
1010Sstevel@tonic-gate warningmsg(warning, grpname);
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate if (gidstr) {
1040Sstevel@tonic-gate /* Given a gid string - validate it */
1050Sstevel@tonic-gate char *ptr;
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate errno = 0;
1080Sstevel@tonic-gate gid = (gid_t)strtol(gidstr, &ptr, 10);
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate if (*ptr || errno == ERANGE) {
1110Sstevel@tonic-gate errmsg(M_GID_INVALID, gidstr);
1120Sstevel@tonic-gate exit(EX_BADARG);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate switch (valid_gid(gid, NULL)) {
1160Sstevel@tonic-gate case RESERVED:
1170Sstevel@tonic-gate errmsg(M_RESERVED, gid);
1180Sstevel@tonic-gate break;
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate case NOTUNIQUE:
1210Sstevel@tonic-gate if (!oflag) {
1220Sstevel@tonic-gate errmsg(M_GRP_USED, gidstr);
1230Sstevel@tonic-gate exit(EX_ID_EXISTS);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate break;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate case INVALID:
1280Sstevel@tonic-gate errmsg(M_GID_INVALID, gidstr);
1290Sstevel@tonic-gate exit(EX_BADARG);
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate case TOOBIG:
1320Sstevel@tonic-gate errmsg(M_TOOBIG, gid);
1330Sstevel@tonic-gate exit(EX_BADARG);
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate } else {
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate if ((gid = findnextgid()) < 0) {
1400Sstevel@tonic-gate errmsg(M_GID_INVALID, "default id");
1410Sstevel@tonic-gate exit(EX_ID_EXISTS);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate if ((rc = add_group(grpname, gid)) != EX_SUCCESS)
1470Sstevel@tonic-gate errmsg(M_UPDATE, "created");
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate return (rc);
1500Sstevel@tonic-gate }
151