The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
token.c
Go to the documentation of this file.
1/*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/** Tokenisation code and constants
18 *
19 * This is mostly for the attribute filter and user files.
20 *
21 * @file src/lib/util/token.c
22 *
23 * @copyright 2001,2006 The FreeRADIUS server project
24 */
25RCSID("$Id: 09a3bc9c0e62068b6c465e1758f33d2330893b86 $")
26
27#include <stdio.h>
28
29#include <freeradius-devel/util/skip.h>
30#include <freeradius-devel/util/token.h>
31
32
34 { L("=~"), T_OP_REG_EQ }, /* order is important! */
35 { L("!~"), T_OP_REG_NE },
36 { L("{"), T_LCBRACE },
37 { L("}"), T_RCBRACE },
38 { L("("), T_LBRACE },
39 { L(")"), T_RBRACE },
40 { L(","), T_COMMA },
41 { L("++"), T_OP_INCRM },
42 { L("+="), T_OP_ADD_EQ },
43 { L("-="), T_OP_SUB_EQ },
44 { L("*="), T_OP_MUL_EQ },
45 { L("/="), T_OP_DIV_EQ },
46 { L(":="), T_OP_SET },
47 { L("=*"), T_OP_CMP_TRUE },
48 { L("!*"), T_OP_CMP_FALSE },
49 { L("=="), T_OP_CMP_EQ },
50 { L("==="), T_OP_CMP_EQ_TYPE },
51 { L("^="), T_OP_PREPEND },
52 { L("|="), T_OP_OR_EQ },
53 { L("&="), T_OP_AND_EQ },
54 { L("="), T_OP_EQ },
55 { L("!="), T_OP_NE },
56 { L("!=="), T_OP_CMP_NE_TYPE },
57 { L(">>="), T_OP_RSHIFT_EQ },
58 { L(">="), T_OP_GE },
59 { L(">"), T_OP_GT },
60 { L("<<="), T_OP_LSHIFT_EQ },
61 { L("<="), T_OP_LE },
62 { L("<"), T_OP_LT },
63 { L("#"), T_HASH },
64 { L(";"), T_SEMICOLON }
65};
67
76
77/*
78 * String versions for all of the tokens.
79 */
80char const *fr_tokens[T_TOKEN_LAST] = {
81 [T_INVALID] = "?",
82 [T_EOL] = "EOL",
83
84 [T_LCBRACE] = "{",
85 [T_RCBRACE] = "}",
86 [T_LBRACE] = "(",
87 [T_RBRACE] = ")",
88 [T_COMMA] = ",",
89 [T_SEMICOLON] = ";",
90
91 [T_ADD] = "+",
92 [T_SUB] = "-",
93 [T_MUL] = "*",
94 [T_DIV] = "/",
95 [T_AND] = "&",
96 [T_OR] = "|",
97 [T_NOT] = "!",
98 [T_XOR] = "^",
99 [T_COMPLEMENT] = "~",
100 [T_MOD] = "%",
101
102 [T_RSHIFT] = ">>",
103 [T_LSHIFT] = "<<",
104
105 [T_LAND] = "&&",
106 [T_LOR] = "||",
107
108 [T_OP_INCRM] = "++",
109
110 [T_OP_ADD_EQ] = "+=",
111 [T_OP_SUB_EQ] = "-=",
112 [T_OP_SET] = ":=",
113 [T_OP_EQ] = "=",
114 [T_OP_OR_EQ] = "|=",
115 [T_OP_AND_EQ] = "&=",
116
117 [T_OP_RSHIFT_EQ] = ">>=",
118 [T_OP_LSHIFT_EQ] = "<<=",
119
120 [T_OP_NE] = "!=",
121 [T_OP_GE] = ">=",
122 [T_OP_GT] = ">",
123 [T_OP_LE] = "<=",
124 [T_OP_LT] = "<",
125 [T_OP_REG_EQ] = "=~",
126 [T_OP_REG_NE] = "!~",
127
128 [T_OP_CMP_TRUE] = "=*",
129 [T_OP_CMP_FALSE] = "!*",
130
131 [T_OP_CMP_EQ] = "==",
132
133 [T_OP_CMP_EQ_TYPE] = "===",
134 [T_OP_CMP_NE_TYPE] = "!==",
135
136 [T_OP_PREPEND] = "^=",
137
138 [T_HASH] = "#",
139 [T_BARE_WORD] = "<BARE-WORD>",
140 [T_DOUBLE_QUOTED_STRING] = "<\"STRING\">",
141 [T_SINGLE_QUOTED_STRING] = "<'STRING'>",
142 [T_BACK_QUOTED_STRING] = "<`STRING`>",
143 [T_SOLIDUS_QUOTED_STRING] = "</STRING/>",
144};
145
146
147/*
148 * This is fine. Don't complain.
149 */
150#ifdef __clang__
151#pragma clang diagnostic ignored "-Wgnu-designator"
152#endif
153
154/** Convert tokens back to a quoting character
155 *
156 * Non-string types convert to '?' to screw ups can be identified easily
157 */
159 [ 0 ... T_HASH ] = '?', /* GCC extension for range initialization, also allowed by clang */
160
161 [T_BARE_WORD] = '\0',
163 [T_SINGLE_QUOTED_STRING] = '\'',
164 [T_BACK_QUOTED_STRING] = '`',
166};
167
168#define T(_x) [T_OP_ ## _x] = true
169
171 T(INCRM), /* only used by LDAP :( */
172
173 T(ADD_EQ),
174 T(SUB_EQ),
175 T(MUL_EQ),
176 T(DIV_EQ),
177 T(AND_EQ),
178 T(OR_EQ),
179 T(RSHIFT_EQ),
180 T(LSHIFT_EQ),
181
182 T(SET),
183 T(EQ),
184 T(PREPEND),
185};
186
188 T(ADD_EQ), /* append */
189 T(SUB_EQ), /* remove */
190 T(AND_EQ), /* intersection */
191 T(OR_EQ), /* union */
192 T(LE), /* merge RHS */
193 T(GE), /* merge LHS */
194
195 T(SET),
196 T(EQ),
197 T(PREPEND), /* prepend */
198};
199
201 T(NE),
202 T(GE),
203 T(GT),
204 T(LE),
205 T(LT),
206 T(REG_EQ),
207 T(REG_NE),
208 T(CMP_TRUE),
209 T(CMP_FALSE),
210 T(CMP_EQ),
211 T(CMP_EQ_TYPE),
212 T(CMP_NE_TYPE),
213};
214
215#undef T
216#define T(_x) [T_ ## _x] = true
217
219 T(ADD),
220 T(SUB),
221 T(MUL),
222 T(DIV),
223 T(AND),
224 T(OR),
225 T(MOD),
226 T(RSHIFT),
227 T(LSHIFT),
228};
229
230
231#undef T
232#define T(_x) [T_## _x] = true
234 T(BARE_WORD),
235 T(DOUBLE_QUOTED_STRING),
236 T(SINGLE_QUOTED_STRING),
237 T(BACK_QUOTED_STRING),
238};
239
240/*
241 * This works only as long as special tokens
242 * are max. 2 characters, but it's fast.
243 */
244#define TOKEN_MATCH(bptr, tptr) \
245 ( (tptr)[0] == (bptr)[0] && \
246 ((tptr)[1] == (bptr)[1] || (tptr)[1] == 0))
247
248/*
249 * Read a word from a buffer and advance pointer.
250 * This function knows about escapes and quotes.
251 *
252 * At end-of-line, buf[0] is set to '\0'.
253 * Returns 0 or special token value.
254 */
255static fr_token_t getthing(char const **ptr, char *buf, int buflen, bool tok,
256 fr_table_num_ordered_t const *tokenlist, size_t tokenlist_len, bool unescape)
257{
258 char *s;
259 char const *p;
260 char quote;
261 bool triple = false;
262 unsigned int x;
263 size_t i;
264 fr_token_t token;
265
266 buf[0] = '\0';
267
268 /* Skip whitespace */
269 p = *ptr;
270
272
273 if (!*p) {
274 *ptr = p;
275 return T_EOL;
276 }
277
278 /*
279 * Might be a 1 or 2 character token.
280 */
281 if (tok) {
282 for (i = 0; i < tokenlist_len; i++) {
283 if (TOKEN_MATCH(p, tokenlist[i].name.str)) {
284 strcpy(buf, tokenlist[i].name.str);
285 p += tokenlist[i].name.len;
286
287 /*
288 * Try to shut up Coverity, which claims fr_token_t can be between 0..63, not
289 * 0..48???
290 */
291 if ((tokenlist[i].value < 0) || (tokenlist[i].value >= T_TOKEN_LAST)) return T_INVALID;
292
293 token = tokenlist[i].value;
294 goto done;
295 }
296 }
297 }
298
299 /* Read word. */
300 quote = '\0';
301 switch (*p) {
302 default:
303 token = T_BARE_WORD;
304 break;
305
306 case '\'':
308 break;
309
310 case '"':
312 break;
313
314 case '`':
315 token = T_BACK_QUOTED_STRING;
316 break;
317 }
318
319 if (token != T_BARE_WORD) {
320 quote = *p;
321
322 /*
323 * Triple-quoted strings are copied over verbatim, without escapes.
324 */
325 if ((buflen >= 3) && (p[1] == quote) && (p[2] == quote)) {
326 p += 3;
327 triple = true;
328 }
329
330 p++;
331 }
332 s = buf;
333
334 while (*p && buflen-- > 1) {
335 /*
336 * We're looking for strings. Stop on spaces, or
337 * (if given a token list), on a token, or on a
338 * comma.
339 */
340 if (!quote) {
341 if (isspace((uint8_t) *p)) break;
342
343
344 if (tok) {
345 for (i = 0; i < tokenlist_len; i++) {
346 if (TOKEN_MATCH(p, tokenlist[i].name.str)) {
347 *s++ = 0;
348 goto done;
349 }
350 }
351 }
352 if (*p == ',') break;
353
354 /*
355 * Copy the character over.
356 */
357 *s++ = *p++;
358 continue;
359 } /* else there was a quotation character */
360
361 /*
362 * Un-escaped quote character. We're done.
363 */
364 if (*p == quote) {
365 if (!triple) {
366 p++;
367 *s++ = 0;
368 goto done;
369 }
370
371 if ((buflen >= 3) && (p[1] == quote) && (p[2] == quote)) {
372 p += 3;
373 *s++ = 0;
374 goto done;
375 }
376
377 *s++ = *p++;
378 continue;
379 }
380
381 /*
382 * Everything but backslash gets copied over.
383 */
384 if (*p != '\\') {
385 *s++ = *p++;
386 continue;
387 }
388
389 /*
390 * There's nothing after the backslash, it's an error.
391 */
392 if (!p[1]) {
393 fr_strerror_const("Unterminated string");
394 return T_INVALID;
395 }
396
397 if (unescape) {
398 p++;
399
400 switch (*p) {
401 case 'r':
402 *s++ = '\r';
403 break;
404 case 'n':
405 *s++ = '\n';
406 break;
407 case 't':
408 *s++ = '\t';
409 break;
410
411 default:
412 if (*p >= '0' && *p <= '9' &&
413 sscanf(p, "%3o", &x) == 1) {
414 *s++ = x;
415 p += 2;
416 } else
417 *s++ = *p;
418 break;
419 }
420 p++;
421
422 } else {
423 /*
424 * Convert backslash-quote to quote, but
425 * leave everything else alone.
426 */
427 if (p[1] == quote) { /* convert '\'' --> ' */
428 p++;
429 } else {
430 if (buflen < 2) {
431 fr_strerror_const("Truncated input");
432 return T_INVALID;
433 }
434
435 *(s++) = *(p++);
436 }
437 *(s++) = *(p++);
438 }
439 }
440
441 *s++ = 0;
442
443 if (quote) {
444 fr_strerror_const("Unterminated string");
445 return T_INVALID;
446 }
447
448done:
449 /* Skip whitespace again. */
451
452 *ptr = p;
453
454 return token;
455}
456
457/*
458 * Read a "word" - this means we don't honor
459 * tokens as delimiters.
460 */
461int getword(char const **ptr, char *buf, int buflen, bool unescape)
462{
463 return getthing(ptr, buf, buflen, false, fr_tokens_table, fr_tokens_table_len, unescape) == T_EOL ? 0 : 1;
464}
465
466
467/*
468 * Read the next word, use tokens as delimiters.
469 */
470fr_token_t gettoken(char const **ptr, char *buf, int buflen, bool unescape)
471{
472 return getthing(ptr, buf, buflen, true, fr_tokens_table, fr_tokens_table_len, unescape);
473}
474
475/*
476 * Expect an operator.
477 */
478fr_token_t getop(char const **ptr)
479{
480 char op[3];
481 fr_token_t token;
482
483 token = getthing(ptr, op, sizeof(op), true, fr_tokens_table, fr_tokens_table_len, false);
484 if (!fr_assignment_op[token] && !fr_comparison_op[token]) {
485 fr_strerror_const("Expected operator");
486 return T_INVALID;
487 }
488 return token;
489}
490
491/*
492 * Expect a string.
493 */
494fr_token_t getstring(char const **ptr, char *buf, int buflen, bool unescape)
495{
496 char const *p;
497
498 if (!ptr || !*ptr || !buf) return T_INVALID;
499
500 p = *ptr;
501
503
504 *ptr = p;
505
506 if ((*p == '"') || (*p == '\'') || (*p == '`')) {
507 return gettoken(ptr, buf, buflen, unescape);
508 }
509
510 return getthing(ptr, buf, buflen, false, fr_tokens_table, fr_tokens_table_len, unescape);
511}
512
513char const *fr_token_name(int token)
514{
515 return fr_table_str_by_value(fr_tokens_table, token, "<INVALID>");
516}
strcpy(log_entry->msg, buffer)
#define RCSID(id)
Definition build.h:488
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:210
#define NUM_ELEMENTS(_t)
Definition build.h:340
Test enumeration values.
Definition dict_test.h:92
unsigned char uint8_t
static bool done
Definition radclient.c:80
static char const * name
#define fr_skip_whitespace(_p)
Skip whitespace ('\t', '\n', '\v', '\f', '\r', ' ')
Definition skip.h:36
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:772
fr_table_elem_name_t name
Definition table.h:58
size_t len
Literal string length.
Definition table.h:43
An element in an arbitrarily ordered array of name to num mappings.
Definition table.h:57
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
#define MOD(a, b)
const bool fr_assignment_op[T_TOKEN_LAST]
Definition token.c:170
const bool fr_list_assignment_op[T_TOKEN_LAST]
Definition token.c:187
#define TOKEN_MATCH(bptr, tptr)
Definition token.c:244
char const * fr_token_name(int token)
Definition token.c:513
fr_token_t gettoken(char const **ptr, char *buf, int buflen, bool unescape)
Definition token.c:470
fr_table_num_ordered_t const fr_tokens_table[]
Definition token.c:33
static fr_token_t getthing(char const **ptr, char *buf, int buflen, bool tok, fr_table_num_ordered_t const *tokenlist, size_t tokenlist_len, bool unescape)
Definition token.c:255
const bool fr_str_tok[T_TOKEN_LAST]
Definition token.c:233
fr_table_num_sorted_t const fr_token_quotes_table[]
Definition token.c:68
size_t fr_token_quotes_table_len
Definition token.c:75
size_t fr_tokens_table_len
Definition token.c:66
fr_token_t getop(char const **ptr)
Definition token.c:478
const char fr_token_quote[T_TOKEN_LAST]
Convert tokens back to a quoting character.
Definition token.c:158
char const * fr_tokens[T_TOKEN_LAST]
Definition token.c:80
fr_token_t getstring(char const **ptr, char *buf, int buflen, bool unescape)
Definition token.c:494
int getword(char const **ptr, char *buf, int buflen, bool unescape)
Definition token.c:461
const bool fr_comparison_op[T_TOKEN_LAST]
Definition token.c:200
const bool fr_binary_op[T_TOKEN_LAST]
Definition token.c:218
#define T(_x)
Definition token.c:168
enum fr_token fr_token_t
@ T_AND
Definition token.h:53
@ T_OP_SUB_EQ
Definition token.h:68
@ T_INVALID
Definition token.h:37
@ T_SUB
Definition token.h:50
@ T_RSHIFT
Definition token.h:60
@ T_RCBRACE
Definition token.h:40
@ T_NOT
Definition token.h:55
@ T_OP_DIV_EQ
Definition token.h:70
@ T_XOR
Definition token.h:56
@ T_RBRACE
Definition token.h:42
@ T_EOL
Definition token.h:38
@ T_SEMICOLON
Definition token.h:44
@ T_DIV
Definition token.h:52
@ T_SINGLE_QUOTED_STRING
Definition token.h:120
@ T_MOD
Definition token.h:58
@ T_OP_AND_EQ
Definition token.h:72
@ T_OP_CMP_TRUE
Definition token.h:102
@ T_BARE_WORD
Definition token.h:118
@ T_OP_EQ
Definition token.h:81
@ T_OP_MUL_EQ
Definition token.h:69
@ T_LAND
Definition token.h:89
@ T_COMPLEMENT
Definition token.h:57
@ T_ADD
Definition token.h:49
@ T_BACK_QUOTED_STRING
Definition token.h:121
@ T_HASH
Definition token.h:117
@ T_OP_SET
Definition token.h:82
@ T_OP_NE
Definition token.h:95
@ T_OP_ADD_EQ
Definition token.h:67
@ T_OP_CMP_FALSE
Definition token.h:103
@ T_OP_LSHIFT_EQ
Definition token.h:75
@ T_LOR
Definition token.h:90
@ T_LCBRACE
Definition token.h:39
@ T_LSHIFT
Definition token.h:61
@ T_OP_RSHIFT_EQ
Definition token.h:74
@ T_OP_REG_EQ
Definition token.h:100
@ T_OP_CMP_EQ_TYPE
Definition token.h:105
@ T_DOUBLE_QUOTED_STRING
Definition token.h:119
@ T_OP_CMP_EQ
Definition token.h:104
@ T_OP_INCRM
Definition token.h:111
@ T_LBRACE
Definition token.h:41
@ T_MUL
Definition token.h:51
@ T_OP_LE
Definition token.h:98
@ T_OP_CMP_NE_TYPE
Definition token.h:106
@ T_OP_GE
Definition token.h:96
@ T_OP_GT
Definition token.h:97
@ T_OP_OR_EQ
Definition token.h:71
@ T_SOLIDUS_QUOTED_STRING
Definition token.h:122
@ T_OP_LT
Definition token.h:99
@ T_OP_REG_NE
Definition token.h:101
@ T_COMMA
Definition token.h:43
@ T_OR
Definition token.h:54
@ T_OP_PREPEND
Definition token.h:83
#define T_TOKEN_LAST
Definition token.h:127
#define fr_strerror_const(_msg)
Definition strerror.h:223