| 1 |
/*************************************************************************** |
| 2 |
* _ _ ____ _ |
| 3 |
* Project ___| | | | _ \| | |
| 4 |
* / __| | | | |_) | | |
| 5 |
* | (__| |_| | _ <| |___ |
| 6 |
* \___|\___/|_| \_\_____| |
| 7 |
* |
| 8 |
* Copyright (C) Daniel Stenberg, <[email protected]>, et al. |
| 9 |
* |
| 10 |
* This software is licensed as described in the file COPYING, which |
| 11 |
* you should have received as part of this distribution. The terms |
| 12 |
* are also available at https://curl.se/docs/copyright.html. |
| 13 |
* |
| 14 |
* You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 15 |
* copies of the Software, and permit persons to whom the Software is |
| 16 |
* furnished to do so, under the terms of the COPYING file. |
| 17 |
* |
| 18 |
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 |
* KIND, either express or implied. |
| 20 |
* |
| 21 |
* SPDX-License-Identifier: curl |
| 22 |
* |
| 23 |
* RFC4616 PLAIN authentication |
| 24 |
* Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt> |
| 25 |
* |
| 26 |
***************************************************************************/ |
| 27 |
|
| 28 |
#include "../curl_setup.h" |
| 29 |
|
| 30 |
#if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_SMTP) || \ |
| 31 |
!defined(CURL_DISABLE_POP3) || \ |
| 32 |
(!defined(CURL_DISABLE_LDAP) && defined(USE_OPENLDAP)) |
| 33 |
|
| 34 |
#include <curl/curl.h> |
| 35 |
#include "../urldata.h" |
| 36 |
|
| 37 |
#include "vauth.h" |
| 38 |
#include "../curlx/warnless.h" |
| 39 |
#include "../sendf.h" |
| 40 |
#include "../curl_printf.h" |
| 41 |
|
| 42 |
/* The last #include files should be: */ |
| 43 |
#include "../curl_memory.h" |
| 44 |
#include "../memdebug.h" |
| 45 |
|
| 46 |
/* |
| 47 |
* Curl_auth_create_plain_message() |
| 48 |
* |
| 49 |
* This is used to generate an already encoded PLAIN message ready |
| 50 |
* for sending to the recipient. |
| 51 |
* |
| 52 |
* Parameters: |
| 53 |
* |
| 54 |
* authzid [in] - The authorization identity. |
| 55 |
* authcid [in] - The authentication identity. |
| 56 |
* passwd [in] - The password. |
| 57 |
* out [out] - The result storage. |
| 58 |
* |
| 59 |
* Returns CURLE_OK on success. |
| 60 |
*/ |
| 61 |
CURLcode Curl_auth_create_plain_message(const char *authzid, |
| 62 |
const char *authcid, |
| 63 |
const char *passwd, |
| 64 |
struct bufref *out) |
| 65 |
{ |
| 66 |
char *plainauth; |
| 67 |
size_t plainlen; |
| 68 |
size_t zlen; |
| 69 |
size_t clen; |
| 70 |
size_t plen; |
| 71 |
|
| 72 |
zlen = (authzid == NULL ? 0 : strlen(authzid)); |
| 73 |
clen = strlen(authcid); |
| 74 |
plen = strlen(passwd); |
| 75 |
|
| 76 |
/* Compute binary message length. Check for overflows. */ |
| 77 |
if((zlen > SIZE_T_MAX/4) || (clen > SIZE_T_MAX/4) || |
| 78 |
(plen > (SIZE_T_MAX/2 - 2))) |
| 79 |
return CURLE_OUT_OF_MEMORY; |
| 80 |
plainlen = zlen + clen + plen + 2; |
| 81 |
|
| 82 |
plainauth = malloc(plainlen + 1); |
| 83 |
if(!plainauth) |
| 84 |
return CURLE_OUT_OF_MEMORY; |
| 85 |
|
| 86 |
/* Calculate the reply */ |
| 87 |
if(zlen) |
| 88 |
memcpy(plainauth, authzid, zlen); |
| 89 |
plainauth[zlen] = '\0'; |
| 90 |
memcpy(plainauth + zlen + 1, authcid, clen); |
| 91 |
plainauth[zlen + clen + 1] = '\0'; |
| 92 |
memcpy(plainauth + zlen + clen + 2, passwd, plen); |
| 93 |
plainauth[plainlen] = '\0'; |
| 94 |
Curl_bufref_set(out, plainauth, plainlen, curl_free); |
| 95 |
return CURLE_OK; |
| 96 |
} |
| 97 |
|
| 98 |
/* |
| 99 |
* Curl_auth_create_login_message() |
| 100 |
* |
| 101 |
* This is used to generate an already encoded LOGIN message containing the |
| 102 |
* username or password ready for sending to the recipient. |
| 103 |
* |
| 104 |
* Parameters: |
| 105 |
* |
| 106 |
* valuep [in] - The username or user's password. |
| 107 |
* out [out] - The result storage. |
| 108 |
* |
| 109 |
* Returns void. |
| 110 |
*/ |
| 111 |
void Curl_auth_create_login_message(const char *valuep, struct bufref *out) |
| 112 |
{ |
| 113 |
Curl_bufref_set(out, valuep, strlen(valuep), NULL); |
| 114 |
} |
| 115 |
|
| 116 |
/* |
| 117 |
* Curl_auth_create_external_message() |
| 118 |
* |
| 119 |
* This is used to generate an already encoded EXTERNAL message containing |
| 120 |
* the username ready for sending to the recipient. |
| 121 |
* |
| 122 |
* Parameters: |
| 123 |
* |
| 124 |
* user [in] - The username. |
| 125 |
* out [out] - The result storage. |
| 126 |
* |
| 127 |
* Returns void. |
| 128 |
*/ |
| 129 |
void Curl_auth_create_external_message(const char *user, |
| 130 |
struct bufref *out) |
| 131 |
{ |
| 132 |
/* This is the same formatting as the login message */ |
| 133 |
Curl_auth_create_login_message(user, out); |
| 134 |
} |
| 135 |
|
| 136 |
#endif /* if no users */ |