TLS: add variables for the IETF standard name for the connection ciphersuite
[exim.git] / src / src / rewrite.c
index e6face16a6ccf7b5e1924261645680ec576b438c..6b194ca91c740c2e613dba469a9dbf2a329ac5dd 100644 (file)
@@ -1,10 +1,8 @@
-/* $Cambridge: exim/src/src/rewrite.c,v 1.5 2009/11/16 19:50:37 nm4 Exp $ */
-
 /*************************************************
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/* Copyright (c) University of Cambridge 1995 - 2009 */
+/* Copyright (c) University of Cambridge 1995 - 2018 */
 /* See the file NOTICE for conditions of use and distribution. */
 
 /* Functions concerned with rewriting headers */
@@ -102,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;
@@ -110,22 +107,23 @@ 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;
   int count = 0;
-  uschar *save_localpart, *save_domain;
+  uschar *save_localpart;
+  const uschar *save_domain;
   uschar *error, *new, *newparsed;
 
   /* 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. */
@@ -138,12 +136,12 @@ 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 (!expand_string_forcedfail)
+      if (!f.expand_string_forcedfail)
         log_write(0, LOG_MAIN|LOG_PANIC, "failed to expand \"%s\" while "
           "checking for SMTP rewriting: %s", rule->key, expand_string_message);
       continue;
@@ -159,12 +157,12 @@ 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). */
 
-    if (match_address_list(subject, FALSE, TRUE, &(rule->key), NULL, 0,
+    if (match_address_list(subject, FALSE, TRUE, CUSS &(rule->key), NULL, 0,
         UCHAR_MAX + 1, NULL) != OK)
       continue;
 
@@ -202,10 +200,13 @@ for (rule = rewrite_rules;
   give up altogether. For other expansion failures we have a configuration
   error. */
 
-  if (new == NULL)
+  if (!new)
     {
-    if (expand_string_forcedfail)
-      { if ((rule->flags & rewrite_quit) != 0) break; else continue; }
+    if (f.expand_string_forcedfail)
+      { if (rule->flags & rewrite_quit) break; else continue; }
+
+    expand_string_message = expand_hide_passwords(expand_string_message);
+
     log_write(0, LOG_MAIN|LOG_PANIC, "Expansion of %s failed while rewriting: "
       "%s", rule->replacement, expand_string_message);
     break;
@@ -217,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);
@@ -231,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,
@@ -248,20 +249,16 @@ for (rule = rewrite_rules;
 
   /* We have a validly rewritten address */
 
-  if ((log_write_selector & L_address_rewrite) != 0 ||
-      (debug_selector & D_rewrite) != 0)
+  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);
@@ -283,18 +280,18 @@ 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] == '<')
         {
         uschar *p1 = new + start - 1;
         uschar *p2 = new + end + 1;
-        uschar *pf1, *pf2;
+        const uschar *pf1, *pf2;
         uschar buff1[256], buff2[256];
 
         while (p1 > new && p1[-1] == ' ') p1--;
@@ -308,7 +305,7 @@ for (rule = rewrite_rules;
 
         start = Ustrlen(pf1) + start + new - p1;
         end = start + Ustrlen(newparsed);
-        new = string_sprintf("%s%.*s%s", pf1, p2 - p1, p1, pf2);
+        new = string_sprintf("%s%.*s%s", pf1, (int)(p2 - p1), p1, pf2);
         }
 
       /* Now accept the whole thing */
@@ -354,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 "
@@ -451,8 +448,9 @@ Returns:         NULL if header unchanged; otherwise the rewritten header
 */
 
 static header_line *
-rewrite_one_header(header_line *h, int flag, uschar *routed_old,
-  uschar *routed_new, rewrite_rule *rewrite_rules, int existflags, BOOL replace)
+rewrite_one_header(header_line *h, int flag,
+  const uschar *routed_old, const uschar *routed_new,
+  rewrite_rule *rewrite_rules, int existflags, BOOL replace)
 {
 int lastnewline = 0;
 header_line *newh = NULL;
@@ -463,7 +461,7 @@ while (isspace(*s)) s++;
 DEBUG(D_rewrite)
   debug_printf("rewrite_one_header: type=%c:\n  %s", h->type, h->text);
 
-parse_allow_group = TRUE;     /* Allow group syntax */
+f.parse_allow_group = TRUE;     /* Allow group syntax */
 
 /* Loop for multiple addresses in the header. We have to go through them all
 in case any need qualifying, even if there's no rewriting. Pathological headers
@@ -472,7 +470,7 @@ 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);
@@ -496,7 +494,7 @@ 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);
     continue;
@@ -542,8 +540,8 @@ while (*s != 0)
 
     /* Can only qualify if permitted; if not, no rewrite. */
 
-    if (changed && ((is_recipient && !allow_unqualified_recipient) ||
-                    (!is_recipient && !allow_unqualified_sender)))
+    if (changed && ((is_recipient && !f.allow_unqualified_recipient) ||
+                    (!is_recipient && !f.allow_unqualified_sender)))
       {
       store_reset(loop_reset_point);
       continue;
@@ -671,8 +669,8 @@ while (*s != 0)
     }
   }
 
-parse_allow_group = FALSE;  /* Reset group flags */
-parse_found_group = FALSE;
+f.parse_allow_group = FALSE;  /* Reset group flags */
+f.parse_found_group = FALSE;
 
 /* If a rewrite happened and "replace" is true, put the new header into the
 chain following the old one, and mark the old one as replaced. */
@@ -719,7 +717,8 @@ Returns:         NULL if header unchanged; otherwise the rewritten header
 */
 
 header_line *
-rewrite_header(header_line *h, uschar *routed_old, uschar *routed_new,
+rewrite_header(header_line *h,
+  const uschar *routed_old, const uschar *routed_new,
   rewrite_rule *rewrite_rules, int existflags, BOOL replace)
 {
 switch (h->type)
@@ -768,7 +767,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)
@@ -805,14 +804,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;