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