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