xref: /freebsd/lib/libc/net/nsparser.y (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
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
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 "namespace.h"
36 #define _NS_PRIVATE
37 #include <nsswitch.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <syslog.h>
42 #include "un-namespace.h"
43 
44 static	void	_nsaddsrctomap(const char *);
45 
46 static	ns_dbt		curdbt;
47 static	ns_src		cursrc;
48 %}
49 
50 %union {
51 	char *str;
52 	int   mapval;
53 }
54 
55 %token	NL
56 %token	SUCCESS UNAVAIL NOTFOUND TRYAGAIN
57 %token	RETURN CONTINUE
58 %token  ERRORTOKEN
59 %token	<str> STRING
60 
61 %type	<mapval> Status Action
62 
63 %%
64 
65 File
66 	:	/* empty */
67 	| Lines
68 	;
69 
70 Lines
71 	: Entry
72 	| Lines Entry
73 	;
74 
75 Entry
76 	: NL
77 	| Database ':' NL
78 		{
79 			free((char*)curdbt.name);
80 		}
81 	| Database ':' Srclist NL
82 		{
83 			_nsdbtput(&curdbt);
84 		}
85 	| error NL
86 		{
87 			yyerrok;
88 		}
89 	;
90 
91 Database
92 	: STRING
93 		{
94 			curdbt.name = yylval.str;
95 			curdbt.srclist = NULL;
96 			curdbt.srclistsize = 0;
97 		}
98 	;
99 
100 Srclist
101 	: Item
102 	| Srclist Item
103 	;
104 
105 Item
106 	: STRING
107 		{
108 			cursrc.flags = NS_TERMINATE;
109 			_nsaddsrctomap($1);
110 		}
111 	| STRING '[' { cursrc.flags = NS_SUCCESS; } Criteria ']'
112 		{
113 			_nsaddsrctomap($1);
114 		}
115 	;
116 
117 Criteria
118 	: Criterion
119 	| Criteria Criterion
120 	;
121 
122 Criterion
123 	: Status '=' Action
124 		{
125 			if ($3)	     /* if action == RETURN set RETURN bit */
126 				cursrc.flags |= $1;
127 			else	     /* else unset it */
128 				cursrc.flags &= ~$1;
129 		}
130 	;
131 
132 Status
133 	: SUCCESS	{ $$ = NS_SUCCESS; }
134 	| UNAVAIL	{ $$ = NS_UNAVAIL; }
135 	| NOTFOUND	{ $$ = NS_NOTFOUND; }
136 	| TRYAGAIN	{ $$ = NS_TRYAGAIN; }
137 	;
138 
139 Action
140 	: RETURN	{ $$ = NS_ACTION_RETURN; }
141 	| CONTINUE	{ $$ = NS_ACTION_CONTINUE; }
142 	;
143 
144 %%
145 
146 static void
147 _nsaddsrctomap(const char *elem)
148 {
149 	int		i, lineno;
150 	extern int	_nsyylineno;
151 	extern char *	_nsyytext;
152 
153 	lineno = _nsyylineno - (*_nsyytext == '\n' ? 1 : 0);
154 	if (curdbt.srclistsize > 0) {
155 		if (((strcasecmp(elem, NSSRC_COMPAT) == 0) &&
156 		    (strcasecmp(curdbt.srclist[0].name, NSSRC_CACHE) != 0)) ||
157 		    (strcasecmp(curdbt.srclist[0].name, NSSRC_COMPAT) == 0)) {
158 			syslog(LOG_ERR,
159 	    "NSSWITCH(nsparser): %s line %d: 'compat' used with sources, other than 'cache'",
160 			    _PATH_NS_CONF, lineno);
161 			free((void*)elem);
162 			return;
163 		}
164 	}
165 	for (i = 0; i < curdbt.srclistsize; i++) {
166 		if (strcasecmp(curdbt.srclist[i].name, elem) == 0) {
167 			syslog(LOG_ERR,
168 		       "NSSWITCH(nsparser): %s line %d: duplicate source '%s'",
169 			    _PATH_NS_CONF, lineno, elem);
170 			free((void*)elem);
171 			return;
172 		}
173 	}
174 	cursrc.name = elem;
175 	_nsdbtaddsrc(&curdbt, &cursrc);
176 }
177