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