3 * $Id: tripe.c,v 1.8 2002/01/13 17:28:29 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 /*----- Revision history --------------------------------------------------*
32 * Revision 1.8 2002/01/13 17:28:29 mdw
33 * Don't turn tracing on if tracing is turned off (!).
35 * Revision 1.7 2002/01/13 17:14:05 mdw
36 * Don't include the tracing option in the help if not compiled in.
38 * Revision 1.6 2001/06/19 22:08:37 mdw
39 * Moved buffers to peer.c.
41 * Revision 1.5 2001/02/16 21:43:12 mdw
42 * Provide a more helpful usage message.
44 * Revision 1.4 2001/02/16 21:41:31 mdw
47 * Revision 1.3 2001/02/04 17:10:40 mdw
48 * Remove a debugging @abort@ call.
50 * Revision 1.2 2001/02/03 22:33:00 mdw
51 * Stuff more randomness into the pool in the interval timer.
53 * Revision 1.1 2001/02/03 20:26:37 mdw
58 /*----- Header files ------------------------------------------------------*/
62 /*----- Global variables --------------------------------------------------*/
66 /*----- Static variables --------------------------------------------------*/
69 #define T_INTERVAL MIN(1)
71 /*----- Main code ---------------------------------------------------------*/
73 /* --- @interval@ --- *
75 * Arguments: @struct timeval *tv@ = time when called
76 * @void *v@ = boring pointer
80 * Use: Called periodically to do housekeeping tasks.
83 void interval(struct timeval *tv, void *v)
86 T( trace(T_PEER, "peer: interval timer"); )
87 rand_seed(RAND_GLOBAL, HASHSZ);
90 tvv.tv_sec += T_INTERVAL;
91 sel_addtimer(&sel, &it, &tvv, interval, v);
96 * Arguments: @int argc@ = number of command line arguments
97 * @char *argv[]@ = vector of arguments
99 * Returns: Zero if OK, nonzero on error.
101 * Use: Main program. Provides a simple VPN.
104 static void usage(FILE *fp)
106 pquis(fp, "Usage: $ [-D] [-p port] [-T trace-opts] [-d dir] [-a socket]\n\
107 [-k priv-keyring] [-K pub-keyring] [-t key-tag]\n");
110 static void version(FILE *fp)
112 pquis(fp, "$, version " VERSION "\n");
115 static void help(FILE *fp)
123 -h, --help Display this help text.\n\
124 -v, --version Display version number.\n\
125 -u, --usage Display pointless usage message.\n\
127 -D, --daemon Run in the background.\n\
128 -d, --directory=DIR Switch to directory DIR (default $TRIPEDIR).\n\
129 -p, --port=PORT Select UDP port to listen to.\n\
130 -k, --priv-keyring=FILE Get private key from FILE.\n\
131 -K, --pub-keyring=FILE Get public keys from FILE.\n\
132 -t, --tag=KEYTAG Use private key labelled TAG.\n\
133 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
135 -T, --trace=OPTIONS Turn on tracing options.\n\
140 int main(int argc, char *argv[])
142 const char *kr_priv = "keyring", *kr_pub = "keyring.pub";
143 const char *tag_priv = "tripe-dh";
144 const char *csock = "tripesock";
145 const char *dir = "/var/lib/tripe";
156 T( trace_on(stderr, 0); )
158 if ((p = getenv("TRIPEDIR")) != 0)
162 static const struct option opts[] = {
163 { "help", 0, 0, 'h' },
164 { "version", 0, 0, 'v' },
165 { "usage", 0, 0, 'u' },
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 { "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 int i = mdwopt(argc, argv, "hvu DU:G: p:d:k:K:t:a:" T("T:"),
205 unsigned long i = strtoul(optarg, &p, 0);
210 if ((pw = getpwnam(optarg)) == 0)
211 die(EXIT_FAILURE, "user name `%s' not found", optarg);
217 unsigned long i = strtoul(optarg, &p, 0);
222 if ((gr = getgrnam(optarg)) == 0)
223 die(EXIT_FAILURE, "group name `%s' not found", optarg);
230 unsigned long i = strtoul(optarg, &p, 0);
232 struct servent *s = getservbyname(optarg, "udp");
234 die(EXIT_FAILURE, "unknown service name `%s'", optarg);
235 i = ntohs(s->s_port);
237 if (i == 0 || i >= 65536)
238 die(EXIT_FAILURE, "bad port number %lu", i);
258 tr_flags = traceopt(tr_opts, optarg, tr_flags, 0);
259 trace_level(tr_flags);
268 if (optind < argc || (f & f_bogus)) {
274 die(EXIT_FAILURE, "can't set current directory to `%s': %s",
275 dir, strerror(errno));
280 rand_noisesrc(RAND_GLOBAL, &noise_source);
281 rand_seed(RAND_GLOBAL, RMD160_HASHSZ);
282 signal(SIGPIPE, SIG_IGN);
286 a_create(STDIN_FILENO, STDOUT_FILENO);
289 die(EXIT_FAILURE, "couldn't setgid to %u: %s",
290 (unsigned)g, strerror(errno));
295 die(EXIT_FAILURE, "couldn't setuid to %u: %s",
296 (unsigned)u, strerror(errno));
299 km_init(kr_priv, kr_pub, tag_priv);
303 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);
317 if (!sel_select(&sel))
319 else if (errno != EINTR && errno != EAGAIN) {
320 a_warn("select failed: %s", strerror(errno));
323 a_warn("too many select errors: bailing out");
333 /*----- That's all, folks -------------------------------------------------*/