]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/external/session/ext_session_acl.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / acl / external / session / ext_session_acl.cc
1 /*
2 * Copyright (C) 1996-2018 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
32 #include "helper/protocol_defines.h"
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_UNISTD_H
47 #include <unistd.h>
48 #endif
49
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.
53 */
54 #ifndef __BIT_TYPES_DEFINED__
55 #define __BIT_TYPES_DEFINED__
56 #endif
57
58 static int session_ttl = 3600;
59 static int fixed_timeout = 0;
60 char *db_path = NULL;
61 const char *program_name;
62
63 DB *db = NULL;
64 DB_ENV *db_env = NULL;
65
66 static void init_db(void)
67 {
68 struct stat st_buf;
69
70 if (db_path) {
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);
79 exit(EXIT_FAILURE);
80 }
81 db_create(&db, db_env, 0);
82 }
83 }
84 }
85
86 if (db_env) {
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);
91 exit(EXIT_FAILURE);
92 }
93 } else {
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);
97 exit(EXIT_FAILURE);
98 }
99 }
100 }
101
102 static void shutdown_db(void)
103 {
104 db->close(db, 0);
105 if (db_env) {
106 db_env->close(db_env, 0);
107 }
108 }
109
110 int session_is_active = 0;
111
112 static int session_active(const char *details, size_t len)
113 {
114 DBT key = {0};
115 DBT data = {0};
116 key.data = (void *)details;
117 key.size = len;
118 if (db->get(db, NULL, &key, &data, 0) == 0) {
119 time_t timestamp;
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);
123 return 0;
124 }
125 memcpy(&timestamp, data.data, sizeof(timestamp));
126 if (timestamp + session_ttl >= time(NULL))
127 return 1;
128 }
129 return 0;
130 }
131
132 static void session_login(const char *details, size_t len)
133 {
134 DBT key = {0};
135 DBT data = {0};
136 key.data = (void *)details;
137 key.size = len;
138 time_t now = time(NULL);
139 data.data = &now;
140 data.size = sizeof(now);
141 db->put(db, NULL, &key, &data, 0);
142 }
143
144 static void session_logout(const char *details, size_t len)
145 {
146 DBT key = {0};
147 key.data = (void *)details;
148 key.size = len;
149 db->del(db, NULL, &key, 0);
150 }
151
152 static void usage(void)
153 {
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");
159 }
160 int main(int argc, char **argv)
161 {
162 char request[HELPER_INPUT_BUFFER];
163 int opt;
164 int default_action = 1;
165
166 program_name = argv[0];
167
168 while ((opt = getopt(argc, argv, "t:T:b:a?")) != -1) {
169 switch (opt) {
170 case 'T':
171 fixed_timeout = 1;
172 case 't':
173 session_ttl = strtol(optarg, NULL, 0);
174 break;
175 case 'b':
176 db_path = optarg;
177 break;
178 case 'a':
179 default_action = 0;
180 break;
181 case '?':
182 usage();
183 exit(EXIT_SUCCESS);
184 break;
185 }
186 }
187
188 setbuf(stdout, NULL);
189
190 init_db();
191
192 while (fgets(request, HELPER_INPUT_BUFFER, stdin)) {
193 int action = 0;
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);
199 shutdown_db();
200 exit(EXIT_FAILURE);
201 }
202 char *lastdetail = strrchr(detail, ' ');
203 size_t detail_len = strlen(detail);
204 if (lastdetail) {
205 if (strcmp(lastdetail, " LOGIN") == 0) {
206 action = 1;
207 detail_len = (size_t)(lastdetail-detail);
208 *lastdetail = '\0';
209 } else if (strcmp(lastdetail, " LOGOUT") == 0) {
210 action = -1;
211 detail_len = (size_t)(lastdetail-detail);
212 *lastdetail = '\0';
213 } else if (!default_action && strcmp(lastdetail, " -") == 0) {
214 // no action; LOGIN/LOGOUT not supplied
215 // but truncate the '-' %DATA value given by Squid-4 and later
216 detail_len = (size_t)(lastdetail-detail);
217 *lastdetail = '\0';
218 }
219 }
220 if (action == -1) {
221 session_logout(detail, detail_len);
222 printf("%s OK message=\"Bye\"\n", channel_id);
223 } else if (action == 1) {
224 session_login(detail, detail_len);
225 printf("%s OK message=\"Welcome\"\n", channel_id);
226 } else if (session_active(detail, detail_len)) {
227 if (fixed_timeout == 0) {
228 session_login(detail, detail_len);
229 }
230 printf("%s OK\n", channel_id);
231 } else if (default_action == 1) {
232 session_login(detail, detail_len);
233 printf("%s ERR message=\"Welcome\"\n", channel_id);
234 } else {
235 printf("%s ERR message=\"No session available\"\n", channel_id);
236 }
237 }
238 shutdown_db();
239 return EXIT_SUCCESS;
240 }
241