From 195f172dd7da557a93926b90f492ca3deddc9860 Mon Sep 17 00:00:00 2001 From: Tobias Wilken Date: Tue, 8 Jul 2014 14:34:43 +0200 Subject: [PATCH 1/2] Include cloudControl introduced functions - Cherry-pick setSaslData and configureSasl with tests - Add deprecation warning - Add vagrant --- .gitignore | 1 + Vagrantfile | 12 +++ memcached-api.php | 10 ++- php_memcached.c | 89 ++++++++++++++++++++++- script.sh | 28 +++++++ tests/setSaslAuth/set_sasl_auth.ini | 3 + tests/setSaslAuth/set_sasl_auth_data.phpt | 37 ++++++++++ 7 files changed, 176 insertions(+), 4 deletions(-) create mode 100644 Vagrantfile create mode 100644 script.sh create mode 100644 tests/setSaslAuth/set_sasl_auth.ini create mode 100644 tests/setSaslAuth/set_sasl_auth_data.phpt diff --git a/.gitignore b/.gitignore index bc9d558c..4ab6f25f 100644 --- a/.gitignore +++ b/.gitignore @@ -69,3 +69,4 @@ tests/*/*/*.php tests/*/*/*.exp tests/*/*/*.sh tmp-php.ini +.vagrant/ diff --git a/Vagrantfile b/Vagrantfile new file mode 100644 index 00000000..2f639ed6 --- /dev/null +++ b/Vagrantfile @@ -0,0 +1,12 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +VAGRANTFILE_API_VERSION = "2" + +Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| + config.vm.box = "precise" + + config.vm.synced_folder ".", "/vagrant_data" + config.vm.provision "shell", path: "script.sh" + +end diff --git a/memcached-api.php b/memcached-api.php index d22d0cf9..d389b05f 100644 --- a/memcached-api.php +++ b/memcached-api.php @@ -202,7 +202,7 @@ public function getDelayed( array $keys, $with_cas = null, $value_cb = null ) {} public function getDelayedByKey( $server_key, array $keys, $with_cas = null, $value_cb = null ) {} public function fetch( ) {} - + public function fetchAll( ) {} public function set( $key, $value, $expiration = 0, $udf_flags = 0 ) {} @@ -250,7 +250,7 @@ public function increment( $key, $offset = 1, $initial_value = 0, $expiry = 0) { public function decrement( $key, $offset = 1, $initial_value = 0, $expiry = 0) {} public function getOption( $option ) {} - + public function setOption( $option, $value ) {} public function setOptions( array $options ) {} @@ -276,7 +276,7 @@ public function getLastDisconnectedServer( ) {} public function flush( $delay = 0 ) {} public function getStats( ) {} - + public function getVersion( ) {} public function getResultCode( ) {} @@ -289,6 +289,10 @@ public function isPristine( ) {} public function setSaslAuthData( $username, $password ) {} + public function setSaslData($username, $password) {} + + public function configureSasl($username, $password) {} + } class MemcachedException extends Exception { diff --git a/php_memcached.c b/php_memcached.c index b21ecd42..484e6b64 100644 --- a/php_memcached.c +++ b/php_memcached.c @@ -2749,6 +2749,81 @@ static PHP_METHOD(Memcached, setSaslAuthData) RETURN_TRUE; } /* }}} */ + +/* {{{ Memcached::setSaslData(string user, string pass) + Sets sasl credentials (the same as Memcached::setSaslAuthData, for backward compatibility only) */ +static PHP_METHOD(Memcached, setSaslData) +{ + zend_error(E_DEPRECATED, "setSaslData is deprecated, use setSaslAuthData instead"); + + MEMC_METHOD_INIT_VARS; + memcached_return status; + + char *user, *pass; + int user_len, pass_len; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &user, &user_len, &pass, &pass_len) == FAILURE) { + return; + } + + if (!MEMC_G(use_sasl)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "SASL support (memcached.use_sasl) isn't enabled in php.ini"); + RETURN_FALSE; + } + + MEMC_METHOD_FETCH_OBJECT; + + if (!memcached_behavior_get(m_obj->memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "SASL is only supported with binary protocol"); + RETURN_FALSE; + } + m_obj->has_sasl_data = 1; + status = memcached_set_sasl_auth_data(m_obj->memc, user, pass); + + if (php_memc_handle_error(i_obj, status TSRMLS_CC) < 0) { + RETURN_FALSE; + } + RETURN_TRUE; +} +/* }}} */ + +/* {{{ Memcached::configureSasl(string user, string pass) + Sets sasl credentials and changes the behavior to binary protocol */ +static PHP_METHOD(Memcached, configureSasl) +{ + zend_error(E_DEPRECATED, "configureSasl is deprecated, use setSaslAuthData instead and set binary protocol"); + + MEMC_METHOD_INIT_VARS; + memcached_return status; + + char *user, *pass; + int user_len, pass_len; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &user, &user_len, &pass, &pass_len) == FAILURE) { + return; + } + + if (!MEMC_G(use_sasl)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "SASL support (memcached.use_sasl) isn't enabled in php.ini"); + RETURN_FALSE; + } + + MEMC_METHOD_FETCH_OBJECT; + + if (memcached_behavior_set(m_obj->memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1) != MEMCACHED_SUCCESS) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set SASL binary protocol"); + RETURN_FALSE; + } + m_obj->has_sasl_data = 1; + status = memcached_set_sasl_auth_data(m_obj->memc, user, pass); + + if (php_memc_handle_error(i_obj, status TSRMLS_CC) < 0) { + RETURN_FALSE; + } + RETURN_TRUE; +} +/* }}} */ + #endif /* HAVE_MEMCACHED_SASL */ /* {{{ Memcached::getResultCode() @@ -4040,6 +4115,16 @@ ZEND_BEGIN_ARG_INFO(arginfo_setSaslAuthData, 0) ZEND_ARG_INFO(0, password) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO(arginfo_setSaslData, 0) + ZEND_ARG_INFO(0, username) + ZEND_ARG_INFO(0, password) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO(arginfo_configureSasl, 0) + ZEND_ARG_INFO(0, username) + ZEND_ARG_INFO(0, password) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO(arginfo_setOption, 0) ZEND_ARG_INFO(0, option) ZEND_ARG_INFO(0, value) @@ -4143,7 +4228,9 @@ static zend_function_entry memcached_class_methods[] = { MEMC_ME(setOptions, arginfo_setOptions) MEMC_ME(setBucket, arginfo_setBucket) #ifdef HAVE_MEMCACHED_SASL - MEMC_ME(setSaslAuthData, arginfo_setSaslAuthData) + MEMC_ME(setSaslAuthData, arginfo_setSaslAuthData) + MEMC_ME(setSaslData, arginfo_setSaslData) + MEMC_ME(configureSasl, arginfo_configureSasl) #endif MEMC_ME(isPersistent, arginfo_isPersistent) MEMC_ME(isPristine, arginfo_isPristine) diff --git a/script.sh b/script.sh new file mode 100644 index 00000000..b9acd8a4 --- /dev/null +++ b/script.sh @@ -0,0 +1,28 @@ +cd /tmp +export LC_ALL="en_US.UTF-8" +locale-gen en_US.UTF-8 + +# libmemcache +apt-get install -y libsasl2-dev +wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz +tar -zxvf libmemcached-1.0.18.tar.gz +cd libmemcached-1.0.18 +./configure +make +make install +cd .. + +# php-memcached +apt-get install -y git +apt-get install -y php5-dev +apt-get install -y pkg-config + +git clone /vagrant_data php-memcached +cd php-memcached + +phpize +./configure +make + +apt-get install -y php5-cli +#make test diff --git a/tests/setSaslAuth/set_sasl_auth.ini b/tests/setSaslAuth/set_sasl_auth.ini new file mode 100644 index 00000000..7d80fac0 --- /dev/null +++ b/tests/setSaslAuth/set_sasl_auth.ini @@ -0,0 +1,3 @@ +password = yourpassword +servers = yourhost +username = yourusername diff --git a/tests/setSaslAuth/set_sasl_auth_data.phpt b/tests/setSaslAuth/set_sasl_auth_data.phpt new file mode 100644 index 00000000..cdcc39f2 --- /dev/null +++ b/tests/setSaslAuth/set_sasl_auth_data.phpt @@ -0,0 +1,37 @@ +--TEST-- +set sasl authentification +--FILE-- +addServer($server, 11211); + +$m->set('visitorcount', 0); +$m->increment('visitorcount'); +echo sprintf(">%d<\n", $m->get('visitorcount')); + +// with authentification +var_dump($m->setOption(Memcached::OPT_BINARY_PROTOCOL, true)); +var_dump($m->setSaslAuthData($user, $password)); +var_dump($m->configureSasl($user, $password)); +var_dump($m->setSaslData($user, $password)); + +$m->set('private_visitorcount', 0); +$m->increment('private_visitorcount'); +echo sprintf(">%d<", $m->get('private_visitorcount')); +?> +--EXPECT-- +>0< +bool(true) +bool(true) +bool(true) +bool(true) +>1< From ce99c079c675bc421d2da3eac17601b3c8858ea4 Mon Sep 17 00:00:00 2001 From: Tobias Wilken Date: Fri, 11 Jul 2014 13:10:16 +0200 Subject: [PATCH 2/2] With SASL set use_sasl to 1 Due to the fact the the php inis are copied in the buildpack from the stack image to the deployment image, introducing memcached.use_sasl=1 gets more complicated. use_sasl is not set to 1 by default. --- php_memcached.c | 4 ++-- script.sh | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/php_memcached.c b/php_memcached.c index 484e6b64..bb149c61 100644 --- a/php_memcached.c +++ b/php_memcached.c @@ -304,7 +304,7 @@ PHP_INI_BEGIN() STD_PHP_INI_ENTRY("memcached.serializer", SERIALIZER_DEFAULT_NAME, PHP_INI_ALL, OnUpdateSerializer, serializer_name, zend_php_memcached_globals, php_memcached_globals) #if HAVE_MEMCACHED_SASL - STD_PHP_INI_ENTRY("memcached.use_sasl", "0", PHP_INI_SYSTEM, OnUpdateBool, use_sasl, zend_php_memcached_globals, php_memcached_globals) + STD_PHP_INI_ENTRY("memcached.use_sasl", "1", PHP_INI_SYSTEM, OnUpdateBool, use_sasl, zend_php_memcached_globals, php_memcached_globals) #endif STD_PHP_INI_ENTRY("memcached.store_retry_count", "2", PHP_INI_ALL, OnUpdateLong, store_retry_count, zend_php_memcached_globals, php_memcached_globals) PHP_INI_END() @@ -4303,7 +4303,7 @@ PHP_GINIT_FUNCTION(php_memcached) php_memcached_globals->compression_type_real = COMPRESSION_TYPE_FASTLZ; php_memcached_globals->compression_factor = 1.30; #if HAVE_MEMCACHED_SASL - php_memcached_globals->use_sasl = 0; + php_memcached_globals->use_sasl = 1; #endif php_memcached_globals->store_retry_count = 2; } diff --git a/script.sh b/script.sh index b9acd8a4..64feb116 100644 --- a/script.sh +++ b/script.sh @@ -1,6 +1,7 @@ cd /tmp export LC_ALL="en_US.UTF-8" locale-gen en_US.UTF-8 +aptitude update # libmemcache apt-get install -y libsasl2-dev