xref: /freebsd/usr.bin/iscsictl/parse.y (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1009ea47eSEdward Tomasz Napierala %{
2009ea47eSEdward Tomasz Napierala /*-
3*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
41de7b4b8SPedro F. Giffuni  *
5009ea47eSEdward Tomasz Napierala  * Copyright (c) 2012 The FreeBSD Foundation
6009ea47eSEdward Tomasz Napierala  *
7009ea47eSEdward Tomasz Napierala  * This software was developed by Edward Tomasz Napierala under sponsorship
8009ea47eSEdward Tomasz Napierala  * from the FreeBSD Foundation.
9009ea47eSEdward Tomasz Napierala  *
10009ea47eSEdward Tomasz Napierala  * Redistribution and use in source and binary forms, with or without
11009ea47eSEdward Tomasz Napierala  * modification, are permitted provided that the following conditions
12009ea47eSEdward Tomasz Napierala  * are met:
13009ea47eSEdward Tomasz Napierala  * 1. Redistributions of source code must retain the above copyright
14009ea47eSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer.
15009ea47eSEdward Tomasz Napierala  * 2. Redistributions in binary form must reproduce the above copyright
16009ea47eSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer in the
17009ea47eSEdward Tomasz Napierala  *    documentation and/or other materials provided with the distribution.
18009ea47eSEdward Tomasz Napierala  *
19009ea47eSEdward Tomasz Napierala  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20009ea47eSEdward Tomasz Napierala  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21009ea47eSEdward Tomasz Napierala  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22009ea47eSEdward Tomasz Napierala  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23009ea47eSEdward Tomasz Napierala  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24009ea47eSEdward Tomasz Napierala  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25009ea47eSEdward Tomasz Napierala  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26009ea47eSEdward Tomasz Napierala  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27009ea47eSEdward Tomasz Napierala  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28009ea47eSEdward Tomasz Napierala  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29009ea47eSEdward Tomasz Napierala  * SUCH DAMAGE.
30009ea47eSEdward Tomasz Napierala  */
31009ea47eSEdward Tomasz Napierala 
32009ea47eSEdward Tomasz Napierala #include <sys/queue.h>
33009ea47eSEdward Tomasz Napierala #include <sys/types.h>
34009ea47eSEdward Tomasz Napierala #include <sys/stat.h>
35009ea47eSEdward Tomasz Napierala #include <assert.h>
36009ea47eSEdward Tomasz Napierala #include <stdio.h>
37009ea47eSEdward Tomasz Napierala #include <stdint.h>
38009ea47eSEdward Tomasz Napierala #include <stdlib.h>
39009ea47eSEdward Tomasz Napierala #include <string.h>
40009ea47eSEdward Tomasz Napierala 
4101d54ad2SEdward Tomasz Napierala #include <libxo/xo.h>
4201d54ad2SEdward Tomasz Napierala 
43009ea47eSEdward Tomasz Napierala #include "iscsictl.h"
44bfabdadeSRichard Scheffenegger #include <netinet/in.h>
45bfabdadeSRichard Scheffenegger #include <netinet/ip.h>
46009ea47eSEdward Tomasz Napierala 
47009ea47eSEdward Tomasz Napierala extern FILE *yyin;
48009ea47eSEdward Tomasz Napierala extern char *yytext;
49009ea47eSEdward Tomasz Napierala extern int lineno;
50009ea47eSEdward Tomasz Napierala 
51009ea47eSEdward Tomasz Napierala static struct conf *conf;
52009ea47eSEdward Tomasz Napierala static struct target *target;
53009ea47eSEdward Tomasz Napierala 
54009ea47eSEdward Tomasz Napierala extern void	yyerror(const char *);
55009ea47eSEdward Tomasz Napierala extern void	yyrestart(FILE *);
56009ea47eSEdward Tomasz Napierala 
57009ea47eSEdward Tomasz Napierala %}
58009ea47eSEdward Tomasz Napierala 
59ba165a31SEdward Tomasz Napierala %token AUTH_METHOD ENABLE HEADER_DIGEST DATA_DIGEST TARGET_NAME TARGET_ADDRESS
60009ea47eSEdward Tomasz Napierala %token INITIATOR_NAME INITIATOR_ADDRESS INITIATOR_ALIAS USER SECRET
6182babffbSEdward Tomasz Napierala %token MUTUAL_USER MUTUAL_SECRET SEMICOLON SESSION_TYPE PROTOCOL OFFLOAD
62bd6bb493SRichard Scheffenegger %token IGNORED EQUALS OPENING_BRACKET CLOSING_BRACKET DSCP PINGTIMEOUT LOGINTIMEOUT
63bfabdadeSRichard Scheffenegger %token AF11 AF12 AF13 AF21 AF22 AF23 AF31 AF32 AF33 AF41 AF42 AF43
64bfabdadeSRichard Scheffenegger %token BE EF CS0 CS1 CS2 CS3 CS4 CS5 CS6 CS7
65009ea47eSEdward Tomasz Napierala 
66009ea47eSEdward Tomasz Napierala %union
67009ea47eSEdward Tomasz Napierala {
68009ea47eSEdward Tomasz Napierala 	char *str;
69009ea47eSEdward Tomasz Napierala }
70009ea47eSEdward Tomasz Napierala 
71009ea47eSEdward Tomasz Napierala %token <str> STR
72009ea47eSEdward Tomasz Napierala 
73009ea47eSEdward Tomasz Napierala %%
74009ea47eSEdward Tomasz Napierala 
755381f862SEdward Tomasz Napierala targets:
76009ea47eSEdward Tomasz Napierala 	|
775381f862SEdward Tomasz Napierala 	targets target
78009ea47eSEdward Tomasz Napierala 	;
79009ea47eSEdward Tomasz Napierala 
805381f862SEdward Tomasz Napierala target:		STR OPENING_BRACKET target_entries CLOSING_BRACKET
81009ea47eSEdward Tomasz Napierala 	{
82009ea47eSEdward Tomasz Napierala 		if (target_find(conf, $1) != NULL)
8301d54ad2SEdward Tomasz Napierala 			xo_errx(1, "duplicated target %s", $1);
84009ea47eSEdward Tomasz Napierala 		target->t_nickname = $1;
85009ea47eSEdward Tomasz Napierala 		target = target_new(conf);
86009ea47eSEdward Tomasz Napierala 	}
87009ea47eSEdward Tomasz Napierala 	;
88009ea47eSEdward Tomasz Napierala 
89009ea47eSEdward Tomasz Napierala target_entries:
90009ea47eSEdward Tomasz Napierala 	|
91009ea47eSEdward Tomasz Napierala 	target_entries target_entry
9257cf56ceSEdward Tomasz Napierala 	|
9357cf56ceSEdward Tomasz Napierala 	target_entries target_entry SEMICOLON
94009ea47eSEdward Tomasz Napierala 	;
95009ea47eSEdward Tomasz Napierala 
96009ea47eSEdward Tomasz Napierala target_entry:
975381f862SEdward Tomasz Napierala 	target_name
98009ea47eSEdward Tomasz Napierala 	|
995381f862SEdward Tomasz Napierala 	target_address
100009ea47eSEdward Tomasz Napierala 	|
1015381f862SEdward Tomasz Napierala 	initiator_name
102009ea47eSEdward Tomasz Napierala 	|
1035381f862SEdward Tomasz Napierala 	initiator_address
104009ea47eSEdward Tomasz Napierala 	|
1055381f862SEdward Tomasz Napierala 	initiator_alias
106009ea47eSEdward Tomasz Napierala 	|
1075381f862SEdward Tomasz Napierala 	user
108009ea47eSEdward Tomasz Napierala 	|
1095381f862SEdward Tomasz Napierala 	secret
110009ea47eSEdward Tomasz Napierala 	|
1115381f862SEdward Tomasz Napierala 	mutual_user
112009ea47eSEdward Tomasz Napierala 	|
1135381f862SEdward Tomasz Napierala 	mutual_secret
114009ea47eSEdward Tomasz Napierala 	|
1155381f862SEdward Tomasz Napierala 	auth_method
116009ea47eSEdward Tomasz Napierala 	|
1175381f862SEdward Tomasz Napierala 	header_digest
118009ea47eSEdward Tomasz Napierala 	|
1195381f862SEdward Tomasz Napierala 	data_digest
120009ea47eSEdward Tomasz Napierala 	|
1215381f862SEdward Tomasz Napierala 	session_type
122009ea47eSEdward Tomasz Napierala 	|
123ba165a31SEdward Tomasz Napierala 	enable
124ba165a31SEdward Tomasz Napierala 	|
12582babffbSEdward Tomasz Napierala 	offload
12682babffbSEdward Tomasz Napierala 	|
1275381f862SEdward Tomasz Napierala 	protocol
128009ea47eSEdward Tomasz Napierala 	|
1295381f862SEdward Tomasz Napierala 	ignored
130bfabdadeSRichard Scheffenegger 	|
131bfabdadeSRichard Scheffenegger 	dscp
1324dfbcffbSRichard Scheffenegger 	|
1334dfbcffbSRichard Scheffenegger 	pcp
134bd6bb493SRichard Scheffenegger 	|
135bd6bb493SRichard Scheffenegger 	ping_timeout
136bd6bb493SRichard Scheffenegger 	|
137bd6bb493SRichard Scheffenegger 	login_timeout
138009ea47eSEdward Tomasz Napierala 	;
139009ea47eSEdward Tomasz Napierala 
1405381f862SEdward Tomasz Napierala target_name:	TARGET_NAME EQUALS STR
141009ea47eSEdward Tomasz Napierala 	{
142009ea47eSEdward Tomasz Napierala 		if (target->t_name != NULL)
14301d54ad2SEdward Tomasz Napierala 			xo_errx(1, "duplicated TargetName at line %d", lineno);
144009ea47eSEdward Tomasz Napierala 		target->t_name = $3;
145009ea47eSEdward Tomasz Napierala 	}
146009ea47eSEdward Tomasz Napierala 	;
147009ea47eSEdward Tomasz Napierala 
1485381f862SEdward Tomasz Napierala target_address:	TARGET_ADDRESS EQUALS STR
149009ea47eSEdward Tomasz Napierala 	{
150009ea47eSEdward Tomasz Napierala 		if (target->t_address != NULL)
15101d54ad2SEdward Tomasz Napierala 			xo_errx(1, "duplicated TargetAddress at line %d", lineno);
152009ea47eSEdward Tomasz Napierala 		target->t_address = $3;
153009ea47eSEdward Tomasz Napierala 	}
154009ea47eSEdward Tomasz Napierala 	;
155009ea47eSEdward Tomasz Napierala 
1565381f862SEdward Tomasz Napierala initiator_name:	INITIATOR_NAME EQUALS STR
157009ea47eSEdward Tomasz Napierala 	{
158009ea47eSEdward Tomasz Napierala 		if (target->t_initiator_name != NULL)
15901d54ad2SEdward Tomasz Napierala 			xo_errx(1, "duplicated InitiatorName at line %d", lineno);
160009ea47eSEdward Tomasz Napierala 		target->t_initiator_name = $3;
161009ea47eSEdward Tomasz Napierala 	}
162009ea47eSEdward Tomasz Napierala 	;
163009ea47eSEdward Tomasz Napierala 
1645381f862SEdward Tomasz Napierala initiator_address:	INITIATOR_ADDRESS EQUALS STR
165009ea47eSEdward Tomasz Napierala 	{
166009ea47eSEdward Tomasz Napierala 		if (target->t_initiator_address != NULL)
16701d54ad2SEdward Tomasz Napierala 			xo_errx(1, "duplicated InitiatorAddress at line %d", lineno);
168009ea47eSEdward Tomasz Napierala 		target->t_initiator_address = $3;
169009ea47eSEdward Tomasz Napierala 	}
170009ea47eSEdward Tomasz Napierala 	;
171009ea47eSEdward Tomasz Napierala 
1725381f862SEdward Tomasz Napierala initiator_alias:	INITIATOR_ALIAS EQUALS STR
173009ea47eSEdward Tomasz Napierala 	{
174009ea47eSEdward Tomasz Napierala 		if (target->t_initiator_alias != NULL)
17501d54ad2SEdward Tomasz Napierala 			xo_errx(1, "duplicated InitiatorAlias at line %d", lineno);
176009ea47eSEdward Tomasz Napierala 		target->t_initiator_alias = $3;
177009ea47eSEdward Tomasz Napierala 	}
178009ea47eSEdward Tomasz Napierala 	;
179009ea47eSEdward Tomasz Napierala 
1805381f862SEdward Tomasz Napierala user:		USER EQUALS STR
181009ea47eSEdward Tomasz Napierala 	{
182009ea47eSEdward Tomasz Napierala 		if (target->t_user != NULL)
18301d54ad2SEdward Tomasz Napierala 			xo_errx(1, "duplicated chapIName at line %d", lineno);
184009ea47eSEdward Tomasz Napierala 		target->t_user = $3;
185009ea47eSEdward Tomasz Napierala 	}
186009ea47eSEdward Tomasz Napierala 	;
187009ea47eSEdward Tomasz Napierala 
1885381f862SEdward Tomasz Napierala secret:		SECRET EQUALS STR
189009ea47eSEdward Tomasz Napierala 	{
190009ea47eSEdward Tomasz Napierala 		if (target->t_secret != NULL)
19101d54ad2SEdward Tomasz Napierala 			xo_errx(1, "duplicated chapSecret at line %d", lineno);
192009ea47eSEdward Tomasz Napierala 		target->t_secret = $3;
193009ea47eSEdward Tomasz Napierala 	}
194009ea47eSEdward Tomasz Napierala 	;
195009ea47eSEdward Tomasz Napierala 
1965381f862SEdward Tomasz Napierala mutual_user:	MUTUAL_USER EQUALS STR
197009ea47eSEdward Tomasz Napierala 	{
198009ea47eSEdward Tomasz Napierala 		if (target->t_mutual_user != NULL)
19901d54ad2SEdward Tomasz Napierala 			xo_errx(1, "duplicated tgtChapName at line %d", lineno);
200009ea47eSEdward Tomasz Napierala 		target->t_mutual_user = $3;
201009ea47eSEdward Tomasz Napierala 	}
202009ea47eSEdward Tomasz Napierala 	;
203009ea47eSEdward Tomasz Napierala 
2045381f862SEdward Tomasz Napierala mutual_secret:	MUTUAL_SECRET EQUALS STR
205009ea47eSEdward Tomasz Napierala 	{
206009ea47eSEdward Tomasz Napierala 		if (target->t_mutual_secret != NULL)
20701d54ad2SEdward Tomasz Napierala 			xo_errx(1, "duplicated tgtChapSecret at line %d", lineno);
208009ea47eSEdward Tomasz Napierala 		target->t_mutual_secret = $3;
209009ea47eSEdward Tomasz Napierala 	}
210009ea47eSEdward Tomasz Napierala 	;
211009ea47eSEdward Tomasz Napierala 
2125381f862SEdward Tomasz Napierala auth_method:	AUTH_METHOD EQUALS STR
213009ea47eSEdward Tomasz Napierala 	{
214009ea47eSEdward Tomasz Napierala 		if (target->t_auth_method != AUTH_METHOD_UNSPECIFIED)
21501d54ad2SEdward Tomasz Napierala 			xo_errx(1, "duplicated AuthMethod at line %d", lineno);
216009ea47eSEdward Tomasz Napierala 		if (strcasecmp($3, "none") == 0)
217009ea47eSEdward Tomasz Napierala 			target->t_auth_method = AUTH_METHOD_NONE;
218009ea47eSEdward Tomasz Napierala 		else if (strcasecmp($3, "chap") == 0)
219009ea47eSEdward Tomasz Napierala 			target->t_auth_method = AUTH_METHOD_CHAP;
220009ea47eSEdward Tomasz Napierala 		else
22101d54ad2SEdward Tomasz Napierala 			xo_errx(1, "invalid AuthMethod at line %d; "
2225d386b72SEdward Tomasz Napierala 			    "must be either \"none\" or \"CHAP\"", lineno);
223009ea47eSEdward Tomasz Napierala 	}
224009ea47eSEdward Tomasz Napierala 	;
225009ea47eSEdward Tomasz Napierala 
2265381f862SEdward Tomasz Napierala header_digest:	HEADER_DIGEST EQUALS STR
227009ea47eSEdward Tomasz Napierala 	{
228009ea47eSEdward Tomasz Napierala 		if (target->t_header_digest != DIGEST_UNSPECIFIED)
22901d54ad2SEdward Tomasz Napierala 			xo_errx(1, "duplicated HeaderDigest at line %d", lineno);
230009ea47eSEdward Tomasz Napierala 		if (strcasecmp($3, "none") == 0)
231009ea47eSEdward Tomasz Napierala 			target->t_header_digest = DIGEST_NONE;
232009ea47eSEdward Tomasz Napierala 		else if (strcasecmp($3, "CRC32C") == 0)
233009ea47eSEdward Tomasz Napierala 			target->t_header_digest = DIGEST_CRC32C;
234009ea47eSEdward Tomasz Napierala 		else
23501d54ad2SEdward Tomasz Napierala 			xo_errx(1, "invalid HeaderDigest at line %d; "
2365d386b72SEdward Tomasz Napierala 			    "must be either \"none\" or \"CRC32C\"", lineno);
237009ea47eSEdward Tomasz Napierala 	}
238009ea47eSEdward Tomasz Napierala 	;
239009ea47eSEdward Tomasz Napierala 
2405381f862SEdward Tomasz Napierala data_digest:	DATA_DIGEST EQUALS STR
241009ea47eSEdward Tomasz Napierala 	{
242009ea47eSEdward Tomasz Napierala 		if (target->t_data_digest != DIGEST_UNSPECIFIED)
24301d54ad2SEdward Tomasz Napierala 			xo_errx(1, "duplicated DataDigest at line %d", lineno);
244009ea47eSEdward Tomasz Napierala 		if (strcasecmp($3, "none") == 0)
245009ea47eSEdward Tomasz Napierala 			target->t_data_digest = DIGEST_NONE;
246009ea47eSEdward Tomasz Napierala 		else if (strcasecmp($3, "CRC32C") == 0)
247009ea47eSEdward Tomasz Napierala 			target->t_data_digest = DIGEST_CRC32C;
248009ea47eSEdward Tomasz Napierala 		else
24901d54ad2SEdward Tomasz Napierala 			xo_errx(1, "invalid DataDigest at line %d; "
2505d386b72SEdward Tomasz Napierala 			    "must be either \"none\" or \"CRC32C\"", lineno);
251009ea47eSEdward Tomasz Napierala 	}
252009ea47eSEdward Tomasz Napierala 	;
253009ea47eSEdward Tomasz Napierala 
2545381f862SEdward Tomasz Napierala session_type:	SESSION_TYPE EQUALS STR
255009ea47eSEdward Tomasz Napierala 	{
256009ea47eSEdward Tomasz Napierala 		if (target->t_session_type != SESSION_TYPE_UNSPECIFIED)
25701d54ad2SEdward Tomasz Napierala 			xo_errx(1, "duplicated SessionType at line %d", lineno);
258009ea47eSEdward Tomasz Napierala 		if (strcasecmp($3, "normal") == 0)
259009ea47eSEdward Tomasz Napierala 			target->t_session_type = SESSION_TYPE_NORMAL;
260009ea47eSEdward Tomasz Napierala 		else if (strcasecmp($3, "discovery") == 0)
261009ea47eSEdward Tomasz Napierala 			target->t_session_type = SESSION_TYPE_DISCOVERY;
262009ea47eSEdward Tomasz Napierala 		else
26301d54ad2SEdward Tomasz Napierala 			xo_errx(1, "invalid SessionType at line %d; "
2645d386b72SEdward Tomasz Napierala 			    "must be either \"normal\" or \"discovery\"", lineno);
265009ea47eSEdward Tomasz Napierala 	}
266009ea47eSEdward Tomasz Napierala 	;
267009ea47eSEdward Tomasz Napierala 
268ba165a31SEdward Tomasz Napierala enable:		ENABLE EQUALS STR
269ba165a31SEdward Tomasz Napierala 	{
270ba165a31SEdward Tomasz Napierala 		if (target->t_enable != ENABLE_UNSPECIFIED)
271ba165a31SEdward Tomasz Napierala 			xo_errx(1, "duplicated enable at line %d", lineno);
272ba165a31SEdward Tomasz Napierala 		target->t_enable = parse_enable($3);
273ba165a31SEdward Tomasz Napierala 		if (target->t_enable == ENABLE_UNSPECIFIED)
274ba165a31SEdward Tomasz Napierala 			xo_errx(1, "invalid enable at line %d; "
275ba165a31SEdward Tomasz Napierala 			    "must be either \"on\" or \"off\"", lineno);
276ba165a31SEdward Tomasz Napierala 	}
277ba165a31SEdward Tomasz Napierala 	;
278ba165a31SEdward Tomasz Napierala 
27982babffbSEdward Tomasz Napierala offload:	OFFLOAD EQUALS STR
28082babffbSEdward Tomasz Napierala 	{
28182babffbSEdward Tomasz Napierala 		if (target->t_offload != NULL)
28201d54ad2SEdward Tomasz Napierala 			xo_errx(1, "duplicated offload at line %d", lineno);
28382babffbSEdward Tomasz Napierala 		target->t_offload = $3;
28482babffbSEdward Tomasz Napierala 	}
28582babffbSEdward Tomasz Napierala 	;
28682babffbSEdward Tomasz Napierala 
2875381f862SEdward Tomasz Napierala protocol:	PROTOCOL EQUALS STR
288009ea47eSEdward Tomasz Napierala 	{
289009ea47eSEdward Tomasz Napierala 		if (target->t_protocol != PROTOCOL_UNSPECIFIED)
29001d54ad2SEdward Tomasz Napierala 			xo_errx(1, "duplicated protocol at line %d", lineno);
291009ea47eSEdward Tomasz Napierala 		if (strcasecmp($3, "iscsi") == 0)
292009ea47eSEdward Tomasz Napierala 			target->t_protocol = PROTOCOL_ISCSI;
293009ea47eSEdward Tomasz Napierala 		else if (strcasecmp($3, "iser") == 0)
294009ea47eSEdward Tomasz Napierala 			target->t_protocol = PROTOCOL_ISER;
295009ea47eSEdward Tomasz Napierala 		else
29601d54ad2SEdward Tomasz Napierala 			xo_errx(1, "invalid protocol at line %d; "
2975d386b72SEdward Tomasz Napierala 			    "must be either \"iscsi\" or \"iser\"", lineno);
298009ea47eSEdward Tomasz Napierala 	}
299009ea47eSEdward Tomasz Napierala 	;
300009ea47eSEdward Tomasz Napierala 
3015381f862SEdward Tomasz Napierala ignored:	IGNORED EQUALS STR
302009ea47eSEdward Tomasz Napierala 	{
30301d54ad2SEdward Tomasz Napierala 		xo_warnx("obsolete statement ignored at line %d", lineno);
304009ea47eSEdward Tomasz Napierala 	}
305009ea47eSEdward Tomasz Napierala 	;
306009ea47eSEdward Tomasz Napierala 
307bfabdadeSRichard Scheffenegger dscp:		DSCP EQUALS STR
308bfabdadeSRichard Scheffenegger 	{
309bfabdadeSRichard Scheffenegger 		uint64_t tmp;
310bfabdadeSRichard Scheffenegger 
3114dfbcffbSRichard Scheffenegger 		if (target->t_dscp != -1)
3124dfbcffbSRichard Scheffenegger 			xo_errx(1, "duplicated dscp at line %d", lineno);
313bfabdadeSRichard Scheffenegger 		if (strcmp($3, "0x") == 0) {
314bfabdadeSRichard Scheffenegger 			tmp = strtol($3 + 2, NULL, 16);
315bfabdadeSRichard Scheffenegger 		} else if (expand_number($3, &tmp) != 0) {
316bfabdadeSRichard Scheffenegger 			yyerror("invalid numeric value");
317bfabdadeSRichard Scheffenegger 			free($3);
318bfabdadeSRichard Scheffenegger 			return(1);
319bfabdadeSRichard Scheffenegger 		}
320bfabdadeSRichard Scheffenegger 		if (tmp >= 0x40) {
321bfabdadeSRichard Scheffenegger 			yyerror("invalid dscp value");
322bfabdadeSRichard Scheffenegger 			return(1);
323bfabdadeSRichard Scheffenegger 		}
324bfabdadeSRichard Scheffenegger 
325bfabdadeSRichard Scheffenegger 		target->t_dscp = tmp;
326bfabdadeSRichard Scheffenegger 	}
327bfabdadeSRichard Scheffenegger 	| DSCP EQUALS BE	{ target->t_dscp = IPTOS_DSCP_CS0  >> 2 ; }
328bfabdadeSRichard Scheffenegger 	| DSCP EQUALS EF	{ target->t_dscp = IPTOS_DSCP_EF   >> 2 ; }
329bfabdadeSRichard Scheffenegger 	| DSCP EQUALS CS0	{ target->t_dscp = IPTOS_DSCP_CS0  >> 2 ; }
330bfabdadeSRichard Scheffenegger 	| DSCP EQUALS CS1	{ target->t_dscp = IPTOS_DSCP_CS1  >> 2 ; }
331bfabdadeSRichard Scheffenegger 	| DSCP EQUALS CS2	{ target->t_dscp = IPTOS_DSCP_CS2  >> 2 ; }
332bfabdadeSRichard Scheffenegger 	| DSCP EQUALS CS3	{ target->t_dscp = IPTOS_DSCP_CS3  >> 2 ; }
333bfabdadeSRichard Scheffenegger 	| DSCP EQUALS CS4	{ target->t_dscp = IPTOS_DSCP_CS4  >> 2 ; }
334bfabdadeSRichard Scheffenegger 	| DSCP EQUALS CS5	{ target->t_dscp = IPTOS_DSCP_CS5  >> 2 ; }
335bfabdadeSRichard Scheffenegger 	| DSCP EQUALS CS6	{ target->t_dscp = IPTOS_DSCP_CS6  >> 2 ; }
336bfabdadeSRichard Scheffenegger 	| DSCP EQUALS CS7	{ target->t_dscp = IPTOS_DSCP_CS7  >> 2 ; }
337bfabdadeSRichard Scheffenegger 	| DSCP EQUALS AF11	{ target->t_dscp = IPTOS_DSCP_AF11 >> 2 ; }
338bfabdadeSRichard Scheffenegger 	| DSCP EQUALS AF12	{ target->t_dscp = IPTOS_DSCP_AF12 >> 2 ; }
339bfabdadeSRichard Scheffenegger 	| DSCP EQUALS AF13	{ target->t_dscp = IPTOS_DSCP_AF13 >> 2 ; }
340bfabdadeSRichard Scheffenegger 	| DSCP EQUALS AF21	{ target->t_dscp = IPTOS_DSCP_AF21 >> 2 ; }
341bfabdadeSRichard Scheffenegger 	| DSCP EQUALS AF22	{ target->t_dscp = IPTOS_DSCP_AF22 >> 2 ; }
342bfabdadeSRichard Scheffenegger 	| DSCP EQUALS AF23	{ target->t_dscp = IPTOS_DSCP_AF23 >> 2 ; }
343bfabdadeSRichard Scheffenegger 	| DSCP EQUALS AF31	{ target->t_dscp = IPTOS_DSCP_AF31 >> 2 ; }
344bfabdadeSRichard Scheffenegger 	| DSCP EQUALS AF32	{ target->t_dscp = IPTOS_DSCP_AF32 >> 2 ; }
345bfabdadeSRichard Scheffenegger 	| DSCP EQUALS AF33	{ target->t_dscp = IPTOS_DSCP_AF33 >> 2 ; }
346bfabdadeSRichard Scheffenegger 	| DSCP EQUALS AF41	{ target->t_dscp = IPTOS_DSCP_AF41 >> 2 ; }
347bfabdadeSRichard Scheffenegger 	| DSCP EQUALS AF42	{ target->t_dscp = IPTOS_DSCP_AF42 >> 2 ; }
348bfabdadeSRichard Scheffenegger 	| DSCP EQUALS AF43	{ target->t_dscp = IPTOS_DSCP_AF43 >> 2 ; }
349bfabdadeSRichard Scheffenegger 	;
350bfabdadeSRichard Scheffenegger 
3514dfbcffbSRichard Scheffenegger pcp:	PCP EQUALS STR
3524dfbcffbSRichard Scheffenegger 	{
3534dfbcffbSRichard Scheffenegger 		uint64_t tmp;
3544dfbcffbSRichard Scheffenegger 
3554dfbcffbSRichard Scheffenegger 		if (target->t_pcp != -1)
3564dfbcffbSRichard Scheffenegger 			xo_errx(1, "duplicated pcp at line %d", lineno);
3574dfbcffbSRichard Scheffenegger 
3584dfbcffbSRichard Scheffenegger 		if (expand_number($3, &tmp) != 0) {
3594dfbcffbSRichard Scheffenegger 			yyerror("invalid numeric value");
3604dfbcffbSRichard Scheffenegger 			free($3);
3614dfbcffbSRichard Scheffenegger 			return(1);
3624dfbcffbSRichard Scheffenegger 		}
363c2a5b2abSAdrian Chadd 		if (tmp > 7) {
3644dfbcffbSRichard Scheffenegger 			yyerror("invalid pcp value");
3654dfbcffbSRichard Scheffenegger 			return(1);
3664dfbcffbSRichard Scheffenegger 		}
3674dfbcffbSRichard Scheffenegger 
3684dfbcffbSRichard Scheffenegger 		target->t_pcp = tmp;
3694dfbcffbSRichard Scheffenegger 	}
3704dfbcffbSRichard Scheffenegger 	;
3714dfbcffbSRichard Scheffenegger 
372bd6bb493SRichard Scheffenegger ping_timeout:	PINGTIMEOUT EQUALS STR
373bd6bb493SRichard Scheffenegger 	{
374bd6bb493SRichard Scheffenegger 		uint64_t tmp;
375bd6bb493SRichard Scheffenegger 
376bd6bb493SRichard Scheffenegger 		if (target->t_pingtimeout != -1)
377bd6bb493SRichard Scheffenegger 			xo_errx(1, "duplicated PingTimeout at line %d", lineno);
378bd6bb493SRichard Scheffenegger 
379bd6bb493SRichard Scheffenegger 		if (expand_number($3, &tmp) != 0) {
380bd6bb493SRichard Scheffenegger 			yyerror("invalid numeric value");
381bd6bb493SRichard Scheffenegger 			free($3);
382bd6bb493SRichard Scheffenegger 			return(1);
383bd6bb493SRichard Scheffenegger 		}
384bd6bb493SRichard Scheffenegger 		target->t_pingtimeout = tmp;
385bd6bb493SRichard Scheffenegger 	}
386bd6bb493SRichard Scheffenegger 	;
387bd6bb493SRichard Scheffenegger 
388bd6bb493SRichard Scheffenegger login_timeout:	LOGINTIMEOUT EQUALS STR
389bd6bb493SRichard Scheffenegger 	{
390bd6bb493SRichard Scheffenegger 		uint64_t tmp;
391bd6bb493SRichard Scheffenegger 
392bd6bb493SRichard Scheffenegger 		if (target->t_logintimeout != -1)
393bd6bb493SRichard Scheffenegger 			xo_errx(1, "duplicated LoginTimeout at line %d", lineno);
394bd6bb493SRichard Scheffenegger 
395bd6bb493SRichard Scheffenegger 		if (expand_number($3, &tmp) != 0) {
396bd6bb493SRichard Scheffenegger 			yyerror("invalid numeric value");
397bd6bb493SRichard Scheffenegger 			free($3);
398bd6bb493SRichard Scheffenegger 			return(1);
399bd6bb493SRichard Scheffenegger 		}
400bd6bb493SRichard Scheffenegger 		target->t_logintimeout = tmp;
401bd6bb493SRichard Scheffenegger 	}
402bd6bb493SRichard Scheffenegger 	;
403bd6bb493SRichard Scheffenegger 
404009ea47eSEdward Tomasz Napierala %%
405009ea47eSEdward Tomasz Napierala 
406009ea47eSEdward Tomasz Napierala void
407009ea47eSEdward Tomasz Napierala yyerror(const char *str)
408009ea47eSEdward Tomasz Napierala {
409009ea47eSEdward Tomasz Napierala 
41001d54ad2SEdward Tomasz Napierala 	xo_errx(1, "error in configuration file at line %d near '%s': %s",
4115d386b72SEdward Tomasz Napierala 	    lineno, yytext, str);
412009ea47eSEdward Tomasz Napierala }
413009ea47eSEdward Tomasz Napierala 
414009ea47eSEdward Tomasz Napierala static void
check_perms(const char * path)415009ea47eSEdward Tomasz Napierala check_perms(const char *path)
416009ea47eSEdward Tomasz Napierala {
417009ea47eSEdward Tomasz Napierala 	struct stat sb;
418009ea47eSEdward Tomasz Napierala 	int error;
419009ea47eSEdward Tomasz Napierala 
420009ea47eSEdward Tomasz Napierala 	error = stat(path, &sb);
421009ea47eSEdward Tomasz Napierala 	if (error != 0) {
42201d54ad2SEdward Tomasz Napierala 		xo_warn("stat");
423009ea47eSEdward Tomasz Napierala 		return;
424009ea47eSEdward Tomasz Napierala 	}
425009ea47eSEdward Tomasz Napierala 	if (sb.st_mode & S_IWOTH) {
42601d54ad2SEdward Tomasz Napierala 		xo_warnx("%s is world-writable", path);
427009ea47eSEdward Tomasz Napierala 	} else if (sb.st_mode & S_IROTH) {
42801d54ad2SEdward Tomasz Napierala 		xo_warnx("%s is world-readable", path);
429009ea47eSEdward Tomasz Napierala 	} else if (sb.st_mode & S_IXOTH) {
430009ea47eSEdward Tomasz Napierala 		/*
431009ea47eSEdward Tomasz Napierala 		 * Ok, this one doesn't matter, but still do it,
432009ea47eSEdward Tomasz Napierala 		 * just for consistency.
433009ea47eSEdward Tomasz Napierala 		 */
43401d54ad2SEdward Tomasz Napierala 		xo_warnx("%s is world-executable", path);
435009ea47eSEdward Tomasz Napierala 	}
436009ea47eSEdward Tomasz Napierala 
437009ea47eSEdward Tomasz Napierala 	/*
438009ea47eSEdward Tomasz Napierala 	 * XXX: Should we also check for owner != 0?
439009ea47eSEdward Tomasz Napierala 	 */
440009ea47eSEdward Tomasz Napierala }
441009ea47eSEdward Tomasz Napierala 
442009ea47eSEdward Tomasz Napierala struct conf *
conf_new_from_file(const char * path)443009ea47eSEdward Tomasz Napierala conf_new_from_file(const char *path)
444009ea47eSEdward Tomasz Napierala {
445009ea47eSEdward Tomasz Napierala 	int error;
446009ea47eSEdward Tomasz Napierala 
447009ea47eSEdward Tomasz Napierala 	conf = conf_new();
448009ea47eSEdward Tomasz Napierala 	target = target_new(conf);
449009ea47eSEdward Tomasz Napierala 
450009ea47eSEdward Tomasz Napierala 	yyin = fopen(path, "r");
451009ea47eSEdward Tomasz Napierala 	if (yyin == NULL)
45201d54ad2SEdward Tomasz Napierala 		xo_err(1, "unable to open configuration file %s", path);
453009ea47eSEdward Tomasz Napierala 	check_perms(path);
4545d386b72SEdward Tomasz Napierala 	lineno = 1;
455009ea47eSEdward Tomasz Napierala 	yyrestart(yyin);
456009ea47eSEdward Tomasz Napierala 	error = yyparse();
457009ea47eSEdward Tomasz Napierala 	assert(error == 0);
458009ea47eSEdward Tomasz Napierala 	fclose(yyin);
459009ea47eSEdward Tomasz Napierala 
460009ea47eSEdward Tomasz Napierala 	assert(target->t_nickname == NULL);
461009ea47eSEdward Tomasz Napierala 	target_delete(target);
462009ea47eSEdward Tomasz Napierala 
463009ea47eSEdward Tomasz Napierala 	conf_verify(conf);
464009ea47eSEdward Tomasz Napierala 
465009ea47eSEdward Tomasz Napierala 	return (conf);
466009ea47eSEdward Tomasz Napierala }
467