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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors