410c8acf |
1 | /* -*-c-*- |
2 | * |
d13e5724 |
3 | * $Id: tripe.c,v 1.11 2003/05/17 11:02:03 mdw Exp $ |
410c8acf |
4 | * |
5 | * Main program |
6 | * |
7 | * (c) 2001 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
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. |
18 | * |
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. |
23 | * |
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 | |
29 | /*----- Revision history --------------------------------------------------* |
30 | * |
31 | * $Log: tripe.c,v $ |
d13e5724 |
32 | * Revision 1.11 2003/05/17 11:02:03 mdw |
33 | * Document the new `-b' option. |
34 | * |
767b36e2 |
35 | * Revision 1.10 2003/05/16 12:09:03 mdw |
36 | * Allow binding to a chosen address. |
37 | * |
b303b584 |
38 | * Revision 1.9 2003/04/15 14:11:09 mdw |
39 | * Rationalize the behaviour of the `-G' and `-U' options. |
40 | * |
d360f042 |
41 | * Revision 1.8 2002/01/13 17:28:29 mdw |
42 | * Don't turn tracing on if tracing is turned off (!). |
43 | * |
c5a2ea9b |
44 | * Revision 1.7 2002/01/13 17:14:05 mdw |
45 | * Don't include the tracing option in the help if not compiled in. |
46 | * |
2666d7dd |
47 | * Revision 1.6 2001/06/19 22:08:37 mdw |
48 | * Moved buffers to peer.c. |
49 | * |
680bfcf4 |
50 | * Revision 1.5 2001/02/16 21:43:12 mdw |
51 | * Provide a more helpful usage message. |
52 | * |
37852359 |
53 | * Revision 1.4 2001/02/16 21:41:31 mdw |
54 | * Add a new buffer. |
55 | * |
efd6c9e3 |
56 | * Revision 1.3 2001/02/04 17:10:40 mdw |
57 | * Remove a debugging @abort@ call. |
58 | * |
e31c0b14 |
59 | * Revision 1.2 2001/02/03 22:33:00 mdw |
60 | * Stuff more randomness into the pool in the interval timer. |
61 | * |
410c8acf |
62 | * Revision 1.1 2001/02/03 20:26:37 mdw |
63 | * Initial checkin. |
64 | * |
65 | */ |
66 | |
67 | /*----- Header files ------------------------------------------------------*/ |
68 | |
69 | #include "tripe.h" |
70 | |
71 | /*----- Global variables --------------------------------------------------*/ |
72 | |
73 | sel_state sel; |
410c8acf |
74 | |
75 | /*----- Static variables --------------------------------------------------*/ |
76 | |
77 | static sel_timer it; |
78 | #define T_INTERVAL MIN(1) |
79 | |
80 | /*----- Main code ---------------------------------------------------------*/ |
81 | |
82 | /* --- @interval@ --- * |
83 | * |
84 | * Arguments: @struct timeval *tv@ = time when called |
85 | * @void *v@ = boring pointer |
86 | * |
87 | * Returns: --- |
88 | * |
89 | * Use: Called periodically to do housekeeping tasks. |
90 | */ |
91 | |
92 | void interval(struct timeval *tv, void *v) |
93 | { |
94 | struct timeval tvv; |
95 | T( trace(T_PEER, "peer: interval timer"); ) |
37852359 |
96 | rand_seed(RAND_GLOBAL, HASHSZ); |
410c8acf |
97 | p_interval(); |
98 | tvv = *tv; |
99 | tvv.tv_sec += T_INTERVAL; |
100 | sel_addtimer(&sel, &it, &tvv, interval, v); |
101 | } |
102 | |
103 | /* --- @main@ --- * |
104 | * |
105 | * Arguments: @int argc@ = number of command line arguments |
106 | * @char *argv[]@ = vector of arguments |
107 | * |
108 | * Returns: Zero if OK, nonzero on error. |
109 | * |
110 | * Use: Main program. Provides a simple VPN. |
111 | */ |
112 | |
113 | static void usage(FILE *fp) |
114 | { |
d13e5724 |
115 | pquis(fp, "Usage: $ [-D] [-d dir] [-b addr] [-p port]\n\ |
116 | [-U user] [-G group] [-a socket] [-T trace-opts]\n\ |
117 | [-k priv-keyring] [-K pub-keyring] [-t key-tag]\n"); |
410c8acf |
118 | } |
119 | |
120 | static void version(FILE *fp) |
121 | { |
122 | pquis(fp, "$, version " VERSION "\n"); |
123 | } |
124 | |
125 | static void help(FILE *fp) |
126 | { |
127 | version(fp); |
128 | fputc('\n', fp); |
129 | usage(fp); |
130 | fputs("\n\ |
131 | Options:\n\ |
132 | \n\ |
133 | -h, --help Display this help text.\n\ |
134 | -v, --version Display version number.\n\ |
135 | -u, --usage Display pointless usage message.\n\ |
136 | \n\ |
137 | -D, --daemon Run in the background.\n\ |
138 | -d, --directory=DIR Switch to directory DIR (default $TRIPEDIR).\n\ |
d13e5724 |
139 | -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\ |
410c8acf |
140 | -p, --port=PORT Select UDP port to listen to.\n\ |
d13e5724 |
141 | -U, --setuid=USER Set uid to USER after initialization.\n\ |
142 | -G, --setgid=GROUP Set gid to GROUP after initialization.\n\ |
410c8acf |
143 | -k, --priv-keyring=FILE Get private key from FILE.\n\ |
144 | -K, --pub-keyring=FILE Get public keys from FILE.\n\ |
145 | -t, --tag=KEYTAG Use private key labelled TAG.\n\ |
146 | -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\ |
c5a2ea9b |
147 | " T( "\ |
410c8acf |
148 | -T, --trace=OPTIONS Turn on tracing options.\n\ |
c5a2ea9b |
149 | " ) "\ |
410c8acf |
150 | ", fp); |
151 | } |
152 | |
153 | int main(int argc, char *argv[]) |
154 | { |
155 | const char *kr_priv = "keyring", *kr_pub = "keyring.pub"; |
156 | const char *tag_priv = "tripe-dh"; |
157 | const char *csock = "tripesock"; |
158 | const char *dir = "/var/lib/tripe"; |
159 | const char *p; |
160 | unsigned port = 0; |
767b36e2 |
161 | struct in_addr baddr = { INADDR_ANY }; |
410c8acf |
162 | unsigned f = 0; |
163 | uid_t u = -1; |
164 | gid_t g = -1; |
165 | |
166 | #define f_bogus 1u |
167 | #define f_daemon 2u |
168 | |
169 | ego(argv[0]); |
d360f042 |
170 | T( trace_on(stderr, 0); ) |
410c8acf |
171 | |
172 | if ((p = getenv("TRIPEDIR")) != 0) |
173 | dir = p; |
174 | |
175 | for (;;) { |
176 | static const struct option opts[] = { |
177 | { "help", 0, 0, 'h' }, |
178 | { "version", 0, 0, 'v' }, |
179 | { "usage", 0, 0, 'u' }, |
180 | |
181 | { "daemon", 0, 0, 'D' }, |
182 | { "uid", OPTF_ARGREQ, 0, 'U' }, |
183 | { "setuid", OPTF_ARGREQ, 0, 'U' }, |
184 | { "gid", OPTF_ARGREQ, 0, 'G' }, |
185 | { "setgid", OPTF_ARGREQ, 0, 'G' }, |
767b36e2 |
186 | { "bind-address", OPTF_ARGREQ, 0, 'b' }, |
410c8acf |
187 | { "port", OPTF_ARGREQ, 0, 'p' }, |
188 | { "directory", OPTF_ARGREQ, 0, 'd' }, |
189 | { "priv-keyring", OPTF_ARGREQ, 0, 'k' }, |
190 | { "pub-keyring", OPTF_ARGREQ, 0, 'K' }, |
191 | { "tag", OPTF_ARGREQ, 0, 't' }, |
192 | { "admin-socket", OPTF_ARGREQ, 0, 'a' }, |
193 | #ifndef NTRACE |
194 | { "trace", OPTF_ARGREQ, 0, 'T' }, |
195 | #endif |
196 | |
197 | { 0, 0, 0, 0 } |
198 | }; |
199 | |
767b36e2 |
200 | int i = mdwopt(argc, argv, "hvu DU:G: b:p:d:k:K:t:a:" T("T:"), |
410c8acf |
201 | opts, 0, 0, 0); |
202 | if (i < 0) |
203 | break; |
204 | switch (i) { |
205 | case 'h': |
206 | help(stdout); |
207 | exit(0); |
208 | case 'v': |
209 | version(stdout); |
210 | exit(0); |
211 | case 'u': |
212 | usage(stdout); |
213 | exit(0); |
214 | |
215 | case 'D': |
216 | f |= f_daemon; |
217 | break; |
218 | case 'U': { |
b303b584 |
219 | struct passwd *pw; |
410c8acf |
220 | char *p; |
221 | unsigned long i = strtoul(optarg, &p, 0); |
222 | if (!*p) |
b303b584 |
223 | pw = getpwuid(i); |
224 | else |
225 | pw = getpwnam(optarg); |
226 | if (!pw) |
227 | die(EXIT_FAILURE, "user `%s' not found", optarg); |
228 | u = pw->pw_uid; |
229 | if (g == -1) |
230 | g = pw->pw_gid; |
410c8acf |
231 | } break; |
232 | case 'G': { |
b303b584 |
233 | struct group *gr; |
410c8acf |
234 | char *p; |
235 | unsigned long i = strtoul(optarg, &p, 0); |
236 | if (!*p) |
b303b584 |
237 | gr = getgrgid(i); |
238 | else |
239 | gr = getgrnam(optarg); |
240 | if (!gr) |
241 | die(EXIT_FAILURE, "group `%s' not found", optarg); |
242 | g = gr->gr_gid; |
410c8acf |
243 | } break; |
767b36e2 |
244 | |
245 | case 'b': { |
246 | struct hostent *h = gethostbyname(optarg); |
247 | if (!h) |
248 | die(EXIT_FAILURE, "unknown host name `%s'", optarg); |
249 | memcpy(&baddr, h->h_addr, sizeof(struct in_addr)); |
250 | } break; |
410c8acf |
251 | case 'p': { |
252 | char *p; |
253 | unsigned long i = strtoul(optarg, &p, 0); |
254 | if (*p) { |
255 | struct servent *s = getservbyname(optarg, "udp"); |
256 | if (!s) |
257 | die(EXIT_FAILURE, "unknown service name `%s'", optarg); |
258 | i = ntohs(s->s_port); |
259 | } |
260 | if (i == 0 || i >= 65536) |
261 | die(EXIT_FAILURE, "bad port number %lu", i); |
262 | port = i; |
263 | } break; |
264 | case 'd': |
265 | dir = optarg; |
266 | break; |
267 | case 'k': |
268 | kr_priv = optarg; |
269 | break; |
270 | case 'K': |
271 | kr_pub = optarg; |
272 | break; |
273 | case 'a': |
274 | csock = optarg; |
275 | break; |
276 | case 't': |
277 | tag_priv = optarg; |
278 | break; |
279 | #ifndef NTRACE |
280 | case 'T': |
281 | tr_flags = traceopt(tr_opts, optarg, tr_flags, 0); |
282 | trace_level(tr_flags); |
283 | break; |
284 | #endif |
285 | default: |
286 | f |= f_bogus; |
287 | break; |
288 | } |
289 | } |
290 | |
291 | if (optind < argc || (f & f_bogus)) { |
292 | usage(stderr); |
293 | exit(EXIT_FAILURE); |
294 | } |
295 | |
296 | if (chdir(dir)) { |
297 | die(EXIT_FAILURE, "can't set current directory to `%s': %s", |
298 | dir, strerror(errno)); |
299 | } |
300 | |
301 | sel_init(&sel); |
302 | sig_init(&sel); |
303 | rand_noisesrc(RAND_GLOBAL, &noise_source); |
304 | rand_seed(RAND_GLOBAL, RMD160_HASHSZ); |
305 | signal(SIGPIPE, SIG_IGN); |
306 | tun_init(); |
767b36e2 |
307 | p_init(baddr, port); |
410c8acf |
308 | if (!(f & f_daemon)) |
309 | a_create(STDIN_FILENO, STDOUT_FILENO); |
b303b584 |
310 | if (g != (gid_t)-1) { |
311 | if (setgid(g) || (getuid() == 0 && setgroups(1, &g))) { |
410c8acf |
312 | die(EXIT_FAILURE, "couldn't setgid to %u: %s", |
313 | (unsigned)g, strerror(errno)); |
314 | } |
315 | } |
b303b584 |
316 | if (u != (uid_t)-1) { |
410c8acf |
317 | if (setuid(u)) { |
318 | die(EXIT_FAILURE, "couldn't setuid to %u: %s", |
319 | (unsigned)u, strerror(errno)); |
320 | } |
321 | } |
322 | km_init(kr_priv, kr_pub, tag_priv); |
323 | a_init(csock); |
324 | if (f & f_daemon) { |
37852359 |
325 | if (u_daemon()) |
410c8acf |
326 | die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno)); |
327 | a_daemon(); |
328 | } |
329 | |
330 | { |
331 | struct timeval tv; |
332 | tv.tv_sec = time(0) + T_INTERVAL; |
333 | tv.tv_usec = 0; |
334 | sel_addtimer(&sel, &it, &tv, interval, 0); |
335 | } |
336 | |
337 | { |
338 | int selerr = 0; |
339 | for (;;) { |
340 | if (!sel_select(&sel)) |
341 | selerr = 0; |
342 | else if (errno != EINTR && errno != EAGAIN) { |
343 | a_warn("select failed: %s", strerror(errno)); |
410c8acf |
344 | selerr++; |
345 | if (selerr > 8) { |
346 | a_warn("too many select errors: bailing out"); |
347 | a_quit(); |
348 | } |
349 | } |
350 | } |
351 | } |
352 | |
353 | return (0); |
354 | } |
355 | |
356 | /*----- That's all, folks -------------------------------------------------*/ |