Home | History | Annotate | Line # | Download | only in passwd
local_passwd.c revision 1.29
      1 /*	$NetBSD: local_passwd.c,v 1.29 2005/01/12 03:34:58 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "from: @(#)local_passwd.c    8.3 (Berkeley) 4/2/94";
     36 #else
     37 __RCSID("$NetBSD: local_passwd.c,v 1.29 2005/01/12 03:34:58 christos Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <sys/types.h>
     42 #include <sys/stat.h>
     43 #include <ctype.h>
     44 #include <err.h>
     45 #include <errno.h>
     46 #include <fcntl.h>
     47 #include <pwd.h>
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <string.h>
     51 #include <limits.h>
     52 #include <time.h>
     53 #include <unistd.h>
     54 #include <util.h>
     55 #include <login_cap.h>
     56 
     57 #include "extern.h"
     58 
     59 static	char   *getnewpasswd __P((struct passwd *, int));
     60 
     61 static uid_t uid;
     62 static int force_local;
     63 
     64 char *tempname;
     65 
     66 static char *
     67 getnewpasswd(pw, min_pw_len)
     68 	struct passwd *pw;
     69 	int min_pw_len;
     70 {
     71 	int tries;
     72 	char *p, *t;
     73 	char buf[_PASSWORD_LEN+1], salt[_PASSWORD_LEN+1];
     74 	char option[LINE_MAX], *key, *opt;
     75 
     76 	(void)printf("Changing local password for %s.\n", pw->pw_name);
     77 
     78 	if (uid && pw->pw_passwd[0] &&
     79 	    strcmp(crypt(getpass("Old password:"), pw->pw_passwd),
     80 	    pw->pw_passwd)) {
     81 		errno = EACCES;
     82 		pw_error(NULL, 1, 1);
     83 	}
     84 
     85 	for (buf[0] = '\0', tries = 0;;) {
     86 		p = getpass("New password:");
     87 		if (!*p) {
     88 			(void)printf("Password unchanged.\n");
     89 			pw_error(NULL, 0, 0);
     90 		}
     91 		if (min_pw_len > 0 && strlen(p) < min_pw_len) {
     92 			(void) printf("Password is too short.\n");
     93 			continue;
     94 		}
     95 		if (strlen(p) <= 5 && ++tries < 2) {
     96 			(void)printf("Please enter a longer password.\n");
     97 			continue;
     98 		}
     99 		for (t = p; *t && islower((unsigned char)*t); ++t);
    100 		if (!*t && ++tries < 2) {
    101 			(void)printf("Please don't use an all-lower case "
    102 				     "password.\nUnusual capitalization, "
    103 				     "control characters or digits are "
    104 				     "suggested.\n");
    105 			continue;
    106 		}
    107 		(void)strlcpy(buf, p, sizeof(buf));
    108 		if (!strcmp(buf, getpass("Retype new password:")))
    109 			break;
    110 		(void)printf("Mismatch; try again, EOF to quit.\n");
    111 	}
    112 
    113 	pw_getpwconf(option, sizeof(option), pw, "localcipher");
    114 	opt = option;
    115 	key = strsep(&opt, ",");
    116 	if(pw_gensalt(salt, _PASSWORD_LEN, key, opt) == -1) {
    117 		warn("Couldn't generate salt");
    118 		pw_error(NULL, 0, 0);
    119 	}
    120 	return(crypt(buf, salt));
    121 }
    122 
    123 int
    124 local_init(progname)
    125 	const char *progname;
    126 {
    127 	force_local = 0;
    128 	return (0);
    129 }
    130 
    131 int
    132 local_arg(char arg, const char *optarg)
    133 {
    134 	switch (arg) {
    135 	case 'l':
    136 		force_local = 1;
    137 		break;
    138 	default:
    139 		return(0);
    140 	}
    141 	return(1);
    142 }
    143 
    144 int
    145 local_arg_end()
    146 {
    147 	if (force_local)
    148 		return(PW_USE_FORCE);
    149 	return(PW_USE);
    150 }
    151 
    152 void
    153 local_end()
    154 {
    155 	/* NOOP */
    156 }
    157 
    158 int
    159 local_chpw(uname)
    160 	const char *uname;
    161 {
    162 	struct passwd *pw;
    163 	struct passwd old_pw;
    164 	time_t old_change;
    165 	int pfd, tfd;
    166 	int min_pw_len = 0;
    167 	int pw_expiry  = 0;
    168 #ifdef LOGIN_CAP
    169 	login_cap_t *lc;
    170 #endif
    171 
    172 	if (!(pw = getpwnam(uname))) {
    173 		warnx("unknown user %s", uname);
    174 		return (1);
    175 	}
    176 
    177 	uid = getuid();
    178 	if (uid && uid != pw->pw_uid) {
    179 		warnx("%s", strerror(EACCES));
    180 		return (1);
    181 	}
    182 
    183 	/* Save the old pw information for comparing on pw_copy(). */
    184 	old_pw = *pw;
    185 
    186 	/*
    187 	 * Get class restrictions for this user, then get the new password.
    188 	 */
    189 #ifdef LOGIN_CAP
    190 	if((lc = login_getclass(pw->pw_class))) {
    191 		min_pw_len = (int) login_getcapnum(lc, "minpasswordlen", 0, 0);
    192 		pw_expiry  = (int) login_getcaptime(lc, "passwordtime", 0, 0);
    193 		login_close(lc);
    194 	}
    195 #endif
    196 
    197 	pw->pw_passwd = getnewpasswd(pw, min_pw_len);
    198 	old_change = pw->pw_change;
    199 	pw->pw_change = pw_expiry ? pw_expiry + time(NULL) : 0;
    200 
    201 	/*
    202 	 * Now that the user has given us a new password, let us
    203 	 * change the database.
    204 	 */
    205 	pw_init();
    206 	tfd = pw_lock(0);
    207 	if (tfd < 0) {
    208 		warnx ("The passwd file is busy, waiting...");
    209 		tfd = pw_lock(10);
    210 		if (tfd < 0)
    211 			errx(1, "The passwd file is still busy, "
    212 			     "try again later.");
    213 	}
    214 
    215 	pfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0);
    216 	if (pfd < 0)
    217 		pw_error(_PATH_MASTERPASSWD, 1, 1);
    218 
    219 	pw_copy(pfd, tfd, pw, &old_pw);
    220 
    221 	if (pw_mkdb(uname, old_change == pw->pw_change) < 0)
    222 		pw_error((char *)NULL, 0, 1);
    223 	return (0);
    224 }
    225