3 * $Id: tripe.c,v 1.3 2001/02/04 17:10:40 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.3 2001/02/04 17:10:40 mdw
33 * Remove a debugging @abort@ call.
35 * Revision 1.2 2001/02/03 22:33:00 mdw
36 * Stuff more randomness into the pool in the interval timer.
38 * Revision 1.1 2001/02/03 20:26:37 mdw
43 /*----- Header files ------------------------------------------------------*/
47 /*----- Global variables --------------------------------------------------*/
50 octet buf_i[PKBUFSZ], buf_o[PKBUFSZ];
52 /*----- Static variables --------------------------------------------------*/
55 #define T_INTERVAL MIN(1)
57 /*----- Main code ---------------------------------------------------------*/
59 /* --- @interval@ --- *
61 * Arguments: @struct timeval *tv@ = time when called
62 * @void *v@ = boring pointer
66 * Use: Called periodically to do housekeeping tasks.
69 void interval(struct timeval *tv, void *v)
72 T( trace(T_PEER, "peer: interval timer"); )
73 rand_seed(RAND_GLOBAL, RMD160_HASHSZ);
76 tvv.tv_sec += T_INTERVAL;
77 sel_addtimer(&sel, &it, &tvv, interval, v);
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: $ [-options]\n");
95 static void version(FILE *fp)
97 pquis(fp, "$, version " VERSION "\n");
100 static void help(FILE *fp)
108 -h, --help Display this help text.\n\
109 -v, --version Display version number.\n\
110 -u, --usage Display pointless usage message.\n\
112 -D, --daemon Run in the background.\n\
113 -d, --directory=DIR Switch to directory DIR (default $TRIPEDIR).\n\
114 -p, --port=PORT Select UDP port to listen to.\n\
115 -k, --priv-keyring=FILE Get private key from FILE.\n\
116 -K, --pub-keyring=FILE Get public keys from FILE.\n\
117 -t, --tag=KEYTAG Use private key labelled TAG.\n\
118 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
119 -T, --trace=OPTIONS Turn on tracing options.\n\
123 int main(int argc, char *argv[])
125 const char *kr_priv = "keyring", *kr_pub = "keyring.pub";
126 const char *tag_priv = "tripe-dh";
127 const char *csock = "tripesock";
128 const char *dir = "/var/lib/tripe";
141 if ((p = getenv("TRIPEDIR")) != 0)
145 static const struct option opts[] = {
146 { "help", 0, 0, 'h' },
147 { "version", 0, 0, 'v' },
148 { "usage", 0, 0, 'u' },
150 { "daemon", 0, 0, 'D' },
151 { "uid", OPTF_ARGREQ, 0, 'U' },
152 { "setuid", OPTF_ARGREQ, 0, 'U' },
153 { "gid", OPTF_ARGREQ, 0, 'G' },
154 { "setgid", OPTF_ARGREQ, 0, 'G' },
155 { "port", OPTF_ARGREQ, 0, 'p' },
156 { "directory", OPTF_ARGREQ, 0, 'd' },
157 { "priv-keyring", OPTF_ARGREQ, 0, 'k' },
158 { "pub-keyring", OPTF_ARGREQ, 0, 'K' },
159 { "tag", OPTF_ARGREQ, 0, 't' },
160 { "admin-socket", OPTF_ARGREQ, 0, 'a' },
162 { "trace", OPTF_ARGREQ, 0, 'T' },
168 int i = mdwopt(argc, argv, "hvu DU:G: p:d:k:K:t:a:" T("T:"),
188 unsigned long i = strtoul(optarg, &p, 0);
193 if ((pw = getpwnam(optarg)) == 0)
194 die(EXIT_FAILURE, "user name `%s' not found", optarg);
200 unsigned long i = strtoul(optarg, &p, 0);
205 if ((gr = getgrnam(optarg)) == 0)
206 die(EXIT_FAILURE, "group name `%s' not found", optarg);
213 unsigned long i = strtoul(optarg, &p, 0);
215 struct servent *s = getservbyname(optarg, "udp");
217 die(EXIT_FAILURE, "unknown service name `%s'", optarg);
218 i = ntohs(s->s_port);
220 if (i == 0 || i >= 65536)
221 die(EXIT_FAILURE, "bad port number %lu", i);
241 tr_flags = traceopt(tr_opts, optarg, tr_flags, 0);
242 trace_level(tr_flags);
251 if (optind < argc || (f & f_bogus)) {
257 die(EXIT_FAILURE, "can't set current directory to `%s': %s",
258 dir, strerror(errno));
263 rand_noisesrc(RAND_GLOBAL, &noise_source);
264 rand_seed(RAND_GLOBAL, RMD160_HASHSZ);
265 signal(SIGPIPE, SIG_IGN);
269 a_create(STDIN_FILENO, STDOUT_FILENO);
272 die(EXIT_FAILURE, "couldn't setgid to %u: %s",
273 (unsigned)g, strerror(errno));
278 die(EXIT_FAILURE, "couldn't setuid to %u: %s",
279 (unsigned)u, strerror(errno));
282 km_init(kr_priv, kr_pub, tag_priv);
286 die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno));
292 tv.tv_sec = time(0) + T_INTERVAL;
294 sel_addtimer(&sel, &it, &tv, interval, 0);
300 if (!sel_select(&sel))
302 else if (errno != EINTR && errno != EAGAIN) {
303 a_warn("select failed: %s", strerror(errno));
306 a_warn("too many select errors: bailing out");
316 /*----- That's all, folks -------------------------------------------------*/