dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / usr.lib / pppoe / options.c
blobfd6ac8062c4ff64ed96cf03c7209c79b7ad45fff
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * PPPoE Server-mode daemon option parsing.
24 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
26 * Copyright (c) 2016 by Delphix. All rights reserved.
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <assert.h>
33 #include <ctype.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #include <pwd.h>
38 #include <grp.h>
39 #include <errno.h>
40 #include <netdb.h>
41 #include <stropts.h>
42 #include <sys/stat.h>
43 #include <sys/socket.h>
44 #include <net/if.h>
45 #include <netinet/in.h>
46 #include <netinet/if_ether.h>
47 #include <net/sppptun.h>
49 #include "common.h"
50 #include "logging.h"
52 #define MAX_KEYWORD 4096 /* Maximum token length */
53 #define MAX_NEST 32 /* Maximum ${$sub} nesting */
54 #define MAXARGS 256 /* Maximum number of pppd arguments */
57 * Client filter entry. These are linked in *reverse* order so that
58 * the DAG created by file inclusion nesting works as expected. Since
59 * the administrator who wrote the configuration expects "first
60 * match," this means that tests against the filter list must actually
61 * use "last match."
63 struct filter_entry {
64 struct filter_entry *fe_prev; /* Previous filter in list */
65 struct ether_addr fe_mac; /* MAC address */
66 struct ether_addr fe_mask; /* Mask for above address test */
67 uchar_t fe_isexcept; /* invert sense; exclude matching clients */
68 uchar_t fe_prevcopy; /* fe_prev points to copied list */
69 uchar_t fe_unused[2]; /* padding */
73 * Note: I would like to make the strings and filters here const, but
74 * I can't because they have to be passed to free() during parsing. I
75 * could work around this with offsetof() or data copies, but it's not
76 * worth the effort.
78 struct service_entry {
79 const char *se_name; /* Name of service */
80 struct filter_entry *se_flist; /* Pointer to list of client filters */
81 uint_t se_flags; /* SEF_* flags (below) */
82 int se_debug; /* Debug level (0=nodebug) */
83 char *se_server; /* Server (AC) name */
84 char *se_pppd; /* Options for pppd */
85 char *se_path; /* Path to pppd executable */
86 char *se_extra; /* Extra options */
87 char *se_log; /* Log file */
88 uid_t se_uid; /* User ID */
89 gid_t se_gid; /* Group ID */
92 #define SEF_WILD 0x00000001 /* Offer in wildcard reply */
93 #define SEF_NOWILD 0x00000002 /* Don't offer in wildcard */
94 #define SEF_CFLIST 0x00000004 /* se_flist copied from global */
95 #define SEF_CSERVER 0x00000008 /* se_server copied from global */
96 #define SEF_CPPPD 0x00000010 /* se_pppd copied from global */
97 #define SEF_CPATH 0x00000020 /* se_path copied from global */
98 #define SEF_CEXTRA 0x00000040 /* se_extra copied from global */
99 #define SEF_CLOG 0x00000080 /* se_log copied from global */
100 #define SEF_UIDSET 0x00000100 /* se_uid has been set */
101 #define SEF_GIDSET 0x00000200 /* se_gid has been set */
102 #define SEF_DEBUGCLR 0x00000400 /* do not add se_debug from global */
103 #define SEF_CDEV 0x00000800 /* copied devs (parse only) */
106 * One of these is allocated per lower-level stream (device) that is
107 * referenced by the configuration files. The queries are received
108 * per device, and this structure allows us to find all of the
109 * services that correspond to that device.
111 struct device_entry {
112 const char *de_name;
113 const struct service_entry **de_services;
114 int de_nservices;
118 * This is the parsed configuration. While a new configuration is
119 * being read, this is kept around until the new configuration is
120 * ready, and then it is discarded in one operation. It has an array
121 * of device entries (as above) -- one per referenced lower stream --
122 * and a pointer to the allocated parser information. The latter is
123 * kept around because we reuse pointers rather than reallocating and
124 * copying the data. There are thus multiple aliases to the dynamic
125 * data, and the "owner" (for purposes of freeing the storage) is
126 * considered to be this 'junk' list.
128 struct option_state {
129 const struct device_entry *os_devices;
130 int os_ndevices;
131 struct per_file *os_pfjunk; /* Kept for deallocation */
132 char **os_evjunk; /* ditto */
136 * This is the root pointer to the current parsed options.
137 * This cannot be const because it's passed to free() when reparsing
138 * options.
140 static struct option_state *cur_options;
142 /* Global settings for module-wide options. */
143 static struct service_entry glob_svc;
146 * *******************************************************************
147 * Data structures generated during parsing.
150 /* List of device names attached to one service */
151 struct device_list {
152 struct device_list *dl_next;
153 const char *dl_name; /* Name of one device */
156 /* Entry for a single defined service. */
157 struct service_list {
158 struct service_entry sl_entry; /* Parsed service data */
159 struct service_list *sl_next; /* Next service entry */
160 struct parse_state *sl_parse; /* Back pointer to state */
161 struct device_list *sl_dev; /* List of devices */
162 int sl_serial; /* Serial number (conflict resolve) */
164 #define SESERIAL(x) ((struct service_list *)&(x))->sl_serial
165 #define ISGLOBAL(x) ((x) == &(x)->sl_parse->ps_cfile->pf_global)
168 * Structure allocated for each file opened. File nesting is chained
169 * in reverse order so that global option scoping works as expected.
171 struct per_file {
172 struct per_file *pf_prev; /* Back chain */
173 struct service_list pf_global; /* Global (default) service context */
174 struct service_list *pf_svc; /* List of services */
175 struct service_list *pf_svc_last;
176 FILE *pf_input; /* File for input */
177 const char *pf_name; /* File name */
178 int pf_nsvc; /* Count of services */
181 /* State of parser */
182 enum key_state {
183 ksDefault, ksService, ksDevice, ksClient, ksClientE, ksServer,
184 ksPppd, ksFile, ksPath, ksExtra, ksLog, ksUser, ksGroup
188 * Global parser state. There is one of these structures, and it
189 * exists only while actively parsing configuration files.
191 struct parse_state {
192 enum key_state ps_state; /* Parser state */
193 int ps_serial; /* Service serial number */
194 struct per_file *ps_files; /* Parsed files */
195 struct per_file *ps_cfile; /* Current file */
196 struct service_list *ps_csvc; /* Current service */
197 struct device_list *ps_star; /* Wildcard device */
198 int ps_flags; /* PSF_* below */
199 char **ps_evlist; /* allocated environment variables */
200 int ps_evsize; /* max length; for realloc */
203 #define PSF_PERDEV 0x0001 /* In a per-device file */
204 #define PSF_SETLEVEL 0x0002 /* Set log level along the way */
206 /* Should be in a library somewhere. */
207 static char *
208 strsave(const char *str)
210 char *newstr;
212 if (str == NULL)
213 return (NULL);
214 newstr = (char *)malloc(strlen(str) + 1);
215 if (newstr != NULL)
216 (void) strcpy(newstr, str);
217 return (newstr);
221 * Stop defining current service and revert to global definition.
222 * This resolves any implicit references to global options by copying
223 * ("inheriting") from the current global state.
225 static void
226 close_service(struct service_list *slp)
228 struct parse_state *psp;
229 struct per_file *cfile;
230 struct service_entry *sep;
231 struct service_entry *sedefp;
232 struct filter_entry *fep;
234 assert(slp != NULL);
235 psp = slp->sl_parse;
236 cfile = psp->ps_cfile;
238 /* If no current file, then nothing to close. */
239 if (cfile == NULL)
240 return;
242 sep = &slp->sl_entry;
245 * Fix up filter pointers to make DAG. First, locate
246 * the end of the filter list.
248 if (sep->se_flags & SEF_CFLIST) {
249 sep->se_flist = fep = NULL;
250 } else {
251 for (fep = sep->se_flist; fep != NULL; fep = fep->fe_prev)
252 if (fep->fe_prev == NULL || fep->fe_prevcopy) {
253 fep->fe_prev = NULL;
254 break;
257 if (slp == &cfile->pf_global) {
259 * If we're in a global context, then we're about to
260 * open a new service, so it's time to fix up the
261 * filter list so that it's usable as a reference.
262 * Loop through files from which we were included, and
263 * link up filters. Note: closure may occur more than
264 * once here.
266 /* We don't inherit from ourselves. */
267 cfile = cfile->pf_prev;
268 while (cfile != NULL) {
269 if (fep == NULL) {
270 sep->se_flist = fep =
271 cfile->pf_global.sl_entry.se_flist;
272 sep->se_flags |= SEF_CFLIST;
273 } else if (fep->fe_prev == NULL) {
274 fep->fe_prev =
275 cfile->pf_global.sl_entry.se_flist;
276 fep->fe_prevcopy = 1;
278 cfile = cfile->pf_prev;
280 } else {
282 * Loop through default options in current and all
283 * enclosing include files. Inherit options.
285 logdbg("service %s ends", slp->sl_entry.se_name);
286 while (cfile != NULL) {
287 /* Inherit from global service options. */
288 if (slp->sl_dev == NULL) {
289 slp->sl_dev = cfile->pf_global.sl_dev;
290 sep->se_flags |= SEF_CDEV;
292 sedefp = &cfile->pf_global.sl_entry;
293 if (fep == NULL) {
294 sep->se_flist = fep = sedefp->se_flist;
295 sep->se_flags |= SEF_CFLIST;
296 } else if (fep->fe_prev == NULL) {
297 fep->fe_prev = sedefp->se_flist;
298 fep->fe_prevcopy = 1;
300 if (sep->se_server == NULL) {
301 sep->se_server = sedefp->se_server;
302 sep->se_flags |= SEF_CSERVER;
304 if (sep->se_pppd == NULL) {
305 sep->se_pppd = sedefp->se_pppd;
306 sep->se_flags |= SEF_CPPPD;
308 if (sep->se_path == NULL) {
309 sep->se_path = sedefp->se_path;
310 sep->se_flags |= SEF_CPATH;
312 if (sep->se_extra == NULL) {
313 sep->se_extra = sedefp->se_extra;
314 sep->se_flags |= SEF_CEXTRA;
316 if (sep->se_log == NULL) {
317 sep->se_log = sedefp->se_log;
318 sep->se_flags |= SEF_CLOG;
320 if (!(sep->se_flags & SEF_UIDSET) &&
321 (sedefp->se_flags & SEF_UIDSET)) {
322 sep->se_uid = sedefp->se_uid;
323 sep->se_flags |= SEF_UIDSET;
325 if (!(sep->se_flags & SEF_GIDSET) &&
326 (sedefp->se_flags & SEF_GIDSET)) {
327 sep->se_gid = sedefp->se_gid;
328 sep->se_flags |= SEF_GIDSET;
330 if (!(sep->se_flags & (SEF_WILD|SEF_NOWILD)))
331 sep->se_flags |= sedefp->se_flags &
332 (SEF_WILD|SEF_NOWILD);
333 if (!(sep->se_flags & SEF_DEBUGCLR)) {
334 sep->se_debug += sedefp->se_debug;
335 sep->se_flags |= sedefp->se_flags &
336 SEF_DEBUGCLR;
338 cfile = cfile->pf_prev;
341 /* Revert to global definitions. */
342 psp->ps_csvc = &psp->ps_cfile->pf_global;
345 /* Discard a dynamic device list */
346 static void
347 free_device_list(struct device_list *dlp)
349 struct device_list *dln;
351 while (dlp != NULL) {
352 dln = dlp->dl_next;
353 free(dlp);
354 dlp = dln;
359 * Handle "service <name>" -- finish up previous service definition
360 * (if any) by copying from global state where necessary, and start
361 * defining new service.
363 static int
364 set_service(struct service_list *slp, const char *str)
366 struct parse_state *psp;
367 struct per_file *cfile;
369 /* Finish current service */
370 close_service(slp);
372 /* Start new service */
373 psp = slp->sl_parse;
374 slp = (struct service_list *)calloc(sizeof (*slp) + strlen(str) + 1,
376 if (slp == NULL) {
377 logerr("no memory for service \"%s\"", str);
378 return (-1);
381 /* Add to end of list */
382 cfile = psp->ps_cfile;
383 if (cfile->pf_svc_last == NULL)
384 cfile->pf_svc = slp;
385 else
386 cfile->pf_svc_last->sl_next = slp;
387 cfile->pf_svc_last = slp;
388 cfile->pf_nsvc++;
390 /* Fill in initial service entry */
391 slp->sl_entry.se_name = (const char *)(slp+1);
392 (void) strcpy((char *)(slp+1), str);
393 logdbg("service %s begins", slp->sl_entry.se_name);
394 slp->sl_serial = psp->ps_serial++;
395 slp->sl_parse = psp;
397 /* This is now the current service that we're defining. */
398 psp->ps_csvc = slp;
399 return (0);
403 * Handle both "wildcard" and "nowildcard" options.
405 static int
406 set_wildcard(struct service_list *slp, const char *str)
408 /* Allow global context to switch back and forth without error. */
409 if (!ISGLOBAL(slp) &&
410 (slp->sl_entry.se_flags & (SEF_WILD|SEF_NOWILD))) {
411 logdbg("%s: extra \"%s\" ignored",
412 slp->sl_parse->ps_cfile->pf_name, str);
413 return (0);
415 slp->sl_entry.se_flags =
416 (slp->sl_entry.se_flags & ~(SEF_WILD|SEF_NOWILD)) |
417 (*str == 'n' ? SEF_NOWILD : SEF_WILD);
418 return (0);
422 * Handle "debug" option.
424 /*ARGSUSED*/
425 static int
426 set_debug(struct service_list *slp, const char *str)
428 slp->sl_entry.se_debug++;
429 if (ISGLOBAL(slp) && (slp->sl_parse->ps_flags & PSF_SETLEVEL)) {
430 log_level = slp->sl_entry.se_debug;
432 return (0);
436 * Handle "nodebug" option.
438 /*ARGSUSED*/
439 static int
440 set_nodebug(struct service_list *slp, const char *str)
442 slp->sl_entry.se_flags |= SEF_DEBUGCLR;
443 slp->sl_entry.se_debug = 0;
444 if (ISGLOBAL(slp) && (slp->sl_parse->ps_flags & PSF_SETLEVEL)) {
445 log_level = slp->sl_entry.se_debug;
447 return (0);
451 * Handle all plain string options; "server", "pppd", "path", "extra",
452 * and "log".
454 static int
455 set_string(struct service_list *slp, const char *str)
457 char **cpp;
459 assert(!(slp->sl_entry.se_flags &
460 (SEF_CSERVER|SEF_CPPPD|SEF_CPATH|SEF_CEXTRA|SEF_CLOG)));
461 switch (slp->sl_parse->ps_state) {
462 case ksServer:
463 cpp = &slp->sl_entry.se_server;
464 break;
465 case ksPppd:
466 cpp = &slp->sl_entry.se_pppd;
467 break;
468 case ksPath:
469 cpp = &slp->sl_entry.se_path;
470 break;
471 case ksExtra:
472 cpp = &slp->sl_entry.se_extra;
473 break;
474 case ksLog:
475 cpp = &slp->sl_entry.se_log;
476 break;
477 default:
478 assert(0);
479 return (-1);
481 free(*cpp);
482 *cpp = strsave(str);
483 return (0);
487 * Handle "file <name>" option. Close out current service (if any)
488 * and begin parsing from new file.
490 static int
491 set_file(struct service_list *slp, const char *str)
493 FILE *fp;
494 struct per_file *pfp;
495 struct parse_state *psp;
497 close_service(slp);
499 if ((fp = fopen(str, "r")) == NULL) {
500 logwarn("%s: %s: %s", slp->sl_parse->ps_cfile->pf_name, str,
501 mystrerror(errno));
502 return (-1);
504 pfp = (struct per_file *)calloc(sizeof (*pfp) + strlen(str) + 1, 1);
505 if (pfp == NULL) {
506 logerr("no memory for parsing file %s", str);
507 (void) fclose(fp);
508 return (-1);
510 logdbg("config file %s open", str);
512 /* Fill in new file structure. */
513 pfp->pf_name = (const char *)(pfp+1);
514 (void) strcpy((char *)(pfp+1), str);
515 pfp->pf_input = fp;
516 psp = slp->sl_parse;
517 pfp->pf_prev = psp->ps_cfile;
518 psp->ps_cfile = pfp;
520 /* Start off in global context for this file. */
521 psp->ps_csvc = &pfp->pf_global;
522 pfp->pf_global.sl_parse = psp;
523 pfp->pf_global.sl_entry.se_name = "<global>";
524 return (0);
528 * Handle "device <list>" option.
530 static int
531 set_device(struct service_list *slp, const char *str)
533 struct parse_state *psp = slp->sl_parse;
534 struct device_list *dlp;
535 struct device_list *dln;
536 struct device_list **dlpp;
537 const char *cp;
538 int len;
540 /* Can't use this option in the per-device files. */
541 if (psp->ps_flags & PSF_PERDEV) {
542 logerr("\"device %s\" ignored in %s", str,
543 psp->ps_cfile->pf_name);
544 return (0);
547 if (strcmp(str, "*") == 0 || strcmp(str, "all") == 0) {
548 if (!(slp->sl_entry.se_flags & SEF_CDEV))
549 free_device_list(slp->sl_dev);
550 slp->sl_dev = psp->ps_star;
551 slp->sl_entry.se_flags |= SEF_CDEV;
552 } else {
553 dlpp = &dlp;
554 for (;;) {
555 while (isspace(*str) || *str == ',')
556 str++;
557 if (*str == '\0')
558 break;
559 cp = str;
560 while (*str != '\0' && !isspace(*str) && *str != ',')
561 str++;
562 len = str - cp;
563 if ((len == 1 && *cp == '*') ||
564 (len == 3 && strncmp(cp, "all", 3) == 0)) {
565 logerr("%s: cannot use %.*s in device list",
566 psp->ps_cfile->pf_name, len, cp);
567 continue;
569 dln = (struct device_list *)malloc(sizeof (*dln) +
570 len + 1);
571 if (dln == NULL) {
572 logerr("no memory for device name");
573 break;
575 dln->dl_name = (const char *)(dln + 1);
576 /* Cannot use strcpy because cp isn't terminated. */
577 (void) memcpy(dln + 1, cp, len);
578 ((char *)(dln + 1))[len] = '\0';
579 logdbg("%s: device %s", psp->ps_cfile->pf_name,
580 dln->dl_name);
581 *dlpp = dln;
582 dlpp = &dln->dl_next;
584 *dlpp = NULL;
586 dlpp = &slp->sl_dev;
587 if (!(slp->sl_entry.se_flags & SEF_CDEV))
588 while (*dlpp != NULL)
589 dlpp = &(*dlpp)->dl_next;
590 *dlpp = dlp;
591 slp->sl_entry.se_flags &= ~SEF_CDEV;
594 return (0);
598 * Handle <list> portion of "client [except] <list>" option. Attach
599 * to list of filters in reverse order.
601 static int
602 set_client(struct service_list *slp, const char *str)
604 struct parse_state *psp = slp->sl_parse;
605 struct filter_entry *fep;
606 struct filter_entry *fen;
607 const char *cp;
608 int len;
609 char hbuf[MAXHOSTNAMELEN];
610 struct ether_addr ea;
611 struct ether_addr mask;
612 uchar_t *ucp;
613 uchar_t *mcp;
615 /* Head of list. */
616 fep = slp->sl_entry.se_flist;
617 for (;;) {
618 while (isspace(*str) || *str == ',')
619 str++;
620 if (*str == '\0')
621 break;
622 cp = str;
623 while (*str != '\0' && !isspace(*str) && *str != ',')
624 str++;
625 len = str - cp;
626 (void) memcpy(hbuf, cp, len);
627 hbuf[len] = '\0';
628 mcp = mask.ether_addr_octet;
629 mcp[0] = mcp[1] = mcp[2] = mcp[3] = mcp[4] = mcp[5] = 0xFF;
630 if (ether_hostton(hbuf, &ea) != 0) {
631 ucp = ea.ether_addr_octet;
632 while (cp < str) {
633 if (ucp >= ea.ether_addr_octet + sizeof (ea))
634 break;
635 if (*cp == '*') {
636 *mcp++ = *ucp++ = 0;
637 cp++;
638 } else {
639 if (!isxdigit(*cp))
640 break;
641 *ucp = hexdecode(*cp++);
642 if (cp < str && isxdigit(*cp)) {
643 *ucp = (*ucp << 4) |
644 hexdecode(*cp++);
646 ucp++;
647 *mcp++ = 0xFF;
649 if (cp < str) {
650 if (*cp != ':' || cp + 1 == str)
651 break;
652 cp++;
655 if (cp < str) {
656 logerr("%s: illegal Ethernet address %.*s",
657 psp->ps_cfile->pf_name, len, cp);
658 continue;
661 fen = (struct filter_entry *)malloc(sizeof (*fen));
662 if (fen == NULL) {
663 logerr("unable to allocate memory for filter");
664 break;
666 fen->fe_isexcept = psp->ps_state == ksClientE;
667 fen->fe_prevcopy = 0;
668 (void) memcpy(&fen->fe_mac, &ea, sizeof (fen->fe_mac));
669 (void) memcpy(&fen->fe_mask, &mask, sizeof (fen->fe_mask));
670 fen->fe_prev = fep;
671 fep = fen;
673 slp->sl_entry.se_flist = fep;
674 return (0);
678 * Handle "user <name>" option.
680 static int
681 set_user(struct service_list *slp, const char *str)
683 struct passwd *pw;
684 char *cp;
685 uid_t myuid, uid;
687 if ((pw = getpwnam(str)) == NULL) {
688 uid = (uid_t)strtol(str, &cp, 0);
689 if (str == cp || *cp != '\0') {
690 logerr("%s: bad user name \"%s\"",
691 slp->sl_parse->ps_cfile->pf_name, str);
692 return (0);
694 } else {
695 uid = pw->pw_uid;
697 slp->sl_entry.se_uid = uid;
698 myuid = getuid();
699 if (myuid != 0) {
700 if (myuid == uid)
701 return (0);
702 logdbg("%s: not root; ignoring attempt to set UID %d (%s)",
703 slp->sl_parse->ps_cfile->pf_name, uid, str);
704 return (0);
706 slp->sl_entry.se_flags |= SEF_UIDSET;
707 return (0);
711 * Handle "group <name>" option.
713 static int
714 set_group(struct service_list *slp, const char *str)
716 struct group *gr;
717 char *cp;
718 gid_t gid;
720 if ((gr = getgrnam(str)) == NULL) {
721 gid = (gid_t)strtol(str, &cp, 0);
722 if (str == cp || *cp != '\0') {
723 logerr("%s: bad group name \"%s\"",
724 slp->sl_parse->ps_cfile->pf_name, str);
725 return (0);
727 } else {
728 gid = gr->gr_gid;
730 slp->sl_entry.se_gid = gid;
731 if (getuid() != 0) {
732 logdbg("%s: not root; ignoring attempt to set GID %d (%s)",
733 slp->sl_parse->ps_cfile->pf_name, gid, str);
734 return (0);
736 slp->sl_entry.se_flags |= SEF_GIDSET;
737 return (0);
741 * This state machine is used to parse the configuration files. The
742 * "kwe_in" is the state in which the keyword is recognized. The
743 * "kwe_out" is the state that the keyword produces.
745 struct kw_entry {
746 const char *kwe_word;
747 enum key_state kwe_in;
748 enum key_state kwe_out;
749 int (*kwe_func)(struct service_list *slp, const char *str);
752 static const struct kw_entry key_list[] = {
753 { "service", ksDefault, ksService, NULL },
754 { "device", ksDefault, ksDevice, NULL },
755 { "client", ksDefault, ksClient, NULL },
756 { "except", ksClient, ksClientE, NULL },
757 { "wildcard", ksDefault, ksDefault, set_wildcard },
758 { "nowildcard", ksDefault, ksDefault, set_wildcard },
759 { "server", ksDefault, ksServer, NULL },
760 { "pppd", ksDefault, ksPppd, NULL },
761 { "debug", ksDefault, ksDefault, set_debug },
762 { "nodebug", ksDefault, ksDefault, set_nodebug },
763 { "file", ksDefault, ksFile, NULL },
764 { "path", ksDefault, ksPath, NULL },
765 { "extra", ksDefault, ksExtra, NULL },
766 { "log", ksDefault, ksLog, NULL },
767 { "user", ksDefault, ksUser, NULL },
768 { "group", ksDefault, ksGroup, NULL },
769 /* Wildcards only past this point. */
770 { "", ksService, ksDefault, set_service },
771 { "", ksDevice, ksDefault, set_device },
772 { "", ksClient, ksDefault, set_client },
773 { "", ksClientE, ksDefault, set_client },
774 { "", ksServer, ksDefault, set_string },
775 { "", ksPppd, ksDefault, set_string },
776 { "", ksFile, ksDefault, set_file },
777 { "", ksPath, ksDefault, set_string },
778 { "", ksExtra, ksDefault, set_string },
779 { "", ksLog, ksDefault, set_string },
780 { "", ksUser, ksDefault, set_user },
781 { "", ksGroup, ksDefault, set_group },
782 { NULL, ksDefault, ksDefault, NULL }
786 * Produce a string for the keyword that would have gotten us into the
787 * current state.
789 static const char *
790 after_key(enum key_state kstate)
792 const struct kw_entry *kep;
794 for (kep = key_list; kep->kwe_word != NULL; kep++)
795 if (kep->kwe_out == kstate)
796 return (kep->kwe_word);
797 return ("nothing");
801 * Handle end-of-file processing -- close service, close file, revert
802 * to global context in previous include file nest level.
804 static void
805 file_end(struct parse_state *psp)
807 struct per_file *pfp;
809 /* Must not be in the middle of parsing a multi-word sequence now. */
810 if (psp->ps_state != ksDefault) {
811 logerr("%s ends with \"%s\"", psp->ps_cfile->pf_name,
812 after_key(psp->ps_state));
813 psp->ps_state = ksDefault;
815 close_service(psp->ps_csvc);
816 if ((pfp = psp->ps_cfile) != NULL) {
817 /* Put this file on the list of finished files. */
818 psp->ps_cfile = pfp->pf_prev;
819 pfp->pf_prev = psp->ps_files;
820 psp->ps_files = pfp;
821 if (pfp->pf_input != NULL) {
822 logdbg("file %s closed", pfp->pf_name);
823 (void) fclose(pfp->pf_input);
824 pfp->pf_input = NULL;
827 /* Back up to previous file, if any, and set global context. */
828 if ((pfp = psp->ps_cfile) != NULL)
829 psp->ps_csvc = &pfp->pf_global;
834 * Dispatch a single keyword against the parser state machine or
835 * handle an environment variable assignment. The input is a string
836 * containing the single word to be dispatched.
838 static int
839 dispatch_keyword(struct parse_state *psp, const char *keybuf)
841 const struct kw_entry *kep;
842 int retv;
843 char *cp;
844 char *env;
845 char **evlist;
846 int len;
848 retv = 0;
849 for (kep = key_list; kep->kwe_word != NULL; kep++) {
850 if (kep->kwe_in == psp->ps_state &&
851 (*kep->kwe_word == '\0' ||
852 strcasecmp(kep->kwe_word, keybuf) == 0)) {
853 if (kep->kwe_func != NULL)
854 retv = (*kep->kwe_func)(psp->ps_csvc, keybuf);
855 psp->ps_state = kep->kwe_out;
856 return (retv);
859 if (strchr(keybuf, '=') != NULL) {
860 if ((cp = strsave(keybuf)) == NULL) {
861 logerr("no memory to save %s", keybuf);
862 return (0);
864 len = (strchr(cp, '=') - cp) + 1;
865 if ((evlist = psp->ps_evlist) == NULL) {
866 psp->ps_evlist = evlist =
867 (char **)malloc(8 * sizeof (*evlist));
868 if (evlist == NULL) {
869 logerr("no memory for evlist");
870 free(cp);
871 return (0);
873 psp->ps_evsize = 8;
874 evlist[0] = evlist[1] = NULL;
875 } else {
876 while ((env = *evlist) != NULL) {
877 if (strncmp(cp, env, len) == 0)
878 break;
879 evlist++;
881 if (env == NULL &&
882 evlist-psp->ps_evlist >= psp->ps_evsize-1) {
883 evlist = reallocarray(psp->ps_evlist,
884 psp->ps_evsize + 8, sizeof (*evlist));
885 if (evlist == NULL) {
886 logerr("cannot realloc evlist to %d",
887 psp->ps_evsize + 8);
888 free(cp);
889 return (0);
891 psp->ps_evlist = evlist;
892 evlist += psp->ps_evsize - 1;
893 psp->ps_evsize += 8;
894 evlist[1] = NULL;
897 logdbg("setenv \"%s\"", cp);
898 free(*evlist);
899 *evlist = cp;
900 return (0);
902 logerr("%s: unknown keyword '%s'", psp->ps_cfile->pf_name, keybuf);
903 return (-1);
907 * Modified version of standard getenv; looks in locally-stored
908 * environment first. This function exists because we need to be able
909 * to revert to the original environment during a reread (SIGHUP), and
910 * the putenv() function overwrites that environment.
912 static char *
913 my_getenv(struct parse_state *psp, char *estr)
915 char **evlist, *ent;
916 int elen;
918 if (psp != NULL && (evlist = psp->ps_evlist) != NULL) {
919 elen = strlen(estr);
920 while ((ent = *evlist++) != NULL) {
921 if (strncmp(ent, estr, elen) == 0 &&
922 ent[elen] == '=')
923 return (ent + elen + 1);
926 return (getenv(estr));
930 * Expand an environment variable at the end of current buffer and
931 * return pointer to next spot in buffer for character append. psp
932 * context may be null.
934 static char *
935 env_replace(struct parse_state *psp, char *keybuf, char kwstate)
937 char *cpe;
938 char *cp;
940 if ((cp = strrchr(keybuf, kwstate)) != NULL) {
941 if ((cpe = my_getenv(psp, cp + 1)) != NULL) {
942 *cp = '\0';
943 (void) strncat(cp, cpe,
944 MAX_KEYWORD - (cp - keybuf) - 1);
945 keybuf[MAX_KEYWORD - 1] = '\0';
946 cp += strlen(cp);
947 } else {
948 logerr("unknown variable \"%s\"", cp + 1);
950 } else {
951 /* Should not occur. */
952 cp = keybuf + strlen(keybuf);
954 return (cp);
958 * Given a character-at-a-time input function, get a delimited keyword
959 * from the input. This function handles the usual escape sequences,
960 * quoting, commenting, and environment variable expansion.
962 * The standard wordexp(3C) function isn't used here because the POSIX
963 * definition is hard to use, and the Solaris implementation is
964 * resource-intensive and insecure. The "hard-to-use" part is that
965 * wordexp expands only variables from the environment, and can't
966 * handle an environment overlay. Instead, the caller must use the
967 * feeble putenv/getenv interface, and rewinding to the initial
968 * environment without leaking storage is hard. The Solaris
969 * implementation invokes an undocumented extensions via
970 * fork/exec("/bin/ksh -\005 %s") for every invocation, and gathers
971 * the expanded result with pipe. This makes it slow to execute and
972 * exposes the string being expanded to users with access to "ps -f."
974 * psp may be null; it's used only for environment variable expansion.
975 * Input "flag" is 1 to ignore EOL, '#', and '$'; 0 for normal file parsing.
977 * Returns:
978 * 0 - keyword parsed.
979 * 1 - end of file; no keyword.
980 * 2 - end of file after this keyword.
982 static int
983 getkeyword(struct parse_state *psp, char *keybuf, int keymax,
984 int (*nextchr)(void *), void *arg, int flag)
986 char varnest[MAX_NEST];
987 char *kbp;
988 char *vnp;
989 char chr;
990 int ichr;
991 char kwstate;
992 static const char escstr[] = "a\ab\bf\fn\nr\r";
993 const char *cp;
995 keymax--; /* Account for trailing NUL byte */
997 kwstate = '\0';
998 kbp = keybuf;
999 vnp = varnest;
1000 for (;;) {
1001 ichr = (*nextchr)(arg);
1002 chr = (char)ichr;
1003 tryagain:
1004 switch (kwstate) {
1005 case '\\': /* Start of unquoted escape sequence */
1006 case '|': /* Start of escape sequence in double quotes */
1007 case '~': /* Start of escape sequence in single quotes */
1008 /* Convert the character if we can. */
1009 if (chr == '\n')
1010 chr = '\0';
1011 else if (isalpha(chr) &&
1012 (cp = strchr(escstr, chr)) != NULL)
1013 chr = cp[1];
1014 /* Revert to previous state */
1015 switch (kwstate) {
1016 case '\\':
1017 kwstate = 'A';
1018 break;
1019 case '|':
1020 kwstate = '"';
1021 break;
1022 case '~':
1023 kwstate = '\'';
1024 break;
1026 break;
1027 case '"': /* In double-quote string */
1028 if (!flag && chr == '$') {
1029 /* Handle variable expansion. */
1030 kwstate = '%';
1031 chr = '\0';
1032 break;
1034 /* FALLTHROUGH */
1035 case '\'': /* In single-quote string */
1036 if (chr == '\\') {
1037 /* Handle start of escape sequence */
1038 kwstate = kwstate == '"' ? '|' : '~';
1039 chr = '\0';
1040 break;
1042 if (chr == kwstate) {
1043 /* End of quoted string; revert to normal */
1044 kwstate = 'A';
1045 chr = '\0';
1047 break;
1048 case '$': /* Start of unquoted variable name */
1049 case '%': /* Start of variable name in quoted string */
1050 if (chr == '{') {
1051 /* Variable name is bracketed. */
1052 kwstate = chr =
1053 kwstate == '$' ? '{' : '[';
1054 break;
1056 *kbp++ = kwstate = kwstate == '$' ? '+' : '*';
1057 /* FALLTHROUGH */
1058 case '+': /* Gathering unquoted variable name */
1059 case '*': /* Gathering variable name in quoted string */
1060 if (chr == '$' &&
1061 vnp < varnest + sizeof (varnest)) {
1062 *vnp++ = kwstate;
1063 kwstate = '$';
1064 chr = '\0';
1065 break;
1067 if (!isalnum(chr) && chr != '_' &&
1068 chr != '.' && chr != '-') {
1069 *kbp = '\0';
1070 kbp = env_replace(psp, keybuf, kwstate);
1071 if (vnp > varnest)
1072 kwstate = *--vnp;
1073 else
1074 kwstate = kwstate == '+' ?
1075 'A' : '"';
1076 /* Go reinterpret in new context */
1077 goto tryagain;
1079 break;
1080 case '{': /* Gathering bracketed, unquoted var name */
1081 case '[': /* Gathering bracketed, quoted var name */
1082 if (chr == '}') {
1083 *kbp = '\0';
1084 kbp = env_replace(psp, keybuf, kwstate);
1085 kwstate = kwstate == '{' ? 'A' : '"';
1086 chr = '\0';
1088 break;
1089 case '#': /* Comment before word state */
1090 case '@': /* Comment after word state */
1091 if (chr == '\n' || chr == '\r' || ichr == EOF) {
1092 /* At end of line, revert to previous state */
1093 kwstate = kwstate == '#' ? '\0' : ' ';
1094 chr = '\0';
1095 break;
1097 chr = '\0';
1098 break;
1099 case '\0': /* Initial state; no word seen yet. */
1100 if (ichr == EOF || isspace(chr)) {
1101 chr = '\0'; /* Skip over leading spaces */
1102 break;
1104 if (chr == '#') {
1105 kwstate = '#';
1106 chr = '\0'; /* Skip over comments */
1107 break;
1109 /* Start of keyword seen. */
1110 kwstate = 'A';
1111 /* FALLTHROUGH */
1112 default: /* Middle of keyword parsing. */
1113 if (ichr == EOF)
1114 break;
1115 if (isspace(chr)) { /* Space terminates word */
1116 kwstate = ' ';
1117 break;
1119 if (chr == '"' || chr == '\'' || chr == '\\') {
1120 kwstate = chr; /* Begin quote or escape */
1121 chr = '\0';
1122 break;
1124 if (flag) /* Allow ignore; for string reparse */
1125 break;
1126 if (chr == '#') { /* Comment terminates word */
1127 kwstate = '@'; /* Must consume comment also */
1128 chr = '\0';
1129 break;
1131 if (chr == '$') {
1132 kwstate = '$'; /* Begin variable expansion */
1133 chr = '\0';
1135 break;
1138 * If we've reached a space at the end of the word,
1139 * then we're done.
1141 if (ichr == EOF || kwstate == ' ')
1142 break;
1144 * If there's a character to store and space
1145 * available, then add it to the string
1147 if (chr != '\0' && kbp < keybuf + keymax)
1148 *kbp++ = (char)chr;
1151 *kbp = '\0';
1153 if (ichr == EOF) {
1154 return (kwstate == '\0' ? 1 : 2);
1156 return (0);
1160 * Fetch words from current file until all files are closed. Handles
1161 * include files.
1163 static void
1164 parse_from_file(struct parse_state *psp)
1166 char keybuf[MAX_KEYWORD];
1167 int retv;
1169 while (psp->ps_cfile != NULL && psp->ps_cfile->pf_input != NULL) {
1170 retv = getkeyword(psp, keybuf, sizeof (keybuf),
1171 (int (*)(void *))fgetc, (void *)psp->ps_cfile->pf_input,
1174 if (retv != 1)
1175 (void) dispatch_keyword(psp, keybuf);
1177 if (retv != 0)
1178 file_end(psp);
1183 * Open and parse named file. This is for the predefined
1184 * configuration files in /etc/ppp -- it's not an error if any of
1185 * these are missing.
1187 static void
1188 parse_file(struct parse_state *psp, const char *fname)
1190 struct stat sb;
1192 /* It's ok if any of these files are missing. */
1193 if (stat(fname, &sb) == -1 && errno == ENOENT)
1194 return;
1195 if (set_file(psp->ps_csvc, fname) == 0)
1196 parse_from_file(psp);
1200 * Dispatch keywords from command line. Handles any files included
1201 * from there.
1203 static void
1204 parse_arg_list(struct parse_state *psp, int argc, char **argv)
1206 /* The first argument (program name) can be null. */
1207 if (--argc <= 0)
1208 return;
1209 while (--argc >= 0) {
1210 (void) dispatch_keyword(psp, *++argv);
1211 if (psp->ps_cfile->pf_input != NULL)
1212 parse_from_file(psp);
1216 /* Count length of dynamic device list */
1217 static int
1218 count_devs(struct device_list *dlp)
1220 int ndevs;
1222 ndevs = 0;
1223 for (; dlp != NULL; dlp = dlp->dl_next)
1224 ndevs++;
1225 return (ndevs);
1228 /* Count number of devices named in entire file. */
1229 static int
1230 count_per_file(struct per_file *pfp)
1232 struct service_list *slp;
1233 int ndevs = 0;
1235 for (; pfp != NULL; pfp = pfp->pf_prev) {
1236 ndevs += count_devs(pfp->pf_global.sl_dev);
1237 for (slp = pfp->pf_svc; slp != NULL; slp = slp->sl_next)
1238 if (!(slp->sl_entry.se_flags & SEF_CDEV))
1239 ndevs += count_devs(slp->sl_dev);
1241 return (ndevs);
1244 /* Write device names into linear array. */
1245 static const char **
1246 devs_to_list(struct device_list *dlp, const char **dnames)
1248 for (; dlp != NULL; dlp = dlp->dl_next)
1249 *dnames++ = dlp->dl_name;
1250 return (dnames);
1253 /* Write all device names from file into a linear array. */
1254 static const char **
1255 per_file_to_list(struct per_file *pfp, const char **dnames)
1257 struct service_list *slp;
1259 for (; pfp != NULL; pfp = pfp->pf_prev) {
1260 dnames = devs_to_list(pfp->pf_global.sl_dev, dnames);
1261 for (slp = pfp->pf_svc; slp != NULL; slp = slp->sl_next)
1262 if (!(slp->sl_entry.se_flags & SEF_CDEV))
1263 dnames = devs_to_list(slp->sl_dev, dnames);
1265 return (dnames);
1268 /* Compare device names; used with qsort */
1269 static int
1270 devcmp(const void *d1, const void *d2)
1272 return (strcmp(*(const char **)d1, *(const char **)d2));
1276 * Get sorted list of unique device names among all defined and
1277 * partially defined services in all files.
1279 static const char **
1280 get_unique_devs(struct parse_state *psp)
1282 int ndevs;
1283 const char **dnames;
1284 const char **dnp;
1285 const char **dnf;
1288 * Count number of explicitly referenced devices among all
1289 * services (including duplicates).
1291 ndevs = count_per_file(psp->ps_files);
1292 ndevs += count_per_file(psp->ps_cfile);
1293 if (ndevs <= 0) {
1294 return (NULL);
1297 /* Sort and trim out duplicate devices. */
1298 dnames = (const char **)malloc((ndevs+1) * sizeof (const char *));
1299 if (dnames == NULL) {
1300 logerr("unable to allocate space for %d devices", ndevs + 1);
1301 return (NULL);
1303 dnp = per_file_to_list(psp->ps_files, dnames);
1304 (void) per_file_to_list(psp->ps_cfile, dnp);
1305 qsort(dnames, ndevs, sizeof (const char *), devcmp);
1306 for (dnf = (dnp = dnames) + 1; dnf < dnames+ndevs; dnf++)
1307 if (strcmp(*dnf, *dnp) != 0)
1308 *++dnp = *dnf;
1309 *++dnp = NULL;
1311 /* Return array of pointers to names. */
1312 return (dnames);
1316 * Convert data structures created by parsing process into data
1317 * structures used by service dispatch. This gathers the unique
1318 * device (lower stream) names and attaches the services available on
1319 * each device to a list while triming duplicate services.
1321 static struct option_state *
1322 organize_state(struct parse_state *psp)
1324 struct per_file *pfp;
1325 struct per_file *pftopp;
1326 struct service_list *slp;
1327 struct device_list *dlp;
1328 int ndevs;
1329 int nsvcs;
1330 const char **dnames;
1331 const char **dnp;
1332 struct device_entry *dep;
1333 struct option_state *osp;
1334 struct service_entry **sepp;
1335 struct service_entry **sebpp;
1336 struct service_entry **se2pp;
1339 * Parsing is now done.
1341 close_service(psp->ps_csvc);
1342 psp->ps_csvc = NULL;
1343 if ((pfp = psp->ps_cfile) != NULL) {
1344 pfp->pf_prev = psp->ps_files;
1345 psp->ps_files = pfp;
1346 psp->ps_cfile = NULL;
1349 /* Link the services from all files together for easy referencing. */
1350 pftopp = psp->ps_files;
1351 for (pfp = pftopp->pf_prev; pfp != NULL; pfp = pfp->pf_prev)
1352 if (pfp->pf_svc != NULL) {
1353 if (pftopp->pf_svc_last == NULL)
1354 pftopp->pf_svc = pfp->pf_svc;
1355 else
1356 pftopp->pf_svc_last->sl_next = pfp->pf_svc;
1357 pftopp->pf_svc_last = pfp->pf_svc_last;
1358 pfp->pf_svc = pfp->pf_svc_last = NULL;
1362 * Count up number of services per device, including
1363 * duplicates but not including defaults.
1365 nsvcs = 0;
1366 for (slp = psp->ps_files->pf_svc; slp != NULL; slp = slp->sl_next)
1367 for (dlp = slp->sl_dev; dlp != NULL; dlp = dlp->dl_next)
1368 nsvcs++;
1371 * Get the unique devices referenced by all services.
1373 dnames = get_unique_devs(psp);
1374 if (dnames == NULL) {
1375 logdbg("no devices referenced by any service");
1376 return (NULL);
1378 ndevs = 0;
1379 for (dnp = dnames; *dnp != NULL; dnp++)
1380 ndevs++;
1383 * Allocate room for main structure, device records, and
1384 * per-device lists. Worst case is all devices having all
1385 * services; that's why we allocate for nsvcs * ndevs.
1387 osp = (struct option_state *)malloc(sizeof (*osp) +
1388 ndevs * sizeof (*dep) + nsvcs * ndevs * sizeof (*sepp));
1389 if (osp == NULL) {
1390 logerr("unable to allocate option state structure");
1391 free(dnames);
1392 return (NULL);
1395 /* We're going to succeed now, so steal these over. */
1396 osp->os_devices = dep = (struct device_entry *)(osp+1);
1397 osp->os_pfjunk = psp->ps_files;
1398 psp->ps_files = NULL;
1399 osp->os_evjunk = psp->ps_evlist;
1400 psp->ps_evlist = NULL;
1402 /* Loop over devices, install services, remove duplicates. */
1403 sepp = (struct service_entry **)(dep + ndevs);
1404 for (dnp = dnames; *dnp != NULL; dnp++) {
1405 dep->de_name = *dnp;
1406 dep->de_services = (const struct service_entry **)sepp;
1407 sebpp = sepp;
1408 for (slp = osp->os_pfjunk->pf_svc; slp != NULL;
1409 slp = slp->sl_next)
1410 for (dlp = slp->sl_dev; dlp != NULL;
1411 dlp = dlp->dl_next) {
1412 if (dlp->dl_name == *dnp ||
1413 strcmp(dlp->dl_name, *dnp) == 0) {
1414 for (se2pp = sebpp; se2pp < sepp;
1415 se2pp++)
1416 if ((*se2pp)->se_name ==
1417 slp->sl_entry.se_name ||
1418 strcmp((*se2pp)->
1419 se_name, slp->sl_entry.
1420 se_name) == 0)
1421 break;
1423 * We retain a service if it's
1424 * unique or if its serial
1425 * number (position in the
1426 * file) is greater than than
1427 * any other.
1429 if (se2pp >= sepp)
1430 *sepp++ = &slp->sl_entry;
1431 else if (SESERIAL(**se2pp) <
1432 SESERIAL(slp->sl_entry))
1433 *se2pp = &slp->sl_entry;
1436 /* Count up the services on this device. */
1437 dep->de_nservices = (const struct service_entry **)sepp -
1438 dep->de_services;
1439 /* Ignore devices having no services at all. */
1440 if (dep->de_nservices > 0)
1441 dep++;
1443 /* Count up the devices. */
1444 osp->os_ndevices = dep - osp->os_devices;
1445 /* Free the list of device names */
1446 free(dnames);
1447 return (osp);
1451 * Free storage unique to a given service. Pointers copied from other
1452 * services are ignored.
1454 static void
1455 free_service(struct service_list *slp)
1457 struct filter_entry *fep;
1458 struct filter_entry *fen;
1460 if (!(slp->sl_entry.se_flags & SEF_CDEV))
1461 free_device_list(slp->sl_dev);
1462 if (!(slp->sl_entry.se_flags & SEF_CFLIST)) {
1463 fep = slp->sl_entry.se_flist;
1464 while (fep != NULL) {
1465 fen = fep->fe_prevcopy ? NULL : fep->fe_prev;
1466 free(fep);
1467 fep = fen;
1470 if (!(slp->sl_entry.se_flags & SEF_CPPPD))
1471 free(slp->sl_entry.se_pppd);
1472 if (!(slp->sl_entry.se_flags & SEF_CSERVER))
1473 free(slp->sl_entry.se_server);
1474 if (!(slp->sl_entry.se_flags & SEF_CPATH))
1475 free(slp->sl_entry.se_path);
1476 if (!(slp->sl_entry.se_flags & SEF_CEXTRA))
1477 free(slp->sl_entry.se_extra);
1478 if (!(slp->sl_entry.se_flags & SEF_CLOG))
1479 free(slp->sl_entry.se_log);
1483 * Free a linked list of services.
1485 static void
1486 free_service_list(struct service_list *slp)
1488 struct service_list *sln;
1490 while (slp != NULL) {
1491 free_service(slp);
1492 sln = slp->sl_next;
1493 free(slp);
1494 slp = sln;
1499 * Free a linked list of files and all services in those files.
1501 static void
1502 free_file_list(struct per_file *pfp)
1504 struct per_file *pfn;
1506 while (pfp != NULL) {
1507 free_service(&pfp->pf_global);
1508 free_service_list(pfp->pf_svc);
1509 pfn = pfp->pf_prev;
1510 free(pfp);
1511 pfp = pfn;
1516 * Free an array of local environment variables.
1518 static void
1519 free_env_list(char **evlist)
1521 char **evp;
1522 char *env;
1524 if ((evp = evlist) != NULL) {
1525 while ((env = *evp++) != NULL)
1526 free(env);
1527 free(evlist);
1532 * Add a new device (lower stream) to the list for which we're the
1533 * PPPoE server.
1535 static void
1536 add_new_dev(int tunfd, const char *dname)
1538 union ppptun_name ptn;
1540 (void) snprintf(ptn.ptn_name, sizeof (ptn.ptn_name), "%s:pppoed",
1541 dname);
1542 if (strioctl(tunfd, PPPTUN_SCTL, &ptn, sizeof (ptn), 0) < 0) {
1543 logerr("PPPTUN_SCTL %s: %s", ptn.ptn_name, mystrerror(errno));
1544 } else {
1545 logdbg("added %s", ptn.ptn_name);
1550 * Remove an existing device (lower stream) from the list for which we
1551 * were the PPPoE server.
1553 static void
1554 rem_old_dev(int tunfd, const char *dname)
1556 union ppptun_name ptn;
1558 (void) snprintf(ptn.ptn_name, sizeof (ptn.ptn_name), "%s:pppoed",
1559 dname);
1560 if (strioctl(tunfd, PPPTUN_DCTL, &ptn, sizeof (ptn), 0) < 0) {
1561 logerr("PPPTUN_DCTL %s: %s", ptn.ptn_name, mystrerror(errno));
1562 } else {
1563 logdbg("removed %s", ptn.ptn_name);
1568 * Get a list of all of the devices currently plumbed for PPPoE. This
1569 * is used for supporting the "*" and "all" device aliases.
1571 static void
1572 get_device_list(struct parse_state *psp, int tunfd)
1574 struct device_list *dlp;
1575 struct device_list **dlpp;
1576 struct device_list *dlalt;
1577 struct device_list **dl2pp;
1578 struct device_list *dla;
1579 int i;
1580 union ppptun_name ptn;
1581 char *cp;
1583 /* First pass; just allocate space for all *:pppoe* devices */
1584 dlpp = &psp->ps_star;
1585 dl2pp = &dlalt;
1586 for (i = 0; ; i++) {
1587 ptn.ptn_index = i;
1588 if (strioctl(tunfd, PPPTUN_GNNAME, &ptn, sizeof (ptn),
1589 sizeof (ptn)) < 0) {
1590 logerr("PPPTUN_GNNAME %d: %s", i, mystrerror(errno));
1591 break;
1593 if (ptn.ptn_name[0] == '\0')
1594 break;
1595 if ((cp = strchr(ptn.ptn_name, ':')) == NULL ||
1596 strncmp(cp, ":pppoe", 6) != 0 ||
1597 (cp[6] != '\0' && strcmp(cp+6, "d") != 0))
1598 continue;
1599 *cp = '\0';
1600 dlp = (struct device_list *)malloc(sizeof (*dlp) +
1601 strlen(ptn.ptn_name) + 1);
1602 if (dlp == NULL)
1603 break;
1604 dlp->dl_name = (const char *)(dlp + 1);
1605 (void) strcpy((char *)(dlp + 1), ptn.ptn_name);
1606 if (cp[6] == '\0') {
1607 *dlpp = dlp;
1608 dlpp = &dlp->dl_next;
1609 } else {
1610 *dl2pp = dlp;
1611 dl2pp = &dlp->dl_next;
1614 *dlpp = NULL;
1615 *dl2pp = NULL;
1617 /* Second pass; eliminate improperly plumbed devices */
1618 for (dlpp = &psp->ps_star; (dlp = *dlpp) != NULL; ) {
1619 for (dla = dlalt; dla != NULL; dla = dla->dl_next)
1620 if (strcmp(dla->dl_name, dlp->dl_name) == 0)
1621 break;
1622 if (dla == NULL) {
1623 *dlpp = dlp->dl_next;
1624 free(dlp);
1625 } else {
1626 dlpp = &dlp->dl_next;
1629 free_device_list(dlalt);
1631 /* Add in "*" so we can always handle dynamic plumbing. */
1632 dlp = (struct device_list *)malloc(sizeof (*dlp) + 2);
1633 if (dlp != NULL) {
1634 dlp->dl_name = (const char *)(dlp + 1);
1635 (void) strcpy((char *)(dlp + 1), "*");
1636 dlp->dl_next = psp->ps_star;
1637 psp->ps_star = dlp;
1642 * Set logging subsystem back to configured global default values.
1644 void
1645 global_logging(void)
1647 log_for_service(glob_svc.se_log, glob_svc.se_debug);
1651 * Handle SIGHUP -- reparse command line and all configuration files.
1652 * When reparsing is complete, free old parsed data and replace with
1653 * new.
1655 void
1656 parse_options(int tunfd, int argc, char **argv)
1658 struct parse_state pstate;
1659 struct per_file *argpf;
1660 struct option_state *newopt;
1661 const char **dnames;
1662 const char **dnp;
1663 const struct device_entry *newdep, *newmax;
1664 const struct device_entry *olddep, *oldmax;
1665 int cmpval;
1666 struct service_entry newglobsvc, *mainsvc;
1668 /* Note that all per_file structures must be freeable */
1669 argpf = (struct per_file *)calloc(sizeof (*argpf), 1);
1670 if (argpf == NULL) {
1671 return;
1673 (void) memset(&pstate, '\0', sizeof (pstate));
1674 pstate.ps_state = ksDefault;
1675 pstate.ps_cfile = argpf;
1676 pstate.ps_csvc = &argpf->pf_global;
1677 argpf->pf_global.sl_parse = &pstate;
1678 argpf->pf_name = "command line";
1680 /* Default is 1 -- errors only */
1681 argpf->pf_global.sl_entry.se_debug++;
1682 argpf->pf_global.sl_entry.se_name = "<global>";
1684 /* Get list of all devices */
1685 get_device_list(&pstate, tunfd);
1687 /* Parse options from command line and main configuration file. */
1688 pstate.ps_flags |= PSF_SETLEVEL;
1689 parse_arg_list(&pstate, argc, argv);
1690 parse_file(&pstate, "/etc/ppp/pppoe");
1691 pstate.ps_flags &= ~PSF_SETLEVEL;
1694 * At this point, global options from the main configuration
1695 * file are pointed to by ps_files, and options from command
1696 * line are in argpf. We need to pull three special options
1697 * from these -- wildcard, debug, and log. Note that the main
1698 * options file overrides the command line. This is
1699 * intentional. The semantics are such that the system
1700 * behaves as though the main configuration file were
1701 * "included" from the command line, and thus options there
1702 * override the command line. This may seem odd, but at least
1703 * it's self-consistent.
1705 newglobsvc = argpf->pf_global.sl_entry;
1706 if (pstate.ps_files != NULL) {
1707 mainsvc = &pstate.ps_files->pf_global.sl_entry;
1708 if (mainsvc->se_log != NULL)
1709 newglobsvc.se_log = mainsvc->se_log;
1710 if (mainsvc->se_flags & (SEF_WILD|SEF_NOWILD))
1711 newglobsvc.se_flags =
1712 (newglobsvc.se_flags & ~(SEF_WILD|SEF_NOWILD)) |
1713 (mainsvc->se_flags & (SEF_WILD|SEF_NOWILD));
1714 if (mainsvc->se_flags & SEF_DEBUGCLR)
1715 newglobsvc.se_debug = 0;
1716 newglobsvc.se_debug += mainsvc->se_debug;
1718 glob_svc = newglobsvc;
1719 global_logging();
1721 /* Get the list of devices referenced by configuration above. */
1722 dnames = get_unique_devs(&pstate);
1723 if (dnames != NULL) {
1724 /* Read per-device configuration files. */
1725 pstate.ps_flags |= PSF_PERDEV;
1726 for (dnp = dnames; *dnp != NULL; dnp++)
1727 parse_file(&pstate, *dnp);
1728 pstate.ps_flags &= ~PSF_PERDEV;
1729 free(dnames);
1731 file_end(&pstate);
1734 * Convert parsed data structures into per-device structures.
1735 * (Invert the table.)
1737 newopt = organize_state(&pstate);
1739 /* If we're going to free the file name, then stop logging there. */
1740 if (newopt == NULL && glob_svc.se_log != NULL) {
1741 glob_svc.se_log = NULL;
1742 global_logging();
1746 * Unless an error has occurred, these pointers are normally
1747 * all NULL. Nothing is freed until the file is re-read.
1749 free_file_list(pstate.ps_files);
1750 free_file_list(pstate.ps_cfile);
1751 free_device_list(pstate.ps_star);
1752 free_env_list(pstate.ps_evlist);
1755 * Match up entries on device list. Detach devices no longer
1756 * referenced. Attach ones now referenced. (The use of null
1757 * pointers here may look fishy, but it actually works.
1758 * NULL>=NULL is always true.)
1760 if (newopt != NULL) {
1761 newdep = newopt->os_devices;
1762 newmax = newdep + newopt->os_ndevices;
1763 } else {
1764 newdep = newmax = NULL;
1766 if (cur_options != NULL) {
1767 olddep = cur_options->os_devices;
1768 oldmax = olddep + cur_options->os_ndevices;
1769 } else {
1770 olddep = oldmax = NULL;
1772 while ((newdep != NULL && newdep < newmax) ||
1773 (olddep != NULL && olddep < oldmax)) {
1774 if (newdep < newmax) {
1775 if (olddep >= oldmax) {
1776 add_new_dev(tunfd, newdep->de_name);
1777 newdep++;
1778 } else {
1779 cmpval = strcmp(newdep->de_name,
1780 olddep->de_name);
1781 if (cmpval < 0) {
1782 /* Brand new device seen. */
1783 add_new_dev(tunfd, newdep->de_name);
1784 newdep++;
1785 } else if (cmpval == 0) {
1786 /* Existing device; skip it. */
1787 newdep++;
1788 olddep++;
1790 /* No else clause -- removal is below */
1793 if (olddep < oldmax) {
1794 if (newdep >= newmax) {
1795 rem_old_dev(tunfd, olddep->de_name);
1796 olddep++;
1797 } else {
1798 cmpval = strcmp(newdep->de_name,
1799 olddep->de_name);
1800 if (cmpval > 0) {
1801 /* Old device is gone */
1802 rem_old_dev(tunfd, olddep->de_name);
1803 olddep++;
1804 } else if (cmpval == 0) {
1805 /* Existing device; skip it. */
1806 newdep++;
1807 olddep++;
1809 /* No else clause -- insert handled above */
1814 /* Discard existing parsed data storage. */
1815 if (cur_options != NULL) {
1816 free_file_list(cur_options->os_pfjunk);
1817 free_env_list(cur_options->os_evjunk);
1818 free(cur_options);
1820 /* Install new. */
1821 cur_options = newopt;
1825 * Check if configured filters permit requesting client to use a given
1826 * service. Note -- filters are stored in reverse order in order to
1827 * make file-inclusion work as expected. Thus, the "first match"
1828 * filter rule becomes "last match" here.
1830 static boolean_t
1831 allow_service(const struct service_entry *sep, const ppptun_atype *pap)
1833 const struct filter_entry *fep;
1834 const struct filter_entry *lmatch;
1835 boolean_t anynonexcept = B_FALSE;
1836 const uchar_t *upt;
1837 const uchar_t *macp;
1838 const uchar_t *maskp;
1839 int i;
1841 lmatch = NULL;
1842 for (fep = sep->se_flist; fep != NULL; fep = fep->fe_prev) {
1843 anynonexcept |= !fep->fe_isexcept;
1844 upt = pap->pta_pppoe.ptma_mac;
1845 macp = fep->fe_mac.ether_addr_octet;
1846 maskp = fep->fe_mask.ether_addr_octet;
1847 for (i = sizeof (pap->pta_pppoe.ptma_mac); i > 0; i--)
1848 if (((*macp++ ^ *upt++) & *maskp++) != 0)
1849 break;
1850 if (i <= 0)
1851 lmatch = fep;
1854 if (lmatch == NULL) {
1856 * Assume reject by default if any positive-match
1857 * (non-except) filters are given. Otherwise, if
1858 * there are no positive-match filters, then
1859 * non-matching means accept by default.
1861 return (!anynonexcept);
1863 return (!lmatch->fe_isexcept);
1867 * Locate available service(s) based on client request. Assumes that
1868 * outp points to a buffer of at least size PPPOE_MSGMAX. Creates a
1869 * PPPoE response message in outp. Returns count of matched services
1870 * and (through *srvp) a pointer to the last (or only) service. If
1871 * some error is found in the request, an error string is added and -1
1872 * is returned; the caller should just send the message without
1873 * alteration.
1876 locate_service(poep_t *poep, int plen, const char *iname, ppptun_atype *pap,
1877 uint32_t *outp, void **srvp)
1879 poep_t *opoe;
1880 const uint8_t *tagp;
1881 const char *cp;
1882 int ttyp;
1883 int tlen;
1884 int nsvcs;
1885 const struct device_entry *dep, *depe;
1886 const struct device_entry *wdep;
1887 const struct service_entry **sepp, **seppe;
1888 const struct service_entry *sep;
1889 char *str;
1890 boolean_t ispadi;
1892 ispadi = poep->poep_code == POECODE_PADI;
1893 opoe = poe_mkheader(outp, ispadi ? POECODE_PADO : POECODE_PADS, 0);
1895 *srvp = NULL;
1896 if (cur_options == NULL)
1897 return (0);
1899 /* Search for named device (lower stream) in tables. */
1900 dep = cur_options->os_devices;
1901 depe = dep + cur_options->os_ndevices;
1902 wdep = NULL;
1903 if ((cp = strchr(iname, ':')) != NULL)
1904 tlen = cp - iname;
1905 else
1906 tlen = strlen(iname);
1907 for (; dep < depe; dep++)
1908 if (strncmp(iname, dep->de_name, tlen) == 0 &&
1909 dep->de_name[tlen] == '\0')
1910 break;
1911 else if (dep->de_name[0] == '*' && dep->de_name[1] == '\0')
1912 wdep = dep;
1913 if (dep >= depe)
1914 dep = wdep;
1916 * Return if interface not found. Zero-service case can't
1917 * occur, since devices with no services aren't included in
1918 * the list, but the code is just being safe here.
1920 if (dep == NULL || dep->de_services == NULL || dep->de_nservices <= 0)
1921 return (0);
1924 * Loop over tags in client message and process them.
1925 * Services must be matched against our list. Host-Uniq and
1926 * Relay-Session-Id must be copied to the reply. All others
1927 * must be discarded.
1929 nsvcs = 0;
1930 sepp = dep->de_services;
1931 tagp = (const uint8_t *)(poep + 1);
1932 while (poe_tagcheck(poep, plen, tagp)) {
1933 ttyp = POET_GET_TYPE(tagp);
1934 if (ttyp == POETT_END)
1935 break;
1936 tlen = POET_GET_LENG(tagp);
1937 switch (ttyp) {
1938 case POETT_SERVICE: /* Service-Name */
1940 * Allow only one. (Note that this test works
1941 * because there's always at least one service
1942 * per device; otherwise, the device is
1943 * removed from the list.)
1945 if (sepp != dep->de_services) {
1946 if (nsvcs != -1)
1947 (void) poe_add_str(opoe, POETT_NAMERR,
1948 "Too many Service-Name tags");
1949 nsvcs = -1;
1950 break;
1952 seppe = sepp + dep->de_nservices;
1953 if (tlen == 0) {
1955 * If config specifies "nowild" in a
1956 * global context, then we don't
1957 * respond to wildcard PADRs. The
1958 * client must know the exact service
1959 * name to get access.
1962 if (!ispadi && (glob_svc.se_flags & SEF_NOWILD))
1963 sepp = seppe;
1964 while (sepp < seppe) {
1965 sep = *sepp++;
1966 if (sep->se_name[0] == '\0' ||
1967 (sep->se_flags & SEF_NOWILD) ||
1968 !allow_service(sep, pap))
1969 continue;
1970 *srvp = (void *)sep;
1972 * RFC requires that PADO includes the
1973 * wildcard service request in response
1974 * to PADI.
1976 if (ispadi && nsvcs == 0 &&
1977 !(glob_svc.se_flags & SEF_NOWILD))
1978 (void) poe_tag_copy(opoe, tagp);
1979 nsvcs++;
1980 (void) poe_add_str(opoe, POETT_SERVICE,
1981 sep->se_name);
1982 /* If PADR, then one is enough */
1983 if (!ispadi)
1984 break;
1986 /* Just for generating error messages */
1987 if (nsvcs == 0)
1988 (void) poe_tag_copy(opoe, tagp);
1989 } else {
1991 * Clients's requested service must appear in
1992 * reply.
1994 (void) poe_tag_copy(opoe, tagp);
1996 /* Requested specific service; find it. */
1997 cp = (char *)POET_DATA(tagp);
1998 while (sepp < seppe) {
1999 sep = *sepp++;
2000 if (strlen(sep->se_name) == tlen &&
2001 strncasecmp(sep->se_name, cp,
2002 tlen) == 0) {
2003 if (allow_service(sep, pap)) {
2004 nsvcs++;
2005 *srvp = (void *)sep;
2007 break;
2012 * Allow service definition to override
2013 * AC-Name (concentrator [server] name) field.
2015 if (*srvp != NULL) {
2016 sep = (const struct service_entry *)*srvp;
2017 log_for_service(sep->se_log, sep->se_debug);
2018 str = "Solaris PPPoE";
2019 if (sep->se_server != NULL)
2020 str = sep->se_server;
2021 (void) poe_add_str(opoe, POETT_ACCESS, str);
2023 break;
2024 /* Ones we should discard */
2025 case POETT_ACCESS: /* AC-Name */
2026 case POETT_COOKIE: /* AC-Cookie */
2027 case POETT_NAMERR: /* Service-Name-Error */
2028 case POETT_SYSERR: /* AC-System-Error */
2029 case POETT_GENERR: /* Generic-Error */
2030 case POETT_HURL: /* Host-URL */
2031 case POETT_MOTM: /* Message-Of-The-Minute */
2032 case POETT_RTEADD: /* IP-Route-Add */
2033 case POETT_VENDOR: /* Vendor-Specific */
2034 case POETT_MULTI: /* Multicast-Capable */
2035 default:
2036 break;
2037 /* Ones we should copy */
2038 case POETT_UNIQ: /* Host-Uniq */
2039 case POETT_RELAY: /* Relay-Session-Id */
2040 (void) poe_tag_copy(opoe, tagp);
2041 break;
2043 tagp = POET_NEXT(tagp);
2045 return (nsvcs);
2049 * Like fgetc, but reads from a string.
2051 static int
2052 sgetc(void *arg)
2054 char **cpp = (char **)arg;
2055 if (**cpp == '\0')
2056 return (EOF);
2057 return (*(*cpp)++);
2061 * Given a service structure, launch pppd. Called by handle_input()
2062 * in pppoed.c if locate_service() [above] finds exactly one service
2063 * matching a PADR.
2066 launch_service(int tunfd, poep_t *poep, void *srvp, struct ppptun_control *ptc)
2068 const struct service_entry *sep = (const struct service_entry *)srvp;
2069 const char *path;
2070 const char *extra;
2071 const char *pppd;
2072 const char *cp;
2073 pid_t pidv;
2074 int newtun;
2075 struct ppptun_peer ptp;
2076 union ppptun_name ptn;
2077 const char *args[MAXARGS];
2078 struct strbuf ctrl;
2079 struct strbuf data;
2080 const char **cpp;
2081 char *sptr;
2082 char *spv;
2083 int slen;
2084 int retv;
2085 char keybuf[MAX_KEYWORD];
2087 assert(sep != NULL);
2089 /* Get tunnel driver connection for new PPP session. */
2090 newtun = open(tunnam, O_RDWR);
2091 if (newtun == -1)
2092 goto syserr;
2094 /* Set this session up for standard PPP and client's address. */
2095 (void) memset(&ptp, '\0', sizeof (ptp));
2096 ptp.ptp_style = PTS_PPPOE;
2097 ptp.ptp_address = ptc->ptc_address;
2098 if (strioctl(newtun, PPPTUN_SPEER, &ptp, sizeof (ptp), sizeof (ptp)) <
2100 goto syserr;
2101 ptp.ptp_rsessid = ptp.ptp_lsessid;
2102 if (strioctl(newtun, PPPTUN_SPEER, &ptp, sizeof (ptp), sizeof (ptp)) <
2104 goto syserr;
2106 /* Attach the requested lower stream. */
2107 cp = strchr(ptc->ptc_name, ':');
2108 if (cp == NULL)
2109 cp = ptc->ptc_name + strlen(ptc->ptc_name);
2110 (void) snprintf(ptn.ptn_name, sizeof (ptn.ptn_name), "%.*s:pppoe",
2111 cp-ptc->ptc_name, ptc->ptc_name);
2112 if (strioctl(newtun, PPPTUN_SDATA, &ptn, sizeof (ptn), 0) < 0)
2113 goto syserr;
2114 (void) snprintf(ptn.ptn_name, sizeof (ptn.ptn_name), "%.*s:pppoed",
2115 cp-ptc->ptc_name, ptc->ptc_name);
2116 if (strioctl(newtun, PPPTUN_SCTL, &ptn, sizeof (ptn), 0) < 0)
2117 goto syserr;
2119 pidv = fork();
2120 if (pidv == (pid_t)-1)
2121 goto syserr;
2123 if (pidv == (pid_t)0) {
2125 * Use syslog only in order to avoid mixing log messages
2126 * in regular files.
2128 close_log_files();
2130 if ((path = sep->se_path) == NULL)
2131 path = "/usr/bin/pppd";
2132 if ((extra = sep->se_extra) == NULL)
2133 extra = "plugin pppoe.so directtty";
2134 if ((pppd = sep->se_pppd) == NULL)
2135 pppd = "";
2137 /* Concatenate these. */
2138 slen = strlen(path) + strlen(extra) + strlen(pppd) + 3;
2139 if ((sptr = (char *)malloc(slen)) == NULL)
2140 goto bail_out;
2141 (void) strcpy(sptr, path);
2142 (void) strcat(sptr, " ");
2143 (void) strcat(sptr, extra);
2144 (void) strcat(sptr, " ");
2145 (void) strcat(sptr, pppd);
2147 /* Parse out into arguments */
2148 cpp = args;
2149 spv = sptr;
2150 while (cpp < args + MAXARGS - 1) {
2151 retv = getkeyword(NULL, keybuf, sizeof (keybuf), sgetc,
2152 (void *)&spv, 1);
2153 if (retv != 1)
2154 *cpp++ = strsave(keybuf);
2155 if (retv != 0)
2156 break;
2158 *cpp = NULL;
2159 if (cpp == args)
2160 goto bail_out;
2163 * Fix tunnel device on stdin/stdout and error file on
2164 * stderr.
2166 if (newtun != 0 && dup2(newtun, 0) < 0)
2167 goto bail_out;
2168 if (newtun != 1 && dup2(newtun, 1) < 0)
2169 goto bail_out;
2170 if (newtun > 1)
2171 (void) close(newtun);
2172 if (tunfd > 1)
2173 (void) close(tunfd);
2174 (void) close(2);
2175 (void) open("/etc/ppp/pppoe-errors", O_WRONLY | O_APPEND |
2176 O_CREAT, 0600);
2179 * Change GID first, for obvious reasons. Note that
2180 * we log any problems to syslog, not the errors file.
2181 * The errors file is intended for problems in the
2182 * exec'd program.
2184 if ((sep->se_flags & SEF_GIDSET) &&
2185 setgid(sep->se_gid) == -1) {
2186 cp = mystrerror(errno);
2187 reopen_log();
2188 logerr("setgid(%d): %s", sep->se_gid, cp);
2189 goto logged;
2191 if ((sep->se_flags & SEF_UIDSET) &&
2192 setuid(sep->se_uid) == -1) {
2193 cp = mystrerror(errno);
2194 reopen_log();
2195 logerr("setuid(%d): %s", sep->se_uid, cp);
2196 goto logged;
2199 /* Run pppd */
2200 path = args[0];
2201 cp = strrchr(args[0], '/');
2202 if (cp != NULL && cp[1] != '\0')
2203 args[0] = cp+1;
2204 errno = 0;
2205 (void) execv(path, (char * const *)args);
2206 newtun = 0;
2209 * Exec failure; attempt to log the problem and send a
2210 * PADT to the client so that it knows the session
2211 * went south.
2213 bail_out:
2214 cp = mystrerror(errno);
2215 reopen_log();
2216 logerr("\"%s\": %s", (sptr == NULL ? path : sptr), cp);
2217 logged:
2218 poep = poe_mkheader(pkt_output, POECODE_PADT, ptp.ptp_lsessid);
2219 poep->poep_session_id = htons(ptp.ptp_lsessid);
2220 (void) poe_add_str(poep, POETT_SYSERR, cp);
2221 (void) sleep(1);
2222 ctrl.len = sizeof (*ptc);
2223 ctrl.buf = (caddr_t)ptc;
2224 data.len = poe_length(poep) + sizeof (*poep);
2225 data.buf = (caddr_t)poep;
2226 if (putmsg(newtun, &ctrl, &data, 0) < 0) {
2227 logerr("putmsg %s: %s", ptc->ptc_name,
2228 mystrerror(errno));
2230 exit(1);
2233 (void) close(newtun);
2235 /* Give session ID to client in reply. */
2236 poep->poep_session_id = htons(ptp.ptp_lsessid);
2237 return (1);
2239 syserr:
2240 /* Peer doesn't know session ID yet; hope for the best. */
2241 retv = errno;
2242 if (newtun >= 0)
2243 (void) close(newtun);
2244 (void) poe_add_str(poep, POETT_SYSERR, mystrerror(retv));
2245 return (0);
2249 * This is pretty awful -- it uses recursion to print a simple list.
2250 * It's just for debug, though, and does a reasonable job of printing
2251 * the filters in the right order.
2253 static void
2254 print_filter_list(FILE *fp, struct filter_entry *fep)
2256 if (fep->fe_prev != NULL)
2257 print_filter_list(fp, fep->fe_prev);
2258 (void) fprintf(fp, "\t\t MAC %s", ehost2(&fep->fe_mac));
2259 (void) fprintf(fp, ", mask %s%s\n", ehost2(&fep->fe_mask),
2260 (fep->fe_isexcept ? ", except" : ""));
2264 * Write summary of parsed configuration data to given file.
2266 void
2267 dump_configuration(FILE *fp)
2269 const struct device_entry *dep;
2270 const struct service_entry *sep, **sepp;
2271 struct per_file *pfp;
2272 int i, j;
2274 (void) fprintf(fp, "Will%s respond to wildcard queries.\n",
2275 (glob_svc.se_flags & SEF_NOWILD) ? " not" : "");
2276 (void) fprintf(fp,
2277 "Global debug level %d, log to %s; current level %d\n",
2278 glob_svc.se_debug,
2279 ((glob_svc.se_log == NULL || *glob_svc.se_log == '\0') ?
2280 "syslog" : glob_svc.se_log),
2281 log_level);
2282 if (cur_options == NULL) {
2283 (void) fprintf(fp, "No current configuration.\n");
2284 return;
2286 (void) fprintf(fp, "Current configuration:\n");
2287 (void) fprintf(fp, " %d device(s):\n", cur_options->os_ndevices);
2288 dep = cur_options->os_devices;
2289 for (i = 0; i < cur_options->os_ndevices; i++, dep++) {
2290 (void) fprintf(fp, "\t%s: %d service(s):\n",
2291 dep->de_name, dep->de_nservices);
2292 sepp = dep->de_services;
2293 for (j = 0; j < dep->de_nservices; j++, sepp++) {
2294 sep = *sepp;
2295 (void) fprintf(fp, "\t %s: debug level %d",
2296 sep->se_name, sep->se_debug);
2297 if (sep->se_flags & SEF_UIDSET)
2298 (void) fprintf(fp, ", UID %u", sep->se_uid);
2299 if (sep->se_flags & SEF_GIDSET)
2300 (void) fprintf(fp, ", GID %u", sep->se_gid);
2301 if (sep->se_flags & SEF_WILD)
2302 (void) fprintf(fp, ", wildcard");
2303 else if (sep->se_flags & SEF_NOWILD)
2304 (void) fprintf(fp, ", nowildcard");
2305 else
2306 (void) fprintf(fp, ", wildcard (default)");
2307 (void) putc('\n', fp);
2308 if (sep->se_server != NULL)
2309 (void) fprintf(fp, "\t\tserver \"%s\"\n",
2310 sep->se_server);
2311 if (sep->se_pppd != NULL)
2312 (void) fprintf(fp, "\t\tpppd \"%s\"\n",
2313 sep->se_pppd);
2314 if (sep->se_path != NULL)
2315 (void) fprintf(fp, "\t\tpath \"%s\"\n",
2316 sep->se_path);
2317 if (sep->se_extra != NULL)
2318 (void) fprintf(fp, "\t\textra \"%s\"\n",
2319 sep->se_extra);
2320 if (sep->se_log != NULL)
2321 (void) fprintf(fp, "\t\tlog \"%s\"\n",
2322 sep->se_log);
2323 if (sep->se_flist != NULL) {
2324 (void) fprintf(fp, "\t\tfilter list:\n");
2325 print_filter_list(fp, sep->se_flist);
2329 (void) fprintf(fp, "\nConfiguration read from:\n");
2330 for (pfp = cur_options->os_pfjunk; pfp != NULL; pfp = pfp->pf_prev) {
2331 (void) fprintf(fp, " %s: %d service(s)\n", pfp->pf_name,
2332 pfp->pf_nsvc);