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