From: Brian Aker Date: Mon, 15 Aug 2011 06:40:24 +0000 (-0700) Subject: Merge in fixes for SASL. X-Git-Tag: 0.52~26 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=b77f874c7d7ff386d01eeedb44c14d3003354bae;p=m6w6%2Flibmemcached Merge in fixes for SASL. --- diff --git a/.bzrignore b/.bzrignore index b0daef42..034514bb 100644 --- a/.bzrignore +++ b/.bzrignore @@ -121,3 +121,7 @@ tests/testudp tests/var/ unittests/unittests libtest/version.h +clients/memping +libtest/skiptest +tests/sasl +tests/c_sasl_test diff --git a/clients/include.am b/clients/include.am index 98511366..8fc8c00b 100644 --- a/clients/include.am +++ b/clients/include.am @@ -3,7 +3,6 @@ # All paths should be given relative to the root CLIENTS_LDADDS= \ - $(LIBM) \ clients/libutilities.la \ libmemcached/libmemcached.la @@ -19,6 +18,7 @@ bin_PROGRAMS+= \ clients/memerror \ clients/memflush \ clients/memparse \ + clients/memping \ clients/memrm \ clients/memslap \ clients/memstat @@ -69,13 +69,17 @@ clients_memrm_LDADD= $(CLIENTS_LDADDS) clients_memflush_SOURCES= clients/memflush.cc clients_memflush_LDADD= $(CLIENTS_LDADDS) +clients_memping_SOURCES= clients/memping.cc +clients_memping_LDADD= $(CLIENTS_LDADDS) libmemcached/libmemcachedutil.la + clients_memerror_SOURCES= clients/memerror.cc clients_memerror_LDADD= $(CLIENTS_LDADDS) clients_memslap_SOURCES = clients/memslap.cc clients_memslap_SOURCES+= clients/generator.cc clients/execute.cc clients_memslap_CXXFLAGS = ${PTHREAD_CFLAGS} -clients_memslap_LDADD = $(PTHREAD_LIBS) $(CLIENTS_LDADDS) +clients_memslap_LDADD= $(CLIENTS_LDADDS) +clients_memslap_LDADD+= $(PTHREAD_LIBS) clients_memaslap_SOURCES= \ clients/memaslap.c \ diff --git a/clients/memcat.cc b/clients/memcat.cc index 12df3479..fdbcb67e 100644 --- a/clients/memcat.cc +++ b/clients/memcat.cc @@ -11,6 +11,7 @@ #include "config.h" +#include #include #include #include @@ -73,7 +74,14 @@ int main(int argc, char *argv[]) memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t)opt_binary); - if (!initialize_sasl(memc, opt_username, opt_passwd)) + if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) + { + memcached_free(memc); + std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl; + return EXIT_FAILURE; + } + + if (opt_username and initialize_sasl(memc, opt_username, opt_passwd) == false) { memcached_free(memc); return EXIT_FAILURE; diff --git a/clients/memcp.cc b/clients/memcp.cc index 3869242b..2a73390b 100644 --- a/clients/memcp.cc +++ b/clients/memcp.cc @@ -11,6 +11,7 @@ #include "config.h" +#include #include #include #include @@ -114,8 +115,17 @@ int main(int argc, char *argv[]) memcached_server_list_free(servers); memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t)opt_binary); - if (!initialize_sasl(memc, opt_username, opt_passwd)) + + if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) + { + memcached_free(memc); + std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl; + return EXIT_FAILURE; + } + + if (initialize_sasl(memc, opt_username, opt_passwd) == false) { + std::cerr << "Failed to initialize SASL support." << std::endl; memcached_free(memc); return EXIT_FAILURE; } diff --git a/clients/memdump.cc b/clients/memdump.cc index 0e81dad4..6f3c3e53 100644 --- a/clients/memdump.cc +++ b/clients/memdump.cc @@ -11,18 +11,18 @@ #include "config.h" -#include -#include -#include -#include +#include +#include +#include +#include +#include #include -#include +#include +#include #include #include -#include -#include -#include -#include +#include +#include #include @@ -89,8 +89,18 @@ int main(int argc, char *argv[]) memcached_server_list_free(servers); memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t)opt_binary); - if (!initialize_sasl(memc, opt_username, opt_passwd)) + + if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) { + memcached_free(memc); + std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl; + return EXIT_FAILURE; + } + + if (opt_username and initialize_sasl(memc, opt_username, opt_passwd) == false) + { + std::cerr << "Failed to initialize SASL support." << std::endl; + memcached_free(memc); return EXIT_FAILURE; } diff --git a/clients/memflush.cc b/clients/memflush.cc index 848bc1e7..b19c2860 100644 --- a/clients/memflush.cc +++ b/clients/memflush.cc @@ -10,10 +10,12 @@ */ #include "config.h" -#include -#include -#include +#include +#include #include +#include +#include + #include #include "client_options.h" #include "utilities.h" @@ -60,6 +62,14 @@ int main(int argc, char *argv[]) memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t) opt_binary); + if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) + { + memcached_free(memc); + std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl; + return EXIT_FAILURE; + } + + if (!initialize_sasl(memc, opt_username, opt_passwd)) { memcached_free(memc); diff --git a/clients/memping.cc b/clients/memping.cc new file mode 100644 index 00000000..ee245cda --- /dev/null +++ b/clients/memping.cc @@ -0,0 +1,147 @@ +/* LibMemcached + * Copyright (C) 2006-2009 Brian Aker + * All rights reserved. + * + * Use and distribution licensed under the BSD license. See + * the COPYING file in the parent directory for full text. + * + * Summary: + * + */ +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include "client_options.h" +#include "utilities.h" + +#include + +static int opt_binary= 0; +static int opt_verbose= 0; +static time_t opt_expire= 0; +static char *opt_servers= NULL; +static char *opt_username; +static char *opt_passwd; + +#define PROGRAM_NAME "memping" +#define PROGRAM_DESCRIPTION "Ping a server to see if it is alive" + +/* Prototypes */ +void options_parse(int argc, char *argv[]); + +int main(int argc, char *argv[]) +{ + options_parse(argc, argv); + + if (opt_servers == NULL) + { + char *temp; + + if ((temp= getenv("MEMCACHED_SERVERS"))) + { + opt_servers= strdup(temp); + } + else + { + std::cerr << "No Servers provided" << std::endl; + exit(EXIT_FAILURE); + } + } + + int exit_code= EXIT_SUCCESS; + memcached_server_st *servers= memcached_servers_parse(opt_servers); + { + for (uint32_t x= 0; x < memcached_server_list_count(servers); x++) + { + memcached_return_t instance_rc; + const char *hostname= servers[x].hostname; + in_port_t port= servers[x].port; + + if (libmemcached_util_ping2(hostname, port, opt_username, opt_passwd, &instance_rc) == false) + { + std::cerr << "Failed to ping " << hostname << ":" << port << " " << memcached_strerror(NULL, instance_rc) << std::endl; + exit_code= EXIT_FAILURE; + } + } + } + memcached_server_list_free(servers); + + free(opt_servers); + + shutdown_sasl(); + + return exit_code; +} + + +void options_parse(int argc, char *argv[]) +{ + memcached_programs_help_st help_options[]= + { + {0}, + }; + + static struct option long_options[]= + { + {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION}, + {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP}, + {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE}, + {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG}, + {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS}, + {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE}, + {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY}, + {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME}, + {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD}, + {0, 0, 0, 0}, + }; + int option_index= 0; + int option_rv; + + while (1) + { + option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index); + if (option_rv == -1) break; + switch (option_rv) + { + case 0: + break; + case OPT_BINARY: + opt_binary = 1; + break; + case OPT_VERBOSE: /* --verbose or -v */ + opt_verbose = OPT_VERBOSE; + break; + case OPT_DEBUG: /* --debug or -d */ + opt_verbose = OPT_DEBUG; + break; + case OPT_VERSION: /* --version or -V */ + version_command(PROGRAM_NAME); + break; + case OPT_HELP: /* --help or -h */ + help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options); + break; + case OPT_SERVERS: /* --servers or -s */ + opt_servers= strdup(optarg); + break; + case OPT_EXPIRE: /* --expire */ + opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10); + break; + case OPT_USERNAME: + opt_username= optarg; + break; + case OPT_PASSWD: + opt_passwd= optarg; + break; + case '?': + /* getopt_long already printed an error message. */ + exit(1); + default: + abort(); + } + } +} diff --git a/clients/memrm.cc b/clients/memrm.cc index d4d93c2e..0551e326 100644 --- a/clients/memrm.cc +++ b/clients/memrm.cc @@ -10,11 +10,13 @@ */ #include "config.h" -#include -#include +#include +#include #include +#include +#include + #include -#include #include "client_options.h" #include "utilities.h" @@ -43,7 +45,7 @@ int main(int argc, char *argv[]) options_parse(argc, argv); initialize_sockets(); - if (!opt_servers) + if (opt_servers == 0) { char *temp; @@ -65,8 +67,18 @@ int main(int argc, char *argv[]) memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t) opt_binary); - if (!initialize_sasl(memc, opt_username, opt_passwd)) + if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) + { + memcached_free(memc); + std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl; + return EXIT_FAILURE; + } + + + if (opt_username and initialize_sasl(memc, opt_username, opt_passwd) == false) { + std::cerr << "Failed to initialize SASL support." << std::endl; + memcached_free(memc); return EXIT_FAILURE; } diff --git a/clients/utilities.cc b/clients/utilities.cc index ca109adc..8be59abb 100644 --- a/clients/utilities.cc +++ b/clients/utilities.cc @@ -127,10 +127,12 @@ void process_hash_option(memcached_st *memc, char *opt_hash) } } -#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT + static char *username; static char *passwd; +#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT + static int get_username(void *context, int id, const char **result, unsigned int *len) { (void)context; @@ -139,7 +141,9 @@ static int get_username(void *context, int id, const char **result, unsigned int *result= username; if (len) + { *len= (username == NULL) ? 0 : (unsigned int)strlen(username); + } return SASL_OK; } @@ -181,44 +185,55 @@ static sasl_callback_t sasl_callbacks[] = { { SASL_CB_PASS, (local_sasl_fn)get_password, NULL }, { SASL_CB_LIST_END, NULL, NULL } }; + #endif bool initialize_sasl(memcached_st *memc, char *user, char *password) { -#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT + if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) + { + return false; + } + + if (memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t)true) == 0) + { + return false; + } + if (user != NULL && password != NULL) { username= user; passwd= password; +#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT if (sasl_client_init(NULL) != SASL_OK) { fprintf(stderr, "Failed to initialize sasl library!\n"); return false; } memcached_set_sasl_callbacks(memc, sasl_callbacks); - } #else - (void)memc; - (void)user; - (void)password; + (void)memc; #endif + } return true; } void shutdown_sasl(void) { -#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT - if (username != NULL || passwd != NULL) + if (username or passwd) + { +#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT sasl_done(); #endif + } } void initialize_sockets(void) { /* Define the function for all platforms to avoid #ifdefs in each program */ -#ifdef WIN32 +#if defined(WIN32) && WIN32 WSADATA wsaData; if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0) { diff --git a/configure.ac b/configure.ac index 98539130..5fc2c6c7 100644 --- a/configure.ac +++ b/configure.ac @@ -54,20 +54,29 @@ AC_SUBST(HASHKIT_LIBRARY_VERSION) LT_INIT m4_include([m4/memcached.m4]) +m4_include([m4/memcached_sasl.m4]) + AM_CONDITIONAL(BUILDING_LIBMEMCACHED, true) AM_CONDITIONAL(HAVE_LIBMEMCACHED, false) +AC_SUBST(_WITH_LIBMEMCACHED_SUPPORT, ["_WITH_LIBMEMCACHED_SUPPORT 1"]) AM_CONDITIONAL(BUILDING_GEARMAN, false) AM_CONDITIONAL(HAVE_LIBGEARMAN, false) -AC_DEFINE([GEARMAN_BINARY], [0], [dummy variable]) +AC_SUBST(_WITH_LIBGEARMAN_SUPPORT, ["_WITH_LIBGEARMAN_SUPPORT 0"]) AC_SEARCH_LIBS(getopt_long, gnugetopt) AC_SEARCH_LIBS(gethostbyname, nsl) PANDORA_HAVE_LIBEVENT -my_saved_libs="$LIBS" -LIBS= -LIBS="$my_saved_libs" + +case "$target_os" in + *linux*) + AS_IF([test "x$GCC" = "xyes"], + [ + LDFLAGS="$LDFLAGS -z relro -z now" + ]) + ;; + esac dnl Specialty checks ACX_PTHREAD @@ -110,6 +119,7 @@ AC_CHECK_HEADERS([netdb.h]) AC_CHECK_HEADERS([netinet/in.h]) AC_CHECK_HEADERS([stddef.h]) AC_CHECK_HEADERS([sys/time.h]) +AC_CHECK_HEADERS([sasl/sasl.h]) AC_FUNC_ALLOCA AC_FUNC_ERROR_AT_LINE AC_FUNC_FORK @@ -131,7 +141,13 @@ AC_TYPE_UINT8_T dnl The sasl functions should only be visible if we build with sasl support AS_IF([test "x$ac_cv_sasl" = "xyes"], - [LIBMEMCACHED_WITH_SASL_SUPPORT="#define LIBMEMCACHED_WITH_SASL_SUPPORT 1"]) + [ + [ LIBMEMCACHED_WITH_SASL_SUPPORT="#define LIBMEMCACHED_WITH_SASL_SUPPORT 1" ] + ], + [ + [ LIBMEMCACHED_WITH_SASL_SUPPORT="#define LIBMEMCACHED_WITH_SASL_SUPPORT 0" ] + ] + ) AC_SUBST(LIBMEMCACHED_WITH_SASL_SUPPORT) AC_CHECK_HEADERS([atomic.h]) diff --git a/libmemcached/connect.cc b/libmemcached/connect.cc index 07bb545b..3c12339c 100644 --- a/libmemcached/connect.cc +++ b/libmemcached/connect.cc @@ -593,18 +593,19 @@ memcached_return_t memcached_connect(memcached_server_write_instance_st ptr) case MEMCACHED_CONNECTION_UDP: case MEMCACHED_CONNECTION_TCP: rc= network_connect(ptr); -#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT - if (ptr->fd != INVALID_SOCKET and ptr->root->sasl.callbacks) + if (LIBMEMCACHED_WITH_SASL_SUPPORT) { - rc= memcached_sasl_authenticate_connection(ptr); - if (memcached_failed(rc) and ptr->fd != INVALID_SOCKET) + if (ptr->fd != INVALID_SOCKET and ptr->root->sasl.callbacks) { - WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET); - (void)closesocket(ptr->fd); - ptr->fd= INVALID_SOCKET; + rc= memcached_sasl_authenticate_connection(ptr); + if (memcached_failed(rc) and ptr->fd != INVALID_SOCKET) + { + WATCHPOINT_ASSERT(ptr->fd != INVALID_SOCKET); + (void)closesocket(ptr->fd); + ptr->fd= INVALID_SOCKET; + } } } -#endif break; case MEMCACHED_CONNECTION_UNIX_SOCKET: diff --git a/libmemcached/include.am b/libmemcached/include.am index 4950f3ca..425cec0b 100644 --- a/libmemcached/include.am +++ b/libmemcached/include.am @@ -141,10 +141,8 @@ libmemcached_libmemcached_la_DEPENDENCIES= libmemcached_libmemcached_la_LIBADD= $(LIBM) libmemcached_libmemcached_la_LDFLAGS+= ${AM_LDFLAGS} -version-info ${MEMCACHED_LIBRARY_VERSION} -if HAVE_SASL -libmemcached_libmemcached_la_LDFLAGS+= $(LTLIBSASL) $(LTLIBSASL2) -libmemcached_libmemcached_la_SOURCES += libmemcached/sasl.c -endif +libmemcached_libmemcached_la_LIBADD+= $(LTLIBSASL) $(LTLIBSASL2) +libmemcached_libmemcached_la_SOURCES += libmemcached/sasl.cc if HAVE_DTRACE BUILT_SOURCES+= libmemcached/dtrace_probes.h diff --git a/libmemcached/memcached.cc b/libmemcached/memcached.cc index 359a4403..ce3fb8e3 100644 --- a/libmemcached/memcached.cc +++ b/libmemcached/memcached.cc @@ -165,11 +165,9 @@ static void _free(memcached_st *ptr, bool release_st) memcached_error_free(*ptr); - if (ptr->sasl.callbacks) + if (LIBMEMCACHED_WITH_SASL_SUPPORT and ptr->sasl.callbacks) { -#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT memcached_destroy_sasl_auth_data(ptr); -#endif } if (release_st) @@ -374,8 +372,7 @@ memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source) new_clone->_namespace= memcached_array_clone(new_clone, source->_namespace); new_clone->configure.filename= memcached_array_clone(new_clone, source->_namespace); -#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT - if (source->sasl.callbacks) + if (LIBMEMCACHED_WITH_SASL_SUPPORT and source->sasl.callbacks) { if (memcached_clone_sasl(new_clone, source) != MEMCACHED_SUCCESS) { @@ -383,7 +380,6 @@ memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source) return NULL; } } -#endif rc= run_distribution(new_clone); diff --git a/libmemcached/protocol/include.am b/libmemcached/protocol/include.am index 9c4c1bc2..d5bd9c46 100644 --- a/libmemcached/protocol/include.am +++ b/libmemcached/protocol/include.am @@ -15,12 +15,14 @@ libmemcached_libmemcachedprotocol_la_SOURCES= \ libmemcached_libmemcachedprotocol_la_CFLAGS= \ ${AM_CFLAGS} \ ${NO_CONVERSION} \ - ${PTHREAD_CFLAGS} \ -DBUILDING_LIBMEMCACHED +libmemcached_libmemcachedprotocol_la_CFLAGS+= ${PTHREAD_CFLAGS} libmemcached_libmemcachedprotocol_la_CXXFLAGS= \ ${AM_CXXFLAGS} \ - ${PTHREAD_CFLAGS} \ -DBUILDING_LIBMEMCACHED +libmemcached_libmemcachedprotocol_la_CXXFLAGS+= ${PTHREAD_CFLAGS} -libmemcached_libmemcachedprotocol_la_LDFLAGS= ${AM_LDFLAGS} ${PTHREAD_LIBS} -version-info ${MEMCACHED_PROTOCAL_LIBRARY_VERSION} +libmemcached_libmemcachedprotocol_la_LIBADD= ${PTHREAD_LIBS} +libmemcached_libmemcachedprotocol_la_LDFLAGS= ${AM_LDFLAGS} +libmemcached_libmemcachedprotocol_la_LDFLAGS+= -version-info ${MEMCACHED_PROTOCAL_LIBRARY_VERSION} diff --git a/libmemcached/sasl.c b/libmemcached/sasl.c deleted file mode 100644 index 434d2db6..00000000 --- a/libmemcached/sasl.c +++ /dev/null @@ -1,408 +0,0 @@ -/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: - * - * Libmemcached library - * - * Copyright (C) 2011 Data Differential, http://datadifferential.com/ - * Copyright (C) 2006-2009 Brian Aker All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * * The names of its contributors may not be used to endorse or - * promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - */ - -#include -#include - -void memcached_set_sasl_callbacks(memcached_st *ptr, - const sasl_callback_t *callbacks) -{ - ptr->sasl.callbacks= callbacks; - ptr->sasl.is_allocated= false; -} - -const sasl_callback_t *memcached_get_sasl_callbacks(memcached_st *ptr) -{ - return ptr->sasl.callbacks; -} - -/** - * Resolve the names for both ends of a connection - * @param fd socket to check - * @param laddr local address (out) - * @param raddr remote address (out) - * @return true on success false otherwise (errno contains more info) - */ -static memcached_return_t resolve_names(int fd, char *laddr, size_t laddr_length, char *raddr, size_t raddr_length) -{ - char host[NI_MAXHOST]; - char port[NI_MAXSERV]; - struct sockaddr_storage saddr; - socklen_t salen= sizeof(saddr); - - if (getsockname(fd, (struct sockaddr *)&saddr, &salen) < 0) - { - return MEMCACHED_ERRNO; - } - - if (getnameinfo((struct sockaddr *)&saddr, salen, host, sizeof(host), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV) < 0) - { - return MEMCACHED_HOST_LOOKUP_FAILURE; - } - - (void)snprintf(laddr, laddr_length, "%s;%s", host, port); - salen= sizeof(saddr); - - if (getpeername(fd, (struct sockaddr *)&saddr, &salen) < 0) - { - return MEMCACHED_ERRNO; - } - - if (getnameinfo((struct sockaddr *)&saddr, salen, host, sizeof(host), - port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV) < 0) - { - return MEMCACHED_HOST_LOOKUP_FAILURE; - } - - (void)snprintf(raddr, raddr_length, "%s;%s", host, port); - - return MEMCACHED_SUCCESS; -} - -memcached_return_t memcached_sasl_authenticate_connection(memcached_server_st *server) -{ - /* SANITY CHECK: SASL can only be used with the binary protocol */ - if (!server->root->flags.binary_protocol) - return MEMCACHED_FAILURE; - - /* Try to get the supported mech from the server. Servers without SASL - * support will return UNKNOWN COMMAND, so we can just treat that - * as authenticated - */ - protocol_binary_request_no_extras request= { - .message.header.request= { - .magic= PROTOCOL_BINARY_REQ, - .opcode= PROTOCOL_BINARY_CMD_SASL_LIST_MECHS - } - }; - - if (memcached_io_write(server, request.bytes, - sizeof(request.bytes), 1) != sizeof(request.bytes)) - { - return MEMCACHED_WRITE_FAILURE; - } - - memcached_server_response_increment(server); - - char mech[MEMCACHED_MAX_BUFFER]; - memcached_return_t rc= memcached_response(server, mech, sizeof(mech), NULL); - if (memcached_failed(rc)) - { - if (rc == MEMCACHED_PROTOCOL_ERROR) - { - /* If the server doesn't support SASL it will return PROTOCOL_ERROR. - * This error may also be returned for other errors, but let's assume - * that the server don't support SASL and treat it as success and - * let the client fail with the next operation if the error was - * caused by another problem.... - */ - rc= MEMCACHED_SUCCESS; - } - - return rc; - } - - /* set ip addresses */ - char laddr[NI_MAXHOST + NI_MAXSERV]; - char raddr[NI_MAXHOST + NI_MAXSERV]; - - if (memcached_failed(rc= resolve_names(server->fd, laddr, sizeof(laddr), raddr, sizeof(raddr)))) - { - return rc; - } - - sasl_conn_t *conn; - int ret= sasl_client_new("memcached", server->hostname, laddr, raddr, server->root->sasl.callbacks, 0, &conn); - if (ret != SASL_OK) - { - return MEMCACHED_AUTH_PROBLEM; - } - - const char *data; - const char *chosenmech; - unsigned int len; - ret= sasl_client_start(conn, mech, NULL, &data, &len, &chosenmech); - - if (ret != SASL_OK && ret != SASL_CONTINUE) - { - rc= MEMCACHED_AUTH_PROBLEM; - goto end; - } - - uint16_t keylen= (uint16_t)strlen(chosenmech); - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SASL_AUTH; - request.message.header.request.keylen= htons(keylen); - request.message.header.request.bodylen= htonl(len + keylen); - - do { - /* send the packet */ - - struct libmemcached_io_vector_st vector[]= - { - { .length= sizeof(request.bytes), .buffer= request.bytes }, - { .length= keylen, .buffer= chosenmech }, - { .length= len, .buffer= data } - }; - - if (memcached_io_writev(server, vector, 3, true) == -1) - { - rc= MEMCACHED_WRITE_FAILURE; - goto end; - } - memcached_server_response_increment(server); - - /* read the response */ - rc= memcached_response(server, NULL, 0, NULL); - if (rc != MEMCACHED_AUTH_CONTINUE) - { - goto end; - } - - ret= sasl_client_step(conn, memcached_result_value(&server->root->result), - (unsigned int)memcached_result_length(&server->root->result), - NULL, &data, &len); - - if (ret != SASL_OK && ret != SASL_CONTINUE) - { - rc= MEMCACHED_AUTH_PROBLEM; - goto end; - } - - request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SASL_STEP; - request.message.header.request.bodylen= htonl(len + keylen); - } while (true); - -end: - /* Release resources */ - sasl_dispose(&conn); - - return rc; -} - -static int get_username(void *context, int id, const char **result, - unsigned int *len) -{ - if (!context || !result || (id != SASL_CB_USER && id != SASL_CB_AUTHNAME)) - { - return SASL_BADPARAM; - } - - *result= context; - if (len) - { - *len= (unsigned int)strlen(*result); - } - - return SASL_OK; -} - -static int get_password(sasl_conn_t *conn, void *context, int id, - sasl_secret_t **psecret) -{ - if (!conn || ! psecret || id != SASL_CB_PASS) - { - return SASL_BADPARAM; - } - - *psecret= context; - - return SASL_OK; -} - -memcached_return_t memcached_set_sasl_auth_data(memcached_st *ptr, - const char *username, - const char *password) -{ - if (ptr == NULL || username == NULL || - password == NULL || ptr->sasl.callbacks != NULL) - { - return MEMCACHED_FAILURE; - } - - sasl_callback_t *callbacks= libmemcached_calloc(ptr, 4, sizeof(sasl_callback_t)); - size_t password_length= strlen(password); - size_t username_length= strlen(username); - char *name= libmemcached_malloc(ptr, username_length +1); - sasl_secret_t *secret= libmemcached_malloc(ptr, password_length +1 + sizeof(sasl_secret_t)); - - if (callbacks == NULL || name == NULL || secret == NULL) - { - libmemcached_free(ptr, callbacks); - libmemcached_free(ptr, name); - libmemcached_free(ptr, secret); - return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - } - - secret->len= password_length; - memcpy(secret->data, password, password_length); - secret->data[password_length]= 0; - - callbacks[0].id= SASL_CB_USER; - callbacks[0].proc= get_username; - callbacks[0].context= strncpy(name, username, username_length +1); - callbacks[1].id= SASL_CB_AUTHNAME; - callbacks[1].proc= get_username; - callbacks[1].context= name; - callbacks[2].id= SASL_CB_PASS; - callbacks[2].proc= get_password; - callbacks[2].context= secret; - callbacks[3].id= SASL_CB_LIST_END; - - ptr->sasl.callbacks= callbacks; - ptr->sasl.is_allocated= true; - - return MEMCACHED_SUCCESS; -} - -memcached_return_t memcached_destroy_sasl_auth_data(memcached_st *ptr) -{ - if (ptr == NULL || ptr->sasl.callbacks == NULL) - { - return MEMCACHED_FAILURE; - } - - if (ptr->sasl.is_allocated) - { - libmemcached_free(ptr, ptr->sasl.callbacks[0].context); - libmemcached_free(ptr, ptr->sasl.callbacks[2].context); - libmemcached_free(ptr, (void*)ptr->sasl.callbacks); - ptr->sasl.is_allocated= false; - } - - ptr->sasl.callbacks= NULL; - - return MEMCACHED_SUCCESS; -} - -memcached_return_t memcached_clone_sasl(memcached_st *clone, const memcached_st *source) -{ - - if (source->sasl.callbacks == NULL) - { - return MEMCACHED_SUCCESS; - } - - /* Hopefully we are using our own callback mechanisms.. */ - if (source->sasl.callbacks[0].id == SASL_CB_USER && - source->sasl.callbacks[0].proc == get_username && - source->sasl.callbacks[1].id == SASL_CB_AUTHNAME && - source->sasl.callbacks[1].proc == get_username && - source->sasl.callbacks[2].id == SASL_CB_PASS && - source->sasl.callbacks[2].proc == get_password && - source->sasl.callbacks[3].id == SASL_CB_LIST_END) - { - sasl_secret_t *secret= source->sasl.callbacks[2].context; - return memcached_set_sasl_auth_data(clone, - source->sasl.callbacks[0].context, - (const char*)secret->data); - } - - /* - * But we're not. It may work if we know what the user tries to pass - * into the list, but if we don't know the ID we don't know how to handle - * the context... - */ - size_t total= 0; - - while (source->sasl.callbacks[total].id != SASL_CB_LIST_END) - { - switch (source->sasl.callbacks[total].id) - { - case SASL_CB_USER: - case SASL_CB_AUTHNAME: - case SASL_CB_PASS: - break; - default: - /* I don't know how to deal with this... */ - return MEMCACHED_NOT_SUPPORTED; - } - - ++total; - } - - sasl_callback_t *callbacks= libmemcached_calloc(clone, total + 1, sizeof(sasl_callback_t)); - if (callbacks == NULL) - { - return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - } - memcpy(callbacks, source->sasl.callbacks, (total + 1) * sizeof(sasl_callback_t)); - - /* Now update the context... */ - for (size_t x= 0; x < total; ++x) - { - if (callbacks[x].id == SASL_CB_USER || callbacks[x].id == SASL_CB_AUTHNAME) - { - callbacks[x].context= libmemcached_malloc(clone, strlen(source->sasl.callbacks[x].context)); - - if (callbacks[x].context == NULL) - { - /* Failed to allocate memory, clean up previously allocated memory */ - for (size_t y= 0; y < x; ++y) - { - libmemcached_free(clone, clone->sasl.callbacks[y].context); - } - - libmemcached_free(clone, callbacks); - return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - } - strncpy(callbacks[x].context, source->sasl.callbacks[x].context, sizeof(callbacks[x].context)); - } - else - { - sasl_secret_t *src = source->sasl.callbacks[x].context; - sasl_secret_t *n = libmemcached_malloc(clone, src->len + 1 + sizeof(*n)); - if (n == NULL) - { - /* Failed to allocate memory, clean up previously allocated memory */ - for (size_t y= 0; y < x; ++y) - { - libmemcached_free(clone, clone->sasl.callbacks[y].context); - } - - libmemcached_free(clone, callbacks); - return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - } - memcpy(n, src, src->len + 1 + sizeof(*n)); - callbacks[x].context= n; - } - } - - clone->sasl.callbacks= callbacks; - clone->sasl.is_allocated= true; - - return MEMCACHED_SUCCESS; -} diff --git a/libmemcached/sasl.cc b/libmemcached/sasl.cc new file mode 100644 index 00000000..4f18ca2a --- /dev/null +++ b/libmemcached/sasl.cc @@ -0,0 +1,475 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * Libmemcached library + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * Copyright (C) 2006-2009 Brian Aker All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT + +#include + +void memcached_set_sasl_callbacks(memcached_st *ptr, + const sasl_callback_t *callbacks) +{ + ptr->sasl.callbacks= const_cast(callbacks); + ptr->sasl.is_allocated= false; +} + +sasl_callback_t *memcached_get_sasl_callbacks(memcached_st *ptr) +{ + return ptr->sasl.callbacks; +} + +/** + * Resolve the names for both ends of a connection + * @param fd socket to check + * @param laddr local address (out) + * @param raddr remote address (out) + * @return true on success false otherwise (errno contains more info) + */ +static memcached_return_t resolve_names(memcached_server_st& server, char *laddr, size_t laddr_length, char *raddr, size_t raddr_length) +{ + char host[NI_MAXHOST]; + char port[NI_MAXSERV]; + struct sockaddr_storage saddr; + socklen_t salen= sizeof(saddr); + + if (getsockname(server.fd, (struct sockaddr *)&saddr, &salen) < 0) + { + return memcached_set_errno(server, MEMCACHED_ERRNO, MEMCACHED_AT); + } + + if (getnameinfo((struct sockaddr *)&saddr, salen, host, sizeof(host), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV) < 0) + { + return MEMCACHED_HOST_LOOKUP_FAILURE; + } + + (void)snprintf(laddr, laddr_length, "%s;%s", host, port); + salen= sizeof(saddr); + + if (getpeername(server.fd, (struct sockaddr *)&saddr, &salen) < 0) + { + return memcached_set_errno(server, MEMCACHED_ERRNO, MEMCACHED_AT); + } + + if (getnameinfo((struct sockaddr *)&saddr, salen, host, sizeof(host), + port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV) < 0) + { + return memcached_set_error(server, MEMCACHED_HOST_LOOKUP_FAILURE, MEMCACHED_AT); + } + + (void)snprintf(raddr, raddr_length, "%s;%s", host, port); + + return MEMCACHED_SUCCESS; +} + +memcached_return_t memcached_sasl_authenticate_connection(memcached_server_st *server) +{ + if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) + { + return MEMCACHED_NOT_SUPPORTED; + } + + if (server == NULL) + { + return MEMCACHED_INVALID_ARGUMENTS; + } + + /* SANITY CHECK: SASL can only be used with the binary protocol */ + if (server->root->flags.binary_protocol == false) + { + return MEMCACHED_PROTOCOL_ERROR; + } + + /* Try to get the supported mech from the server. Servers without SASL + * support will return UNKNOWN COMMAND, so we can just treat that + * as authenticated + */ + protocol_binary_request_no_extras request= { }; + request.message.header.request.magic= PROTOCOL_BINARY_REQ; + request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SASL_LIST_MECHS; + + if (memcached_io_write(server, request.bytes, + sizeof(request.bytes), 1) != sizeof(request.bytes)) + { + return MEMCACHED_WRITE_FAILURE; + } + + memcached_server_response_increment(server); + + char mech[MEMCACHED_MAX_BUFFER]; + memcached_return_t rc= memcached_response(server, mech, sizeof(mech), NULL); + if (memcached_failed(rc)) + { + if (rc == MEMCACHED_PROTOCOL_ERROR) + { + /* If the server doesn't support SASL it will return PROTOCOL_ERROR. + * This error may also be returned for other errors, but let's assume + * that the server don't support SASL and treat it as success and + * let the client fail with the next operation if the error was + * caused by another problem.... + */ + rc= MEMCACHED_SUCCESS; + } + + return rc; + } + + /* set ip addresses */ + char laddr[NI_MAXHOST + NI_MAXSERV]; + char raddr[NI_MAXHOST + NI_MAXSERV]; + + if (memcached_failed(rc= resolve_names(*server, laddr, sizeof(laddr), raddr, sizeof(raddr)))) + { + return rc; + } + + int ret; + if ((ret= sasl_client_init(NULL)) != SASL_OK) + { + const char *sasl_error_msg= sasl_errstring(ret, NULL, NULL); + return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT, + memcached_string_make_from_cstr(sasl_error_msg)); + } + + sasl_conn_t *conn; + if ((ret= sasl_client_new("memcached", server->hostname, laddr, raddr, server->root->sasl.callbacks, 0, &conn) ) != SASL_OK) + { + const char *sasl_error_msg= sasl_errstring(ret, NULL, NULL); + return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT, + memcached_string_make_from_cstr(sasl_error_msg)); + } + + const char *data; + const char *chosenmech; + unsigned int len; + ret= sasl_client_start(conn, mech, NULL, &data, &len, &chosenmech); + if (ret != SASL_OK and ret != SASL_CONTINUE) + { + const char *sasl_error_msg= sasl_errstring(ret, NULL, NULL); + return memcached_set_error(*server, MEMCACHED_AUTH_PROBLEM, MEMCACHED_AT, + memcached_string_make_from_cstr(sasl_error_msg)); + } + uint16_t keylen= (uint16_t)strlen(chosenmech); + request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SASL_AUTH; + request.message.header.request.keylen= htons(keylen); + request.message.header.request.bodylen= htonl(len + keylen); + + do { + /* send the packet */ + + struct libmemcached_io_vector_st vector[]= + { + { sizeof(request.bytes), request.bytes }, + { keylen, chosenmech }, + { len, data } + }; + + if (memcached_io_writev(server, vector, 3, true) == -1) + { + rc= MEMCACHED_WRITE_FAILURE; + break; + } + memcached_server_response_increment(server); + + /* read the response */ + rc= memcached_response(server, NULL, 0, NULL); + if (rc != MEMCACHED_AUTH_CONTINUE) + { + break; + } + + ret= sasl_client_step(conn, memcached_result_value(&server->root->result), + (unsigned int)memcached_result_length(&server->root->result), + NULL, &data, &len); + + if (ret != SASL_OK && ret != SASL_CONTINUE) + { + rc= MEMCACHED_AUTH_PROBLEM; + break; + } + + request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SASL_STEP; + request.message.header.request.bodylen= htonl(len + keylen); + } while (true); + + /* Release resources */ + sasl_dispose(&conn); + + return memcached_set_error(*server, rc, MEMCACHED_AT); +} + +static int get_username(void *context, int id, const char **result, unsigned int *len) +{ + if (!context || !result || (id != SASL_CB_USER && id != SASL_CB_AUTHNAME)) + { + return SASL_BADPARAM; + } + + *result= (char *)context; + if (len) + { + *len= (unsigned int)strlen(*result); + } + + return SASL_OK; +} + +static int get_password(sasl_conn_t *conn, void *context, int id, + sasl_secret_t **psecret) +{ + if (!conn || ! psecret || id != SASL_CB_PASS) + { + return SASL_BADPARAM; + } + + *psecret= (sasl_secret_t *)context; + + return SASL_OK; +} + +memcached_return_t memcached_set_sasl_auth_data(memcached_st *ptr, + const char *username, + const char *password) +{ + if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) + { + return MEMCACHED_NOT_SUPPORTED; + } + + if (ptr == NULL or username == NULL or password == NULL) + { + return MEMCACHED_INVALID_ARGUMENTS; + } + + memcached_destroy_sasl_auth_data(ptr); + + sasl_callback_t *callbacks= (sasl_callback_t*)libmemcached_calloc(ptr, 4, sizeof(sasl_callback_t)); + size_t password_length= strlen(password); + size_t username_length= strlen(username); + char *name= (char *)libmemcached_malloc(ptr, username_length +1); + sasl_secret_t *secret= (sasl_secret_t*)libmemcached_malloc(ptr, password_length +1 + sizeof(sasl_secret_t)); + + if (callbacks == NULL or name == NULL or secret == NULL) + { + libmemcached_free(ptr, callbacks); + libmemcached_free(ptr, name); + libmemcached_free(ptr, secret); + return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT); + } + + secret->len= password_length; + memcpy(secret->data, password, password_length); + secret->data[password_length]= 0; + + callbacks[0].id= SASL_CB_USER; + callbacks[0].proc= (int (*)())get_username; + callbacks[0].context= strncpy(name, username, username_length +1); + callbacks[1].id= SASL_CB_AUTHNAME; + callbacks[1].proc= (int (*)())get_username; + callbacks[1].context= name; + callbacks[2].id= SASL_CB_PASS; + callbacks[2].proc= (int (*)())get_password; + callbacks[2].context= secret; + callbacks[3].id= SASL_CB_LIST_END; + + ptr->sasl.callbacks= callbacks; + ptr->sasl.is_allocated= true; + + return MEMCACHED_SUCCESS; +} + +memcached_return_t memcached_destroy_sasl_auth_data(memcached_st *ptr) +{ + if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) + { + return MEMCACHED_NOT_SUPPORTED; + } + + if (ptr == NULL) + { + return MEMCACHED_INVALID_ARGUMENTS; + } + + if (ptr->sasl.callbacks == NULL) + { + return MEMCACHED_SUCCESS; + } + + if (ptr->sasl.is_allocated) + { + libmemcached_free(ptr, ptr->sasl.callbacks[0].context); + libmemcached_free(ptr, ptr->sasl.callbacks[2].context); + libmemcached_free(ptr, (void*)ptr->sasl.callbacks); + ptr->sasl.is_allocated= false; + } + + ptr->sasl.callbacks= NULL; + + return MEMCACHED_SUCCESS; +} + +memcached_return_t memcached_clone_sasl(memcached_st *clone, const memcached_st *source) +{ + if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) + { + return MEMCACHED_NOT_SUPPORTED; + } + + if (clone == NULL or source == NULL) + { + return MEMCACHED_INVALID_ARGUMENTS; + } + + if (source->sasl.callbacks == NULL) + { + return MEMCACHED_SUCCESS; + } + + /* Hopefully we are using our own callback mechanisms.. */ + if (source->sasl.callbacks[0].id == SASL_CB_USER && + source->sasl.callbacks[0].proc == (int (*)())get_username && + source->sasl.callbacks[1].id == SASL_CB_AUTHNAME && + source->sasl.callbacks[1].proc == (int (*)())get_username && + source->sasl.callbacks[2].id == SASL_CB_PASS && + source->sasl.callbacks[2].proc == (int (*)())get_password && + source->sasl.callbacks[3].id == SASL_CB_LIST_END) + { + sasl_secret_t *secret= (sasl_secret_t *)source->sasl.callbacks[2].context; + return memcached_set_sasl_auth_data(clone, + (const char*)source->sasl.callbacks[0].context, + (const char*)secret->data); + } + + /* + * But we're not. It may work if we know what the user tries to pass + * into the list, but if we don't know the ID we don't know how to handle + * the context... + */ + size_t total= 0; + + while (source->sasl.callbacks[total].id != SASL_CB_LIST_END) + { + switch (source->sasl.callbacks[total].id) + { + case SASL_CB_USER: + case SASL_CB_AUTHNAME: + case SASL_CB_PASS: + break; + default: + /* I don't know how to deal with this... */ + return MEMCACHED_NOT_SUPPORTED; + } + + ++total; + } + + sasl_callback_t *callbacks= (sasl_callback_t*)libmemcached_calloc(clone, total +1, sizeof(sasl_callback_t)); + if (callbacks == NULL) + { + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + } + memcpy(callbacks, source->sasl.callbacks, (total + 1) * sizeof(sasl_callback_t)); + + /* Now update the context... */ + for (size_t x= 0; x < total; ++x) + { + if (callbacks[x].id == SASL_CB_USER || callbacks[x].id == SASL_CB_AUTHNAME) + { + callbacks[x].context= (sasl_callback_t*)libmemcached_malloc(clone, strlen((const char*)source->sasl.callbacks[x].context)); + + if (callbacks[x].context == NULL) + { + /* Failed to allocate memory, clean up previously allocated memory */ + for (size_t y= 0; y < x; ++y) + { + libmemcached_free(clone, clone->sasl.callbacks[y].context); + } + + libmemcached_free(clone, callbacks); + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + } + strncpy((char*)callbacks[x].context, (const char*)source->sasl.callbacks[x].context, sizeof(callbacks[x].context)); + } + else + { + sasl_secret_t *src= (sasl_secret_t *)source->sasl.callbacks[x].context; + sasl_secret_t *n= (sasl_secret_t*)libmemcached_malloc(clone, src->len + 1 + sizeof(*n)); + if (n == NULL) + { + /* Failed to allocate memory, clean up previously allocated memory */ + for (size_t y= 0; y < x; ++y) + { + libmemcached_free(clone, clone->sasl.callbacks[y].context); + } + + libmemcached_free(clone, callbacks); + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + } + memcpy(n, src, src->len + 1 + sizeof(*n)); + callbacks[x].context= n; + } + } + + clone->sasl.callbacks= callbacks; + clone->sasl.is_allocated= true; + + return MEMCACHED_SUCCESS; +} + +#else + +void memcached_set_sasl_callbacks(memcached_st *, const sasl_callback_t *) +{ +} + +sasl_callback_t *memcached_get_sasl_callbacks(memcached_st *) +{ + return NULL; +} + +memcached_return_t memcached_set_sasl_auth_data(memcached_st *, const char *, const char *) +{ + return MEMCACHED_NOT_SUPPORTED; +} + +memcached_return_t memcached_clone_sasl(memcached_st *, const memcached_st *) +{ + return MEMCACHED_NOT_SUPPORTED; +} + +#endif diff --git a/libmemcached/sasl.h b/libmemcached/sasl.h index 5a0236bf..8602246d 100644 --- a/libmemcached/sasl.h +++ b/libmemcached/sasl.h @@ -37,8 +37,11 @@ #pragma once -#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT +#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT #include +#else +#define sasl_callback_t void +#endif #ifdef __cplusplus extern "C" { @@ -58,7 +61,7 @@ memcached_return_t memcached_destroy_sasl_auth_data(memcached_st *ptr); LIBMEMCACHED_API -const sasl_callback_t *memcached_get_sasl_callbacks(memcached_st *ptr); +sasl_callback_t *memcached_get_sasl_callbacks(memcached_st *ptr); LIBMEMCACHED_LOCAL memcached_return_t memcached_clone_sasl(memcached_st *clone, const memcached_st *source); @@ -70,17 +73,11 @@ memcached_return_t memcached_sasl_authenticate_connection(memcached_server_st *s } #endif -#endif /* LIBMEMCACHED_WITH_SASL_SUPPORT */ - struct memcached_sasl_st { -#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT - const sasl_callback_t *callbacks; -#else - const void *callbacks; -#endif - /* - ** Did we allocate data inside the callbacks, or did the user - ** supply that. - */ - bool is_allocated; + sasl_callback_t *callbacks; + /* + ** Did we allocate data inside the callbacks, or did the user + ** supply that. + */ + bool is_allocated; }; diff --git a/libmemcached/util/include.am b/libmemcached/util/include.am index 2f446517..10f3b944 100644 --- a/libmemcached/util/include.am +++ b/libmemcached/util/include.am @@ -19,17 +19,13 @@ libmemcached_libmemcachedutil_la_SOURCES= \ libmemcached/util/ping.cc \ libmemcached/util/pool.cc \ libmemcached/util/version.cc -libmemcached_libmemcachedutil_la_CFLAGS= \ - ${AM_CFLAGS} \ - ${NO_CONVERSION} \ - ${PTHREAD_CFLAGS} \ - -DBUILDING_LIBMEMCACHED libmemcached_libmemcachedutil_la_CXXFLAGS= \ ${AM_CXXFLAGS} \ ${NO_CONVERSION} \ - ${PTHREAD_CFLAGS} \ -DBUILDING_LIBMEMCACHED -libmemcached_libmemcachedutil_la_LIBADD= libmemcached/libmemcached.la ${PTHREAD_LIBS} +libmemcached_libmemcachedutil_la_CXXFLAGS+= ${PTHREAD_CFLAGS} +libmemcached_libmemcachedutil_la_LIBADD= libmemcached/libmemcached.la +libmemcached_libmemcachedutil_la_LIBADD+= ${PTHREAD_LIBS} libmemcached_libmemcachedutil_la_LDFLAGS= ${AM_LDFLAGS} -version-info ${MEMCACHED_UTIL_LIBRARY_VERSION} libmemcached_libmemcachedutil_la_DEPENDENCIES= libmemcached/libmemcached.la diff --git a/libmemcached/util/pid.cc b/libmemcached/util/pid.cc index 2d7a86dd..de009fbb 100644 --- a/libmemcached/util/pid.cc +++ b/libmemcached/util/pid.cc @@ -92,3 +92,74 @@ pid_t libmemcached_util_getpid(const char *hostname, in_port_t port, memcached_r return pid; } +pid_t libmemcached_util_getpid2(const char *hostname, in_port_t port, const char *username, const char *password, memcached_return_t *ret) +{ + if (username == NULL) + { + return libmemcached_util_getpid(hostname, port, ret); + } + + pid_t pid= -1; + + memcached_return_t unused; + if (not ret) + ret= &unused; + + if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) + { + *ret= MEMCACHED_NOT_SUPPORTED; + return pid; + } + + memcached_st *memc_ptr= memcached_create(NULL); + if (not memc_ptr) + { + *ret= MEMCACHED_MEMORY_ALLOCATION_FAILURE; + return -1; + } + + if (memcached_failed(*ret= memcached_behavior_set(memc_ptr, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1))) + { + memcached_free(memc_ptr); + return false; + } + + if (memcached_failed(*ret= memcached_set_sasl_auth_data(memc_ptr, username, password))) + { + memcached_free(memc_ptr); + return false; + } + + + memcached_return_t rc= memcached_server_add(memc_ptr, hostname, port); + if (memcached_success(rc)) + { + memcached_stat_st *stat= memcached_stat(memc_ptr, NULL, &rc); + if (memcached_success(rc) and stat and stat->pid != -1) + { + pid= stat->pid; + } + else if (memcached_success(rc)) + { + rc= MEMCACHED_UNKNOWN_STAT_KEY; // Something went wrong if this happens + } + else if (rc == MEMCACHED_SOME_ERRORS) // Generic answer, we will now find the specific reason (if one exists) + { + memcached_server_instance_st instance= + memcached_server_instance_by_position(memc_ptr, 0); + + assert_msg(instance and instance->error_messages, " "); + if (instance and instance->error_messages) + { + rc= memcached_server_error_return(instance); + } + } + + memcached_stat_free(memc_ptr, stat); + } + memcached_free(memc_ptr); + + *ret= rc; + + return pid; +} diff --git a/libmemcached/util/pid.h b/libmemcached/util/pid.h index f2fb7488..4101c11f 100644 --- a/libmemcached/util/pid.h +++ b/libmemcached/util/pid.h @@ -43,6 +43,9 @@ extern "C" { LIBMEMCACHED_API pid_t libmemcached_util_getpid(const char *hostname, in_port_t port, memcached_return_t *ret); +LIBMEMCACHED_API +pid_t libmemcached_util_getpid2(const char *hostname, in_port_t port, const char *username, const char *password, memcached_return_t *ret); + #ifdef __cplusplus } #endif diff --git a/libmemcached/util/ping.cc b/libmemcached/util/ping.cc index 45f94b19..907f6dc0 100644 --- a/libmemcached/util/ping.cc +++ b/libmemcached/util/ping.cc @@ -40,7 +40,6 @@ #include #include - bool libmemcached_util_ping(const char *hostname, in_port_t port, memcached_return_t *ret) { memcached_return_t unused; @@ -77,3 +76,63 @@ bool libmemcached_util_ping(const char *hostname, in_port_t port, memcached_retu return memcached_success(rc); } + +bool libmemcached_util_ping2(const char *hostname, in_port_t port, const char *username, const char *password, memcached_return_t *ret) +{ + if (username == NULL) + { + return libmemcached_util_ping(hostname, port, ret); + } + + memcached_return_t unused; + if (not ret) + ret= &unused; + + if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) + { + *ret= MEMCACHED_NOT_SUPPORTED; + return false; + } + + memcached_st *memc_ptr= memcached_create(NULL); + if (not memc_ptr) + { + *ret= MEMCACHED_MEMORY_ALLOCATION_FAILURE; + return false; + } + + if (memcached_failed(*ret= memcached_behavior_set(memc_ptr, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1))) + { + memcached_free(memc_ptr); + return false; + } + + if (memcached_failed(*ret= memcached_set_sasl_auth_data(memc_ptr, username, password))) + { + memcached_free(memc_ptr); + return false; + } + + memcached_return_t rc= memcached_server_add(memc_ptr, hostname, port); + if (memcached_success(rc)) + { + rc= memcached_version(memc_ptr); + } + + if (memcached_failed(rc) and rc == MEMCACHED_SOME_ERRORS) + { + memcached_server_instance_st instance= + memcached_server_instance_by_position(memc_ptr, 0); + + assert_msg(instance and instance->error_messages, " "); + if (instance and instance->error_messages) + { + rc= memcached_server_error_return(instance); + } + } + memcached_free(memc_ptr); + + *ret= rc; + + return memcached_success(rc); +} diff --git a/libmemcached/util/ping.h b/libmemcached/util/ping.h index e9f3bc50..faea8b2b 100644 --- a/libmemcached/util/ping.h +++ b/libmemcached/util/ping.h @@ -44,6 +44,9 @@ extern "C" { LIBMEMCACHED_API bool libmemcached_util_ping(const char *hostname, in_port_t port, memcached_return_t *ret); +LIBMEMCACHED_API +bool libmemcached_util_ping2(const char *hostname, in_port_t port, const char *username, const char *password, memcached_return_t *ret); + #ifdef __cplusplus } #endif diff --git a/libmemcachedinternal/util/include.am b/libmemcachedinternal/util/include.am index c18a4eff..7b5e8007 100644 --- a/libmemcachedinternal/util/include.am +++ b/libmemcachedinternal/util/include.am @@ -8,16 +8,11 @@ noinst_LTLIBRARIES+= libmemcachedinternal/libmemcachedutilinternal.la libmemcachedinternal_libmemcachedutilinternal_la_SOURCES= $(libmemcached_libmemcachedutil_la_SOURCES) -libmemcachedinternal_libmemcachedutilinternal_la_CFLAGS= \ - ${AM_CFLAGS} \ - ${NO_CONVERSION} \ - ${PTHREAD_CFLAGS} \ - -DBUILDING_LIBMEMCACHEDINTERNAL libmemcachedinternal_libmemcachedutilinternal_la_CXXFLAGS= \ ${AM_CXXFLAGS} \ ${NO_CONVERSION} \ - ${PTHREAD_CFLAGS} \ -DBUILDING_LIBMEMCACHEDINTERNAL +libmemcachedinternal_libmemcachedutilinternal_la_CXXFLAGS+= ${PTHREAD_CFLAGS} libmemcachedinternal_libmemcachedutilinternal_la_LIBADD= libmemcachedinternal/libmemcachedinternal.la ${PTHREAD_LIBS} libmemcachedinternal_libmemcachedutilinternal_la_DEPENDENCIES= libmemcachedinternal/libmemcachedinternal.la diff --git a/libtest/blobslap_worker.cc b/libtest/blobslap_worker.cc index a12df583..378f8268 100644 --- a/libtest/blobslap_worker.cc +++ b/libtest/blobslap_worker.cc @@ -52,24 +52,35 @@ private: public: BlobslapWorker(in_port_t port_arg) : Server("localhost", port_arg) - { } + { + set_pid_file(); + } pid_t get_pid(bool error_is_ok) { - if (not pid_file().empty()) + if (pid_file().empty()) + { + Error << "pid_file was empty"; + return -1; + } + + Wait wait(pid_file(), 0); + + if (error_is_ok and not wait.successful()) { - Wait wait(pid_file(), 0); + Error << "Pidfile was not found:" << pid_file(); + return -1; + } - if (error_is_ok and not wait.successful()) - { - Error << "Pidfile was not found:" << pid_file(); - return -1; + std::stringstream error_message; + pid_t ret= get_pid_from_file(pid_file(), error_message); - return get_pid_from_file(pid_file()); - } + if (error_is_ok and is_pid_valid(ret) == false) + { + Error << error_message.str(); } - return -1; + return ret; } bool ping() @@ -87,12 +98,15 @@ public: return false; } - pid_t local_pid= get_pid_from_file(pid_file()); - if (local_pid <= 0) + std::stringstream error_message; + pid_t local_pid= get_pid_from_file(pid_file(), error_message); + if (is_pid_valid(local_pid) == false) { + Error << error_message.str(); return false; } + // Use kill to determine is the process exist if (::kill(local_pid, 0) == 0) { return true; @@ -123,7 +137,7 @@ public: const char *log_file_option() { - return NULL; + return "--log-file="; } const char *port_option() diff --git a/libtest/cmdline.cc b/libtest/cmdline.cc index b48d6917..421fa006 100644 --- a/libtest/cmdline.cc +++ b/libtest/cmdline.cc @@ -33,7 +33,7 @@ bool exec_cmdline(const std::string& executable, const char *args[]) { std::stringstream arg_buffer; - arg_buffer << "./libtool --mode=execute "; + arg_buffer << libtool(); if (getenv("LIBTEST_TEST_ENVIRONMENT")) { @@ -64,4 +64,9 @@ bool exec_cmdline(const std::string& executable, const char *args[]) return true; } +const char *gearmand_binary() +{ + return GEARMAND_BINARY; +} + } // namespace exec_cmdline diff --git a/libtest/cmdline.h b/libtest/cmdline.h index 0fcfe39c..dcb4b0ab 100644 --- a/libtest/cmdline.h +++ b/libtest/cmdline.h @@ -25,4 +25,6 @@ namespace libtest { bool exec_cmdline(const std::string& executable, const char *args[]); +const char *gearmand_binary(); + } diff --git a/libtest/common.h b/libtest/common.h index 9963682e..8f2c0108 100644 --- a/libtest/common.h +++ b/libtest/common.h @@ -53,8 +53,15 @@ #include #endif +static inline bool is_pid_valid(const pid_t pid) +{ + return (pid > 1) ? true : false; +} + #include #include #include +#include + #include diff --git a/libtest/comparison.hpp b/libtest/comparison.hpp index 6f7c5a5b..5c687fc3 100644 --- a/libtest/comparison.hpp +++ b/libtest/comparison.hpp @@ -47,18 +47,22 @@ bool _compare_true_hint(const char *file, int line, const char *func, T_comparab } template -bool _compare(const char *file, int line, const char *func, T_comparable __expected, T_comparable __actual) +bool _compare(const char *file, int line, const char *func, const T_comparable __expected, const T_comparable __actual) { if (__expected != __actual) { if (typeid(__expected) == typeid(test_return_t)) { + const char *expected_str= test_strerror(test_return_t(__expected)); + const char *got_str= test_strerror(test_return_t(__actual)); + libtest::stream::make_cerr(file, line, func) << "Expected \"" - << test_strerror(test_return_t(__expected)) + << expected_str << "\" got \"" - << test_strerror(test_return_t(__actual)) << "\""; + << got_str + << "\""; } -#if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED +#if (defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED) else if (typeid(__expected) == typeid(memcached_return_t)) { libtest::stream::make_cerr(file, line, func) << "Expected \"" diff --git a/libtest/framework.h b/libtest/framework.h index 69941b1b..90908244 100644 --- a/libtest/framework.h +++ b/libtest/framework.h @@ -143,6 +143,11 @@ public: { _servers.set_socket(); } + + void set_sasl(const std::string& username_arg, const std::string& password_arg) + { + _servers.set_sasl(username_arg, password_arg); + } /** Runner represents the callers for the tests. If not implemented we will use diff --git a/libtest/gearmand.cc b/libtest/gearmand.cc index 6149b3c6..deac09f3 100644 --- a/libtest/gearmand.cc +++ b/libtest/gearmand.cc @@ -37,6 +37,7 @@ using namespace libtest; #include #include #include +#include #include #include #include @@ -66,13 +67,12 @@ public: bool call(const bool success, const std::string &response) { _pid= -1; - if (success and response.size()) { _pid= atoi(response.c_str()); } - if (_pid < 1) + if (is_pid_valid(_pid) == false) { _pid= -1; return false; @@ -90,7 +90,9 @@ private: public: Gearmand(const std::string& host_arg, in_port_t port_arg) : libtest::Server(host_arg, port_arg) - { } + { + set_pid_file(); + } pid_t get_pid(bool error_is_ok) { @@ -183,9 +185,6 @@ public: bool build(int argc, const char *argv[]); }; - -#include - bool Gearmand::build(int argc, const char *argv[]) { std::stringstream arg_buffer; diff --git a/libtest/include.am b/libtest/include.am index d65c4169..3ee7d3f9 100644 --- a/libtest/include.am +++ b/libtest/include.am @@ -20,9 +20,7 @@ # All paths should be given relative to the root # -LIBTEST_TMP = ${abs_top_builddir}/tests/var/tmp/ - -LIBTOOL_COMMAND=$(LIBTOOL) --mode=execute +LIBTOOL_COMMAND= ${abs_top_builddir}/libtool --mode=execute VALGRIND_COMMAND= $(LIBTOOL_COMMAND) valgrind --error-exitcode=1 --leak-check=yes --show-reachable=yes --track-fds=yes --malloc-fill=A5 --free-fill=DE HELGRIND_COMMAND= $(LIBTOOL_COMMAND) valgrind --tool=helgrind --read-var-info=yes --error-exitcode=1 DRD_COMMAND= $(LIBTOOL_COMMAND) valgrind --tool=drd @@ -34,16 +32,25 @@ export HELGRIND_COMMAND export DRD_COMMAND export GDB_COMMAND +valgrind: + @echo make check TESTS_ENVIRONMENT="\"$(VALGRIND_COMMAND)\"" + +helgrind: + @echo make check TESTS_ENVIRONMENT="\"$(HELGRIND_COMMAND)\"" + +drd: + @echo make check TESTS_ENVIRONMENT="\"$(DRD_COMMAND)\"" + EXTRA_DIST+= libtest/run.gdb CLEANFILES+= \ - tests/var/log/* \ - tests/var/run/* \ - tests/var/tmp/* + tmp_chroot/var/log/* \ + tmp_chroot/var/run/* \ + tmp_chroot/var/tmp/* -.PHONY: distclean-tests-check -distclean-tests-check: - -rm -rf tests/var +.PHONY: distclean-libtest-check +distclean-libtest-check: + -rm -rf tmp_chroot noinst_HEADERS+= \ libtest/blobslap_worker.h \ @@ -59,6 +66,7 @@ noinst_HEADERS+= \ libtest/gearmand.h \ libtest/get.h \ libtest/killpid.h \ + libtest/libtool.hpp \ libtest/memcached.h \ libtest/runner.h \ libtest/server.h \ @@ -77,6 +85,7 @@ libtest_libtest_la_SOURCES= \ libtest/cmdline.cc \ libtest/framework.cc \ libtest/killpid.cc \ + libtest/libtool.cc \ libtest/runner.cc \ libtest/server.cc \ libtest/signal.cc \ @@ -86,11 +95,13 @@ libtest_libtest_la_CXXFLAGS= ${AM_CXXFLAGS} libtest_libtest_la_CXXFLAGS+= ${NO_CONVERSION} libtest_libtest_la_CXXFLAGS+= -DBUILDING_LIBTEST libtest_libtest_la_CXXFLAGS+= $(PTHREAD_CFLAGS) +libtest_libtest_la_CXXFLAGS+= -DLIBTEST_TEMP="\"tmp_chroot\"" libtest_libtest_la_LIBADD= libtest_libtest_la_LIBADD+= $(PTHREAD_LIBS) -libtest_libtest_la_DEPENDENCIES= +libtest_libtest_la_DEPENDENCIES= libtest_tmp_dir -libtest_unittest_CXXFLAGS= ${AM_CXXFLAGS} +# Declare unittest so that we can append to it +libtest_unittest_CXXFLAGS= libtest_unittest_LDADD= libtest_unittest_DEPENDENCIES= @@ -106,9 +117,11 @@ libtest_libtest_la_CXXFLAGS+= -DHAVE_LIBMEMCACHED libtest_unittest_CXXFLAGS+= -DHAVE_LIBMEMCACHED libtest_unittest_LDADD+= libmemcached/libmemcached.la libtest_unittest_LDADD+= libmemcached/libmemcachedutil.la +libtest_unittest_DEPENDENCIES+= libmemcached/libmemcached.la +libtest_unittest_DEPENDENCIES+= libmemcached/libmemcachedutil.la else if HAVE_LIBMEMCACHED -libtest_libtest_la_LIBADD+= $(libmemcached_LIBS) -lmemcachedutil +libtest_libtest_la_LIBADD+= $(libmemcached_LIBS) libtest_libtest_la_SOURCES+= libtest/memcached.cc else libtest_libtest_la_CXXFLAGS+= -DHAVE_LIBMEMCACHED=0 @@ -124,10 +137,10 @@ libtest_libtest_la_SOURCES+= libtest/gearmand.cc libtest_libtest_la_SOURCES+= util/instance.cc libtest_libtest_la_SOURCES+= util/operation.cc libtest_libtest_la_CXXFLAGS+= -DHAVE_LIBGEARMAN -libtest_libtest_la_CXXFLAGS+= -DGEARMAND_BINARY="gearmand/gearmand" +libtest_libtest_la_CXXFLAGS+= -DGEARMAND_BINARY="\"${abs_top_builddir}/gearmand/gearmand\"" +libtest_libtest_la_CXXFLAGS+= -DGEARMAND_BLOBSLAP_WORKER="\"${abs_top_builddir}/benchmark/blobslap_worker\"" libtest_unittest_CXXFLAGS+= -DHAVE_LIBGEARMAN -libtest_unittest_CXXFLAGS+= -DGEARMAND_BINARY="gearmand/gearmand" libtest_unittest_LDADD+= libgearman/libgearman.la libtest_unittest_DEPENDENCIES+= libgearman/libgearman.la else @@ -140,32 +153,36 @@ libtest_libtest_la_SOURCES+= util/instance.cc libtest_libtest_la_SOURCES+= util/operation.cc else libtest_libtest_la_CXXFLAGS+= -DGEARMAND_BINARY=0 +libtest_libtest_la_CXXFLAGS+= -DGEARMAND_BLOBSLAP_WORKER=0 libtest_libtest_la_CXXFLAGS+= -DHAVE_LIBGEARMAN=0 libtest_unittest_CXXFLAGS+= -DGEARMAND_BINARY=0 libtest_unittest_CXXFLAGS+= -DHAVE_LIBGEARMAN=0 endif endif -libtest_tmp_dir: tests/var/log tests/var/tmp tests/var/run +libtest_tmp_dir: tmp_chroot/var/log tmp_chroot/var/tmp tmp_chroot/var/run + +tmp_chroot: + @$(mkdir_p) tmp_chroot -tests/var: - @$(mkdir_p) tests/var +tmp_chroot/var: tmp_chroot + @$(mkdir_p) tmp_chroot/var -tests/var/log: tests/var - @$(mkdir_p) tests/var/log +tmp_chroot/var/log: tmp_chroot/var + @$(mkdir_p) tmp_chroot/var/log -tests/var/tmp: tests/var - @$(mkdir_p) tests/var/tmp +tmp_chroot/var/tmp: tmp_chroot/var + @$(mkdir_p) tmp_chroot/var/tmp -tests/var/run: tests/var - @$(mkdir_p) tests/var/run +tmp_chroot/var/run: tmp_chroot/var + @$(mkdir_p) tmp_chroot/var/run +libtest_unittest_CXXFLAGS+= ${AM_CXXFLAGS} libtest_unittest_DEPENDENCIES+= libtest/libtest.la libtest_tmp_dir libtest_unittest_LDADD+= libtest/libtest.la libtest_unittest_SOURCES= libtest/unittest.cc check_PROGRAMS+= libtest/unittest -noinst_PROGRAMS+= libtest/unittest test-unittest: libtest/unittest @libtest/unittest @@ -181,5 +198,10 @@ helgrind-unittest: libtest/unittest drd-unittest: libtest/unittest @$(DRD_COMMAND) libtest/unittest +libtest_skiptest_LDADD= libtest/libtest.la +libtest_skiptest_SOURCES= libtest/skiptest.cc +check_PROGRAMS+= libtest/skiptest +noinst_PROGRAMS+= libtest/skiptest + libtest_wait_SOURCES= libtest/wait.cc noinst_PROGRAMS+= libtest/wait diff --git a/libtest/killpid.cc b/libtest/killpid.cc index 7bbdd636..8a1543f4 100644 --- a/libtest/killpid.cc +++ b/libtest/killpid.cc @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -114,14 +115,18 @@ pid_t kill_file(const std::string &filename) return ret; } -pid_t get_pid_from_file(const std::string &filename) +#define STRINGIFY(x) #x +#define TOSTRING(x) STRINGIFY(x) +#define LIBTEST_AT __FILE__ ":" TOSTRING(__LINE__) + +pid_t get_pid_from_file(const std::string &filename, std::stringstream& error_message) { pid_t ret= -1; FILE *fp; if (filename.empty()) { - Error << "empty pid file"; + error_message << LIBTEST_AT << " empty pid file"; return ret; } @@ -135,11 +140,23 @@ pid_t get_pid_from_file(const std::string &filename) if (ptr) { ret= (pid_t)atoi(pid_buffer); - if (ret <= 0) + if (ret < 1) { - return ret; + error_message << LIBTEST_AT << " Invalid pid was read from file " << filename; } } + else + { + error_message << LIBTEST_AT << " File " << filename << " was empty "; + } + + return ret; + } + else + { + char buffer[1024]; + char *current_directory= getcwd(buffer, sizeof(buffer)); + error_message << "Error while opening " << current_directory << "/" << filename << " " << strerror(errno); } return ret; diff --git a/libtest/killpid.h b/libtest/killpid.h index 83c7bb0a..becfd0ed 100644 --- a/libtest/killpid.h +++ b/libtest/killpid.h @@ -27,4 +27,4 @@ bool kill_pid(pid_t pid_arg); pid_t kill_file(const std::string &filename); -pid_t get_pid_from_file(const std::string &filename); +pid_t get_pid_from_file(const std::string &filename, std::stringstream& error_message); diff --git a/libtest/libtool.cc b/libtest/libtool.cc new file mode 100644 index 00000000..c7aa29d7 --- /dev/null +++ b/libtest/libtool.cc @@ -0,0 +1,58 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * libtest + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +char _libtool[1024]= { 0 }; + +namespace libtest { + +const char *libtool(void) +{ + if (_libtool[0]) + { + std::string libtool_buffer; + if (getenv("srcdir")) + { + libtool_buffer+= getenv("srcdir"); + libtool_buffer+= "/"; + } + else + { + libtool_buffer+= "./"; + } + + libtool_buffer+= "libtool"; + if (access(libtool_buffer.c_str(), R_OK | W_OK | X_OK)) + { + return NULL; + } + + libtool_buffer+= " --mode=execute "; + + snprintf(_libtool, sizeof(_libtool), "%s", libtool_buffer.c_str()); + } + + return _libtool; +} + +} diff --git a/libtest/libtool.hpp b/libtest/libtool.hpp new file mode 100644 index 00000000..d523aa28 --- /dev/null +++ b/libtest/libtool.hpp @@ -0,0 +1,28 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * libtest + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#pragma once + +namespace libtest { + +const char *libtool(void); + +} diff --git a/libtest/memcached.cc b/libtest/memcached.cc index 1344c618..3aba2543 100644 --- a/libtest/memcached.cc +++ b/libtest/memcached.cc @@ -51,10 +51,36 @@ using namespace libtest; class Memcached : public libtest::Server { + std::string _username; + std::string _password; + public: + Memcached(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg, const std::string& username_arg, const std::string& password_arg) : + libtest::Server(host_arg, port_arg, is_socket_arg), + _username(username_arg), + _password(password_arg) + { } + Memcached(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg) : libtest::Server(host_arg, port_arg, is_socket_arg) - { } + { + set_pid_file(); + } + + virtual const char *sasl() const + { + return NULL; + } + + const std::string& password() const + { + return _password; + } + + const std::string& username() const + { + return _username; + } pid_t get_pid(bool error_is_ok) { @@ -74,14 +100,28 @@ public: memcached_return_t rc; if (has_socket()) { - local_pid= libmemcached_util_getpid(socket().c_str(), port(), &rc); + if (username().empty()) + { + local_pid= libmemcached_util_getpid(socket().c_str(), 0, &rc); + } + else + { + local_pid= libmemcached_util_getpid2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc); + } } else { - local_pid= libmemcached_util_getpid(hostname().c_str(), port(), &rc); + if (username().empty()) + { + local_pid= libmemcached_util_getpid(hostname().c_str(), port(), &rc); + } + else + { + local_pid= libmemcached_util_getpid2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc); + } } - if (error_is_ok and ((memcached_failed(rc) or local_pid < 1))) + if (error_is_ok and ((memcached_failed(rc) or not is_pid_valid(local_pid)))) { Error << "libmemcached_util_getpid(" << memcached_strerror(NULL, rc) << ") pid: " << local_pid << " for:" << *this; } @@ -105,19 +145,42 @@ public: memcached_return_t rc; bool ret; + if (has_socket()) { - ret= libmemcached_util_ping(socket().c_str(), 0, &rc); + if (username().empty()) + { + ret= libmemcached_util_ping(socket().c_str(), 0, &rc); + } + else + { + ret= libmemcached_util_ping2(socket().c_str(), 0, username().c_str(), password().c_str(), &rc); + } } else { - ret= libmemcached_util_ping(hostname().c_str(), port(), &rc); + if (username().empty()) + { + ret= libmemcached_util_ping(hostname().c_str(), port(), &rc); + } + else + { + ret= libmemcached_util_ping2(hostname().c_str(), port(), username().c_str(), password().c_str(), &rc); + } } if (memcached_failed(rc) or not ret) { - Error << "libmemcached_util_ping(" << memcached_strerror(NULL, rc) << ")"; + if (username().empty()) + { + Error << "libmemcached_util_ping(" << hostname() << ", " << port() << ") error: " << memcached_strerror(NULL, rc); + } + else + { + Error << "libmemcached_util_ping2(" << hostname() << ", " << port() << ", " << username() << ", " << password() << ") error: " << memcached_strerror(NULL, rc); + } } + return ret; } @@ -175,6 +238,30 @@ public: bool build(int argc, const char *argv[]); }; +class MemcachedSaSL : public Memcached +{ +public: + MemcachedSaSL(const std::string& host_arg, const in_port_t port_arg, const bool is_socket_arg, const std::string& username_arg, const std::string &password_arg) : + Memcached(host_arg, port_arg, is_socket_arg, username_arg, password_arg) + { } + + const char *name() + { + return "memcached-sasl"; + }; + + const char *sasl() const + { + return " -S -B binary "; + } + + const char *executable() + { + return MEMCACHED_SASL_BINARY; + } + +}; + #include @@ -191,6 +278,11 @@ bool Memcached::build(int argc, const char *argv[]) arg_buffer << " -m 128 "; arg_buffer << " -M "; + if (sasl()) + { + arg_buffer << sasl(); + } + for (int x= 1 ; x < argc ; x++) { arg_buffer << " " << argv[x] << " "; @@ -208,9 +300,20 @@ libtest::Server *build_memcached(const std::string& hostname, const in_port_t tr return new Memcached(hostname, try_port, false); } -libtest::Server *build_memcached_socket(const std::string& hostname, const in_port_t try_port) +libtest::Server *build_memcached_socket(const std::string& socket_file, const in_port_t try_port) +{ + return new Memcached(socket_file, try_port, true); +} + + +libtest::Server *build_memcached_sasl(const std::string& hostname, const in_port_t try_port, const std::string& username, const std::string &password) +{ + return new MemcachedSaSL(hostname, try_port, false, username, password); +} + +libtest::Server *build_memcached_sasl_socket(const std::string& socket_file, const in_port_t try_port, const std::string& username, const std::string &password) { - return new Memcached(hostname, try_port, true); + return new MemcachedSaSL(socket_file, try_port, true, username, password); } } diff --git a/libtest/memcached.h b/libtest/memcached.h index 58e1d79a..a0899614 100644 --- a/libtest/memcached.h +++ b/libtest/memcached.h @@ -27,5 +27,9 @@ libtest::Server *build_memcached(const std::string& hostname, const in_port_t tr libtest::Server *build_memcached_socket(const std::string& socket_file, const in_port_t try_port); +libtest::Server *build_memcached_sasl(const std::string& hostname, const in_port_t try_port, const std::string& username, const std::string& password); + +libtest::Server *build_memcached_sasl_socket(const std::string& socket_file, const in_port_t try_port, const std::string& username, const std::string& password); + } diff --git a/libtest/server.cc b/libtest/server.cc index ee94c945..603b3f45 100644 --- a/libtest/server.cc +++ b/libtest/server.cc @@ -127,7 +127,7 @@ bool Server::cycle() // Try to ping, and kill the server #limit number of times pid_t current_pid; - while (--limit and (current_pid= get_pid()) != -1) + while (--limit and is_pid_valid(current_pid= get_pid())) { if (kill(current_pid)) { @@ -192,22 +192,22 @@ bool Server::start() return false; } - if (is_helgrind()) + if (is_helgrind() or is_valgrind()) { sleep(4); } if (pid_file_option() and not pid_file().empty()) { - Wait wait(pid_file()); + Wait wait(pid_file(), 8); if (not wait.successful()) { - Error << "Unable to open pidfile: " << pid_file(); + Error << "Unable to open pidfile for: " << _running; } } - int count= is_helgrind() ? 20 : 5; + int count= is_helgrind() or is_valgrind() ? 20 : 5; while (not ping() and --count) { nap(); @@ -215,7 +215,12 @@ bool Server::start() if (count == 0) { - Error << "Failed to ping() server once started:" << *this; + // If we happen to have a pid file, lets try to kill it + if (pid_file_option() and not pid_file().empty()) + { + kill_file(pid_file()); + } + Error << "Failed to ping() server started with:" << _running; _running.clear(); return false; } @@ -316,10 +321,9 @@ bool Server::set_log_file() void Server::rebuild_base_command() { _base_command.clear(); - if (is_libtool() and getenv("LIBTOOL_COMMAND")) + if (is_libtool()) { - _base_command+= getenv("LIBTOOL_COMMAND"); - _base_command+= " "; + _base_command+= libtool(); } if (is_debug() and getenv("GDB_COMMAND")) @@ -364,7 +368,7 @@ bool Server::args(std::string& options) // Update pid_file if (pid_file_option()) { - if (not set_pid_file()) + if (_pid_file.empty() and not set_pid_file()) { return false; } @@ -563,7 +567,7 @@ bool server_startup(server_startup_st& construct, const std::string& server_type } else if (server_type.compare("blobslap_worker") == 0) { - if (GEARMAND_BINARY) + if (GEARMAND_BINARY and GEARMAND_BLOBSLAP_WORKER) { if (HAVE_LIBGEARMAN) { @@ -579,6 +583,24 @@ bool server_startup(server_startup_st& construct, const std::string& server_type Error << "No gearmand binary is available"; } } + else if (server_type.compare("memcached-sasl") == 0) + { + if (MEMCACHED_SASL_BINARY) + { + if (HAVE_LIBMEMCACHED) + { + server= build_memcached_sasl("localhost", try_port, construct.username(), construct.password()); + } + else + { + Error << "Libmemcached was not found"; + } + } + else + { + Error << "No memcached binary that was compiled with sasl is available"; + } + } else if (server_type.compare("memcached") == 0) { if (MEMCACHED_BINARY) @@ -664,6 +686,24 @@ bool server_startup_st::start_socket_server(const std::string& server_type, cons { Error << "Socket files are not supported for gearmand yet"; } + else if (server_type.compare("memcached-sasl") == 0) + { + if (MEMCACHED_SASL_BINARY) + { + if (HAVE_LIBMEMCACHED) + { + server= build_memcached_sasl_socket("localhost", try_port, username(), password()); + } + else + { + Error << "Libmemcached was not found"; + } + } + else + { + Error << "No memcached binary is available"; + } + } else if (server_type.compare("memcached") == 0) { if (MEMCACHED_BINARY) diff --git a/libtest/server.h b/libtest/server.h index 01b2ce9a..b4a48446 100644 --- a/libtest/server.h +++ b/libtest/server.h @@ -36,6 +36,7 @@ struct Server { private: bool _is_socket; std::string _socket; + std::string _sasl; std::string _pid_file; std::string _log_file; std::string _base_command; // executable command which include libtool, valgrind, gdb, etc @@ -170,13 +171,13 @@ public: protected: void nap(); + bool set_pid_file(); private: bool is_helgrind() const; bool is_valgrind() const; bool is_debug() const; bool set_log_file(); - bool set_pid_file(); bool set_socket_file(); void rebuild_base_command(); void reset_pid(); @@ -189,6 +190,9 @@ class server_startup_st private: std::string server_list; bool _socket; + bool _sasl; + std::string _username; + std::string _password; public: @@ -197,6 +201,7 @@ public: server_startup_st() : _socket(false), + _sasl(false), udp(0) { } @@ -209,6 +214,17 @@ public: return servers.size(); } + const std::string& password() const + { + return _password; + } + + const std::string& username() const + { + return _username; + } + + bool is_debug() const; bool is_helgrind() const; bool is_valgrind() const; @@ -218,11 +234,23 @@ public: return _socket; } + bool sasl() + { + return _sasl; + } + void set_socket() { _socket= true; } + void set_sasl(const std::string& username_arg, const std::string& password_arg) + { + _sasl= true; + _username= username_arg; + _password= password_arg; + } + void shutdown(bool remove= false); void push_server(Server *); diff --git a/libtest/signal.cc b/libtest/signal.cc index 50234d8d..ed017b2b 100644 --- a/libtest/signal.cc +++ b/libtest/signal.cc @@ -85,11 +85,18 @@ void SignalThread::test() SignalThread::~SignalThread() { + if (not is_shutdown()) + { + set_shutdown(SHUTDOWN_GRACEFUL); + } + +#if 0 if (pthread_equal(thread, pthread_self()) != 0 and (pthread_kill(thread, 0) == ESRCH) == true) { void *retval; pthread_join(thread, &retval); } +#endif sem_destroy(&lock); } diff --git a/libtest/skiptest.cc b/libtest/skiptest.cc new file mode 100644 index 00000000..784f3a8f --- /dev/null +++ b/libtest/skiptest.cc @@ -0,0 +1,48 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * libtest + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +#include + +#include + +#include +#include + +using namespace libtest; + + +collection_st collection[] ={ + {0, 0, 0, 0} +}; + +static void *world_create(server_startup_st&, test_return_t& rc) +{ + rc= TEST_SKIPPED; + + return NULL; +} + +void get_world(Framework *world) +{ + world->collections= collection; + world->_create= world_create; +} diff --git a/libtest/test.cc b/libtest/test.cc index 7b13ab79..5c43e40b 100644 --- a/libtest/test.cc +++ b/libtest/test.cc @@ -149,21 +149,41 @@ int main(int argc, char *argv[]) { srandom((unsigned int)time(NULL)); - if (getenv("srcdir")) + if (getenv("LIBTEST_QUIET")) { - char buffer[1024]; - snprintf(buffer, sizeof(buffer), "%s/%s", getenv("srcdir"), "tests"); - chdir(buffer); + close(STDOUT_FILENO); + } + + char buffer[1024]; + if (getenv("LIBTEST_TMP")) + { + snprintf(buffer, sizeof(buffer), "%s", getenv("LIBTEST_TMP")); } else { - chdir("tests"); + snprintf(buffer, sizeof(buffer), "%s", LIBTEST_TEMP); + } + + if (chdir(buffer) == -1) + { + char getcwd_buffer[1024]; + char *dir= getcwd(getcwd_buffer, sizeof(getcwd_buffer)); + + Error << "Unable to chdir() from " << dir << " to " << buffer << " errno:" << strerror(errno); + return EXIT_FAILURE; + } + + if (libtest::libtool() == NULL) + { + Error << "Failed to locate libtool"; + return EXIT_FAILURE; } world= new Framework(); if (not world) { + Error << "Failed to create Framework()"; return EXIT_FAILURE; } @@ -193,7 +213,7 @@ int main(int argc, char *argv[]) case TEST_FATAL: case TEST_FAILURE: case TEST_MEMORY_ALLOCATION_FAILURE: - Error << argv[0] << "create() failed"; + Error << argv[0] << " failed in Framework::create()"; delete world; return EXIT_FAILURE; } diff --git a/libtest/unittest.cc b/libtest/unittest.cc index 6fcc5cff..d2c965ba 100644 --- a/libtest/unittest.cc +++ b/libtest/unittest.cc @@ -140,7 +140,9 @@ static test_return_t var_log_exists_test(void *) static test_return_t var_tmp_test(void *) { FILE *file= fopen("var/tmp/junk", "w+"); - test_true(file); + char buffer[1024]; + const char *dir= getcwd(buffer, sizeof(buffer)); + test_true_got(file, dir); fclose(file); return TEST_SUCCESS; } @@ -179,16 +181,6 @@ static test_return_t var_log_rm_test(void *) return TEST_SUCCESS; } - -#if 0 -static test_return_t pause_test(void *) -{ - (void)getchar(); - return TEST_SUCCESS; -} -#endif - - static test_return_t gearmand_cycle_test(void *object) { server_startup_st *servers= (server_startup_st*)object; diff --git a/libtest/version.h.in b/libtest/version.h.in index 16c4656b..a670a2e1 100644 --- a/libtest/version.h.in +++ b/libtest/version.h.in @@ -26,6 +26,9 @@ extern "C" { #endif +#define LIBTEST@_WITH_LIBMEMCACHED_SUPPORT@ +#define LIBTEST@_WITH_LIBGEARMAN_SUPPORT@ + #define LIBTEST_VERSION_STRING "@VERSION@" #define LIBTEST_VERSION_HEX @HEX_VERSION@ diff --git a/m4/memcached_sasl.m4 b/m4/memcached_sasl.m4 new file mode 100644 index 00000000..5983c112 --- /dev/null +++ b/m4/memcached_sasl.m4 @@ -0,0 +1,8 @@ +AX_WITH_PROG(MEMCACHED_SASL_BINARY,memcached_sasl) +AS_IF([test -f "$ac_cv_path_MEMCACHED_SASL_BINARY"], + [ + AC_DEFINE_UNQUOTED([MEMCACHED_SASL_BINARY], "$ac_cv_path_MEMCACHED_SASL_BINARY", [Name of the memcached_sasl binary used in make test]) + ], + [ + AC_DEFINE([MEMCACHED_SASL_BINARY], [0], [Name of the memcached_sasl binary used in make test]) + ]) diff --git a/m4/pandora_sasl.m4 b/m4/pandora_sasl.m4 deleted file mode 100644 index ca4a21dc..00000000 --- a/m4/pandora_sasl.m4 +++ /dev/null @@ -1,133 +0,0 @@ -dnl Copyright (C) 2009 Sun Microsystems, Inc. -dnl This file is free software; Sun Microsystems, Inc. -dnl gives unlimited permission to copy and/or distribute it, -dnl with or without modifications, as long as this notice is preserved. - -AC_DEFUN([_PANDORA_SEARCH_SASL],[ - AC_REQUIRE([AC_LIB_PREFIX]) - - dnl -------------------------------------------------------------------- - dnl Check for sasl - dnl -------------------------------------------------------------------- - AC_ARG_ENABLE([sasl], - [AS_HELP_STRING([--disable-sasl], - [Build with sasl support @<:@default=on@:>@])], - [ac_enable_sasl="$enableval"], - [ac_enable_sasl="yes"]) - - AS_IF([test "x$ac_enable_sasl" = "xyes"], - [ - AC_LIB_HAVE_LINKFLAGS(sasl,,[ - #include - #include - ],[ - sasl_server_init(NULL, NULL); - ]) - - AS_IF([test "x${ac_cv_libsasl}" != "xyes" ], - [ - AC_LIB_HAVE_LINKFLAGS(sasl2,,[ - #include - #include - ],[ - sasl_server_init(NULL, NULL); - ]) - HAVE_LIBSASL="$HAVE_LIBSASL2" - LIBSASL="$LIBSASL2" - LIBSASL_PREFIX="$LIBSASL2_PREFIX" - LTLIBSASL="$LT_LIBSASL2" - ]) - ]) - - AS_IF([test "x${ac_cv_libsasl}" = "xyes" -o "x${ac_cv_libsasl2}" = "xyes"], - [ac_cv_sasl=yes], - [ac_cv_sasl=no]) - - AM_CONDITIONAL(HAVE_LIBSASL, [test "x${ac_cv_libsasl}" = "xyes"]) - AM_CONDITIONAL(HAVE_LIBSASL2, [test "x${ac_cv_libsasl2}" = "xyes"]) - AM_CONDITIONAL(HAVE_SASL, [test "x${ac_cv_sasl}" = "xyes"]) -]) - -AC_DEFUN([PANDORA_HAVE_SASL],[ - AC_REQUIRE([_PANDORA_SEARCH_SASL]) -]) - -AC_DEFUN([PANDORA_REQUIRE_SASL],[ - AC_REQUIRE([_PANDORA_SEARCH_SASL]) - AS_IF([test "x${ac_cv_sasl}" = "xno"], - AC_MSG_ERROR([SASL (libsasl or libsasl2) is required for ${PACKAGE}])) -]) - -AC_DEFUN([_PANDORA_SEARCH_LIBSASL],[ - AC_REQUIRE([AC_LIB_PREFIX]) - - dnl -------------------------------------------------------------------- - dnl Check for libsasl - dnl -------------------------------------------------------------------- - - AC_ARG_ENABLE([libsasl], - [AS_HELP_STRING([--disable-libsasl], - [Build with libsasl support @<:@default=on@:>@])], - [ac_enable_libsasl="$enableval"], - [ac_enable_libsasl="yes"]) - - AS_IF([test "x$ac_enable_libsasl" = "xyes"],[ - AC_LIB_HAVE_LINKFLAGS(sasl,,[ - #include - #include - ],[ - sasl_server_init(NULL, NULL); - ]) - ],[ - ac_cv_libsasl="no" - ]) - - AM_CONDITIONAL(HAVE_LIBSASL, [test "x${ac_cv_libsasl}" = "xyes"]) -]) - -AC_DEFUN([PANDORA_HAVE_LIBSASL],[ - AC_REQUIRE([_PANDORA_SEARCH_LIBSASL]) -]) - -AC_DEFUN([PANDORA_REQUIRE_LIBSASL],[ - AC_REQUIRE([_PANDORA_SEARCH_LIBSASL]) - AS_IF([test "x${ac_cv_libsasl}" = "xno"], - AC_MSG_ERROR([libsasl is required for ${PACKAGE}])) -]) - -AC_DEFUN([_PANDORA_SEARCH_LIBSASL2],[ - AC_REQUIRE([AC_LIB_PREFIX]) - - dnl -------------------------------------------------------------------- - dnl Check for libsasl2 - dnl -------------------------------------------------------------------- - - AC_ARG_ENABLE([libsasl2], - [AS_HELP_STRING([--disable-libsasl2], - [Build with libsasl2 support @<:@default=on@:>@])], - [ac_enable_libsasl2="$enableval"], - [ac_enable_libsasl2="yes"]) - - AS_IF([test "x$ac_enable_libsasl2" = "xyes"],[ - AC_LIB_HAVE_LINKFLAGS(sasl2,,[ - #include - #include - ],[ - sasl2_server_init(NULL, NULL); - ]) - ],[ - ac_cv_libsasl2="no" - ]) - - AM_CONDITIONAL(HAVE_LIBSASL2, [test "x${ac_cv_libsasl2}" = "xyes"]) -]) - -AC_DEFUN([PANDORA_HAVE_LIBSASL2],[ - AC_REQUIRE([_PANDORA_SEARCH_LIBSASL2]) -]) - -AC_DEFUN([PANDORA_REQUIRE_LIBSASL2],[ - AC_REQUIRE([_PANDORA_SEARCH_LIBSASL2]) - AS_IF([test "x${ac_cv_libsasl2}" = "xno"], - AC_MSG_ERROR([libsasl2 is required for ${PACKAGE}])) -]) diff --git a/support/libmemcached.pc.in b/support/libmemcached.pc.in index 03031f92..d8012913 100644 --- a/support/libmemcached.pc.in +++ b/support/libmemcached.pc.in @@ -4,7 +4,8 @@ libdir=@libdir@ includedir=@includedir@ Name: libmemcached -Description: libmemcached C library. +URL: http://libmemcachd.org/ +Description: libmemcached C/C++ library. Version: @VERSION@ -Libs: @LTLIBSASL@ @LTLIBSASL2@ -L${libdir} -lmemcached -lmemcachedutil -pthread +Libs: -L${libdir} -lmemcached -lmemcachedutil Cflags: -I${includedir} diff --git a/tests/atomsmasher.cc b/tests/atomsmasher.cc index 573e7f53..6949bcd6 100644 --- a/tests/atomsmasher.cc +++ b/tests/atomsmasher.cc @@ -281,6 +281,7 @@ collection_st collection[] ={ }; +#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10 #define SERVERS_TO_CREATE 5 #include "libmemcached_world.h" diff --git a/tests/c_sasl_test.c b/tests/c_sasl_test.c new file mode 100644 index 00000000..18e3f422 --- /dev/null +++ b/tests/c_sasl_test.c @@ -0,0 +1,56 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * Libmemcached C sasl test app + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +/* + * @file @brief C dummy test, aka testing C linking, etc + */ + +#include + +#ifdef HAVE_SASL_SASL_H +#include +#endif + +#include + +int main(void) +{ + memcached_st *memc= memcached_create(NULL); + memcached_free(memc); + + return EXIT_SUCCESS; +} + diff --git a/tests/cycle.cc b/tests/cycle.cc index 957b52b0..5693b04d 100644 --- a/tests/cycle.cc +++ b/tests/cycle.cc @@ -127,6 +127,7 @@ collection_st collection[] ={ }; +#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10 #include "tests/libmemcached_world.h" void get_world(Framework *world) diff --git a/tests/include.am b/tests/include.am index dc11c910..b5f4fb0d 100644 --- a/tests/include.am +++ b/tests/include.am @@ -41,7 +41,8 @@ noinst_HEADERS+= \ # Cycle should always run first tests_cycle_CFLAGS= $(AM_CFLAGS) $(NO_CONVERSION) $(NO_STRICT_ALIASING) -tests_cycle_CXXFLAGS = $(AM_CXXFLAGS) ${PTHREAD_CFLAGS} +tests_cycle_CXXFLAGS= $(AM_CXXFLAGS) +tests_cycle_CXXFLAGS+= ${PTHREAD_CFLAGS} tests_cycle_SOURCES= tests/cycle.cc tests_cycle_DEPENDENCIES= $(TESTS_LDADDS) tests_cycle_LDADD= $(tests_cycle_DEPENDENCIES) @@ -84,7 +85,6 @@ tests_testapp_DEPENDENCIES= \ libhashkit/libhashkit.la \ libmemcached/libmemcachedutil.la tests_testapp_LDADD= \ - $(LIBSASL) \ ${PTHREAD_LIBS} \ libmemcached/libmemcached.la \ libmemcached/libmemcachedutil.la \ @@ -93,10 +93,38 @@ tests_testapp_LDADD= \ check_PROGRAMS+= tests/testapp noinst_PROGRAMS+= tests/testapp +tests_sasl_CXXFLAGS = $(AM_CXXFLAGS) ${PTHREAD_CFLAGS} +tests_sasl_CFLAGS= $(AM_CFLAGS) $(NO_CONVERSION) $(NO_STRICT_ALIASING) +tests_sasl_SOURCES= \ + tests/basic.cc \ + tests/debug.cc \ + tests/deprecated.cc \ + tests/error_conditions.cc \ + tests/ketama.cc \ + tests/sasl.cc \ + tests/namespace.cc \ + tests/parser.cc \ + tests/pool.cc \ + tests/print.cc \ + tests/replication.cc \ + tests/virtual_buckets.cc +tests_sasl_SOURCES+= clients/generator.cc clients/execute.cc +tests_sasl_DEPENDENCIES= \ + libmemcached/libmemcached.la \ + libtest/libtest.la +tests_sasl_LDADD= \ + ${PTHREAD_LIBS} \ + libmemcached/libmemcached.la \ + libmemcached/libmemcachedutil.la \ + libtest/libtest.la \ + libhashkit/libhashkit.la +check_PROGRAMS+= tests/sasl +noinst_PROGRAMS+= tests/sasl + tests_testplus_SOURCES= tests/plus.cpp tests_testplus_CXXFLAGS = $(AM_CXXFLAGS) $(NO_EFF_CXX) tests_testplus_DEPENDENCIES= $(TESTS_LDADDS) -tests_testplus_LDADD= $(tests_testplus_DEPENDENCIES) $(LIBSASL) +tests_testplus_LDADD= $(tests_testplus_DEPENDENCIES) check_PROGRAMS+= tests/testplus noinst_PROGRAMS+= tests/testplus @@ -107,13 +135,13 @@ tests_atomsmasher_SOURCES= \ clients/generator.cc \ clients/execute.cc tests_atomsmasher_DEPENDENCIES= $(TESTS_LDADDS) -tests_atomsmasher_LDADD= $(tests_atomsmasher_DEPENDENCIES) $(LIBSASL) +tests_atomsmasher_LDADD= $(tests_atomsmasher_DEPENDENCIES) noinst_PROGRAMS+= tests/atomsmasher tests_testudp_CFLAGS= $(AM_CFLAGS) $(NO_CONVERSION) $(NO_STRICT_ALIASING) tests_testudp_SOURCES= tests/mem_udp.cc tests_testudp_DEPENDENCIES= $(TESTS_LDADDS) -tests_testudp_LDADD= $(tests_testudp_DEPENDENCIES) $(LIBSASL) +tests_testudp_LDADD= $(tests_testudp_DEPENDENCIES) check_PROGRAMS+= tests/testudp noinst_PROGRAMS+= tests/testudp @@ -151,12 +179,19 @@ tests_c_test_DEPENDENCIES= libmemcached/libmemcached.la check_PROGRAMS+=tests/c_test noinst_PROGRAMS+=tests/c_test +# Test linking with C application/SASL include +tests_c_sasl_test_SOURCES= tests/c_sasl_test.c +tests_c_sasl_test_LDADD= libmemcached/libmemcached.la +tests_c_sasl_test_DEPENDENCIES= libmemcached/libmemcached.la +check_PROGRAMS+=tests/c_sasl_test +noinst_PROGRAMS+=tests/c_sasl_test + test: check -check-local: tests/var $(TEST_DOCS) +check-local: $(TEST_DOCS) @echo "Tests completed" -test-x: tests/var test-plus test-memcat test-memcp test-memrm test-memerror test-memdump test-memflush test-memstat +test-x: test-plus test-memcat test-memcp test-memrm test-memerror test-memdump test-memflush test-memstat @echo "Tests completed" test-memcat: clients/memcat clients/memcp @@ -266,6 +301,10 @@ valgrind-memerror: clients/memerror test-mem: tests/var tests/testapp @tests/testapp +test-sasl: tests/sasl + @tests/sasl + + test-udp: tests/var tests/testudp @tests/testudp @@ -293,6 +332,9 @@ pahole-mem: tests/testapp gdb-mem: tests/testapp @$(DEBUG_COMMAND) tests/testapp +gdb-sasl: tests/sasl + @$(DEBUG_COMMAND) tests/sasl + gdb-atom: tests/atomsmasher @$(DEBUG_COMMAND) tests/testudp diff --git a/tests/libmemcached_world.h b/tests/libmemcached_world.h index 968c235e..17561838 100644 --- a/tests/libmemcached_world.h +++ b/tests/libmemcached_world.h @@ -28,10 +28,15 @@ struct libmemcached_test_container_st }; #define SERVERS_TO_CREATE 5 -#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10 static void *world_create(server_startup_st& servers, test_return_t& error) { + if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) + { + error= TEST_SKIPPED; + return NULL; + } + in_port_t max_port; for (uint32_t x= 0; x < SERVERS_TO_CREATE; x++) { @@ -52,20 +57,43 @@ static void *world_create(server_startup_st& servers, test_return_t& error) max_port= port; const char *argv[1]= { "memcached" }; - if (not server_startup(servers, "memcached", port, 1, argv)) + if (servers.sasl()) { - error= TEST_FAILURE; - return NULL; + if (not server_startup(servers, "memcached-sasl", port, 1, argv)) + { + error= TEST_FAILURE; + return NULL; + } + } + else + { + if (not server_startup(servers, "memcached", port, 1, argv)) + { + error= TEST_FAILURE; + return NULL; + } } } if (servers.socket()) { - const char *argv[1]= { "memcached" }; - if (not servers.start_socket_server("memcached", max_port +1, 1, argv)) + if (servers.sasl()) + { + const char *argv[1]= { "memcached" }; + if (not servers.start_socket_server("memcached-sasl", max_port +1, 1, argv)) + { + error= TEST_FAILURE; + return NULL; + } + } + else { - error= TEST_FAILURE; - return NULL; + const char *argv[1]= { "memcached" }; + if (not servers.start_socket_server("memcached", max_port +1, 1, argv)) + { + error= TEST_FAILURE; + return NULL; + } } } @@ -95,6 +123,21 @@ static test_return_t world_container_startup(libmemcached_test_container_st *con container->parent= memcached(container->construct.option_string().c_str(), container->construct.option_string().size()); test_true(container->parent); + if (container->construct.sasl()) + { + if (memcached_failed(memcached_behavior_set(container->parent, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1))) + { + memcached_free(container->parent); + return TEST_FAILURE; + } + + if (memcached_failed(memcached_set_sasl_auth_data(container->parent, container->construct.username().c_str(), container->construct.password().c_str()))) + { + memcached_free(container->parent); + return TEST_FAILURE; + } + } + for (uint32_t host= 0; host < memcached_server_count(container->parent); ++host) { memcached_server_instance_st instance= @@ -174,8 +217,11 @@ static test_return_t world_on_error(test_return_t test_state, libmemcached_test_ static bool world_destroy(void *object) { libmemcached_test_container_st *container= (libmemcached_test_container_st *)object; -#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT - sasl_done(); +#if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT + if (LIBMEMCACHED_WITH_SASL_SUPPORT) + { + sasl_done(); + } #endif delete container; diff --git a/tests/mem_functions.cc b/tests/mem_functions.cc index 7418cc21..6845074b 100644 --- a/tests/mem_functions.cc +++ b/tests/mem_functions.cc @@ -1288,10 +1288,11 @@ static test_return_t stats_servername_test(memcached_st *memc) memcached_server_instance_st instance= memcached_server_instance_by_position(memc, 0); -#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT - if (memcached_get_sasl_callbacks(memc) != NULL) + if (LIBMEMCACHED_WITH_SASL_SUPPORT and memcached_get_sasl_callbacks(memc)) + { return TEST_SKIPPED; -#endif + } + test_compare(MEMCACHED_SUCCESS, memcached_stat_servername(&memc_stat, NULL, memcached_server_name(instance), memcached_server_port(instance))); @@ -3627,32 +3628,6 @@ static test_return_t pre_binary(memcached_st *memc) return rc == MEMCACHED_SUCCESS ? TEST_SUCCESS : TEST_SKIPPED; } -static test_return_t pre_sasl(memcached_st *memc) -{ - memcached_return_t rc= MEMCACHED_FAILURE; - -#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT - const char *server= getenv("LIBMEMCACHED_TEST_SASL_SERVER"); - const char *user= getenv("LIBMEMCACHED_TEST_SASL_USERNAME"); - const char *pass= getenv("LIBMEMCACHED_TEST_SASL_PASSWORD"); - - if (server and user and pass) - { - memcached_server_st *servers= memcached_servers_parse(server); - test_true(servers); - memcached_servers_reset(memc); - test_true(memcached_server_push(memc, servers) == MEMCACHED_SUCCESS); - memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1); - rc= memcached_set_sasl_auth_data(memc, user, pass); - test_compare(MEMCACHED_SUCCESS, rc); - } -#else - (void)memc; -#endif - - return rc == MEMCACHED_SUCCESS ? TEST_SUCCESS : TEST_SKIPPED; -} - static test_return_t pre_replication(memcached_st *memc) { test_skip(TEST_SUCCESS, pre_binary(memc)); @@ -5838,39 +5813,6 @@ static test_return_t regression_bug_(memcached_st *memc) return TEST_SUCCESS; } -/* - * Test that the sasl authentication works. We cannot use the default - * pool of servers, because that would require that all servers we want - * to test supports SASL authentication, and that they use the default - * creds. - */ -static test_return_t sasl_auth_test(memcached_st *memc) -{ -#ifdef LIBMEMCACHED_WITH_SASL_SUPPORT - test_compare(MEMCACHED_SUCCESS, memcached_set(memc, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0)); - test_compare(MEMCACHED_SUCCESS, memcached_delete(memc, "foo", 3, 0)); - test_compare(MEMCACHED_SUCCESS, memcached_destroy_sasl_auth_data(memc)); - test_compare(MEMCACHED_FAILURE, memcached_destroy_sasl_auth_data(memc)); - test_compare(MEMCACHED_FAILURE, memcached_destroy_sasl_auth_data(NULL)); - memcached_quit(memc); - - test_compare(MEMCACHED_SUCCESS, - memcached_set_sasl_auth_data(memc, - getenv("LIBMEMCACHED_TEST_SASL_USERNAME"), - getenv("LIBMEMCACHED_TEST_SASL_SERVER"))); - - test_compare(MEMCACHED_AUTH_FAILURE, - memcached_set(memc, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0)); - test_compare(MEMCACHED_SUCCESS, memcached_destroy_sasl_auth_data(memc)); - - memcached_quit(memc); - return TEST_SUCCESS; -#else - (void)memc; - return TEST_FAILURE; -#endif -} - /* Clean the server before beginning testing */ test_st tests[] ={ {"util_version", true, (test_callback_fn*)util_version_test }, @@ -6072,11 +6014,6 @@ test_st regression_tests[]= { {0, false, (test_callback_fn*)0} }; -test_st sasl_auth_tests[]= { - {"sasl_auth", true, (test_callback_fn*)sasl_auth_test }, - {0, 0, (test_callback_fn*)0} -}; - test_st ketama_compatibility[]= { {"libmemcached", true, (test_callback_fn*)ketama_compatibility_libmemcached }, {"spymemcached", true, (test_callback_fn*)ketama_compatibility_spymemcached }, @@ -6237,8 +6174,6 @@ collection_st collection[] ={ {"namespace(BINARY)", (test_callback_fn*)set_namespace_and_binary, 0, tests}, {"specific namespace", 0, 0, namespace_tests}, {"specific namespace(BINARY)", (test_callback_fn*)pre_binary, 0, namespace_tests}, - {"sasl_auth", (test_callback_fn*)pre_sasl, 0, sasl_auth_tests }, - {"sasl", (test_callback_fn*)pre_sasl, 0, tests }, {"version_1_2_3", (test_callback_fn*)check_for_1_2_3, 0, version_1_2_3}, {"result", 0, 0, result_tests}, {"async", (test_callback_fn*)pre_nonblock, 0, async_tests}, @@ -6273,6 +6208,8 @@ collection_st collection[] ={ {0, 0, 0, 0} }; +#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10 + #include "tests/libmemcached_world.h" void get_world(Framework *world) diff --git a/tests/mem_udp.cc b/tests/mem_udp.cc index 846d2bfd..a53db785 100644 --- a/tests/mem_udp.cc +++ b/tests/mem_udp.cc @@ -491,6 +491,7 @@ collection_st collection[] ={ #define SERVERS_TO_CREATE 5 +#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10 #include "libmemcached_world.h" void get_world(Framework *world) diff --git a/tests/plus.cpp b/tests/plus.cpp index aed34a49..3bc85bc1 100644 --- a/tests/plus.cpp +++ b/tests/plus.cpp @@ -258,6 +258,7 @@ collection_st collection[] ={ #define SERVERS_TO_CREATE 5 +#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +10 #include "libmemcached_world.h" void get_world(Framework *world) diff --git a/tests/sasl.cc b/tests/sasl.cc new file mode 100644 index 00000000..70b42987 --- /dev/null +++ b/tests/sasl.cc @@ -0,0 +1,122 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * Libmemcached library + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +using namespace libtest; + +/* + Test cases +*/ + +#define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT +20 +#include + +static test_return_t pre_sasl(memcached_st *) +{ + if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) + { + return TEST_SKIPPED; + } + + return TEST_SUCCESS; +} + +/* + * Test that the sasl authentication works. We cannot use the default + * pool of servers, because that would require that all servers we want + * to test supports SASL authentication, and that they use the default + * creds. + */ +static test_return_t sasl_auth_test(memcached_st *memc) +{ + if (LIBMEMCACHED_WITH_SASL_SUPPORT) + { + test_compare(MEMCACHED_SUCCESS, memcached_set(memc, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0)); + test_compare(MEMCACHED_SUCCESS, memcached_delete(memc, "foo", 3, 0)); + test_compare(MEMCACHED_SUCCESS, memcached_destroy_sasl_auth_data(memc)); + test_compare(MEMCACHED_SUCCESS, memcached_destroy_sasl_auth_data(memc)); + test_compare(MEMCACHED_INVALID_ARGUMENTS, memcached_destroy_sasl_auth_data(NULL)); + memcached_quit(memc); + + test_compare(MEMCACHED_AUTH_FAILURE, + memcached_set(memc, "foo", 3, "bar", 3, (time_t)0, (uint32_t)0)); + test_compare(MEMCACHED_SUCCESS, memcached_destroy_sasl_auth_data(memc)); + + memcached_quit(memc); + return TEST_SUCCESS; + } + + return TEST_SKIPPED; +} + + +test_st sasl_auth_tests[]= { + {"sasl_auth", true, (test_callback_fn*)sasl_auth_test }, + {0, 0, (test_callback_fn*)0} +}; + +collection_st collection[] ={ + {"sasl_auth", (test_callback_fn*)pre_sasl, 0, sasl_auth_tests }, +#if 0 + {"sasl", (test_callback_fn*)pre_sasl, 0, tests }, +#endif + {0, 0, 0, 0} +}; + +#include "tests/libmemcached_world.h" + +void get_world(Framework *world) +{ + world->collections= collection; + + world->_create= (test_callback_create_fn*)world_create; + world->_destroy= (test_callback_destroy_fn*)world_destroy; + + world->item._startup= (test_callback_fn*)world_test_startup; + world->item.set_pre((test_callback_fn*)world_pre_run); + world->item.set_flush((test_callback_fn*)world_flush); + world->item.set_post((test_callback_fn*)world_post_run); + world->_on_error= (test_callback_error_fn*)world_on_error; + + world->collection_startup= (test_callback_fn*)world_container_startup; + world->collection_shutdown= (test_callback_fn*)world_container_shutdown; + + world->set_runner(&defualt_libmemcached_runner); + + world->set_sasl("memcached", "memcached"); +} diff --git a/util/include.am b/util/include.am index 9eed1999..5c00525d 100644 --- a/util/include.am +++ b/util/include.am @@ -14,5 +14,6 @@ noinst_HEADERS+= \ util/daemon.hpp \ util/instance.hpp \ util/operation.hpp \ + util/signal.hpp \ util/string.hpp \ util/pidfile.hpp diff --git a/util/pidfile.cc b/util/pidfile.cc index f9a5afbb..c44b9081 100644 --- a/util/pidfile.cc +++ b/util/pidfile.cc @@ -42,12 +42,32 @@ #include #include #include -#include +#include #include #include +#include #include #include +extern "C" { + + char pid_file[1024 * 4]= { 0 }; + + static void remove_pidfile(void) + { + if (pid_file[0]) + { + if (unlink(pid_file) == -1) + { + std::cerr << "Could not remove pidfile: " << pid_file << "(" << strerror(errno) << ")" << std::endl; + } + + pid_file[0]= 0; + } + } + +} + namespace datadifferential { namespace util { @@ -60,35 +80,61 @@ Pidfile::Pidfile(const std::string &arg) : Pidfile::~Pidfile() { - if (not _filename.empty() and (unlink(_filename.c_str()) == -1)) + if (not _filename.empty()) { - _error_message+= "Could not remove the pid file: "; - _error_message+= _filename; + if (access(_filename.c_str(), F_OK) == -1) + { + std::stringstream error_stream; + error_stream << "Could not access the pid file: " << _filename << "(" << strerror(errno) << ")"; + _error_message= error_stream.str(); + } + else if (unlink(_filename.c_str()) == -1) + { + std::stringstream error_stream; + error_stream << "Could not remove the pid file: " << _filename << "(" << strerror(errno) << ")"; + _error_message= error_stream.str(); + } } + pid_file[0]= 0; } bool Pidfile::create() { if (_filename.empty()) + { return true; + } + + if (access(_filename.c_str(), F_OK) == 0) + { + if (unlink(_filename.c_str()) == -1) + { + std::stringstream error_stream; + error_stream << "Unable to remove exisiting file:" << _filename << "(" << strerror(errno) << ")"; + _error_message= error_stream.str(); + + return false; + } + } int file; if ((file = open(_filename.c_str(), O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU|S_IRGRP|S_IROTH)) < 0) { - _error_message+= "Could not open pid file for writing: "; - _error_message+= _filename; + std::stringstream error_stream; + error_stream << "Could not open pid file for writing: " << _filename << "(" << strerror(errno) << ")"; + _error_message= error_stream.str(); + return false; } char buffer[BUFSIZ]; - unsigned long temp= static_cast(getpid()); int length= snprintf(buffer, sizeof(buffer), "%lu\n", temp); - if (write(file, buffer, length) != length) { - _error_message+= "Could not write pid to file: "; - _error_message+= _filename; + std::stringstream error_stream; + error_stream << "Could not write pid to file: " << _filename << "(" << strerror(errno) << ")"; + _error_message= error_stream.str(); close(file); return false; @@ -100,6 +146,8 @@ bool Pidfile::create() _error_message+= _filename; return false; } + snprintf(pid_file, sizeof(pid_file), "%s", _filename.c_str()); + atexit(remove_pidfile); return true; } diff --git a/util/pidfile.hpp b/util/pidfile.hpp index 71d89e11..73ba49ce 100644 --- a/util/pidfile.hpp +++ b/util/pidfile.hpp @@ -37,7 +37,6 @@ #pragma once - #include namespace datadifferential { @@ -59,7 +58,7 @@ public: private: int _last_errno; - std::string _filename; + const std::string _filename; std::string _error_message; }; diff --git a/util/signal.cc b/util/signal.cc new file mode 100644 index 00000000..551803ab --- /dev/null +++ b/util/signal.cc @@ -0,0 +1,199 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * DD Util + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +#include + +#include +#include +#include +#include +#include +#include + +#include + +namespace datadifferential { +namespace util { + +#define MAGIC_MEMORY 123569 + +bool SignalThread::is_shutdown() +{ + bool ret; + pthread_mutex_lock(&shutdown_mutex); + ret= bool(__shutdown != SHUTDOWN_RUNNING); + pthread_mutex_unlock(&shutdown_mutex); + + return ret; +} + +void SignalThread::set_shutdown(shutdown_t arg) +{ + pthread_mutex_lock(&shutdown_mutex); + __shutdown= arg; + pthread_mutex_unlock(&shutdown_mutex); + + if (arg == SHUTDOWN_GRACEFUL) + { + if (pthread_kill(thread, SIGUSR2) == 0) + { + void *retval; + pthread_join(thread, &retval); + } + } +} + +shutdown_t SignalThread::get_shutdown() +{ + shutdown_t local; + pthread_mutex_lock(&shutdown_mutex); + local= __shutdown; + pthread_mutex_unlock(&shutdown_mutex); + + return local; +} + +void SignalThread::post() +{ + sem_post(&lock); +} + +void SignalThread::test() +{ + assert(magic_memory == MAGIC_MEMORY); + assert(sigismember(&set, SIGABRT)); + assert(sigismember(&set, SIGINT)); + assert(sigismember(&set, SIGQUIT)); + assert(sigismember(&set, SIGTERM)); + assert(sigismember(&set, SIGUSR2)); +} + +SignalThread::~SignalThread() +{ + if (not is_shutdown()) + { + set_shutdown(SHUTDOWN_GRACEFUL); + } + +#if 0 + if (pthread_equal(thread, pthread_self()) != 0 and (pthread_kill(thread, 0) == ESRCH) == true) + { + void *retval; + pthread_join(thread, &retval); + } +#endif + sem_destroy(&lock); +} + +extern "C" { + +static void *sig_thread(void *arg) +{ + SignalThread *context= (SignalThread*)arg; + + context->test(); + context->post(); + + while (context->get_shutdown() == SHUTDOWN_RUNNING) + { + int sig; + + if (context->wait(sig) == -1) + { + std::cerr << "sigwait() returned errno:" << strerror(errno) << std::endl; + continue; + } + + switch (sig) + { + case SIGUSR2: + break; + + case SIGABRT: + case SIGINT: + case SIGQUIT: + case SIGTERM: + if (context->is_shutdown() == false) + { + context->set_shutdown(SHUTDOWN_FORCED); + } + + if (context->exit_on_signal()) + { + exit(EXIT_SUCCESS); + } + + break; + + default: + std::cerr << "Signal handling thread got unexpected signal " << strsignal(sig) << std::endl; + break; + } + } + + return NULL; +} + +} + +SignalThread::SignalThread(bool exit_on_signal_arg) : + _exit_on_signal(exit_on_signal_arg), + magic_memory(MAGIC_MEMORY), + thread(pthread_self()) +{ + pthread_mutex_init(&shutdown_mutex, NULL); + sigemptyset(&set); + + sigaddset(&set, SIGABRT); + sigaddset(&set, SIGINT); + sigaddset(&set, SIGQUIT); + sigaddset(&set, SIGTERM); + sigaddset(&set, SIGUSR2); + + sem_init(&lock, 0, 0); +} + + +bool SignalThread::setup() +{ + set_shutdown(SHUTDOWN_RUNNING); + + int error; + if ((error= pthread_sigmask(SIG_BLOCK, &set, NULL)) != 0) + { + std::cerr << "pthread_sigmask() died during pthread_sigmask(" << strerror(error) << ")" << std::endl; + return false; + } + + if ((error= pthread_create(&thread, NULL, &sig_thread, this)) != 0) + { + std::cerr << "pthread_create() died during pthread_create(" << strerror(error) << ")" << std::endl; + return false; + } + + sem_wait(&lock); + + return true; +} + +} /* namespace util */ +} /* namespace datadifferential */ diff --git a/util/signal.hpp b/util/signal.hpp new file mode 100644 index 00000000..7573fe65 --- /dev/null +++ b/util/signal.hpp @@ -0,0 +1,73 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * libtest + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + + + +#pragma once + +#include +#include + +namespace datadifferential { +namespace util { + +enum shutdown_t { + SHUTDOWN_RUNNING, + SHUTDOWN_GRACEFUL, + SHUTDOWN_FORCED +}; + +class SignalThread { + bool _exit_on_signal; + sigset_t set; + sem_t lock; + uint64_t magic_memory; + volatile shutdown_t __shutdown; + pthread_mutex_t shutdown_mutex; + pthread_t thread; + +public: + + SignalThread(bool exit_on_signal_arg= false); + + void test(); + void post(); + bool setup(); + + bool exit_on_signal() + { + return _exit_on_signal; + } + + int wait(int& sig) + { + return sigwait(&set, &sig); + } + + ~SignalThread(); + + void set_shutdown(shutdown_t arg); + bool is_shutdown(); + shutdown_t get_shutdown(); +}; + +} /* namespace util */ +} /* namespace datadifferential */