2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1995, 1996
5 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/ioctl.h>
41 #include <sys/socket.h>
43 #include <sys/resource.h>
44 #include <netinet/in.h>
52 #include <stdlib.h> /* getenv, exit */
53 #include <string.h> /* strcmp */
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>
64 #include "yppasswdd_extern.h"
65 #include "yppasswd_private.h"
66 #include "ypxfr_extern.h"
67 #include "yp_extern.h"
70 #define SIG_PF void(*)(int)
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 */
88 static char _localhost[] = "localhost";
89 static char _passwd_byname[] = "passwd.byname";
90 extern int _rpcsvcstate; /* Set when a request is serviced */
91 static char _progname[] = "rpc.yppasswdd";
92 char *progname = _progname;
93 static char _yp_dir[] = _PATH_YP;
94 char *yp_dir = _yp_dir;
95 static char _passfile_default[] = _PATH_YP "master.passwd";
96 char *passfile_default = _passfile_default;
98 char *yppasswd_domain = NULL;
101 int allow_additions = 0;
106 char sockname[] = YP_SOCKNAME;
109 terminate(int sig __unused)
111 rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL);
112 rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL);
118 reload(int sig __unused)
124 closedown(int sig __unused)
126 if (_rpcsvcstate == _IDLE) {
127 extern fd_set svc_fdset;
131 if (_rpcfdtype == SOCK_DGRAM) {
136 size = getdtablesize();
138 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
139 if (FD_ISSET(i, &svc_fdset))
146 if (_rpcsvcstate == _SERVED)
147 _rpcsvcstate = _IDLE;
149 (void) signal(SIGALRM, (SIG_PF) closedown);
150 (void) alarm(_RPCSVC_CLOSEDOWN/2);
156 fprintf(stderr, "%s\n%s\n",
157 "usage: rpc.yppasswdd [-t master.passwd file] [-d domain] [-p path] [-s]",
158 " [-f] [-m] [-i] [-a] [-v] [-u] [-h]");
163 main(int argc, char *argv[])
166 SVCXPRT *transp = NULL;
167 struct sockaddr_in saddr;
168 socklen_t asize = sizeof (saddr);
169 struct netconfig *nconf;
174 char myname[MAXHOSTNAMELEN + 2];
175 int maxrec = RPC_MAXDATASIZE;
179 while ((ch = getopt(argc, argv, "t:d:p:sfamuivh")) != -1) {
182 passfile_default = optarg;
185 yppasswd_domain = optarg;
218 if (yppasswd_domain == NULL) {
219 if (yp_get_default_domain(&yppasswd_domain)) {
220 yp_error("no domain specified and system domain \
221 name isn't set -- aborting");
228 if (getrpcport(_localhost, YPPROG, YPVERS, IPPROTO_UDP) <= 0) {
229 yp_error("no ypserv processes registered with local portmap");
230 yp_error("this host is not an NIS server -- aborting");
234 if ((mastername = ypxfr_get_master(yppasswd_domain,
235 _passwd_byname, _localhost, 0)) == NULL) {
236 yp_error("can't get name of NIS master server for domain %s",
241 if (gethostname((char *)&myname, sizeof(myname)) == -1) {
242 yp_error("can't get local hostname: %s", strerror(errno));
246 if (strncasecmp(mastername, (char *)&myname, sizeof(myname))) {
247 yp_error("master of %s is %s, but we are %s",
248 "passwd.byname", mastername, myname);
249 yp_error("this host is not the NIS master server for \
250 the %s domain -- aborting", yppasswd_domain);
256 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
257 socklen_t ssize = sizeof (int);
258 if (saddr.sin_family != AF_INET)
260 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
261 (char *)&_rpcfdtype, &ssize) == -1)
266 if (!debug && _rpcpmstart == 0) {
268 err(1,"cannot fork");
271 openlog("rpc.yppasswdd", LOG_PID, LOG_DAEMON);
272 memset(&sa, 0, sizeof(sa));
273 sa.sa_flags = SA_NOCLDWAIT;
274 sigaction(SIGCHLD, &sa, NULL);
276 rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL);
277 rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL);
279 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
281 if (svc_create(yppasswdprog_1, YPPASSWDPROG, YPPASSWDVERS, "netpath") == 0) {
282 yp_error("cannot create yppasswd service.");
285 if (svc_create(master_yppasswdprog_1, MASTER_YPPASSWDPROG,
286 MASTER_YPPASSWDVERS, "netpath") == 0) {
287 yp_error("cannot create master_yppasswd service.");
292 localhandle = setnetconfig();
293 while ((nconf = getnetconfig(localhandle)) != NULL) {
294 if (nconf->nc_protofmly != NULL &&
295 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
299 yp_error("getnetconfigent unix: %s", nc_sperror());
303 transp = svcunix_create(RPC_ANYSOCK, 0, 0, sockname);
304 if (transp == NULL) {
305 yp_error("cannot create AF_LOCAL service.");
308 if (!svc_reg(transp, MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS,
309 master_yppasswdprog_1, nconf)) {
310 yp_error("unable to register (MASTER_YPPASSWDPROG, \
311 MASTER_YPPASSWDVERS, unix).");
314 endnetconfig(localhandle);
316 /* Only root may connect() to the AF_UNIX link. */
317 if (chmod(sockname, 0))
318 err(1, "chmod of %s failed", sockname);
320 if (transp == (SVCXPRT *)NULL) {
321 yp_error("could not create a handle");
325 (void) signal(SIGALRM, (SIG_PF) closedown);
326 (void) alarm(_RPCSVC_CLOSEDOWN/2);
329 /* Unlimited resource limits. */
330 rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
331 (void)setrlimit(RLIMIT_CPU, &rlim);
332 (void)setrlimit(RLIMIT_FSIZE, &rlim);
333 (void)setrlimit(RLIMIT_STACK, &rlim);
334 (void)setrlimit(RLIMIT_DATA, &rlim);
335 (void)setrlimit(RLIMIT_RSS, &rlim);
337 /* Don't drop core (not really necessary, but GP's). */
338 rlim.rlim_cur = rlim.rlim_max = 0;
339 (void)setrlimit(RLIMIT_CORE, &rlim);
341 /* Turn off signals. */
342 (void)signal(SIGALRM, SIG_IGN);
343 (void)signal(SIGHUP, (SIG_PF) reload);
344 (void)signal(SIGINT, SIG_IGN);
345 (void)signal(SIGPIPE, SIG_IGN);
346 (void)signal(SIGQUIT, SIG_IGN);
347 (void)signal(SIGTERM, (SIG_PF) terminate);
350 yp_error("svc_run returned");