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