Commit | Line | Data |
---|---|---|
059ec3d9 PH |
1 | /************************************************* |
2 | * Exim - an Internet mail transport agent * | |
3 | *************************************************/ | |
4 | ||
0a49a7a4 | 5 | /* Copyright (c) University of Cambridge 1995 - 2009 */ |
059ec3d9 PH |
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 | ||
96c065cb PH |
14 | /* Recursively called function */ |
15 | ||
16 | static uschar *expand_string_internal(uschar *, BOOL, uschar **, BOOL); | |
17 | ||
059ec3d9 PH |
18 | #ifdef STAND_ALONE |
19 | #ifndef SUPPORT_CRYPTEQ | |
20 | #define SUPPORT_CRYPTEQ | |
21 | #endif | |
22 | #endif | |
23 | ||
96c065cb PH |
24 | #ifdef LOOKUP_LDAP |
25 | #include "lookups/ldap.h" | |
26 | #endif | |
27 | ||
059ec3d9 PH |
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 | ||
96c065cb PH |
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 | */ | |
059ec3d9 PH |
93 | |
94 | ||
059ec3d9 PH |
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[] = { | |
1a46a8c5 | 105 | US"dlfunc", |
059ec3d9 | 106 | US"extract", |
29f89cad | 107 | US"filter", |
059ec3d9 PH |
108 | US"hash", |
109 | US"hmac", | |
110 | US"if", | |
111 | US"length", | |
112 | US"lookup", | |
29f89cad | 113 | US"map", |
059ec3d9 | 114 | US"nhash", |
1a46a8c5 | 115 | US"perl", |
fffda43a TK |
116 | US"prvs", |
117 | US"prvscheck", | |
059ec3d9 PH |
118 | US"readfile", |
119 | US"readsocket", | |
29f89cad | 120 | US"reduce", |
059ec3d9 PH |
121 | US"run", |
122 | US"sg", | |
123 | US"substr", | |
124 | US"tr" }; | |
125 | ||
126 | enum { | |
1a46a8c5 | 127 | EITEM_DLFUNC, |
059ec3d9 | 128 | EITEM_EXTRACT, |
29f89cad | 129 | EITEM_FILTER, |
059ec3d9 PH |
130 | EITEM_HASH, |
131 | EITEM_HMAC, | |
132 | EITEM_IF, | |
133 | EITEM_LENGTH, | |
134 | EITEM_LOOKUP, | |
29f89cad | 135 | EITEM_MAP, |
059ec3d9 | 136 | EITEM_NHASH, |
1a46a8c5 | 137 | EITEM_PERL, |
fffda43a TK |
138 | EITEM_PRVS, |
139 | EITEM_PRVSCHECK, | |
059ec3d9 PH |
140 | EITEM_READFILE, |
141 | EITEM_READSOCK, | |
29f89cad | 142 | EITEM_REDUCE, |
059ec3d9 PH |
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", | |
83e029d5 | 157 | US"reverse_ip", |
f90d018c | 158 | US"time_eval", |
059ec3d9 PH |
159 | US"time_interval"}; |
160 | ||
161 | enum { | |
162 | EOP_FROM_UTF8, | |
163 | EOP_LOCAL_PART, | |
164 | EOP_QUOTE_LOCAL_PART, | |
83e029d5 | 165 | EOP_REVERSE_IP, |
f90d018c | 166 | EOP_TIME_EVAL, |
059ec3d9 PH |
167 | EOP_TIME_INTERVAL }; |
168 | ||
169 | static uschar *op_table_main[] = { | |
170 | US"address", | |
29f89cad | 171 | US"addresses", |
059ec3d9 PH |
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"mask", | |
186 | US"md5", | |
187 | US"nh", | |
188 | US"nhash", | |
189 | US"quote", | |
9e3331ea | 190 | US"randint", |
059ec3d9 | 191 | US"rfc2047", |
9c57cbc0 | 192 | US"rfc2047d", |
059ec3d9 PH |
193 | US"rxquote", |
194 | US"s", | |
195 | US"sha1", | |
196 | US"stat", | |
197 | US"str2b64", | |
198 | US"strlen", | |
199 | US"substr", | |
200 | US"uc" }; | |
201 | ||
202 | enum { | |
203 | EOP_ADDRESS = sizeof(op_table_underscore)/sizeof(uschar *), | |
29f89cad | 204 | EOP_ADDRESSES, |
059ec3d9 PH |
205 | EOP_BASE62, |
206 | EOP_BASE62D, | |
207 | EOP_DOMAIN, | |
208 | EOP_ESCAPE, | |
209 | EOP_EVAL, | |
210 | EOP_EVAL10, | |
211 | EOP_EXPAND, | |
212 | EOP_H, | |
213 | EOP_HASH, | |
214 | EOP_HEX2B64, | |
215 | EOP_L, | |
216 | EOP_LC, | |
217 | EOP_LENGTH, | |
218 | EOP_MASK, | |
219 | EOP_MD5, | |
220 | EOP_NH, | |
221 | EOP_NHASH, | |
222 | EOP_QUOTE, | |
9e3331ea | 223 | EOP_RANDINT, |
059ec3d9 | 224 | EOP_RFC2047, |
9c57cbc0 | 225 | EOP_RFC2047D, |
059ec3d9 PH |
226 | EOP_RXQUOTE, |
227 | EOP_S, | |
228 | EOP_SHA1, | |
229 | EOP_STAT, | |
230 | EOP_STR2B64, | |
231 | EOP_STRLEN, | |
232 | EOP_SUBSTR, | |
233 | EOP_UC }; | |
234 | ||
235 | ||
236 | /* Table of condition names, and corresponding switch numbers. The names must | |
237 | be in alphabetical order. */ | |
238 | ||
239 | static uschar *cond_table[] = { | |
240 | US"<", | |
241 | US"<=", | |
242 | US"=", | |
243 | US"==", /* Backward compatibility */ | |
244 | US">", | |
245 | US">=", | |
246 | US"and", | |
f3766eb5 | 247 | US"bool", |
6a8de854 | 248 | US"bool_lax", |
059ec3d9 PH |
249 | US"crypteq", |
250 | US"def", | |
251 | US"eq", | |
252 | US"eqi", | |
253 | US"exists", | |
254 | US"first_delivery", | |
0ce9abe6 PH |
255 | US"forall", |
256 | US"forany", | |
059ec3d9 PH |
257 | US"ge", |
258 | US"gei", | |
259 | US"gt", | |
260 | US"gti", | |
261 | US"isip", | |
262 | US"isip4", | |
263 | US"isip6", | |
264 | US"ldapauth", | |
265 | US"le", | |
266 | US"lei", | |
267 | US"lt", | |
268 | US"lti", | |
269 | US"match", | |
270 | US"match_address", | |
271 | US"match_domain", | |
32d668a5 | 272 | US"match_ip", |
059ec3d9 PH |
273 | US"match_local_part", |
274 | US"or", | |
275 | US"pam", | |
276 | US"pwcheck", | |
277 | US"queue_running", | |
278 | US"radius", | |
279 | US"saslauthd" | |
280 | }; | |
281 | ||
282 | enum { | |
283 | ECOND_NUM_L, | |
284 | ECOND_NUM_LE, | |
285 | ECOND_NUM_E, | |
286 | ECOND_NUM_EE, | |
287 | ECOND_NUM_G, | |
288 | ECOND_NUM_GE, | |
289 | ECOND_AND, | |
f3766eb5 | 290 | ECOND_BOOL, |
6a8de854 | 291 | ECOND_BOOL_LAX, |
059ec3d9 PH |
292 | ECOND_CRYPTEQ, |
293 | ECOND_DEF, | |
294 | ECOND_STR_EQ, | |
295 | ECOND_STR_EQI, | |
296 | ECOND_EXISTS, | |
297 | ECOND_FIRST_DELIVERY, | |
0ce9abe6 PH |
298 | ECOND_FORALL, |
299 | ECOND_FORANY, | |
059ec3d9 PH |
300 | ECOND_STR_GE, |
301 | ECOND_STR_GEI, | |
302 | ECOND_STR_GT, | |
303 | ECOND_STR_GTI, | |
304 | ECOND_ISIP, | |
305 | ECOND_ISIP4, | |
306 | ECOND_ISIP6, | |
307 | ECOND_LDAPAUTH, | |
308 | ECOND_STR_LE, | |
309 | ECOND_STR_LEI, | |
310 | ECOND_STR_LT, | |
311 | ECOND_STR_LTI, | |
312 | ECOND_MATCH, | |
313 | ECOND_MATCH_ADDRESS, | |
314 | ECOND_MATCH_DOMAIN, | |
32d668a5 | 315 | ECOND_MATCH_IP, |
059ec3d9 PH |
316 | ECOND_MATCH_LOCAL_PART, |
317 | ECOND_OR, | |
318 | ECOND_PAM, | |
319 | ECOND_PWCHECK, | |
320 | ECOND_QUEUE_RUNNING, | |
321 | ECOND_RADIUS, | |
322 | ECOND_SASLAUTHD | |
323 | }; | |
324 | ||
325 | ||
326 | /* Type for main variable table */ | |
327 | ||
328 | typedef struct { | |
1ba28e2b PP |
329 | const char *name; |
330 | int type; | |
331 | void *value; | |
059ec3d9 PH |
332 | } var_entry; |
333 | ||
334 | /* Type for entries pointing to address/length pairs. Not currently | |
335 | in use. */ | |
336 | ||
337 | typedef struct { | |
338 | uschar **address; | |
339 | int *length; | |
340 | } alblock; | |
341 | ||
342 | /* Types of table entry */ | |
343 | ||
344 | enum { | |
345 | vtype_int, /* value is address of int */ | |
346 | vtype_filter_int, /* ditto, but recognized only when filtering */ | |
347 | vtype_ino, /* value is address of ino_t (not always an int) */ | |
348 | vtype_uid, /* value is address of uid_t (not always an int) */ | |
349 | vtype_gid, /* value is address of gid_t (not always an int) */ | |
350 | vtype_stringptr, /* value is address of pointer to string */ | |
351 | vtype_msgbody, /* as stringptr, but read when first required */ | |
352 | vtype_msgbody_end, /* ditto, the end of the message */ | |
ff75a1f7 PH |
353 | vtype_msgheaders, /* the message's headers, processed */ |
354 | vtype_msgheaders_raw, /* the message's headers, unprocessed */ | |
059ec3d9 PH |
355 | vtype_localpart, /* extract local part from string */ |
356 | vtype_domain, /* extract domain from string */ | |
357 | vtype_recipients, /* extract recipients from recipients list */ | |
0e20aff9 MH |
358 | /* (available only in system filters, ACLs, and */ |
359 | /* local_scan()) */ | |
059ec3d9 PH |
360 | vtype_todbsdin, /* value not used; generate BSD inbox tod */ |
361 | vtype_tode, /* value not used; generate tod in epoch format */ | |
362 | vtype_todf, /* value not used; generate full tod */ | |
363 | vtype_todl, /* value not used; generate log tod */ | |
364 | vtype_todlf, /* value not used; generate log file datestamp tod */ | |
365 | vtype_todzone, /* value not used; generate time zone only */ | |
366 | vtype_todzulu, /* value not used; generate zulu tod */ | |
367 | vtype_reply, /* value not used; get reply from headers */ | |
368 | vtype_pid, /* value not used; result is pid */ | |
369 | vtype_host_lookup, /* value not used; get host name */ | |
5cb8cbc6 PH |
370 | vtype_load_avg, /* value not used; result is int from os_getloadavg */ |
371 | vtype_pspace, /* partition space; value is T/F for spool/log */ | |
8e669ac1 | 372 | vtype_pinodes /* partition inodes; value is T/F for spool/log */ |
80a47a2c TK |
373 | #ifndef DISABLE_DKIM |
374 | ,vtype_dkim /* Lookup of value in DKIM signature */ | |
375 | #endif | |
059ec3d9 PH |
376 | }; |
377 | ||
378 | /* This table must be kept in alphabetical order. */ | |
379 | ||
380 | static var_entry var_table[] = { | |
38a0a95f PH |
381 | /* WARNING: Do not invent variables whose names start acl_c or acl_m because |
382 | they will be confused with user-creatable ACL variables. */ | |
059ec3d9 PH |
383 | { "acl_verify_message", vtype_stringptr, &acl_verify_message }, |
384 | { "address_data", vtype_stringptr, &deliver_address_data }, | |
385 | { "address_file", vtype_stringptr, &address_file }, | |
386 | { "address_pipe", vtype_stringptr, &address_pipe }, | |
387 | { "authenticated_id", vtype_stringptr, &authenticated_id }, | |
388 | { "authenticated_sender",vtype_stringptr, &authenticated_sender }, | |
389 | { "authentication_failed",vtype_int, &authentication_failed }, | |
8523533c TK |
390 | #ifdef EXPERIMENTAL_BRIGHTMAIL |
391 | { "bmi_alt_location", vtype_stringptr, &bmi_alt_location }, | |
392 | { "bmi_base64_tracker_verdict", vtype_stringptr, &bmi_base64_tracker_verdict }, | |
393 | { "bmi_base64_verdict", vtype_stringptr, &bmi_base64_verdict }, | |
394 | { "bmi_deliver", vtype_int, &bmi_deliver }, | |
395 | #endif | |
059ec3d9 PH |
396 | { "body_linecount", vtype_int, &body_linecount }, |
397 | { "body_zerocount", vtype_int, &body_zerocount }, | |
398 | { "bounce_recipient", vtype_stringptr, &bounce_recipient }, | |
399 | { "bounce_return_size_limit", vtype_int, &bounce_return_size_limit }, | |
400 | { "caller_gid", vtype_gid, &real_gid }, | |
401 | { "caller_uid", vtype_uid, &real_uid }, | |
402 | { "compile_date", vtype_stringptr, &version_date }, | |
403 | { "compile_number", vtype_stringptr, &version_cnumber }, | |
e5a9dba6 | 404 | { "csa_status", vtype_stringptr, &csa_status }, |
6a8f9482 TK |
405 | #ifdef EXPERIMENTAL_DCC |
406 | { "dcc_header", vtype_stringptr, &dcc_header }, | |
407 | { "dcc_result", vtype_stringptr, &dcc_result }, | |
408 | #endif | |
8523533c TK |
409 | #ifdef WITH_OLD_DEMIME |
410 | { "demime_errorlevel", vtype_int, &demime_errorlevel }, | |
411 | { "demime_reason", vtype_stringptr, &demime_reason }, | |
fb2274d4 | 412 | #endif |
80a47a2c TK |
413 | #ifndef DISABLE_DKIM |
414 | { "dkim_algo", vtype_dkim, (void *)DKIM_ALGO }, | |
415 | { "dkim_bodylength", vtype_dkim, (void *)DKIM_BODYLENGTH }, | |
416 | { "dkim_canon_body", vtype_dkim, (void *)DKIM_CANON_BODY }, | |
417 | { "dkim_canon_headers", vtype_dkim, (void *)DKIM_CANON_HEADERS }, | |
418 | { "dkim_copiedheaders", vtype_dkim, (void *)DKIM_COPIEDHEADERS }, | |
419 | { "dkim_created", vtype_dkim, (void *)DKIM_CREATED }, | |
2df588c9 | 420 | { "dkim_cur_signer", vtype_stringptr, &dkim_cur_signer }, |
e08d09e5 | 421 | { "dkim_domain", vtype_stringptr, &dkim_signing_domain }, |
80a47a2c TK |
422 | { "dkim_expires", vtype_dkim, (void *)DKIM_EXPIRES }, |
423 | { "dkim_headernames", vtype_dkim, (void *)DKIM_HEADERNAMES }, | |
424 | { "dkim_identity", vtype_dkim, (void *)DKIM_IDENTITY }, | |
425 | { "dkim_key_granularity",vtype_dkim, (void *)DKIM_KEY_GRANULARITY }, | |
426 | { "dkim_key_nosubdomains",vtype_dkim, (void *)DKIM_NOSUBDOMAINS }, | |
427 | { "dkim_key_notes", vtype_dkim, (void *)DKIM_KEY_NOTES }, | |
428 | { "dkim_key_srvtype", vtype_dkim, (void *)DKIM_KEY_SRVTYPE }, | |
429 | { "dkim_key_testing", vtype_dkim, (void *)DKIM_KEY_TESTING }, | |
e08d09e5 | 430 | { "dkim_selector", vtype_stringptr, &dkim_signing_selector }, |
9e5d6b55 | 431 | { "dkim_signers", vtype_stringptr, &dkim_signers }, |
80a47a2c TK |
432 | { "dkim_verify_reason", vtype_dkim, (void *)DKIM_VERIFY_REASON }, |
433 | { "dkim_verify_status", vtype_dkim, (void *)DKIM_VERIFY_STATUS}, | |
8523533c | 434 | #endif |
059ec3d9 | 435 | { "dnslist_domain", vtype_stringptr, &dnslist_domain }, |
93655c46 | 436 | { "dnslist_matched", vtype_stringptr, &dnslist_matched }, |
059ec3d9 PH |
437 | { "dnslist_text", vtype_stringptr, &dnslist_text }, |
438 | { "dnslist_value", vtype_stringptr, &dnslist_value }, | |
439 | { "domain", vtype_stringptr, &deliver_domain }, | |
440 | { "domain_data", vtype_stringptr, &deliver_domain_data }, | |
441 | { "exim_gid", vtype_gid, &exim_gid }, | |
442 | { "exim_path", vtype_stringptr, &exim_path }, | |
443 | { "exim_uid", vtype_uid, &exim_uid }, | |
8523533c TK |
444 | #ifdef WITH_OLD_DEMIME |
445 | { "found_extension", vtype_stringptr, &found_extension }, | |
8e669ac1 | 446 | #endif |
059ec3d9 PH |
447 | { "home", vtype_stringptr, &deliver_home }, |
448 | { "host", vtype_stringptr, &deliver_host }, | |
449 | { "host_address", vtype_stringptr, &deliver_host_address }, | |
450 | { "host_data", vtype_stringptr, &host_data }, | |
b08b24c8 | 451 | { "host_lookup_deferred",vtype_int, &host_lookup_deferred }, |
059ec3d9 PH |
452 | { "host_lookup_failed", vtype_int, &host_lookup_failed }, |
453 | { "inode", vtype_ino, &deliver_inode }, | |
454 | { "interface_address", vtype_stringptr, &interface_address }, | |
455 | { "interface_port", vtype_int, &interface_port }, | |
0ce9abe6 | 456 | { "item", vtype_stringptr, &iterate_item }, |
059ec3d9 PH |
457 | #ifdef LOOKUP_LDAP |
458 | { "ldap_dn", vtype_stringptr, &eldap_dn }, | |
459 | #endif | |
460 | { "load_average", vtype_load_avg, NULL }, | |
461 | { "local_part", vtype_stringptr, &deliver_localpart }, | |
462 | { "local_part_data", vtype_stringptr, &deliver_localpart_data }, | |
463 | { "local_part_prefix", vtype_stringptr, &deliver_localpart_prefix }, | |
464 | { "local_part_suffix", vtype_stringptr, &deliver_localpart_suffix }, | |
465 | { "local_scan_data", vtype_stringptr, &local_scan_data }, | |
466 | { "local_user_gid", vtype_gid, &local_user_gid }, | |
467 | { "local_user_uid", vtype_uid, &local_user_uid }, | |
468 | { "localhost_number", vtype_int, &host_number }, | |
5cb8cbc6 | 469 | { "log_inodes", vtype_pinodes, (void *)FALSE }, |
8e669ac1 | 470 | { "log_space", vtype_pspace, (void *)FALSE }, |
059ec3d9 | 471 | { "mailstore_basename", vtype_stringptr, &mailstore_basename }, |
8523533c TK |
472 | #ifdef WITH_CONTENT_SCAN |
473 | { "malware_name", vtype_stringptr, &malware_name }, | |
474 | #endif | |
d677b2f2 | 475 | { "max_received_linelength", vtype_int, &max_received_linelength }, |
059ec3d9 PH |
476 | { "message_age", vtype_int, &message_age }, |
477 | { "message_body", vtype_msgbody, &message_body }, | |
478 | { "message_body_end", vtype_msgbody_end, &message_body_end }, | |
479 | { "message_body_size", vtype_int, &message_body_size }, | |
1ab52c69 | 480 | { "message_exim_id", vtype_stringptr, &message_id }, |
059ec3d9 | 481 | { "message_headers", vtype_msgheaders, NULL }, |
ff75a1f7 | 482 | { "message_headers_raw", vtype_msgheaders_raw, NULL }, |
059ec3d9 | 483 | { "message_id", vtype_stringptr, &message_id }, |
2e0c1448 | 484 | { "message_linecount", vtype_int, &message_linecount }, |
059ec3d9 | 485 | { "message_size", vtype_int, &message_size }, |
8523533c TK |
486 | #ifdef WITH_CONTENT_SCAN |
487 | { "mime_anomaly_level", vtype_int, &mime_anomaly_level }, | |
488 | { "mime_anomaly_text", vtype_stringptr, &mime_anomaly_text }, | |
489 | { "mime_boundary", vtype_stringptr, &mime_boundary }, | |
490 | { "mime_charset", vtype_stringptr, &mime_charset }, | |
491 | { "mime_content_description", vtype_stringptr, &mime_content_description }, | |
492 | { "mime_content_disposition", vtype_stringptr, &mime_content_disposition }, | |
493 | { "mime_content_id", vtype_stringptr, &mime_content_id }, | |
494 | { "mime_content_size", vtype_int, &mime_content_size }, | |
495 | { "mime_content_transfer_encoding",vtype_stringptr, &mime_content_transfer_encoding }, | |
496 | { "mime_content_type", vtype_stringptr, &mime_content_type }, | |
497 | { "mime_decoded_filename", vtype_stringptr, &mime_decoded_filename }, | |
498 | { "mime_filename", vtype_stringptr, &mime_filename }, | |
499 | { "mime_is_coverletter", vtype_int, &mime_is_coverletter }, | |
500 | { "mime_is_multipart", vtype_int, &mime_is_multipart }, | |
501 | { "mime_is_rfc822", vtype_int, &mime_is_rfc822 }, | |
502 | { "mime_part_count", vtype_int, &mime_part_count }, | |
503 | #endif | |
059ec3d9 PH |
504 | { "n0", vtype_filter_int, &filter_n[0] }, |
505 | { "n1", vtype_filter_int, &filter_n[1] }, | |
506 | { "n2", vtype_filter_int, &filter_n[2] }, | |
507 | { "n3", vtype_filter_int, &filter_n[3] }, | |
508 | { "n4", vtype_filter_int, &filter_n[4] }, | |
509 | { "n5", vtype_filter_int, &filter_n[5] }, | |
510 | { "n6", vtype_filter_int, &filter_n[6] }, | |
511 | { "n7", vtype_filter_int, &filter_n[7] }, | |
512 | { "n8", vtype_filter_int, &filter_n[8] }, | |
513 | { "n9", vtype_filter_int, &filter_n[9] }, | |
514 | { "original_domain", vtype_stringptr, &deliver_domain_orig }, | |
515 | { "original_local_part", vtype_stringptr, &deliver_localpart_orig }, | |
516 | { "originator_gid", vtype_gid, &originator_gid }, | |
517 | { "originator_uid", vtype_uid, &originator_uid }, | |
518 | { "parent_domain", vtype_stringptr, &deliver_domain_parent }, | |
519 | { "parent_local_part", vtype_stringptr, &deliver_localpart_parent }, | |
520 | { "pid", vtype_pid, NULL }, | |
521 | { "primary_hostname", vtype_stringptr, &primary_hostname }, | |
fffda43a TK |
522 | { "prvscheck_address", vtype_stringptr, &prvscheck_address }, |
523 | { "prvscheck_keynum", vtype_stringptr, &prvscheck_keynum }, | |
524 | { "prvscheck_result", vtype_stringptr, &prvscheck_result }, | |
059ec3d9 PH |
525 | { "qualify_domain", vtype_stringptr, &qualify_domain_sender }, |
526 | { "qualify_recipient", vtype_stringptr, &qualify_domain_recipient }, | |
527 | { "rcpt_count", vtype_int, &rcpt_count }, | |
528 | { "rcpt_defer_count", vtype_int, &rcpt_defer_count }, | |
529 | { "rcpt_fail_count", vtype_int, &rcpt_fail_count }, | |
530 | { "received_count", vtype_int, &received_count }, | |
531 | { "received_for", vtype_stringptr, &received_for }, | |
194cc0e4 PH |
532 | { "received_ip_address", vtype_stringptr, &interface_address }, |
533 | { "received_port", vtype_int, &interface_port }, | |
059ec3d9 | 534 | { "received_protocol", vtype_stringptr, &received_protocol }, |
7dbf77c9 | 535 | { "received_time", vtype_int, &received_time }, |
059ec3d9 | 536 | { "recipient_data", vtype_stringptr, &recipient_data }, |
8e669ac1 | 537 | { "recipient_verify_failure",vtype_stringptr,&recipient_verify_failure }, |
059ec3d9 PH |
538 | { "recipients", vtype_recipients, NULL }, |
539 | { "recipients_count", vtype_int, &recipients_count }, | |
8523533c TK |
540 | #ifdef WITH_CONTENT_SCAN |
541 | { "regex_match_string", vtype_stringptr, ®ex_match_string }, | |
542 | #endif | |
059ec3d9 PH |
543 | { "reply_address", vtype_reply, NULL }, |
544 | { "return_path", vtype_stringptr, &return_path }, | |
545 | { "return_size_limit", vtype_int, &bounce_return_size_limit }, | |
546 | { "runrc", vtype_int, &runrc }, | |
547 | { "self_hostname", vtype_stringptr, &self_hostname }, | |
548 | { "sender_address", vtype_stringptr, &sender_address }, | |
2a3eea10 | 549 | { "sender_address_data", vtype_stringptr, &sender_address_data }, |
059ec3d9 PH |
550 | { "sender_address_domain", vtype_domain, &sender_address }, |
551 | { "sender_address_local_part", vtype_localpart, &sender_address }, | |
552 | { "sender_data", vtype_stringptr, &sender_data }, | |
553 | { "sender_fullhost", vtype_stringptr, &sender_fullhost }, | |
554 | { "sender_helo_name", vtype_stringptr, &sender_helo_name }, | |
555 | { "sender_host_address", vtype_stringptr, &sender_host_address }, | |
556 | { "sender_host_authenticated",vtype_stringptr, &sender_host_authenticated }, | |
557 | { "sender_host_name", vtype_host_lookup, NULL }, | |
558 | { "sender_host_port", vtype_int, &sender_host_port }, | |
559 | { "sender_ident", vtype_stringptr, &sender_ident }, | |
870f6ba8 TF |
560 | { "sender_rate", vtype_stringptr, &sender_rate }, |
561 | { "sender_rate_limit", vtype_stringptr, &sender_rate_limit }, | |
562 | { "sender_rate_period", vtype_stringptr, &sender_rate_period }, | |
059ec3d9 | 563 | { "sender_rcvhost", vtype_stringptr, &sender_rcvhost }, |
8e669ac1 | 564 | { "sender_verify_failure",vtype_stringptr, &sender_verify_failure }, |
41c7c167 PH |
565 | { "sending_ip_address", vtype_stringptr, &sending_ip_address }, |
566 | { "sending_port", vtype_int, &sending_port }, | |
8e669ac1 | 567 | { "smtp_active_hostname", vtype_stringptr, &smtp_active_hostname }, |
3ee512ff PH |
568 | { "smtp_command", vtype_stringptr, &smtp_cmd_buffer }, |
569 | { "smtp_command_argument", vtype_stringptr, &smtp_cmd_argument }, | |
b01dd148 | 570 | { "smtp_count_at_connection_start", vtype_int, &smtp_accept_count }, |
8f128379 | 571 | { "smtp_notquit_reason", vtype_stringptr, &smtp_notquit_reason }, |
059ec3d9 PH |
572 | { "sn0", vtype_filter_int, &filter_sn[0] }, |
573 | { "sn1", vtype_filter_int, &filter_sn[1] }, | |
574 | { "sn2", vtype_filter_int, &filter_sn[2] }, | |
575 | { "sn3", vtype_filter_int, &filter_sn[3] }, | |
576 | { "sn4", vtype_filter_int, &filter_sn[4] }, | |
577 | { "sn5", vtype_filter_int, &filter_sn[5] }, | |
578 | { "sn6", vtype_filter_int, &filter_sn[6] }, | |
579 | { "sn7", vtype_filter_int, &filter_sn[7] }, | |
580 | { "sn8", vtype_filter_int, &filter_sn[8] }, | |
581 | { "sn9", vtype_filter_int, &filter_sn[9] }, | |
8523533c TK |
582 | #ifdef WITH_CONTENT_SCAN |
583 | { "spam_bar", vtype_stringptr, &spam_bar }, | |
584 | { "spam_report", vtype_stringptr, &spam_report }, | |
585 | { "spam_score", vtype_stringptr, &spam_score }, | |
586 | { "spam_score_int", vtype_stringptr, &spam_score_int }, | |
587 | #endif | |
588 | #ifdef EXPERIMENTAL_SPF | |
65a7d8c3 | 589 | { "spf_guess", vtype_stringptr, &spf_guess }, |
8523533c TK |
590 | { "spf_header_comment", vtype_stringptr, &spf_header_comment }, |
591 | { "spf_received", vtype_stringptr, &spf_received }, | |
592 | { "spf_result", vtype_stringptr, &spf_result }, | |
593 | { "spf_smtp_comment", vtype_stringptr, &spf_smtp_comment }, | |
594 | #endif | |
059ec3d9 | 595 | { "spool_directory", vtype_stringptr, &spool_directory }, |
5cb8cbc6 | 596 | { "spool_inodes", vtype_pinodes, (void *)TRUE }, |
8e669ac1 | 597 | { "spool_space", vtype_pspace, (void *)TRUE }, |
8523533c TK |
598 | #ifdef EXPERIMENTAL_SRS |
599 | { "srs_db_address", vtype_stringptr, &srs_db_address }, | |
600 | { "srs_db_key", vtype_stringptr, &srs_db_key }, | |
601 | { "srs_orig_recipient", vtype_stringptr, &srs_orig_recipient }, | |
602 | { "srs_orig_sender", vtype_stringptr, &srs_orig_sender }, | |
603 | { "srs_recipient", vtype_stringptr, &srs_recipient }, | |
604 | { "srs_status", vtype_stringptr, &srs_status }, | |
605 | #endif | |
059ec3d9 PH |
606 | { "thisaddress", vtype_stringptr, &filter_thisaddress }, |
607 | { "tls_certificate_verified", vtype_int, &tls_certificate_verified }, | |
608 | { "tls_cipher", vtype_stringptr, &tls_cipher }, | |
609 | { "tls_peerdn", vtype_stringptr, &tls_peerdn }, | |
610 | { "tod_bsdinbox", vtype_todbsdin, NULL }, | |
611 | { "tod_epoch", vtype_tode, NULL }, | |
612 | { "tod_full", vtype_todf, NULL }, | |
613 | { "tod_log", vtype_todl, NULL }, | |
614 | { "tod_logfile", vtype_todlf, NULL }, | |
615 | { "tod_zone", vtype_todzone, NULL }, | |
616 | { "tod_zulu", vtype_todzulu, NULL }, | |
617 | { "value", vtype_stringptr, &lookup_value }, | |
618 | { "version_number", vtype_stringptr, &version_string }, | |
619 | { "warn_message_delay", vtype_stringptr, &warnmsg_delay }, | |
620 | { "warn_message_recipient",vtype_stringptr, &warnmsg_recipients }, | |
621 | { "warn_message_recipients",vtype_stringptr,&warnmsg_recipients }, | |
622 | { "warnmsg_delay", vtype_stringptr, &warnmsg_delay }, | |
623 | { "warnmsg_recipient", vtype_stringptr, &warnmsg_recipients }, | |
624 | { "warnmsg_recipients", vtype_stringptr, &warnmsg_recipients } | |
625 | }; | |
626 | ||
627 | static int var_table_size = sizeof(var_table)/sizeof(var_entry); | |
628 | static uschar var_buffer[256]; | |
629 | static BOOL malformed_header; | |
630 | ||
631 | /* For textual hashes */ | |
632 | ||
1ba28e2b PP |
633 | static const char *hashcodes = "abcdefghijklmnopqrtsuvwxyz" |
634 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
635 | "0123456789"; | |
059ec3d9 PH |
636 | |
637 | enum { HMAC_MD5, HMAC_SHA1 }; | |
638 | ||
639 | /* For numeric hashes */ | |
640 | ||
641 | static unsigned int prime[] = { | |
642 | 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, | |
643 | 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, | |
644 | 73, 79, 83, 89, 97, 101, 103, 107, 109, 113}; | |
645 | ||
646 | /* For printing modes in symbolic form */ | |
647 | ||
648 | static uschar *mtable_normal[] = | |
649 | { US"---", US"--x", US"-w-", US"-wx", US"r--", US"r-x", US"rw-", US"rwx" }; | |
650 | ||
651 | static uschar *mtable_setid[] = | |
652 | { US"--S", US"--s", US"-wS", US"-ws", US"r-S", US"r-s", US"rwS", US"rws" }; | |
653 | ||
654 | static uschar *mtable_sticky[] = | |
655 | { US"--T", US"--t", US"-wT", US"-wt", US"r-T", US"r-t", US"rwT", US"rwt" }; | |
656 | ||
657 | ||
658 | ||
659 | /************************************************* | |
660 | * Tables for UTF-8 support * | |
661 | *************************************************/ | |
662 | ||
663 | /* Table of the number of extra characters, indexed by the first character | |
664 | masked with 0x3f. The highest number for a valid UTF-8 character is in fact | |
665 | 0x3d. */ | |
666 | ||
667 | static uschar utf8_table1[] = { | |
668 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, | |
669 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, | |
670 | 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, | |
671 | 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5 }; | |
672 | ||
673 | /* These are the masks for the data bits in the first byte of a character, | |
674 | indexed by the number of additional bytes. */ | |
675 | ||
676 | static int utf8_table2[] = { 0xff, 0x1f, 0x0f, 0x07, 0x03, 0x01}; | |
677 | ||
678 | /* Get the next UTF-8 character, advancing the pointer. */ | |
679 | ||
680 | #define GETUTF8INC(c, ptr) \ | |
681 | c = *ptr++; \ | |
682 | if ((c & 0xc0) == 0xc0) \ | |
683 | { \ | |
684 | int a = utf8_table1[c & 0x3f]; /* Number of additional bytes */ \ | |
685 | int s = 6*a; \ | |
686 | c = (c & utf8_table2[a]) << s; \ | |
687 | while (a-- > 0) \ | |
688 | { \ | |
689 | s -= 6; \ | |
690 | c |= (*ptr++ & 0x3f) << s; \ | |
691 | } \ | |
692 | } | |
693 | ||
694 | ||
695 | /************************************************* | |
696 | * Binary chop search on a table * | |
697 | *************************************************/ | |
698 | ||
699 | /* This is used for matching expansion items and operators. | |
700 | ||
701 | Arguments: | |
702 | name the name that is being sought | |
703 | table the table to search | |
704 | table_size the number of items in the table | |
705 | ||
706 | Returns: the offset in the table, or -1 | |
707 | */ | |
708 | ||
709 | static int | |
710 | chop_match(uschar *name, uschar **table, int table_size) | |
711 | { | |
712 | uschar **bot = table; | |
713 | uschar **top = table + table_size; | |
714 | ||
715 | while (top > bot) | |
716 | { | |
717 | uschar **mid = bot + (top - bot)/2; | |
718 | int c = Ustrcmp(name, *mid); | |
719 | if (c == 0) return mid - table; | |
720 | if (c > 0) bot = mid + 1; else top = mid; | |
721 | } | |
722 | ||
723 | return -1; | |
724 | } | |
725 | ||
726 | ||
727 | ||
728 | /************************************************* | |
729 | * Check a condition string * | |
730 | *************************************************/ | |
731 | ||
732 | /* This function is called to expand a string, and test the result for a "true" | |
733 | or "false" value. Failure of the expansion yields FALSE; logged unless it was a | |
734 | forced fail or lookup defer. All store used by the function can be released on | |
735 | exit. | |
736 | ||
6a8de854 PP |
737 | The actual false-value tests should be replicated for ECOND_BOOL_LAX. |
738 | ||
059ec3d9 PH |
739 | Arguments: |
740 | condition the condition string | |
741 | m1 text to be incorporated in panic error | |
742 | m2 ditto | |
743 | ||
744 | Returns: TRUE if condition is met, FALSE if not | |
745 | */ | |
746 | ||
747 | BOOL | |
748 | expand_check_condition(uschar *condition, uschar *m1, uschar *m2) | |
749 | { | |
750 | int rc; | |
751 | void *reset_point = store_get(0); | |
752 | uschar *ss = expand_string(condition); | |
753 | if (ss == NULL) | |
754 | { | |
755 | if (!expand_string_forcedfail && !search_find_defer) | |
756 | log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand condition \"%s\" " | |
757 | "for %s %s: %s", condition, m1, m2, expand_string_message); | |
758 | return FALSE; | |
759 | } | |
760 | rc = ss[0] != 0 && Ustrcmp(ss, "0") != 0 && strcmpic(ss, US"no") != 0 && | |
761 | strcmpic(ss, US"false") != 0; | |
762 | store_reset(reset_point); | |
763 | return rc; | |
764 | } | |
765 | ||
766 | ||
767 | ||
9e3331ea TK |
768 | /************************************************* |
769 | * Pseudo-random number generation * | |
770 | *************************************************/ | |
771 | ||
772 | /* Pseudo-random number generation. The result is not "expected" to be | |
773 | cryptographically strong but not so weak that someone will shoot themselves | |
774 | in the foot using it as a nonce in some email header scheme or whatever | |
775 | weirdness they'll twist this into. The result should ideally handle fork(). | |
776 | ||
777 | However, if we're stuck unable to provide this, then we'll fall back to | |
778 | appallingly bad randomness. | |
779 | ||
780 | If SUPPORT_TLS is defined and OpenSSL is used, then this will not be used. | |
781 | The GNUTLS randomness functions found do not seem amenable to extracting | |
782 | random numbers outside of a TLS context. Any volunteers? | |
783 | ||
784 | Arguments: | |
785 | max range maximum | |
786 | Returns a random number in range [0, max-1] | |
787 | */ | |
788 | ||
789 | #if !defined(SUPPORT_TLS) || defined(USE_GNUTLS) | |
790 | int | |
791 | pseudo_random_number(int max) | |
792 | { | |
793 | static pid_t pid = 0; | |
794 | pid_t p2; | |
795 | #if defined(HAVE_SRANDOM) && !defined(HAVE_SRANDOMDEV) | |
796 | struct timeval tv; | |
797 | #endif | |
798 | ||
799 | p2 = getpid(); | |
800 | if (p2 != pid) | |
801 | { | |
802 | if (pid != 0) | |
803 | { | |
804 | ||
805 | #ifdef HAVE_ARC4RANDOM | |
806 | /* cryptographically strong randomness, common on *BSD platforms, not | |
807 | so much elsewhere. Alas. */ | |
808 | arc4random_stir(); | |
809 | #elif defined(HAVE_SRANDOM) || defined(HAVE_SRANDOMDEV) | |
810 | #ifdef HAVE_SRANDOMDEV | |
811 | /* uses random(4) for seeding */ | |
812 | srandomdev(); | |
813 | #else | |
814 | gettimeofday(&tv, NULL); | |
815 | srandom(tv.tv_sec | tv.tv_usec | getpid()); | |
816 | #endif | |
817 | #else | |
818 | /* Poor randomness and no seeding here */ | |
819 | #endif | |
820 | ||
821 | } | |
822 | pid = p2; | |
823 | } | |
824 | ||
825 | #ifdef HAVE_ARC4RANDOM | |
826 | return arc4random() % max; | |
827 | #elif defined(HAVE_SRANDOM) || defined(HAVE_SRANDOMDEV) | |
828 | return random() % max; | |
829 | #else | |
830 | /* This one returns a 16-bit number, definitely not crypto-strong */ | |
831 | return random_number(max); | |
832 | #endif | |
833 | } | |
834 | ||
835 | #endif | |
836 | ||
059ec3d9 PH |
837 | /************************************************* |
838 | * Pick out a name from a string * | |
839 | *************************************************/ | |
840 | ||
841 | /* If the name is too long, it is silently truncated. | |
842 | ||
843 | Arguments: | |
844 | name points to a buffer into which to put the name | |
845 | max is the length of the buffer | |
846 | s points to the first alphabetic character of the name | |
847 | extras chars other than alphanumerics to permit | |
848 | ||
849 | Returns: pointer to the first character after the name | |
850 | ||
851 | Note: The test for *s != 0 in the while loop is necessary because | |
852 | Ustrchr() yields non-NULL if the character is zero (which is not something | |
853 | I expected). */ | |
854 | ||
855 | static uschar * | |
856 | read_name(uschar *name, int max, uschar *s, uschar *extras) | |
857 | { | |
858 | int ptr = 0; | |
859 | while (*s != 0 && (isalnum(*s) || Ustrchr(extras, *s) != NULL)) | |
860 | { | |
861 | if (ptr < max-1) name[ptr++] = *s; | |
862 | s++; | |
863 | } | |
864 | name[ptr] = 0; | |
865 | return s; | |
866 | } | |
867 | ||
868 | ||
869 | ||
870 | /************************************************* | |
871 | * Pick out the rest of a header name * | |
872 | *************************************************/ | |
873 | ||
874 | /* A variable name starting $header_ (or just $h_ for those who like | |
875 | abbreviations) might not be the complete header name because headers can | |
876 | contain any printing characters in their names, except ':'. This function is | |
877 | called to read the rest of the name, chop h[eader]_ off the front, and put ':' | |
878 | on the end, if the name was terminated by white space. | |
879 | ||
880 | Arguments: | |
881 | name points to a buffer in which the name read so far exists | |
882 | max is the length of the buffer | |
883 | s points to the first character after the name so far, i.e. the | |
884 | first non-alphameric character after $header_xxxxx | |
885 | ||
886 | Returns: a pointer to the first character after the header name | |
887 | */ | |
888 | ||
889 | static uschar * | |
890 | read_header_name(uschar *name, int max, uschar *s) | |
891 | { | |
892 | int prelen = Ustrchr(name, '_') - name + 1; | |
893 | int ptr = Ustrlen(name) - prelen; | |
894 | if (ptr > 0) memmove(name, name+prelen, ptr); | |
895 | while (mac_isgraph(*s) && *s != ':') | |
896 | { | |
897 | if (ptr < max-1) name[ptr++] = *s; | |
898 | s++; | |
899 | } | |
900 | if (*s == ':') s++; | |
901 | name[ptr++] = ':'; | |
902 | name[ptr] = 0; | |
903 | return s; | |
904 | } | |
905 | ||
906 | ||
907 | ||
908 | /************************************************* | |
909 | * Pick out a number from a string * | |
910 | *************************************************/ | |
911 | ||
912 | /* Arguments: | |
913 | n points to an integer into which to put the number | |
914 | s points to the first digit of the number | |
915 | ||
916 | Returns: a pointer to the character after the last digit | |
917 | */ | |
918 | ||
919 | static uschar * | |
920 | read_number(int *n, uschar *s) | |
921 | { | |
922 | *n = 0; | |
923 | while (isdigit(*s)) *n = *n * 10 + (*s++ - '0'); | |
924 | return s; | |
925 | } | |
926 | ||
927 | ||
928 | ||
929 | /************************************************* | |
930 | * Extract keyed subfield from a string * | |
931 | *************************************************/ | |
932 | ||
933 | /* The yield is in dynamic store; NULL means that the key was not found. | |
934 | ||
935 | Arguments: | |
936 | key points to the name of the key | |
937 | s points to the string from which to extract the subfield | |
938 | ||
939 | Returns: NULL if the subfield was not found, or | |
940 | a pointer to the subfield's data | |
941 | */ | |
942 | ||
943 | static uschar * | |
944 | expand_getkeyed(uschar *key, uschar *s) | |
945 | { | |
946 | int length = Ustrlen(key); | |
947 | while (isspace(*s)) s++; | |
948 | ||
949 | /* Loop to search for the key */ | |
950 | ||
951 | while (*s != 0) | |
952 | { | |
953 | int dkeylength; | |
954 | uschar *data; | |
955 | uschar *dkey = s; | |
956 | ||
957 | while (*s != 0 && *s != '=' && !isspace(*s)) s++; | |
958 | dkeylength = s - dkey; | |
959 | while (isspace(*s)) s++; | |
960 | if (*s == '=') while (isspace((*(++s)))); | |
961 | ||
962 | data = string_dequote(&s); | |
963 | if (length == dkeylength && strncmpic(key, dkey, length) == 0) | |
964 | return data; | |
965 | ||
966 | while (isspace(*s)) s++; | |
967 | } | |
968 | ||
969 | return NULL; | |
970 | } | |
971 | ||
972 | ||
973 | ||
974 | ||
975 | /************************************************* | |
976 | * Extract numbered subfield from string * | |
977 | *************************************************/ | |
978 | ||
979 | /* Extracts a numbered field from a string that is divided by tokens - for | |
980 | example a line from /etc/passwd is divided by colon characters. First field is | |
981 | numbered one. Negative arguments count from the right. Zero returns the whole | |
982 | string. Returns NULL if there are insufficient tokens in the string | |
983 | ||
984 | ***WARNING*** | |
985 | Modifies final argument - this is a dynamically generated string, so that's OK. | |
986 | ||
987 | Arguments: | |
988 | field number of field to be extracted, | |
989 | first field = 1, whole string = 0, last field = -1 | |
990 | separators characters that are used to break string into tokens | |
991 | s points to the string from which to extract the subfield | |
992 | ||
993 | Returns: NULL if the field was not found, | |
994 | a pointer to the field's data inside s (modified to add 0) | |
995 | */ | |
996 | ||
997 | static uschar * | |
998 | expand_gettokened (int field, uschar *separators, uschar *s) | |
999 | { | |
1000 | int sep = 1; | |
1001 | int count; | |
1002 | uschar *ss = s; | |
1003 | uschar *fieldtext = NULL; | |
1004 | ||
1005 | if (field == 0) return s; | |
1006 | ||
1007 | /* Break the line up into fields in place; for field > 0 we stop when we have | |
1008 | done the number of fields we want. For field < 0 we continue till the end of | |
1009 | the string, counting the number of fields. */ | |
1010 | ||
1011 | count = (field > 0)? field : INT_MAX; | |
1012 | ||
1013 | while (count-- > 0) | |
1014 | { | |
1015 | size_t len; | |
1016 | ||
1017 | /* Previous field was the last one in the string. For a positive field | |
1018 | number, this means there are not enough fields. For a negative field number, | |
1019 | check that there are enough, and scan back to find the one that is wanted. */ | |
1020 | ||
1021 | if (sep == 0) | |
1022 | { | |
1023 | if (field > 0 || (-field) > (INT_MAX - count - 1)) return NULL; | |
1024 | if ((-field) == (INT_MAX - count - 1)) return s; | |
1025 | while (field++ < 0) | |
1026 | { | |
1027 | ss--; | |
1028 | while (ss[-1] != 0) ss--; | |
1029 | } | |
1030 | fieldtext = ss; | |
1031 | break; | |
1032 | } | |
1033 | ||
1034 | /* Previous field was not last in the string; save its start and put a | |
1035 | zero at its end. */ | |
1036 | ||
1037 | fieldtext = ss; | |
1038 | len = Ustrcspn(ss, separators); | |
1039 | sep = ss[len]; | |
1040 | ss[len] = 0; | |
1041 | ss += len + 1; | |
1042 | } | |
1043 | ||
1044 | return fieldtext; | |
1045 | } | |
1046 | ||
1047 | ||
1048 | ||
1049 | /************************************************* | |
1050 | * Extract a substring from a string * | |
1051 | *************************************************/ | |
1052 | ||
1053 | /* Perform the ${substr or ${length expansion operations. | |
1054 | ||
1055 | Arguments: | |
1056 | subject the input string | |
1057 | value1 the offset from the start of the input string to the start of | |
1058 | the output string; if negative, count from the right. | |
1059 | value2 the length of the output string, or negative (-1) for unset | |
1060 | if value1 is positive, unset means "all after" | |
1061 | if value1 is negative, unset means "all before" | |
1062 | len set to the length of the returned string | |
1063 | ||
1064 | Returns: pointer to the output string, or NULL if there is an error | |
1065 | */ | |
1066 | ||
1067 | static uschar * | |
1068 | extract_substr(uschar *subject, int value1, int value2, int *len) | |
1069 | { | |
1070 | int sublen = Ustrlen(subject); | |
1071 | ||
1072 | if (value1 < 0) /* count from right */ | |
1073 | { | |
1074 | value1 += sublen; | |
1075 | ||
1076 | /* If the position is before the start, skip to the start, and adjust the | |
1077 | length. If the length ends up negative, the substring is null because nothing | |
1078 | can precede. This falls out naturally when the length is unset, meaning "all | |
1079 | to the left". */ | |
1080 | ||
1081 | if (value1 < 0) | |
1082 | { | |
1083 | value2 += value1; | |
1084 | if (value2 < 0) value2 = 0; | |
1085 | value1 = 0; | |
1086 | } | |
1087 | ||
1088 | /* Otherwise an unset length => characters before value1 */ | |
1089 | ||
1090 | else if (value2 < 0) | |
1091 | { | |
1092 | value2 = value1; | |
1093 | value1 = 0; | |
1094 | } | |
1095 | } | |
1096 | ||
1097 | /* For a non-negative offset, if the starting position is past the end of the | |
1098 | string, the result will be the null string. Otherwise, an unset length means | |
1099 | "rest"; just set it to the maximum - it will be cut down below if necessary. */ | |
1100 | ||
1101 | else | |
1102 | { | |
1103 | if (value1 > sublen) | |
1104 | { | |
1105 | value1 = sublen; | |
1106 | value2 = 0; | |
1107 | } | |
1108 | else if (value2 < 0) value2 = sublen; | |
1109 | } | |
1110 | ||
1111 | /* Cut the length down to the maximum possible for the offset value, and get | |
1112 | the required characters. */ | |
1113 | ||
1114 | if (value1 + value2 > sublen) value2 = sublen - value1; | |
1115 | *len = value2; | |
1116 | return subject + value1; | |
1117 | } | |
1118 | ||
1119 | ||
1120 | ||
1121 | ||
1122 | /************************************************* | |
1123 | * Old-style hash of a string * | |
1124 | *************************************************/ | |
1125 | ||
1126 | /* Perform the ${hash expansion operation. | |
1127 | ||
1128 | Arguments: | |
1129 | subject the input string (an expanded substring) | |
1130 | value1 the length of the output string; if greater or equal to the | |
1131 | length of the input string, the input string is returned | |
1132 | value2 the number of hash characters to use, or 26 if negative | |
1133 | len set to the length of the returned string | |
1134 | ||
1135 | Returns: pointer to the output string, or NULL if there is an error | |
1136 | */ | |
1137 | ||
1138 | static uschar * | |
1139 | compute_hash(uschar *subject, int value1, int value2, int *len) | |
1140 | { | |
1141 | int sublen = Ustrlen(subject); | |
1142 | ||
1143 | if (value2 < 0) value2 = 26; | |
1144 | else if (value2 > Ustrlen(hashcodes)) | |
1145 | { | |
1146 | expand_string_message = | |
1147 | string_sprintf("hash count \"%d\" too big", value2); | |
1148 | return NULL; | |
1149 | } | |
1150 | ||
1151 | /* Calculate the hash text. We know it is shorter than the original string, so | |
1152 | can safely place it in subject[] (we know that subject is always itself an | |
1153 | expanded substring). */ | |
1154 | ||
1155 | if (value1 < sublen) | |
1156 | { | |
1157 | int c; | |
1158 | int i = 0; | |
1159 | int j = value1; | |
1160 | while ((c = (subject[j])) != 0) | |
1161 | { | |
1162 | int shift = (c + j++) & 7; | |
1163 | subject[i] ^= (c << shift) | (c >> (8-shift)); | |
1164 | if (++i >= value1) i = 0; | |
1165 | } | |
1166 | for (i = 0; i < value1; i++) | |
1167 | subject[i] = hashcodes[(subject[i]) % value2]; | |
1168 | } | |
1169 | else value1 = sublen; | |
1170 | ||
1171 | *len = value1; | |
1172 | return subject; | |
1173 | } | |
1174 | ||
1175 | ||
1176 | ||
1177 | ||
1178 | /************************************************* | |
1179 | * Numeric hash of a string * | |
1180 | *************************************************/ | |
1181 | ||
1182 | /* Perform the ${nhash expansion operation. The first characters of the | |
1183 | string are treated as most important, and get the highest prime numbers. | |
1184 | ||
1185 | Arguments: | |
1186 | subject the input string | |
1187 | value1 the maximum value of the first part of the result | |
1188 | value2 the maximum value of the second part of the result, | |
1189 | or negative to produce only a one-part result | |
1190 | len set to the length of the returned string | |
1191 | ||
1192 | Returns: pointer to the output string, or NULL if there is an error. | |
1193 | */ | |
1194 | ||
1195 | static uschar * | |
1196 | compute_nhash (uschar *subject, int value1, int value2, int *len) | |
1197 | { | |
1198 | uschar *s = subject; | |
1199 | int i = 0; | |
1200 | unsigned long int total = 0; /* no overflow */ | |
1201 | ||
1202 | while (*s != 0) | |
1203 | { | |
1204 | if (i == 0) i = sizeof(prime)/sizeof(int) - 1; | |
1205 | total += prime[i--] * (unsigned int)(*s++); | |
1206 | } | |
1207 | ||
1208 | /* If value2 is unset, just compute one number */ | |
1209 | ||
1210 | if (value2 < 0) | |
1211 | { | |
1212 | s = string_sprintf("%d", total % value1); | |
1213 | } | |
1214 | ||
1215 | /* Otherwise do a div/mod hash */ | |
1216 | ||
1217 | else | |
1218 | { | |
1219 | total = total % (value1 * value2); | |
1220 | s = string_sprintf("%d/%d", total/value2, total % value2); | |
1221 | } | |
1222 | ||
1223 | *len = Ustrlen(s); | |
1224 | return s; | |
1225 | } | |
1226 | ||
1227 | ||
1228 | ||
1229 | ||
1230 | ||
1231 | /************************************************* | |
1232 | * Find the value of a header or headers * | |
1233 | *************************************************/ | |
1234 | ||
1235 | /* Multiple instances of the same header get concatenated, and this function | |
1236 | can also return a concatenation of all the header lines. When concatenating | |
1237 | specific headers that contain lists of addresses, a comma is inserted between | |
1238 | them. Otherwise we use a straight concatenation. Because some messages can have | |
1239 | pathologically large number of lines, there is a limit on the length that is | |
1240 | returned. Also, to avoid massive store use which would result from using | |
1241 | string_cat() as it copies and extends strings, we do a preliminary pass to find | |
1242 | out exactly how much store will be needed. On "normal" messages this will be | |
1243 | pretty trivial. | |
1244 | ||
1245 | Arguments: | |
1246 | name the name of the header, without the leading $header_ or $h_, | |
1247 | or NULL if a concatenation of all headers is required | |
1248 | exists_only TRUE if called from a def: test; don't need to build a string; | |
1249 | just return a string that is not "" and not "0" if the header | |
1250 | exists | |
1251 | newsize return the size of memory block that was obtained; may be NULL | |
1252 | if exists_only is TRUE | |
1253 | want_raw TRUE if called for $rh_ or $rheader_ variables; no processing, | |
ff75a1f7 PH |
1254 | other than concatenating, will be done on the header. Also used |
1255 | for $message_headers_raw. | |
059ec3d9 PH |
1256 | charset name of charset to translate MIME words to; used only if |
1257 | want_raw is false; if NULL, no translation is done (this is | |
1258 | used for $bh_ and $bheader_) | |
1259 | ||
1260 | Returns: NULL if the header does not exist, else a pointer to a new | |
1261 | store block | |
1262 | */ | |
1263 | ||
1264 | static uschar * | |
1265 | find_header(uschar *name, BOOL exists_only, int *newsize, BOOL want_raw, | |
1266 | uschar *charset) | |
1267 | { | |
1268 | BOOL found = name == NULL; | |
1269 | int comma = 0; | |
1270 | int len = found? 0 : Ustrlen(name); | |
1271 | int i; | |
1272 | uschar *yield = NULL; | |
1273 | uschar *ptr = NULL; | |
1274 | ||
1275 | /* Loop for two passes - saves code repetition */ | |
1276 | ||
1277 | for (i = 0; i < 2; i++) | |
1278 | { | |
1279 | int size = 0; | |
1280 | header_line *h; | |
1281 | ||
1282 | for (h = header_list; size < header_insert_maxlen && h != NULL; h = h->next) | |
1283 | { | |
1284 | if (h->type != htype_old && h->text != NULL) /* NULL => Received: placeholder */ | |
1285 | { | |
1286 | if (name == NULL || (len <= h->slen && strncmpic(name, h->text, len) == 0)) | |
1287 | { | |
1288 | int ilen; | |
1289 | uschar *t; | |
1290 | ||
1291 | if (exists_only) return US"1"; /* don't need actual string */ | |
1292 | found = TRUE; | |
1293 | t = h->text + len; /* text to insert */ | |
1294 | if (!want_raw) /* unless wanted raw, */ | |
1295 | while (isspace(*t)) t++; /* remove leading white space */ | |
1296 | ilen = h->slen - (t - h->text); /* length to insert */ | |
1297 | ||
fd700877 PH |
1298 | /* Unless wanted raw, remove trailing whitespace, including the |
1299 | newline. */ | |
1300 | ||
1301 | if (!want_raw) | |
1302 | while (ilen > 0 && isspace(t[ilen-1])) ilen--; | |
1303 | ||
059ec3d9 PH |
1304 | /* Set comma = 1 if handling a single header and it's one of those |
1305 | that contains an address list, except when asked for raw headers. Only | |
1306 | need to do this once. */ | |
1307 | ||
1308 | if (!want_raw && name != NULL && comma == 0 && | |
1309 | Ustrchr("BCFRST", h->type) != NULL) | |
1310 | comma = 1; | |
1311 | ||
1312 | /* First pass - compute total store needed; second pass - compute | |
1313 | total store used, including this header. */ | |
1314 | ||
fd700877 | 1315 | size += ilen + comma + 1; /* +1 for the newline */ |
059ec3d9 PH |
1316 | |
1317 | /* Second pass - concatentate the data, up to a maximum. Note that | |
1318 | the loop stops when size hits the limit. */ | |
1319 | ||
1320 | if (i != 0) | |
1321 | { | |
1322 | if (size > header_insert_maxlen) | |
1323 | { | |
fd700877 | 1324 | ilen -= size - header_insert_maxlen - 1; |
059ec3d9 PH |
1325 | comma = 0; |
1326 | } | |
1327 | Ustrncpy(ptr, t, ilen); | |
1328 | ptr += ilen; | |
fd700877 PH |
1329 | |
1330 | /* For a non-raw header, put in the comma if needed, then add | |
3168332a PH |
1331 | back the newline we removed above, provided there was some text in |
1332 | the header. */ | |
fd700877 | 1333 | |
3168332a | 1334 | if (!want_raw && ilen > 0) |
059ec3d9 | 1335 | { |
3168332a | 1336 | if (comma != 0) *ptr++ = ','; |
059ec3d9 PH |
1337 | *ptr++ = '\n'; |
1338 | } | |
1339 | } | |
1340 | } | |
1341 | } | |
1342 | } | |
1343 | ||
fd700877 PH |
1344 | /* At end of first pass, return NULL if no header found. Then truncate size |
1345 | if necessary, and get the buffer to hold the data, returning the buffer size. | |
1346 | */ | |
059ec3d9 PH |
1347 | |
1348 | if (i == 0) | |
1349 | { | |
1350 | if (!found) return NULL; | |
1351 | if (size > header_insert_maxlen) size = header_insert_maxlen; | |
1352 | *newsize = size + 1; | |
1353 | ptr = yield = store_get(*newsize); | |
1354 | } | |
1355 | } | |
1356 | ||
059ec3d9 PH |
1357 | /* That's all we do for raw header expansion. */ |
1358 | ||
1359 | if (want_raw) | |
1360 | { | |
1361 | *ptr = 0; | |
1362 | } | |
1363 | ||
fd700877 PH |
1364 | /* Otherwise, remove a final newline and a redundant added comma. Then we do |
1365 | RFC 2047 decoding, translating the charset if requested. The rfc2047_decode2() | |
059ec3d9 PH |
1366 | function can return an error with decoded data if the charset translation |
1367 | fails. If decoding fails, it returns NULL. */ | |
1368 | ||
1369 | else | |
1370 | { | |
1371 | uschar *decoded, *error; | |
3168332a | 1372 | if (ptr > yield && ptr[-1] == '\n') ptr--; |
fd700877 | 1373 | if (ptr > yield && comma != 0 && ptr[-1] == ',') ptr--; |
059ec3d9 | 1374 | *ptr = 0; |
a0d6ba8a PH |
1375 | decoded = rfc2047_decode2(yield, check_rfc2047_length, charset, '?', NULL, |
1376 | newsize, &error); | |
059ec3d9 PH |
1377 | if (error != NULL) |
1378 | { | |
1379 | DEBUG(D_any) debug_printf("*** error in RFC 2047 decoding: %s\n" | |
1380 | " input was: %s\n", error, yield); | |
1381 | } | |
1382 | if (decoded != NULL) yield = decoded; | |
1383 | } | |
1384 | ||
1385 | return yield; | |
1386 | } | |
1387 | ||
1388 | ||
1389 | ||
1390 | ||
1391 | /************************************************* | |
1392 | * Find value of a variable * | |
1393 | *************************************************/ | |
1394 | ||
1395 | /* The table of variables is kept in alphabetic order, so we can search it | |
1396 | using a binary chop. The "choplen" variable is nothing to do with the binary | |
1397 | chop. | |
1398 | ||
1399 | Arguments: | |
1400 | name the name of the variable being sought | |
1401 | exists_only TRUE if this is a def: test; passed on to find_header() | |
1402 | skipping TRUE => skip any processing evaluation; this is not the same as | |
1403 | exists_only because def: may test for values that are first | |
1404 | evaluated here | |
1405 | newsize pointer to an int which is initially zero; if the answer is in | |
1406 | a new memory buffer, *newsize is set to its size | |
1407 | ||
1408 | Returns: NULL if the variable does not exist, or | |
1409 | a pointer to the variable's contents, or | |
1410 | something non-NULL if exists_only is TRUE | |
1411 | */ | |
1412 | ||
1413 | static uschar * | |
1414 | find_variable(uschar *name, BOOL exists_only, BOOL skipping, int *newsize) | |
1415 | { | |
1416 | int first = 0; | |
1417 | int last = var_table_size; | |
1418 | ||
38a0a95f PH |
1419 | /* Handle ACL variables, whose names are of the form acl_cxxx or acl_mxxx. |
1420 | Originally, xxx had to be a number in the range 0-9 (later 0-19), but from | |
1421 | release 4.64 onwards arbitrary names are permitted, as long as the first 5 | |
641cb756 PH |
1422 | characters are acl_c or acl_m and the sixth is either a digit or an underscore |
1423 | (this gave backwards compatibility at the changeover). There may be built-in | |
1424 | variables whose names start acl_ but they should never start in this way. This | |
1425 | slightly messy specification is a consequence of the history, needless to say. | |
47ca6d6c | 1426 | |
38a0a95f PH |
1427 | If an ACL variable does not exist, treat it as empty, unless strict_acl_vars is |
1428 | set, in which case give an error. */ | |
47ca6d6c | 1429 | |
641cb756 PH |
1430 | if ((Ustrncmp(name, "acl_c", 5) == 0 || Ustrncmp(name, "acl_m", 5) == 0) && |
1431 | !isalpha(name[5])) | |
38a0a95f PH |
1432 | { |
1433 | tree_node *node = | |
1434 | tree_search((name[4] == 'c')? acl_var_c : acl_var_m, name + 4); | |
1435 | return (node == NULL)? (strict_acl_vars? NULL : US"") : node->data.ptr; | |
47ca6d6c PH |
1436 | } |
1437 | ||
38a0a95f | 1438 | /* Handle $auth<n> variables. */ |
f78eb7c6 PH |
1439 | |
1440 | if (Ustrncmp(name, "auth", 4) == 0) | |
1441 | { | |
1442 | uschar *endptr; | |
1443 | int n = Ustrtoul(name + 4, &endptr, 10); | |
1444 | if (*endptr == 0 && n != 0 && n <= AUTH_VARS) | |
1445 | return (auth_vars[n-1] == NULL)? US"" : auth_vars[n-1]; | |
1446 | } | |
1447 | ||
47ca6d6c PH |
1448 | /* For all other variables, search the table */ |
1449 | ||
059ec3d9 PH |
1450 | while (last > first) |
1451 | { | |
1452 | uschar *s, *domain; | |
1453 | uschar **ss; | |
1454 | int middle = (first + last)/2; | |
1455 | int c = Ustrcmp(name, var_table[middle].name); | |
1456 | ||
1457 | if (c > 0) { first = middle + 1; continue; } | |
1458 | if (c < 0) { last = middle; continue; } | |
1459 | ||
1460 | /* Found an existing variable. If in skipping state, the value isn't needed, | |
47ca6d6c | 1461 | and we want to avoid processing (such as looking up the host name). */ |
059ec3d9 PH |
1462 | |
1463 | if (skipping) return US""; | |
1464 | ||
1465 | switch (var_table[middle].type) | |
1466 | { | |
9a26b6b2 PH |
1467 | case vtype_filter_int: |
1468 | if (!filter_running) return NULL; | |
1469 | /* Fall through */ | |
1470 | /* VVVVVVVVVVVV */ | |
059ec3d9 PH |
1471 | case vtype_int: |
1472 | sprintf(CS var_buffer, "%d", *(int *)(var_table[middle].value)); /* Integer */ | |
1473 | return var_buffer; | |
1474 | ||
1475 | case vtype_ino: | |
1476 | sprintf(CS var_buffer, "%ld", (long int)(*(ino_t *)(var_table[middle].value))); /* Inode */ | |
1477 | return var_buffer; | |
1478 | ||
1479 | case vtype_gid: | |
1480 | sprintf(CS var_buffer, "%ld", (long int)(*(gid_t *)(var_table[middle].value))); /* gid */ | |
1481 | return var_buffer; | |
1482 | ||
1483 | case vtype_uid: | |
1484 | sprintf(CS var_buffer, "%ld", (long int)(*(uid_t *)(var_table[middle].value))); /* uid */ | |
1485 | return var_buffer; | |
1486 | ||
1487 | case vtype_stringptr: /* Pointer to string */ | |
1488 | s = *((uschar **)(var_table[middle].value)); | |
1489 | return (s == NULL)? US"" : s; | |
1490 | ||
1491 | case vtype_pid: | |
1492 | sprintf(CS var_buffer, "%d", (int)getpid()); /* pid */ | |
1493 | return var_buffer; | |
1494 | ||
1495 | case vtype_load_avg: | |
8669f003 | 1496 | sprintf(CS var_buffer, "%d", OS_GETLOADAVG()); /* load_average */ |
059ec3d9 PH |
1497 | return var_buffer; |
1498 | ||
1499 | case vtype_host_lookup: /* Lookup if not done so */ | |
1500 | if (sender_host_name == NULL && sender_host_address != NULL && | |
1501 | !host_lookup_failed && host_name_lookup() == OK) | |
1502 | host_build_sender_fullhost(); | |
1503 | return (sender_host_name == NULL)? US"" : sender_host_name; | |
1504 | ||
1505 | case vtype_localpart: /* Get local part from address */ | |
1506 | s = *((uschar **)(var_table[middle].value)); | |
1507 | if (s == NULL) return US""; | |
1508 | domain = Ustrrchr(s, '@'); | |
1509 | if (domain == NULL) return s; | |
1510 | if (domain - s > sizeof(var_buffer) - 1) | |
1511 | log_write(0, LOG_MAIN|LOG_PANIC_DIE, "local part longer than %d in " | |
1512 | "string expansion", sizeof(var_buffer)); | |
1513 | Ustrncpy(var_buffer, s, domain - s); | |
1514 | var_buffer[domain - s] = 0; | |
1515 | return var_buffer; | |
1516 | ||
1517 | case vtype_domain: /* Get domain from address */ | |
1518 | s = *((uschar **)(var_table[middle].value)); | |
1519 | if (s == NULL) return US""; | |
1520 | domain = Ustrrchr(s, '@'); | |
1521 | return (domain == NULL)? US"" : domain + 1; | |
1522 | ||
1523 | case vtype_msgheaders: | |
1524 | return find_header(NULL, exists_only, newsize, FALSE, NULL); | |
1525 | ||
ff75a1f7 PH |
1526 | case vtype_msgheaders_raw: |
1527 | return find_header(NULL, exists_only, newsize, TRUE, NULL); | |
1528 | ||
059ec3d9 PH |
1529 | case vtype_msgbody: /* Pointer to msgbody string */ |
1530 | case vtype_msgbody_end: /* Ditto, the end of the msg */ | |
1531 | ss = (uschar **)(var_table[middle].value); | |
1532 | if (*ss == NULL && deliver_datafile >= 0) /* Read body when needed */ | |
1533 | { | |
1534 | uschar *body; | |
0d7eb84a | 1535 | off_t start_offset = SPOOL_DATA_START_OFFSET; |
059ec3d9 PH |
1536 | int len = message_body_visible; |
1537 | if (len > message_size) len = message_size; | |
1538 | *ss = body = store_malloc(len+1); | |
1539 | body[0] = 0; | |
1540 | if (var_table[middle].type == vtype_msgbody_end) | |
1541 | { | |
1542 | struct stat statbuf; | |
1543 | if (fstat(deliver_datafile, &statbuf) == 0) | |
1544 | { | |
1545 | start_offset = statbuf.st_size - len; | |
1546 | if (start_offset < SPOOL_DATA_START_OFFSET) | |
1547 | start_offset = SPOOL_DATA_START_OFFSET; | |
1548 | } | |
1549 | } | |
1550 | lseek(deliver_datafile, start_offset, SEEK_SET); | |
1551 | len = read(deliver_datafile, body, len); | |
1552 | if (len > 0) | |
1553 | { | |
1554 | body[len] = 0; | |
ddea74fa | 1555 | if (message_body_newlines) /* Separate loops for efficiency */ |
059ec3d9 | 1556 | { |
ddea74fa PH |
1557 | while (len > 0) |
1558 | { if (body[--len] == 0) body[len] = ' '; } | |
1559 | } | |
1560 | else | |
1561 | { | |
1562 | while (len > 0) | |
1563 | { if (body[--len] == '\n' || body[len] == 0) body[len] = ' '; } | |
059ec3d9 PH |
1564 | } |
1565 | } | |
1566 | } | |
1567 | return (*ss == NULL)? US"" : *ss; | |
1568 | ||
1569 | case vtype_todbsdin: /* BSD inbox time of day */ | |
1570 | return tod_stamp(tod_bsdin); | |
1571 | ||
1572 | case vtype_tode: /* Unix epoch time of day */ | |
1573 | return tod_stamp(tod_epoch); | |
1574 | ||
1575 | case vtype_todf: /* Full time of day */ | |
1576 | return tod_stamp(tod_full); | |
1577 | ||
1578 | case vtype_todl: /* Log format time of day */ | |
1579 | return tod_stamp(tod_log_bare); /* (without timezone) */ | |
1580 | ||
1581 | case vtype_todzone: /* Time zone offset only */ | |
1582 | return tod_stamp(tod_zone); | |
1583 | ||
1584 | case vtype_todzulu: /* Zulu time */ | |
1585 | return tod_stamp(tod_zulu); | |
1586 | ||
1587 | case vtype_todlf: /* Log file datestamp tod */ | |
f1e5fef5 | 1588 | return tod_stamp(tod_log_datestamp_daily); |
059ec3d9 PH |
1589 | |
1590 | case vtype_reply: /* Get reply address */ | |
c8ea1597 | 1591 | s = find_header(US"reply-to:", exists_only, newsize, TRUE, |
059ec3d9 | 1592 | headers_charset); |
6979240a | 1593 | if (s != NULL) while (isspace(*s)) s++; |
059ec3d9 | 1594 | if (s == NULL || *s == 0) |
41a13e0a PH |
1595 | { |
1596 | *newsize = 0; /* For the *s==0 case */ | |
c8ea1597 PH |
1597 | s = find_header(US"from:", exists_only, newsize, TRUE, headers_charset); |
1598 | } | |
1599 | if (s != NULL) | |
1600 | { | |
1601 | uschar *t; | |
1602 | while (isspace(*s)) s++; | |
1603 | for (t = s; *t != 0; t++) if (*t == '\n') *t = ' '; | |
6979240a PH |
1604 | while (t > s && isspace(t[-1])) t--; |
1605 | *t = 0; | |
41a13e0a | 1606 | } |
059ec3d9 PH |
1607 | return (s == NULL)? US"" : s; |
1608 | ||
1609 | /* A recipients list is available only during system message filtering, | |
1610 | during ACL processing after DATA, and while expanding pipe commands | |
1611 | generated from a system filter, but not elsewhere. */ | |
1612 | ||
1613 | case vtype_recipients: | |
1614 | if (!enable_dollar_recipients) return NULL; else | |
1615 | { | |
1616 | int size = 128; | |
1617 | int ptr = 0; | |
1618 | int i; | |
1619 | s = store_get(size); | |
1620 | for (i = 0; i < recipients_count; i++) | |
1621 | { | |
1622 | if (i != 0) s = string_cat(s, &size, &ptr, US", ", 2); | |
1623 | s = string_cat(s, &size, &ptr, recipients_list[i].address, | |
1624 | Ustrlen(recipients_list[i].address)); | |
1625 | } | |
1626 | s[ptr] = 0; /* string_cat() leaves room */ | |
1627 | } | |
1628 | return s; | |
8e669ac1 | 1629 | |
5cb8cbc6 PH |
1630 | case vtype_pspace: |
1631 | { | |
1632 | int inodes; | |
8e669ac1 PH |
1633 | sprintf(CS var_buffer, "%d", |
1634 | receive_statvfs(var_table[middle].value == (void *)TRUE, &inodes)); | |
5cb8cbc6 PH |
1635 | } |
1636 | return var_buffer; | |
8e669ac1 | 1637 | |
5cb8cbc6 PH |
1638 | case vtype_pinodes: |
1639 | { | |
1640 | int inodes; | |
8e669ac1 | 1641 | (void) receive_statvfs(var_table[middle].value == (void *)TRUE, &inodes); |
5cb8cbc6 PH |
1642 | sprintf(CS var_buffer, "%d", inodes); |
1643 | } | |
1644 | return var_buffer; | |
80a47a2c | 1645 | |
c8307c12 | 1646 | #ifndef DISABLE_DKIM |
80a47a2c | 1647 | case vtype_dkim: |
da5dfc3a | 1648 | return dkim_exim_expand_query((int)(long)var_table[middle].value); |
80a47a2c TK |
1649 | #endif |
1650 | ||
059ec3d9 PH |
1651 | } |
1652 | } | |
1653 | ||
1654 | return NULL; /* Unknown variable name */ | |
1655 | } | |
1656 | ||
1657 | ||
1658 | ||
1659 | ||
1660 | /************************************************* | |
1661 | * Read and expand substrings * | |
1662 | *************************************************/ | |
1663 | ||
1664 | /* This function is called to read and expand argument substrings for various | |
1665 | expansion items. Some have a minimum requirement that is less than the maximum; | |
1666 | in these cases, the first non-present one is set to NULL. | |
1667 | ||
1668 | Arguments: | |
1669 | sub points to vector of pointers to set | |
1670 | n maximum number of substrings | |
1671 | m minimum required | |
1672 | sptr points to current string pointer | |
1673 | skipping the skipping flag | |
1674 | check_end if TRUE, check for final '}' | |
1675 | name name of item, for error message | |
1676 | ||
1677 | Returns: 0 OK; string pointer updated | |
1678 | 1 curly bracketing error (too few arguments) | |
1679 | 2 too many arguments (only if check_end is set); message set | |
1680 | 3 other error (expansion failure) | |
1681 | */ | |
1682 | ||
1683 | static int | |
1684 | read_subs(uschar **sub, int n, int m, uschar **sptr, BOOL skipping, | |
1685 | BOOL check_end, uschar *name) | |
1686 | { | |
1687 | int i; | |
1688 | uschar *s = *sptr; | |
1689 | ||
1690 | while (isspace(*s)) s++; | |
1691 | for (i = 0; i < n; i++) | |
1692 | { | |
1693 | if (*s != '{') | |
1694 | { | |
1695 | if (i < m) return 1; | |
1696 | sub[i] = NULL; | |
1697 | break; | |
1698 | } | |
1699 | sub[i] = expand_string_internal(s+1, TRUE, &s, skipping); | |
1700 | if (sub[i] == NULL) return 3; | |
1701 | if (*s++ != '}') return 1; | |
1702 | while (isspace(*s)) s++; | |
1703 | } | |
1704 | if (check_end && *s++ != '}') | |
1705 | { | |
1706 | if (s[-1] == '{') | |
1707 | { | |
1708 | expand_string_message = string_sprintf("Too many arguments for \"%s\" " | |
1709 | "(max is %d)", name, n); | |
1710 | return 2; | |
1711 | } | |
1712 | return 1; | |
1713 | } | |
1714 | ||
1715 | *sptr = s; | |
1716 | return 0; | |
1717 | } | |
1718 | ||
1719 | ||
1720 | ||
1721 | ||
641cb756 PH |
1722 | /************************************************* |
1723 | * Elaborate message for bad variable * | |
1724 | *************************************************/ | |
1725 | ||
1726 | /* For the "unknown variable" message, take a look at the variable's name, and | |
1727 | give additional information about possible ACL variables. The extra information | |
1728 | is added on to expand_string_message. | |
1729 | ||
1730 | Argument: the name of the variable | |
1731 | Returns: nothing | |
1732 | */ | |
1733 | ||
1734 | static void | |
1735 | check_variable_error_message(uschar *name) | |
1736 | { | |
1737 | if (Ustrncmp(name, "acl_", 4) == 0) | |
1738 | expand_string_message = string_sprintf("%s (%s)", expand_string_message, | |
1739 | (name[4] == 'c' || name[4] == 'm')? | |
1740 | (isalpha(name[5])? | |
1741 | US"6th character of a user-defined ACL variable must be a digit or underscore" : | |
1742 | US"strict_acl_vars is set" /* Syntax is OK, it has to be this */ | |
1743 | ) : | |
1744 | US"user-defined ACL variables must start acl_c or acl_m"); | |
1745 | } | |
1746 | ||
1747 | ||
1748 | ||
059ec3d9 PH |
1749 | /************************************************* |
1750 | * Read and evaluate a condition * | |
1751 | *************************************************/ | |
1752 | ||
1753 | /* | |
1754 | Arguments: | |
1755 | s points to the start of the condition text | |
1756 | yield points to a BOOL to hold the result of the condition test; | |
1757 | if NULL, we are just reading through a condition that is | |
1758 | part of an "or" combination to check syntax, or in a state | |
1759 | where the answer isn't required | |
1760 | ||
1761 | Returns: a pointer to the first character after the condition, or | |
1762 | NULL after an error | |
1763 | */ | |
1764 | ||
1765 | static uschar * | |
1766 | eval_condition(uschar *s, BOOL *yield) | |
1767 | { | |
1768 | BOOL testfor = TRUE; | |
1769 | BOOL tempcond, combined_cond; | |
1770 | BOOL *subcondptr; | |
1771 | int i, rc, cond_type, roffset; | |
1772 | int num[2]; | |
1773 | struct stat statbuf; | |
1774 | uschar name[256]; | |
1775 | uschar *sub[4]; | |
1776 | ||
1777 | const pcre *re; | |
1778 | const uschar *rerror; | |
1779 | ||
1780 | for (;;) | |
1781 | { | |
1782 | while (isspace(*s)) s++; | |
1783 | if (*s == '!') { testfor = !testfor; s++; } else break; | |
1784 | } | |
1785 | ||
1786 | /* Numeric comparisons are symbolic */ | |
1787 | ||
1788 | if (*s == '=' || *s == '>' || *s == '<') | |
1789 | { | |
1790 | int p = 0; | |
1791 | name[p++] = *s++; | |
1792 | if (*s == '=') | |
1793 | { | |
1794 | name[p++] = '='; | |
1795 | s++; | |
1796 | } | |
1797 | name[p] = 0; | |
1798 | } | |
1799 | ||
1800 | /* All other conditions are named */ | |
1801 | ||
1802 | else s = read_name(name, 256, s, US"_"); | |
1803 | ||
1804 | /* If we haven't read a name, it means some non-alpha character is first. */ | |
1805 | ||
1806 | if (name[0] == 0) | |
1807 | { | |
1808 | expand_string_message = string_sprintf("condition name expected, " | |
1809 | "but found \"%.16s\"", s); | |
1810 | return NULL; | |
1811 | } | |
1812 | ||
1813 | /* Find which condition we are dealing with, and switch on it */ | |
1814 | ||
1815 | cond_type = chop_match(name, cond_table, sizeof(cond_table)/sizeof(uschar *)); | |
1816 | switch(cond_type) | |
1817 | { | |
9b4768fa PH |
1818 | /* def: tests for a non-empty variable, or for the existence of a header. If |
1819 | yield == NULL we are in a skipping state, and don't care about the answer. */ | |
059ec3d9 PH |
1820 | |
1821 | case ECOND_DEF: | |
1822 | if (*s != ':') | |
1823 | { | |
1824 | expand_string_message = US"\":\" expected after \"def\""; | |
1825 | return NULL; | |
1826 | } | |
1827 | ||
1828 | s = read_name(name, 256, s+1, US"_"); | |
1829 | ||
0d85fa3f PH |
1830 | /* Test for a header's existence. If the name contains a closing brace |
1831 | character, this may be a user error where the terminating colon has been | |
1832 | omitted. Set a flag to adjust a subsequent error message in this case. */ | |
059ec3d9 PH |
1833 | |
1834 | if (Ustrncmp(name, "h_", 2) == 0 || | |
1835 | Ustrncmp(name, "rh_", 3) == 0 || | |
1836 | Ustrncmp(name, "bh_", 3) == 0 || | |
1837 | Ustrncmp(name, "header_", 7) == 0 || | |
1838 | Ustrncmp(name, "rheader_", 8) == 0 || | |
1839 | Ustrncmp(name, "bheader_", 8) == 0) | |
1840 | { | |
1841 | s = read_header_name(name, 256, s); | |
0d85fa3f | 1842 | if (Ustrchr(name, '}') != NULL) malformed_header = TRUE; |
059ec3d9 PH |
1843 | if (yield != NULL) *yield = |
1844 | (find_header(name, TRUE, NULL, FALSE, NULL) != NULL) == testfor; | |
1845 | } | |
1846 | ||
9b4768fa PH |
1847 | /* Test for a variable's having a non-empty value. A non-existent variable |
1848 | causes an expansion failure. */ | |
059ec3d9 PH |
1849 | |
1850 | else | |
1851 | { | |
1852 | uschar *value = find_variable(name, TRUE, yield == NULL, NULL); | |
1853 | if (value == NULL) | |
1854 | { | |
1855 | expand_string_message = (name[0] == 0)? | |
1856 | string_sprintf("variable name omitted after \"def:\"") : | |
1857 | string_sprintf("unknown variable \"%s\" after \"def:\"", name); | |
641cb756 | 1858 | check_variable_error_message(name); |
059ec3d9 PH |
1859 | return NULL; |
1860 | } | |
9b4768fa | 1861 | if (yield != NULL) *yield = (value[0] != 0) == testfor; |
059ec3d9 PH |
1862 | } |
1863 | ||
1864 | return s; | |
1865 | ||
1866 | ||
1867 | /* first_delivery tests for first delivery attempt */ | |
1868 | ||
1869 | case ECOND_FIRST_DELIVERY: | |
1870 | if (yield != NULL) *yield = deliver_firsttime == testfor; | |
1871 | return s; | |
1872 | ||
1873 | ||
1874 | /* queue_running tests for any process started by a queue runner */ | |
1875 | ||
1876 | case ECOND_QUEUE_RUNNING: | |
1877 | if (yield != NULL) *yield = (queue_run_pid != (pid_t)0) == testfor; | |
1878 | return s; | |
1879 | ||
1880 | ||
1881 | /* exists: tests for file existence | |
1882 | isip: tests for any IP address | |
1883 | isip4: tests for an IPv4 address | |
1884 | isip6: tests for an IPv6 address | |
1885 | pam: does PAM authentication | |
1886 | radius: does RADIUS authentication | |
1887 | ldapauth: does LDAP authentication | |
1888 | pwcheck: does Cyrus SASL pwcheck authentication | |
1889 | */ | |
1890 | ||
1891 | case ECOND_EXISTS: | |
1892 | case ECOND_ISIP: | |
1893 | case ECOND_ISIP4: | |
1894 | case ECOND_ISIP6: | |
1895 | case ECOND_PAM: | |
1896 | case ECOND_RADIUS: | |
1897 | case ECOND_LDAPAUTH: | |
1898 | case ECOND_PWCHECK: | |
1899 | ||
1900 | while (isspace(*s)) s++; | |
1901 | if (*s != '{') goto COND_FAILED_CURLY_START; | |
1902 | ||
1903 | sub[0] = expand_string_internal(s+1, TRUE, &s, yield == NULL); | |
1904 | if (sub[0] == NULL) return NULL; | |
1905 | if (*s++ != '}') goto COND_FAILED_CURLY_END; | |
1906 | ||
1907 | if (yield == NULL) return s; /* No need to run the test if skipping */ | |
1908 | ||
1909 | switch(cond_type) | |
1910 | { | |
1911 | case ECOND_EXISTS: | |
1912 | if ((expand_forbid & RDO_EXISTS) != 0) | |
1913 | { | |
1914 | expand_string_message = US"File existence tests are not permitted"; | |
1915 | return NULL; | |
1916 | } | |
1917 | *yield = (Ustat(sub[0], &statbuf) == 0) == testfor; | |
1918 | break; | |
1919 | ||
1920 | case ECOND_ISIP: | |
1921 | case ECOND_ISIP4: | |
1922 | case ECOND_ISIP6: | |
1923 | rc = string_is_ip_address(sub[0], NULL); | |
7e66e54d | 1924 | *yield = ((cond_type == ECOND_ISIP)? (rc != 0) : |
059ec3d9 PH |
1925 | (cond_type == ECOND_ISIP4)? (rc == 4) : (rc == 6)) == testfor; |
1926 | break; | |
1927 | ||
1928 | /* Various authentication tests - all optionally compiled */ | |
1929 | ||
1930 | case ECOND_PAM: | |
1931 | #ifdef SUPPORT_PAM | |
1932 | rc = auth_call_pam(sub[0], &expand_string_message); | |
1933 | goto END_AUTH; | |
1934 | #else | |
1935 | goto COND_FAILED_NOT_COMPILED; | |
1936 | #endif /* SUPPORT_PAM */ | |
1937 | ||
1938 | case ECOND_RADIUS: | |
1939 | #ifdef RADIUS_CONFIG_FILE | |
1940 | rc = auth_call_radius(sub[0], &expand_string_message); | |
1941 | goto END_AUTH; | |
1942 | #else | |
1943 | goto COND_FAILED_NOT_COMPILED; | |
1944 | #endif /* RADIUS_CONFIG_FILE */ | |
1945 | ||
1946 | case ECOND_LDAPAUTH: | |
1947 | #ifdef LOOKUP_LDAP | |
1948 | { | |
1949 | /* Just to keep the interface the same */ | |
1950 | BOOL do_cache; | |
1951 | int old_pool = store_pool; | |
1952 | store_pool = POOL_SEARCH; | |
1953 | rc = eldapauth_find((void *)(-1), NULL, sub[0], Ustrlen(sub[0]), NULL, | |
1954 | &expand_string_message, &do_cache); | |
1955 | store_pool = old_pool; | |
1956 | } | |
1957 | goto END_AUTH; | |
1958 | #else | |
1959 | goto COND_FAILED_NOT_COMPILED; | |
1960 | #endif /* LOOKUP_LDAP */ | |
1961 | ||
1962 | case ECOND_PWCHECK: | |
1963 | #ifdef CYRUS_PWCHECK_SOCKET | |
1964 | rc = auth_call_pwcheck(sub[0], &expand_string_message); | |
1965 | goto END_AUTH; | |
1966 | #else | |
1967 | goto COND_FAILED_NOT_COMPILED; | |
1968 | #endif /* CYRUS_PWCHECK_SOCKET */ | |
1969 | ||
1970 | #if defined(SUPPORT_PAM) || defined(RADIUS_CONFIG_FILE) || \ | |
1971 | defined(LOOKUP_LDAP) || defined(CYRUS_PWCHECK_SOCKET) | |
1972 | END_AUTH: | |
1973 | if (rc == ERROR || rc == DEFER) return NULL; | |
1974 | *yield = (rc == OK) == testfor; | |
1975 | #endif | |
1976 | } | |
1977 | return s; | |
1978 | ||
1979 | ||
1980 | /* saslauthd: does Cyrus saslauthd authentication. Four parameters are used: | |
1981 | ||
1982 | ${if saslauthd {{username}{password}{service}{realm}} {yes}[no}} | |
1983 | ||
1984 | However, the last two are optional. That is why the whole set is enclosed | |
1985 | in their own set or braces. */ | |
1986 | ||
1987 | case ECOND_SASLAUTHD: | |
1988 | #ifndef CYRUS_SASLAUTHD_SOCKET | |
1989 | goto COND_FAILED_NOT_COMPILED; | |
1990 | #else | |
1991 | while (isspace(*s)) s++; | |
1992 | if (*s++ != '{') goto COND_FAILED_CURLY_START; | |
1993 | switch(read_subs(sub, 4, 2, &s, yield == NULL, TRUE, US"saslauthd")) | |
1994 | { | |
1995 | case 1: expand_string_message = US"too few arguments or bracketing " | |
1996 | "error for saslauthd"; | |
1997 | case 2: | |
1998 | case 3: return NULL; | |
1999 | } | |
2000 | if (sub[2] == NULL) sub[3] = NULL; /* realm if no service */ | |
2001 | if (yield != NULL) | |
2002 | { | |
2003 | int rc; | |
2004 | rc = auth_call_saslauthd(sub[0], sub[1], sub[2], sub[3], | |
2005 | &expand_string_message); | |
2006 | if (rc == ERROR || rc == DEFER) return NULL; | |
2007 | *yield = (rc == OK) == testfor; | |
2008 | } | |
2009 | return s; | |
2010 | #endif /* CYRUS_SASLAUTHD_SOCKET */ | |
2011 | ||
2012 | ||
2013 | /* symbolic operators for numeric and string comparison, and a number of | |
2014 | other operators, all requiring two arguments. | |
2015 | ||
2016 | match: does a regular expression match and sets up the numerical | |
2017 | variables if it succeeds | |
2018 | match_address: matches in an address list | |
2019 | match_domain: matches in a domain list | |
32d668a5 | 2020 | match_ip: matches a host list that is restricted to IP addresses |
059ec3d9 PH |
2021 | match_local_part: matches in a local part list |
2022 | crypteq: encrypts plaintext and compares against an encrypted text, | |
2023 | using crypt(), crypt16(), MD5 or SHA-1 | |
2024 | */ | |
2025 | ||
2026 | case ECOND_MATCH: | |
2027 | case ECOND_MATCH_ADDRESS: | |
2028 | case ECOND_MATCH_DOMAIN: | |
32d668a5 | 2029 | case ECOND_MATCH_IP: |
059ec3d9 PH |
2030 | case ECOND_MATCH_LOCAL_PART: |
2031 | case ECOND_CRYPTEQ: | |
2032 | ||
2033 | case ECOND_NUM_L: /* Numerical comparisons */ | |
2034 | case ECOND_NUM_LE: | |
2035 | case ECOND_NUM_E: | |
2036 | case ECOND_NUM_EE: | |
2037 | case ECOND_NUM_G: | |
2038 | case ECOND_NUM_GE: | |
2039 | ||
2040 | case ECOND_STR_LT: /* String comparisons */ | |
2041 | case ECOND_STR_LTI: | |
2042 | case ECOND_STR_LE: | |
2043 | case ECOND_STR_LEI: | |
2044 | case ECOND_STR_EQ: | |
2045 | case ECOND_STR_EQI: | |
2046 | case ECOND_STR_GT: | |
2047 | case ECOND_STR_GTI: | |
2048 | case ECOND_STR_GE: | |
2049 | case ECOND_STR_GEI: | |
2050 | ||
2051 | for (i = 0; i < 2; i++) | |
2052 | { | |
2053 | while (isspace(*s)) s++; | |
2054 | if (*s != '{') | |
2055 | { | |
2056 | if (i == 0) goto COND_FAILED_CURLY_START; | |
2057 | expand_string_message = string_sprintf("missing 2nd string in {} " | |
2058 | "after \"%s\"", name); | |
2059 | return NULL; | |
2060 | } | |
2061 | sub[i] = expand_string_internal(s+1, TRUE, &s, yield == NULL); | |
2062 | if (sub[i] == NULL) return NULL; | |
2063 | if (*s++ != '}') goto COND_FAILED_CURLY_END; | |
2064 | ||
2065 | /* Convert to numerical if required; we know that the names of all the | |
2066 | conditions that compare numbers do not start with a letter. This just saves | |
2067 | checking for them individually. */ | |
2068 | ||
d6066548 | 2069 | if (!isalpha(name[0]) && yield != NULL) |
059ec3d9 | 2070 | { |
5dd1517f PH |
2071 | if (sub[i][0] == 0) |
2072 | { | |
2073 | num[i] = 0; | |
2074 | DEBUG(D_expand) | |
2075 | debug_printf("empty string cast to zero for numerical comparison\n"); | |
2076 | } | |
2077 | else | |
2078 | { | |
2079 | num[i] = expand_string_integer(sub[i], FALSE); | |
2080 | if (expand_string_message != NULL) return NULL; | |
2081 | } | |
059ec3d9 PH |
2082 | } |
2083 | } | |
2084 | ||
2085 | /* Result not required */ | |
2086 | ||
2087 | if (yield == NULL) return s; | |
2088 | ||
2089 | /* Do an appropriate comparison */ | |
2090 | ||
2091 | switch(cond_type) | |
2092 | { | |
2093 | case ECOND_NUM_E: | |
2094 | case ECOND_NUM_EE: | |
2095 | *yield = (num[0] == num[1]) == testfor; | |
2096 | break; | |
2097 | ||
2098 | case ECOND_NUM_G: | |
2099 | *yield = (num[0] > num[1]) == testfor; | |
2100 | break; | |
2101 | ||
2102 | case ECOND_NUM_GE: | |
2103 | *yield = (num[0] >= num[1]) == testfor; | |
2104 | break; | |
2105 | ||
2106 | case ECOND_NUM_L: | |
2107 | *yield = (num[0] < num[1]) == testfor; | |
2108 | break; | |
2109 | ||
2110 | case ECOND_NUM_LE: | |
2111 | *yield = (num[0] <= num[1]) == testfor; | |
2112 | break; | |
2113 | ||
2114 | case ECOND_STR_LT: | |
2115 | *yield = (Ustrcmp(sub[0], sub[1]) < 0) == testfor; | |
2116 | break; | |
2117 | ||
2118 | case ECOND_STR_LTI: | |
2119 | *yield = (strcmpic(sub[0], sub[1]) < 0) == testfor; | |
2120 | break; | |
2121 | ||
2122 | case ECOND_STR_LE: | |
2123 | *yield = (Ustrcmp(sub[0], sub[1]) <= 0) == testfor; | |
2124 | break; | |
2125 | ||
2126 | case ECOND_STR_LEI: | |
2127 | *yield = (strcmpic(sub[0], sub[1]) <= 0) == testfor; | |
2128 | break; | |
2129 | ||
2130 | case ECOND_STR_EQ: | |
2131 | *yield = (Ustrcmp(sub[0], sub[1]) == 0) == testfor; | |
2132 | break; | |
2133 | ||
2134 | case ECOND_STR_EQI: | |
2135 | *yield = (strcmpic(sub[0], sub[1]) == 0) == testfor; | |
2136 | break; | |
2137 | ||
2138 | case ECOND_STR_GT: | |
2139 | *yield = (Ustrcmp(sub[0], sub[1]) > 0) == testfor; | |
2140 | break; | |
2141 | ||
2142 | case ECOND_STR_GTI: | |
2143 | *yield = (strcmpic(sub[0], sub[1]) > 0) == testfor; | |
2144 | break; | |
2145 | ||
2146 | case ECOND_STR_GE: | |
2147 | *yield = (Ustrcmp(sub[0], sub[1]) >= 0) == testfor; | |
2148 | break; | |
2149 | ||
2150 | case ECOND_STR_GEI: | |
2151 | *yield = (strcmpic(sub[0], sub[1]) >= 0) == testfor; | |
2152 | break; | |
2153 | ||
2154 | case ECOND_MATCH: /* Regular expression match */ | |
2155 | re = pcre_compile(CS sub[1], PCRE_COPT, (const char **)&rerror, &roffset, | |
2156 | NULL); | |
2157 | if (re == NULL) | |
2158 | { | |
2159 | expand_string_message = string_sprintf("regular expression error in " | |
2160 | "\"%s\": %s at offset %d", sub[1], rerror, roffset); | |
2161 | return NULL; | |
2162 | } | |
2163 | *yield = regex_match_and_setup(re, sub[0], 0, -1) == testfor; | |
2164 | break; | |
2165 | ||
2166 | case ECOND_MATCH_ADDRESS: /* Match in an address list */ | |
2167 | rc = match_address_list(sub[0], TRUE, FALSE, &(sub[1]), NULL, -1, 0, NULL); | |
2168 | goto MATCHED_SOMETHING; | |
2169 | ||
2170 | case ECOND_MATCH_DOMAIN: /* Match in a domain list */ | |
2171 | rc = match_isinlist(sub[0], &(sub[1]), 0, &domainlist_anchor, NULL, | |
2172 | MCL_DOMAIN + MCL_NOEXPAND, TRUE, NULL); | |
2173 | goto MATCHED_SOMETHING; | |
2174 | ||
32d668a5 | 2175 | case ECOND_MATCH_IP: /* Match IP address in a host list */ |
7e66e54d | 2176 | if (sub[0][0] != 0 && string_is_ip_address(sub[0], NULL) == 0) |
32d668a5 PH |
2177 | { |
2178 | expand_string_message = string_sprintf("\"%s\" is not an IP address", | |
2179 | sub[0]); | |
2180 | return NULL; | |
2181 | } | |
2182 | else | |
2183 | { | |
2184 | unsigned int *nullcache = NULL; | |
2185 | check_host_block cb; | |
2186 | ||
2187 | cb.host_name = US""; | |
2188 | cb.host_address = sub[0]; | |
2189 | ||
2190 | /* If the host address starts off ::ffff: it is an IPv6 address in | |
2191 | IPv4-compatible mode. Find the IPv4 part for checking against IPv4 | |
2192 | addresses. */ | |
2193 | ||
2194 | cb.host_ipv4 = (Ustrncmp(cb.host_address, "::ffff:", 7) == 0)? | |
2195 | cb.host_address + 7 : cb.host_address; | |
2196 | ||
2197 | rc = match_check_list( | |
2198 | &sub[1], /* the list */ | |
2199 | 0, /* separator character */ | |
2200 | &hostlist_anchor, /* anchor pointer */ | |
2201 | &nullcache, /* cache pointer */ | |
2202 | check_host, /* function for testing */ | |
2203 | &cb, /* argument for function */ | |
2204 | MCL_HOST, /* type of check */ | |
2205 | sub[0], /* text for debugging */ | |
2206 | NULL); /* where to pass back data */ | |
2207 | } | |
2208 | goto MATCHED_SOMETHING; | |
2209 | ||
059ec3d9 PH |
2210 | case ECOND_MATCH_LOCAL_PART: |
2211 | rc = match_isinlist(sub[0], &(sub[1]), 0, &localpartlist_anchor, NULL, | |
2212 | MCL_LOCALPART + MCL_NOEXPAND, TRUE, NULL); | |
2213 | /* Fall through */ | |
9a26b6b2 | 2214 | /* VVVVVVVVVVVV */ |
059ec3d9 PH |
2215 | MATCHED_SOMETHING: |
2216 | switch(rc) | |
2217 | { | |
2218 | case OK: | |
2219 | *yield = testfor; | |
2220 | break; | |
2221 | ||
2222 | case FAIL: | |
2223 | *yield = !testfor; | |
2224 | break; | |
2225 | ||
2226 | case DEFER: | |
2227 | expand_string_message = string_sprintf("unable to complete match " | |
2228 | "against \"%s\": %s", sub[1], search_error_message); | |
2229 | return NULL; | |
2230 | } | |
2231 | ||
2232 | break; | |
2233 | ||
2234 | /* Various "encrypted" comparisons. If the second string starts with | |
2235 | "{" then an encryption type is given. Default to crypt() or crypt16() | |
2236 | (build-time choice). */ | |
2237 | ||
2238 | case ECOND_CRYPTEQ: | |
2239 | #ifndef SUPPORT_CRYPTEQ | |
2240 | goto COND_FAILED_NOT_COMPILED; | |
2241 | #else | |
2242 | if (strncmpic(sub[1], US"{md5}", 5) == 0) | |
2243 | { | |
2244 | int sublen = Ustrlen(sub[1]+5); | |
2245 | md5 base; | |
2246 | uschar digest[16]; | |
2247 | ||
2248 | md5_start(&base); | |
2249 | md5_end(&base, (uschar *)sub[0], Ustrlen(sub[0]), digest); | |
2250 | ||
2251 | /* If the length that we are comparing against is 24, the MD5 digest | |
2252 | is expressed as a base64 string. This is the way LDAP does it. However, | |
2253 | some other software uses a straightforward hex representation. We assume | |
2254 | this if the length is 32. Other lengths fail. */ | |
2255 | ||
2256 | if (sublen == 24) | |
2257 | { | |
2258 | uschar *coded = auth_b64encode((uschar *)digest, 16); | |
2259 | DEBUG(D_auth) debug_printf("crypteq: using MD5+B64 hashing\n" | |
2260 | " subject=%s\n crypted=%s\n", coded, sub[1]+5); | |
2261 | *yield = (Ustrcmp(coded, sub[1]+5) == 0) == testfor; | |
2262 | } | |
2263 | else if (sublen == 32) | |
2264 | { | |
2265 | int i; | |
2266 | uschar coded[36]; | |
2267 | for (i = 0; i < 16; i++) sprintf(CS (coded+2*i), "%02X", digest[i]); | |
2268 | coded[32] = 0; | |
2269 | DEBUG(D_auth) debug_printf("crypteq: using MD5+hex hashing\n" | |
2270 | " subject=%s\n crypted=%s\n", coded, sub[1]+5); | |
2271 | *yield = (strcmpic(coded, sub[1]+5) == 0) == testfor; | |
2272 | } | |
2273 | else | |
2274 | { | |
2275 | DEBUG(D_auth) debug_printf("crypteq: length for MD5 not 24 or 32: " | |
2276 | "fail\n crypted=%s\n", sub[1]+5); | |
2277 | *yield = !testfor; | |
2278 | } | |
2279 | } | |
2280 | ||
2281 | else if (strncmpic(sub[1], US"{sha1}", 6) == 0) | |
2282 | { | |
2283 | int sublen = Ustrlen(sub[1]+6); | |
2284 | sha1 base; | |
2285 | uschar digest[20]; | |
2286 | ||
2287 | sha1_start(&base); | |
2288 | sha1_end(&base, (uschar *)sub[0], Ustrlen(sub[0]), digest); | |
2289 | ||
2290 | /* If the length that we are comparing against is 28, assume the SHA1 | |
2291 | digest is expressed as a base64 string. If the length is 40, assume a | |
2292 | straightforward hex representation. Other lengths fail. */ | |
2293 | ||
2294 | if (sublen == 28) | |
2295 | { | |
2296 | uschar *coded = auth_b64encode((uschar *)digest, 20); | |
2297 | DEBUG(D_auth) debug_printf("crypteq: using SHA1+B64 hashing\n" | |
2298 | " subject=%s\n crypted=%s\n", coded, sub[1]+6); | |
2299 | *yield = (Ustrcmp(coded, sub[1]+6) == 0) == testfor; | |
2300 | } | |
2301 | else if (sublen == 40) | |
2302 | { | |
2303 | int i; | |
2304 | uschar coded[44]; | |
2305 | for (i = 0; i < 20; i++) sprintf(CS (coded+2*i), "%02X", digest[i]); | |
2306 | coded[40] = 0; | |
2307 | DEBUG(D_auth) debug_printf("crypteq: using SHA1+hex hashing\n" | |
2308 | " subject=%s\n crypted=%s\n", coded, sub[1]+6); | |
2309 | *yield = (strcmpic(coded, sub[1]+6) == 0) == testfor; | |
2310 | } | |
2311 | else | |
2312 | { | |
2313 | DEBUG(D_auth) debug_printf("crypteq: length for SHA-1 not 28 or 40: " | |
2314 | "fail\n crypted=%s\n", sub[1]+6); | |
2315 | *yield = !testfor; | |
2316 | } | |
2317 | } | |
2318 | ||
2319 | else /* {crypt} or {crypt16} and non-{ at start */ | |
2320 | { | |
2321 | int which = 0; | |
2322 | uschar *coded; | |
2323 | ||
2324 | if (strncmpic(sub[1], US"{crypt}", 7) == 0) | |
2325 | { | |
2326 | sub[1] += 7; | |
2327 | which = 1; | |
2328 | } | |
2329 | else if (strncmpic(sub[1], US"{crypt16}", 9) == 0) | |
2330 | { | |
2331 | sub[1] += 9; | |
2332 | which = 2; | |
2333 | } | |
2334 | else if (sub[1][0] == '{') | |
2335 | { | |
2336 | expand_string_message = string_sprintf("unknown encryption mechanism " | |
2337 | "in \"%s\"", sub[1]); | |
2338 | return NULL; | |
2339 | } | |
2340 | ||
2341 | switch(which) | |
2342 | { | |
2343 | case 0: coded = US DEFAULT_CRYPT(CS sub[0], CS sub[1]); break; | |
2344 | case 1: coded = US crypt(CS sub[0], CS sub[1]); break; | |
2345 | default: coded = US crypt16(CS sub[0], CS sub[1]); break; | |
2346 | } | |
2347 | ||
2348 | #define STR(s) # s | |
2349 | #define XSTR(s) STR(s) | |
2350 | DEBUG(D_auth) debug_printf("crypteq: using %s()\n" | |
2351 | " subject=%s\n crypted=%s\n", | |
2352 | (which == 0)? XSTR(DEFAULT_CRYPT) : (which == 1)? "crypt" : "crypt16", | |
2353 | coded, sub[1]); | |
2354 | #undef STR | |
2355 | #undef XSTR | |
2356 | ||
2357 | /* If the encrypted string contains fewer than two characters (for the | |
2358 | salt), force failure. Otherwise we get false positives: with an empty | |
2359 | string the yield of crypt() is an empty string! */ | |
2360 | ||
2361 | *yield = (Ustrlen(sub[1]) < 2)? !testfor : | |
2362 | (Ustrcmp(coded, sub[1]) == 0) == testfor; | |
2363 | } | |
2364 | break; | |
2365 | #endif /* SUPPORT_CRYPTEQ */ | |
2366 | } /* Switch for comparison conditions */ | |
2367 | ||
2368 | return s; /* End of comparison conditions */ | |
2369 | ||
2370 | ||
2371 | /* and/or: computes logical and/or of several conditions */ | |
2372 | ||
2373 | case ECOND_AND: | |
2374 | case ECOND_OR: | |
2375 | subcondptr = (yield == NULL)? NULL : &tempcond; | |
2376 | combined_cond = (cond_type == ECOND_AND); | |
2377 | ||
2378 | while (isspace(*s)) s++; | |
2379 | if (*s++ != '{') goto COND_FAILED_CURLY_START; | |
2380 | ||
2381 | for (;;) | |
2382 | { | |
2383 | while (isspace(*s)) s++; | |
2384 | if (*s == '}') break; | |
2385 | if (*s != '{') | |
2386 | { | |
2387 | expand_string_message = string_sprintf("each subcondition " | |
2388 | "inside an \"%s{...}\" condition must be in its own {}", name); | |
2389 | return NULL; | |
2390 | } | |
2391 | ||
2392 | s = eval_condition(s+1, subcondptr); | |
2393 | if (s == NULL) | |
2394 | { | |
2395 | expand_string_message = string_sprintf("%s inside \"%s{...}\" condition", | |
2396 | expand_string_message, name); | |
2397 | return NULL; | |
2398 | } | |
2399 | while (isspace(*s)) s++; | |
2400 | ||
2401 | if (*s++ != '}') | |
2402 | { | |
2403 | expand_string_message = string_sprintf("missing } at end of condition " | |
2404 | "inside \"%s\" group", name); | |
2405 | return NULL; | |
2406 | } | |
2407 | ||
2408 | if (yield != NULL) | |
2409 | { | |
2410 | if (cond_type == ECOND_AND) | |
2411 | { | |
2412 | combined_cond &= tempcond; | |
2413 | if (!combined_cond) subcondptr = NULL; /* once false, don't */ | |
2414 | } /* evaluate any more */ | |
2415 | else | |
2416 | { | |
2417 | combined_cond |= tempcond; | |
2418 | if (combined_cond) subcondptr = NULL; /* once true, don't */ | |
2419 | } /* evaluate any more */ | |
2420 | } | |
2421 | } | |
2422 | ||
2423 | if (yield != NULL) *yield = (combined_cond == testfor); | |
2424 | return ++s; | |
2425 | ||
2426 | ||
0ce9abe6 PH |
2427 | /* forall/forany: iterates a condition with different values */ |
2428 | ||
2429 | case ECOND_FORALL: | |
2430 | case ECOND_FORANY: | |
2431 | { | |
2432 | int sep = 0; | |
282b357d | 2433 | uschar *save_iterate_item = iterate_item; |
0ce9abe6 PH |
2434 | |
2435 | while (isspace(*s)) s++; | |
2436 | if (*s++ != '{') goto COND_FAILED_CURLY_START; | |
0ce9abe6 PH |
2437 | sub[0] = expand_string_internal(s, TRUE, &s, (yield == NULL)); |
2438 | if (sub[0] == NULL) return NULL; | |
2439 | if (*s++ != '}') goto COND_FAILED_CURLY_END; | |
2440 | ||
2441 | while (isspace(*s)) s++; | |
2442 | if (*s++ != '{') goto COND_FAILED_CURLY_START; | |
2443 | ||
2444 | sub[1] = s; | |
2445 | ||
2446 | /* Call eval_condition once, with result discarded (as if scanning a | |
2447 | "false" part). This allows us to find the end of the condition, because if | |
2448 | the list it empty, we won't actually evaluate the condition for real. */ | |
2449 | ||
2450 | s = eval_condition(sub[1], NULL); | |
2451 | if (s == NULL) | |
2452 | { | |
2453 | expand_string_message = string_sprintf("%s inside \"%s\" condition", | |
2454 | expand_string_message, name); | |
2455 | return NULL; | |
2456 | } | |
2457 | while (isspace(*s)) s++; | |
2458 | ||
2459 | if (*s++ != '}') | |
2460 | { | |
2461 | expand_string_message = string_sprintf("missing } at end of condition " | |
2462 | "inside \"%s\"", name); | |
2463 | return NULL; | |
2464 | } | |
2465 | ||
2466 | if (yield != NULL) *yield = !testfor; | |
2467 | while ((iterate_item = string_nextinlist(&sub[0], &sep, NULL, 0)) != NULL) | |
2468 | { | |
2469 | DEBUG(D_expand) debug_printf("%s: $item = \"%s\"\n", name, iterate_item); | |
2470 | if (eval_condition(sub[1], &tempcond) == NULL) | |
2471 | { | |
2472 | expand_string_message = string_sprintf("%s inside \"%s\" condition", | |
2473 | expand_string_message, name); | |
e58c13cc | 2474 | iterate_item = save_iterate_item; |
0ce9abe6 PH |
2475 | return NULL; |
2476 | } | |
2477 | DEBUG(D_expand) debug_printf("%s: condition evaluated to %s\n", name, | |
2478 | tempcond? "true":"false"); | |
2479 | ||
2480 | if (yield != NULL) *yield = (tempcond == testfor); | |
2481 | if (tempcond == (cond_type == ECOND_FORANY)) break; | |
2482 | } | |
2483 | ||
282b357d | 2484 | iterate_item = save_iterate_item; |
0ce9abe6 PH |
2485 | return s; |
2486 | } | |
2487 | ||
2488 | ||
f3766eb5 NM |
2489 | /* The bool{} expansion condition maps a string to boolean. |
2490 | The values supported should match those supported by the ACL condition | |
2491 | (acl.c, ACLC_CONDITION) so that we keep to a minimum the different ideas | |
2492 | of true/false. Note that Router "condition" rules have a different | |
2493 | interpretation, where general data can be used and only a few values | |
2494 | map to FALSE. | |
2495 | Note that readconf.c boolean matching, for boolean configuration options, | |
6a8de854 PP |
2496 | only matches true/yes/false/no. |
2497 | The bool_lax{} condition matches the Router logic, which is much more | |
2498 | liberal. */ | |
f3766eb5 | 2499 | case ECOND_BOOL: |
6a8de854 | 2500 | case ECOND_BOOL_LAX: |
f3766eb5 NM |
2501 | { |
2502 | uschar *sub_arg[1]; | |
71265ae9 | 2503 | uschar *t, *t2; |
6a8de854 | 2504 | uschar *ourname; |
f3766eb5 NM |
2505 | size_t len; |
2506 | BOOL boolvalue = FALSE; | |
2507 | while (isspace(*s)) s++; | |
2508 | if (*s != '{') goto COND_FAILED_CURLY_START; | |
6a8de854 PP |
2509 | ourname = cond_type == ECOND_BOOL_LAX ? US"bool_lax" : US"bool"; |
2510 | switch(read_subs(sub_arg, 1, 1, &s, yield == NULL, FALSE, ourname)) | |
f3766eb5 | 2511 | { |
6a8de854 PP |
2512 | case 1: expand_string_message = string_sprintf( |
2513 | "too few arguments or bracketing error for %s", | |
2514 | ourname); | |
f3766eb5 NM |
2515 | /*FALLTHROUGH*/ |
2516 | case 2: | |
2517 | case 3: return NULL; | |
2518 | } | |
2519 | t = sub_arg[0]; | |
2520 | while (isspace(*t)) t++; | |
2521 | len = Ustrlen(t); | |
71265ae9 PP |
2522 | if (len) |
2523 | { | |
2524 | /* trailing whitespace: seems like a good idea to ignore it too */ | |
2525 | t2 = t + len - 1; | |
2526 | while (isspace(*t2)) t2--; | |
2527 | if (t2 != (t + len)) | |
2528 | { | |
2529 | *++t2 = '\0'; | |
2530 | len = t2 - t; | |
2531 | } | |
2532 | } | |
f3766eb5 | 2533 | DEBUG(D_expand) |
6a8de854 PP |
2534 | debug_printf("considering %s: %s\n", ourname, len ? t : US"<empty>"); |
2535 | /* logic for the lax case from expand_check_condition(), which also does | |
2536 | expands, and the logic is both short and stable enough that there should | |
2537 | be no maintenance burden from replicating it. */ | |
f3766eb5 NM |
2538 | if (len == 0) |
2539 | boolvalue = FALSE; | |
2540 | else if (Ustrspn(t, "0123456789") == len) | |
6a8de854 | 2541 | { |
f3766eb5 | 2542 | boolvalue = (Uatoi(t) == 0) ? FALSE : TRUE; |
6a8de854 PP |
2543 | /* expand_check_condition only does a literal string "0" check */ |
2544 | if ((cond_type == ECOND_BOOL_LAX) && (len > 1)) | |
2545 | boolvalue = TRUE; | |
2546 | } | |
f3766eb5 NM |
2547 | else if (strcmpic(t, US"true") == 0 || strcmpic(t, US"yes") == 0) |
2548 | boolvalue = TRUE; | |
2549 | else if (strcmpic(t, US"false") == 0 || strcmpic(t, US"no") == 0) | |
2550 | boolvalue = FALSE; | |
6a8de854 PP |
2551 | else if (cond_type == ECOND_BOOL_LAX) |
2552 | boolvalue = TRUE; | |
f3766eb5 NM |
2553 | else |
2554 | { | |
2555 | expand_string_message = string_sprintf("unrecognised boolean " | |
2556 | "value \"%s\"", t); | |
2557 | return NULL; | |
2558 | } | |
5ee6f336 | 2559 | if (yield != NULL) *yield = (boolvalue == testfor); |
f3766eb5 NM |
2560 | return s; |
2561 | } | |
2562 | ||
059ec3d9 PH |
2563 | /* Unknown condition */ |
2564 | ||
2565 | default: | |
2566 | expand_string_message = string_sprintf("unknown condition \"%s\"", name); | |
2567 | return NULL; | |
2568 | } /* End switch on condition type */ | |
2569 | ||
2570 | /* Missing braces at start and end of data */ | |
2571 | ||
2572 | COND_FAILED_CURLY_START: | |
2573 | expand_string_message = string_sprintf("missing { after \"%s\"", name); | |
2574 | return NULL; | |
2575 | ||
2576 | COND_FAILED_CURLY_END: | |
2577 | expand_string_message = string_sprintf("missing } at end of \"%s\" condition", | |
2578 | name); | |
2579 | return NULL; | |
2580 | ||
2581 | /* A condition requires code that is not compiled */ | |
2582 | ||
2583 | #if !defined(SUPPORT_PAM) || !defined(RADIUS_CONFIG_FILE) || \ | |
2584 | !defined(LOOKUP_LDAP) || !defined(CYRUS_PWCHECK_SOCKET) || \ | |
2585 | !defined(SUPPORT_CRYPTEQ) || !defined(CYRUS_SASLAUTHD_SOCKET) | |
2586 | COND_FAILED_NOT_COMPILED: | |
2587 | expand_string_message = string_sprintf("support for \"%s\" not compiled", | |
2588 | name); | |
2589 | return NULL; | |
2590 | #endif | |
2591 | } | |
2592 | ||
2593 | ||
2594 | ||
2595 | ||
2596 | /************************************************* | |
2597 | * Save numerical variables * | |
2598 | *************************************************/ | |
2599 | ||
2600 | /* This function is called from items such as "if" that want to preserve and | |
2601 | restore the numbered variables. | |
2602 | ||
2603 | Arguments: | |
2604 | save_expand_string points to an array of pointers to set | |
2605 | save_expand_nlength points to an array of ints for the lengths | |
2606 | ||
2607 | Returns: the value of expand max to save | |
2608 | */ | |
2609 | ||
2610 | static int | |
2611 | save_expand_strings(uschar **save_expand_nstring, int *save_expand_nlength) | |
2612 | { | |
2613 | int i; | |
2614 | for (i = 0; i <= expand_nmax; i++) | |
2615 | { | |
2616 | save_expand_nstring[i] = expand_nstring[i]; | |
2617 | save_expand_nlength[i] = expand_nlength[i]; | |
2618 | } | |
2619 | return expand_nmax; | |
2620 | } | |
2621 | ||
2622 | ||
2623 | ||
2624 | /************************************************* | |
2625 | * Restore numerical variables * | |
2626 | *************************************************/ | |
2627 | ||
2628 | /* This function restored saved values of numerical strings. | |
2629 | ||
2630 | Arguments: | |
2631 | save_expand_nmax the number of strings to restore | |
2632 | save_expand_string points to an array of pointers | |
2633 | save_expand_nlength points to an array of ints | |
2634 | ||
2635 | Returns: nothing | |
2636 | */ | |
2637 | ||
2638 | static void | |
2639 | restore_expand_strings(int save_expand_nmax, uschar **save_expand_nstring, | |
2640 | int *save_expand_nlength) | |
2641 | { | |
2642 | int i; | |
2643 | expand_nmax = save_expand_nmax; | |
2644 | for (i = 0; i <= expand_nmax; i++) | |
2645 | { | |
2646 | expand_nstring[i] = save_expand_nstring[i]; | |
2647 | expand_nlength[i] = save_expand_nlength[i]; | |
2648 | } | |
2649 | } | |
2650 | ||
2651 | ||
2652 | ||
2653 | ||
2654 | ||
2655 | /************************************************* | |
2656 | * Handle yes/no substrings * | |
2657 | *************************************************/ | |
2658 | ||
2659 | /* This function is used by ${if}, ${lookup} and ${extract} to handle the | |
2660 | alternative substrings that depend on whether or not the condition was true, | |
2661 | or the lookup or extraction succeeded. The substrings always have to be | |
2662 | expanded, to check their syntax, but "skipping" is set when the result is not | |
2663 | needed - this avoids unnecessary nested lookups. | |
2664 | ||
2665 | Arguments: | |
2666 | skipping TRUE if we were skipping when this item was reached | |
2667 | yes TRUE if the first string is to be used, else use the second | |
2668 | save_lookup a value to put back into lookup_value before the 2nd expansion | |
2669 | sptr points to the input string pointer | |
2670 | yieldptr points to the output string pointer | |
2671 | sizeptr points to the output string size | |
2672 | ptrptr points to the output string pointer | |
2673 | type "lookup" or "if" or "extract" or "run", for error message | |
2674 | ||
2675 | Returns: 0 OK; lookup_value has been reset to save_lookup | |
2676 | 1 expansion failed | |
2677 | 2 expansion failed because of bracketing error | |
2678 | */ | |
2679 | ||
2680 | static int | |
2681 | process_yesno(BOOL skipping, BOOL yes, uschar *save_lookup, uschar **sptr, | |
2682 | uschar **yieldptr, int *sizeptr, int *ptrptr, uschar *type) | |
2683 | { | |
2684 | int rc = 0; | |
2685 | uschar *s = *sptr; /* Local value */ | |
2686 | uschar *sub1, *sub2; | |
2687 | ||
2688 | /* If there are no following strings, we substitute the contents of $value for | |
063b1e99 | 2689 | lookups and for extractions in the success case. For the ${if item, the string |
8e669ac1 | 2690 | "true" is substituted. In the fail case, nothing is substituted for all three |
063b1e99 | 2691 | items. */ |
059ec3d9 PH |
2692 | |
2693 | while (isspace(*s)) s++; | |
2694 | if (*s == '}') | |
2695 | { | |
063b1e99 PH |
2696 | if (type[0] == 'i') |
2697 | { | |
8e669ac1 | 2698 | if (yes) *yieldptr = string_cat(*yieldptr, sizeptr, ptrptr, US"true", 4); |
063b1e99 PH |
2699 | } |
2700 | else | |
8e669ac1 | 2701 | { |
063b1e99 PH |
2702 | if (yes && lookup_value != NULL) |
2703 | *yieldptr = string_cat(*yieldptr, sizeptr, ptrptr, lookup_value, | |
2704 | Ustrlen(lookup_value)); | |
2705 | lookup_value = save_lookup; | |
2706 | } | |
059ec3d9 PH |
2707 | s++; |
2708 | goto RETURN; | |
2709 | } | |
2710 | ||
9b4768fa PH |
2711 | /* The first following string must be braced. */ |
2712 | ||
2713 | if (*s++ != '{') goto FAILED_CURLY; | |
2714 | ||
059ec3d9 PH |
2715 | /* Expand the first substring. Forced failures are noticed only if we actually |
2716 | want this string. Set skipping in the call in the fail case (this will always | |
2717 | be the case if we were already skipping). */ | |
2718 | ||
9b4768fa | 2719 | sub1 = expand_string_internal(s, TRUE, &s, !yes); |
059ec3d9 PH |
2720 | if (sub1 == NULL && (yes || !expand_string_forcedfail)) goto FAILED; |
2721 | expand_string_forcedfail = FALSE; | |
2722 | if (*s++ != '}') goto FAILED_CURLY; | |
2723 | ||
2724 | /* If we want the first string, add it to the output */ | |
2725 | ||
2726 | if (yes) | |
2727 | *yieldptr = string_cat(*yieldptr, sizeptr, ptrptr, sub1, Ustrlen(sub1)); | |
2728 | ||
2729 | /* If this is called from a lookup or an extract, we want to restore $value to | |
2730 | what it was at the start of the item, so that it has this value during the | |
d20976dc PH |
2731 | second string expansion. For the call from "if" or "run" to this function, |
2732 | save_lookup is set to lookup_value, so that this statement does nothing. */ | |
059ec3d9 PH |
2733 | |
2734 | lookup_value = save_lookup; | |
2735 | ||
2736 | /* There now follows either another substring, or "fail", or nothing. This | |
2737 | time, forced failures are noticed only if we want the second string. We must | |
2738 | set skipping in the nested call if we don't want this string, or if we were | |
2739 | already skipping. */ | |
2740 | ||
2741 | while (isspace(*s)) s++; | |
2742 | if (*s == '{') | |
2743 | { | |
2744 | sub2 = expand_string_internal(s+1, TRUE, &s, yes || skipping); | |
2745 | if (sub2 == NULL && (!yes || !expand_string_forcedfail)) goto FAILED; | |
2746 | expand_string_forcedfail = FALSE; | |
2747 | if (*s++ != '}') goto FAILED_CURLY; | |
2748 | ||
2749 | /* If we want the second string, add it to the output */ | |
2750 | ||
2751 | if (!yes) | |
2752 | *yieldptr = string_cat(*yieldptr, sizeptr, ptrptr, sub2, Ustrlen(sub2)); | |
2753 | } | |
2754 | ||
2755 | /* If there is no second string, but the word "fail" is present when the use of | |
2756 | the second string is wanted, set a flag indicating it was a forced failure | |
2757 | rather than a syntactic error. Swallow the terminating } in case this is nested | |
2758 | inside another lookup or if or extract. */ | |
2759 | ||
2760 | else if (*s != '}') | |
2761 | { | |
2762 | uschar name[256]; | |
2763 | s = read_name(name, sizeof(name), s, US"_"); | |
2764 | if (Ustrcmp(name, "fail") == 0) | |
2765 | { | |
2766 | if (!yes && !skipping) | |
2767 | { | |
2768 | while (isspace(*s)) s++; | |
2769 | if (*s++ != '}') goto FAILED_CURLY; | |
2770 | expand_string_message = | |
2771 | string_sprintf("\"%s\" failed and \"fail\" requested", type); | |
2772 | expand_string_forcedfail = TRUE; | |
2773 | goto FAILED; | |
2774 | } | |
2775 | } | |
2776 | else | |
2777 | { | |
2778 | expand_string_message = | |
2779 | string_sprintf("syntax error in \"%s\" item - \"fail\" expected", type); | |
2780 | goto FAILED; | |
2781 | } | |
2782 | } | |
2783 | ||
2784 | /* All we have to do now is to check on the final closing brace. */ | |
2785 | ||
2786 | while (isspace(*s)) s++; | |
2787 | if (*s++ == '}') goto RETURN; | |
2788 | ||
2789 | /* Get here if there is a bracketing failure */ | |
2790 | ||
2791 | FAILED_CURLY: | |
2792 | rc++; | |
2793 | ||
2794 | /* Get here for other failures */ | |
2795 | ||
2796 | FAILED: | |
2797 | rc++; | |
2798 | ||
2799 | /* Update the input pointer value before returning */ | |
2800 | ||
2801 | RETURN: | |
2802 | *sptr = s; | |
2803 | return rc; | |
2804 | } | |
2805 | ||
2806 | ||
2807 | ||
2808 | ||
059ec3d9 PH |
2809 | /************************************************* |
2810 | * Handle MD5 or SHA-1 computation for HMAC * | |
2811 | *************************************************/ | |
2812 | ||
2813 | /* These are some wrapping functions that enable the HMAC code to be a bit | |
2814 | cleaner. A good compiler will spot the tail recursion. | |
2815 | ||
2816 | Arguments: | |
2817 | type HMAC_MD5 or HMAC_SHA1 | |
2818 | remaining are as for the cryptographic hash functions | |
2819 | ||
2820 | Returns: nothing | |
2821 | */ | |
2822 | ||
2823 | static void | |
2824 | chash_start(int type, void *base) | |
2825 | { | |
2826 | if (type == HMAC_MD5) | |
2827 | md5_start((md5 *)base); | |
2828 | else | |
2829 | sha1_start((sha1 *)base); | |
2830 | } | |
2831 | ||
2832 | static void | |
2833 | chash_mid(int type, void *base, uschar *string) | |
2834 | { | |
2835 | if (type == HMAC_MD5) | |
2836 | md5_mid((md5 *)base, string); | |
2837 | else | |
2838 | sha1_mid((sha1 *)base, string); | |
2839 | } | |
2840 | ||
2841 | static void | |
2842 | chash_end(int type, void *base, uschar *string, int length, uschar *digest) | |
2843 | { | |
2844 | if (type == HMAC_MD5) | |
2845 | md5_end((md5 *)base, string, length, digest); | |
2846 | else | |
2847 | sha1_end((sha1 *)base, string, length, digest); | |
2848 | } | |
2849 | ||
2850 | ||
2851 | ||
2852 | ||
2853 | ||
1549ea3b PH |
2854 | /******************************************************** |
2855 | * prvs: Get last three digits of days since Jan 1, 1970 * | |
2856 | ********************************************************/ | |
2857 | ||
2858 | /* This is needed to implement the "prvs" BATV reverse | |
2859 | path signing scheme | |
2860 | ||
2861 | Argument: integer "days" offset to add or substract to | |
2862 | or from the current number of days. | |
2863 | ||
2864 | Returns: pointer to string containing the last three | |
2865 | digits of the number of days since Jan 1, 1970, | |
2866 | modified by the offset argument, NULL if there | |
2867 | was an error in the conversion. | |
2868 | ||
2869 | */ | |
2870 | ||
2871 | static uschar * | |
2872 | prvs_daystamp(int day_offset) | |
2873 | { | |
a86229cf PH |
2874 | uschar *days = store_get(32); /* Need at least 24 for cases */ |
2875 | (void)string_format(days, 32, TIME_T_FMT, /* where TIME_T_FMT is %lld */ | |
1549ea3b | 2876 | (time(NULL) + day_offset*86400)/86400); |
e169f567 | 2877 | return (Ustrlen(days) >= 3) ? &days[Ustrlen(days)-3] : US"100"; |
1549ea3b PH |
2878 | } |
2879 | ||
2880 | ||
2881 | ||
2882 | /******************************************************** | |
2883 | * prvs: perform HMAC-SHA1 computation of prvs bits * | |
2884 | ********************************************************/ | |
2885 | ||
2886 | /* This is needed to implement the "prvs" BATV reverse | |
2887 | path signing scheme | |
2888 | ||
2889 | Arguments: | |
2890 | address RFC2821 Address to use | |
2891 | key The key to use (must be less than 64 characters | |
2892 | in size) | |
2893 | key_num Single-digit key number to use. Defaults to | |
2894 | '0' when NULL. | |
2895 | ||
2896 | Returns: pointer to string containing the first three | |
2897 | bytes of the final hash in hex format, NULL if | |
2898 | there was an error in the process. | |
2899 | */ | |
2900 | ||
2901 | static uschar * | |
2902 | prvs_hmac_sha1(uschar *address, uschar *key, uschar *key_num, uschar *daystamp) | |
2903 | { | |
2904 | uschar *hash_source, *p; | |
2905 | int size = 0,offset = 0,i; | |
2906 | sha1 sha1_base; | |
2907 | void *use_base = &sha1_base; | |
2908 | uschar innerhash[20]; | |
2909 | uschar finalhash[20]; | |
2910 | uschar innerkey[64]; | |
2911 | uschar outerkey[64]; | |
2912 | uschar *finalhash_hex = store_get(40); | |
2913 | ||
2914 | if (key_num == NULL) | |
2915 | key_num = US"0"; | |
2916 | ||
2917 | if (Ustrlen(key) > 64) | |
2918 | return NULL; | |
2919 | ||
2920 | hash_source = string_cat(NULL,&size,&offset,key_num,1); | |
2921 | string_cat(hash_source,&size,&offset,daystamp,3); | |
2922 | string_cat(hash_source,&size,&offset,address,Ustrlen(address)); | |
2923 | hash_source[offset] = '\0'; | |
2924 | ||
2925 | DEBUG(D_expand) debug_printf("prvs: hash source is '%s'\n", hash_source); | |
2926 | ||
2927 | memset(innerkey, 0x36, 64); | |
2928 | memset(outerkey, 0x5c, 64); | |
2929 | ||
2930 | for (i = 0; i < Ustrlen(key); i++) | |
2931 | { | |
2932 | innerkey[i] ^= key[i]; | |
2933 | outerkey[i] ^= key[i]; | |
2934 | } | |
2935 | ||
2936 | chash_start(HMAC_SHA1, use_base); | |
2937 | chash_mid(HMAC_SHA1, use_base, innerkey); | |
2938 | chash_end(HMAC_SHA1, use_base, hash_source, offset, innerhash); | |
2939 | ||
2940 | chash_start(HMAC_SHA1, use_base); | |
2941 | chash_mid(HMAC_SHA1, use_base, outerkey); | |
2942 | chash_end(HMAC_SHA1, use_base, innerhash, 20, finalhash); | |
2943 | ||
2944 | p = finalhash_hex; | |
2945 | for (i = 0; i < 3; i++) | |
2946 | { | |
2947 | *p++ = hex_digits[(finalhash[i] & 0xf0) >> 4]; | |
2948 | *p++ = hex_digits[finalhash[i] & 0x0f]; | |
2949 | } | |
2950 | *p = '\0'; | |
2951 | ||
2952 | return finalhash_hex; | |
2953 | } | |
2954 | ||
2955 | ||
2956 | ||
2957 | ||
059ec3d9 PH |
2958 | /************************************************* |
2959 | * Join a file onto the output string * | |
2960 | *************************************************/ | |
2961 | ||
2962 | /* This is used for readfile and after a run expansion. It joins the contents | |
2963 | of a file onto the output string, globally replacing newlines with a given | |
2964 | string (optionally). The file is closed at the end. | |
2965 | ||
2966 | Arguments: | |
2967 | f the FILE | |
2968 | yield pointer to the expandable string | |
2969 | sizep pointer to the current size | |
2970 | ptrp pointer to the current position | |
2971 | eol newline replacement string, or NULL | |
2972 | ||
2973 | Returns: new value of string pointer | |
2974 | */ | |
2975 | ||
2976 | static uschar * | |
2977 | cat_file(FILE *f, uschar *yield, int *sizep, int *ptrp, uschar *eol) | |
2978 | { | |
2979 | int eollen; | |
2980 | uschar buffer[1024]; | |
2981 | ||
2982 | eollen = (eol == NULL)? 0 : Ustrlen(eol); | |
2983 | ||
2984 | while (Ufgets(buffer, sizeof(buffer), f) != NULL) | |
2985 | { | |
2986 | int len = Ustrlen(buffer); | |
2987 | if (eol != NULL && buffer[len-1] == '\n') len--; | |
2988 | yield = string_cat(yield, sizep, ptrp, buffer, len); | |
2989 | if (buffer[len] != 0) | |
2990 | yield = string_cat(yield, sizep, ptrp, eol, eollen); | |
2991 | } | |
2992 | ||
2993 | if (yield != NULL) yield[*ptrp] = 0; | |
2994 | ||
2995 | return yield; | |
2996 | } | |
2997 | ||
2998 | ||
2999 | ||
3000 | ||
3001 | /************************************************* | |
3002 | * Evaluate numeric expression * | |
3003 | *************************************************/ | |
3004 | ||
af561417 PH |
3005 | /* This is a set of mutually recursive functions that evaluate an arithmetic |
3006 | expression involving + - * / % & | ^ ~ << >> and parentheses. The only one of | |
3007 | these functions that is called from elsewhere is eval_expr, whose interface is: | |
059ec3d9 PH |
3008 | |
3009 | Arguments: | |
af561417 PH |
3010 | sptr pointer to the pointer to the string - gets updated |
3011 | decimal TRUE if numbers are to be assumed decimal | |
3012 | error pointer to where to put an error message - must be NULL on input | |
3013 | endket TRUE if ')' must terminate - FALSE for external call | |
059ec3d9 | 3014 | |
af561417 PH |
3015 | Returns: on success: the value of the expression, with *error still NULL |
3016 | on failure: an undefined value, with *error = a message | |
059ec3d9 PH |
3017 | */ |
3018 | ||
af561417 PH |
3019 | static int eval_op_or(uschar **, BOOL, uschar **); |
3020 | ||
059ec3d9 PH |
3021 | |
3022 | static int | |
3023 | eval_expr(uschar **sptr, BOOL decimal, uschar **error, BOOL endket) | |
3024 | { | |
3025 | uschar *s = *sptr; | |
af561417 | 3026 | int x = eval_op_or(&s, decimal, error); |
059ec3d9 PH |
3027 | if (*error == NULL) |
3028 | { | |
af561417 | 3029 | if (endket) |
059ec3d9 | 3030 | { |
af561417 PH |
3031 | if (*s != ')') |
3032 | *error = US"expecting closing parenthesis"; | |
3033 | else | |
3034 | while (isspace(*(++s))); | |
059ec3d9 | 3035 | } |
af561417 | 3036 | else if (*s != 0) *error = US"expecting operator"; |
059ec3d9 | 3037 | } |
059ec3d9 PH |
3038 | *sptr = s; |
3039 | return x; | |
3040 | } | |
3041 | ||
af561417 | 3042 | |
059ec3d9 | 3043 | static int |
af561417 | 3044 | eval_number(uschar **sptr, BOOL decimal, uschar **error) |
059ec3d9 PH |
3045 | { |
3046 | register int c; | |
3047 | int n; | |
3048 | uschar *s = *sptr; | |
3049 | while (isspace(*s)) s++; | |
3050 | c = *s; | |
af561417 | 3051 | if (isdigit(c)) |
059ec3d9 PH |
3052 | { |
3053 | int count; | |
3054 | (void)sscanf(CS s, (decimal? "%d%n" : "%i%n"), &n, &count); | |
3055 | s += count; | |
3056 | if (tolower(*s) == 'k') { n *= 1024; s++; } | |
3057 | else if (tolower(*s) == 'm') { n *= 1024*1024; s++; } | |
3058 | while (isspace (*s)) s++; | |
3059 | } | |
3060 | else if (c == '(') | |
3061 | { | |
3062 | s++; | |
3063 | n = eval_expr(&s, decimal, error, 1); | |
3064 | } | |
3065 | else | |
3066 | { | |
3067 | *error = US"expecting number or opening parenthesis"; | |
3068 | n = 0; | |
3069 | } | |
3070 | *sptr = s; | |
3071 | return n; | |
3072 | } | |
3073 | ||
af561417 PH |
3074 | |
3075 | static int eval_op_unary(uschar **sptr, BOOL decimal, uschar **error) | |
3076 | { | |
3077 | uschar *s = *sptr; | |
3078 | int x; | |
3079 | while (isspace(*s)) s++; | |
3080 | if (*s == '+' || *s == '-' || *s == '~') | |
3081 | { | |
3082 | int op = *s++; | |
3083 | x = eval_op_unary(&s, decimal, error); | |
3084 | if (op == '-') x = -x; | |
3085 | else if (op == '~') x = ~x; | |
3086 | } | |
3087 | else | |
3088 | { | |
3089 | x = eval_number(&s, decimal, error); | |
3090 | } | |
3091 | *sptr = s; | |
3092 | return x; | |
3093 | } | |
3094 | ||
3095 | ||
3096 | static int eval_op_mult(uschar **sptr, BOOL decimal, uschar **error) | |
059ec3d9 PH |
3097 | { |
3098 | uschar *s = *sptr; | |
af561417 | 3099 | int x = eval_op_unary(&s, decimal, error); |
059ec3d9 PH |
3100 | if (*error == NULL) |
3101 | { | |
5591031b | 3102 | while (*s == '*' || *s == '/' || *s == '%') |
059ec3d9 PH |
3103 | { |
3104 | int op = *s++; | |
af561417 | 3105 | int y = eval_op_unary(&s, decimal, error); |
059ec3d9 | 3106 | if (*error != NULL) break; |
053a9aa3 PP |
3107 | /* SIGFPE both on div/mod by zero and on INT_MIN / -1, which would give |
3108 | * a value of INT_MAX+1. Note that INT_MIN * -1 gives INT_MIN for me, which | |
3109 | * is a bug somewhere in [gcc 4.2.1, FreeBSD, amd64]. In fact, -N*-M where | |
3110 | * -N*M is INT_MIN will yielf INT_MIN. | |
3111 | * Since we don't support floating point, this is somewhat simpler. | |
3112 | * Ideally, we'd return an error, but since we overflow for all other | |
3113 | * arithmetic, consistency suggests otherwise, but what's the correct value | |
3114 | * to use? There is none. | |
3115 | * The C standard guarantees overflow for unsigned arithmetic but signed | |
3116 | * overflow invokes undefined behaviour; in practice, this is overflow | |
3117 | * except for converting INT_MIN to INT_MAX+1. We also can't guarantee | |
3118 | * that long/longlong larger than int are available, or we could just work | |
3119 | * with larger types. We should consider whether to guarantee 32bit eval | |
3120 | * and 64-bit working variables, with errors returned. For now ... | |
3121 | * So, the only SIGFPEs occur with a non-shrinking div/mod, thus -1; we | |
3122 | * can just let the other invalid results occur otherwise, as they have | |
3123 | * until now. For this one case, we can coerce. | |
3124 | */ | |
3125 | if (y == -1 && x == INT_MIN && op != '*') | |
3126 | { | |
3127 | DEBUG(D_expand) | |
3128 | debug_printf("Integer exception dodging: %d%c-1 coerced to %d\n", | |
3129 | INT_MIN, op, INT_MAX); | |
3130 | x = INT_MAX; | |
3131 | continue; | |
3132 | } | |
a5b52695 PP |
3133 | if (op == '*') |
3134 | x *= y; | |
3135 | else | |
3136 | { | |
3137 | if (y == 0) | |
54e7ce4a | 3138 | { |
a5b52695 PP |
3139 | *error = (op == '/') ? US"divide by zero" : US"modulo by zero"; |
3140 | x = 0; | |
3141 | break; | |
54e7ce4a | 3142 | } |
a5b52695 PP |
3143 | if (op == '/') |
3144 | x /= y; | |
3145 | else | |
3146 | x %= y; | |
3147 | } | |
059ec3d9 PH |
3148 | } |
3149 | } | |
3150 | *sptr = s; | |
3151 | return x; | |
3152 | } | |
3153 | ||
3154 | ||
af561417 PH |
3155 | static int eval_op_sum(uschar **sptr, BOOL decimal, uschar **error) |
3156 | { | |
3157 | uschar *s = *sptr; | |
3158 | int x = eval_op_mult(&s, decimal, error); | |
3159 | if (*error == NULL) | |
3160 | { | |
3161 | while (*s == '+' || *s == '-') | |
3162 | { | |
3163 | int op = *s++; | |
3164 | int y = eval_op_mult(&s, decimal, error); | |
3165 | if (*error != NULL) break; | |
3166 | if (op == '+') x += y; else x -= y; | |
3167 | } | |
3168 | } | |
3169 | *sptr = s; | |
3170 | return x; | |
3171 | } | |
3172 | ||
3173 | ||
3174 | static int eval_op_shift(uschar **sptr, BOOL decimal, uschar **error) | |
3175 | { | |
3176 | uschar *s = *sptr; | |
3177 | int x = eval_op_sum(&s, decimal, error); | |
3178 | if (*error == NULL) | |
3179 | { | |
3180 | while ((*s == '<' || *s == '>') && s[1] == s[0]) | |
3181 | { | |
3182 | int y; | |
3183 | int op = *s++; | |
3184 | s++; | |
3185 | y = eval_op_sum(&s, decimal, error); | |
3186 | if (*error != NULL) break; | |
3187 | if (op == '<') x <<= y; else x >>= y; | |
3188 | } | |
3189 | } | |
3190 | *sptr = s; | |
3191 | return x; | |
3192 | } | |
3193 | ||
3194 | ||
3195 | static int eval_op_and(uschar **sptr, BOOL decimal, uschar **error) | |
3196 | { | |
3197 | uschar *s = *sptr; | |
3198 | int x = eval_op_shift(&s, decimal, error); | |
3199 | if (*error == NULL) | |
3200 | { | |
3201 | while (*s == '&') | |
3202 | { | |
3203 | int y; | |
3204 | s++; | |
3205 | y = eval_op_shift(&s, decimal, error); | |
3206 | if (*error != NULL) break; | |
3207 | x &= y; | |
3208 | } | |
3209 | } | |
3210 | *sptr = s; | |
3211 | return x; | |
3212 | } | |
3213 | ||
3214 | ||
3215 | static int eval_op_xor(uschar **sptr, BOOL decimal, uschar **error) | |
3216 | { | |
3217 | uschar *s = *sptr; | |
3218 | int x = eval_op_and(&s, decimal, error); | |
3219 | if (*error == NULL) | |
3220 | { | |
3221 | while (*s == '^') | |
3222 | { | |
3223 | int y; | |
3224 | s++; | |
3225 | y = eval_op_and(&s, decimal, error); | |
3226 | if (*error != NULL) break; | |
3227 | x ^= y; | |
3228 | } | |
3229 | } | |
3230 | *sptr = s; | |
3231 | return x; | |
3232 | } | |
3233 | ||
3234 | ||
3235 | static int eval_op_or(uschar **sptr, BOOL decimal, uschar **error) | |
3236 | { | |
3237 | uschar *s = *sptr; | |
3238 | int x = eval_op_xor(&s, decimal, error); | |
3239 | if (*error == NULL) | |
3240 | { | |
3241 | while (*s == '|') | |
3242 | { | |
3243 | int y; | |
3244 | s++; | |
3245 | y = eval_op_xor(&s, decimal, error); | |
3246 | if (*error != NULL) break; | |
3247 | x |= y; | |
3248 | } | |
3249 | } | |
3250 | *sptr = s; | |
3251 | return x; | |
3252 | } | |
3253 | ||
059ec3d9 PH |
3254 | |
3255 | ||
3256 | /************************************************* | |
3257 | * Expand string * | |
3258 | *************************************************/ | |
3259 | ||
3260 | /* Returns either an unchanged string, or the expanded string in stacking pool | |
3261 | store. Interpreted sequences are: | |
3262 | ||
3263 | \... normal escaping rules | |
3264 | $name substitutes the variable | |
3265 | ${name} ditto | |
3266 | ${op:string} operates on the expanded string value | |
3267 | ${item{arg1}{arg2}...} expands the args and then does the business | |
3268 | some literal args are not enclosed in {} | |
3269 | ||
3270 | There are now far too many operators and item types to make it worth listing | |
3271 | them here in detail any more. | |
3272 | ||
3273 | We use an internal routine recursively to handle embedded substrings. The | |
3274 | external function follows. The yield is NULL if the expansion failed, and there | |
3275 | are two cases: if something collapsed syntactically, or if "fail" was given | |
3276 | as the action on a lookup failure. These can be distinguised by looking at the | |
3277 | variable expand_string_forcedfail, which is TRUE in the latter case. | |
3278 | ||
3279 | The skipping flag is set true when expanding a substring that isn't actually | |
3280 | going to be used (after "if" or "lookup") and it prevents lookups from | |
3281 | happening lower down. | |
3282 | ||
3283 | Store usage: At start, a store block of the length of the input plus 64 | |
3284 | is obtained. This is expanded as necessary by string_cat(), which might have to | |
3285 | get a new block, or might be able to expand the original. At the end of the | |
3286 | function we can release any store above that portion of the yield block that | |
3287 | was actually used. In many cases this will be optimal. | |
3288 | ||
3289 | However: if the first item in the expansion is a variable name or header name, | |
3290 | we reset the store before processing it; if the result is in fresh store, we | |
3291 | use that without copying. This is helpful for expanding strings like | |
3292 | $message_headers which can get very long. | |
3293 | ||
d6b4d938 TF |
3294 | There's a problem if a ${dlfunc item has side-effects that cause allocation, |
3295 | since resetting the store at the end of the expansion will free store that was | |
3296 | allocated by the plugin code as well as the slop after the expanded string. So | |
3297 | we skip any resets if ${dlfunc has been used. This is an unfortunate | |
3298 | consequence of string expansion becoming too powerful. | |
3299 | ||
059ec3d9 PH |
3300 | Arguments: |
3301 | string the string to be expanded | |
3302 | ket_ends true if expansion is to stop at } | |
3303 | left if not NULL, a pointer to the first character after the | |
3304 | expansion is placed here (typically used with ket_ends) | |
3305 | skipping TRUE for recursive calls when the value isn't actually going | |
3306 | to be used (to allow for optimisation) | |
3307 | ||
3308 | Returns: NULL if expansion fails: | |
3309 | expand_string_forcedfail is set TRUE if failure was forced | |
3310 | expand_string_message contains a textual error message | |
3311 | a pointer to the expanded string on success | |
3312 | */ | |
3313 | ||
3314 | static uschar * | |
3315 | expand_string_internal(uschar *string, BOOL ket_ends, uschar **left, | |
3316 | BOOL skipping) | |
3317 | { | |
3318 | int ptr = 0; | |
3319 | int size = Ustrlen(string)+ 64; | |
3320 | int item_type; | |
3321 | uschar *yield = store_get(size); | |
3322 | uschar *s = string; | |
3323 | uschar *save_expand_nstring[EXPAND_MAXN+1]; | |
3324 | int save_expand_nlength[EXPAND_MAXN+1]; | |
d6b4d938 | 3325 | BOOL resetok = TRUE; |
059ec3d9 PH |
3326 | |
3327 | expand_string_forcedfail = FALSE; | |
3328 | expand_string_message = US""; | |
3329 | ||
3330 | while (*s != 0) | |
3331 | { | |
3332 | uschar *value; | |
3333 | uschar name[256]; | |
3334 | ||
3335 | /* \ escapes the next character, which must exist, or else | |
3336 | the expansion fails. There's a special escape, \N, which causes | |
3337 | copying of the subject verbatim up to the next \N. Otherwise, | |
3338 | the escapes are the standard set. */ | |
3339 | ||
3340 | if (*s == '\\') | |
3341 | { | |
3342 | if (s[1] == 0) | |
3343 | { | |
3344 | expand_string_message = US"\\ at end of string"; | |
3345 | goto EXPAND_FAILED; | |
3346 | } | |
3347 | ||
3348 | if (s[1] == 'N') | |
3349 | { | |
3350 | uschar *t = s + 2; | |
3351 | for (s = t; *s != 0; s++) if (*s == '\\' && s[1] == 'N') break; | |
3352 | yield = string_cat(yield, &size, &ptr, t, s - t); | |
3353 | if (*s != 0) s += 2; | |
3354 | } | |
3355 | ||
3356 | else | |
3357 | { | |
3358 | uschar ch[1]; | |
3359 | ch[0] = string_interpret_escape(&s); | |
3360 | s++; | |
3361 | yield = string_cat(yield, &size, &ptr, ch, 1); | |
3362 | } | |
3363 | ||
3364 | continue; | |
3365 | } | |
3366 | ||
3367 | /* Anything other than $ is just copied verbatim, unless we are | |
3368 | looking for a terminating } character. */ | |
3369 | ||
3370 | if (ket_ends && *s == '}') break; | |
3371 | ||
3372 | if (*s != '$') | |
3373 | { | |
3374 | yield = string_cat(yield, &size, &ptr, s++, 1); | |
3375 | continue; | |
3376 | } | |
3377 | ||
3378 | /* No { after the $ - must be a plain name or a number for string | |
3379 | match variable. There has to be a fudge for variables that are the | |
3380 | names of header fields preceded by "$header_" because header field | |
3381 | names can contain any printing characters except space and colon. | |
3382 | For those that don't like typing this much, "$h_" is a synonym for | |
3383 | "$header_". A non-existent header yields a NULL value; nothing is | |
3384 | inserted. */ | |
3385 | ||
3386 | if (isalpha((*(++s)))) | |
3387 | { | |
3388 | int len; | |
3389 | int newsize = 0; | |
3390 | ||
3391 | s = read_name(name, sizeof(name), s, US"_"); | |
3392 | ||
3393 | /* If this is the first thing to be expanded, release the pre-allocated | |
3394 | buffer. */ | |
3395 | ||
3396 | if (ptr == 0 && yield != NULL) | |
3397 | { | |
d6b4d938 | 3398 | if (resetok) store_reset(yield); |
059ec3d9 PH |
3399 | yield = NULL; |
3400 | size = 0; | |
3401 | } | |
3402 | ||
3403 | /* Header */ | |
3404 | ||
3405 | if (Ustrncmp(name, "h_", 2) == 0 || | |
3406 | Ustrncmp(name, "rh_", 3) == 0 || | |
3407 | Ustrncmp(name, "bh_", 3) == 0 || | |
3408 | Ustrncmp(name, "header_", 7) == 0 || | |
3409 | Ustrncmp(name, "rheader_", 8) == 0 || | |
3410 | Ustrncmp(name, "bheader_", 8) == 0) | |
3411 | { | |
3412 | BOOL want_raw = (name[0] == 'r')? TRUE : FALSE; | |
3413 | uschar *charset = (name[0] == 'b')? NULL : headers_charset; | |
3414 | s = read_header_name(name, sizeof(name), s); | |
3415 | value = find_header(name, FALSE, &newsize, want_raw, charset); | |
3416 | ||
3417 | /* If we didn't find the header, and the header contains a closing brace | |
0d85fa3f | 3418 | character, this may be a user error where the terminating colon |
059ec3d9 PH |
3419 | has been omitted. Set a flag to adjust the error message in this case. |
3420 | But there is no error here - nothing gets inserted. */ | |
3421 | ||
3422 | if (value == NULL) | |
3423 | { | |
3424 | if (Ustrchr(name, '}') != NULL) malformed_header = TRUE; | |
3425 | continue; | |
3426 | } | |
3427 | } | |
3428 | ||
3429 | /* Variable */ | |
3430 | ||
3431 | else | |
3432 | { | |
3433 | value = find_variable(name, FALSE, skipping, &newsize); | |
3434 | if (value == NULL) | |
3435 | { | |
3436 | expand_string_message = | |
3437 | string_sprintf("unknown variable name \"%s\"", name); | |
641cb756 | 3438 | check_variable_error_message(name); |
059ec3d9 PH |
3439 | goto EXPAND_FAILED; |
3440 | } | |
3441 | } | |
3442 | ||
3443 | /* If the data is known to be in a new buffer, newsize will be set to the | |
3444 | size of that buffer. If this is the first thing in an expansion string, | |
3445 | yield will be NULL; just point it at the new store instead of copying. Many | |
3446 | expansion strings contain just one reference, so this is a useful | |
3447 | optimization, especially for humungous headers. */ | |
3448 | ||
3449 | len = Ustrlen(value); | |
3450 | if (yield == NULL && newsize != 0) | |
3451 | { | |
3452 | yield = value; | |
3453 | size = newsize; | |
3454 | ptr = len; | |
3455 | } | |
3456 | else yield = string_cat(yield, &size, &ptr, value, len); | |
3457 | ||
3458 | continue; | |
3459 | } | |
3460 | ||
3461 | if (isdigit(*s)) | |
3462 | { | |
3463 | int n; | |
3464 | s = read_number(&n, s); | |
3465 | if (n >= 0 && n <= expand_nmax) | |
3466 | yield = string_cat(yield, &size, &ptr, expand_nstring[n], | |
3467 | expand_nlength[n]); | |
3468 | continue; | |
3469 | } | |
3470 | ||
3471 | /* Otherwise, if there's no '{' after $ it's an error. */ | |
3472 | ||
3473 | if (*s != '{') | |
3474 | { | |
3475 | expand_string_message = US"$ not followed by letter, digit, or {"; | |
3476 | goto EXPAND_FAILED; | |
3477 | } | |
3478 | ||
3479 | /* After { there can be various things, but they all start with | |
3480 | an initial word, except for a number for a string match variable. */ | |
3481 | ||
3482 | if (isdigit((*(++s)))) | |
3483 | { | |
3484 | int n; | |
3485 | s = read_number(&n, s); | |
3486 | if (*s++ != '}') | |
3487 | { | |
3488 | expand_string_message = US"} expected after number"; | |
3489 | goto EXPAND_FAILED; | |
3490 | } | |
3491 | if (n >= 0 && n <= expand_nmax) | |
3492 | yield = string_cat(yield, &size, &ptr, expand_nstring[n], | |
3493 | expand_nlength[n]); | |
3494 | continue; | |
3495 | } | |
3496 | ||
3497 | if (!isalpha(*s)) | |
3498 | { | |
3499 | expand_string_message = US"letter or digit expected after ${"; | |
3500 | goto EXPAND_FAILED; | |
3501 | } | |
3502 | ||
3503 | /* Allow "-" in names to cater for substrings with negative | |
3504 | arguments. Since we are checking for known names after { this is | |
3505 | OK. */ | |
3506 | ||
3507 | s = read_name(name, sizeof(name), s, US"_-"); | |
3508 | item_type = chop_match(name, item_table, sizeof(item_table)/sizeof(uschar *)); | |
3509 | ||
3510 | switch(item_type) | |
3511 | { | |
3512 | /* Handle conditionals - preserve the values of the numerical expansion | |
3513 | variables in case they get changed by a regular expression match in the | |
3514 | condition. If not, they retain their external settings. At the end | |
3515 | of this "if" section, they get restored to their previous values. */ | |
3516 | ||
3517 | case EITEM_IF: | |
3518 | { | |
3519 | BOOL cond = FALSE; | |
3520 | uschar *next_s; | |
3521 | int save_expand_nmax = | |
3522 | save_expand_strings(save_expand_nstring, save_expand_nlength); | |
3523 | ||
3524 | while (isspace(*s)) s++; | |
3525 | next_s = eval_condition(s, skipping? NULL : &cond); | |
3526 | if (next_s == NULL) goto EXPAND_FAILED; /* message already set */ | |
3527 | ||
3528 | DEBUG(D_expand) | |
3529 | debug_printf("condition: %.*s\n result: %s\n", (int)(next_s - s), s, | |
3530 | cond? "true" : "false"); | |
3531 | ||
3532 | s = next_s; | |
3533 | ||
3534 | /* The handling of "yes" and "no" result strings is now in a separate | |
3535 | function that is also used by ${lookup} and ${extract} and ${run}. */ | |
3536 | ||
3537 | switch(process_yesno( | |
3538 | skipping, /* were previously skipping */ | |
3539 | cond, /* success/failure indicator */ | |
3540 | lookup_value, /* value to reset for string2 */ | |
3541 | &s, /* input pointer */ | |
3542 | &yield, /* output pointer */ | |
3543 | &size, /* output size */ | |
3544 | &ptr, /* output current point */ | |
3545 | US"if")) /* condition type */ | |
3546 | { | |
3547 | case 1: goto EXPAND_FAILED; /* when all is well, the */ | |
3548 | case 2: goto EXPAND_FAILED_CURLY; /* returned value is 0 */ | |
3549 | } | |
3550 | ||
3551 | /* Restore external setting of expansion variables for continuation | |
3552 | at this level. */ | |
3553 | ||
3554 | restore_expand_strings(save_expand_nmax, save_expand_nstring, | |
3555 | save_expand_nlength); | |
3556 | continue; | |
3557 | } | |
3558 | ||
3559 | /* Handle database lookups unless locked out. If "skipping" is TRUE, we are | |
3560 | expanding an internal string that isn't actually going to be used. All we | |
3561 | need to do is check the syntax, so don't do a lookup at all. Preserve the | |
3562 | values of the numerical expansion variables in case they get changed by a | |
3563 | partial lookup. If not, they retain their external settings. At the end | |
3564 | of this "lookup" section, they get restored to their previous values. */ | |
3565 | ||
3566 | case EITEM_LOOKUP: | |
3567 | { | |
3568 | int stype, partial, affixlen, starflags; | |
3569 | int expand_setup = 0; | |
3570 | int nameptr = 0; | |
3571 | uschar *key, *filename, *affix; | |
3572 | uschar *save_lookup_value = lookup_value; | |
3573 | int save_expand_nmax = | |
3574 | save_expand_strings(save_expand_nstring, save_expand_nlength); | |
3575 | ||
3576 | if ((expand_forbid & RDO_LOOKUP) != 0) | |
3577 | { | |
3578 | expand_string_message = US"lookup expansions are not permitted"; | |
3579 | goto EXPAND_FAILED; | |
3580 | } | |
3581 | ||
3582 | /* Get the key we are to look up for single-key+file style lookups. | |
3583 | Otherwise set the key NULL pro-tem. */ | |
3584 | ||
3585 | while (isspace(*s)) s++; | |
3586 | if (*s == '{') | |
3587 | { | |
3588 | key = expand_string_internal(s+1, TRUE, &s, skipping); | |
3589 | if (key == NULL) goto EXPAND_FAILED; | |
3590 | if (*s++ != '}') goto EXPAND_FAILED_CURLY; | |
3591 | while (isspace(*s)) s++; | |
3592 | } | |
3593 | else key = NULL; | |
3594 | ||
3595 | /* Find out the type of database */ | |
3596 | ||
3597 | if (!isalpha(*s)) | |
3598 | { | |
3599 | expand_string_message = US"missing lookup type"; | |
3600 | goto EXPAND_FAILED; | |
3601 | } | |
3602 | ||
3603 | /* The type is a string that may contain special characters of various | |
3604 | kinds. Allow everything except space or { to appear; the actual content | |
3605 | is checked by search_findtype_partial. */ | |
3606 | ||
3607 | while (*s != 0 && *s != '{' && !isspace(*s)) | |
3608 | { | |
3609 | if (nameptr < sizeof(name) - 1) name[nameptr++] = *s; | |
3610 | s++; | |
3611 | } | |
3612 | name[nameptr] = 0; | |
3613 | while (isspace(*s)) s++; | |
3614 | ||
3615 | /* Now check for the individual search type and any partial or default | |
3616 | options. Only those types that are actually in the binary are valid. */ | |
3617 | ||
3618 | stype = search_findtype_partial(name, &partial, &affix, &affixlen, | |
3619 | &starflags); | |
3620 | if (stype < 0) | |
3621 | { | |
3622 | expand_string_message = search_error_message; | |
3623 | goto EXPAND_FAILED; | |
3624 | } | |
3625 | ||
3626 | /* Check that a key was provided for those lookup types that need it, | |
3627 | and was not supplied for those that use the query style. */ | |
3628 | ||
13b685f9 | 3629 | if (!mac_islookup(stype, lookup_querystyle|lookup_absfilequery)) |
059ec3d9 PH |
3630 | { |
3631 | if (key == NULL) | |
3632 | { | |
3633 | expand_string_message = string_sprintf("missing {key} for single-" | |
3634 | "key \"%s\" lookup", name); | |
3635 | goto EXPAND_FAILED; | |
3636 | } | |
3637 | } | |
3638 | else | |
3639 | { | |
3640 | if (key != NULL) | |
3641 | { | |
3642 | expand_string_message = string_sprintf("a single key was given for " | |
3643 | "lookup type \"%s\", which is not a single-key lookup type", name); | |
3644 | goto EXPAND_FAILED; | |
3645 | } | |
3646 | } | |
3647 | ||
3648 | /* Get the next string in brackets and expand it. It is the file name for | |
13b685f9 PH |
3649 | single-key+file lookups, and the whole query otherwise. In the case of |
3650 | queries that also require a file name (e.g. sqlite), the file name comes | |
3651 | first. */ | |
059ec3d9 PH |
3652 | |
3653 | if (*s != '{') goto EXPAND_FAILED_CURLY; | |
3654 | filename = expand_string_internal(s+1, TRUE, &s, skipping); | |
3655 | if (filename == NULL) goto EXPAND_FAILED; | |
3656 | if (*s++ != '}') goto EXPAND_FAILED_CURLY; | |
3657 | while (isspace(*s)) s++; | |
3658 | ||
3659 | /* If this isn't a single-key+file lookup, re-arrange the variables | |
13b685f9 PH |
3660 | to be appropriate for the search_ functions. For query-style lookups, |
3661 | there is just a "key", and no file name. For the special query-style + | |
3662 | file types, the query (i.e. "key") starts with a file name. */ | |
059ec3d9 PH |
3663 | |
3664 | if (key == NULL) | |
3665 | { | |
13b685f9 | 3666 | while (isspace(*filename)) filename++; |
059ec3d9 | 3667 | key = filename; |
13b685f9 PH |
3668 | |
3669 | if (mac_islookup(stype, lookup_querystyle)) | |
3670 | { | |
3671 | filename = NULL; | |
3672 | } | |
3673 | else | |
3674 | { | |
3675 | if (*filename != '/') | |
3676 | { | |
3677 | expand_string_message = string_sprintf( | |
3678 | "absolute file name expected for \"%s\" lookup", name); | |
3679 | goto EXPAND_FAILED; | |
3680 | } | |
3681 | while (*key != 0 && !isspace(*key)) key++; | |
3682 | if (*key != 0) *key++ = 0; | |
3683 | } | |
059ec3d9 PH |
3684 | } |
3685 | ||
3686 | /* If skipping, don't do the next bit - just lookup_value == NULL, as if | |
3687 | the entry was not found. Note that there is no search_close() function. | |
3688 | Files are left open in case of re-use. At suitable places in higher logic, | |
3689 | search_tidyup() is called to tidy all open files. This can save opening | |
3690 | the same file several times. However, files may also get closed when | |
3691 | others are opened, if too many are open at once. The rule is that a | |
3692 | handle should not be used after a second search_open(). | |
3693 | ||
3694 | Request that a partial search sets up $1 and maybe $2 by passing | |
3695 | expand_setup containing zero. If its value changes, reset expand_nmax, | |
3696 | since new variables will have been set. Note that at the end of this | |
3697 | "lookup" section, the old numeric variables are restored. */ | |
3698 | ||
3699 | if (skipping) | |
3700 | lookup_value = NULL; | |
3701 | else | |
3702 | { | |
3703 | void *handle = search_open(filename, stype, 0, NULL, NULL); | |
3704 | if (handle == NULL) | |
3705 | { | |
3706 | expand_string_message = search_error_message; | |
3707 | goto EXPAND_FAILED; | |
3708 | } | |
3709 | lookup_value = search_find(handle, filename, key, partial, affix, | |
3710 | affixlen, starflags, &expand_setup); | |
3711 | if (search_find_defer) | |
3712 | { | |
3713 | expand_string_message = | |
b72aab72 PP |
3714 | string_sprintf("lookup of \"%s\" gave DEFER: %s", |
3715 | string_printing2(key, FALSE), search_error_message); | |
059ec3d9 PH |
3716 | goto EXPAND_FAILED; |
3717 | } | |
3718 | if (expand_setup > 0) expand_nmax = expand_setup; | |
3719 | } | |
3720 | ||
3721 | /* The handling of "yes" and "no" result strings is now in a separate | |
3722 | function that is also used by ${if} and ${extract}. */ | |
3723 | ||
3724 | switch(process_yesno( | |
3725 | skipping, /* were previously skipping */ | |
3726 | lookup_value != NULL, /* success/failure indicator */ | |
3727 | save_lookup_value, /* value to reset for string2 */ | |
3728 | &s, /* input pointer */ | |
3729 | &yield, /* output pointer */ | |
3730 | &size, /* output size */ | |
3731 | &ptr, /* output current point */ | |
3732 | US"lookup")) /* condition type */ | |
3733 | { | |
3734 | case 1: goto EXPAND_FAILED; /* when all is well, the */ | |
3735 | case 2: goto EXPAND_FAILED_CURLY; /* returned value is 0 */ | |
3736 | } | |
3737 | ||
3738 | /* Restore external setting of expansion variables for carrying on | |
3739 | at this level, and continue. */ | |
3740 | ||
3741 | restore_expand_strings(save_expand_nmax, save_expand_nstring, | |
3742 | save_expand_nlength); | |
3743 | continue; | |
3744 | } | |
3745 | ||
3746 | /* If Perl support is configured, handle calling embedded perl subroutines, | |
3747 | unless locked out at this time. Syntax is ${perl{sub}} or ${perl{sub}{arg}} | |
3748 | or ${perl{sub}{arg1}{arg2}} or up to a maximum of EXIM_PERL_MAX_ARGS | |
3749 | arguments (defined below). */ | |
3750 | ||
059ec3d9 PH |
3751 | #define EXIM_PERL_MAX_ARGS 8 |
3752 | ||
3753 | case EITEM_PERL: | |
1a46a8c5 PH |
3754 | #ifndef EXIM_PERL |
3755 | expand_string_message = US"\"${perl\" encountered, but this facility " | |
3756 | "is not included in this binary"; | |
3757 | goto EXPAND_FAILED; | |
3758 | ||
3759 | #else /* EXIM_PERL */ | |
059ec3d9 PH |
3760 | { |
3761 | uschar *sub_arg[EXIM_PERL_MAX_ARGS + 2]; | |
3762 | uschar *new_yield; | |
3763 | ||
3764 | if ((expand_forbid & RDO_PERL) != 0) | |
3765 | { | |
3766 | expand_string_message = US"Perl calls are not permitted"; | |
3767 | goto EXPAND_FAILED; | |
3768 | } | |
3769 | ||
3770 | switch(read_subs(sub_arg, EXIM_PERL_MAX_ARGS + 1, 1, &s, skipping, TRUE, | |
3771 | US"perl")) | |
3772 | { | |
3773 | case 1: goto EXPAND_FAILED_CURLY; | |
3774 | case 2: | |
3775 | case 3: goto EXPAND_FAILED; | |
3776 | } | |
3777 | ||
3778 | /* If skipping, we don't actually do anything */ | |
3779 | ||
3780 | if (skipping) continue; | |
3781 | ||
3782 | /* Start the interpreter if necessary */ | |
3783 | ||
3784 | if (!opt_perl_started) | |
3785 | { | |
3786 | uschar *initerror; | |
3787 | if (opt_perl_startup == NULL) | |
3788 | { | |
3789 | expand_string_message = US"A setting of perl_startup is needed when " | |
3790 | "using the Perl interpreter"; | |
3791 | goto EXPAND_FAILED; | |
3792 | } | |
3793 | DEBUG(D_any) debug_printf("Starting Perl interpreter\n"); | |
3794 | initerror = init_perl(opt_perl_startup); | |
3795 | if (initerror != NULL) | |
3796 | { | |
3797 | expand_string_message = | |
3798 | string_sprintf("error in perl_startup code: %s\n", initerror); | |
3799 | goto EXPAND_FAILED; | |
3800 | } | |
3801 | opt_perl_started = TRUE; | |
3802 | } | |
3803 | ||
3804 | /* Call the function */ | |
3805 | ||
3806 | sub_arg[EXIM_PERL_MAX_ARGS + 1] = NULL; | |
3807 | new_yield = call_perl_cat(yield, &size, &ptr, &expand_string_message, | |
3808 | sub_arg[0], sub_arg + 1); | |
3809 | ||
3810 | /* NULL yield indicates failure; if the message pointer has been set to | |
3811 | NULL, the yield was undef, indicating a forced failure. Otherwise the | |
3812 | message will indicate some kind of Perl error. */ | |
3813 | ||
3814 | if (new_yield == NULL) | |
3815 | { | |
3816 | if (expand_string_message == NULL) | |
3817 | { | |
3818 | expand_string_message = | |
3819 | string_sprintf("Perl subroutine \"%s\" returned undef to force " | |
3820 | "failure", sub_arg[0]); | |
3821 | expand_string_forcedfail = TRUE; | |
3822 | } | |
3823 | goto EXPAND_FAILED; | |
3824 | } | |
3825 | ||
3826 | /* Yield succeeded. Ensure forcedfail is unset, just in case it got | |
3827 | set during a callback from Perl. */ | |
3828 | ||
3829 | expand_string_forcedfail = FALSE; | |
3830 | yield = new_yield; | |
3831 | continue; | |
3832 | } | |
3833 | #endif /* EXIM_PERL */ | |
3834 | ||
fffda43a TK |
3835 | /* Transform email address to "prvs" scheme to use |
3836 | as BATV-signed return path */ | |
3837 | ||
3838 | case EITEM_PRVS: | |
3839 | { | |
3840 | uschar *sub_arg[3]; | |
3841 | uschar *p,*domain; | |
3842 | ||
3843 | switch(read_subs(sub_arg, 3, 2, &s, skipping, TRUE, US"prvs")) | |
3844 | { | |
3845 | case 1: goto EXPAND_FAILED_CURLY; | |
3846 | case 2: | |
3847 | case 3: goto EXPAND_FAILED; | |
3848 | } | |
3849 | ||
3850 | /* If skipping, we don't actually do anything */ | |
3851 | if (skipping) continue; | |
3852 | ||
3853 | /* sub_arg[0] is the address */ | |
3854 | domain = Ustrrchr(sub_arg[0],'@'); | |
3855 | if ( (domain == NULL) || (domain == sub_arg[0]) || (Ustrlen(domain) == 1) ) | |
3856 | { | |
cb9328de PH |
3857 | expand_string_message = US"prvs first argument must be a qualified email address"; |
3858 | goto EXPAND_FAILED; | |
3859 | } | |
3860 | ||
3861 | /* Calculate the hash. The second argument must be a single-digit | |
3862 | key number, or unset. */ | |
3863 | ||
3864 | if (sub_arg[2] != NULL && | |
3865 | (!isdigit(sub_arg[2][0]) || sub_arg[2][1] != 0)) | |
3866 | { | |
3867 | expand_string_message = US"prvs second argument must be a single digit"; | |
fffda43a TK |
3868 | goto EXPAND_FAILED; |
3869 | } | |
3870 | ||
fffda43a TK |
3871 | p = prvs_hmac_sha1(sub_arg[0],sub_arg[1],sub_arg[2],prvs_daystamp(7)); |
3872 | if (p == NULL) | |
3873 | { | |
cb9328de | 3874 | expand_string_message = US"prvs hmac-sha1 conversion failed"; |
fffda43a TK |
3875 | goto EXPAND_FAILED; |
3876 | } | |
3877 | ||
3878 | /* Now separate the domain from the local part */ | |
3879 | *domain++ = '\0'; | |
3880 | ||
3881 | yield = string_cat(yield,&size,&ptr,US"prvs=",5); | |
fffda43a TK |
3882 | string_cat(yield,&size,&ptr,(sub_arg[2] != NULL) ? sub_arg[2] : US"0", 1); |
3883 | string_cat(yield,&size,&ptr,prvs_daystamp(7),3); | |
3884 | string_cat(yield,&size,&ptr,p,6); | |
a48ced90 TK |
3885 | string_cat(yield,&size,&ptr,US"=",1); |
3886 | string_cat(yield,&size,&ptr,sub_arg[0],Ustrlen(sub_arg[0])); | |
fffda43a TK |
3887 | string_cat(yield,&size,&ptr,US"@",1); |
3888 | string_cat(yield,&size,&ptr,domain,Ustrlen(domain)); | |
3889 | ||
3890 | continue; | |
3891 | } | |
3892 | ||
3893 | /* Check a prvs-encoded address for validity */ | |
3894 | ||
3895 | case EITEM_PRVSCHECK: | |
3896 | { | |
3897 | uschar *sub_arg[3]; | |
3898 | int mysize = 0, myptr = 0; | |
3899 | const pcre *re; | |
3900 | uschar *p; | |
72fdd6ae PH |
3901 | |
3902 | /* TF: Ugliness: We want to expand parameter 1 first, then set | |
fffda43a TK |
3903 | up expansion variables that are used in the expansion of |
3904 | parameter 2. So we clone the string for the first | |
72fdd6ae PH |
3905 | expansion, where we only expand parameter 1. |
3906 | ||
3907 | PH: Actually, that isn't necessary. The read_subs() function is | |
3908 | designed to work this way for the ${if and ${lookup expansions. I've | |
3909 | tidied the code. | |
3910 | */ | |
fffda43a TK |
3911 | |
3912 | /* Reset expansion variables */ | |
3913 | prvscheck_result = NULL; | |
3914 | prvscheck_address = NULL; | |
3915 | prvscheck_keynum = NULL; | |
3916 | ||
72fdd6ae | 3917 | switch(read_subs(sub_arg, 1, 1, &s, skipping, FALSE, US"prvs")) |
fffda43a TK |
3918 | { |
3919 | case 1: goto EXPAND_FAILED_CURLY; | |
3920 | case 2: | |
3921 | case 3: goto EXPAND_FAILED; | |
3922 | } | |
3923 | ||
a48ced90 | 3924 | re = regex_must_compile(US"^prvs\\=([0-9])([0-9]{3})([A-F0-9]{6})\\=(.+)\\@(.+)$", |
fffda43a TK |
3925 | TRUE,FALSE); |
3926 | ||
72fdd6ae PH |
3927 | if (regex_match_and_setup(re,sub_arg[0],0,-1)) |
3928 | { | |
a48ced90 TK |
3929 | uschar *local_part = string_copyn(expand_nstring[4],expand_nlength[4]); |
3930 | uschar *key_num = string_copyn(expand_nstring[1],expand_nlength[1]); | |
3931 | uschar *daystamp = string_copyn(expand_nstring[2],expand_nlength[2]); | |
3932 | uschar *hash = string_copyn(expand_nstring[3],expand_nlength[3]); | |
fffda43a TK |
3933 | uschar *domain = string_copyn(expand_nstring[5],expand_nlength[5]); |
3934 | ||
3935 | DEBUG(D_expand) debug_printf("prvscheck localpart: %s\n", local_part); | |
3936 | DEBUG(D_expand) debug_printf("prvscheck key number: %s\n", key_num); | |
3937 | DEBUG(D_expand) debug_printf("prvscheck daystamp: %s\n", daystamp); | |
3938 | DEBUG(D_expand) debug_printf("prvscheck hash: %s\n", hash); | |
3939 | DEBUG(D_expand) debug_printf("prvscheck domain: %s\n", domain); | |
3940 | ||
3941 | /* Set up expansion variables */ | |
3942 | prvscheck_address = string_cat(NULL, &mysize, &myptr, local_part, Ustrlen(local_part)); | |
2740a2ca | 3943 | string_cat(prvscheck_address,&mysize,&myptr,US"@",1); |
fffda43a TK |
3944 | string_cat(prvscheck_address,&mysize,&myptr,domain,Ustrlen(domain)); |
3945 | prvscheck_address[myptr] = '\0'; | |
3946 | prvscheck_keynum = string_copy(key_num); | |
3947 | ||
72fdd6ae PH |
3948 | /* Now expand the second argument */ |
3949 | switch(read_subs(sub_arg, 1, 1, &s, skipping, FALSE, US"prvs")) | |
fffda43a TK |
3950 | { |
3951 | case 1: goto EXPAND_FAILED_CURLY; | |
3952 | case 2: | |
3953 | case 3: goto EXPAND_FAILED; | |
3954 | } | |
3955 | ||
fffda43a | 3956 | /* Now we have the key and can check the address. */ |
72fdd6ae PH |
3957 | |
3958 | p = prvs_hmac_sha1(prvscheck_address, sub_arg[0], prvscheck_keynum, | |
3959 | daystamp); | |
3960 | ||
fffda43a TK |
3961 | if (p == NULL) |
3962 | { | |
3963 | expand_string_message = US"hmac-sha1 conversion failed"; | |
3964 | goto EXPAND_FAILED; | |
3965 | } | |
3966 | ||
3967 | DEBUG(D_expand) debug_printf("prvscheck: received hash is %s\n", hash); | |
3968 | DEBUG(D_expand) debug_printf("prvscheck: own hash is %s\n", p); | |
72fdd6ae | 3969 | |
fffda43a TK |
3970 | if (Ustrcmp(p,hash) == 0) |
3971 | { | |
3972 | /* Success, valid BATV address. Now check the expiry date. */ | |
3973 | uschar *now = prvs_daystamp(0); | |
3974 | unsigned int inow = 0,iexpire = 1; | |
3975 | ||
ff790e47 PH |
3976 | (void)sscanf(CS now,"%u",&inow); |
3977 | (void)sscanf(CS daystamp,"%u",&iexpire); | |
fffda43a TK |
3978 | |
3979 | /* When "iexpire" is < 7, a "flip" has occured. | |
3980 | Adjust "inow" accordingly. */ | |
3981 | if ( (iexpire < 7) && (inow >= 993) ) inow = 0; | |
3982 | ||
686c36b7 | 3983 | if (iexpire >= inow) |
fffda43a TK |
3984 | { |
3985 | prvscheck_result = US"1"; | |
3986 | DEBUG(D_expand) debug_printf("prvscheck: success, $pvrs_result set to 1\n"); | |
3987 | } | |
3988 | else | |
3989 | { | |
3990 | prvscheck_result = NULL; | |
3991 | DEBUG(D_expand) debug_printf("prvscheck: signature expired, $pvrs_result unset\n"); | |
3992 | } | |
3993 | } | |
3994 | else | |
3995 | { | |
3996 | prvscheck_result = NULL; | |
3997 | DEBUG(D_expand) debug_printf("prvscheck: hash failure, $pvrs_result unset\n"); | |
3998 | } | |
72fdd6ae PH |
3999 | |
4000 | /* Now expand the final argument. We leave this till now so that | |
4001 | it can include $prvscheck_result. */ | |
4002 | ||
4003 | switch(read_subs(sub_arg, 1, 0, &s, skipping, TRUE, US"prvs")) | |
4004 | { | |
4005 | case 1: goto EXPAND_FAILED_CURLY; | |
4006 | case 2: | |
4007 | case 3: goto EXPAND_FAILED; | |
4008 | } | |
4009 | ||
4010 | if (sub_arg[0] == NULL || *sub_arg[0] == '\0') | |
4011 | yield = string_cat(yield,&size,&ptr,prvscheck_address,Ustrlen(prvscheck_address)); | |
4012 | else | |
4013 | yield = string_cat(yield,&size,&ptr,sub_arg[0],Ustrlen(sub_arg[0])); | |
4014 | ||
4015 | /* Reset the "internal" variables afterwards, because they are in | |
4016 | dynamic store that will be reclaimed if the expansion succeeded. */ | |
4017 | ||
4018 | prvscheck_address = NULL; | |
4019 | prvscheck_keynum = NULL; | |
4020 | } | |
fffda43a TK |
4021 | else |
4022 | { | |
4023 | /* Does not look like a prvs encoded address, return the empty string. | |
72fdd6ae PH |
4024 | We need to make sure all subs are expanded first, so as to skip over |
4025 | the entire item. */ | |
4026 | ||
5a03bd24 | 4027 | switch(read_subs(sub_arg, 2, 1, &s, skipping, TRUE, US"prvs")) |
fffda43a TK |
4028 | { |
4029 | case 1: goto EXPAND_FAILED_CURLY; | |
4030 | case 2: | |
4031 | case 3: goto EXPAND_FAILED; | |
4032 | } | |
4033 | } | |
4034 | ||
4035 | continue; | |
4036 | } | |
4037 | ||
059ec3d9 PH |
4038 | /* Handle "readfile" to insert an entire file */ |
4039 | ||
4040 | case EITEM_READFILE: | |
4041 | { | |
4042 | FILE *f; | |
4043 | uschar *sub_arg[2]; | |
4044 | ||
4045 | if ((expand_forbid & RDO_READFILE) != 0) | |
4046 | { | |
4047 | expand_string_message = US"file insertions are not permitted"; | |
4048 | goto EXPAND_FAILED; | |
4049 | } | |
4050 | ||
4051 | switch(read_subs(sub_arg, 2, 1, &s, skipping, TRUE, US"readfile")) | |
4052 | { | |
4053 | case 1: goto EXPAND_FAILED_CURLY; | |
4054 | case 2: | |
4055 | case 3: goto EXPAND_FAILED; | |
4056 | } | |
4057 | ||
4058 | /* If skipping, we don't actually do anything */ | |
4059 | ||
4060 | if (skipping) continue; | |
4061 | ||
4062 | /* Open the file and read it */ | |
4063 | ||
4064 | f = Ufopen(sub_arg[0], "rb"); | |
4065 | if (f == NULL) | |
4066 | { | |
4067 | expand_string_message = string_open_failed(errno, "%s", sub_arg[0]); | |
4068 | goto EXPAND_FAILED; | |
4069 | } | |
4070 | ||
4071 | yield = cat_file(f, yield, &size, &ptr, sub_arg[1]); | |
f1e894f3 | 4072 | (void)fclose(f); |
059ec3d9 PH |
4073 | continue; |
4074 | } | |
4075 | ||
4076 | /* Handle "readsocket" to insert data from a Unix domain socket */ | |
4077 | ||
4078 | case EITEM_READSOCK: | |
4079 | { | |
4080 | int fd; | |
4081 | int timeout = 5; | |
4082 | int save_ptr = ptr; | |
4083 | FILE *f; | |
4084 | struct sockaddr_un sockun; /* don't call this "sun" ! */ | |
4085 | uschar *arg; | |
4086 | uschar *sub_arg[4]; | |
4087 | ||
4088 | if ((expand_forbid & RDO_READSOCK) != 0) | |
4089 | { | |
4090 | expand_string_message = US"socket insertions are not permitted"; | |
4091 | goto EXPAND_FAILED; | |
4092 | } | |
4093 | ||
4094 | /* Read up to 4 arguments, but don't do the end of item check afterwards, | |
4095 | because there may be a string for expansion on failure. */ | |
4096 | ||
4097 | switch(read_subs(sub_arg, 4, 2, &s, skipping, FALSE, US"readsocket")) | |
4098 | { | |
4099 | case 1: goto EXPAND_FAILED_CURLY; | |
4100 | case 2: /* Won't occur: no end check */ | |
4101 | case 3: goto EXPAND_FAILED; | |
4102 | } | |
4103 | ||
4104 | /* Sort out timeout, if given */ | |
4105 | ||
4106 | if (sub_arg[2] != NULL) | |
4107 | { | |
4108 | timeout = readconf_readtime(sub_arg[2], 0, FALSE); | |
4109 | if (timeout < 0) | |
4110 | { | |
4111 | expand_string_message = string_sprintf("bad time value %s", | |
4112 | sub_arg[2]); | |
4113 | goto EXPAND_FAILED; | |
4114 | } | |
4115 | } | |
4116 | else sub_arg[3] = NULL; /* No eol if no timeout */ | |
4117 | ||
1cce3af8 PH |
4118 | /* If skipping, we don't actually do anything. Otherwise, arrange to |
4119 | connect to either an IP or a Unix socket. */ | |
059ec3d9 PH |
4120 | |
4121 | if (!skipping) | |
4122 | { | |
1cce3af8 | 4123 | /* Handle an IP (internet) domain */ |
059ec3d9 | 4124 | |
91ecef39 | 4125 | if (Ustrncmp(sub_arg[0], "inet:", 5) == 0) |
059ec3d9 | 4126 | { |
1cce3af8 PH |
4127 | BOOL connected = FALSE; |
4128 | int namelen, port; | |
4129 | host_item shost; | |
4130 | host_item *h; | |
4131 | uschar *server_name = sub_arg[0] + 5; | |
4132 | uschar *port_name = Ustrrchr(server_name, ':'); | |
4133 | ||
4134 | /* Sort out the port */ | |
4135 | ||
4136 | if (port_name == NULL) | |
4137 | { | |
4138 | expand_string_message = | |
4139 | string_sprintf("missing port for readsocket %s", sub_arg[0]); | |
4140 | goto EXPAND_FAILED; | |
4141 | } | |
4142 | *port_name++ = 0; /* Terminate server name */ | |
4143 | ||
4144 | if (isdigit(*port_name)) | |
4145 | { | |
4146 | uschar *end; | |
4147 | port = Ustrtol(port_name, &end, 0); | |
4148 | if (end != port_name + Ustrlen(port_name)) | |
4149 | { | |
4150 | expand_string_message = | |
4151 | string_sprintf("invalid port number %s", port_name); | |
4152 | goto EXPAND_FAILED; | |
4153 | } | |
4154 | } | |
4155 | else | |
4156 | { | |
4157 | struct servent *service_info = getservbyname(CS port_name, "tcp"); | |
4158 | if (service_info == NULL) | |
4159 | { | |
4160 | expand_string_message = string_sprintf("unknown port \"%s\"", | |
4161 | port_name); | |
4162 | goto EXPAND_FAILED; | |
4163 | } | |
4164 | port = ntohs(service_info->s_port); | |
4165 | } | |
4166 | ||
4167 | /* Sort out the server. */ | |
4168 | ||
4169 | shost.next = NULL; | |
4170 | shost.address = NULL; | |
4171 | shost.port = port; | |
4172 | shost.mx = -1; | |
4173 | ||
4174 | namelen = Ustrlen(server_name); | |
4175 | ||
4176 | /* Anything enclosed in [] must be an IP address. */ | |
4177 | ||
4178 | if (server_name[0] == '[' && | |
4179 | server_name[namelen - 1] == ']') | |
4180 | { | |
4181 | server_name[namelen - 1] = 0; | |
4182 | server_name++; | |
4183 | if (string_is_ip_address(server_name, NULL) == 0) | |
4184 | { | |
4185 | expand_string_message = | |
4186 | string_sprintf("malformed IP address \"%s\"", server_name); | |
4187 | goto EXPAND_FAILED; | |
4188 | } | |
4189 | shost.name = shost.address = server_name; | |
4190 | } | |
4191 | ||
4192 | /* Otherwise check for an unadorned IP address */ | |
4193 | ||
4194 | else if (string_is_ip_address(server_name, NULL) != 0) | |
4195 | shost.name = shost.address = server_name; | |
4196 | ||
4197 | /* Otherwise lookup IP address(es) from the name */ | |
4198 | ||
4199 | else | |
4200 | { | |
4201 | shost.name = server_name; | |
322050c2 PH |
4202 | if (host_find_byname(&shost, NULL, HOST_FIND_QUALIFY_SINGLE, NULL, |
4203 | FALSE) != HOST_FOUND) | |
1cce3af8 PH |
4204 | { |
4205 | expand_string_message = | |
4206 | string_sprintf("no IP address found for host %s", shost.name); | |
4207 | goto EXPAND_FAILED; | |
4208 | } | |
4209 | } | |
4210 | ||
4211 | /* Try to connect to the server - test each IP till one works */ | |
4212 | ||
4213 | for (h = &shost; h != NULL; h = h->next) | |
4214 | { | |
4215 | int af = (Ustrchr(h->address, ':') != 0)? AF_INET6 : AF_INET; | |
4216 | if ((fd = ip_socket(SOCK_STREAM, af)) == -1) | |
4217 | { | |
4218 | expand_string_message = string_sprintf("failed to create socket: " | |
4219 | "%s", strerror(errno)); | |
4220 | goto SOCK_FAIL; | |
4221 | } | |
4222 | ||
4223 | if (ip_connect(fd, af, h->address, port, timeout) == 0) | |
4224 | { | |
4225 | connected = TRUE; | |
4226 | break; | |
4227 | } | |
4228 | } | |
4229 | ||
4230 | if (!connected) | |
4231 | { | |
4232 | expand_string_message = string_sprintf("failed to connect to " | |
4233 | "socket %s: couldn't connect to any host", sub_arg[0], | |
4234 | strerror(errno)); | |
4235 | goto SOCK_FAIL; | |
4236 | } | |
059ec3d9 PH |
4237 | } |
4238 | ||
1cce3af8 PH |
4239 | /* Handle a Unix domain socket */ |
4240 | ||
4241 | else | |
059ec3d9 | 4242 | { |
a401ddaa | 4243 | int rc; |
1cce3af8 PH |
4244 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) |
4245 | { | |
4246 | expand_string_message = string_sprintf("failed to create socket: %s", | |
4247 | strerror(errno)); | |
4248 | goto SOCK_FAIL; | |
4249 | } | |
4250 | ||
4251 | sockun.sun_family = AF_UNIX; | |
4252 | sprintf(sockun.sun_path, "%.*s", (int)(sizeof(sockun.sun_path)-1), | |
4253 | sub_arg[0]); | |
a401ddaa PH |
4254 | |
4255 | sigalrm_seen = FALSE; | |
4256 | alarm(timeout); | |
4257 | rc = connect(fd, (struct sockaddr *)(&sockun), sizeof(sockun)); | |
4258 | alarm(0); | |
734e1499 | 4259 | if (sigalrm_seen) |
1cce3af8 | 4260 | { |
734e1499 | 4261 | expand_string_message = US "socket connect timed out"; |
1cce3af8 PH |
4262 | goto SOCK_FAIL; |
4263 | } | |
734e1499 | 4264 | if (rc < 0) |
a401ddaa | 4265 | { |
734e1499 PH |
4266 | expand_string_message = string_sprintf("failed to connect to socket " |
4267 | "%s: %s", sub_arg[0], strerror(errno)); | |
a401ddaa PH |
4268 | goto SOCK_FAIL; |
4269 | } | |
059ec3d9 | 4270 | } |
1cce3af8 | 4271 | |
059ec3d9 PH |
4272 | DEBUG(D_expand) debug_printf("connected to socket %s\n", sub_arg[0]); |
4273 | ||
4274 | /* Write the request string, if not empty */ | |
4275 | ||
4276 | if (sub_arg[1][0] != 0) | |
4277 | { | |
4278 | int len = Ustrlen(sub_arg[1]); | |
4279 | DEBUG(D_expand) debug_printf("writing \"%s\" to socket\n", | |
4280 | sub_arg[1]); | |
4281 | if (write(fd, sub_arg[1], len) != len) | |
4282 | { | |
4283 | expand_string_message = string_sprintf("request write to socket " | |
4284 | "failed: %s", strerror(errno)); | |
4285 | goto SOCK_FAIL; | |
4286 | } | |
4287 | } | |
4288 | ||
c1114884 PH |
4289 | /* Shut down the sending side of the socket. This helps some servers to |
4290 | recognise that it is their turn to do some work. Just in case some | |
4291 | system doesn't have this function, make it conditional. */ | |
4292 | ||
4293 | #ifdef SHUT_WR | |
4294 | shutdown(fd, SHUT_WR); | |
4295 | #endif | |
4296 | ||
059ec3d9 PH |
4297 | /* Now we need to read from the socket, under a timeout. The function |
4298 | that reads a file can be used. */ | |
4299 | ||
4300 | f = fdopen(fd, "rb"); | |
4301 | sigalrm_seen = FALSE; | |
4302 | alarm(timeout); | |
4303 | yield = cat_file(f, yield, &size, &ptr, sub_arg[3]); | |
4304 | alarm(0); | |
f1e894f3 | 4305 | (void)fclose(f); |
059ec3d9 PH |
4306 | |
4307 | /* After a timeout, we restore the pointer in the result, that is, | |
4308 | make sure we add nothing from the socket. */ | |
4309 | ||
4310 | if (sigalrm_seen) | |
4311 | { | |
4312 | ptr = save_ptr; | |
1cce3af8 | 4313 | expand_string_message = US "socket read timed out"; |
059ec3d9 PH |
4314 | goto SOCK_FAIL; |
4315 | } | |
4316 | } | |
4317 | ||
4318 | /* The whole thing has worked (or we were skipping). If there is a | |
4319 | failure string following, we need to skip it. */ | |
4320 | ||
4321 | if (*s == '{') | |
4322 | { | |
4323 | if (expand_string_internal(s+1, TRUE, &s, TRUE) == NULL) | |
4324 | goto EXPAND_FAILED; | |
4325 | if (*s++ != '}') goto EXPAND_FAILED_CURLY; | |
4326 | while (isspace(*s)) s++; | |
4327 | } | |
4328 | if (*s++ != '}') goto EXPAND_FAILED_CURLY; | |
4329 | continue; | |
4330 | ||
4331 | /* Come here on failure to create socket, connect socket, write to the | |
4332 | socket, or timeout on reading. If another substring follows, expand and | |
4333 | use it. Otherwise, those conditions give expand errors. */ | |
4334 | ||
4335 | SOCK_FAIL: | |
4336 | if (*s != '{') goto EXPAND_FAILED; | |
4337 | DEBUG(D_any) debug_printf("%s\n", expand_string_message); | |
4338 | arg = expand_string_internal(s+1, TRUE, &s, FALSE); | |
4339 | if (arg == NULL) goto EXPAND_FAILED; | |
4340 | yield = string_cat(yield, &size, &ptr, arg, Ustrlen(arg)); | |
4341 | if (*s++ != '}') goto EXPAND_FAILED_CURLY; | |
4342 | while (isspace(*s)) s++; | |
4343 | if (*s++ != '}') goto EXPAND_FAILED_CURLY; | |
4344 | continue; | |
4345 | } | |
4346 | ||
4347 | /* Handle "run" to execute a program. */ | |
4348 | ||
4349 | case EITEM_RUN: | |
4350 | { | |
4351 | FILE *f; | |
059ec3d9 PH |
4352 | uschar *arg; |
4353 | uschar **argv; | |
4354 | pid_t pid; | |
4355 | int fd_in, fd_out; | |
4356 | int lsize = 0; | |
4357 | int lptr = 0; | |
4358 | ||
4359 | if ((expand_forbid & RDO_RUN) != 0) | |
4360 | { | |
4361 | expand_string_message = US"running a command is not permitted"; | |
4362 | goto EXPAND_FAILED; | |
4363 | } | |
4364 | ||
4365 | while (isspace(*s)) s++; | |
4366 | if (*s != '{') goto EXPAND_FAILED_CURLY; | |
4367 | arg = expand_string_internal(s+1, TRUE, &s, skipping); | |
4368 | if (arg == NULL) goto EXPAND_FAILED; | |
4369 | while (isspace(*s)) s++; | |
4370 | if (*s++ != '}') goto EXPAND_FAILED_CURLY; | |
4371 | ||
4372 | if (skipping) /* Just pretend it worked when we're skipping */ | |
4373 | { | |
4374 | runrc = 0; | |
4375 | } | |
4376 | else | |
4377 | { | |
4378 | if (!transport_set_up_command(&argv, /* anchor for arg list */ | |
4379 | arg, /* raw command */ | |
4380 | FALSE, /* don't expand the arguments */ | |
4381 | 0, /* not relevant when... */ | |
4382 | NULL, /* no transporting address */ | |
4383 | US"${run} expansion", /* for error messages */ | |
4384 | &expand_string_message)) /* where to put error message */ | |
4385 | { | |
4386 | goto EXPAND_FAILED; | |
4387 | } | |
4388 | ||
4389 | /* Create the child process, making it a group leader. */ | |
4390 | ||
4391 | pid = child_open(argv, NULL, 0077, &fd_in, &fd_out, TRUE); | |
4392 | ||
4393 | if (pid < 0) | |
4394 | { | |
4395 | expand_string_message = | |
4396 | string_sprintf("couldn't create child process: %s", strerror(errno)); | |
4397 | goto EXPAND_FAILED; | |
4398 | } | |
4399 | ||
4400 | /* Nothing is written to the standard input. */ | |
4401 | ||
f1e894f3 | 4402 | (void)close(fd_in); |
059ec3d9 PH |
4403 | |
4404 | /* Wait for the process to finish, applying the timeout, and inspect its | |
4405 | return code for serious disasters. Simple non-zero returns are passed on. | |
4406 | */ | |
4407 | ||
4408 | if ((runrc = child_close(pid, 60)) < 0) | |
4409 | { | |
4410 | if (runrc == -256) | |
4411 | { | |
4412 | expand_string_message = string_sprintf("command timed out"); | |
4413 | killpg(pid, SIGKILL); /* Kill the whole process group */ | |
4414 | } | |
4415 | ||
4416 | else if (runrc == -257) | |
4417 | expand_string_message = string_sprintf("wait() failed: %s", | |
4418 | strerror(errno)); | |
4419 | ||
4420 | else | |
4421 | expand_string_message = string_sprintf("command killed by signal %d", | |
4422 | -runrc); | |
4423 | ||
4424 | goto EXPAND_FAILED; | |
4425 | } | |
4426 | ||
4427 | /* Read the pipe to get the command's output into $value (which is kept | |
4428 | in lookup_value). */ | |
4429 | ||
4430 | f = fdopen(fd_out, "rb"); | |
059ec3d9 PH |
4431 | lookup_value = NULL; |
4432 | lookup_value = cat_file(f, lookup_value, &lsize, &lptr, NULL); | |
f1e894f3 | 4433 | (void)fclose(f); |
059ec3d9 PH |
4434 | } |
4435 | ||
d20976dc | 4436 | /* Process the yes/no strings; $value may be useful in both cases */ |
059ec3d9 PH |
4437 | |
4438 | switch(process_yesno( | |
4439 | skipping, /* were previously skipping */ | |
4440 | runrc == 0, /* success/failure indicator */ | |
d20976dc | 4441 | lookup_value, /* value to reset for string2 */ |
059ec3d9 PH |
4442 | &s, /* input pointer */ |
4443 | &yield, /* output pointer */ | |
4444 | &size, /* output size */ | |
4445 | &ptr, /* output current point */ | |
4446 | US"run")) /* condition type */ | |
4447 | { | |
4448 | case 1: goto EXPAND_FAILED; /* when all is well, the */ | |
4449 | case 2: goto EXPAND_FAILED_CURLY; /* returned value is 0 */ | |
4450 | } | |
4451 | ||
4452 | continue; | |
4453 | } | |
4454 | ||
4455 | /* Handle character translation for "tr" */ | |
4456 | ||
4457 | case EITEM_TR: | |
4458 | { | |
4459 | int oldptr = ptr; | |
4460 | int o2m; | |
4461 | uschar *sub[3]; | |
4462 | ||
4463 | switch(read_subs(sub, 3, 3, &s, skipping, TRUE, US"tr")) | |
4464 | { | |
4465 | case 1: goto EXPAND_FAILED_CURLY; | |
4466 | case 2: | |
4467 | case 3: goto EXPAND_FAILED; | |
4468 | } | |
4469 | ||
4470 | yield = string_cat(yield, &size, &ptr, sub[0], Ustrlen(sub[0])); | |
4471 | o2m = Ustrlen(sub[2]) - 1; | |
4472 | ||
4473 | if (o2m >= 0) for (; oldptr < ptr; oldptr++) | |
4474 | { | |
4475 | uschar *m = Ustrrchr(sub[1], yield[oldptr]); | |
4476 | if (m != NULL) | |
4477 | { | |
4478 | int o = m - sub[1]; | |
4479 | yield[oldptr] = sub[2][(o < o2m)? o : o2m]; | |
4480 | } | |
4481 | } | |
4482 | ||
4483 | continue; | |
4484 | } | |
4485 | ||
4486 | /* Handle "hash", "length", "nhash", and "substr" when they are given with | |
4487 | expanded arguments. */ | |
4488 | ||
4489 | case EITEM_HASH: | |
4490 | case EITEM_LENGTH: | |
4491 | case EITEM_NHASH: | |
4492 | case EITEM_SUBSTR: | |
4493 | { | |
4494 | int i; | |
4495 | int len; | |
4496 | uschar *ret; | |
4497 | int val[2] = { 0, -1 }; | |
4498 | uschar *sub[3]; | |
4499 | ||
4500 | /* "length" takes only 2 arguments whereas the others take 2 or 3. | |
4501 | Ensure that sub[2] is set in the ${length case. */ | |
4502 | ||
4503 | sub[2] = NULL; | |
4504 | switch(read_subs(sub, (item_type == EITEM_LENGTH)? 2:3, 2, &s, skipping, | |
4505 | TRUE, name)) | |
4506 | { | |
4507 | case 1: goto EXPAND_FAILED_CURLY; | |
4508 | case 2: | |
4509 | case 3: goto EXPAND_FAILED; | |
4510 | } | |
4511 | ||
4512 | /* Juggle the arguments if there are only two of them: always move the | |
4513 | string to the last position and make ${length{n}{str}} equivalent to | |
4514 | ${substr{0}{n}{str}}. See the defaults for val[] above. */ | |
4515 | ||
4516 | if (sub[2] == NULL) | |
4517 | { | |
4518 | sub[2] = sub[1]; | |
4519 | sub[1] = NULL; | |
4520 | if (item_type == EITEM_LENGTH) | |
4521 | { | |
4522 | sub[1] = sub[0]; | |
4523 | sub[0] = NULL; | |
4524 | } | |
4525 | } | |
4526 | ||
4527 | for (i = 0; i < 2; i++) | |
4528 | { | |
4529 | if (sub[i] == NULL) continue; | |
4530 | val[i] = (int)Ustrtol(sub[i], &ret, 10); | |
4531 | if (*ret != 0 || (i != 0 && val[i] < 0)) | |
4532 | { | |
4533 | expand_string_message = string_sprintf("\"%s\" is not a%s number " | |
4534 | "(in \"%s\" expansion)", sub[i], (i != 0)? " positive" : "", name); | |
4535 | goto EXPAND_FAILED; | |
4536 | } | |
4537 | } | |
4538 | ||
4539 | ret = | |
4540 | (item_type == EITEM_HASH)? | |
4541 | compute_hash(sub[2], val[0], val[1], &len) : | |
4542 | (item_type == EITEM_NHASH)? | |
4543 | compute_nhash(sub[2], val[0], val[1], &len) : | |
4544 | extract_substr(sub[2], val[0], val[1], &len); | |
4545 | ||
4546 | if (ret == NULL) goto EXPAND_FAILED; | |
4547 | yield = string_cat(yield, &size, &ptr, ret, len); | |
4548 | continue; | |
4549 | } | |
4550 | ||
4551 | /* Handle HMAC computation: ${hmac{<algorithm>}{<secret>}{<text>}} | |
4552 | This code originally contributed by Steve Haslam. It currently supports | |
4553 | the use of MD5 and SHA-1 hashes. | |
4554 | ||
4555 | We need some workspace that is large enough to handle all the supported | |
4556 | hash types. Use macros to set the sizes rather than be too elaborate. */ | |
4557 | ||
4558 | #define MAX_HASHLEN 20 | |
4559 | #define MAX_HASHBLOCKLEN 64 | |
4560 | ||
4561 | case EITEM_HMAC: | |
4562 | { | |
4563 | uschar *sub[3]; | |
4564 | md5 md5_base; | |
4565 | sha1 sha1_base; | |
4566 | void *use_base; | |
4567 | int type, i; | |
4568 | int hashlen; /* Number of octets for the hash algorithm's output */ | |
4569 | int hashblocklen; /* Number of octets the hash algorithm processes */ | |
4570 | uschar *keyptr, *p; | |
4571 | unsigned int keylen; | |
4572 | ||
4573 | uschar keyhash[MAX_HASHLEN]; | |
4574 | uschar innerhash[MAX_HASHLEN]; | |
4575 | uschar finalhash[MAX_HASHLEN]; | |
4576 | uschar finalhash_hex[2*MAX_HASHLEN]; | |
4577 | uschar innerkey[MAX_HASHBLOCKLEN]; | |
4578 | uschar outerkey[MAX_HASHBLOCKLEN]; | |
4579 | ||
4580 | switch (read_subs(sub, 3, 3, &s, skipping, TRUE, name)) | |
4581 | { | |
4582 | case 1: goto EXPAND_FAILED_CURLY; | |
4583 | case 2: | |
4584 | case 3: goto EXPAND_FAILED; | |
4585 | } | |
4586 | ||
4587 | if (Ustrcmp(sub[0], "md5") == 0) | |
4588 | { | |
4589 | type = HMAC_MD5; | |
4590 | use_base = &md5_base; | |
4591 | hashlen = 16; | |
4592 | hashblocklen = 64; | |
4593 | } | |
4594 | else if (Ustrcmp(sub[0], "sha1") == 0) | |
4595 | { | |
4596 | type = HMAC_SHA1; | |
4597 | use_base = &sha1_base; | |
4598 | hashlen = 20; | |
4599 | hashblocklen = 64; | |
4600 | } | |
4601 | else | |
4602 | { | |
4603 | expand_string_message = | |
4604 | string_sprintf("hmac algorithm \"%s\" is not recognised", sub[0]); | |
4605 | goto EXPAND_FAILED; | |
4606 | } | |
4607 | ||
4608 | keyptr = sub[1]; | |
4609 | keylen = Ustrlen(keyptr); | |
4610 | ||
4611 | /* If the key is longer than the hash block length, then hash the key | |
4612 | first */ | |
4613 | ||
4614 | if (keylen > hashblocklen) | |
4615 | { | |
4616 | chash_start(type, use_base); | |
4617 | chash_end(type, use_base, keyptr, keylen, keyhash); | |
4618 | keyptr = keyhash; | |
4619 | keylen = hashlen; | |
4620 | } | |
4621 | ||
4622 | /* Now make the inner and outer key values */ | |
4623 | ||
4624 | memset(innerkey, 0x36, hashblocklen); | |
4625 | memset(outerkey, 0x5c, hashblocklen); | |
4626 | ||
4627 | for (i = 0; i < keylen; i++) | |
4628 | { | |
4629 | innerkey[i] ^= keyptr[i]; | |
4630 | outerkey[i] ^= keyptr[i]; | |
4631 | } | |
4632 | ||
4633 | /* Now do the hashes */ | |
4634 | ||
4635 | chash_start(type, use_base); | |
4636 | chash_mid(type, use_base, innerkey); | |
4637 | chash_end(type, use_base, sub[2], Ustrlen(sub[2]), innerhash); | |
4638 | ||
4639 | chash_start(type, use_base); | |
4640 | chash_mid(type, use_base, outerkey); | |
4641 | chash_end(type, use_base, innerhash, hashlen, finalhash); | |
4642 | ||
4643 | /* Encode the final hash as a hex string */ | |
4644 | ||
4645 | p = finalhash_hex; | |
4646 | for (i = 0; i < hashlen; i++) | |
4647 | { | |
4648 | *p++ = hex_digits[(finalhash[i] & 0xf0) >> 4]; | |
4649 | *p++ = hex_digits[finalhash[i] & 0x0f]; | |
4650 | } | |
4651 | ||
4652 | DEBUG(D_any) debug_printf("HMAC[%s](%.*s,%.*s)=%.*s\n", sub[0], | |
4653 | (int)keylen, keyptr, Ustrlen(sub[2]), sub[2], hashlen*2, finalhash_hex); | |
4654 | ||
4655 | yield = string_cat(yield, &size, &ptr, finalhash_hex, hashlen*2); | |
4656 | } | |
4657 | ||
4658 | continue; | |
4659 | ||
4660 | /* Handle global substitution for "sg" - like Perl's s/xxx/yyy/g operator. | |
4661 | We have to save the numerical variables and restore them afterwards. */ | |
4662 | ||
4663 | case EITEM_SG: | |
4664 | { | |
4665 | const pcre *re; | |
4666 | int moffset, moffsetextra, slen; | |
4667 | int roffset; | |
4668 | int emptyopt; | |
4669 | const uschar *rerror; | |
4670 | uschar *subject; | |
4671 | uschar *sub[3]; | |
4672 | int save_expand_nmax = | |
4673 | save_expand_strings(save_expand_nstring, save_expand_nlength); | |
4674 | ||
4675 | switch(read_subs(sub, 3, 3, &s, skipping, TRUE, US"sg")) | |
4676 | { | |
4677 | case 1: goto EXPAND_FAILED_CURLY; | |
4678 | case 2: | |
4679 | case 3: goto EXPAND_FAILED; | |
4680 | } | |
4681 | ||
4682 | /* Compile the regular expression */ | |
4683 | ||
4684 | re = pcre_compile(CS sub[1], PCRE_COPT, (const char **)&rerror, &roffset, | |
4685 | NULL); | |
4686 | ||
4687 | if (re == NULL) | |
4688 | { | |
4689 | expand_string_message = string_sprintf("regular expression error in " | |
4690 | "\"%s\": %s at offset %d", sub[1], rerror, roffset); | |
4691 | goto EXPAND_FAILED; | |
4692 | } | |
4693 | ||
4694 | /* Now run a loop to do the substitutions as often as necessary. It ends | |
4695 | when there are no more matches. Take care over matches of the null string; | |
4696 | do the same thing as Perl does. */ | |
4697 | ||
4698 | subject = sub[0]; | |
4699 | slen = Ustrlen(sub[0]); | |
4700 | moffset = moffsetextra = 0; | |
4701 | emptyopt = 0; | |
4702 | ||
4703 | for (;;) | |
4704 | { | |
4705 | int ovector[3*(EXPAND_MAXN+1)]; | |
4706 | int n = pcre_exec(re, NULL, CS subject, slen, moffset + moffsetextra, | |
4707 | PCRE_EOPT | emptyopt, ovector, sizeof(ovector)/sizeof(int)); | |
4708 | int nn; | |
4709 | uschar *insert; | |
4710 | ||
4711 | /* No match - if we previously set PCRE_NOTEMPTY after a null match, this | |
4712 | is not necessarily the end. We want to repeat the match from one | |
4713 | character further along, but leaving the basic offset the same (for | |
4714 | copying below). We can't be at the end of the string - that was checked | |
4715 | before setting PCRE_NOTEMPTY. If PCRE_NOTEMPTY is not set, we are | |
4716 | finished; copy the remaining string and end the loop. */ | |
4717 | ||
4718 | if (n < 0) | |
4719 | { | |
4720 | if (emptyopt != 0) | |
4721 | { | |
4722 | moffsetextra = 1; | |
4723 | emptyopt = 0; | |
4724 | continue; | |
4725 | } | |
4726 | yield = string_cat(yield, &size, &ptr, subject+moffset, slen-moffset); | |
4727 | break; | |
4728 | } | |
4729 | ||
4730 | /* Match - set up for expanding the replacement. */ | |
4731 | ||
4732 | if (n == 0) n = EXPAND_MAXN + 1; | |
4733 | expand_nmax = 0; | |
4734 | for (nn = 0; nn < n*2; nn += 2) | |
4735 | { | |
4736 | expand_nstring[expand_nmax] = subject + ovector[nn]; | |
4737 | expand_nlength[expand_nmax++] = ovector[nn+1] - ovector[nn]; | |
4738 | } | |
4739 | expand_nmax--; | |
4740 | ||
4741 | /* Copy the characters before the match, plus the expanded insertion. */ | |
4742 | ||
4743 | yield = string_cat(yield, &size, &ptr, subject + moffset, | |
4744 | ovector[0] - moffset); | |
4745 | insert = expand_string(sub[2]); | |
4746 | if (insert == NULL) goto EXPAND_FAILED; | |
4747 | yield = string_cat(yield, &size, &ptr, insert, Ustrlen(insert)); | |
4748 | ||
4749 | moffset = ovector[1]; | |
4750 | moffsetextra = 0; | |
4751 | emptyopt = 0; | |
4752 | ||
4753 | /* If we have matched an empty string, first check to see if we are at | |
4754 | the end of the subject. If so, the loop is over. Otherwise, mimic | |
4755 | what Perl's /g options does. This turns out to be rather cunning. First | |
4756 | we set PCRE_NOTEMPTY and PCRE_ANCHORED and try the match a non-empty | |
4757 | string at the same point. If this fails (picked up above) we advance to | |
4758 | the next character. */ | |
4759 | ||
4760 | if (ovector[0] == ovector[1]) | |
4761 | { | |
4762 | if (ovector[0] == slen) break; | |
4763 | emptyopt = PCRE_NOTEMPTY | PCRE_ANCHORED; | |
4764 | } | |
4765 | } | |
4766 | ||
4767 | /* All done - restore numerical variables. */ | |
4768 | ||
4769 | restore_expand_strings(save_expand_nmax, save_expand_nstring, | |
4770 | save_expand_nlength); | |
4771 | continue; | |
4772 | } | |
4773 | ||
4774 | /* Handle keyed and numbered substring extraction. If the first argument | |
4775 | consists entirely of digits, then a numerical extraction is assumed. */ | |
4776 | ||
4777 | case EITEM_EXTRACT: | |
4778 | { | |
4779 | int i; | |
4780 | int j = 2; | |
4781 | int field_number = 1; | |
4782 | BOOL field_number_set = FALSE; | |
4783 | uschar *save_lookup_value = lookup_value; | |
4784 | uschar *sub[3]; | |
4785 | int save_expand_nmax = | |
4786 | save_expand_strings(save_expand_nstring, save_expand_nlength); | |
4787 | ||
4788 | /* Read the arguments */ | |
4789 | ||
4790 | for (i = 0; i < j; i++) | |
4791 | { | |
4792 | while (isspace(*s)) s++; | |
4793 | if (*s == '{') | |
4794 | { | |
4795 | sub[i] = expand_string_internal(s+1, TRUE, &s, skipping); | |
4796 | if (sub[i] == NULL) goto EXPAND_FAILED; | |
4797 | if (*s++ != '}') goto EXPAND_FAILED_CURLY; | |
4798 | ||
4799 | /* After removal of leading and trailing white space, the first | |
4800 | argument must not be empty; if it consists entirely of digits | |
4801 | (optionally preceded by a minus sign), this is a numerical | |
4802 | extraction, and we expect 3 arguments. */ | |
4803 | ||
4804 | if (i == 0) | |
4805 | { | |
4806 | int len; | |
4807 | int x = 0; | |
4808 | uschar *p = sub[0]; | |
4809 | ||
4810 | while (isspace(*p)) p++; | |
4811 | sub[0] = p; | |
4812 | ||
4813 | len = Ustrlen(p); | |
4814 | while (len > 0 && isspace(p[len-1])) len--; | |
4815 | p[len] = 0; | |
4816 | ||
e2803e40 | 4817 | if (*p == 0 && !skipping) |
059ec3d9 | 4818 | { |
554d2369 TF |
4819 | expand_string_message = US"first argument of \"extract\" must " |
4820 | "not be empty"; | |
059ec3d9 PH |
4821 | goto EXPAND_FAILED; |
4822 | } | |
4823 | ||
4824 | if (*p == '-') | |
4825 | { | |
4826 | field_number = -1; | |
4827 | p++; | |
4828 | } | |
4829 | while (*p != 0 && isdigit(*p)) x = x * 10 + *p++ - '0'; | |
4830 | if (*p == 0) | |
4831 | { | |
4832 | field_number *= x; | |
4833 | j = 3; /* Need 3 args */ | |
4834 | field_number_set = TRUE; | |
4835 | } | |
4836 | } | |
4837 | } | |
4838 | else goto EXPAND_FAILED_CURLY; | |
4839 | } | |
4840 | ||
4841 | /* Extract either the numbered or the keyed substring into $value. If | |
4842 | skipping, just pretend the extraction failed. */ | |
4843 | ||
4844 | lookup_value = skipping? NULL : field_number_set? | |
4845 | expand_gettokened(field_number, sub[1], sub[2]) : | |
4846 | expand_getkeyed(sub[0], sub[1]); | |
4847 | ||
4848 | /* If no string follows, $value gets substituted; otherwise there can | |
4849 | be yes/no strings, as for lookup or if. */ | |
4850 | ||
4851 | switch(process_yesno( | |
4852 | skipping, /* were previously skipping */ | |
4853 | lookup_value != NULL, /* success/failure indicator */ | |
4854 | save_lookup_value, /* value to reset for string2 */ | |
4855 | &s, /* input pointer */ | |
4856 | &yield, /* output pointer */ | |
4857 | &size, /* output size */ | |
4858 | &ptr, /* output current point */ | |
4859 | US"extract")) /* condition type */ | |
4860 | { | |
4861 | case 1: goto EXPAND_FAILED; /* when all is well, the */ | |
4862 | case 2: goto EXPAND_FAILED_CURLY; /* returned value is 0 */ | |
4863 | } | |
4864 | ||
4865 | /* All done - restore numerical variables. */ | |
4866 | ||
4867 | restore_expand_strings(save_expand_nmax, save_expand_nstring, | |
4868 | save_expand_nlength); | |
4869 | ||
4870 | continue; | |
4871 | } | |
1a46a8c5 PH |
4872 | |
4873 | ||
29f89cad PH |
4874 | /* Handle list operations */ |
4875 | ||
4876 | case EITEM_FILTER: | |
4877 | case EITEM_MAP: | |
4878 | case EITEM_REDUCE: | |
4879 | { | |
4880 | int sep = 0; | |
4881 | int save_ptr = ptr; | |
4882 | uschar outsep[2] = { '\0', '\0' }; | |
4883 | uschar *list, *expr, *temp; | |
4884 | uschar *save_iterate_item = iterate_item; | |
4885 | uschar *save_lookup_value = lookup_value; | |
4886 | ||
4887 | while (isspace(*s)) s++; | |
4888 | if (*s++ != '{') goto EXPAND_FAILED_CURLY; | |
4889 | ||
4890 | list = expand_string_internal(s, TRUE, &s, skipping); | |
4891 | if (list == NULL) goto EXPAND_FAILED; | |
4892 | if (*s++ != '}') goto EXPAND_FAILED_CURLY; | |
4893 | ||
4894 | if (item_type == EITEM_REDUCE) | |
4895 | { | |
4896 | while (isspace(*s)) s++; | |
4897 | if (*s++ != '{') goto EXPAND_FAILED_CURLY; | |
4898 | temp = expand_string_internal(s, TRUE, &s, skipping); | |
4899 | if (temp == NULL) goto EXPAND_FAILED; | |
4900 | lookup_value = temp; | |
4901 | if (*s++ != '}') goto EXPAND_FAILED_CURLY; | |
4902 | } | |
4903 | ||
4904 | while (isspace(*s)) s++; | |
4905 | if (*s++ != '{') goto EXPAND_FAILED_CURLY; | |
4906 | ||
4907 | expr = s; | |
4908 | ||
4909 | /* For EITEM_FILTER, call eval_condition once, with result discarded (as | |
4910 | if scanning a "false" part). This allows us to find the end of the | |
4911 | condition, because if the list is empty, we won't actually evaluate the | |
4912 | condition for real. For EITEM_MAP and EITEM_REDUCE, do the same, using | |
4913 | the normal internal expansion function. */ | |
4914 | ||
4915 | if (item_type == EITEM_FILTER) | |
4916 | { | |
4917 | temp = eval_condition(expr, NULL); | |
4918 | if (temp != NULL) s = temp; | |
4919 | } | |
4920 | else | |
4921 | { | |
4922 | temp = expand_string_internal(s, TRUE, &s, TRUE); | |
4923 | } | |
4924 | ||
4925 | if (temp == NULL) | |
4926 | { | |
4927 | expand_string_message = string_sprintf("%s inside \"%s\" item", | |
4928 | expand_string_message, name); | |
4929 | goto EXPAND_FAILED; | |
4930 | } | |
4931 | ||
4932 | while (isspace(*s)) s++; | |
4933 | if (*s++ != '}') | |
4934 | { | |
4935 | expand_string_message = string_sprintf("missing } at end of condition " | |
4936 | "or expression inside \"%s\"", name); | |
4937 | goto EXPAND_FAILED; | |
4938 | } | |
4939 | ||
4940 | while (isspace(*s)) s++; | |
4941 | if (*s++ != '}') | |
4942 | { | |
4943 | expand_string_message = string_sprintf("missing } at end of \"%s\"", | |
4944 | name); | |
4945 | goto EXPAND_FAILED; | |
4946 | } | |
4947 | ||
4948 | /* If we are skipping, we can now just move on to the next item. When | |
4949 | processing for real, we perform the iteration. */ | |
4950 | ||
4951 | if (skipping) continue; | |
4952 | while ((iterate_item = string_nextinlist(&list, &sep, NULL, 0)) != NULL) | |
4953 | { | |
4954 | *outsep = (uschar)sep; /* Separator as a string */ | |
4955 | ||
4956 | DEBUG(D_expand) debug_printf("%s: $item = \"%s\"\n", name, iterate_item); | |
4957 | ||
4958 | if (item_type == EITEM_FILTER) | |
4959 | { | |
4960 | BOOL condresult; | |
4961 | if (eval_condition(expr, &condresult) == NULL) | |
4962 | { | |
e58c13cc PH |
4963 | iterate_item = save_iterate_item; |
4964 | lookup_value = save_lookup_value; | |
29f89cad PH |
4965 | expand_string_message = string_sprintf("%s inside \"%s\" condition", |
4966 | expand_string_message, name); | |
4967 | goto EXPAND_FAILED; | |
4968 | } | |
4969 | DEBUG(D_expand) debug_printf("%s: condition is %s\n", name, | |
4970 | condresult? "true":"false"); | |
4971 | if (condresult) | |
4972 | temp = iterate_item; /* TRUE => include this item */ | |
4973 | else | |
4974 | continue; /* FALSE => skip this item */ | |
4975 | } | |
4976 | ||
4977 | /* EITEM_MAP and EITEM_REDUCE */ | |
4978 | ||
4979 | else | |
4980 | { | |
4981 | temp = expand_string_internal(expr, TRUE, NULL, skipping); | |
4982 | if (temp == NULL) | |
4983 | { | |
e58c13cc | 4984 | iterate_item = save_iterate_item; |
29f89cad PH |
4985 | expand_string_message = string_sprintf("%s inside \"%s\" item", |
4986 | expand_string_message, name); | |
4987 | goto EXPAND_FAILED; | |
4988 | } | |
4989 | if (item_type == EITEM_REDUCE) | |
4990 | { | |
4991 | lookup_value = temp; /* Update the value of $value */ | |
4992 | continue; /* and continue the iteration */ | |
4993 | } | |
4994 | } | |
4995 | ||
4996 | /* We reach here for FILTER if the condition is true, always for MAP, | |
4997 | and never for REDUCE. The value in "temp" is to be added to the output | |
4998 | list that is being created, ensuring that any occurrences of the | |
4999 | separator character are doubled. Unless we are dealing with the first | |
5000 | item of the output list, add in a space if the new item begins with the | |
5001 | separator character, or is an empty string. */ | |
5002 | ||
5003 | if (ptr != save_ptr && (temp[0] == *outsep || temp[0] == 0)) | |
5004 | yield = string_cat(yield, &size, &ptr, US" ", 1); | |
5005 | ||
5006 | /* Add the string in "temp" to the output list that we are building, | |
5007 | This is done in chunks by searching for the separator character. */ | |
5008 | ||
5009 | for (;;) | |
5010 | { | |
5011 | size_t seglen = Ustrcspn(temp, outsep); | |
5012 | yield = string_cat(yield, &size, &ptr, temp, seglen + 1); | |
5013 | ||
5014 | /* If we got to the end of the string we output one character | |
5015 | too many; backup and end the loop. Otherwise arrange to double the | |
5016 | separator. */ | |
5017 | ||
5018 | if (temp[seglen] == '\0') { ptr--; break; } | |
5019 | yield = string_cat(yield, &size, &ptr, outsep, 1); | |
5020 | temp += seglen + 1; | |
5021 | } | |
5022 | ||
5023 | /* Output a separator after the string: we will remove the redundant | |
5024 | final one at the end. */ | |
5025 | ||
5026 | yield = string_cat(yield, &size, &ptr, outsep, 1); | |
5027 | } /* End of iteration over the list loop */ | |
5028 | ||
5029 | /* REDUCE has generated no output above: output the final value of | |
5030 | $value. */ | |
5031 | ||
5032 | if (item_type == EITEM_REDUCE) | |
5033 | { | |
5034 | yield = string_cat(yield, &size, &ptr, lookup_value, | |
5035 | Ustrlen(lookup_value)); | |
5036 | lookup_value = save_lookup_value; /* Restore $value */ | |
5037 | } | |
5038 | ||
5039 | /* FILTER and MAP generate lists: if they have generated anything, remove | |
5040 | the redundant final separator. Even though an empty item at the end of a | |
5041 | list does not count, this is tidier. */ | |
5042 | ||
5043 | else if (ptr != save_ptr) ptr--; | |
5044 | ||
5045 | /* Restore preserved $item */ | |
5046 | ||
5047 | iterate_item = save_iterate_item; | |
5048 | continue; | |
5049 | } | |
5050 | ||
5051 | ||
1a46a8c5 PH |
5052 | /* If ${dlfunc support is configured, handle calling dynamically-loaded |
5053 | functions, unless locked out at this time. Syntax is ${dlfunc{file}{func}} | |
5054 | or ${dlfunc{file}{func}{arg}} or ${dlfunc{file}{func}{arg1}{arg2}} or up to | |
5055 | a maximum of EXPAND_DLFUNC_MAX_ARGS arguments (defined below). */ | |
5056 | ||
5057 | #define EXPAND_DLFUNC_MAX_ARGS 8 | |
5058 | ||
5059 | case EITEM_DLFUNC: | |
5060 | #ifndef EXPAND_DLFUNC | |
5061 | expand_string_message = US"\"${dlfunc\" encountered, but this facility " | |
5062 | "is not included in this binary"; | |
5063 | goto EXPAND_FAILED; | |
5064 | ||
5065 | #else /* EXPAND_DLFUNC */ | |
5066 | { | |
5067 | tree_node *t; | |
5068 | exim_dlfunc_t *func; | |
5069 | uschar *result; | |
5070 | int status, argc; | |
5071 | uschar *argv[EXPAND_DLFUNC_MAX_ARGS + 3]; | |
5072 | ||
5073 | if ((expand_forbid & RDO_DLFUNC) != 0) | |
5074 | { | |
5075 | expand_string_message = | |
5076 | US"dynamically-loaded functions are not permitted"; | |
5077 | goto EXPAND_FAILED; | |
5078 | } | |
5079 | ||
5080 | switch(read_subs(argv, EXPAND_DLFUNC_MAX_ARGS + 2, 2, &s, skipping, | |
5081 | TRUE, US"dlfunc")) | |
5082 | { | |
5083 | case 1: goto EXPAND_FAILED_CURLY; | |
5084 | case 2: | |
5085 | case 3: goto EXPAND_FAILED; | |
5086 | } | |
5087 | ||
5088 | /* If skipping, we don't actually do anything */ | |
5089 | ||
5090 | if (skipping) continue; | |
5091 | ||
5092 | /* Look up the dynamically loaded object handle in the tree. If it isn't | |
5093 | found, dlopen() the file and put the handle in the tree for next time. */ | |
5094 | ||
5095 | t = tree_search(dlobj_anchor, argv[0]); | |
5096 | if (t == NULL) | |
5097 | { | |
5098 | void *handle = dlopen(CS argv[0], RTLD_LAZY); | |
5099 | if (handle == NULL) | |
5100 | { | |
5101 | expand_string_message = string_sprintf("dlopen \"%s\" failed: %s", | |
5102 | argv[0], dlerror()); | |
5103 | log_write(0, LOG_MAIN|LOG_PANIC, "%s", expand_string_message); | |
5104 | goto EXPAND_FAILED; | |
5105 | } | |
5106 | t = store_get_perm(sizeof(tree_node) + Ustrlen(argv[0])); | |
5107 | Ustrcpy(t->name, argv[0]); | |
5108 | t->data.ptr = handle; | |
5109 | (void)tree_insertnode(&dlobj_anchor, t); | |
5110 | } | |
5111 | ||
5112 | /* Having obtained the dynamically loaded object handle, look up the | |
5113 | function pointer. */ | |
5114 | ||
5115 | func = (exim_dlfunc_t *)dlsym(t->data.ptr, CS argv[1]); | |
5116 | if (func == NULL) | |
5117 | { | |
5118 | expand_string_message = string_sprintf("dlsym \"%s\" in \"%s\" failed: " | |
5119 | "%s", argv[1], argv[0], dlerror()); | |
7dbf77c9 | 5120 | log_write(0, LOG_MAIN|LOG_PANIC, "%s", expand_string_message); |
1a46a8c5 PH |
5121 | goto EXPAND_FAILED; |
5122 | } | |
5123 | ||
5124 | /* Call the function and work out what to do with the result. If it | |
5125 | returns OK, we have a replacement string; if it returns DEFER then | |
5126 | expansion has failed in a non-forced manner; if it returns FAIL then | |
5127 | failure was forced; if it returns ERROR or any other value there's a | |
d6b4d938 TF |
5128 | problem, so panic slightly. In any case, assume that the function has |
5129 | side-effects on the store that must be preserved. */ | |
1a46a8c5 | 5130 | |
d6b4d938 | 5131 | resetok = FALSE; |
1a46a8c5 PH |
5132 | result = NULL; |
5133 | for (argc = 0; argv[argc] != NULL; argc++); | |
5134 | status = func(&result, argc - 2, &argv[2]); | |
5135 | if(status == OK) | |
5136 | { | |
5137 | if (result == NULL) result = US""; | |
5138 | yield = string_cat(yield, &size, &ptr, result, Ustrlen(result)); | |
5139 | continue; | |
5140 | } | |
5141 | else | |
5142 | { | |
5143 | expand_string_message = result == NULL ? US"(no message)" : result; | |
5144 | if(status == FAIL_FORCED) expand_string_forcedfail = TRUE; | |
5145 | else if(status != FAIL) | |
5146 | log_write(0, LOG_MAIN|LOG_PANIC, "dlfunc{%s}{%s} failed (%d): %s", | |
5147 | argv[0], argv[1], status, expand_string_message); | |
5148 | goto EXPAND_FAILED; | |
5149 | } | |
5150 | } | |
5151 | #endif /* EXPAND_DLFUNC */ | |
059ec3d9 PH |
5152 | } |
5153 | ||
5154 | /* Control reaches here if the name is not recognized as one of the more | |
5155 | complicated expansion items. Check for the "operator" syntax (name terminated | |
5156 | by a colon). Some of the operators have arguments, separated by _ from the | |
5157 | name. */ | |
5158 | ||
5159 | if (*s == ':') | |
5160 | { | |
5161 | int c; | |
5162 | uschar *arg = NULL; | |
5163 | uschar *sub = expand_string_internal(s+1, TRUE, &s, skipping); | |
5164 | if (sub == NULL) goto EXPAND_FAILED; | |
5165 | s++; | |
5166 | ||
5167 | /* Owing to an historical mis-design, an underscore may be part of the | |
5168 | operator name, or it may introduce arguments. We therefore first scan the | |
5169 | table of names that contain underscores. If there is no match, we cut off | |
5170 | the arguments and then scan the main table. */ | |
5171 | ||
5172 | c = chop_match(name, op_table_underscore, | |
5173 | sizeof(op_table_underscore)/sizeof(uschar *)); | |
5174 | ||
5175 | if (c < 0) | |
5176 | { | |
5177 | arg = Ustrchr(name, '_'); | |
5178 | if (arg != NULL) *arg = 0; | |
5179 | c = chop_match(name, op_table_main, | |
5180 | sizeof(op_table_main)/sizeof(uschar *)); | |
5181 | if (c >= 0) c += sizeof(op_table_underscore)/sizeof(uschar *); | |
5182 | if (arg != NULL) *arg++ = '_'; /* Put back for error messages */ | |
5183 | } | |
5184 | ||
5185 | /* If we are skipping, we don't need to perform the operation at all. | |
5186 | This matters for operations like "mask", because the data may not be | |
5187 | in the correct format when skipping. For example, the expression may test | |
5188 | for the existence of $sender_host_address before trying to mask it. For | |
5189 | other operations, doing them may not fail, but it is a waste of time. */ | |
5190 | ||
5191 | if (skipping && c >= 0) continue; | |
5192 | ||
5193 | /* Otherwise, switch on the operator type */ | |
5194 | ||
5195 | switch(c) | |
5196 | { | |
5197 | case EOP_BASE62: | |
5198 | { | |
5199 | uschar *t; | |
5200 | unsigned long int n = Ustrtoul(sub, &t, 10); | |
5201 | if (*t != 0) | |
5202 | { | |
5203 | expand_string_message = string_sprintf("argument for base62 " | |
5204 | "operator is \"%s\", which is not a decimal number", sub); | |
5205 | goto EXPAND_FAILED; | |
5206 | } | |
5207 | t = string_base62(n); | |
5208 | yield = string_cat(yield, &size, &ptr, t, Ustrlen(t)); | |
5209 | continue; | |
5210 | } | |
5211 | ||
9a799bc0 PH |
5212 | /* Note that for Darwin and Cygwin, BASE_62 actually has the value 36 */ |
5213 | ||
059ec3d9 PH |
5214 | case EOP_BASE62D: |
5215 | { | |
5216 | uschar buf[16]; | |
5217 | uschar *tt = sub; | |
5218 | unsigned long int n = 0; | |
5219 | while (*tt != 0) | |
5220 | { | |
5221 | uschar *t = Ustrchr(base62_chars, *tt++); | |
5222 | if (t == NULL) | |
5223 | { | |
5224 | expand_string_message = string_sprintf("argument for base62d " | |
9a799bc0 PH |
5225 | "operator is \"%s\", which is not a base %d number", sub, |
5226 | BASE_62); | |
059ec3d9 PH |
5227 | goto EXPAND_FAILED; |
5228 | } | |
9a799bc0 | 5229 | n = n * BASE_62 + (t - base62_chars); |
059ec3d9 PH |
5230 | } |
5231 | (void)sprintf(CS buf, "%ld", n); | |
5232 | yield = string_cat(yield, &size, &ptr, buf, Ustrlen(buf)); | |
5233 | continue; | |
5234 | } | |
5235 | ||
5236 | case EOP_EXPAND: | |
5237 | { | |
5238 | uschar *expanded = expand_string_internal(sub, FALSE, NULL, skipping); | |
5239 | if (expanded == NULL) | |
5240 | { | |
5241 | expand_string_message = | |
5242 | string_sprintf("internal expansion of \"%s\" failed: %s", sub, | |
5243 | expand_string_message); | |
5244 | goto EXPAND_FAILED; | |
5245 | } | |
5246 | yield = string_cat(yield, &size, &ptr, expanded, Ustrlen(expanded)); | |
5247 | continue; | |
5248 | } | |
5249 | ||
5250 | case EOP_LC: | |
5251 | { | |
5252 | int count = 0; | |
5253 | uschar *t = sub - 1; | |
5254 | while (*(++t) != 0) { *t = tolower(*t); count++; } | |
5255 | yield = string_cat(yield, &size, &ptr, sub, count); | |
5256 | continue; | |
5257 | } | |
5258 | ||
5259 | case EOP_UC: | |
5260 | { | |
5261 | int count = 0; | |
5262 | uschar *t = sub - 1; | |
5263 | while (*(++t) != 0) { *t = toupper(*t); count++; } | |
5264 | yield = string_cat(yield, &size, &ptr, sub, count); | |
5265 | continue; | |
5266 | } | |
5267 | ||
5268 | case EOP_MD5: | |
5269 | { | |
5270 | md5 base; | |
5271 | uschar digest[16]; | |
5272 | int j; | |
5273 | char st[33]; | |
5274 | md5_start(&base); | |
5275 | md5_end(&base, sub, Ustrlen(sub), digest); | |
5276 | for(j = 0; j < 16; j++) sprintf(st+2*j, "%02x", digest[j]); | |
5277 | yield = string_cat(yield, &size, &ptr, US st, (int)strlen(st)); | |
5278 | continue; | |
5279 | } | |
5280 | ||
5281 | case EOP_SHA1: | |
5282 | { | |
5283 | sha1 base; | |
5284 | uschar digest[20]; | |
5285 | int j; | |
5286 | char st[41]; | |
5287 | sha1_start(&base); | |
5288 | sha1_end(&base, sub, Ustrlen(sub), digest); | |
5289 | for(j = 0; j < 20; j++) sprintf(st+2*j, "%02X", digest[j]); | |
5290 | yield = string_cat(yield, &size, &ptr, US st, (int)strlen(st)); | |
5291 | continue; | |
5292 | } | |
5293 | ||
5294 | /* Convert hex encoding to base64 encoding */ | |
5295 | ||
5296 | case EOP_HEX2B64: | |
5297 | { | |
5298 | int c = 0; | |
5299 | int b = -1; | |
5300 | uschar *in = sub; | |
5301 | uschar *out = sub; | |
5302 | uschar *enc; | |
5303 | ||
5304 | for (enc = sub; *enc != 0; enc++) | |
5305 | { | |
5306 | if (!isxdigit(*enc)) | |
5307 | { | |
5308 | expand_string_message = string_sprintf("\"%s\" is not a hex " | |
5309 | "string", sub); | |
5310 | goto EXPAND_FAILED; | |
5311 | } | |
5312 | c++; | |
5313 | } | |
5314 | ||
5315 | if ((c & 1) != 0) | |
5316 | { | |
5317 | expand_string_message = string_sprintf("\"%s\" contains an odd " | |
5318 | "number of characters", sub); | |
5319 | goto EXPAND_FAILED; | |
5320 | } | |
5321 | ||
5322 | while ((c = *in++) != 0) | |
5323 | { | |
5324 | if (isdigit(c)) c -= '0'; | |
5325 | else c = toupper(c) - 'A' + 10; | |
5326 | if (b == -1) | |
5327 | { | |
5328 | b = c << 4; | |
5329 | } | |
5330 | else | |
5331 | { | |
5332 | *out++ = b | c; | |
5333 | b = -1; | |
5334 | } | |
5335 | } | |
5336 | ||
5337 | enc = auth_b64encode(sub, out - sub); | |
5338 | yield = string_cat(yield, &size, &ptr, enc, Ustrlen(enc)); | |
5339 | continue; | |
5340 | } | |
5341 | ||
5342 | /* mask applies a mask to an IP address; for example the result of | |
5343 | ${mask:131.111.10.206/28} is 131.111.10.192/28. */ | |
5344 | ||
5345 | case EOP_MASK: | |
5346 | { | |
5347 | int count; | |
5348 | uschar *endptr; | |
5349 | int binary[4]; | |
5350 | int mask, maskoffset; | |
5351 | int type = string_is_ip_address(sub, &maskoffset); | |
5352 | uschar buffer[64]; | |
5353 | ||
5354 | if (type == 0) | |
5355 | { | |
5356 | expand_string_message = string_sprintf("\"%s\" is not an IP address", | |
5357 | sub); | |
5358 | goto EXPAND_FAILED; | |
5359 | } | |
5360 | ||
5361 | if (maskoffset == 0) | |
5362 | { | |
5363 | expand_string_message = string_sprintf("missing mask value in \"%s\"", | |
5364 | sub); | |
5365 | goto EXPAND_FAILED; | |
5366 | } | |
5367 | ||
5368 | mask = Ustrtol(sub + maskoffset + 1, &endptr, 10); | |
5369 | ||
5370 | if (*endptr != 0 || mask < 0 || mask > ((type == 4)? 32 : 128)) | |
5371 | { | |
5372 | expand_string_message = string_sprintf("mask value too big in \"%s\"", | |
5373 | sub); | |
5374 | goto EXPAND_FAILED; | |
5375 | } | |
5376 | ||
5377 | /* Convert the address to binary integer(s) and apply the mask */ | |
5378 | ||
5379 | sub[maskoffset] = 0; | |
5380 | count = host_aton(sub, binary); | |
5381 | host_mask(count, binary, mask); | |
5382 | ||
5383 | /* Convert to masked textual format and add to output. */ | |
5384 | ||
5385 | yield = string_cat(yield, &size, &ptr, buffer, | |
6f0c9a4f | 5386 | host_nmtoa(count, binary, mask, buffer, '.')); |
059ec3d9 PH |
5387 | continue; |
5388 | } | |
5389 | ||
5390 | case EOP_ADDRESS: | |
5391 | case EOP_LOCAL_PART: | |
5392 | case EOP_DOMAIN: | |
5393 | { | |
5394 | uschar *error; | |
5395 | int start, end, domain; | |
5396 | uschar *t = parse_extract_address(sub, &error, &start, &end, &domain, | |
5397 | FALSE); | |
5398 | if (t != NULL) | |
5399 | { | |
5400 | if (c != EOP_DOMAIN) | |
5401 | { | |
5402 | if (c == EOP_LOCAL_PART && domain != 0) end = start + domain - 1; | |
5403 | yield = string_cat(yield, &size, &ptr, sub+start, end-start); | |
5404 | } | |
5405 | else if (domain != 0) | |
5406 | { | |
5407 | domain += start; | |
5408 | yield = string_cat(yield, &size, &ptr, sub+domain, end-domain); | |
5409 | } | |
5410 | } | |
5411 | continue; | |
5412 | } | |
5413 | ||
29f89cad PH |
5414 | case EOP_ADDRESSES: |
5415 | { | |
5416 | uschar outsep[2] = { ':', '\0' }; | |
5417 | uschar *address, *error; | |
5418 | int save_ptr = ptr; | |
5419 | int start, end, domain; /* Not really used */ | |
5420 | ||
5421 | while (isspace(*sub)) sub++; | |
5422 | if (*sub == '>') { *outsep = *++sub; ++sub; } | |
5423 | parse_allow_group = TRUE; | |
5424 | ||
5425 | for (;;) | |
5426 | { | |
5427 | uschar *p = parse_find_address_end(sub, FALSE); | |
5428 | uschar saveend = *p; | |
5429 | *p = '\0'; | |
5430 | address = parse_extract_address(sub, &error, &start, &end, &domain, | |
5431 | FALSE); | |
5432 | *p = saveend; | |
5433 | ||
5434 | /* Add the address to the output list that we are building. This is | |
5435 | done in chunks by searching for the separator character. At the | |
5436 | start, unless we are dealing with the first address of the output | |
5437 | list, add in a space if the new address begins with the separator | |
5438 | character, or is an empty string. */ | |
5439 | ||
5440 | if (address != NULL) | |
5441 | { | |
5442 | if (ptr != save_ptr && address[0] == *outsep) | |
5443 | yield = string_cat(yield, &size, &ptr, US" ", 1); | |
5444 | ||
5445 | for (;;) | |
5446 | { | |
5447 | size_t seglen = Ustrcspn(address, outsep); | |
5448 | yield = string_cat(yield, &size, &ptr, address, seglen + 1); | |
5449 | ||
5450 | /* If we got to the end of the string we output one character | |
5451 | too many. */ | |
5452 | ||
5453 | if (address[seglen] == '\0') { ptr--; break; } | |
5454 | yield = string_cat(yield, &size, &ptr, outsep, 1); | |
5455 | address += seglen + 1; | |
5456 | } | |
5457 | ||
5458 | /* Output a separator after the string: we will remove the | |
5459 | redundant final one at the end. */ | |
5460 | ||
5461 | yield = string_cat(yield, &size, &ptr, outsep, 1); | |
5462 | } | |
5463 | ||
5464 | if (saveend == '\0') break; | |
5465 | sub = p + 1; | |
5466 | } | |
5467 | ||
5468 | /* If we have generated anything, remove the redundant final | |
5469 | separator. */ | |
5470 | ||
5471 | if (ptr != save_ptr) ptr--; | |
5472 | parse_allow_group = FALSE; | |
5473 | continue; | |
5474 | } | |
5475 | ||
5476 | ||
059ec3d9 PH |
5477 | /* quote puts a string in quotes if it is empty or contains anything |
5478 | other than alphamerics, underscore, dot, or hyphen. | |
5479 | ||
5480 | quote_local_part puts a string in quotes if RFC 2821/2822 requires it to | |
5481 | be quoted in order to be a valid local part. | |
5482 | ||
5483 | In both cases, newlines and carriage returns are converted into \n and \r | |
5484 | respectively */ | |
5485 | ||
5486 | case EOP_QUOTE: | |
5487 | case EOP_QUOTE_LOCAL_PART: | |
5488 | if (arg == NULL) | |
5489 | { | |
5490 | BOOL needs_quote = (*sub == 0); /* TRUE for empty string */ | |
5491 | uschar *t = sub - 1; | |
5492 | ||
5493 | if (c == EOP_QUOTE) | |
5494 | { | |
5495 | while (!needs_quote && *(++t) != 0) | |
5496 | needs_quote = !isalnum(*t) && !strchr("_-.", *t); | |
5497 | } | |
5498 | else /* EOP_QUOTE_LOCAL_PART */ | |
5499 | { | |
5500 | while (!needs_quote && *(++t) != 0) | |
5501 | needs_quote = !isalnum(*t) && | |
5502 | strchr("!#$%&'*+-/=?^_`{|}~", *t) == NULL && | |
5503 | (*t != '.' || t == sub || t[1] == 0); | |
5504 | } | |
5505 | ||
5506 | if (needs_quote) | |
5507 | { | |
5508 | yield = string_cat(yield, &size, &ptr, US"\"", 1); | |
5509 | t = sub - 1; | |
5510 | while (*(++t) != 0) | |
5511 | { | |
5512 | if (*t == '\n') | |
5513 | yield = string_cat(yield, &size, &ptr, US"\\n", 2); | |
5514 | else if (*t == '\r') | |
5515 | yield = string_cat(yield, &size, &ptr, US"\\r", 2); | |
5516 | else | |
5517 | { | |
5518 | if (*t == '\\' || *t == '"') | |
5519 | yield = string_cat(yield, &size, &ptr, US"\\", 1); | |
5520 | yield = string_cat(yield, &size, &ptr, t, 1); | |
5521 | } | |
5522 | } | |
5523 | yield = string_cat(yield, &size, &ptr, US"\"", 1); | |
5524 | } | |
5525 | else yield = string_cat(yield, &size, &ptr, sub, Ustrlen(sub)); | |
5526 | continue; | |
5527 | } | |
5528 | ||
5529 | /* quote_lookuptype does lookup-specific quoting */ | |
5530 | ||
5531 | else | |
5532 | { | |
5533 | int n; | |
5534 | uschar *opt = Ustrchr(arg, '_'); | |
5535 | ||
5536 | if (opt != NULL) *opt++ = 0; | |
5537 | ||
5538 | n = search_findtype(arg, Ustrlen(arg)); | |
5539 | if (n < 0) | |
5540 | { | |
5541 | expand_string_message = search_error_message; | |
5542 | goto EXPAND_FAILED; | |
5543 | } | |
5544 | ||
e6d225ae DW |
5545 | if (lookup_list[n]->quote != NULL) |
5546 | sub = (lookup_list[n]->quote)(sub, opt); | |
059ec3d9 PH |
5547 | else if (opt != NULL) sub = NULL; |
5548 | ||
5549 | if (sub == NULL) | |
5550 | { | |
5551 | expand_string_message = string_sprintf( | |
5552 | "\"%s\" unrecognized after \"${quote_%s\"", | |
5553 | opt, arg); | |
5554 | goto EXPAND_FAILED; | |
5555 | } | |
5556 | ||
5557 | yield = string_cat(yield, &size, &ptr, sub, Ustrlen(sub)); | |
5558 | continue; | |
5559 | } | |
5560 | ||
5561 | /* rx quote sticks in \ before any non-alphameric character so that | |
5562 | the insertion works in a regular expression. */ | |
5563 | ||
5564 | case EOP_RXQUOTE: | |
5565 | { | |
5566 | uschar *t = sub - 1; | |
5567 | while (*(++t) != 0) | |
5568 | { | |
5569 | if (!isalnum(*t)) | |
5570 | yield = string_cat(yield, &size, &ptr, US"\\", 1); | |
5571 | yield = string_cat(yield, &size, &ptr, t, 1); | |
5572 | } | |
5573 | continue; | |
5574 | } | |
5575 | ||
5576 | /* RFC 2047 encodes, assuming headers_charset (default ISO 8859-1) as | |
5577 | prescribed by the RFC, if there are characters that need to be encoded */ | |
5578 | ||
5579 | case EOP_RFC2047: | |
5580 | { | |
14702f5b | 5581 | uschar buffer[2048]; |
059ec3d9 | 5582 | uschar *string = parse_quote_2047(sub, Ustrlen(sub), headers_charset, |
46218253 | 5583 | buffer, sizeof(buffer), FALSE); |
059ec3d9 PH |
5584 | yield = string_cat(yield, &size, &ptr, string, Ustrlen(string)); |
5585 | continue; | |
5586 | } | |
5587 | ||
9c57cbc0 PH |
5588 | /* RFC 2047 decode */ |
5589 | ||
5590 | case EOP_RFC2047D: | |
5591 | { | |
5592 | int len; | |
5593 | uschar *error; | |
5594 | uschar *decoded = rfc2047_decode(sub, check_rfc2047_length, | |
5595 | headers_charset, '?', &len, &error); | |
5596 | if (error != NULL) | |
5597 | { | |
5598 | expand_string_message = error; | |
5599 | goto EXPAND_FAILED; | |
5600 | } | |
5601 | yield = string_cat(yield, &size, &ptr, decoded, len); | |
5602 | continue; | |
5603 | } | |
5604 | ||
059ec3d9 PH |
5605 | /* from_utf8 converts UTF-8 to 8859-1, turning non-existent chars into |
5606 | underscores */ | |
5607 | ||
5608 | case EOP_FROM_UTF8: | |
5609 | { | |
5610 | while (*sub != 0) | |
5611 | { | |
5612 | int c; | |
5613 | uschar buff[4]; | |
5614 | GETUTF8INC(c, sub); | |
5615 | if (c > 255) c = '_'; | |
5616 | buff[0] = c; | |
5617 | yield = string_cat(yield, &size, &ptr, buff, 1); | |
5618 | } | |
5619 | continue; | |
5620 | } | |
5621 | ||
5622 | /* escape turns all non-printing characters into escape sequences. */ | |
5623 | ||
5624 | case EOP_ESCAPE: | |
5625 | { | |
5626 | uschar *t = string_printing(sub); | |
5627 | yield = string_cat(yield, &size, &ptr, t, Ustrlen(t)); | |
5628 | continue; | |
5629 | } | |
5630 | ||
5631 | /* Handle numeric expression evaluation */ | |
5632 | ||
5633 | case EOP_EVAL: | |
5634 | case EOP_EVAL10: | |
5635 | { | |
5636 | uschar *save_sub = sub; | |
5637 | uschar *error = NULL; | |
5638 | int n = eval_expr(&sub, (c == EOP_EVAL10), &error, FALSE); | |
5639 | if (error != NULL) | |
5640 | { | |
5641 | expand_string_message = string_sprintf("error in expression " | |
5642 | "evaluation: %s (after processing \"%.*s\")", error, sub-save_sub, | |
5643 | save_sub); | |
5644 | goto EXPAND_FAILED; | |
5645 | } | |
5646 | sprintf(CS var_buffer, "%d", n); | |
5647 | yield = string_cat(yield, &size, &ptr, var_buffer, Ustrlen(var_buffer)); | |
5648 | continue; | |
5649 | } | |
5650 | ||
5651 | /* Handle time period formating */ | |
5652 | ||
f90d018c PH |
5653 | case EOP_TIME_EVAL: |
5654 | { | |
5655 | int n = readconf_readtime(sub, 0, FALSE); | |
5656 | if (n < 0) | |
5657 | { | |
5658 | expand_string_message = string_sprintf("string \"%s\" is not an " | |
5659 | "Exim time interval in \"%s\" operator", sub, name); | |
5660 | goto EXPAND_FAILED; | |
5661 | } | |
5662 | sprintf(CS var_buffer, "%d", n); | |
5663 | yield = string_cat(yield, &size, &ptr, var_buffer, Ustrlen(var_buffer)); | |
5664 | continue; | |
5665 | } | |
5666 | ||
059ec3d9 PH |
5667 | case EOP_TIME_INTERVAL: |
5668 | { | |
5669 | int n; | |
5670 | uschar *t = read_number(&n, sub); | |
5671 | if (*t != 0) /* Not A Number*/ | |
5672 | { | |
5673 | expand_string_message = string_sprintf("string \"%s\" is not a " | |
5674 | "positive number in \"%s\" operator", sub, name); | |
5675 | goto EXPAND_FAILED; | |
5676 | } | |
5677 | t = readconf_printtime(n); | |
5678 | yield = string_cat(yield, &size, &ptr, t, Ustrlen(t)); | |
5679 | continue; | |
5680 | } | |
5681 | ||
5682 | /* Convert string to base64 encoding */ | |
5683 | ||
5684 | case EOP_STR2B64: | |
5685 | { | |
5686 | uschar *encstr = auth_b64encode(sub, Ustrlen(sub)); | |
5687 | yield = string_cat(yield, &size, &ptr, encstr, Ustrlen(encstr)); | |
5688 | continue; | |
5689 | } | |
5690 | ||
5691 | /* strlen returns the length of the string */ | |
5692 | ||
5693 | case EOP_STRLEN: | |
5694 | { | |
5695 | uschar buff[24]; | |
5696 | (void)sprintf(CS buff, "%d", Ustrlen(sub)); | |
5697 | yield = string_cat(yield, &size, &ptr, buff, Ustrlen(buff)); | |
5698 | continue; | |
5699 | } | |
5700 | ||
5701 | /* length_n or l_n takes just the first n characters or the whole string, | |
5702 | whichever is the shorter; | |
5703 | ||
5704 | substr_m_n, and s_m_n take n characters from offset m; negative m take | |
5705 | from the end; l_n is synonymous with s_0_n. If n is omitted in substr it | |
5706 | takes the rest, either to the right or to the left. | |
5707 | ||
5708 | hash_n or h_n makes a hash of length n from the string, yielding n | |
5709 | characters from the set a-z; hash_n_m makes a hash of length n, but | |
5710 | uses m characters from the set a-zA-Z0-9. | |
5711 | ||
5712 | nhash_n returns a single number between 0 and n-1 (in text form), while | |
5713 | nhash_n_m returns a div/mod hash as two numbers "a/b". The first lies | |
5714 | between 0 and n-1 and the second between 0 and m-1. */ | |
5715 | ||
5716 | case EOP_LENGTH: | |
5717 | case EOP_L: | |
5718 | case EOP_SUBSTR: | |
5719 | case EOP_S: | |
5720 | case EOP_HASH: | |
5721 | case EOP_H: | |
5722 | case EOP_NHASH: | |
5723 | case EOP_NH: | |
5724 | { | |
5725 | int sign = 1; | |
5726 | int value1 = 0; | |
5727 | int value2 = -1; | |
5728 | int *pn; | |
5729 | int len; | |
5730 | uschar *ret; | |
5731 | ||
5732 | if (arg == NULL) | |
5733 | { | |
5734 | expand_string_message = string_sprintf("missing values after %s", | |
5735 | name); | |
5736 | goto EXPAND_FAILED; | |
5737 | } | |
5738 | ||
5739 | /* "length" has only one argument, effectively being synonymous with | |
5740 | substr_0_n. */ | |
5741 | ||
5742 | if (c == EOP_LENGTH || c == EOP_L) | |
5743 | { | |
5744 | pn = &value2; | |
5745 | value2 = 0; | |
5746 | } | |
5747 | ||
5748 | /* The others have one or two arguments; for "substr" the first may be | |
5749 | negative. The second being negative means "not supplied". */ | |
5750 | ||
5751 | else | |
5752 | { | |
5753 | pn = &value1; | |
5754 | if (name[0] == 's' && *arg == '-') { sign = -1; arg++; } | |
5755 | } | |
5756 | ||
5757 | /* Read up to two numbers, separated by underscores */ | |
5758 | ||
5759 | ret = arg; | |
5760 | while (*arg != 0) | |
5761 | { | |
5762 | if (arg != ret && *arg == '_' && pn == &value1) | |
5763 | { | |
5764 | pn = &value2; | |
5765 | value2 = 0; | |
5766 | if (arg[1] != 0) arg++; | |
5767 | } | |
5768 | else if (!isdigit(*arg)) | |
5769 | { | |
5770 | expand_string_message = | |
5771 | string_sprintf("non-digit after underscore in \"%s\"", name); | |
5772 | goto EXPAND_FAILED; | |
5773 | } | |
5774 | else *pn = (*pn)*10 + *arg++ - '0'; | |
5775 | } | |
5776 | value1 *= sign; | |
5777 | ||
5778 | /* Perform the required operation */ | |
5779 | ||
5780 | ret = | |
5781 | (c == EOP_HASH || c == EOP_H)? | |
5782 | compute_hash(sub, value1, value2, &len) : | |
5783 | (c == EOP_NHASH || c == EOP_NH)? | |
5784 | compute_nhash(sub, value1, value2, &len) : | |
5785 | extract_substr(sub, value1, value2, &len); | |
5786 | ||
5787 | if (ret == NULL) goto EXPAND_FAILED; | |
5788 | yield = string_cat(yield, &size, &ptr, ret, len); | |
5789 | continue; | |
5790 | } | |
5791 | ||
5792 | /* Stat a path */ | |
5793 | ||
5794 | case EOP_STAT: | |
5795 | { | |
5796 | uschar *s; | |
5797 | uschar smode[12]; | |
5798 | uschar **modetable[3]; | |
5799 | int i; | |
5800 | mode_t mode; | |
5801 | struct stat st; | |
5802 | ||
254e032f PH |
5803 | if ((expand_forbid & RDO_EXISTS) != 0) |
5804 | { | |
5805 | expand_string_message = US"Use of the stat() expansion is not permitted"; | |
5806 | goto EXPAND_FAILED; | |
5807 | } | |
5808 | ||
059ec3d9 PH |
5809 | if (stat(CS sub, &st) < 0) |
5810 | { | |
5811 | expand_string_message = string_sprintf("stat(%s) failed: %s", | |
5812 | sub, strerror(errno)); | |
5813 | goto EXPAND_FAILED; | |
5814 | } | |
5815 | mode = st.st_mode; | |
5816 | switch (mode & S_IFMT) | |
5817 | { | |
5818 | case S_IFIFO: smode[0] = 'p'; break; | |
5819 | case S_IFCHR: smode[0] = 'c'; break; | |
5820 | case S_IFDIR: smode[0] = 'd'; break; | |
5821 | case S_IFBLK: smode[0] = 'b'; break; | |
5822 | case S_IFREG: smode[0] = '-'; break; | |
5823 | default: smode[0] = '?'; break; | |
5824 | } | |
5825 | ||
5826 | modetable[0] = ((mode & 01000) == 0)? mtable_normal : mtable_sticky; | |
5827 | modetable[1] = ((mode & 02000) == 0)? mtable_normal : mtable_setid; | |
5828 | modetable[2] = ((mode & 04000) == 0)? mtable_normal : mtable_setid; | |
5829 | ||
5830 | for (i = 0; i < 3; i++) | |
5831 | { | |
5832 | memcpy(CS(smode + 7 - i*3), CS(modetable[i][mode & 7]), 3); | |
5833 | mode >>= 3; | |
5834 | } | |
5835 | ||
5836 | smode[10] = 0; | |
5837 | s = string_sprintf("mode=%04lo smode=%s inode=%ld device=%ld links=%ld " | |
b1c749bb | 5838 | "uid=%ld gid=%ld size=" OFF_T_FMT " atime=%ld mtime=%ld ctime=%ld", |
059ec3d9 PH |
5839 | (long)(st.st_mode & 077777), smode, (long)st.st_ino, |
5840 | (long)st.st_dev, (long)st.st_nlink, (long)st.st_uid, | |
b1c749bb | 5841 | (long)st.st_gid, st.st_size, (long)st.st_atime, |
059ec3d9 PH |
5842 | (long)st.st_mtime, (long)st.st_ctime); |
5843 | yield = string_cat(yield, &size, &ptr, s, Ustrlen(s)); | |
5844 | continue; | |
5845 | } | |
5846 | ||
9e3331ea TK |
5847 | /* pseudo-random number less than N */ |
5848 | ||
5849 | case EOP_RANDINT: | |
5850 | { | |
5851 | int max; | |
5852 | uschar *s; | |
5853 | ||
5854 | max = expand_string_integer(sub, TRUE); | |
5855 | if (expand_string_message != NULL) | |
5856 | goto EXPAND_FAILED; | |
5857 | s = string_sprintf("%d", pseudo_random_number(max)); | |
5858 | yield = string_cat(yield, &size, &ptr, s, Ustrlen(s)); | |
5859 | continue; | |
5860 | } | |
5861 | ||
83e029d5 PP |
5862 | /* Reverse IP, including IPv6 to dotted-nibble */ |
5863 | ||
5864 | case EOP_REVERSE_IP: | |
5865 | { | |
5866 | int family, maskptr; | |
5867 | uschar reversed[128]; | |
5868 | ||
5869 | family = string_is_ip_address(sub, &maskptr); | |
5870 | if (family == 0) | |
5871 | { | |
5872 | expand_string_message = string_sprintf( | |
5873 | "reverse_ip() not given an IP address [%s]", sub); | |
5874 | goto EXPAND_FAILED; | |
5875 | } | |
5876 | invert_address(reversed, sub); | |
5877 | yield = string_cat(yield, &size, &ptr, reversed, Ustrlen(reversed)); | |
5878 | continue; | |
5879 | } | |
5880 | ||
059ec3d9 PH |
5881 | /* Unknown operator */ |
5882 | ||
5883 | default: | |
5884 | expand_string_message = | |
5885 | string_sprintf("unknown expansion operator \"%s\"", name); | |
5886 | goto EXPAND_FAILED; | |
5887 | } | |
5888 | } | |
5889 | ||
5890 | /* Handle a plain name. If this is the first thing in the expansion, release | |
5891 | the pre-allocated buffer. If the result data is known to be in a new buffer, | |
5892 | newsize will be set to the size of that buffer, and we can just point at that | |
5893 | store instead of copying. Many expansion strings contain just one reference, | |
5894 | so this is a useful optimization, especially for humungous headers | |
5895 | ($message_headers). */ | |
5896 | ||
5897 | if (*s++ == '}') | |
5898 | { | |
5899 | int len; | |
5900 | int newsize = 0; | |
5901 | if (ptr == 0) | |
5902 | { | |
d6b4d938 | 5903 | if (resetok) store_reset(yield); |
059ec3d9 PH |
5904 | yield = NULL; |
5905 | size = 0; | |
5906 | } | |
5907 | value = find_variable(name, FALSE, skipping, &newsize); | |
5908 | if (value == NULL) | |
5909 | { | |
5910 | expand_string_message = | |
5911 | string_sprintf("unknown variable in \"${%s}\"", name); | |
641cb756 | 5912 | check_variable_error_message(name); |
059ec3d9 PH |
5913 | goto EXPAND_FAILED; |
5914 | } | |
5915 | len = Ustrlen(value); | |
5916 | if (yield == NULL && newsize != 0) | |
5917 | { | |
5918 | yield = value; | |
5919 | size = newsize; | |
5920 | ptr = len; | |
5921 | } | |
5922 | else yield = string_cat(yield, &size, &ptr, value, len); | |
5923 | continue; | |
5924 | } | |
5925 | ||
5926 | /* Else there's something wrong */ | |
5927 | ||
5928 | expand_string_message = | |
5929 | string_sprintf("\"${%s\" is not a known operator (or a } is missing " | |
5930 | "in a variable reference)", name); | |
5931 | goto EXPAND_FAILED; | |
5932 | } | |
5933 | ||
5934 | /* If we hit the end of the string when ket_ends is set, there is a missing | |
5935 | terminating brace. */ | |
5936 | ||
5937 | if (ket_ends && *s == 0) | |
5938 | { | |
5939 | expand_string_message = malformed_header? | |
5940 | US"missing } at end of string - could be header name not terminated by colon" | |
5941 | : | |
5942 | US"missing } at end of string"; | |
5943 | goto EXPAND_FAILED; | |
5944 | } | |
5945 | ||
5946 | /* Expansion succeeded; yield may still be NULL here if nothing was actually | |
5947 | added to the string. If so, set up an empty string. Add a terminating zero. If | |
5948 | left != NULL, return a pointer to the terminator. */ | |
5949 | ||
5950 | if (yield == NULL) yield = store_get(1); | |
5951 | yield[ptr] = 0; | |
5952 | if (left != NULL) *left = s; | |
5953 | ||
5954 | /* Any stacking store that was used above the final string is no longer needed. | |
5955 | In many cases the final string will be the first one that was got and so there | |
5956 | will be optimal store usage. */ | |
5957 | ||
d6b4d938 | 5958 | if (resetok) store_reset(yield + ptr + 1); |
059ec3d9 PH |
5959 | DEBUG(D_expand) |
5960 | { | |
5961 | debug_printf("expanding: %.*s\n result: %s\n", (int)(s - string), string, | |
5962 | yield); | |
5963 | if (skipping) debug_printf("skipping: result is not used\n"); | |
5964 | } | |
5965 | return yield; | |
5966 | ||
5967 | /* This is the failure exit: easiest to program with a goto. We still need | |
5968 | to update the pointer to the terminator, for cases of nested calls with "fail". | |
5969 | */ | |
5970 | ||
5971 | EXPAND_FAILED_CURLY: | |
5972 | expand_string_message = malformed_header? | |
5973 | US"missing or misplaced { or } - could be header name not terminated by colon" | |
5974 | : | |
5975 | US"missing or misplaced { or }"; | |
5976 | ||
5977 | /* At one point, Exim reset the store to yield (if yield was not NULL), but | |
5978 | that is a bad idea, because expand_string_message is in dynamic store. */ | |
5979 | ||
5980 | EXPAND_FAILED: | |
5981 | if (left != NULL) *left = s; | |
5982 | DEBUG(D_expand) | |
5983 | { | |
5984 | debug_printf("failed to expand: %s\n", string); | |
5985 | debug_printf(" error message: %s\n", expand_string_message); | |
5986 | if (expand_string_forcedfail) debug_printf("failure was forced\n"); | |
5987 | } | |
5988 | return NULL; | |
5989 | } | |
5990 | ||
5991 | ||
5992 | /* This is the external function call. Do a quick check for any expansion | |
5993 | metacharacters, and if there are none, just return the input string. | |
5994 | ||
5995 | Argument: the string to be expanded | |
5996 | Returns: the expanded string, or NULL if expansion failed; if failure was | |
5997 | due to a lookup deferring, search_find_defer will be TRUE | |
5998 | */ | |
5999 | ||
6000 | uschar * | |
6001 | expand_string(uschar *string) | |
6002 | { | |
6003 | search_find_defer = FALSE; | |
6004 | malformed_header = FALSE; | |
6005 | return (Ustrpbrk(string, "$\\") == NULL)? string : | |
6006 | expand_string_internal(string, FALSE, NULL, FALSE); | |
6007 | } | |
6008 | ||
6009 | ||
6010 | ||
6011 | /************************************************* | |
6012 | * Expand and copy * | |
6013 | *************************************************/ | |
6014 | ||
6015 | /* Now and again we want to expand a string and be sure that the result is in a | |
6016 | new bit of store. This function does that. | |
6017 | ||
6018 | Argument: the string to be expanded | |
6019 | Returns: the expanded string, always in a new bit of store, or NULL | |
6020 | */ | |
6021 | ||
6022 | uschar * | |
6023 | expand_string_copy(uschar *string) | |
6024 | { | |
6025 | uschar *yield = expand_string(string); | |
6026 | if (yield == string) yield = string_copy(string); | |
6027 | return yield; | |
6028 | } | |
6029 | ||
6030 | ||
6031 | ||
6032 | /************************************************* | |
6033 | * Expand and interpret as an integer * | |
6034 | *************************************************/ | |
6035 | ||
6036 | /* Expand a string, and convert the result into an integer. | |
6037 | ||
d45b1de8 PH |
6038 | Arguments: |
6039 | string the string to be expanded | |
6040 | isplus TRUE if a non-negative number is expected | |
059ec3d9 PH |
6041 | |
6042 | Returns: the integer value, or | |
6043 | -1 for an expansion error ) in both cases, message in | |
6044 | -2 for an integer interpretation error ) expand_string_message | |
d45b1de8 | 6045 | expand_string_message is set NULL for an OK integer |
059ec3d9 PH |
6046 | */ |
6047 | ||
6048 | int | |
d45b1de8 | 6049 | expand_string_integer(uschar *string, BOOL isplus) |
059ec3d9 PH |
6050 | { |
6051 | long int value; | |
6052 | uschar *s = expand_string(string); | |
6053 | uschar *msg = US"invalid integer \"%s\""; | |
6054 | uschar *endptr; | |
6055 | ||
d45b1de8 PH |
6056 | /* If expansion failed, expand_string_message will be set. */ |
6057 | ||
059ec3d9 PH |
6058 | if (s == NULL) return -1; |
6059 | ||
6060 | /* On an overflow, strtol() returns LONG_MAX or LONG_MIN, and sets errno | |
6061 | to ERANGE. When there isn't an overflow, errno is not changed, at least on some | |
6062 | systems, so we set it zero ourselves. */ | |
6063 | ||
6064 | errno = 0; | |
d45b1de8 | 6065 | expand_string_message = NULL; /* Indicates no error */ |
b52bc06e NM |
6066 | |
6067 | /* Before Exim 4.64, strings consisting entirely of whitespace compared | |
6068 | equal to 0. Unfortunately, people actually relied upon that, so preserve | |
6069 | the behaviour explicitly. Stripping leading whitespace is a harmless | |
6070 | noop change since strtol skips it anyway (provided that there is a number | |
6071 | to find at all). */ | |
6072 | if (isspace(*s)) | |
6073 | { | |
6074 | while (isspace(*s)) ++s; | |
6075 | if (*s == '\0') | |
6076 | { | |
6077 | DEBUG(D_expand) | |
6078 | debug_printf("treating blank string as number 0\n"); | |
6079 | return 0; | |
6080 | } | |
6081 | } | |
6082 | ||
5dd1517f | 6083 | value = strtol(CS s, CSS &endptr, 10); |
059ec3d9 PH |
6084 | |
6085 | if (endptr == s) | |
6086 | { | |
6087 | msg = US"integer expected but \"%s\" found"; | |
6088 | } | |
d45b1de8 PH |
6089 | else if (value < 0 && isplus) |
6090 | { | |
6091 | msg = US"non-negative integer expected but \"%s\" found"; | |
6092 | } | |
059ec3d9 PH |
6093 | else |
6094 | { | |
6095 | /* Ensure we can cast this down to an int */ | |
6096 | if (value > INT_MAX || value < INT_MIN) errno = ERANGE; | |
6097 | ||
6098 | if (errno != ERANGE) | |
6099 | { | |
6100 | if (tolower(*endptr) == 'k') | |
6101 | { | |
6102 | if (value > INT_MAX/1024 || value < INT_MIN/1024) errno = ERANGE; | |
6103 | else value *= 1024; | |
6104 | endptr++; | |
6105 | } | |
6106 | else if (tolower(*endptr) == 'm') | |
6107 | { | |
6108 | if (value > INT_MAX/(1024*1024) || value < INT_MIN/(1024*1024)) | |
6109 | errno = ERANGE; | |
6110 | else value *= 1024*1024; | |
6111 | endptr++; | |
6112 | } | |
6113 | } | |
6114 | if (errno == ERANGE) | |
6115 | msg = US"absolute value of integer \"%s\" is too large (overflow)"; | |
6116 | else | |
6117 | { | |
6118 | while (isspace(*endptr)) endptr++; | |
6119 | if (*endptr == 0) return (int)value; | |
6120 | } | |
6121 | } | |
6122 | ||
6123 | expand_string_message = string_sprintf(CS msg, s); | |
6124 | return -2; | |
6125 | } | |
6126 | ||
059ec3d9 PH |
6127 | |
6128 | /************************************************* | |
6129 | ************************************************** | |
6130 | * Stand-alone test program * | |
6131 | ************************************************** | |
6132 | *************************************************/ | |
6133 | ||
6134 | #ifdef STAND_ALONE | |
6135 | ||
6136 | ||
6137 | BOOL | |
6138 | regex_match_and_setup(const pcre *re, uschar *subject, int options, int setup) | |
6139 | { | |
6140 | int ovector[3*(EXPAND_MAXN+1)]; | |
6141 | int n = pcre_exec(re, NULL, subject, Ustrlen(subject), 0, PCRE_EOPT|options, | |
6142 | ovector, sizeof(ovector)/sizeof(int)); | |
6143 | BOOL yield = n >= 0; | |
6144 | if (n == 0) n = EXPAND_MAXN + 1; | |
6145 | if (yield) | |
6146 | { | |
6147 | int nn; | |
6148 | expand_nmax = (setup < 0)? 0 : setup + 1; | |
6149 | for (nn = (setup < 0)? 0 : 2; nn < n*2; nn += 2) | |
6150 | { | |
6151 | expand_nstring[expand_nmax] = subject + ovector[nn]; | |
6152 | expand_nlength[expand_nmax++] = ovector[nn+1] - ovector[nn]; | |
6153 | } | |
6154 | expand_nmax--; | |
6155 | } | |
6156 | return yield; | |
6157 | } | |
6158 | ||
6159 | ||
6160 | int main(int argc, uschar **argv) | |
6161 | { | |
6162 | int i; | |
6163 | uschar buffer[1024]; | |
6164 | ||
6165 | debug_selector = D_v; | |
6166 | debug_file = stderr; | |
6167 | debug_fd = fileno(debug_file); | |
6168 | big_buffer = malloc(big_buffer_size); | |
6169 | ||
6170 | for (i = 1; i < argc; i++) | |
6171 | { | |
6172 | if (argv[i][0] == '+') | |
6173 | { | |
6174 | debug_trace_memory = 2; | |
6175 | argv[i]++; | |
6176 | } | |
6177 | if (isdigit(argv[i][0])) | |
6178 | debug_selector = Ustrtol(argv[i], NULL, 0); | |
6179 | else | |
6180 | if (Ustrspn(argv[i], "abcdefghijklmnopqrtsuvwxyz0123456789-.:/") == | |
6181 | Ustrlen(argv[i])) | |
6182 | { | |
6183 | #ifdef LOOKUP_LDAP | |
6184 | eldap_default_servers = argv[i]; | |
6185 | #endif | |
6186 | #ifdef LOOKUP_MYSQL | |
6187 | mysql_servers = argv[i]; | |
6188 | #endif | |
6189 | #ifdef LOOKUP_PGSQL | |
6190 | pgsql_servers = argv[i]; | |
6191 | #endif | |
6192 | } | |
6193 | #ifdef EXIM_PERL | |
6194 | else opt_perl_startup = argv[i]; | |
6195 | #endif | |
6196 | } | |
6197 | ||
6198 | printf("Testing string expansion: debug_level = %d\n\n", debug_level); | |
6199 | ||
6200 | expand_nstring[1] = US"string 1...."; | |
6201 | expand_nlength[1] = 8; | |
6202 | expand_nmax = 1; | |
6203 | ||
6204 | #ifdef EXIM_PERL | |
6205 | if (opt_perl_startup != NULL) | |
6206 | { | |
6207 | uschar *errstr; | |
6208 | printf("Starting Perl interpreter\n"); | |
6209 | errstr = init_perl(opt_perl_startup); | |
6210 | if (errstr != NULL) | |
6211 | { | |
6212 | printf("** error in perl_startup code: %s\n", errstr); | |
6213 | return EXIT_FAILURE; | |
6214 | } | |
6215 | } | |
6216 | #endif /* EXIM_PERL */ | |
6217 | ||
6218 | while (fgets(buffer, sizeof(buffer), stdin) != NULL) | |
6219 | { | |
6220 | void *reset_point = store_get(0); | |
6221 | uschar *yield = expand_string(buffer); | |
6222 | if (yield != NULL) | |
6223 | { | |
6224 | printf("%s\n", yield); | |
6225 | store_reset(reset_point); | |
6226 | } | |
6227 | else | |
6228 | { | |
6229 | if (search_find_defer) printf("search_find deferred\n"); | |
6230 | printf("Failed: %s\n", expand_string_message); | |
6231 | if (expand_string_forcedfail) printf("Forced failure\n"); | |
6232 | printf("\n"); | |
6233 | } | |
6234 | } | |
6235 | ||
6236 | search_tidyup(); | |
6237 | ||
6238 | return 0; | |
6239 | } | |
6240 | ||
6241 | #endif | |
6242 | ||
6243 | /* End of expand.c */ |