passwd.c revision 1.21 1 /* $NetBSD: passwd.c,v 1.21 2001/02/19 23:03:50 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "from: @(#)passwd.c 8.3 (Berkeley) 4/2/94";
45 #else
46 __RCSID("$NetBSD: passwd.c,v 1.21 2001/02/19 23:03:50 cgd Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <err.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <pwd.h>
56
57 #include "extern.h"
58
59 static struct pw_module_s {
60 const char *argv0;
61 const char *args;
62 const char *usage;
63 int (*pw_init) __P((const char *));
64 int (*pw_arg) __P((char, const char *));
65 int (*pw_arg_end) __P((void));
66 void (*pw_end) __P((void));
67
68 int (*pw_chpw) __P((const char*));
69 int invalid;
70 #define INIT_INVALID 1
71 #define ARG_INVALID 2
72 int use_class;
73 } pw_modules[] = {
74 #ifdef KERBEROS5
75 { NULL, "5ku:", "[-5] [-k] [-u principal]",
76 krb5_init, krb5_arg, krb5_arg_end, krb5_end, krb5_chpw, 0, 0 },
77 { "kpasswd", "5ku:", "[-5] [-k] [-u principal]",
78 krb5_init, krb5_arg, krb5_arg_end, krb5_end, krb5_chpw, 0, 0 },
79 #endif
80 #ifdef KERBEROS
81 { NULL, "4ku:i:r:", "[-4] [-k] [-u user] [-i instance] [-r realm]",
82 krb4_init, krb4_arg, krb4_arg_end, krb4_end, krb4_chpw, 0, 0 },
83 { "kpasswd", "4ku:i:r:", "[-4] [-k] [-u user] [-i instance] [-r realm]",
84 krb4_init, krb4_arg, krb4_arg_end, krb4_end, krb4_chpw, 0, 0 },
85 #endif
86 #ifdef YP
87 { NULL, "y", "[-y]",
88 yp_init, yp_arg, yp_arg_end, yp_end, yp_chpw, 0, 0 },
89 { "yppasswd", "", "[-y]",
90 yp_init, yp_arg, yp_arg_end, yp_end, yp_chpw, 0, 0 },
91 #endif
92 /* local */
93 { NULL, "l", "[-l]",
94 local_init, local_arg, local_arg_end, local_end, local_chpw, 0, 0 },
95
96 /* terminator */
97 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
98 };
99
100 void usage __P((void));
101
102 int main __P((int, char **));
103
104 int
105 main(argc, argv)
106 int argc;
107 char **argv;
108 {
109 int ch;
110 char *username;
111 char optstring[64]; /* if we ever get more than 64 args, shoot me. */
112 const char *curopt, *optopt;
113 int i, j;
114 int valid;
115 int use_always;
116
117 /* allow passwd modules to do argv[0] specific processing */
118 use_always = 0;
119 valid = 0;
120 for (i = 0; pw_modules[i].pw_init != NULL; i++) {
121 pw_modules[i].invalid = 0;
122 if (pw_modules[i].argv0) {
123 /*
124 * If we have a module that matches this progname, be
125 * sure that no modules but those that match this
126 * progname can be used. If we have a module that
127 * matches against a particular progname, but does NOT
128 * match this one, don't use that module.
129 */
130 if ((strcmp(getprogname(), pw_modules[i].argv0) == 0) &&
131 use_always == 0) {
132 for (j = 0; j < i; j++) {
133 pw_modules[j].invalid |= INIT_INVALID;
134 (*pw_modules[j].pw_end)();
135 }
136 use_always = 1;
137 } else if (use_always == 0)
138 pw_modules[i].invalid |= INIT_INVALID;
139 } else if (use_always)
140 pw_modules[i].invalid |= INIT_INVALID;
141
142 if (pw_modules[i].invalid)
143 continue;
144
145 pw_modules[i].invalid |= (*pw_modules[i].pw_init)(getprogname()) ?
146 /* zero on success, non-zero on error */
147 INIT_INVALID : 0;
148
149 if (! pw_modules[i].invalid)
150 valid = 1;
151 }
152
153 if (valid == 0)
154 errx(1, "Can't change password.");
155
156 /* Build the option string from the individual modules' option
157 * strings. Note that two modules can share a single option
158 * letter. */
159 optstring[0] = '\0';
160 j = 0;
161 for (i = 0; pw_modules[i].pw_init != NULL; i++) {
162 if (pw_modules[i].invalid)
163 continue;
164
165 curopt = pw_modules[i].args;
166 while (*curopt != '\0') {
167 if ((optopt = strchr(optstring, *curopt)) == NULL) {
168 optstring[j++] = *curopt;
169 if (curopt[1] == ':') {
170 curopt++;
171 optstring[j++] = *curopt;
172 }
173 optstring[j] = '\0';
174 } else if ((optopt[1] == ':' && curopt[1] != ':') ||
175 (optopt[1] != ':' && curopt[1] == ':')) {
176 errx(1, "NetBSD ERROR! Different password "
177 "modules have two different ideas about "
178 "%c argument format.", curopt[0]);
179 }
180 curopt++;
181 }
182 }
183
184 while ((ch = getopt(argc, argv, optstring)) != -1)
185 {
186 valid = 0;
187 for (i = 0; pw_modules[i].pw_init != NULL; i++) {
188 if (pw_modules[i].invalid)
189 continue;
190 if ((optopt = strchr(pw_modules[i].args, ch)) != NULL) {
191 j = (optopt[1] == ':') ?
192 ! (*pw_modules[i].pw_arg)(ch, optarg) :
193 ! (*pw_modules[i].pw_arg)(ch, NULL);
194 if (j != 0)
195 pw_modules[i].invalid |= ARG_INVALID;
196 if (pw_modules[i].invalid)
197 (*pw_modules[i].pw_end)();
198 } else {
199 /* arg doesn't match this module */
200 pw_modules[i].invalid |= ARG_INVALID;
201 (*pw_modules[i].pw_end)();
202 }
203 if (! pw_modules[i].invalid)
204 valid = 1;
205 }
206 if (! valid) {
207 usage();
208 exit(1);
209 }
210 }
211
212 /* select which module to use to actually change the password. */
213 use_always = 0;
214 valid = 0;
215 for (i = 0; pw_modules[i].pw_init != NULL; i++)
216 if (! pw_modules[i].invalid) {
217 pw_modules[i].use_class = (*pw_modules[i].pw_arg_end)();
218 if (pw_modules[i].use_class != PW_DONT_USE)
219 valid = 1;
220 if (pw_modules[i].use_class == PW_USE_FORCE)
221 use_always = 1;
222 }
223
224
225 if (! valid)
226 /* hang the DJ */
227 errx(1, "No valid password module specified.");
228
229 argc -= optind;
230 argv += optind;
231
232 username = getlogin();
233 if (username == NULL)
234 errx(1, "who are you ??");
235
236 switch(argc) {
237 case 0:
238 break;
239 case 1:
240 username = argv[0];
241 break;
242 default:
243 usage();
244 exit(1);
245 }
246
247 /* allow for fallback to other chpw() methods. */
248 for (i = 0; pw_modules[i].pw_init != NULL; i++) {
249 if (pw_modules[i].invalid)
250 continue;
251 if ((use_always && pw_modules[i].use_class == PW_USE_FORCE) ||
252 (!use_always && pw_modules[i].use_class == PW_USE)) {
253 valid = (*pw_modules[i].pw_chpw)(username);
254 (*pw_modules[i].pw_end)();
255 if (valid >= 0)
256 exit(valid);
257 /* return value < 0 indicates continuation. */
258 }
259 }
260 exit(1);
261 }
262
263 void
264 usage()
265 {
266 int i;
267
268 fprintf(stderr, "usage:\n");
269 for (i = 0; pw_modules[i].pw_init != NULL; i++)
270 if (! (pw_modules[i].invalid & INIT_INVALID))
271 fprintf(stderr, "\t%s %s [user]\n", getprogname(),
272 pw_modules[i].usage);
273 exit(1);
274 }
275