2 * Copyright (c) 1995, 1996
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/usr.sbin/rpc.yppasswdd/yppasswdd_main.c,v 1.14.2.2 2002/02/15 00:46:57 des Exp $
33 * $DragonFly: src/usr.sbin/rpc.yppasswdd/yppasswdd_main.c,v 1.5 2005/11/25 01:32:49 swildner Exp $
38 #include <sys/types.h>
39 #include <stdlib.h> /* getenv, exit */
42 #include <sys/param.h>
43 #include <rpc/pmap_clnt.h> /* for pmap_unset */
44 #include <string.h> /* strcmp */
47 #include <sys/ioctl.h>
50 #include <sysent.h> /* getdtablesize, open */
51 #endif /* __cplusplus */
53 #include <sys/socket.h>
54 #include <netinet/in.h>
58 #include <rpcsvc/yp.h>
59 struct dom_binding
{};
60 #include <rpcsvc/ypclnt.h>
61 #include "yppasswdd_extern.h"
62 #include "yppasswd_private.h"
63 #include "ypxfr_extern.h"
66 #define SIG_PF void(*)(int)
73 #define _RPCSVC_CLOSEDOWN 120
74 int _rpcpmstart
= 0; /* Started by a port monitor ? */
75 static int _rpcfdtype
;
76 /* Whether Stream or Datagram ? */
77 /* States a server can be in wrt request */
83 extern int _rpcsvcstate
; /* Set when a request is serviced */
84 char *progname
= "rpc.yppasswdd";
85 char *yp_dir
= _PATH_YP
;
86 char *passfile_default
= _PATH_YP
"master.passwd";
88 char *yppasswd_domain
= NULL
;
91 int allow_additions
= 0;
96 char *sockname
= YP_SOCKNAME
;
101 svc_unregister(YPPASSWDPROG
, YPPASSWDVERS
);
102 svc_unregister(MASTER_YPPASSWDPROG
, MASTER_YPPASSWDVERS
);
116 if (_rpcsvcstate
== _IDLE
) {
117 extern fd_set svc_fdset
;
121 if (_rpcfdtype
== SOCK_DGRAM
) {
126 size
= getdtablesize();
128 for (i
= 0, openfd
= 0; i
< size
&& openfd
< 2; i
++)
129 if (FD_ISSET(i
, &svc_fdset
))
136 if (_rpcsvcstate
== _SERVED
)
137 _rpcsvcstate
= _IDLE
;
139 signal(SIGALRM
, (SIG_PF
) closedown
);
140 alarm(_RPCSVC_CLOSEDOWN
/2);
143 static void usage(void)
145 fprintf(stderr
, "%s\n%s\n",
146 "usage: rpc.yppasswdd [-t master.passwd file] [-d domain] [-p path] [-s]",
147 " [-f] [-m] [-i] [-a] [-v] [-u] [-h]");
152 main(int argc
, char *argv
[])
154 SVCXPRT
*transp
= NULL
;
157 struct sockaddr_in saddr
;
158 int asize
= sizeof (saddr
);
161 char myname
[MAXHOSTNAMELEN
+ 2];
166 while ((ch
= getopt(argc
, argv
, "t:d:p:sfamuivh")) != -1) {
169 passfile_default
= optarg
;
172 yppasswd_domain
= optarg
;
205 if (yppasswd_domain
== NULL
) {
206 if (yp_get_default_domain(&yppasswd_domain
)) {
207 yp_error("no domain specified and system domain \
208 name isn't set -- aborting");
215 if (getrpcport("localhost", YPPROG
, YPVERS
, IPPROTO_UDP
) <= 0) {
216 yp_error("no ypserv processes registered with local portmap");
217 yp_error("this host is not an NIS server -- aborting");
221 if ((mastername
= ypxfr_get_master(yppasswd_domain
, "passwd.byname",
222 "localhost",0)) == NULL
) {
223 yp_error("can't get name of NIS master server for domain %s",
228 if (gethostname((char *)&myname
, sizeof(myname
)) == -1) {
229 yp_error("can't get local hostname: %s", strerror(errno
));
233 if (strncasecmp(mastername
, (char *)&myname
, sizeof(myname
))) {
234 yp_error("master of %s is %s, but we are %s",
235 "passwd.byname", mastername
, myname
);
236 yp_error("this host is not the NIS master server for \
237 the %s domain -- aborting", yppasswd_domain
);
243 if (getsockname(0, (struct sockaddr
*)&saddr
, &asize
) == 0) {
244 int ssize
= sizeof (int);
246 if (saddr
.sin_family
!= AF_INET
)
248 if (getsockopt(0, SOL_SOCKET
, SO_TYPE
,
249 (char *)&_rpcfdtype
, &ssize
) == -1)
254 openlog("rpc.yppasswdd", LOG_PID
, LOG_DAEMON
);
258 err(1,"cannot fork");
261 openlog("rpc.yppasswdd", LOG_PID
, LOG_DAEMON
);
263 pmap_unset(YPPASSWDPROG
, YPPASSWDVERS
);
264 pmap_unset(MASTER_YPPASSWDPROG
, MASTER_YPPASSWDVERS
);
268 if ((_rpcfdtype
== 0) || (_rpcfdtype
== SOCK_DGRAM
)) {
269 transp
= svcudp_create(sock
);
270 if (transp
== NULL
) {
271 yp_error("cannot create udp service.");
276 if (!svc_register(transp
, YPPASSWDPROG
, YPPASSWDVERS
, yppasswdprog_1
, proto
)) {
277 yp_error("unable to register (YPPASSWDPROG, YPPASSWDVERS, udp).");
282 if ((_rpcfdtype
== 0) || (_rpcfdtype
== SOCK_STREAM
)) {
283 transp
= svctcp_create(sock
, 0, 0);
284 if (transp
== NULL
) {
285 yp_error("cannot create tcp service.");
290 if (!svc_register(transp
, YPPASSWDPROG
, YPPASSWDVERS
, yppasswdprog_1
, proto
)) {
291 yp_error("unable to register (YPPASSWDPROG, YPPASSWDVERS, tcp).");
297 transp
= svcunix_create(sock
, 0, 0, sockname
);
298 if (transp
== NULL
) {
299 yp_error("cannot create AF_LOCAL service.");
302 if (!svc_register(transp
, MASTER_YPPASSWDPROG
, MASTER_YPPASSWDVERS
, master_yppasswdprog_1
, 0)) {
303 yp_error("unable to register (MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, unix).");
306 /* Only root may connect() to the AF_UNIX link. */
307 if (chmod(sockname
, 0))
308 err(1, "chmod of %s failed", sockname
);
310 if (transp
== (SVCXPRT
*)NULL
) {
311 yp_error("could not create a handle");
315 signal(SIGALRM
, (SIG_PF
) closedown
);
316 alarm(_RPCSVC_CLOSEDOWN
/2);
318 /* set up resource limits and block signals */
321 /* except SIGCHLD, which we need to catch */
323 signal(SIGTERM
, (SIG_PF
) terminate
);
325 signal(SIGHUP
, (SIG_PF
) reload
);
328 yp_error("svc_run returned");