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 #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.2 */
32 /*LINTLIBRARY*/
33
34 #include <sys/types.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <pwd.h>
39 #include <stdlib.h>
40 #include <limits.h>
41 #include "libadm.h"
42
43 #define PROMPT "Enter the login name of an existing user"
44 #define MESG "Please enter the login name of an existing user."
45 #define ALTMESG "Please enter one of the following login names:\\n\\t"
46 #define MALSIZ 64
47
48 #define DELIM1 '/'
49 #define BLANK ' '
50
51 static char *
setmsg(int disp)52 setmsg(int disp)
53 {
54 struct passwd
55 *pwdptr;
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 = (char *) calloc(m, sizeof (char));
66 (void) strcpy(msg, ALTMESG);
67
68 setpwent();
69 count = 0;
70 while (pwdptr = getpwent()) {
71 n += strlen(pwdptr->pw_name) + 2;
72 while (n >= m) {
73 m += MALSIZ;
74 msg = (char *) realloc(msg, m*sizeof (char));
75 }
76 if (count++)
77 (void) strcat(msg, ", ");
78 (void) strcat(msg, pwdptr->pw_name);
79 }
80 endpwent();
81 return (msg);
82 }
83
84 int
ckuid_dsp(void)85 ckuid_dsp(void)
86 {
87 struct passwd *pwdptr;
88
89 /* if display flag is set, then list out passwd file */
90 if (ckpwdfile() == 1)
91 return (1);
92 setpwent();
93 while (pwdptr = getpwent())
94 (void) printf("%s\n", pwdptr->pw_name);
95 endpwent();
96 return (0);
97 }
98
99 int
ckuid_val(char * usrnm)100 ckuid_val(char *usrnm)
101 {
102 int valid;
103
104 setpwent();
105 valid = (getpwnam(usrnm) ? 0 : 1);
106 endpwent();
107 return (valid);
108 }
109
110 int
ckpwdfile(void)111 ckpwdfile(void) /* check to see if passwd file there */
112 {
113 struct passwd *pwdptr;
114
115 setpwent();
116 pwdptr = getpwent();
117 if (!pwdptr) {
118 endpwent();
119 return (1);
120 }
121 endpwent();
122 return (0);
123 }
124
125 void
ckuid_err(short disp,char * error)126 ckuid_err(short 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
ckuid_hlp(int disp,char * help)137 ckuid_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
ckuid(char * uid,short disp,char * defstr,char * error,char * help,char * prompt)148 ckuid(char *uid, 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(uid, 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 (ckuid_val(input)) {
187 if (!defmesg)
188 defmesg = setmsg(disp);
189 puterror(stderr, defmesg, error);
190 goto start;
191 }
192 (void) strcpy(uid, input);
193 if (disp && defmesg)
194 free(defmesg);
195 return (0);
196 }
197