server/addrmap.c (hash): Visually tighten the arithmetic.
[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 -D, --daemon Run in the background.\n\
95 -F, --foreground Quit when stdin reports end-of-file.\n\
96 -d, --directory=DIR Switch to directory DIR [default " CONFIGDIR "].\n\
97 -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
98 -p, --port=PORT Select UDP port to listen to "
99 "[default " STR(TRIPE_PORT) "].\n\
100 -n, --tunnel=TUNNEL Seelect default tunnel driver.\n\
101 -U, --setuid=USER Set uid to USER after initialization.\n\
102 -G, --setgid=GROUP Set gid to GROUP after initialization.\n\
103 -k, --priv-keyring=FILE Get private key from FILE.\n\
104 -K, --pub-keyring=FILE Get public keys from FILE.\n\
105 -t, --tag=KEYTAG Use private key labelled TAG.\n\
106 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
107 -m, --admin-perms=MODE Permissions to set on admin socket [default 600].\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 = 0;
118 const char *csock = SOCKETDIR "/tripesock";
119 int csockmode = 0600;
120 const char *dir = CONFIGDIR;
121 const char *p;
122 const char *bindhost = 0, *bindsvc = STR(TRIPE_PORT);
123 struct addrinfo aihint = { 0 }, *ailist;
124 unsigned f = 0;
125 int i;
126 int err, selerr = 0;
127 unsigned af;
128 struct timeval tv;
129 uid_t u = -1;
130 gid_t g = -1;
131
132 #define f_bogus 1u
133 #define f_daemon 2u
134 #define f_foreground 4u
135
136 ego(argv[0]);
137 T( trace_on(stderr, 0); )
138
139 if ((p = getenv("TRIPEDIR")) != 0)
140 dir = p;
141 if ((p = getenv("TRIPESOCK")) != 0)
142 csock = p;
143 tun_default = tunnels[0];
144 aihint.ai_family = AF_INET;
145
146 for (;;) {
147 static const struct option opts[] = {
148 { "help", 0, 0, 'h' },
149 { "version", 0, 0, 'v' },
150 { "usage", 0, 0, 'u' },
151 { "tunnels", 0, 0, '0' },
152
153 { "daemon", 0, 0, 'D' },
154 { "foreground", 0, 0, 'F' },
155 { "uid", OPTF_ARGREQ, 0, 'U' },
156 { "setuid", OPTF_ARGREQ, 0, 'U' },
157 { "gid", OPTF_ARGREQ, 0, 'G' },
158 { "setgid", OPTF_ARGREQ, 0, 'G' },
159 { "bind-address", OPTF_ARGREQ, 0, 'b' },
160 { "tunnel", OPTF_ARGREQ, 0, 'n' },
161 { "port", OPTF_ARGREQ, 0, 'p' },
162 { "directory", OPTF_ARGREQ, 0, 'd' },
163 { "priv-keyring", OPTF_ARGREQ, 0, 'k' },
164 { "pub-keyring", OPTF_ARGREQ, 0, 'K' },
165 { "tag", OPTF_ARGREQ, 0, 't' },
166 { "admin-socket", OPTF_ARGREQ, 0, 'a' },
167 { "admin-perms", OPTF_ARGREQ, 0, 'm' },
168 #ifndef NTRACE
169 { "trace", OPTF_ARGREQ, 0, 'T' },
170 #endif
171
172 { 0, 0, 0, 0 }
173 };
174
175 i = mdwopt(argc, argv, "hvuDFU:G:b:n:p:d:k:K:t:a:m:" T("T:"),
176 opts, 0, 0, 0);
177 if (i < 0)
178 break;
179 switch (i) {
180 case 'h':
181 help(stdout);
182 exit(0);
183 case 'v':
184 version(stdout);
185 exit(0);
186 case 'u':
187 usage(stdout);
188 exit(0);
189
190 case 'D':
191 f |= f_daemon;
192 break;
193 case 'U':
194 u = u_getuser(optarg, &g);
195 break;
196 case 'G':
197 g = u_getgroup(optarg);
198 break;
199 case 'F':
200 f |= f_foreground;
201 break;
202
203 case 'b':
204 bindhost = optarg;
205 break;
206 case 'p':
207 bindsvc = optarg;
208 break;
209 case 'n': {
210 int i;
211 for (i = 0;; i++) {
212 if (!tunnels[i])
213 die(EXIT_FAILURE, "unknown tunnel `%s'", optarg);
214 if (mystrieq(optarg, tunnels[i]->name))
215 break;
216 }
217 tun_default = tunnels[i];
218 } break;
219 case 'd':
220 dir = optarg;
221 break;
222 case 'k':
223 kr_priv = optarg;
224 break;
225 case 'K':
226 kr_pub = optarg;
227 break;
228 case 'a':
229 csock = optarg;
230 break;
231 case 'm': {
232 char *p;
233 csockmode = strtol(optarg, &p, 8);
234 if (*p) die(EXIT_FAILURE, "bad permissions: `%s'", optarg);
235 } break;
236 case 't':
237 tag_priv = optarg;
238 break;
239 #ifndef NTRACE
240 case 'T':
241 tr_flags = traceopt(tr_opts, optarg, tr_flags, 0);
242 trace_level(tr_flags);
243 break;
244 #endif
245 case '0': {
246 int i;
247 for (i = 0; tunnels[i]; i++)
248 puts(tunnels[i]->name);
249 exit(0);
250 } break;
251 default:
252 f |= f_bogus;
253 break;
254 }
255 }
256
257 if (optind < argc || (f & f_bogus)) {
258 usage(stderr);
259 exit(EXIT_FAILURE);
260 }
261 if (!(~f & (f_daemon | f_foreground)))
262 die(EXIT_FAILURE, "foreground operation for a daemon is silly");
263
264 aihint.ai_protocol = IPPROTO_UDP;
265 aihint.ai_socktype = SOCK_DGRAM;
266 aihint.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
267 if ((err = getaddrinfo(bindhost, bindsvc, &aihint, &ailist)) != 0) {
268 die(EXIT_FAILURE, "couldn't resolve hostname %c%s%c, port `%s': %s",
269 bindhost ? '`' : '<',
270 bindhost ? bindhost : "nil",
271 bindhost ? '\'' : '>',
272 bindsvc, gai_strerror(err));
273 }
274
275 if (chdir(dir)) {
276 die(EXIT_FAILURE, "can't set current directory to `%s': %s",
277 dir, strerror(errno));
278 }
279
280 sel_init(&sel);
281 sig_init(&sel);
282 rand_noisesrc(RAND_GLOBAL, &noise_source);
283 rand_seed(RAND_GLOBAL, MAXHASHSZ);
284 signal(SIGPIPE, SIG_IGN);
285 for (i = 0; tunnels[i]; i++)
286 tunnels[i]->init();
287 p_init(ailist); freeaddrinfo(ailist);
288 if (!(f & f_daemon)) {
289 af = AF_WARN;
290 #ifndef NTRACE
291 af |= AF_TRACE;
292 #endif
293 if (f & f_foreground)
294 af |= AF_FOREGROUND;
295 a_create(STDIN_FILENO, STDOUT_FILENO, af);
296 }
297 ps_split(f & f_daemon);
298 a_init(csock, u, g, csockmode);
299 u_setugid(u, g);
300 km_init(kr_priv, kr_pub, tag_priv);
301 if (f & f_daemon) {
302 if (daemonize())
303 die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno));
304 a_daemon();
305 }
306
307 tv.tv_sec = time(0) + T_INTERVAL;
308 tv.tv_usec = 0;
309 sel_addtimer(&sel, &it, &tv, interval, 0);
310
311 for (;;) {
312 a_preselect();
313 if (!sel_select(&sel))
314 selerr = 0;
315 else if (errno != EINTR && errno != EAGAIN) {
316 a_warn("SERVER", "select-error", "?ERRNO", A_END);
317 selerr++;
318 if (selerr > 8) {
319 a_warn("ABORT", "repeated-select-errors", A_END);
320 abort();
321 }
322 }
323 }
324
325 return (0);
326 }
327
328 /*----- That's all, folks -------------------------------------------------*/