~ubuntu-branches/ubuntu/oneiric/nis/oneiric-proposed

« back to all changes in this revision

Viewing changes to ypserv-2.18/rpc.ypxfrd/ypxfrd.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2005-11-16 23:42:06 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20051116234206-p00omaw5ji5q0qhr
Tags: 3.15-3ubuntu1
Resynchronise with Debian.  (me)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2005 Thorsten Kukuk
 
2
   Author: Thorsten Kukuk <kukuk@suse.de>
 
3
 
 
4
   The YP Server is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU General Public License
 
6
   version 2 as published by the Free Software Foundation.
 
7
 
 
8
   The YP Server is distributed in the hope that it will be useful,
 
9
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
   General Public License for more details.
 
12
 
 
13
   You should have received a copy of the GNU General Public
 
14
   License along with the YP Server; see the file COPYING. If
 
15
   not, write to the Free Software Foundation, Inc., 675 Mass Ave,
 
16
   Cambridge, MA 02139, USA. */
 
17
 
 
18
/* ypxfrd - ypxfrd main routines.  */
 
19
 
 
20
#define _GNU_SOURCE
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
#include "config.h"
 
24
#endif
 
25
 
 
26
#include <stdio.h>
 
27
#include <errno.h>
 
28
#include <fcntl.h>
 
29
#include <stdarg.h>
 
30
#include <stdlib.h>
 
31
#include <string.h>
 
32
#include <memory.h>
 
33
#include <unistd.h>
 
34
#include <syslog.h>
 
35
#ifndef LOG_DAEMON
 
36
#include <sys/syslog.h>
 
37
#endif
 
38
#include <signal.h>
 
39
#include <sys/wait.h>
 
40
#include <sys/socket.h>
 
41
#include <sys/types.h>
 
42
#include <sys/stat.h>
 
43
#include <rpc/rpc.h>
 
44
#ifdef HAVE_RPC_SVC_SOC_H
 
45
#include <rpc/svc_soc.h>
 
46
#endif /* HAVE_RPC_SVC_SOC_H */
 
47
#include <rpc/pmap_clnt.h>
 
48
#ifdef HAVE_GETOPT_H
 
49
#include <getopt.h>
 
50
#endif /* HAVE_GETOPT_H */
 
51
#include "ypxfrd.h"
 
52
#include "access.h"
 
53
#include "ypserv_conf.h"
 
54
 
 
55
#ifndef SA_RESTART
 
56
#define SA_RESTART 0
 
57
#endif
 
58
 
 
59
#include "log_msg.h"
 
60
#include "compat.h"
 
61
 
 
62
extern void ypxfrd_freebsd_prog_1(struct svc_req *, SVCXPRT *);
 
63
 
 
64
int _rpcpmstart = 0;
 
65
int _rpcfdtype = 0;
 
66
int _rpcsvcdirty = 0;
 
67
 
 
68
#ifndef _RPCSVC_CLOSEDOWN
 
69
#define _RPCSVC_CLOSEDOWN       120
 
70
#endif
 
71
 
 
72
#ifndef YPMAPDIR
 
73
#define YPMAPDIR   "/var/yp"
 
74
#endif
 
75
char *path_ypdb = YPMAPDIR;
 
76
 
 
77
char *progname;
 
78
 
 
79
/*
 
80
** Needed, if we start rpc.ypxfrd from inetd
 
81
*/
 
82
static void
 
83
closedown (int sig)
 
84
{
 
85
  signal(sig, closedown);
 
86
  if (_rpcsvcdirty == 0)
 
87
    {
 
88
      static int size;
 
89
      int i, openfd;
 
90
 
 
91
      if (_rpcfdtype == SOCK_DGRAM)
 
92
        exit(0);
 
93
      if (size == 0)
 
94
        size = _rpc_dtablesize();
 
95
 
 
96
      for (i = 0, openfd = 0; i < size && openfd < 2; ++i)
 
97
        if (FD_ISSET(i, &svc_fdset))
 
98
          openfd++;
 
99
      if (openfd <= 1)
 
100
        exit(0);
 
101
    }
 
102
  alarm(_RPCSVC_CLOSEDOWN);
 
103
}
 
104
 
 
105
/* Clean up after child processes signal their termination.  */
 
