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 /*----- Main code ---------------------------------------------------------*/
41 /* --- @interval@ --- *
43 * Arguments: @struct timeval *tv@ = time when called
44 * @void *v@ = boring pointer
48 * Use: Called periodically to do housekeeping tasks.
51 static void interval(struct timeval *tv, void *v)
54 T( trace(T_PEER, "peer: interval timer"); )
55 rand_seed(RAND_GLOBAL, MAXHASHSZ);
58 tvv.tv_sec += T_INTERVAL;
59 sel_addtimer(&sel, &it, &tvv, interval, v);
64 * Arguments: @int argc@ = number of command line arguments
65 * @char *argv[]@ = vector of arguments
67 * Returns: Zero if OK, nonzero on error.
69 * Use: Main program. Provides a simple VPN.
72 static void usage(FILE *fp)
74 pquis(fp, "Usage: $ [-DF] [-d DIR] [-b ADDR] [-p PORT] [-n TUNNEL]\n\
75 [-U USER] [-G GROUP] [-a SOCKET] [-m MODE] [-T TRACE-OPTS]\n\
76 [-k PRIV-KEYRING] [-K PUB-KEYRING] [-t KEY-TAG]\n");
79 static void version(FILE *fp) { pquis(fp, "$, version " VERSION "\n"); }
81 static void help(FILE *fp)
89 -h, --help Display this help text.\n\
90 -v, --version Display version number.\n\
91 -u, --usage Display pointless usage message.\n\
92 --tunnels Display IP tunnel drivers and exit.\n\
94 -4, --ipv4 Transport over IPv4 only.\n\
95 -6, --ipv6 Transport over IPv6 only.\n\
96 -D, --daemon Run in the background.\n\
97 -F, --foreground Quit when stdin reports end-of-file.\n\
98 -d, --directory=DIR Switch to directory DIR [default " CONFIGDIR "].\n\
99 -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
100 -p, --port=PORT Select UDP port to listen to "
101 "[default " STR(TRIPE_PORT) "].\n\
102 -n, --tunnel=TUNNEL Seelect default tunnel driver.\n\
103 -U, --setuid=USER Set uid to USER after initialization.\n\
104 -G, --setgid=GROUP Set gid to GROUP after initialization.\n\
105 -k, --priv-keyring=FILE Get private key from FILE.\n\
106 -K, --pub-keyring=FILE Get public keys from FILE.\n\
107 -t, --tag=KEYTAG Use private key labelled TAG.\n\
108 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
109 -m, --admin-perms=MODE Permissions to set on admin socket [default 600].\n\
111 -T, --trace=OPTIONS Turn on tracing options.\n\
116 int main(int argc, char *argv[])
118 const char *kr_priv = "keyring", *kr_pub = "keyring.pub";
119 const char *tag_priv = 0;
120 const char *csock = SOCKETDIR "/tripesock";
121 int csockmode = 0600;
122 const char *dir = CONFIGDIR;
124 const char *bindhost = 0, *bindsvc = STR(TRIPE_PORT);
125 struct addrinfo aihint = { 0 }, *ailist;
136 #define f_foreground 4u
139 T( trace_on(stderr, 0); )
141 if ((p = getenv("TRIPEDIR")) != 0)
143 if ((p = getenv("TRIPESOCK")) != 0)
145 tun_default = tunnels[0];
146 aihint.ai_family = AF_UNSPEC;
149 static const struct option opts[] = {
150 { "help", 0, 0, 'h' },
151 { "version", 0, 0, 'v' },
152 { "usage", 0, 0, 'u' },
153 { "tunnels", 0, 0, '0' },
155 { "ipv4", 0, 0, '4' },
156 { "ipv6", 0, 0, '6' },
157 { "daemon", 0, 0, 'D' },
158 { "foreground", 0, 0, 'F' },
159 { "uid", OPTF_ARGREQ, 0, 'U' },
160 { "setuid", OPTF_ARGREQ, 0, 'U' },
161 { "gid", OPTF_ARGREQ, 0, 'G' },
162 { "setgid", OPTF_ARGREQ, 0, 'G' },
163 { "bind-address", OPTF_ARGREQ, 0, 'b' },
164 { "tunnel", OPTF_ARGREQ, 0, 'n' },
165 { "port", OPTF_ARGREQ, 0, 'p' },
166 { "directory", OPTF_ARGREQ, 0, 'd' },
167 { "priv-keyring", OPTF_ARGREQ, 0, 'k' },
168 { "pub-keyring", OPTF_ARGREQ, 0, 'K' },
169 { "tag", OPTF_ARGREQ, 0, 't' },
170 { "admin-socket", OPTF_ARGREQ, 0, 'a' },
171 { "admin-perms", OPTF_ARGREQ, 0, 'm' },
173 { "trace", OPTF_ARGREQ, 0, 'T' },
179 i = mdwopt(argc, argv, "hvu46DFU:G:b:n:p:d:k:K:t:a:m:" T("T:"),
195 aihint.ai_family = AF_INET;
198 aihint.ai_family = AF_INET6;
204 u = u_getuser(optarg, &g);
207 g = u_getgroup(optarg);
223 die(EXIT_FAILURE, "unknown tunnel `%s'", optarg);
224 if (mystrieq(optarg, tunnels[i]->name))
227 tun_default = tunnels[i];
243 csockmode = strtol(optarg, &p, 8);
244 if (*p) die(EXIT_FAILURE, "bad permissions: `%s'", optarg);
251 tr_flags = traceopt(tr_opts, optarg, tr_flags, 0);
252 trace_level(tr_flags);
257 for (i = 0; tunnels[i]; i++)
258 puts(tunnels[i]->name);
267 if (optind < argc || (f & f_bogus)) {
271 if (!(~f & (f_daemon | f_foreground)))
272 die(EXIT_FAILURE, "foreground operation for a daemon is silly");
274 aihint.ai_protocol = IPPROTO_UDP;
275 aihint.ai_socktype = SOCK_DGRAM;
276 aihint.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
277 if ((err = getaddrinfo(bindhost, bindsvc, &aihint, &ailist)) != 0) {
278 die(EXIT_FAILURE, "couldn't resolve hostname %c%s%c, port `%s': %s",
279 bindhost ? '`' : '<',
280 bindhost ? bindhost : "nil",
281 bindhost ? '\'' : '>',
282 bindsvc, gai_strerror(err));
286 die(EXIT_FAILURE, "can't set current directory to `%s': %s",
287 dir, strerror(errno));
292 rand_noisesrc(RAND_GLOBAL, &noise_source);
293 rand_seed(RAND_GLOBAL, MAXHASHSZ);
294 signal(SIGPIPE, SIG_IGN);
295 for (i = 0; tunnels[i]; i++)
297 p_init(ailist); freeaddrinfo(ailist);
298 if (!(f & f_daemon)) {
303 if (f & f_foreground)
305 a_create(STDIN_FILENO, STDOUT_FILENO, af);
308 for (i = 0; tunnels[i]; i++) {
309 if (tunnels[i]->flags&TUNF_PRIVOPEN) {
310 ps_split(f & f_daemon);
315 a_init(csock, u, g, csockmode);
317 km_init(kr_priv, kr_pub, tag_priv);
321 die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno));
325 tv.tv_sec = time(0) + T_INTERVAL;
327 sel_addtimer(&sel, &it, &tv, interval, 0);
331 if (!sel_select(&sel))
333 else if (errno != EINTR && errno != EAGAIN) {
334 a_warn("SERVER", "select-error", "?ERRNO", A_END);
337 a_warn("ABORT", "repeated-select-errors", A_END);
346 /*----- That's all, folks -------------------------------------------------*/