xref: /titanic_52/usr/src/lib/libadm/common/ckgid.c (revision c2580b931007758eab8cb5ae8726ebe1588e259b)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright (c) 1997, by Sun Microsystems, Inc.
28  * All rights reserved.
29  */
30 
31 /*LINTLIBRARY*/
32 #pragma	ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.2 */
33 
34 #include <stdio.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <grp.h>
38 #include <sys/types.h>
39 #include "libadm.h"
40 #include <stdlib.h>
41 #include <limits.h>
42 
43 #define	PROMPT	"Enter the name of an existing group"
44 #define	MESG	"Please enter the name of an existing group."
45 #define	ALTMESG	"Please enter one of the following group names:\\n\\t"
46 #define	MALSIZ	64
47 
48 #define	DELIM1 '/'
49 #define	BLANK ' '
50 
51 static char *
52 setmsg(int disp)
53 {
54 	struct group
55 		*grpptr;
56 	int	count;
57 	size_t	n, m;
58 	char	*msg;
59 
60 	if (disp == 0)
61 		return (MESG);
62 
63 	m = MALSIZ;
64 	n = sizeof (ALTMESG);
65 	msg = calloc(m, sizeof (char));
66 	(void) strcpy(msg, ALTMESG);
67 
68 	setgrent();
69 	count = 0;
70 	while ((grpptr = getgrent()) != NULL) {
71 		n += strlen(grpptr->gr_name) + 2;
72 		while (n >= m) {
73 			m += MALSIZ;
74 			msg = realloc(msg, m*sizeof (char));
75 		}
76 		if (count++)
77 			(void) strcat(msg, ", ");
78 		(void) strcat(msg, grpptr->gr_name);
79 	}
80 	endgrent();
81 	return (msg);
82 }
83 
84 int
85 ckgid_dsp(void)
86 {
87 	struct group *grpptr;
88 
89 	/* if display flag is set, then list out group file */
90 	if (ckgrpfile() == 1)
91 		return (1);
92 	setgrent();
93 	while (grpptr = getgrent())
94 		(void) printf("%s\n", grpptr->gr_name);
95 	endgrent();
96 	return (0);
97 }
98 
99 int
100 ckgid_val(char *grpnm)
101 {
102 	int	valid;
103 
104 	setgrent();
105 	valid = (getgrnam(grpnm) ? 0 : 1);
106 	endgrent();
107 	return (valid);
108 }
109 
110 int
111 ckgrpfile(void) /* check to see if group file there */
112 {
113 	struct group *grpptr;
114 
115 	setgrent();
116 	grpptr = getgrent();
117 	if (!grpptr) {
118 		endgrent();
119 		return (1);
120 	}
121 	endgrent();
122 	return (0);
123 }
124 
125 void
126 ckgid_err(int disp, char *error)
127 {
128 	char	*msg;
129 
130 	msg = setmsg(disp);
131 	puterror(stdout, msg, error);
132 	if (disp)
133 		free(msg);
134 }
135 
136 void
137 ckgid_hlp(int disp, char *help)
138 {
139 	char	*msg;
140 
141 	msg = setmsg(disp);
142 	puthelp(stdout, msg, help);
143 	if (disp)
144 		free(msg);
145 }
146 
147 int
148 ckgid(char *gid, short disp, char *defstr, char *error, char *help,
149     char *prompt)
150 {
151 	char	*defmesg,
152 		input[MAX_INPUT];
153 
154 	defmesg = NULL;
155 	if (!prompt)
156 		prompt = PROMPT;
157 
158 start:
159 	putprmpt(stderr, prompt, NULL, defstr);
160 	if (getinput(input)) {
161 		if (disp && defmesg)
162 			free(defmesg);
163 		return (1);
164 	}
165 
166 	if (!strlen(input)) {
167 		if (defstr) {
168 			if (disp && defmesg)
169 				free(defmesg);
170 			(void) strcpy(gid, defstr);
171 			return (0);
172 		}
173 		if (!defmesg)
174 			defmesg = setmsg(disp);
175 		puterror(stderr, defmesg, error);
176 		goto start;
177 	} else if (strcmp(input, "?") == 0) {
178 		if (!defmesg)
179 			defmesg = setmsg(disp);
180 		puthelp(stderr, defmesg, help);
181 		goto start;
182 	} else if (ckquit && (strcmp(input, "q") == 0)) {
183 		if (disp && defmesg)
184 			free(defmesg);
185 		return (3);
186 	} else if (ckgid_val(input)) {
187 		if (!defmesg)
188 			defmesg = setmsg(disp);
189 		puterror(stderr, defmesg, error);
190 		goto start;
191 	}
192 	(void) strcpy(gid, input);
193 	if (disp && defmesg)
194 		free(defmesg);
195 	return (0);
196 }
197