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