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