7 /* recover attributes from byte stream
11 /* int attr_scan_plain(fp, flags, type, name, ..., ATTR_TYPE_END)
17 /* int attr_vscan_plain(fp, flags, ap)
22 /* attr_scan_plain() takes zero or more (name, value) request attributes
23 /* and recovers the attribute values from the byte stream that was
24 /* possibly generated by attr_print_plain().
26 /* attr_vscan_plain() provides an alternative interface that is convenient
27 /* for calling from within a variadic function.
29 /* The input stream is formatted as follows, where (item)* stands
30 /* for zero or more instances of the specified item, and where
31 /* (item1 | item2) stands for choice:
34 /* attr-list :== simple-attr* newline
36 /* simple-attr :== attr-name "=" attr-value newline
38 /* attr-name :== any string without null or "=" or newline.
40 /* attr-value :== any string without null or newline.
42 /* newline :== the ASCII newline character
45 /* All attribute names and attribute values are sent as plain
46 /* strings. Each string must be no longer than 4*var_line_limit
47 /* characters. The formatting rules aim to make implementations in PERL
48 /* and other languages easy.
50 /* Normally, attributes must be received in the sequence as specified
51 /* with the attr_scan_plain() argument list. The input stream may
52 /* contain additional attributes at any point in the input stream,
53 /* including additional instances of requested attributes.
55 /* Additional input attributes or input attribute instances are silently
56 /* skipped over, unless the ATTR_FLAG_EXTRA processing flag is specified
57 /* (see below). This allows for some flexibility in the evolution of
58 /* protocols while still providing the option of being strict where
63 /* Stream to recover the input attributes from.
65 /* The bit-wise OR of zero or more of the following.
67 /* .IP ATTR_FLAG_MISSING
68 /* Log a warning when the input attribute list terminates before all
69 /* requested attributes are recovered. It is always an error when the
70 /* input stream ends without the newline attribute list terminator.
71 /* .IP ATTR_FLAG_EXTRA
72 /* Log a warning and stop attribute recovery when the input stream
73 /* contains an attribute that was not requested. This includes the
74 /* case of additional instances of a requested attribute.
76 /* After recovering the requested attributes, leave the input stream
77 /* in a state that is usable for more attr_scan_plain() operations
78 /* from the same input attribute list.
79 /* By default, attr_scan_plain() skips forward past the input attribute
81 /* .IP ATTR_FLAG_STRICT
82 /* For convenience, this value combines both ATTR_FLAG_MISSING and
85 /* For convenience, this value requests none of the above.
88 /* The type argument determines the arguments that follow.
90 /* .IP "ATTR_TYPE_INT (char *, int *)"
91 /* This argument is followed by an attribute name and an integer pointer.
92 /* .IP "ATTR_TYPE_LONG (char *, long *)"
93 /* This argument is followed by an attribute name and a long pointer.
94 /* .IP "ATTR_TYPE_STR (char *, VSTRING *)"
95 /* This argument is followed by an attribute name and a VSTRING pointer.
96 /* .IP "ATTR_TYPE_DATA (char *, VSTRING *)"
97 /* This argument is followed by an attribute name and a VSTRING pointer.
98 /* .IP "ATTR_TYPE_FUNC (ATTR_SCAN_SLAVE_FN, void *)"
99 /* This argument is followed by a function pointer and a generic data
100 /* pointer. The caller-specified function returns < 0 in case of
102 /* .IP "ATTR_TYPE_HASH (HTABLE *)"
103 /* .IP "ATTR_TYPE_NAMEVAL (NVTABLE *)"
104 /* All further input attributes are processed as string attributes.
105 /* No specific attribute sequence is enforced.
106 /* All attributes up to the attribute list terminator are read,
107 /* but only the first instance of each attribute is stored.
108 /* There can be no more than 1024 attributes in a hash table.
110 /* The attribute string values are stored in the hash table under
111 /* keys equal to the attribute name (obtained from the input stream).
112 /* Values from the input stream are added to the hash table. Existing
113 /* hash table entries are not replaced.
115 /* N.B. This construct must be followed by an ATTR_TYPE_END argument.
117 /* This argument terminates the requested attribute list.
120 /* ATTR_TYPE_HASH (ATTR_TYPE_NAMEVAL) accepts attributes with arbitrary
121 /* names from possibly untrusted sources.
122 /* This is unsafe, unless the resulting table is queried only with
123 /* known to be good attribute names.
125 /* attr_scan_plain() and attr_vscan_plain() return -1 when malformed input
126 /* is detected (string too long, incomplete line, missing end marker).
127 /* Otherwise, the result value is the number of attributes that were
128 /* successfully recovered from the input stream (a hash table counts
129 /* as the number of entries stored into the table).
131 /* Panic: interface violation. All system call errors are fatal.
133 /* attr_print_plain(3) send attributes over byte stream.
137 /* The Secure Mailer license must be distributed with this software.
140 /* IBM T.J. Watson Research
142 /* Yorktown Heights, NY 10598, USA
145 /* System library. */
147 #include <sys_defs.h>
152 /* Utility library. */
155 #include <mymalloc.h>
159 #include <base64_code.h>
162 /* Application specific. */
164 #define STR(x) vstring_str(x)
165 #define LEN(x) VSTRING_LEN(x)
167 /* attr_scan_plain_string - pull a string from the input stream */
169 static int attr_scan_plain_string(VSTREAM
*fp
, VSTRING
*plain_buf
,
170 int terminator
, const char *context
)
173 extern int var_line_limit
; /* XXX */
174 int limit
= var_line_limit
* 4;
179 VSTRING_RESET(plain_buf
);
180 while ((ch
= VSTREAM_GETC(fp
)) != '\n'
181 && (terminator
== 0 || ch
!= terminator
)) {
182 if (ch
== VSTREAM_EOF
) {
183 msg_warn("%s on %s while reading %s",
184 vstream_ftimeout(fp
) ? "timeout" : "premature end-of-input",
185 VSTREAM_PATH(fp
), context
);
188 VSTRING_ADDCH(plain_buf
, ch
);
190 if (LEN(plain_buf
) > limit
) {
191 msg_warn("string length > %d characters from %s while reading %s",
192 limit
, VSTREAM_PATH(fp
), context
);
197 VSTRING_TERMINATE(plain_buf
);
200 msg_info("%s: %s", context
, *STR(plain_buf
) ? STR(plain_buf
) : "(end)");
204 /* attr_scan_plain_data - pull a data blob from the input stream */
206 static int attr_scan_plain_data(VSTREAM
*fp
, VSTRING
*str_buf
,
210 static VSTRING
*base64_buf
= 0;
214 base64_buf
= vstring_alloc(10);
215 if ((ch
= attr_scan_plain_string(fp
, base64_buf
, terminator
, context
)) < 0)
217 if (base64_decode(str_buf
, STR(base64_buf
), LEN(base64_buf
)) == 0) {
218 msg_warn("malformed base64 data from %s while reading %s: %.100s",
219 VSTREAM_PATH(fp
), context
, STR(base64_buf
));
225 /* attr_scan_plain_number - pull a number from the input stream */
227 static int attr_scan_plain_number(VSTREAM
*fp
, unsigned *ptr
, VSTRING
*str_buf
,
228 int terminator
, const char *context
)
233 if ((ch
= attr_scan_plain_string(fp
, str_buf
, terminator
, context
)) < 0)
235 if (sscanf(STR(str_buf
), "%u%c", ptr
, &junk
) != 1 || junk
!= 0) {
236 msg_warn("malformed numerical data from %s while reading %s: %.100s",
237 VSTREAM_PATH(fp
), context
, STR(str_buf
));
243 /* attr_scan_plain_long_number - pull a number from the input stream */
245 static int attr_scan_plain_long_number(VSTREAM
*fp
, unsigned long *ptr
,
253 if ((ch
= attr_scan_plain_string(fp
, str_buf
, terminator
, context
)) < 0)
255 if (sscanf(STR(str_buf
), "%lu%c", ptr
, &junk
) != 1 || junk
!= 0) {
256 msg_warn("malformed numerical data from %s while reading %s: %.100s",
257 VSTREAM_PATH(fp
), context
, STR(str_buf
));
263 /* attr_vscan_plain - receive attribute list from stream */
265 int attr_vscan_plain(VSTREAM
*fp
, int flags
, va_list ap
)
267 const char *myname
= "attr_scan_plain";
268 static VSTRING
*str_buf
= 0;
269 static VSTRING
*name_buf
= 0;
270 int wanted_type
= -1;
272 unsigned int *number
;
273 unsigned long *long_number
;
278 ATTR_SCAN_SLAVE_FN scan_fn
;
284 if (flags
& ~ATTR_FLAG_ALL
)
285 msg_panic("%s: bad flags: 0x%x", myname
, flags
);
290 if ((ch
= VSTREAM_GETC(fp
)) == VSTREAM_EOF
)
292 vstream_ungetc(fp
, ch
);
298 str_buf
= vstring_alloc(10);
299 name_buf
= vstring_alloc(10);
303 * Iterate over all (type, name, value) triples.
305 for (conversions
= 0; /* void */ ; conversions
++) {
308 * Determine the next attribute type and attribute name on the
309 * caller's wish list.
311 * If we're reading into a hash table, we already know that the
312 * attribute value is string-valued, and we get the attribute name
313 * from the input stream instead. This is secure only when the
314 * resulting table is queried with known to be good attribute names.
316 if (wanted_type
!= ATTR_TYPE_HASH
) {
317 wanted_type
= va_arg(ap
, int);
318 if (wanted_type
== ATTR_TYPE_END
) {
319 if ((flags
& ATTR_FLAG_MORE
) != 0)
320 return (conversions
);
321 wanted_name
= "(list terminator)";
322 } else if (wanted_type
== ATTR_TYPE_HASH
) {
323 wanted_name
= "(any attribute name or list terminator)";
324 hash_table
= va_arg(ap
, HTABLE
*);
325 if (va_arg(ap
, int) != ATTR_TYPE_END
)
326 msg_panic("%s: ATTR_TYPE_HASH not followed by ATTR_TYPE_END",
328 } else if (wanted_type
!= ATTR_TYPE_FUNC
) {
329 wanted_name
= va_arg(ap
, char *);
334 * Locate the next attribute of interest in the input stream.
336 while (wanted_type
!= ATTR_TYPE_FUNC
) {
339 * Get the name of the next attribute. Hitting EOF is always bad.
340 * Hitting the end-of-input early is OK if the caller is prepared
341 * to deal with missing inputs.
344 msg_info("%s: wanted attribute: %s",
345 VSTREAM_PATH(fp
), wanted_name
);
346 if ((ch
= attr_scan_plain_string(fp
, name_buf
, '=',
347 "input attribute name")) == VSTREAM_EOF
)
349 if (ch
== '\n' && LEN(name_buf
) == 0) {
350 if (wanted_type
== ATTR_TYPE_END
351 || wanted_type
== ATTR_TYPE_HASH
)
352 return (conversions
);
353 if ((flags
& ATTR_FLAG_MISSING
) != 0)
354 msg_warn("missing attribute %s in input from %s",
355 wanted_name
, VSTREAM_PATH(fp
));
356 return (conversions
);
360 * See if the caller asks for this attribute.
362 if (wanted_type
== ATTR_TYPE_HASH
363 || (wanted_type
!= ATTR_TYPE_END
364 && strcmp(wanted_name
, STR(name_buf
)) == 0))
366 if ((flags
& ATTR_FLAG_EXTRA
) != 0) {
367 msg_warn("unexpected attribute %s from %s (expecting: %s)",
368 STR(name_buf
), VSTREAM_PATH(fp
), wanted_name
);
369 return (conversions
);
373 * Skip over this attribute. The caller does not ask for it.
375 while (ch
!= '\n' && (ch
= VSTREAM_GETC(fp
)) != VSTREAM_EOF
)
380 * Do the requested conversion.
382 switch (wanted_type
) {
385 msg_warn("missing value for number attribute %s from %s",
386 STR(name_buf
), VSTREAM_PATH(fp
));
389 number
= va_arg(ap
, unsigned int *);
390 if ((ch
= attr_scan_plain_number(fp
, number
, str_buf
, 0,
391 "input attribute value")) < 0)
396 msg_warn("missing value for number attribute %s from %s",
397 STR(name_buf
), VSTREAM_PATH(fp
));
400 long_number
= va_arg(ap
, unsigned long *);
401 if ((ch
= attr_scan_plain_long_number(fp
, long_number
, str_buf
,
402 0, "input attribute value")) < 0)
407 msg_warn("missing value for string attribute %s from %s",
408 STR(name_buf
), VSTREAM_PATH(fp
));
411 string
= va_arg(ap
, VSTRING
*);
412 if ((ch
= attr_scan_plain_string(fp
, string
, 0,
413 "input attribute value")) < 0)
418 msg_warn("missing value for data attribute %s from %s",
419 STR(name_buf
), VSTREAM_PATH(fp
));
422 string
= va_arg(ap
, VSTRING
*);
423 if ((ch
= attr_scan_plain_data(fp
, string
, 0,
424 "input attribute value")) < 0)
428 scan_fn
= va_arg(ap
, ATTR_SCAN_SLAVE_FN
);
429 scan_arg
= va_arg(ap
, void *);
430 if (scan_fn(attr_scan_plain
, fp
, flags
| ATTR_FLAG_MORE
, scan_arg
) < 0)
435 msg_warn("missing value for string attribute %s from %s",
436 STR(name_buf
), VSTREAM_PATH(fp
));
439 if ((ch
= attr_scan_plain_string(fp
, str_buf
, 0,
440 "input attribute value")) < 0)
442 if (htable_locate(hash_table
, STR(name_buf
)) != 0) {
443 if ((flags
& ATTR_FLAG_EXTRA
) != 0) {
444 msg_warn("duplicate attribute %s in input from %s",
445 STR(name_buf
), VSTREAM_PATH(fp
));
446 return (conversions
);
448 } else if (hash_table
->used
>= ATTR_HASH_LIMIT
) {
449 msg_warn("attribute count exceeds limit %d in input from %s",
450 ATTR_HASH_LIMIT
, VSTREAM_PATH(fp
));
451 return (conversions
);
453 htable_enter(hash_table
, STR(name_buf
),
454 mystrdup(STR(str_buf
)));
458 msg_panic("%s: unknown type code: %d", myname
, wanted_type
);
463 /* attr_scan_plain - read attribute list from stream */
465 int attr_scan_plain(VSTREAM
*fp
, int flags
,...)
471 ret
= attr_vscan_plain(fp
, flags
, ap
);
479 * Proof of concept test program. Mirror image of the attr_scan_plain test
482 #include <msg_vstream.h>
484 int var_line_limit
= 2048;
486 int main(int unused_argc
, char **used_argv
)
488 VSTRING
*data_val
= vstring_alloc(1);
489 VSTRING
*str_val
= vstring_alloc(1);
490 HTABLE
*table
= htable_create(1);
491 HTABLE_INFO
**ht_info_list
;
498 msg_vstream_init(used_argv
[0], VSTREAM_ERR
);
499 if ((ret
= attr_scan_plain(VSTREAM_IN
,
501 ATTR_TYPE_INT
, ATTR_NAME_INT
, &int_val
,
502 ATTR_TYPE_LONG
, ATTR_NAME_LONG
, &long_val
,
503 ATTR_TYPE_STR
, ATTR_NAME_STR
, str_val
,
504 ATTR_TYPE_DATA
, ATTR_NAME_DATA
, data_val
,
505 ATTR_TYPE_HASH
, table
,
506 ATTR_TYPE_END
)) > 4) {
507 vstream_printf("%s %d\n", ATTR_NAME_INT
, int_val
);
508 vstream_printf("%s %ld\n", ATTR_NAME_LONG
, long_val
);
509 vstream_printf("%s %s\n", ATTR_NAME_STR
, STR(str_val
));
510 vstream_printf("%s %s\n", ATTR_NAME_DATA
, STR(data_val
));
511 ht_info_list
= htable_list(table
);
512 for (ht
= ht_info_list
; *ht
; ht
++)
513 vstream_printf("(hash) %s %s\n", ht
[0]->key
, ht
[0]->value
);
514 myfree((char *) ht_info_list
);
516 vstream_printf("return: %d\n", ret
);
518 if ((ret
= attr_scan_plain(VSTREAM_IN
,
520 ATTR_TYPE_INT
, ATTR_NAME_INT
, &int_val
,
521 ATTR_TYPE_LONG
, ATTR_NAME_LONG
, &long_val
,
522 ATTR_TYPE_STR
, ATTR_NAME_STR
, str_val
,
523 ATTR_TYPE_DATA
, ATTR_NAME_DATA
, data_val
,
524 ATTR_TYPE_END
)) == 4) {
525 vstream_printf("%s %d\n", ATTR_NAME_INT
, int_val
);
526 vstream_printf("%s %ld\n", ATTR_NAME_LONG
, long_val
);
527 vstream_printf("%s %s\n", ATTR_NAME_STR
, STR(str_val
));
528 vstream_printf("%s %s\n", ATTR_NAME_DATA
, STR(data_val
));
529 ht_info_list
= htable_list(table
);
530 for (ht
= ht_info_list
; *ht
; ht
++)
531 vstream_printf("(hash) %s %s\n", ht
[0]->key
, ht
[0]->value
);
532 myfree((char *) ht_info_list
);
534 vstream_printf("return: %d\n", ret
);
536 if (vstream_fflush(VSTREAM_OUT
) != 0)
537 msg_fatal("write error: %m");
539 vstring_free(data_val
);
540 vstring_free(str_val
);
541 htable_free(table
, myfree
);