1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * RFC4616 PLAIN authentication
23 ***************************************************************************/
27 #include <curl/curl.h>
30 #include "curl_base64.h"
31 #include "curl_ntlm_msgs.h"
32 #include "curl_sasl.h"
34 /* The last #include file should be: */
38 * Curl_sasl_create_plain_message()
40 * This is used to generate an already encoded plain message ready
41 * for sending to the recipient.
45 * data [in] - The session handle.
46 * userp [in] - The user name.
47 * passdwp [in] - The user's password.
48 * outptr [in/out] - The address where a pointer to newly allocated memory
49 * holding the result will be stored upon completion.
50 * outlen [out] - The length of the output message.
52 * Returns CURLE_OK on success.
54 CURLcode Curl_sasl_create_plain_message(struct SessionHandle *data,
57 char **outptr, size_t *outlen)
59 char plainauth[2 * MAX_CURL_USER_LENGTH + MAX_CURL_PASSWORD_LENGTH];
64 plen = strlen(passwdp);
66 if(2 * ulen + plen + 2 > sizeof(plainauth)) {
69 return CURLE_OUT_OF_MEMORY; /* plainauth too small */
72 memcpy(plainauth, userp, ulen);
73 plainauth[ulen] = '\0';
74 memcpy(plainauth + ulen + 1, userp, ulen);
75 plainauth[2 * ulen + 1] = '\0';
76 memcpy(plainauth + 2 * ulen + 2, passwdp, plen);
78 return Curl_base64_encode(data, plainauth, 2 * ulen + plen + 2, outptr,
83 * Curl_sasl_create_login_message()
85 * This is used to generate an already encoded login message containing the
86 * user name or password ready for sending to the recipient.
90 * data [in] - The session handle.
91 * userp [in] - The user name.
92 * outptr [in/out] - The address where a pointer to newly allocated memory
93 * holding the result will be stored upon completion.
94 * outlen [out] - The length of the output message.
96 * Returns CURLE_OK on success.
98 CURLcode Curl_sasl_create_login_message(struct SessionHandle *data,
99 const char* valuep, char **outptr,
102 size_t vlen = strlen(valuep);
105 *outptr = strdup("=");
107 *outlen = (size_t) 1;
112 return CURLE_OUT_OF_MEMORY;
115 return Curl_base64_encode(data, valuep, vlen, outptr, outlen);
120 * Curl_sasl_create_ntlm_type1_message()
122 * This is used to generate an already encoded NTLM type-1 message ready for
123 * sending to the recipient.
125 * Note: This is a simple wrapper of the NTLM function which means that any
126 * SASL based protocols don't have to include the NTLM functions directly.
130 * userp [in] - The user name in the format User or Domain\User.
131 * passdwp [in] - The user's password.
132 * ntlm [in/out] - The ntlm data struct being used and modified.
133 * outptr [in/out] - The address where a pointer to newly allocated memory
134 * holding the result will be stored upon completion.
135 * outlen [out] - The length of the output message.
137 * Returns CURLE_OK on success.
139 CURLcode Curl_sasl_create_ntlm_type1_message(const char *userp,
141 struct ntlmdata *ntlm,
142 char **outptr, size_t *outlen)
144 return Curl_ntlm_create_type1_message(userp, passwdp, ntlm, outptr,
149 * Curl_sasl_decode_ntlm_type2_message()
151 * This is used to decode a ntlm type-2 message received from a recipient and
152 * generate the already encoded NTLM type-3 message ready for sending back.
156 * data [in] - Pointer to session handle.
157 * header [in] - Pointer to the input buffer.
158 * userp [in] - The user name in the format User or Domain\User.
159 * passdwp [in] - The user's password.
160 * ntlm [in/out] - The ntlm data struct being used and modified.
161 * outptr [in/out] - The address where a pointer to newly allocated memory
162 * holding the result will be stored upon completion.
163 * outlen [out] - The length of the output message.
165 * Returns CURLE_OK on success.
167 CURLcode Curl_sasl_decode_ntlm_type2_message(struct SessionHandle *data,
171 struct ntlmdata *ntlm,
172 char **outptr, size_t *outlen)
174 CURLcode result = Curl_ntlm_decode_type2_message(data, header, ntlm);
177 result = Curl_ntlm_create_type3_message(data, userp, passwdp, ntlm,
182 #endif /* USE_NTLM */
185 * Curl_sasl_cleanup()
187 * This is used to cleanup any libraries or curl modules used by the sasl
192 * conn [in] - Pointer to the connection data.
193 * authused [in] - The authentication mechanism used.
195 void Curl_sasl_cleanup(struct connectdata *conn, unsigned int authused)
198 /* Cleanup the ntlm structure */
199 if(authused == SASL_AUTH_NTLM) {
200 Curl_ntlm_sspi_cleanup(&conn->ntlm);
203 /* Reserved for future use */