]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/rpc.yppasswdd/yppasswdd_main.c
Remove spurious newline
[FreeBSD/FreeBSD.git] / usr.sbin / rpc.yppasswdd / yppasswdd_main.c
1 /*
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1995, 1996
5  *      Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
40 #include <sys/stat.h>
41 #include <sys/socket.h>
42 #include <sys/time.h>
43 #include <sys/resource.h>
44 #include <netinet/in.h>
45
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <memory.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h> /* getenv, exit */
53 #include <string.h> /* strcmp */
54 #include <syslog.h>
55 #include <unistd.h>
56
57 #include <rpc/rpc.h>
58 #include <rpc/rpc_com.h>
59 #include <rpc/pmap_clnt.h> /* for pmap_unset */
60 #include <rpcsvc/yp.h>
61 #include <rpcsvc/ypclnt.h>
62
63 #include "yppasswd.h"
64 #include "yppasswdd_extern.h"
65 #include "yppasswd_private.h"
66 #include "ypxfr_extern.h"
67 #include "yp_extern.h"
68
69 #ifndef SIG_PF
70 #define SIG_PF void(*)(int)
71 #endif
72
73 #ifdef DEBUG
74 #define RPC_SVC_FG
75 #endif
76
77 #define _RPCSVC_CLOSEDOWN 120
78 int _rpcpmstart = 0;            /* Started by a port monitor ? */
79 static int _rpcfdtype;
80                  /* Whether Stream or Datagram ? */
81         /* States a server can be in wrt request */
82
83 #define _IDLE 0
84 #define _SERVED 1
85 #define _SERVING 2
86
87 static char _localhost[] = "localhost";
88 static char _passwd_byname[] = "passwd.byname";
89 extern int _rpcsvcstate;         /* Set when a request is serviced */
90 static char _progname[] = "rpc.yppasswdd";
91 char *progname = _progname;
92 static char _yp_dir[] = _PATH_YP;
93 char *yp_dir = _yp_dir;
94 static char _passfile_default[] = _PATH_YP "master.passwd";
95 char *passfile_default = _passfile_default;
96 char *passfile;
97 char *yppasswd_domain = NULL;
98 int no_chsh = 0;
99 int no_chfn = 0;
100 int allow_additions = 0;
101 int multidomain = 0;
102 int verbose = 0;
103 int resvport = 1;
104 int inplace = 0;
105 char sockname[] = YP_SOCKNAME;
106
107 static void
108 terminate(int sig __unused)
109 {
110         rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL);
111         rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL);
112         unlink(sockname);
113         exit(0);
114 }
115
116 static void
117 reload(int sig __unused)
118 {
119         load_securenets();
120 }
121
122 static void
123 closedown(int sig __unused)
124 {
125         if (_rpcsvcstate == _IDLE) {
126                 extern fd_set svc_fdset;
127                 static int size;
128                 int i, openfd;
129
130                 if (_rpcfdtype == SOCK_DGRAM) {
131                         unlink(sockname);
132                         exit(0);
133                 }
134                 if (size == 0) {
135                         size = getdtablesize();
136                 }
137                 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
138                         if (FD_ISSET(i, &svc_fdset))
139                                 openfd++;
140                 if (openfd <= 1) {
141                         unlink(sockname);
142                         exit(0);
143                 }
144         }
145         if (_rpcsvcstate == _SERVED)
146                 _rpcsvcstate = _IDLE;
147
148         (void) signal(SIGALRM, (SIG_PF) closedown);
149         (void) alarm(_RPCSVC_CLOSEDOWN/2);
150 }
151
152 static void
153 usage(void)
154 {
155         fprintf(stderr, "%s\n%s\n",
156 "usage: rpc.yppasswdd [-t master.passwd file] [-d domain] [-p path] [-s]",
157 "                     [-f] [-m] [-i] [-a] [-v] [-u] [-h]");
158         exit(1);
159 }
160
161 int
162 main(int argc, char *argv[])
163 {
164         struct rlimit rlim;
165         SVCXPRT *transp = NULL;
166         struct sockaddr_in saddr;
167         socklen_t asize = sizeof (saddr);
168         struct netconfig *nconf;
169         struct sigaction sa;
170         void *localhandle;
171         int ch;
172         char *mastername;
173         char myname[MAXHOSTNAMELEN + 2];
174         int maxrec = RPC_MAXDATASIZE;
175
176         extern int debug;
177
178         debug = 1;
179
180         while ((ch = getopt(argc, argv, "t:d:p:sfamuivh")) != -1) {
181                 switch (ch) {
182                 case 't':
183                         passfile_default = optarg;
184                         break;
185                 case 'd':
186                         yppasswd_domain = optarg;
187                         break;
188                 case 's':
189                         no_chsh++;
190                         break;
191                 case 'f':
192                         no_chfn++;
193                         break;
194                 case 'p':
195                         yp_dir = optarg;
196                         break;
197                 case 'a':
198                         allow_additions++;
199                         break;
200                 case 'm':
201                         multidomain++;
202                         break;
203                 case 'i':
204                         inplace++;
205                         break;
206                 case 'v':
207                         verbose++;
208                         break;
209                 case 'u':
210                         resvport = 0;
211                         break;
212                 default:
213                 case 'h':
214                         usage();
215                         break;
216                 }
217         }
218
219         if (yppasswd_domain == NULL) {
220                 if (yp_get_default_domain(&yppasswd_domain)) {
221                         yp_error("no domain specified and system domain \
222 name isn't set -- aborting");
223                 usage();
224                 }
225         }
226
227         load_securenets();
228
229         if (getrpcport(_localhost, YPPROG, YPVERS, IPPROTO_UDP) <= 0) {
230                 yp_error("no ypserv processes registered with local portmap");
231                 yp_error("this host is not an NIS server -- aborting");
232                 exit(1);
233         }
234
235         if ((mastername = ypxfr_get_master(yppasswd_domain,
236                  _passwd_byname, _localhost, 0)) == NULL) {
237                 yp_error("can't get name of NIS master server for domain %s",
238                                                         yppasswd_domain);
239                 exit(1);
240         }
241
242         if (gethostname((char *)&myname, sizeof(myname)) == -1) {
243                 yp_error("can't get local hostname: %s", strerror(errno));
244                 exit(1);
245         }
246
247         if (strncasecmp(mastername, (char *)&myname, sizeof(myname))) {
248                 yp_error("master of %s is %s, but we are %s",
249                         "passwd.byname", mastername, myname);
250                 yp_error("this host is not the NIS master server for \
251 the %s domain -- aborting", yppasswd_domain);
252                 exit(1);
253         }
254
255         debug = 0;
256
257         if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
258                 socklen_t ssize = sizeof (int);
259                 if (saddr.sin_family != AF_INET)
260                         exit(1);
261                 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
262                     (char *)&_rpcfdtype, &ssize) == -1)
263                         exit(1);
264                 _rpcpmstart = 1;
265         }
266
267         if (!debug && _rpcpmstart == 0) {
268                 if (daemon(0,0)) {
269                         err(1,"cannot fork");
270                 }
271         }
272         openlog("rpc.yppasswdd", LOG_PID, LOG_DAEMON);
273         memset(&sa, 0, sizeof(sa));
274         sa.sa_flags = SA_NOCLDWAIT;
275         sigaction(SIGCHLD, &sa, NULL);
276
277         rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL);
278         rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL);
279
280         rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
281
282         if (svc_create(yppasswdprog_1, YPPASSWDPROG, YPPASSWDVERS, "netpath") == 0) {
283                 yp_error("cannot create yppasswd service.");
284                 exit(1);
285         }
286         if (svc_create(master_yppasswdprog_1, MASTER_YPPASSWDPROG,
287             MASTER_YPPASSWDVERS, "netpath") == 0) {
288                 yp_error("cannot create master_yppasswd service.");
289                 exit(1);
290         }
291
292         nconf = NULL;
293         localhandle = setnetconfig();
294         while ((nconf = getnetconfig(localhandle)) != NULL) {
295                 if (nconf->nc_protofmly != NULL &&
296                     strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
297                         break;
298         }
299         if (nconf == NULL) {
300                 yp_error("getnetconfigent unix: %s", nc_sperror());
301                 exit(1);
302         }
303         unlink(sockname);
304         transp = svcunix_create(RPC_ANYSOCK, 0, 0, sockname);
305         if (transp == NULL) {
306                 yp_error("cannot create AF_LOCAL service.");
307                 exit(1);
308         }
309         if (!svc_reg(transp, MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS,
310             master_yppasswdprog_1, nconf)) {
311                 yp_error("unable to register (MASTER_YPPASSWDPROG, \
312                     MASTER_YPPASSWDVERS, unix).");
313                 exit(1);
314         }
315         endnetconfig(localhandle);
316
317         /* Only root may connect() to the AF_UNIX link. */
318         if (chmod(sockname, 0))
319                 err(1, "chmod of %s failed", sockname);
320
321         if (transp == (SVCXPRT *)NULL) {
322                 yp_error("could not create a handle");
323                 exit(1);
324         }
325         if (_rpcpmstart) {
326                 (void) signal(SIGALRM, (SIG_PF) closedown);
327                 (void) alarm(_RPCSVC_CLOSEDOWN/2);
328         }
329
330         /* Unlimited resource limits. */
331         rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
332         (void)setrlimit(RLIMIT_CPU, &rlim);
333         (void)setrlimit(RLIMIT_FSIZE, &rlim);
334         (void)setrlimit(RLIMIT_STACK, &rlim);
335         (void)setrlimit(RLIMIT_DATA, &rlim);
336         (void)setrlimit(RLIMIT_RSS, &rlim);
337
338         /* Don't drop core (not really necessary, but GP's). */
339         rlim.rlim_cur = rlim.rlim_max = 0;
340         (void)setrlimit(RLIMIT_CORE, &rlim);
341
342         /* Turn off signals. */
343         (void)signal(SIGALRM, SIG_IGN);
344         (void)signal(SIGHUP, (SIG_PF) reload);
345         (void)signal(SIGINT, SIG_IGN);
346         (void)signal(SIGPIPE, SIG_IGN);
347         (void)signal(SIGQUIT, SIG_IGN);
348         (void)signal(SIGTERM, (SIG_PF) terminate);
349
350         svc_run();
351         yp_error("svc_run returned");
352         exit(1);
353         /* NOTREACHED */
354 }