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]
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.
35 #include <sys/types.h>
43 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <netinet/if_ether.h>
47 #include <net/sppptun.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
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
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
{
113 const struct service_entry
**de_services
;
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
;
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
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 */
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.
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 */
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.
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. */
208 strsave(const char *str
)
214 newstr
= (char *)malloc(strlen(str
) + 1);
216 (void) strcpy(newstr
, str
);
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.
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
;
236 cfile
= psp
->ps_cfile
;
238 /* If no current file, then nothing to close. */
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
;
251 for (fep
= sep
->se_flist
; fep
!= NULL
; fep
= fep
->fe_prev
)
252 if (fep
->fe_prev
== NULL
|| fep
->fe_prevcopy
) {
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
266 /* We don't inherit from ourselves. */
267 cfile
= cfile
->pf_prev
;
268 while (cfile
!= 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
) {
275 cfile
->pf_global
.sl_entry
.se_flist
;
276 fep
->fe_prevcopy
= 1;
278 cfile
= cfile
->pf_prev
;
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
;
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
&
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 */
347 free_device_list(struct device_list
*dlp
)
349 struct device_list
*dln
;
351 while (dlp
!= NULL
) {
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.
364 set_service(struct service_list
*slp
, const char *str
)
366 struct parse_state
*psp
;
367 struct per_file
*cfile
;
369 /* Finish current service */
372 /* Start new service */
374 slp
= (struct service_list
*)calloc(sizeof (*slp
) + strlen(str
) + 1,
377 logerr("no memory for service \"%s\"", str
);
381 /* Add to end of list */
382 cfile
= psp
->ps_cfile
;
383 if (cfile
->pf_svc_last
== NULL
)
386 cfile
->pf_svc_last
->sl_next
= slp
;
387 cfile
->pf_svc_last
= slp
;
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
++;
397 /* This is now the current service that we're defining. */
403 * Handle both "wildcard" and "nowildcard" options.
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
);
415 slp
->sl_entry
.se_flags
=
416 (slp
->sl_entry
.se_flags
& ~(SEF_WILD
|SEF_NOWILD
)) |
417 (*str
== 'n' ? SEF_NOWILD
: SEF_WILD
);
422 * Handle "debug" option.
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
;
436 * Handle "nodebug" option.
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
;
451 * Handle all plain string options; "server", "pppd", "path", "extra",
455 set_string(struct service_list
*slp
, const char *str
)
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
) {
463 cpp
= &slp
->sl_entry
.se_server
;
466 cpp
= &slp
->sl_entry
.se_pppd
;
469 cpp
= &slp
->sl_entry
.se_path
;
472 cpp
= &slp
->sl_entry
.se_extra
;
475 cpp
= &slp
->sl_entry
.se_log
;
487 * Handle "file <name>" option. Close out current service (if any)
488 * and begin parsing from new file.
491 set_file(struct service_list
*slp
, const char *str
)
494 struct per_file
*pfp
;
495 struct parse_state
*psp
;
499 if ((fp
= fopen(str
, "r")) == NULL
) {
500 logwarn("%s: %s: %s", slp
->sl_parse
->ps_cfile
->pf_name
, str
,
504 pfp
= (struct per_file
*)calloc(sizeof (*pfp
) + strlen(str
) + 1, 1);
506 logerr("no memory for parsing file %s", str
);
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
);
517 pfp
->pf_prev
= psp
->ps_cfile
;
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>";
528 * Handle "device <list>" option.
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
;
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
);
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
;
555 while (isspace(*str
) || *str
== ',')
560 while (*str
!= '\0' && !isspace(*str
) && *str
!= ',')
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
);
569 dln
= (struct device_list
*)malloc(sizeof (*dln
) +
572 logerr("no memory for device name");
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
,
582 dlpp
= &dln
->dl_next
;
587 if (!(slp
->sl_entry
.se_flags
& SEF_CDEV
))
588 while (*dlpp
!= NULL
)
589 dlpp
= &(*dlpp
)->dl_next
;
591 slp
->sl_entry
.se_flags
&= ~SEF_CDEV
;
598 * Handle <list> portion of "client [except] <list>" option. Attach
599 * to list of filters in reverse order.
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
;
609 char hbuf
[MAXHOSTNAMELEN
];
610 struct ether_addr ea
;
611 struct ether_addr mask
;
616 fep
= slp
->sl_entry
.se_flist
;
618 while (isspace(*str
) || *str
== ',')
623 while (*str
!= '\0' && !isspace(*str
) && *str
!= ',')
626 (void) memcpy(hbuf
, cp
, len
);
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
;
633 if (ucp
>= ea
.ether_addr_octet
+ sizeof (ea
))
641 *ucp
= hexdecode(*cp
++);
642 if (cp
< str
&& isxdigit(*cp
)) {
650 if (*cp
!= ':' || cp
+ 1 == str
)
656 logerr("%s: illegal Ethernet address %.*s",
657 psp
->ps_cfile
->pf_name
, len
, cp
);
661 fen
= (struct filter_entry
*)malloc(sizeof (*fen
));
663 logerr("unable to allocate memory for filter");
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
));
673 slp
->sl_entry
.se_flist
= fep
;
678 * Handle "user <name>" option.
681 set_user(struct service_list
*slp
, const char *str
)
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
);
697 slp
->sl_entry
.se_uid
= uid
;
702 logdbg("%s: not root; ignoring attempt to set UID %d (%s)",
703 slp
->sl_parse
->ps_cfile
->pf_name
, uid
, str
);
706 slp
->sl_entry
.se_flags
|= SEF_UIDSET
;
711 * Handle "group <name>" option.
714 set_group(struct service_list
*slp
, const char *str
)
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
);
730 slp
->sl_entry
.se_gid
= gid
;
732 logdbg("%s: not root; ignoring attempt to set GID %d (%s)",
733 slp
->sl_parse
->ps_cfile
->pf_name
, gid
, str
);
736 slp
->sl_entry
.se_flags
|= SEF_GIDSET
;
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.
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
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
);
801 * Handle end-of-file processing -- close service, close file, revert
802 * to global context in previous include file nest level.
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
;
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.
839 dispatch_keyword(struct parse_state
*psp
, const char *keybuf
)
841 const struct kw_entry
*kep
;
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
;
859 if (strchr(keybuf
, '=') != NULL
) {
860 if ((cp
= strsave(keybuf
)) == NULL
) {
861 logerr("no memory to save %s", keybuf
);
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");
874 evlist
[0] = evlist
[1] = NULL
;
876 while ((env
= *evlist
) != NULL
) {
877 if (strncmp(cp
, env
, len
) == 0)
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",
891 psp
->ps_evlist
= evlist
;
892 evlist
+= psp
->ps_evsize
- 1;
897 logdbg("setenv \"%s\"", cp
);
902 logerr("%s: unknown keyword '%s'", psp
->ps_cfile
->pf_name
, keybuf
);
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.
913 my_getenv(struct parse_state
*psp
, char *estr
)
918 if (psp
!= NULL
&& (evlist
= psp
->ps_evlist
) != NULL
) {
920 while ((ent
= *evlist
++) != NULL
) {
921 if (strncmp(ent
, estr
, elen
) == 0 &&
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.
935 env_replace(struct parse_state
*psp
, char *keybuf
, char kwstate
)
940 if ((cp
= strrchr(keybuf
, kwstate
)) != NULL
) {
941 if ((cpe
= my_getenv(psp
, cp
+ 1)) != NULL
) {
943 (void) strncat(cp
, cpe
,
944 MAX_KEYWORD
- (cp
- keybuf
) - 1);
945 keybuf
[MAX_KEYWORD
- 1] = '\0';
948 logerr("unknown variable \"%s\"", cp
+ 1);
951 /* Should not occur. */
952 cp
= keybuf
+ strlen(keybuf
);
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.
978 * 0 - keyword parsed.
979 * 1 - end of file; no keyword.
980 * 2 - end of file after this keyword.
983 getkeyword(struct parse_state
*psp
, char *keybuf
, int keymax
,
984 int (*nextchr
)(void *), void *arg
, int flag
)
986 char varnest
[MAX_NEST
];
992 static const char escstr
[] = "a\ab\bf\fn\nr\r";
995 keymax
--; /* Account for trailing NUL byte */
1001 ichr
= (*nextchr
)(arg
);
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. */
1011 else if (isalpha(chr
) &&
1012 (cp
= strchr(escstr
, chr
)) != NULL
)
1014 /* Revert to previous state */
1027 case '"': /* In double-quote string */
1028 if (!flag
&& chr
== '$') {
1029 /* Handle variable expansion. */
1035 case '\'': /* In single-quote string */
1037 /* Handle start of escape sequence */
1038 kwstate
= kwstate
== '"' ? '|' : '~';
1042 if (chr
== kwstate
) {
1043 /* End of quoted string; revert to normal */
1048 case '$': /* Start of unquoted variable name */
1049 case '%': /* Start of variable name in quoted string */
1051 /* Variable name is bracketed. */
1053 kwstate
== '$' ? '{' : '[';
1056 *kbp
++ = kwstate
= kwstate
== '$' ? '+' : '*';
1058 case '+': /* Gathering unquoted variable name */
1059 case '*': /* Gathering variable name in quoted string */
1061 vnp
< varnest
+ sizeof (varnest
)) {
1067 if (!isalnum(chr
) && chr
!= '_' &&
1068 chr
!= '.' && chr
!= '-') {
1070 kbp
= env_replace(psp
, keybuf
, kwstate
);
1074 kwstate
= kwstate
== '+' ?
1076 /* Go reinterpret in new context */
1080 case '{': /* Gathering bracketed, unquoted var name */
1081 case '[': /* Gathering bracketed, quoted var name */
1084 kbp
= env_replace(psp
, keybuf
, kwstate
);
1085 kwstate
= kwstate
== '{' ? 'A' : '"';
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' : ' ';
1099 case '\0': /* Initial state; no word seen yet. */
1100 if (ichr
== EOF
|| isspace(chr
)) {
1101 chr
= '\0'; /* Skip over leading spaces */
1106 chr
= '\0'; /* Skip over comments */
1109 /* Start of keyword seen. */
1112 default: /* Middle of keyword parsing. */
1115 if (isspace(chr
)) { /* Space terminates word */
1119 if (chr
== '"' || chr
== '\'' || chr
== '\\') {
1120 kwstate
= chr
; /* Begin quote or escape */
1124 if (flag
) /* Allow ignore; for string reparse */
1126 if (chr
== '#') { /* Comment terminates word */
1127 kwstate
= '@'; /* Must consume comment also */
1132 kwstate
= '$'; /* Begin variable expansion */
1138 * If we've reached a space at the end of the word,
1141 if (ichr
== EOF
|| kwstate
== ' ')
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
)
1154 return (kwstate
== '\0' ? 1 : 2);
1160 * Fetch words from current file until all files are closed. Handles
1164 parse_from_file(struct parse_state
*psp
)
1166 char keybuf
[MAX_KEYWORD
];
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
,
1175 (void) dispatch_keyword(psp
, keybuf
);
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.
1188 parse_file(struct parse_state
*psp
, const char *fname
)
1192 /* It's ok if any of these files are missing. */
1193 if (stat(fname
, &sb
) == -1 && errno
== ENOENT
)
1195 if (set_file(psp
->ps_csvc
, fname
) == 0)
1196 parse_from_file(psp
);
1200 * Dispatch keywords from command line. Handles any files included
1204 parse_arg_list(struct parse_state
*psp
, int argc
, char **argv
)
1206 /* The first argument (program name) can be null. */
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 */
1218 count_devs(struct device_list
*dlp
)
1223 for (; dlp
!= NULL
; dlp
= dlp
->dl_next
)
1228 /* Count number of devices named in entire file. */
1230 count_per_file(struct per_file
*pfp
)
1232 struct service_list
*slp
;
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
);
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
;
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
);
1268 /* Compare device names; used with qsort */
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
)
1283 const char **dnames
;
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
);
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);
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)
1311 /* Return array of pointers to names. */
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
;
1330 const char **dnames
;
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
;
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.
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
)
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");
1379 for (dnp
= dnames
; *dnp
!= NULL
; dnp
++)
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
));
1390 logerr("unable to allocate option state structure");
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
;
1408 for (slp
= osp
->os_pfjunk
->pf_svc
; slp
!= NULL
;
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
;
1416 if ((*se2pp
)->se_name
==
1417 slp
->sl_entry
.se_name
||
1419 se_name
, slp
->sl_entry
.
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
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
-
1439 /* Ignore devices having no services at all. */
1440 if (dep
->de_nservices
> 0)
1443 /* Count up the devices. */
1444 osp
->os_ndevices
= dep
- osp
->os_devices
;
1445 /* Free the list of device names */
1451 * Free storage unique to a given service. Pointers copied from other
1452 * services are ignored.
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
;
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.
1486 free_service_list(struct service_list
*slp
)
1488 struct service_list
*sln
;
1490 while (slp
!= NULL
) {
1499 * Free a linked list of files and all services in those files.
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
);
1516 * Free an array of local environment variables.
1519 free_env_list(char **evlist
)
1524 if ((evp
= evlist
) != NULL
) {
1525 while ((env
= *evp
++) != NULL
)
1532 * Add a new device (lower stream) to the list for which we're the
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",
1542 if (strioctl(tunfd
, PPPTUN_SCTL
, &ptn
, sizeof (ptn
), 0) < 0) {
1543 logerr("PPPTUN_SCTL %s: %s", ptn
.ptn_name
, mystrerror(errno
));
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.
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",
1560 if (strioctl(tunfd
, PPPTUN_DCTL
, &ptn
, sizeof (ptn
), 0) < 0) {
1561 logerr("PPPTUN_DCTL %s: %s", ptn
.ptn_name
, mystrerror(errno
));
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.
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
;
1580 union ppptun_name ptn
;
1583 /* First pass; just allocate space for all *:pppoe* devices */
1584 dlpp
= &psp
->ps_star
;
1586 for (i
= 0; ; i
++) {
1588 if (strioctl(tunfd
, PPPTUN_GNNAME
, &ptn
, sizeof (ptn
),
1589 sizeof (ptn
)) < 0) {
1590 logerr("PPPTUN_GNNAME %d: %s", i
, mystrerror(errno
));
1593 if (ptn
.ptn_name
[0] == '\0')
1595 if ((cp
= strchr(ptn
.ptn_name
, ':')) == NULL
||
1596 strncmp(cp
, ":pppoe", 6) != 0 ||
1597 (cp
[6] != '\0' && strcmp(cp
+6, "d") != 0))
1600 dlp
= (struct device_list
*)malloc(sizeof (*dlp
) +
1601 strlen(ptn
.ptn_name
) + 1);
1604 dlp
->dl_name
= (const char *)(dlp
+ 1);
1605 (void) strcpy((char *)(dlp
+ 1), ptn
.ptn_name
);
1606 if (cp
[6] == '\0') {
1608 dlpp
= &dlp
->dl_next
;
1611 dl2pp
= &dlp
->dl_next
;
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)
1623 *dlpp
= dlp
->dl_next
;
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);
1634 dlp
->dl_name
= (const char *)(dlp
+ 1);
1635 (void) strcpy((char *)(dlp
+ 1), "*");
1636 dlp
->dl_next
= psp
->ps_star
;
1642 * Set logging subsystem back to configured global default values.
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
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
;
1663 const struct device_entry
*newdep
, *newmax
;
1664 const struct device_entry
*olddep
, *oldmax
;
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
) {
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
;
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
;
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
;
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
;
1764 newdep
= newmax
= NULL
;
1766 if (cur_options
!= NULL
) {
1767 olddep
= cur_options
->os_devices
;
1768 oldmax
= olddep
+ cur_options
->os_ndevices
;
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
);
1779 cmpval
= strcmp(newdep
->de_name
,
1782 /* Brand new device seen. */
1783 add_new_dev(tunfd
, newdep
->de_name
);
1785 } else if (cmpval
== 0) {
1786 /* Existing device; skip it. */
1790 /* No else clause -- removal is below */
1793 if (olddep
< oldmax
) {
1794 if (newdep
>= newmax
) {
1795 rem_old_dev(tunfd
, olddep
->de_name
);
1798 cmpval
= strcmp(newdep
->de_name
,
1801 /* Old device is gone */
1802 rem_old_dev(tunfd
, olddep
->de_name
);
1804 } else if (cmpval
== 0) {
1805 /* Existing device; skip it. */
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
);
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.
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
;
1837 const uchar_t
*macp
;
1838 const uchar_t
*maskp
;
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)
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
1876 locate_service(poep_t
*poep
, int plen
, const char *iname
, ppptun_atype
*pap
,
1877 uint32_t *outp
, void **srvp
)
1880 const uint8_t *tagp
;
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
;
1892 ispadi
= poep
->poep_code
== POECODE_PADI
;
1893 opoe
= poe_mkheader(outp
, ispadi
? POECODE_PADO
: POECODE_PADS
, 0);
1896 if (cur_options
== NULL
)
1899 /* Search for named device (lower stream) in tables. */
1900 dep
= cur_options
->os_devices
;
1901 depe
= dep
+ cur_options
->os_ndevices
;
1903 if ((cp
= strchr(iname
, ':')) != NULL
)
1906 tlen
= strlen(iname
);
1907 for (; dep
< depe
; dep
++)
1908 if (strncmp(iname
, dep
->de_name
, tlen
) == 0 &&
1909 dep
->de_name
[tlen
] == '\0')
1911 else if (dep
->de_name
[0] == '*' && dep
->de_name
[1] == '\0')
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)
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.
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
)
1936 tlen
= POET_GET_LENG(tagp
);
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
) {
1947 (void) poe_add_str(opoe
, POETT_NAMERR
,
1948 "Too many Service-Name tags");
1952 seppe
= sepp
+ dep
->de_nservices
;
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
))
1964 while (sepp
< seppe
) {
1966 if (sep
->se_name
[0] == '\0' ||
1967 (sep
->se_flags
& SEF_NOWILD
) ||
1968 !allow_service(sep
, pap
))
1970 *srvp
= (void *)sep
;
1972 * RFC requires that PADO includes the
1973 * wildcard service request in response
1976 if (ispadi
&& nsvcs
== 0 &&
1977 !(glob_svc
.se_flags
& SEF_NOWILD
))
1978 (void) poe_tag_copy(opoe
, tagp
);
1980 (void) poe_add_str(opoe
, POETT_SERVICE
,
1982 /* If PADR, then one is enough */
1986 /* Just for generating error messages */
1988 (void) poe_tag_copy(opoe
, tagp
);
1991 * Clients's requested service must appear in
1994 (void) poe_tag_copy(opoe
, tagp
);
1996 /* Requested specific service; find it. */
1997 cp
= (char *)POET_DATA(tagp
);
1998 while (sepp
< seppe
) {
2000 if (strlen(sep
->se_name
) == tlen
&&
2001 strncasecmp(sep
->se_name
, cp
,
2003 if (allow_service(sep
, pap
)) {
2005 *srvp
= (void *)sep
;
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
);
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 */
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
);
2043 tagp
= POET_NEXT(tagp
);
2049 * Like fgetc, but reads from a string.
2054 char **cpp
= (char **)arg
;
2061 * Given a service structure, launch pppd. Called by handle_input()
2062 * in pppoed.c if locate_service() [above] finds exactly one service
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
;
2075 struct ppptun_peer ptp
;
2076 union ppptun_name ptn
;
2077 const char *args
[MAXARGS
];
2085 char keybuf
[MAX_KEYWORD
];
2087 assert(sep
!= NULL
);
2089 /* Get tunnel driver connection for new PPP session. */
2090 newtun
= open(tunnam
, O_RDWR
);
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
)) <
2101 ptp
.ptp_rsessid
= ptp
.ptp_lsessid
;
2102 if (strioctl(newtun
, PPPTUN_SPEER
, &ptp
, sizeof (ptp
), sizeof (ptp
)) <
2106 /* Attach the requested lower stream. */
2107 cp
= strchr(ptc
->ptc_name
, ':');
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)
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)
2120 if (pidv
== (pid_t
)-1)
2123 if (pidv
== (pid_t
)0) {
2125 * Use syslog only in order to avoid mixing log messages
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
)
2137 /* Concatenate these. */
2138 slen
= strlen(path
) + strlen(extra
) + strlen(pppd
) + 3;
2139 if ((sptr
= (char *)malloc(slen
)) == NULL
)
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 */
2150 while (cpp
< args
+ MAXARGS
- 1) {
2151 retv
= getkeyword(NULL
, keybuf
, sizeof (keybuf
), sgetc
,
2154 *cpp
++ = strsave(keybuf
);
2163 * Fix tunnel device on stdin/stdout and error file on
2166 if (newtun
!= 0 && dup2(newtun
, 0) < 0)
2168 if (newtun
!= 1 && dup2(newtun
, 1) < 0)
2171 (void) close(newtun
);
2173 (void) close(tunfd
);
2175 (void) open("/etc/ppp/pppoe-errors", O_WRONLY
| O_APPEND
|
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
2184 if ((sep
->se_flags
& SEF_GIDSET
) &&
2185 setgid(sep
->se_gid
) == -1) {
2186 cp
= mystrerror(errno
);
2188 logerr("setgid(%d): %s", sep
->se_gid
, cp
);
2191 if ((sep
->se_flags
& SEF_UIDSET
) &&
2192 setuid(sep
->se_uid
) == -1) {
2193 cp
= mystrerror(errno
);
2195 logerr("setuid(%d): %s", sep
->se_uid
, cp
);
2201 cp
= strrchr(args
[0], '/');
2202 if (cp
!= NULL
&& cp
[1] != '\0')
2205 (void) execv(path
, (char * const *)args
);
2209 * Exec failure; attempt to log the problem and send a
2210 * PADT to the client so that it knows the session
2214 cp
= mystrerror(errno
);
2216 logerr("\"%s\": %s", (sptr
== NULL
? path
: sptr
), cp
);
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
);
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
,
2233 (void) close(newtun
);
2235 /* Give session ID to client in reply. */
2236 poep
->poep_session_id
= htons(ptp
.ptp_lsessid
);
2240 /* Peer doesn't know session ID yet; hope for the best. */
2243 (void) close(newtun
);
2244 (void) poe_add_str(poep
, POETT_SYSERR
, mystrerror(retv
));
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.
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.
2267 dump_configuration(FILE *fp
)
2269 const struct device_entry
*dep
;
2270 const struct service_entry
*sep
, **sepp
;
2271 struct per_file
*pfp
;
2274 (void) fprintf(fp
, "Will%s respond to wildcard queries.\n",
2275 (glob_svc
.se_flags
& SEF_NOWILD
) ? " not" : "");
2277 "Global debug level %d, log to %s; current level %d\n",
2279 ((glob_svc
.se_log
== NULL
|| *glob_svc
.se_log
== '\0') ?
2280 "syslog" : glob_svc
.se_log
),
2282 if (cur_options
== NULL
) {
2283 (void) fprintf(fp
, "No current configuration.\n");
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
++) {
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");
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",
2311 if (sep
->se_pppd
!= NULL
)
2312 (void) fprintf(fp
, "\t\tpppd \"%s\"\n",
2314 if (sep
->se_path
!= NULL
)
2315 (void) fprintf(fp
, "\t\tpath \"%s\"\n",
2317 if (sep
->se_extra
!= NULL
)
2318 (void) fprintf(fp
, "\t\textra \"%s\"\n",
2320 if (sep
->se_log
!= NULL
)
2321 (void) fprintf(fp
, "\t\tlog \"%s\"\n",
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
,