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 -D, --daemon Run in the background.\n\
95 -F, --foreground Quit when stdin reports end-of-file.\n\
96 -d, --directory=DIR Switch to directory DIR [default " CONFIGDIR "].\n\
97 -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
98 -p, --port=PORT Select UDP port to listen to "
99 "[default " STR(TRIPE_PORT) "].\n\
100 -n, --tunnel=TUNNEL Seelect default tunnel driver.\n\
101 -U, --setuid=USER Set uid to USER after initialization.\n\
102 -G, --setgid=GROUP Set gid to GROUP after initialization.\n\
103 -k, --priv-keyring=FILE Get private key from FILE.\n\
104 -K, --pub-keyring=FILE Get public keys from FILE.\n\
105 -t, --tag=KEYTAG Use private key labelled TAG.\n\
106 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
107 -m, --admin-perms=MODE Permissions to set on admin socket [default 600].\n\
109 -T, --trace=OPTIONS Turn on tracing options.\n\
114 int main(int argc, char *argv[])
116 const char *kr_priv = "keyring", *kr_pub = "keyring.pub";
117 const char *tag_priv = 0;
118 const char *csock = SOCKETDIR "/tripesock";
119 int csockmode = 0600;
120 const char *dir = CONFIGDIR;
122 const char *bindhost = 0, *bindsvc = STR(TRIPE_PORT);
123 struct addrinfo aihint = { 0 }, *ailist;
134 #define f_foreground 4u
137 T( trace_on(stderr, 0); )
139 if ((p = getenv("TRIPEDIR")) != 0)
141 if ((p = getenv("TRIPESOCK")) != 0)
143 tun_default = tunnels[0];
144 aihint.ai_family = AF_INET;
147 static const struct option opts[] = {
148 { "help", 0, 0, 'h' },
149 { "version", 0, 0, 'v' },
150 { "usage", 0, 0, 'u' },
151 { "tunnels", 0, 0, '0' },
153 { "daemon", 0, 0, 'D' },
154 { "foreground", 0, 0, 'F' },
155 { "uid", OPTF_ARGREQ, 0, 'U' },
156 { "setuid", OPTF_ARGREQ, 0, 'U' },
157 { "gid", OPTF_ARGREQ, 0, 'G' },
158 { "setgid", OPTF_ARGREQ, 0, 'G' },
159 { "bind-address", OPTF_ARGREQ, 0, 'b' },
160 { "tunnel", OPTF_ARGREQ, 0, 'n' },
161 { "port", OPTF_ARGREQ, 0, 'p' },
162 { "directory", OPTF_ARGREQ, 0, 'd' },
163 { "priv-keyring", OPTF_ARGREQ, 0, 'k' },
164 { "pub-keyring", OPTF_ARGREQ, 0, 'K' },
165 { "tag", OPTF_ARGREQ, 0, 't' },
166 { "admin-socket", OPTF_ARGREQ, 0, 'a' },
167 { "admin-perms", OPTF_ARGREQ, 0, 'm' },
169 { "trace", OPTF_ARGREQ, 0, 'T' },
175 i = mdwopt(argc, argv, "hvuDFU:G:b:n:p:d:k:K:t:a:m:" T("T:"),
194 u = u_getuser(optarg, &g);
197 g = u_getgroup(optarg);
213 die(EXIT_FAILURE, "unknown tunnel `%s'", optarg);
214 if (mystrieq(optarg, tunnels[i]->name))
217 tun_default = tunnels[i];
233 csockmode = strtol(optarg, &p, 8);
234 if (*p) die(EXIT_FAILURE, "bad permissions: `%s'", optarg);
241 tr_flags = traceopt(tr_opts, optarg, tr_flags, 0);
242 trace_level(tr_flags);
247 for (i = 0; tunnels[i]; i++)
248 puts(tunnels[i]->name);
257 if (optind < argc || (f & f_bogus)) {
261 if (!(~f & (f_daemon | f_foreground)))
262 die(EXIT_FAILURE, "foreground operation for a daemon is silly");
264 aihint.ai_protocol = IPPROTO_UDP;
265 aihint.ai_socktype = SOCK_DGRAM;
266 aihint.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
267 if ((err = getaddrinfo(bindhost, bindsvc, &aihint, &ailist)) != 0) {
268 die(EXIT_FAILURE, "couldn't resolve hostname %c%s%c, port `%s': %s",
269 bindhost ? '`' : '<',
270 bindhost ? bindhost : "nil",
271 bindhost ? '\'' : '>',
272 bindsvc, gai_strerror(err));
276 die(EXIT_FAILURE, "can't set current directory to `%s': %s",
277 dir, strerror(errno));
282 rand_noisesrc(RAND_GLOBAL, &noise_source);
283 rand_seed(RAND_GLOBAL, MAXHASHSZ);
284 signal(SIGPIPE, SIG_IGN);
285 for (i = 0; tunnels[i]; i++)
287 p_init(ailist); freeaddrinfo(ailist);
288 if (!(f & f_daemon)) {
293 if (f & f_foreground)
295 a_create(STDIN_FILENO, STDOUT_FILENO, af);
297 ps_split(f & f_daemon);
298 a_init(csock, u, g, csockmode);
300 km_init(kr_priv, kr_pub, tag_priv);
303 die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno));
307 tv.tv_sec = time(0) + T_INTERVAL;
309 sel_addtimer(&sel, &it, &tv, interval, 0);
313 if (!sel_select(&sel))
315 else if (errno != EINTR && errno != EAGAIN) {
316 a_warn("SERVER", "select-error", "?ERRNO", A_END);
319 a_warn("ABORT", "repeated-select-errors", A_END);
328 /*----- That's all, folks -------------------------------------------------*/