server/tripe.c: Establish the stdio admin client early.
[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 it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
16 *
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
24 */
25
26 /*----- Header files ------------------------------------------------------*/
27
28 #include "tripe.h"
29
30 /*----- Global variables --------------------------------------------------*/
31
32 sel_state sel;
33
34 /*----- Static variables --------------------------------------------------*/
35
36 static sel_timer it;
37 #define T_INTERVAL MIN(1)
38
39 /*----- Main code ---------------------------------------------------------*/
40
41 /* --- @interval@ --- *
42 *
43 * Arguments: @struct timeval *tv@ = time when called
44 * @void *v@ = boring pointer
45 *
46 * Returns: ---
47 *
48 * Use: Called periodically to do housekeeping tasks.
49 */
50
51 static void interval(struct timeval *tv, void *v)
52 {
53 struct timeval tvv;
54 T( trace(T_PEER, "peer: interval timer"); )
55 rand_seed(RAND_GLOBAL, MAXHASHSZ);
56 p_interval();
57 tvv = *tv;
58 tvv.tv_sec += T_INTERVAL;
59 sel_addtimer(&sel, &it, &tvv, interval, v);
60 }
61
62 /* --- @main@ --- *
63 *
64 * Arguments: @int argc@ = number of command line arguments
65 * @char *argv[]@ = vector of arguments
66 *
67 * Returns: Zero if OK, nonzero on error.
68 *
69 * Use: Main program. Provides a simple VPN.
70 */
71
72 static void usage(FILE *fp)
73 {
74 pquis(fp, "Usage: $ [-DF] [-d DIR] [-b ADDR] [-p PORT] [-n TUNNEL]\n\
75 [-U USER] [-G GROUP] [-a SOCKET] [-m MODE] [-T TRACE-OPTS]\n\
76 [-k PRIV-KEYRING] [-K PUB-KEYRING] [-t KEY-TAG]\n");
77 }
78
79 static void version(FILE *fp) { pquis(fp, "$, version " VERSION "\n"); }
80
81 static void help(FILE *fp)
82 {
83 version(fp);
84 fputc('\n', fp);
85 usage(fp);
86 fputs("\n\
87 Options:\n\
88 \n\
89 -h, --help Display this help text.\n\
90 -v, --version Display version number.\n\
91 -u, --usage Display pointless usage message.\n\
92 --tunnels Display IP tunnel drivers and exit.\n\
93 \n\
94 -4, --ipv4 Transport over IPv4 only.\n\
95 -6, --ipv6 Transport over IPv6 only.\n\
96 -D, --daemon Run in the background.\n\
97 -F, --foreground Quit when stdin reports end-of-file.\n\
98 -d, --directory=DIR Switch to directory DIR [default " CONFIGDIR "].\n\
99 -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
100 -p, --port=PORT Select UDP port to listen to "
101 "[default " STR(TRIPE_PORT) "].\n\
102 -n, --tunnel=TUNNEL Seelect default tunnel driver.\n\
103 -U, --setuid=USER Set uid to USER after initialization.\n\
104 -G, --setgid=GROUP Set gid to GROUP after initialization.\n\
105 -k, --priv-keyring=FILE Get private key from FILE.\n\
106 -K, --pub-keyring=FILE Get public keys from FILE.\n\
107 -t, --tag=KEYTAG Use private key labelled TAG.\n\
108 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
109 -m, --admin-perms=MODE Permissions to set on admin socket [default 600].\n\
110 " T( "\
111 -T, --trace=OPTIONS Turn on tracing options.\n\
112 " ) "\
113 ", fp);
114 }
115
116 int main(int argc, char *argv[])
117 {
118 const char *kr_priv = "keyring", *kr_pub = "keyring.pub";
119 const char *tag_priv = 0;
120 const char *csock = SOCKETDIR "/tripesock";
121 int csockmode = 0600;
122 const char *dir = CONFIGDIR;
123 const char *p;
124 const char *bindhost = 0, *bindsvc = STR(TRIPE_PORT);
125 struct addrinfo aihint = { 0 }, *ailist;
126 unsigned f = 0;
127 int i;
128 int err, selerr = 0;
129 unsigned af;
130 struct timeval tv;
131 uid_t u = -1;
132 gid_t g = -1;
133
134 #define f_bogus 1u
135 #define f_daemon 2u
136 #define f_foreground 4u
137
138 ego(argv[0]);
139 T( trace_on(stderr, 0); )
140
141 if ((p = getenv("TRIPEDIR")) != 0)
142 dir = p;
143 if ((p = getenv("TRIPESOCK")) != 0)
144 csock = p;
145 tun_default = tunnels[0];
146 aihint.ai_family = AF_UNSPEC;
147
148 for (;;) {
149 static const struct option opts[] = {
150 { "help", 0, 0, 'h' },
151 { "version", 0, 0, 'v' },
152 { "usage", 0, 0, 'u' },
153 { "tunnels", 0, 0, '0' },
154
155 { "ipv4", 0, 0, '4' },
156 { "ipv6", 0, 0, '6' },
157 { "daemon", 0, 0, 'D' },
158 { "foreground", 0, 0, 'F' },
159 { "uid", OPTF_ARGREQ, 0, 'U' },
160 { "setuid", OPTF_ARGREQ, 0, 'U' },
161 { "gid", OPTF_ARGREQ, 0, 'G' },
162 { "setgid", OPTF_ARGREQ, 0, 'G' },
163 { "bind-address", OPTF_ARGREQ, 0, 'b' },
164 { "tunnel", OPTF_ARGREQ, 0, 'n' },
165 { "port", OPTF_ARGREQ, 0, 'p' },
166 { "directory", OPTF_ARGREQ, 0, 'd' },
167 { "priv-keyring", OPTF_ARGREQ, 0, 'k' },
168 { "pub-keyring", OPTF_ARGREQ, 0, 'K' },
169 { "tag", OPTF_ARGREQ, 0, 't' },
170 { "admin-socket", OPTF_ARGREQ, 0, 'a' },
171 { "admin-perms", OPTF_ARGREQ, 0, 'm' },
172 #ifndef NTRACE
173 { "trace", OPTF_ARGREQ, 0, 'T' },
174 #endif
175
176 { 0, 0, 0, 0 }
177 };
178
179 i = mdwopt(argc, argv, "hvu46DFU:G:b:n:p:d:k:K:t:a:m:" T("T:"),
180 opts, 0, 0, 0);
181 if (i < 0)
182 break;
183 switch (i) {
184 case 'h':
185 help(stdout);
186 exit(0);
187 case 'v':
188 version(stdout);
189 exit(0);
190 case 'u':
191 usage(stdout);
192 exit(0);
193
194 case '4':
195 aihint.ai_family = AF_INET;
196 break;
197 case '6':
198 aihint.ai_family = AF_INET6;
199 break;
200 case 'D':
201 f |= f_daemon;
202 break;
203 case 'U':
204 u = u_getuser(optarg, &g);
205 break;
206 case 'G':
207 g = u_getgroup(optarg);
208 break;
209 case 'F':
210 f |= f_foreground;
211 break;
212
213 case 'b':
214 bindhost = optarg;
215 break;
216 case 'p':
217 bindsvc = optarg;
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 'm': {
242 char *p;
243 csockmode = strtol(optarg, &p, 8);
244 if (*p) die(EXIT_FAILURE, "bad permissions: `%s'", optarg);
245 } break;
246 case 't':
247 tag_priv = optarg;
248 break;
249 #ifndef NTRACE
250 case 'T':
251 tr_flags = traceopt(tr_opts, optarg, tr_flags, 0);
252 trace_level(tr_flags);
253 break;
254 #endif
255 case '0': {
256 int i;
257 for (i = 0; tunnels[i]; i++)
258 puts(tunnels[i]->name);
259 exit(0);
260 } break;
261 default:
262 f |= f_bogus;
263 break;
264 }
265 }
266
267 if (optind < argc || (f & f_bogus)) {
268 usage(stderr);
269 exit(EXIT_FAILURE);
270 }
271 if (!(~f & (f_daemon | f_foreground)))
272 die(EXIT_FAILURE, "foreground operation for a daemon is silly");
273
274 aihint.ai_protocol = IPPROTO_UDP;
275 aihint.ai_socktype = SOCK_DGRAM;
276 aihint.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
277 if ((err = getaddrinfo(bindhost, bindsvc, &aihint, &ailist)) != 0) {
278 die(EXIT_FAILURE, "couldn't resolve hostname %c%s%c, port `%s': %s",
279 bindhost ? '`' : '<',
280 bindhost ? bindhost : "nil",
281 bindhost ? '\'' : '>',
282 bindsvc, gai_strerror(err));
283 }
284
285 if (chdir(dir)) {
286 die(EXIT_FAILURE, "can't set current directory to `%s': %s",
287 dir, strerror(errno));
288 }
289
290 sel_init(&sel);
291 sig_init(&sel);
292 rand_noisesrc(RAND_GLOBAL, &noise_source);
293 rand_seed(RAND_GLOBAL, MAXHASHSZ);
294 signal(SIGPIPE, SIG_IGN);
295
296 if (!(f & f_daemon)) {
297 af = AF_WARN;
298 #ifndef NTRACE
299 af |= AF_TRACE;
300 #endif
301 if (f & f_foreground)
302 af |= AF_FOREGROUND;
303 a_create(STDIN_FILENO, STDOUT_FILENO, af);
304 a_switcherr();
305 }
306
307 p_init();
308 for (i = 0; tunnels[i]; i++)
309 tunnels[i]->init();
310 p_bind(ailist); freeaddrinfo(ailist);
311
312 for (i = 0; tunnels[i]; i++) {
313 if (tunnels[i]->flags&TUNF_PRIVOPEN) {
314 ps_split(f & f_daemon);
315 break;
316 }
317 }
318
319 a_init();
320 a_signals();
321 a_listen(csock, u, g, csockmode);
322 u_setugid(u, g);
323 km_init(kr_priv, kr_pub, tag_priv);
324 kx_init();
325 if (f & f_daemon) {
326 if (daemonize())
327 die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno));
328 a_daemon();
329 a_switcherr();
330 }
331
332 tv.tv_sec = time(0) + T_INTERVAL;
333 tv.tv_usec = 0;
334 sel_addtimer(&sel, &it, &tv, interval, 0);
335
336 for (;;) {
337 a_preselect();
338 if (!sel_select(&sel))
339 selerr = 0;
340 else if (errno != EINTR && errno != EAGAIN) {
341 a_warn("SERVER", "select-error", "?ERRNO", A_END);
342 selerr++;
343 if (selerr > 8) {
344 a_warn("ABORT", "repeated-select-errors", A_END);
345 abort();
346 }
347 }
348 }
349
350 return (0);
351 }
352
353 /*----- That's all, folks -------------------------------------------------*/