137 lines
4.0 KiB
C
Raw Normal View History

2016-09-11 17:22:32 +02:00
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
2021-09-14 00:13:48 +02:00
* Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
2016-09-11 17:22:32 +02:00
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
2021-09-14 00:13:48 +02:00
* are also available at https://curl.se/docs/copyright.html.
2016-09-11 17:22:32 +02:00
*
* 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
* Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
*
***************************************************************************/
#include "curl_setup.h"
2019-11-11 23:01:05 +01:00
#if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_SMTP) || \
!defined(CURL_DISABLE_POP3)
2016-09-11 17:22:32 +02:00
#include <curl/curl.h>
#include "urldata.h"
#include "vauth/vauth.h"
#include "curl_md5.h"
#include "warnless.h"
#include "strtok.h"
#include "sendf.h"
#include "curl_printf.h"
/* The last #include files should be: */
#include "curl_memory.h"
#include "memdebug.h"
/*
* Curl_auth_create_plain_message()
*
* This is used to generate an already encoded PLAIN message ready
* for sending to the recipient.
*
* Parameters:
*
2019-11-11 23:01:05 +01:00
* authzid [in] - The authorization identity.
* authcid [in] - The authentication identity.
* passwd [in] - The password.
2021-09-14 00:13:48 +02:00
* out [out] - The result storage.
2016-09-11 17:22:32 +02:00
*
* Returns CURLE_OK on success.
*/
2021-09-14 00:13:48 +02:00
CURLcode Curl_auth_create_plain_message(const char *authzid,
2019-11-11 23:01:05 +01:00
const char *authcid,
const char *passwd,
2021-09-14 00:13:48 +02:00
struct bufref *out)
2016-09-11 17:22:32 +02:00
{
char *plainauth;
2021-09-14 00:13:48 +02:00
size_t plainlen;
2019-11-11 23:01:05 +01:00
size_t zlen;
size_t clen;
2016-09-11 17:22:32 +02:00
size_t plen;
2019-11-11 23:01:05 +01:00
zlen = (authzid == NULL ? 0 : strlen(authzid));
clen = strlen(authcid);
plen = strlen(passwd);
2016-09-11 17:22:32 +02:00
2018-08-09 18:06:22 +02:00
/* Compute binary message length. Check for overflows. */
2020-08-30 11:54:41 +02:00
if((zlen > SIZE_T_MAX/4) || (clen > SIZE_T_MAX/4) ||
(plen > (SIZE_T_MAX/2 - 2)))
2017-07-20 19:35:53 +02:00
return CURLE_OUT_OF_MEMORY;
2019-11-11 23:01:05 +01:00
plainlen = zlen + clen + plen + 2;
2017-07-20 19:35:53 +02:00
2021-09-14 00:13:48 +02:00
plainauth = malloc(plainlen + 1);
2017-07-20 19:35:53 +02:00
if(!plainauth)
2016-09-11 17:22:32 +02:00
return CURLE_OUT_OF_MEMORY;
/* Calculate the reply */
2021-09-14 00:13:48 +02:00
if(zlen)
2019-11-11 23:01:05 +01:00
memcpy(plainauth, authzid, zlen);
plainauth[zlen] = '\0';
memcpy(plainauth + zlen + 1, authcid, clen);
plainauth[zlen + clen + 1] = '\0';
memcpy(plainauth + zlen + clen + 2, passwd, plen);
2021-09-14 00:13:48 +02:00
plainauth[plainlen] = '\0';
Curl_bufref_set(out, plainauth, plainlen, curl_free);
return CURLE_OK;
2016-09-11 17:22:32 +02:00
}
/*
* Curl_auth_create_login_message()
*
* This is used to generate an already encoded LOGIN message containing the
* user name or password ready for sending to the recipient.
*
* Parameters:
*
* valuep [in] - The user name or user's password.
2021-09-14 00:13:48 +02:00
* out [out] - The result storage.
2016-09-11 17:22:32 +02:00
*
* Returns CURLE_OK on success.
*/
2021-09-14 00:13:48 +02:00
CURLcode Curl_auth_create_login_message(const char *valuep, struct bufref *out)
2016-09-11 17:22:32 +02:00
{
2021-09-14 00:13:48 +02:00
Curl_bufref_set(out, valuep, strlen(valuep), NULL);
return CURLE_OK;
2016-09-11 17:22:32 +02:00
}
/*
* Curl_auth_create_external_message()
*
* This is used to generate an already encoded EXTERNAL message containing
* the user name ready for sending to the recipient.
*
* Parameters:
*
* user [in] - The user name.
2021-09-14 00:13:48 +02:00
* out [out] - The result storage.
2016-09-11 17:22:32 +02:00
*
* Returns CURLE_OK on success.
*/
2021-09-14 00:13:48 +02:00
CURLcode Curl_auth_create_external_message(const char *user,
struct bufref *out)
2016-09-11 17:22:32 +02:00
{
/* This is the same formatting as the login message */
2021-09-14 00:13:48 +02:00
return Curl_auth_create_login_message(user, out);
2016-09-11 17:22:32 +02:00
}
2019-11-11 23:01:05 +01:00
#endif /* if no users */