zxid

 view release on metacpan or  search on metacpan

zxid_httpd.c  view on Meta::CPAN

    gid = pwd->pw_gid;
  }
#endif

  /* Log file. */
  if (logfile) {
    re_open_logfile();
    if (logfile[0] != '/') {
      syslog(LOG_WARNING, "logfile is not an absolute path, you may not be able to re-open it");
      (void) fprintf(stderr, "%s: logfile is not an absolute path, you may not be able to re-open it\n", argv0);
    }
#ifndef MINGW
    if (!getuid()) {
      /* If we are root then we chown the log file to the user we'll
      ** be switching to.
      */
      if (fchown(fileno(logfp), uid, gid) < 0) {
	perror("fchown logfile");
	syslog(LOG_WARNING, "fchown logfile - %m");
      }
    }
#endif
  }

  /* Look up hostname. */
  lookup_hostname(&host_addr4, sizeof(host_addr4), &gotv4,
		  &host_addr6, sizeof(host_addr6), &gotv6);
  if (!hostname) {
    (void) gethostname(hostname_buf, sizeof(hostname_buf));
    hostname_buf[sizeof(hostname_buf)-1]=0;
    hostname = hostname_buf;
  }
  if (! (gotv4 || gotv6)) {
    syslog(LOG_CRIT, "can't find any valid address");
    (void) fprintf(stderr, "%s: can't find any valid address\n", argv0);
    exit(1);
  }

  /* Initialize listen sockets.  Try v6 first because of a Linux peculiarity;
  ** like some other systems, it has magical v6 sockets that also listen for
  ** v4, but in Linux if you bind a v4 socket first then the v6 bind fails. */
  if (gotv6)
    listen_fd = initialize_listen_socket(&host_addr6);
  else if (gotv4)
    listen_fd = initialize_listen_socket(&host_addr4);
  else
    listen_fd = -1;
  /* If we didn't get any valid sockets, fail. */
  if (listen_fd == -1) {
    D("gotv4=%d gotv6=%d  ip4(%s) ip6(%s)", gotv4, gotv6, ntoa(&host_addr4), ntoa(&host_addr6));
    die_perror("listen(2): can't bind to any address");
  }

  if (do_ssl)	{
    SSL_load_error_strings();
    SSLeay_add_ssl_algorithms();
    ssl_ctx = SSL_CTX_new(SSLv23_server_method());
    if (certfile[0] != '\0')
      if (!SSL_CTX_use_certificate_file(ssl_ctx, certfile, SSL_FILETYPE_PEM) ||
	  !SSL_CTX_use_PrivateKey_file(ssl_ctx, certfile, SSL_FILETYPE_PEM) ||
	  !SSL_CTX_check_private_key(ssl_ctx)) {
	ERR_print_errors_fp(stderr);
	exit(1);
      }
    if (cipher) {
      if (!SSL_CTX_set_cipher_list(ssl_ctx, cipher)) {
	ERR_print_errors_fp(stderr);
	exit(1);
      }
    }
  }
  
#ifdef HAVE_SETSID
  /* Even if we don't daemonize, we still want to disown our parent
  ** process.
  */
  (void) setsid();
#endif /* HAVE_SETSID */

  if (pidfile) {
    /* Write the PID file. */
    FILE* pidfp = fopen(pidfile, "w");
    if (pidfp == (FILE*) 0) die_perror(pidfile);
    (void) fprintf(pidfp, "%d\n", (int) getpid());
    (void) fclose(pidfp);
  }
  
#ifndef MINGW
  /* If we're root, start becoming someone else. */
  if (!getuid()) {
    /* Set aux groups to null. */
    if (setgroups(0, (gid_t*) 0) < 0) die_perror("setgroups");
    /* Set primary group. */
    if (setgid(gid) < 0) die_perror("setgid");
    /* Try setting aux groups correctly - not critical if this fails. */
    if (initgroups(user, gid) < 0) {
      perror("initgroups");
      syslog(LOG_ERR, "initgroups - %m");
    }
#ifdef HAVE_SETLOGIN
    /* Set login name. */
    (void) setlogin(user);
#endif /* HAVE_SETLOGIN */
  }
#endif /* !MINGW */

  /* Switch directories if requested. */
  if (dir) {
    if (chdir(dir) < 0) die_perror("chdir");
  }

  /* Get current directory. */
  (void) getcwd(cwd, sizeof(cwd) - 1);
  if (cwd[strlen(cwd) - 1] != '/')
    (void) strcat(cwd, "/");

#ifndef MINGW
  /* Chroot if requested. */
  if (do_chroot) {
    if (chroot(cwd) < 0) die_perror("chroot");
    /* If we are logging and the logfile's pathname begins with the



( run in 2.080 seconds using v1.01-cache-2.11-cpan-302cb4679cc )