]> git.ipfire.org Git - thirdparty/squid.git/blob - helpers/external_acl/session/squid_session.c
Summary: Synced with libecap, adopted pass-all-changes-through transactions
[thirdparty/squid.git] / helpers / external_acl / session / squid_session.c
1 /*
2 * squid_session: 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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #if HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #include <string.h>
34 #include <time.h>
35 #if HAVE_GETOPT_H
36 #include <getopt.h>
37 #endif
38
39 #if defined(HAVE_DB_185_H)
40 #include <db_185.h>
41 #elif defined(HAVE_DB_H)
42 #include <db.h>
43 #else
44 #include <db_185.h>
45 #endif
46
47 static int session_ttl = 3600;
48 char *db_path = NULL;
49 const char *program_name;
50
51 DB *db = NULL;
52
53 static void init_db(void)
54 {
55 db = dbopen(db_path, O_CREAT | O_RDWR, 0666, DB_BTREE, NULL);
56 if (!db) {
57 fprintf(stderr, "%s: Failed to open session db '%s'\n", program_name, db_path);
58 exit(1);
59 }
60 }
61
62 static void shutdown_db(void)
63 {
64 db->close(db);
65 }
66
67 int session_is_active = 0;
68
69 static int session_active(const char *details)
70 {
71 DBT key, data;
72 key.data = (void *)details;
73 key.size = strlen(details);
74 if (db->get(db, &key, &data, 0) == 0) {
75 time_t timestamp;
76 if (data.size != sizeof(timestamp)) {
77 fprintf(stderr, "%s: CORRUPTED DATABASE (%s)\n", program_name, details);
78 db->del(db, &key, 0);
79 return 0;
80 }
81 memcpy(&timestamp, data.data, sizeof(timestamp));
82 if (timestamp + session_ttl >= time(NULL))
83 return 1;
84 }
85 return 0;
86 }
87
88 static void session_login(const char *details)
89 {
90 DBT key, data;
91 time_t now = time(NULL);
92 key.data = (void *)details;
93 key.size = strlen(details);
94 data.data = &now;
95 data.size = sizeof(now);
96 db->put(db, &key, &data, 0);
97 }
98
99 static void session_logout(const char *details)
100 {
101 DBT key;
102 key.data = (void *)details;
103 key.size = strlen(details);
104 db->del(db, &key, 0);
105 }
106
107 static void usage(void)
108 {
109 fprintf(stderr, "Usage: %s [-t session_timeout] [-b dbpath] [-a]\n", program_name);
110 fprintf(stderr, " -t sessiontimeout Idle timeout after which sessions will be forgotten\n");
111 fprintf(stderr, " -b dbpath Path where persistent session database will be kept\n");
112 fprintf(stderr, " -a Active mode requiring LOGIN argument to start a session\n");
113 }
114 int main(int argc, char **argv)
115 {
116 char request[256];
117 int opt;
118 int default_action = 1;
119
120 program_name = argv[0];
121
122 while ((opt = getopt(argc, argv, "t:b:a?")) != -1) {
123 switch(opt) {
124 case 't':
125 session_ttl = strtol(optarg, NULL, 0);
126 break;
127 case 'b':
128 db_path = optarg;
129 break;
130 case 'a':
131 default_action = 0;
132 break;
133 case '?':
134 usage();
135 exit(0);
136 break;
137 }
138 }
139
140 setbuf(stdout, NULL);
141
142 init_db();
143
144 while (fgets(request, sizeof(request), stdin)) {
145 const char *index, *detail;
146 char *lastdetail;
147 int action = 0;
148 index = strtok(request, " \n");
149 detail = strtok(NULL, "\n");
150 lastdetail = strrchr(detail, ' ');
151 if (lastdetail) {
152 if (strcmp(lastdetail, " LOGIN") == 0) {
153 *lastdetail++ = '\0';
154 action = 1;
155 } else if (strcmp(lastdetail, " LOGOUT") == 0) {
156 action = -1;
157 *lastdetail++ = '\0';
158 }
159 }
160 if (action == -1) {
161 session_logout(detail);
162 printf("%s OK message=\"Bye\"\n", index);
163 } else if (action == 1) {
164 session_login(detail);
165 printf("%s OK message=\"Welcome\"\n", index);
166 } else if (session_active(detail)) {
167 session_login(detail);
168 printf("%s OK\n", index);
169 } else if (default_action == 1) {
170 session_login(detail);
171 printf("%s ERR message=\"Welcome\"\n", index);
172 } else {
173 printf("%s ERR message=\"No session available\"\n", index);
174 }
175 }
176 shutdown_db();
177 return 0;
178 }