2 * Copyright (C) 2005-2008 Atsushi Konno All rights reserved.
3 * Copyright (C) 2005 QSDN,Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #include "chxj_cookie.h"
21 #include "chxj_url_encode.h"
22 #include "chxj_apply_convrule.h"
24 #include "ap_release.h"
30 #include "apr_base64.h"
33 static char *s_get_hostname_from_url(request_rec *r, char *value);
34 static char *s_cut_until_end_hostname(request_rec *, char *value);
40 chxj_save_cookie(request_rec *r)
43 apr_array_header_t *headers;
44 apr_table_entry_t *hentryp;
51 unsigned char *md5_value;
54 mod_chxj_config *dconf;
55 chxjconvrule_entry *entryp;
57 apr_table_t *new_cookie_table;
66 DBG(r, "start chxj_save_cookie()");
68 cookie = (cookie_t*)apr_palloc(r->pool, sizeof(cookie_t));
69 cookie->cookie_id = NULL;
74 dconf = ap_get_module_config(r->per_dir_config, &chxj_module);
75 entryp = chxj_apply_convrule(r, dconf->convrules);
77 DBG(r, "end chxj_save_cookie() no pattern");
80 if (! (entryp->action & CONVRULE_COOKIE_ON_BIT)) {
81 DBG(r, "end chxj_save_cookie() CookieOff");
87 headers = (apr_array_header_t*)apr_table_elts(r->headers_out);
88 hentryp = (apr_table_entry_t*)headers->elts;
91 new_cookie_table = apr_table_make(r->pool, 0);
93 for (ii=0; ii<headers->nelts; ii++) {
94 if (strcasecmp(hentryp[ii].key, "Set-Cookie") == 0) {
95 DBG(r, "=====================================");
96 DBG(r, "cookie=[%s:%s]", hentryp[ii].key, hentryp[ii].val);
103 buff = apr_pstrdup(r->pool, hentryp[ii].val);
104 val = strchr(buff, '=');
108 apr_table_add(new_cookie_table, key, val);
109 if (strcasecmp(REFERER_COOKIE_KEY, key) == 0) has_refer++;
114 DBG(r, "=====================================");
117 apr_table_unset(r->headers_out, "Set-Cookie");
120 apr_uri_parse(r->pool,r->uri, &parsed_uri);
121 refer_string = apr_psprintf(r->pool,
123 #if AP_SERVER_MAJORVERSION_NUMBER == 2 && AP_SERVER_MINORVERSION_NUMBER == 2
124 ap_run_http_scheme(r),
126 ap_run_http_method(r),
129 apr_uri_unparse(r->pool,
131 APR_URI_UNP_OMITSITEPART));
132 if (r->args && strlen(r->args)) {
133 refer_string = apr_pstrcat(r->pool, refer_string, "?", r->args, NULL);
135 apr_table_setn(new_cookie_table, REFERER_COOKIE_KEY, refer_string);
136 DBG(r, "ADD REFER[%s]", refer_string);
142 * check input parameters
144 old_cookie_id = (char*)apr_table_get(r->headers_in, "CHXJ_COOKIE_ID");
146 old_cookie = chxj_load_cookie(r, old_cookie_id);
147 if (old_cookie && old_cookie->cookie_headers) {
148 hentryp = (apr_table_entry_t*)old_cookie->cookie_headers->elts;
149 for (ii=0; ii<old_cookie->cookie_headers->nelts; ii++) {
150 if (hentryp && apr_table_get(new_cookie_table, hentryp[ii].key) == NULL) {
151 apr_table_add(new_cookie_table, hentryp[ii].key, hentryp[ii].val);
155 chxj_delete_cookie(r, old_cookie_id);
156 chxj_delete_cookie_expire(r, old_cookie_id);
162 DBG(r, "end chxj_save_cookie()");
166 file = chxj_cookie_db_lock(r);
168 ERR(r, "mod_chxj: Can't lock cookie db");
169 DBG(r, "end chxj_save_cookie()");
175 retval = apr_dbm_open_ex(&f,
177 chxj_cookie_db_name_create(r, dconf->cookie_db_dir),
181 if (retval != APR_SUCCESS) {
182 DBG(r, "end chxj_save_cookie()");
183 ERR(r, "could not open dbm (type %s) auth file: %s",
185 chxj_cookie_db_name_create(r,dconf->cookie_db_dir));
186 chxj_cookie_db_unlock(r, file);
191 uuid_string = apr_palloc(r->pool, APR_UUID_FORMATTED_LENGTH + 1);
192 memset(uuid_string, 0, APR_UUID_FORMATTED_LENGTH + 1);
193 apr_uuid_format(uuid_string, &uuid);;
195 md5_value = (unsigned char*)apr_palloc(r->pool, APR_MD5_DIGESTSIZE + 1);
196 memset(md5_value, 0, APR_MD5_DIGESTSIZE + 1);
198 retval = apr_md5(md5_value,
199 (const char*)uuid_string,
200 (apr_size_t)APR_UUID_FORMATTED_LENGTH);
201 if (retval != APR_SUCCESS) {
202 ERR(r, "md5 failed.");
206 cookie->cookie_id = apr_palloc(r->pool, apr_base64_encode_len(APR_MD5_DIGESTSIZE)+1);
207 memset(cookie->cookie_id, 0, APR_MD5_DIGESTSIZE+1);
208 apr_base64_encode(cookie->cookie_id, (char*)md5_value, APR_MD5_DIGESTSIZE);
210 DBG(r, "cookie->cookie_id=[%s]", cookie->cookie_id);
212 cookie->cookie_id = chxj_url_encode(r,cookie->cookie_id);
214 DBG(r, "cookie->cookie_id=[%s]", cookie->cookie_id);
220 dbmkey.dptr = cookie->cookie_id;
221 dbmkey.dsize = strlen(cookie->cookie_id);
226 cookie->cookie_headers = (apr_array_header_t*)apr_table_elts(new_cookie_table);
227 store_string = apr_palloc(r->pool, 1);
229 hentryp = (apr_table_entry_t*)cookie->cookie_headers->elts;
231 for (ii=0; ii<cookie->cookie_headers->nelts; ii++) {
232 if (ii) store_string = apr_pstrcat(r->pool,
237 store_string = apr_pstrcat(r->pool,
244 dbmval.dptr = store_string;
245 dbmval.dsize = strlen(store_string);
250 retval = apr_dbm_store(f, dbmkey, dbmval);
251 if (retval != APR_SUCCESS) {
252 ERR(r, "Cannot store Cookie data to DBM file `%s'",
253 chxj_cookie_db_name_create(r, dconf->cookie_db_dir));
257 chxj_save_cookie_expire(r, cookie);
262 chxj_cookie_db_unlock(r, file);
264 DBG(r, "end chxj_save_cookie()");
272 chxj_update_cookie(request_rec *r, cookie_t *old_cookie)
275 apr_array_header_t *headers;
276 apr_table_entry_t *hentryp;
283 unsigned char *md5_value;
285 mod_chxj_config *dconf;
286 chxjconvrule_entry *entryp;
291 DBG(r, "start chxj_update_cookie()");
292 if (!old_cookie || ! old_cookie->cookie_headers || ! old_cookie->cookie_id) {
293 DBG(r, "end chxj_update_cookie() (old_cookie is null)");
297 cookie = (cookie_t*)apr_palloc(r->pool, sizeof(cookie_t));
298 cookie->cookie_id = NULL;
301 dconf = ap_get_module_config(r->per_dir_config, &chxj_module);
302 entryp = chxj_apply_convrule(r, dconf->convrules);
304 DBG(r, "end chxj_update_cookie() no pattern");
307 if (! (entryp->action & CONVRULE_COOKIE_ON_BIT)) {
308 DBG(r, "end chxj_update_cookie() CookieOff");
313 headers = (apr_array_header_t*)apr_table_elts(r->headers_out);
314 hentryp = (apr_table_entry_t*)headers->elts;
317 chxj_delete_cookie(r, old_cookie->cookie_id);
318 chxj_delete_cookie_expire(r, old_cookie->cookie_id);
320 file = chxj_cookie_db_lock(r);
322 ERR(r, "mod_chxj: Can't lock cookie db");
328 retval = apr_dbm_open_ex(&f,
330 chxj_cookie_db_name_create(r, dconf->cookie_db_dir),
334 if (retval != APR_SUCCESS) {
335 ERR(r, "could not open dbm (type %s) auth file: %s",
337 chxj_cookie_db_name_create(r,dconf->cookie_db_dir));
338 chxj_cookie_db_unlock(r, file);
344 uuid_string = apr_palloc(r->pool, APR_UUID_FORMATTED_LENGTH + 1);
345 memset(uuid_string, 0, APR_UUID_FORMATTED_LENGTH + 1);
346 apr_uuid_format(uuid_string, &uuid);;
348 md5_value = (unsigned char*)apr_palloc(r->pool, APR_MD5_DIGESTSIZE + 1);
349 memset(md5_value, 0, APR_MD5_DIGESTSIZE + 1);
351 retval = apr_md5(md5_value,
352 (const char*)uuid_string,
353 (apr_size_t)APR_UUID_FORMATTED_LENGTH);
354 if (retval != APR_SUCCESS) {
355 ERR(r, "md5 failed.");
360 cookie->cookie_id = apr_palloc(r->pool, apr_base64_encode_len(APR_MD5_DIGESTSIZE)+1);
361 memset(cookie->cookie_id, 0, APR_MD5_DIGESTSIZE+1);
362 apr_base64_encode(cookie->cookie_id, (char*)md5_value, APR_MD5_DIGESTSIZE);
364 cookie->cookie_id = chxj_url_encode(r,cookie->cookie_id);
372 dbmkey.dptr = cookie->cookie_id;
373 dbmkey.dsize = strlen(cookie->cookie_id);
378 cookie->cookie_headers = old_cookie->cookie_headers;
379 store_string = apr_palloc(r->pool, 1);
381 hentryp = (apr_table_entry_t*)cookie->cookie_headers->elts;
383 for (ii=0; ii<cookie->cookie_headers->nelts; ii++) {
384 if (ii) store_string = apr_pstrcat(r->pool,
389 DBG(r, "OLD COOKIE VALUE=[%s][%s]", hentryp[ii].key, hentryp[ii].val);
390 store_string = apr_pstrcat(r->pool,
397 dbmval.dptr = store_string;
398 dbmval.dsize = strlen(store_string);
403 retval = apr_dbm_store(f, dbmkey, dbmval);
404 if (retval != APR_SUCCESS) {
405 ERR(r, "Cannot store Cookie data to DBM file `%s'",
406 chxj_cookie_db_name_create(r, dconf->cookie_db_dir));
410 chxj_save_cookie_expire(r, cookie);
412 apr_table_setn(r->headers_in, "CHXJ_COOKIE_ID", cookie->cookie_id);
417 chxj_cookie_db_unlock(r, file);
419 DBG(r, "end chxj_update_cookie()");
426 * @return loaded data.
429 chxj_load_cookie(request_rec *r, char *cookie_id)
435 mod_chxj_config *dconf;
436 chxjconvrule_entry *entryp;
439 apr_table_t *load_cookie_table;
446 DBG(r, "========================================================");
447 DBG(r, "========================================================");
448 DBG(r, "========================================================");
449 DBG(r, "========================================================");
450 DBG(r, "start chxj_load_cookie() cookie_id=[%s]", cookie_id);
451 chxj_cookie_expire_gc(r);
453 cookie = (cookie_t*)apr_palloc(r->pool, sizeof(cookie_t));
454 cookie->cookie_headers = NULL;
455 cookie->cookie_id = apr_pstrdup(r->pool, cookie_id);
457 dconf = ap_get_module_config(r->per_dir_config, &chxj_module);
458 entryp = chxj_apply_convrule(r, dconf->convrules);
460 DBG(r, "end chxj_load_cookie() no pattern");
463 if (! (entryp->action & CONVRULE_COOKIE_ON_BIT)) {
464 DBG(r, "end chxj_load_cookie() CookieOff");
469 file = chxj_cookie_db_lock(r);
471 ERR(r, "mod_chxj: Can't lock cookie db");
475 retval = apr_dbm_open_ex(&f,
477 chxj_cookie_db_name_create(r, dconf->cookie_db_dir),
481 if (retval != APR_SUCCESS) {
483 "could not open dbm (type %s) auth file: %s",
485 chxj_cookie_db_name_create(r, dconf->cookie_db_dir));
492 dbmkey.dptr = apr_pstrdup(r->pool, cookie->cookie_id);
493 dbmkey.dsize = strlen(dbmkey.dptr);
495 if (apr_dbm_exists(f, dbmkey)) {
497 retval = apr_dbm_fetch(f, dbmkey, &dbmval);
498 if (retval != APR_SUCCESS) {
500 "could not fetch dbm (type %s) auth file: %s", "default",
501 chxj_cookie_db_name_create(r, dconf->cookie_db_dir));
504 load_cookie_table = apr_table_make(r->pool, 0);
505 load_string = apr_palloc(r->pool, dbmval.dsize+1);
507 memset(load_string, 0, dbmval.dsize+1);
508 memcpy(load_string, dbmval.dptr, dbmval.dsize);
510 char *header_cookie = apr_palloc(r->pool, 1);
512 header_cookie[0] = 0;
515 pair = apr_strtok(load_string, "\n", &pstat);
519 DBG(r, "Cookie:[%s]", pair);
522 tmp_pair = apr_pstrdup(r->pool, pair);
523 val = strchr(tmp_pair, '=');
527 apr_table_add(load_cookie_table, key, val);
529 tmp_sem = strchr(pair, ';');
533 if (strlen(header_cookie))
534 header_cookie = apr_pstrcat(r->pool, header_cookie, ";", NULL);
536 header_cookie = apr_pstrcat(r->pool, header_cookie, pair, NULL);
538 if (strlen(header_cookie))
539 apr_table_add(r->headers_in, "Cookie", header_cookie);
541 cookie->cookie_headers = (apr_array_header_t*)apr_table_elts(load_cookie_table);
543 if (apr_table_get(r->headers_in, "referer") == NULL) {
544 apr_table_setn(r->headers_in,
546 apr_table_get(load_cookie_table, REFERER_COOKIE_KEY));
550 * save cookie_id to request header.
552 apr_table_setn(r->headers_in, "CHXJ_COOKIE_ID", cookie->cookie_id);
555 chxj_cookie_db_unlock(r, file);
556 DBG(r, "end chxj_load_cookie()");
557 DBG(r, "========================================================");
558 DBG(r, "========================================================");
559 DBG(r, "========================================================");
560 DBG(r, "========================================================");
569 chxj_cookie_db_unlock(r, file);
573 DBG(r, "end chxj_load_cookie()");
574 DBG(r, "========================================================");
575 DBG(r, "========================================================");
576 DBG(r, "========================================================");
577 DBG(r, "========================================================");
583 chxj_add_cookie_parameter(request_rec *r, char *value, cookie_t *cookie)
588 DBG(r, "start chxj_add_cookie_parameter() cookie_id=[%s]", (cookie) ? cookie->cookie_id : NULL);
590 dst = apr_pstrdup(r->pool, value);
595 if (!cookie->cookie_id)
598 if (chxj_cookie_check_host(r, value) != 0) {
599 DBG(r, "end chxj_add_cookie_parameter()(check host)");
603 qs = strchr(dst, '?');
605 dst = apr_psprintf(r->pool, "%s&%s=%s", dst, CHXJ_COOKIE_PARAM, cookie->cookie_id);
608 dst = apr_psprintf(r->pool, "%s?%s=%s", dst, CHXJ_COOKIE_PARAM, cookie->cookie_id);
611 DBG(r, "end chxj_add_cookie_parameter() dst=[%s]", dst);
616 DBG(r, "end chxj_add_cookie_parameter() (on_error)");
622 chxj_cookie_check_host(request_rec *r, char *value)
626 DBG(r, "hostname=[%s]", r->hostname);
628 hostnm = s_get_hostname_from_url(r, value);
630 if (strcasecmp(hostnm, r->hostname) == 0)
640 s_get_hostname_from_url(request_rec *r, char *value)
645 if (strncasecmp(value, "http://", 7) == 0 )
646 return s_cut_until_end_hostname(r, &value[7]);
648 if (strncasecmp(value, "https://", 8) == 0)
649 return s_cut_until_end_hostname(r, &value[8]);
656 s_cut_until_end_hostname(request_rec *r, char *value)
661 hostnm = sp = apr_pstrdup(r->pool, value);
663 if (*sp == '/'|| *sp == '?') {
673 chxj_cookie_db_lock(request_rec *r)
677 mod_chxj_config *dconf;
679 dconf = (mod_chxj_config *)ap_get_module_config(r->per_dir_config, &chxj_module);
681 rv = apr_file_open(&file,
682 chxj_cookie_db_lock_name_create(r, dconf->cookie_db_dir),
683 APR_CREATE|APR_WRITE,
686 if (rv != APR_SUCCESS) {
687 ERR(r, "cookie lock file open failed.");
691 rv = apr_file_lock(file,APR_FLOCK_EXCLUSIVE);
692 if (rv != APR_SUCCESS) {
693 ERR(r, "cookie lock file open failed.");
694 apr_file_close(file);
703 chxj_cookie_db_unlock(request_rec *r, apr_file_t *file)
707 rv = apr_file_unlock(file);
708 if (rv != APR_SUCCESS) {
709 ERR(r, "cookie lock file open failed.");
713 apr_file_close(file);
718 chxj_delete_cookie(request_rec *r, char *cookie_id)
724 mod_chxj_config *dconf;
726 DBG(r, "start chxj_delete_cookie()");
728 file = chxj_cookie_db_lock(r);
730 ERR(r, "mod_chxj: Can't lock cookie db");
733 dconf = ap_get_module_config(r->per_dir_config, &chxj_module);
735 retval = apr_dbm_open_ex(&f,
737 chxj_cookie_db_name_create(r, dconf->cookie_db_dir),
741 if (retval != APR_SUCCESS) {
743 "could not open dbm (type %s) auth file: %s",
745 chxj_cookie_db_name_create(r,dconf->cookie_db_dir));
752 dbmkey.dptr = apr_pstrdup(r->pool, cookie_id);
753 dbmkey.dsize = strlen(dbmkey.dptr);
754 if (apr_dbm_exists(f, dbmkey)) {
755 apr_dbm_delete(f, dbmkey);
758 chxj_cookie_db_unlock(r, file);
760 DBG(r, "end chxj_delete_cookie()");
765 chxj_cookie_db_unlock(r, file);
774 chxj_cookie_db_name_create(request_rec *r, const char *dir)
779 dst = apr_pstrdup(r->pool, DEFAULT_COOKIE_DB_DIR);
782 dst = apr_pstrdup(r->pool, dir);
785 if (dst[strlen(dst)-1] != '/') {
786 dst = apr_pstrcat(r->pool, dst, "/", COOKIE_DB_NAME, NULL);
789 dst = apr_pstrcat(r->pool, dst, COOKIE_DB_NAME, NULL);
797 chxj_cookie_db_lock_name_create(request_rec *r, const char *dir)
800 DBG(r, "start chxj_cookie_db_lock_name_create()");
804 dst = apr_pstrdup(r->pool, DEFAULT_COOKIE_DB_DIR);
808 dst = apr_pstrdup(r->pool, dir);
811 DBG(r, "dst[strlen(dst)-1]=[%c]", dst[strlen(dst)-1]);
812 if (dst[strlen(dst)-1] != '/') {
813 dst = apr_pstrcat(r->pool, dst, "/", COOKIE_DB_LOCK_NAME, NULL);
816 dst = apr_pstrcat(r->pool, dst, COOKIE_DB_LOCK_NAME, NULL);
818 DBG(r, "end chxj_cookie_db_lock_name_create()");
825 chxj_save_cookie_expire(request_rec *r, cookie_t *cookie)
832 mod_chxj_config *dconf;
835 DBG(r, "start chxj_save_cookie_expire()");
837 DBG(r, "cookie is NULL");
841 dconf = ap_get_module_config(r->per_dir_config, &chxj_module);
843 DBG(r, "dconf is NULL");
847 file = chxj_cookie_expire_db_lock(r);
849 ERR(r, "mod_chxj: Can't lock cookie db");
855 retval = apr_dbm_open_ex(&f,
857 chxj_cookie_expire_db_name_create(r, dconf->cookie_db_dir),
861 if (retval != APR_SUCCESS) {
862 ERR(r, "could not open dbm (type %s) auth file: %s",
864 chxj_cookie_expire_db_name_create(r,dconf->cookie_db_dir));
865 chxj_cookie_expire_db_unlock(r, file);
872 dbmkey.dptr = cookie->cookie_id;
873 dbmkey.dsize = strlen(cookie->cookie_id);
879 store_string = apr_psprintf(r->pool, "%d", (int)time(NULL));
880 dbmval.dptr = store_string;
881 dbmval.dsize = strlen(store_string);
886 retval = apr_dbm_store(f, dbmkey, dbmval);
887 if (retval != APR_SUCCESS) {
888 ERR(r, "Cannot store Cookie data to DBM file `%s'",
889 chxj_cookie_db_name_create(r, dconf->cookie_db_dir));
894 chxj_cookie_expire_db_unlock(r, file);
896 DBG(r, "end chxj_save_cookie_expire()");
901 chxj_cookie_expire_db_name_create(request_rec *r, const char *dir)
906 dst = apr_pstrdup(r->pool, DEFAULT_COOKIE_DB_DIR);
909 dst = apr_pstrdup(r->pool, dir);
912 if (dst[strlen(dst)-1] != '/') {
913 dst = apr_pstrcat(r->pool, dst, "/", COOKIE_EXPIRE_DB_NAME, NULL);
916 dst = apr_pstrcat(r->pool, dst, COOKIE_EXPIRE_DB_NAME, NULL);
924 chxj_cookie_expire_db_lock_name_create(request_rec *r, const char *dir)
929 dst = apr_pstrdup(r->pool, DEFAULT_COOKIE_DB_DIR);
932 dst = apr_pstrdup(r->pool, dir);
934 if (dst[strlen(dst)-1] != '/') {
935 dst = apr_pstrcat(r->pool, dst, "/", COOKIE_EXPIRE_DB_LOCK_NAME, NULL);
938 dst = apr_pstrcat(r->pool, dst, COOKIE_EXPIRE_DB_LOCK_NAME, NULL);
946 chxj_cookie_expire_db_lock(request_rec *r)
950 mod_chxj_config *dconf;
952 dconf = (mod_chxj_config *)ap_get_module_config(r->per_dir_config, &chxj_module);
954 rv = apr_file_open(&file,
955 chxj_cookie_expire_db_lock_name_create(r, dconf->cookie_db_dir),
956 APR_CREATE|APR_WRITE,
959 if (rv != APR_SUCCESS) {
960 ERR(r, "cookie lock file open failed.");
964 rv = apr_file_lock(file,APR_FLOCK_EXCLUSIVE);
965 if (rv != APR_SUCCESS) {
966 ERR(r, "cookie lock file open failed.");
967 apr_file_close(file);
976 chxj_cookie_expire_db_unlock(request_rec *r, apr_file_t *file)
980 rv = apr_file_unlock(file);
981 if (rv != APR_SUCCESS) {
982 ERR(r, "cookie lock file open failed.");
986 apr_file_close(file);
990 chxj_delete_cookie_expire(request_rec *r, char *cookie_id)
996 mod_chxj_config *dconf;
998 DBG(r, "start chxj_delete_cookie_expire()");
1000 file = chxj_cookie_expire_db_lock(r);
1002 ERR(r, "mod_chxj: Can't lock cookie db");
1005 dconf = ap_get_module_config(r->per_dir_config, &chxj_module);
1007 retval = apr_dbm_open_ex(&f,
1009 chxj_cookie_expire_db_name_create(r, dconf->cookie_db_dir),
1013 if (retval != APR_SUCCESS) {
1015 "could not open dbm (type %s) auth file: %s",
1017 chxj_cookie_expire_db_name_create(r,dconf->cookie_db_dir));
1024 dbmkey.dptr = apr_pstrdup(r->pool, cookie_id);
1025 dbmkey.dsize = strlen(dbmkey.dptr);
1026 if (apr_dbm_exists(f, dbmkey)) {
1027 apr_dbm_delete(f, dbmkey);
1030 chxj_cookie_expire_db_unlock(r, file);
1032 DBG(r, "end chxj_delete_cookie_expire()");
1037 chxj_cookie_expire_db_unlock(r, file);
1046 chxj_cookie_expire_gc(request_rec *r)
1048 apr_status_t retval;
1053 mod_chxj_config *dconf;
1056 DBG(r, "start chxj_cookie_expire_gc()");
1058 file = chxj_cookie_expire_db_lock(r);
1060 ERR(r, "mod_chxj: Can't lock cookie db");
1063 dconf = ap_get_module_config(r->per_dir_config, &chxj_module);
1065 retval = apr_dbm_open_ex(&f,
1067 chxj_cookie_expire_db_name_create(r, dconf->cookie_db_dir),
1071 if (retval != APR_SUCCESS) {
1073 "could not open dbm (type %s) auth file: %s",
1075 chxj_cookie_expire_db_name_create(r,dconf->cookie_db_dir));
1082 memset(&dbmkey, 0, sizeof(apr_datum_t));
1084 now_time = time(NULL);
1086 retval = apr_dbm_firstkey(f, &dbmkey);
1087 if (retval == APR_SUCCESS) {
1088 DBG(r, "firstkey=[%.*s]", (int)dbmkey.dsize, dbmkey.dptr);
1091 char *old_cookie_id;
1095 retval = apr_dbm_fetch(f, dbmkey, &dbmval);
1096 if (retval != APR_SUCCESS) {
1099 tmp = apr_palloc(r->pool, dbmval.dsize+1);
1100 memset(tmp, 0, dbmval.dsize+1);
1101 memcpy(tmp, dbmval.dptr, dbmval.dsize);
1104 val_time = atoi(tmp);
1106 if (dconf->cookie_timeout == 0)
1107 cmp_time = now_time - DEFAULT_COOKIE_TIMEOUT;
1109 cmp_time = now_time - dconf->cookie_timeout;
1111 DBG(r, "dconf->cookie_timeout=[%d]", (int)dconf->cookie_timeout);
1112 DBG(r, "key=[%.*s] cmp_time=[%d] val_time=[%d]", (int)dbmkey.dsize, dbmkey.dptr, cmp_time, val_time);
1113 if (cmp_time >= val_time) {
1114 apr_dbm_delete(f, dbmkey);
1116 old_cookie_id = apr_palloc(r->pool, dbmkey.dsize+1);
1117 memset(old_cookie_id, 0, dbmkey.dsize+1);
1118 memcpy(old_cookie_id, dbmkey.dptr, dbmkey.dsize);
1120 chxj_delete_cookie(r,old_cookie_id);
1121 DBG(r, "detect timeout cookie [%s]", old_cookie_id);
1124 retval = apr_dbm_nextkey(f, &dbmkey);
1125 } while(retval == APR_SUCCESS && dbmkey.dptr != NULL);
1129 chxj_cookie_expire_db_unlock(r, file);
1131 DBG(r, "end chxj_cookie_expire_gc()");
1136 chxj_cookie_expire_db_unlock(r, file);