]>
Commit | Line | Data |
---|---|---|
1815bff5 | 1 | /* |
cf37c299 | 2 | * Copyright (c) 1999-2016 Apple Inc. All rights reserved. |
1815bff5 A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
cf37c299 | 5 | * |
2fc1e207 A |
6 | * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights |
7 | * Reserved. This file contains Original Code and/or Modifications of | |
8 | * Original Code as defined in and that are subject to the Apple Public | |
9 | * Source License Version 1.0 (the 'License'). You may not use this file | |
10 | * except in compliance with the License. Please obtain a copy of the | |
11 | * License at http://www.apple.com/publicsource and read it before using | |
12 | * this file. | |
cf37c299 | 13 | * |
1815bff5 A |
14 | * The Original Code and all software distributed under the License are |
15 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2fc1e207 A |
18 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
19 | * License for the specific language governing rights and limitations | |
20 | * under the License." | |
cf37c299 | 21 | * |
1815bff5 A |
22 | * @APPLE_LICENSE_HEADER_END@ |
23 | */ | |
24 | /*- | |
25 | * Copyright (c) 1990, 1993, 1994 | |
26 | * The Regents of the University of California. All rights reserved. | |
27 | * | |
28 | * Redistribution and use in source and binary forms, with or without | |
29 | * modification, are permitted provided that the following conditions | |
30 | * are met: | |
31 | * 1. Redistributions of source code must retain the above copyright | |
32 | * notice, this list of conditions and the following disclaimer. | |
33 | * 2. Redistributions in binary form must reproduce the above copyright | |
34 | * notice, this list of conditions and the following disclaimer in the | |
35 | * documentation and/or other materials provided with the distribution. | |
36 | * 3. All advertising materials mentioning features or use of this software | |
37 | * must display the following acknowledgement: | |
38 | * This product includes software developed by the University of | |
39 | * California, Berkeley and its contributors. | |
40 | * 4. Neither the name of the University nor the names of its contributors | |
41 | * may be used to endorse or promote products derived from this software | |
42 | * without specific prior written permission. | |
43 | * | |
44 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
45 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
46 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
47 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
48 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
49 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
50 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
51 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
52 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
53 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
54 | * SUCH DAMAGE. | |
55 | */ | |
56 | ||
57 | #ifndef lint | |
34d340d7 A |
58 | #include <sys/cdefs.h> |
59 | __unused static char sccsid[] = "@(#)pw_util.c 8.4 (Berkeley) 4/28/95"; | |
1815bff5 A |
60 | #endif /* not lint */ |
61 | ||
62 | /* | |
63 | * This file is used by all the "password" programs; vipw(8), chpass(1), | |
64 | * and passwd(1). | |
65 | */ | |
66 | ||
67 | #include <sys/param.h> | |
68 | #include <sys/time.h> | |
69 | #include <sys/resource.h> | |
70 | #include <sys/stat.h> | |
71 | #include <sys/wait.h> | |
72 | ||
73 | #include <err.h> | |
74 | #include <errno.h> | |
75 | #include <fcntl.h> | |
76 | #include <paths.h> | |
77 | #include <pwd.h> | |
78 | #include <signal.h> | |
79 | #include <stdio.h> | |
80 | #include <stdlib.h> | |
81 | #include <string.h> | |
82 | #include <unistd.h> | |
83 | ||
84 | #include "pw_util.h" | |
85 | ||
86 | extern char *tempname; | |
87 | static pid_t editpid = -1; | |
88 | static int lockfd; | |
89 | ||
cf37c299 A |
90 | static void |
91 | pw_cont(int sig) | |
1815bff5 | 92 | { |
1815bff5 A |
93 | if (editpid != -1) |
94 | kill(editpid, sig); | |
95 | } | |
96 | ||
97 | void | |
cf37c299 | 98 | pw_init(void) |
1815bff5 A |
99 | { |
100 | struct rlimit rlim; | |
101 | ||
102 | /* Unlimited resource limits. */ | |
103 | rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY; | |
104 | (void)setrlimit(RLIMIT_CPU, &rlim); | |
105 | (void)setrlimit(RLIMIT_FSIZE, &rlim); | |
106 | (void)setrlimit(RLIMIT_STACK, &rlim); | |
107 | (void)setrlimit(RLIMIT_DATA, &rlim); | |
108 | (void)setrlimit(RLIMIT_RSS, &rlim); | |
109 | ||
110 | /* Don't drop core (not really necessary, but GP's). */ | |
111 | rlim.rlim_cur = rlim.rlim_max = 0; | |
112 | (void)setrlimit(RLIMIT_CORE, &rlim); | |
113 | ||
114 | /* Turn off signals. */ | |
115 | (void)signal(SIGALRM, SIG_IGN); | |
116 | (void)signal(SIGHUP, SIG_IGN); | |
117 | (void)signal(SIGINT, SIG_IGN); | |
118 | (void)signal(SIGPIPE, SIG_IGN); | |
119 | (void)signal(SIGQUIT, SIG_IGN); | |
120 | (void)signal(SIGTERM, SIG_IGN); | |
121 | (void)signal(SIGCONT, pw_cont); | |
122 | ||
123 | /* Create with exact permissions. */ | |
124 | (void)umask(0); | |
125 | } | |
126 | ||
127 | int | |
cf37c299 | 128 | pw_lock(void) |
1815bff5 | 129 | { |
cf37c299 | 130 | /* |
1815bff5 A |
131 | * If the master password file doesn't exist, the system is hosed. |
132 | * Might as well try to build one. Set the close-on-exec bit so | |
133 | * that users can't get at the encrypted passwords while editing. | |
134 | * Open should allow flock'ing the file; see 4.4BSD. XXX | |
135 | */ | |
136 | lockfd = open(_PATH_MASTERPASSWD, O_RDONLY, 0); | |
137 | if (lockfd < 0 || fcntl(lockfd, F_SETFD, 1) == -1) | |
138 | err(1, "%s", _PATH_MASTERPASSWD); | |
139 | if (flock(lockfd, LOCK_EX|LOCK_NB)) | |
140 | errx(1, "the password db file is busy"); | |
141 | return (lockfd); | |
142 | } | |
143 | ||
144 | int | |
cf37c299 | 145 | pw_tmp(void) |
1815bff5 A |
146 | { |
147 | static char path[MAXPATHLEN] = _PATH_MASTERPASSWD; | |
148 | int fd; | |
149 | char *p; | |
150 | ||
cf37c299 | 151 | if ((p = strrchr(path, '/'))) |
1815bff5 A |
152 | ++p; |
153 | else | |
154 | p = path; | |
155 | strcpy(p, "pw.XXXXXX"); | |
156 | if ((fd = mkstemp(path)) == -1) | |
157 | err(1, "%s", path); | |
158 | tempname = path; | |
159 | return (fd); | |
160 | } | |
161 | ||
162 | int | |
cf37c299 | 163 | pw_mkdb(void) |
1815bff5 A |
164 | { |
165 | int pstat; | |
166 | pid_t pid; | |
167 | ||
168 | warnx("rebuilding the database..."); | |
169 | (void)fflush(stderr); | |
170 | if (!(pid = vfork())) { | |
171 | execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p", tempname, NULL); | |
172 | pw_error(_PATH_PWD_MKDB, 1, 1); | |
173 | } | |
174 | pid = waitpid(pid, &pstat, 0); | |
175 | if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0) | |
176 | return (1); | |
177 | warnx("done"); | |
178 | return (0); | |
179 | } | |
180 | ||
181 | void | |
cf37c299 | 182 | pw_edit(int notsetuid) |
1815bff5 A |
183 | { |
184 | int pstat; | |
185 | char *p, *editor; | |
186 | ||
187 | if (!(editor = getenv("EDITOR"))) | |
188 | editor = _PATH_VI; | |
cf37c299 | 189 | if ((p = strrchr(editor, '/'))) |
1815bff5 | 190 | ++p; |
cf37c299 | 191 | else |
1815bff5 A |
192 | p = editor; |
193 | ||
194 | if (!(editpid = vfork())) { | |
195 | if (notsetuid) { | |
196 | (void)setgid(getgid()); | |
197 | (void)setuid(getuid()); | |
198 | } | |
199 | execlp(editor, p, tempname, NULL); | |
200 | _exit(1); | |
201 | } | |
202 | for (;;) { | |
203 | editpid = waitpid(editpid, (int *)&pstat, WUNTRACED); | |
204 | if (editpid == -1) | |
205 | pw_error(editor, 1, 1); | |
206 | else if (WIFSTOPPED(pstat)) | |
207 | raise(WSTOPSIG(pstat)); | |
208 | else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0) | |
209 | break; | |
210 | else | |
211 | pw_error(editor, 1, 1); | |
212 | } | |
213 | editpid = -1; | |
214 | } | |
215 | ||
216 | void | |
cf37c299 | 217 | pw_prompt(void) |
1815bff5 A |
218 | { |
219 | int c; | |
220 | ||
221 | (void)printf("re-edit the password file? [y]: "); | |
222 | (void)fflush(stdout); | |
223 | c = getchar(); | |
224 | if (c != EOF && c != '\n') | |
225 | while (getchar() != '\n'); | |
226 | if (c == 'n') | |
227 | pw_error(NULL, 0, 0); | |
228 | } | |
229 | ||
230 | void | |
cf37c299 | 231 | pw_error(char *name, int err, int eval) |
1815bff5 A |
232 | { |
233 | if (err) | |
c3a08f59 | 234 | warn("%s", name); |
1815bff5 A |
235 | |
236 | warnx("%s: unchanged", _PATH_MASTERPASSWD); | |
237 | (void)unlink(tempname); | |
238 | exit(eval); | |
239 | } |