3 * $Id: tripe.c,v 1.9 2003/04/15 14:11:09 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.9 2003/04/15 14:11:09 mdw
33 * Rationalize the behaviour of the `-G' and `-U' options.
35 * Revision 1.8 2002/01/13 17:28:29 mdw
36 * Don't turn tracing on if tracing is turned off (!).
38 * Revision 1.7 2002/01/13 17:14:05 mdw
39 * Don't include the tracing option in the help if not compiled in.
41 * Revision 1.6 2001/06/19 22:08:37 mdw
42 * Moved buffers to peer.c.
44 * Revision 1.5 2001/02/16 21:43:12 mdw
45 * Provide a more helpful usage message.
47 * Revision 1.4 2001/02/16 21:41:31 mdw
50 * Revision 1.3 2001/02/04 17:10:40 mdw
51 * Remove a debugging @abort@ call.
53 * Revision 1.2 2001/02/03 22:33:00 mdw
54 * Stuff more randomness into the pool in the interval timer.
56 * Revision 1.1 2001/02/03 20:26:37 mdw
61 /*----- Header files ------------------------------------------------------*/
65 /*----- Global variables --------------------------------------------------*/
69 /*----- Static variables --------------------------------------------------*/
72 #define T_INTERVAL MIN(1)
74 /*----- Main code ---------------------------------------------------------*/
76 /* --- @interval@ --- *
78 * Arguments: @struct timeval *tv@ = time when called
79 * @void *v@ = boring pointer
83 * Use: Called periodically to do housekeeping tasks.
86 void interval(struct timeval
*tv
, void *v
)
89 T( trace(T_PEER
, "peer: interval timer"); )
90 rand_seed(RAND_GLOBAL
, HASHSZ
);
93 tvv
.tv_sec
+= T_INTERVAL
;
94 sel_addtimer(&sel
, &it
, &tvv
, interval
, v
);
99 * Arguments: @int argc@ = number of command line arguments
100 * @char *argv[]@ = vector of arguments
102 * Returns: Zero if OK, nonzero on error.
104 * Use: Main program. Provides a simple VPN.
107 static void usage(FILE *fp
)
109 pquis(fp
, "Usage: $ [-D] [-d dir] [-p port] [-U user] [-G group]\n\
110 [-k priv-keyring] [-K pub-keyring] [-t key-tag]\n\
111 [-a socket] [-T trace-opts]\n");
114 static void version(FILE *fp
)
116 pquis(fp
, "$, version " VERSION
"\n");
119 static void help(FILE *fp
)
127 -h, --help Display this help text.\n\
128 -v, --version Display version number.\n\
129 -u, --usage Display pointless usage message.\n\
131 -D, --daemon Run in the background.\n\
132 -d, --directory=DIR Switch to directory DIR (default $TRIPEDIR).\n\
133 -p, --port=PORT Select UDP port to listen to.\n\
134 -u, --setuid=USER Set uid to USER after initialization.\n\
135 -g, --setgid=GROUP Set gid to GROUP after initialization.\n\
136 -k, --priv-keyring=FILE Get private key from FILE.\n\
137 -K, --pub-keyring=FILE Get public keys from FILE.\n\
138 -t, --tag=KEYTAG Use private key labelled TAG.\n\
139 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
141 -T, --trace=OPTIONS Turn on tracing options.\n\
146 int main(int argc
, char *argv
[])
148 const char *kr_priv
= "keyring", *kr_pub
= "keyring.pub";
149 const char *tag_priv
= "tripe-dh";
150 const char *csock
= "tripesock";
151 const char *dir
= "/var/lib/tripe";
162 T( trace_on(stderr
, 0); )
164 if ((p
= getenv("TRIPEDIR")) != 0)
168 static const struct option opts
[] = {
169 { "help", 0, 0, 'h' },
170 { "version", 0, 0, 'v' },
171 { "usage", 0, 0, 'u' },
173 { "daemon", 0, 0, 'D' },
174 { "uid", OPTF_ARGREQ
, 0, 'U' },
175 { "setuid", OPTF_ARGREQ
, 0, 'U' },
176 { "gid", OPTF_ARGREQ
, 0, 'G' },
177 { "setgid", OPTF_ARGREQ
, 0, 'G' },
178 { "port", OPTF_ARGREQ
, 0, 'p' },
179 { "directory", OPTF_ARGREQ
, 0, 'd' },
180 { "priv-keyring", OPTF_ARGREQ
, 0, 'k' },
181 { "pub-keyring", OPTF_ARGREQ
, 0, 'K' },
182 { "tag", OPTF_ARGREQ
, 0, 't' },
183 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
185 { "trace", OPTF_ARGREQ
, 0, 'T' },
191 int i
= mdwopt(argc
, argv
, "hvu DU:G: p:d:k:K:t:a:" T("T:"),
212 unsigned long i
= strtoul(optarg
, &p
, 0);
216 pw
= getpwnam(optarg
);
218 die(EXIT_FAILURE
, "user `%s' not found", optarg
);
226 unsigned long i
= strtoul(optarg
, &p
, 0);
230 gr
= getgrnam(optarg
);
232 die(EXIT_FAILURE
, "group `%s' not found", optarg
);
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
);
245 if (i
== 0 || i
>= 65536)
246 die(EXIT_FAILURE
, "bad port number %lu", i
);
266 tr_flags
= traceopt(tr_opts
, optarg
, tr_flags
, 0);
267 trace_level(tr_flags
);
276 if (optind
< argc
|| (f
& f_bogus
)) {
282 die(EXIT_FAILURE
, "can't set current directory to `%s': %s",
283 dir
, strerror(errno
));
288 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
289 rand_seed(RAND_GLOBAL
, RMD160_HASHSZ
);
290 signal(SIGPIPE
, SIG_IGN
);
294 a_create(STDIN_FILENO
, STDOUT_FILENO
);
295 if (g
!= (gid_t
)-1) {
296 if (setgid(g
) || (getuid() == 0 && setgroups(1, &g
))) {
297 die(EXIT_FAILURE
, "couldn't setgid to %u: %s",
298 (unsigned)g
, strerror(errno
));
301 if (u
!= (uid_t
)-1) {
303 die(EXIT_FAILURE
, "couldn't setuid to %u: %s",
304 (unsigned)u
, strerror(errno
));
307 km_init(kr_priv
, kr_pub
, tag_priv
);
311 die(EXIT_FAILURE
, "couldn't become a daemon: %s", strerror(errno
));
317 tv
.tv_sec
= time(0) + T_INTERVAL
;
319 sel_addtimer(&sel
, &it
, &tv
, interval
, 0);
325 if (!sel_select(&sel
))
327 else if (errno
!= EINTR
&& errno
!= EAGAIN
) {
328 a_warn("select failed: %s", strerror(errno
));
331 a_warn("too many select errors: bailing out");
341 /*----- That's all, folks -------------------------------------------------*/