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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/* zoneinfo_db.c -- zoneinfo DB routines
*
* Copyright (c) 1994-2013 Carnegie Mellon University. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The name "Carnegie Mellon University" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For permission or any legal
* details, please contact
* Carnegie Mellon University
* Center for Technology Transfer and Enterprise Creation
* 4615 Forbes Avenue
* Suite 302
* Pittsburgh, PA 15213
* (412) 268-7393, fax: (412) 268-7395
* innovation@andrew.cmu.edu
*
* 4. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by Computing Services
* at Carnegie Mellon University (http://www.cmu.edu/computing/)."
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include <config.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <syslog.h>
#include "assert.h"
#include "cyrusdb.h"
#include "global.h"
#include "tok.h"
#include "util.h"
#include "xmalloc.h"
#include "zoneinfo_db.h"
#define DB config_zoneinfo_db
struct db *zoneinfodb;
static int zoneinfo_dbopen = 0;
static struct buf databuf = BUF_INITIALIZER;
EXPORTED int zoneinfo_open(const char *fname)
{
int ret;
char *tofree = NULL;
if (!fname) fname = config_getstring(IMAPOPT_ZONEINFO_DB_PATH);
/* create db file name */
if (!fname) {
tofree = strconcat(config_dir, FNAME_ZONEINFODB, (char *)NULL);
fname = tofree;
}
ret = cyrusdb_open(DB, fname, CYRUSDB_CREATE, &zoneinfodb);
if (ret != 0) {
syslog(LOG_ERR, "DBERROR: opening %s: %s", fname,
cyrusdb_strerror(ret));
}
else zoneinfo_dbopen = 1;
free(tofree);
return ret;
}
EXPORTED void zoneinfo_close(struct txn *tid)
{
if (zoneinfo_dbopen) {
int r;
if (tid) {
r = cyrusdb_commit(zoneinfodb, tid);
if (r) {
syslog(LOG_ERR, "DBERROR: error committing zoneinfo: %s",
cyrusdb_strerror(r));
}
}
r = cyrusdb_close(zoneinfodb);
if (r) {
syslog(LOG_ERR, "DBERROR: error closing zoneinfo: %s",
cyrusdb_strerror(r));
}
zoneinfo_dbopen = 0;
buf_free(&databuf);
}
}
void zoneinfo_done(void)
{
/* DB->done() handled by cyrus_done() */
}
static int parse_zoneinfo(const char *data, int datalen,
struct zoneinfo *zi, int all)
{
const char *dend = data + datalen;
unsigned version;
char *p;
memset(zi, 0, sizeof(struct zoneinfo));
/* version SP type SP dtstamp SP (string *(TAB string)) */
version = strtoul(data, &p, 10);
if (version != ZONEINFO_VERSION) return CYRUSDB_IOERROR;
if (p < dend) zi->type = strtoul(p, &p, 10);
if (p < dend) zi->dtstamp = strtotimet(p, &p, 10);
if (all && p < dend) {
size_t len = dend - ++p;
char *str = xstrndup(p, len);
tok_t tok;
tok_initm(&tok, str, "\t", TOK_FREEBUFFER);
while ((str = tok_next(&tok))) appendstrlist(&zi->data, str);
tok_fini(&tok);
}
return 0;
}
EXPORTED int zoneinfo_lookup(const char *tzid, struct zoneinfo *zi)
{
const char *data = NULL;
size_t datalen;
int r;
/* Don't access DB if it hasn't been opened */
if (!zoneinfo_dbopen) return CYRUSDB_INTERNAL;
assert(tzid);
/* Check if there is an entry in the database */
do {
r = cyrusdb_fetch(zoneinfodb, tzid, strlen(tzid), &data, &datalen, NULL);
} while (r == CYRUSDB_AGAIN);
if (r || !data || (datalen < 6)) return r ? r : CYRUSDB_IOERROR;
if (!zi) return 0;
return parse_zoneinfo(data, datalen, zi, 1);
}
EXPORTED int zoneinfo_store(const char *tzid, struct zoneinfo *zi, struct txn **tid)
{
struct strlist *sl;
const char *sep;
int r;
/* Don't access DB if it hasn't been opened */
if (!zoneinfo_dbopen) return CYRUSDB_INTERNAL;
assert(tzid && zi);
/* version SP type SP dtstamp SP (string *(TAB string)) */
buf_reset(&databuf);
buf_printf(&databuf, "%u %u " TIME_T_FMT " ", ZONEINFO_VERSION, zi->type, zi->dtstamp);
for (sl = zi->data, sep = ""; sl; sl = sl->next, sep = "\t")
buf_printf(&databuf, "%s%s", sep, sl->s);
r = cyrusdb_store(zoneinfodb, tzid, strlen(tzid),
buf_cstring(&databuf), buf_len(&databuf), tid);
if (r != CYRUSDB_OK) {
syslog(LOG_ERR, "DBERROR: error updating zoneinfo: %s (%s)",
tzid, cyrusdb_strerror(r));
}
return r;
}
static int tzmatch(const char *str, size_t slen, const char *pat)
{
for ( ; *pat; str++, slen--, pat++) {
/* End of string and more pattern */
if (!slen && *pat != '*') return 0;
switch (*pat) {
case '*':
/* Collapse consecutive stars */
while (*++pat == '*') continue;
/* Trailing star matches anything */
if (!*pat) return 1;
while (slen) if (tzmatch(str++, slen--, pat)) return 1;
return 0;
case ' ':
case '_':
/* Treat ' ' == '_' */
if (*str != ' ' && *str != '_') return 0;
break;
default:
/* Case-insensitive comparison */
if (tolower(*str) != tolower(*pat)) return 0;
break;
}
}
/* Did we reach end of string? */
return (!slen);
}
struct findrock {
const char *find;
int tzid_only;
time_t changedsince;
int (*proc)();
void *rock;
};
static int find_p(void *rock,
const char *tzid, size_t tzidlen,
const char *data, size_t datalen)
{
struct findrock *frock = (struct findrock *) rock;
struct zoneinfo zi;
if (parse_zoneinfo(data, datalen, &zi, 0)) return 0;
switch (zi.type) {
case ZI_INFO: return 0;
case ZI_LINK:
if (frock->tzid_only) return 0;
break;
case ZI_ZONE:
if (zi.dtstamp <= frock->changedsince) return 0;
break;
}
if (!frock->find) return 1;
if (frock->tzid_only) return (tzidlen == strlen(frock->find));
return tzmatch(tzid, tzidlen, frock->find);
}
static int find_cb(void *rock,
const char *tzid, size_t tzidlen,
const char *data, size_t datalen)
{
struct findrock *frock = (struct findrock *) rock;
struct zoneinfo zi;
int r;
r = parse_zoneinfo(data, datalen, &zi, 1);
if (!r) {
struct strlist *linkto = NULL;
if (zi.type == ZI_LINK) {
linkto = zi.data;
zi.data = NULL;
tzid = linkto->s;
tzidlen = strlen(tzid);
r = zoneinfo_lookup(tzid, &zi);
}
if (!r) r = (*frock->proc)(tzid, tzidlen, &zi, frock->rock);
freestrlist(zi.data);
freestrlist(linkto);
}
return r;
}
EXPORTED int zoneinfo_find(const char *find, int tzid_only, time_t changedsince,
int (*proc)(), void *rock)
{
struct findrock frock;
/* Don't access DB if it hasn't been opened */
if (!zoneinfo_dbopen) return CYRUSDB_INTERNAL;
assert(proc);
frock.find = find;
frock.tzid_only = tzid_only;
frock.changedsince = changedsince;
frock.proc = proc;
frock.rock = rock;
if (!find || !tzid_only) find = "";
/* process each matching entry in our database */
return cyrusdb_foreach(zoneinfodb, (char *) find, strlen(find),
&find_p, &find_cb, &frock, NULL);
}
|