]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/external/session/ext_session_acl.cc
2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 * ext_session_acl: Squid external acl helper for tracking sessions
12 * Copyright (C) 2006 Henrik Nordstrom <henrik@henriknordstrom.net>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 #include "helper/protocol_defines.h"
44 #include <sys/types.h>
50 /* At this point all Bit Types are already defined, so we must
51 protect from multiple type definition on platform where
52 __BIT_TYPES_DEFINED__ is not defined.
54 #ifndef __BIT_TYPES_DEFINED__
55 #define __BIT_TYPES_DEFINED__
58 static int session_ttl
= 3600;
59 static int fixed_timeout
= 0;
61 const char *program_name
;
64 DB_ENV
*db_env
= NULL
;
66 static void init_db(void)
71 if (!stat(db_path
, &st_buf
)) {
72 if (S_ISDIR (st_buf
.st_mode
)) {
73 /* If directory then open database environment. This prevents sync problems
74 between different processes. Otherwise fallback to single file */
75 db_env_create(&db_env
, 0);
76 if (db_env
->open(db_env
, db_path
, DB_CREATE
| DB_INIT_MPOOL
| DB_INIT_LOCK
, 0666)) {
77 fprintf(stderr
, "FATAL: %s: Failed to open database environment in '%s'\n", program_name
, db_path
);
78 db_env
->close(db_env
, 0);
81 db_create(&db
, db_env
, 0);
87 if (db
->open(db
, NULL
, "session", NULL
, DB_BTREE
, DB_CREATE
, 0666)) {
88 fprintf(stderr
, "FATAL: %s: Failed to open db file '%s' in dir '%s'\n",
89 program_name
, "session", db_path
);
90 db_env
->close(db_env
, 0);
94 db_create(&db
, NULL
, 0);
95 if (db
->open(db
, NULL
, db_path
, NULL
, DB_BTREE
, DB_CREATE
, 0666)) {
96 fprintf(stderr
, "FATAL: %s: Failed to open session db '%s'\n", program_name
, db_path
);
102 static void shutdown_db(void)
106 db_env
->close(db_env
, 0);
110 int session_is_active
= 0;
112 static int session_active(const char *details
, size_t len
)
116 key
.data
= (void *)details
;
118 if (db
->get(db
, NULL
, &key
, &data
, 0) == 0) {
120 if (data
.size
!= sizeof(timestamp
)) {
121 fprintf(stderr
, "ERROR: %s: CORRUPTED DATABASE (%s)\n", program_name
, details
);
122 db
->del(db
, NULL
, &key
, 0);
125 memcpy(×tamp
, data
.data
, sizeof(timestamp
));
126 if (timestamp
+ session_ttl
>= time(NULL
))
132 static void session_login(const char *details
, size_t len
)
136 key
.data
= (void *)details
;
138 time_t now
= time(NULL
);
140 data
.size
= sizeof(now
);
141 db
->put(db
, NULL
, &key
, &data
, 0);
144 static void session_logout(const char *details
, size_t len
)
147 key
.data
= (void *)details
;
149 db
->del(db
, NULL
, &key
, 0);
152 static void usage(void)
154 fprintf(stderr
, "Usage: %s [-t|-T session_timeout] [-b dbpath] [-a]\n", program_name
);
155 fprintf(stderr
, " -t sessiontimeout Idle timeout after which sessions will be forgotten (user activity will reset)\n");
156 fprintf(stderr
, " -T sessiontimeout Fixed timeout after which sessions will be forgotten (regardless of user activity)\n");
157 fprintf(stderr
, " -b dbpath Path where persistent session database will be kept\n");
158 fprintf(stderr
, " -a Active mode requiring LOGIN argument to start a session\n");
160 int main(int argc
, char **argv
)
162 char request
[HELPER_INPUT_BUFFER
];
164 int default_action
= 1;
166 program_name
= argv
[0];
168 while ((opt
= getopt(argc
, argv
, "t:T:b:a?")) != -1) {
173 session_ttl
= strtol(optarg
, NULL
, 0);
188 setbuf(stdout
, NULL
);
192 while (fgets(request
, HELPER_INPUT_BUFFER
, stdin
)) {
194 const char *channel_id
= strtok(request
, " ");
195 char *detail
= strtok(NULL
, "\n");
196 if (detail
== NULL
) {
197 // Only 1 paramater supplied. We are expecting at least 2 (including the channel ID)
198 fprintf(stderr
, "FATAL: %s is concurrent and requires the concurrency option to be specified.\n", program_name
);
202 char *lastdetail
= strrchr(detail
, ' ');
203 size_t detail_len
= strlen(detail
);
205 if (strcmp(lastdetail
, " LOGIN") == 0) {
207 detail_len
= (size_t)(lastdetail
-detail
);
209 } else if (strcmp(lastdetail
, " LOGOUT") == 0) {
211 detail_len
= (size_t)(lastdetail
-detail
);
216 session_logout(detail
, detail_len
);
217 printf("%s OK message=\"Bye\"\n", channel_id
);
218 } else if (action
== 1) {
219 session_login(detail
, detail_len
);
220 printf("%s OK message=\"Welcome\"\n", channel_id
);
221 } else if (session_active(detail
, detail_len
)) {
222 if (fixed_timeout
== 0) {
223 session_login(detail
, detail_len
);
225 printf("%s OK\n", channel_id
);
226 } else if (default_action
== 1) {
227 session_login(detail
, detail_len
);
228 printf("%s ERR message=\"Welcome\"\n", channel_id
);
230 printf("%s ERR message=\"No session available\"\n", channel_id
);