]>
git.ipfire.org Git - thirdparty/squid.git/blob - helpers/external_acl/session/squid_session.c
2 * squid_session: Squid external acl helper for tracking sessions
4 * Copyright (C) 2006 Henrik Nordstrom <henrik@henriknordstrom.net>
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.
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.
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.
25 #include <sys/types.h>
39 #if defined(HAVE_DB_185_H)
41 #elif defined(HAVE_DB_H)
47 static int session_ttl
= 3600;
49 const char *program_name
;
53 static void init_db(void)
55 db
= dbopen(db_path
, O_CREAT
| O_RDWR
, 0666, DB_BTREE
, NULL
);
57 fprintf(stderr
, "%s: Failed to open session db '%s'\n", program_name
, db_path
);
62 static void shutdown_db(void)
67 int session_is_active
= 0;
69 static int session_active(const char *details
)
72 key
.data
= (void *)details
;
73 key
.size
= strlen(details
);
74 if (db
->get(db
, &key
, &data
, 0) == 0) {
76 if (data
.size
!= sizeof(timestamp
)) {
77 fprintf(stderr
, "%s: CORRUPTED DATABASE (%s)\n", program_name
, details
);
81 memcpy(×tamp
, data
.data
, sizeof(timestamp
));
82 if (timestamp
+ session_ttl
>= time(NULL
))
88 static void session_login(const char *details
)
91 time_t now
= time(NULL
);
92 key
.data
= (void *)details
;
93 key
.size
= strlen(details
);
95 data
.size
= sizeof(now
);
96 db
->put(db
, &key
, &data
, 0);
99 static void session_logout(const char *details
)
102 key
.data
= (void *)details
;
103 key
.size
= strlen(details
);
104 db
->del(db
, &key
, 0);
107 static void usage(void)
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");
114 int main(int argc
, char **argv
)
118 int default_action
= 1;
120 program_name
= argv
[0];
122 while ((opt
= getopt(argc
, argv
, "t:b:a?")) != -1) {
125 session_ttl
= strtol(optarg
, NULL
, 0);
140 setbuf(stdout
, NULL
);
144 while (fgets(request
, sizeof(request
), stdin
)) {
145 const char *index
, *detail
;
148 index
= strtok(request
, " \n");
149 detail
= strtok(NULL
, "\n");
150 lastdetail
= strrchr(detail
, ' ');
152 if (strcmp(lastdetail
, " LOGIN") == 0) {
153 *lastdetail
++ = '\0';
155 } else if (strcmp(lastdetail
, " LOGOUT") == 0) {
157 *lastdetail
++ = '\0';
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
);
173 printf("%s ERR message=\"No session available\"\n", index
);