]> granicus.if.org Git - curl/commitdiff
sasl: Moved plain text authentication message creation from smtp.c
authorSteve Holme <steve_holme@hotmail.com>
Wed, 30 May 2012 19:52:52 +0000 (20:52 +0100)
committerSteve Holme <steve_holme@hotmail.com>
Wed, 30 May 2012 19:52:52 +0000 (20:52 +0100)
Moved the plain text message creation from smtp.c into the sasl module
to allow for use by other modules such as pop3.

lib/Makefile.inc
lib/curl_sasl.c [new file with mode: 0644]
lib/curl_sasl.h
lib/smtp.c

index da11f8e0cb0f48b4605609c4dc41a67373f129fe..504e9d023f6441ac5b89f690cbf577fb9a3459f7 100644 (file)
@@ -23,7 +23,7 @@ CSOURCES = file.c timeval.c base64.c hostip.c progress.c formdata.c   \
   curl_rtmp.c openldap.c curl_gethostname.c gopher.c axtls.c           \
   idn_win32.c http_negotiate_sspi.c cyassl.c http_proxy.c non-ascii.c  \
   asyn-ares.c asyn-thread.c curl_gssapi.c curl_ntlm.c curl_ntlm_wb.c   \
-  curl_ntlm_core.c curl_ntlm_msgs.c
+  curl_ntlm_core.c curl_ntlm_msgs.c curl_sasl.c
 
 HHEADERS = arpa_telnet.h netrc.h file.h timeval.h qssl.h hostip.h      \
   progress.h formdata.h cookie.h http.h sendf.h ftp.h url.h dict.h     \
diff --git a/lib/curl_sasl.c b/lib/curl_sasl.c
new file mode 100644 (file)
index 0000000..e2e1e3e
--- /dev/null
@@ -0,0 +1,79 @@
+/***************************************************************************
+ *                                  _   _ ____  _
+ *  Project                     ___| | | |  _ \| |
+ *                             / __| | | | |_) | |
+ *                            | (__| |_| |  _ <| |___
+ *                             \___|\___/|_| \_\_____|
+ *
+ * Copyright (C) 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
+ *
+ * This software is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution. The terms
+ * are also available at http://curl.haxx.se/docs/copyright.html.
+ *
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
+ * copies of the Software, and permit persons to whom the Software is
+ * furnished to do so, under the terms of the COPYING file.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ * RFC4616 PLAIN authentication
+ *
+ ***************************************************************************/
+
+#include "setup.h"
+
+#include <curl/curl.h>
+#include "urldata.h"
+
+#include "curl_base64.h"
+#include "curl_sasl.h"
+
+/* The last #include file should be: */
+#include "memdebug.h"
+
+/*
+ * Curl_sasl_create_plain_message()
+ *
+ * This is used to generate an already encoded plain message ready
+ * for sending to the recipient.
+ *
+ * Parameters:
+ *
+ * data    [in]     - The session handle.
+ * userp   [in]     - The user name.
+ * passdwp [in]     - The user's password.
+ * outptr  [in/out] - The address where a pointer to newly allocated memory
+ *                    holding the result will be stored upon completion.
+ * outlen  [out]    - The length of the output message.
+ *
+ * Returns CURLE_OK on success.
+ */
+CURLcode Curl_sasl_create_plain_message(struct SessionHandle *data,
+                                        const char* userp,
+                                        const char* passwdp,
+                                        char **outptr, size_t *outlen)
+{
+  char plainauth[2 * MAX_CURL_USER_LENGTH + MAX_CURL_PASSWORD_LENGTH];
+  size_t ulen;
+  size_t plen;
+
+  ulen = strlen(userp);
+  plen = strlen(passwdp);
+
+  if(2 * ulen + plen + 2 > sizeof(plainauth)) {
+    *outlen = 0;
+    *outptr = NULL;
+    return CURLE_OUT_OF_MEMORY; /* plainauth too small */
+  }
+
+  memcpy(plainauth, userp, ulen);
+  plainauth[ulen] = '\0';
+  memcpy(plainauth + ulen + 1, userp, ulen);
+  plainauth[2 * ulen + 1] = '\0';
+  memcpy(plainauth + 2 * ulen + 2, passwdp, plen);
+
+  return Curl_base64_encode(data, plainauth, 2 * ulen + plen + 2, outptr,
+                            outlen);
+}
index b0d4d365e1348724623e69d0875639147e6e12d6..2366456578303db91d94d6a1925f22b4993829f8 100644 (file)
 #define SASL_AUTH_EXTERNAL      0x0020
 #define SASL_AUTH_NTLM          0x0040
 
+/* This is to generate a base64 encoded plain authentication message */
+CURLcode Curl_sasl_create_plain_message(struct SessionHandle *data,
+                                        const char* userp,
+                                        const char* passwdp,
+                                        char **outptr, size_t *outlen);
+
 #endif /* HEADER_CURL_SASL_H */
index ae8d9a5b883670ebf680a7d11b3347644c0b1ffb..36bda319627bfe6103bb322499a709a841c979bd 100644 (file)
@@ -383,32 +383,6 @@ static CURLcode smtp_state_helo(struct connectdata *conn)
   return CURLE_OK;
 }
 
-static CURLcode smtp_auth_plain_data(struct connectdata *conn,
-                                     char **outptr, size_t *outlen)
-{
-  char plainauth[2 * MAX_CURL_USER_LENGTH + MAX_CURL_PASSWORD_LENGTH];
-  size_t ulen;
-  size_t plen;
-
-  ulen = strlen(conn->user);
-  plen = strlen(conn->passwd);
-
-  if(2 * ulen + plen + 2 > sizeof(plainauth)) {
-    *outlen = 0;
-    *outptr = NULL;
-    return CURLE_OUT_OF_MEMORY; /* plainauth too small */
-  }
-
-  memcpy(plainauth, conn->user, ulen);
-  plainauth[ulen] = '\0';
-  memcpy(plainauth + ulen + 1, conn->user, ulen);
-  plainauth[2 * ulen + 1] = '\0';
-  memcpy(plainauth + 2 * ulen + 2, conn->passwd, plen);
-
-  return Curl_base64_encode(conn->data, plainauth, 2 * ulen + plen + 2,
-                            outptr, outlen);
-}
-
 static CURLcode smtp_auth_login_user(struct connectdata *conn,
                                      char **outptr, size_t *outlen)
 {
@@ -491,7 +465,8 @@ static CURLcode smtp_authenticate(struct connectdata *conn)
     state1 = SMTP_AUTHPLAIN;
     state2 = SMTP_AUTH;
     smtpc->authused = SASL_AUTH_PLAIN;
-    result = smtp_auth_plain_data(conn, &initresp, &len);
+    result = Curl_sasl_create_plain_message(conn->data, conn->user,
+                                            conn->passwd, &initresp, &len);
   }
   else {
     infof(conn->data, "No known auth mechanisms supported!\n");
@@ -675,7 +650,8 @@ static CURLcode smtp_state_authplain_resp(struct connectdata *conn,
     result = CURLE_LOGIN_DENIED;
   }
   else {
-    result = smtp_auth_plain_data(conn, &plainauth, &len);
+    result = Curl_sasl_create_plain_message(conn->data, conn->user,
+                                            conn->passwd, &plainauth, &len);
 
     if(!result) {
       if(plainauth) {