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