1%{
2/*	$NetBSD: nsparser.y,v 1.3 1999/01/25 00:16:18 lukem Exp $	*/
3
4/*-
5 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6 *
7 * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
8 * All rights reserved.
9 *
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Luke Mewburn.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD$");
37
38#include "namespace.h"
39#define _NS_PRIVATE
40#include <nsswitch.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <syslog.h>
45#include "un-namespace.h"
46
47static	void	_nsaddsrctomap(const char *);
48
49static	ns_dbt		curdbt;
50static	ns_src		cursrc;
51%}
52
53%union {
54	char *str;
55	int   mapval;
56}
57
58%token	NL
59%token	SUCCESS UNAVAIL NOTFOUND TRYAGAIN
60%token	RETURN CONTINUE
61%token  ERRORTOKEN
62%token	<str> STRING
63
64%type	<mapval> Status Action
65
66%%
67
68File
69	:	/* empty */
70	| Lines
71	;
72
73Lines
74	: Entry
75	| Lines Entry
76	;
77
78Entry
79	: NL
80	| Database ':' NL
81		{
82			free((char*)curdbt.name);
83		}
84	| Database ':' Srclist NL
85		{
86			_nsdbtput(&curdbt);
87		}
88	| error NL
89		{
90			yyerrok;
91		}
92	;
93
94Database
95	: STRING
96		{
97			curdbt.name = yylval.str;
98			curdbt.srclist = NULL;
99			curdbt.srclistsize = 0;
100		}
101	;
102
103Srclist
104	: Item
105	| Srclist Item
106	;
107
108Item
109	: STRING
110		{
111			cursrc.flags = NS_TERMINATE;
112			_nsaddsrctomap($1);
113		}
114	| STRING '[' { cursrc.flags = NS_SUCCESS; } Criteria ']'
115		{
116			_nsaddsrctomap($1);
117		}
118	;
119
120Criteria
121	: Criterion
122	| Criteria Criterion
123	;
124
125Criterion
126	: Status '=' Action
127		{
128			if ($3)	     /* if action == RETURN set RETURN bit */
129				cursrc.flags |= $1;
130			else	     /* else unset it */
131				cursrc.flags &= ~$1;
132		}
133	;
134
135Status
136	: SUCCESS	{ $$ = NS_SUCCESS; }
137	| UNAVAIL	{ $$ = NS_UNAVAIL; }
138	| NOTFOUND	{ $$ = NS_NOTFOUND; }
139	| TRYAGAIN	{ $$ = NS_TRYAGAIN; }
140	;
141
142Action
143	: RETURN	{ $$ = NS_ACTION_RETURN; }
144	| CONTINUE	{ $$ = NS_ACTION_CONTINUE; }
145	;
146
147%%
148
149static void
150_nsaddsrctomap(const char *elem)
151{
152	int		i, lineno;
153	extern int	_nsyylineno;
154	extern char *	_nsyytext;
155
156	lineno = _nsyylineno - (*_nsyytext == '\n' ? 1 : 0);
157	if (curdbt.srclistsize > 0) {
158		if (((strcasecmp(elem, NSSRC_COMPAT) == 0) &&
159		    (strcasecmp(curdbt.srclist[0].name, NSSRC_CACHE) != 0)) ||
160		    (strcasecmp(curdbt.srclist[0].name, NSSRC_COMPAT) == 0)) {
161			syslog(LOG_ERR,
162	    "NSSWITCH(nsparser): %s line %d: 'compat' used with sources, other than 'cache'",
163			    _PATH_NS_CONF, lineno);
164			free((void*)elem);
165			return;
166		}
167	}
168	for (i = 0; i < curdbt.srclistsize; i++) {
169		if (strcasecmp(curdbt.srclist[i].name, elem) == 0) {
170			syslog(LOG_ERR,
171		       "NSSWITCH(nsparser): %s line %d: duplicate source '%s'",
172			    _PATH_NS_CONF, lineno, elem);
173			free((void*)elem);
174			return;
175		}
176	}
177	cursrc.name = elem;
178	_nsdbtaddsrc(&curdbt, &cursrc);
179}
180