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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
|
/* mboxkey.c -- implementation of URLAUTH mailbox keys
*
* Copyright (c) 1994-2008 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>
#include <stdlib.h>
#include <syslog.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include "assert.h"
#include "cyrusdb.h"
#include "map.h"
#include "util.h"
#include "global.h"
#include "xmalloc.h"
#include "mailbox.h"
#include "mboxkey.h"
/* generated headers are not necessarily in current directory */
#include "imap/imap_err.h"
enum {
MBOXKEY_VERSION = 1,
MBOXKEY_DEBUG = 0
};
struct mboxkey {
char *user; /* what user is this for? */
char *fname; /* filename (full path) of db */
struct db *db;
struct txn *tid; /* outstanding txn, if any */
};
static struct mboxkey *lastmboxkey = NULL;
#define DB (config_mboxkey_db)
static void abortcurrent(struct mboxkey *s)
{
if (s && s->tid) {
int r = cyrusdb_abort(s->db, s->tid);
if (r) {
syslog(LOG_ERR, "DBERROR: error aborting txn: %s",
cyrusdb_strerror(r));
}
s->tid = NULL;
}
}
EXPORTED char *mboxkey_getpath(const char *userid)
{
mbname_t *mbname = mbname_from_userid(userid);
char *fname = mboxname_conf_getpath(mbname, FNAME_MBOXKEYSUFFIX);
mbname_free(&mbname);
return fname;
}
EXPORTED int mboxkey_open(const char *user,
int flags,
struct mboxkey **mboxkeydbptr)
{
struct mboxkey *mboxkeydb;
struct stat sbuf;
char *fname = NULL;
int r;
/* try to reuse the last db handle */
mboxkeydb = lastmboxkey;
lastmboxkey = NULL;
if (MBOXKEY_DEBUG) {
syslog(LOG_DEBUG, "mboxkey_db: mboxkey_open(%s)", user);
}
/* if this is the db we've already opened, return it */
if (mboxkeydb && !strcmp(mboxkeydb->user, user) &&
!stat(mboxkeydb->fname, &sbuf)) {
abortcurrent(mboxkeydb);
*mboxkeydbptr = mboxkeydb;
return 0;
}
*mboxkeydbptr = NULL;
/* otherwise, close the existing database */
if (mboxkeydb) {
abortcurrent(mboxkeydb);
r = cyrusdb_close(mboxkeydb->db);
if (r) {
syslog(LOG_ERR, "DBERROR: error closing mboxkeydb: %s",
cyrusdb_strerror(r));
}
free(mboxkeydb->user);
free(mboxkeydb->fname);
} else {
/* create mboxkeydb */
mboxkeydb = (struct mboxkey *) xmalloc(sizeof(struct mboxkey));
}
/* open the mboxkeydb corresponding to user */
fname = mboxkey_getpath(user);
r = cyrusdb_open(DB, fname, (flags & MBOXKEY_CREATE) ? CYRUSDB_CREATE : 0,
&mboxkeydb->db);
if (r != 0) {
int level = (flags & MBOXKEY_CREATE) ? LOG_ERR : LOG_DEBUG;
syslog(level, "DBERROR: opening %s: %s", fname,
cyrusdb_strerror(r));
r = IMAP_IOERROR;
free(mboxkeydb);
free(fname);
return r;
}
syslog(LOG_DEBUG, "mboxkey_db: user %s opened %s", user, fname);
mboxkeydb->tid = NULL;
mboxkeydb->user = xstrdup(user);
mboxkeydb->fname = fname;
*mboxkeydbptr = mboxkeydb;
return r;
}
static int mboxkey_readit(struct mboxkey *mboxkeydb, const char *mailbox,
const char **mboxkey, size_t *mboxkeylen,
int rw)
{
int r;
const char *data;
size_t datalen;
unsigned short version, s;
assert(mboxkeydb && mailbox);
if (rw || mboxkeydb->tid) {
r = cyrusdb_fetchlock(mboxkeydb->db, mailbox, strlen(mailbox),
&data, &datalen, &mboxkeydb->tid);
} else {
r = cyrusdb_fetch(mboxkeydb->db, mailbox, strlen(mailbox),
&data, &datalen, NULL);
}
switch (r) {
case 0:
break;
case CYRUSDB_AGAIN:
syslog(LOG_DEBUG, "deadlock in mboxkey database for '%s/%s'",
mboxkeydb->user, mailbox);
return IMAP_AGAIN;
break;
case CYRUSDB_IOERROR:
syslog(LOG_ERR, "DBERROR: error fetching txn %s",
cyrusdb_strerror(r));
return IMAP_IOERROR;
break;
case CYRUSDB_NOTFOUND:
*mboxkey = NULL;
*mboxkeylen = 0;
return 0;
break;
}
/* 'data' is <version><mboxkey> */
memcpy(&s, data, sizeof(s));
version = ntohs(s);
assert(version == MBOXKEY_VERSION);
*mboxkey = data + sizeof(s);
*mboxkeylen = datalen - sizeof(s);
return 0;
}
EXPORTED int mboxkey_read(struct mboxkey *mboxkeydb, const char *mailbox,
const char **mboxkey, size_t *mboxkeylen)
{
if (MBOXKEY_DEBUG) {
syslog(LOG_DEBUG, "mboxkey_db: mboxkey_read(%s, %s)",
mboxkeydb->user, mailbox);
}
return mboxkey_readit(mboxkeydb, mailbox, mboxkey, mboxkeylen, 0);
}
int mboxkey_lockread(struct mboxkey *mboxkeydb, const char *mailbox,
const char **mboxkey, size_t *mboxkeylen)
{
if (MBOXKEY_DEBUG) {
syslog(LOG_DEBUG, "mboxkey_db: mboxkey_lockread(%s, %s)",
mboxkeydb->user, mailbox);
}
return mboxkey_readit(mboxkeydb, mailbox, mboxkey, mboxkeylen, 1);
}
EXPORTED int mboxkey_write(struct mboxkey *mboxkeydb, const char *mailbox,
const char *mboxkey, size_t mboxkeylen)
{
int r;
assert(mboxkeydb && mailbox);
/* assert(mboxkeydb->tid);*/
if (MBOXKEY_DEBUG) {
syslog(LOG_DEBUG, "mboxkey_db: mboxkey_write(%s, %s, %s)",
mboxkeydb->user, mailbox, mboxkey ? "KEY" : "NIL");
}
if (!mboxkey) {
r = cyrusdb_delete(mboxkeydb->db, mailbox, strlen(mailbox),
&mboxkeydb->tid, 1);
}
else {
unsigned short version = MBOXKEY_VERSION, s;
int datalen = sizeof(s) + mboxkeylen;
char *data = xmalloc(datalen);
s = htons(version);
memcpy(data, &s, sizeof(s));
memcpy(data+sizeof(s), mboxkey, mboxkeylen);
r = cyrusdb_store(mboxkeydb->db, mailbox, strlen(mailbox),
data, datalen, &mboxkeydb->tid);
free(data);
}
switch (r) {
case CYRUSDB_OK:
break;
case CYRUSDB_IOERROR:
r = IMAP_AGAIN;
break;
default:
syslog(LOG_ERR, "DBERROR: error updating database: %s",
cyrusdb_strerror(r));
r = IMAP_IOERROR;
break;
}
return r;
}
EXPORTED int mboxkey_close(struct mboxkey *mboxkeydb)
{
int r;
if (MBOXKEY_DEBUG) {
syslog(LOG_DEBUG, "mboxkey_db: mboxkey_close(%s)",
mboxkeydb->user);
}
if (mboxkeydb->tid) {
r = cyrusdb_commit(mboxkeydb->db, mboxkeydb->tid);
if (r != CYRUSDB_OK) {
syslog(LOG_ERR, "DBERROR: error committing mboxkey txn; "
"mboxkey state lost: %s", cyrusdb_strerror(r));
}
mboxkeydb->tid = NULL;
}
if (lastmboxkey) {
int r;
/* free the old database hanging around */
abortcurrent(lastmboxkey);
r = cyrusdb_close(lastmboxkey->db);
if (r != CYRUSDB_OK) {
syslog(LOG_ERR, "DBERROR: error closing lastmboxkey: %s",
cyrusdb_strerror(r));
r = IMAP_IOERROR;
}
if(!r) lastmboxkey->db = NULL;
free(lastmboxkey->user);
free(lastmboxkey->fname);
free(lastmboxkey);
lastmboxkey = NULL;
}
/* this database can now be reused */
lastmboxkey = mboxkeydb;
return 0;
}
EXPORTED int mboxkey_delete_user(const char *user)
{
char *fname = mboxkey_getpath(user);
int r = 0;
if (MBOXKEY_DEBUG) {
syslog(LOG_DEBUG, "mboxkey_db: mboxkey_delete_user(%s)",
user);
}
/* erp! */
r = unlink(fname);
if (r < 0 && errno == ENOENT) {
syslog(LOG_DEBUG, "cannot unlink %s: %m", fname);
/* but maybe the user just never read anything? */
r = 0;
}
else if (r < 0) {
syslog(LOG_ERR, "error unlinking %s: %m", fname);
r = IMAP_IOERROR;
}
free(fname);
if (lastmboxkey) {
free(lastmboxkey->user);
free(lastmboxkey->fname);
free(lastmboxkey);
lastmboxkey = NULL;
}
return r;
}
/* database better have been locked before this ! */
int mboxkey_unlock(struct mboxkey *mboxkeydb)
{
int r;
assert(mboxkeydb);
if (!mboxkeydb->tid) return 0;
if (MBOXKEY_DEBUG) {
syslog(LOG_DEBUG, "mboxkey_db: mboxkey_unlock(%s)",
mboxkeydb->user);
}
r = cyrusdb_commit(mboxkeydb->db, mboxkeydb->tid);
if (r != CYRUSDB_OK) {
syslog(LOG_ERR, "DBERROR: error committing mboxkey txn; "
"mboxkey state lost: %s", cyrusdb_strerror(r));
}
mboxkeydb->tid = NULL;
return 0;
}
EXPORTED int mboxkey_done(void)
{
int r = 0;
if (MBOXKEY_DEBUG) {
syslog(LOG_DEBUG, "mboxkey_db: mboxkey_done()");
}
if (lastmboxkey) {
abortcurrent(lastmboxkey);
r = cyrusdb_close(lastmboxkey->db);
if (r) {
syslog(LOG_ERR, "DBERROR: error closing lastmboxkey: %s",
cyrusdb_strerror(r));
r = IMAP_IOERROR;
}
free(lastmboxkey->user);
free(lastmboxkey->fname);
free(lastmboxkey);
}
return r;
}
struct mboxkey_merge_rock
{
struct db *db;
struct txn *tid;
};
/* Copy keys from tmp file to tgt file.
*
* XXX We currently have nothing to compare against.
*/
static int mboxkey_merge_cb(void *rockp,
const char *key, size_t keylen,
const char *tmpdata, size_t tmpdatalen)
{
int r;
struct mboxkey_merge_rock *rockdata = (struct mboxkey_merge_rock *)rockp;
struct db *tgtdb = rockdata->db;
const char *tgtdata;
size_t tgtdatalen;
if (!tgtdb) return IMAP_INTERNAL;
r = cyrusdb_fetchlock(tgtdb, key, keylen, &tgtdata, &tgtdatalen,
&(rockdata->tid));
if(!r && tgtdata) {
unsigned short version, s;
const char *tmp = tmpdata, *tgt = tgtdata;
/* get version */
memcpy(&s, tgt, sizeof(s));
version = ntohs(s);
assert(version == MBOXKEY_VERSION);
/* get version */
memcpy(&s, tmp, sizeof(s));
version = ntohs(s);
assert(version == MBOXKEY_VERSION);
}
return cyrusdb_store(tgtdb, key, keylen, tmpdata, tmpdatalen,
&(rockdata->tid));
}
HIDDEN int mboxkey_merge(const char *tmpfile, const char *tgtfile)
{
int r = 0;
struct db *tmp = NULL, *tgt = NULL;
struct mboxkey_merge_rock rock;
/* xxx does this need to be CYRUSDB_CREATE? */
r = cyrusdb_open(DB, tmpfile, CYRUSDB_CREATE, &tmp);
if(r) goto done;
r = cyrusdb_open(DB, tgtfile, CYRUSDB_CREATE, &tgt);
if(r) goto done;
rock.db = tgt;
rock.tid = NULL;
r = cyrusdb_foreach(tmp, "", 0, NULL, mboxkey_merge_cb, &rock, &rock.tid);
if(r) cyrusdb_abort(rock.db, rock.tid);
else cyrusdb_commit(rock.db, rock.tid);
done:
if(tgt) cyrusdb_close(tgt);
if(tmp) cyrusdb_close(tmp);
return r;
}
|