X-Git-Url: https://git.exim.org/exim.git/blobdiff_plain/8768d5483a5894400ae1f70cda1beb44ed9b087c..fc2ba7b9fae5992dd76f721f283714a6d2ea137d:/src/src/rewrite.c diff --git a/src/src/rewrite.c b/src/src/rewrite.c index 2196bfa69..f942bec05 100644 --- a/src/src/rewrite.c +++ b/src/src/rewrite.c @@ -100,7 +100,6 @@ uschar * rewrite_one(uschar *s, int flag, BOOL *whole, BOOL add_header, uschar *name, rewrite_rule *rewrite_rules) { -rewrite_rule *rule; uschar *yield = s; uschar *subject = s; uschar *domain = NULL; @@ -108,12 +107,12 @@ BOOL done = FALSE; int rule_number = 1; int yield_start = 0, yield_end = 0; -if (whole != NULL) *whole = FALSE; +if (whole) *whole = FALSE; /* Scan the rewriting rules */ -for (rule = rewrite_rules; - rule != NULL && !done; +for (rewrite_rule * rule = rewrite_rules; + rule && !done; rule_number++, rule = rule->next) { int start, end, pdomain; @@ -124,7 +123,7 @@ for (rule = rewrite_rules; /* Ensure that the flag matches the flags in the rule. */ - if ((rule->flags & flag) == 0) continue; + if (!(rule->flags & flag)) continue; /* Come back here for a repeat after a successful rewrite. We do this only so many times. */ @@ -137,10 +136,10 @@ for (rule = rewrite_rules; with the other kinds of rewrite, where expansion happens inside match_address_list(). */ - if ((flag & rewrite_smtp) != 0) + if (flag & rewrite_smtp) { uschar *key = expand_string(rule->key); - if (key == NULL) + if (!key) { if (!f.expand_string_forcedfail) log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand \"%s\" while " @@ -158,7 +157,7 @@ for (rule = rewrite_rules; else { - if (domain == NULL) domain = Ustrrchr(subject, '@') + 1; + if (!domain) domain = Ustrrchr(subject, '@') + 1; /* Use the general function for matching an address against a list (here just one item, so use the "impossible value" separator UCHAR_MAX+1). */ @@ -201,10 +200,10 @@ for (rule = rewrite_rules; give up altogether. For other expansion failures we have a configuration error. */ - if (new == NULL) + if (!new) { if (f.expand_string_forcedfail) - { if ((rule->flags & rewrite_quit) != 0) break; else continue; } + { if (rule->flags & rewrite_quit) break; else continue; } expand_string_message = expand_hide_passwords(expand_string_message); @@ -219,7 +218,7 @@ for (rule = rewrite_rules; newparsed = parse_extract_address(new, &error, &start, &end, &pdomain, flag == rewrite_envfrom || flag == (rewrite_smtp|rewrite_smtp_sender)); - if (newparsed == NULL) + if (!newparsed) { log_write(0, LOG_MAIN|LOG_PANIC, "Rewrite of %s yielded unparseable " "address: %s in address %s", subject, error, new); @@ -233,7 +232,7 @@ for (rule = rewrite_rules; if (pdomain == 0 && (*newparsed != 0 || (flag != rewrite_envfrom && flag != (rewrite_smtp|rewrite_smtp_sender)))) { - if ((rule->flags & rewrite_qualify) != 0) + if (rule->flags & rewrite_qualify) { newparsed = rewrite_address_qualify(newparsed, TRUE); new = string_sprintf("%.*s%s%.*s", start, new, newparsed, @@ -252,17 +251,14 @@ for (rule = rewrite_rules; if (LOGGING(address_rewrite) || (debug_selector & D_rewrite) != 0) { - int i; const uschar *where = CUS"?"; - for (i = 0; i < where_list_size; i++) - { + for (int i = 0; i < where_list_size; i++) if (flag == where_list[i].bit) { where = where_list[i].string; break; } - } log_write(L_address_rewrite, LOG_MAIN, "\"%s\" from %s rewritten as \"%s\" by rule %d", yield, where, new, rule_number); @@ -284,12 +280,12 @@ for (rule = rewrite_rules; flag set and so we must preserve the non-active portion of the current subject unless the current rule also has the w flag set. */ - if (whole != NULL && (flag & rewrite_all_headers) != 0) + if (whole && (flag & rewrite_all_headers)) { /* Current rule has the w flag set. We must ensure the phrase parts are syntactically valid if they are present. */ - if ((rule->flags & rewrite_whole) != 0) + if (rule->flags & rewrite_whole) { if (start > 0 && new[start-1] == '<') { @@ -355,12 +351,12 @@ for (rule = rewrite_rules; /* If no further rewrites are to be done, set the done flag. This allows repeats of the current rule if configured before breaking the loop. */ - if ((rule->flags & rewrite_quit) != 0) done = TRUE; + if (rule->flags & rewrite_quit) done = TRUE; /* Allow the current rule to be applied up to 10 times if requested. */ - if ((rule->flags & rewrite_repeat) != 0) + if (rule->flags & rewrite_repeat) { if (count++ < 10) goto REPEAT_RULE; log_write(0, LOG_MAIN|LOG_PANIC, "rewrite rule repeat ignored after 10 " @@ -458,7 +454,7 @@ rewrite_one_header(header_line *h, int flag, { int lastnewline = 0; header_line *newh = NULL; -void *function_reset_point = store_get(0); +rmark function_reset_point = store_mark(); uschar *s = Ustrchr(h->text, ':') + 1; while (isspace(*s)) s++; @@ -474,12 +470,12 @@ any that don't actually get rewritten. We also play silly games for those that _are_ rewritten so as to avoid runaway store usage for these kinds of header. We want to avoid keeping store for any intermediate versions. */ -while (*s != 0) +while (*s) { uschar *sprev; uschar *ss = parse_find_address_end(s, FALSE); uschar *recipient, *new, *errmess; - void *loop_reset_point = store_get(0); + rmark loop_reset_point = store_mark(); BOOL changed = FALSE; int terminator = *ss; int start, end, domain; @@ -498,9 +494,9 @@ while (*s != 0) /* There isn't much we can do for syntactic disasters at this stage. Pro tem (possibly for ever) ignore them. */ - if (recipient == NULL) + if (!recipient) { - store_reset(loop_reset_point); + loop_reset_point = store_reset(loop_reset_point); continue; } @@ -547,7 +543,7 @@ while (*s != 0) if (changed && ((is_recipient && !f.allow_unqualified_recipient) || (!is_recipient && !f.allow_unqualified_sender))) { - store_reset(loop_reset_point); + loop_reset_point = store_reset(loop_reset_point); continue; } } @@ -579,7 +575,7 @@ while (*s != 0) point, because we may have a rewritten line from a previous time round the loop. */ - if (!changed) store_reset(loop_reset_point); + if (!changed) loop_reset_point = store_reset(loop_reset_point); /* If the address has changed, create a new header containing the rewritten address. We do not need to set the chain pointers at this @@ -596,9 +592,9 @@ while (*s != 0) int newlen = Ustrlen(new); int oldlen = end - start; - header_line *prev = (newh == NULL)? h : newh; - uschar *newt = store_malloc(prev->slen - oldlen + newlen + 4); - uschar *newtstart = newt; + header_line * prev = newh ? newh : h; + uschar * newt = store_get_perm(prev->slen - oldlen + newlen + 4, TRUE); + uschar * newtstart = newt; int type = prev->type; int slen = prev->slen - oldlen + newlen; @@ -637,7 +633,7 @@ while (*s != 0) if (*p != '\n') { lastnewline = newt - newtstart; - Ustrcat(newt, "\n\t"); + Ustrcat(newt, US"\n\t"); slen += 2; } } @@ -660,11 +656,11 @@ while (*s != 0) rewritten copy from a previous time round this loop. */ store_reset(function_reset_point); - newh = store_get(sizeof(header_line)); + function_reset_point = store_mark(); + newh = store_get(sizeof(header_line), FALSE); newh->type = type; newh->slen = slen; newh->text = string_copyn(newtstart, slen); - store_free(newtstart); /* Set up for scanning the rest of the header */ @@ -725,34 +721,19 @@ rewrite_header(header_line *h, const uschar *routed_old, const uschar *routed_new, rewrite_rule *rewrite_rules, int existflags, BOOL replace) { +int flag; switch (h->type) { - case htype_sender: - return rewrite_one_header(h, rewrite_sender, routed_old, routed_new, - rewrite_rules, existflags, replace); - - case htype_from: - return rewrite_one_header(h, rewrite_from, routed_old, routed_new, - rewrite_rules, existflags, replace); - - case htype_to: - return rewrite_one_header(h, rewrite_to, routed_old, routed_new, - rewrite_rules, existflags, replace); - - case htype_cc: - return rewrite_one_header(h, rewrite_cc, routed_old, routed_new, - rewrite_rules, existflags, replace); - - case htype_bcc: - return rewrite_one_header(h, rewrite_bcc, routed_old, routed_new, - rewrite_rules, existflags, replace); - - case htype_reply_to: - return rewrite_one_header(h, rewrite_replyto, routed_old, routed_new, - rewrite_rules, existflags, replace); + case htype_sender: flag = rewrite_sender; break; + case htype_from: flag = rewrite_from; break; + case htype_to: flag = rewrite_to; break; + case htype_cc: flag = rewrite_cc; break; + case htype_bcc: flag = rewrite_bcc; break; + case htype_reply_to: flag = rewrite_replyto; break; + default: return NULL; } - -return NULL; +return rewrite_one_header(h, flag, routed_old, routed_new, + rewrite_rules, existflags, replace); } @@ -771,7 +752,7 @@ Returns: nothing void rewrite_test(uschar *s) { uschar *recipient, *error; -int i, start, end, domain; +int start, end, domain; BOOL done_smtp = FALSE; if (rewrite_existflags == 0) @@ -808,14 +789,14 @@ if (parse_find_at(s) == NULL) recipient = parse_extract_address(s, &error, &start, &end, &domain, FALSE); -if (recipient == NULL) +if (!recipient) { if (!done_smtp) printf("Syntax error in %s\n%c%s\n", s, toupper(error[0]), error+1); return; } -for (i = 0; i < 8; i++) +for (int i = 0; i < 8; i++) { BOOL whole = FALSE; int flag = 1 << i;