Squid Web Cache v8/master
Loading...
Searching...
No Matches
ext_session_acl.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2026 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9/*
10 * ext_session_acl: Squid external acl helper for tracking sessions
11 *
12 * Copyright (C) 2006 Henrik Nordstrom <henrik@henriknordstrom.net>
13 *
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.
18 *
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.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, see <https://www.gnu.org/licenses/>.
26 */
27
28#if HAVE_CONFIG_H
29#include "squid.h"
30#endif
32
33#include <cstdlib>
34#include <cstring>
35#include <ctime>
36#if HAVE_DB_H
37#include <db.h>
38#endif
39#include <fcntl.h>
40#if HAVE_GETOPT_H
41#include <getopt.h>
42#endif
43#include <sys/types.h>
44#include <sys/stat.h>
45#if HAVE_TDB_H
46#include <tdb.h>
47#endif
48#if HAVE_UNISTD_H
49#include <unistd.h>
50#endif
51
52/* At this point all Bit Types are already defined, so we must
53 protect from multiple type definition on platform where
54 __BIT_TYPES_DEFINED__ is not defined.
55 */
56#ifndef __BIT_TYPES_DEFINED__
57#define __BIT_TYPES_DEFINED__
58#endif
59
60static int session_ttl = 3600;
61static int fixed_timeout = 0;
62char *db_path = nullptr;
63const char *program_name;
64
65#if USE_BERKLEYDB
66DB *db = nullptr;
67DB_ENV *db_env = nullptr;
68typedef DBT DB_ENTRY;
69
70#elif HAVE_LIBTDB
71TDB_CONTEXT *db = nullptr;
72typedef TDB_DATA DB_ENTRY;
73
74#else
75#error "Either Berkeley DB or Trivial DB must be available"
76#endif
77
78static void
80{
81 if (db) {
82#if USE_BERKLEYDB
83 db->close(db, 0);
84 }
85 if (db_env) {
86 db_env->close(db_env, 0);
87
88#elif HAVE_LIBTDB
89 if (tdb_close(db) != 0) {
90 fprintf(stderr, "%s| WARNING: error closing session db '%s'\n", program_name, db_path);
91 exit(EXIT_FAILURE);
92 }
93#endif
94 }
96}
97
98static void init_db(void)
99{
100 struct stat st_buf;
101
102 if (db_path) {
103 if (!stat(db_path, &st_buf)) {
104 if (S_ISDIR (st_buf.st_mode)) {
105#if USE_BERKLEYDB
106 /* If directory then open database environment. This prevents sync problems
107 between different processes. Otherwise fallback to single file */
108 db_env_create(&db_env, 0);
109 if (db_env->open(db_env, db_path, DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK, 0666)) {
110 fprintf(stderr, "FATAL: %s: Failed to open database environment in '%s'\n", program_name, db_path);
111 db_env->close(db_env, 0);
112 exit(EXIT_FAILURE);
113 }
114 db_create(&db, db_env, 0);
115#elif HAVE_LIBTDB
116 std::string newPath(db_path);
117 newPath.append("session", 7);
118 db_path = xstrdup(newPath.c_str());
119#endif
120 }
121 }
122 }
123
124#if USE_BERKLEYDB
125 if (db_env) {
126 if (db->open(db, nullptr, "session", nullptr, DB_BTREE, DB_CREATE, 0666)) {
127 fprintf(stderr, "FATAL: %s: Failed to open db file '%s' in dir '%s'\n",
128 program_name, "session", db_path);
129 db_env->close(db_env, 0);
130 exit(EXIT_FAILURE);
131 }
132 } else {
133 db_create(&db, nullptr, 0);
134 if (db->open(db, nullptr, db_path, nullptr, DB_BTREE, DB_CREATE, 0666)) {
135 db = nullptr;
136 }
137 }
138#elif HAVE_LIBTDB
139#if _SQUID_FREEBSD_ && !defined(O_DSYNC)
140 // FreeBSD lacks O_DSYNC, O_SYNC is closest to correct behaviour
141#define O_DSYNC O_SYNC
142#endif
143 db = tdb_open(db_path, 0, TDB_CLEAR_IF_FIRST, O_CREAT|O_DSYNC, 0666);
144#endif
145 if (!db) {
146 fprintf(stderr, "FATAL: %s: Failed to open session db '%s'\n", program_name, db_path);
147 shutdown_db();
148 exit(EXIT_FAILURE);
149 }
150}
151
153
154static size_t
155dataSize(DB_ENTRY *data)
156{
157#if USE_BERKLEYDB
158 return data->size;
159#elif HAVE_LIBTDB
160 return data->dsize;
161#endif
162}
163
164static bool
165fetchKey(/*const*/ DB_ENTRY &key, DB_ENTRY *data)
166{
167#if USE_BERKLEYDB
168 return (db->get(db, nullptr, &key, data, 0) == 0);
169#elif HAVE_LIBTDB
170 // NP: API says returns NULL on errors, but return is a struct type WTF??
171 *data = tdb_fetch(db, key);
172 return (data->dptr != nullptr);
173#endif
174}
175
176static void
177deleteEntry(/*const*/ DB_ENTRY &key)
178{
179#if USE_BERKLEYDB
180 db->del(db, nullptr, &key, 0);
181#elif HAVE_LIBTDB
182 tdb_delete(db, key);
183#endif
184}
185
186static void
187copyValue(void *dst, const DB_ENTRY *src, size_t sz)
188{
189#if USE_BERKLEYDB
190 memcpy(dst, src->data, sz);
191#elif HAVE_LIBTDB
192 memcpy(dst, src->dptr, sz);
193#endif
194}
195
196static int session_active(const char *details, size_t len)
197{
198#if USE_BERKLEYDB
199 DBT key = {};
200 key.data = const_cast<char*>(details);
201 key.size = len;
202
203 DBT data = {};
204#elif HAVE_LIBTDB
205 TDB_DATA key = {};
206 key.dptr = reinterpret_cast<decltype(key.dptr)>(const_cast<char*>(details));
207 key.dsize = len;
208
209 TDB_DATA data = {};
210#else
211 (void)len;
212#endif
213 if (fetchKey(key, &data)) {
214 time_t timestamp;
215 if (dataSize(&data) != sizeof(timestamp)) {
216 fprintf(stderr, "ERROR: %s: CORRUPTED DATABASE (%s)\n", program_name, details);
217 deleteEntry(key);
218 return 0;
219 }
220 copyValue(&timestamp, &data, sizeof(timestamp));
221 if (timestamp + session_ttl >= time(nullptr))
222 return 1;
223 }
224 return 0;
225}
226
227static void
228session_login(/*const*/ char *details, size_t len)
229{
230 DB_ENTRY key = {};
231 DB_ENTRY data = {};
232 time_t now = time(nullptr);
233#if USE_BERKLEYDB
234 key.data = static_cast<decltype(key.data)>(details);
235 key.size = len;
236 data.data = &now;
237 data.size = sizeof(now);
238 db->put(db, nullptr, &key, &data, 0);
239#elif HAVE_LIBTDB
240 key.dptr = reinterpret_cast<decltype(key.dptr)>(details);
241 key.dsize = len;
242 data.dptr = reinterpret_cast<decltype(data.dptr)>(&now);
243 data.dsize = sizeof(now);
244 tdb_store(db, key, data, 0);
245#endif
246}
247
248static void
249session_logout(/*const*/ char *details, size_t len)
250{
251 DB_ENTRY key = {};
252#if USE_BERKLEYDB
253 key.data = static_cast<decltype(key.data)>(details);
254 key.size = len;
255#elif HAVE_LIBTDB
256 key.dptr = reinterpret_cast<decltype(key.dptr)>(details);
257 key.dsize = len;
258#endif
259 deleteEntry(key);
260}
261
262static void usage(void)
263{
264 fprintf(stderr, "Usage: %s [-t|-T session_timeout] [-b dbpath] [-a]\n", program_name);
265 fprintf(stderr, " -t sessiontimeout Idle timeout after which sessions will be forgotten (user activity will reset)\n");
266 fprintf(stderr, " -T sessiontimeout Fixed timeout after which sessions will be forgotten (regardless of user activity)\n");
267 fprintf(stderr, " -b dbpath Path where persistent session database will be kept\n");
268 fprintf(stderr, " -a Active mode requiring LOGIN argument to start a session\n");
269}
270int main(int argc, char **argv)
271{
272 char request[HELPER_INPUT_BUFFER];
273 int opt;
274 int default_action = 1;
275
276 program_name = argv[0];
277
278 while ((opt = getopt(argc, argv, "t:T:b:a?")) != -1) {
279 switch (opt) {
280 case 'T':
281 fixed_timeout = 1;
282 [[fallthrough]];
283 case 't':
284 session_ttl = strtol(optarg, nullptr, 0);
285 break;
286 case 'b':
288 break;
289 case 'a':
290 default_action = 0;
291 break;
292 case '?':
293 usage();
294 exit(EXIT_SUCCESS);
295 break;
296 }
297 }
298
299 setbuf(stdout, nullptr);
300
301 init_db();
302
303 while (fgets(request, HELPER_INPUT_BUFFER, stdin)) {
304 int action = 0;
305 const char *channel_id = strtok(request, " ");
306 char *detail = strtok(nullptr, "\n");
307 if (detail == nullptr) {
308 // Only 1 parameter supplied. We are expecting at least 2 (including the channel ID)
309 fprintf(stderr, "FATAL: %s is concurrent and requires the concurrency option to be specified.\n", program_name);
310 shutdown_db();
311 exit(EXIT_FAILURE);
312 }
313 char *lastdetail = strrchr(detail, ' ');
314 size_t detail_len = strlen(detail);
315 if (lastdetail) {
316 if (strcmp(lastdetail, " LOGIN") == 0) {
317 action = 1;
318 detail_len = (size_t)(lastdetail-detail);
319 *lastdetail = '\0';
320 } else if (strcmp(lastdetail, " LOGOUT") == 0) {
321 action = -1;
322 detail_len = (size_t)(lastdetail-detail);
323 *lastdetail = '\0';
324 } else if (!default_action && strcmp(lastdetail, " -") == 0) {
325 // no action; LOGIN/LOGOUT not supplied
326 // but truncate the '-' %DATA value given by Squid-4 and later
327 detail_len = (size_t)(lastdetail-detail);
328 *lastdetail = '\0';
329 }
330 }
331 if (action == -1) {
332 session_logout(detail, detail_len);
333 printf("%s OK message=\"Bye\"\n", channel_id);
334 } else if (action == 1) {
335 session_login(detail, detail_len);
336 printf("%s OK message=\"Welcome\"\n", channel_id);
337 } else if (session_active(detail, detail_len)) {
338 if (fixed_timeout == 0) {
339 session_login(detail, detail_len);
340 }
341 printf("%s OK\n", channel_id);
342 } else if (default_action == 1) {
343 session_login(detail, detail_len);
344 printf("%s ERR message=\"Welcome\"\n", channel_id);
345 } else {
346 printf("%s ERR message=\"No session available\"\n", channel_id);
347 }
348 }
349 shutdown_db();
350 return EXIT_SUCCESS;
351}
352
#define HELPER_INPUT_BUFFER
static bool fetchKey(DB_ENTRY &key, DB_ENTRY *data)
static void shutdown_db()
static void session_login(char *details, size_t len)
static void init_db(void)
char * db_path
static int fixed_timeout
static int session_ttl
int session_is_active
static void session_logout(char *details, size_t len)
static void copyValue(void *dst, const DB_ENTRY *src, size_t sz)
static int session_active(const char *details, size_t len)
static size_t dataSize(DB_ENTRY *data)
static void deleteEntry(DB_ENTRY &key)
static void usage(void)
const char * program_name
TDB_CONTEXT * db
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition getopt.c:62
char * optarg
Definition getopt.c:51
int main()
#define xfree
#define xstrdup
int const char size_t