X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fsasl.cc;h=902ccd872cf77ee7ede0903730a31c6c9bd96f3d;hb=77eab04d2140f8e2bd904f7ac4a59b4eef54244f;hp=f1de41eaec4fe30f86416da7714f625e169c0ba6;hpb=58c279d58e2a44562f729e93e301dfedf42f530b;p=m6w6%2Flibmemcached diff --git a/libmemcached/sasl.cc b/libmemcached/sasl.cc index f1de41ea..902ccd87 100644 --- a/libmemcached/sasl.cc +++ b/libmemcached/sasl.cc @@ -98,12 +98,15 @@ static memcached_return_t resolve_names(memcached_server_st& server, char *laddr return MEMCACHED_SUCCESS; } +extern "C" { + static void sasl_shutdown_function() { sasl_done(); } -static int sasl_startup_state= SASL_OK; +static volatile int sasl_startup_state= SASL_OK; +pthread_mutex_t sasl_startup_state_LOCK= PTHREAD_MUTEX_INITIALIZER; static pthread_once_t sasl_startup_once= PTHREAD_ONCE_INIT; static void sasl_startup_function(void) { @@ -115,6 +118,8 @@ static void sasl_startup_function(void) } } +} // extern "C" + memcached_return_t memcached_sasl_authenticate_connection(memcached_server_st *server) { if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) @@ -128,9 +133,10 @@ memcached_return_t memcached_sasl_authenticate_connection(memcached_server_st *s } /* SANITY CHECK: SASL can only be used with the binary protocol */ - if (server->root->flags.binary_protocol == false) + if (memcached_is_binary(server->root) == false) { - return MEMCACHED_PROTOCOL_ERROR; + return memcached_set_error(*server, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, + memcached_literal_param("memcached_sasl_authenticate_connection() is not supported via the ASCII protocol")); } /* Try to get the supported mech from the server. Servers without SASL @@ -182,12 +188,14 @@ memcached_return_t memcached_sasl_authenticate_connection(memcached_server_st *s return memcached_set_errno(*server, pthread_error, MEMCACHED_AT); } + (void)pthread_mutex_lock(&sasl_startup_state_LOCK); if (sasl_startup_state != SASL_OK) { const char *sasl_error_msg= sasl_errstring(sasl_startup_state, NULL, NULL); return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT, memcached_string_make_from_cstr(sasl_error_msg)); } + (void)pthread_mutex_unlock(&sasl_startup_state_LOCK); sasl_conn_t *conn; int ret; @@ -222,11 +230,11 @@ memcached_return_t memcached_sasl_authenticate_connection(memcached_server_st *s do { /* send the packet */ - struct libmemcached_io_vector_st vector[]= + libmemcached_io_vector_st vector[]= { - { sizeof(request.bytes), request.bytes }, - { keylen, chosenmech }, - { len, data } + { request.bytes, sizeof(request.bytes) }, + { chosenmech, keylen }, + { data, len } }; if (memcached_io_writev(server, vector, 3, true) == -1) @@ -314,7 +322,7 @@ memcached_return_t memcached_set_sasl_auth_data(memcached_st *ptr, memcached_destroy_sasl_auth_data(ptr); - sasl_callback_t *callbacks= (sasl_callback_t*)libmemcached_calloc(ptr, 4, sizeof(sasl_callback_t)); + sasl_callback_t *callbacks= libmemcached_xcalloc(ptr, 4, sasl_callback_t); size_t password_length= strlen(password); size_t username_length= strlen(username); char *name= (char *)libmemcached_malloc(ptr, username_length +1); @@ -434,7 +442,7 @@ memcached_return_t memcached_clone_sasl(memcached_st *clone, const memcached_st ++total; } - sasl_callback_t *callbacks= (sasl_callback_t*)libmemcached_calloc(clone, total +1, sizeof(sasl_callback_t)); + sasl_callback_t *callbacks= libmemcached_xcalloc(clone, total +1, sasl_callback_t); if (callbacks == NULL) { return MEMCACHED_MEMORY_ALLOCATION_FAILURE;