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
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
31 /*----- Global variables --------------------------------------------------*/
35 /*----- Static variables --------------------------------------------------*/
38 #define T_INTERVAL MIN(1)
40 /*----- Main code ---------------------------------------------------------*/
42 /* --- @interval@ --- *
44 * Arguments: @struct timeval *tv@ = time when called
45 * @void *v@ = boring pointer
49 * Use: Called periodically to do housekeeping tasks.
52 static void interval(struct timeval *tv, void *v)
55 T( trace(T_PEER, "peer: interval timer"); )
56 rand_seed(RAND_GLOBAL, MAXHASHSZ);
59 tvv.tv_sec += T_INTERVAL;
60 sel_addtimer(&sel, &it, &tvv, interval, v);
63 /* --- @mystrieq@ --- *
65 * Arguments: @const char *x, *y@ = two strings
67 * Returns: True if @x@ and @y are equal, up to case.
70 int mystrieq(const char *x, const char *y)
73 if (!*x && !*y) return (1);
74 if (tolower((unsigned char)*x) != tolower((unsigned char)*y))
82 * Arguments: @int argc@ = number of command line arguments
83 * @char *argv[]@ = vector of arguments
85 * Returns: Zero if OK, nonzero on error.
87 * Use: Main program. Provides a simple VPN.
90 static void usage(FILE *fp)
92 pquis(fp, "Usage: $ [-D] [-d DIR] [-b ADDR] [-p PORT] [-n TUNNEL]\n\
93 [-U USER] [-G GROUP] [-a SOCKET] [-T TRACE-OPTS]\n\
94 [-k PRIV-KEYRING] [-K PUB-KEYRING] [-t KEY-TAG]\n");
97 static void version(FILE *fp) { pquis(fp, "$, version " VERSION "\n"); }
99 static void help(FILE *fp)
107 -h, --help Display this help text.\n\
108 -v, --version Display version number.\n\
109 -u, --usage Display pointless usage message.\n\
110 --tunnels Display IP tunnel drivers and exit.\n\
112 -D, --daemon Run in the background.\n\
113 -d, --directory=DIR Switch to directory DIR [default " CONFIGDIR "].\n\
114 -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
115 -p, --port=PORT Select UDP port to listen to "
116 "[default " STR(TRIPE_PORT) "].\n\
117 -n, --tunnel=TUNNEL Seelect default tunnel driver.\n\
118 -U, --setuid=USER Set uid to USER after initialization.\n\
119 -G, --setgid=GROUP Set gid to GROUP after initialization.\n\
120 -k, --priv-keyring=FILE Get private key from FILE.\n\
121 -K, --pub-keyring=FILE Get public keys from FILE.\n\
122 -t, --tag=KEYTAG Use private key labelled TAG.\n\
123 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
125 -T, --trace=OPTIONS Turn on tracing options.\n\
130 int main(int argc, char *argv[])
132 const char *kr_priv = "keyring", *kr_pub = "keyring.pub";
133 const char *tag_priv = "tripe-dh";
134 const char *csock = SOCKETDIR "/tripesock";
135 const char *dir = CONFIGDIR;
137 unsigned port = TRIPE_PORT;
138 struct in_addr baddr = { INADDR_ANY };
150 T( trace_on(stderr, 0); )
152 if ((p = getenv("TRIPEDIR")) != 0)
154 if ((p = getenv("TRIPESOCK")) != 0)
156 tun_default = tunnels[0];
159 static const struct option opts[] = {
160 { "help", 0, 0, 'h' },
161 { "version", 0, 0, 'v' },
162 { "usage", 0, 0, 'u' },
163 { "tunnels", 0, 0, '0' },
165 { "daemon", 0, 0, 'D' },
166 { "uid", OPTF_ARGREQ, 0, 'U' },
167 { "setuid", OPTF_ARGREQ, 0, 'U' },
168 { "gid", OPTF_ARGREQ, 0, 'G' },
169 { "setgid", OPTF_ARGREQ, 0, 'G' },
170 { "bind-address", OPTF_ARGREQ, 0, 'b' },
171 { "tunnel", OPTF_ARGREQ, 0, 'n' },
172 { "port", OPTF_ARGREQ, 0, 'p' },
173 { "directory", OPTF_ARGREQ, 0, 'd' },
174 { "priv-keyring", OPTF_ARGREQ, 0, 'k' },
175 { "pub-keyring", OPTF_ARGREQ, 0, 'K' },
176 { "tag", OPTF_ARGREQ, 0, 't' },
177 { "admin-socket", OPTF_ARGREQ, 0, 'a' },
179 { "trace", OPTF_ARGREQ, 0, 'T' },
185 i = mdwopt(argc, argv, "hvuDU:G:b:p:d:k:K:t:a:" T("T:"),
206 unsigned long i = strtoul(optarg, &p, 0);
210 pw = getpwnam(optarg);
212 die(EXIT_FAILURE, "user `%s' not found", optarg);
220 unsigned long i = strtoul(optarg, &p, 0);
224 gr = getgrnam(optarg);
226 die(EXIT_FAILURE, "group `%s' not found", optarg);
231 struct hostent *h = gethostbyname(optarg);
233 die(EXIT_FAILURE, "unknown host name `%s'", optarg);
234 memcpy(&baddr, h->h_addr, sizeof(struct in_addr));
238 unsigned long i = strtoul(optarg, &p, 0);
240 struct servent *s = getservbyname(optarg, "udp");
242 die(EXIT_FAILURE, "unknown service name `%s'", optarg);
243 i = ntohs(s->s_port);
246 die(EXIT_FAILURE, "bad port number %lu", i);
253 die(EXIT_FAILURE, "unknown tunnel `%s'", optarg);
254 if (mystrieq(optarg, tunnels[i]->name))
257 tun_default = tunnels[i];
276 tr_flags = traceopt(tr_opts, optarg, tr_flags, 0);
277 trace_level(tr_flags);
282 for (i = 0; tunnels[i]; i++)
283 puts(tunnels[i]->name);
292 if (optind < argc || (f & f_bogus)) {
298 die(EXIT_FAILURE, "can't set current directory to `%s': %s",
299 dir, strerror(errno));
304 rand_noisesrc(RAND_GLOBAL, &noise_source);
305 rand_seed(RAND_GLOBAL, MAXHASHSZ);
306 signal(SIGPIPE, SIG_IGN);
307 for (i = 0; tunnels[i]; i++)
310 if (!(f & f_daemon)) {
312 a_create(STDIN_FILENO, STDOUT_FILENO, AF_TRACE | AF_WARN);
314 a_create(STDIN_FILENO, STDOUT_FILENO, AF_WARN);
317 if (g != (gid_t)-1) {
318 if (setgid(g) || (getuid() == 0 && setgroups(1, &g))) {
319 die(EXIT_FAILURE, "couldn't setgid to %u: %s",
320 (unsigned)g, strerror(errno));
323 if (u != (uid_t)-1) {
325 die(EXIT_FAILURE, "couldn't setuid to %u: %s",
326 (unsigned)u, strerror(errno));
329 km_init(kr_priv, kr_pub, tag_priv);
333 die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno));
337 tv.tv_sec = time(0) + T_INTERVAL;
339 sel_addtimer(&sel, &it, &tv, interval, 0);
343 if (!sel_select(&sel))
345 else if (errno != EINTR && errno != EAGAIN) {
346 a_warn("SERVER", "select-error", "?ERRNO", A_END);
349 a_warn("ABORT", "repeated-select-errors", A_END);
358 /*----- That's all, folks -------------------------------------------------*/