3 * $Id: tripe.c,v 1.13 2004/04/08 01:36:17 mdw Exp $
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 void interval(struct timeval *tv, void *v)
57 T( trace(T_PEER, "peer: interval timer"); )
58 rand_seed(RAND_GLOBAL, HASHSZ);
61 tvv.tv_sec += T_INTERVAL;
62 sel_addtimer(&sel, &it, &tvv, interval, v);
67 * Arguments: @int argc@ = number of command line arguments
68 * @char *argv[]@ = vector of arguments
70 * Returns: Zero if OK, nonzero on error.
72 * Use: Main program. Provides a simple VPN.
75 static void usage(FILE *fp)
77 pquis(fp, "Usage: $ [-D] [-d dir] [-b addr] [-p port]\n\
78 [-U user] [-G group] [-a socket] [-T trace-opts]\n\
79 [-k priv-keyring] [-K pub-keyring] [-t key-tag]\n");
82 static void version(FILE *fp)
84 pquis(fp, "$, version " VERSION "\n");
87 static void help(FILE *fp)
95 -h, --help Display this help text.\n\
96 -v, --version Display version number.\n\
97 -u, --usage Display pointless usage message.\n\
99 -D, --daemon Run in the background.\n\
100 -d, --directory=DIR Switch to directory DIR [default " CONFIGDIR "].\n\
101 -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
102 -p, --port=PORT Select UDP port to listen to.\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\
110 -T, --trace=OPTIONS Turn on tracing options.\n\
115 int main(int argc, char *argv[])
117 const char *kr_priv = "keyring", *kr_pub = "keyring.pub";
118 const char *tag_priv = "tripe-dh";
119 const char *csock = SOCKETDIR "/tripesock";
120 const char *dir = CONFIGDIR;
123 struct in_addr baddr = { INADDR_ANY };
132 T( trace_on(stderr, 0); )
134 if ((p = getenv("TRIPEDIR")) != 0)
138 static const struct option opts[] = {
139 { "help", 0, 0, 'h' },
140 { "version", 0, 0, 'v' },
141 { "usage", 0, 0, 'u' },
143 { "daemon", 0, 0, 'D' },
144 { "uid", OPTF_ARGREQ, 0, 'U' },
145 { "setuid", OPTF_ARGREQ, 0, 'U' },
146 { "gid", OPTF_ARGREQ, 0, 'G' },
147 { "setgid", OPTF_ARGREQ, 0, 'G' },
148 { "bind-address", OPTF_ARGREQ, 0, 'b' },
149 { "port", OPTF_ARGREQ, 0, 'p' },
150 { "directory", OPTF_ARGREQ, 0, 'd' },
151 { "priv-keyring", OPTF_ARGREQ, 0, 'k' },
152 { "pub-keyring", OPTF_ARGREQ, 0, 'K' },
153 { "tag", OPTF_ARGREQ, 0, 't' },
154 { "admin-socket", OPTF_ARGREQ, 0, 'a' },
156 { "trace", OPTF_ARGREQ, 0, 'T' },
159 { "tunnel", 0, 0, '0' },
163 int i = mdwopt(argc, argv, "hvu DU:G: b:p:d:k:K:t:a:" T("T:"),
184 unsigned long i = strtoul(optarg, &p, 0);
188 pw = getpwnam(optarg);
190 die(EXIT_FAILURE, "user `%s' not found", optarg);
198 unsigned long i = strtoul(optarg, &p, 0);
202 gr = getgrnam(optarg);
204 die(EXIT_FAILURE, "group `%s' not found", optarg);
209 struct hostent *h = gethostbyname(optarg);
211 die(EXIT_FAILURE, "unknown host name `%s'", optarg);
212 memcpy(&baddr, h->h_addr, sizeof(struct in_addr));
216 unsigned long i = strtoul(optarg, &p, 0);
218 struct servent *s = getservbyname(optarg, "udp");
220 die(EXIT_FAILURE, "unknown service name `%s'", optarg);
221 i = ntohs(s->s_port);
223 if (i == 0 || i >= 65536)
224 die(EXIT_FAILURE, "bad port number %lu", i);
244 tr_flags = traceopt(tr_opts, optarg, tr_flags, 0);
245 trace_level(tr_flags);
249 static const char *tun[] = { "notdef", "unet", "bsd", "linux" };
259 if (optind < argc || (f & f_bogus)) {
265 die(EXIT_FAILURE, "can't set current directory to `%s': %s",
266 dir, strerror(errno));
271 rand_noisesrc(RAND_GLOBAL, &noise_source);
272 rand_seed(RAND_GLOBAL, RMD160_HASHSZ);
273 signal(SIGPIPE, SIG_IGN);
277 a_create(STDIN_FILENO, STDOUT_FILENO);
278 if (g != (gid_t)-1) {
279 if (setgid(g) || (getuid() == 0 && setgroups(1, &g))) {
280 die(EXIT_FAILURE, "couldn't setgid to %u: %s",
281 (unsigned)g, strerror(errno));
284 if (u != (uid_t)-1) {
286 die(EXIT_FAILURE, "couldn't setuid to %u: %s",
287 (unsigned)u, strerror(errno));
290 km_init(kr_priv, kr_pub, tag_priv);
294 die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno));
300 tv.tv_sec = time(0) + T_INTERVAL;
302 sel_addtimer(&sel, &it, &tv, interval, 0);
308 if (!sel_select(&sel))
310 else if (errno != EINTR && errno != EAGAIN) {
311 a_warn("select failed: %s", strerror(errno));
314 a_warn("too many select errors: bailing out");
324 /*----- That's all, folks -------------------------------------------------*/