chiark / gitweb /
server/tests.at (AWAIT_KXDONE): Ignore the correct server messages.
[tripe] / server / tripe.c
CommitLineData
410c8acf 1/* -*-c-*-
2 *
410c8acf 3 * Main program
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
e04c2d50 8/*----- Licensing notice --------------------------------------------------*
410c8acf 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.
e04c2d50 16 *
410c8acf 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.
e04c2d50 21 *
410c8acf 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
410c8acf 27/*----- Header files ------------------------------------------------------*/
28
29#include "tripe.h"
30
31/*----- Global variables --------------------------------------------------*/
32
33sel_state sel;
410c8acf 34
35/*----- Static variables --------------------------------------------------*/
36
37static 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
42da2a58 52static void interval(struct timeval *tv, void *v)
410c8acf 53{
54 struct timeval tvv;
55 T( trace(T_PEER, "peer: interval timer"); )
b5c45da1 56 rand_seed(RAND_GLOBAL, MAXHASHSZ);
410c8acf 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
73static void usage(FILE *fp)
74{
46dde080 75 pquis(fp, "Usage: $ [-DF] [-d DIR] [-b ADDR] [-p PORT] [-n TUNNEL]\n\
a9279e37 76 [-U USER] [-G GROUP] [-a SOCKET] [-m MODE] [-T TRACE-OPTS]\n\
2d752320 77 [-k PRIV-KEYRING] [-K PUB-KEYRING] [-t KEY-TAG]\n");
410c8acf 78}
79
f98df549 80static void version(FILE *fp) { pquis(fp, "$, version " VERSION "\n"); }
410c8acf 81
82static void help(FILE *fp)
83{
84 version(fp);
85 fputc('\n', fp);
86 usage(fp);
87 fputs("\n\
88Options:\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\
42da2a58 93 --tunnels Display IP tunnel drivers and exit.\n\
410c8acf 94\n\
95-D, --daemon Run in the background.\n\
46dde080 96-F, --foreground Quit when stdin reports end-of-file.\n\
ef4a1ab7 97-d, --directory=DIR Switch to directory DIR [default " CONFIGDIR "].\n\
d13e5724 98-b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
165efde7
MW
99-p, --port=PORT Select UDP port to listen to "
100 "[default " STR(TRIPE_PORT) "].\n\
42da2a58 101-n, --tunnel=TUNNEL Seelect default tunnel driver.\n\
d13e5724 102-U, --setuid=USER Set uid to USER after initialization.\n\
103-G, --setgid=GROUP Set gid to GROUP after initialization.\n\
410c8acf 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\
a9279e37 108-m, --admin-perms=MODE Permissions to set on admin socket [default 600].\n\
c5a2ea9b 109" T( "\
410c8acf 110-T, --trace=OPTIONS Turn on tracing options.\n\
c5a2ea9b 111" ) "\
410c8acf 112", fp);
113}
114
115int main(int argc, char *argv[])
116{
117 const char *kr_priv = "keyring", *kr_pub = "keyring.pub";
fc5f4823 118 const char *tag_priv = 0;
ef4a1ab7 119 const char *csock = SOCKETDIR "/tripesock";
a9279e37 120 int csockmode = 0600;
ef4a1ab7 121 const char *dir = CONFIGDIR;
410c8acf 122 const char *p;
165efde7 123 unsigned port = TRIPE_PORT;
767b36e2 124 struct in_addr baddr = { INADDR_ANY };
410c8acf 125 unsigned f = 0;
42da2a58 126 int i;
127 int selerr = 0;
46dde080 128 unsigned af;
42da2a58 129 struct timeval tv;
410c8acf 130 uid_t u = -1;
131 gid_t g = -1;
132
133#define f_bogus 1u
134#define f_daemon 2u
46dde080 135#define f_foreground 4u
410c8acf 136
137 ego(argv[0]);
d360f042 138 T( trace_on(stderr, 0); )
410c8acf 139
140 if ((p = getenv("TRIPEDIR")) != 0)
141 dir = p;
797cf76b
MW
142 if ((p = getenv("TRIPESOCK")) != 0)
143 csock = p;
42da2a58 144 tun_default = tunnels[0];
410c8acf 145
146 for (;;) {
147 static const struct option opts[] = {
148 { "help", 0, 0, 'h' },
149 { "version", 0, 0, 'v' },
150 { "usage", 0, 0, 'u' },
42da2a58 151 { "tunnels", 0, 0, '0' },
410c8acf 152
153 { "daemon", 0, 0, 'D' },
46dde080 154 { "foreground", 0, 0, 'F' },
410c8acf 155 { "uid", OPTF_ARGREQ, 0, 'U' },
156 { "setuid", OPTF_ARGREQ, 0, 'U' },
157 { "gid", OPTF_ARGREQ, 0, 'G' },
158 { "setgid", OPTF_ARGREQ, 0, 'G' },
767b36e2 159 { "bind-address", OPTF_ARGREQ, 0, 'b' },
42da2a58 160 { "tunnel", OPTF_ARGREQ, 0, 'n' },
410c8acf 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' },
a9279e37 167 { "admin-perms", OPTF_ARGREQ, 0, 'm' },
410c8acf 168#ifndef NTRACE
169 { "trace", OPTF_ARGREQ, 0, 'T' },
170#endif
171
172 { 0, 0, 0, 0 }
173 };
174
a9279e37 175 i = mdwopt(argc, argv, "hvuDFU:G:b:n:p:d:k:K:t:a:m:" T("T:"),
42da2a58 176 opts, 0, 0, 0);
410c8acf 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;
52b86648
MW
193 case 'U':
194 u = u_getuser(optarg, &g);
195 break;
196 case 'G':
197 g = u_getgroup(optarg);
198 break;
46dde080
MW
199 case 'F':
200 f |= f_foreground;
201 break;
767b36e2 202
203 case 'b': {
204 struct hostent *h = gethostbyname(optarg);
205 if (!h)
206 die(EXIT_FAILURE, "unknown host name `%s'", optarg);
207 memcpy(&baddr, h->h_addr, sizeof(struct in_addr));
208 } break;
410c8acf 209 case 'p': {
210 char *p;
211 unsigned long i = strtoul(optarg, &p, 0);
212 if (*p) {
213 struct servent *s = getservbyname(optarg, "udp");
214 if (!s)
215 die(EXIT_FAILURE, "unknown service name `%s'", optarg);
216 i = ntohs(s->s_port);
217 }
165efde7 218 if (i >= 65536)
410c8acf 219 die(EXIT_FAILURE, "bad port number %lu", i);
220 port = i;
221 } break;
42da2a58 222 case 'n': {
223 int i;
224 for (i = 0;; i++) {
225 if (!tunnels[i])
226 die(EXIT_FAILURE, "unknown tunnel `%s'", optarg);
227 if (mystrieq(optarg, tunnels[i]->name))
228 break;
229 }
230 tun_default = tunnels[i];
231 } break;
410c8acf 232 case 'd':
233 dir = optarg;
234 break;
235 case 'k':
236 kr_priv = optarg;
237 break;
238 case 'K':
239 kr_pub = optarg;
240 break;
241 case 'a':
242 csock = optarg;
243 break;
a9279e37
MW
244 case 'm': {
245 char *p;
246 csockmode = strtol(optarg, &p, 8);
247 if (*p) die(EXIT_FAILURE, "bad permissions: `%s'", optarg);
248 } break;
410c8acf 249 case 't':
250 tag_priv = optarg;
251 break;
252#ifndef NTRACE
253 case 'T':
254 tr_flags = traceopt(tr_opts, optarg, tr_flags, 0);
255 trace_level(tr_flags);
256 break;
257#endif
ef4a1ab7 258 case '0': {
42da2a58 259 int i;
260 for (i = 0; tunnels[i]; i++)
261 puts(tunnels[i]->name);
ef4a1ab7 262 exit(0);
263 } break;
410c8acf 264 default:
265 f |= f_bogus;
266 break;
267 }
268 }
269
270 if (optind < argc || (f & f_bogus)) {
271 usage(stderr);
272 exit(EXIT_FAILURE);
273 }
46dde080
MW
274 if (!(~f & (f_daemon | f_foreground)))
275 die(EXIT_FAILURE, "foreground operation for a daemon is silly");
410c8acf 276
277 if (chdir(dir)) {
278 die(EXIT_FAILURE, "can't set current directory to `%s': %s",
279 dir, strerror(errno));
280 }
281
282 sel_init(&sel);
283 sig_init(&sel);
284 rand_noisesrc(RAND_GLOBAL, &noise_source);
b5c45da1 285 rand_seed(RAND_GLOBAL, MAXHASHSZ);
410c8acf 286 signal(SIGPIPE, SIG_IGN);
42da2a58 287 for (i = 0; tunnels[i]; i++)
288 tunnels[i]->init();
767b36e2 289 p_init(baddr, port);
3cdc3f3a 290 if (!(f & f_daemon)) {
46dde080 291 af = AF_WARN;
3cdc3f3a 292#ifndef NTRACE
46dde080 293 af |= AF_TRACE;
3cdc3f3a 294#endif
46dde080
MW
295 if (f & f_foreground)
296 af |= AF_FOREGROUND;
297 a_create(STDIN_FILENO, STDOUT_FILENO, af);
3cdc3f3a 298 }
388e0319 299 ps_split(f & f_daemon);
a9279e37 300 a_init(csock, u, g, csockmode);
52b86648 301 u_setugid(u, g);
410c8acf 302 km_init(kr_priv, kr_pub, tag_priv);
410c8acf 303 if (f & f_daemon) {
19dd2531 304 if (daemonize())
410c8acf 305 die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno));
306 a_daemon();
307 }
308
42da2a58 309 tv.tv_sec = time(0) + T_INTERVAL;
310 tv.tv_usec = 0;
311 sel_addtimer(&sel, &it, &tv, interval, 0);
410c8acf 312
42da2a58 313 for (;;) {
c511e1f9 314 a_preselect();
42da2a58 315 if (!sel_select(&sel))
316 selerr = 0;
317 else if (errno != EINTR && errno != EAGAIN) {
f43df819 318 a_warn("SERVER", "select-error", "?ERRNO", A_END);
42da2a58 319 selerr++;
320 if (selerr > 8) {
f43df819 321 a_warn("ABORT", "repeated-select-errors", A_END);
42da2a58 322 abort();
410c8acf 323 }
324 }
325 }
326
327 return (0);
328}
329
330/*----- That's all, folks -------------------------------------------------*/