diff --git a/lib/Makefile.inc b/lib/Makefile.inc index da11f8e0c..504e9d023 100644 --- a/lib/Makefile.inc +++ b/lib/Makefile.inc @@ -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 index 000000000..e2e1e3e7a --- /dev/null +++ b/lib/curl_sasl.c @@ -0,0 +1,79 @@ +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * Copyright (C) 2012, Daniel Stenberg, , 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 +#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); +} diff --git a/lib/curl_sasl.h b/lib/curl_sasl.h index b0d4d365e..236645657 100644 --- a/lib/curl_sasl.h +++ b/lib/curl_sasl.h @@ -33,4 +33,10 @@ #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 */ diff --git a/lib/smtp.c b/lib/smtp.c index ae8d9a5b8..36bda3196 100644 --- a/lib/smtp.c +++ b/lib/smtp.c @@ -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) {