7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
29 /*----- Header files ------------------------------------------------------*/
33 /*----- Global variables --------------------------------------------------*/
37 /*----- Static variables --------------------------------------------------*/
40 #define T_INTERVAL MIN(1)
42 /*----- Main code ---------------------------------------------------------*/
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)
57 T( trace(T_PEER, "peer: interval timer"); )
58 rand_seed(RAND_GLOBAL, MAXHASHSZ);
61 tvv.tv_sec += T_INTERVAL;
62 sel_addtimer(&sel, &it, &tvv, interval, v);
65 /* --- @mystrieq@ --- *
67 * Arguments: @const char *x, *y@ = two strings
69 * Returns: True if @x@ and @y are equal, up to case.
72 int mystrieq(const char *x, const char *y)
75 if (!*x && !*y) return (1);
76 if (tolower((unsigned char)*x) != tolower((unsigned char)*y))
84 * Arguments: @int argc@ = number of command line arguments
85 * @char *argv[]@ = vector of arguments
87 * Returns: Zero if OK, nonzero on error.
89 * Use: Main program. Provides a simple VPN.
92 static void usage(FILE *fp)
94 pquis(fp, "Usage: $ [-D] [-d DIR] [-b ADDR] [-p PORT] [-n TUNNEL]\n\
95 [-U USER] [-G GROUP] [-a SOCKET] [-T TRACE-OPTS]\n\
96 [-k PRIV-KEYRING] [-K PUB-KEYRING] [-t KEY-TAG]\n");
99 static void version(FILE *fp) { pquis(fp, "$, version " VERSION "\n"); }
101 static void help(FILE *fp)
109 -h, --help Display this help text.\n\
110 -v, --version Display version number.\n\
111 -u, --usage Display pointless usage message.\n\
112 --tunnels Display IP tunnel drivers and exit.\n\
114 -D, --daemon Run in the background.\n\
115 -d, --directory=DIR Switch to directory DIR [default " CONFIGDIR "].\n\
116 -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
117 -p, --port=PORT Select UDP port to listen to "
118 "[default " STR(TRIPE_PORT) "].\n\
119 -n, --tunnel=TUNNEL Seelect default tunnel driver.\n\
120 -U, --setuid=USER Set uid to USER after initialization.\n\
121 -G, --setgid=GROUP Set gid to GROUP after initialization.\n\
122 -k, --priv-keyring=FILE Get private key from FILE.\n\
123 -K, --pub-keyring=FILE Get public keys from FILE.\n\
124 -t, --tag=KEYTAG Use private key labelled TAG.\n\
125 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
127 -T, --trace=OPTIONS Turn on tracing options.\n\
132 int main(int argc, char *argv[])
134 const char *kr_priv = "keyring", *kr_pub = "keyring.pub";
135 const char *tag_priv = "tripe-dh";
136 const char *csock = SOCKETDIR "/tripesock";
137 const char *dir = CONFIGDIR;
139 unsigned port = TRIPE_PORT;
140 struct in_addr baddr = { INADDR_ANY };
152 T( trace_on(stderr, 0); )
154 if ((p = getenv("TRIPEDIR")) != 0)
156 if ((p = getenv("TRIPESOCK")) != 0)
158 tun_default = tunnels[0];
161 static const struct option opts[] = {
162 { "help", 0, 0, 'h' },
163 { "version", 0, 0, 'v' },
164 { "usage", 0, 0, 'u' },
165 { "tunnels", 0, 0, '0' },
167 { "daemon", 0, 0, 'D' },
168 { "uid", OPTF_ARGREQ, 0, 'U' },
169 { "setuid", OPTF_ARGREQ, 0, 'U' },
170 { "gid", OPTF_ARGREQ, 0, 'G' },
171 { "setgid", OPTF_ARGREQ, 0, 'G' },
172 { "bind-address", OPTF_ARGREQ, 0, 'b' },
173 { "tunnel", OPTF_ARGREQ, 0, 'n' },
174 { "port", OPTF_ARGREQ, 0, 'p' },
175 { "directory", OPTF_ARGREQ, 0, 'd' },
176 { "priv-keyring", OPTF_ARGREQ, 0, 'k' },
177 { "pub-keyring", OPTF_ARGREQ, 0, 'K' },
178 { "tag", OPTF_ARGREQ, 0, 't' },
179 { "admin-socket", OPTF_ARGREQ, 0, 'a' },
181 { "trace", OPTF_ARGREQ, 0, 'T' },
187 i = mdwopt(argc, argv, "hvuDU:G:b:p:d:k:K:t:a:" T("T:"),
208 unsigned long i = strtoul(optarg, &p, 0);
212 pw = getpwnam(optarg);
214 die(EXIT_FAILURE, "user `%s' not found", optarg);
222 unsigned long i = strtoul(optarg, &p, 0);
226 gr = getgrnam(optarg);
228 die(EXIT_FAILURE, "group `%s' not found", optarg);
233 struct hostent *h = gethostbyname(optarg);
235 die(EXIT_FAILURE, "unknown host name `%s'", optarg);
236 memcpy(&baddr, h->h_addr, sizeof(struct in_addr));
240 unsigned long i = strtoul(optarg, &p, 0);
242 struct servent *s = getservbyname(optarg, "udp");
244 die(EXIT_FAILURE, "unknown service name `%s'", optarg);
245 i = ntohs(s->s_port);
248 die(EXIT_FAILURE, "bad port number %lu", i);
255 die(EXIT_FAILURE, "unknown tunnel `%s'", optarg);
256 if (mystrieq(optarg, tunnels[i]->name))
259 tun_default = tunnels[i];
278 tr_flags = traceopt(tr_opts, optarg, tr_flags, 0);
279 trace_level(tr_flags);
284 for (i = 0; tunnels[i]; i++)
285 puts(tunnels[i]->name);
294 if (optind < argc || (f & f_bogus)) {
300 die(EXIT_FAILURE, "can't set current directory to `%s': %s",
301 dir, strerror(errno));
306 rand_noisesrc(RAND_GLOBAL, &noise_source);
307 rand_seed(RAND_GLOBAL, MAXHASHSZ);
308 signal(SIGPIPE, SIG_IGN);
309 for (i = 0; tunnels[i]; i++)
312 if (!(f & f_daemon)) {
314 a_create(STDIN_FILENO, STDOUT_FILENO, AF_TRACE | AF_WARN);
316 a_create(STDIN_FILENO, STDOUT_FILENO, AF_WARN);
319 if (g != (gid_t)-1) {
320 if (setgid(g) || (getuid() == 0 && setgroups(1, &g))) {
321 die(EXIT_FAILURE, "couldn't setgid to %u: %s",
322 (unsigned)g, strerror(errno));
325 if (u != (uid_t)-1) {
327 die(EXIT_FAILURE, "couldn't setuid to %u: %s",
328 (unsigned)u, strerror(errno));
331 km_init(kr_priv, kr_pub, tag_priv);
335 die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno));
339 tv.tv_sec = time(0) + T_INTERVAL;
341 sel_addtimer(&sel, &it, &tv, interval, 0);
345 if (!sel_select(&sel))
347 else if (errno != EINTR && errno != EAGAIN) {
348 a_warn("SERVER", "select-error", "?ERRNO", A_END);
351 a_warn("ABORT", "repeated-select-errors", A_END);
360 /*----- That's all, folks -------------------------------------------------*/