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.

481 lines
10 KiB

29 years ago
29 years ago
27 years ago
29 years ago
27 years ago
29 years ago
29 years ago
21 years ago
21 years ago
29 years ago
21 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
21 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
21 years ago
29 years ago
29 years ago
21 years ago
29 years ago
29 years ago
21 years ago
29 years ago
21 years ago
29 years ago
29 years ago
21 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
21 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
27 years ago
29 years ago
22 years ago
29 years ago
21 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
29 years ago
22 years ago
29 years ago
29 years ago
27 years ago
29 years ago
29 years ago
29 years ago
  1. /* $OpenBSD: passwd.c,v 1.49 2006/12/20 23:07:36 ray 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. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <sys/time.h>
  33. #include <sys/resource.h>
  34. #include <sys/wait.h>
  35. #include <fcntl.h>
  36. #include <unistd.h>
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <ctype.h>
  41. #include <pwd.h>
  42. #include <err.h>
  43. #include <errno.h>
  44. #include <paths.h>
  45. #include <signal.h>
  46. #include <limits.h>
  47. #include "util.h"
  48. static char pw_defdir[] = "/etc";
  49. static char *pw_dir = pw_defdir;
  50. static char *pw_lck;
  51. char *
  52. pw_file(const char *nm)
  53. {
  54. const char *p = strrchr(nm, '/');
  55. char *new_nm;
  56. if (p)
  57. p++;
  58. else
  59. p = nm;
  60. if (asprintf(&new_nm, "%s/%s", pw_dir, p) == -1)
  61. return NULL;
  62. return new_nm;
  63. }
  64. void
  65. pw_setdir(const char *dir)
  66. {
  67. char *p;
  68. if (strcmp (dir, pw_dir) == 0)
  69. return;
  70. if (pw_dir != pw_defdir)
  71. free(pw_dir);
  72. pw_dir = strdup(dir);
  73. if (pw_lck) {
  74. p = pw_file(pw_lck);
  75. free(pw_lck);
  76. pw_lck = p;
  77. }
  78. }
  79. int
  80. pw_lock(int retries)
  81. {
  82. int i, fd;
  83. mode_t old_mode;
  84. int save_errno;
  85. if (!pw_lck) {
  86. errno = EINVAL;
  87. return (-1);
  88. }
  89. /* Acquire the lock file. */
  90. old_mode = umask(0);
  91. fd = open(pw_lck, O_WRONLY|O_CREAT|O_EXCL, 0600);
  92. for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) {
  93. sleep(1);
  94. fd = open(pw_lck, O_WRONLY|O_CREAT|O_EXCL, 0600);
  95. }
  96. save_errno = errno;
  97. if (fd != -1 && fcntl(fd, F_SETFD, 1) == -1) {
  98. close(fd);
  99. fd = -1;
  100. }
  101. (void) umask(old_mode);
  102. errno = save_errno;
  103. return (fd);
  104. }
  105. int
  106. pw_mkdb(char *username, int flags)
  107. {
  108. int pstat, ac;
  109. pid_t pid;
  110. char *av[8];
  111. struct stat sb;
  112. if (pw_lck == NULL)
  113. return(-1);
  114. /* A zero length passwd file is never ok */
  115. if (stat(pw_lck, &sb) == 0 && sb.st_size == 0) {
  116. warnx("%s is zero length", pw_lck);
  117. return (-1);
  118. }
  119. ac = 0;
  120. av[ac++] = "pwd_mkdb";
  121. av[ac++] = "-d";
  122. av[ac++] = pw_dir;
  123. if (flags & _PASSWORD_SECUREONLY)
  124. av[ac++] = "-s";
  125. else if (!(flags & _PASSWORD_OMITV7))
  126. av[ac++] = "-p";
  127. if (username) {
  128. av[ac++] = "-u";
  129. av[ac++] = username;
  130. }
  131. av[ac++] = pw_lck;
  132. av[ac] = NULL;
  133. pid = vfork();
  134. if (pid == -1)
  135. return (-1);
  136. if (pid == 0) {
  137. if (pw_lck)
  138. execv(_PATH_PWD_MKDB, av);
  139. _exit(1);
  140. }
  141. pid = waitpid(pid, &pstat, 0);
  142. if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0)
  143. return (-1);
  144. return (0);
  145. }
  146. int
  147. pw_abort(void)
  148. {
  149. return (pw_lck ? unlink(pw_lck) : -1);
  150. }
  151. /* Everything below this point is intended for the convenience of programs
  152. * which allow a user to interactively edit the passwd file. Errors in the
  153. * routines below will cause the process to abort. */
  154. static pid_t editpid = -1;
  155. static void
  156. pw_cont(int signo)
  157. {
  158. int save_errno = errno;
  159. if (editpid != -1)
  160. kill(editpid, signo);
  161. errno = save_errno;
  162. }
  163. void
  164. pw_init(void)
  165. {
  166. struct rlimit rlim;
  167. /* Unlimited resource limits. */
  168. rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
  169. (void)setrlimit(RLIMIT_CPU, &rlim);
  170. (void)setrlimit(RLIMIT_FSIZE, &rlim);
  171. (void)setrlimit(RLIMIT_STACK, &rlim);
  172. (void)setrlimit(RLIMIT_DATA, &rlim);
  173. (void)setrlimit(RLIMIT_RSS, &rlim);
  174. /* Don't drop core (not really necessary, but GP's). */
  175. rlim.rlim_cur = rlim.rlim_max = 0;
  176. (void)setrlimit(RLIMIT_CORE, &rlim);
  177. /* Turn off signals. */
  178. (void)signal(SIGALRM, SIG_IGN);
  179. (void)signal(SIGHUP, SIG_IGN);
  180. (void)signal(SIGINT, SIG_IGN);
  181. (void)signal(SIGPIPE, SIG_IGN);
  182. (void)signal(SIGQUIT, SIG_IGN);
  183. (void)signal(SIGTERM, SIG_IGN);
  184. (void)signal(SIGCONT, pw_cont);
  185. if (!pw_lck)
  186. pw_lck = pw_file(_PATH_MASTERPASSWD_LOCK);
  187. }
  188. void
  189. pw_edit(int notsetuid, const char *filename)
  190. {
  191. int pstat;
  192. char *p;
  193. char * volatile editor;
  194. char *argp[] = {"sh", "-c", NULL, NULL};
  195. if (!filename) {
  196. filename = pw_lck;
  197. if (!filename)
  198. return;
  199. }
  200. if ((editor = getenv("EDITOR")) == NULL)
  201. editor = _PATH_VI;
  202. if (asprintf(&p, "%s %s", editor, filename) == -1)
  203. return;
  204. argp[2] = p;
  205. switch (editpid = vfork()) {
  206. case -1: /* error */
  207. free(p);
  208. return;
  209. case 0: /* child */
  210. if (notsetuid) {
  211. setgid(getgid());
  212. setuid(getuid());
  213. }
  214. execv(_PATH_BSHELL, argp);
  215. _exit(127);
  216. }
  217. free(p);
  218. for (;;) {
  219. editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
  220. if (editpid == -1)
  221. pw_error(editor, 1, 1);
  222. else if (WIFSTOPPED(pstat))
  223. raise(WSTOPSIG(pstat));
  224. else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0)
  225. break;
  226. else
  227. pw_error(editor, 1, 1);
  228. }
  229. editpid = -1;
  230. }
  231. void
  232. pw_prompt(void)
  233. {
  234. int first, c;
  235. (void)printf("re-edit the password file? [y]: ");
  236. (void)fflush(stdout);
  237. first = c = getchar();
  238. while (c != '\n' && c != EOF)
  239. c = getchar();
  240. switch (first) {
  241. case EOF:
  242. putchar('\n');
  243. /* FALLTHROUGH */
  244. case 'n':
  245. case 'N':
  246. pw_error(NULL, 0, 0);
  247. break;
  248. }
  249. }
  250. static int
  251. pw_equal(const struct passwd *pw1, const struct passwd *pw2)
  252. {
  253. return (strcmp(pw1->pw_name, pw2->pw_name) == 0 &&
  254. pw1->pw_uid == pw2->pw_uid &&
  255. pw1->pw_gid == pw2->pw_gid &&
  256. strcmp(pw1->pw_class, pw2->pw_class) == 0 &&
  257. pw1->pw_change == pw2->pw_change &&
  258. pw1->pw_expire == pw2->pw_expire &&
  259. strcmp(pw1->pw_gecos, pw2->pw_gecos) == 0 &&
  260. strcmp(pw1->pw_dir, pw2->pw_dir) == 0 &&
  261. strcmp(pw1->pw_shell, pw2->pw_shell) == 0);
  262. }
  263. void
  264. pw_copy(int ffd, int tfd, const struct passwd *pw, const struct passwd *opw)
  265. {
  266. struct passwd tpw;
  267. FILE *from, *to;
  268. int done;
  269. char *p, *ep, buf[8192];
  270. char *master = pw_file(_PATH_MASTERPASSWD);
  271. if (!master)
  272. pw_error(NULL, 0, 1);
  273. if (!(from = fdopen(ffd, "r")))
  274. pw_error(master, 1, 1);
  275. if (!(to = fdopen(tfd, "w")))
  276. pw_error(pw_lck ? pw_lck : NULL, pw_lck ? 1 : 0, 1);
  277. for (done = 0; fgets(buf, (int)sizeof(buf), from);) {
  278. if ((ep = strchr(buf, '\n')) == NULL) {
  279. warnx("%s: line too long", master);
  280. pw_error(NULL, 0, 1);
  281. }
  282. if (done) {
  283. (void)fprintf(to, "%s", buf);
  284. if (ferror(to))
  285. goto fail;
  286. continue;
  287. }
  288. if (!(p = strchr(buf, ':'))) {
  289. warnx("%s: corrupted entry", master);
  290. pw_error(NULL, 0, 1);
  291. }
  292. *p = '\0';
  293. if (strcmp(buf, opw ? opw->pw_name : pw->pw_name)) {
  294. *p = ':';
  295. (void)fprintf(to, "%s", buf);
  296. if (ferror(to))
  297. goto fail;
  298. continue;
  299. }
  300. if (opw != NULL) {
  301. *p = ':';
  302. *ep = '\0';
  303. if (!pw_scan(buf, &tpw, NULL))
  304. pw_error(NULL, 0, 1);
  305. if (!pw_equal(&tpw, opw)) {
  306. warnx("%s: inconsistent entry", master);
  307. pw_error(NULL, 0, 1);
  308. }
  309. }
  310. (void)fprintf(to, "%s:%s:%u:%u:%s:%d:%d:%s:%s:%s\n",
  311. pw->pw_name, pw->pw_passwd, (u_int)pw->pw_uid,
  312. (u_int)pw->pw_gid, pw->pw_class, pw->pw_change,
  313. pw->pw_expire, pw->pw_gecos, pw->pw_dir,
  314. pw->pw_shell);
  315. done = 1;
  316. if (ferror(to))
  317. goto fail;
  318. }
  319. if (!done)
  320. (void)fprintf(to, "%s:%s:%d:%d:%s:%d:%d:%s:%s:%s\n",
  321. pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
  322. pw->pw_class, pw->pw_change, pw->pw_expire, pw->pw_gecos,
  323. pw->pw_dir, pw->pw_shell);
  324. if (ferror(to))
  325. fail:
  326. pw_error(NULL, 0, 1);
  327. free(master);
  328. (void)fclose(to);
  329. }
  330. int
  331. pw_scan(char *bp, struct passwd *pw, int *flags)
  332. {
  333. u_long id;
  334. int root;
  335. char *p, *sh, *p2;
  336. if (flags != (int *)NULL)
  337. *flags = 0;
  338. if (!(p = strsep(&bp, ":")) || *p == '\0') /* login */
  339. goto fmt;
  340. pw->pw_name = p;
  341. root = !strcmp(pw->pw_name, "root");
  342. if (!(pw->pw_passwd = strsep(&bp, ":"))) /* passwd */
  343. goto fmt;
  344. if (!(p = strsep(&bp, ":"))) /* uid */
  345. goto fmt;
  346. id = strtoul(p, &p2, 10);
  347. if (root && id) {
  348. warnx("root uid should be 0");
  349. return (0);
  350. }
  351. if (*p2 != '\0') {
  352. warnx("illegal uid field");
  353. return (0);
  354. }
  355. if (id > UID_MAX) {
  356. /* errno is set to ERANGE by strtoul(3) */
  357. warnx("uid greater than %u", UID_MAX-1);
  358. return (0);
  359. }
  360. pw->pw_uid = (uid_t)id;
  361. if ((*p == '\0') && (flags != (int *)NULL))
  362. *flags |= _PASSWORD_NOUID;
  363. if (!(p = strsep(&bp, ":"))) /* gid */
  364. goto fmt;
  365. id = strtoul(p, &p2, 10);
  366. if (*p2 != '\0') {
  367. warnx("illegal gid field");
  368. return (0);
  369. }
  370. if (id > UID_MAX) {
  371. /* errno is set to ERANGE by strtoul(3) */
  372. warnx("gid greater than %u", UID_MAX-1);
  373. return (0);
  374. }
  375. pw->pw_gid = (gid_t)id;
  376. if ((*p == '\0') && (flags != (int *)NULL))
  377. *flags |= _PASSWORD_NOGID;
  378. pw->pw_class = strsep(&bp, ":"); /* class */
  379. if (!(p = strsep(&bp, ":"))) /* change */
  380. goto fmt;
  381. pw->pw_change = atol(p);
  382. if ((*p == '\0') && (flags != (int *)NULL))
  383. *flags |= _PASSWORD_NOCHG;
  384. if (!(p = strsep(&bp, ":"))) /* expire */
  385. goto fmt;
  386. pw->pw_expire = atol(p);
  387. if ((*p == '\0') && (flags != (int *)NULL))
  388. *flags |= _PASSWORD_NOEXP;
  389. pw->pw_gecos = strsep(&bp, ":"); /* gecos */
  390. pw->pw_dir = strsep(&bp, ":"); /* directory */
  391. if (!(pw->pw_shell = strsep(&bp, ":"))) /* shell */
  392. goto fmt;
  393. p = pw->pw_shell;
  394. if (root && *p) { /* empty == /bin/sh */
  395. for (setusershell();;) {
  396. if (!(sh = getusershell())) {
  397. warnx("warning, unknown root shell");
  398. break;
  399. }
  400. if (!strcmp(p, sh))
  401. break;
  402. }
  403. endusershell();
  404. }
  405. if ((p = strsep(&bp, ":"))) { /* too many */
  406. fmt: warnx("corrupted entry");
  407. return (0);
  408. }
  409. return (1);
  410. }
  411. __dead void
  412. pw_error(const char *name, int error, int eval)
  413. {
  414. char *master = pw_file(_PATH_MASTERPASSWD);
  415. if (error) {
  416. if (name)
  417. warn("%s", name);
  418. else
  419. warn(NULL);
  420. }
  421. if (master) {
  422. warnx("%s: unchanged", master);
  423. free(master);
  424. }
  425. pw_abort();
  426. exit(eval);
  427. }