git/ACKNOWLEDGMENTS: coerce charset of git tools
[exim.git] / src / src / expand.c
1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
4
5 /* Copyright (c) University of Cambridge 1995 - 2012 */
6 /* See the file NOTICE for conditions of use and distribution. */
7
8
9 /* Functions for handling string expansion. */
10
11
12 #include "exim.h"
13
14 /* Recursively called function */
15
16 static uschar *expand_string_internal(uschar *, BOOL, uschar **, BOOL, BOOL);
17
18 #ifdef STAND_ALONE
19 #ifndef SUPPORT_CRYPTEQ
20 #define SUPPORT_CRYPTEQ
21 #endif
22 #endif
23
24 #ifdef LOOKUP_LDAP
25 #include "lookups/ldap.h"
26 #endif
27
28 #ifdef SUPPORT_CRYPTEQ
29 #ifdef CRYPT_H
30 #include <crypt.h>
31 #endif
32 #ifndef HAVE_CRYPT16
33 extern char* crypt16(char*, char*);
34 #endif
35 #endif
36
37 /* The handling of crypt16() is a mess. I will record below the analysis of the
38 mess that was sent to me. We decided, however, to make changing this very low
39 priority, because in practice people are moving away from the crypt()
40 algorithms nowadays, so it doesn't seem worth it.
41
42 <quote>
43 There is an algorithm named "crypt16" in Ultrix and Tru64. It crypts
44 the first 8 characters of the password using a 20-round version of crypt
45 (standard crypt does 25 rounds). It then crypts the next 8 characters,
46 or an empty block if the password is less than 9 characters, using a
47 20-round version of crypt and the same salt as was used for the first
48 block. Charaters after the first 16 are ignored. It always generates
49 a 16-byte hash, which is expressed together with the salt as a string
50 of 24 base 64 digits. Here are some links to peruse:
51
52 http://cvs.pld.org.pl/pam/pamcrypt/crypt16.c?rev=1.2
53 http://seclists.org/bugtraq/1999/Mar/0076.html
54
55 There's a different algorithm named "bigcrypt" in HP-UX, Digital Unix,
56 and OSF/1. This is the same as the standard crypt if given a password
57 of 8 characters or less. If given more, it first does the same as crypt
58 using the first 8 characters, then crypts the next 8 (the 9th to 16th)
59 using as salt the first two base 64 digits from the first hash block.
60 If the password is more than 16 characters then it crypts the 17th to 24th
61 characters using as salt the first two base 64 digits from the second hash
62 block. And so on: I've seen references to it cutting off the password at
63 40 characters (5 blocks), 80 (10 blocks), or 128 (16 blocks). Some links:
64
65 http://cvs.pld.org.pl/pam/pamcrypt/bigcrypt.c?rev=1.2
66 http://seclists.org/bugtraq/1999/Mar/0109.html
67 http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/HTML/AA-Q0R2D-
68 TET1_html/sec.c222.html#no_id_208
69
70 Exim has something it calls "crypt16". It will either use a native
71 crypt16 or its own implementation. A native crypt16 will presumably
72 be the one that I called "crypt16" above. The internal "crypt16"
73 function, however, is a two-block-maximum implementation of what I called
74 "bigcrypt". The documentation matches the internal code.
75
76 I suspect that whoever did the "crypt16" stuff for Exim didn't realise
77 that crypt16 and bigcrypt were different things.
78
79 Exim uses the LDAP-style scheme identifier "{crypt16}" to refer
80 to whatever it is using under that name. This unfortunately sets a
81 precedent for using "{crypt16}" to identify two incompatible algorithms
82 whose output can't be distinguished. With "{crypt16}" thus rendered
83 ambiguous, I suggest you deprecate it and invent two new identifiers
84 for the two algorithms.
85
86 Both crypt16 and bigcrypt are very poor algorithms, btw. Hashing parts
87 of the password separately means they can be cracked separately, so
88 the double-length hash only doubles the cracking effort instead of
89 squaring it. I recommend salted SHA-1 ({SSHA}), or the Blowfish-based
90 bcrypt ({CRYPT}$2a$).
91 </quote>
92 */
93
94
95
96
97 /*************************************************
98 * Local statics and tables *
99 *************************************************/
100
101 /* Table of item names, and corresponding switch numbers. The names must be in
102 alphabetical order. */
103
104 static uschar *item_table[] = {
105 US"dlfunc",
106 US"extract",
107 US"filter",
108 US"hash",
109 US"hmac",
110 US"if",
111 US"length",
112 US"lookup",
113 US"map",
114 US"nhash",
115 US"perl",
116 US"prvs",
117 US"prvscheck",
118 US"readfile",
119 US"readsocket",
120 US"reduce",
121 US"run",
122 US"sg",
123 US"substr",
124 US"tr" };
125
126 enum {
127 EITEM_DLFUNC,
128 EITEM_EXTRACT,
129 EITEM_FILTER,
130 EITEM_HASH,
131 EITEM_HMAC,
132 EITEM_IF,
133 EITEM_LENGTH,
134 EITEM_LOOKUP,
135 EITEM_MAP,
136 EITEM_NHASH,
137 EITEM_PERL,
138 EITEM_PRVS,
139 EITEM_PRVSCHECK,
140 EITEM_READFILE,
141 EITEM_READSOCK,
142 EITEM_REDUCE,
143 EITEM_RUN,
144 EITEM_SG,
145 EITEM_SUBSTR,
146 EITEM_TR };
147
148 /* Tables of operator names, and corresponding switch numbers. The names must be
149 in alphabetical order. There are two tables, because underscore is used in some
150 cases to introduce arguments, whereas for other it is part of the name. This is
151 an historical mis-design. */
152
153 static uschar *op_table_underscore[] = {
154 US"from_utf8",
155 US"local_part",
156 US"quote_local_part",
157 US"reverse_ip",
158 US"time_eval",
159 US"time_interval"};
160
161 enum {
162 EOP_FROM_UTF8,
163 EOP_LOCAL_PART,
164 EOP_QUOTE_LOCAL_PART,
165 EOP_REVERSE_IP,
166 EOP_TIME_EVAL,
167 EOP_TIME_INTERVAL };
168
169 static uschar *op_table_main[] = {
170 US"address",
171 US"addresses",
172 US"base62",
173 US"base62d",
174 US"domain",
175 US"escape",
176 US"eval",
177 US"eval10",
178 US"expand",
179 US"h",
180 US"hash",
181 US"hex2b64",
182 US"l",
183 US"lc",
184 US"length",
185 US"listcount",
186 US"listnamed",
187 US"mask",
188 US"md5",
189 US"nh",
190 US"nhash",
191 US"quote",
192 US"randint",
193 US"rfc2047",
194 US"rfc2047d",
195 US"rxquote",
196 US"s",
197 US"sha1",
198 US"stat",
199 US"str2b64",
200 US"strlen",
201 US"substr",
202 US"uc" };
203
204 enum {
205 EOP_ADDRESS = sizeof(op_table_underscore)/sizeof(uschar *),
206 EOP_ADDRESSES,
207 EOP_BASE62,
208 EOP_BASE62D,
209 EOP_DOMAIN,
210 EOP_ESCAPE,
211 EOP_EVAL,
212 EOP_EVAL10,
213 EOP_EXPAND,
214 EOP_H,
215 EOP_HASH,
216 EOP_HEX2B64,
217 EOP_L,
218 EOP_LC,
219 EOP_LENGTH,
220 EOP_LISTCOUNT,
221 EOP_LISTNAMED,
222 EOP_MASK,
223 EOP_MD5,
224 EOP_NH,
225 EOP_NHASH,
226 EOP_QUOTE,
227 EOP_RANDINT,
228 EOP_RFC2047,
229 EOP_RFC2047D,
230 EOP_RXQUOTE,
231 EOP_S,
232 EOP_SHA1,
233 EOP_STAT,
234 EOP_STR2B64,
235 EOP_STRLEN,
236 EOP_SUBSTR,
237 EOP_UC };
238
239
240 /* Table of condition names, and corresponding switch numbers. The names must
241 be in alphabetical order. */
242
243 static uschar *cond_table[] = {
244 US"<",
245 US"<=",
246 US"=",
247 US"==", /* Backward compatibility */
248 US">",
249 US">=",
250 US"and",
251 US"bool",
252 US"bool_lax",
253 US"crypteq",
254 US"def",
255 US"eq",
256 US"eqi",
257 US"exists",
258 US"first_delivery",
259 US"forall",
260 US"forany",
261 US"ge",
262 US"gei",
263 US"gt",
264 US"gti",
265 US"inlist",
266 US"inlisti",
267 US"isip",
268 US"isip4",
269 US"isip6",
270 US"ldapauth",
271 US"le",
272 US"lei",
273 US"lt",
274 US"lti",
275 US"match",
276 US"match_address",
277 US"match_domain",
278 US"match_ip",
279 US"match_local_part",
280 US"or",
281 US"pam",
282 US"pwcheck",
283 US"queue_running",
284 US"radius",
285 US"saslauthd"
286 };
287
288 enum {
289 ECOND_NUM_L,
290 ECOND_NUM_LE,
291 ECOND_NUM_E,
292 ECOND_NUM_EE,
293 ECOND_NUM_G,
294 ECOND_NUM_GE,
295 ECOND_AND,
296 ECOND_BOOL,
297 ECOND_BOOL_LAX,
298 ECOND_CRYPTEQ,
299 ECOND_DEF,
300 ECOND_STR_EQ,
301 ECOND_STR_EQI,
302 ECOND_EXISTS,
303 ECOND_FIRST_DELIVERY,
304 ECOND_FORALL,
305 ECOND_FORANY,
306 ECOND_STR_GE,
307 ECOND_STR_GEI,
308 ECOND_STR_GT,
309 ECOND_STR_GTI,
310 ECOND_INLIST,
311 ECOND_INLISTI,
312 ECOND_ISIP,
313 ECOND_ISIP4,
314 ECOND_ISIP6,
315 ECOND_LDAPAUTH,
316 ECOND_STR_LE,
317 ECOND_STR_LEI,
318 ECOND_STR_LT,
319 ECOND_STR_LTI,
320 ECOND_MATCH,
321 ECOND_MATCH_ADDRESS,
322 ECOND_MATCH_DOMAIN,
323 ECOND_MATCH_IP,
324 ECOND_MATCH_LOCAL_PART,
325 ECOND_OR,
326 ECOND_PAM,
327 ECOND_PWCHECK,
328 ECOND_QUEUE_RUNNING,
329 ECOND_RADIUS,
330 ECOND_SASLAUTHD
331 };
332
333
334 /* Type for main variable table */
335
336 typedef struct {
337 const char *name;
338 int type;
339 void *value;
340 } var_entry;
341
342 /* Type for entries pointing to address/length pairs. Not currently
343 in use. */
344
345 typedef struct {
346 uschar **address;
347 int *length;
348 } alblock;
349
350 /* Types of table entry */
351
352 enum {
353 vtype_int, /* value is address of int */
354 vtype_filter_int, /* ditto, but recognized only when filtering */
355 vtype_ino, /* value is address of ino_t (not always an int) */
356 vtype_uid, /* value is address of uid_t (not always an int) */
357 vtype_gid, /* value is address of gid_t (not always an int) */
358 vtype_bool, /* value is address of bool */
359 vtype_stringptr, /* value is address of pointer to string */
360 vtype_msgbody, /* as stringptr, but read when first required */
361 vtype_msgbody_end, /* ditto, the end of the message */
362 vtype_msgheaders, /* the message's headers, processed */
363 vtype_msgheaders_raw, /* the message's headers, unprocessed */
364 vtype_localpart, /* extract local part from string */
365 vtype_domain, /* extract domain from string */
366 vtype_recipients, /* extract recipients from recipients list */
367 /* (available only in system filters, ACLs, and */
368 /* local_scan()) */
369 vtype_todbsdin, /* value not used; generate BSD inbox tod */
370 vtype_tode, /* value not used; generate tod in epoch format */
371 vtype_todel, /* value not used; generate tod in epoch/usec format */
372 vtype_todf, /* value not used; generate full tod */
373 vtype_todl, /* value not used; generate log tod */
374 vtype_todlf, /* value not used; generate log file datestamp tod */
375 vtype_todzone, /* value not used; generate time zone only */
376 vtype_todzulu, /* value not used; generate zulu tod */
377 vtype_reply, /* value not used; get reply from headers */
378 vtype_pid, /* value not used; result is pid */
379 vtype_host_lookup, /* value not used; get host name */
380 vtype_load_avg, /* value not used; result is int from os_getloadavg */
381 vtype_pspace, /* partition space; value is T/F for spool/log */
382 vtype_pinodes /* partition inodes; value is T/F for spool/log */
383 #ifndef DISABLE_DKIM
384 ,vtype_dkim /* Lookup of value in DKIM signature */
385 #endif
386 };
387
388 /* This table must be kept in alphabetical order. */
389
390 static var_entry var_table[] = {
391 /* WARNING: Do not invent variables whose names start acl_c or acl_m because
392 they will be confused with user-creatable ACL variables. */
393 { "acl_verify_message", vtype_stringptr, &acl_verify_message },
394 { "address_data", vtype_stringptr, &deliver_address_data },
395 { "address_file", vtype_stringptr, &address_file },
396 { "address_pipe", vtype_stringptr, &address_pipe },
397 { "authenticated_id", vtype_stringptr, &authenticated_id },
398 { "authenticated_sender",vtype_stringptr, &authenticated_sender },
399 { "authentication_failed",vtype_int, &authentication_failed },
400 #ifdef WITH_CONTENT_SCAN
401 { "av_failed", vtype_int, &av_failed },
402 #endif
403 #ifdef EXPERIMENTAL_BRIGHTMAIL
404 { "bmi_alt_location", vtype_stringptr, &bmi_alt_location },
405 { "bmi_base64_tracker_verdict", vtype_stringptr, &bmi_base64_tracker_verdict },
406 { "bmi_base64_verdict", vtype_stringptr, &bmi_base64_verdict },
407 { "bmi_deliver", vtype_int, &bmi_deliver },
408 #endif
409 { "body_linecount", vtype_int, &body_linecount },
410 { "body_zerocount", vtype_int, &body_zerocount },
411 { "bounce_recipient", vtype_stringptr, &bounce_recipient },
412 { "bounce_return_size_limit", vtype_int, &bounce_return_size_limit },
413 { "caller_gid", vtype_gid, &real_gid },
414 { "caller_uid", vtype_uid, &real_uid },
415 { "compile_date", vtype_stringptr, &version_date },
416 { "compile_number", vtype_stringptr, &version_cnumber },
417 { "csa_status", vtype_stringptr, &csa_status },
418 #ifdef EXPERIMENTAL_DCC
419 { "dcc_header", vtype_stringptr, &dcc_header },
420 { "dcc_result", vtype_stringptr, &dcc_result },
421 #endif
422 #ifdef WITH_OLD_DEMIME
423 { "demime_errorlevel", vtype_int, &demime_errorlevel },
424 { "demime_reason", vtype_stringptr, &demime_reason },
425 #endif
426 #ifndef DISABLE_DKIM
427 { "dkim_algo", vtype_dkim, (void *)DKIM_ALGO },
428 { "dkim_bodylength", vtype_dkim, (void *)DKIM_BODYLENGTH },
429 { "dkim_canon_body", vtype_dkim, (void *)DKIM_CANON_BODY },
430 { "dkim_canon_headers", vtype_dkim, (void *)DKIM_CANON_HEADERS },
431 { "dkim_copiedheaders", vtype_dkim, (void *)DKIM_COPIEDHEADERS },
432 { "dkim_created", vtype_dkim, (void *)DKIM_CREATED },
433 { "dkim_cur_signer", vtype_stringptr, &dkim_cur_signer },
434 { "dkim_domain", vtype_stringptr, &dkim_signing_domain },
435 { "dkim_expires", vtype_dkim, (void *)DKIM_EXPIRES },
436 { "dkim_headernames", vtype_dkim, (void *)DKIM_HEADERNAMES },
437 { "dkim_identity", vtype_dkim, (void *)DKIM_IDENTITY },
438 { "dkim_key_granularity",vtype_dkim, (void *)DKIM_KEY_GRANULARITY },
439 { "dkim_key_nosubdomains",vtype_dkim, (void *)DKIM_NOSUBDOMAINS },
440 { "dkim_key_notes", vtype_dkim, (void *)DKIM_KEY_NOTES },
441 { "dkim_key_srvtype", vtype_dkim, (void *)DKIM_KEY_SRVTYPE },
442 { "dkim_key_testing", vtype_dkim, (void *)DKIM_KEY_TESTING },
443 { "dkim_selector", vtype_stringptr, &dkim_signing_selector },
444 { "dkim_signers", vtype_stringptr, &dkim_signers },
445 { "dkim_verify_reason", vtype_dkim, (void *)DKIM_VERIFY_REASON },
446 { "dkim_verify_status", vtype_dkim, (void *)DKIM_VERIFY_STATUS},
447 #endif
448 { "dnslist_domain", vtype_stringptr, &dnslist_domain },
449 { "dnslist_matched", vtype_stringptr, &dnslist_matched },
450 { "dnslist_text", vtype_stringptr, &dnslist_text },
451 { "dnslist_value", vtype_stringptr, &dnslist_value },
452 { "domain", vtype_stringptr, &deliver_domain },
453 { "domain_data", vtype_stringptr, &deliver_domain_data },
454 { "exim_gid", vtype_gid, &exim_gid },
455 { "exim_path", vtype_stringptr, &exim_path },
456 { "exim_uid", vtype_uid, &exim_uid },
457 #ifdef WITH_OLD_DEMIME
458 { "found_extension", vtype_stringptr, &found_extension },
459 #endif
460 { "home", vtype_stringptr, &deliver_home },
461 { "host", vtype_stringptr, &deliver_host },
462 { "host_address", vtype_stringptr, &deliver_host_address },
463 { "host_data", vtype_stringptr, &host_data },
464 { "host_lookup_deferred",vtype_int, &host_lookup_deferred },
465 { "host_lookup_failed", vtype_int, &host_lookup_failed },
466 { "inode", vtype_ino, &deliver_inode },
467 { "interface_address", vtype_stringptr, &interface_address },
468 { "interface_port", vtype_int, &interface_port },
469 { "item", vtype_stringptr, &iterate_item },
470 #ifdef LOOKUP_LDAP
471 { "ldap_dn", vtype_stringptr, &eldap_dn },
472 #endif
473 { "load_average", vtype_load_avg, NULL },
474 { "local_part", vtype_stringptr, &deliver_localpart },
475 { "local_part_data", vtype_stringptr, &deliver_localpart_data },
476 { "local_part_prefix", vtype_stringptr, &deliver_localpart_prefix },
477 { "local_part_suffix", vtype_stringptr, &deliver_localpart_suffix },
478 { "local_scan_data", vtype_stringptr, &local_scan_data },
479 { "local_user_gid", vtype_gid, &local_user_gid },
480 { "local_user_uid", vtype_uid, &local_user_uid },
481 { "localhost_number", vtype_int, &host_number },
482 { "log_inodes", vtype_pinodes, (void *)FALSE },
483 { "log_space", vtype_pspace, (void *)FALSE },
484 { "mailstore_basename", vtype_stringptr, &mailstore_basename },
485 #ifdef WITH_CONTENT_SCAN
486 { "malware_name", vtype_stringptr, &malware_name },
487 #endif
488 { "max_received_linelength", vtype_int, &max_received_linelength },
489 { "message_age", vtype_int, &message_age },
490 { "message_body", vtype_msgbody, &message_body },
491 { "message_body_end", vtype_msgbody_end, &message_body_end },
492 { "message_body_size", vtype_int, &message_body_size },
493 { "message_exim_id", vtype_stringptr, &message_id },
494 { "message_headers", vtype_msgheaders, NULL },
495 { "message_headers_raw", vtype_msgheaders_raw, NULL },
496 { "message_id", vtype_stringptr, &message_id },
497 { "message_linecount", vtype_int, &message_linecount },
498 { "message_size", vtype_int, &message_size },
499 #ifdef WITH_CONTENT_SCAN
500 { "mime_anomaly_level", vtype_int, &mime_anomaly_level },
501 { "mime_anomaly_text", vtype_stringptr, &mime_anomaly_text },
502 { "mime_boundary", vtype_stringptr, &mime_boundary },
503 { "mime_charset", vtype_stringptr, &mime_charset },
504 { "mime_content_description", vtype_stringptr, &mime_content_description },
505 { "mime_content_disposition", vtype_stringptr, &mime_content_disposition },
506 { "mime_content_id", vtype_stringptr, &mime_content_id },
507 { "mime_content_size", vtype_int, &mime_content_size },
508 { "mime_content_transfer_encoding",vtype_stringptr, &mime_content_transfer_encoding },
509 { "mime_content_type", vtype_stringptr, &mime_content_type },
510 { "mime_decoded_filename", vtype_stringptr, &mime_decoded_filename },
511 { "mime_filename", vtype_stringptr, &mime_filename },
512 { "mime_is_coverletter", vtype_int, &mime_is_coverletter },
513 { "mime_is_multipart", vtype_int, &mime_is_multipart },
514 { "mime_is_rfc822", vtype_int, &mime_is_rfc822 },
515 { "mime_part_count", vtype_int, &mime_part_count },
516 #endif
517 { "n0", vtype_filter_int, &filter_n[0] },
518 { "n1", vtype_filter_int, &filter_n[1] },
519 { "n2", vtype_filter_int, &filter_n[2] },
520 { "n3", vtype_filter_int, &filter_n[3] },
521 { "n4", vtype_filter_int, &filter_n[4] },
522 { "n5", vtype_filter_int, &filter_n[5] },
523 { "n6", vtype_filter_int, &filter_n[6] },
524 { "n7", vtype_filter_int, &filter_n[7] },
525 { "n8", vtype_filter_int, &filter_n[8] },
526 { "n9", vtype_filter_int, &filter_n[9] },
527 { "original_domain", vtype_stringptr, &deliver_domain_orig },
528 { "original_local_part", vtype_stringptr, &deliver_localpart_orig },
529 { "originator_gid", vtype_gid, &originator_gid },
530 { "originator_uid", vtype_uid, &originator_uid },
531 { "parent_domain", vtype_stringptr, &deliver_domain_parent },
532 { "parent_local_part", vtype_stringptr, &deliver_localpart_parent },
533 { "pid", vtype_pid, NULL },
534 { "primary_hostname", vtype_stringptr, &primary_hostname },
535 { "prvscheck_address", vtype_stringptr, &prvscheck_address },
536 { "prvscheck_keynum", vtype_stringptr, &prvscheck_keynum },
537 { "prvscheck_result", vtype_stringptr, &prvscheck_result },
538 { "qualify_domain", vtype_stringptr, &qualify_domain_sender },
539 { "qualify_recipient", vtype_stringptr, &qualify_domain_recipient },
540 { "rcpt_count", vtype_int, &rcpt_count },
541 { "rcpt_defer_count", vtype_int, &rcpt_defer_count },
542 { "rcpt_fail_count", vtype_int, &rcpt_fail_count },
543 { "received_count", vtype_int, &received_count },
544 { "received_for", vtype_stringptr, &received_for },
545 { "received_ip_address", vtype_stringptr, &interface_address },
546 { "received_port", vtype_int, &interface_port },
547 { "received_protocol", vtype_stringptr, &received_protocol },
548 { "received_time", vtype_int, &received_time },
549 { "recipient_data", vtype_stringptr, &recipient_data },
550 { "recipient_verify_failure",vtype_stringptr,&recipient_verify_failure },
551 { "recipients", vtype_recipients, NULL },
552 { "recipients_count", vtype_int, &recipients_count },
553 #ifdef WITH_CONTENT_SCAN
554 { "regex_match_string", vtype_stringptr, &regex_match_string },
555 #endif
556 { "reply_address", vtype_reply, NULL },
557 { "return_path", vtype_stringptr, &return_path },
558 { "return_size_limit", vtype_int, &bounce_return_size_limit },
559 { "runrc", vtype_int, &runrc },
560 { "self_hostname", vtype_stringptr, &self_hostname },
561 { "sender_address", vtype_stringptr, &sender_address },
562 { "sender_address_data", vtype_stringptr, &sender_address_data },
563 { "sender_address_domain", vtype_domain, &sender_address },
564 { "sender_address_local_part", vtype_localpart, &sender_address },
565 { "sender_data", vtype_stringptr, &sender_data },
566 { "sender_fullhost", vtype_stringptr, &sender_fullhost },
567 { "sender_helo_name", vtype_stringptr, &sender_helo_name },
568 { "sender_host_address", vtype_stringptr, &sender_host_address },
569 { "sender_host_authenticated",vtype_stringptr, &sender_host_authenticated },
570 { "sender_host_dnssec", vtype_bool, &sender_host_dnssec },
571 { "sender_host_name", vtype_host_lookup, NULL },
572 { "sender_host_port", vtype_int, &sender_host_port },
573 { "sender_ident", vtype_stringptr, &sender_ident },
574 { "sender_rate", vtype_stringptr, &sender_rate },
575 { "sender_rate_limit", vtype_stringptr, &sender_rate_limit },
576 { "sender_rate_period", vtype_stringptr, &sender_rate_period },
577 { "sender_rcvhost", vtype_stringptr, &sender_rcvhost },
578 { "sender_verify_failure",vtype_stringptr, &sender_verify_failure },
579 { "sending_ip_address", vtype_stringptr, &sending_ip_address },
580 { "sending_port", vtype_int, &sending_port },
581 { "smtp_active_hostname", vtype_stringptr, &smtp_active_hostname },
582 { "smtp_command", vtype_stringptr, &smtp_cmd_buffer },
583 { "smtp_command_argument", vtype_stringptr, &smtp_cmd_argument },
584 { "smtp_count_at_connection_start", vtype_int, &smtp_accept_count },
585 { "smtp_notquit_reason", vtype_stringptr, &smtp_notquit_reason },
586 { "sn0", vtype_filter_int, &filter_sn[0] },
587 { "sn1", vtype_filter_int, &filter_sn[1] },
588 { "sn2", vtype_filter_int, &filter_sn[2] },
589 { "sn3", vtype_filter_int, &filter_sn[3] },
590 { "sn4", vtype_filter_int, &filter_sn[4] },
591 { "sn5", vtype_filter_int, &filter_sn[5] },
592 { "sn6", vtype_filter_int, &filter_sn[6] },
593 { "sn7", vtype_filter_int, &filter_sn[7] },
594 { "sn8", vtype_filter_int, &filter_sn[8] },
595 { "sn9", vtype_filter_int, &filter_sn[9] },
596 #ifdef WITH_CONTENT_SCAN
597 { "spam_bar", vtype_stringptr, &spam_bar },
598 { "spam_report", vtype_stringptr, &spam_report },
599 { "spam_score", vtype_stringptr, &spam_score },
600 { "spam_score_int", vtype_stringptr, &spam_score_int },
601 #endif
602 #ifdef EXPERIMENTAL_SPF
603 { "spf_guess", vtype_stringptr, &spf_guess },
604 { "spf_header_comment", vtype_stringptr, &spf_header_comment },
605 { "spf_received", vtype_stringptr, &spf_received },
606 { "spf_result", vtype_stringptr, &spf_result },
607 { "spf_smtp_comment", vtype_stringptr, &spf_smtp_comment },
608 #endif
609 { "spool_directory", vtype_stringptr, &spool_directory },
610 { "spool_inodes", vtype_pinodes, (void *)TRUE },
611 { "spool_space", vtype_pspace, (void *)TRUE },
612 #ifdef EXPERIMENTAL_SRS
613 { "srs_db_address", vtype_stringptr, &srs_db_address },
614 { "srs_db_key", vtype_stringptr, &srs_db_key },
615 { "srs_orig_recipient", vtype_stringptr, &srs_orig_recipient },
616 { "srs_orig_sender", vtype_stringptr, &srs_orig_sender },
617 { "srs_recipient", vtype_stringptr, &srs_recipient },
618 { "srs_status", vtype_stringptr, &srs_status },
619 #endif
620 { "thisaddress", vtype_stringptr, &filter_thisaddress },
621
622 /* The non-(in,out) variables are now deprecated */
623 { "tls_bits", vtype_int, &tls_in.bits },
624 { "tls_certificate_verified", vtype_int, &tls_in.certificate_verified },
625 { "tls_cipher", vtype_stringptr, &tls_in.cipher },
626
627 { "tls_in_bits", vtype_int, &tls_in.bits },
628 { "tls_in_certificate_verified", vtype_int, &tls_in.certificate_verified },
629 { "tls_in_cipher", vtype_stringptr, &tls_in.cipher },
630 { "tls_in_peerdn", vtype_stringptr, &tls_in.peerdn },
631 #if defined(SUPPORT_TLS) && !defined(USE_GNUTLS)
632 { "tls_in_sni", vtype_stringptr, &tls_in.sni },
633 #endif
634 { "tls_out_bits", vtype_int, &tls_out.bits },
635 { "tls_out_certificate_verified", vtype_int,&tls_out.certificate_verified },
636 { "tls_out_cipher", vtype_stringptr, &tls_out.cipher },
637 { "tls_out_peerdn", vtype_stringptr, &tls_out.peerdn },
638 #if defined(SUPPORT_TLS) && !defined(USE_GNUTLS)
639 { "tls_out_sni", vtype_stringptr, &tls_out.sni },
640 #endif
641
642 { "tls_peerdn", vtype_stringptr, &tls_in.peerdn }, /* mind the alphabetical order! */
643 #if defined(SUPPORT_TLS) && !defined(USE_GNUTLS)
644 { "tls_sni", vtype_stringptr, &tls_in.sni }, /* mind the alphabetical order! */
645 #endif
646
647 { "tod_bsdinbox", vtype_todbsdin, NULL },
648 { "tod_epoch", vtype_tode, NULL },
649 { "tod_epoch_l", vtype_todel, NULL },
650 { "tod_full", vtype_todf, NULL },
651 { "tod_log", vtype_todl, NULL },
652 { "tod_logfile", vtype_todlf, NULL },
653 { "tod_zone", vtype_todzone, NULL },
654 { "tod_zulu", vtype_todzulu, NULL },
655 { "value", vtype_stringptr, &lookup_value },
656 { "version_number", vtype_stringptr, &version_string },
657 { "warn_message_delay", vtype_stringptr, &warnmsg_delay },
658 { "warn_message_recipient",vtype_stringptr, &warnmsg_recipients },
659 { "warn_message_recipients",vtype_stringptr,&warnmsg_recipients },
660 { "warnmsg_delay", vtype_stringptr, &warnmsg_delay },
661 { "warnmsg_recipient", vtype_stringptr, &warnmsg_recipients },
662 { "warnmsg_recipients", vtype_stringptr, &warnmsg_recipients }
663 };
664
665 static int var_table_size = sizeof(var_table)/sizeof(var_entry);
666 static uschar var_buffer[256];
667 static BOOL malformed_header;
668
669 /* For textual hashes */
670
671 static const char *hashcodes = "abcdefghijklmnopqrtsuvwxyz"
672 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
673 "0123456789";
674
675 enum { HMAC_MD5, HMAC_SHA1 };
676
677 /* For numeric hashes */
678
679 static unsigned int prime[] = {
680 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
681 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
682 73, 79, 83, 89, 97, 101, 103, 107, 109, 113};
683
684 /* For printing modes in symbolic form */
685
686 static uschar *mtable_normal[] =
687 { US"---", US"--x", US"-w-", US"-wx", US"r--", US"r-x", US"rw-", US"rwx" };
688
689 static uschar *mtable_setid[] =
690 { US"--S", US"--s", US"-wS", US"-ws", US"r-S", US"r-s", US"rwS", US"rws" };
691
692 static uschar *mtable_sticky[] =
693 { US"--T", US"--t", US"-wT", US"-wt", US"r-T", US"r-t", US"rwT", US"rwt" };
694
695
696
697 /*************************************************
698 * Tables for UTF-8 support *
699 *************************************************/
700
701 /* Table of the number of extra characters, indexed by the first character
702 masked with 0x3f. The highest number for a valid UTF-8 character is in fact
703 0x3d. */
704
705 static uschar utf8_table1[] = {
706 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
707 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
708 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
709 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5 };
710
711 /* These are the masks for the data bits in the first byte of a character,
712 indexed by the number of additional bytes. */
713
714 static int utf8_table2[] = { 0xff, 0x1f, 0x0f, 0x07, 0x03, 0x01};
715
716 /* Get the next UTF-8 character, advancing the pointer. */
717
718 #define GETUTF8INC(c, ptr) \
719 c = *ptr++; \
720 if ((c & 0xc0) == 0xc0) \
721 { \
722 int a = utf8_table1[c & 0x3f]; /* Number of additional bytes */ \
723 int s = 6*a; \
724 c = (c & utf8_table2[a]) << s; \
725 while (a-- > 0) \
726 { \
727 s -= 6; \
728 c |= (*ptr++ & 0x3f) << s; \
729 } \
730 }
731
732
733 /*************************************************
734 * Binary chop search on a table *
735 *************************************************/
736
737 /* This is used for matching expansion items and operators.
738
739 Arguments:
740 name the name that is being sought
741 table the table to search
742 table_size the number of items in the table
743
744 Returns: the offset in the table, or -1
745 */
746
747 static int
748 chop_match(uschar *name, uschar **table, int table_size)
749 {
750 uschar **bot = table;
751 uschar **top = table + table_size;
752
753 while (top > bot)
754 {
755 uschar **mid = bot + (top - bot)/2;
756 int c = Ustrcmp(name, *mid);
757 if (c == 0) return mid - table;
758 if (c > 0) bot = mid + 1; else top = mid;
759 }
760
761 return -1;
762 }
763
764
765
766 /*************************************************
767 * Check a condition string *
768 *************************************************/
769
770 /* This function is called to expand a string, and test the result for a "true"
771 or "false" value. Failure of the expansion yields FALSE; logged unless it was a
772 forced fail or lookup defer. All store used by the function can be released on
773 exit.
774
775 The actual false-value tests should be replicated for ECOND_BOOL_LAX.
776
777 Arguments:
778 condition the condition string
779 m1 text to be incorporated in panic error
780 m2 ditto
781
782 Returns: TRUE if condition is met, FALSE if not
783 */
784
785 BOOL
786 expand_check_condition(uschar *condition, uschar *m1, uschar *m2)
787 {
788 int rc;
789 void *reset_point = store_get(0);
790 uschar *ss = expand_string(condition);
791 if (ss == NULL)
792 {
793 if (!expand_string_forcedfail && !search_find_defer)
794 log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand condition \"%s\" "
795 "for %s %s: %s", condition, m1, m2, expand_string_message);
796 return FALSE;
797 }
798 rc = ss[0] != 0 && Ustrcmp(ss, "0") != 0 && strcmpic(ss, US"no") != 0 &&
799 strcmpic(ss, US"false") != 0;
800 store_reset(reset_point);
801 return rc;
802 }
803
804
805
806
807 /*************************************************
808 * Pseudo-random number generation *
809 *************************************************/
810
811 /* Pseudo-random number generation. The result is not "expected" to be
812 cryptographically strong but not so weak that someone will shoot themselves
813 in the foot using it as a nonce in some email header scheme or whatever
814 weirdness they'll twist this into. The result should ideally handle fork().
815
816 However, if we're stuck unable to provide this, then we'll fall back to
817 appallingly bad randomness.
818
819 If SUPPORT_TLS is defined then this will not be used except as an emergency
820 fallback.
821
822 Arguments:
823 max range maximum
824 Returns a random number in range [0, max-1]
825 */
826
827 #ifdef SUPPORT_TLS
828 # define vaguely_random_number vaguely_random_number_fallback
829 #endif
830 int
831 vaguely_random_number(int max)
832 {
833 #ifdef SUPPORT_TLS
834 # undef vaguely_random_number
835 #endif
836 static pid_t pid = 0;
837 pid_t p2;
838 #if defined(HAVE_SRANDOM) && !defined(HAVE_SRANDOMDEV)
839 struct timeval tv;
840 #endif
841
842 p2 = getpid();
843 if (p2 != pid)
844 {
845 if (pid != 0)
846 {
847
848 #ifdef HAVE_ARC4RANDOM
849 /* cryptographically strong randomness, common on *BSD platforms, not
850 so much elsewhere. Alas. */
851 arc4random_stir();
852 #elif defined(HAVE_SRANDOM) || defined(HAVE_SRANDOMDEV)
853 #ifdef HAVE_SRANDOMDEV
854 /* uses random(4) for seeding */
855 srandomdev();
856 #else
857 gettimeofday(&tv, NULL);
858 srandom(tv.tv_sec | tv.tv_usec | getpid());
859 #endif
860 #else
861 /* Poor randomness and no seeding here */
862 #endif
863
864 }
865 pid = p2;
866 }
867
868 #ifdef HAVE_ARC4RANDOM
869 return arc4random() % max;
870 #elif defined(HAVE_SRANDOM) || defined(HAVE_SRANDOMDEV)
871 return random() % max;
872 #else
873 /* This one returns a 16-bit number, definitely not crypto-strong */
874 return random_number(max);
875 #endif
876 }
877
878
879
880
881 /*************************************************
882 * Pick out a name from a string *
883 *************************************************/
884
885 /* If the name is too long, it is silently truncated.
886
887 Arguments:
888 name points to a buffer into which to put the name
889 max is the length of the buffer
890 s points to the first alphabetic character of the name
891 extras chars other than alphanumerics to permit
892
893 Returns: pointer to the first character after the name
894
895 Note: The test for *s != 0 in the while loop is necessary because
896 Ustrchr() yields non-NULL if the character is zero (which is not something
897 I expected). */
898
899 static uschar *
900 read_name(uschar *name, int max, uschar *s, uschar *extras)
901 {
902 int ptr = 0;
903 while (*s != 0 && (isalnum(*s) || Ustrchr(extras, *s) != NULL))
904 {
905 if (ptr < max-1) name[ptr++] = *s;
906 s++;
907 }
908 name[ptr] = 0;
909 return s;
910 }
911
912
913
914 /*************************************************
915 * Pick out the rest of a header name *
916 *************************************************/
917
918 /* A variable name starting $header_ (or just $h_ for those who like
919 abbreviations) might not be the complete header name because headers can
920 contain any printing characters in their names, except ':'. This function is
921 called to read the rest of the name, chop h[eader]_ off the front, and put ':'
922 on the end, if the name was terminated by white space.
923
924 Arguments:
925 name points to a buffer in which the name read so far exists
926 max is the length of the buffer
927 s points to the first character after the name so far, i.e. the
928 first non-alphameric character after $header_xxxxx
929
930 Returns: a pointer to the first character after the header name
931 */
932
933 static uschar *
934 read_header_name(uschar *name, int max, uschar *s)
935 {
936 int prelen = Ustrchr(name, '_') - name + 1;
937 int ptr = Ustrlen(name) - prelen;
938 if (ptr > 0) memmove(name, name+prelen, ptr);
939 while (mac_isgraph(*s) && *s != ':')
940 {
941 if (ptr < max-1) name[ptr++] = *s;
942 s++;
943 }
944 if (*s == ':') s++;
945 name[ptr++] = ':';
946 name[ptr] = 0;
947 return s;
948 }
949
950
951
952 /*************************************************
953 * Pick out a number from a string *
954 *************************************************/
955
956 /* Arguments:
957 n points to an integer into which to put the number
958 s points to the first digit of the number
959
960 Returns: a pointer to the character after the last digit
961 */
962
963 static uschar *
964 read_number(int *n, uschar *s)
965 {
966 *n = 0;
967 while (isdigit(*s)) *n = *n * 10 + (*s++ - '0');
968 return s;
969 }
970
971
972
973 /*************************************************
974 * Extract keyed subfield from a string *
975 *************************************************/
976
977 /* The yield is in dynamic store; NULL means that the key was not found.
978
979 Arguments:
980 key points to the name of the key
981 s points to the string from which to extract the subfield
982
983 Returns: NULL if the subfield was not found, or
984 a pointer to the subfield's data
985 */
986
987 static uschar *
988 expand_getkeyed(uschar *key, uschar *s)
989 {
990 int length = Ustrlen(key);
991 while (isspace(*s)) s++;
992
993 /* Loop to search for the key */
994
995 while (*s != 0)
996 {
997 int dkeylength;
998 uschar *data;
999 uschar *dkey = s;
1000
1001 while (*s != 0 && *s != '=' && !isspace(*s)) s++;
1002 dkeylength = s - dkey;
1003 while (isspace(*s)) s++;
1004 if (*s == '=') while (isspace((*(++s))));
1005
1006 data = string_dequote(&s);
1007 if (length == dkeylength && strncmpic(key, dkey, length) == 0)
1008 return data;
1009
1010 while (isspace(*s)) s++;
1011 }
1012
1013 return NULL;
1014 }
1015
1016
1017
1018
1019 /*************************************************
1020 * Extract numbered subfield from string *
1021 *************************************************/
1022
1023 /* Extracts a numbered field from a string that is divided by tokens - for
1024 example a line from /etc/passwd is divided by colon characters. First field is
1025 numbered one. Negative arguments count from the right. Zero returns the whole
1026 string. Returns NULL if there are insufficient tokens in the string
1027
1028 ***WARNING***
1029 Modifies final argument - this is a dynamically generated string, so that's OK.
1030
1031 Arguments:
1032 field number of field to be extracted,
1033 first field = 1, whole string = 0, last field = -1
1034 separators characters that are used to break string into tokens
1035 s points to the string from which to extract the subfield
1036
1037 Returns: NULL if the field was not found,
1038 a pointer to the field's data inside s (modified to add 0)
1039 */
1040
1041 static uschar *
1042 expand_gettokened (int field, uschar *separators, uschar *s)
1043 {
1044 int sep = 1;
1045 int count;
1046 uschar *ss = s;
1047 uschar *fieldtext = NULL;
1048
1049 if (field == 0) return s;
1050
1051 /* Break the line up into fields in place; for field > 0 we stop when we have
1052 done the number of fields we want. For field < 0 we continue till the end of
1053 the string, counting the number of fields. */
1054
1055 count = (field > 0)? field : INT_MAX;
1056
1057 while (count-- > 0)
1058 {
1059 size_t len;
1060
1061 /* Previous field was the last one in the string. For a positive field
1062 number, this means there are not enough fields. For a negative field number,
1063 check that there are enough, and scan back to find the one that is wanted. */
1064
1065 if (sep == 0)
1066 {
1067 if (field > 0 || (-field) > (INT_MAX - count - 1)) return NULL;
1068 if ((-field) == (INT_MAX - count - 1)) return s;
1069 while (field++ < 0)
1070 {
1071 ss--;
1072 while (ss[-1] != 0) ss--;
1073 }
1074 fieldtext = ss;
1075 break;
1076 }
1077
1078 /* Previous field was not last in the string; save its start and put a
1079 zero at its end. */
1080
1081 fieldtext = ss;
1082 len = Ustrcspn(ss, separators);
1083 sep = ss[len];
1084 ss[len] = 0;
1085 ss += len + 1;
1086 }
1087
1088 return fieldtext;
1089 }
1090
1091
1092
1093 /*************************************************
1094 * Extract a substring from a string *
1095 *************************************************/
1096
1097 /* Perform the ${substr or ${length expansion operations.
1098
1099 Arguments:
1100 subject the input string
1101 value1 the offset from the start of the input string to the start of
1102 the output string; if negative, count from the right.
1103 value2 the length of the output string, or negative (-1) for unset
1104 if value1 is positive, unset means "all after"
1105 if value1 is negative, unset means "all before"
1106 len set to the length of the returned string
1107
1108 Returns: pointer to the output string, or NULL if there is an error
1109 */
1110
1111 static uschar *
1112 extract_substr(uschar *subject, int value1, int value2, int *len)
1113 {
1114 int sublen = Ustrlen(subject);
1115
1116 if (value1 < 0) /* count from right */
1117 {
1118 value1 += sublen;
1119
1120 /* If the position is before the start, skip to the start, and adjust the
1121 length. If the length ends up negative, the substring is null because nothing
1122 can precede. This falls out naturally when the length is unset, meaning "all
1123 to the left". */
1124
1125 if (value1 < 0)
1126 {
1127 value2 += value1;
1128 if (value2 < 0) value2 = 0;
1129 value1 = 0;
1130 }
1131
1132 /* Otherwise an unset length => characters before value1 */
1133
1134 else if (value2 < 0)
1135 {
1136 value2 = value1;
1137 value1 = 0;
1138 }
1139 }
1140
1141 /* For a non-negative offset, if the starting position is past the end of the
1142 string, the result will be the null string. Otherwise, an unset length means
1143 "rest"; just set it to the maximum - it will be cut down below if necessary. */
1144
1145 else
1146 {
1147 if (value1 > sublen)
1148 {
1149 value1 = sublen;
1150 value2 = 0;
1151 }
1152 else if (value2 < 0) value2 = sublen;
1153 }
1154
1155 /* Cut the length down to the maximum possible for the offset value, and get
1156 the required characters. */
1157
1158 if (value1 + value2 > sublen) value2 = sublen - value1;
1159 *len = value2;
1160 return subject + value1;
1161 }
1162
1163
1164
1165
1166 /*************************************************
1167 * Old-style hash of a string *
1168 *************************************************/
1169
1170 /* Perform the ${hash expansion operation.
1171
1172 Arguments:
1173 subject the input string (an expanded substring)
1174 value1 the length of the output string; if greater or equal to the
1175 length of the input string, the input string is returned
1176 value2 the number of hash characters to use, or 26 if negative
1177 len set to the length of the returned string
1178
1179 Returns: pointer to the output string, or NULL if there is an error
1180 */
1181
1182 static uschar *
1183 compute_hash(uschar *subject, int value1, int value2, int *len)
1184 {
1185 int sublen = Ustrlen(subject);
1186
1187 if (value2 < 0) value2 = 26;
1188 else if (value2 > Ustrlen(hashcodes))
1189 {
1190 expand_string_message =
1191 string_sprintf("hash count \"%d\" too big", value2);
1192 return NULL;
1193 }
1194
1195 /* Calculate the hash text. We know it is shorter than the original string, so
1196 can safely place it in subject[] (we know that subject is always itself an
1197 expanded substring). */
1198
1199 if (value1 < sublen)
1200 {
1201 int c;
1202 int i = 0;
1203 int j = value1;
1204 while ((c = (subject[j])) != 0)
1205 {
1206 int shift = (c + j++) & 7;
1207 subject[i] ^= (c << shift) | (c >> (8-shift));
1208 if (++i >= value1) i = 0;
1209 }
1210 for (i = 0; i < value1; i++)
1211 subject[i] = hashcodes[(subject[i]) % value2];
1212 }
1213 else value1 = sublen;
1214
1215 *len = value1;
1216 return subject;
1217 }
1218
1219
1220
1221
1222 /*************************************************
1223 * Numeric hash of a string *
1224 *************************************************/
1225
1226 /* Perform the ${nhash expansion operation. The first characters of the
1227 string are treated as most important, and get the highest prime numbers.
1228
1229 Arguments:
1230 subject the input string
1231 value1 the maximum value of the first part of the result
1232 value2 the maximum value of the second part of the result,
1233 or negative to produce only a one-part result
1234 len set to the length of the returned string
1235
1236 Returns: pointer to the output string, or NULL if there is an error.
1237 */
1238
1239 static uschar *
1240 compute_nhash (uschar *subject, int value1, int value2, int *len)
1241 {
1242 uschar *s = subject;
1243 int i = 0;
1244 unsigned long int total = 0; /* no overflow */
1245
1246 while (*s != 0)
1247 {
1248 if (i == 0) i = sizeof(prime)/sizeof(int) - 1;
1249 total += prime[i--] * (unsigned int)(*s++);
1250 }
1251
1252 /* If value2 is unset, just compute one number */
1253
1254 if (value2 < 0)
1255 {
1256 s = string_sprintf("%d", total % value1);
1257 }
1258
1259 /* Otherwise do a div/mod hash */
1260
1261 else
1262 {
1263 total = total % (value1 * value2);
1264 s = string_sprintf("%d/%d", total/value2, total % value2);
1265 }
1266
1267 *len = Ustrlen(s);
1268 return s;
1269 }
1270
1271
1272
1273
1274
1275 /*************************************************
1276 * Find the value of a header or headers *
1277 *************************************************/
1278
1279 /* Multiple instances of the same header get concatenated, and this function
1280 can also return a concatenation of all the header lines. When concatenating
1281 specific headers that contain lists of addresses, a comma is inserted between
1282 them. Otherwise we use a straight concatenation. Because some messages can have
1283 pathologically large number of lines, there is a limit on the length that is
1284 returned. Also, to avoid massive store use which would result from using
1285 string_cat() as it copies and extends strings, we do a preliminary pass to find
1286 out exactly how much store will be needed. On "normal" messages this will be
1287 pretty trivial.
1288
1289 Arguments:
1290 name the name of the header, without the leading $header_ or $h_,
1291 or NULL if a concatenation of all headers is required
1292 exists_only TRUE if called from a def: test; don't need to build a string;
1293 just return a string that is not "" and not "0" if the header
1294 exists
1295 newsize return the size of memory block that was obtained; may be NULL
1296 if exists_only is TRUE
1297 want_raw TRUE if called for $rh_ or $rheader_ variables; no processing,
1298 other than concatenating, will be done on the header. Also used
1299 for $message_headers_raw.
1300 charset name of charset to translate MIME words to; used only if
1301 want_raw is false; if NULL, no translation is done (this is
1302 used for $bh_ and $bheader_)
1303
1304 Returns: NULL if the header does not exist, else a pointer to a new
1305 store block
1306 */
1307
1308 static uschar *
1309 find_header(uschar *name, BOOL exists_only, int *newsize, BOOL want_raw,
1310 uschar *charset)
1311 {
1312 BOOL found = name == NULL;
1313 int comma = 0;
1314 int len = found? 0 : Ustrlen(name);
1315 int i;
1316 uschar *yield = NULL;
1317 uschar *ptr = NULL;
1318
1319 /* Loop for two passes - saves code repetition */
1320
1321 for (i = 0; i < 2; i++)
1322 {
1323 int size = 0;
1324 header_line *h;
1325
1326 for (h = header_list; size < header_insert_maxlen && h != NULL; h = h->next)
1327 {
1328 if (h->type != htype_old && h->text != NULL) /* NULL => Received: placeholder */
1329 {
1330 if (name == NULL || (len <= h->slen && strncmpic(name, h->text, len) == 0))
1331 {
1332 int ilen;
1333 uschar *t;
1334
1335 if (exists_only) return US"1"; /* don't need actual string */
1336 found = TRUE;
1337 t = h->text + len; /* text to insert */
1338 if (!want_raw) /* unless wanted raw, */
1339 while (isspace(*t)) t++; /* remove leading white space */
1340 ilen = h->slen - (t - h->text); /* length to insert */
1341
1342 /* Unless wanted raw, remove trailing whitespace, including the
1343 newline. */
1344
1345 if (!want_raw)
1346 while (ilen > 0 && isspace(t[ilen-1])) ilen--;
1347
1348 /* Set comma = 1 if handling a single header and it's one of those
1349 that contains an address list, except when asked for raw headers. Only
1350 need to do this once. */
1351
1352 if (!want_raw && name != NULL && comma == 0 &&
1353 Ustrchr("BCFRST", h->type) != NULL)
1354 comma = 1;
1355
1356 /* First pass - compute total store needed; second pass - compute
1357 total store used, including this header. */
1358
1359 size += ilen + comma + 1; /* +1 for the newline */
1360
1361 /* Second pass - concatentate the data, up to a maximum. Note that
1362 the loop stops when size hits the limit. */
1363
1364 if (i != 0)
1365 {
1366 if (size > header_insert_maxlen)
1367 {
1368 ilen -= size - header_insert_maxlen - 1;
1369 comma = 0;
1370 }
1371 Ustrncpy(ptr, t, ilen);
1372 ptr += ilen;
1373
1374 /* For a non-raw header, put in the comma if needed, then add
1375 back the newline we removed above, provided there was some text in
1376 the header. */
1377
1378 if (!want_raw && ilen > 0)
1379 {
1380 if (comma != 0) *ptr++ = ',';
1381 *ptr++ = '\n';
1382 }
1383 }
1384 }
1385 }
1386 }
1387
1388 /* At end of first pass, return NULL if no header found. Then truncate size
1389 if necessary, and get the buffer to hold the data, returning the buffer size.
1390 */
1391
1392 if (i == 0)
1393 {
1394 if (!found) return NULL;
1395 if (size > header_insert_maxlen) size = header_insert_maxlen;
1396 *newsize = size + 1;
1397 ptr = yield = store_get(*newsize);
1398 }
1399 }
1400
1401 /* That's all we do for raw header expansion. */
1402
1403 if (want_raw)
1404 {
1405 *ptr = 0;
1406 }
1407
1408 /* Otherwise, remove a final newline and a redundant added comma. Then we do
1409 RFC 2047 decoding, translating the charset if requested. The rfc2047_decode2()
1410 function can return an error with decoded data if the charset translation
1411 fails. If decoding fails, it returns NULL. */
1412
1413 else
1414 {
1415 uschar *decoded, *error;
1416 if (ptr > yield && ptr[-1] == '\n') ptr--;
1417 if (ptr > yield && comma != 0 && ptr[-1] == ',') ptr--;
1418 *ptr = 0;
1419 decoded = rfc2047_decode2(yield, check_rfc2047_length, charset, '?', NULL,
1420 newsize, &error);
1421 if (error != NULL)
1422 {
1423 DEBUG(D_any) debug_printf("*** error in RFC 2047 decoding: %s\n"
1424 " input was: %s\n", error, yield);
1425 }
1426 if (decoded != NULL) yield = decoded;
1427 }
1428
1429 return yield;
1430 }
1431
1432
1433
1434
1435 /*************************************************
1436 * Find value of a variable *
1437 *************************************************/
1438
1439 /* The table of variables is kept in alphabetic order, so we can search it
1440 using a binary chop. The "choplen" variable is nothing to do with the binary
1441 chop.
1442
1443 Arguments:
1444 name the name of the variable being sought
1445 exists_only TRUE if this is a def: test; passed on to find_header()
1446 skipping TRUE => skip any processing evaluation; this is not the same as
1447 exists_only because def: may test for values that are first
1448 evaluated here
1449 newsize pointer to an int which is initially zero; if the answer is in
1450 a new memory buffer, *newsize is set to its size
1451
1452 Returns: NULL if the variable does not exist, or
1453 a pointer to the variable's contents, or
1454 something non-NULL if exists_only is TRUE
1455 */
1456
1457 static uschar *
1458 find_variable(uschar *name, BOOL exists_only, BOOL skipping, int *newsize)
1459 {
1460 int first = 0;
1461 int last = var_table_size;
1462
1463 /* Handle ACL variables, whose names are of the form acl_cxxx or acl_mxxx.
1464 Originally, xxx had to be a number in the range 0-9 (later 0-19), but from
1465 release 4.64 onwards arbitrary names are permitted, as long as the first 5
1466 characters are acl_c or acl_m and the sixth is either a digit or an underscore
1467 (this gave backwards compatibility at the changeover). There may be built-in
1468 variables whose names start acl_ but they should never start in this way. This
1469 slightly messy specification is a consequence of the history, needless to say.
1470
1471 If an ACL variable does not exist, treat it as empty, unless strict_acl_vars is
1472 set, in which case give an error. */
1473
1474 if ((Ustrncmp(name, "acl_c", 5) == 0 || Ustrncmp(name, "acl_m", 5) == 0) &&
1475 !isalpha(name[5]))
1476 {
1477 tree_node *node =
1478 tree_search((name[4] == 'c')? acl_var_c : acl_var_m, name + 4);
1479 return (node == NULL)? (strict_acl_vars? NULL : US"") : node->data.ptr;
1480 }
1481
1482 /* Handle $auth<n> variables. */
1483
1484 if (Ustrncmp(name, "auth", 4) == 0)
1485 {
1486 uschar *endptr;
1487 int n = Ustrtoul(name + 4, &endptr, 10);
1488 if (*endptr == 0 && n != 0 && n <= AUTH_VARS)
1489 return (auth_vars[n-1] == NULL)? US"" : auth_vars[n-1];
1490 }
1491
1492 /* For all other variables, search the table */
1493
1494 while (last > first)
1495 {
1496 uschar *s, *domain;
1497 uschar **ss;
1498 int middle = (first + last)/2;
1499 int c = Ustrcmp(name, var_table[middle].name);
1500
1501 if (c > 0) { first = middle + 1; continue; }
1502 if (c < 0) { last = middle; continue; }
1503
1504 /* Found an existing variable. If in skipping state, the value isn't needed,
1505 and we want to avoid processing (such as looking up the host name). */
1506
1507 if (skipping) return US"";
1508
1509 switch (var_table[middle].type)
1510 {
1511 case vtype_filter_int:
1512 if (!filter_running) return NULL;
1513 /* Fall through */
1514 /* VVVVVVVVVVVV */
1515 case vtype_int:
1516 sprintf(CS var_buffer, "%d", *(int *)(var_table[middle].value)); /* Integer */
1517 return var_buffer;
1518
1519 case vtype_ino:
1520 sprintf(CS var_buffer, "%ld", (long int)(*(ino_t *)(var_table[middle].value))); /* Inode */
1521 return var_buffer;
1522
1523 case vtype_gid:
1524 sprintf(CS var_buffer, "%ld", (long int)(*(gid_t *)(var_table[middle].value))); /* gid */
1525 return var_buffer;
1526
1527 case vtype_uid:
1528 sprintf(CS var_buffer, "%ld", (long int)(*(uid_t *)(var_table[middle].value))); /* uid */
1529 return var_buffer;
1530
1531 case vtype_bool:
1532 sprintf(CS var_buffer, "%s", *(BOOL *)(var_table[middle].value) ? "yes" : "no"); /* bool */
1533 return var_buffer;
1534
1535 case vtype_stringptr: /* Pointer to string */
1536 s = *((uschar **)(var_table[middle].value));
1537 return (s == NULL)? US"" : s;
1538
1539 case vtype_pid:
1540 sprintf(CS var_buffer, "%d", (int)getpid()); /* pid */
1541 return var_buffer;
1542
1543 case vtype_load_avg:
1544 sprintf(CS var_buffer, "%d", OS_GETLOADAVG()); /* load_average */
1545 return var_buffer;
1546
1547 case vtype_host_lookup: /* Lookup if not done so */
1548 if (sender_host_name == NULL && sender_host_address != NULL &&
1549 !host_lookup_failed && host_name_lookup() == OK)
1550 host_build_sender_fullhost();
1551 return (sender_host_name == NULL)? US"" : sender_host_name;
1552
1553 case vtype_localpart: /* Get local part from address */
1554 s = *((uschar **)(var_table[middle].value));
1555 if (s == NULL) return US"";
1556 domain = Ustrrchr(s, '@');
1557 if (domain == NULL) return s;
1558 if (domain - s > sizeof(var_buffer) - 1)
1559 log_write(0, LOG_MAIN|LOG_PANIC_DIE, "local part longer than " SIZE_T_FMT
1560 " in string expansion", sizeof(var_buffer));
1561 Ustrncpy(var_buffer, s, domain - s);
1562 var_buffer[domain - s] = 0;
1563 return var_buffer;
1564
1565 case vtype_domain: /* Get domain from address */
1566 s = *((uschar **)(var_table[middle].value));
1567 if (s == NULL) return US"";
1568 domain = Ustrrchr(s, '@');
1569 return (domain == NULL)? US"" : domain + 1;
1570
1571 case vtype_msgheaders:
1572 return find_header(NULL, exists_only, newsize, FALSE, NULL);
1573
1574 case vtype_msgheaders_raw:
1575 return find_header(NULL, exists_only, newsize, TRUE, NULL);
1576
1577 case vtype_msgbody: /* Pointer to msgbody string */
1578 case vtype_msgbody_end: /* Ditto, the end of the msg */
1579 ss = (uschar **)(var_table[middle].value);
1580 if (*ss == NULL && deliver_datafile >= 0) /* Read body when needed */
1581 {
1582 uschar *body;
1583 off_t start_offset = SPOOL_DATA_START_OFFSET;
1584 int len = message_body_visible;
1585 if (len > message_size) len = message_size;
1586 *ss = body = store_malloc(len+1);
1587 body[0] = 0;
1588 if (var_table[middle].type == vtype_msgbody_end)
1589 {
1590 struct stat statbuf;
1591 if (fstat(deliver_datafile, &statbuf) == 0)
1592 {
1593 start_offset = statbuf.st_size - len;
1594 if (start_offset < SPOOL_DATA_START_OFFSET)
1595 start_offset = SPOOL_DATA_START_OFFSET;
1596 }
1597 }
1598 lseek(deliver_datafile, start_offset, SEEK_SET);
1599 len = read(deliver_datafile, body, len);
1600 if (len > 0)
1601 {
1602 body[len] = 0;
1603 if (message_body_newlines) /* Separate loops for efficiency */
1604 {
1605 while (len > 0)
1606 { if (body[--len] == 0) body[len] = ' '; }
1607 }
1608 else
1609 {
1610 while (len > 0)
1611 { if (body[--len] == '\n' || body[len] == 0) body[len] = ' '; }
1612 }
1613 }
1614 }
1615 return (*ss == NULL)? US"" : *ss;
1616
1617 case vtype_todbsdin: /* BSD inbox time of day */
1618 return tod_stamp(tod_bsdin);
1619
1620 case vtype_tode: /* Unix epoch time of day */
1621 return tod_stamp(tod_epoch);
1622
1623 case vtype_todel: /* Unix epoch/usec time of day */
1624 return tod_stamp(tod_epoch_l);
1625
1626 case vtype_todf: /* Full time of day */
1627 return tod_stamp(tod_full);
1628
1629 case vtype_todl: /* Log format time of day */
1630 return tod_stamp(tod_log_bare); /* (without timezone) */
1631
1632 case vtype_todzone: /* Time zone offset only */
1633 return tod_stamp(tod_zone);
1634
1635 case vtype_todzulu: /* Zulu time */
1636 return tod_stamp(tod_zulu);
1637
1638 case vtype_todlf: /* Log file datestamp tod */
1639 return tod_stamp(tod_log_datestamp_daily);
1640
1641 case vtype_reply: /* Get reply address */
1642 s = find_header(US"reply-to:", exists_only, newsize, TRUE,
1643 headers_charset);
1644 if (s != NULL) while (isspace(*s)) s++;
1645 if (s == NULL || *s == 0)
1646 {
1647 *newsize = 0; /* For the *s==0 case */
1648 s = find_header(US"from:", exists_only, newsize, TRUE, headers_charset);
1649 }
1650 if (s != NULL)
1651 {
1652 uschar *t;
1653 while (isspace(*s)) s++;
1654 for (t = s; *t != 0; t++) if (*t == '\n') *t = ' ';
1655 while (t > s && isspace(t[-1])) t--;
1656 *t = 0;
1657 }
1658 return (s == NULL)? US"" : s;
1659
1660 /* A recipients list is available only during system message filtering,
1661 during ACL processing after DATA, and while expanding pipe commands
1662 generated from a system filter, but not elsewhere. */
1663
1664 case vtype_recipients:
1665 if (!enable_dollar_recipients) return NULL; else
1666 {
1667 int size = 128;
1668 int ptr = 0;
1669 int i;
1670 s = store_get(size);
1671 for (i = 0; i < recipients_count; i++)
1672 {
1673 if (i != 0) s = string_cat(s, &size, &ptr, US", ", 2);
1674 s = string_cat(s, &size, &ptr, recipients_list[i].address,
1675 Ustrlen(recipients_list[i].address));
1676 }
1677 s[ptr] = 0; /* string_cat() leaves room */
1678 }
1679 return s;
1680
1681 case vtype_pspace:
1682 {
1683 int inodes;
1684 sprintf(CS var_buffer, "%d",
1685 receive_statvfs(var_table[middle].value == (void *)TRUE, &inodes));
1686 }
1687 return var_buffer;
1688
1689 case vtype_pinodes:
1690 {
1691 int inodes;
1692 (void) receive_statvfs(var_table[middle].value == (void *)TRUE, &inodes);
1693 sprintf(CS var_buffer, "%d", inodes);
1694 }
1695 return var_buffer;
1696
1697 #ifndef DISABLE_DKIM
1698 case vtype_dkim:
1699 return dkim_exim_expand_query((int)(long)var_table[middle].value);
1700 #endif
1701
1702 }
1703 }
1704
1705 return NULL; /* Unknown variable name */
1706 }
1707
1708
1709
1710
1711 void
1712 modify_variable(uschar *name, void * value)
1713 {
1714 int first = 0;
1715 int last = var_table_size;
1716
1717 while (last > first)
1718 {
1719 int middle = (first + last)/2;
1720 int c = Ustrcmp(name, var_table[middle].name);
1721
1722 if (c > 0) { first = middle + 1; continue; }
1723 if (c < 0) { last = middle; continue; }
1724
1725 /* Found an existing variable; change the item it refers to */
1726 var_table[middle].value = value;
1727 return;
1728 }
1729 return; /* Unknown variable name, fail silently */
1730 }
1731
1732
1733
1734
1735
1736 /*************************************************
1737 * Read and expand substrings *
1738 *************************************************/
1739
1740 /* This function is called to read and expand argument substrings for various
1741 expansion items. Some have a minimum requirement that is less than the maximum;
1742 in these cases, the first non-present one is set to NULL.
1743
1744 Arguments:
1745 sub points to vector of pointers to set
1746 n maximum number of substrings
1747 m minimum required
1748 sptr points to current string pointer
1749 skipping the skipping flag
1750 check_end if TRUE, check for final '}'
1751 name name of item, for error message
1752
1753 Returns: 0 OK; string pointer updated
1754 1 curly bracketing error (too few arguments)
1755 2 too many arguments (only if check_end is set); message set
1756 3 other error (expansion failure)
1757 */
1758
1759 static int
1760 read_subs(uschar **sub, int n, int m, uschar **sptr, BOOL skipping,
1761 BOOL check_end, uschar *name)
1762 {
1763 int i;
1764 uschar *s = *sptr;
1765
1766 while (isspace(*s)) s++;
1767 for (i = 0; i < n; i++)
1768 {
1769 if (*s != '{')
1770 {
1771 if (i < m) return 1;
1772 sub[i] = NULL;
1773 break;
1774 }
1775 sub[i] = expand_string_internal(s+1, TRUE, &s, skipping, TRUE);
1776 if (sub[i] == NULL) return 3;
1777 if (*s++ != '}') return 1;
1778 while (isspace(*s)) s++;
1779 }
1780 if (check_end && *s++ != '}')
1781 {
1782 if (s[-1] == '{')
1783 {
1784 expand_string_message = string_sprintf("Too many arguments for \"%s\" "
1785 "(max is %d)", name, n);
1786 return 2;
1787 }
1788 return 1;
1789 }
1790
1791 *sptr = s;
1792 return 0;
1793 }
1794
1795
1796
1797
1798 /*************************************************
1799 * Elaborate message for bad variable *
1800 *************************************************/
1801
1802 /* For the "unknown variable" message, take a look at the variable's name, and
1803 give additional information about possible ACL variables. The extra information
1804 is added on to expand_string_message.
1805
1806 Argument: the name of the variable
1807 Returns: nothing
1808 */
1809
1810 static void
1811 check_variable_error_message(uschar *name)
1812 {
1813 if (Ustrncmp(name, "acl_", 4) == 0)
1814 expand_string_message = string_sprintf("%s (%s)", expand_string_message,
1815 (name[4] == 'c' || name[4] == 'm')?
1816 (isalpha(name[5])?
1817 US"6th character of a user-defined ACL variable must be a digit or underscore" :
1818 US"strict_acl_vars is set" /* Syntax is OK, it has to be this */
1819 ) :
1820 US"user-defined ACL variables must start acl_c or acl_m");
1821 }
1822
1823
1824
1825 /*************************************************
1826 * Read and evaluate a condition *
1827 *************************************************/
1828
1829 /*
1830 Arguments:
1831 s points to the start of the condition text
1832 yield points to a BOOL to hold the result of the condition test;
1833 if NULL, we are just reading through a condition that is
1834 part of an "or" combination to check syntax, or in a state
1835 where the answer isn't required
1836
1837 Returns: a pointer to the first character after the condition, or
1838 NULL after an error
1839 */
1840
1841 static uschar *
1842 eval_condition(uschar *s, BOOL *yield)
1843 {
1844 BOOL testfor = TRUE;
1845 BOOL tempcond, combined_cond;
1846 BOOL *subcondptr;
1847 BOOL sub2_honour_dollar = TRUE;
1848 int i, rc, cond_type, roffset;
1849 int_eximarith_t num[2];
1850 struct stat statbuf;
1851 uschar name[256];
1852 uschar *sub[4];
1853
1854 const pcre *re;
1855 const uschar *rerror;
1856
1857 for (;;)
1858 {
1859 while (isspace(*s)) s++;
1860 if (*s == '!') { testfor = !testfor; s++; } else break;
1861 }
1862
1863 /* Numeric comparisons are symbolic */
1864
1865 if (*s == '=' || *s == '>' || *s == '<')
1866 {
1867 int p = 0;
1868 name[p++] = *s++;
1869 if (*s == '=')
1870 {
1871 name[p++] = '=';
1872 s++;
1873 }
1874 name[p] = 0;
1875 }
1876
1877 /* All other conditions are named */
1878
1879 else s = read_name(name, 256, s, US"_");
1880
1881 /* If we haven't read a name, it means some non-alpha character is first. */
1882
1883 if (name[0] == 0)
1884 {
1885 expand_string_message = string_sprintf("condition name expected, "
1886 "but found \"%.16s\"", s);
1887 return NULL;
1888 }
1889
1890 /* Find which condition we are dealing with, and switch on it */
1891
1892 cond_type = chop_match(name, cond_table, sizeof(cond_table)/sizeof(uschar *));
1893 switch(cond_type)
1894 {
1895 /* def: tests for a non-empty variable, or for the existence of a header. If
1896 yield == NULL we are in a skipping state, and don't care about the answer. */
1897
1898 case ECOND_DEF:
1899 if (*s != ':')
1900 {
1901 expand_string_message = US"\":\" expected after \"def\"";
1902 return NULL;
1903 }
1904
1905 s = read_name(name, 256, s+1, US"_");
1906
1907 /* Test for a header's existence. If the name contains a closing brace
1908 character, this may be a user error where the terminating colon has been
1909 omitted. Set a flag to adjust a subsequent error message in this case. */
1910
1911 if (Ustrncmp(name, "h_", 2) == 0 ||
1912 Ustrncmp(name, "rh_", 3) == 0 ||
1913 Ustrncmp(name, "bh_", 3) == 0 ||
1914 Ustrncmp(name, "header_", 7) == 0 ||
1915 Ustrncmp(name, "rheader_", 8) == 0 ||
1916 Ustrncmp(name, "bheader_", 8) == 0)
1917 {
1918 s = read_header_name(name, 256, s);
1919 if (Ustrchr(name, '}') != NULL) malformed_header = TRUE;
1920 if (yield != NULL) *yield =
1921 (find_header(name, TRUE, NULL, FALSE, NULL) != NULL) == testfor;
1922 }
1923
1924 /* Test for a variable's having a non-empty value. A non-existent variable
1925 causes an expansion failure. */
1926
1927 else
1928 {
1929 uschar *value = find_variable(name, TRUE, yield == NULL, NULL);
1930 if (value == NULL)
1931 {
1932 expand_string_message = (name[0] == 0)?
1933 string_sprintf("variable name omitted after \"def:\"") :
1934 string_sprintf("unknown variable \"%s\" after \"def:\"", name);
1935 check_variable_error_message(name);
1936 return NULL;
1937 }
1938 if (yield != NULL) *yield = (value[0] != 0) == testfor;
1939 }
1940
1941 return s;
1942
1943
1944 /* first_delivery tests for first delivery attempt */
1945
1946 case ECOND_FIRST_DELIVERY:
1947 if (yield != NULL) *yield = deliver_firsttime == testfor;
1948 return s;
1949
1950
1951 /* queue_running tests for any process started by a queue runner */
1952
1953 case ECOND_QUEUE_RUNNING:
1954 if (yield != NULL) *yield = (queue_run_pid != (pid_t)0) == testfor;
1955 return s;
1956
1957
1958 /* exists: tests for file existence
1959 isip: tests for any IP address
1960 isip4: tests for an IPv4 address
1961 isip6: tests for an IPv6 address
1962 pam: does PAM authentication
1963 radius: does RADIUS authentication
1964 ldapauth: does LDAP authentication
1965 pwcheck: does Cyrus SASL pwcheck authentication
1966 */
1967
1968 case ECOND_EXISTS:
1969 case ECOND_ISIP:
1970 case ECOND_ISIP4:
1971 case ECOND_ISIP6:
1972 case ECOND_PAM:
1973 case ECOND_RADIUS:
1974 case ECOND_LDAPAUTH:
1975 case ECOND_PWCHECK:
1976
1977 while (isspace(*s)) s++;
1978 if (*s != '{') goto COND_FAILED_CURLY_START;
1979
1980 sub[0] = expand_string_internal(s+1, TRUE, &s, yield == NULL, TRUE);
1981 if (sub[0] == NULL) return NULL;
1982 if (*s++ != '}') goto COND_FAILED_CURLY_END;
1983
1984 if (yield == NULL) return s; /* No need to run the test if skipping */
1985
1986 switch(cond_type)
1987 {
1988 case ECOND_EXISTS:
1989 if ((expand_forbid & RDO_EXISTS) != 0)
1990 {
1991 expand_string_message = US"File existence tests are not permitted";
1992 return NULL;
1993 }
1994 *yield = (Ustat(sub[0], &statbuf) == 0) == testfor;
1995 break;
1996
1997 case ECOND_ISIP:
1998 case ECOND_ISIP4:
1999 case ECOND_ISIP6:
2000 rc = string_is_ip_address(sub[0], NULL);
2001 *yield = ((cond_type == ECOND_ISIP)? (rc != 0) :
2002 (cond_type == ECOND_ISIP4)? (rc == 4) : (rc == 6)) == testfor;
2003 break;
2004
2005 /* Various authentication tests - all optionally compiled */
2006
2007 case ECOND_PAM:
2008 #ifdef SUPPORT_PAM
2009 rc = auth_call_pam(sub[0], &expand_string_message);
2010 goto END_AUTH;
2011 #else
2012 goto COND_FAILED_NOT_COMPILED;
2013 #endif /* SUPPORT_PAM */
2014
2015 case ECOND_RADIUS:
2016 #ifdef RADIUS_CONFIG_FILE
2017 rc = auth_call_radius(sub[0], &expand_string_message);
2018 goto END_AUTH;
2019 #else
2020 goto COND_FAILED_NOT_COMPILED;
2021 #endif /* RADIUS_CONFIG_FILE */
2022
2023 case ECOND_LDAPAUTH:
2024 #ifdef LOOKUP_LDAP
2025 {
2026 /* Just to keep the interface the same */
2027 BOOL do_cache;
2028 int old_pool = store_pool;
2029 store_pool = POOL_SEARCH;
2030 rc = eldapauth_find((void *)(-1), NULL, sub[0], Ustrlen(sub[0]), NULL,
2031 &expand_string_message, &do_cache);
2032 store_pool = old_pool;
2033 }
2034 goto END_AUTH;
2035 #else
2036 goto COND_FAILED_NOT_COMPILED;
2037 #endif /* LOOKUP_LDAP */
2038
2039 case ECOND_PWCHECK:
2040 #ifdef CYRUS_PWCHECK_SOCKET
2041 rc = auth_call_pwcheck(sub[0], &expand_string_message);
2042 goto END_AUTH;
2043 #else
2044 goto COND_FAILED_NOT_COMPILED;
2045 #endif /* CYRUS_PWCHECK_SOCKET */
2046
2047 #if defined(SUPPORT_PAM) || defined(RADIUS_CONFIG_FILE) || \
2048 defined(LOOKUP_LDAP) || defined(CYRUS_PWCHECK_SOCKET)
2049 END_AUTH:
2050 if (rc == ERROR || rc == DEFER) return NULL;
2051 *yield = (rc == OK) == testfor;
2052 #endif
2053 }
2054 return s;
2055
2056
2057 /* saslauthd: does Cyrus saslauthd authentication. Four parameters are used:
2058
2059 ${if saslauthd {{username}{password}{service}{realm}} {yes}[no}}
2060
2061 However, the last two are optional. That is why the whole set is enclosed
2062 in their own set or braces. */
2063
2064 case ECOND_SASLAUTHD:
2065 #ifndef CYRUS_SASLAUTHD_SOCKET
2066 goto COND_FAILED_NOT_COMPILED;
2067 #else
2068 while (isspace(*s)) s++;
2069 if (*s++ != '{') goto COND_FAILED_CURLY_START;
2070 switch(read_subs(sub, 4, 2, &s, yield == NULL, TRUE, US"saslauthd"))
2071 {
2072 case 1: expand_string_message = US"too few arguments or bracketing "
2073 "error for saslauthd";
2074 case 2:
2075 case 3: return NULL;
2076 }
2077 if (sub[2] == NULL) sub[3] = NULL; /* realm if no service */
2078 if (yield != NULL)
2079 {
2080 int rc;
2081 rc = auth_call_saslauthd(sub[0], sub[1], sub[2], sub[3],
2082 &expand_string_message);
2083 if (rc == ERROR || rc == DEFER) return NULL;
2084 *yield = (rc == OK) == testfor;
2085 }
2086 return s;
2087 #endif /* CYRUS_SASLAUTHD_SOCKET */
2088
2089
2090 /* symbolic operators for numeric and string comparison, and a number of
2091 other operators, all requiring two arguments.
2092
2093 crypteq: encrypts plaintext and compares against an encrypted text,
2094 using crypt(), crypt16(), MD5 or SHA-1
2095 inlist/inlisti: checks if first argument is in the list of the second
2096 match: does a regular expression match and sets up the numerical
2097 variables if it succeeds
2098 match_address: matches in an address list
2099 match_domain: matches in a domain list
2100 match_ip: matches a host list that is restricted to IP addresses
2101 match_local_part: matches in a local part list
2102 */
2103
2104 case ECOND_MATCH_ADDRESS:
2105 case ECOND_MATCH_DOMAIN:
2106 case ECOND_MATCH_IP:
2107 case ECOND_MATCH_LOCAL_PART:
2108 #ifndef EXPAND_LISTMATCH_RHS
2109 sub2_honour_dollar = FALSE;
2110 #endif
2111 /* FALLTHROUGH */
2112
2113 case ECOND_CRYPTEQ:
2114 case ECOND_INLIST:
2115 case ECOND_INLISTI:
2116 case ECOND_MATCH:
2117
2118 case ECOND_NUM_L: /* Numerical comparisons */
2119 case ECOND_NUM_LE:
2120 case ECOND_NUM_E:
2121 case ECOND_NUM_EE:
2122 case ECOND_NUM_G:
2123 case ECOND_NUM_GE:
2124
2125 case ECOND_STR_LT: /* String comparisons */
2126 case ECOND_STR_LTI:
2127 case ECOND_STR_LE:
2128 case ECOND_STR_LEI:
2129 case ECOND_STR_EQ:
2130 case ECOND_STR_EQI:
2131 case ECOND_STR_GT:
2132 case ECOND_STR_GTI:
2133 case ECOND_STR_GE:
2134 case ECOND_STR_GEI:
2135
2136 for (i = 0; i < 2; i++)
2137 {
2138 /* Sometimes, we don't expand substrings; too many insecure configurations
2139 created using match_address{}{} and friends, where the second param
2140 includes information from untrustworthy sources. */
2141 BOOL honour_dollar = TRUE;
2142 if ((i > 0) && !sub2_honour_dollar)
2143 honour_dollar = FALSE;
2144
2145 while (isspace(*s)) s++;
2146 if (*s != '{')
2147 {
2148 if (i == 0) goto COND_FAILED_CURLY_START;
2149 expand_string_message = string_sprintf("missing 2nd string in {} "
2150 "after \"%s\"", name);
2151 return NULL;
2152 }
2153 sub[i] = expand_string_internal(s+1, TRUE, &s, yield == NULL,
2154 honour_dollar);
2155 if (sub[i] == NULL) return NULL;
2156 if (*s++ != '}') goto COND_FAILED_CURLY_END;
2157
2158 /* Convert to numerical if required; we know that the names of all the
2159 conditions that compare numbers do not start with a letter. This just saves
2160 checking for them individually. */
2161
2162 if (!isalpha(name[0]) && yield != NULL)
2163 {
2164 if (sub[i][0] == 0)
2165 {
2166 num[i] = 0;
2167 DEBUG(D_expand)
2168 debug_printf("empty string cast to zero for numerical comparison\n");
2169 }
2170 else
2171 {
2172 num[i] = expand_string_integer(sub[i], FALSE);
2173 if (expand_string_message != NULL) return NULL;
2174 }
2175 }
2176 }
2177
2178 /* Result not required */
2179
2180 if (yield == NULL) return s;
2181
2182 /* Do an appropriate comparison */
2183
2184 switch(cond_type)
2185 {
2186 case ECOND_NUM_E:
2187 case ECOND_NUM_EE:
2188 *yield = (num[0] == num[1]) == testfor;
2189 break;
2190
2191 case ECOND_NUM_G:
2192 *yield = (num[0] > num[1]) == testfor;
2193 break;
2194
2195 case ECOND_NUM_GE:
2196 *yield = (num[0] >= num[1]) == testfor;
2197 break;
2198
2199 case ECOND_NUM_L:
2200 *yield = (num[0] < num[1]) == testfor;
2201 break;
2202
2203 case ECOND_NUM_LE:
2204 *yield = (num[0] <= num[1]) == testfor;
2205 break;
2206
2207 case ECOND_STR_LT:
2208 *yield = (Ustrcmp(sub[0], sub[1]) < 0) == testfor;
2209 break;
2210
2211 case ECOND_STR_LTI:
2212 *yield = (strcmpic(sub[0], sub[1]) < 0) == testfor;
2213 break;
2214
2215 case ECOND_STR_LE:
2216 *yield = (Ustrcmp(sub[0], sub[1]) <= 0) == testfor;
2217 break;
2218
2219 case ECOND_STR_LEI:
2220 *yield = (strcmpic(sub[0], sub[1]) <= 0) == testfor;
2221 break;
2222
2223 case ECOND_STR_EQ:
2224 *yield = (Ustrcmp(sub[0], sub[1]) == 0) == testfor;
2225 break;
2226
2227 case ECOND_STR_EQI:
2228 *yield = (strcmpic(sub[0], sub[1]) == 0) == testfor;
2229 break;
2230
2231 case ECOND_STR_GT:
2232 *yield = (Ustrcmp(sub[0], sub[1]) > 0) == testfor;
2233 break;
2234
2235 case ECOND_STR_GTI:
2236 *yield = (strcmpic(sub[0], sub[1]) > 0) == testfor;
2237 break;
2238
2239 case ECOND_STR_GE:
2240 *yield = (Ustrcmp(sub[0], sub[1]) >= 0) == testfor;
2241 break;
2242
2243 case ECOND_STR_GEI:
2244 *yield = (strcmpic(sub[0], sub[1]) >= 0) == testfor;
2245 break;
2246
2247 case ECOND_MATCH: /* Regular expression match */
2248 re = pcre_compile(CS sub[1], PCRE_COPT, (const char **)&rerror, &roffset,
2249 NULL);
2250 if (re == NULL)
2251 {
2252 expand_string_message = string_sprintf("regular expression error in "
2253 "\"%s\": %s at offset %d", sub[1], rerror, roffset);
2254 return NULL;
2255 }
2256 *yield = regex_match_and_setup(re, sub[0], 0, -1) == testfor;
2257 break;
2258
2259 case ECOND_MATCH_ADDRESS: /* Match in an address list */
2260 rc = match_address_list(sub[0], TRUE, FALSE, &(sub[1]), NULL, -1, 0, NULL);
2261 goto MATCHED_SOMETHING;
2262
2263 case ECOND_MATCH_DOMAIN: /* Match in a domain list */
2264 rc = match_isinlist(sub[0], &(sub[1]), 0, &domainlist_anchor, NULL,
2265 MCL_DOMAIN + MCL_NOEXPAND, TRUE, NULL);
2266 goto MATCHED_SOMETHING;
2267
2268 case ECOND_MATCH_IP: /* Match IP address in a host list */
2269 if (sub[0][0] != 0 && string_is_ip_address(sub[0], NULL) == 0)
2270 {
2271 expand_string_message = string_sprintf("\"%s\" is not an IP address",
2272 sub[0]);
2273 return NULL;
2274 }
2275 else
2276 {
2277 unsigned int *nullcache = NULL;
2278 check_host_block cb;
2279
2280 cb.host_name = US"";
2281 cb.host_address = sub[0];
2282
2283 /* If the host address starts off ::ffff: it is an IPv6 address in
2284 IPv4-compatible mode. Find the IPv4 part for checking against IPv4
2285 addresses. */
2286
2287 cb.host_ipv4 = (Ustrncmp(cb.host_address, "::ffff:", 7) == 0)?
2288 cb.host_address + 7 : cb.host_address;
2289
2290 rc = match_check_list(
2291 &sub[1], /* the list */
2292 0, /* separator character */
2293 &hostlist_anchor, /* anchor pointer */
2294 &nullcache, /* cache pointer */
2295 check_host, /* function for testing */
2296 &cb, /* argument for function */
2297 MCL_HOST, /* type of check */
2298 sub[0], /* text for debugging */
2299 NULL); /* where to pass back data */
2300 }
2301 goto MATCHED_SOMETHING;
2302
2303 case ECOND_MATCH_LOCAL_PART:
2304 rc = match_isinlist(sub[0], &(sub[1]), 0, &localpartlist_anchor, NULL,
2305 MCL_LOCALPART + MCL_NOEXPAND, TRUE, NULL);
2306 /* Fall through */
2307 /* VVVVVVVVVVVV */
2308 MATCHED_SOMETHING:
2309 switch(rc)
2310 {
2311 case OK:
2312 *yield = testfor;
2313 break;
2314
2315 case FAIL:
2316 *yield = !testfor;
2317 break;
2318
2319 case DEFER:
2320 expand_string_message = string_sprintf("unable to complete match "
2321 "against \"%s\": %s", sub[1], search_error_message);
2322 return NULL;
2323 }
2324
2325 break;
2326
2327 /* Various "encrypted" comparisons. If the second string starts with
2328 "{" then an encryption type is given. Default to crypt() or crypt16()
2329 (build-time choice). */
2330
2331 case ECOND_CRYPTEQ:
2332 #ifndef SUPPORT_CRYPTEQ
2333 goto COND_FAILED_NOT_COMPILED;
2334 #else
2335 if (strncmpic(sub[1], US"{md5}", 5) == 0)
2336 {
2337 int sublen = Ustrlen(sub[1]+5);
2338 md5 base;
2339 uschar digest[16];
2340
2341 md5_start(&base);
2342 md5_end(&base, (uschar *)sub[0], Ustrlen(sub[0]), digest);
2343
2344 /* If the length that we are comparing against is 24, the MD5 digest
2345 is expressed as a base64 string. This is the way LDAP does it. However,
2346 some other software uses a straightforward hex representation. We assume
2347 this if the length is 32. Other lengths fail. */
2348
2349 if (sublen == 24)
2350 {
2351 uschar *coded = auth_b64encode((uschar *)digest, 16);
2352 DEBUG(D_auth) debug_printf("crypteq: using MD5+B64 hashing\n"
2353 " subject=%s\n crypted=%s\n", coded, sub[1]+5);
2354 *yield = (Ustrcmp(coded, sub[1]+5) == 0) == testfor;
2355 }
2356 else if (sublen == 32)
2357 {
2358 int i;
2359 uschar coded[36];
2360 for (i = 0; i < 16; i++) sprintf(CS (coded+2*i), "%02X", digest[i]);
2361 coded[32] = 0;
2362 DEBUG(D_auth) debug_printf("crypteq: using MD5+hex hashing\n"
2363 " subject=%s\n crypted=%s\n", coded, sub[1]+5);
2364 *yield = (strcmpic(coded, sub[1]+5) == 0) == testfor;
2365 }
2366 else
2367 {
2368 DEBUG(D_auth) debug_printf("crypteq: length for MD5 not 24 or 32: "
2369 "fail\n crypted=%s\n", sub[1]+5);
2370 *yield = !testfor;
2371 }
2372 }
2373
2374 else if (strncmpic(sub[1], US"{sha1}", 6) == 0)
2375 {
2376 int sublen = Ustrlen(sub[1]+6);
2377 sha1 base;
2378 uschar digest[20];
2379
2380 sha1_start(&base);
2381 sha1_end(&base, (uschar *)sub[0], Ustrlen(sub[0]), digest);
2382
2383 /* If the length that we are comparing against is 28, assume the SHA1
2384 digest is expressed as a base64 string. If the length is 40, assume a
2385 straightforward hex representation. Other lengths fail. */
2386
2387 if (sublen == 28)
2388 {
2389 uschar *coded = auth_b64encode((uschar *)digest, 20);
2390 DEBUG(D_auth) debug_printf("crypteq: using SHA1+B64 hashing\n"
2391 " subject=%s\n crypted=%s\n", coded, sub[1]+6);
2392 *yield = (Ustrcmp(coded, sub[1]+6) == 0) == testfor;
2393 }
2394 else if (sublen == 40)
2395 {
2396 int i;
2397 uschar coded[44];
2398 for (i = 0; i < 20; i++) sprintf(CS (coded+2*i), "%02X", digest[i]);
2399 coded[40] = 0;
2400 DEBUG(D_auth) debug_printf("crypteq: using SHA1+hex hashing\n"
2401 " subject=%s\n crypted=%s\n", coded, sub[1]+6);
2402 *yield = (strcmpic(coded, sub[1]+6) == 0) == testfor;
2403 }
2404 else
2405 {
2406 DEBUG(D_auth) debug_printf("crypteq: length for SHA-1 not 28 or 40: "
2407 "fail\n crypted=%s\n", sub[1]+6);
2408 *yield = !testfor;
2409 }
2410 }
2411
2412 else /* {crypt} or {crypt16} and non-{ at start */
2413 /* }-for-text-editors */
2414 {
2415 int which = 0;
2416 uschar *coded;
2417
2418 if (strncmpic(sub[1], US"{crypt}", 7) == 0)
2419 {
2420 sub[1] += 7;
2421 which = 1;
2422 }
2423 else if (strncmpic(sub[1], US"{crypt16}", 9) == 0)
2424 {
2425 sub[1] += 9;
2426 which = 2;
2427 }
2428 else if (sub[1][0] == '{')
2429 {
2430 expand_string_message = string_sprintf("unknown encryption mechanism "
2431 "in \"%s\"", sub[1]);
2432 return NULL;
2433 }
2434
2435 switch(which)
2436 {
2437 case 0: coded = US DEFAULT_CRYPT(CS sub[0], CS sub[1]); break;
2438 case 1: coded = US crypt(CS sub[0], CS sub[1]); break;
2439 default: coded = US crypt16(CS sub[0], CS sub[1]); break;
2440 }
2441
2442 #define STR(s) # s
2443 #define XSTR(s) STR(s)
2444 DEBUG(D_auth) debug_printf("crypteq: using %s()\n"
2445 " subject=%s\n crypted=%s\n",
2446 (which == 0)? XSTR(DEFAULT_CRYPT) : (which == 1)? "crypt" : "crypt16",
2447 coded, sub[1]);
2448 #undef STR
2449 #undef XSTR
2450
2451 /* If the encrypted string contains fewer than two characters (for the
2452 salt), force failure. Otherwise we get false positives: with an empty
2453 string the yield of crypt() is an empty string! */
2454
2455 *yield = (Ustrlen(sub[1]) < 2)? !testfor :
2456 (Ustrcmp(coded, sub[1]) == 0) == testfor;
2457 }
2458 break;
2459 #endif /* SUPPORT_CRYPTEQ */
2460
2461 case ECOND_INLIST:
2462 case ECOND_INLISTI:
2463 {
2464 int sep = 0;
2465 BOOL found = FALSE;
2466 uschar *save_iterate_item = iterate_item;
2467 int (*compare)(const uschar *, const uschar *);
2468
2469 if (cond_type == ECOND_INLISTI)
2470 compare = strcmpic;
2471 else
2472 compare = (int (*)(const uschar *, const uschar *)) strcmp;
2473
2474 while ((iterate_item = string_nextinlist(&sub[1], &sep, NULL, 0)) != NULL)
2475 if (compare(sub[0], iterate_item) == 0)
2476 {
2477 found = TRUE;
2478 break;
2479 }
2480 iterate_item = save_iterate_item;
2481 *yield = found;
2482 }
2483
2484 } /* Switch for comparison conditions */
2485
2486 return s; /* End of comparison conditions */
2487
2488
2489 /* and/or: computes logical and/or of several conditions */
2490
2491 case ECOND_AND:
2492 case ECOND_OR:
2493 subcondptr = (yield == NULL)? NULL : &tempcond;
2494 combined_cond = (cond_type == ECOND_AND);
2495
2496 while (isspace(*s)) s++;
2497 if (*s++ != '{') goto COND_FAILED_CURLY_START;
2498
2499 for (;;)
2500 {
2501 while (isspace(*s)) s++;
2502 if (*s == '}') break;
2503 if (*s != '{')
2504 {
2505 expand_string_message = string_sprintf("each subcondition "
2506 "inside an \"%s{...}\" condition must be in its own {}", name);
2507 return NULL;
2508 }
2509
2510 s = eval_condition(s+1, subcondptr);
2511 if (s == NULL)
2512 {
2513 expand_string_message = string_sprintf("%s inside \"%s{...}\" condition",
2514 expand_string_message, name);
2515 return NULL;
2516 }
2517 while (isspace(*s)) s++;
2518
2519 if (*s++ != '}')
2520 {
2521 expand_string_message = string_sprintf("missing } at end of condition "
2522 "inside \"%s\" group", name);
2523 return NULL;
2524 }
2525
2526 if (yield != NULL)
2527 {
2528 if (cond_type == ECOND_AND)
2529 {
2530 combined_cond &= tempcond;
2531 if (!combined_cond) subcondptr = NULL; /* once false, don't */
2532 } /* evaluate any more */
2533 else
2534 {
2535 combined_cond |= tempcond;
2536 if (combined_cond) subcondptr = NULL; /* once true, don't */
2537 } /* evaluate any more */
2538 }
2539 }
2540
2541 if (yield != NULL) *yield = (combined_cond == testfor);
2542 return ++s;
2543
2544
2545 /* forall/forany: iterates a condition with different values */
2546
2547 case ECOND_FORALL:
2548 case ECOND_FORANY:
2549 {
2550 int sep = 0;
2551 uschar *save_iterate_item = iterate_item;
2552
2553 while (isspace(*s)) s++;
2554 if (*s++ != '{') goto COND_FAILED_CURLY_START;
2555 sub[0] = expand_string_internal(s, TRUE, &s, (yield == NULL), TRUE);
2556 if (sub[0] == NULL) return NULL;
2557 if (*s++ != '}') goto COND_FAILED_CURLY_END;
2558
2559 while (isspace(*s)) s++;
2560 if (*s++ != '{') goto COND_FAILED_CURLY_START;
2561
2562 sub[1] = s;
2563
2564 /* Call eval_condition once, with result discarded (as if scanning a
2565 "false" part). This allows us to find the end of the condition, because if
2566 the list it empty, we won't actually evaluate the condition for real. */
2567
2568 s = eval_condition(sub[1], NULL);
2569 if (s == NULL)
2570 {
2571 expand_string_message = string_sprintf("%s inside \"%s\" condition",
2572 expand_string_message, name);
2573 return NULL;
2574 }
2575 while (isspace(*s)) s++;
2576
2577 if (*s++ != '}')
2578 {
2579 expand_string_message = string_sprintf("missing } at end of condition "
2580 "inside \"%s\"", name);
2581 return NULL;
2582 }
2583
2584 if (yield != NULL) *yield = !testfor;
2585 while ((iterate_item = string_nextinlist(&sub[0], &sep, NULL, 0)) != NULL)
2586 {
2587 DEBUG(D_expand) debug_printf("%s: $item = \"%s\"\n", name, iterate_item);
2588 if (eval_condition(sub[1], &tempcond) == NULL)
2589 {
2590 expand_string_message = string_sprintf("%s inside \"%s\" condition",
2591 expand_string_message, name);
2592 iterate_item = save_iterate_item;
2593 return NULL;
2594 }
2595 DEBUG(D_expand) debug_printf("%s: condition evaluated to %s\n", name,
2596 tempcond? "true":"false");
2597
2598 if (yield != NULL) *yield = (tempcond == testfor);
2599 if (tempcond == (cond_type == ECOND_FORANY)) break;
2600 }
2601
2602 iterate_item = save_iterate_item;
2603 return s;
2604 }
2605
2606
2607 /* The bool{} expansion condition maps a string to boolean.
2608 The values supported should match those supported by the ACL condition
2609 (acl.c, ACLC_CONDITION) so that we keep to a minimum the different ideas
2610 of true/false. Note that Router "condition" rules have a different
2611 interpretation, where general data can be used and only a few values
2612 map to FALSE.
2613 Note that readconf.c boolean matching, for boolean configuration options,
2614 only matches true/yes/false/no.
2615 The bool_lax{} condition matches the Router logic, which is much more
2616 liberal. */
2617 case ECOND_BOOL:
2618 case ECOND_BOOL_LAX:
2619 {
2620 uschar *sub_arg[1];
2621 uschar *t, *t2;
2622 uschar *ourname;
2623 size_t len;
2624 BOOL boolvalue = FALSE;
2625 while (isspace(*s)) s++;
2626 if (*s != '{') goto COND_FAILED_CURLY_START;
2627 ourname = cond_type == ECOND_BOOL_LAX ? US"bool_lax" : US"bool";
2628 switch(read_subs(sub_arg, 1, 1, &s, yield == NULL, FALSE, ourname))
2629 {
2630 case 1: expand_string_message = string_sprintf(
2631 "too few arguments or bracketing error for %s",
2632 ourname);
2633 /*FALLTHROUGH*/
2634 case 2:
2635 case 3: return NULL;
2636 }
2637 t = sub_arg[0];
2638 while (isspace(*t)) t++;
2639 len = Ustrlen(t);
2640 if (len)
2641 {
2642 /* trailing whitespace: seems like a good idea to ignore it too */
2643 t2 = t + len - 1;
2644 while (isspace(*t2)) t2--;
2645 if (t2 != (t + len))
2646 {
2647 *++t2 = '\0';
2648 len = t2 - t;
2649 }
2650 }
2651 DEBUG(D_expand)
2652 debug_printf("considering %s: %s\n", ourname, len ? t : US"<empty>");
2653 /* logic for the lax case from expand_check_condition(), which also does
2654 expands, and the logic is both short and stable enough that there should
2655 be no maintenance burden from replicating it. */
2656 if (len == 0)
2657 boolvalue = FALSE;
2658 else if (Ustrspn(t, "0123456789") == len)
2659 {
2660 boolvalue = (Uatoi(t) == 0) ? FALSE : TRUE;
2661 /* expand_check_condition only does a literal string "0" check */
2662 if ((cond_type == ECOND_BOOL_LAX) && (len > 1))
2663 boolvalue = TRUE;
2664 }
2665 else if (strcmpic(t, US"true") == 0 || strcmpic(t, US"yes") == 0)
2666 boolvalue = TRUE;
2667 else if (strcmpic(t, US"false") == 0 || strcmpic(t, US"no") == 0)
2668 boolvalue = FALSE;
2669 else if (cond_type == ECOND_BOOL_LAX)
2670 boolvalue = TRUE;
2671 else
2672 {
2673 expand_string_message = string_sprintf("unrecognised boolean "
2674 "value \"%s\"", t);
2675 return NULL;
2676 }
2677 if (yield != NULL) *yield = (boolvalue == testfor);
2678 return s;
2679 }
2680
2681 /* Unknown condition */
2682
2683 default:
2684 expand_string_message = string_sprintf("unknown condition \"%s\"", name);
2685 return NULL;
2686 } /* End switch on condition type */
2687
2688 /* Missing braces at start and end of data */
2689
2690 COND_FAILED_CURLY_START:
2691 expand_string_message = string_sprintf("missing { after \"%s\"", name);
2692 return NULL;
2693
2694 COND_FAILED_CURLY_END:
2695 expand_string_message = string_sprintf("missing } at end of \"%s\" condition",
2696 name);
2697 return NULL;
2698
2699 /* A condition requires code that is not compiled */
2700
2701 #if !defined(SUPPORT_PAM) || !defined(RADIUS_CONFIG_FILE) || \
2702 !defined(LOOKUP_LDAP) || !defined(CYRUS_PWCHECK_SOCKET) || \
2703 !defined(SUPPORT_CRYPTEQ) || !defined(CYRUS_SASLAUTHD_SOCKET)
2704 COND_FAILED_NOT_COMPILED:
2705 expand_string_message = string_sprintf("support for \"%s\" not compiled",
2706 name);
2707 return NULL;
2708 #endif
2709 }
2710
2711
2712
2713
2714 /*************************************************
2715 * Save numerical variables *
2716 *************************************************/
2717
2718 /* This function is called from items such as "if" that want to preserve and
2719 restore the numbered variables.
2720
2721 Arguments:
2722 save_expand_string points to an array of pointers to set
2723 save_expand_nlength points to an array of ints for the lengths
2724
2725 Returns: the value of expand max to save
2726 */
2727
2728 static int
2729 save_expand_strings(uschar **save_expand_nstring, int *save_expand_nlength)
2730 {
2731 int i;
2732 for (i = 0; i <= expand_nmax; i++)
2733 {
2734 save_expand_nstring[i] = expand_nstring[i];
2735 save_expand_nlength[i] = expand_nlength[i];
2736 }
2737 return expand_nmax;
2738 }
2739
2740
2741
2742 /*************************************************
2743 * Restore numerical variables *
2744 *************************************************/
2745
2746 /* This function restored saved values of numerical strings.
2747
2748 Arguments:
2749 save_expand_nmax the number of strings to restore
2750 save_expand_string points to an array of pointers
2751 save_expand_nlength points to an array of ints
2752
2753 Returns: nothing
2754 */
2755
2756 static void
2757 restore_expand_strings(int save_expand_nmax, uschar **save_expand_nstring,
2758 int *save_expand_nlength)
2759 {
2760 int i;
2761 expand_nmax = save_expand_nmax;
2762 for (i = 0; i <= expand_nmax; i++)
2763 {
2764 expand_nstring[i] = save_expand_nstring[i];
2765 expand_nlength[i] = save_expand_nlength[i];
2766 }
2767 }
2768
2769
2770
2771
2772
2773 /*************************************************
2774 * Handle yes/no substrings *
2775 *************************************************/
2776
2777 /* This function is used by ${if}, ${lookup} and ${extract} to handle the
2778 alternative substrings that depend on whether or not the condition was true,
2779 or the lookup or extraction succeeded. The substrings always have to be
2780 expanded, to check their syntax, but "skipping" is set when the result is not
2781 needed - this avoids unnecessary nested lookups.
2782
2783 Arguments:
2784 skipping TRUE if we were skipping when this item was reached
2785 yes TRUE if the first string is to be used, else use the second
2786 save_lookup a value to put back into lookup_value before the 2nd expansion
2787 sptr points to the input string pointer
2788 yieldptr points to the output string pointer
2789 sizeptr points to the output string size
2790 ptrptr points to the output string pointer
2791 type "lookup" or "if" or "extract" or "run", for error message
2792
2793 Returns: 0 OK; lookup_value has been reset to save_lookup
2794 1 expansion failed
2795 2 expansion failed because of bracketing error
2796 */
2797
2798 static int
2799 process_yesno(BOOL skipping, BOOL yes, uschar *save_lookup, uschar **sptr,
2800 uschar **yieldptr, int *sizeptr, int *ptrptr, uschar *type)
2801 {
2802 int rc = 0;
2803 uschar *s = *sptr; /* Local value */
2804 uschar *sub1, *sub2;
2805
2806 /* If there are no following strings, we substitute the contents of $value for
2807 lookups and for extractions in the success case. For the ${if item, the string
2808 "true" is substituted. In the fail case, nothing is substituted for all three
2809 items. */
2810
2811 while (isspace(*s)) s++;
2812 if (*s == '}')
2813 {
2814 if (type[0] == 'i')
2815 {
2816 if (yes) *yieldptr = string_cat(*yieldptr, sizeptr, ptrptr, US"true", 4);
2817 }
2818 else
2819 {
2820 if (yes && lookup_value != NULL)
2821 *yieldptr = string_cat(*yieldptr, sizeptr, ptrptr, lookup_value,
2822 Ustrlen(lookup_value));
2823 lookup_value = save_lookup;
2824 }
2825 s++;
2826 goto RETURN;
2827 }
2828
2829 /* The first following string must be braced. */
2830
2831 if (*s++ != '{') goto FAILED_CURLY;
2832
2833 /* Expand the first substring. Forced failures are noticed only if we actually
2834 want this string. Set skipping in the call in the fail case (this will always
2835 be the case if we were already skipping). */
2836
2837 sub1 = expand_string_internal(s, TRUE, &s, !yes, TRUE);
2838 if (sub1 == NULL && (yes || !expand_string_forcedfail)) goto FAILED;
2839 expand_string_forcedfail = FALSE;
2840 if (*s++ != '}') goto FAILED_CURLY;
2841
2842 /* If we want the first string, add it to the output */
2843
2844 if (yes)
2845 *yieldptr = string_cat(*yieldptr, sizeptr, ptrptr, sub1, Ustrlen(sub1));
2846
2847 /* If this is called from a lookup or an extract, we want to restore $value to
2848 what it was at the start of the item, so that it has this value during the
2849 second string expansion. For the call from "if" or "run" to this function,
2850 save_lookup is set to lookup_value, so that this statement does nothing. */
2851
2852 lookup_value = save_lookup;
2853
2854 /* There now follows either another substring, or "fail", or nothing. This
2855 time, forced failures are noticed only if we want the second string. We must
2856 set skipping in the nested call if we don't want this string, or if we were
2857 already skipping. */
2858
2859 while (isspace(*s)) s++;
2860 if (*s == '{')
2861 {
2862 sub2 = expand_string_internal(s+1, TRUE, &s, yes || skipping, TRUE);
2863 if (sub2 == NULL && (!yes || !expand_string_forcedfail)) goto FAILED;
2864 expand_string_forcedfail = FALSE;
2865 if (*s++ != '}') goto FAILED_CURLY;
2866
2867 /* If we want the second string, add it to the output */
2868
2869 if (!yes)
2870 *yieldptr = string_cat(*yieldptr, sizeptr, ptrptr, sub2, Ustrlen(sub2));
2871 }
2872
2873 /* If there is no second string, but the word "fail" is present when the use of
2874 the second string is wanted, set a flag indicating it was a forced failure
2875 rather than a syntactic error. Swallow the terminating } in case this is nested
2876 inside another lookup or if or extract. */
2877
2878 else if (*s != '}')
2879 {
2880 uschar name[256];
2881 s = read_name(name, sizeof(name), s, US"_");
2882 if (Ustrcmp(name, "fail") == 0)
2883 {
2884 if (!yes && !skipping)
2885 {
2886 while (isspace(*s)) s++;
2887 if (*s++ != '}') goto FAILED_CURLY;
2888 expand_string_message =
2889 string_sprintf("\"%s\" failed and \"fail\" requested", type);
2890 expand_string_forcedfail = TRUE;
2891 goto FAILED;
2892 }
2893 }
2894 else
2895 {
2896 expand_string_message =
2897 string_sprintf("syntax error in \"%s\" item - \"fail\" expected", type);
2898 goto FAILED;
2899 }
2900 }
2901
2902 /* All we have to do now is to check on the final closing brace. */
2903
2904 while (isspace(*s)) s++;
2905 if (*s++ == '}') goto RETURN;
2906
2907 /* Get here if there is a bracketing failure */
2908
2909 FAILED_CURLY:
2910 rc++;
2911
2912 /* Get here for other failures */
2913
2914 FAILED:
2915 rc++;
2916
2917 /* Update the input pointer value before returning */
2918
2919 RETURN:
2920 *sptr = s;
2921 return rc;
2922 }
2923
2924
2925
2926
2927 /*************************************************
2928 * Handle MD5 or SHA-1 computation for HMAC *
2929 *************************************************/
2930
2931 /* These are some wrapping functions that enable the HMAC code to be a bit
2932 cleaner. A good compiler will spot the tail recursion.
2933
2934 Arguments:
2935 type HMAC_MD5 or HMAC_SHA1
2936 remaining are as for the cryptographic hash functions
2937
2938 Returns: nothing
2939 */
2940
2941 static void
2942 chash_start(int type, void *base)
2943 {
2944 if (type == HMAC_MD5)
2945 md5_start((md5 *)base);
2946 else
2947 sha1_start((sha1 *)base);
2948 }
2949
2950 static void
2951 chash_mid(int type, void *base, uschar *string)
2952 {
2953 if (type == HMAC_MD5)
2954 md5_mid((md5 *)base, string);
2955 else
2956 sha1_mid((sha1 *)base, string);
2957 }
2958
2959 static void
2960 chash_end(int type, void *base, uschar *string, int length, uschar *digest)
2961 {
2962 if (type == HMAC_MD5)
2963 md5_end((md5 *)base, string, length, digest);
2964 else
2965 sha1_end((sha1 *)base, string, length, digest);
2966 }
2967
2968
2969
2970
2971
2972 /********************************************************
2973 * prvs: Get last three digits of days since Jan 1, 1970 *
2974 ********************************************************/
2975
2976 /* This is needed to implement the "prvs" BATV reverse
2977 path signing scheme
2978
2979 Argument: integer "days" offset to add or substract to
2980 or from the current number of days.
2981
2982 Returns: pointer to string containing the last three
2983 digits of the number of days since Jan 1, 1970,
2984 modified by the offset argument, NULL if there
2985 was an error in the conversion.
2986
2987 */
2988
2989 static uschar *
2990 prvs_daystamp(int day_offset)
2991 {
2992 uschar *days = store_get(32); /* Need at least 24 for cases */
2993 (void)string_format(days, 32, TIME_T_FMT, /* where TIME_T_FMT is %lld */
2994 (time(NULL) + day_offset*86400)/86400);
2995 return (Ustrlen(days) >= 3) ? &days[Ustrlen(days)-3] : US"100";
2996 }
2997
2998
2999
3000 /********************************************************
3001 * prvs: perform HMAC-SHA1 computation of prvs bits *
3002 ********************************************************/
3003
3004 /* This is needed to implement the "prvs" BATV reverse
3005 path signing scheme
3006
3007 Arguments:
3008 address RFC2821 Address to use
3009 key The key to use (must be less than 64 characters
3010 in size)
3011 key_num Single-digit key number to use. Defaults to
3012 '0' when NULL.
3013
3014 Returns: pointer to string containing the first three
3015 bytes of the final hash in hex format, NULL if
3016 there was an error in the process.
3017 */
3018
3019 static uschar *
3020 prvs_hmac_sha1(uschar *address, uschar *key, uschar *key_num, uschar *daystamp)
3021 {
3022 uschar *hash_source, *p;
3023 int size = 0,offset = 0,i;
3024 sha1 sha1_base;
3025 void *use_base = &sha1_base;
3026 uschar innerhash[20];
3027 uschar finalhash[20];
3028 uschar innerkey[64];
3029 uschar outerkey[64];
3030 uschar *finalhash_hex = store_get(40);
3031
3032 if (key_num == NULL)
3033 key_num = US"0";
3034
3035 if (Ustrlen(key) > 64)
3036 return NULL;
3037
3038 hash_source = string_cat(NULL,&size,&offset,key_num,1);
3039 string_cat(hash_source,&size,&offset,daystamp,3);
3040 string_cat(hash_source,&size,&offset,address,Ustrlen(address));
3041 hash_source[offset] = '\0';
3042
3043 DEBUG(D_expand) debug_printf("prvs: hash source is '%s'\n", hash_source);
3044
3045 memset(innerkey, 0x36, 64);
3046 memset(outerkey, 0x5c, 64);
3047
3048 for (i = 0; i < Ustrlen(key); i++)
3049 {
3050 innerkey[i] ^= key[i];
3051 outerkey[i] ^= key[i];
3052 }
3053
3054 chash_start(HMAC_SHA1, use_base);
3055 chash_mid(HMAC_SHA1, use_base, innerkey);
3056 chash_end(HMAC_SHA1, use_base, hash_source, offset, innerhash);
3057
3058 chash_start(HMAC_SHA1, use_base);
3059 chash_mid(HMAC_SHA1, use_base, outerkey);
3060 chash_end(HMAC_SHA1, use_base, innerhash, 20, finalhash);
3061
3062 p = finalhash_hex;
3063 for (i = 0; i < 3; i++)
3064 {
3065 *p++ = hex_digits[(finalhash[i] & 0xf0) >> 4];
3066 *p++ = hex_digits[finalhash[i] & 0x0f];
3067 }
3068 *p = '\0';
3069
3070 return finalhash_hex;
3071 }
3072
3073
3074
3075
3076 /*************************************************
3077 * Join a file onto the output string *
3078 *************************************************/
3079
3080 /* This is used for readfile and after a run expansion. It joins the contents
3081 of a file onto the output string, globally replacing newlines with a given
3082 string (optionally). The file is closed at the end.
3083
3084 Arguments:
3085 f the FILE
3086 yield pointer to the expandable string
3087 sizep pointer to the current size
3088 ptrp pointer to the current position
3089 eol newline replacement string, or NULL
3090
3091 Returns: new value of string pointer
3092 */
3093
3094 static uschar *
3095 cat_file(FILE *f, uschar *yield, int *sizep, int *ptrp, uschar *eol)
3096 {
3097 int eollen;
3098 uschar buffer[1024];
3099
3100 eollen = (eol == NULL)? 0 : Ustrlen(eol);
3101
3102 while (Ufgets(buffer, sizeof(buffer), f) != NULL)
3103 {
3104 int len = Ustrlen(buffer);
3105 if (eol != NULL && buffer[len-1] == '\n') len--;
3106 yield = string_cat(yield, sizep, ptrp, buffer, len);
3107 if (buffer[len] != 0)
3108 yield = string_cat(yield, sizep, ptrp, eol, eollen);
3109 }
3110
3111 if (yield != NULL) yield[*ptrp] = 0;
3112
3113 return yield;
3114 }
3115
3116
3117
3118
3119 /*************************************************
3120 * Evaluate numeric expression *
3121 *************************************************/
3122
3123 /* This is a set of mutually recursive functions that evaluate an arithmetic
3124 expression involving + - * / % & | ^ ~ << >> and parentheses. The only one of
3125 these functions that is called from elsewhere is eval_expr, whose interface is:
3126
3127 Arguments:
3128 sptr pointer to the pointer to the string - gets updated
3129 decimal TRUE if numbers are to be assumed decimal
3130 error pointer to where to put an error message - must be NULL on input
3131 endket TRUE if ')' must terminate - FALSE for external call
3132
3133 Returns: on success: the value of the expression, with *error still NULL
3134 on failure: an undefined value, with *error = a message
3135 */
3136
3137 static int_eximarith_t eval_op_or(uschar **, BOOL, uschar **);
3138
3139
3140 static int_eximarith_t
3141 eval_expr(uschar **sptr, BOOL decimal, uschar **error, BOOL endket)
3142 {
3143 uschar *s = *sptr;
3144 int_eximarith_t x = eval_op_or(&s, decimal, error);
3145 if (*error == NULL)
3146 {
3147 if (endket)
3148 {
3149 if (*s != ')')
3150 *error = US"expecting closing parenthesis";
3151 else
3152 while (isspace(*(++s)));
3153 }
3154 else if (*s != 0) *error = US"expecting operator";
3155 }
3156 *sptr = s;
3157 return x;
3158 }
3159
3160
3161 static int_eximarith_t
3162 eval_number(uschar **sptr, BOOL decimal, uschar **error)
3163 {
3164 register int c;
3165 int_eximarith_t n;
3166 uschar *s = *sptr;
3167 while (isspace(*s)) s++;
3168 c = *s;
3169 if (isdigit(c))
3170 {
3171 int count;
3172 (void)sscanf(CS s, (decimal? SC_EXIM_DEC "%n" : SC_EXIM_ARITH "%n"), &n, &count);
3173 s += count;
3174 switch (tolower(*s))
3175 {
3176 default: break;
3177 case 'k': n *= 1024; s++; break;
3178 case 'm': n *= 1024*1024; s++; break;
3179 case 'g': n *= 1024*1024*1024; s++; break;
3180 }
3181 while (isspace (*s)) s++;
3182 }
3183 else if (c == '(')
3184 {
3185 s++;
3186 n = eval_expr(&s, decimal, error, 1);
3187 }
3188 else
3189 {
3190 *error = US"expecting number or opening parenthesis";
3191 n = 0;
3192 }
3193 *sptr = s;
3194 return n;
3195 }
3196
3197
3198 static int_eximarith_t
3199 eval_op_unary(uschar **sptr, BOOL decimal, uschar **error)
3200 {
3201 uschar *s = *sptr;
3202 int_eximarith_t x;
3203 while (isspace(*s)) s++;
3204 if (*s == '+' || *s == '-' || *s == '~')
3205 {
3206 int op = *s++;
3207 x = eval_op_unary(&s, decimal, error);
3208 if (op == '-') x = -x;
3209 else if (op == '~') x = ~x;
3210 }
3211 else
3212 {
3213 x = eval_number(&s, decimal, error);
3214 }
3215 *sptr = s;
3216 return x;
3217 }
3218
3219
3220 static int_eximarith_t
3221 eval_op_mult(uschar **sptr, BOOL decimal, uschar **error)
3222 {
3223 uschar *s = *sptr;
3224 int_eximarith_t x = eval_op_unary(&s, decimal, error);
3225 if (*error == NULL)
3226 {
3227 while (*s == '*' || *s == '/' || *s == '%')
3228 {
3229 int op = *s++;
3230 int_eximarith_t y = eval_op_unary(&s, decimal, error);
3231 if (*error != NULL) break;
3232 /* SIGFPE both on div/mod by zero and on INT_MIN / -1, which would give
3233 * a value of INT_MAX+1. Note that INT_MIN * -1 gives INT_MIN for me, which
3234 * is a bug somewhere in [gcc 4.2.1, FreeBSD, amd64]. In fact, -N*-M where
3235 * -N*M is INT_MIN will yielf INT_MIN.
3236 * Since we don't support floating point, this is somewhat simpler.
3237 * Ideally, we'd return an error, but since we overflow for all other
3238 * arithmetic, consistency suggests otherwise, but what's the correct value
3239 * to use? There is none.
3240 * The C standard guarantees overflow for unsigned arithmetic but signed
3241 * overflow invokes undefined behaviour; in practice, this is overflow
3242 * except for converting INT_MIN to INT_MAX+1. We also can't guarantee
3243 * that long/longlong larger than int are available, or we could just work
3244 * with larger types. We should consider whether to guarantee 32bit eval
3245 * and 64-bit working variables, with errors returned. For now ...
3246 * So, the only SIGFPEs occur with a non-shrinking div/mod, thus -1; we
3247 * can just let the other invalid results occur otherwise, as they have
3248 * until now. For this one case, we can coerce.
3249 */
3250 if (y == -1 && x == LLONG_MIN && op != '*')
3251 {
3252 DEBUG(D_expand)
3253 debug_printf("Integer exception dodging: " PR_EXIM_ARITH "%c-1 coerced to " PR_EXIM_ARITH "\n",
3254 LLONG_MIN, op, LLONG_MAX);
3255 x = LLONG_MAX;
3256 continue;
3257 }
3258 if (op == '*')
3259 x *= y;
3260 else
3261 {
3262 if (y == 0)
3263 {
3264 *error = (op == '/') ? US"divide by zero" : US"modulo by zero";
3265 x = 0;
3266 break;
3267 }
3268 if (op == '/')
3269 x /= y;
3270 else
3271 x %= y;
3272 }
3273 }
3274 }
3275 *sptr = s;
3276 return x;
3277 }
3278
3279
3280 static int_eximarith_t
3281 eval_op_sum(uschar **sptr, BOOL decimal, uschar **error)
3282 {
3283 uschar *s = *sptr;
3284 int_eximarith_t x = eval_op_mult(&s, decimal, error);
3285 if (*error == NULL)
3286 {
3287 while (*s == '+' || *s == '-')
3288 {
3289 int op = *s++;
3290 int_eximarith_t y = eval_op_mult(&s, decimal, error);
3291 if (*error != NULL) break;
3292 if (op == '+') x += y; else x -= y;
3293 }
3294 }
3295 *sptr = s;
3296 return x;
3297 }
3298
3299
3300 static int_eximarith_t
3301 eval_op_shift(uschar **sptr, BOOL decimal, uschar **error)
3302 {
3303 uschar *s = *sptr;
3304 int_eximarith_t x = eval_op_sum(&s, decimal, error);
3305 if (*error == NULL)
3306 {
3307 while ((*s == '<' || *s == '>') && s[1] == s[0])
3308 {
3309 int_eximarith_t y;
3310 int op = *s++;
3311 s++;
3312 y = eval_op_sum(&s, decimal, error);
3313 if (*error != NULL) break;
3314 if (op == '<') x <<= y; else x >>= y;
3315 }
3316 }
3317 *sptr = s;
3318 return x;
3319 }
3320
3321
3322 static int_eximarith_t
3323 eval_op_and(uschar **sptr, BOOL decimal, uschar **error)
3324 {
3325 uschar *s = *sptr;
3326 int_eximarith_t x = eval_op_shift(&s, decimal, error);
3327 if (*error == NULL)
3328 {
3329 while (*s == '&')
3330 {
3331 int_eximarith_t y;
3332 s++;
3333 y = eval_op_shift(&s, decimal, error);
3334 if (*error != NULL) break;
3335 x &= y;
3336 }
3337 }
3338 *sptr = s;
3339 return x;
3340 }
3341
3342
3343 static int_eximarith_t
3344 eval_op_xor(uschar **sptr, BOOL decimal, uschar **error)
3345 {
3346 uschar *s = *sptr;
3347 int_eximarith_t x = eval_op_and(&s, decimal, error);
3348 if (*error == NULL)
3349 {
3350 while (*s == '^')
3351 {
3352 int_eximarith_t y;
3353 s++;
3354 y = eval_op_and(&s, decimal, error);
3355 if (*error != NULL) break;
3356 x ^= y;
3357 }
3358 }
3359 *sptr = s;
3360 return x;
3361 }
3362
3363
3364 static int_eximarith_t
3365 eval_op_or(uschar **sptr, BOOL decimal, uschar **error)
3366 {
3367 uschar *s = *sptr;
3368 int_eximarith_t x = eval_op_xor(&s, decimal, error);
3369 if (*error == NULL)
3370 {
3371 while (*s == '|')
3372 {
3373 int_eximarith_t y;
3374 s++;
3375 y = eval_op_xor(&s, decimal, error);
3376 if (*error != NULL) break;
3377 x |= y;
3378