msrv.c 19.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2008 Free Software Foundation, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
   Public License along with this library; If not, see
   <http://www.gnu.org/licenses/>.  */

/* This is an `m-server' - a universal framework for multi-process TCP
   servers. An `m-' stands for `mail-', or `multi-' or maybe `meta-',
   I don't remember what. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <syslog.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <ctype.h>
#include <mailutils/server.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/cfg.h>
#include <mailutils/nls.h>
#include <mailutils/daemon.h>
#include <mailutils/acl.h>

typedef RETSIGTYPE (*mu_sig_handler_t) (int);

static mu_sig_handler_t
set_signal (int sig, mu_sig_handler_t handler)
{
#ifdef HAVE_SIGACTION
  {
    struct sigaction act, oldact;
    act.sa_handler = handler;
    sigemptyset (&act.sa_mask);
    act.sa_flags = 0;
    sigaction (sig, &act, &oldact);
    return oldact.sa_handler;
  }
#else
  return signal (sig, handler);
#endif
}

#ifndef NSIG
# define NSIG 64
#endif

struct _mu_m_server
{
  char *ident;                   /* Server identifier, for logging purposes.*/
  int deftype;                   /* Default server type: MU_IP_TCP/MU_IP_UDP */
  mu_server_t server;            /* The server object. */
  mu_list_t srvlist;             /* A list of configured mu_ip_server_t
				    objects. It is cleared after the objects
				    are opened and attached to the server. */
  
  mu_m_server_conn_fp conn;      /* Connection handler function. */
  mu_m_server_prefork_fp prefork;/* Pre-fork function. */
  void *data;                    /* User-supplied data for conn and prefork. */
  
  int mode;                      /* Server mode: should be removed. */
  
  int foreground;                /* Should the server remain in foregorund? */
  size_t max_children;           /* Maximum number of sub-processes to run. */
  char *pidfile;                 /* Name of a PID-file. */ 
  unsigned short defport;        /* Default port number. */
  time_t timeout;                /* Default idle timeout. */
  mu_acl_t acl;                  /* Global access control list. */

  sigset_t sigmask;              /* A set of signals to handle by the
				    m-server.  */
  mu_sig_handler_t sigtab[NSIG]; /* Keeps old signal handlers. */
};

struct m_srv_config        /* Configuration data for a single TCP server. */
{
  mu_m_server_t msrv;      /* Parent m-server. */  
  mu_ip_server_t tcpsrv;  /* TCP server these data are for. */
  mu_acl_t acl;            /* Access control list for this server. */ 
  int single_process;      /* Should it run as a single process? */
  int transcript;          /* Enable session transcript. */
  time_t timeout;          /* Idle timeout for this server. */
};


static int need_cleanup = 0;
static int stop = 0;
static size_t children;

void
mu_m_server_stop (int code)
{
  stop = code;
}

static int
mu_m_server_idle (void *server_data MU_ARG_UNUSED)
{
  pid_t pid;
  int status;
  
  if (need_cleanup)
    {
      need_cleanup = 0;
      while ( (pid = waitpid (-1, &status, WNOHANG)) > 0)
	{
	  --children;
	  if (WIFEXITED (status))
	    {
	      int prio = MU_DIAG_INFO;
	      
	      status = WEXITSTATUS (status);
	      if (status == 0)
		prio = MU_DIAG_DEBUG;
	      mu_diag_output (prio, "process %lu finished with code %d",
			      (unsigned long) pid,
			      status);
	    }
	  else if (WIFSIGNALED (status))
	    mu_diag_output (MU_DIAG_ERR, "process %lu terminated on signal %d",
			    (unsigned long) pid,
			    WTERMSIG (status));
	  else
	    mu_diag_output (MU_DIAG_ERR,
			    "process %lu terminated (cause unknown)",
			    (unsigned long) pid);
	}
    }
  return stop;
}

static RETSIGTYPE
m_srv_signal (int signo)
{
  switch (signo)
    {
    case SIGCHLD:
      need_cleanup = 1;
      break;

    default:
      stop = 1;
      break;
    }
#ifndef HAVE_SIGACTION
  signal (signo, m_srv_sigchld);
#endif
}

