8000 Fix #10, AUTH with session handler. · jrtkcoder/phpredis@d8de200 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d8de200

Browse files
committed
Fix phpredis#10, AUTH with session handler.
1 parent d19379c commit d8de200

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

README.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ session.save_path = "tcp://host1:6379?weight=1, tcp://host2:6379?weight=2&timeou
5151
* timeout (float): the connection timeout to a redis host, expressed in seconds. If the host is unreachable in that amount of time, the session storage will be unavailable for the client. The default timeout is very high (86400 seconds).
5252
* persistent (integer, should be 1 or 0): defines if a persistent connection should be used. **(experimental setting)**
5353
* prefix (string, defaults to "PHPREDIS_SESSION:"): used as a prefix to the Redis key in which the session is stored. The key is composed of the prefix followed by the session ID.
54+
* auth (string, empty by default): used to authenticate with the Redis server prior to sending commands.
5455

5556
Sessions have a lifetime expressed in seconds and stored in the INI variable "session.gc_maxlifetime". You can change it with [`ini_set()`](http://php.net/ini_set).
5657
The session handler requires a version of Redis with the `SETEX` command (at least 2.0).

redis_session.c

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ typedef struct redis_pool_member_ {
4949
char *prefix;
5050
size_t prefix_len;
5151

52+
char *auth;
53+
size_t auth_len;
54+
5255
struct redis_pool_member_ *next;
5356

5457
} redis_pool_member;
@@ -69,14 +72,18 @@ redis_pool_new(TSRMLS_D) {
6972

7073
PHPAPI void
7174
redis_pool_add(redis_pool *pool, RedisSock *redis_sock, int weight,
72-
char *prefix TSRMLS_DC) {
75+
char *prefix, char *auth TSRMLS_DC) {
7376

7477
redis_pool_member *rpm = ecalloc(1, sizeof(redis_pool_member));
7578
rpm->redis_sock = redis_sock;
7679
rpm->weight = weight;
80+
7781
rpm->prefix = prefix;
7882
rpm->prefix_len = (prefix?strlen(prefix):0);
7983

84+
rpm->auth = auth;
85+
rpm->auth_len = (auth?strlen(auth):0);
86+
8087
rpm->next = pool->head;
8188
pool->head = rpm;
8289

@@ -93,12 +100,32 @@ redis_pool_free(redis_pool *pool TSRMLS_DC) {
93100
redis_sock_disconnect(rpm->redis_sock TSRMLS_CC);
94101
efree(rpm->redis_sock);
95102
if(rpm->prefix) efree(rpm->prefix);
103+
if(rpm->auth) efree(rpm->auth);
96104
efree(rpm);
97105
rpm = next;
98106
}
99107
efree(pool);
100108
}
101109

110+
void
111+
redis_pool_member_auth(redis_pool_member *rpm TSRMLS_DC) {
112+
RedisSock *redis_sock = rpm->redis_sock;
113+
char *response, *cmd;
114+
int response_len, cmd_len;
115+
116+
if(!rpm->auth || !rpm->auth_len) { /* no password given. */
117+
return;
118+
}
119+
cmd_len = redis_cmd_format_static(&cmd, "AUTH", "s", rpm->auth, rpm->auth_len);
120+
121+
if(redis_sock_write(redis_sock, cmd, cmd_len TSRMLS_CC) >= 0) {
122+
if ((response = redis_sock_read(redis_sock, &response_len TSRMLS_CC))) {
123+
efree(response);
124+
}
125+
}
126+
efree(cmd);
127+
}
128+
102129
PHPAPI redis_pool_member *
103130
redis_pool_get_sock(redis_pool *pool, const char *key TSRMLS_DC) {
104131

@@ -110,7 +137,15 @@ redis_pool_get_sock(redis_pool *pool, const char *key TSRMLS_DC) {
110137

111138
for(i = 0; i < pool->totalWeight;) {
112139
if(pos >= i && pos < i + rpm->weight) {
113-
redis_sock_server_open(rpm->redis_sock, 0 TSRMLS_CC);
140+
int needs_auth = 0;
141+
if(rpm->auth && rpm->auth_len && rpm->redis_sock->status != REDIS_SOCK_STATUS_CONNECTED) {
142+
needs_auth = 1;
143+
}
144+
redis_sock_server_open(rpm->redis_sock, 0 TSRMLS_CC);
145+
if(needs_auth) {
146+
redis_pool_member_auth(rpm TSRMLS_CC);
147+
}
148+
114149
return rpm;
115150
}
116151
i += rpm->weight;
@@ -145,7 +180,7 @@ PS_OPEN_FUNC(redis)
145180
int weight = 1;
146181
double timeout = 86400.0;
147182
int persistent = 0;
148-
char *prefix = NULL;
183+
char *prefix = NULL, *auth = NULL;
149184

150185
/* translate unix: into file: */
151186
if (!strncmp(save_path+i, "unix:", sizeof("unix:")-1)) {
@@ -190,6 +225,9 @@ PS_OPEN_FUNC(redis)
190225
if (zend_hash_find(Z_ARRVAL_P(params), "prefix", sizeof("prefix"), (void **) &param) != FAILURE) {
191226
prefix = estrndup(Z_STRVAL_PP(param), Z_STRLEN_PP(param));
192227
}
228+
if (zend_hash_find(Z_ARRVAL_P(params), "auth", sizeof("auth"), (void **) &param) != FAILURE) {
229+
auth = estrndup(Z_STRVAL_PP(param), Z_STRLEN_PP(param));
230+
}
193231

194232
/* // not supported yet
195233
if (zend_hash_find(Z_ARRVAL_P(params), "retry_interval", sizeof("retry_interval"), (void **) &param) != FAILURE) {
@@ -214,7 +252,7 @@ PS_OPEN_FUNC(redis)
214252
} else {
215253
redis_sock = redis_sock_create(url->host, strlen(url->host), url->port, timeout, persistent);
216254
}
217-
redis_pool_add(pool, redis_sock, weight, prefix TSRMLS_CC);
255+
redis_pool_add(pool, redis_sock, weight, prefix, auth TSRMLS_CC);
218256

219257
php_url_free(url);
220258
}

0 commit comments

Comments
 (0)
0