106
static void
 
107
sig_child (int sig UNUSED)
 
108
{
 
109
  int save_errno = errno;
 
110
 
 
111
  while (wait3 (NULL, WNOHANG, NULL) > 0)
 
112
    ;
 
113
  errno = save_errno;
 
114
}
 
115
 
 
116
/* Clean up if we quit the program.  */
 
117
static void
 
118
sig_quit (int sig UNUSED)
 
119
{
 
120
  pmap_unset (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS);
 
121
  exit (0);
 
122
}
 
123
 
 
124
/*
 
125
** Reload securenets and config file
 
126
*/
 
127
static void
 
128
sig_hup (int sig UNUSED)
 
129
{
 
130
  load_securenets();
 
131
  load_config();
 
132
  /* we don't wish to cache the file handles.  */
 
133
  cached_filehandles = 0;
 
134
}
 
135
 
 
136
static void
 
137
Usage (int exitcode)
 
138
{
 
139
  fputs ("usage: rpc.ypxfrd [--debug] [-d path] [-p port]\n", stderr);
 
140
  fputs ("       rpc.ypxfrd --version\n", stderr);
 
141
 
 
142
  exit (exitcode);
 
143
}
 
144
 
 
145
int
 
146
main (int argc, char **argv)
 
147
{
 
148
  SVCXPRT *main_transp;
 
149
  int my_port = -1;
 
150
  int my_socket;
 
151
  struct sockaddr_in socket_address;
 
152
  int result;
 
153
  struct sigaction sa;
 
154
#if defined(__hpux)
 
155
  int socket_size;
 
156
#else /* not __hpux */
 
157
  socklen_t socket_size;
 
158
#endif
 
159
 
 
160
  progname = strrchr (argv[0], '/');
 
161
  if (progname == (char *) NULL)
 
162
    progname = argv[0];
 
163
  else
 
164
    progname++;
 
165
 
 
166
  openlog(progname, LOG_PID, LOG_DAEMON);
 
167
 
 
168
  while(1)
 
169
    {
 
170
      int c;
 
171
      int option_index = 0;
 
172
      static struct option long_options[] =
 
173
      {
 
174
        {"version", no_argument, NULL, '\255'},
 
175
        {"debug", no_argument, NULL, '\254'},
 
176
        {"port", required_argument, NULL, 'p'},
 
177
        {"path", required_argument, NULL, 'd'},
 
178
        {"dir", required_argument, NULL, 'd'},
 
179
        {"usage", no_argument, NULL, 'u'},
 
180
        {"help", no_argument, NULL, 'h'},
 
181
        {NULL, 0, NULL, '\0'}
 
182
      };
 
183
 
 
184
      c=getopt_long(argc, argv, "p:d:uh",long_options, &option_index);
 
185
      if (c==EOF) break;
 
186
      switch (c)
 
187
        {
 
188
        case '\255':
 
189
          debug_flag = 1;
 
190
          log_msg("rpc.ypxfrd (%s) %s\n", PACKAGE, VERSION);
 
191
          exit(0);
 
192
        case '\254':
 
193
          debug_flag++;
 
194
          break;
 
195
        case 'd':
 
196
          path_ypdb = optarg;
 
197
          if (debug_flag)
 
198
            log_msg("Using database directory: %s\n", path_ypdb);
 
199
          break;
 
200
        case 'p':
 
201
          my_port = atoi(optarg);
 
202
          if (debug_flag)
 
203
            log_msg("Using port %d\n", my_port);
 
204
          break;
 
205
        case 'u':
 
206
        case 'h':
 
207
          Usage(0);
 
208
          break;
 
209
        case '?':
 
210
          Usage(1);
 
211
          break;
 
212
        }
 
213
    }
 
214
 
 
215
  argc-=optind;
 
216
  argv+=optind;
 
217
 
 
218
  if (debug_flag)
 
219
    log_msg("[Welcome to the rpc.ypxfrd Daemon, version %s]\n", VERSION);
 
220
  else
 
221
    if(!_rpcpmstart)
 
222
      {
 
223
        int i;
 
224
 
 
225
        if ((i = fork()) > 0)
 
226
          exit(0);
 
227
 
 
228
        if (i < 0)
 
229
          {
 
230
            log_msg ("Cannot fork: %s\n", strerror (errno));
 
231
            exit (-1);
 
232
          }
 
233
 
 
234
        if (setsid() == -1)
 
235
          {
 
236
            log_msg ("Cannot setsid: %s\n", strerror (errno));
 
237
            exit (-1);
 
238
          }
 
239
 
 
240
        if ((i = fork()) > 0)
 
241
          exit(0);
 
242
 
 
243
        if (i < 0)
 
244
          {
 
245
            log_msg ("Cannot fork: %s\n", strerror (errno));
 
246
            exit (-1);
 
247
          }
 
248
 
 
249
        for (i = 0; i < getdtablesize(); ++i)
 
250
          close(i);
 
251
        errno = 0;
 
252
 
 
253
        umask(0);
 
254
        i = open("/dev/null", O_RDWR);
 
255
        dup(i);
 
256
        dup(i);
 
257
      }
 
258
 
 
259
  /* Change current directory to database location */
 
260
  if (chdir(path_ypdb) < 0)
 
261
    {
 
262
      log_msg("%s: chdir: %", argv[0], strerror(errno));
 
263
      exit(1);
 
264
    }
 
265
 
 
266
  load_securenets();
 
267
  load_config();
 
268
  /* we don't wish to cache the file handles.  */
 
269
  cached_filehandles = 0;
 
270
 
 
271
  /*
 
272
   * Ignore SIGPIPEs. They can hurt us if someone does a ypcat
 
273
   * and then hits CTRL-C before it terminates.
 
274
   */
 
275
  sigaction(SIGPIPE, NULL, &sa);
 
276
  sa.sa_handler = SIG_IGN;
 
277
#if !defined(sun) || (defined(sun) && defined(__svr4__))
 
278
  sa.sa_flags |= SA_RESTART;
 
279
  /*
 
280
   * The opposite to SA_ONESHOT, do  not  restore
 
281
   * the  signal  action.  This provides behavior
 
282
   * compatible with BSD signal semantics.
 
283
   */
 
284
#endif
 
285
  sigemptyset(&sa.sa_mask);
 
286
  sigaction(SIGPIPE, &sa, NULL);
 
287
  /*
 
288
   * Clear up if child exists
 
289
   */
 
290
  sigaction(SIGCHLD, NULL, &sa);
 
291
#if !defined(sun) || (defined(sun) && defined(__svr4__))
 
292
  sa.sa_flags |= SA_RESTART;
 
293
#endif
 
294
  sa.sa_handler = sig_child;
 
295
  sigemptyset(&sa.sa_mask);
 
296
  sigaction(SIGCHLD, &sa, NULL);
 
297
  /*
 
298
   * If program quits, give ports free.
 
299
   */
 
300
  sigaction(SIGTERM, NULL, &sa);
 
301
#if !defined(sun) || (defined(sun) && defined(__svr4__))
 
302
  sa.sa_flags |= SA_RESTART;
 
303
#endif
 
304
  sa.sa_handler = sig_quit;
 
305
  sigemptyset(&sa.sa_mask);
 
306
  sigaction(SIGTERM, &sa, NULL);
 
307
 
 
308
  sigaction(SIGINT, NULL, &sa);
 
309
#if !defined(sun) || (defined(sun) && defined(__svr4__))
 
310
  sa.sa_flags |= SA_RESTART;
 
311
#endif
 
312
  sa.sa_handler = sig_quit;
 
313
  sigemptyset(&sa.sa_mask);
 
314
  sigaction(SIGINT, &sa, NULL);
 
315
 
 
316
  /*
 
317
   * If we get a SIGHUP, reload the securenets and config file.
 
318
   */
 
319
  sigaction(SIGHUP, NULL, &sa);
 
320
#if !defined(sun) || (defined(sun) && defined(__svr4__))
 
321
  sa.sa_flags |= SA_RESTART;
 
322
#endif
 
323
  sa.sa_handler = sig_hup;
 
324
  sigemptyset(&sa.sa_mask);
 
325
  sigaction(SIGHUP, &sa, NULL);
 
326
 
 
327
  pmap_unset(YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS);
 
328
 
 
329
  socket_size = sizeof(socket_address);
 
330
  _rpcfdtype = 0;
 
331
  if (getsockname(0, (struct sockaddr *)&socket_address, &socket_size) == 0)
 
332
    {
 
333
#if defined(__hpux)
 
334
      int int_size = sizeof (int);
 
335
#else /* not __hpux */
 
336
      socklen_t  int_size = sizeof (int);
 
337
#endif
 
338
      if (socket_address.sin_family != AF_INET)
 
339
        return 1;
 
340
      if (getsockopt(0, SOL_SOCKET, SO_TYPE, (void*)&_rpcfdtype,
 
341
                     &int_size) == -1)
 
342
        return 1;
 
343
      _rpcpmstart = 1;
 
344
      my_socket = 0;
 
345
    }
 
346
  else
 
347
    my_socket = RPC_ANYSOCK;
 
348
 
 
349
  if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM))
 
