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