xref: /illumos-gate/usr/src/lib/libadm/common/ckgid.c (revision 1da57d55)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1997, by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  * All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <string.h>
35*7c478bd9Sstevel@tonic-gate #include <ctype.h>
36*7c478bd9Sstevel@tonic-gate #include <grp.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
38*7c478bd9Sstevel@tonic-gate #include "libadm.h"
39*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
40*7c478bd9Sstevel@tonic-gate #include <limits.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #define	PROMPT	"Enter the name of an existing group"
43*7c478bd9Sstevel@tonic-gate #define	MESG	"Please enter the name of an existing group."
44*7c478bd9Sstevel@tonic-gate #define	ALTMESG	"Please enter one of the following group names:\\n\\t"
45*7c478bd9Sstevel@tonic-gate #define	MALSIZ	64
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #define	DELIM1 '/'
48*7c478bd9Sstevel@tonic-gate #define	BLANK ' '
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate static char *
setmsg(int disp)51*7c478bd9Sstevel@tonic-gate setmsg(int disp)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	struct group
54*7c478bd9Sstevel@tonic-gate 		*grpptr;
55*7c478bd9Sstevel@tonic-gate 	int	count;
56*7c478bd9Sstevel@tonic-gate 	size_t	n, m;
57*7c478bd9Sstevel@tonic-gate 	char	*msg;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	if (disp == 0)
60*7c478bd9Sstevel@tonic-gate 		return (MESG);
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 	m = MALSIZ;
63*7c478bd9Sstevel@tonic-gate 	n = sizeof (ALTMESG);
64*7c478bd9Sstevel@tonic-gate 	msg = calloc(m, sizeof (char));
65*7c478bd9Sstevel@tonic-gate 	(void) strcpy(msg, ALTMESG);
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	setgrent();
68*7c478bd9Sstevel@tonic-gate 	count = 0;
69*7c478bd9Sstevel@tonic-gate 	while ((grpptr = getgrent()) != NULL) {
70*7c478bd9Sstevel@tonic-gate 		n += strlen(grpptr->gr_name) + 2;
71*7c478bd9Sstevel@tonic-gate 		while (n >= m) {
72*7c478bd9Sstevel@tonic-gate 			m += MALSIZ;
73*7c478bd9Sstevel@tonic-gate 			msg = realloc(msg, m*sizeof (char));
74*7c478bd9Sstevel@tonic-gate 		}
75*7c478bd9Sstevel@tonic-gate 		if (count++)
76*7c478bd9Sstevel@tonic-gate 			(void) strcat(msg, ", ");
77*7c478bd9Sstevel@tonic-gate 		(void) strcat(msg, grpptr->gr_name);
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 	endgrent();
80*7c478bd9Sstevel@tonic-gate 	return (msg);
81*7c478bd9Sstevel@tonic-gate }
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate int
ckgid_dsp(void)84*7c478bd9Sstevel@tonic-gate ckgid_dsp(void)
85*7c478bd9Sstevel@tonic-gate {
86*7c478bd9Sstevel@tonic-gate 	struct group *grpptr;
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	/* if display flag is set, then list out group file */
89*7c478bd9Sstevel@tonic-gate 	if (ckgrpfile() == 1)
90*7c478bd9Sstevel@tonic-gate 		return (1);
91*7c478bd9Sstevel@tonic-gate 	setgrent();
92*7c478bd9Sstevel@tonic-gate 	while (grpptr = getgrent())
93*7c478bd9Sstevel@tonic-gate 		(void) printf("%s\n", grpptr->gr_name);
94*7c478bd9Sstevel@tonic-gate 	endgrent();
95*7c478bd9Sstevel@tonic-gate 	return (0);
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate int
ckgid_val(char * grpnm)99*7c478bd9Sstevel@tonic-gate ckgid_val(char *grpnm)
100*7c478bd9Sstevel@tonic-gate {
101*7c478bd9Sstevel@tonic-gate 	int	valid;
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	setgrent();
104*7c478bd9Sstevel@tonic-gate 	valid = (getgrnam(grpnm) ? 0 : 1);
105*7c478bd9Sstevel@tonic-gate 	endgrent();
106*7c478bd9Sstevel@tonic-gate 	return (valid);
107*7c478bd9Sstevel@tonic-gate }
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate int
ckgrpfile(void)110*7c478bd9Sstevel@tonic-gate ckgrpfile(void) /* check to see if group file there */
111*7c478bd9Sstevel@tonic-gate {
112*7c478bd9Sstevel@tonic-gate 	struct group *grpptr;
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	setgrent();
115*7c478bd9Sstevel@tonic-gate 	grpptr = getgrent();
116*7c478bd9Sstevel@tonic-gate 	if (!grpptr) {
117*7c478bd9Sstevel@tonic-gate 		endgrent();
118*7c478bd9Sstevel@tonic-gate 		return (1);
119*7c478bd9Sstevel@tonic-gate 	}
120*7c478bd9Sstevel@tonic-gate 	endgrent();
121*7c478bd9Sstevel@tonic-gate 	return (0);
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate void
ckgid_err(int disp,char * error)125*7c478bd9Sstevel@tonic-gate ckgid_err(int disp, char *error)
126*7c478bd9Sstevel@tonic-gate {
127*7c478bd9Sstevel@tonic-gate 	char	*msg;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	msg = setmsg(disp);
130*7c478bd9Sstevel@tonic-gate 	puterror(stdout, msg, error);
131*7c478bd9Sstevel@tonic-gate 	if (disp)
132*7c478bd9Sstevel@tonic-gate 		free(msg);
133*7c478bd9Sstevel@tonic-gate }
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate void
ckgid_hlp(int disp,char * help)136*7c478bd9Sstevel@tonic-gate ckgid_hlp(int disp, char *help)
137*7c478bd9Sstevel@tonic-gate {
138*7c478bd9Sstevel@tonic-gate 	char	*msg;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	msg = setmsg(disp);
141*7c478bd9Sstevel@tonic-gate 	puthelp(stdout, msg, help);
142*7c478bd9Sstevel@tonic-gate 	if (disp)
143*7c478bd9Sstevel@tonic-gate 		free(msg);
144*7c478bd9Sstevel@tonic-gate }
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate int
ckgid(char * gid,short disp,char * defstr,char * error,char * help,char * prompt)147*7c478bd9Sstevel@tonic-gate ckgid(char *gid, short disp, char *defstr, char *error, char *help,
148*7c478bd9Sstevel@tonic-gate     char *prompt)
149*7c478bd9Sstevel@tonic-gate {
150*7c478bd9Sstevel@tonic-gate 	char	*defmesg,
151*7c478bd9Sstevel@tonic-gate 		input[MAX_INPUT];
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	defmesg = NULL;
154*7c478bd9Sstevel@tonic-gate 	if (!prompt)
155*7c478bd9Sstevel@tonic-gate 		prompt = PROMPT;
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate start:
158*7c478bd9Sstevel@tonic-gate 	putprmpt(stderr, prompt, NULL, defstr);
159*7c478bd9Sstevel@tonic-gate 	if (getinput(input)) {
160*7c478bd9Sstevel@tonic-gate 		if (disp && defmesg)
161*7c478bd9Sstevel@tonic-gate 			free(defmesg);
162*7c478bd9Sstevel@tonic-gate 		return (1);
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	if (!strlen(input)) {
166*7c478bd9Sstevel@tonic-gate 		if (defstr) {
167*7c478bd9Sstevel@tonic-gate 			if (disp && defmesg)
168*7c478bd9Sstevel@tonic-gate 				free(defmesg);
169*7c478bd9Sstevel@tonic-gate 			(void) strcpy(gid, defstr);
170*7c478bd9Sstevel@tonic-gate 			return (0);
171*7c478bd9Sstevel@tonic-gate 		}
172*7c478bd9Sstevel@tonic-gate 		if (!defmesg)
173*7c478bd9Sstevel@tonic-gate 			defmesg = setmsg(disp);
174*7c478bd9Sstevel@tonic-gate 		puterror(stderr, defmesg, error);
175*7c478bd9Sstevel@tonic-gate 		goto start;
176*7c478bd9Sstevel@tonic-gate 	} else if (strcmp(input, "?") == 0) {
177*7c478bd9Sstevel@tonic-gate 		if (!defmesg)
178*7c478bd9Sstevel@tonic-gate 			defmesg = setmsg(disp);
179*7c478bd9Sstevel@tonic-gate 		puthelp(stderr, defmesg, help);
180*7c478bd9Sstevel@tonic-gate 		goto start;
181*7c478bd9Sstevel@tonic-gate 	} else if (ckquit && (strcmp(input, "q") == 0)) {
182*7c478bd9Sstevel@tonic-gate 		if (disp && defmesg)
183*7c478bd9Sstevel@tonic-gate 			free(defmesg);
184*7c478bd9Sstevel@tonic-gate 		return (3);
185*7c478bd9Sstevel@tonic-gate 	} else if (ckgid_val(input)) {
186*7c478bd9Sstevel@tonic-gate 		if (!defmesg)
187*7c478bd9Sstevel@tonic-gate 			defmesg = setmsg(disp);
188*7c478bd9Sstevel@tonic-gate 		puterror(stderr, defmesg, error);
189*7c478bd9Sstevel@tonic-gate 		goto start;
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate 	(void) strcpy(gid, input);
192*7c478bd9Sstevel@tonic-gate 	if (disp && defmesg)
193*7c478bd9Sstevel@tonic-gate 		free(defmesg);
194*7c478bd9Sstevel@tonic-gate 	return (0);
195*7c478bd9Sstevel@tonic-gate }
196