File: main.c

package info (click to toggle)
dns2tcp 0.5.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,268 kB
  • sloc: ansic: 5,320; sh: 3,553; makefile: 63
file content (185 lines) | stat: -rw-r--r-- 4,178 bytes parent folder | download
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
/*
** Copyright (C) 2006 Olivier DEMBOUR
** $Id: main.c,v 1.28.4.2 2009/12/07 08:46:29 dembour Exp $
**
** 
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with This program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <pwd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <grp.h>

#include "config.h"
#include "server.h"
#include "dns.h"
#include "list.h"
#include "socket.h"
#include "options.h"
#include "debug.h"
#include "myerror.h"
#include "log.h"

int debug;

int			detach_process(t_conf *conf)
{
  int			fd;
  pid_t                 pid;
  FILE                  *pidfile;

  if (!(pid = fork()))
    { /* child */
      if (setsid() < 0)
	{
	  perror("");
	  return (-1); 
	}
      if ((fd = open("/dev/null", O_RDWR)) == -1)
	return (-1);
      fclose(stdin);  fclose(stdout); fclose(stderr);
      dup2(fd, 0); dup2(fd, 1); dup2(fd, 2);
      close(fd);
      return (0);
      
    }
  if (!conf->pidfile)
    exit (0);
  else if (! (pidfile = fopen(conf->pidfile, "w")))
    {
      LOG("Could not open pidfile %s\n", conf->pidfile);
      exit (0);
    }
  fprintf(pidfile, "%ld\n", (long) pid);
  fclose(pidfile);
  exit (0);
}

/*
  Just do a real DNS request to load libresolv
  before the chroot
*/

void			load_resolv(t_conf *conf)
{
  struct addrinfo	*res;
  struct addrinfo	hints;
  char			*domain;

  memset(&hints, 0, sizeof(hints));

  hints.ai_flags    = AI_CANONNAME;
  hints.ai_family   = PF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;

  /* 
     jump the subdomain, do not try to resolv ourself 
     because we are not a real DNS server
  */
  if (!(domain = strchr(conf->my_domain, '.'))
      || (!*++domain))
    domain = conf->my_domain;
  if (!getaddrinfo(domain, NULL, &hints, &res))
    freeaddrinfo(res);
}

int			jail(t_conf *conf)
{
  struct passwd		*pwd = 0;
  FILE			*pid_file;

  if (!conf->foreground)
    detach_process(conf);
  if ((conf->user) && (!(pwd = getpwnam(conf->user))))
    {
      LOG("User unknown %s", conf->user);
      return (-1);
    }
  if (conf->pid_file)
    {
      pid_file = fopen(conf->pid_file, "w");
      if (pid_file)
        {
          fprintf(pid_file, "%d\n", getpid());
          fclose(pid_file);
        }
      else
        {
	  LOG("Failed to write pidfile");
	  return (-1);
        }
    }
  if (conf->chroot)
    {
      DPRINTF(1, "Chroot to %s\n", conf->chroot);
      load_resolv(conf);
      if ((chroot(conf->chroot) == -1) || chdir("/"))
	{
	  LOG("Failed to chroot in %s\n", conf->chroot);
	  return (-1);
	}
    }
  if (pwd) 
    {
      DPRINTF(1, "Change to user %s\n", conf->user);
      if (setgroups(0, NULL) || setgid(pwd->pw_gid) || setuid(pwd->pw_uid))
	{
	  LOG("Failed to change to user %s\n", conf->user);
	  return (-1);
	}
    }
#ifdef RLIMIT_NPROC
   struct rlimit		rlim;
 
   /* fork() is not need anymore, disable it */
   rlim.rlim_max = rlim.rlim_cur = 0;
   if (setrlimit(RLIMIT_NPROC, &rlim))
     return (-1);
#endif
  return (0);
}

int			main(int argc, char **argv)
{
  t_conf		my_conf;
  t_conf		*conf;

  conf = &my_conf;
  if (get_option(argc, argv, conf))
    return (-1);
  openlog("dns2tcp", LOG_PID , LOG_SYSLOG);
  signal(SIGPIPE, SIG_IGN);
  if (!bind_socket(conf))
    {
      LOG("Starting Server v%s...", VERSION);
      if (jail(conf))
	return (-1);
      srand(getpid()^time(0));
      do_server(conf);
    }
  closelog();
  return (0);
}