server: Introduce privilege separation.
[tripe] / server / tripe.c
1 /* -*-c-*-
2 *
3 * Main program
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
12 * TrIPE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 /*----- Header files ------------------------------------------------------*/
28
29 #include "tripe.h"
30
31 /*----- Global variables --------------------------------------------------*/
32
33 sel_state sel;
34
35 /*----- Static variables --------------------------------------------------*/
36
37 static sel_timer it;
38 #define T_INTERVAL MIN(1)
39
40 /*----- Main code ---------------------------------------------------------*/
41
42 /* --- @interval@ --- *
43 *
44 * Arguments: @struct timeval *tv@ = time when called
45 * @void *v@ = boring pointer
46 *
47 * Returns: ---
48 *
49 * Use: Called periodically to do housekeeping tasks.
50 */
51
52 static void interval(struct timeval *tv, void *v)
53 {
54 struct timeval tvv;
55 T( trace(T_PEER, "peer: interval timer"); )
56 rand_seed(RAND_GLOBAL, MAXHASHSZ);
57 p_interval();
58 tvv = *tv;
59 tvv.tv_sec += T_INTERVAL;
60 sel_addtimer(&sel, &it, &tvv, interval, v);
61 }
62
63 /* --- @main@ --- *
64 *
65 * Arguments: @int argc@ = number of command line arguments
66 * @char *argv[]@ = vector of arguments
67 *
68 * Returns: Zero if OK, nonzero on error.
69 *
70 * Use: Main program. Provides a simple VPN.
71 */
72
73 static void usage(FILE *fp)
74 {
75 pquis(fp, "Usage: $ [-DF] [-d DIR] [-b ADDR] [-p PORT] [-n TUNNEL]\n\
76 [-U USER] [-G GROUP] [-a SOCKET] [-T TRACE-OPTS]\n\
77 [-k PRIV-KEYRING] [-K PUB-KEYRING] [-t KEY-TAG]\n");
78 }
79
80 static void version(FILE *fp) { pquis(fp, "$, version " VERSION "\n"); }
81
82 static void help(FILE *fp)
83 {
84 version(fp);
85 fputc('\n', fp);
86 usage(fp);
87 fputs("\n\
88 Options:\n\
89 \n\
90 -h, --help Display this help text.\n\
91 -v, --version Display version number.\n\
92 -u, --usage Display pointless usage message.\n\
93 --tunnels Display IP tunnel drivers and exit.\n\
94 \n\
95 -D, --daemon Run in the background.\n\
96 -F, --foreground Quit when stdin reports end-of-file.\n\
97 -d, --directory=DIR Switch to directory DIR [default " CONFIGDIR "].\n\
98 -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
99 -p, --port=PORT Select UDP port to listen to "
100 "[default " STR(TRIPE_PORT) "].\n\
101 -n, --tunnel=TUNNEL Seelect default tunnel driver.\n\
102 -U, --setuid=USER Set uid to USER after initialization.\n\
103 -G, --setgid=GROUP Set gid to GROUP after initialization.\n\
104 -k, --priv-keyring=FILE Get private key from FILE.\n\
105 -K, --pub-keyring=FILE Get public keys from FILE.\n\
106 -t, --tag=KEYTAG Use private key labelled TAG.\n\
107 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
108 " T( "\
109 -T, --trace=OPTIONS Turn on tracing options.\n\
110 " ) "\
111 ", fp);
112 }
113
114 int main(int argc, char *argv[])
115 {
116 const char *kr_priv = "keyring", *kr_pub = "keyring.pub";
117 const char *tag_priv = "tripe-dh";
118 const char *csock = SOCKETDIR "/tripesock";
119 const char *dir = CONFIGDIR;
120 const char *p;
121 unsigned port = TRIPE_PORT;
122 struct in_addr baddr = { INADDR_ANY };
123 unsigned f = 0;
124 int i;
125 int selerr = 0;
126 unsigned af;
127 struct timeval tv;
128 uid_t u = -1;
129 gid_t g = -1;
130
131 #define f_bogus 1u
132 #define f_daemon 2u
133 #define f_foreground 4u
134
135 ego(argv[0]);
136 T( trace_on(stderr, 0); )
137
138 if ((p = getenv("TRIPEDIR")) != 0)
139 dir = p;
140 if ((p = getenv("TRIPESOCK")) != 0)
141 csock = p;
142 tun_default = tunnels[0];
143
144 for (;;) {
145 static const struct option opts[] = {
146 { "help", 0, 0, 'h' },
147 { "version", 0, 0, 'v' },
148 { "usage", 0, 0, 'u' },
149 { "tunnels", 0, 0, '0' },
150
151 { "daemon", 0, 0, 'D' },
152 { "foreground", 0, 0, 'F' },
153 { "uid", OPTF_ARGREQ, 0, 'U' },
154 { "setuid", OPTF_ARGREQ, 0, 'U' },
155 { "gid", OPTF_ARGREQ, 0, 'G' },
156 { "setgid", OPTF_ARGREQ, 0, 'G' },
157 { "bind-address", OPTF_ARGREQ, 0, 'b' },
158 { "tunnel", OPTF_ARGREQ, 0, 'n' },
159 { "port", OPTF_ARGREQ, 0, 'p' },
160 { "directory", OPTF_ARGREQ, 0, 'd' },
161 { "priv-keyring", OPTF_ARGREQ, 0, 'k' },
162 { "pub-keyring", OPTF_ARGREQ, 0, 'K' },
163 { "tag", OPTF_ARGREQ, 0, 't' },
164 { "admin-socket", OPTF_ARGREQ, 0, 'a' },
165 #ifndef NTRACE
166 { "trace", OPTF_ARGREQ, 0, 'T' },
167 #endif
168
169 { 0, 0, 0, 0 }
170 };
171
172 i = mdwopt(argc, argv, "hvuDFU:G:b:n:p:d:k:K:t:a:" T("T:"),
173 opts, 0, 0, 0);
174 if (i < 0)
175 break;
176 switch (i) {
177 case 'h':
178 help(stdout);
179 exit(0);
180 case 'v':
181 version(stdout);
182 exit(0);
183 case 'u':
184 usage(stdout);
185 exit(0);
186
187 case 'D':
188 f |= f_daemon;
189 break;
190 case 'U':
191 u = u_getuser(optarg, &g);
192 break;
193 case 'G':
194 g = u_getgroup(optarg);
195 break;
196 case 'F':
197 f |= f_foreground;
198 break;
199
200 case 'b': {
201 struct hostent *h = gethostbyname(optarg);
202 if (!h)
203 die(EXIT_FAILURE, "unknown host name `%s'", optarg);
204 memcpy(&baddr, h->h_addr, sizeof(struct in_addr));
205 } break;
206 case 'p': {
207 char *p;
208 unsigned long i = strtoul(optarg, &p, 0);
209 if (*p) {
210 struct servent *s = getservbyname(optarg, "udp");
211 if (!s)
212 die(EXIT_FAILURE, "unknown service name `%s'", optarg);
213 i = ntohs(s->s_port);
214 }
215 if (i >= 65536)
216 die(EXIT_FAILURE, "bad port number %lu", i);
217 port = i;
218 } break;
219 case 'n': {
220 int i;
221 for (i = 0;; i++) {
222 if (!tunnels[i])
223 die(EXIT_FAILURE, "unknown tunnel `%s'", optarg);
224 if (mystrieq(optarg, tunnels[i]->name))
225 break;
226 }
227 tun_default = tunnels[i];
228 } break;
229 case 'd':
230 dir = optarg;
231 break;
232 case 'k':
233 kr_priv = optarg;
234 break;
235 case 'K':
236 kr_pub = optarg;
237 break;
238 case 'a':
239 csock = optarg;
240 break;
241 case 't':
242 tag_priv = optarg;
243 break;
244 #ifndef NTRACE
245 case 'T':
246 tr_flags = traceopt(tr_opts, optarg, tr_flags, 0);
247 trace_level(tr_flags);
248 break;
249 #endif
250 case '0': {
251 int i;
252 for (i = 0; tunnels[i]; i++)
253 puts(tunnels[i]->name);
254 exit(0);
255 } break;
256 default:
257 f |= f_bogus;
258 break;
259 }
260 }
261
262 if (optind < argc || (f & f_bogus)) {
263 usage(stderr);
264 exit(EXIT_FAILURE);
265 }
266 if (!(~f & (f_daemon | f_foreground)))
267 die(EXIT_FAILURE, "foreground operation for a daemon is silly");
268
269 if (chdir(dir)) {
270 die(EXIT_FAILURE, "can't set current directory to `%s': %s",
271 dir, strerror(errno));
272 }
273
274 sel_init(&sel);
275 sig_init(&sel);
276 rand_noisesrc(RAND_GLOBAL, &noise_source);
277 rand_seed(RAND_GLOBAL, MAXHASHSZ);
278 signal(SIGPIPE, SIG_IGN);
279 for (i = 0; tunnels[i]; i++)
280 tunnels[i]->init();
281 p_init(baddr, port);
282 if (!(f & f_daemon)) {
283 af = AF_WARN;
284 #ifndef NTRACE
285 af |= AF_TRACE;
286 #endif
287 if (f & f_foreground)
288 af |= AF_FOREGROUND;
289 a_create(STDIN_FILENO, STDOUT_FILENO, af);
290 }
291 ps_split(f & f_daemon);
292 a_init(csock, u, g);
293 u_setugid(u, g);
294 km_init(kr_priv, kr_pub, tag_priv);
295 if (f & f_daemon) {
296 if (daemonize())
297 die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno));
298 a_daemon();
299 }
300
301 tv.tv_sec = time(0) + T_INTERVAL;
302 tv.tv_usec = 0;
303 sel_addtimer(&sel, &it, &tv, interval, 0);
304
305 for (;;) {
306 a_preselect();
307 if (!sel_select(&sel))
308 selerr = 0;
309 else if (errno != EINTR && errno != EAGAIN) {
310 a_warn("SERVER", "select-error", "?ERRNO", A_END);
311 selerr++;
312 if (selerr > 8) {
313 a_warn("ABORT", "repeated-select-errors", A_END);
314 abort();
315 }
316 }
317 }
318
319 return (0);
320 }
321
322 /*----- That's all, folks -------------------------------------------------*/