1 /* $NetBSD: passwd.c,v 1.28 2000/12/08 21:41:14 tron Exp $ */ 2 3 /* 4 * Copyright (c) 1987, 1993, 1994, 1995 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 #if defined(LIBC_SCCS) && !defined(lint) 38 __RCSID("$NetBSD: passwd.c,v 1.28 2000/12/08 21:41:14 tron Exp $"); 39 #endif /* LIBC_SCCS and not lint */ 40 41 #include <sys/types.h> 42 #include <sys/param.h> 43 #include <sys/stat.h> 44 #include <sys/time.h> 45 #include <sys/resource.h> 46 #include <sys/wait.h> 47 48 #include <assert.h> 49 #include <ctype.h> 50 #include <err.h> 51 #include <errno.h> 52 #include <fcntl.h> 53 #include <limits.h> 54 #include <paths.h> 55 #include <pwd.h> 56 #include <signal.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <unistd.h> 61 #include <util.h> 62 63 static const char *pw_filename(const char *filename); 64 static void pw_cont(int sig); 65 static int pw_equal(char *buf, struct passwd *old_pw); 66 static const char *pw_default(const char *option); 67 static int read_line(FILE *fp, char *line, int max); 68 static void trim_whitespace(char *line); 69 70 static char pw_prefix[MAXPATHLEN]; 71 72 const char * 73 pw_getprefix(void) 74 { 75 76 return(pw_prefix); 77 } 78 79 int 80 pw_setprefix(const char *new_prefix) 81 { 82 size_t length; 83 84 length = strlen(new_prefix); 85 if (length < sizeof(pw_prefix)) { 86 (void)strcpy(pw_prefix, new_prefix); 87 while (length > 0 && pw_prefix[length - 1] == '/') 88 pw_prefix[--length] = '\0'; 89 return(0); 90 } 91 errno = ENAMETOOLONG; 92 return(-1); 93 } 94 95 static const char * 96 pw_filename(const char *filename) 97 { 98 static char newfilename[MAXPATHLEN]; 99 100 if (pw_prefix[0] == '\0') 101 return filename; 102 103 if (strlen(pw_prefix) + strlen(filename) < sizeof(newfilename)) 104 return strcat(strcpy(newfilename, pw_prefix), filename); 105 106 errno = ENAMETOOLONG; 107 return(NULL); 108 } 109 110 int 111 pw_lock(int retries) 112 { 113 const char *filename; 114 int i, fd; 115 mode_t old_mode; 116 int oerrno; 117 118 /* Acquire the lock file. */ 119 filename = pw_filename(_PATH_MASTERPASSWD_LOCK); 120 if (filename == NULL) 121 return(-1); 122 old_mode = umask(0); 123 fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, 0600); 124 for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) { 125 sleep(1); 126 fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, 127 0600); 128 } 129 oerrno = errno; 130 (void)umask(old_mode); 131 errno = oerrno; 132 return(fd); 133 } 134 135 int 136 pw_mkdb(void) 137 { 138 int pstat; 139 pid_t pid; 140 141 pid = vfork(); 142 if (pid == -1) 143 return (-1); 144 145 if (pid == 0) { 146 execl(_PATH_PWD_MKDB, "pwd_mkdb", "-d", pw_prefix, 147 "-p", pw_filename(_PATH_MASTERPASSWD_LOCK), NULL); 148 _exit(1); 149 } 150 pid = waitpid(pid, &pstat, 0); 151 if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0) 152 return(-1); 153 return(0); 154 } 155 156 int 157 pw_abort(void) 158 { 159 const char *filename; 160 161 filename = pw_filename(_PATH_MASTERPASSWD_LOCK); 162 return((filename == NULL) ? -1 : unlink(filename)); 163 } 164 165 /* Everything below this point is intended for the convenience of programs 166 * which allow a user to interactively edit the passwd file. Errors in the 167 * routines below will cause the process to abort. */ 168 169 static pid_t editpid = -1; 170 171 static void 172 pw_cont(int sig) 173 { 174 175 if (editpid != -1) 176 kill(editpid, sig); 177 } 178 179 void 180 pw_init(void) 181 { 182 struct rlimit rlim; 183 184 /* Unlimited resource limits. */ 185 rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY; 186 (void)setrlimit(RLIMIT_CPU, &rlim); 187 (void)setrlimit(RLIMIT_FSIZE, &rlim); 188 (void)setrlimit(RLIMIT_STACK, &rlim); 189 (void)setrlimit(RLIMIT_DATA, &rlim); 190 (void)setrlimit(RLIMIT_RSS, &rlim); 191 192 /* Don't drop core (not really necessary, but GP's). */ 193 rlim.rlim_cur = rlim.rlim_max = 0; 194 (void)setrlimit(RLIMIT_CORE, &rlim); 195 196 /* Turn off signals. */ 197 (void)signal(SIGALRM, SIG_IGN); 198 (void)signal(SIGHUP, SIG_IGN); 199 (void)signal(SIGINT, SIG_IGN); 200 (void)signal(SIGPIPE, SIG_IGN); 201 (void)signal(SIGQUIT, SIG_IGN); 202 (void)signal(SIGTERM, SIG_IGN); 203 (void)signal(SIGCONT, pw_cont); 204 } 205 206 void 207 pw_edit(int notsetuid, const char *filename) 208 { 209 int pstat; 210 char *p, *editor; 211 char *argp[] = { "sh", "-c", NULL, NULL }; 212 213 #ifdef __GNUC__ 214 (void) &editor; 215 #endif 216 217 if (filename == NULL) 218 filename = _PATH_MASTERPASSWD_LOCK; 219 220 filename = pw_filename(filename); 221 if (filename == NULL) 222 return; 223 224 if ((editor = getenv("EDITOR")) == NULL) 225 editor = _PATH_VI; 226 227 p = malloc(strlen(editor) + 1 + strlen(filename) + 1); 228 if (p == NULL) 229 return; 230 231 sprintf(p, "%s %s", editor, filename); 232 argp[2] = p; 233 234 switch(editpid = vfork()) { 235 case -1: 236 free(p); 237 return; 238 case 0: 239 if (notsetuid) { 240 setgid(getgid()); 241 setuid(getuid()); 242 } 243 execvp(_PATH_BSHELL, argp); 244 _exit(1); 245 } 246 247 free(p); 248 249 for (;;) { 250 editpid = waitpid(editpid, (int *)&pstat, WUNTRACED); 251 if (editpid == -1) 252 pw_error(editor, 1, 1); 253 else if (WIFSTOPPED(pstat)) 254 raise(WSTOPSIG(pstat)); 255 else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0) 256 break; 257 else 258 pw_error(editor, 1, 1); 259 } 260 editpid = -1; 261 } 262 263 void 264 pw_prompt(void) 265 { 266 int c; 267 268 (void)printf("re-edit the password file? [y]: "); 269 (void)fflush(stdout); 270 c = getchar(); 271 if (c != EOF && c != '\n') 272 while (getchar() != '\n'); 273 if (c == 'n') 274 pw_error(NULL, 0, 0); 275 } 276 277 /* for use in pw_copy(). Compare a pw entry to a pw struct. */ 278 static int 279 pw_equal(char *buf, struct passwd *pw) 280 { 281 struct passwd buf_pw; 282 int len; 283 284 _DIAGASSERT(buf != NULL); 285 _DIAGASSERT(pw != NULL); 286 287 len = strlen (buf); 288 if (buf[len-1] == '\n') 289 buf[len-1] = '\0'; 290 if (!pw_scan(buf, &buf_pw, NULL)) 291 return 0; 292 return !strcmp(pw->pw_name, buf_pw.pw_name) 293 && pw->pw_uid == buf_pw.pw_uid 294 && pw->pw_gid == buf_pw.pw_gid 295 && !strcmp(pw->pw_class, buf_pw.pw_class) 296 && (long)pw->pw_change == (long)buf_pw.pw_change 297 && (long)pw->pw_expire == (long)buf_pw.pw_expire 298 && !strcmp(pw->pw_gecos, buf_pw.pw_gecos) 299 && !strcmp(pw->pw_dir, buf_pw.pw_dir) 300 && !strcmp(pw->pw_shell, buf_pw.pw_shell); 301 } 302 303 void 304 pw_copy(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw) 305 { 306 const char *filename; 307 char mpwd[MAXPATHLEN], mpwdl[MAXPATHLEN], *p, buf[8192]; 308 FILE *from, *to; 309 int done; 310 311 _DIAGASSERT(pw != NULL); 312 /* old_pw may be NULL */ 313 314 if ((filename = pw_filename(_PATH_MASTERPASSWD)) == NULL) 315 pw_error(pw_prefix, 1,1); 316 (void)strcpy(mpwd, filename); 317 if ((filename = pw_filename(_PATH_MASTERPASSWD_LOCK)) == NULL) 318 pw_error(pw_prefix, 1,1); 319 (void)strcpy(mpwdl, filename); 320 321 if (!(from = fdopen(ffd, "r"))) 322 pw_error(mpwd, 1, 1); 323 if (!(to = fdopen(tfd, "w"))) 324 pw_error(mpwdl, 1, 1); 325 326 for (done = 0; fgets(buf, sizeof(buf), from);) { 327 if (!strchr(buf, '\n')) { 328 warnx("%s: line too long", mpwd); 329 pw_error(NULL, 0, 1); 330 } 331 if (done) { 332 (void)fprintf(to, "%s", buf); 333 if (ferror(to)) 334 goto err; 335 continue; 336 } 337 if (!(p = strchr(buf, ':'))) { 338 warnx("%s: corrupted entry", mpwd); 339 pw_error(NULL, 0, 1); 340 } 341 *p = '\0'; 342 if (strcmp(buf, pw->pw_name)) { 343 *p = ':'; 344 (void)fprintf(to, "%s", buf); 345 if (ferror(to)) 346 goto err; 347 continue; 348 } 349 *p = ':'; 350 if (old_pw && !pw_equal(buf, old_pw)) { 351 warnx("%s: entry inconsistent", mpwd); 352 pw_error(NULL, 0, 1); 353 } 354 (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", 355 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, 356 pw->pw_class, (long)pw->pw_change, (long)pw->pw_expire, 357 pw->pw_gecos, pw->pw_dir, pw->pw_shell); 358 done = 1; 359 if (ferror(to)) 360 goto err; 361 } 362 /* Only append a new entry if real uid is root! */ 363 if (!done) { 364 if (getuid() == 0) 365 (void)fprintf(to, "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", 366 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, 367 pw->pw_class, (long)pw->pw_change, 368 (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir, 369 pw->pw_shell); 370 else 371 warnx("%s: changes not made, no such entry", mpwd); 372 } 373 374 if (ferror(to)) 375 err: pw_error(NULL, 1, 1); 376 (void)fclose(to); 377 } 378 379 void 380 pw_error(const char *name, int err, int eval) 381 { 382 383 if (err) { 384 if (name) 385 warn("%s", name); 386 else 387 warn(NULL); 388 } 389 390 warnx("%s%s: unchanged", pw_prefix, _PATH_MASTERPASSWD); 391 pw_abort(); 392 exit(eval); 393 } 394 395 /* Removes head and/or tail spaces. */ 396 static void 397 trim_whitespace(char *line) 398 { 399 char *p; 400 401 /* Remove leading spaces */ 402 p = line; 403 while (isspace((unsigned char) *p)) 404 p++; 405 memmove(line, p, strlen(p) + 1); 406 407 /* Remove trailing spaces */ 408 p = line + strlen(line) - 1; 409 while (isspace((unsigned char) *p)) 410 p--; 411 *(p + 1) = '\0'; 412 } 413 414 415 /* Get one line, remove spaces from front and tail */ 416 static int 417 read_line(FILE *fp, char *line, int max) 418 { 419 char *p; 420 421 /* Read one line of config */ 422 if (fgets(line, max, fp) == NULL) 423 return (0); 424 425 if ((p = strchr(line, '\n')) == NULL) { 426 warnx("line too long"); 427 return (0); 428 } 429 *p = '\0'; 430 431 /* Remove comments */ 432 if ((p = strchr(line, '#')) != NULL) 433 *p = '\0'; 434 435 trim_whitespace(line); 436 return (1); 437 } 438 439 static const char * 440 pw_default(const char *option) 441 { 442 static const char *options[][2] = { 443 { "localcipher", "old" }, 444 { "ypcipher", "old" }, 445 }; 446 int i; 447 448 for (i = 0; i < sizeof(options) / sizeof(options[0]); i++) 449 if (strcmp(options[i][0], option) == 0) 450 return (options[i][1]); 451 452 return (NULL); 453 } 454 455 /* 456 * Retrieve password information from the /etc/passwd.conf file, at the 457 * moment this is only for choosing the cipher to use. It could easily be 458 * used for other authentication methods as well. 459 */ 460 void 461 pw_getconf(char *data, size_t max, const char *key, const char *option) 462 { 463 FILE *fp; 464 char line[LINE_MAX], *p, *p2; 465 static char result[LINE_MAX]; 466 int got, found; 467 const char *cp; 468 469 got = 0; 470 found = 0; 471 result[0] = '\0'; 472 473 if ((fp = fopen(_PATH_PASSWDCONF, "r")) == NULL) { 474 if ((cp = pw_default(option)) != NULL) 475 strlcpy(data, cp, max); 476 else 477 data[0] = '\0'; 478 return; 479 } 480 481 while (!found && (got || read_line(fp, line, LINE_MAX))) { 482 got = 0; 483 484 if (strncmp(key, line, strlen(key)) != 0 || 485 line[strlen(key)] != ':') 486 continue; 487 488 /* Now we found our specified key */ 489 while (read_line(fp, line, LINE_MAX)) { 490 /* Leaving key field */ 491 if (line[0] != '\0' && strchr(line + 1, ':') != NULL) { 492 got = 1; 493 break; 494 } 495 p2 = line; 496 if ((p = strsep(&p2, "=")) == NULL || p2 == NULL) 497 continue; 498 trim_whitespace(p); 499 500 if (!strncmp(p, option, strlen(option))) { 501 trim_whitespace(p2); 502 strcpy(result, p2); 503 found = 1; 504 break; 505 } 506 } 507 } 508 fclose(fp); 509 510 /* 511 * If we got no result and were looking for a default 512 * value, try hard coded defaults. 513 */ 514 515 if (strlen(result) == 0 && strcmp(key, "default") == 0 && 516 (cp = pw_default(option)) != NULL) 517 strlcpy(data, cp, max); 518 else 519 strlcpy(data, result, max); 520 } 521