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