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