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 unsigned port = TRIPE_PORT;
123 struct in_addr baddr = { INADDR_ANY };
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];
146 static const struct option opts[] = {
147 { "help", 0, 0, 'h' },
148 { "version", 0, 0, 'v' },
149 { "usage", 0, 0, 'u' },
150 { "tunnels", 0, 0, '0' },
152 { "daemon", 0, 0, 'D' },
153 { "foreground", 0, 0, 'F' },
154 { "uid", OPTF_ARGREQ, 0, 'U' },
155 { "setuid", OPTF_ARGREQ, 0, 'U' },
156 { "gid", OPTF_ARGREQ, 0, 'G' },
157 { "setgid", OPTF_ARGREQ, 0, 'G' },
158 { "bind-address", OPTF_ARGREQ, 0, 'b' },
159 { "tunnel", OPTF_ARGREQ, 0, 'n' },
160 { "port", OPTF_ARGREQ, 0, 'p' },
161 { "directory", OPTF_ARGREQ, 0, 'd' },
162 { "priv-keyring", OPTF_ARGREQ, 0, 'k' },
163 { "pub-keyring", OPTF_ARGREQ, 0, 'K' },
164 { "tag", OPTF_ARGREQ, 0, 't' },
165 { "admin-socket", OPTF_ARGREQ, 0, 'a' },
166 { "admin-perms", OPTF_ARGREQ, 0, 'm' },
168 { "trace", OPTF_ARGREQ, 0, 'T' },
174 i = mdwopt(argc, argv, "hvuDFU:G:b:n:p:d:k:K:t:a:m:" T("T:"),
193 u = u_getuser(optarg, &g);
196 g = u_getgroup(optarg);
203 struct hostent *h = gethostbyname(optarg);
205 die(EXIT_FAILURE, "unknown host name `%s'", optarg);
206 memcpy(&baddr, h->h_addr, sizeof(struct in_addr));
210 unsigned long i = strtoul(optarg, &p, 0);
212 struct servent *s = getservbyname(optarg, "udp");
214 die(EXIT_FAILURE, "unknown service name `%s'", optarg);
215 i = ntohs(s->s_port);
218 die(EXIT_FAILURE, "bad port number %lu", i);
225 die(EXIT_FAILURE, "unknown tunnel `%s'", optarg);
226 if (mystrieq(optarg, tunnels[i]->name))
229 tun_default = tunnels[i];
245 csockmode = strtol(optarg, &p, 8);
246 if (*p) die(EXIT_FAILURE, "bad permissions: `%s'", optarg);
253 tr_flags = traceopt(tr_opts, optarg, tr_flags, 0);
254 trace_level(tr_flags);
259 for (i = 0; tunnels[i]; i++)
260 puts(tunnels[i]->name);
269 if (optind < argc || (f & f_bogus)) {
273 if (!(~f & (f_daemon | f_foreground)))
274 die(EXIT_FAILURE, "foreground operation for a daemon is silly");
277 die(EXIT_FAILURE, "can't set current directory to `%s': %s",
278 dir, strerror(errno));
283 rand_noisesrc(RAND_GLOBAL, &noise_source);
284 rand_seed(RAND_GLOBAL, MAXHASHSZ);
285 signal(SIGPIPE, SIG_IGN);
286 for (i = 0; tunnels[i]; i++)
289 if (!(f & f_daemon)) {
294 if (f & f_foreground)
296 a_create(STDIN_FILENO, STDOUT_FILENO, af);
298 ps_split(f & f_daemon);
299 a_init(csock, u, g, csockmode);
301 km_init(kr_priv, kr_pub, tag_priv);
305 die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno));
309 tv.tv_sec = time(0) + T_INTERVAL;
311 sel_addtimer(&sel, &it, &tv, interval, 0);
315 if (!sel_select(&sel))
317 else if (errno != EINTR && errno != EAGAIN) {
318 a_warn("SERVER", "select-error", "?ERRNO", A_END);
321 a_warn("ABORT", "repeated-select-errors", A_END);
330 /*----- That's all, folks -------------------------------------------------*/