5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
30 /*----- Global variables --------------------------------------------------*/
34 /*----- Static variables --------------------------------------------------*/
37 #define T_INTERVAL MIN(1)
39 static unsigned iv_nreasons = 0;
40 static struct timeval iv_next = { 0, 0 };
42 /*----- The interval timer ------------------------------------------------*/
44 /* --- @interval@ --- *
46 * Arguments: @struct timeval *tv@ = time when called
47 * @void *v@ = boring pointer
51 * Use: Called periodically to do housekeeping tasks.
54 static void interval(struct timeval *tv, void *v)
56 T( trace(T_PEER, "peer: interval timer"); )
58 rand_seed(RAND_GLOBAL, MAXHASHSZ);
60 iv_next.tv_sec += T_INTERVAL;
61 sel_addtimer(&sel, &it, &iv_next, interval, v);
64 /* --- @iv_addreason@ --- *
70 * Use: Adds an `interval timer reason'; if there are no others, the
71 * interval timer is engaged.
74 void iv_addreason(void)
80 if (TV_CMP(&tv, >=, &iv_next)) interval(&tv, 0);
81 else sel_addtimer(&sel, &it, &iv_next, interval, 0);
86 /* --- @iv_rmreason@ --- *
92 * Use: Removes an interval timer reason; if there are none left, the
93 * interval timer is disengaged.
96 void iv_rmreason(void)
98 assert(iv_nreasons); iv_nreasons--;
99 if (!iv_nreasons) sel_rmtimer(&it);
102 /*----- Main code ---------------------------------------------------------*/
106 * Arguments: @int argc@ = number of command line arguments
107 * @char *argv[]@ = vector of arguments
109 * Returns: Zero if OK, nonzero on error.
111 * Use: Main program. Provides a simple VPN.
114 static void usage(FILE *fp)
116 pquis(fp, "Usage: $ [-DF] [-d DIR] [-b ADDR] [-p PORT] [-n TUNNEL]\n\
117 [-U USER] [-G GROUP] [-a SOCKET] [-m MODE] [-T TRACE-OPTS]\n\
118 [-k PRIV-KEYRING] [-K PUB-KEYRING] [-t KEY-TAG]\n");
121 static void version(FILE *fp) { pquis(fp, "$, version " VERSION "\n"); }
123 static void help(FILE *fp)
131 -h, --help Display this help text.\n\
132 -v, --version Display version number.\n\
133 -u, --usage Display pointless usage message.\n\
134 --tunnels Display IP tunnel drivers and exit.\n\
136 -4, --ipv4 Transport over IPv4 only.\n\
137 -6, --ipv6 Transport over IPv6 only.\n\
138 -D, --daemon Run in the background.\n\
139 -F, --foreground Quit when stdin reports end-of-file.\n\
140 -d, --directory=DIR Switch to directory DIR [default " CONFIGDIR "].\n\
141 -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
142 -p, --port=PORT Select UDP port to listen to "
143 "[default " STR(TRIPE_PORT) "].\n\
144 -n, --tunnel=TUNNEL Seelect default tunnel driver.\n\
145 -U, --setuid=USER Set uid to USER after initialization.\n\
146 -G, --setgid=GROUP Set gid to GROUP after initialization.\n\
147 -k, --priv-keyring=FILE Get private key from FILE.\n\
148 -K, --pub-keyring=FILE Get public keys from FILE.\n\
149 -t, --tag=KEYTAG Use private key labelled TAG.\n\
150 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
151 -m, --admin-perms=MODE Permissions to set on admin socket [default 600].\n\
153 -T, --trace=OPTIONS Turn on tracing options.\n\
158 int main(int argc, char *argv[])
160 const char *kr_priv = "keyring", *kr_pub = "keyring.pub";
161 const char *tag_priv = 0;
162 const char *csock = SOCKETDIR "/tripesock";
163 int csockmode = 0600;
164 const char *dir = CONFIGDIR;
166 const char *bindhost = 0, *bindsvc = STR(TRIPE_PORT);
167 struct addrinfo aihint = { 0 }, *ailist;
177 #define f_foreground 4u
180 T( trace_on(stderr, 0); )
182 if ((p = getenv("TRIPEDIR")) != 0)
184 if ((p = getenv("TRIPESOCK")) != 0)
186 tun_default = tunnels[0];
187 aihint.ai_family = AF_UNSPEC;
190 static const struct option opts[] = {
191 { "help", 0, 0, 'h' },
192 { "version", 0, 0, 'v' },
193 { "usage", 0, 0, 'u' },
194 { "tunnels", 0, 0, '0' },
196 { "ipv4", 0, 0, '4' },
197 { "ipv6", 0, 0, '6' },
198 { "daemon", 0, 0, 'D' },
199 { "foreground", 0, 0, 'F' },
200 { "uid", OPTF_ARGREQ, 0, 'U' },
201 { "setuid", OPTF_ARGREQ, 0, 'U' },
202 { "gid", OPTF_ARGREQ, 0, 'G' },
203 { "setgid", OPTF_ARGREQ, 0, 'G' },
204 { "bind-address", OPTF_ARGREQ, 0, 'b' },
205 { "tunnel", OPTF_ARGREQ, 0, 'n' },
206 { "port", OPTF_ARGREQ, 0, 'p' },
207 { "directory", OPTF_ARGREQ, 0, 'd' },
208 { "priv-keyring", OPTF_ARGREQ, 0, 'k' },
209 { "pub-keyring", OPTF_ARGREQ, 0, 'K' },
210 { "tag", OPTF_ARGREQ, 0, 't' },
211 { "admin-socket", OPTF_ARGREQ, 0, 'a' },
212 { "admin-perms", OPTF_ARGREQ, 0, 'm' },
214 { "trace", OPTF_ARGREQ, 0, 'T' },
220 i = mdwopt(argc, argv, "hvu46DFU:G:b:n:p:d:k:K:t:a:m:" T("T:"),
236 aihint.ai_family = AF_INET;
239 aihint.ai_family = AF_INET6;
245 u = u_getuser(optarg, &g);
248 g = u_getgroup(optarg);
264 die(EXIT_FAILURE, "unknown tunnel `%s'", optarg);
265 if (mystrieq(optarg, tunnels[i]->name))
268 tun_default = tunnels[i];
284 csockmode = strtol(optarg, &p, 8);
285 if (*p) die(EXIT_FAILURE, "bad permissions: `%s'", optarg);
292 tr_flags = traceopt(tr_opts, optarg, tr_flags, 0);
293 trace_level(tr_flags);
298 for (i = 0; tunnels[i]; i++)
299 puts(tunnels[i]->name);
308 if (optind < argc || (f & f_bogus)) {
312 if (!(~f & (f_daemon | f_foreground)))
313 die(EXIT_FAILURE, "foreground operation for a daemon is silly");
315 aihint.ai_protocol = IPPROTO_UDP;
316 aihint.ai_socktype = SOCK_DGRAM;
317 aihint.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
318 if ((err = getaddrinfo(bindhost, bindsvc, &aihint, &ailist)) != 0) {
319 die(EXIT_FAILURE, "couldn't resolve hostname %c%s%c, port `%s': %s",
320 bindhost ? '`' : '<',
321 bindhost ? bindhost : "nil",
322 bindhost ? '\'' : '>',
323 bindsvc, gai_strerror(err));
327 die(EXIT_FAILURE, "can't set current directory to `%s': %s",
328 dir, strerror(errno));
333 rand_noisesrc(RAND_GLOBAL, &noise_source);
334 rand_seed(RAND_GLOBAL, MAXHASHSZ);
335 signal(SIGPIPE, SIG_IGN);
337 if (!(f & f_daemon)) {
342 if (f & f_foreground)
344 a_create(STDIN_FILENO, STDOUT_FILENO, af);
349 for (i = 0; tunnels[i]; i++)
351 p_bind(ailist); freeaddrinfo(ailist);
353 for (i = 0; tunnels[i]; i++) {
354 if (tunnels[i]->flags&TUNF_PRIVOPEN) {
355 ps_split(f & f_daemon);
362 a_listen(csock, u, g, csockmode);
364 km_init(kr_priv, kr_pub, tag_priv);
368 a_warn("SERVER", "daemon-error", "?ERRNO", A_END);
375 gettimeofday(&iv_next, 0); iv_next.tv_sec += T_INTERVAL;
380 if (!sel_select(&sel))
382 else if (errno != EINTR && errno != EAGAIN) {
383 a_warn("SERVER", "select-error", "?ERRNO", A_END);
386 a_warn("ABORT", "repeated-select-errors", A_END);
395 /*----- That's all, folks -------------------------------------------------*/