void
mu_m_server_create (mu_m_server_t *psrv, const char *ident)
{
  mu_m_server_t srv = calloc (1, sizeof *srv);
  if (!srv)
    {
      mu_error ("%s", mu_strerror (ENOMEM));
      exit (1);
    }
  if (ident)
    {
      srv->ident = strdup (ident);
      if (!srv->ident)
	{
	  mu_error ("%s", mu_strerror (ENOMEM));
	  exit (1);
	}
    }
  srv->deftype = MU_IP_TCP;
  MU_ASSERT (mu_server_create (&srv->server));
  mu_server_set_idle (srv->server, mu_m_server_idle);
  sigemptyset (&srv->sigmask);
  sigaddset (&srv->sigmask, SIGCHLD);
  sigaddset (&srv->sigmask, SIGINT);
  sigaddset (&srv->sigmask, SIGTERM);
  sigaddset (&srv->sigmask, SIGQUIT);
  sigaddset (&srv->sigmask, SIGHUP);
  *psrv = srv;
}

void
mu_m_server_set_type (mu_m_server_t srv, int type)
{
  srv->deftype = type;
}

void
mu_m_server_get_type (mu_m_server_t srv, int *type)
{
  *type = srv->deftype;
}

void
mu_m_server_set_sigset (mu_m_server_t srv, sigset_t *sigset)
{
  srv->sigmask = *sigset;
  sigaddset (&srv->sigmask, SIGCHLD);
}

void
mu_m_server_get_sigset (mu_m_server_t srv, sigset_t *sigset)
{
  *sigset = srv->sigmask;
}

void
mu_m_server_set_mode (mu_m_server_t srv, int mode)
{
  srv->mode = mode;
}

void
mu_m_server_set_conn (mu_m_server_t srv, mu_m_server_conn_fp conn)
{
  srv->conn = conn;
}

void
mu_m_server_set_prefork (mu_m_server_t srv, mu_m_server_prefork_fp fun)
{
  srv->prefork = fun;
}

void
mu_m_server_set_data (mu_m_server_t srv, void *data)
{
  srv->data = data;
}

void
mu_m_server_set_max_children (mu_m_server_t srv, size_t num)
{
  srv->max_children = num;
}

int
mu_m_server_set_pidfile (mu_m_server_t srv, const char *pidfile)
{
  free (srv->pidfile);
  srv->pidfile = strdup (pidfile);
  return srv->pidfile ? 0 : errno;
}

void
mu_m_server_set_default_port (mu_m_server_t srv, int port)
{
  srv->defport = port;
}

void
mu_m_server_set_timeout (mu_m_server_t srv, time_t t)
{
  srv->timeout = t;
}

int
mu_m_server_mode (mu_m_server_t srv)
{
  return srv->mode;
}

time_t
mu_m_server_timeout (mu_m_server_t srv)
{
  return srv->timeout;
}

void
m_srv_config_free (void *data)
{
  struct m_srv_config *pconf = data;
  /* FIXME */
  free (pconf);
}

static int m_srv_conn (int fd, struct sockaddr *sa, int salen,
		       void *server_data, void *call_data,
		       mu_ip_server_t srv);

static struct m_srv_config *
add_server (mu_m_server_t msrv, struct sockaddr *s, int slen, int type)
{
  mu_ip_server_t tcpsrv;
  struct m_srv_config *pconf;

  MU_ASSERT (mu_ip_server_create (&tcpsrv, s, slen, type)); /* FIXME: type */
  MU_ASSERT (mu_ip_server_set_conn (tcpsrv, m_srv_conn));
  pconf = calloc (1, sizeof (*pconf));
  if (!pconf)
    {
      mu_error ("%s", mu_strerror (ENOMEM));
      exit (1);
    }
  pconf->msrv = msrv;
  pconf->tcpsrv = tcpsrv;
  pconf->single_process = 0;
  pconf->timeout = msrv->timeout;
  MU_ASSERT (mu_ip_server_set_data (tcpsrv, pconf, m_srv_config_free));
  if (!msrv->srvlist)
    MU_ASSERT (mu_list_create (&msrv->srvlist));
  MU_ASSERT (mu_list_append (msrv->srvlist, tcpsrv));
  return pconf;
}

