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: 469c678c20313da398a0f9eb503cfce966d24d3f $")
26
27#include <freeradius-devel/util/misc.h>
28#include <freeradius-devel/util/strerror.h>
29#include <freeradius-devel/util/token.h>
30
31
33 { L("=~"), T_OP_REG_EQ }, /* order is important! */
34 { L("!~"), T_OP_REG_NE },
35 { L("{"), T_LCBRACE },
36 { L("}"), T_RCBRACE },
37 { L("("), T_LBRACE },
38 { L(")"), T_RBRACE },
39 { L(","), T_COMMA },
40 { L("++"), T_OP_INCRM },
41 { L("+="), T_OP_ADD_EQ },
42 { L("-="), T_OP_SUB_EQ },
43 { L(":="), T_OP_SET },
44 { L("=*"), T_OP_CMP_TRUE },
45 { L("!*"), T_OP_CMP_FALSE },
46 { L("=="), T_OP_CMP_EQ },
47 { L("==="), T_OP_CMP_EQ_TYPE },
48 { L("^="), T_OP_PREPEND },
49 { L("|="), T_OP_OR_EQ },
50 { L("&="), T_OP_AND_EQ },
51 { L("="), T_OP_EQ },
52 { L("!="), T_OP_NE },
53 { L("!=="), T_OP_CMP_NE_TYPE },
54 { L(">>="), T_OP_RSHIFT_EQ },
55 { L(">="), T_OP_GE },
56 { L(">"), T_OP_GT },
57 { L("<<="), T_OP_LSHIFT_EQ },
58 { L("<="), T_OP_LE },
59 { L("<"), T_OP_LT },
60 { L("#"), T_HASH },
61 { L(";"), T_SEMICOLON }
62};
64
73
74/*
75 * String versions for all of the tokens.
76 */
77char const *fr_tokens[T_TOKEN_LAST] = {
78 [T_INVALID] = "?",
79 [T_EOL] = "EOL",
80
81 [T_LCBRACE] = "{",
82 [T_RCBRACE] = "}",
83 [T_LBRACE] = "(",
84 [T_RBRACE] = ")",
85 [T_COMMA] = ",",
86 [T_SEMICOLON] = ";",
87
88 [T_ADD] = "+",
89 [T_SUB] = "-",
90 [T_MUL] = "*",
91 [T_DIV] = "/",
92 [T_AND] = "&",
93 [T_OR] = "|",
94 [T_NOT] = "!",
95 [T_XOR] = "^",
96 [T_COMPLEMENT] = "~",
97 [T_MOD] = "%",
98
99 [T_RSHIFT] = ">>",
100 [T_LSHIFT] = "<<",
101
102 [T_LAND] = "&&",
103 [T_LOR] = "||",
104
105 [T_OP_INCRM] = "++",
106
107 [T_OP_ADD_EQ] = "+=",
108 [T_OP_SUB_EQ] = "-=",
109 [T_OP_SET] = ":=",
110 [T_OP_EQ] = "=",
111 [T_OP_OR_EQ] = "|=",
112 [T_OP_AND_EQ] = "&=",
113
114 [T_OP_RSHIFT_EQ] = ">>=",
115 [T_OP_LSHIFT_EQ] = "<<=",
116
117 [T_OP_NE] = "!=",
118 [T_OP_GE] = ">=",
119 [T_OP_GT] = ">",
120 [T_OP_LE] = "<=",
121 [T_OP_LT] = "<",
122 [T_OP_REG_EQ] = "=~",
123 [T_OP_REG_NE] = "!~",
124
125 [T_OP_CMP_TRUE] = "=*",
126 [T_OP_CMP_FALSE] = "!*",
127
128 [T_OP_CMP_EQ] = "==",
129
130 [T_OP_CMP_EQ_TYPE] = "===",
131 [T_OP_CMP_NE_TYPE] = "!==",
132
133 [T_OP_PREPEND] = "^=",
134
135 [T_HASH] = "#",
136 [T_BARE_WORD] = "<BARE-WORD>",
137 [T_DOUBLE_QUOTED_STRING] = "<\"STRING\">",
138 [T_SINGLE_QUOTED_STRING] = "<'STRING'>",
139 [T_BACK_QUOTED_STRING] = "<`STRING`>",
140 [T_SOLIDUS_QUOTED_STRING] = "</STRING/>",
141};
142
143
144/*
145 * This is fine. Don't complain.
146 */
147#ifdef __clang__
148#pragma clang diagnostic ignored "-Wgnu-designator"
149#endif
150
151/** Convert tokens back to a quoting character
152 *
153 * Non-string types convert to '?' to screw ups can be identified easily
154 */
156 [ 0 ... T_HASH ] = '?', /* GCC extension for range initialization, also allowed by clang */
157
158 [T_BARE_WORD] = '\0',
160 [T_SINGLE_QUOTED_STRING] = '\'',
161 [T_BACK_QUOTED_STRING] = '`',
163};
164
165#define T(_x) [T_OP_ ## _x] = true
166
168 T(INCRM), /* only used by LDAP :( */
169
170 T(ADD_EQ),
171 T(SUB_EQ),
172 T(MUL_EQ),
173 T(DIV_EQ),
174 T(AND_EQ),
175 T(OR_EQ),
176 T(RSHIFT_EQ),
177 T(LSHIFT_EQ),
178
179 T(SET),
180 T(EQ),
181 T(PREPEND),
182};
183
185 T(ADD_EQ), /* append */
186 T(SUB_EQ), /* remove */
187 T(AND_EQ), /* intersection */
188 T(OR_EQ), /* union */
189 T(LE), /* merge RHS */
190 T(GE), /* merge LHS */
191
192 T(SET),
193 T(EQ),
194 T(PREPEND), /* prepend */
195};
196
198 T(NE),
199 T(GE),
200 T(GT),
201 T(LE),
202 T(LT),
203 T(REG_EQ),
204 T(REG_NE),
205 T(CMP_TRUE),
206 T(CMP_FALSE),
207 T(CMP_EQ),
208 T(CMP_EQ_TYPE),
209 T(CMP_NE_TYPE),
210};
211
212#undef T
213#define T(_x) [T_ ## _x] = true
214
216 T(ADD),
217 T(SUB),
218 T(MUL),
219 T(DIV),
220 T(AND),
221 T(OR),
222 T(MOD),
223 T(RSHIFT),
224 T(LSHIFT),
225};
226
227
228#undef T
229#define T(_x) [T_## _x] = true
231 T(BARE_WORD),
232 T(DOUBLE_QUOTED_STRING),
233 T(SINGLE_QUOTED_STRING),
234 T(BACK_QUOTED_STRING),
235};
236
237/*
238 * This works only as long as special tokens
239 * are max. 2 characters, but it's fast.
240 */
241#define TOKEN_MATCH(bptr, tptr) \
242 ( (tptr)[0] == (bptr)[0] && \
243 ((tptr)[1] == (bptr)[1] || (tptr)[1] == 0))
244
245/*
246 * Read a word from a buffer and advance pointer.
247 * This function knows about escapes and quotes.
248 *
249 * At end-of-line, buf[0] is set to '\0'.
250 * Returns 0 or special token value.
251 */
252static fr_token_t getthing(char const **ptr, char *buf, int buflen, bool tok,
253 fr_table_num_ordered_t const *tokenlist, size_t tokenlist_len, bool unescape)
254{
255 char *s;
256 char const *p;
257 char quote;
258 bool triple = false;
259 unsigned int x;
260 size_t i;
261 fr_token_t token;
262
263 buf[0] = '\0';
264
265 /* Skip whitespace */
266 p = *ptr;
267
269
270 if (!*p) {
271 *ptr = p;
272 return T_EOL;
273 }
274
275 /*
276 * Might be a 1 or 2 character token.
277 */
278 if (tok) {
279 for (i = 0; i < tokenlist_len; i++) {
280 if (TOKEN_MATCH(p, tokenlist[i].name.str)) {
281 strcpy(buf, tokenlist[i].name.str);
282 p += tokenlist[i].name.len;
283
284 /*
285 * Try to shut up Coverity, which claims fr_token_t can be between 0..63, not
286 * 0..48???
287 */
288 if ((tokenlist[i].value < 0) || (tokenlist[i].value >= T_TOKEN_LAST)) return T_INVALID;
289
290 token = tokenlist[i].value;
291 goto done;
292 }
293 }
294 }
295
296 /* Read word. */
297 quote = '\0';
298 switch (*p) {
299 default:
300 token = T_BARE_WORD;
301 break;
302
303 case '\'':
305 break;
306
307 case '"':
309 break;
310
311 case '`':
312 token = T_BACK_QUOTED_STRING;
313 break;
314 }
315
316 if (token != T_BARE_WORD) {
317 quote = *p;
318
319 /*
320 * Triple-quoted strings are copied over verbatim, without escapes.
321 */
322 if ((buflen >= 3) && (p[1] == quote) && (p[2] == quote)) {
323 p += 3;
324 triple = true;
325 }
326
327 p++;
328 }
329 s = buf;
330
331 while (*p && buflen-- > 1) {
332 /*
333 * We're looking for strings. Stop on spaces, or
334 * (if given a token list), on a token, or on a
335 * comma.
336 */
337 if (!quote) {
338 if (isspace((uint8_t) *p)) break;
339
340
341 if (tok) {
342 for (i = 0; i < tokenlist_len; i++) {
343 if (TOKEN_MATCH(p, tokenlist[i].name.str)) {
344 *s++ = 0;
345 goto done;
346 }
347 }
348 }
349 if (*p == ',') break;
350
351 /*
352 * Copy the character over.
353 */
354 *s++ = *p++;
355 continue;
356 } /* else there was a quotation character */
357
358 /*
359 * Un-escaped quote character. We're done.
360 */
361 if (*p == quote) {
362 if (!triple) {
363 p++;
364 *s++ = 0;
365 goto done;
366 }
367
368 if ((buflen >= 3) && (p[1] == quote) && (p[2] == quote)) {
369 p += 3;
370 *s++ = 0;
371 goto done;
372 }
373
374 *s++ = *p++;
375 continue;
376 }
377
378 /*
379 * Everything but backslash gets copied over.
380 */
381 if (*p != '\\') {
382 *s++ = *p++;
383 continue;
384 }
385
386 /*
387 * There's nothing after the backslash, it's an error.
388 */
389 if (!p[1]) {
390 fr_strerror_const("Unterminated string");
391 return T_INVALID;
392 }
393
394 if (unescape) {
395 p++;
396
397 switch (*p) {
398 case 'r':
399 *s++ = '\r';
400 break;
401 case 'n':
402 *s++ = '\n';
403 break;
404 case 't':
405 *s++ = '\t';
406 break;
407
408 default:
409 if (*p >= '0' && *p <= '9' &&
410 sscanf(p, "%3o", &x) == 1) {
411 *s++ = x;
412 p += 2;
413 } else
414 *s++ = *p;
415 break;
416 }
417 p++;
418
419 } else {
420 /*
421 * Convert backslash-quote to quote, but
422 * leave everything else alone.
423 */
424 if (p[1] == quote) { /* convert '\'' --> ' */
425 p++;
426 } else {
427 if (buflen < 2) {
428 fr_strerror_const("Truncated input");
429 return T_INVALID;
430 }
431
432 *(s++) = *(p++);
433 }
434 *(s++) = *(p++);
435 }
436 }
437
438 *s++ = 0;
439
440 if (quote) {
441 fr_strerror_const("Unterminated string");
442 return T_INVALID;
443 }
444
445done:
446 /* Skip whitespace again. */
448
449 *ptr = p;
450
451 return token;
452}
453
454/*
455 * Read a "word" - this means we don't honor
456 * tokens as delimiters.
457 */
458int getword(char const **ptr, char *buf, int buflen, bool unescape)
459{
460 return getthing(ptr, buf, buflen, false, fr_tokens_table, fr_tokens_table_len, unescape) == T_EOL ? 0 : 1;
461}
462
463
464/*
465 * Read the next word, use tokens as delimiters.
466 */
467fr_token_t gettoken(char const **ptr, char *buf, int buflen, bool unescape)
468{
469 return getthing(ptr, buf, buflen, true, fr_tokens_table, fr_tokens_table_len, unescape);
470}
471
472/*
473 * Expect an operator.
474 */
475fr_token_t getop(char const **ptr)
476{
477 char op[3];
478 fr_token_t token;
479
480 token = getthing(ptr, op, sizeof(op), true, fr_tokens_table, fr_tokens_table_len, false);
481 if (!fr_assignment_op[token] && !fr_comparison_op[token]) {
482 fr_strerror_const("Expected operator");
483 return T_INVALID;
484 }
485 return token;
486}
487
488/*
489 * Expect a string.
490 */
491fr_token_t getstring(char const **ptr, char *buf, int buflen, bool unescape)
492{
493 char const *p;
494
495 if (!ptr || !*ptr || !buf) return T_INVALID;
496
497 p = *ptr;
498
500
501 *ptr = p;
502
503 if ((*p == '"') || (*p == '\'') || (*p == '`')) {
504 return gettoken(ptr, buf, buflen, unescape);
505 }
506
507 return getthing(ptr, buf, buflen, false, fr_tokens_table, fr_tokens_table_len, unescape);
508}
509
510char const *fr_token_name(int token)
511{
512 return fr_table_str_by_value(fr_tokens_table, token, "<INVALID>");
513}
514
515
516/** Skip a quoted string.
517 *
518 * @param[in] start start of the string, pointing to the quotation character
519 * @param[in] end end of the string (or NULL for zero-terminated strings)
520 * @return
521 * >0 length of the string which was parsed
522 * <=0 on error
523 */
524ssize_t fr_skip_string(char const *start, char const *end)
525{
526 char const *p = start;
527 char quote;
528
529 quote = *(p++);
530
531 while ((end && (p < end)) || *p) {
532 /*
533 * Stop at the quotation character
534 */
535 if (*p == quote) {
536 p++;
537 return p - start;
538 }
539
540 /*
541 * Not an escape character: it's OK.
542 */
543 if (*p != '\\') {
544 p++;
545 continue;
546 }
547
548 if (end && ((p + 2) >= end)) {
549 fail:
550 fr_strerror_const("Unexpected escape at end of string");
551 return -(p - start);
552 }
553
554 /*
555 * Escape at EOL is not allowed.
556 */
557 if (p[1] < ' ') goto fail;
558
559 /*
560 * \r or \n, etc.
561 */
562 if (!isdigit((uint8_t) p[1])) {
563 p += 2;
564 continue;
565 }
566
567 /*
568 * Double-quoted strings use \000
569 * Regexes use \0
570 */
571 if (quote == '/') {
572 p++;
573 continue;
574 }
575
576 if (end && ((p + 4) >= end)) goto fail;
577
578 /*
579 * Allow for \1f in single quoted strings
580 */
581 if ((quote == '\'') && isxdigit((uint8_t) p[1]) && isxdigit((uint8_t) p[2])) {
582 p += 3;
583 continue;
584 }
585
586 if (!isdigit((uint8_t) p[2]) || !isdigit((uint8_t) p[3])) {
587 fr_strerror_const("Invalid octal escape");
588 return -(p - start);
589 }
590
591 p += 4;
592 }
593
594 /*
595 * Unexpected end of string.
596 */
597 fr_strerror_const("Unexpected end of string");
598 return -(p - start);
599}
600
strcpy(log_entry->msg, buffer)
#define RCSID(id)
Definition build.h:485
#define L(_str)
Helper for initialising arrays of string literals.
Definition build.h:209
#define NUM_ELEMENTS(_t)
Definition build.h:339
Test enumeration values.
Definition dict_test.h:92
long int ssize_t
unsigned char uint8_t
#define fr_skip_whitespace(_p)
Skip whitespace ('\t', '\n', '\v', '\f', '\r', ' ')
Definition misc.h:59
static bool done
Definition radclient.c:81
static char const * name
#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:167
const bool fr_list_assignment_op[T_TOKEN_LAST]
Definition token.c:184
#define TOKEN_MATCH(bptr, tptr)
Definition token.c:241
char const * fr_token_name(int token)
Definition token.c:510
ssize_t fr_skip_string(char const *start, char const *end)
Skip a quoted string.
Definition token.c:524
fr_token_t gettoken(char const **ptr, char *buf, int buflen, bool unescape)
Definition token.c:467
fr_table_num_ordered_t const fr_tokens_table[]
Definition token.c:32
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:252
const bool fr_str_tok[T_TOKEN_LAST]
Definition token.c:230
fr_table_num_sorted_t const fr_token_quotes_table[]
Definition token.c:65
size_t fr_token_quotes_table_len
Definition token.c:72
size_t fr_tokens_table_len
Definition token.c:63
fr_token_t getop(char const **ptr)
Definition token.c:475
const char fr_token_quote[T_TOKEN_LAST]
Convert tokens back to a quoting character.
Definition token.c:155
char const * fr_tokens[T_TOKEN_LAST]
Definition token.c:77
fr_token_t getstring(char const **ptr, char *buf, int buflen, bool unescape)
Definition token.c:491
int getword(char const **ptr, char *buf, int buflen, bool unescape)
Definition token.c:458
const bool fr_comparison_op[T_TOKEN_LAST]
Definition token.c:197
const bool fr_binary_op[T_TOKEN_LAST]
Definition token.c:215
#define T(_x)
Definition token.c:165
enum fr_token fr_token_t
@ T_AND
Definition token.h:55
@ T_OP_SUB_EQ
Definition token.h:70
@ T_INVALID
Definition token.h:39
@ T_SUB
Definition token.h:52
@ T_RSHIFT
Definition token.h:62
@ T_RCBRACE
Definition token.h:42
@ T_NOT
Definition token.h:57
@ T_XOR
Definition token.h:58
@ T_RBRACE
Definition token.h:44
@ T_EOL
Definition token.h:40
@ T_SEMICOLON
Definition token.h:46
@ T_DIV
Definition token.h:54
@ T_SINGLE_QUOTED_STRING
Definition token.h:122
@ T_MOD
Definition token.h:60
@ T_OP_AND_EQ
Definition token.h:74
@ T_OP_CMP_TRUE
Definition token.h:104
@ T_BARE_WORD
Definition token.h:120
@ T_OP_EQ
Definition token.h:83
@ T_LAND
Definition token.h:91
@ T_COMPLEMENT
Definition token.h:59
@ T_ADD
Definition token.h:51
@ T_BACK_QUOTED_STRING
Definition token.h:123
@ T_HASH
Definition token.h:119
@ T_OP_SET
Definition token.h:84
@ T_OP_NE
Definition token.h:97
@ T_OP_ADD_EQ
Definition token.h:69
@ T_OP_CMP_FALSE
Definition token.h:105
@ T_OP_LSHIFT_EQ
Definition token.h:77
@ T_LOR
Definition token.h:92
@ T_LCBRACE
Definition token.h:41
@ T_LSHIFT
Definition token.h:63
@ T_OP_RSHIFT_EQ
Definition token.h:76
@ T_OP_REG_EQ
Definition token.h:102
@ T_OP_CMP_EQ_TYPE
Definition token.h:107
@ T_DOUBLE_QUOTED_STRING
Definition token.h:121
@ T_OP_CMP_EQ
Definition token.h:106
@ T_OP_INCRM
Definition token.h:113
@ T_LBRACE
Definition token.h:43
@ T_MUL
Definition token.h:53
@ T_OP_LE
Definition token.h:100
@ T_OP_CMP_NE_TYPE
Definition token.h:108
@ T_OP_GE
Definition token.h:98
@ T_OP_GT
Definition token.h:99
@ T_OP_OR_EQ
Definition token.h:73
@ T_SOLIDUS_QUOTED_STRING
Definition token.h:124
@ T_OP_LT
Definition token.h:101
@ T_OP_REG_NE
Definition token.h:103
@ T_COMMA
Definition token.h:45
@ T_OR
Definition token.h:56
@ T_OP_PREPEND
Definition token.h:85
#define T_TOKEN_LAST
Definition token.h:129
#define fr_strerror_const(_msg)
Definition strerror.h:223