#include <config.h>
#include <sys/param.h>
#ifndef MAXPATHLEN
#define MAXPATHLEN MAXPATHNAMELEN
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "auth_pts.h"
#include "cyrusdb.h"
#include "exitcodes.h"
#include "global.h"
#include "libconfig.h"
#include "lock.h"
#include "xmalloc.h"
static char rcsid[] = "$Id: ptexpire.c,v 1.19 2006/11/30 17:11:24 murch Exp $";
time_t timenow;
time_t expire_time = (3*60*60);
int config_need_data = 0;
static int expire_p(void *rockp __attribute__((unused)),
const char *key __attribute__((unused)),
int keylen __attribute__((unused)),
const char *data,
int datalen __attribute__((unused)))
{
struct auth_state *authstate = (struct auth_state *)data;
if (authstate->mark + expire_time < timenow) {
return 1;
}
return 0;
}
static int expire_cb(void *rockp,
const char *key, int keylen,
const char *data __attribute__((unused)),
int datalen __attribute__((unused)))
{
syslog(LOG_DEBUG, "deleteing entry for %s", key);
config_ptscache_db->delete((struct db *)rockp, key, keylen, NULL, 0);
return 0;
}
int main(int argc, char *argv[])
{
struct db *ptdb;
char fnamebuf[1024];
extern char *optarg;
int opt;
int r;
char *alt_config = NULL;
if (geteuid() == 0) fatal("must run as the Cyrus user", EC_USAGE);
openlog("ptexpire", LOG_PID, SYSLOG_FACILITY);
while ((opt = getopt(argc, argv, "C:E:")) != EOF) {
switch (opt) {
case 'C':
alt_config = optarg;
break;
case 'E':
expire_time = atoi(optarg);
break;
default:
fprintf(stderr,"usage: [-C filename] [-E time]"
"\n\t-C <filename>\tAlternate Config File"
"\n\t-E <seconds>\tExpiration time"
"\n");
syslog(LOG_ERR, "Invalid command line option");
exit(-1);
break;
}
}
cyrus_init(alt_config, "ptexpire", 0);
timenow = time(0);
syslog(LOG_INFO, "Expiring entries older than %d seconds (currently %d)",
expire_time, timenow);
syslog(LOG_DEBUG, "%s", rcsid);
strcpy(fnamebuf, config_dir);
strcat(fnamebuf, PTS_DBFIL);
r = config_ptscache_db->open(fnamebuf, CYRUSDB_CREATE, &ptdb);
if(r != CYRUSDB_OK) {
syslog(LOG_ERR, "error opening %s (%s)", fnamebuf,
cyrusdb_strerror(r));
exit(1);
}
config_ptscache_db->foreach(ptdb, "", 0, expire_p, expire_cb, ptdb, NULL);
config_ptscache_db->close(ptdb);
cyrus_done();
syslog(LOG_INFO, "finished");
return 0;
}