Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

619 lines
13 KiB

28 years ago
28 years ago
28 years ago
27 years ago
28 years ago
27 years ago
28 years ago
28 years ago
22 years ago
28 years ago
21 years ago
21 years ago
21 years ago
21 years ago
27 years ago
22 years ago
21 years ago
22 years ago
21 years ago
22 years ago
21 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
22 years ago
21 years ago
28 years ago
21 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
21 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
21 years ago
28 years ago
28 years ago
21 years ago
28 years ago
28 years ago
21 years ago
28 years ago
21 years ago
28 years ago
28 years ago
21 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
21 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
27 years ago
28 years ago
21 years ago
28 years ago
21 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
28 years ago
22 years ago
28 years ago
28 years ago
27 years ago
28 years ago
21 years ago
28 years ago
28 years ago
  1. /* $OpenBSD: passwd.c,v 1.43 2004/04/20 23:20:07 millert Exp $ */
  2. /*
  3. * Copyright (c) 1987, 1993, 1994, 1995
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #if defined(LIBC_SCCS) && !defined(lint)
  31. static const char rcsid[] = "$OpenBSD: passwd.c,v 1.43 2004/04/20 23:20:07 millert Exp $";
  32. #endif /* LIBC_SCCS and not lint */
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <sys/time.h>
  36. #include <sys/resource.h>
  37. #include <sys/wait.h>
  38. #include <fcntl.h>
  39. #include <unistd.h>
  40. #include <stdlib.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <ctype.h>
  44. #include <pwd.h>
  45. #include <err.h>
  46. #include <errno.h>
  47. #include <paths.h>
  48. #include <signal.h>
  49. #include <limits.h>
  50. #include "util.h"
  51. #define NUM_OPTIONS 2 /* Number of hardcoded defaults */
  52. static void pw_cont(int sig);
  53. static const char options[NUM_OPTIONS][2][80] = {
  54. {"localcipher", "blowfish,4"},
  55. {"ypcipher", "old"}
  56. };
  57. static char pw_defdir[] = "/etc";
  58. static char *pw_dir = pw_defdir;
  59. static char *pw_lck;
  60. static void trim_whitespace(char *);
  61. static int read_line(FILE *, char *, int);
  62. static const char *pw_default(const char *);
  63. /* Removes head and/or tail spaces. */
  64. static void
  65. trim_whitespace(char *line)
  66. {
  67. char *p;
  68. /* Remove leading spaces */
  69. p = line;
  70. while (isspace(*p))
  71. p++;
  72. (void) memmove(line, p, strlen(p) + 1);
  73. /* Remove trailing spaces */
  74. p = line + strlen(line) - 1;
  75. while (isspace(*p))
  76. p--;
  77. *(p + 1) = '\0';
  78. }
  79. /* Get one line, remove spaces from front and tail */
  80. static int
  81. read_line(FILE *fp, char *line, int max)
  82. {
  83. char *p;
  84. /* Read one line of config */
  85. if (fgets(line, max, fp) == 0)
  86. return 0;
  87. if (!(p = strchr(line, '\n'))) {
  88. warnx("line too long");
  89. return 0;
  90. }
  91. *p = '\0';
  92. /* Remove comments */
  93. if ((p = strchr(line, '#')))
  94. *p = '\0';
  95. trim_whitespace(line);
  96. return 1;
  97. }
  98. static const char *
  99. pw_default(const char *option)
  100. {
  101. int i;
  102. for (i = 0; i < NUM_OPTIONS; i++)
  103. if (!strcmp(options[i][0], option))
  104. return options[i][1];
  105. return NULL;
  106. }
  107. char *
  108. pw_file(const char *nm)
  109. {
  110. const char *p = strrchr(nm, '/');
  111. char *new_nm;
  112. if (p)
  113. p++;
  114. else
  115. p = nm;
  116. if (asprintf(&new_nm, "%s/%s", pw_dir, p) == -1)
  117. return NULL;
  118. return new_nm;
  119. }
  120. /*
  121. * Retrieve password information from the /etc/passwd.conf file,
  122. * at the moment this is only for choosing the cipher to use.
  123. * It could easily be used for other authentication methods as
  124. * well.
  125. */
  126. void
  127. pw_getconf(char *data, size_t max, const char *key, const char *option)
  128. {
  129. FILE *fp;
  130. char line[LINE_MAX];
  131. static char result[LINE_MAX];
  132. char *p;
  133. int got = 0;
  134. int found = 0;
  135. result[0] = '\0';
  136. p = pw_file(_PATH_PASSWDCONF);
  137. if (!p || (fp = fopen(p, "r")) == NULL) {
  138. if (p)
  139. free(p);
  140. if ((p = (char *)pw_default(option))) {
  141. strncpy(data, p, max - 1);
  142. data[max - 1] = '\0';
  143. } else
  144. data[0] = '\0';
  145. return;
  146. }
  147. free(p);
  148. while (!found && (got || read_line(fp, line, LINE_MAX))) {
  149. got = 0;
  150. if (strncmp(key, line, strlen(key)) ||
  151. line[strlen(key)] != ':')
  152. continue;
  153. /* Now we found our specified key */
  154. while (read_line(fp, line, LINE_MAX)) {
  155. char *p2;
  156. /* Leaving key field */
  157. if (strchr(line, ':')) {
  158. got = 1;
  159. break;
  160. }
  161. p2 = line;
  162. if (!(p = strsep(&p2, "=")) || p2 == NULL)
  163. continue;
  164. trim_whitespace(p);
  165. if (!strncmp(p, option, strlen(option))) {
  166. trim_whitespace(p2);
  167. strlcpy(result, p2, sizeof result);
  168. found = 1;
  169. break;
  170. }
  171. }
  172. }
  173. fclose(fp);
  174. /*
  175. * If we got no result and were looking for a default
  176. * value, try hard coded defaults.
  177. */
  178. if (!strlen(result) && !strcmp(key,"default") &&
  179. (p = (char *)pw_default(option)))
  180. strncpy(data, p, max - 1);
  181. else
  182. strncpy(data, result, max - 1);
  183. data[max - 1] = '\0';
  184. }
  185. void
  186. pw_setdir(const char *dir)
  187. {
  188. char *p;
  189. if (strcmp (dir, pw_dir) == 0)
  190. return;
  191. if (pw_dir != pw_defdir)
  192. free(pw_dir);
  193. pw_dir = strdup(dir);
  194. if (pw_lck) {
  195. p = pw_file(pw_lck);
  196. free(pw_lck);
  197. pw_lck = p;
  198. }
  199. }
  200. int
  201. pw_lock(int retries)
  202. {
  203. int i, fd;
  204. mode_t old_mode;
  205. int save_errno;
  206. if (!pw_lck)
  207. return (-1);
  208. /* Acquire the lock file. */
  209. old_mode = umask(0);
  210. fd = open(pw_lck, O_WRONLY|O_CREAT|O_EXCL, 0600);
  211. for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) {
  212. sleep(1);
  213. fd = open(pw_lck, O_WRONLY|O_CREAT|O_EXCL, 0600);
  214. }
  215. save_errno = errno;
  216. if (fd != -1 && fcntl(fd, F_SETFD, 1) == -1) {
  217. close(fd);
  218. fd = -1;
  219. }
  220. (void) umask(old_mode);
  221. errno = save_errno;
  222. return (fd);
  223. }
  224. int
  225. pw_mkdb(char *username, int flags)
  226. {
  227. int pstat, ac;
  228. pid_t pid;
  229. char *av[8];
  230. struct stat sb;
  231. if (pw_lck == NULL)
  232. return(-1);
  233. /* A zero length passwd file is never ok */
  234. if (stat(pw_lck, &sb) == 0 && sb.st_size == 0) {
  235. warnx("%s is zero length", pw_lck);
  236. return (-1);
  237. }
  238. ac = 0;
  239. av[ac++] = "pwd_mkdb";
  240. av[ac++] = "-d";
  241. av[ac++] = pw_dir;
  242. if (flags & _PASSWORD_SECUREONLY)
  243. av[ac++] = "-s";
  244. else if (!(flags & _PASSWORD_OMITV7))
  245. av[ac++] = "-p";
  246. if (username) {
  247. av[ac++] = "-u";
  248. av[ac++] = username;
  249. }
  250. av[ac++] = pw_lck;
  251. av[ac] = NULL;
  252. pid = vfork();
  253. if (pid == -1)
  254. return (-1);
  255. if (pid == 0) {
  256. if (pw_lck)
  257. execv(_PATH_PWD_MKDB, av);
  258. _exit(1);
  259. }
  260. pid = waitpid(pid, &pstat, 0);
  261. if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
  262. return (-1);
  263. return (0);
  264. }
  265. int
  266. pw_abort(void)
  267. {
  268. return (pw_lck ? unlink(pw_lck) : -1);
  269. }
  270. /* Everything below this point is intended for the convenience of programs
  271. * which allow a user to interactively edit the passwd file. Errors in the
  272. * routines below will cause the process to abort. */
  273. static pid_t editpid = -1;
  274. static void
  275. pw_cont(int signo)
  276. {
  277. int save_errno = errno;
  278. if (editpid != -1)
  279. kill(editpid, signo);
  280. errno = save_errno;
  281. }
  282. void
  283. pw_init(void)
  284. {
  285. struct rlimit rlim;
  286. /* Unlimited resource limits. */
  287. rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
  288. (void)setrlimit(RLIMIT_CPU, &rlim);
  289. (void)setrlimit(RLIMIT_FSIZE, &rlim);
  290. (void)setrlimit(RLIMIT_STACK, &rlim);
  291. (void)setrlimit(RLIMIT_DATA, &rlim);
  292. (void)setrlimit(RLIMIT_RSS, &rlim);
  293. /* Don't drop core (not really necessary, but GP's). */
  294. rlim.rlim_cur = rlim.rlim_max = 0;
  295. (void)setrlimit(RLIMIT_CORE, &rlim);
  296. /* Turn off signals. */
  297. (void)signal(SIGALRM, SIG_IGN);
  298. (void)signal(SIGHUP, SIG_IGN);
  299. (void)signal(SIGINT, SIG_IGN);
  300. (void)signal(SIGPIPE, SIG_IGN);
  301. (void)signal(SIGQUIT, SIG_IGN);
  302. (void)signal(SIGTERM, SIG_IGN);
  303. (void)signal(SIGCONT, pw_cont);
  304. if (!pw_lck)
  305. pw_lck = pw_file(_PATH_MASTERPASSWD_LOCK);
  306. }
  307. void
  308. pw_edit(int notsetuid, const char *filename)
  309. {
  310. int pstat;
  311. char *p;
  312. char * volatile editor;
  313. char *argp[] = {"sh", "-c", NULL, NULL};
  314. if (!filename) {
  315. filename = pw_lck;
  316. if (!filename)
  317. return;
  318. }
  319. if ((editor = getenv("EDITOR")) == NULL)
  320. editor = _PATH_VI;
  321. if (asprintf(&p, "%s %s", editor, filename) == -1)
  322. return;
  323. argp[2] = p;
  324. switch (editpid = vfork()) {
  325. case -1: /* error */
  326. free(p);
  327. return;
  328. case 0: /* child */
  329. if (notsetuid) {
  330. setgid(getgid());
  331. setuid(getuid());
  332. }
  333. execv(_PATH_BSHELL, argp);
  334. _exit(127);
  335. }
  336. free(p);
  337. for (;;) {
  338. editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
  339. if (editpid == -1)
  340. pw_error(editor, 1, 1);
  341. else if (WIFSTOPPED(pstat))
  342. raise(WSTOPSIG(pstat));
  343. else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0)
  344. break;
  345. else
  346. pw_error(editor, 1, 1);
  347. }
  348. editpid = -1;
  349. }
  350. void
  351. pw_prompt(void)
  352. {
  353. int first, c;
  354. (void)printf("re-edit the password file? [y]: ");
  355. (void)fflush(stdout);
  356. first = c = getchar();
  357. while (c != '\n' && c != EOF)
  358. c = getchar();
  359. if (first == 'n')
  360. pw_error(NULL, 0, 0);
  361. }
  362. static int
  363. pw_equal(const struct passwd *pw1, const struct passwd *pw2)
  364. {
  365. return (strcmp(pw1->pw_name, pw2->pw_name) == 0 &&
  366. pw1->pw_uid == pw2->pw_uid &&
  367. pw1->pw_gid == pw2->pw_gid &&
  368. strcmp(pw1->pw_class, pw2->pw_class) == 0 &&
  369. pw1->pw_change == pw2->pw_change &&
  370. pw1->pw_expire == pw2->pw_expire &&
  371. strcmp(pw1->pw_gecos, pw2->pw_gecos) == 0 &&
  372. strcmp(pw1->pw_dir, pw2->pw_dir) == 0 &&
  373. strcmp(pw1->pw_shell, pw2->pw_shell) == 0);
  374. }
  375. void
  376. pw_copy(int ffd, int tfd, const struct passwd *pw, const struct passwd *opw)
  377. {
  378. struct passwd tpw;
  379. FILE *from, *to;
  380. int done;
  381. char *p, *ep, buf[8192];
  382. char *master = pw_file(_PATH_MASTERPASSWD);
  383. if (!master)
  384. pw_error(NULL, 0, 1);
  385. if (!(from = fdopen(ffd, "r")))
  386. pw_error(master, 1, 1);
  387. if (!(to = fdopen(tfd, "w")))
  388. pw_error(pw_lck ? pw_lck : NULL, pw_lck ? 1 : 0, 1);
  389. for (done = 0; fgets(buf, sizeof(buf), from);) {
  390. if ((ep = strchr(buf, '\n')) == NULL) {
  391. warnx("%s: line too long", master);
  392. pw_error(NULL, 0, 1);
  393. }
  394. if (done) {
  395. (void)fprintf(to, "%s", buf);
  396. if (ferror(to))
  397. goto err;
  398. continue;
  399. }
  400. if (!(p = strchr(buf, ':'))) {
  401. warnx("%s: corrupted entry", master);
  402. pw_error(NULL, 0, 1);
  403. }
  404. *p = '\0';
  405. if (strcmp(buf, opw ? opw->pw_name : pw->pw_name)) {
  406. *p = ':';
  407. (void)fprintf(to, "%s", buf);
  408. if (ferror(to))
  409. goto err;
  410. continue;
  411. }
  412. if (opw != NULL) {
  413. *p = ':';
  414. *ep = '\0';
  415. if (!pw_scan(buf, &tpw, NULL))
  416. pw_error(NULL, 0, 1);
  417. if (!pw_equal(&tpw, opw)) {
  418. warnx("%s: inconsistent entry", master);
  419. pw_error(NULL, 0, 1);
  420. }
  421. }
  422. (void)fprintf(to, "%s:%s:%u:%u:%s:%d:%d:%s:%s:%s\n",
  423. pw->pw_name, pw->pw_passwd, (u_int)pw->pw_uid,
  424. (u_int)pw->pw_gid, pw->pw_class, pw->pw_change,
  425. pw->pw_expire, pw->pw_gecos, pw->pw_dir,
  426. pw->pw_shell);
  427. done = 1;
  428. if (ferror(to))
  429. goto err;
  430. }
  431. if (!done)
  432. (void)fprintf(to, "%s:%s:%d:%d:%s:%d:%d:%s:%s:%s\n",
  433. pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
  434. pw->pw_class, pw->pw_change, pw->pw_expire, pw->pw_gecos,
  435. pw->pw_dir, pw->pw_shell);
  436. if (ferror(to))
  437. err:
  438. pw_error(NULL, 0, 1);
  439. free(master);
  440. (void)fclose(to);
  441. }
  442. int
  443. pw_scan(char *bp, struct passwd *pw, int *flags)
  444. {
  445. u_long id;
  446. int root;
  447. char *p, *sh, *p2;
  448. if (flags != (int *)NULL)
  449. *flags = 0;
  450. if (!(p = strsep(&bp, ":")) || *p == '\0') /* login */
  451. goto fmt;
  452. pw->pw_name = p;
  453. root = !strcmp(pw->pw_name, "root");
  454. if (!(pw->pw_passwd = strsep(&bp, ":"))) /* passwd */
  455. goto fmt;
  456. if (!(p = strsep(&bp, ":"))) /* uid */
  457. goto fmt;
  458. id = strtoul(p, &p2, 10);
  459. if (root && id) {
  460. warnx("root uid should be 0");
  461. return (0);
  462. }
  463. if (*p2 != '\0') {
  464. warnx("illegal uid field");
  465. return (0);
  466. }
  467. if (id > UID_MAX) {
  468. /* errno is set to ERANGE by strtoul(3) */
  469. warnx("uid greater than %u", UID_MAX-1);
  470. return (0);
  471. }
  472. pw->pw_uid = (uid_t)id;
  473. if ((*p == '\0') && (flags != (int *)NULL))
  474. *flags |= _PASSWORD_NOUID;
  475. if (!(p = strsep(&bp, ":"))) /* gid */
  476. goto fmt;
  477. id = strtoul(p, &p2, 10);
  478. if (*p2 != '\0') {
  479. warnx("illegal gid field");
  480. return (0);
  481. }
  482. if (id > UID_MAX) {
  483. /* errno is set to ERANGE by strtoul(3) */
  484. warnx("gid greater than %u", UID_MAX-1);
  485. return (0);
  486. }
  487. pw->pw_gid = (gid_t)id;
  488. if ((*p == '\0') && (flags != (int *)NULL))
  489. *flags |= _PASSWORD_NOGID;
  490. pw->pw_class = strsep(&bp, ":"); /* class */
  491. if (!(p = strsep(&bp, ":"))) /* change */
  492. goto fmt;
  493. pw->pw_change = atol(p);
  494. if ((*p == '\0') && (flags != (int *)NULL))
  495. *flags |= _PASSWORD_NOCHG;
  496. if (!(p = strsep(&bp, ":"))) /* expire */
  497. goto fmt;
  498. pw->pw_expire = atol(p);
  499. if ((*p == '\0') && (flags != (int *)NULL))
  500. *flags |= _PASSWORD_NOEXP;
  501. pw->pw_gecos = strsep(&bp, ":"); /* gecos */
  502. pw->pw_dir = strsep(&bp, ":"); /* directory */
  503. if (!(pw->pw_shell = strsep(&bp, ":"))) /* shell */
  504. goto fmt;
  505. p = pw->pw_shell;
  506. if (root && *p) { /* empty == /bin/sh */
  507. for (setusershell();;) {
  508. if (!(sh = getusershell())) {
  509. warnx("warning, unknown root shell");
  510. break;
  511. }
  512. if (!strcmp(p, sh))
  513. break;
  514. }
  515. endusershell();
  516. }
  517. if ((p = strsep(&bp, ":"))) { /* too many */
  518. fmt: warnx("corrupted entry");
  519. return (0);
  520. }
  521. return (1);
  522. }
  523. __dead void
  524. pw_error(const char *name, int err, int eval)
  525. {
  526. char *master = pw_file(_PATH_MASTERPASSWD);
  527. if (err) {
  528. if (name)
  529. warn("%s", name);
  530. else
  531. warn(NULL);
  532. }
  533. if (master) {
  534. warnx("%s: unchanged", master);
  535. free(master);
  536. }
  537. pw_abort();
  538. exit(eval);
  539. }