chiark / gitweb /
server/keymgmt.c: Track and find keys by their 32-bit IDs.
[tripe] / server / tripe.c
1 /* -*-c-*-
2  *
3  * Main program
4  *
5  * (c) 2001 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Trivial IP Encryption (TrIPE).
11  *
12  * TrIPE is free software: you can redistribute it and/or modify it under
13  * the terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 3 of the License, or (at your
15  * option) any later version.
16  *
17  * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20  * for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with TrIPE.  If not, see <https://www.gnu.org/licenses/>.
24  */
25
26 /*----- Header files ------------------------------------------------------*/
27
28 #include "tripe.h"
29
30 /*----- Global variables --------------------------------------------------*/
31
32 sel_state sel;
33
34 /*----- Static variables --------------------------------------------------*/
35
36 static sel_timer it;
37 #define T_INTERVAL MIN(1)
38
39 /*----- Main code ---------------------------------------------------------*/
40
41 /* --- @interval@ --- *
42  *
43  * Arguments:   @struct timeval *tv@ = time when called
44  *              @void *v@ = boring pointer
45  *
46  * Returns:     ---
47  *
48  * Use:         Called periodically to do housekeeping tasks.
49  */
50
51 static void interval(struct timeval *tv, void *v)
52 {
53   struct timeval tvv;
54   T( trace(T_PEER, "peer: interval timer"); )
55   rand_seed(RAND_GLOBAL, MAXHASHSZ);
56   p_interval();
57   tvv = *tv;
58   tvv.tv_sec += T_INTERVAL;
59   sel_addtimer(&sel, &it, &tvv, interval, v);
60 }
61
62 /* --- @main@ --- *
63  *
64  * Arguments:   @int argc@ = number of command line arguments
65  *              @char *argv[]@ = vector of arguments
66  *
67  * Returns:     Zero if OK, nonzero on error.
68  *
69  * Use:         Main program.  Provides a simple VPN.
70  */
71
72 static void usage(FILE *fp)
73 {
74   pquis(fp, "Usage: $ [-DF] [-d DIR] [-b ADDR] [-p PORT] [-n TUNNEL]\n\
75         [-U USER] [-G GROUP] [-a SOCKET] [-m MODE] [-T TRACE-OPTS]\n\
76         [-k PRIV-KEYRING] [-K PUB-KEYRING] [-t KEY-TAG]\n");
77 }
78
79 static void version(FILE *fp) { pquis(fp, "$, version " VERSION "\n"); }
80
81 static void help(FILE *fp)
82 {
83   version(fp);
84   fputc('\n', fp);
85   usage(fp);
86   fputs("\n\
87 Options:\n\
88 \n\
89 -h, --help              Display this help text.\n\
90 -v, --version           Display version number.\n\
91 -u, --usage             Display pointless usage message.\n\
92     --tunnels           Display IP tunnel drivers and exit.\n\
93 \n\
94 -D, --daemon            Run in the background.\n\
95 -F, --foreground        Quit when stdin reports end-of-file.\n\
96 -d, --directory=DIR     Switch to directory DIR [default " CONFIGDIR "].\n\
97 -b, --bind-address=ADDR Bind UDP socket to this IP ADDR.\n\
98 -p, --port=PORT         Select UDP port to listen to "
99         "[default " STR(TRIPE_PORT) "].\n\
100 -n, --tunnel=TUNNEL     Seelect default tunnel driver.\n\
101 -U, --setuid=USER       Set uid to USER after initialization.\n\
102 -G, --setgid=GROUP      Set gid to GROUP after initialization.\n\
103 -k, --priv-keyring=FILE Get private key from FILE.\n\
104 -K, --pub-keyring=FILE  Get public keys from FILE.\n\
105 -t, --tag=KEYTAG        Use private key labelled TAG.\n\
106 -a, --admin-socket=FILE Use FILE as the adminstration socket.\n\
107 -m, --admin-perms=MODE  Permissions to set on admin socket [default 600].\n\
108 " T( "\
109 -T, --trace=OPTIONS     Turn on tracing options.\n\
110 " ) "\
111 ", fp);
112 }
113
114 int main(int argc, char *argv[])
115 {
116   const char *kr_priv = "keyring", *kr_pub = "keyring.pub";
117   const char *tag_priv = 0;
118   const char *csock = SOCKETDIR "/tripesock";
119   int csockmode = 0600;
120   const char *dir = CONFIGDIR;
121   const char *p;
122   unsigned port = TRIPE_PORT;
123   struct in_addr baddr = { INADDR_ANY };
124   unsigned f = 0;
125   int i;
126   int selerr = 0;
127   unsigned af;
128   struct timeval tv;
129   uid_t u = -1;
130   gid_t g = -1;
131
132 #define f_bogus 1u
133 #define f_daemon 2u
134 #define f_foreground 4u
135
136   ego(argv[0]);
137   T( trace_on(stderr, 0); )
138
139   if ((p = getenv("TRIPEDIR")) != 0)
140     dir = p;
141   if ((p = getenv("TRIPESOCK")) != 0)
142     csock = p;
143   tun_default = tunnels[0];
144
145   for (;;) {
146     static const struct option opts[] = {
147       { "help",         0,              0,      'h' },
148       { "version",      0,              0,      'v' },
149       { "usage",        0,              0,      'u' },
150       { "tunnels",      0,              0,      '0' },
151
152       { "daemon",       0,              0,      'D' },
153       { "foreground",   0,              0,      'F' },
154       { "uid",          OPTF_ARGREQ,    0,      'U' },
155       { "setuid",       OPTF_ARGREQ,    0,      'U' },
156       { "gid",          OPTF_ARGREQ,    0,      'G' },
157       { "setgid",       OPTF_ARGREQ,    0,      'G' },
158       { "bind-address", OPTF_ARGREQ,    0,      'b' },
159       { "tunnel",       OPTF_ARGREQ,    0,      'n' },
160       { "port",         OPTF_ARGREQ,    0,      'p' },
161       { "directory",    OPTF_ARGREQ,    0,      'd' },
162       { "priv-keyring", OPTF_ARGREQ,    0,      'k' },
163       { "pub-keyring",  OPTF_ARGREQ,    0,      'K' },
164       { "tag",          OPTF_ARGREQ,    0,      't' },
165       { "admin-socket", OPTF_ARGREQ,    0,      'a' },
166       { "admin-perms",  OPTF_ARGREQ,    0,      'm' },
167 #ifndef NTRACE
168       { "trace",        OPTF_ARGREQ,    0,      'T' },
169 #endif
170
171       { 0,              0,              0,      0 }
172     };
173
174     i = mdwopt(argc, argv, "hvuDFU:G:b:n:p:d:k:K:t:a:m:" T("T:"),
175                opts, 0, 0, 0);
176     if (i < 0)
177       break;
178     switch (i) {
179       case 'h':
180         help(stdout);
181         exit(0);
182       case 'v':
183         version(stdout);
184         exit(0);
185       case 'u':
186         usage(stdout);
187         exit(0);
188
189       case 'D':
190         f |= f_daemon;
191         break;
192       case 'U':
193         u = u_getuser(optarg, &g);
194         break;
195       case 'G':
196         g = u_getgroup(optarg);
197         break;
198       case 'F':
199         f |= f_foreground;
200         break;
201
202       case 'b': {
203         struct hostent *h = gethostbyname(optarg);
204         if (!h)
205           die(EXIT_FAILURE, "unknown host name `%s'", optarg);
206         memcpy(&baddr, h->h_addr, sizeof(struct in_addr));
207       } break;
208       case 'p': {
209         char *p;
210         unsigned long i = strtoul(optarg, &p, 0);
211         if (*p) {
212           struct servent *s = getservbyname(optarg, "udp");
213           if (!s)
214             die(EXIT_FAILURE, "unknown service name `%s'", optarg);
215           i = ntohs(s->s_port);
216         }
217         if (i >= 65536)
218           die(EXIT_FAILURE, "bad port number %lu", i);
219         port = i;
220       } break;
221       case 'n': {
222         int i;
223         for (i = 0;; i++) {
224           if (!tunnels[i])
225             die(EXIT_FAILURE, "unknown tunnel `%s'", optarg);
226           if (mystrieq(optarg, tunnels[i]->name))
227             break;
228         }
229         tun_default = tunnels[i];
230       } break;
231       case 'd':
232         dir = optarg;
233         break;
234       case 'k':
235         kr_priv = optarg;
236         break;
237       case 'K':
238         kr_pub = optarg;
239         break;
240       case 'a':
241         csock = optarg;
242         break;
243       case 'm': {
244         char *p;
245         csockmode = strtol(optarg, &p, 8);
246         if (*p) die(EXIT_FAILURE, "bad permissions: `%s'", optarg);
247       } break;
248       case 't':
249         tag_priv = optarg;
250         break;
251 #ifndef NTRACE
252       case 'T':
253         tr_flags = traceopt(tr_opts, optarg, tr_flags, 0);
254         trace_level(tr_flags);
255         break;
256 #endif
257       case '0': {
258         int i;
259         for (i = 0; tunnels[i]; i++)
260           puts(tunnels[i]->name);
261         exit(0);
262       } break;
263       default:
264         f |= f_bogus;
265         break;
266     }
267   }
268
269   if (optind < argc || (f & f_bogus)) {
270     usage(stderr);
271     exit(EXIT_FAILURE);
272   }
273   if (!(~f & (f_daemon | f_foreground)))
274     die(EXIT_FAILURE, "foreground operation for a daemon is silly");
275
276   if (chdir(dir)) {
277     die(EXIT_FAILURE, "can't set current directory to `%s': %s",
278         dir, strerror(errno));
279   }
280
281   sel_init(&sel);
282   sig_init(&sel);
283   rand_noisesrc(RAND_GLOBAL, &noise_source);
284   rand_seed(RAND_GLOBAL, MAXHASHSZ);
285   signal(SIGPIPE, SIG_IGN);
286   for (i = 0; tunnels[i]; i++)
287     tunnels[i]->init();
288   p_init(baddr, port);
289   if (!(f & f_daemon)) {
290     af = AF_WARN;
291 #ifndef NTRACE
292     af |= AF_TRACE;
293 #endif
294     if (f & f_foreground)
295       af |= AF_FOREGROUND;
296     a_create(STDIN_FILENO, STDOUT_FILENO, af);
297   }
298   ps_split(f & f_daemon);
299   a_init(csock, u, g, csockmode);
300   u_setugid(u, g);
301   km_init(kr_priv, kr_pub, tag_priv);
302   if (f & f_daemon) {
303     if (daemonize())
304       die(EXIT_FAILURE, "couldn't become a daemon: %s", strerror(errno));
305     a_daemon();
306   }
307
308   tv.tv_sec = time(0) + T_INTERVAL;
309   tv.tv_usec = 0;
310   sel_addtimer(&sel, &it, &tv, interval, 0);
311
312   for (;;) {
313     a_preselect();
314     if (!sel_select(&sel))
315       selerr = 0;
316     else if (errno != EINTR && errno != EAGAIN) {
317       a_warn("SERVER", "select-error", "?ERRNO", A_END);
318       selerr++;
319       if (selerr > 8) {
320         a_warn("ABORT", "repeated-select-errors", A_END);
321         abort();
322       }
323     }
324   }
325
326   return (0);
327 }
328
329 /*----- That's all, folks -------------------------------------------------*/