void
mu_m_server_configured_count (mu_m_server_t msrv, size_t count)
{
  mu_list_count (msrv->srvlist, &count);
}

void
mu_m_server_begin (mu_m_server_t msrv)
{
  int i, rc;
  size_t count = 0;

  mu_list_count (msrv->srvlist, &count);
  if (count == 0 && msrv->defport)
    {
      /* Add default server */
      struct sockaddr_in s;
      s.sin_family = AF_INET;
      s.sin_addr.s_addr = htonl (INADDR_ANY);
      s.sin_port = htons (msrv->defport);
      add_server (msrv, (struct sockaddr *)&s, sizeof s, msrv->deftype);
    }
  
  if (!msrv->foreground)
    {
      /* Become a daemon. Take care to close inherited fds and to hold
	 first three one, in, out, err   */
      if (daemon (0, 0) < 0)
	{
	  mu_error (_("Failed to become a daemon: %s"), mu_strerror (errno));
	  exit (EXIT_FAILURE);
	}
    }

  if (msrv->pidfile)
    switch (rc = mu_daemon_create_pidfile (msrv->pidfile))
      {
      case EINVAL:
	mu_error (_("%s: Invalid name for a pidfile"), msrv->pidfile);
	break;
	
      default:
	mu_error (_("cannot create pidfile `%s': %s"), msrv->pidfile,
		  mu_strerror (rc));
      }

  for (i = 0; i < NSIG; i++)
    if (sigismember (&msrv->sigmask, i))
      msrv->sigtab[i] = set_signal (i, m_srv_signal);
}

void
mu_m_server_end (mu_m_server_t msrv)
{
  int i;
  
  for (i = 0; i < NSIG; i++)
    if (sigismember (&msrv->sigmask, i))
      set_signal (i, msrv->sigtab[i]);
}

void
mu_m_server_destroy (mu_m_server_t *pmsrv)
{
  mu_m_server_t msrv = *pmsrv;
  mu_server_destroy (&msrv->server);
  /* FIXME: Send processes the TERM signal */
  free (msrv->ident);
  free (msrv);
  *pmsrv = NULL;
}

static int
tcp_conn_handler (int fd, void *conn_data, void *server_data)
{
  mu_ip_server_t tcpsrv = (mu_ip_server_t) conn_data;
  int rc = mu_ip_server_accept (tcpsrv, server_data);
  if (rc && rc != EINTR)
    {
      mu_ip_server_shutdown (tcpsrv);
      return MU_SERVER_CLOSE_CONN;
    }
  return stop ? MU_SERVER_SHUTDOWN : MU_SERVER_SUCCESS;
}

static void
tcp_conn_free (void *conn_data, void *server_data)
{
  mu_ip_server_t tcpsrv = (mu_ip_server_t) conn_data;
  mu_ip_server_destroy (&tcpsrv);
}

static int
_open_conn (void *item, void *data)
{
  union
  {
    struct sockaddr sa;
    char pad[512];
  }
  addr;
  int addrlen = sizeof addr;
  char *p;
  mu_ip_server_t tcpsrv = item;
  mu_m_server_t msrv = data;
  int rc = mu_ip_server_open (tcpsrv);
  if (rc)
    {
      mu_ip_server_get_sockaddr (tcpsrv, &addr.sa, &addrlen);
      p = mu_sockaddr_to_astr (&addr.sa, addrlen);
      mu_error (_("Cannot open connection on %s: %s"), p, mu_strerror (rc));
      free (p);
      return 0;
    }
  rc = mu_server_add_connection (msrv->server,
				 mu_ip_server_get_fd (tcpsrv),
				 tcpsrv,
				 tcp_conn_handler, tcp_conn_free);
  if (rc)
    {
      mu_ip_server_get_sockaddr (tcpsrv, &addr.sa, &addrlen);
      p = mu_sockaddr_to_astr (&addr.sa, addrlen);
      mu_error (_("Cannot add connection %s: %s"), p, mu_strerror (rc));
      free (p);
      mu_ip_server_shutdown (tcpsrv);
      mu_ip_server_destroy (&tcpsrv);
    }
  return 0;
}  

