chiark / gitweb /
Allow admin clients to filter out async messages. Send notifications
[tripe] / tripe.c
1 /* -*-c-*-
2  *
3  * $Id$
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 /*----- Header files ------------------------------------------------------*/
30
31 #include "tripe.h"
32
33 /*----- Global variables --------------------------------------------------*/
34
35 sel_state sel;
36
37 /*----- Static variables --------------------------------------------------*/
38
39 static 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
54 void interval(struct timeval *tv, void *v)
55 {
56   struct timeval tvv;
57   T( trace(T_PEER, "peer: interval timer"); )
58   rand_seed(RAND_GLOBAL, MAXHASHSZ);
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
75 static void usage(FILE *fp)
76 {
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");
80 }
81
82 static void version(FILE *fp)
83 {
84   pquis(fp, "$, version " VERSION "\n");
85 }
86
87 static void help(FILE *fp)
88 {
89   version(fp);
90   fputc('\n', fp);
91   usage(fp);
92   fputs("\n\
93 Options:\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     --tunnel            Display IP tunnelling technique and exit.\n\
99 \n\
100 -D, --daemon            Run in the background.\n\
101 -d, --directory=DIR     Switch to directory DIR [default " CONFIGDIR "].\n\
102 -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
103 -p, --port=PORT         Select UDP port to listen to.\n\
104 -U, --setuid=USER       Set uid to USER after initialization.\n\
105 -G, --setgid=GROUP      Set gid to GROUP after initialization.\n\
106 -k, --priv-keyring=FILE Get private key from FILE.\n\
107 -K, --pub-keyring=FILE  Get public keys from FILE.\n\
108 -t, --tag=KEYTAG        Use private key labelled TAG.\n\
109 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
110 " T( "\
111 -T, --trace=OPTIONS     Turn on tracing options.\n\
112 " ) "\
113 ", fp);
114 }
115
116 int main(int argc, char *argv[])
117 {
118   const char *kr_priv = "keyring", *kr_pub = "keyring.pub";
119   const char *tag_priv = "tripe-dh";
120   const char *csock = SOCKETDIR "/tripesock";
121   const char *dir = CONFIGDIR;
122   const char *p;
123   unsigned port = 0;
124   struct in_addr baddr = { INADDR_ANY };
125   unsigned f = 0;
126   uid_t u = -1;
127   gid_t g = -1;
128
129 #define f_bogus 1u
130 #define f_daemon 2u
131
132   ego(argv[0]);
133   T( trace_on(stderr, 0); )
134
135   if ((p = getenv("TRIPEDIR")) != 0)
136     dir = p;
137
138   for (;;) {
139     static const struct option opts[] = {
140       { "help",         0,              0,      'h' },
141       { "version",      0,              0,      'v' },
142       { "usage",        0,              0,      'u' },
143
144       { "daemon",       0,              0,      'D' },
145       { "uid",          OPTF_ARGREQ,    0,      'U' },
146       { "setuid",       OPTF_ARGREQ,    0,      'U' },
147       { "gid",          OPTF_ARGREQ,    0,      'G' },
148       { "setgid",       OPTF_ARGREQ,    0,      'G' },
149       { "bind-address", OPTF_ARGREQ,    0,      'b' },
150       { "port",         OPTF_ARGREQ,    0,      'p' },
151       { "directory",    OPTF_ARGREQ,    0,      'd' },
152       { "priv-keyring", OPTF_ARGREQ,    0,      'k' },
153       { "pub-keyring",  OPTF_ARGREQ,    0,      'K' },
154       { "tag",          OPTF_ARGREQ,    0,      't' },
155       { "admin-socket", OPTF_ARGREQ,    0,      'a' },
156 #ifndef NTRACE
157       { "trace",        OPTF_ARGREQ,    0,      'T' },
158 #endif
159
160       { "tunnel",       0,              0,      '0' },
161       { 0,              0,              0,      0 }
162     };
163
164     int i = mdwopt(argc, argv, "hvuDU:G:b:p:d:k:K:t:a:" T("T:"),
165                    opts, 0, 0, 0);
166     if (i < 0)
167       break;
168     switch (i) {
169       case 'h':
170         help(stdout);
171         exit(0);
172       case 'v':
173         version(stdout);
174         exit(0);
175       case 'u':
176         usage(stdout);
177         exit(0);
178
179       case 'D':
180         f |= f_daemon;
181         break;
182       case 'U': {
183         struct passwd *pw;
184         char *p;
185         unsigned long i = strtoul(optarg, &p, 0);
186         if (!*p)
187           pw = getpwuid(i);
188         else
189           pw = getpwnam(optarg);
190         if (!pw)
191           die(EXIT_FAILURE, "user `%s' not found", optarg);
192         u = pw->pw_uid;
193         if (g == -1)
194           g = pw->pw_gid;
195       } break;
196       case 'G': {
197         struct group *gr;
198         char *p;
199         unsigned long i = strtoul(optarg, &p, 0);
200         if (!*p)
201           gr = getgrgid(i);
202         else
203           gr = getgrnam(optarg);
204         if (!gr)
205           die(EXIT_FAILURE, "group `%s' not found", optarg);
206         g = gr->gr_gid;
207       } break;
208
209       case 'b': {
210         struct hostent *h = gethostbyname(optarg);
211         if (!h)
212           die(EXIT_FAILURE, "unknown host name `%s'", optarg);
213         memcpy(&baddr, h->h_addr, sizeof(struct in_addr));
214       } break;
215       case 'p': {
216         char *p;
217         unsigned long i = strtoul(optarg, &p, 0);
218         if (*p) {
219           struct servent *s = getservbyname(optarg, "udp");
220           if (!s)
221             die(EXIT_FAILURE, "unknown service name `%s'", optarg);
222           i = ntohs(s->s_port);
223         }
224         if (i == 0 || i >= 65536)
225           die(EXIT_FAILURE, "bad port number %lu", i);
226         port = i;
227       } break;
228       case 'd':
229         dir = optarg;
230         break;
231       case 'k':
232         kr_priv = optarg;
233         break;
234       case 'K':
235         kr_pub = optarg;
236         break;
237       case 'a':
238         csock = optarg;
239         break;
240       case 't':
241         tag_priv = optarg;
242         break;
243 #ifndef NTRACE
244       case 'T':
245         tr_flags = traceopt(tr_opts, optarg, tr_flags, 0);
246         trace_level(tr_flags);
247         break;
248 #endif
249       case '0': {
250         static const char *tun[] = { "notdef", "unet", "bsd", "linux" };
251         puts(tun[TUN_TYPE]);
252         exit(0);
253       } break;
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, MAXHASHSZ);
274   signal(SIGPIPE, SIG_IGN);
275   tun_init();
276   p_init(baddr, port);
277   if (!(f & f_daemon)) {
278 #ifndef NTRACE
279     a_create(STDIN_FILENO, STDOUT_FILENO, AF_TRACE | AF_WARN);
280 #else
281     a_create(STDIN_FILENO, STDOUT_FILENO, AF_WARN);
282 #endif
283   }
284   if (g != (gid_t)-1) {
285     if (setgid(g) || (getuid() == 0 && setgroups(1, &g))) {
286       die(EXIT_FAILURE, "couldn't setgid to %u: %s",
287           (unsigned)g, strerror(errno));
288     }
289   }
290   if (u != (uid_t)-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) {
299     if (u_daemon())
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("SERVER select-error -- %s", strerror(errno));
318         selerr++;
319         if (selerr > 8) {
320           a_warn("ABORT repeated-select-errors");
321           abort();
322         }
323       }
324     }
325   }
326
327   return (0);
328 }
329
330 /*----- That's all, folks -------------------------------------------------*/