350
    {
 
351
      if (_rpcfdtype == 0 && my_port > 0)
 
352
        {
 
353
          my_socket = socket (AF_INET, SOCK_DGRAM, 0);
 
354
          if (my_socket < 0)
 
355
            {
 
356
              log_msg("can not create UDP: %s",strerror(errno));
 
357
              return 1;
 
358
            }
 
359
 
 
360
          memset((char *) &socket_address, 0, sizeof(socket_address));
 
361
          socket_address.sin_family = AF_INET;
 
362
          socket_address.sin_addr.s_addr = htonl (INADDR_ANY);
 
363
          socket_address.sin_port = htons (my_port);
 
364
 
 
365
          result = bind (my_socket, (struct sockaddr *) &socket_address,
 
366
                         sizeof (socket_address));
 
367
          if (result < 0)
 
368
            {
 
369
              log_msg("%s: can not bind UDP: %s ", progname,strerror(errno));
 
370
              return 1;
 
371
            }
 
372
        }
 
373
 
 
374
      main_transp = svcudp_create(my_socket);
 
375
      if (main_transp == NULL)
 
376
        {
 
377
          log_msg("cannot create udp service.");
 
378
          return 1;
 
379
        }
 
380
      if (!svc_register(main_transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS,
 
381
                        ypxfrd_freebsd_prog_1, IPPROTO_UDP))
 
382
        {
 
383
          log_msg("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, udp).");
 
384
          return 1;
 
385
        }
 
386
    }
 
