Commit | Line | Data |
---|---|---|
410c8acf | 1 | /* -*-c-*- |
410c8acf | 2 | * |
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 | ||
33 | sel_state sel; | |
410c8acf | 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 | ||
42da2a58 | 52 | static 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 | ||
42da2a58 | 63 | /* --- @mystrieq@ --- * |
64 | * | |
65 | * Arguments: @const char *x, *y@ = two strings | |
66 | * | |
67 | * Returns: True if @x@ and @y are equal, up to case. | |
68 | */ | |
69 | ||
70 | int mystrieq(const char *x, const char *y) | |
71 | { | |
72 | for (;;) { | |
73 | if (!*x && !*y) return (1); | |
74 | if (tolower((unsigned char)*x) != tolower((unsigned char)*y)) | |
75 | return (0); | |
76 | x++; y++; | |
77 | } | |
78 | } | |
79 | ||
410c8acf | 80 | /* --- @main@ --- * |
81 | * | |
82 | * Arguments: @int argc@ = number of command line arguments | |
83 | * @char *argv[]@ = vector of arguments | |
84 | * | |
85 | * Returns: Zero if OK, nonzero on error. | |
86 | * | |
87 | * Use: Main program. Provides a simple VPN. | |
88 | */ | |
89 | ||
90 | static void usage(FILE *fp) | |
91 | { | |
42da2a58 | 92 | pquis(fp, "Usage: $ [-D] [-d DIR] [-b ADDR] [-p PORT] [-n TUNNEL]\n\ |
2d752320 | 93 | [-U USER] [-G GROUP] [-a SOCKET] [-T TRACE-OPTS]\n\ |
94 | [-k PRIV-KEYRING] [-K PUB-KEYRING] [-t KEY-TAG]\n"); | |
410c8acf | 95 | } |
96 | ||
f98df549 | 97 | static void version(FILE *fp) { pquis(fp, "$, version " VERSION "\n"); } |
410c8acf | 98 | |
99 | static void help(FILE *fp) | |
100 | { | |
101 | version(fp); | |
102 | fputc('\n', fp); | |
103 | usage(fp); | |
104 | fputs("\n\ | |
105 | Options:\n\ | |
106 | \n\ | |
107 | -h, --help Display this help text.\n\ | |
108 | -v, --version Display version number.\n\ | |
109 | -u, --usage Display pointless usage message.\n\ | |
42da2a58 | 110 | --tunnels Display IP tunnel drivers and exit.\n\ |
410c8acf | 111 | \n\ |
112 | -D, --daemon Run in the background.\n\ | |
ef4a1ab7 | 113 | -d, --directory=DIR Switch to directory DIR [default " CONFIGDIR "].\n\ |
d13e5724 | 114 | -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\ |
165efde7 MW |
115 | -p, --port=PORT Select UDP port to listen to " |
116 | "[default " STR(TRIPE_PORT) "].\n\ | |
42da2a58 | 117 | -n, --tunnel=TUNNEL Seelect default tunnel driver.\n\ |
d13e5724 | 118 | -U, --setuid=USER Set uid to USER after initialization.\n\ |
119 | -G, --setgid=GROUP Set gid to GROUP after initialization.\n\ | |
410c8acf | 120 | -k, --priv-keyring=FILE Get private key from FILE.\n\ |
121 | -K, --pub-keyring=FILE Get public keys from FILE.\n\ | |
122 | -t, --tag=KEYTAG Use private key labelled TAG.\n\ | |
123 | -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\ | |
c5a2ea9b | 124 | " T( "\ |
410c8acf | 125 | -T, --trace=OPTIONS Turn on tracing options.\n\ |
c5a2ea9b | 126 | " ) "\ |
410c8acf | 127 | ", fp); |
128 | } | |
129 | ||
130 | int main(int argc, char *argv[]) | |
131 | { | |
132 | const char *kr_priv = "keyring", *kr_pub = "keyring.pub"; | |
133 | const char *tag_priv = "tripe-dh"; | |
ef4a1ab7 | 134 | const char *csock = SOCKETDIR "/tripesock"; |
135 | const char *dir = CONFIGDIR; | |
410c8acf | 136 | const char *p; |
165efde7 | 137 | unsigned port = TRIPE_PORT; |
767b36e2 | 138 | struct in_addr baddr = { INADDR_ANY }; |
410c8acf | 139 | unsigned f = 0; |
42da2a58 | 140 | int i; |
141 | int selerr = 0; | |
142 | struct timeval tv; | |
410c8acf | 143 | uid_t u = -1; |
144 | gid_t g = -1; | |
145 | ||
146 | #define f_bogus 1u | |
147 | #define f_daemon 2u | |
148 | ||
149 | ego(argv[0]); | |
d360f042 | 150 | T( trace_on(stderr, 0); ) |
410c8acf | 151 | |
152 | if ((p = getenv("TRIPEDIR")) != 0) | |
153 | dir = p; | |
797cf76b MW |
154 | if ((p = getenv("TRIPESOCK")) != 0) |
155 | csock = p; | |
42da2a58 | 156 | tun_default = tunnels[0]; |
410c8acf | 157 | |
158 | for (;;) { | |
159 | static const struct option opts[] = { | |
160 | { "help", 0, 0, 'h' }, | |
161 | { "version", 0, 0, 'v' }, | |
162 | { "usage", 0, 0, 'u' }, | |
42da2a58 | 163 | { "tunnels", 0, 0, '0' }, |
410c8acf | 164 | |
165 | { "daemon", 0, 0, 'D' }, | |
166 | { "uid", OPTF_ARGREQ, 0, 'U' }, | |
167 | { "setuid", OPTF_ARGREQ, 0, 'U' }, | |
168 | { "gid", OPTF_ARGREQ, 0, 'G' }, | |
169 | { "setgid", OPTF_ARGREQ, 0, 'G' }, | |
767b36e2 | 170 | { "bind-address", OPTF_ARGREQ, 0, 'b' }, |
42da2a58 | 171 | { "tunnel", OPTF_ARGREQ, 0, 'n' }, |
410c8acf | 172 | { "port", OPTF_ARGREQ, 0, 'p' }, |
173 | { "directory", OPTF_ARGREQ, 0, 'd' }, | |
174 | { "priv-keyring", OPTF_ARGREQ, 0, 'k' }, | |
175 | { "pub-keyring", OPTF_ARGREQ, 0, 'K' }, | |
176 | { "tag", OPTF_ARGREQ, 0, 't' }, | |
177 | { "admin-socket", OPTF_ARGREQ, 0, 'a' }, | |
178 | #ifndef NTRACE | |
179 | { "trace", OPTF_ARGREQ, 0, 'T' }, | |
180 | #endif | |
181 | ||
182 | { 0, 0, 0, 0 } | |
183 | }; | |
184 | ||
42da2a58 | 185 | i = mdwopt(argc, argv, "hvuDU:G:b:p:d:k:K:t:a:" T("T:"), |
186 | opts, 0, 0, 0); | |
410c8acf | 187 | if (i < 0) |
188 | break; | |
189 | switch (i) { | |
190 | case 'h': | |
191 | help(stdout); | |
192 | exit(0); | |
193 | case 'v': | |
194 | version(stdout); | |
195 | exit(0); | |
196 | case 'u': | |
197 | usage(stdout); | |
198 | exit(0); | |
199 | ||
200 | case 'D': | |
201 | f |= f_daemon; | |
202 | break; | |
203 | case 'U': { | |
b303b584 | 204 | struct passwd *pw; |
410c8acf | 205 | char *p; |
206 | unsigned long i = strtoul(optarg, &p, 0); | |
207 | if (!*p) | |
b303b584 | 208 | pw = getpwuid(i); |
209 | else | |
210 | pw = getpwnam(optarg); | |
211 | if (!pw) | |
212 | die(EXIT_FAILURE, "user `%s' not found", optarg); | |
213 | u = pw->pw_uid; | |
214 | if (g == -1) | |
215 | g = pw->pw_gid; | |
410c8acf | 216 | } break; |
217 | case 'G': { | |
b303b584 | 218 | struct group *gr; |
410c8acf | 219 | char *p; |
220 | unsigned long i = strtoul(optarg, &p, 0); | |
221 | if (!*p) | |
b303b584 | 222 | gr = getgrgid(i); |
223 | else | |
224 | gr = getgrnam(optarg); | |
225 | if (!gr) | |
226 | die(EXIT_FAILURE, "group `%s' not found", optarg); | |
227 | g = gr->gr_gid; | |
410c8acf | 228 | } break; |
767b36e2 | 229 | |
230 | case 'b': { | |
231 | struct hostent *h = gethostbyname(optarg); | |
232 | if (!h) | |
233 | die(EXIT_FAILURE, "unknown host name `%s'", optarg); | |
234 | memcpy(&baddr, h->h_addr, sizeof(struct in_addr)); | |
235 | } break; | |
410c8acf | 236 | case 'p': { |
237 | char *p; | |
238 | unsigned long i = strtoul(optarg, &p, 0); | |
239 | if (*p) { | |
240 | struct servent *s = getservbyname(optarg, "udp"); | |
241 | if (!s) | |
242 | die(EXIT_FAILURE, "unknown service name `%s'", optarg); | |
243 | i = ntohs(s->s_port); | |
244 | } | |
165efde7 | 245 | if (i >= 65536) |
410c8acf | 246 | die(EXIT_FAILURE, "bad port number %lu", i); |
247 | port = i; | |
248 | } break; | |
42da2a58 | 249 | case 'n': { |
250 | int i; | |
251 | for (i = 0;; i++) { | |
252 | if (!tunnels[i]) | |
253 | die(EXIT_FAILURE, "unknown tunnel `%s'", optarg); | |
254 | if (mystrieq(optarg, tunnels[i]->name)) | |
255 | break; | |
256 | } | |
257 | tun_default = tunnels[i]; | |
258 | } break; | |
410c8acf | 259 | case 'd': |
260 | dir = optarg; | |
261 | break; | |
262 | case 'k': | |
263 | kr_priv = optarg; | |
264 | break; | |
265 | case 'K': | |
266 | kr_pub = optarg; | |
267 | break; | |
268 | case 'a': | |
269 | csock = optarg; | |
270 | break; | |
271 | case 't': | |
272 | tag_priv = optarg; | |
273 | break; | |
274 | #ifndef NTRACE | |
275 | case 'T': | |
276 | tr_flags = traceopt(tr_opts, optarg, tr_flags, 0); | |
277 | trace_level(tr_flags); | |
278 | break; | |
279 | #endif | |
ef4a1ab7 | 280 | case '0': { |
42da2a58 | 281 | int i; |
282 | for (i = 0; tunnels[i]; i++) | |
283 | puts(tunnels[i]->name); | |
ef4a1ab7 | 284 | exit(0); |
285 | } break; | |
410c8acf | 286 | default: |
287 | f |= f_bogus; | |
288 | break; | |
289 | } | |
290 | } | |
291 | ||
292 | if (optind < argc || (f & f_bogus)) { | |
293 | usage(stderr); | |
294 | exit(EXIT_FAILURE); | |
295 | } | |
296 | ||
297 | if (chdir(dir)) { | |
298 | die(EXIT_FAILURE, "can't set current directory to `%s': %s", | |
299 | dir, strerror(errno)); | |
300 | } | |
301 | ||
302 | sel_init(&sel); | |
303 | sig_init(&sel); | |
304 | rand_noisesrc(RAND_GLOBAL, &noise_source); | |
b5c45da1 | 305 | rand_seed(RAND_GLOBAL, MAXHASHSZ); |
410c8acf | 306 | signal(SIGPIPE, SIG_IGN); |
42da2a58 | 307 | for (i = 0; tunnels[i]; i++) |
308 | tunnels[i]->init(); | |
767b36e2 | 309 | p_init(baddr, port); |
3cdc3f3a | 310 | if (!(f & f_daemon)) { |
311 | #ifndef NTRACE | |
312 | a_create(STDIN_FILENO, STDOUT_FILENO, AF_TRACE | AF_WARN); | |
313 | #else | |
314 | a_create(STDIN_FILENO, STDOUT_FILENO, AF_WARN); | |
315 | #endif | |
316 | } | |
b303b584 | 317 | if (g != (gid_t)-1) { |
318 | if (setgid(g) || (getuid() == 0 && setgroups(1, &g))) { | |
410c8acf | 319 | die(EXIT_FAILURE, "couldn't setgid to %u: %s", |
320 | (unsigned)g, strerror(errno)); | |
321 | } | |
322 | } | |
b303b584 | 323 | if (u != (uid_t)-1) { |
410c8acf | 324 | if (setuid(u)) { |
325 | die(EXIT_FAILURE, "couldn't setuid to %u: %s", | |
326 | (unsigned)u, strerror(errno)); | |
327 | } | |
328 | } | |
329 | km_init(kr_priv, kr_pub, tag_priv); | |
330 | a_init(csock); | |
331 | if (f & f_daemon) { | |
19dd2531 | 332 | if (daemonize()) |
410c8acf | 333 | die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno)); |
334 | a_daemon(); | |
335 | } | |
336 | ||
42da2a58 | 337 | tv.tv_sec = time(0) + T_INTERVAL; |
338 | tv.tv_usec = 0; | |
339 | sel_addtimer(&sel, &it, &tv, interval, 0); | |
410c8acf | 340 | |
42da2a58 | 341 | for (;;) { |
c511e1f9 | 342 | a_preselect(); |
42da2a58 | 343 | if (!sel_select(&sel)) |
344 | selerr = 0; | |
345 | else if (errno != EINTR && errno != EAGAIN) { | |
f43df819 | 346 | a_warn("SERVER", "select-error", "?ERRNO", A_END); |
42da2a58 | 347 | selerr++; |
348 | if (selerr > 8) { | |
f43df819 | 349 | a_warn("ABORT", "repeated-select-errors", A_END); |
42da2a58 | 350 | abort(); |
410c8acf | 351 | } |
352 | } | |
353 | } | |
354 | ||
355 | return (0); | |
356 | } | |
357 | ||
358 | /*----- That's all, folks -------------------------------------------------*/ |