yp*(8): Fix building with -fno-common.
[dragonfly.git] / usr.sbin / rpc.yppasswdd / yppasswdd_main.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1995, 1996
3 * Bill Paul <wpaul@ctr.columbia.edu>. 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 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.
19 *
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
30 * SUCH DAMAGE.
1de703da 31 *
ce0e08e2 32 * $FreeBSD: src/usr.sbin/rpc.yppasswdd/yppasswdd_main.c,v 1.27 2008/10/30 01:54:31 rafan Exp $
984263bc
MD
33 */
34
984263bc 35#include <sys/param.h>
984263bc
MD
36#include <sys/ioctl.h>
37#include <sys/stat.h>
984263bc 38#include <sys/socket.h>
ce0e08e2
PA
39#include <sys/time.h>
40#include <sys/resource.h>
984263bc 41#include <netinet/in.h>
ce0e08e2 42
984263bc
MD
43#include <err.h>
44#include <errno.h>
ce0e08e2
PA
45#include <fcntl.h>
46#include <memory.h>
47#include <signal.h>
48#include <stdio.h>
49#include <stdlib.h> /* getenv, exit */
ce0e08e2
PA
50#include <string.h> /* strcmp */
51#include <syslog.h>
52#include <unistd.h>
53
54#include <rpc/rpc.h>
55#include <rpc/rpc_com.h>
56#include <rpc/pmap_clnt.h> /* for pmap_unset */
984263bc 57#include <rpcsvc/yp.h>
984263bc 58#include <rpcsvc/ypclnt.h>
ce0e08e2
PA
59
60#include "yppasswd.h"
984263bc
MD
61#include "yppasswdd_extern.h"
62#include "yppasswd_private.h"
63#include "ypxfr_extern.h"
ce0e08e2 64#include "yp_extern.h"
984263bc
MD
65
66#ifndef SIG_PF
67#define SIG_PF void(*)(int)
68#endif
69
70#ifdef DEBUG
71#define RPC_SVC_FG
72#endif
73
74#define _RPCSVC_CLOSEDOWN 120
75int _rpcpmstart = 0; /* Started by a port monitor ? */
76static int _rpcfdtype;
77 /* Whether Stream or Datagram ? */
78 /* States a server can be in wrt request */
79
80#define _IDLE 0
81#define _SERVED 1
82#define _SERVING 2
04277bb2 83int debug;
984263bc 84
ce0e08e2
PA
85static char _localhost[] = "localhost";
86static char _passwd_byname[] = "passwd.byname";
984263bc 87extern int _rpcsvcstate; /* Set when a request is serviced */
ce0e08e2
PA
88static char _progname[] = "rpc.yppasswdd";
89char *progname = _progname;
90static char _yp_dir[] = _PATH_YP;
299d9671 91const char *yp_dir = _yp_dir;
ce0e08e2
PA
92static char _passfile_default[] = _PATH_YP "master.passwd";
93char *passfile_default = _passfile_default;
984263bc
MD
94char *passfile;
95char *yppasswd_domain = NULL;
96int no_chsh = 0;
97int no_chfn = 0;
98int allow_additions = 0;
99int multidomain = 0;
100int verbose = 0;
101int resvport = 1;
102int inplace = 0;
ce0e08e2 103char sockname[] = YP_SOCKNAME;
984263bc 104
2e3ed54d 105static void
ce0e08e2 106terminate(int sig __unused)
984263bc 107{
ce0e08e2
PA
108 rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL);
109 rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL);
984263bc
MD
110 unlink(sockname);
111 exit(0);
112}
113
2e3ed54d 114static void
ce0e08e2 115reload(int sig __unused)
984263bc
MD
116{
117 load_securenets();
118}
119
120static void
ce0e08e2 121closedown(int sig __unused)
984263bc
MD
122{
123 if (_rpcsvcstate == _IDLE) {
124 extern fd_set svc_fdset;
125 static int size;
126 int i, openfd;
127
128 if (_rpcfdtype == SOCK_DGRAM) {
129 unlink(sockname);
130 exit(0);
131 }
132 if (size == 0) {
133 size = getdtablesize();
134 }
135 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
136 if (FD_ISSET(i, &svc_fdset))
137 openfd++;
138 if (openfd <= 1) {
139 unlink(sockname);
140 exit(0);
141 }
142 }
143 if (_rpcsvcstate == _SERVED)
144 _rpcsvcstate = _IDLE;
145
71126e33
SW
146 signal(SIGALRM, (SIG_PF) closedown);
147 alarm(_RPCSVC_CLOSEDOWN/2);
984263bc
MD
148}
149
ce0e08e2
PA
150static void
151usage(void)
984263bc
MD
152{
153 fprintf(stderr, "%s\n%s\n",
154"usage: rpc.yppasswdd [-t master.passwd file] [-d domain] [-p path] [-s]",
155" [-f] [-m] [-i] [-a] [-v] [-u] [-h]");
156 exit(1);
157}
158
159int
2e3ed54d 160main(int argc, char *argv[])
984263bc 161{
ce0e08e2 162 struct rlimit rlim;
1ab20d67 163 SVCXPRT *transp = NULL;
984263bc 164 struct sockaddr_in saddr;
ce0e08e2
PA
165 socklen_t asize = sizeof (saddr);
166 struct netconfig *nconf;
167 struct sigaction sa;
168 void *localhandle;
984263bc
MD
169 int ch;
170 char *mastername;
171 char myname[MAXHOSTNAMELEN + 2];
ce0e08e2
PA
172 int maxrec = RPC_MAXDATASIZE;
173
984263bc
MD
174 debug = 1;
175
176 while ((ch = getopt(argc, argv, "t:d:p:sfamuivh")) != -1) {
177 switch (ch) {
178 case 't':
179 passfile_default = optarg;
180 break;
181 case 'd':
182 yppasswd_domain = optarg;
183 break;
184 case 's':
185 no_chsh++;
186 break;
187 case 'f':
188 no_chfn++;
189 break;
190 case 'p':
191 yp_dir = optarg;
192 break;
193 case 'a':
194 allow_additions++;
195 break;
196 case 'm':
197 multidomain++;
198 break;
199 case 'i':
200 inplace++;
201 break;
202 case 'v':
203 verbose++;
204 break;
205 case 'u':
206 resvport = 0;
207 break;
208 default:
209 case 'h':
210 usage();
211 break;
212 }
213 }
214
215 if (yppasswd_domain == NULL) {
216 if (yp_get_default_domain(&yppasswd_domain)) {
217 yp_error("no domain specified and system domain \
218name isn't set -- aborting");
219 usage();
220 }
221 }
222
223 load_securenets();
224
ce0e08e2 225 if (getrpcport(_localhost, YPPROG, YPVERS, IPPROTO_UDP) <= 0) {
984263bc
MD
226 yp_error("no ypserv processes registered with local portmap");
227 yp_error("this host is not an NIS server -- aborting");
228 exit(1);
229 }
230
ce0e08e2
PA
231 if ((mastername = ypxfr_get_master(yppasswd_domain,
232 _passwd_byname, _localhost, 0)) == NULL) {
984263bc
MD
233 yp_error("can't get name of NIS master server for domain %s",
234 yppasswd_domain);
235 exit(1);
236 }
237
238 if (gethostname((char *)&myname, sizeof(myname)) == -1) {
239 yp_error("can't get local hostname: %s", strerror(errno));
240 exit(1);
241 }
242
243 if (strncasecmp(mastername, (char *)&myname, sizeof(myname))) {
244 yp_error("master of %s is %s, but we are %s",
245 "passwd.byname", mastername, myname);
246 yp_error("this host is not the NIS master server for \
247the %s domain -- aborting", yppasswd_domain);
248 exit(1);
249 }
250
251 debug = 0;
252
253 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
ce0e08e2 254 socklen_t ssize = sizeof (int);
984263bc
MD
255 if (saddr.sin_family != AF_INET)
256 exit(1);
257 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
ce0e08e2 258 (char *)&_rpcfdtype, &ssize) == -1)
984263bc 259 exit(1);
984263bc 260 _rpcpmstart = 1;
984263bc
MD
261 }
262
ce0e08e2
PA
263 if (!debug && _rpcpmstart == 0) {
264 if (daemon(0,0)) {
265 err(1,"cannot fork");
984263bc
MD
266 }
267 }
ce0e08e2
PA
268 openlog("rpc.yppasswdd", LOG_PID, LOG_DAEMON);
269 memset(&sa, 0, sizeof(sa));
270 sa.sa_flags = SA_NOCLDWAIT;
271 sigaction(SIGCHLD, &sa, NULL);
984263bc 272
ce0e08e2
PA
273 rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL);
274 rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL);
275
276 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
277
278 if (svc_create(yppasswdprog_1, YPPASSWDPROG, YPPASSWDVERS, "netpath") == 0) {
279 yp_error("cannot create yppasswd service.");
280 exit(1);
281 }
282 if (svc_create(master_yppasswdprog_1, MASTER_YPPASSWDPROG,
283 MASTER_YPPASSWDVERS, "netpath") == 0) {
284 yp_error("cannot create master_yppasswd service.");
285 exit(1);
984263bc
MD
286 }
287
ce0e08e2
PA
288 nconf = NULL;
289 localhandle = setnetconfig();
290 while ((nconf = getnetconfig(localhandle)) != NULL) {
291 if (nconf->nc_protofmly != NULL &&
292 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
293 break;
294 }
295 if (nconf == NULL) {
296 yp_error("getnetconfigent unix: %s", nc_sperror());
297 exit(1);
298 }
984263bc 299 unlink(sockname);
ce0e08e2 300 transp = svcunix_create(RPC_ANYSOCK, 0, 0, sockname);
984263bc
MD
301 if (transp == NULL) {
302 yp_error("cannot create AF_LOCAL service.");
303 exit(1);
304 }
ce0e08e2
PA
305 if (!svc_reg(transp, MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS,
306 master_yppasswdprog_1, nconf)) {
307 yp_error("unable to register (MASTER_YPPASSWDPROG, \
308 MASTER_YPPASSWDVERS, unix).");
984263bc
MD
309 exit(1);
310 }
ce0e08e2
PA
311 endnetconfig(localhandle);
312
984263bc
MD
313 /* Only root may connect() to the AF_UNIX link. */
314 if (chmod(sockname, 0))
315 err(1, "chmod of %s failed", sockname);
316
2038fb68 317 if (transp == NULL) {
984263bc
MD
318 yp_error("could not create a handle");
319 exit(1);
320 }
321 if (_rpcpmstart) {
71126e33
SW
322 signal(SIGALRM, (SIG_PF) closedown);
323 alarm(_RPCSVC_CLOSEDOWN/2);
984263bc 324 }
984263bc 325
ce0e08e2
PA
326 /* Unlimited resource limits. */
327 rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
328 setrlimit(RLIMIT_CPU, &rlim);
329 setrlimit(RLIMIT_FSIZE, &rlim);
330 setrlimit(RLIMIT_STACK, &rlim);
331 setrlimit(RLIMIT_DATA, &rlim);
332 setrlimit(RLIMIT_RSS, &rlim);
333
334 /* Don't drop core (not really necessary, but GP's). */
335 rlim.rlim_cur = rlim.rlim_max = 0;
336 setrlimit(RLIMIT_CORE, &rlim);
984263bc 337
ce0e08e2
PA
338 /* Turn off signals. */
339 signal(SIGALRM, SIG_IGN);
984263bc 340 signal(SIGHUP, (SIG_PF) reload);
ce0e08e2
PA
341 signal(SIGINT, SIG_IGN);
342 signal(SIGPIPE, SIG_IGN);
343 signal(SIGQUIT, SIG_IGN);
344 signal(SIGTERM, (SIG_PF) terminate);
984263bc
MD
345
346 svc_run();
347 yp_error("svc_run returned");
348 exit(1);
349 /* NOTREACHED */
350}