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