int
mu_m_server_run (mu_m_server_t msrv)
{
  int rc;
  size_t count;
  mode_t saved_umask = umask (0117);
  mu_list_do (msrv->srvlist, _open_conn, msrv);
  umask (saved_umask);
  mu_list_destroy (&msrv->srvlist);
  MU_ASSERT (mu_server_count (msrv->server, &count));
  if (count == 0)
    {
      mu_error (_("No servers configured: exiting"));
      exit (1);
    }
  if (msrv->ident)
    mu_diag_output (MU_DIAG_INFO, _("%s started"), msrv->ident);
  rc = mu_server_run (msrv->server);
  if (msrv->ident)
    mu_diag_output (MU_DIAG_INFO, _("%s terminated"), msrv->ident);
  return rc;
}



int
mu_m_server_check_acl (mu_m_server_t msrv, struct sockaddr *s, int salen)
{
  if (msrv->acl)
    {
      mu_acl_result_t res;
      int rc = mu_acl_check_sockaddr (msrv->acl, s, salen, &res);
      if (rc)
	{
	  char *p = mu_sockaddr_to_astr (s, salen);
	  mu_error (_("Access from %s blocked: cannot check ACLs: %s"),
		    p, mu_strerror (rc));
	  free (p);
	  return 1;
	}
      switch (res)
	{
	case mu_acl_result_undefined:
	  {
	    char *p = mu_sockaddr_to_astr (s, salen);
	    mu_diag_output (MU_DIAG_INFO,
			    _("%s: undefined ACL result; access allowed"),
			    p);
	    free (p);
	  }
	  break;
	      
	case mu_acl_result_accept:
	  break;
	      
	case mu_acl_result_deny:
	  {
	    char *p = mu_sockaddr_to_astr (s, salen);
	    mu_error (_("Access from %s blocked."), p);
	    free (p);
	    return 1;
	  }
	}
    }
  return 0;
}

int
m_srv_conn (int fd, struct sockaddr *sa, int salen,
	    void *server_data, void *call_data,
	    mu_ip_server_t srv)
{
  int status;
  struct m_srv_config *pconf = server_data;
  
  if (mu_m_server_check_acl (pconf->msrv, sa, salen))
    return 0;

  if (!pconf->single_process)
    {
      pid_t pid;

      if (mu_m_server_idle (server_data))
	return MU_SERVER_SHUTDOWN;
      if (children >= pconf->msrv->max_children)
        {
	  mu_diag_output (MU_DIAG_ERROR, _("too many children (%lu)"),
			  (unsigned long) children);
          pause ();
          return 0;
        }
      if (pconf->msrv->prefork
	  && pconf->msrv->prefork (fd, pconf->msrv->data, sa, salen))
	return 0;
      
      pid = fork ();
      if (pid == -1)
	mu_diag_output (MU_DIAG_ERROR, "fork: %s", strerror (errno));
      else if (pid == 0) /* Child.  */
	{
	  mu_ip_server_shutdown (srv); /* FIXME: does it harm for MU_IP_UDP? */
	  status = pconf->msrv->conn (fd, sa, salen, pconf->msrv->data, srv,
				      pconf->timeout, pconf->transcript);
	  closelog ();
	  exit (status);
	}
      else
	{
	  children++;
	}
    }
  else if (pconf->msrv->prefork
	   && pconf->msrv->prefork (fd, pconf->msrv->data, sa, salen) == 0)
    pconf->msrv->conn (fd, sa, salen, pconf->msrv->data, srv,
		       pconf->timeout, pconf->transcript);
  return 0;
}



unsigned short
get_port (mu_debug_t debug, char *p)
{
  if (p)
    {
      char *q;
      unsigned long n = strtoul (p, &q, 0);
      if (*q == 0)
	{
	  if (n > USHRT_MAX)
	    {
	      mu_debug_printf (debug, MU_DEBUG_ERROR,
				   _("invalid port number: %s\n"), p);
	      return 1;
	    }
	  
	  return htons (n);
	}
      else
	{
	  struct servent *sp = getservbyname (p, "tcp");
	  if (!sp)
	    return 0;
	  return sp->s_port;
	}
    }
  return 0;
}

