1 /*- 2 * Copyright (c) 1988, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (c) 2002 Networks Associates Technology, Inc. 5 * All rights reserved. 6 * 7 * Portions of this software were developed for the FreeBSD Project by 8 * ThinkSec AS and NAI Labs, the Security Research Division of Network 9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 10 * ("CBOSS"), as part of the DARPA CHATS research program. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 */ 40 41 #if 0 42 #ifndef lint 43 static const char copyright[] = 44 "@(#) Copyright (c) 1988, 1993, 1994\n\ 45 The Regents of the University of California. All rights reserved.\n"; 46 #endif /* not lint */ 47 48 #ifndef lint 49 static char sccsid[] = "@(#)chpass.c 8.4 (Berkeley) 4/2/94"; 50 #endif /* not lint */ 51 #endif 52 #include <sys/cdefs.h> 53 __FBSDID("$FreeBSD$"); 54 55 #include <sys/param.h> 56 57 #include <err.h> 58 #include <errno.h> 59 #include <pwd.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <unistd.h> 64 #ifdef YP 65 #include <ypclnt.h> 66 #endif 67 68 #include <pw_scan.h> 69 #include <libutil.h> 70 71 #include "chpass.h" 72 73 int master_mode; 74 75 static void baduser(void); 76 static void usage(void); 77 78 int 79 main(int argc, char *argv[]) 80 { 81 enum { NEWSH, LOADENTRY, EDITENTRY, NEWPW, NEWEXP } op; 82 struct passwd lpw, *old_pw, *pw; 83 int ch, pfd, tfd; 84 const char *password; 85 char *arg = NULL; 86 uid_t uid; 87 #ifdef YP 88 struct ypclnt *ypclnt; 89 const char *yp_domain = NULL, *yp_host = NULL; 90 #endif 91 92 pw = old_pw = NULL; 93 op = EDITENTRY; 94 #ifdef YP 95 while ((ch = getopt(argc, argv, "a:p:s:e:d:h:loy")) != -1) 96 #else 97 while ((ch = getopt(argc, argv, "a:p:s:e:")) != -1) 98 #endif 99 switch (ch) { 100 case 'a': 101 op = LOADENTRY; 102 arg = optarg; 103 break; 104 case 's': 105 op = NEWSH; 106 arg = optarg; 107 break; 108 case 'p': 109 op = NEWPW; 110 arg = optarg; 111 break; 112 case 'e': 113 op = NEWEXP; 114 arg = optarg; 115 break; 116 #ifdef YP 117 case 'd': 118 yp_domain = optarg; 119 break; 120 case 'h': 121 yp_host = optarg; 122 break; 123 case 'l': 124 case 'o': 125 case 'y': 126 /* compatibility */ 127 break; 128 #endif 129 case '?': 130 default: 131 usage(); 132 } 133 134 argc -= optind; 135 argv += optind; 136 137 if (argc > 1) 138 usage(); 139 140 uid = getuid(); 141 142 if (op == EDITENTRY || op == NEWSH || op == NEWPW || op == NEWEXP) { 143 if (argc == 0) { 144 if ((pw = getpwuid(uid)) == NULL) 145 errx(1, "unknown user: uid %lu", 146 (unsigned long)uid); 147 } else { 148 if ((pw = getpwnam(*argv)) == NULL) 149 errx(1, "unknown user: %s", *argv); 150 if (uid != 0 && uid != pw->pw_uid) 151 baduser(); 152 } 153 154 /* Make a copy for later verification */ 155 if ((pw = pw_dup(pw)) == NULL || 156 (old_pw = pw_dup(pw)) == NULL) 157 err(1, "pw_dup"); 158 } 159 160 #ifdef YP 161 if (pw != NULL && (pw->pw_fields & _PWF_SOURCE) == _PWF_NIS) { 162 ypclnt = ypclnt_new(yp_domain, "passwd.byname", yp_host); 163 master_mode = (ypclnt != NULL && 164 ypclnt_connect(ypclnt) != -1 && 165 ypclnt_havepasswdd(ypclnt) == 1); 166 ypclnt_free(ypclnt); 167 } else 168 #endif 169 master_mode = (uid == 0); 170 171 if (op == NEWSH) { 172 /* protect p_shell -- it thinks NULL is /bin/sh */ 173 if (!arg[0]) 174 usage(); 175 if (p_shell(arg, pw, (ENTRY *)NULL) == -1) 176 exit(1); 177 } 178 179 if (op == NEWEXP) { 180 if (uid) /* only root can change expire */ 181 baduser(); 182 if (p_expire(arg, pw, (ENTRY *)NULL) == -1) 183 exit(1); 184 } 185 186 if (op == LOADENTRY) { 187 if (uid) 188 baduser(); 189 pw = &lpw; 190 old_pw = NULL; 191 if (!__pw_scan(arg, pw, _PWSCAN_WARN|_PWSCAN_MASTER)) 192 exit(1); 193 } 194 195 if (op == NEWPW) { 196 if (uid) 197 baduser(); 198 199 if (strchr(arg, ':')) 200 errx(1, "invalid format for password"); 201 pw->pw_passwd = arg; 202 } 203 204 if (op == EDITENTRY) { 205 /* 206 * We don't really need pw_*() here, but pw_edit() (used 207 * by edit()) is just too useful... 208 */ 209 if (pw_init(NULL, NULL)) 210 err(1, "pw_init()"); 211 if ((tfd = pw_tmp(-1)) == -1) { 212 pw_fini(); 213 err(1, "pw_tmp()"); 214 } 215 free(pw); 216 pw = edit(pw_tempname(), old_pw); 217 pw_fini(); 218 if (pw == NULL) 219 err(1, "edit()"); 220 /* 221 * pw_equal does not check for crypted passwords, so we 222 * should do it explicitly 223 */ 224 if (pw_equal(old_pw, pw) && 225 strcmp(old_pw->pw_passwd, pw->pw_passwd) == 0) 226 errx(0, "user information unchanged"); 227 } 228 229 if (old_pw && !master_mode) { 230 password = getpass("Password: "); 231 if (strcmp(crypt(password, old_pw->pw_passwd), 232 old_pw->pw_passwd) != 0) 233 baduser(); 234 } else { 235 password = ""; 236 } 237 238 if (old_pw != NULL) 239 pw->pw_fields |= (old_pw->pw_fields & _PWF_SOURCE); 240 switch (pw->pw_fields & _PWF_SOURCE) { 241 #ifdef YP 242 case _PWF_NIS: 243 ypclnt = ypclnt_new(yp_domain, "passwd.byname", yp_host); 244 if (ypclnt == NULL || 245 ypclnt_connect(ypclnt) == -1 || 246 ypclnt_passwd(ypclnt, pw, password) == -1) { 247 warnx("%s", ypclnt->error); 248 ypclnt_free(ypclnt); 249 exit(1); 250 } 251 ypclnt_free(ypclnt); 252 errx(0, "NIS user information updated"); 253 #endif /* YP */ 254 case 0: 255 case _PWF_FILES: 256 if (pw_init(NULL, NULL)) 257 err(1, "pw_init()"); 258 if ((pfd = pw_lock()) == -1) { 259 pw_fini(); 260 err(1, "pw_lock()"); 261 } 262 if ((tfd = pw_tmp(-1)) == -1) { 263 pw_fini(); 264 err(1, "pw_tmp()"); 265 } 266 if (pw_copy(pfd, tfd, pw, old_pw) == -1) { 267 pw_fini(); 268 err(1, "pw_copy"); 269 } 270 if (pw_mkdb(pw->pw_name) == -1) { 271 pw_fini(); 272 err(1, "pw_mkdb()"); 273 } 274 pw_fini(); 275 errx(0, "user information updated"); 276 break; 277 default: 278 errx(1, "unsupported passwd source"); 279 } 280 } 281 282 static void 283 baduser(void) 284 { 285 286 errx(1, "%s", strerror(EACCES)); 287 } 288 289 static void 290 usage(void) 291 { 292 293 (void)fprintf(stderr, 294 "usage: chpass%s %s [user]\n", 295 #ifdef YP 296 " [-d domain] [-h host]", 297 #else 298 "", 299 #endif 300 "[-a list] [-p encpass] [-s shell] [-e mmm dd yy]"); 301 exit(1); 302 } 303