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