387
 
 
388
  if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM))
 
389
    {
 
390
      if (_rpcfdtype == 0 && my_port >= 0)
 
391
        {
 
392
          my_socket = socket (AF_INET, SOCK_STREAM, 0);
 
393
          if (my_socket < 0)
 
394
            {
 
395
              log_msg ("%s: can not create TCP ",progname);
 
396
              return 1;
 
397
            }
 
398
 
 
399
          memset((char *) &socket_address, 0, sizeof(socket_address));
 
400
          socket_address.sin_family = AF_INET;
 
401
          socket_address.sin_addr.s_addr = htonl (INADDR_ANY);
 
402
          socket_address.sin_port = htons (my_port);
 
403
 
 
404
          result = bind (my_socket, (struct sockaddr *) &socket_address,
 
405
                         sizeof (socket_address));
 
406
          if (result < 0)
 
407
            {
 
408
              log_msg("%s: can not bind TCP ",progname);
 
409
              return 1;
 
410
            }
 
411
        }
 
412
      main_transp = svctcp_create(my_socket, 0, 0);
 
413
      if (main_transp == NULL)
 
414
        {
 
415
          log_msg("%s: cannot create tcp service\n", progname);
 
416
          exit(1);
 
417
        }
 
418
      if (!svc_register(main_transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS,
 
419
                        ypxfrd_freebsd_prog_1, IPPROTO_TCP))
 
420
        {
 
421
          log_msg("%s: unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, tcp)\n",
 
422
                 progname);
 
423
          exit(1);
 
424
        }
 
425
    }
 
426
 
 
427
  if (_rpcpmstart)
 
428
    {
 
429
      signal (SIGALRM, closedown);
 
430
      alarm (_RPCSVC_CLOSEDOWN);
 
431
    }
 
432
 
 
433
  svc_run();
 
434
  log_msg("svc_run returned");
 
435
  exit(1);
 
436
  /* NOTREACHED */
 
437
}