static int
get_family (char **pstr, sa_family_t *pfamily)
{
  static struct family_tab
  {
    int len;
    char *pfx;
    int family;
  } ftab[] = {
#define S(s,f) { sizeof (#s":") - 1, #s":", f }
    S (file, AF_UNIX),
    S (unix, AF_UNIX),
    S (local, AF_UNIX),
    S (socket, AF_UNIX),
    S (inet, AF_INET),
    S (tcp, AF_INET),
#undef S
    { 0 }
  };
  struct family_tab *fp;
  
  char *str = *pstr;
  int len = strlen (str);
  for (fp = ftab; fp->len; fp++)
    {
      if (len > fp->len && memcmp (str, fp->pfx, fp->len) == 0)
	{
	  str += fp->len;
	  if (str[0] == '/' && str[1] == '/')
	    str += 2;
	  *pstr = str;
	  *pfamily = fp->family;
	  return 0;
	}
    }
  return 1;
}

static int
is_ip_addr (const char *arg)
{
  int     dot_count;
  int     digit_count;

  dot_count = 0;
  digit_count = 0;
  for (; *arg != 0 && *arg != ':'; arg++)
    {
      if (*arg == '.')
	{
	  if (++dot_count > 3)
	    break;
	  digit_count = 0;
	}
      else if (!(isdigit (*arg) && ++digit_count <= 3))
	return 0;
    }
  return dot_count == 3;
}  

static int
server_block_begin (mu_debug_t debug, char *arg, mu_m_server_t msrv,
		    void **pdata)
{
  char *p;
  union
  {
    struct sockaddr s_sa;
    struct sockaddr_in s_in;
    struct sockaddr_un s_un;
  }
  s;
  int salen = sizeof s.s_in;
  
  s.s_sa.sa_family = AF_INET;
  
  if (!arg)
    {
      s.s_in.sin_addr.s_addr = htonl (INADDR_ANY);
      s.s_in.sin_port = htons (msrv->defport);
    }
  else
    {
      unsigned short n;
      int len;
      
      if (!is_ip_addr (arg) && get_family (&arg, &s.s_sa.sa_family))
	{
	  mu_debug_printf (debug, MU_DEBUG_ERROR, _("invalid family\n"));
	  return 1;
	}
      
      switch (s.s_in.sin_family)
	{
	case AF_INET:
	  if ((n = get_port (debug, arg)))
	    {
	      s.s_in.sin_addr.s_addr = htonl (INADDR_ANY);
	      s.s_in.sin_port = htons (n);	  
	    }
	  else
	    {
	      p = strchr (arg, ':');
	      if (p)
		*p++ = 0;
	      if (inet_aton (arg, &s.s_in.sin_addr) == 0)
		{
		  struct hostent *hp = gethostbyname (arg);
		  if (hp)
		    s.s_in.sin_addr.s_addr = *(unsigned long *)hp->h_addr;
		  else
		    {
		      mu_debug_printf (debug, MU_DEBUG_ERROR,
				       _("invalid IP address: %s\n"), arg);
		      return 1;
		    }
		}
	      if (p)
		{
		  n = get_port (debug, p);
		  if (!n)
		    {
		      mu_debug_printf (debug, MU_DEBUG_ERROR,
				       _("invalid port number: %s\n"), p);
		      return 1;
		    }
		  s.s_in.sin_port = n;
		}
	      else if (msrv->defport)
		s.s_in.sin_port = htons (msrv->defport);
	      else
		{
		  mu_debug_printf (debug, MU_DEBUG_ERROR,
				   _("missing port number\n"));
		  return 1;
		}
	    }
	  break;

	case AF_UNIX:
	  salen = sizeof s.s_un;
	  len = strlen (arg);
	  if (len > sizeof s.s_un.sun_path - 1)
	    {
	      mu_error (_("%s: file name too long"), arg);
	      return 1;
	    }
	  strcpy (s.s_un.sun_path, arg);
	  break;
	}
    }
      
  *pdata = add_server (msrv, &s.s_sa, salen, msrv->deftype);
  return 0;
}

