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