Prebuild the data structure for builtin macros
[exim.git] / src / src / auths / plaintext.c
index e5f261a161607a095209203a4a0ff5ce3912d55a..8bba05823ed79bf5512ad69724497cd717030b45 100644 (file)
@@ -1,10 +1,8 @@
-/* $Cambridge: exim/src/src/auths/plaintext.c,v 1.4 2006/02/10 14:25:43 ph10 Exp $ */
-
 /*************************************************
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/* Copyright (c) University of Cambridge 1995 - 2006 */
+/* Copyright (c) University of Cambridge 1995 - 2015 */
 /* See the file NOTICE for conditions of use and distribution. */
 
 #include "../exim.h"
 /* Options specific to the plaintext authentication mechanism. */
 
 optionlist auth_plaintext_options[] = {
+  { "client_ignore_invalid_base64", opt_bool,
+      (void *)(offsetof(auth_plaintext_options_block, client_ignore_invalid_base64)) },
   { "client_send",        opt_stringptr,
       (void *)(offsetof(auth_plaintext_options_block, client_send)) },
-  { "server_condition",   opt_stringptr,
-      (void *)(offsetof(auth_plaintext_options_block, server_condition)) },
   { "server_prompts",     opt_stringptr,
       (void *)(offsetof(auth_plaintext_options_block, server_prompts)) }
 };
@@ -31,12 +29,24 @@ int auth_plaintext_options_count =
 /* Default private options block for the plaintext authentication method. */
 
 auth_plaintext_options_block auth_plaintext_option_defaults = {
-  NULL,              /* server_condition */
   NULL,              /* server_prompts */
-  NULL               /* client_send */
+  NULL,              /* client_send */
+  FALSE              /* client_ignore_invalid_base64 */
 };
 
 
+#ifdef MACRO_PREDEF
+
+/* Dummy values */
+void auth_plaintext_init(auth_instance *ablock) {}
+int auth_plaintext_server(auth_instance *ablock, uschar *data) {return 0;}
+int auth_plaintext_client(auth_instance *ablock, smtp_inblock *inblock,
+  smtp_outblock *outblock, int timeout, uschar *buffer, int buffsize) {return 0;}
+
+#else   /*!MACRO_PREDEF*/
+
+
+
 /*************************************************
 *          Initialization entry point            *
 *************************************************/
@@ -51,7 +61,7 @@ auth_plaintext_init(auth_instance *ablock)
 auth_plaintext_options_block *ob =
   (auth_plaintext_options_block *)(ablock->options_block);
 if (ablock->public_name == NULL) ablock->public_name = ablock->name;
-if (ob->server_condition != NULL) ablock->server = TRUE;
+if (ablock->server_condition != NULL) ablock->server = TRUE;
 if (ob->client_send != NULL) ablock->client = TRUE;
 }
 
