Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / rpc.yppasswdd / pw_util.c
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 char sccsid[] = "@(#)pw_util.c   8.3 (Berkeley) 4/2/94";
37 #endif
38 static const char rcsid[] =
39   "$FreeBSD: src/usr.sbin/rpc.yppasswdd/pw_util.c,v 1.4.2.1 2002/02/15 00:46:57 des 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/time.h>
49 #include <sys/resource.h>
50 #include <sys/stat.h>
51 #include <sys/wait.h>
52
53 #include <err.h>
54 #include <errno.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 #include "yppasswdd_extern.h"
66
67 int pstat;
68 pid_t pid;
69
70 void
71 pw_init()
72 {
73         struct rlimit rlim;
74
75         /* Unlimited resource limits. */
76         rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
77         (void)setrlimit(RLIMIT_CPU, &rlim);
78         (void)setrlimit(RLIMIT_FSIZE, &rlim);
79         (void)setrlimit(RLIMIT_STACK, &rlim);
80         (void)setrlimit(RLIMIT_DATA, &rlim);
81         (void)setrlimit(RLIMIT_RSS, &rlim);
82
83         /* Don't drop core (not really necessary, but GP's). */
84         rlim.rlim_cur = rlim.rlim_max = 0;
85         (void)setrlimit(RLIMIT_CORE, &rlim);
86
87         /* Turn off signals. */
88         /* (void)signal(SIGALRM, SIG_IGN); */
89         (void)signal(SIGHUP, SIG_IGN);
90         (void)signal(SIGINT, SIG_IGN);
91         (void)signal(SIGPIPE, SIG_IGN);
92         (void)signal(SIGQUIT, SIG_IGN);
93         (void)signal(SIGTSTP, SIG_IGN);
94         (void)signal(SIGTTOU, SIG_IGN);
95
96         /* Create with exact permissions. */
97         (void)umask(0);
98 }
99
100 static int lockfd;
101
102 int
103 pw_lock()
104 {
105         /*
106          * If the master password file doesn't exist, the system is hosed.
107          * Might as well try to build one.  Set the close-on-exec bit so
108          * that users can't get at the encrypted passwords while editing.
109          * Open should allow flock'ing the file; see 4.4BSD.    XXX
110          */
111         lockfd = open(passfile, O_RDONLY, 0);
112         if (lockfd < 0 || fcntl(lockfd, F_SETFD, 1) == -1) {
113                 yp_error("%s: %s", passfile, strerror(errno));
114                 return (-1);
115         }
116         if (flock(lockfd, LOCK_EX|LOCK_NB)) {
117                 yp_error("%s: the password db file is busy", passfile);
118                 return(-1);
119         }
120         return (lockfd);
121 }
122
123 int
124 pw_tmp()
125 {
126         static char path[MAXPATHLEN];
127         int fd;
128         char *p;
129
130         sprintf(path,"%s",passfile);
131         if ((p = strrchr(path, '/')))
132                 ++p;
133         else
134                 p = path;
135         strcpy(p, "pw.XXXXXX");
136         if ((fd = mkstemp(path)) == -1) {
137                 yp_error("%s: %s", path, strerror(errno));
138                 return(-1);
139         }
140         tempname = path;
141         return (fd);
142 }
143
144 int
145 pw_mkdb(username)
146 char *username;
147 {
148
149         yp_error("rebuilding the database...");
150         (void)fflush(stderr);
151         /* Temporarily turn off SIGCHLD catching */
152         install_reaper(0);
153         if (!(pid = vfork())) {
154                 if (!username) {
155                         execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p", tempname, NULL);
156                 } else {
157                         execl(_PATH_PWD_MKDB, "pwd_mkdb", "-p", "-u", username,
158                                                 tempname, NULL);
159                 }
160                 pw_error(_PATH_PWD_MKDB, 1, 1);
161                 return(-1);
162         }
163         /* Handle this ourselves. */
164         reaper(-1);
165         /* Put the handler back. Foo. */
166         install_reaper(1);
167         if (pid == -1 || !WIFEXITED(pstat) || WEXITSTATUS(pstat) != 0) {
168                 return (-1);
169         }
170         yp_error("done");
171         return (0);
172 }
173
174 void
175 pw_error(name, err, eval)
176         char *name;
177         int err, eval;
178 {
179         if (err && name != NULL)
180                 yp_error("%s", name);
181
182         yp_error("%s: unchanged", passfile);
183         (void)unlink(tempname);
184 }