chiark / gitweb /
Don't include the tracing option in the help if not compiled in.
[tripe] / tripe.c
CommitLineData
410c8acf 1/* -*-c-*-
2 *
c5a2ea9b 3 * $Id: tripe.c,v 1.7 2002/01/13 17:14:05 mdw Exp $
410c8acf 4 *
5 * Main program
6 *
7 * (c) 2001 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Trivial IP Encryption (TrIPE).
13 *
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.
18 *
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.
23 *
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.
27 */
28
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: tripe.c,v $
c5a2ea9b 32 * Revision 1.7 2002/01/13 17:14:05 mdw
33 * Don't include the tracing option in the help if not compiled in.
34 *
2666d7dd 35 * Revision 1.6 2001/06/19 22:08:37 mdw
36 * Moved buffers to peer.c.
37 *
680bfcf4 38 * Revision 1.5 2001/02/16 21:43:12 mdw
39 * Provide a more helpful usage message.
40 *
37852359 41 * Revision 1.4 2001/02/16 21:41:31 mdw
42 * Add a new buffer.
43 *
efd6c9e3 44 * Revision 1.3 2001/02/04 17:10:40 mdw
45 * Remove a debugging @abort@ call.
46 *
e31c0b14 47 * Revision 1.2 2001/02/03 22:33:00 mdw
48 * Stuff more randomness into the pool in the interval timer.
49 *
410c8acf 50 * Revision 1.1 2001/02/03 20:26:37 mdw
51 * Initial checkin.
52 *
53 */
54
55/*----- Header files ------------------------------------------------------*/
56
57#include "tripe.h"
58
59/*----- Global variables --------------------------------------------------*/
60
61sel_state sel;
410c8acf 62
63/*----- Static variables --------------------------------------------------*/
64
65static sel_timer it;
66#define T_INTERVAL MIN(1)
67
68/*----- Main code ---------------------------------------------------------*/
69
70/* --- @interval@ --- *
71 *
72 * Arguments: @struct timeval *tv@ = time when called
73 * @void *v@ = boring pointer
74 *
75 * Returns: ---
76 *
77 * Use: Called periodically to do housekeeping tasks.
78 */
79
80void interval(struct timeval *tv, void *v)
81{
82 struct timeval tvv;
83 T( trace(T_PEER, "peer: interval timer"); )
37852359 84 rand_seed(RAND_GLOBAL, HASHSZ);
410c8acf 85 p_interval();
86 tvv = *tv;
87 tvv.tv_sec += T_INTERVAL;
88 sel_addtimer(&sel, &it, &tvv, interval, v);
89}
90
91/* --- @main@ --- *
92 *
93 * Arguments: @int argc@ = number of command line arguments
94 * @char *argv[]@ = vector of arguments
95 *
96 * Returns: Zero if OK, nonzero on error.
97 *
98 * Use: Main program. Provides a simple VPN.
99 */
100
101static void usage(FILE *fp)
102{
680bfcf4 103 pquis(fp, "Usage: $ [-D] [-p port] [-T trace-opts] [-d dir] [-a socket]\n\
104 [-k priv-keyring] [-K pub-keyring] [-t key-tag]\n");
410c8acf 105}
106
107static void version(FILE *fp)
108{
109 pquis(fp, "$, version " VERSION "\n");
110}
111
112static void help(FILE *fp)
113{
114 version(fp);
115 fputc('\n', fp);
116 usage(fp);
117 fputs("\n\
118Options:\n\
119\n\
120-h, --help Display this help text.\n\
121-v, --version Display version number.\n\
122-u, --usage Display pointless usage message.\n\
123\n\
124-D, --daemon Run in the background.\n\
125-d, --directory=DIR Switch to directory DIR (default $TRIPEDIR).\n\
126-p, --port=PORT Select UDP port to listen to.\n\
127-k, --priv-keyring=FILE Get private key from FILE.\n\
128-K, --pub-keyring=FILE Get public keys from FILE.\n\
129-t, --tag=KEYTAG Use private key labelled TAG.\n\
130-a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
c5a2ea9b 131" T( "\
410c8acf 132-T, --trace=OPTIONS Turn on tracing options.\n\
c5a2ea9b 133" ) "\
410c8acf 134", fp);
135}
136
137int main(int argc, char *argv[])
138{
139 const char *kr_priv = "keyring", *kr_pub = "keyring.pub";
140 const char *tag_priv = "tripe-dh";
141 const char *csock = "tripesock";
142 const char *dir = "/var/lib/tripe";
143 const char *p;
144 unsigned port = 0;
145 unsigned f = 0;
146 uid_t u = -1;
147 gid_t g = -1;
148
149#define f_bogus 1u
150#define f_daemon 2u
151
152 ego(argv[0]);
153 trace_on(stderr, 0);
154
155 if ((p = getenv("TRIPEDIR")) != 0)
156 dir = p;
157
158 for (;;) {
159 static const struct option opts[] = {
160 { "help", 0, 0, 'h' },
161 { "version", 0, 0, 'v' },
162 { "usage", 0, 0, 'u' },
163
164 { "daemon", 0, 0, 'D' },
165 { "uid", OPTF_ARGREQ, 0, 'U' },
166 { "setuid", OPTF_ARGREQ, 0, 'U' },
167 { "gid", OPTF_ARGREQ, 0, 'G' },
168 { "setgid", OPTF_ARGREQ, 0, 'G' },
169 { "port", OPTF_ARGREQ, 0, 'p' },
170 { "directory", OPTF_ARGREQ, 0, 'd' },
171 { "priv-keyring", OPTF_ARGREQ, 0, 'k' },
172 { "pub-keyring", OPTF_ARGREQ, 0, 'K' },
173 { "tag", OPTF_ARGREQ, 0, 't' },
174 { "admin-socket", OPTF_ARGREQ, 0, 'a' },
175#ifndef NTRACE
176 { "trace", OPTF_ARGREQ, 0, 'T' },
177#endif
178
179 { 0, 0, 0, 0 }
180 };
181
182 int i = mdwopt(argc, argv, "hvu DU:G: p:d:k:K:t:a:" T("T:"),
183 opts, 0, 0, 0);
184 if (i < 0)
185 break;
186 switch (i) {
187 case 'h':
188 help(stdout);
189 exit(0);
190 case 'v':
191 version(stdout);
192 exit(0);
193 case 'u':
194 usage(stdout);
195 exit(0);
196
197 case 'D':
198 f |= f_daemon;
199 break;
200 case 'U': {
201 char *p;
202 unsigned long i = strtoul(optarg, &p, 0);
203 if (!*p)
204 u = i;
205 else {
206 struct passwd *pw;
207 if ((pw = getpwnam(optarg)) == 0)
208 die(EXIT_FAILURE, "user name `%s' not found", optarg);
209 u = pw->pw_uid;
210 }
211 } break;
212 case 'G': {
213 char *p;
214 unsigned long i = strtoul(optarg, &p, 0);
215 if (!*p)
216 g = i;
217 else {
218 struct group *gr;
219 if ((gr = getgrnam(optarg)) == 0)
220 die(EXIT_FAILURE, "group name `%s' not found", optarg);
221 g = gr->gr_gid;
222 }
223 } break;
224
225 case 'p': {
226 char *p;
227 unsigned long i = strtoul(optarg, &p, 0);
228 if (*p) {
229 struct servent *s = getservbyname(optarg, "udp");
230 if (!s)
231 die(EXIT_FAILURE, "unknown service name `%s'", optarg);
232 i = ntohs(s->s_port);
233 }
234 if (i == 0 || i >= 65536)
235 die(EXIT_FAILURE, "bad port number %lu", i);
236 port = i;
237 } break;
238 case 'd':
239 dir = optarg;
240 break;
241 case 'k':
242 kr_priv = optarg;
243 break;
244 case 'K':
245 kr_pub = optarg;
246 break;
247 case 'a':
248 csock = optarg;
249 break;
250 case 't':
251 tag_priv = optarg;
252 break;
253#ifndef NTRACE
254 case 'T':
255 tr_flags = traceopt(tr_opts, optarg, tr_flags, 0);
256 trace_level(tr_flags);
257 break;
258#endif
259 default:
260 f |= f_bogus;
261 break;
262 }
263 }
264
265 if (optind < argc || (f & f_bogus)) {
266 usage(stderr);
267 exit(EXIT_FAILURE);
268 }
269
270 if (chdir(dir)) {
271 die(EXIT_FAILURE, "can't set current directory to `%s': %s",
272 dir, strerror(errno));
273 }
274
275 sel_init(&sel);
276 sig_init(&sel);
277 rand_noisesrc(RAND_GLOBAL, &noise_source);
278 rand_seed(RAND_GLOBAL, RMD160_HASHSZ);
279 signal(SIGPIPE, SIG_IGN);
280 tun_init();
281 p_init(port);
282 if (!(f & f_daemon))
283 a_create(STDIN_FILENO, STDOUT_FILENO);
284 if (g != -1) {
285 if (setgid(g)) {
286 die(EXIT_FAILURE, "couldn't setgid to %u: %s",
287 (unsigned)g, strerror(errno));
288 }
289 }
290 if (u != -1) {
291 if (setuid(u)) {
292 die(EXIT_FAILURE, "couldn't setuid to %u: %s",
293 (unsigned)u, strerror(errno));
294 }
295 }
296 km_init(kr_priv, kr_pub, tag_priv);
297 a_init(csock);
298 if (f & f_daemon) {
37852359 299 if (u_daemon())
410c8acf 300 die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno));
301 a_daemon();
302 }
303
304 {
305 struct timeval tv;
306 tv.tv_sec = time(0) + T_INTERVAL;
307 tv.tv_usec = 0;
308 sel_addtimer(&sel, &it, &tv, interval, 0);
309 }
310
311 {
312 int selerr = 0;
313 for (;;) {
314 if (!sel_select(&sel))
315 selerr = 0;
316 else if (errno != EINTR && errno != EAGAIN) {
317 a_warn("select failed: %s", strerror(errno));
410c8acf 318 selerr++;
319 if (selerr > 8) {
320 a_warn("too many select errors: bailing out");
321 a_quit();
322 }
323 }
324 }
325 }
326
327 return (0);
328}
329
330/*----- That's all, folks -------------------------------------------------*/