@@ -68,8 +78,8 @@ auth_plaintext_server(auth_instance *ablock, uschar *data)
 {
 auth_plaintext_options_block *ob =
   (auth_plaintext_options_block *)(ablock->options_block);
-uschar *prompts = ob->server_prompts;
-uschar *clear, *cond, *end, *s;
+const uschar *prompts = ob->server_prompts;
+uschar *clear, *end, *s;
 int number = 1;
 int len, rc;
 int sep = 0;
@@ -78,7 +88,7 @@ int sep = 0;
 
 if (prompts != NULL)
   {
-  prompts = expand_string(prompts);
+  prompts = expand_cstring(prompts);
   if (prompts == NULL)
     {
     auth_defer_msg = expand_string_message;
@@ -101,7 +111,7 @@ if (*data != 0)
     }
   else
     {
-    if ((len = auth_b64decode(data, &clear)) < 0) return BAD64;
+    if ((len = b64decode(data, &clear)) < 0) return BAD64;
     end = clear + len;
     while (clear < end && expand_nmax < EXPAND_MAXN)
       {
@@ -123,7 +133,7 @@ while ((s = string_nextinlist(&prompts, &sep, big_buffer, big_buffer_size))
   {
   if (number++ <= expand_nmax) continue;
   if ((rc = auth_get_data(&data, s, Ustrlen(s))) != OK) return rc;
-  if ((len = auth_b64decode(data, &clear)) < 0) return BAD64;
+  if ((len = b64decode(data, &clear)) < 0) return BAD64;
   end = clear + len;
 
   /* This loop must run at least once, in case the length is zero */
@@ -138,59 +148,12 @@ while ((s = string_nextinlist(&prompts, &sep, big_buffer, big_buffer_size))
   }
 
 /* We now have a number of items of data in $auth1, $auth2, etc (and also, for
-compatibility, in $1, $2, etc). Match against the decoded data by expanding the
-condition. */
-
-cond = expand_string(ob->server_condition);
-
-HDEBUG(D_auth)
-  {
-  int i;
-  debug_printf("%s authenticator:\n", ablock->name);
-  for (i = 0; i < AUTH_VARS; i++)
-    {
-    if (auth_vars[i] != NULL)
-      debug_printf("  $auth%d = %s\n", i + 1, auth_vars[i]);
-    }
-  for (i = 1; i <= expand_nmax; i++)
-    debug_printf("  $%d = %.*s\n", i, expand_nlength[i], expand_nstring[i]);
-  debug_print_string(ablock->server_debug_string);    /* customized debug */
-  if (cond == NULL)
-    debug_printf("expansion failed: %s\n", expand_string_message);
-  else
-    debug_printf("expanded string: %s\n", cond);
-  }
-
-/* A forced expansion failure causes authentication to fail. Other expansion
-failures yield DEFER, which will cause a temporary error code to be returned to
-the AUTH command. The problem is at the server end, so the client should try
-again later. */
-
-if (cond == NULL)
-  {
-  if (expand_string_forcedfail) return FAIL;
-  auth_defer_msg = expand_string_message;
-  return DEFER;
-  }
-
-/* Return FAIL for empty string, "0", "no", and "false"; return OK for
-"1", "yes", and "true"; return DEFER for anything else, with the string
-available as an error text for the user. */
+compatibility, in $1, $2, etc). Authentication and authorization are handled
+together for this authenticator by expanding the server_condition option. Note
+that ablock->server_condition is always non-NULL because that's what configures
+this authenticator as a server. */
 
-if (*cond == 0 ||
-    Ustrcmp(cond, "0") == 0 ||
-    strcmpic(cond, US"no") == 0 ||
-    strcmpic(cond, US"false") == 0)
-  return FAIL;
-
-if (Ustrcmp(cond, "1") == 0 ||
-    strcmpic(cond, US"yes") == 0 ||
-    strcmpic(cond, US"true") == 0)
-  return OK;
-
-auth_defer_msg = cond;
-auth_defer_user_msg = string_sprintf(": %s", cond);
-return DEFER;
+return auth_check_serv_cond(ablock);
 }
 
 
@@ -212,34 +175,40 @@ auth_plaintext_client(
 {
 auth_plaintext_options_block *ob =
   (auth_plaintext_options_block *)(ablock->options_block);
-uschar *text = ob->client_send;
+const uschar *text = ob->client_send;
 uschar *s;
 BOOL first = TRUE;
 int sep = 0;
+int auth_var_idx = 0;
 
 /* The text is broken up into a number of different data items, which are
 sent one by one. The first one is sent with the AUTH command; the remainder are
 sent in response to subsequent prompts. Each is expanded before being sent. */
 
-while ((s = string_nextinlist(&text, &sep, big_buffer, big_buffer_size)) != NULL)
+while ((s = string_nextinlist(&text, &sep, big_buffer, big_buffer_size)))
   {
-  int i, len;
+  int i, len, clear_len;
   uschar *ss = expand_string(s);
+  uschar *clear;
 
   /* Forced expansion failure is not an error; authentication is abandoned. On
   all but the first string, we have to abandon the authentication attempt by
   sending a line containing "*". Save the failed expansion string, because it
   is in big_buffer, and that gets used by the sending function. */
 
-  if (ss == NULL)
+  if (!ss)
     {
     uschar *ssave = string_copy(s);
     if (!first)
       {
-      if (smtp_write_command(outblock, FALSE, "*\r\n") >= 0)
+      if (smtp_write_command(outblock, SCMD_FLUSH, "*\r\n") >= 0)
         (void) smtp_read_response(inblock, US buffer, buffsize, '2', timeout);
       }
-    if (expand_string_forcedfail) return CANCELLED;
+    if (expand_string_forcedfail)
+      {
+      *buffer = 0;       /* No message */
+      return CANCELLED;
+      }
     string_format(buffer, buffsize, "expansion of \"%s\" failed in %s "
       "authenticator: %s", ssave, ablock->name, expand_string_message);
     return ERROR;
@@ -251,33 +220,31 @@ while ((s = string_nextinlist(&text, &sep, big_buffer, big_buffer_size)) != NULL
   needed for the PLAIN mechanism. It must be doubled if really needed. */
 
   for (i = 0; i < len; i++)
-    {
     if (ss[i] == '^')
-      {
-      if (ss[i+1] != '^') ss[i] = 0; else
+      if (ss[i+1] != '^')
+       ss[i] = 0;
+      else
         {
         i++;
         len--;
         memmove(ss + i, ss + i + 1, len - i);
         }
-      }
-    }
 
   /* The first string is attached to the AUTH command; others are sent
-  unembelished. */
+  unembellished. */
 
   if (first)
     {
     first = FALSE;
-    if (smtp_write_command(outblock, FALSE, "AUTH %s%s%s\r\n",
+    if (smtp_write_command(outblock, SCMD_FLUSH, "AUTH %s%s%s\r\n",
          ablock->public_name, (len == 0)? "" : " ",
-         auth_b64encode(ss, len)) < 0)
+         b64encode(ss, len)) < 0)
       return FAIL_SEND;
     }
   else
     {
-    if (smtp_write_command(outblock, FALSE, "%s\r\n",
-          auth_b64encode(ss, len)) < 0)
+    if (smtp_write_command(outblock, SCMD_FLUSH, "%s\r\n",
+          b64encode(ss, len)) < 0)
       return FAIL_SEND;
     }
 
@@ -298,12 +265,40 @@ while ((s = string_nextinlist(&text, &sep, big_buffer, big_buffer_size)) != NULL
 
   if (text == NULL)
     {
-    if (smtp_write_command(outblock, FALSE, "*\r\n") >= 0)
+    if (smtp_write_command(outblock, SCMD_FLUSH, "*\r\n") >= 0)
       (void)smtp_read_response(inblock, US buffer, buffsize, '2', timeout);
     string_format(buffer, buffsize, "Too few items in client_send in %s "
       "authenticator", ablock->name);
     return ERROR;
     }
+
+  /* Now that we know we'll continue, we put the received data into $auth<n>,
+  if possible. First, decode it: buffer+4 skips over the SMTP status code. */
+
+  clear_len = b64decode(buffer+4, &clear);
+
+  /* If decoding failed, the default is to terminate the authentication, and
+  return FAIL, with the SMTP response still in the buffer. However, if client_
+  ignore_invalid_base64 is set, we ignore the error, and put an empty string
+  into $auth<n>. */
+
+  if (clear_len < 0)
+    {
+    uschar *save_bad = string_copy(buffer);
+    if (!ob->client_ignore_invalid_base64)
+      {
+      if (smtp_write_command(outblock, SCMD_FLUSH, "*\r\n") >= 0)
+        (void)smtp_read_response(inblock, US buffer, buffsize, '2', timeout);
+      string_format(buffer, buffsize, "Invalid base64 string in server "
+        "response \"%s\"", save_bad);
+      return CANCELLED;
+      }
+    clear = US"";
+    clear_len = 0;
+    }
+
+  if (auth_var_idx < AUTH_VARS)
+    auth_vars[auth_var_idx++] = string_copy(clear);
   }
 
 /* Control should never actually get here. */
@@ -311,4 +306,5 @@ while ((s = string_nextinlist(&text, &sep, big_buffer, big_buffer_size)) != NULL
 return FAIL;
 }
 
+#endif   /*!MACRO_PREDEF*/
 /* End of plaintext.c */