1 1.53 kamil /* $NetBSD: passwd.c,v 1.53 2018/06/24 01:53:14 kamil Exp $ */ 2 1.8 christos 3 1.1 jtc /* 4 1.1 jtc * Copyright (c) 1987, 1993, 1994, 1995 5 1.1 jtc * The Regents of the University of California. All rights reserved. 6 1.1 jtc * 7 1.1 jtc * Redistribution and use in source and binary forms, with or without 8 1.1 jtc * modification, are permitted provided that the following conditions 9 1.1 jtc * are met: 10 1.1 jtc * 1. Redistributions of source code must retain the above copyright 11 1.1 jtc * notice, this list of conditions and the following disclaimer. 12 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jtc * notice, this list of conditions and the following disclaimer in the 14 1.1 jtc * documentation and/or other materials provided with the distribution. 15 1.35 agc * 3. Neither the name of the University nor the names of its contributors 16 1.1 jtc * may be used to endorse or promote products derived from this software 17 1.1 jtc * without specific prior written permission. 18 1.1 jtc * 19 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 1.1 jtc * SUCH DAMAGE. 30 1.1 jtc */ 31 1.1 jtc 32 1.8 christos #include <sys/cdefs.h> 33 1.1 jtc #if defined(LIBC_SCCS) && !defined(lint) 34 1.53 kamil __RCSID("$NetBSD: passwd.c,v 1.53 2018/06/24 01:53:14 kamil Exp $"); 35 1.1 jtc #endif /* LIBC_SCCS and not lint */ 36 1.1 jtc 37 1.1 jtc #include <sys/types.h> 38 1.27 tron #include <sys/param.h> 39 1.1 jtc #include <sys/stat.h> 40 1.1 jtc #include <sys/time.h> 41 1.1 jtc #include <sys/resource.h> 42 1.1 jtc #include <sys/wait.h> 43 1.1 jtc 44 1.16 lukem #include <assert.h> 45 1.3 thorpej #include <ctype.h> 46 1.5 mycroft #include <err.h> 47 1.12 lukem #include <errno.h> 48 1.1 jtc #include <fcntl.h> 49 1.12 lukem #include <limits.h> 50 1.12 lukem #include <paths.h> 51 1.12 lukem #include <pwd.h> 52 1.38 christos #include <grp.h> 53 1.12 lukem #include <signal.h> 54 1.12 lukem #include <stdio.h> 55 1.1 jtc #include <stdlib.h> 56 1.1 jtc #include <string.h> 57 1.12 lukem #include <unistd.h> 58 1.1 jtc #include <util.h> 59 1.1 jtc 60 1.27 tron static const char *pw_filename(const char *filename); 61 1.27 tron static void pw_cont(int sig); 62 1.46 christos static const char * pw_equal(char *buf, struct passwd *old_pw); 63 1.27 tron static const char *pw_default(const char *option); 64 1.27 tron static int read_line(FILE *fp, char *line, int max); 65 1.27 tron static void trim_whitespace(char *line); 66 1.27 tron 67 1.27 tron static char pw_prefix[MAXPATHLEN]; 68 1.27 tron 69 1.27 tron const char * 70 1.27 tron pw_getprefix(void) 71 1.27 tron { 72 1.27 tron 73 1.27 tron return(pw_prefix); 74 1.27 tron } 75 1.27 tron 76 1.27 tron int 77 1.27 tron pw_setprefix(const char *new_prefix) 78 1.27 tron { 79 1.27 tron size_t length; 80 1.27 tron 81 1.29 lukem _DIAGASSERT(new_prefix != NULL); 82 1.29 lukem 83 1.27 tron length = strlen(new_prefix); 84 1.27 tron if (length < sizeof(pw_prefix)) { 85 1.27 tron (void)strcpy(pw_prefix, new_prefix); 86 1.27 tron while (length > 0 && pw_prefix[length - 1] == '/') 87 1.27 tron pw_prefix[--length] = '\0'; 88 1.27 tron return(0); 89 1.27 tron } 90 1.27 tron errno = ENAMETOOLONG; 91 1.27 tron return(-1); 92 1.27 tron } 93 1.27 tron 94 1.27 tron static const char * 95 1.27 tron pw_filename(const char *filename) 96 1.27 tron { 97 1.27 tron static char newfilename[MAXPATHLEN]; 98 1.27 tron 99 1.29 lukem _DIAGASSERT(filename != NULL); 100 1.29 lukem 101 1.27 tron if (pw_prefix[0] == '\0') 102 1.27 tron return filename; 103 1.27 tron 104 1.27 tron if (strlen(pw_prefix) + strlen(filename) < sizeof(newfilename)) 105 1.27 tron return strcat(strcpy(newfilename, pw_prefix), filename); 106 1.27 tron 107 1.27 tron errno = ENAMETOOLONG; 108 1.27 tron return(NULL); 109 1.27 tron } 110 1.1 jtc 111 1.1 jtc int 112 1.20 ad pw_lock(int retries) 113 1.1 jtc { 114 1.27 tron const char *filename; 115 1.1 jtc int i, fd; 116 1.1 jtc mode_t old_mode; 117 1.14 christos int oerrno; 118 1.1 jtc 119 1.1 jtc /* Acquire the lock file. */ 120 1.27 tron filename = pw_filename(_PATH_MASTERPASSWD_LOCK); 121 1.27 tron if (filename == NULL) 122 1.27 tron return(-1); 123 1.1 jtc old_mode = umask(0); 124 1.27 tron fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, 0600); 125 1.1 jtc for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) { 126 1.1 jtc sleep(1); 127 1.27 tron fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, 128 1.1 jtc 0600); 129 1.1 jtc } 130 1.14 christos oerrno = errno; 131 1.14 christos (void)umask(old_mode); 132 1.14 christos errno = oerrno; 133 1.1 jtc return(fd); 134 1.1 jtc } 135 1.1 jtc 136 1.1 jtc int 137 1.52 abs pw_mkdb(const char *username, int secureonly) 138 1.1 jtc { 139 1.30 ad const char *args[9]; 140 1.30 ad int pstat, i; 141 1.1 jtc pid_t pid; 142 1.1 jtc 143 1.1 jtc pid = vfork(); 144 1.19 mjl if (pid == -1) 145 1.50 christos return -1; 146 1.19 mjl 147 1.1 jtc if (pid == 0) { 148 1.30 ad args[0] = "pwd_mkdb"; 149 1.30 ad args[1] = "-d"; 150 1.30 ad args[2] = pw_prefix; 151 1.49 christos args[3] = "-pl"; 152 1.30 ad i = 4; 153 1.30 ad 154 1.30 ad if (secureonly) 155 1.30 ad args[i++] = "-s"; 156 1.30 ad if (username != NULL) { 157 1.30 ad args[i++] = "-u"; 158 1.30 ad args[i++] = username; 159 1.30 ad } 160 1.30 ad 161 1.30 ad args[i++] = pw_filename(_PATH_MASTERPASSWD_LOCK); 162 1.30 ad args[i] = NULL; 163 1.37 christos execv(_PATH_PWD_MKDB, (char * const *)__UNCONST(args)); 164 1.11 thorpej _exit(1); 165 1.1 jtc } 166 1.1 jtc pid = waitpid(pid, &pstat, 0); 167 1.50 christos if (pid == -1) { 168 1.50 christos warn("error waiting for pid %lu", (unsigned long)pid); 169 1.50 christos return -1; 170 1.50 christos } 171 1.50 christos if (WIFEXITED(pstat)) { 172 1.50 christos if (WEXITSTATUS(pstat) != 0) { 173 1.51 christos warnx("pwd_mkdb exited with status %d", 174 1.50 christos WEXITSTATUS(pstat)); 175 1.50 christos return -1; 176 1.50 christos } 177 1.50 christos } else if (WIFSIGNALED(pstat)) { 178 1.50 christos warnx("pwd_mkdb exited with signal %d", WTERMSIG(pstat)); 179 1.50 christos return -1; 180 1.50 christos } 181 1.50 christos return 0; 182 1.1 jtc } 183 1.1 jtc 184 1.1 jtc int 185 1.20 ad pw_abort(void) 186 1.1 jtc { 187 1.27 tron const char *filename; 188 1.20 ad 189 1.27 tron filename = pw_filename(_PATH_MASTERPASSWD_LOCK); 190 1.27 tron return((filename == NULL) ? -1 : unlink(filename)); 191 1.1 jtc } 192 1.1 jtc 193 1.1 jtc /* Everything below this point is intended for the convenience of programs 194 1.1 jtc * which allow a user to interactively edit the passwd file. Errors in the 195 1.1 jtc * routines below will cause the process to abort. */ 196 1.1 jtc 197 1.1 jtc static pid_t editpid = -1; 198 1.1 jtc 199 1.1 jtc static void 200 1.20 ad pw_cont(int sig) 201 1.1 jtc { 202 1.1 jtc 203 1.1 jtc if (editpid != -1) 204 1.1 jtc kill(editpid, sig); 205 1.1 jtc } 206 1.1 jtc 207 1.1 jtc void 208 1.20 ad pw_init(void) 209 1.1 jtc { 210 1.1 jtc struct rlimit rlim; 211 1.1 jtc 212 1.1 jtc /* Unlimited resource limits. */ 213 1.1 jtc rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY; 214 1.1 jtc (void)setrlimit(RLIMIT_CPU, &rlim); 215 1.1 jtc (void)setrlimit(RLIMIT_FSIZE, &rlim); 216 1.1 jtc (void)setrlimit(RLIMIT_STACK, &rlim); 217 1.1 jtc (void)setrlimit(RLIMIT_DATA, &rlim); 218 1.1 jtc (void)setrlimit(RLIMIT_RSS, &rlim); 219 1.1 jtc 220 1.1 jtc /* Don't drop core (not really necessary, but GP's). */ 221 1.1 jtc rlim.rlim_cur = rlim.rlim_max = 0; 222 1.1 jtc (void)setrlimit(RLIMIT_CORE, &rlim); 223 1.1 jtc 224 1.1 jtc /* Turn off signals. */ 225 1.1 jtc (void)signal(SIGALRM, SIG_IGN); 226 1.1 jtc (void)signal(SIGHUP, SIG_IGN); 227 1.1 jtc (void)signal(SIGINT, SIG_IGN); 228 1.1 jtc (void)signal(SIGPIPE, SIG_IGN); 229 1.1 jtc (void)signal(SIGQUIT, SIG_IGN); 230 1.1 jtc (void)signal(SIGTERM, SIG_IGN); 231 1.1 jtc (void)signal(SIGCONT, pw_cont); 232 1.1 jtc } 233 1.1 jtc 234 1.1 jtc void 235 1.20 ad pw_edit(int notsetuid, const char *filename) 236 1.1 jtc { 237 1.19 mjl int pstat; 238 1.37 christos char *p; 239 1.44 christos const char * volatile editor; 240 1.37 christos const char *argp[] = { "sh", "-c", NULL, NULL }; 241 1.19 mjl 242 1.3 thorpej if (filename == NULL) 243 1.1 jtc filename = _PATH_MASTERPASSWD_LOCK; 244 1.19 mjl 245 1.27 tron filename = pw_filename(filename); 246 1.27 tron if (filename == NULL) 247 1.27 tron return; 248 1.27 tron 249 1.3 thorpej if ((editor = getenv("EDITOR")) == NULL) 250 1.19 mjl editor = _PATH_VI; 251 1.3 thorpej 252 1.19 mjl p = malloc(strlen(editor) + 1 + strlen(filename) + 1); 253 1.19 mjl if (p == NULL) 254 1.19 mjl return; 255 1.19 mjl 256 1.19 mjl sprintf(p, "%s %s", editor, filename); 257 1.19 mjl argp[2] = p; 258 1.19 mjl 259 1.19 mjl switch(editpid = vfork()) { 260 1.19 mjl case -1: 261 1.19 mjl free(p); 262 1.19 mjl return; 263 1.19 mjl case 0: 264 1.1 jtc if (notsetuid) { 265 1.1 jtc setgid(getgid()); 266 1.1 jtc setuid(getuid()); 267 1.1 jtc } 268 1.37 christos execvp(_PATH_BSHELL, (char *const *)__UNCONST(argp)); 269 1.1 jtc _exit(1); 270 1.1 jtc } 271 1.19 mjl 272 1.19 mjl free(p); 273 1.19 mjl 274 1.1 jtc for (;;) { 275 1.1 jtc editpid = waitpid(editpid, (int *)&pstat, WUNTRACED); 276 1.1 jtc if (editpid == -1) 277 1.1 jtc pw_error(editor, 1, 1); 278 1.1 jtc else if (WIFSTOPPED(pstat)) 279 1.1 jtc raise(WSTOPSIG(pstat)); 280 1.1 jtc else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0) 281 1.1 jtc break; 282 1.1 jtc else 283 1.1 jtc pw_error(editor, 1, 1); 284 1.1 jtc } 285 1.1 jtc editpid = -1; 286 1.1 jtc } 287 1.1 jtc 288 1.1 jtc void 289 1.20 ad pw_prompt(void) 290 1.1 jtc { 291 1.1 jtc int c; 292 1.1 jtc 293 1.1 jtc (void)printf("re-edit the password file? [y]: "); 294 1.1 jtc (void)fflush(stdout); 295 1.1 jtc c = getchar(); 296 1.1 jtc if (c != EOF && c != '\n') 297 1.1 jtc while (getchar() != '\n'); 298 1.1 jtc if (c == 'n') 299 1.1 jtc pw_error(NULL, 0, 0); 300 1.1 jtc } 301 1.1 jtc 302 1.10 phil /* for use in pw_copy(). Compare a pw entry to a pw struct. */ 303 1.46 christos /* returns a character string labelling the miscompared field or 0 */ 304 1.46 christos static const char * 305 1.20 ad pw_equal(char *buf, struct passwd *pw) 306 1.10 phil { 307 1.10 phil struct passwd buf_pw; 308 1.41 elad size_t len; 309 1.16 lukem 310 1.16 lukem _DIAGASSERT(buf != NULL); 311 1.16 lukem _DIAGASSERT(pw != NULL); 312 1.16 lukem 313 1.16 lukem len = strlen (buf); 314 1.10 phil if (buf[len-1] == '\n') 315 1.10 phil buf[len-1] = '\0'; 316 1.10 phil if (!pw_scan(buf, &buf_pw, NULL)) 317 1.46 christos return "corrupt line"; 318 1.46 christos if (strcmp(pw->pw_name, buf_pw.pw_name) != 0) 319 1.46 christos return "name"; 320 1.46 christos if (pw->pw_uid != buf_pw.pw_uid) 321 1.46 christos return "uid"; 322 1.46 christos if (pw->pw_gid != buf_pw.pw_gid) 323 1.46 christos return "gid"; 324 1.46 christos if (strcmp( pw->pw_class, buf_pw.pw_class) != 0) 325 1.46 christos return "class"; 326 1.46 christos if (pw->pw_change != buf_pw.pw_change) 327 1.46 christos return "change"; 328 1.46 christos if (pw->pw_expire != buf_pw.pw_expire) 329 1.46 christos return "expire"; 330 1.46 christos if (strcmp( pw->pw_gecos, buf_pw.pw_gecos) != 0) 331 1.46 christos return "gecos"; 332 1.46 christos if (strcmp( pw->pw_dir, buf_pw.pw_dir) != 0) 333 1.46 christos return "dir"; 334 1.46 christos if (strcmp( pw->pw_shell, buf_pw.pw_shell) != 0) 335 1.46 christos return "shell"; 336 1.46 christos return (char *)0; 337 1.10 phil } 338 1.10 phil 339 1.1 jtc void 340 1.20 ad pw_copy(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw) 341 1.1 jtc { 342 1.36 thorpej char errbuf[200]; 343 1.36 thorpej int rv; 344 1.36 thorpej 345 1.36 thorpej rv = pw_copyx(ffd, tfd, pw, old_pw, errbuf, sizeof(errbuf)); 346 1.36 thorpej if (rv == 0) { 347 1.36 thorpej warnx("%s", errbuf); 348 1.36 thorpej pw_error(NULL, 0, 1); 349 1.36 thorpej } 350 1.36 thorpej } 351 1.36 thorpej 352 1.47 christos static void 353 1.47 christos pw_print(FILE *to, const struct passwd *pw) 354 1.47 christos { 355 1.47 christos (void)fprintf(to, "%s:%s:%d:%d:%s:%lld:%lld:%s:%s:%s\n", 356 1.47 christos pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, 357 1.47 christos pw->pw_class, (long long)pw->pw_change, 358 1.47 christos (long long)pw->pw_expire, 359 1.47 christos pw->pw_gecos, pw->pw_dir, pw->pw_shell); 360 1.47 christos } 361 1.47 christos 362 1.36 thorpej int 363 1.36 thorpej pw_copyx(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw, 364 1.36 thorpej char *errbuf, size_t errbufsz) 365 1.36 thorpej { 366 1.27 tron const char *filename; 367 1.27 tron char mpwd[MAXPATHLEN], mpwdl[MAXPATHLEN], *p, buf[8192]; 368 1.1 jtc FILE *from, *to; 369 1.1 jtc int done; 370 1.1 jtc 371 1.16 lukem _DIAGASSERT(pw != NULL); 372 1.36 thorpej _DIAGASSERT(errbuf != NULL); 373 1.16 lukem /* old_pw may be NULL */ 374 1.16 lukem 375 1.36 thorpej if ((filename = pw_filename(_PATH_MASTERPASSWD)) == NULL) { 376 1.36 thorpej snprintf(errbuf, errbufsz, "%s: %s", pw_prefix, 377 1.36 thorpej strerror(errno)); 378 1.36 thorpej return (0); 379 1.36 thorpej } 380 1.27 tron (void)strcpy(mpwd, filename); 381 1.36 thorpej if ((filename = pw_filename(_PATH_MASTERPASSWD_LOCK)) == NULL) { 382 1.36 thorpej snprintf(errbuf, errbufsz, "%s: %s", pw_prefix, 383 1.36 thorpej strerror(errno)); 384 1.36 thorpej return (0); 385 1.36 thorpej } 386 1.27 tron (void)strcpy(mpwdl, filename); 387 1.27 tron 388 1.36 thorpej if (!(from = fdopen(ffd, "r"))) { 389 1.36 thorpej snprintf(errbuf, errbufsz, "%s: %s", mpwd, strerror(errno)); 390 1.36 thorpej return (0); 391 1.36 thorpej } 392 1.36 thorpej if (!(to = fdopen(tfd, "w"))) { 393 1.36 thorpej snprintf(errbuf, errbufsz, "%s: %s", mpwdl, strerror(errno)); 394 1.43 elad (void)fclose(from); 395 1.36 thorpej return (0); 396 1.36 thorpej } 397 1.1 jtc 398 1.41 elad for (done = 0; fgets(buf, (int)sizeof(buf), from);) { 399 1.46 christos const char *neq; 400 1.1 jtc if (!strchr(buf, '\n')) { 401 1.36 thorpej snprintf(errbuf, errbufsz, "%s: line too long", mpwd); 402 1.43 elad (void)fclose(from); 403 1.43 elad (void)fclose(to); 404 1.36 thorpej return (0); 405 1.1 jtc } 406 1.1 jtc if (done) { 407 1.1 jtc (void)fprintf(to, "%s", buf); 408 1.36 thorpej if (ferror(to)) { 409 1.36 thorpej snprintf(errbuf, errbufsz, "%s", 410 1.36 thorpej strerror(errno)); 411 1.43 elad (void)fclose(from); 412 1.43 elad (void)fclose(to); 413 1.36 thorpej return (0); 414 1.36 thorpej } 415 1.1 jtc continue; 416 1.1 jtc } 417 1.1 jtc if (!(p = strchr(buf, ':'))) { 418 1.36 thorpej snprintf(errbuf, errbufsz, "%s: corrupted entry", mpwd); 419 1.43 elad (void)fclose(from); 420 1.43 elad (void)fclose(to); 421 1.36 thorpej return (0); 422 1.1 jtc } 423 1.1 jtc *p = '\0'; 424 1.1 jtc if (strcmp(buf, pw->pw_name)) { 425 1.1 jtc *p = ':'; 426 1.1 jtc (void)fprintf(to, "%s", buf); 427 1.36 thorpej if (ferror(to)) { 428 1.36 thorpej snprintf(errbuf, errbufsz, "%s", 429 1.36 thorpej strerror(errno)); 430 1.43 elad (void)fclose(from); 431 1.43 elad (void)fclose(to); 432 1.36 thorpej return (0); 433 1.36 thorpej } 434 1.1 jtc continue; 435 1.1 jtc } 436 1.10 phil *p = ':'; 437 1.46 christos if (old_pw && (neq = pw_equal(buf, old_pw)) != NULL) { 438 1.46 christos if (strcmp(neq, "corrupt line") == 0) 439 1.46 christos (void)snprintf(errbuf, errbufsz, 440 1.46 christos "%s: entry %s corrupted", mpwd, 441 1.46 christos pw->pw_name); 442 1.46 christos else 443 1.46 christos (void)snprintf(errbuf, errbufsz, 444 1.46 christos "%s: entry %s inconsistent %s", 445 1.46 christos mpwd, pw->pw_name, neq); 446 1.43 elad (void)fclose(from); 447 1.43 elad (void)fclose(to); 448 1.36 thorpej return (0); 449 1.10 phil } 450 1.47 christos pw_print(to, pw); 451 1.1 jtc done = 1; 452 1.36 thorpej if (ferror(to)) { 453 1.36 thorpej snprintf(errbuf, errbufsz, "%s", strerror(errno)); 454 1.43 elad (void)fclose(from); 455 1.43 elad (void)fclose(to); 456 1.36 thorpej return (0); 457 1.36 thorpej } 458 1.1 jtc } 459 1.10 phil /* Only append a new entry if real uid is root! */ 460 1.13 thorpej if (!done) { 461 1.36 thorpej if (getuid() == 0) { 462 1.47 christos pw_print(to, pw); 463 1.36 thorpej done = 1; 464 1.36 thorpej } else { 465 1.36 thorpej snprintf(errbuf, errbufsz, 466 1.36 thorpej "%s: changes not made, no such entry", mpwd); 467 1.36 thorpej } 468 1.13 thorpej } 469 1.1 jtc 470 1.36 thorpej if (ferror(to)) { 471 1.36 thorpej snprintf(errbuf, errbufsz, "%s", strerror(errno)); 472 1.43 elad (void)fclose(from); 473 1.43 elad (void)fclose(to); 474 1.36 thorpej return (0); 475 1.36 thorpej } 476 1.43 elad (void)fclose(from); 477 1.1 jtc (void)fclose(to); 478 1.36 thorpej 479 1.36 thorpej return (done); 480 1.1 jtc } 481 1.1 jtc 482 1.1 jtc void 483 1.32 lukem pw_error(const char *name, int error, int eval) 484 1.1 jtc { 485 1.16 lukem 486 1.32 lukem if (error) { 487 1.26 itojun if (name) 488 1.26 itojun warn("%s", name); 489 1.26 itojun else 490 1.26 itojun warn(NULL); 491 1.26 itojun } 492 1.1 jtc 493 1.27 tron warnx("%s%s: unchanged", pw_prefix, _PATH_MASTERPASSWD); 494 1.1 jtc pw_abort(); 495 1.1 jtc exit(eval); 496 1.20 ad } 497 1.20 ad 498 1.20 ad /* Removes head and/or tail spaces. */ 499 1.20 ad static void 500 1.20 ad trim_whitespace(char *line) 501 1.20 ad { 502 1.20 ad char *p; 503 1.20 ad 504 1.29 lukem _DIAGASSERT(line != NULL); 505 1.29 lukem 506 1.53 kamil /* Handle empty string */ 507 1.53 kamil if (*line == '\0') 508 1.53 kamil return; 509 1.53 kamil 510 1.20 ad /* Remove leading spaces */ 511 1.20 ad p = line; 512 1.24 itohy while (isspace((unsigned char) *p)) 513 1.20 ad p++; 514 1.20 ad memmove(line, p, strlen(p) + 1); 515 1.20 ad 516 1.53 kamil /* Handle empty string after removal of whitespace characters */ 517 1.53 kamil if (*line == '\0') 518 1.53 kamil return; 519 1.53 kamil 520 1.53 kamil /* Remove trailing spaces, line must not be empty string here */ 521 1.20 ad p = line + strlen(line) - 1; 522 1.24 itohy while (isspace((unsigned char) *p)) 523 1.20 ad p--; 524 1.20 ad *(p + 1) = '\0'; 525 1.20 ad } 526 1.20 ad 527 1.20 ad 528 1.20 ad /* Get one line, remove spaces from front and tail */ 529 1.20 ad static int 530 1.20 ad read_line(FILE *fp, char *line, int max) 531 1.20 ad { 532 1.20 ad char *p; 533 1.20 ad 534 1.29 lukem _DIAGASSERT(fp != NULL); 535 1.29 lukem _DIAGASSERT(line != NULL); 536 1.29 lukem 537 1.20 ad /* Read one line of config */ 538 1.20 ad if (fgets(line, max, fp) == NULL) 539 1.20 ad return (0); 540 1.20 ad 541 1.20 ad if ((p = strchr(line, '\n')) == NULL) { 542 1.20 ad warnx("line too long"); 543 1.20 ad return (0); 544 1.20 ad } 545 1.20 ad *p = '\0'; 546 1.20 ad 547 1.20 ad /* Remove comments */ 548 1.20 ad if ((p = strchr(line, '#')) != NULL) 549 1.20 ad *p = '\0'; 550 1.20 ad 551 1.20 ad trim_whitespace(line); 552 1.20 ad return (1); 553 1.20 ad } 554 1.20 ad 555 1.20 ad static const char * 556 1.20 ad pw_default(const char *option) 557 1.20 ad { 558 1.20 ad static const char *options[][2] = { 559 1.34 ad { "localcipher", "old" }, 560 1.20 ad { "ypcipher", "old" }, 561 1.20 ad }; 562 1.48 lukem size_t i; 563 1.20 ad 564 1.29 lukem _DIAGASSERT(option != NULL); 565 1.20 ad for (i = 0; i < sizeof(options) / sizeof(options[0]); i++) 566 1.20 ad if (strcmp(options[i][0], option) == 0) 567 1.20 ad return (options[i][1]); 568 1.20 ad 569 1.20 ad return (NULL); 570 1.20 ad } 571 1.20 ad 572 1.20 ad /* 573 1.20 ad * Retrieve password information from the /etc/passwd.conf file, at the 574 1.20 ad * moment this is only for choosing the cipher to use. It could easily be 575 1.20 ad * used for other authentication methods as well. 576 1.20 ad */ 577 1.20 ad void 578 1.20 ad pw_getconf(char *data, size_t max, const char *key, const char *option) 579 1.20 ad { 580 1.20 ad FILE *fp; 581 1.20 ad char line[LINE_MAX], *p, *p2; 582 1.20 ad static char result[LINE_MAX]; 583 1.20 ad int got, found; 584 1.20 ad const char *cp; 585 1.29 lukem 586 1.29 lukem _DIAGASSERT(data != NULL); 587 1.29 lukem _DIAGASSERT(key != NULL); 588 1.29 lukem _DIAGASSERT(option != NULL); 589 1.20 ad 590 1.20 ad got = 0; 591 1.20 ad found = 0; 592 1.20 ad result[0] = '\0'; 593 1.20 ad 594 1.31 lukem if ((fp = fopen(_PATH_PASSWD_CONF, "r")) == NULL) { 595 1.20 ad if ((cp = pw_default(option)) != NULL) 596 1.23 ad strlcpy(data, cp, max); 597 1.20 ad else 598 1.20 ad data[0] = '\0'; 599 1.20 ad return; 600 1.20 ad } 601 1.20 ad 602 1.20 ad while (!found && (got || read_line(fp, line, LINE_MAX))) { 603 1.20 ad got = 0; 604 1.20 ad 605 1.20 ad if (strncmp(key, line, strlen(key)) != 0 || 606 1.20 ad line[strlen(key)] != ':') 607 1.20 ad continue; 608 1.20 ad 609 1.20 ad /* Now we found our specified key */ 610 1.20 ad while (read_line(fp, line, LINE_MAX)) { 611 1.20 ad /* Leaving key field */ 612 1.21 ad if (line[0] != '\0' && strchr(line + 1, ':') != NULL) { 613 1.20 ad got = 1; 614 1.20 ad break; 615 1.20 ad } 616 1.20 ad p2 = line; 617 1.20 ad if ((p = strsep(&p2, "=")) == NULL || p2 == NULL) 618 1.20 ad continue; 619 1.20 ad trim_whitespace(p); 620 1.20 ad 621 1.20 ad if (!strncmp(p, option, strlen(option))) { 622 1.20 ad trim_whitespace(p2); 623 1.20 ad strcpy(result, p2); 624 1.20 ad found = 1; 625 1.20 ad break; 626 1.20 ad } 627 1.20 ad } 628 1.20 ad } 629 1.20 ad fclose(fp); 630 1.20 ad 631 1.40 elad if (!found) 632 1.40 elad errno = ENOENT; 633 1.40 elad if (!got) 634 1.40 elad errno = ENOTDIR; 635 1.40 elad 636 1.20 ad /* 637 1.20 ad * If we got no result and were looking for a default 638 1.20 ad * value, try hard coded defaults. 639 1.20 ad */ 640 1.20 ad 641 1.20 ad if (strlen(result) == 0 && strcmp(key, "default") == 0 && 642 1.20 ad (cp = pw_default(option)) != NULL) 643 1.23 ad strlcpy(data, cp, max); 644 1.20 ad else 645 1.20 ad strlcpy(data, result, max); 646 1.1 jtc } 647 1.38 christos 648 1.38 christos void 649 1.38 christos pw_getpwconf(char *data, size_t max, const struct passwd *pwd, 650 1.38 christos const char *option) 651 1.38 christos { 652 1.38 christos char grpkey[LINE_MAX]; 653 1.42 christos struct group grs, *grp; 654 1.42 christos char grbuf[1024]; 655 1.38 christos 656 1.38 christos pw_getconf(data, max, pwd->pw_name, option); 657 1.38 christos 658 1.38 christos /* Try to find an entry for the group */ 659 1.38 christos if (*data == '\0') { 660 1.42 christos (void)getgrgid_r(pwd->pw_gid, &grs, grbuf, sizeof(grbuf), &grp); 661 1.42 christos if (grp != NULL) { 662 1.42 christos (void)snprintf(grpkey, sizeof(grpkey), ":%s", 663 1.42 christos grp->gr_name); 664 1.38 christos pw_getconf(data, max, grpkey, option); 665 1.38 christos } 666 1.39 christos if (*data == '\0') 667 1.38 christos pw_getconf(data, max, "default", option); 668 1.38 christos } 669 1.38 christos } 670