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