passwd.c revision 1.20 1 1.20 ad /* $NetBSD: passwd.c,v 1.20 2000/07/06 11:19:40 ad 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.20 ad __RCSID("$NetBSD: passwd.c,v 1.20 2000/07/06 11:19:40 ad 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.8 thorpej extern char *__progname; /* from crt0.o */
103 1.5 deraadt
104 1.12 lukem int main __P((int, char **));
105 1.12 lukem
106 1.12 lukem int
107 1.1 cgd main(argc, argv)
108 1.1 cgd int argc;
109 1.1 cgd char **argv;
110 1.1 cgd {
111 1.10 tls int ch;
112 1.5 deraadt char *username;
113 1.16 aidan char optstring[64]; /* if we ever get more than 64 args, shoot me. */
114 1.16 aidan const char *curopt, *optopt;
115 1.16 aidan int i, j;
116 1.16 aidan int valid;
117 1.16 aidan int use_always;
118 1.16 aidan
119 1.16 aidan /* allow passwd modules to do argv[0] specific processing */
120 1.16 aidan use_always = 0;
121 1.16 aidan valid = 0;
122 1.16 aidan for (i = 0; pw_modules[i].pw_init != NULL; i++) {
123 1.16 aidan pw_modules[i].invalid = 0;
124 1.16 aidan if (pw_modules[i].argv0) {
125 1.16 aidan /*
126 1.16 aidan * If we have a module that matches this progname, be
127 1.16 aidan * sure that no modules but those that match this
128 1.16 aidan * progname can be used. If we have a module that
129 1.16 aidan * matches against a particular progname, but does NOT
130 1.16 aidan * match this one, don't use that module.
131 1.16 aidan */
132 1.16 aidan if ((strcmp(__progname, pw_modules[i].argv0) == 0) &&
133 1.16 aidan use_always == 0) {
134 1.16 aidan for (j = 0; j < i; j++) {
135 1.16 aidan pw_modules[j].invalid |= INIT_INVALID;
136 1.16 aidan (*pw_modules[j].pw_end)();
137 1.16 aidan }
138 1.16 aidan use_always = 1;
139 1.16 aidan } else if (use_always == 0)
140 1.16 aidan pw_modules[i].invalid |= INIT_INVALID;
141 1.16 aidan } else if (use_always)
142 1.16 aidan pw_modules[i].invalid |= INIT_INVALID;
143 1.16 aidan
144 1.16 aidan if (pw_modules[i].invalid)
145 1.16 aidan continue;
146 1.16 aidan
147 1.16 aidan pw_modules[i].invalid |= (*pw_modules[i].pw_init)(__progname) ?
148 1.16 aidan /* zero on success, non-zero on error */
149 1.16 aidan INIT_INVALID : 0;
150 1.16 aidan
151 1.16 aidan if (! pw_modules[i].invalid)
152 1.16 aidan valid = 1;
153 1.16 aidan }
154 1.7 phil
155 1.16 aidan if (valid == 0)
156 1.16 aidan errx(1, "Can't change password.");
157 1.7 phil
158 1.16 aidan /* Build the option string from the individual modules' option
159 1.16 aidan * strings. Note that two modules can share a single option
160 1.16 aidan * letter. */
161 1.16 aidan optstring[0] = '\0';
162 1.16 aidan j = 0;
163 1.16 aidan for (i = 0; pw_modules[i].pw_init != NULL; i++) {
164 1.16 aidan if (pw_modules[i].invalid)
165 1.16 aidan continue;
166 1.16 aidan
167 1.16 aidan curopt = pw_modules[i].args;
168 1.16 aidan while (*curopt != '\0') {
169 1.17 joda if ((optopt = strchr(optstring, *curopt)) == NULL) {
170 1.16 aidan optstring[j++] = *curopt;
171 1.16 aidan if (curopt[1] == ':') {
172 1.16 aidan curopt++;
173 1.16 aidan optstring[j++] = *curopt;
174 1.16 aidan }
175 1.16 aidan optstring[j] = '\0';
176 1.16 aidan } else if ((optopt[1] == ':' && curopt[1] != ':') ||
177 1.16 aidan (optopt[1] != ':' && curopt[1] == ':')) {
178 1.16 aidan errx(1, "NetBSD ERROR! Different password "
179 1.16 aidan "modules have two different ideas about "
180 1.16 aidan "%c argument format.", curopt[0]);
181 1.16 aidan }
182 1.16 aidan curopt++;
183 1.16 aidan }
184 1.7 phil }
185 1.7 phil
186 1.16 aidan while ((ch = getopt(argc, argv, optstring)) != -1)
187 1.16 aidan {
188 1.16 aidan valid = 0;
189 1.16 aidan for (i = 0; pw_modules[i].pw_init != NULL; i++) {
190 1.16 aidan if (pw_modules[i].invalid)
191 1.16 aidan continue;
192 1.17 joda if ((optopt = strchr(pw_modules[i].args, ch)) != NULL) {
193 1.16 aidan j = (optopt[1] == ':') ?
194 1.16 aidan ! (*pw_modules[i].pw_arg)(ch, optarg) :
195 1.17 joda ! (*pw_modules[i].pw_arg)(ch, NULL);
196 1.16 aidan if (j != 0)
197 1.16 aidan pw_modules[i].invalid |= ARG_INVALID;
198 1.16 aidan if (pw_modules[i].invalid)
199 1.16 aidan (*pw_modules[i].pw_end)();
200 1.16 aidan } else {
201 1.16 aidan /* arg doesn't match this module */
202 1.16 aidan pw_modules[i].invalid |= ARG_INVALID;
203 1.16 aidan (*pw_modules[i].pw_end)();
204 1.16 aidan }
205 1.16 aidan if (! pw_modules[i].invalid)
206 1.16 aidan valid = 1;
207 1.16 aidan }
208 1.16 aidan if (! valid) {
209 1.1 cgd usage();
210 1.16 aidan exit(1);
211 1.16 aidan }
212 1.16 aidan }
213 1.16 aidan
214 1.16 aidan /* select which module to use to actually change the password. */
215 1.16 aidan use_always = 0;
216 1.16 aidan valid = 0;
217 1.16 aidan for (i = 0; pw_modules[i].pw_init != NULL; i++)
218 1.16 aidan if (! pw_modules[i].invalid) {
219 1.16 aidan pw_modules[i].use_class = (*pw_modules[i].pw_arg_end)();
220 1.16 aidan if (pw_modules[i].use_class != PW_DONT_USE)
221 1.16 aidan valid = 1;
222 1.16 aidan if (pw_modules[i].use_class == PW_USE_FORCE)
223 1.16 aidan use_always = 1;
224 1.1 cgd }
225 1.1 cgd
226 1.16 aidan
227 1.16 aidan if (! valid)
228 1.16 aidan /* hang the DJ */
229 1.16 aidan errx(1, "No valid password module specified.");
230 1.16 aidan
231 1.1 cgd argc -= optind;
232 1.1 cgd argv += optind;
233 1.1 cgd
234 1.5 deraadt username = getlogin();
235 1.8 thorpej if (username == NULL)
236 1.8 thorpej errx(1, "who are you ??");
237 1.5 deraadt
238 1.1 cgd switch(argc) {
239 1.1 cgd case 0:
240 1.1 cgd break;
241 1.1 cgd case 1:
242 1.5 deraadt username = argv[0];
243 1.1 cgd break;
244 1.1 cgd default:
245 1.1 cgd usage();
246 1.1 cgd exit(1);
247 1.1 cgd }
248 1.1 cgd
249 1.16 aidan /* allow for fallback to other chpw() methods. */
250 1.16 aidan for (i = 0; pw_modules[i].pw_init != NULL; i++) {
251 1.16 aidan if (pw_modules[i].invalid)
252 1.16 aidan continue;
253 1.16 aidan if ((use_always && pw_modules[i].use_class == PW_USE_FORCE) ||
254 1.16 aidan (!use_always && pw_modules[i].use_class == PW_USE)) {
255 1.16 aidan valid = (*pw_modules[i].pw_chpw)(username);
256 1.16 aidan (*pw_modules[i].pw_end)();
257 1.16 aidan if (valid >= 0)
258 1.16 aidan exit(valid);
259 1.16 aidan /* return value < 0 indicates continuation. */
260 1.16 aidan }
261 1.16 aidan }
262 1.16 aidan exit(1);
263 1.1 cgd }
264 1.1 cgd
265 1.10 tls void
266 1.1 cgd usage()
267 1.1 cgd {
268 1.16 aidan int i;
269 1.8 thorpej
270 1.16 aidan fprintf(stderr, "usage:\n");
271 1.16 aidan for (i = 0; pw_modules[i].pw_init != NULL; i++)
272 1.16 aidan if (! (pw_modules[i].invalid & INIT_INVALID))
273 1.16 aidan fprintf(stderr, "\t%s %s [user]\n", __progname,
274 1.16 aidan pw_modules[i].usage);
275 1.8 thorpej exit(1);
276 1.1 cgd }
277