chiark / gitweb /
Make `tripe' be the default key type.
[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\
2d752320 76 [-U USER] [-G GROUP] [-a SOCKET] [-T TRACE-OPTS]\n\
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\
c5a2ea9b 108" T( "\
410c8acf 109-T, --trace=OPTIONS Turn on tracing options.\n\
c5a2ea9b 110" ) "\
410c8acf 111", fp);
112}
113
114int main(int argc, char *argv[])
115{
116 const char *kr_priv = "keyring", *kr_pub = "keyring.pub";
fc5f4823 117 const char *tag_priv = 0;
ef4a1ab7 118 const char *csock = SOCKETDIR "/tripesock";
119 const char *dir = CONFIGDIR;
410c8acf 120 const char *p;
165efde7 121 unsigned port = TRIPE_PORT;
767b36e2 122 struct in_addr baddr = { INADDR_ANY };
410c8acf 123 unsigned f = 0;
42da2a58 124 int i;
125 int selerr = 0;
46dde080 126 unsigned af;
42da2a58 127 struct timeval tv;
410c8acf 128 uid_t u = -1;
129 gid_t g = -1;
130
131#define f_bogus 1u
132#define f_daemon 2u
46dde080 133#define f_foreground 4u
410c8acf 134
135 ego(argv[0]);
d360f042 136 T( trace_on(stderr, 0); )
410c8acf 137
138 if ((p = getenv("TRIPEDIR")) != 0)
139 dir = p;
797cf76b
MW
140 if ((p = getenv("TRIPESOCK")) != 0)
141 csock = p;
42da2a58 142 tun_default = tunnels[0];
410c8acf 143
144 for (;;) {
145 static const struct option opts[] = {
146 { "help", 0, 0, 'h' },
147 { "version", 0, 0, 'v' },
148 { "usage", 0, 0, 'u' },
42da2a58 149 { "tunnels", 0, 0, '0' },
410c8acf 150
151 { "daemon", 0, 0, 'D' },
46dde080 152 { "foreground", 0, 0, 'F' },
410c8acf 153 { "uid", OPTF_ARGREQ, 0, 'U' },
154 { "setuid", OPTF_ARGREQ, 0, 'U' },
155 { "gid", OPTF_ARGREQ, 0, 'G' },
156 { "setgid", OPTF_ARGREQ, 0, 'G' },
767b36e2 157 { "bind-address", OPTF_ARGREQ, 0, 'b' },
42da2a58 158 { "tunnel", OPTF_ARGREQ, 0, 'n' },
410c8acf 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
46dde080 172 i = mdwopt(argc, argv, "hvuDFU:G:b:n:p:d:k:K:t:a:" T("T:"),
42da2a58 173 opts, 0, 0, 0);
410c8acf 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;
52b86648
MW
190 case 'U':
191 u = u_getuser(optarg, &g);
192 break;
193 case 'G':
194 g = u_getgroup(optarg);
195 break;
46dde080
MW
196 case 'F':
197 f |= f_foreground;
198 break;
767b36e2 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;
410c8acf 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 }
165efde7 215 if (i >= 65536)
410c8acf 216 die(EXIT_FAILURE, "bad port number %lu", i);
217 port = i;
218 } break;
42da2a58 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;
410c8acf 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
ef4a1ab7 250 case '0': {
42da2a58 251 int i;
252 for (i = 0; tunnels[i]; i++)
253 puts(tunnels[i]->name);
ef4a1ab7 254 exit(0);
255 } break;
410c8acf 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 }
46dde080
MW
266 if (!(~f & (f_daemon | f_foreground)))
267 die(EXIT_FAILURE, "foreground operation for a daemon is silly");
410c8acf 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);
b5c45da1 277 rand_seed(RAND_GLOBAL, MAXHASHSZ);
410c8acf 278 signal(SIGPIPE, SIG_IGN);
42da2a58 279 for (i = 0; tunnels[i]; i++)
280 tunnels[i]->init();
767b36e2 281 p_init(baddr, port);
3cdc3f3a 282 if (!(f & f_daemon)) {
46dde080 283 af = AF_WARN;
3cdc3f3a 284#ifndef NTRACE
46dde080 285 af |= AF_TRACE;
3cdc3f3a 286#endif
46dde080
MW
287 if (f & f_foreground)
288 af |= AF_FOREGROUND;
289 a_create(STDIN_FILENO, STDOUT_FILENO, af);
3cdc3f3a 290 }
388e0319 291 ps_split(f & f_daemon);
aa2405e8 292 a_init(csock, u, g);
52b86648 293 u_setugid(u, g);
410c8acf 294 km_init(kr_priv, kr_pub, tag_priv);
410c8acf 295 if (f & f_daemon) {
19dd2531 296 if (daemonize())
410c8acf 297 die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno));
298 a_daemon();
299 }
300
42da2a58 301 tv.tv_sec = time(0) + T_INTERVAL;
302 tv.tv_usec = 0;
303 sel_addtimer(&sel, &it, &tv, interval, 0);
410c8acf 304
42da2a58 305 for (;;) {
c511e1f9 306 a_preselect();
42da2a58 307 if (!sel_select(&sel))
308 selerr = 0;
309 else if (errno != EINTR && errno != EAGAIN) {
f43df819 310 a_warn("SERVER", "select-error", "?ERRNO", A_END);
42da2a58 311 selerr++;
312 if (selerr > 8) {
f43df819 313 a_warn("ABORT", "repeated-select-errors", A_END);
42da2a58 314 abort();
410c8acf 315 }
316 }
317 }
318
319 return (0);
320}
321
322/*----- That's all, folks -------------------------------------------------*/