Skip to content

Commit

Permalink
Authenticate in redis_sock_server_open
Browse files Browse the repository at this point in the history
  • Loading branch information
yatsukhnenko authored and michael-grunder committed May 1, 2020
1 parent 650ac05 commit 80f2529
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 60 deletions.
13 changes: 1 addition & 12 deletions cluster_library.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,6 @@ cluster_read_sock_resp(RedisSock *redis_sock, REDIS_REPLY_TYPE type,
return r;
}

/* Helper to open connection and send AUTH if necessary */
static zend_always_inline int
cluster_sock_open(RedisSock *redis_sock)
{
zend_bool need_auth = (redis_sock->auth && redis_sock->status != REDIS_SOCK_STATUS_CONNECTED);
if (!redis_sock_server_open(redis_sock) && (!need_auth || !redis_sock_auth(redis_sock ))) {
return SUCCESS;
}
return FAILURE;
}

/*
* Helpers to send various 'control type commands to a specific node, e.g.
* MULTI, ASKING, READONLY, READWRITE, etc
Expand Down Expand Up @@ -1138,7 +1127,7 @@ PHP_REDIS_API int cluster_map_keyspace(redisCluster *c) {
// Iterate over seeds until we can get slots
ZEND_HASH_FOREACH_PTR(c->seeds, seed) {
// Attempt to connect to this seed node
if (seed == NULL || cluster_sock_open(seed) != 0) {
if (seed == NULL || redis_sock_server_open(seed) != SUCCESS) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion cluster_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

/* Protected sending of data down the wire to a RedisSock->stream */
#define CLUSTER_SEND_PAYLOAD(sock, buf, len) \
(sock && !cluster_sock_open(sock) && sock->stream && !redis_check_eof(sock, 1 ) && \
(sock && !redis_sock_server_open(sock) && sock->stream && !redis_check_eof(sock, 1 ) && \
php_stream_write(sock->stream, buf, len)==len)

/* Macro to read our reply type character */
Expand Down
3 changes: 2 additions & 1 deletion common.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
typedef enum {
REDIS_SOCK_STATUS_FAILED = -1,
REDIS_SOCK_STATUS_DISCONNECTED,
REDIS_SOCK_STATUS_CONNECTED
REDIS_SOCK_STATUS_CONNECTED,
REDIS_SOCK_STATUS_READY
} redis_sock_status;

#define _NL "\r\n"
Expand Down
22 changes: 17 additions & 5 deletions library.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ redis_check_eof(RedisSock *redis_sock, int no_throw)
errmsg = "AUTH failed while reconnecting";
break;
}
redis_sock->status = REDIS_SOCK_STATUS_READY;
/* If we're using a non-zero db, reselect it */
if (redis_sock->dbNumber && reselect_db(redis_sock) != 0) {
errmsg = "SELECT failed while reconnecting";
Expand Down Expand Up @@ -1890,7 +1891,7 @@ PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock)
zend_llist_remove_tail(&p->list);

if (redis_sock_check_liveness(redis_sock) == SUCCESS) {
redis_sock->status = REDIS_SOCK_STATUS_CONNECTED;
redis_sock->status = REDIS_SOCK_STATUS_READY;
return SUCCESS;
} else if (redis_sock->stream) {
php_stream_pclose(redis_sock->stream);
Expand Down Expand Up @@ -1974,12 +1975,23 @@ redis_sock_server_open(RedisSock *redis_sock)
{
if (redis_sock) {
switch (redis_sock->status) {
case REDIS_SOCK_STATUS_FAILED:
return FAILURE;
case REDIS_SOCK_STATUS_DISCONNECTED:
return redis_sock_connect(redis_sock);
default:
if (redis_sock_connect(redis_sock) != SUCCESS) {
break;
} else if (redis_sock->status == REDIS_SOCK_STATUS_READY) {
return SUCCESS;
}
// fall through
case REDIS_SOCK_STATUS_CONNECTED:
if (redis_sock->auth && redis_sock_auth(redis_sock) != SUCCESS) {
break;
}
redis_sock->status = REDIS_SOCK_STATUS_READY;
// fall through
case REDIS_SOCK_STATUS_READY:
return SUCCESS;
default:
return FAILURE;
}
}
return FAILURE;
Expand Down
2 changes: 1 addition & 1 deletion redis.c
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ PHP_REDIS_API RedisSock *redis_sock_get_connected(INTERNAL_FUNCTION_PARAMETERS)
if((zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
&object, redis_ce) == FAILURE) ||
(redis_sock = redis_sock_get(object, 1)) == NULL ||
redis_sock->status != REDIS_SOCK_STATUS_CONNECTED)
redis_sock->status < REDIS_SOCK_STATUS_CONNECTED)
{
return NULL;
}
Expand Down
14 changes: 7 additions & 7 deletions redis_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, i

