passwd.c revision 1.25 1 1.25 thorpej /* $NetBSD: passwd.c,v 1.25 2005/02/26 07:19:25 thorpej Exp $ */
2 1.8 thorpej
3 1.1 cgd /*
4 1.10 tls * Copyright (c) 1988, 1993, 1994
5 1.10 tls * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.23 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.12 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.12 lukem __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\n\
35 1.12 lukem The Regents of the University of California. All rights reserved.\n");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.8 thorpej #if 0
40 1.10 tls static char sccsid[] = "from: @(#)passwd.c 8.3 (Berkeley) 4/2/94";
41 1.8 thorpej #else
42 1.25 thorpej __RCSID("$NetBSD: passwd.c,v 1.25 2005/02/26 07:19:25 thorpej Exp $");
43 1.8 thorpej #endif
44 1.1 cgd #endif /* not lint */
45 1.1 cgd
46 1.25 thorpej #include <assert.h>
47 1.8 thorpej #include <err.h>
48 1.1 cgd #include <stdio.h>
49 1.19 matt #include <stdlib.h>
50 1.7 phil #include <string.h>
51 1.1 cgd #include <unistd.h>
52 1.20 ad #include <pwd.h>
53 1.1 cgd
54 1.10 tls #include "extern.h"
55 1.16 aidan
56 1.25 thorpej #ifdef USE_PAM
57 1.25 thorpej
58 1.25 thorpej static void global_usage(const char *);
59 1.25 thorpej
60 1.25 thorpej static const struct pw_module_s {
61 1.25 thorpej const char *argv0;
62 1.25 thorpej const char *dbname;
63 1.25 thorpej char compat_opt;
64 1.25 thorpej void (*pw_usage)(const char *);
65 1.25 thorpej void (*pw_process)(const char *, int, char **);
66 1.25 thorpej } pw_modules[] = {
67 1.25 thorpej /* "files" -- local password database */
68 1.25 thorpej { NULL, "files", 'l', pwlocal_usage, pwlocal_process },
69 1.25 thorpej #ifdef YP
70 1.25 thorpej /* "nis" -- YP/NIS password database */
71 1.25 thorpej { NULL, "nis", 'y', pwyp_usage, pwyp_process },
72 1.25 thorpej { "yppasswd", NULL, 0, pwyp_argv0_usage, pwyp_process },
73 1.25 thorpej #endif
74 1.25 thorpej #ifdef KERBEROS5
75 1.25 thorpej /* "krb5" -- Kerberos 5 password database */
76 1.25 thorpej { NULL, "krb5", 'k', pwkrb5_usage, pwkrb5_process },
77 1.25 thorpej { "kpasswd", NULL, 0, pwkrb5_argv0_usage, pwkrb5_process },
78 1.25 thorpej #endif
79 1.25 thorpej /* default -- use whatever PAM decides */
80 1.25 thorpej { NULL, NULL, 0, NULL, pwpam_process },
81 1.25 thorpej
82 1.25 thorpej { 0 }
83 1.25 thorpej };
84 1.25 thorpej
85 1.25 thorpej static const struct pw_module_s *personality;
86 1.25 thorpej
87 1.25 thorpej static void
88 1.25 thorpej global_usage(const char *prefix)
89 1.25 thorpej {
90 1.25 thorpej const struct pw_module_s *pwm;
91 1.25 thorpej
92 1.25 thorpej (void) fprintf(stderr, "%s %s [user]\n", prefix, getprogname());
93 1.25 thorpej for (pwm = pw_modules; pwm->pw_process != NULL; pwm++) {
94 1.25 thorpej if (pwm->argv0 == NULL && pwm->pw_usage != NULL)
95 1.25 thorpej (*pwm->pw_usage)(" ");
96 1.25 thorpej }
97 1.25 thorpej }
98 1.25 thorpej
99 1.25 thorpej void
100 1.25 thorpej usage(void)
101 1.25 thorpej {
102 1.25 thorpej
103 1.25 thorpej if (personality != NULL && personality->pw_usage != NULL)
104 1.25 thorpej (*personality->pw_usage)("usage:");
105 1.25 thorpej else
106 1.25 thorpej global_usage("usage:");
107 1.25 thorpej exit(1);
108 1.25 thorpej }
109 1.25 thorpej
110 1.25 thorpej int
111 1.25 thorpej main(int argc, char **argv)
112 1.25 thorpej {
113 1.25 thorpej const struct pw_module_s *pwm;
114 1.25 thorpej const char *username;
115 1.25 thorpej int ch, i;
116 1.25 thorpej char opts[16];
117 1.25 thorpej
118 1.25 thorpej /* Build opts string from module compat_opts */
119 1.25 thorpej i = 0;
120 1.25 thorpej opts[i++] = 'd';
121 1.25 thorpej opts[i++] = ':';
122 1.25 thorpej for (pwm = pw_modules; pwm->pw_process != NULL; pwm++) {
123 1.25 thorpej if (pwm->compat_opt != 0)
124 1.25 thorpej opts[i++] = pwm->compat_opt;
125 1.25 thorpej }
126 1.25 thorpej opts[i++] = '\0';
127 1.25 thorpej
128 1.25 thorpej /* First, look for personality based on argv[0]. */
129 1.25 thorpej for (pwm = pw_modules; pwm->pw_process != NULL; pwm++) {
130 1.25 thorpej if (pwm->argv0 != NULL &&
131 1.25 thorpej strcmp(pwm->argv0, getprogname()) == 0)
132 1.25 thorpej goto got_personality;
133 1.25 thorpej }
134 1.25 thorpej
135 1.25 thorpej /* Try based on compat_opt or -d. */
136 1.25 thorpej for (ch = 0, pwm = pw_modules; pwm->pw_process != NULL; pwm++) {
137 1.25 thorpej if (pwm->argv0 == NULL && pwm->dbname == NULL &&
138 1.25 thorpej pwm->compat_opt == 0) {
139 1.25 thorpej /*
140 1.25 thorpej * We have reached the default personality case.
141 1.25 thorpej * Make sure the user didn't provide a bogus
142 1.25 thorpej * personality name.
143 1.25 thorpej */
144 1.25 thorpej if (ch == 'd')
145 1.25 thorpej usage();
146 1.25 thorpej break;
147 1.25 thorpej }
148 1.25 thorpej
149 1.25 thorpej ch = getopt(argc, argv, opts);
150 1.25 thorpej if (ch == '?')
151 1.25 thorpej usage();
152 1.25 thorpej
153 1.25 thorpej if (ch == 'd' && pwm->dbname != NULL &&
154 1.25 thorpej strcmp(pwm->dbname, optarg) == 0) {
155 1.25 thorpej /*
156 1.25 thorpej * "passwd -d dbname" matches; this is our
157 1.25 thorpej * chosen personality.
158 1.25 thorpej */
159 1.25 thorpej break;
160 1.25 thorpej }
161 1.25 thorpej
162 1.25 thorpej if (pwm->compat_opt != 0 && ch == pwm->compat_opt) {
163 1.25 thorpej /*
164 1.25 thorpej * Legacy "passwd -l" or similar matches; this
165 1.25 thorpej * is our chosen personality.
166 1.25 thorpej */
167 1.25 thorpej break;
168 1.25 thorpej }
169 1.25 thorpej
170 1.25 thorpej /* Reset getopt() and go around again. */
171 1.25 thorpej optind = 1;
172 1.25 thorpej optreset = 1;
173 1.25 thorpej }
174 1.25 thorpej
175 1.25 thorpej got_personality:
176 1.25 thorpej personality = pwm;
177 1.25 thorpej
178 1.25 thorpej /*
179 1.25 thorpej * At this point, optind should be either 1 ("passwd"),
180 1.25 thorpej * 2 ("passwd -l"), or 3 ("passwd -d files"). Consume
181 1.25 thorpej * these arguments and reset getopt() for the modules to use.
182 1.25 thorpej */
183 1.25 thorpej assert(optind >= 1 && optind <= 3);
184 1.25 thorpej argc -= optind;
185 1.25 thorpej argv += optind;
186 1.25 thorpej optind = 0;
187 1.25 thorpej optreset = 1;
188 1.25 thorpej
189 1.25 thorpej username = getlogin();
190 1.25 thorpej if (username == NULL)
191 1.25 thorpej errx(1, "who are you ??");
192 1.25 thorpej
193 1.25 thorpej (*personality->pw_process)(username, argc, argv);
194 1.25 thorpej exit(0);
195 1.25 thorpej }
196 1.25 thorpej
197 1.25 thorpej #else /* ! USE_PAM */
198 1.25 thorpej
199 1.16 aidan static struct pw_module_s {
200 1.16 aidan const char *argv0;
201 1.16 aidan const char *args;
202 1.16 aidan const char *usage;
203 1.16 aidan int (*pw_init) __P((const char *));
204 1.16 aidan int (*pw_arg) __P((char, const char *));
205 1.16 aidan int (*pw_arg_end) __P((void));
206 1.16 aidan void (*pw_end) __P((void));
207 1.16 aidan
208 1.16 aidan int (*pw_chpw) __P((const char*));
209 1.16 aidan int invalid;
210 1.16 aidan #define INIT_INVALID 1
211 1.16 aidan #define ARG_INVALID 2
212 1.16 aidan int use_class;
213 1.16 aidan } pw_modules[] = {
214 1.16 aidan #ifdef KERBEROS5
215 1.16 aidan { NULL, "5ku:", "[-5] [-k] [-u principal]",
216 1.16 aidan krb5_init, krb5_arg, krb5_arg_end, krb5_end, krb5_chpw, 0, 0 },
217 1.16 aidan { "kpasswd", "5ku:", "[-5] [-k] [-u principal]",
218 1.16 aidan krb5_init, krb5_arg, krb5_arg_end, krb5_end, krb5_chpw, 0, 0 },
219 1.16 aidan #endif
220 1.16 aidan #ifdef KERBEROS
221 1.16 aidan { NULL, "4ku:i:r:", "[-4] [-k] [-u user] [-i instance] [-r realm]",
222 1.16 aidan krb4_init, krb4_arg, krb4_arg_end, krb4_end, krb4_chpw, 0, 0 },
223 1.16 aidan { "kpasswd", "4ku:i:r:", "[-4] [-k] [-u user] [-i instance] [-r realm]",
224 1.16 aidan krb4_init, krb4_arg, krb4_arg_end, krb4_end, krb4_chpw, 0, 0 },
225 1.16 aidan #endif
226 1.16 aidan #ifdef YP
227 1.16 aidan { NULL, "y", "[-y]",
228 1.16 aidan yp_init, yp_arg, yp_arg_end, yp_end, yp_chpw, 0, 0 },
229 1.16 aidan { "yppasswd", "", "[-y]",
230 1.16 aidan yp_init, yp_arg, yp_arg_end, yp_end, yp_chpw, 0, 0 },
231 1.16 aidan #endif
232 1.16 aidan /* local */
233 1.16 aidan { NULL, "l", "[-l]",
234 1.16 aidan local_init, local_arg, local_arg_end, local_end, local_chpw, 0, 0 },
235 1.16 aidan
236 1.16 aidan /* terminator */
237 1.16 aidan { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
238 1.16 aidan };
239 1.10 tls
240 1.25 thorpej static void
241 1.25 thorpej usage(void)
242 1.25 thorpej {
243 1.25 thorpej int i;
244 1.10 tls
245 1.25 thorpej fprintf(stderr, "usage:\n");
246 1.25 thorpej for (i = 0; pw_modules[i].pw_init != NULL; i++)
247 1.25 thorpej if (! (pw_modules[i].invalid & INIT_INVALID))
248 1.25 thorpej fprintf(stderr, "\t%s %s [user]\n", getprogname(),
249 1.25 thorpej pw_modules[i].usage);
250 1.25 thorpej exit(1);
251 1.25 thorpej }
252 1.12 lukem
253 1.12 lukem int
254 1.25 thorpej main(int argc, char **argv)
255 1.1 cgd {
256 1.10 tls int ch;
257 1.5 deraadt char *username;
258 1.16 aidan char optstring[64]; /* if we ever get more than 64 args, shoot me. */
259 1.16 aidan const char *curopt, *optopt;
260 1.16 aidan int i, j;
261 1.16 aidan int valid;
262 1.16 aidan int use_always;
263 1.16 aidan
264 1.16 aidan /* allow passwd modules to do argv[0] specific processing */
265 1.16 aidan use_always = 0;
266 1.16 aidan valid = 0;
267 1.16 aidan for (i = 0; pw_modules[i].pw_init != NULL; i++) {
268 1.16 aidan pw_modules[i].invalid = 0;
269 1.16 aidan if (pw_modules[i].argv0) {
270 1.16 aidan /*
271 1.16 aidan * If we have a module that matches this progname, be
272 1.16 aidan * sure that no modules but those that match this
273 1.16 aidan * progname can be used. If we have a module that
274 1.16 aidan * matches against a particular progname, but does NOT
275 1.16 aidan * match this one, don't use that module.
276 1.16 aidan */
277 1.21 cgd if ((strcmp(getprogname(), pw_modules[i].argv0) == 0) &&
278 1.16 aidan use_always == 0) {
279 1.16 aidan for (j = 0; j < i; j++) {
280 1.16 aidan pw_modules[j].invalid |= INIT_INVALID;
281 1.16 aidan (*pw_modules[j].pw_end)();
282 1.16 aidan }
283 1.16 aidan use_always = 1;
284 1.16 aidan } else if (use_always == 0)
285 1.16 aidan pw_modules[i].invalid |= INIT_INVALID;
286 1.16 aidan } else if (use_always)
287 1.16 aidan pw_modules[i].invalid |= INIT_INVALID;
288 1.16 aidan
289 1.16 aidan if (pw_modules[i].invalid)
290 1.16 aidan continue;
291 1.16 aidan
292 1.22 simonb pw_modules[i].invalid |=
293 1.22 simonb (*pw_modules[i].pw_init)(getprogname()) ?
294 1.16 aidan /* zero on success, non-zero on error */
295 1.16 aidan INIT_INVALID : 0;
296 1.16 aidan
297 1.16 aidan if (! pw_modules[i].invalid)
298 1.16 aidan valid = 1;
299 1.16 aidan }
300 1.7 phil
301 1.16 aidan if (valid == 0)
302 1.16 aidan errx(1, "Can't change password.");
303 1.7 phil
304 1.16 aidan /* Build the option string from the individual modules' option
305 1.16 aidan * strings. Note that two modules can share a single option
306 1.16 aidan * letter. */
307 1.16 aidan optstring[0] = '\0';
308 1.16 aidan j = 0;
309 1.16 aidan for (i = 0; pw_modules[i].pw_init != NULL; i++) {
310 1.16 aidan if (pw_modules[i].invalid)
311 1.16 aidan continue;
312 1.16 aidan
313 1.16 aidan curopt = pw_modules[i].args;
314 1.16 aidan while (*curopt != '\0') {
315 1.17 joda if ((optopt = strchr(optstring, *curopt)) == NULL) {
316 1.16 aidan optstring[j++] = *curopt;
317 1.16 aidan if (curopt[1] == ':') {
318 1.16 aidan curopt++;
319 1.16 aidan optstring[j++] = *curopt;
320 1.16 aidan }
321 1.16 aidan optstring[j] = '\0';
322 1.16 aidan } else if ((optopt[1] == ':' && curopt[1] != ':') ||
323 1.16 aidan (optopt[1] != ':' && curopt[1] == ':')) {
324 1.16 aidan errx(1, "NetBSD ERROR! Different password "
325 1.16 aidan "modules have two different ideas about "
326 1.16 aidan "%c argument format.", curopt[0]);
327 1.16 aidan }
328 1.16 aidan curopt++;
329 1.16 aidan }
330 1.7 phil }
331 1.7 phil
332 1.16 aidan while ((ch = getopt(argc, argv, optstring)) != -1)
333 1.16 aidan {
334 1.16 aidan valid = 0;
335 1.16 aidan for (i = 0; pw_modules[i].pw_init != NULL; i++) {
336 1.16 aidan if (pw_modules[i].invalid)
337 1.16 aidan continue;
338 1.17 joda if ((optopt = strchr(pw_modules[i].args, ch)) != NULL) {
339 1.16 aidan j = (optopt[1] == ':') ?
340 1.16 aidan ! (*pw_modules[i].pw_arg)(ch, optarg) :
341 1.17 joda ! (*pw_modules[i].pw_arg)(ch, NULL);
342 1.16 aidan if (j != 0)
343 1.16 aidan pw_modules[i].invalid |= ARG_INVALID;
344 1.16 aidan if (pw_modules[i].invalid)
345 1.16 aidan (*pw_modules[i].pw_end)();
346 1.16 aidan } else {
347 1.16 aidan /* arg doesn't match this module */
348 1.16 aidan pw_modules[i].invalid |= ARG_INVALID;
349 1.16 aidan (*pw_modules[i].pw_end)();
350 1.16 aidan }
351 1.16 aidan if (! pw_modules[i].invalid)
352 1.16 aidan valid = 1;
353 1.16 aidan }
354 1.16 aidan if (! valid) {
355 1.1 cgd usage();
356 1.16 aidan exit(1);
357 1.16 aidan }
358 1.16 aidan }
359 1.16 aidan
360 1.16 aidan /* select which module to use to actually change the password. */
361 1.16 aidan use_always = 0;
362 1.16 aidan valid = 0;
363 1.16 aidan for (i = 0; pw_modules[i].pw_init != NULL; i++)
364 1.16 aidan if (! pw_modules[i].invalid) {
365 1.16 aidan pw_modules[i].use_class = (*pw_modules[i].pw_arg_end)();
366 1.16 aidan if (pw_modules[i].use_class != PW_DONT_USE)
367 1.16 aidan valid = 1;
368 1.16 aidan if (pw_modules[i].use_class == PW_USE_FORCE)
369 1.16 aidan use_always = 1;
370 1.1 cgd }
371 1.1 cgd
372 1.16 aidan
373 1.16 aidan if (! valid)
374 1.16 aidan /* hang the DJ */
375 1.16 aidan errx(1, "No valid password module specified.");
376 1.16 aidan
377 1.1 cgd argc -= optind;
378 1.1 cgd argv += optind;
379 1.1 cgd
380 1.5 deraadt username = getlogin();
381 1.8 thorpej if (username == NULL)
382 1.8 thorpej errx(1, "who are you ??");
383 1.5 deraadt
384 1.1 cgd switch(argc) {
385 1.1 cgd case 0:
386 1.1 cgd break;
387 1.1 cgd case 1:
388 1.5 deraadt username = argv[0];
389 1.1 cgd break;
390 1.1 cgd default:
391 1.1 cgd usage();
392 1.1 cgd exit(1);
393 1.1 cgd }
394 1.1 cgd
395 1.16 aidan /* allow for fallback to other chpw() methods. */
396 1.16 aidan for (i = 0; pw_modules[i].pw_init != NULL; i++) {
397 1.16 aidan if (pw_modules[i].invalid)
398 1.16 aidan continue;
399 1.16 aidan if ((use_always && pw_modules[i].use_class == PW_USE_FORCE) ||
400 1.16 aidan (!use_always && pw_modules[i].use_class == PW_USE)) {
401 1.16 aidan valid = (*pw_modules[i].pw_chpw)(username);
402 1.16 aidan (*pw_modules[i].pw_end)();
403 1.16 aidan if (valid >= 0)
404 1.16 aidan exit(valid);
405 1.16 aidan /* return value < 0 indicates continuation. */
406 1.16 aidan }
407 1.16 aidan }
408 1.16 aidan exit(1);
409 1.1 cgd }
410 1.1 cgd
411 1.25 thorpej #endif /* USE_PAM */
412