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