passwd.c revision 1.17 1 1.17 joda /* $NetBSD: passwd.c,v 1.17 2000/03/01 12:46:36 joda 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.17 joda __RCSID("$NetBSD: passwd.c,v 1.17 2000/03/01 12:46:36 joda 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.7 phil #include <string.h>
53 1.1 cgd #include <unistd.h>
54 1.1 cgd
55 1.10 tls #include "extern.h"
56 1.16 aidan
57 1.16 aidan static struct pw_module_s {
58 1.16 aidan const char *argv0;
59 1.16 aidan const char *args;
60 1.16 aidan const char *usage;
61 1.16 aidan int (*pw_init) __P((const char *));
62 1.16 aidan int (*pw_arg) __P((char, const char *));
63 1.16 aidan int (*pw_arg_end) __P((void));
64 1.16 aidan void (*pw_end) __P((void));
65 1.16 aidan
66 1.16 aidan int (*pw_chpw) __P((const char*));
67 1.16 aidan int invalid;
68 1.16 aidan #define INIT_INVALID 1
69 1.16 aidan #define ARG_INVALID 2
70 1.16 aidan int use_class;
71 1.16 aidan } pw_modules[] = {
72 1.16 aidan #ifdef KERBEROS5
73 1.16 aidan { NULL, "5ku:", "[-5] [-k] [-u principal]",
74 1.16 aidan krb5_init, krb5_arg, krb5_arg_end, krb5_end, krb5_chpw, 0, 0 },
75 1.16 aidan { "kpasswd", "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 #endif
78 1.16 aidan #ifdef KERBEROS
79 1.16 aidan { NULL, "4ku:i:r:", "[-4] [-k] [-u user] [-i instance] [-r realm]",
80 1.16 aidan krb4_init, krb4_arg, krb4_arg_end, krb4_end, krb4_chpw, 0, 0 },
81 1.16 aidan { "kpasswd", "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 #endif
84 1.16 aidan #ifdef YP
85 1.16 aidan { NULL, "y", "[-y]",
86 1.16 aidan yp_init, yp_arg, yp_arg_end, yp_end, yp_chpw, 0, 0 },
87 1.16 aidan { "yppasswd", "", "[-y]",
88 1.16 aidan yp_init, yp_arg, yp_arg_end, yp_end, yp_chpw, 0, 0 },
89 1.16 aidan #endif
90 1.16 aidan /* local */
91 1.16 aidan { NULL, "l", "[-l]",
92 1.16 aidan local_init, local_arg, local_arg_end, local_end, local_chpw, 0, 0 },
93 1.16 aidan
94 1.16 aidan /* terminator */
95 1.16 aidan { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
96 1.16 aidan };
97 1.10 tls
98 1.10 tls void usage __P((void));
99 1.10 tls
100 1.8 thorpej extern char *__progname; /* from crt0.o */
101 1.5 deraadt
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.1 cgd extern int optind;
110 1.10 tls int ch;
111 1.5 deraadt char *username;
112 1.16 aidan char optstring[64]; /* if we ever get more than 64 args, shoot me. */
113 1.16 aidan const char *curopt, *optopt;
114 1.16 aidan int i, j;
115 1.16 aidan int valid;
116 1.16 aidan int use_always;
117 1.16 aidan
118 1.16 aidan /* allow passwd modules to do argv[0] specific processing */
119 1.16 aidan use_always = 0;
120 1.16 aidan valid = 0;
121 1.16 aidan for (i = 0; pw_modules[i].pw_init != NULL; i++) {
122 1.16 aidan pw_modules[i].invalid = 0;
123 1.16 aidan if (pw_modules[i].argv0) {
124 1.16 aidan /*
125 1.16 aidan * If we have a module that matches this progname, be
126 1.16 aidan * sure that no modules but those that match this
127 1.16 aidan * progname can be used. If we have a module that
128 1.16 aidan * matches against a particular progname, but does NOT
129 1.16 aidan * match this one, don't use that module.
130 1.16 aidan */
131 1.16 aidan if ((strcmp(__progname, pw_modules[i].argv0) == 0) &&
132 1.16 aidan use_always == 0) {
133 1.16 aidan for (j = 0; j < i; j++) {
134 1.16 aidan pw_modules[j].invalid |= INIT_INVALID;
135 1.16 aidan (*pw_modules[j].pw_end)();
136 1.16 aidan }
137 1.16 aidan use_always = 1;
138 1.16 aidan } else if (use_always == 0)
139 1.16 aidan pw_modules[i].invalid |= INIT_INVALID;
140 1.16 aidan } else if (use_always)
141 1.16 aidan pw_modules[i].invalid |= INIT_INVALID;
142 1.16 aidan
143 1.16 aidan if (pw_modules[i].invalid)
144 1.16 aidan continue;
145 1.16 aidan
146 1.16 aidan pw_modules[i].invalid |= (*pw_modules[i].pw_init)(__progname) ?
147 1.16 aidan /* zero on success, non-zero on error */
148 1.16 aidan INIT_INVALID : 0;
149 1.16 aidan
150 1.16 aidan if (! pw_modules[i].invalid)
151 1.16 aidan valid = 1;
152 1.16 aidan }
153 1.7 phil
154 1.16 aidan if (valid == 0)
155 1.16 aidan errx(1, "Can't change password.");
156 1.7 phil
157 1.16 aidan /* Build the option string from the individual modules' option
158 1.16 aidan * strings. Note that two modules can share a single option
159 1.16 aidan * letter. */
160 1.16 aidan optstring[0] = '\0';
161 1.16 aidan j = 0;
162 1.16 aidan for (i = 0; pw_modules[i].pw_init != NULL; i++) {
163 1.16 aidan if (pw_modules[i].invalid)
164 1.16 aidan continue;
165 1.16 aidan
166 1.16 aidan curopt = pw_modules[i].args;
167 1.16 aidan while (*curopt != '\0') {
168 1.17 joda if ((optopt = strchr(optstring, *curopt)) == NULL) {
169 1.16 aidan optstring[j++] = *curopt;
170 1.16 aidan if (curopt[1] == ':') {
171 1.16 aidan curopt++;
172 1.16 aidan optstring[j++] = *curopt;
173 1.16 aidan }
174 1.16 aidan optstring[j] = '\0';
175 1.16 aidan } else if ((optopt[1] == ':' && curopt[1] != ':') ||
176 1.16 aidan (optopt[1] != ':' && curopt[1] == ':')) {
177 1.16 aidan errx(1, "NetBSD ERROR! Different password "
178 1.16 aidan "modules have two different ideas about "
179 1.16 aidan "%c argument format.", curopt[0]);
180 1.16 aidan }
181 1.16 aidan curopt++;
182 1.16 aidan }
183 1.7 phil }
184 1.7 phil
185 1.16 aidan while ((ch = getopt(argc, argv, optstring)) != -1)
186 1.16 aidan {
187 1.16 aidan valid = 0;
188 1.16 aidan for (i = 0; pw_modules[i].pw_init != NULL; i++) {
189 1.16 aidan if (pw_modules[i].invalid)
190 1.16 aidan continue;
191 1.17 joda if ((optopt = strchr(pw_modules[i].args, ch)) != NULL) {
192 1.16 aidan j = (optopt[1] == ':') ?
193 1.16 aidan ! (*pw_modules[i].pw_arg)(ch, optarg) :
194 1.17 joda ! (*pw_modules[i].pw_arg)(ch, NULL);
195 1.16 aidan if (j != 0)
196 1.16 aidan pw_modules[i].invalid |= ARG_INVALID;
197 1.16 aidan if (pw_modules[i].invalid)
198 1.16 aidan (*pw_modules[i].pw_end)();
199 1.16 aidan } else {
200 1.16 aidan /* arg doesn't match this module */
201 1.16 aidan pw_modules[i].invalid |= ARG_INVALID;
202 1.16 aidan (*pw_modules[i].pw_end)();
203 1.16 aidan }
204 1.16 aidan if (! pw_modules[i].invalid)
205 1.16 aidan valid = 1;
206 1.16 aidan }
207 1.16 aidan if (! valid) {
208 1.1 cgd usage();
209 1.16 aidan exit(1);
210 1.16 aidan }
211 1.16 aidan }
212 1.16 aidan
213 1.16 aidan /* select which module to use to actually change the password. */
214 1.16 aidan use_always = 0;
215 1.16 aidan valid = 0;
216 1.16 aidan for (i = 0; pw_modules[i].pw_init != NULL; i++)
217 1.16 aidan if (! pw_modules[i].invalid) {
218 1.16 aidan pw_modules[i].use_class = (*pw_modules[i].pw_arg_end)();
219 1.16 aidan if (pw_modules[i].use_class != PW_DONT_USE)
220 1.16 aidan valid = 1;
221 1.16 aidan if (pw_modules[i].use_class == PW_USE_FORCE)
222 1.16 aidan use_always = 1;
223 1.1 cgd }
224 1.1 cgd
225 1.16 aidan
226 1.16 aidan if (! valid)
227 1.16 aidan /* hang the DJ */
228 1.16 aidan errx(1, "No valid password module specified.");
229 1.16 aidan
230 1.1 cgd argc -= optind;
231 1.1 cgd argv += optind;
232 1.1 cgd
233 1.5 deraadt username = getlogin();
234 1.8 thorpej if (username == NULL)
235 1.8 thorpej errx(1, "who are you ??");
236 1.5 deraadt
237 1.1 cgd switch(argc) {
238 1.1 cgd case 0:
239 1.1 cgd break;
240 1.1 cgd case 1:
241 1.5 deraadt username = argv[0];
242 1.1 cgd break;
243 1.1 cgd default:
244 1.1 cgd usage();
245 1.1 cgd exit(1);
246 1.1 cgd }
247 1.1 cgd
248 1.16 aidan /* allow for fallback to other chpw() methods. */
249 1.16 aidan for (i = 0; pw_modules[i].pw_init != NULL; i++) {
250 1.16 aidan if (pw_modules[i].invalid)
251 1.16 aidan continue;
252 1.16 aidan if ((use_always && pw_modules[i].use_class == PW_USE_FORCE) ||
253 1.16 aidan (!use_always && pw_modules[i].use_class == PW_USE)) {
254 1.16 aidan valid = (*pw_modules[i].pw_chpw)(username);
255 1.16 aidan (*pw_modules[i].pw_end)();
256 1.16 aidan if (valid >= 0)
257 1.16 aidan exit(valid);
258 1.16 aidan /* return value < 0 indicates continuation. */
259 1.16 aidan }
260 1.16 aidan }
261 1.16 aidan exit(1);
262 1.1 cgd }
263 1.1 cgd
264 1.10 tls void
265 1.1 cgd usage()
266 1.1 cgd {
267 1.16 aidan int i;
268 1.8 thorpej
269 1.16 aidan fprintf(stderr, "usage:\n");
270 1.16 aidan for (i = 0; pw_modules[i].pw_init != NULL; i++)
271 1.16 aidan if (! (pw_modules[i].invalid & INIT_INVALID))
272 1.16 aidan fprintf(stderr, "\t%s %s [user]\n", __progname,
273 1.16 aidan pw_modules[i].usage);
274 1.8 thorpej exit(1);
275 1.1 cgd }
276