static int
server_section_parser (enum mu_cfg_section_stage stage,
		       const mu_cfg_node_t *node,
		       const char *section_label, void **section_data,
		       void *call_data,
		       mu_cfg_tree_t *tree)
{
  switch (stage)
    {
    case mu_cfg_section_start:
      {
	/* FIXME: should not modify 2nd arg, or it should not be const */
	return server_block_begin (tree->debug, node->tag_label,
				   *section_data, section_data);
      }
      break;

    case mu_cfg_section_end:
      {
	struct m_srv_config *pconf = *section_data;
	if (pconf->acl)
	  mu_ip_server_set_acl (pconf->tcpsrv, pconf->acl);
      }
      break;
    }
  return 0;
}

static int
_cb_daemon_mode (mu_debug_t debug, void *data, char *arg)
{
  int *pmode = data;
  
  if (strcmp (arg, "inetd") == 0
      || strcmp (arg, "interactive") == 0)
    *pmode = MODE_INTERACTIVE;
  else if (strcmp (arg, "daemon") == 0)
    *pmode = MODE_DAEMON;
  else
    {
      mu_cfg_format_error (debug, MU_DEBUG_ERROR, _("unknown daemon mode"));
      return 1;
    }
  return 0;
}

static struct mu_cfg_param dot_server_cfg_param[] = {
  { "max-children", mu_cfg_size,
    NULL, mu_offsetof (struct _mu_m_server,max_children), NULL,
    N_("Maximum number of children processes to run simultaneously.") },
  { "mode", mu_cfg_callback,
    NULL, mu_offsetof (struct _mu_m_server,mode), _cb_daemon_mode,
    N_("Set daemon mode (either inetd (or interactive) or daemon)."),
    N_("mode") },
  { "foreground", mu_cfg_bool,
    NULL, mu_offsetof (struct _mu_m_server, foreground), NULL,
    N_("Run in foreground.") },
  { "pidfile", mu_cfg_string,
    NULL, mu_offsetof (struct _mu_m_server,pidfile), NULL,
    N_("Store PID of the master process in this file."),
    N_("file") },
  { "port", mu_cfg_ushort,
    NULL, mu_offsetof (struct _mu_m_server,defport), NULL,
    N_("Default port number.") },
  { "timeout", mu_cfg_time,
    NULL, mu_offsetof (struct _mu_m_server,timeout), NULL,
    N_("Set idle timeout.") },
  { "server", mu_cfg_section, NULL, 0, NULL,
    N_("Server configuration.") },
  { "acl", mu_cfg_section, NULL, mu_offsetof (struct _mu_m_server,acl), NULL,
    N_("Per-server access control list") },
  { NULL }
};
    
static struct mu_cfg_param server_cfg_param[] = {
  { "single-process", mu_cfg_bool, 
    NULL, mu_offsetof (struct m_srv_config, single_process), NULL,
    N_("Run this server in foreground.") },
  { "transcript", mu_cfg_bool,
    NULL, mu_offsetof (struct m_srv_config, transcript), NULL,
    N_("Log the session transcript.") },
  { "timeout", mu_cfg_time,
    NULL, mu_offsetof (struct m_srv_config, timeout), NULL,
    N_("Set idle timeout.") },
  { "acl", mu_cfg_section,
    NULL, mu_offsetof (struct m_srv_config, acl), NULL,
    N_("Global access control list.") },
  { NULL }
};

void
mu_m_server_cfg_init ()
{
  struct mu_cfg_section *section;
  if (mu_create_canned_section ("server", &section) == 0)
    {
      section->parser = server_section_parser;
      section->label = N_("ipaddr[:port]");
      mu_cfg_section_add_params (section, server_cfg_param);
    }
  if (mu_create_canned_section (".server", &section) == 0)
    {
      mu_cfg_section_add_params (section, dot_server_cfg_param);
    }
}