]> git.ipfire.org Git - thirdparty/squid.git/blob - helpers/external_acl/session/ext_session_acl.cc
ext_session_acl: version 1.1
[thirdparty/squid.git] / helpers / external_acl / session / ext_session_acl.cc
1 /*
2 * ext_session_acl: Squid external acl helper for tracking sessions
3 *
4 * Copyright (C) 2006 Henrik Nordstrom <henrik@henriknordstrom.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
19 */
20
21 #if HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 #include "helpers/defines.h"
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #if HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #include <string.h>
35 #include <time.h>
36 #if HAVE_GETOPT_H
37 #include <getopt.h>
38 #endif
39
40 /* At this point all Bit Types are already defined, so we must
41 protect from multiple type definition on platform where
42 __BIT_TYPES_DEFINED__ is not defined.
43 */
44 #ifndef __BIT_TYPES_DEFINED__
45 #define __BIT_TYPES_DEFINED__
46 #endif
47
48 #if HAVE_DB_185_H
49 #include <db_185.h>
50 #elif HAVE_DB_H
51 #include <db.h>
52 #endif
53
54 static int session_ttl = 3600;
55 static int fixed_timeout = 0;
56 char *db_path = NULL;
57 const char *program_name;
58
59 DB *db = NULL;
60
61 static void init_db(void)
62 {
63 db = dbopen(db_path, O_CREAT | O_RDWR, 0666, DB_BTREE, NULL);
64 if (!db) {
65 fprintf(stderr, "FATAL: %s: Failed to open session db '%s'\n", program_name, db_path);
66 exit(1);
67 }
68 }
69
70 static void shutdown_db(void)
71 {
72 db->close(db);
73 }
74
75 int session_is_active = 0;
76
77 static int session_active(const char *details, size_t len)
78 {
79 DBT key, data;
80 key.data = (void *)details;
81 key.size = len;
82 if (db->get(db, &key, &data, 0) == 0) {
83 time_t timestamp;
84 if (data.size != sizeof(timestamp)) {
85 fprintf(stderr, "ERROR: %s: CORRUPTED DATABASE (%s)\n", program_name, details);
86 db->del(db, &key, 0);
87 return 0;
88 }
89 memcpy(&timestamp, data.data, sizeof(timestamp));
90 if (timestamp + session_ttl >= time(NULL))
91 return 1;
92 }
93 return 0;
94 }
95
96 static void session_login(const char *details, size_t len)
97 {
98 DBT key, data;
99 time_t now = time(NULL);
100 key.data = (void *)details;
101 key.size = len;
102 data.data = &now;
103 data.size = sizeof(now);
104 db->put(db, &key, &data, 0);
105 db->sync(db, 0);
106 }
107
108 static void session_logout(const char *details, size_t len)
109 {
110 DBT key;
111 key.data = (void *)details;
112 key.size = len;
113 db->del(db, &key, 0);
114 }
115
116 static void usage(void)
117 {
118 fprintf(stderr, "Usage: %s [-t|-T session_timeout] [-b dbpath] [-a]\n", program_name);
119 fprintf(stderr, " -t sessiontimeout Idle timeout after which sessions will be forgotten (user activity will reset)\n");
120 fprintf(stderr, " -T sessiontimeout Fixed timeout after which sessions will be forgotten (regardless of user activity)\n");
121 fprintf(stderr, " -b dbpath Path where persistent session database will be kept\n");
122 fprintf(stderr, " -a Active mode requiring LOGIN argument to start a session\n");
123 }
124 int main(int argc, char **argv)
125 {
126 char request[HELPER_INPUT_BUFFER];
127 int opt;
128 int default_action = 1;
129
130 program_name = argv[0];
131
132 while ((opt = getopt(argc, argv, "t:T:b:a?")) != -1) {
133 switch (opt) {
134 case 'T':
135 fixed_timeout = 1;
136 case 't':
137 session_ttl = strtol(optarg, NULL, 0);
138 break;
139 case 'b':
140 db_path = optarg;
141 break;
142 case 'a':
143 default_action = 0;
144 break;
145 case '?':
146 usage();
147 exit(0);
148 break;
149 }
150 }
151
152 setbuf(stdout, NULL);
153
154 init_db();
155
156 while (fgets(request, HELPER_INPUT_BUFFER, stdin)) {
157 int action = 0;
158 const char *channel_id = strtok(request, " ");
159 const char *detail = strtok(NULL, "\n");
160 if (detail == NULL) {
161 // Only 1 paramater supplied. We are expecting at least 2 (including the channel ID)
162 fprintf(stderr, "FATAL: %s is concurrent and requires the concurrency option to be specified.\n", program_name);
163 exit(1);
164 }
165 const char *lastdetail = strrchr(detail, ' ');
166 size_t detail_len = strlen(detail);
167 if (lastdetail) {
168 if (strcmp(lastdetail, " LOGIN") == 0) {
169 action = 1;
170 detail_len = (size_t)(lastdetail-detail);
171 } else if (strcmp(lastdetail, " LOGOUT") == 0) {
172 action = -1;
173 detail_len = (size_t)(lastdetail-detail);
174 }
175 }
176 if (action == -1) {
177 session_logout(detail, detail_len);
178 printf("%s OK message=\"Bye\"\n", channel_id);
179 } else if (action == 1) {
180 session_login(detail, detail_len);
181 printf("%s OK message=\"Welcome\"\n", channel_id);
182 } else if (session_active(detail, detail_len)) {
183 if (fixed_timeout == 0) {
184 session_login(detail, detail_len);
185 }
186 printf("%s OK\n", channel_id);
187 } else if (default_action == 1) {
188 session_login(detail, detail_len);
189 printf("%s ERR message=\"Welcome\"\n", channel_id);
190 } else {
191 printf("%s ERR message=\"No session available\"\n", channel_id);
192 }
193 }
194 shutdown_db();
195 return 0;
196 }