3 * $Id: tripe.c,v 1.11 2003/05/17 11:02:03 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.11 2003/05/17 11:02:03 mdw
33 * Document the new `-b' option.
35 * Revision 1.10 2003/05/16 12:09:03 mdw
36 * Allow binding to a chosen address.
38 * Revision 1.9 2003/04/15 14:11:09 mdw
39 * Rationalize the behaviour of the `-G' and `-U' options.
41 * Revision 1.8 2002/01/13 17:28:29 mdw
42 * Don't turn tracing on if tracing is turned off (!).
44 * Revision 1.7 2002/01/13 17:14:05 mdw
45 * Don't include the tracing option in the help if not compiled in.
47 * Revision 1.6 2001/06/19 22:08:37 mdw
48 * Moved buffers to peer.c.
50 * Revision 1.5 2001/02/16 21:43:12 mdw
51 * Provide a more helpful usage message.
53 * Revision 1.4 2001/02/16 21:41:31 mdw
56 * Revision 1.3 2001/02/04 17:10:40 mdw
57 * Remove a debugging @abort@ call.
59 * Revision 1.2 2001/02/03 22:33:00 mdw
60 * Stuff more randomness into the pool in the interval timer.
62 * Revision 1.1 2001/02/03 20:26:37 mdw
67 /*----- Header files ------------------------------------------------------*/
71 /*----- Global variables --------------------------------------------------*/
75 /*----- Static variables --------------------------------------------------*/
78 #define T_INTERVAL MIN(1)
80 /*----- Main code ---------------------------------------------------------*/
82 /* --- @interval@ --- *
84 * Arguments: @struct timeval *tv@ = time when called
85 * @void *v@ = boring pointer
89 * Use: Called periodically to do housekeeping tasks.
92 void interval(struct timeval
*tv
, void *v
)
95 T( trace(T_PEER
, "peer: interval timer"); )
96 rand_seed(RAND_GLOBAL
, HASHSZ
);
99 tvv
.tv_sec
+= T_INTERVAL
;
100 sel_addtimer(&sel
, &it
, &tvv
, interval
, v
);
105 * Arguments: @int argc@ = number of command line arguments
106 * @char *argv[]@ = vector of arguments
108 * Returns: Zero if OK, nonzero on error.
110 * Use: Main program. Provides a simple VPN.
113 static void usage(FILE *fp
)
115 pquis(fp
, "Usage: $ [-D] [-d dir] [-b addr] [-p port]\n\
116 [-U user] [-G group] [-a socket] [-T trace-opts]\n\
117 [-k priv-keyring] [-K pub-keyring] [-t key-tag]\n");
120 static void version(FILE *fp
)
122 pquis(fp
, "$, version " VERSION
"\n");
125 static void help(FILE *fp
)
133 -h, --help Display this help text.\n\
134 -v, --version Display version number.\n\
135 -u, --usage Display pointless usage message.\n\
137 -D, --daemon Run in the background.\n\
138 -d, --directory=DIR Switch to directory DIR (default $TRIPEDIR).\n\
139 -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
140 -p, --port=PORT Select UDP port to listen to.\n\
141 -U, --setuid=USER Set uid to USER after initialization.\n\
142 -G, --setgid=GROUP Set gid to GROUP after initialization.\n\
143 -k, --priv-keyring=FILE Get private key from FILE.\n\
144 -K, --pub-keyring=FILE Get public keys from FILE.\n\
145 -t, --tag=KEYTAG Use private key labelled TAG.\n\
146 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
148 -T, --trace=OPTIONS Turn on tracing options.\n\
153 int main(int argc
, char *argv
[])
155 const char *kr_priv
= "keyring", *kr_pub
= "keyring.pub";
156 const char *tag_priv
= "tripe-dh";
157 const char *csock
= "tripesock";
158 const char *dir
= "/var/lib/tripe";
161 struct in_addr baddr
= { INADDR_ANY
};
170 T( trace_on(stderr
, 0); )
172 if ((p
= getenv("TRIPEDIR")) != 0)
176 static const struct option opts
[] = {
177 { "help", 0, 0, 'h' },
178 { "version", 0, 0, 'v' },
179 { "usage", 0, 0, 'u' },
181 { "daemon", 0, 0, 'D' },
182 { "uid", OPTF_ARGREQ
, 0, 'U' },
183 { "setuid", OPTF_ARGREQ
, 0, 'U' },
184 { "gid", OPTF_ARGREQ
, 0, 'G' },
185 { "setgid", OPTF_ARGREQ
, 0, 'G' },
186 { "bind-address", OPTF_ARGREQ
, 0, 'b' },
187 { "port", OPTF_ARGREQ
, 0, 'p' },
188 { "directory", OPTF_ARGREQ
, 0, 'd' },
189 { "priv-keyring", OPTF_ARGREQ
, 0, 'k' },
190 { "pub-keyring", OPTF_ARGREQ
, 0, 'K' },
191 { "tag", OPTF_ARGREQ
, 0, 't' },
192 { "admin-socket", OPTF_ARGREQ
, 0, 'a' },
194 { "trace", OPTF_ARGREQ
, 0, 'T' },
200 int i
= mdwopt(argc
, argv
, "hvu DU:G: b:p:d:k:K:t:a:" T("T:"),
221 unsigned long i
= strtoul(optarg
, &p
, 0);
225 pw
= getpwnam(optarg
);
227 die(EXIT_FAILURE
, "user `%s' not found", optarg
);
235 unsigned long i
= strtoul(optarg
, &p
, 0);
239 gr
= getgrnam(optarg
);
241 die(EXIT_FAILURE
, "group `%s' not found", optarg
);
246 struct hostent
*h
= gethostbyname(optarg
);
248 die(EXIT_FAILURE
, "unknown host name `%s'", optarg
);
249 memcpy(&baddr
, h
->h_addr
, sizeof(struct in_addr
));
253 unsigned long i
= strtoul(optarg
, &p
, 0);
255 struct servent
*s
= getservbyname(optarg
, "udp");
257 die(EXIT_FAILURE
, "unknown service name `%s'", optarg
);
258 i
= ntohs(s
->s_port
);
260 if (i
== 0 || i
>= 65536)
261 die(EXIT_FAILURE
, "bad port number %lu", i
);
281 tr_flags
= traceopt(tr_opts
, optarg
, tr_flags
, 0);
282 trace_level(tr_flags
);
291 if (optind
< argc
|| (f
& f_bogus
)) {
297 die(EXIT_FAILURE
, "can't set current directory to `%s': %s",
298 dir
, strerror(errno
));
303 rand_noisesrc(RAND_GLOBAL
, &noise_source
);
304 rand_seed(RAND_GLOBAL
, RMD160_HASHSZ
);
305 signal(SIGPIPE
, SIG_IGN
);
309 a_create(STDIN_FILENO
, STDOUT_FILENO
);
310 if (g
!= (gid_t
)-1) {
311 if (setgid(g
) || (getuid() == 0 && setgroups(1, &g
))) {
312 die(EXIT_FAILURE
, "couldn't setgid to %u: %s",
313 (unsigned)g
, strerror(errno
));
316 if (u
!= (uid_t
)-1) {
318 die(EXIT_FAILURE
, "couldn't setuid to %u: %s",
319 (unsigned)u
, strerror(errno
));
322 km_init(kr_priv
, kr_pub
, tag_priv
);
326 die(EXIT_FAILURE
, "couldn't become a daemon: %s", strerror(errno
));
332 tv
.tv_sec
= time(0) + T_INTERVAL
;
334 sel_addtimer(&sel
, &it
, &tv
, interval
, 0);
340 if (!sel_select(&sel
))
342 else if (errno
!= EINTR
&& errno
!= EAGAIN
) {
343 a_warn("select failed: %s", strerror(errno
));
346 a_warn("too many select errors: bailing out");
356 /*----- That's all, folks -------------------------------------------------*/