]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - usr.sbin/ypserv/yp_main.c
- Remove extraneous null ptr deref checks
[FreeBSD/stable/9.git] / usr.sbin / ypserv / yp_main.c
1 /*
2  * Copyright (c) 1995
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.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 /*
37  * ypserv startup function.
38  * We need out own main() since we have to do some additional work
39  * that rpcgen won't do for us. Most of this file was generated using
40  * rpcgen.new, and later modified.
41  */
42
43 #include <sys/types.h>
44 #include <sys/queue.h>
45 #include <sys/socket.h>
46 #include <sys/wait.h>
47 #include "yp.h"
48 #include <err.h>
49 #include <errno.h>
50 #include <memory.h>
51 #include <stdio.h>
52 #include <signal.h>
53 #include <stdarg.h>
54 #include <stdlib.h> /* getenv, exit */
55 #include <string.h> /* strcmp */
56 #include <syslog.h>
57 #include <unistd.h>
58 #ifdef __cplusplus
59 #include <sysent.h> /* getdtablesize, open */
60 #endif /* __cplusplus */
61 #include <netinet/in.h>
62 #include <netdb.h>
63 #include "yp_extern.h"
64 #include <netconfig.h>
65 #include <rpc/rpc.h>
66 #include <rpc/rpc_com.h>
67
68 #ifndef SIG_PF
69 #define SIG_PF void(*)(int)
70 #endif
71
72 #define _RPCSVC_CLOSEDOWN 120
73 int _rpcpmstart;                /* Started by a port monitor ? */
74 static int _rpcfdtype;
75                  /* Whether Stream or Datagram ? */
76 static int _rpcaf;
77 static int _rpcfd;
78
79         /* States a server can be in wrt request */
80
81 #define _IDLE 0
82 #define _SERVED 1
83 #define _SERVING 2
84
85 extern void ypprog_1(struct svc_req *, SVCXPRT *);
86 extern void ypprog_2(struct svc_req *, SVCXPRT *);
87 extern int _rpc_dtablesize(void);
88 extern int _rpcsvcstate;         /* Set when a request is serviced */
89 char *progname = "ypserv";
90 char *yp_dir = _PATH_YP;
91 /*int debug = 0;*/
92 int do_dns = 0;
93 int resfd;
94
95 struct socklistent {
96         int                             sle_sock;
97         struct sockaddr_storage         sle_ss;
98         SLIST_ENTRY(socklistent)        sle_next;
99 };
100 static SLIST_HEAD(, socklistent) sle_head =
101         SLIST_HEAD_INITIALIZER(sle_head);
102
103 struct bindaddrlistent {
104         const char                      *ble_hostname;
105         SLIST_ENTRY(bindaddrlistent)    ble_next;
106 };
107 static SLIST_HEAD(, bindaddrlistent) ble_head =
108         SLIST_HEAD_INITIALIZER(ble_head);
109
110 static char *servname = "0";
111
112 static
113 void _msgout(char* msg, ...)
114 {
115         va_list ap;
116
117         va_start(ap, msg);
118         if (debug) {
119                 if (_rpcpmstart)
120                         vsyslog(LOG_ERR, msg, ap);
121                 else
122                         vwarnx(msg, ap);
123         } else
124                 vsyslog(LOG_ERR, msg, ap);
125         va_end(ap);
126 }
127
128 pid_t   yp_pid;
129
130 static void
131 yp_svc_run(void)
132 {
133 #ifdef FD_SETSIZE
134         fd_set readfds;
135 #else
136         int readfds;
137 #endif /* def FD_SETSIZE */
138         int fd_setsize = _rpc_dtablesize();
139         struct timeval timeout;
140
141         /* Establish the identity of the parent ypserv process. */
142         yp_pid = getpid();
143
144         for (;;) {
145 #ifdef FD_SETSIZE
146                 readfds = svc_fdset;
147 #else
148                 readfds = svc_fds;
149 #endif /* def FD_SETSIZE */
150
151                 FD_SET(resfd, &readfds);
152
153                 timeout.tv_sec = RESOLVER_TIMEOUT;
154                 timeout.tv_usec = 0;
155                 switch (select(fd_setsize, &readfds, NULL, NULL,
156                                &timeout)) {
157                 case -1:
158                         if (errno == EINTR) {
159                                 continue;
160                         }
161                         warn("svc_run: - select failed");
162                         return;
163                 case 0:
164                         if (getpid() == yp_pid)
165                                 yp_prune_dnsq();
166                         break;
167                 default:
168                         if (getpid() == yp_pid) {
169                                 if (FD_ISSET(resfd, &readfds)) {
170                                         yp_run_dnsq();
171                                         FD_CLR(resfd, &readfds);
172                                 }
173                                 svc_getreqset(&readfds);
174                         }
175                 }
176                 if (yp_pid != getpid())
177                         _exit(0);
178         }
179 }
180
181 static void
182 unregister(void)
183 {
184         (void)svc_unreg(YPPROG, YPVERS);
185         (void)svc_unreg(YPPROG, YPOLDVERS);
186 }
187
188 static void
189 reaper(int sig)
190 {
191         int                     status;
192         int                     saved_errno;
193
194         saved_errno = errno;
195
196         if (sig == SIGHUP) {
197                 load_securenets();
198 #ifdef DB_CACHE
199                 yp_flush_all();
200 #endif
201                 errno = saved_errno;
202                 return;
203         }
204
205         if (sig == SIGCHLD) {
206                 while (wait3(&status, WNOHANG, NULL) > 0)
207                         children--;
208         } else {
209                 unregister();
210                 exit(0);
211         }
212         errno = saved_errno;
213         return;
214 }
215
216 static void
217 usage(void)
218 {
219         fprintf(stderr, "usage: ypserv [-h addr] [-d] [-n] [-p path] [-P port]\n");
220         exit(1);
221 }
222
223 static void
224 closedown(int sig)
225 {
226         if (_rpcsvcstate == _IDLE) {
227                 extern fd_set svc_fdset;
228                 static int size;
229                 int i, openfd;
230
231                 if (_rpcfdtype == SOCK_DGRAM) {
232                         unregister();
233                         exit(0);
234                 }
235                 if (size == 0) {
236                         size = getdtablesize();
237                 }
238                 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
239                         if (FD_ISSET(i, &svc_fdset))
240                                 openfd++;
241                 if (openfd <= 1) {
242                         unregister();
243                         exit(0);
244                 }
245         }
246         if (_rpcsvcstate == _SERVED)
247                 _rpcsvcstate = _IDLE;
248
249         (void) signal(SIGALRM, (SIG_PF) closedown);
250         (void) alarm(_RPCSVC_CLOSEDOWN/2);
251 }
252
253 static int
254 create_service(const int sock, const struct netconfig *nconf,
255         const struct __rpc_sockinfo *si)
256 {
257         int error;
258         char *sname;
259
260         SVCXPRT *transp;
261         struct addrinfo hints, *res, *res0;
262         struct socklistent *slep;
263         struct bindaddrlistent *blep;
264         struct netbuf svcaddr;
265
266         sname = NULL;
267         SLIST_INIT(&sle_head);
268         memset(&hints, 0, sizeof(hints));
269         memset(&svcaddr, 0, sizeof(svcaddr));
270
271         hints.ai_family = si->si_af;
272         hints.ai_socktype = si->si_socktype;
273         hints.ai_protocol = si->si_proto;
274
275         /*
276          * Build socketlist from bindaddrlist.
277          */
278         if (sock == RPC_ANYFD) {
279                 SLIST_FOREACH(blep, &ble_head, ble_next) {
280                         if (blep->ble_hostname == NULL)
281                                 hints.ai_flags = AI_PASSIVE;
282                         else
283                                 hints.ai_flags = 0;
284                         error = getaddrinfo(blep->ble_hostname, servname,
285                                     &hints, &res0);
286                         if (error) {
287                                 _msgout("getaddrinfo(): %s",
288                                     gai_strerror(error));
289                                 return -1;
290                         }
291                         for (res = res0; res; res = res->ai_next) {
292                                 int s;
293
294                                 s = __rpc_nconf2fd(nconf);
295                                 if (s < 0) {
296                                         if (errno == EPROTONOSUPPORT)
297                                                 _msgout("unsupported"
298                                                     " transport: %s",
299                                                     nconf->nc_netid);
300                                         else
301                                                 _msgout("cannot create"
302                                                     " %s socket: %s",
303                                                     nconf->nc_netid,
304                                                     strerror(errno));
305                                         freeaddrinfo(res0);
306                                         return -1;
307                                 }
308                                 if (bindresvport_sa(s, res->ai_addr) == -1) {
309                                         if ((errno != EPERM) ||
310                                             (bind(s, res->ai_addr,
311                                             res->ai_addrlen) == -1)) {
312                                                 _msgout("cannot bind "
313                                                     "%s socket: %s",
314                                                     nconf->nc_netid,
315                                                 strerror(errno));
316                                                 freeaddrinfo(res0);
317                                                 close(sock);
318                                                 return -1;
319                                         }
320                                 }
321                                 if (nconf->nc_semantics != NC_TPI_CLTS)
322                                         listen(s, SOMAXCONN);
323
324                                 slep = malloc(sizeof(*slep));
325                                 if (slep == NULL) {
326                                         _msgout("malloc failed: %s",
327                                             strerror(errno));
328                                         freeaddrinfo(res0);
329                                         close(s);
330                                         return -1;
331                                 }
332                                 memset(slep, 0, sizeof(*slep));
333                                 memcpy(&slep->sle_ss,
334                                     (struct sockaddr *)(res->ai_addr),
335                                     sizeof(res->ai_addr));
336                                 slep->sle_sock = s;
337                                 SLIST_INSERT_HEAD(&sle_head, slep, sle_next);
338
339                                 /*
340                                  * If servname == "0", redefine it by using
341                                  * the bound socket.
342                                  */
343                                 if (strncmp("0", servname, 1) == 0) {
344                                         struct sockaddr *sap;
345                                         socklen_t slen;
346
347                                         sname = malloc(NI_MAXSERV);
348                                         if (sname == NULL) {
349                                                 _msgout("malloc(): %s",
350                                                     strerror(errno));
351                                                 freeaddrinfo(res0);
352                                                 close(s);
353                                                 return -1;
354                                         }
355                                         memset(sname, 0, NI_MAXSERV);
356
357                                         sap = (struct sockaddr *)&slep->sle_ss;
358                                         slen = sizeof(*sap);
359                                         error = getsockname(s, sap, &slen);
360                                         if (error) {
361                                                 _msgout("getsockname(): %s",
362                                                     strerror(errno));
363                                                 freeaddrinfo(res0);
364                                                 close(s);
365                                                 free(sname);
366                                                 return -1;
367                                         }
368                                         error = getnameinfo(sap, slen,
369                                             NULL, 0,
370                                             sname, NI_MAXSERV,
371                                             NI_NUMERICHOST | NI_NUMERICSERV);
372                                         if (error) {
373                                                 _msgout("getnameinfo(): %s",
374                                                     strerror(errno));
375                                                 freeaddrinfo(res0);
376                                                 close(s);
377                                                 free(sname);
378                                                 return -1;
379                                         }
380                                         servname = sname;
381                                 }
382                         }
383                         freeaddrinfo(res0);
384                 }
385         } else {
386                 slep = malloc(sizeof(*slep));
387                 if (slep == NULL) {
388                         _msgout("malloc failed: %s", strerror(errno));
389                         return -1;
390                 }
391                 memset(slep, 0, sizeof(*slep));
392                 slep->sle_sock = sock;
393                 SLIST_INSERT_HEAD(&sle_head, slep, sle_next);
394         }
395
396         /*
397          * Traverse socketlist and create rpc service handles for each socket.
398          */
399         SLIST_FOREACH(slep, &sle_head, sle_next) {
400                 if (nconf->nc_semantics == NC_TPI_CLTS)
401                         transp = svc_dg_create(slep->sle_sock, 0, 0);
402                 else
403                         transp = svc_vc_create(slep->sle_sock, RPC_MAXDATASIZE,
404                             RPC_MAXDATASIZE);
405                 if (transp == NULL) {
406                         _msgout("unable to create service: %s",
407                             nconf->nc_netid);
408                         continue;
409                 }
410                 if (!svc_reg(transp, YPPROG, YPOLDVERS, ypprog_1, NULL)) {
411                         svc_destroy(transp);
412                         close(slep->sle_sock);
413                         _msgout("unable to register (YPPROG, YPOLDVERS, %s):"
414                             " %s", nconf->nc_netid, strerror(errno));
415                         continue;
416                 }
417                 if (!svc_reg(transp, YPPROG, YPVERS, ypprog_2, NULL)) {
418                         svc_destroy(transp);
419                         close(slep->sle_sock);
420                         _msgout("unable to register (YPPROG, YPVERS, %s): %s",
421                             nconf->nc_netid, strerror(errno));
422                         continue;
423                 }
424         }
425         while(!(SLIST_EMPTY(&sle_head)))
426                 SLIST_REMOVE_HEAD(&sle_head, sle_next);
427
428         /*
429          * Register RPC service to rpcbind by using AI_PASSIVE address.
430          */
431         hints.ai_flags = AI_PASSIVE;
432         error = getaddrinfo(NULL, servname, &hints, &res0);
433         if (error) {
434                 _msgout("getaddrinfo(): %s", gai_strerror(error));
435                 return -1;
436         }
437         svcaddr.buf = res0->ai_addr;
438         svcaddr.len = res0->ai_addrlen;
439
440         if (si->si_af == AF_INET) {
441                 /* XXX: ignore error intentionally */
442                 rpcb_set(YPPROG, YPOLDVERS, nconf, &svcaddr);
443         }
444         /* XXX: ignore error intentionally */
445         rpcb_set(YPPROG, YPVERS, nconf, &svcaddr);
446         free(sname);
447         freeaddrinfo(res0);
448         return 0;
449 }
450
451 int
452 main(int argc, char *argv[])
453 {
454         int ch;
455         int error;
456         int ntrans;
457         
458         void *nc_handle;
459         struct netconfig *nconf;
460         struct __rpc_sockinfo si;
461         struct bindaddrlistent *blep;
462
463         memset(&si, 0, sizeof(si));
464         SLIST_INIT(&ble_head);
465
466         while ((ch = getopt(argc, argv, "dh:np:P:")) != -1) {
467                 switch (ch) {
468                 case 'd':
469                         debug = ypdb_debug = 1;
470                         break;
471                 case 'h':
472                         blep = malloc(sizeof(*blep));
473                         if (blep == NULL)
474                                 err(1, "malloc() failed: -h %s", optarg);
475                         blep->ble_hostname = optarg;
476                         SLIST_INSERT_HEAD(&ble_head, blep, ble_next);
477                         break;
478                 case 'n':
479                         do_dns = 1;
480                         break;
481                 case 'p':
482                         yp_dir = optarg;
483                         break;
484                 case 'P':
485                         servname = optarg;
486                         break;
487                 default:
488                         usage();
489                 }
490         }
491         /*
492          * Add "anyaddr" entry if no -h is specified.
493          */
494         if (SLIST_EMPTY(&ble_head)) {
495                 blep = malloc(sizeof(*blep));
496                 if (blep == NULL)
497                         err(1, "malloc() failed");
498                 memset(blep, 0, sizeof(*blep));
499                 SLIST_INSERT_HEAD(&ble_head, blep, ble_next);
500         }
501
502         load_securenets();
503         yp_init_resolver();
504 #ifdef DB_CACHE
505         yp_init_dbs();
506 #endif
507         nc_handle = setnetconfig();
508         if (nc_handle == NULL)
509                 err(1, "cannot read %s", NETCONFIG);
510         if (__rpc_fd2sockinfo(0, &si) != 0) {
511                 /* invoked from inetd */
512                 _rpcpmstart = 1;
513                 _rpcfdtype = si.si_socktype;
514                 _rpcaf = si.si_af;
515                 _rpcfd = 0;
516                 openlog("ypserv", LOG_PID, LOG_DAEMON);
517         } else {
518                 /* standalone mode */
519                 if (!debug) {
520                         if (daemon(0,0)) {
521                                 err(1,"cannot fork");
522                         }
523                         openlog("ypserv", LOG_PID, LOG_DAEMON);
524                 }
525                 _rpcpmstart = 0;
526                 _rpcaf = AF_INET;
527                 _rpcfd = RPC_ANYFD;
528                 unregister();
529         }
530
531         /*
532          * Create RPC service for each transport.
533          */
534         ntrans = 0;
535         while((nconf = getnetconfig(nc_handle))) {
536                 if ((nconf->nc_flag & NC_VISIBLE)) {
537                         if (__rpc_nconf2sockinfo(nconf, &si) == 0) {
538                                 _msgout("cannot get information for %s.  "
539                                     "Ignored.", nconf->nc_netid);
540                                 continue;
541                         }
542                         if (_rpcpmstart) {
543                                 if (si.si_socktype != _rpcfdtype ||
544                                     si.si_af != _rpcaf)
545                                         continue;
546                         } else if (si.si_af != _rpcaf)
547                                         continue;
548                         error = create_service(_rpcfd, nconf, &si);
549                         if (error) {
550                                 endnetconfig(nc_handle);
551                                 exit(1);
552                         }
553                         ntrans++;
554                 }
555         }
556         endnetconfig(nc_handle);
557         while(!(SLIST_EMPTY(&ble_head)))
558                 SLIST_REMOVE_HEAD(&ble_head, ble_next);
559         if (ntrans == 0) {
560                 _msgout("no transport is available.  Aborted.");
561                 exit(1);
562         }
563         if (_rpcpmstart) {
564                 (void) signal(SIGALRM, (SIG_PF) closedown);
565                 (void) alarm(_RPCSVC_CLOSEDOWN/2);
566         }
567 /*
568  * Make sure SIGPIPE doesn't blow us away while servicing TCP
569  * connections.
570  */
571         (void) signal(SIGPIPE, SIG_IGN);
572         (void) signal(SIGCHLD, (SIG_PF) reaper);
573         (void) signal(SIGTERM, (SIG_PF) reaper);
574         (void) signal(SIGINT, (SIG_PF) reaper);
575         (void) signal(SIGHUP, (SIG_PF) reaper);
576         yp_svc_run();
577         _msgout("svc_run returned");
578         exit(1);
579         /* NOTREACHED */
580 }