/* multi/exec */
if(ra->z_multi_exec) {
ra_call_user_function(&redis_ce->function_table, ra->z_multi_exec, &z_fun, return_value, argc, z_callargs);
call_user_function(&redis_ce->function_table, ra->z_multi_exec, &z_fun, return_value, argc, z_callargs);
zval_dtor(return_value);
zval_dtor(&z_fun);
for (i = 0; i < argc; ++i) {
Expand All @@ -435,7 +435,7 @@ ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, i
/* add MULTI + SADD */
ra_index_multi(redis_inst, MULTI);
/* call using discarded temp value and extract exec results after. */
ra_call_user_function(&redis_ce->function_table, redis_inst, &z_fun, return_value, argc, z_callargs);
call_user_function(&redis_ce->function_table, redis_inst, &z_fun, return_value, argc, z_callargs);
zval_dtor(return_value);

/* add keys to index. */
Expand All @@ -444,7 +444,7 @@ ra_forward_call(INTERNAL_FUNCTION_PARAMETERS, RedisArray *ra, const char *cmd, i
/* call EXEC */
ra_index_exec(redis_inst, return_value, 0);
} else { /* call directly through. */
ra_call_user_function(&redis_ce->function_table, redis_inst, &z_fun, return_value, argc, z_callargs);
call_user_function(&redis_ce->function_table, redis_inst, &z_fun, return_value, argc, z_callargs);

if (!b_write_cmd) {
/* check if we have an error. */
Expand Down Expand Up @@ -662,7 +662,7 @@ multihost_distribute_call(RedisArray *ra, zval *return_value, zval *z_fun, int a
/* Iterate our RedisArray nodes */
for (i = 0; i < ra->count; ++i) {
/* Call each node in turn */
ra_call_user_function(&redis_array_ce->function_table, &ra->redis[i], z_fun, &z_tmp, argc, argv);
call_user_function(&redis_array_ce->function_table, &ra->redis[i], z_fun, &z_tmp, argc, argv);

/* Add the result for this host */
add_assoc_zval_ex(return_value, ZSTR_VAL(ra->hosts[i]), ZSTR_LEN(ra->hosts[i]), &z_tmp);
Expand Down Expand Up @@ -975,7 +975,7 @@ PHP_METHOD(RedisArray, mget)
/* prepare call */
ZVAL_STRINGL(&z_fun, "MGET", 4);
/* call MGET on the node */
ra_call_user_function(&redis_ce->function_table, &ra->redis[n], &z_fun, &z_ret, 1, &z_argarray);
call_user_function(&redis_ce->function_table, &ra->redis[n], &z_fun, &z_ret, 1, &z_argarray);
zval_dtor(&z_fun);

/* cleanup args array */
Expand Down Expand Up @@ -1119,7 +1119,7 @@ PHP_METHOD(RedisArray, mset)
ZVAL_STRINGL(&z_fun, "MSET", 4);

/* call */
ra_call_user_function(&redis_ce->function_table, &ra->redis[n], &z_fun, &z_ret, 1, &z_argarray);
call_user_function(&redis_ce->function_table, &ra->redis[n], &z_fun, &z_ret, 1, &z_argarray);
zval_dtor(&z_fun);
zval_dtor(&z_ret);

Expand Down Expand Up @@ -1251,7 +1251,7 @@ static void ra_generic_del(INTERNAL_FUNCTION_PARAMETERS, char *kw, int kw_len) {
}

/* call */
ra_call_user_function(&redis_ce->function_table, &ra->redis[n], &z_fun, &z_ret, 1, &z_argarray);
call_user_function(&redis_ce->function_table, &ra->redis[n], &z_fun, &z_ret, 1, &z_argarray);

if(ra->index) {
zval_dtor(&z_ret);
Expand Down
2 changes: 0 additions & 2 deletions redis_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,4 @@ typedef struct RedisArray_ {
zend_object *create_redis_array_object(zend_class_entry *ce);
void free_redis_array_object(zend_object *object);

PHP_REDIS_API int ra_call_user_function(HashTable *function_table, zval *object, zval *function_name, zval *retval_ptr, uint32_t param_count, zval params[]);

#endif
48 changes: 24 additions & 24 deletions redis_array_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ ra_load_hosts(RedisArray *ra, HashTable *hosts, zend_string *auth, long retry_in
if (!b_lazy_connect)
{
/* connect */
if (redis_sock_server_open(redis->sock) < 0 || (auth && redis_sock_auth(redis->sock ) < 0)) {
if (redis_sock_server_open(redis->sock) < 0) {
zval_dtor(&z_cons);
ra->count = ++i;
return NULL;
Expand Down Expand Up @@ -484,7 +484,7 @@ ra_call_extractor(RedisArray *ra, const char *key, int key_len)
ZVAL_NULL(&z_ret);
/* call extraction function */
ZVAL_STRINGL(&z_argv, key, key_len);
ra_call_user_function(EG(function_table), NULL, &ra->z_fun, &z_ret, 1, &z_argv);
call_user_function(EG(function_table), NULL, &ra->z_fun, &z_ret, 1, &z_argv);

if (Z_TYPE(z_ret) == IS_STRING) {
out = zval_get_string(&z_ret);
Expand Down Expand Up @@ -525,7 +525,7 @@ ra_call_distributor(RedisArray *ra, const char *key, int key_len)
ZVAL_NULL(&z_ret);
/* call extraction function */
ZVAL_STRINGL(&z_argv, key, key_len);
ra_call_user_function(EG(function_table), NULL, &ra->z_dist, &z_ret, 1, &z_argv);
call_user_function(EG(function_table), NULL, &ra->z_dist, &z_ret, 1, &z_argv);

ret = (Z_TYPE(z_ret) == IS_LONG) ? Z_LVAL(z_ret) : -1;

Expand Down Expand Up @@ -623,7 +623,7 @@ ra_index_multi(zval *z_redis, long multi_value) {
/* run MULTI */
ZVAL_STRINGL(&z_fun_multi, "MULTI", 5);
ZVAL_LONG(&z_args[0], multi_value);
ra_call_user_function(&redis_ce->function_table, z_redis, &z_fun_multi, &z_ret, 1, z_args);
call_user_function(&redis_ce->function_table, z_redis, &z_fun_multi, &z_ret, 1, z_args);
zval_dtor(&z_fun_multi);
zval_dtor(&z_ret);
}
Expand Down Expand Up @@ -653,7 +653,7 @@ ra_index_change_keys(const char *cmd, zval *z_keys, zval *z_redis) {
}

/* run cmd */
ra_call_user_function(&redis_ce->function_table, z_redis, &z_fun, &z_ret, argc, z_args);
call_user_function(&redis_ce->function_table, z_redis, &z_fun, &z_ret, argc, z_args);

zval_dtor(&z_args[0]);
zval_dtor(&z_fun);
Expand Down Expand Up @@ -709,7 +709,7 @@ ra_index_key(const char *key, int key_len, zval *z_redis) {
ZVAL_STRINGL(&z_args[1], key, key_len);

/* run SADD */
ra_call_user_function(&redis_ce->function_table, z_redis, &z_fun_sadd, &z_ret, 2, z_args);
call_user_function(&redis_ce->function_table, z_redis, &z_fun_sadd, &z_ret, 2, z_args);
zval_dtor(&z_fun_sadd);
zval_dtor(&z_args[1]);
zval_dtor(&z_args[0]);
Expand All @@ -723,7 +723,7 @@ ra_index_exec(zval *z_redis, zval *return_value, int keep_all) {

/* run EXEC */
ZVAL_STRINGL(&z_fun_exec, "EXEC", 4);
ra_call_user_function(&redis_ce->function_table, z_redis, &z_fun_exec, &z_ret, 0, NULL);
call_user_function(&redis_ce->function_table, z_redis, &z_fun_exec, &z_ret, 0, NULL);
zval_dtor(&z_fun_exec);

/* extract first element of exec array and put into return_value. */
Expand All @@ -750,7 +750,7 @@ ra_index_discard(zval *z_redis, zval *return_value) {

/* run DISCARD */
ZVAL_STRINGL(&z_fun_discard, "DISCARD", 7);
ra_call_user_function(&redis_ce->function_table, z_redis, &z_fun_discard, &z_ret, 0, NULL);
call_user_function(&redis_ce->function_table, z_redis, &z_fun_discard, &z_ret, 0, NULL);

zval_dtor(&z_fun_discard);
zval_dtor(&z_ret);
Expand All @@ -763,7 +763,7 @@ ra_index_unwatch(zval *z_redis, zval *return_value) {

/* run UNWATCH */
ZVAL_STRINGL(&z_fun_unwatch, "UNWATCH", 7);
ra_call_user_function(&redis_ce->function_table, z_redis, &z_fun_unwatch, &z_ret, 0, NULL);
call_user_function(&redis_ce->function_table, z_redis, &z_fun_unwatch, &z_ret, 0, NULL);

zval_dtor(&z_fun_unwatch);
zval_dtor(&z_ret);
Expand Down Expand Up @@ -803,14 +803,14 @@ ra_get_key_type(zval *z_redis, const char *key, int key_len, zval *z_from, long
/* run TYPE */
ZVAL_NULL(&z_ret);
ZVAL_STRINGL(&z_fun, "TYPE", 4);
ra_call_user_function(&redis_ce->function_table, z_redis, &z_fun, &z_ret, 1, &z_arg);
call_user_function(&redis_ce->function_table, z_redis, &z_fun, &z_ret, 1, &z_arg);
zval_dtor(&z_fun);
zval_dtor(&z_ret);

/* run TYPE */
ZVAL_NULL(&z_ret);
ZVAL_STRINGL(&z_fun, "TTL", 3);
ra_call_user_function(&redis_ce->function_table, z_redis, &z_fun, &z_ret, 1, &z_arg);
call_user_function(&redis_ce->function_table, z_redis, &z_fun, &z_ret, 1, &z_arg);
zval_dtor(&z_fun);
zval_dtor(&z_ret);

Expand Down Expand Up @@ -842,7 +842,7 @@ ra_remove_from_index(zval *z_redis, const char *key, int key_len) {
ZVAL_STRINGL(&z_args[0], PHPREDIS_INDEX_NAME, sizeof(PHPREDIS_INDEX_NAME) - 1);
ZVAL_STRINGL(&z_args[1], key, key_len);

ra_call_user_function(&redis_ce->function_table, z_redis, &z_fun_srem, &z_ret, 2, z_args);
call_user_function(&redis_ce->function_table, z_redis, &z_fun_srem, &z_ret, 2, z_args);

/* cleanup */
zval_dtor(&z_fun_srem);
Expand All @@ -864,7 +864,7 @@ ra_del_key(const char *key, int key_len, zval *z_from) {
/* run DEL on source */
ZVAL_STRINGL(&z_fun_del, "DEL", 3);
ZVAL_STRINGL(&z_args[0], key, key_len);
ra_call_user_function(&redis_ce->function_table, z_from, &z_fun_del, &z_ret, 1, z_args);
call_user_function(&redis_ce->function_table, z_from, &z_fun_del, &z_ret, 1, z_args);
zval_dtor(&z_fun_del);
zval_dtor(&z_args[0]);
zval_dtor(&z_ret);
Expand All @@ -889,7 +889,7 @@ ra_expire_key(const char *key, int key_len, zval *z_to, long ttl) {
ZVAL_STRINGL(&z_fun_expire, "EXPIRE", 6);
ZVAL_STRINGL(&z_args[0], key, key_len);
ZVAL_LONG(&z_args[1], ttl);
ra_call_user_function(&redis_ce->function_table, z_to, &z_fun_expire, &z_ret, 2, z_args);
call_user_function(&redis_ce->function_table, z_to, &z_fun_expire, &z_ret, 2, z_args);
zval_dtor(&z_fun_expire);
zval_dtor(&z_args[0]);
zval_dtor(&z_ret);
Expand All @@ -913,7 +913,7 @@ ra_move_zset(const char *key, int key_len, zval *z_from, zval *z_to, long ttl) {
ZVAL_STRINGL(&z_args[1], "0", 1);
ZVAL_STRINGL(&z_args[2], "-1", 2);
ZVAL_BOOL(&z_args[3], 1);
ra_call_user_function(&redis_ce->function_table, z_from, &z_fun_zrange, &z_ret, 4, z_args);
call_user_function(&redis_ce->function_table, z_from, &z_fun_zrange, &z_ret, 4, z_args);
zval_dtor(&z_fun_zrange);
zval_dtor(&z_args[2]);
zval_dtor(&z_args[1]);
Expand Down Expand Up @@ -951,7 +951,7 @@ ra_move_zset(const char *key, int key_len, zval *z_from, zval *z_to, long ttl) {

/* run ZADD on target */
ZVAL_STRINGL(&z_fun_zadd, "ZADD", 4);
ra_call_user_function(&redis_ce->function_table, z_to, &z_fun_zadd, &z_ret_dest, 1 + 2 * count, z_zadd_args);
call_user_function(&redis_ce->function_table, z_to, &z_fun_zadd, &z_ret_dest, 1 + 2 * count, z_zadd_args);

/* Expire if needed */
ra_expire_key(key, key_len, z_to, ttl);
Expand All @@ -978,7 +978,7 @@ ra_move_string(const char *key, int key_len, zval *z_from, zval *z_to, long ttl)
/* run GET on source */
ZVAL_STRINGL(&z_fun_get, "GET", 3);
ZVAL_STRINGL(&z_args[0], key, key_len);
ra_call_user_function(&redis_ce->function_table, z_from, &z_fun_get, &z_ret, 1, z_args);
call_user_function(&redis_ce->function_table, z_from, &z_fun_get, &z_ret, 1, z_args);
zval_dtor(&z_fun_get);

if(Z_TYPE(z_ret) != IS_STRING) { /* key not found or replaced */
Expand All @@ -994,14 +994,14 @@ ra_move_string(const char *key, int key_len, zval *z_from, zval *z_to, long ttl)
ZVAL_LONG(&z_args[1], ttl);
ZVAL_STRINGL(&z_args[2], Z_STRVAL(z_ret), Z_STRLEN(z_ret)); /* copy z_ret to arg 1 */
zval_dtor(&z_ret); /* free memory from our previous call */
ra_call_user_function(&redis_ce->function_table, z_to, &z_fun_set, &z_ret, 3, z_args);
call_user_function(&redis_ce->function_table, z_to, &z_fun_set, &z_ret, 3, z_args);
/* cleanup */
zval_dtor(&z_args[2]);
} else {
ZVAL_STRINGL(&z_fun_set, "SET", 3);
ZVAL_STRINGL(&z_args[1], Z_STRVAL(z_ret), Z_STRLEN(z_ret)); /* copy z_ret to arg 1 */
zval_dtor(&z_ret); /* free memory from our previous return value */
ra_call_user_function(&redis_ce->function_table, z_to, &z_fun_set, &z_ret, 2, z_args);
call_user_function(&redis_ce->function_table, z_to, &z_fun_set, &z_ret, 2, z_args);
/* cleanup */
zval_dtor(&z_args[1]);
}
Expand All @@ -1019,7 +1019,7 @@ ra_move_hash(const char *key, int key_len, zval *z_from, zval *z_to, long ttl) {
/* run HGETALL on source */
ZVAL_STRINGL(&z_args[0], key, key_len);
ZVAL_STRINGL(&z_fun_hgetall, "HGETALL", 7);
ra_call_user_function(&redis_ce->function_table, z_from, &z_fun_hgetall, &z_args[1], 1, z_args);
call_user_function(&redis_ce->function_table, z_from, &z_fun_hgetall, &z_args[1], 1, z_args);
zval_dtor(&z_fun_hgetall);

if (Z_TYPE(z_args[1]) != IS_ARRAY) { /* key not found or replaced */
Expand All @@ -1031,7 +1031,7 @@ ra_move_hash(const char *key, int key_len, zval *z_from, zval *z_to, long ttl) {

/* run HMSET on target */
ZVAL_STRINGL(&z_fun_hmset, "HMSET", 5);
ra_call_user_function(&redis_ce->function_table, z_to, &z_fun_hmset, &z_ret_dest, 2, z_args);
call_user_function(&redis_ce->function_table, z_to, &z_fun_hmset, &z_ret_dest, 2, z_args);
zval_dtor(&z_fun_hmset);
zval_dtor(&z_ret_dest);

Expand Down Expand Up @@ -1066,7 +1066,7 @@ ra_move_collection(const char *key, int key_len, zval *z_from, zval *z_to,
ZVAL_STRING(&z_retrieve_args[i], cmd_list[i]);
}

ra_call_user_function(&redis_ce->function_table, z_from, &z_fun_retrieve, &z_ret, list_count, z_retrieve_args);
call_user_function(&redis_ce->function_table, z_from, &z_fun_retrieve, &z_ret, list_count, z_retrieve_args);

/* cleanup */
zval_dtor(&z_fun_retrieve);
Expand Down Expand Up @@ -1098,7 +1098,7 @@ ra_move_collection(const char *key, int key_len, zval *z_from, zval *z_to,
/* Clean up our input return value */
zval_dtor(&z_ret);

ra_call_user_function(&redis_ce->function_table, z_to, &z_fun_sadd, &z_ret, count, z_sadd_args);
call_user_function(&redis_ce->function_table, z_to, &z_fun_sadd, &z_ret, count, z_sadd_args);

/* cleanup */
zval_dtor(&z_fun_sadd);
Expand Down Expand Up @@ -1223,7 +1223,7 @@ ra_rehash_server(RedisArray *ra, zval *z_redis, zend_string *hostname, zend_bool
ZVAL_STRING(&z_argv, "*");
}
ZVAL_NULL(&z_ret);
ra_call_user_function(&redis_ce->function_table, z_redis, &z_fun, &z_ret, 1, &z_argv);
call_user_function(&redis_ce->function_table, z_redis, &z_fun, &z_ret, 1, &z_argv);
zval_dtor(&z_argv);
zval_dtor(&z_fun);

Expand Down
7 changes: 0 additions & 7 deletions redis_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,7 @@ redis_pool_get_sock(redis_pool *pool, const char *key) {

for(i = 0; i < pool->totalWeight;) {
if (pos >= i && pos < i + rpm->weight) {
int needs_auth = 0;
if (rpm->redis_sock->auth && rpm->redis_sock->status != REDIS_SOCK_STATUS_CONNECTED) {
needs_auth = 1;
}
if (redis_sock_server_open(rpm->redis_sock) == 0) {
if (needs_auth) {
redis_sock_auth(rpm->redis_sock);
}
if (rpm->database >= 0) { /* default is -1 which leaves the choice to redis. */
redis_pool_member_select(rpm);
}
Expand Down

0 comments on commit 80f2529

Please sign in to comment.