2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
16 #include "libmemcached/common.h"
20 #if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT
22 # if defined(HAVE_LIBSASL) && HAVE_LIBSASL
23 # include <sasl/sasl.h>
26 # define CAST_SASL_CB(cb) reinterpret_cast<int (*)()>(reinterpret_cast<intptr_t>(cb))
30 void memcached_set_sasl_callbacks(memcached_st
*shell
, const sasl_callback_t
*callbacks
) {
31 Memcached
*self
= memcached2Memcached(shell
);
33 self
->sasl
.callbacks
= const_cast<sasl_callback_t
*>(callbacks
);
34 self
->sasl
.is_allocated
= false;
38 sasl_callback_t
*memcached_get_sasl_callbacks(memcached_st
*shell
) {
39 Memcached
*self
= memcached2Memcached(shell
);
41 return self
->sasl
.callbacks
;
48 * Resolve the names for both ends of a connection
49 * @param fd socket to check
50 * @param laddr local address (out)
51 * @param raddr remote address (out)
52 * @return true on success false otherwise (errno contains more info)
54 static memcached_return_t
resolve_names(memcached_instance_st
&server
, char *laddr
,
55 size_t laddr_length
, char *raddr
, size_t raddr_length
) {
56 char host
[MEMCACHED_NI_MAXHOST
];
57 char port
[MEMCACHED_NI_MAXSERV
];
58 struct sockaddr_storage saddr
;
59 socklen_t salen
= sizeof(saddr
);
61 if (getsockname(server
.fd
, (struct sockaddr
*) &saddr
, &salen
) < 0) {
62 return memcached_set_error(server
, MEMCACHED_HOST_LOOKUP_FAILURE
, MEMCACHED_AT
);
65 if (getnameinfo((struct sockaddr
*) &saddr
, salen
, host
, sizeof(host
), port
, sizeof(port
),
66 NI_NUMERICHOST
| NI_NUMERICSERV
)
69 return memcached_set_error(server
, MEMCACHED_HOST_LOOKUP_FAILURE
, MEMCACHED_AT
);
72 (void) snprintf(laddr
, laddr_length
, "%s;%s", host
, port
);
73 salen
= sizeof(saddr
);
75 if (getpeername(server
.fd
, (struct sockaddr
*) &saddr
, &salen
) < 0) {
76 return memcached_set_error(server
, MEMCACHED_HOST_LOOKUP_FAILURE
, MEMCACHED_AT
);
79 if (getnameinfo((struct sockaddr
*) &saddr
, salen
, host
, sizeof(host
), port
, sizeof(port
),
80 NI_NUMERICHOST
| NI_NUMERICSERV
)
83 return memcached_set_error(server
, MEMCACHED_HOST_LOOKUP_FAILURE
, MEMCACHED_AT
);
86 (void) snprintf(raddr
, raddr_length
, "%s;%s", host
, port
);
88 return MEMCACHED_SUCCESS
;
93 static void sasl_shutdown_function() {
97 static std::atomic
<int> sasl_startup_state(SASL_OK
);
98 static pthread_mutex_t sasl_startup_state_LOCK
= PTHREAD_MUTEX_INITIALIZER
;
99 static pthread_once_t sasl_startup_once
= PTHREAD_ONCE_INIT
;
100 static void sasl_startup_function(void) {
101 sasl_startup_state
= sasl_client_init(NULL
);
103 if (sasl_startup_state
== SASL_OK
) {
104 (void) atexit(sasl_shutdown_function
);
110 memcached_return_t
memcached_sasl_authenticate_connection(memcached_instance_st
*server
) {
111 if (LIBMEMCACHED_WITH_SASL_SUPPORT
== 0) {
112 return MEMCACHED_NOT_SUPPORTED
;
115 if (server
== NULL
) {
116 return MEMCACHED_INVALID_ARGUMENTS
;
119 /* SANITY CHECK: SASL can only be used with the binary protocol */
120 if (memcached_is_binary(server
->root
) == false) {
121 return memcached_set_error(
122 *server
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
,
123 memcached_literal_param(
124 "memcached_sasl_authenticate_connection() is not supported via the ASCII protocol"));
127 /* Try to get the supported mech from the server. Servers without SASL
128 * support will return UNKNOWN COMMAND, so we can just treat that
131 protocol_binary_request_no_extras request
= {};
133 initialize_binary_request(server
, request
.message
.header
);
135 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SASL_LIST_MECHS
;
137 if (memcached_io_write(server
, request
.bytes
, sizeof(request
.bytes
), true)
138 != sizeof(request
.bytes
)) {
139 return MEMCACHED_WRITE_FAILURE
;
141 assert_msg(server
->fd
!= INVALID_SOCKET
, "Programmer error, invalid socket");
143 memcached_server_response_increment(server
);
145 char mech
[MEMCACHED_MAX_BUFFER
] = {0};
146 memcached_return_t rc
= memcached_response(server
, mech
, sizeof(mech
) - 1, NULL
);
147 if (memcached_failed(rc
)) {
148 if (rc
== MEMCACHED_PROTOCOL_ERROR
) {
149 /* If the server doesn't support SASL it will return PROTOCOL_ERROR.
150 * This error may also be returned for other errors, but let's assume
151 * that the server don't support SASL and treat it as success and
152 * let the client fail with the next operation if the error was
153 * caused by another problem....
155 rc
= MEMCACHED_SUCCESS
;
160 assert_msg(server
->fd
!= INVALID_SOCKET
, "Programmer error, invalid socket");
162 /* set ip addresses */
163 char laddr
[MEMCACHED_NI_MAXHOST
+ MEMCACHED_NI_MAXSERV
];
164 char raddr
[MEMCACHED_NI_MAXHOST
+ MEMCACHED_NI_MAXSERV
];
166 if (memcached_failed(rc
= resolve_names(*server
, laddr
, sizeof(laddr
), raddr
, sizeof(raddr
)))) {
171 if ((pthread_error
= pthread_once(&sasl_startup_once
, sasl_startup_function
)) != 0) {
172 return memcached_set_errno(*server
, pthread_error
, MEMCACHED_AT
);
175 (void) pthread_mutex_lock(&sasl_startup_state_LOCK
);
176 if (sasl_startup_state
!= SASL_OK
) {
177 const char *sasl_error_msg
= sasl_errstring(sasl_startup_state
, NULL
, NULL
);
178 return memcached_set_error(*server
, MEMCACHED_AUTH_PROBLEM
, MEMCACHED_AT
,
179 memcached_string_make_from_cstr(sasl_error_msg
));
181 (void) pthread_mutex_unlock(&sasl_startup_state_LOCK
);
185 if ((ret
= sasl_client_new("memcached", server
->_hostname
, laddr
, raddr
,
186 server
->root
->sasl
.callbacks
, 0, &conn
))
189 const char *sasl_error_msg
= sasl_errstring(ret
, NULL
, NULL
);
193 return memcached_set_error(*server
, MEMCACHED_AUTH_PROBLEM
, MEMCACHED_AT
,
194 memcached_string_make_from_cstr(sasl_error_msg
));
198 const char *chosenmech
;
200 ret
= sasl_client_start(conn
, mech
, NULL
, &data
, &len
, &chosenmech
);
201 if (ret
!= SASL_OK
and ret
!= SASL_CONTINUE
) {
202 const char *sasl_error_msg
= sasl_errstring(ret
, NULL
, NULL
);
206 return memcached_set_error(*server
, MEMCACHED_AUTH_PROBLEM
, MEMCACHED_AT
,
207 memcached_string_make_from_cstr(sasl_error_msg
));
209 uint16_t keylen
= (uint16_t) strlen(chosenmech
);
210 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SASL_AUTH
;
211 request
.message
.header
.request
.keylen
= htons(keylen
);
212 request
.message
.header
.request
.bodylen
= htonl(len
+ keylen
);
215 /* send the packet */
217 libmemcached_io_vector_st vector
[] = {
218 {request
.bytes
, sizeof(request
.bytes
)}, {chosenmech
, keylen
}, {data
, len
}};
220 assert_msg(server
->fd
!= INVALID_SOCKET
, "Programmer error, invalid socket");
221 if (memcached_io_writev(server
, vector
, 3, true) == false) {
222 rc
= MEMCACHED_WRITE_FAILURE
;
225 assert_msg(server
->fd
!= INVALID_SOCKET
, "Programmer error, invalid socket");
226 memcached_server_response_increment(server
);
228 /* read the response */
229 assert_msg(server
->fd
!= INVALID_SOCKET
, "Programmer error, invalid socket");
230 rc
= memcached_response(server
, NULL
, 0, NULL
);
231 if (rc
!= MEMCACHED_AUTH_CONTINUE
) {
234 assert_msg(server
->fd
!= INVALID_SOCKET
, "Programmer error, invalid socket");
236 ret
= sasl_client_step(conn
, memcached_result_value(&server
->root
->result
),
237 (unsigned int) memcached_result_length(&server
->root
->result
), NULL
,
240 if (ret
!= SASL_OK
&& ret
!= SASL_CONTINUE
) {
241 rc
= MEMCACHED_AUTH_PROBLEM
;
245 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_SASL_STEP
;
246 request
.message
.header
.request
.bodylen
= htonl(len
+ keylen
);
249 /* Release resources */
252 return memcached_set_error(*server
, rc
, MEMCACHED_AT
);
255 static int get_username(void *context
, int id
, const char **result
, unsigned int *len
) {
256 if (!context
|| !result
|| (id
!= SASL_CB_USER
&& id
!= SASL_CB_AUTHNAME
)) {
257 return SASL_BADPARAM
;
260 *result
= (char *) context
;
262 *len
= (unsigned int) strlen(*result
);
268 static int get_password(sasl_conn_t
*conn
, void *context
, int id
, sasl_secret_t
**psecret
) {
269 if (!conn
|| !psecret
|| id
!= SASL_CB_PASS
) {
270 return SASL_BADPARAM
;
273 *psecret
= (sasl_secret_t
*) context
;
278 memcached_return_t
memcached_set_sasl_auth_data(memcached_st
*shell
, const char *username
,
279 const char *password
) {
280 Memcached
*ptr
= memcached2Memcached(shell
);
281 if (LIBMEMCACHED_WITH_SASL_SUPPORT
== 0) {
282 return MEMCACHED_NOT_SUPPORTED
;
285 if (ptr
== NULL
or username
== NULL
or password
== NULL
) {
286 return MEMCACHED_INVALID_ARGUMENTS
;
289 memcached_return_t ret
;
290 if (memcached_failed(ret
= memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, 1))) {
291 return memcached_set_error(
292 *ptr
, ret
, MEMCACHED_AT
,
293 memcached_literal_param("Unable change to binary protocol which is required for SASL."));
296 memcached_destroy_sasl_auth_data(ptr
);
298 sasl_callback_t
*callbacks
= libmemcached_xcalloc(ptr
, 4, sasl_callback_t
);
299 size_t password_length
= strlen(password
);
300 size_t username_length
= strlen(username
);
301 char *name
= (char *) libmemcached_malloc(ptr
, username_length
+ 1);
302 sasl_secret_t
*secret
=
303 (sasl_secret_t
*) libmemcached_malloc(ptr
, password_length
+ 1 + sizeof(sasl_secret_t
));
305 if (callbacks
== NULL
or name
== NULL
or secret
== NULL
) {
306 libmemcached_free(ptr
, callbacks
);
307 libmemcached_free(ptr
, name
);
308 libmemcached_free(ptr
, secret
);
309 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
312 secret
->len
= password_length
;
313 memcpy(secret
->data
, password
, password_length
);
314 secret
->data
[password_length
] = 0;
316 callbacks
[0].id
= SASL_CB_USER
;
317 callbacks
[0].proc
= CAST_SASL_CB(get_username
);
318 callbacks
[0].context
= strncpy(name
, username
, username_length
+ 1);
319 callbacks
[1].id
= SASL_CB_AUTHNAME
;
320 callbacks
[1].proc
= CAST_SASL_CB(get_username
);
321 callbacks
[1].context
= name
;
322 callbacks
[2].id
= SASL_CB_PASS
;
323 callbacks
[2].proc
= CAST_SASL_CB(get_password
);
324 callbacks
[2].context
= secret
;
325 callbacks
[3].id
= SASL_CB_LIST_END
;
327 ptr
->sasl
.callbacks
= callbacks
;
328 ptr
->sasl
.is_allocated
= true;
330 return MEMCACHED_SUCCESS
;
333 memcached_return_t
memcached_destroy_sasl_auth_data(memcached_st
*shell
) {
334 if (LIBMEMCACHED_WITH_SASL_SUPPORT
== 0) {
335 return MEMCACHED_NOT_SUPPORTED
;
338 Memcached
*ptr
= memcached2Memcached(shell
);
340 return MEMCACHED_INVALID_ARGUMENTS
;
343 if (ptr
->sasl
.callbacks
== NULL
) {
344 return MEMCACHED_SUCCESS
;
347 if (ptr
->sasl
.is_allocated
) {
348 libmemcached_free(ptr
, ptr
->sasl
.callbacks
[0].context
);
349 libmemcached_free(ptr
, ptr
->sasl
.callbacks
[2].context
);
350 libmemcached_free(ptr
, (void *) ptr
->sasl
.callbacks
);
351 ptr
->sasl
.is_allocated
= false;
354 ptr
->sasl
.callbacks
= NULL
;
356 return MEMCACHED_SUCCESS
;
359 memcached_return_t
memcached_clone_sasl(memcached_st
*clone
, const memcached_st
*source
) {
360 if (LIBMEMCACHED_WITH_SASL_SUPPORT
== 0) {
361 return MEMCACHED_NOT_SUPPORTED
;
364 if (clone
== NULL
or source
== NULL
) {
365 return MEMCACHED_INVALID_ARGUMENTS
;
368 if (source
->sasl
.callbacks
== NULL
) {
369 return MEMCACHED_SUCCESS
;
372 /* Hopefully we are using our own callback mechanisms.. */
373 if (source
->sasl
.callbacks
[0].id
== SASL_CB_USER
374 && source
->sasl
.callbacks
[0].proc
== CAST_SASL_CB(get_username
)
375 && source
->sasl
.callbacks
[1].id
== SASL_CB_AUTHNAME
376 && source
->sasl
.callbacks
[1].proc
== CAST_SASL_CB(get_username
)
377 && source
->sasl
.callbacks
[2].id
== SASL_CB_PASS
378 && source
->sasl
.callbacks
[2].proc
== CAST_SASL_CB(get_password
)
379 && source
->sasl
.callbacks
[3].id
== SASL_CB_LIST_END
)
381 sasl_secret_t
*secret
= (sasl_secret_t
*) source
->sasl
.callbacks
[2].context
;
382 return memcached_set_sasl_auth_data(clone
, (const char *) source
->sasl
.callbacks
[0].context
,
383 (const char *) secret
->data
);
387 * But we're not. It may work if we know what the user tries to pass
388 * into the list, but if we don't know the ID we don't know how to handle
393 while (source
->sasl
.callbacks
[total
].id
!= SASL_CB_LIST_END
) {
394 switch (source
->sasl
.callbacks
[total
].id
) {
396 case SASL_CB_AUTHNAME
:
397 case SASL_CB_PASS
: break;
399 /* I don't know how to deal with this... */
400 return MEMCACHED_NOT_SUPPORTED
;
406 sasl_callback_t
*callbacks
= libmemcached_xcalloc(clone
, total
+ 1, sasl_callback_t
);
407 if (callbacks
== NULL
) {
408 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
410 memcpy(callbacks
, source
->sasl
.callbacks
, (total
+ 1) * sizeof(sasl_callback_t
));
412 /* Now update the context... */
413 for (ptrdiff_t x
= 0; x
< total
; ++x
) {
414 if (callbacks
[x
].id
== SASL_CB_USER
|| callbacks
[x
].id
== SASL_CB_AUTHNAME
) {
415 callbacks
[x
].context
= (sasl_callback_t
*) libmemcached_malloc(
416 clone
, strlen((const char *) source
->sasl
.callbacks
[x
].context
));
418 if (callbacks
[x
].context
== NULL
) {
419 /* Failed to allocate memory, clean up previously allocated memory */
420 for (ptrdiff_t y
= 0; y
< x
; ++y
) {
421 libmemcached_free(clone
, clone
->sasl
.callbacks
[y
].context
);
424 libmemcached_free(clone
, callbacks
);
425 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
427 strncpy((char *) callbacks
[x
].context
, (const char *) source
->sasl
.callbacks
[x
].context
,
428 sizeof(callbacks
[x
].context
));
430 sasl_secret_t
*src
= (sasl_secret_t
*) source
->sasl
.callbacks
[x
].context
;
431 sasl_secret_t
*n
= (sasl_secret_t
*) libmemcached_malloc(clone
, src
->len
+ 1 + sizeof(*n
));
433 /* Failed to allocate memory, clean up previously allocated memory */
434 for (ptrdiff_t y
= 0; y
< x
; ++y
) {
435 libmemcached_free(clone
, clone
->sasl
.callbacks
[y
].context
);
438 libmemcached_free(clone
, callbacks
);
439 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
441 memcpy(n
, src
, src
->len
+ 1 + sizeof(*n
));
442 callbacks
[x
].context
= n
;
446 clone
->sasl
.callbacks
= callbacks
;
447 clone
->sasl
.is_allocated
= true;
449 return MEMCACHED_SUCCESS
;
454 void memcached_set_sasl_callbacks(memcached_st
*, const sasl_callback_t
*) {}
456 sasl_callback_t
*memcached_get_sasl_callbacks(memcached_st
*) {
460 memcached_return_t
memcached_set_sasl_auth_data(memcached_st
*, const char *, const char *) {
461 return MEMCACHED_NOT_SUPPORTED
;
464 memcached_return_t
memcached_clone_sasl(memcached_st
*, const memcached_st
*) {
465 return MEMCACHED_NOT_SUPPORTED
;