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