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