Commit | Line | Data |
---|---|---|
4cea479e | 1 | /*- |
d78dcdff | 2 | * Copyright (c) 1990, 1993, 1994 |
d928d407 | 3 | * The Regents of the University of California. All rights reserved. |
4cea479e KB |
4 | * |
5 | * %sccs.include.redist.c% | |
6 | */ | |
7 | ||
8 | #ifndef lint | |
d78dcdff | 9 | static char sccsid[] = "@(#)pw_util.c 8.3 (Berkeley) %G%"; |
4cea479e KB |
10 | #endif /* not lint */ |
11 | ||
12 | /* | |
13 | * This file is used by all the "password" programs; vipw(8), chpass(1), | |
14 | * and passwd(1). | |
15 | */ | |
16 | ||
17 | #include <sys/param.h> | |
4cea479e KB |
18 | #include <sys/time.h> |
19 | #include <sys/resource.h> | |
93178cc6 JSP |
20 | #include <sys/stat.h> |
21 | #include <sys/wait.h> | |
d95cae32 | 22 | |
93178cc6 | 23 | #include <err.h> |
d95cae32 | 24 | #include <errno.h> |
4cea479e | 25 | #include <fcntl.h> |
d95cae32 | 26 | #include <paths.h> |
4cea479e | 27 | #include <pwd.h> |
d95cae32 | 28 | #include <signal.h> |
4cea479e | 29 | #include <stdio.h> |
4cea479e | 30 | #include <stdlib.h> |
d95cae32 | 31 | #include <string.h> |
93178cc6 JSP |
32 | #include <unistd.h> |
33 | ||
34 | #include "pw_util.h" | |
4cea479e | 35 | |
4cea479e KB |
36 | extern char *tempname; |
37 | ||
93178cc6 | 38 | void |
4cea479e KB |
39 | pw_init() |
40 | { | |
41 | struct rlimit rlim; | |
4cea479e | 42 | |
f50531a3 | 43 | /* Unlimited resource limits. */ |
4cea479e KB |
44 | rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY; |
45 | (void)setrlimit(RLIMIT_CPU, &rlim); | |
46 | (void)setrlimit(RLIMIT_FSIZE, &rlim); | |
f50531a3 KB |
47 | (void)setrlimit(RLIMIT_STACK, &rlim); |
48 | (void)setrlimit(RLIMIT_DATA, &rlim); | |
49 | (void)setrlimit(RLIMIT_RSS, &rlim); | |
4cea479e KB |
50 | |
51 | /* Don't drop core (not really necessary, but GP's). */ | |
52 | rlim.rlim_cur = rlim.rlim_max = 0; | |
53 | (void)setrlimit(RLIMIT_CORE, &rlim); | |
54 | ||
f50531a3 KB |
55 | /* Turn off signals. */ |
56 | (void)signal(SIGALRM, SIG_IGN); | |
1b7ac17b KB |
57 | (void)signal(SIGHUP, SIG_IGN); |
58 | (void)signal(SIGINT, SIG_IGN); | |
f50531a3 | 59 | (void)signal(SIGPIPE, SIG_IGN); |
1b7ac17b KB |
60 | (void)signal(SIGQUIT, SIG_IGN); |
61 | (void)signal(SIGTERM, SIG_IGN); | |
62 | (void)signal(SIGTSTP, SIG_IGN); | |
f50531a3 | 63 | (void)signal(SIGTTOU, SIG_IGN); |
4cea479e KB |
64 | |
65 | /* Create with exact permissions. */ | |
66 | (void)umask(0); | |
67 | } | |
68 | ||
69 | static int lockfd; | |
93178cc6 JSP |
70 | |
71 | int | |
4cea479e KB |
72 | pw_lock() |
73 | { | |
74 | /* | |
75 | * If the master password file doesn't exist, the system is hosed. | |
d95cae32 KB |
76 | * Might as well try to build one. Set the close-on-exec bit so |
77 | * that users can't get at the encrypted passwords while editing. | |
4cea479e KB |
78 | * Open should allow flock'ing the file; see 4.4BSD. XXX |
79 | */ | |
80 | lockfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0); | |
93178cc6 JSP |
81 | if (lockfd < 0 || fcntl(lockfd, F_SETFD, 1) == -1) |
82 | err(1, "%s", _PATH_MASTERPASSWD); | |
83 | if (flock(lockfd, LOCK_EX|LOCK_NB)) | |
84 | errx(1, "the password db file is busy"); | |
85 | return (lockfd); | |
4cea479e KB |
86 | } |
87 | ||
93178cc6 | 88 | int |
4cea479e KB |
89 | pw_tmp() |
90 | { | |
91 | static char path[MAXPATHLEN] = _PATH_MASTERPASSWD; | |
92 | int fd; | |
93 | char *p; | |
94 | ||
93178cc6 | 95 | if (p = strrchr(path, '/')) |
4cea479e KB |
96 | ++p; |
97 | else | |
98 | p = path; | |
93178cc6 JSP |
99 | strcpy(p, "pw.XXXXXX"); |
100 | if ((fd = mkstemp(path)) == -1) | |
101 | err(1, "%s", path); | |
4cea479e | 102 | tempname = path; |
93178cc6 | 103 | return (fd); |
4cea479e KB |
104 | } |
105 | ||
93178cc6 | 106 | int |
4cea479e KB |
107 | pw_mkdb() |
108 | { | |
93178cc6 | 109 | int pstat; |
4cea479e KB |
110 | pid_t pid; |
111 | ||
93178cc6 JSP |
112 | warnx("rebuilding the database..."); |
113 | (void)fflush(stderr); | |
4cea479e KB |
114 | if (!(pid = vfork())) { |
115 | execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p", tempname, NULL); | |
116 | pw_error(_PATH_PWD_MKDB, 1, 1); | |
117 | } | |
93178cc6 JSP |
118 | pid = waitpid(pid, &pstat, 0); |
119 | if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0) | |
120 | return (0); | |
121 | warnx("done"); | |
122 | return (1); | |
4cea479e KB |
123 | } |
124 | ||
93178cc6 | 125 | void |
4cea479e KB |
126 | pw_edit(notsetuid) |
127 | int notsetuid; | |
128 | { | |
93178cc6 | 129 | int pstat; |
4cea479e KB |
130 | pid_t pid; |
131 | char *p, *editor; | |
132 | ||
133 | if (!(editor = getenv("EDITOR"))) | |
134 | editor = _PATH_VI; | |
93178cc6 | 135 | if (p = strrchr(editor, '/')) |
4cea479e KB |
136 | ++p; |
137 | else | |
138 | p = editor; | |
139 | ||
140 | if (!(pid = vfork())) { | |
141 | if (notsetuid) { | |
142 | (void)setgid(getgid()); | |
143 | (void)setuid(getuid()); | |
144 | } | |
145 | execlp(editor, p, tempname, NULL); | |
36c2926d | 146 | _exit(1); |
4cea479e KB |
147 | } |
148 | pid = waitpid(pid, (int *)&pstat, 0); | |
93178cc6 | 149 | if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0) |
36c2926d | 150 | pw_error(editor, 1, 1); |
4cea479e KB |
151 | } |
152 | ||
93178cc6 | 153 | void |
4cea479e KB |
154 | pw_prompt() |
155 | { | |
93178cc6 JSP |
156 | int c; |
157 | ||
158 | (void)printf("re-edit the password file? [y]: "); | |
159 | (void)fflush(stdout); | |
160 | c = getchar(); | |
161 | if (c != EOF && c != '\n') | |
162 | while (getchar() != '\n'); | |
163 | if (c == 'n') | |
164 | pw_error(NULL, 0, 0); | |
4cea479e KB |
165 | } |
166 | ||
93178cc6 | 167 | void |
4cea479e KB |
168 | pw_error(name, err, eval) |
169 | char *name; | |
170 | int err, eval; | |
171 | { | |
93178cc6 JSP |
172 | if (err) |
173 | warn(name); | |
174 | ||
175 | warnx("%s: unchanged", _PATH_MASTERPASSWD); | |
4cea479e KB |
176 | (void)unlink(tempname); |
177 | exit(eval); | |
178 | } |