1c1a9fa1 |
1 | /* |
75de0d4d |
2 | * userv service (or standalone program) for per-user IP subranges. |
3 | * |
4 | * When invoked appropriately, it creates a point-to-point network |
5 | * interface with specified parameters. It arranges for packets sent out |
6 | * via that interface by the kernel to appear on its own stdout in SLIP or |
7 | * CSLIP encoding, and packets injected into its own stdin to be given to |
8 | * the kernel as if received on that interface. Optionally, additional |
9 | * routes can be set up to arrange for traffic for other address ranges to |
10 | * be routed through the new interface. |
11 | * |
12 | * This is the service program, which is invoked as root from userv (or may |
13 | * be invoked firectly). |
14 | * |
15 | * Its arguments are supposed to be, in order, as follows: |
16 | * |
17 | * The first two arguments are usually supplied by the userv |
18 | * configuration. See the file `ipif/ipif' in the source tree, which |
19 | * is installed in /etc/userv/services.d/ipif by `make install': |
1c1a9fa1 |
20 | * |
1e963473 |
21 | * <config> |
75de0d4d |
22 | * |
23 | * Specifies address ranges and gids which own them. The default |
24 | * configuration supplies /etc/userv/ipif-networks, which is then read |
25 | * for a list of entries, one per line. |
26 | * |
27 | * -- |
28 | * Serves to separate the user-supplied and therefore untrusted |
29 | * arguments from the trusted first argument. |
30 | * |
31 | * The remaining arguments are supplied by the (untrusted) caller: |
32 | * |
1c1a9fa1 |
33 | * <local-addr>,<peer-addr>,<mtu>,<proto> |
75de0d4d |
34 | * |
1c1a9fa1 |
35 | * As for slattach. Supported protocols are slip, cslip, and |
75de0d4d |
36 | * adaptive. Alternatively, set to `debug' to print debugging info |
37 | * and exit. <local-addr> is address of the interface to be created |
38 | * on the local system; <peer-addr> is the address of the |
39 | * point-to-point peer. They must be actual addresses (not |
40 | * hostnames). |
41 | * |
1c1a9fa1 |
42 | * <prefix>/<mask>,<prefix>/<mask>,... |
75de0d4d |
43 | * |
44 | * List of additional routes to add for this interface. routes will |
45 | * be set up on the local system arranging for packets for those |
46 | * networks to be sent via the created interface. <prefix> must be an |
47 | * IPv4 address, and mask must be an integer (dotted-quad masks are |
48 | * not supported). If no additional routes are to be set up, use `-' |
49 | * or supply an empty argument. |
50 | * |
51 | * Each <config> item - whether a line file such as |
52 | * /etc/userv/ipif-networks, or supplied on the service program |
53 | * command line - is one of: |
54 | * |
55 | * /<config-file-name> |
56 | * ./<config-file-name> |
57 | * ../<config-file-name> |
58 | * |
59 | * Reads a file which contains lines which are each <config> |
60 | * items. |
61 | * |
62 | * <gid>,[=]<prefix>/<len>[,<junk>] |
63 | * |
64 | * Indicates that <gid> may allocate addresses in the relevant address |
65 | * range (<junk> is ignored). <gid> must be numeric. To specify a |
66 | * single host address, you must specify a mask of /32. If `=' is |
67 | * specified then the specific subrange is only allowed for the local |
68 | * endpoint address, but not for remote addresses. |
69 | * |
70 | * * |
71 | * Means that anything is to be permitted. This should not appear in |
72 | * /etc/userv/ipif-networks, as that would permit any user on the |
73 | * system to create any interfaces with any addresses and routes |
74 | * attached. It is provided so that root can usefully invoke the ipif |
75 | * service program directly (not via userv), without needing to set up |
76 | * permissions in /etc/userv/ipif-networks. |
77 | * |
78 | * #... |
79 | * |
80 | * Comment. Blank lines are also ignored. |
81 | * |
82 | * NB: Permission is granted if _any_ config entry matches the request. |
83 | * |
84 | * The service program should be run from userv with no-disconnect-hup. |
1c1a9fa1 |
85 | */ |
12ecfeab |
86 | /* |
45fa312e |
87 | * Copyright (C) 1999-2000 Ian Jackson |
12ecfeab |
88 | * |
89 | * This is free software; you can redistribute it and/or modify it |
90 | * under the terms of the GNU General Public License as published by |
91 | * the Free Software Foundation; either version 2 of the License, or |
92 | * (at your option) any later version. |
93 | * |
94 | * This program is distributed in the hope that it will be useful, but |
95 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
96 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
97 | * General Public License for more details. |
98 | * |
99 | * You should have received a copy of the GNU General Public License |
100 | * along with userv-utils; if not, write to the Free Software |
101 | * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
102 | * |
87a7ced4 |
103 | * $Id: service.c,v 1.14 2000/12/10 22:00:21 ian Exp $ |
12ecfeab |
104 | */ |
1c1a9fa1 |
105 | |
106 | #include <stdio.h> |
107 | #include <string.h> |
108 | #include <stdlib.h> |
109 | #include <assert.h> |
110 | #include <errno.h> |
1e963473 |
111 | #include <stdarg.h> |
112 | #include <ctype.h> |
113 | #include <limits.h> |
02b2392d |
114 | #include <signal.h> |
115 | #include <unistd.h> |
116 | |
117 | #include <sys/types.h> |
118 | #include <sys/wait.h> |
119 | #include <sys/stat.h> |
1c1a9fa1 |
120 | |
1e963473 |
121 | #define NARGS 4 |
1c1a9fa1 |
122 | #define MAXEXROUTES 5 |
a48a8f0d |
123 | #define ATXTLEN 16 |
1c1a9fa1 |
124 | |
1e963473 |
125 | static const unsigned long gidmaxval= (unsigned long)((gid_t)-2); |
126 | static const char *const protos_ok[]= { "slip", "cslip", "adaptive", 0 }; |
02b2392d |
127 | static const int signals[]= { SIGHUP, SIGINT, SIGTERM, 0 }; |
1e963473 |
128 | |
129 | static const char *configstr, *proto; |
1c1a9fa1 |
130 | static unsigned long localaddr, peeraddr, mtu; |
1e963473 |
131 | static int localallow, peerallow, allallow; |
1c1a9fa1 |
132 | static int nexroutes; |
133 | static struct exroute { |
134 | unsigned long prefix, mask; |
1e963473 |
135 | int allow; |
1c1a9fa1 |
136 | char prefixtxt[ATXTLEN], masktxt[ATXTLEN]; |
137 | } exroutes[MAXEXROUTES]; |
138 | |
1e963473 |
139 | static char localtxt[ATXTLEN]; |
140 | static char peertxt[ATXTLEN]; |
141 | |
142 | static struct pplace { |
143 | struct pplace *parent; |
144 | const char *filename; |
145 | int lineno; |
146 | } *cpplace; |
147 | |
02b2392d |
148 | |
6d360a53 |
149 | static int slpipe[2], ptmaster, undoslattach; |
02b2392d |
150 | static const char *ifname; |
151 | static const char *ptyname; |
152 | |
153 | #define NPIDS 4 |
154 | |
155 | static union { |
156 | struct { pid_t sl, cout, cin, task; } byname; |
157 | pid_t bynumber[NPIDS]; |
158 | } pids; |
159 | sigset_t emptyset, fullset; |
160 | |
161 | |
6d360a53 |
162 | static int cleantask(void) { |
163 | pid_t pid; |
164 | |
165 | pid= fork(); |
166 | if (!pid) return 1; |
167 | if (pid == (pid_t)-1) |
168 | perror("userv-ipif: fork for undo slattach failed - cannot clean up properly"); |
169 | return 0; |
170 | } |
171 | |
02b2392d |
172 | static void terminate(int estatus) { |
173 | int i, status; |
174 | pid_t pid; |
175 | |
176 | for (i=0; i<NPIDS; i++) |
177 | if (pids.bynumber[i]) kill(pids.bynumber[i], SIGTERM); |
6d360a53 |
178 | |
179 | if (undoslattach) { |
180 | if (cleantask()) { |
181 | execlp("slattach", "slattach", "-p", "tty", ptyname, (char*)0); |
182 | perror("userv-ipif: exec slattach for undo slattach failed"); |
183 | exit(-1); |
184 | } |
185 | if (ifname && cleantask()) { |
186 | execlp("ifconfig", "ifconfig", ifname, "down", (char*)0); |
187 | perror("userv-ipif: exec ifconfig for undo ifconfig failed"); |
188 | exit(-1); |
189 | } |
190 | } |
191 | |
02b2392d |
192 | for (;;) { |
193 | pid= waitpid(-1,&status,0); |
194 | if (pid == (pid_t)-1) break; |
195 | } |
196 | exit(estatus); |
197 | } |
198 | |
199 | |
1e963473 |
200 | static void fatal(const char *fmt, ...) |
201 | __attribute__((format(printf,1,2))); |
202 | static void fatal(const char *fmt, ...) { |
203 | va_list al; |
204 | va_start(al,fmt); |
1c1a9fa1 |
205 | |
1e963473 |
206 | fputs("userv-ipif service: fatal error: ",stderr); |
207 | vfprintf(stderr, fmt, al); |
208 | putc('\n',stderr); |
02b2392d |
209 | terminate(8); |
1c1a9fa1 |
210 | } |
211 | |
1e963473 |
212 | static void sysfatal(const char *fmt, ...) |
213 | __attribute__((format(printf,1,2))); |
214 | static void sysfatal(const char *fmt, ...) { |
215 | va_list al; |
216 | int e; |
217 | |
218 | e= errno; |
219 | va_start(al,fmt); |
220 | |
6d360a53 |
221 | fputs("userv-ipif service: fatal system error: ",stderr); |
1e963473 |
222 | vfprintf(stderr, fmt, al); |
6d360a53 |
223 | fprintf(stderr,": %s\n", strerror(e)); |
02b2392d |
224 | terminate(12); |
1c1a9fa1 |
225 | } |
1e963473 |
226 | |
227 | |
228 | static void badusage(const char *fmt, ...) |
229 | __attribute__((format(printf,1,2))); |
230 | static void badusage(const char *fmt, ...) { |
231 | va_list al; |
232 | struct pplace *cpp; |
1c1a9fa1 |
233 | |
1e963473 |
234 | if (cpplace) { |
235 | fprintf(stderr, |
236 | "userv-ipif service: %s:%d: ", |
237 | cpplace->filename, cpplace->lineno); |
238 | } else { |
239 | fputs("userv-ipif service: invalid usage: ",stderr); |
240 | } |
241 | va_start(al,fmt); |
242 | vfprintf(stderr, fmt, al); |
243 | putc('\n',stderr); |
244 | |
245 | if (cpplace) { |
246 | for (cpp=cpplace->parent; cpp; cpp=cpp->parent) { |
247 | fprintf(stderr, |
248 | "userv-ipif service: %s:%d: ... in file included from here\n", |
249 | cpp->filename, cpp->lineno); |
250 | } |
251 | } |
02b2392d |
252 | terminate(16); |
1c1a9fa1 |
253 | } |
254 | |
255 | static char *ip2txt(unsigned long addr, char *buf) { |
256 | sprintf(buf, "%lu.%lu.%lu.%lu", |
257 | (addr>>24) & 0x0ff, |
258 | (addr>>16) & 0x0ff, |
259 | (addr>>8) & 0x0ff, |
260 | (addr) & 0x0ff); |
261 | return buf; |
262 | } |
263 | |
264 | static unsigned long eat_number(const char **argp, const char *what, |
265 | unsigned long min, unsigned long max, |
266 | const char *endchars, int *endchar_r) { |
267 | /* If !endchars then the endchar must be a nul, otherwise it may be |
268 | * a nul (resulting in *argp set to 0) or something else (*argp set |
269 | * to point to after delim, *endchar_r set to delim). |
270 | * *endchar_r may be 0. |
271 | */ |
272 | unsigned long rv; |
273 | char *ep; |
274 | int endchar; |
275 | |
baba1099 |
276 | if (!*argp) { badusage("missing number %s",what); } |
1c1a9fa1 |
277 | rv= strtoul(*argp,&ep,0); |
278 | if ((endchar= *ep)) { |
baba1099 |
279 | if (!endchars) badusage("junk after number %s",what); |
1e963473 |
280 | if (!strchr(endchars,endchar)) |
281 | badusage("invalid character or delimiter `%c' in or after number, %s:" |
baba1099 |
282 | " expected %s (or none?)", endchar,what,endchars); |
1c1a9fa1 |
283 | *argp= ep+1; |
284 | } else { |
285 | *argp= 0; |
286 | } |
287 | if (endchar_r) *endchar_r= endchar; |
1e963473 |
288 | if (rv < min || rv > max) badusage("number %s value %lu out of range %lu..%lu", |
289 | what, rv, min, max); |
1c1a9fa1 |
290 | return rv; |
291 | } |
292 | |
1c1a9fa1 |
293 | static void addrnet_mustdiffer(const char *w1, unsigned long p1, unsigned long m1, |
294 | const char *w2, unsigned long p2, unsigned long m2) { |
295 | unsigned long mask; |
296 | |
297 | mask= m1&m2; |
298 | if ((p1 & mask) != (p2 & mask)) return; |
1e963473 |
299 | badusage("%s %08lx/%08lx overlaps/clashes with %s %08lx/%08lx", |
300 | w1,p1,m1, w2,p2,m2); |
1c1a9fa1 |
301 | } |
302 | |
303 | static unsigned long eat_addr(const char **argp, const char *what, |
1c1a9fa1 |
304 | const char *endchars, int *endchar_r) { |
305 | char whatbuf[100]; |
306 | unsigned long rv; |
307 | int i; |
308 | |
1c1a9fa1 |
309 | for (rv=0, i=0; |
310 | i<4; |
311 | i++) { |
312 | rv <<= 8; |
313 | sprintf(whatbuf,"%s byte #%d",what,i); |
314 | rv |= eat_number(argp,whatbuf, 0,255, i<3 ? "." : endchars, endchar_r); |
315 | } |
316 | |
1c1a9fa1 |
317 | return rv; |
318 | } |
319 | |
320 | static void eat_prefixmask(const char **argp, const char *what, |
1c1a9fa1 |
321 | const char *endchars, int *endchar_r, |
322 | unsigned long *prefix_r, unsigned long *mask_r, int *len_r) { |
323 | /* mask_r and len_r may be 0 */ |
324 | char whatbuf[100]; |
325 | int len; |
326 | unsigned long prefix, mask; |
327 | |
1e963473 |
328 | prefix= eat_addr(argp,what, "/",0); |
1c1a9fa1 |
329 | sprintf(whatbuf,"%s length",what); |
330 | len= eat_number(argp,whatbuf, 0,32, endchars,endchar_r); |
331 | |
87a7ced4 |
332 | mask= len ? (~0UL << (32-len)) : 0UL; |
baba1099 |
333 | if (prefix & ~mask) badusage("%s prefix %08lx not fully contained in mask %08lx", |
1e963473 |
334 | what,prefix,mask); |
1c1a9fa1 |
335 | *prefix_r= prefix; |
336 | if (mask_r) *mask_r= mask; |
337 | if (len_r) *len_r= len; |
338 | } |
1e963473 |
339 | |
340 | static int addrnet_isin(unsigned long prefix, unsigned long mask, |
341 | unsigned long mprefix, unsigned long mmask) { |
342 | return !(~mask & mmask) && (prefix & mmask) == mprefix; |
343 | } |
1c1a9fa1 |
344 | |
1c1a9fa1 |
345 | |
0fe65164 |
346 | static void permit(unsigned long pprefix, unsigned long pmask, int localonly) { |
1e963473 |
347 | int i, any; |
1c1a9fa1 |
348 | |
1e963473 |
349 | assert(!(pprefix & ~pmask)); |
2c310400 |
350 | any= 0; |
1c1a9fa1 |
351 | |
0fe65164 |
352 | if (!proto) fputs(localonly ? "permits-l" : "permits",stdout); |
1e963473 |
353 | if (addrnet_isin(localaddr,~0UL, pprefix,pmask)) { |
354 | if (!proto) fputs(" local-addr",stdout); |
355 | any= localallow= 1; |
356 | } |
0fe65164 |
357 | if (!localonly) { |
358 | if (addrnet_isin(peeraddr,~0UL, pprefix,pmask)) { |
359 | if (!proto) fputs(" peer-addr",stdout); |
360 | any= peerallow= 1; |
361 | } |
362 | for (i=0; i<nexroutes; i++) { |
363 | if (addrnet_isin(exroutes[i].prefix,exroutes[i].mask, pprefix,pmask)) { |
364 | if (!proto) printf(" route#%d",i); |
365 | any= exroutes[i].allow= 1; |
366 | } |
1e963473 |
367 | } |
368 | } |
369 | if (!proto) { |
a48a8f0d |
370 | if (!any) fputs(" nothing",stdout); |
1e963473 |
371 | putchar('\n'); |
372 | } |
373 | } |
1c1a9fa1 |
374 | |
1e963473 |
375 | static void pconfig(const char *configstr, int truncated); |
376 | |
377 | static void pfile(const char *filename) { |
378 | FILE *file; |
379 | char buf[PATH_MAX]; |
380 | int l, truncated, c; |
381 | struct pplace npp, *cpp; |
382 | |
383 | for (cpp=cpplace; cpp; cpp=cpp->parent) { |
384 | if (!strcmp(cpp->filename,filename)) |
385 | badusage("recursive configuration file `%s'",filename); |
386 | } |
387 | |
388 | file= fopen(filename,"r"); |
389 | if (!file) |
390 | badusage("cannot open configuration file `%s': %s", filename, strerror(errno)); |
391 | |
392 | if (!proto) printf("config file `%s':\n",filename); |
393 | |
394 | npp.parent= cpplace; |
395 | npp.filename= filename; |
396 | npp.lineno= 0; |
397 | cpplace= &npp; |
398 | |
399 | while (fgets(buf, sizeof(buf), file)) { |
400 | npp.lineno++; |
401 | l= strlen(buf); |
402 | if (!l) continue; |
403 | |
404 | truncated= (buf[l-1] != '\n'); |
405 | while (l>0 && isspace((unsigned char) buf[l-1])) l--; |
406 | if (!l) continue; |
407 | buf[l]= 0; |
408 | |
409 | if (truncated) { |
410 | while ((c= getc(file)) != EOF && c != '\n'); |
411 | if (c == EOF) break; |
412 | } |
413 | |
414 | pconfig(buf,truncated); |
415 | } |
416 | if (ferror(file)) |
417 | badusage("failed while reading configuration file: %s", strerror(errno)); |
418 | |
419 | cpplace= npp.parent; |
420 | } |
421 | |
422 | static void pconfig(const char *configstr, int truncated) { |
423 | unsigned long fgid, tgid, pprefix, pmask; |
0fe65164 |
424 | int plen, localonly; |
1e963473 |
425 | char ptxt[ATXTLEN]; |
426 | const char *gidlist; |
427 | |
428 | switch (configstr[0]) { |
429 | case '*': |
430 | if (strcmp(configstr,"*")) badusage("`*' directive must be only thing on line"); |
0fe65164 |
431 | permit(0UL,0UL,0); |
1e963473 |
432 | return; |
433 | |
434 | case '#': |
435 | return; |
436 | |
437 | case '/': case '.': |
438 | if (truncated) badusage("filename too long (`%.100s...')",configstr); |
439 | pfile(configstr); |
440 | return; |
441 | |
442 | default: |
443 | if (!isdigit((unsigned char)configstr[0])) |
444 | badusage("unknown configuration directive"); |
445 | |
446 | fgid= eat_number(&configstr,"gid", 0,gidmaxval, ",",0); |
0fe65164 |
447 | if (configstr[0] == '=') { |
448 | localonly= 1; |
449 | configstr++; |
450 | } else { |
451 | localonly= 0; |
452 | } |
1e963473 |
453 | eat_prefixmask(&configstr,"permitted-prefix", ",",0, &pprefix,&pmask,&plen); |
454 | if (!configstr && truncated) badusage("gid,prefix/len,... spec too long"); |
455 | |
456 | if (!proto) printf(" %5lu,%s/%d: ", fgid, ip2txt(pprefix,ptxt), plen); |
457 | |
458 | gidlist= getenv("USERV_GID"); |
459 | if (!gidlist) fatal("USERV_GID not set"); |
1c1a9fa1 |
460 | for (;;) { |
1e963473 |
461 | if (!gidlist) { |
462 | if (!proto) printf("no matching gid\n"); |
463 | return; |
1c1a9fa1 |
464 | } |
1e963473 |
465 | tgid= eat_number(&gidlist,"userv-gid", 0,gidmaxval, " ",0); |
466 | if (tgid == fgid) break; |
1c1a9fa1 |
467 | } |
0fe65164 |
468 | permit(pprefix,pmask,localonly); |
1e963473 |
469 | return; |
1c1a9fa1 |
470 | } |
1e963473 |
471 | } |
472 | |
473 | static void checkallow(int allow, const char *what, |
474 | const char *prefixtxt, const char *masktxt) { |
475 | if (allow) return; |
476 | fprintf(stderr,"userv-ipif service: access denied for %s, %s/%s\n", |
477 | what, prefixtxt, masktxt); |
478 | allallow= 0; |
479 | } |
480 | |
481 | static void parseargs(int argc, const char *const *argv) { |
482 | unsigned long routeaddr, routemask; |
483 | const char *carg; |
484 | const char *const *cprotop; |
485 | int i; |
486 | char erwhatbuf[100], erwhatbuf2[100]; |
487 | |
488 | if (argc < NARGS+1) { badusage("too few arguments"); } |
489 | if (argc > NARGS+1) { badusage("too many arguments"); } |
490 | |
491 | configstr= *++argv; |
1c1a9fa1 |
492 | |
493 | carg= *++argv; |
1e963473 |
494 | if (strcmp(carg,"--")) badusage("separator argument `--' not found, got `%s'",carg); |
1c1a9fa1 |
495 | |
1e963473 |
496 | carg= *++argv; |
497 | localaddr= eat_addr(&carg,"local-addr", ",",0); |
498 | peeraddr= eat_addr(&carg,"peer-addr", ",",0); |
1c1a9fa1 |
499 | mtu= eat_number(&carg,"mtu", 576,65536, ",",0); |
1e963473 |
500 | localallow= peerallow= 0; |
1c1a9fa1 |
501 | |
502 | if (!strcmp(carg,"debug")) { |
503 | proto= 0; |
504 | } else { |
505 | for (cprotop= protos_ok; |
506 | (proto= *cprotop) && strcmp(proto,carg); |
507 | cprotop++); |
508 | if (!proto) fatal("invalid protocol"); |
509 | } |
510 | |
511 | addrnet_mustdiffer("local-addr",localaddr,~0UL, "peer-addr",peeraddr,~0UL); |
512 | |
513 | carg= *++argv; |
2ed30784 |
514 | if (strcmp(carg,"-")) { |
515 | for (nexroutes=0; |
516 | carg && *carg; |
517 | nexroutes++) { |
518 | if (nexroutes == MAXEXROUTES) |
519 | fatal("too many extra routes (only %d allowed)",MAXEXROUTES); |
520 | sprintf(erwhatbuf,"route#%d",nexroutes); |
1c1a9fa1 |
521 | |
2ed30784 |
522 | eat_prefixmask(&carg,erwhatbuf, ",",0, &routeaddr,&routemask,0); |
523 | if (routemask == ~0UL) { |
524 | addrnet_mustdiffer(erwhatbuf,routeaddr,routemask, "local-addr",localaddr,~0UL); |
525 | addrnet_mustdiffer(erwhatbuf,routeaddr,routemask, "peer-addr",peeraddr,~0UL); |
526 | } |
527 | for (i=0; i<nexroutes; i++) { |
528 | sprintf(erwhatbuf2,"route#%d",i); |
529 | addrnet_mustdiffer(erwhatbuf,routeaddr,routemask, |
530 | erwhatbuf2,exroutes[i].prefix,exroutes[i].mask); |
531 | } |
532 | exroutes[nexroutes].prefix= routeaddr; |
533 | exroutes[nexroutes].mask= routemask; |
534 | exroutes[nexroutes].allow= 0; |
535 | ip2txt(routeaddr,exroutes[nexroutes].prefixtxt); |
536 | ip2txt(routemask,exroutes[nexroutes].masktxt); |
1c1a9fa1 |
537 | } |
1c1a9fa1 |
538 | } |
2ed30784 |
539 | |
1c1a9fa1 |
540 | ip2txt(localaddr,localtxt); |
541 | ip2txt(peeraddr,peertxt); |
1e963473 |
542 | } |
543 | |
544 | static void checkpermit(void) { |
545 | int i; |
546 | char erwhatbuf[100]; |
1c1a9fa1 |
547 | |
1e963473 |
548 | allallow= 1; |
549 | checkallow(localallow,"local-addr", localtxt,"32"); |
550 | checkallow(peerallow,"peer-addr", peertxt,"32"); |
551 | for (i=0; i<nexroutes; i++) { |
552 | sprintf(erwhatbuf, "route#%d", i); |
553 | checkallow(exroutes[i].allow, erwhatbuf, exroutes[i].prefixtxt, exroutes[i].masktxt); |
554 | } |
555 | if (!allallow) fatal("access denied"); |
556 | } |
557 | |
558 | static void dumpdebug(void) __attribute__((noreturn)); |
559 | static void dumpdebug(void) { |
560 | int i; |
561 | char erwhatbuf[100]; |
562 | |
563 | printf("protocol: debug\n" |
564 | "local: %08lx == %s\n" |
565 | "peer: %08lx == %s\n" |
566 | "mtu: %ld\n" |
567 | "routes: %d\n", |
568 | localaddr, localtxt, |
569 | peeraddr, peertxt, |
570 | mtu, |
571 | nexroutes); |
572 | for (i=0; i<nexroutes; i++) { |
573 | sprintf(erwhatbuf, "route#%d:", i); |
574 | printf("%-9s %08lx/%08lx == %s/%s\n", |
575 | erwhatbuf, |
576 | exroutes[i].prefix, exroutes[i].mask, |
577 | exroutes[i].prefixtxt, exroutes[i].masktxt); |
1c1a9fa1 |
578 | } |
1e963473 |
579 | if (ferror(stdout) || fclose(stdout)) sysfatal("flush stdout"); |
580 | exit(0); |
581 | } |
582 | |
6d360a53 |
583 | |
584 | static void setsigmask(const sigset_t *ss) { |
585 | int r; |
586 | |
587 | r= sigprocmask(SIG_SETMASK, ss, 0); |
588 | if (r) sysfatal("[un]block signals"); |
589 | } |
590 | |
02b2392d |
591 | static void setsignals(void (*handler)(int), struct sigaction *sa, int chldflags) { |
592 | const int *signalp; |
593 | int r, sig; |
594 | |
595 | sa->sa_handler= handler; |
596 | sa->sa_flags= 0; |
597 | for (signalp=signals; (sig=*signalp); signalp++) { |
598 | r= sigaction(sig, sa, 0); if (r) sysfatal("uncatch signal"); |
599 | } |
600 | sa->sa_flags= chldflags; |
601 | r= sigaction(SIGCHLD, sa, 0); if (r) sysfatal("uncatch children"); |
602 | } |
603 | |
02b2392d |
604 | static void infork(void) { |
605 | struct sigaction sa; |
606 | |
607 | memset(&pids,0,sizeof(pids)); |
608 | sigemptyset(&sa.sa_mask); |
609 | setsignals(SIG_DFL,&sa,0); |
610 | setsigmask(&emptyset); |
6d360a53 |
611 | undoslattach= 0; |
02b2392d |
612 | } |
613 | |
614 | static pid_t makesubproc(void (*entry)(void)) { |
615 | pid_t pid; |
616 | |
617 | pid= fork(); if (pid == (pid_t)-1) sysfatal("fork for subprocess"); |
618 | if (pid) return pid; |
619 | |
620 | infork(); |
621 | entry(); |
622 | abort(); |
623 | } |
624 | |
625 | static int task(void) { |
626 | pid_t pid; |
627 | |
628 | pid= fork(); |
6d360a53 |
629 | if (pid == (pid_t)-1) sysfatal("fork for task"); |
630 | if (!pid) { infork(); return 1; } |
02b2392d |
631 | |
632 | pids.byname.task= pid; |
633 | while (pids.byname.task) sigsuspend(&emptyset); |
634 | return 0; |
635 | } |
636 | |
6d360a53 |
637 | static void mdup2(int fd1, int fd2, const char *what) { |
638 | int r; |
639 | |
640 | for (;;) { |
641 | r= dup2(fd1,fd2); if (r==fd2) return; |
642 | if (r!=-1) fatal("dup2 in %s gave wrong answer %d instead of %d",what,r,fd2); |
643 | if (errno != EINTR) sysfatal("dup2 failed in %s",what); |
644 | } |
645 | } |
646 | |
02b2392d |
647 | static void sl_entry(void) { |
6d360a53 |
648 | mdup2(slpipe[1],1,"slattach child"); |
02b2392d |
649 | execlp("slattach", "slattach", "-v", "-L", "-p",proto, ptyname, (char*)0); |
650 | sysfatal("cannot exec slattach"); |
651 | } |
652 | |
653 | static void cin_entry(void) { |
6d360a53 |
654 | mdup2(ptmaster,1,"cat input child"); |
02b2392d |
655 | execlp("cat", "cat", (char*)0); |
656 | sysfatal("cannot exec cat input"); |
657 | } |
658 | |
659 | static void cout_entry(void) { |
6d360a53 |
660 | mdup2(ptmaster,0,"cat output child"); |
02b2392d |
661 | execlp("cat", "cat", (char*)0); |
662 | sysfatal("cannot exec cat output"); |
663 | } |
664 | |
665 | static void sighandler(int signum) { |
666 | pid_t pid; |
667 | int estatus, status; |
668 | const char *taskfail; |
669 | |
670 | estatus= 4; |
671 | |
672 | if (signum == SIGCHLD) { |
673 | for (;;) { |
674 | pid= waitpid(-1,&status,WNOHANG); |
675 | if (!pid || pid == (pid_t)-1) return; |
676 | |
677 | if (pid == pids.byname.task) { |
678 | pids.byname.task= 0; |
679 | if (!status) return; |
680 | taskfail= "task"; |
681 | } else if (pid == pids.byname.cin) { |
682 | pids.byname.cin= 0; |
683 | if (status) { |
684 | taskfail= "input cat"; |
685 | } else { |
686 | taskfail= 0; |
687 | estatus= 0; |
688 | } |
689 | } else if (pid == pids.byname.cout) { |
690 | pids.byname.cout= 0; |
691 | taskfail= "output cat"; |
692 | } else if (pid == pids.byname.sl) { |
693 | pids.byname.sl= 0; |
694 | taskfail= "slattach"; |
695 | } else { |
696 | continue; |
697 | } |
6d360a53 |
698 | break; |
02b2392d |
699 | } |
700 | if (taskfail) { |
6d360a53 |
701 | if (WIFEXITED(status)) { |
702 | fprintf(stderr, |
703 | "userv-ipif service: %s unexpectedly exited with exit status %d\n", |
704 | taskfail, WEXITSTATUS(status)); |
705 | } else if (WIFSIGNALED(status)) { |
706 | fprintf(stderr, |
707 | "userv-ipif service: %s unexpectedly killed by signal %s%s\n", |
708 | taskfail, strsignal(WTERMSIG(status)), |
709 | WCOREDUMP(status) ? " (core dumped)" : ""); |
710 | } else { |
711 | fprintf(stderr, "userv-ipif service: %s unexpectedly terminated" |
712 | " with unknown status code %d\n", taskfail, status); |
713 | } |
02b2392d |
714 | } |
715 | } else { |
716 | fprintf(stderr, |
717 | "userv-ipif service: received signal %d, terminating\n", |
718 | signum); |
719 | } |
720 | |
721 | terminate(estatus); |
722 | } |
723 | |
724 | static void startup(void) { |
725 | int r; |
726 | struct sigaction sa; |
727 | |
728 | sigfillset(&fullset); |
729 | sigemptyset(&emptyset); |
730 | |
731 | ptmaster= getpt(); if (ptmaster==-1) sysfatal("allocate pty master"); |
732 | r= grantpt(ptmaster); if (r) sysfatal("grab/grant pty slave"); |
733 | ptyname= ptsname(ptmaster); if (!ptyname) sysfatal("get pty slave name"); |
734 | r= chmod(ptyname,0600); if (r) sysfatal("chmod pty slave"); |
6d360a53 |
735 | r= unlockpt(ptmaster); if (r) sysfatal("unlock pty"); |
02b2392d |
736 | |
737 | sigfillset(&sa.sa_mask); |
738 | setsignals(sighandler,&sa,SA_NOCLDSTOP); |
739 | setsigmask(&fullset); |
740 | } |
741 | |
742 | static void startslattach(void) { |
6d360a53 |
743 | static char ifnbuf[200]; |
744 | |
02b2392d |
745 | FILE *piper; |
02b2392d |
746 | int r, l, k; |
747 | |
748 | r= pipe(slpipe); if (r) sysfatal("create pipe"); |
749 | piper= fdopen(slpipe[0],"r"); if (!piper) sysfatal("fdopen pipe"); |
750 | |
6d360a53 |
751 | undoslattach= 1; |
02b2392d |
752 | pids.byname.sl= makesubproc(sl_entry); |
753 | |
754 | close(slpipe[1]); |
755 | setsigmask(&emptyset); |
756 | if (!fgets(ifnbuf,sizeof(ifnbuf),piper)) { |
757 | if (ferror(piper)) sysfatal("cannot read ifname from slattach"); |
758 | else fatal("cannot read ifname from slattach"); |
759 | } |
760 | setsigmask(&fullset); |
761 | l= strlen(ifnbuf); |
2c310400 |
762 | if (l<=0 || ifnbuf[l-1] != '\n') fatal("slattach gave strange output `%s'",ifnbuf); |
02b2392d |
763 | ifnbuf[l-1]= 0; |
764 | for (k=l; k>0 && ifnbuf[k-1]!=' '; k--); |
765 | ifname= ifnbuf+k; |
766 | } |
767 | |
768 | static void netconfigure(void) { |
769 | char mtutxt[100]; |
770 | int i; |
771 | |
772 | if (task()) { |
773 | sprintf(mtutxt,"%lu",mtu); |
774 | |
775 | execlp("ifconfig", "ifconfig", ifname, localtxt, |
776 | "netmask","255.255.255.255", "-broadcast", "pointopoint",peertxt, |
777 | "mtu",mtutxt, "up", (char*)0); |
778 | sysfatal("cannot exec ifconfig"); |
779 | } |
780 | |
02b2392d |
781 | for (i=0; i<nexroutes; i++) { |
782 | if (task()) { |
6d360a53 |
783 | execlp("route","route", "add", "-net",exroutes[i].prefixtxt, |
02b2392d |
784 | "netmask",exroutes[i].masktxt, |
785 | "gw",peertxt, "dev",ifname, (char*)0); |
786 | sysfatal("cannot exec route (for route)"); |
787 | } |
788 | } |
789 | } |
790 | |
791 | static void copydata(void) __attribute__((noreturn)); |
5f1c67ff |
792 | static void copydata(void) { |
793 | int r; |
794 | |
02b2392d |
795 | pids.byname.cin= makesubproc(cin_entry); |
5f1c67ff |
796 | for (;;) { |
797 | r= write(1, "\300", 1); if (r==1) break; |
798 | assert(r==-1); if (errno != EINTR) sysfatal("send initial delim to confirm"); |
799 | } |
02b2392d |
800 | pids.byname.cout= makesubproc(cout_entry); |
801 | |
802 | for (;;) sigsuspend(&emptyset); |
803 | } |
804 | |
1e963473 |
805 | int main(int argc, const char *const *argv) { |
806 | parseargs(argc,argv); |
807 | pconfig(configstr,0); |
808 | checkpermit(); |
809 | if (!proto) dumpdebug(); |
810 | |
02b2392d |
811 | startup(); |
812 | startslattach(); |
813 | netconfigure(); |
814 | copydata(); |
1c1a9fa1 |
815 | } |