Codebase list cyrus-imapd / debian/3.0.12-1 imap / sync_log.c
debian/3.0.12-1

Tree @debian/3.0.12-1 (Download .tar.gz)

sync_log.c @debian/3.0.12-1raw · history · blame

  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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/* sync_log.c -- Cyrus synchonization logging functions
 *
 * 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.
 *
 * Original version written by David Carter <dpc22@cam.ac.uk>
 * Rewritten and integrated into Cyrus by Ken Murchison <ken@oceana.com>
 */

/* YYY Need better quoting for obscure filenames: use literals? */

#include <config.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>

#include "assert.h"
#include "exitcodes.h"
#include "sync_log.h"
#include "global.h"
#include "cyr_lock.h"
#include "mailbox.h"
#include "retry.h"
#include "util.h"
#include "xmalloc.h"
#include "xstrlcpy.h"

/* generated headers are not necessarily in current directory */
#include "imap/imap_err.h"

static int sync_log_suppressed = 0;
static strarray_t *channels = NULL;
static strarray_t *unsuppressable = NULL;

EXPORTED void sync_log_init(void)
{
    const char *conf;
    int i;


    /* sync_log_init() may be called more than once */
    if (channels) strarray_free(channels);

    conf = config_getstring(IMAPOPT_SYNC_LOG_CHANNELS);
    if (!conf) conf = "\"\"";
    channels = strarray_split(conf, " ", 0);
    /*
     * The sysadmin can specify "" in the value of sync_log_channels to
     * mean the default channel name - this will be useful for sysadmins
     * who want to start using a sync log channel for squatter but who
     * have been using the default sync log channel for sync_client.
     */
    i = strarray_find(channels, "\"\"", 0);
    if (i >= 0)
        strarray_set(channels, i, NULL);

    strarray_free(unsuppressable);
    unsuppressable = NULL;
    conf = config_getstring(IMAPOPT_SYNC_LOG_UNSUPPRESSABLE_CHANNELS);
    if (conf)
        unsuppressable = strarray_split(conf, " ", 0);
}

EXPORTED void sync_log_suppress(void)
{
    sync_log_suppressed = 1;
}

EXPORTED void sync_log_done(void)
{
    strarray_free(channels);
    channels = NULL;

    strarray_free(unsuppressable);
    unsuppressable = NULL;
}

static char *sync_log_fname(const char *channel)
{
    static char buf[MAX_MAILBOX_PATH];

    if (channel)
        snprintf(buf, MAX_MAILBOX_PATH,
                 "%s/sync/%s/log", config_dir, channel);
    else
        snprintf(buf, MAX_MAILBOX_PATH,
                 "%s/sync/log", config_dir);

    return buf;
}

static int sync_log_enabled(const char *channel)
{
    if (!config_getswitch(IMAPOPT_SYNC_LOG))
        return 0;       /* entire mechanism is disabled */
    if (!sync_log_suppressed)
        return 1;       /* _suppress() wasn't called */
    if (unsuppressable && strarray_find(unsuppressable, channel, 0) >= 0)
        return 1;       /* channel is unsuppressable */
    return 0;           /* suppressed */
}

static void sync_log_base(const char *channel, const char *string)
{
    int fd;
    struct stat sbuffile, sbuffd;
    int retries = 0;
    const char *fname;

    fname = sync_log_fname(channel);

    while (retries++ < SYNC_LOG_RETRIES) {
        fd = open(fname, O_WRONLY|O_APPEND|O_CREAT, 0640);
        if (fd < 0 && errno == ENOENT) {
            if (!cyrus_mkdir(fname, 0755)) {
                fd = open(fname, O_WRONLY|O_APPEND|O_CREAT, 0640);
            }
        }
        if (fd < 0) {
            syslog(LOG_ERR, "sync_log(): Unable to write to log file %s: %s",
                   fname, strerror(errno));
            return;
        }

        if (lock_blocking(fd, fname) == -1) {
            syslog(LOG_ERR, "sync_log(): Failed to lock %s for %s: %m",
                   fname, string);
            xclose(fd);
            return;
        }

        /* Check that the file wasn't renamed after it was opened above */
        if ((fstat(fd, &sbuffd) == 0) &&
            (stat(fname, &sbuffile) == 0) &&
            (sbuffd.st_ino == sbuffile.st_ino))
            break;

        lock_unlock(fd, fname);
        xclose(fd);
    }
    if (retries >= SYNC_LOG_RETRIES) {
        xclose(fd);
        syslog(LOG_ERR,
               "sync_log(): Failed to lock %s for %s after %d attempts",
               fname, string, retries);
        return;
    }

    if (retry_write(fd, string, strlen(string)) < 0)
        syslog(LOG_ERR, "write() to %s failed: %s",
               fname, strerror(errno));

    (void)fsync(fd); /* paranoia */
    lock_unlock(fd, fname);
    xclose(fd);
}

static const char *sync_quote_name(const char *name)
{
    static char buf[MAX_MAILBOX_BUFFER+3]; /* "x2 plus \0 */
    char c;
    int src;
    int dst = 0;
    int need_quote = 0;

    /* initial quote */
    buf[dst++] = '"';

    /* degenerate case - no name is the empty string, quote it */
    if (!name || !*name) {
        need_quote = 1;
        goto end;
    }

    for (src = 0; name[src]; src++) {
        c = name[src];
        if ((c == '\r') || (c == '\n'))
            fatal("Illegal line break in folder name", EC_IOERR);

        /* quoteable characters */
        if ((c == '\\') || (c == '\"') || (c == '{') || (c == '}')) {
            need_quote = 1;
            buf[dst++] = '\\';
        }

        /* non-atom characters */
        else if ((c == ' ') || (c == '\t') || (c == '(') || (c == ')')) {
            need_quote = 1;
        }

        buf[dst++] = c;

        if (dst > MAX_MAILBOX_BUFFER)
            fatal("word too long", EC_IOERR);
    }

end:
    if (need_quote) {
        buf[dst++] = '\"';
        buf[dst] = '\0';
        return buf;
    }
    else {
        buf[dst] = '\0';
        return buf + 1; /* skip initial quote */
    }
}

#define BUFSIZE 4096

static char *va_format(const char *fmt, va_list ap)
{
    static char buf[BUFSIZE+1];
    size_t len;
    int ival;
    const char *sval;
    const char *p;

    for (len = 0, p = fmt; *p && len < BUFSIZE; p++) {
        if (*p != '%') {
            buf[len++] = *p;
            continue;
        }
        switch (*++p) {
        case 'd':
            ival = va_arg(ap, int);
            len += snprintf(buf+len, BUFSIZE-len, "%d", ival);
            break;
        case 's':
            sval = va_arg(ap, const char *);
            sval = sync_quote_name(sval);
            strlcpy(buf+len, sval, BUFSIZE-len);
            len += strlen(sval);
            break;
        default:
            buf[len++] = *p;
            break;
        }
    }

    if (buf[len-1] != '\n') buf[len++] = '\n';
    buf[len] = '\0';

    return buf;
}

EXPORTED void sync_log(const char *fmt, ...)
{
    va_list ap;
    const char *val;
    int i;

    if (!channels) return;

    va_start(ap, fmt);
    val = va_format(fmt, ap);
    va_end(ap);

    for (i = 0 ; i < channels->count ; i++) {
        const char *channel = channels->data[i];
        if (sync_log_enabled(channel))
            sync_log_base(channel, val);
    }
}

EXPORTED void sync_log_channel(const char *channel, const char *fmt, ...)
{
    va_list ap;
    const char *val;

    va_start(ap, fmt);
    val = va_format(fmt, ap);
    va_end(ap);

    sync_log_base(channel, val);
}

/*
 * Read-side sync log code
 */
struct sync_log_reader
{
    /*
     * This object works in three modes:
     *
     * - initialised with a sync log channel
     *      - standard mode used by sync_client
     *      - slr->log_file != NULL
     *      - slr->work_file is the name of a rename()d
     *        file that needs to be unlink()ed.
     *
     * - initialised with a saved file name
     *      - used by the sync_client -f option
     *      - slr->log_file = NULL
     *      - slr->work_file is the file given us by the user
     *        which it's important that we do not unlink()
     *
     * - initialised with a file descriptor
     *      - slr->log_file = NULL
     *      - slr->work_file = NULL
     *      - slr->fd is a file descriptor, probably stdin,
     *        and possibly a pipe
     *      - we cannot unlink() anything even if we wanted to.
     */
    char *log_file;
    char *work_file;
    int fd;
    int fd_is_ours;
    struct protstream *input;
    struct buf type;
    struct buf arg1;
    struct buf arg2;
};

static sync_log_reader_t *sync_log_reader_alloc(void)
{
    sync_log_reader_t *slr = xzmalloc(sizeof(sync_log_reader_t));
    slr->fd = -1;
    return slr;
}

/*
 * Create a sync log reader object which will read from the given sync log
 * channel 'channel'.  The channel may be NULL for the default channel.
 * Returns a new object which must be freed with sync_log_reader_free().
 * Does not return NULL.
 */
EXPORTED sync_log_reader_t *sync_log_reader_create_with_channel(const char *channel)
{
    sync_log_reader_t *slr = sync_log_reader_alloc();
    struct buf buf = BUF_INITIALIZER;

    slr->log_file = xstrdup(sync_log_fname(channel));

    /* Create a work log filename.  We will process this
     * first if it exists */
    buf_printf(&buf, "%s-run", slr->log_file);
    slr->work_file = buf_release(&buf);

    return slr;
}

/*
 * Create a sync log reader object which will read from the given file
 * 'filename'.  Returns a new object which must be freed with
 * sync_log_reader_free().  Does not return NULL.
 */
EXPORTED sync_log_reader_t *sync_log_reader_create_with_filename(const char *filename)
{
    sync_log_reader_t *slr = sync_log_reader_alloc();
    slr->work_file = xstrdup(filename);
    /* slr->log_file remain NULL, which matters later */
    return slr;
}

/*
 * Create a sync log reader object which will read from the given file
 * descriptor 'fd'.  The file descriptor must be open for reading and
 * is not closed.  Returns a new object which must be freed with
 * sync_log_reader_free().  Does not return NULL.
 */
EXPORTED sync_log_reader_t *sync_log_reader_create_with_fd(int fd)
{
    sync_log_reader_t *slr = sync_log_reader_alloc();
    slr->fd = fd;
    slr->fd_is_ours = 0;
    /* slr->log_file remain NULL, which matters later */
    return slr;
}

/*
 * Free a sync log reader object.
 */
EXPORTED void sync_log_reader_free(sync_log_reader_t *slr)
{
    if (!slr) return;
    if (slr->input) prot_free(slr->input);
    if (slr->fd_is_ours && slr->fd >= 0) close(slr->fd);
    free(slr->log_file);
    free(slr->work_file);
    buf_free(&slr->type);
    buf_free(&slr->arg1);
    buf_free(&slr->arg2);
    free(slr);
}

/*
 * Begin reading a sync log file.  If the reader is reading from a
 * channel, rename the current log file so it will not be appended to by
 * the write side code, and open the file. Otherwise, just open the file
 * (note this is still necessary even when the reader is reading from a
 * file descriptor).
 *
 * When sync_log_reader_begin() returns success, you should loop calling
 * sync_log_reader_getitem() and handling the items, until it returns
 * EOF, and then call sync_log_reader_end().
 *
 * Returns zero on success, IMAP_AGAIN if reading from a channel and
 * there is no current log file, or an IMAP error code on failure.
 */
EXPORTED int sync_log_reader_begin(sync_log_reader_t *slr)
{
    struct stat sbuf;
    int r;

    if (slr->input) {
        r = sync_log_reader_end(slr);
        if (r) return r;
    }

    if (stat(slr->work_file, &sbuf) == 0) {
        /* Existing work log file - process this first */
        syslog(LOG_NOTICE,
               "Reprocessing sync log file %s", slr->work_file);
    }
    else if (!slr->log_file) {
        syslog(LOG_ERR, "Failed to stat %s: %m",
               slr->log_file);
        return IMAP_IOERROR;
    }
    else {
        /* Check for sync_log file */
        if (stat(slr->log_file, &sbuf) < 0) {
            if (errno == ENOENT)
                return IMAP_AGAIN;  /* no problem, try again later */
            syslog(LOG_ERR, "Failed to stat %s: %m",
                   slr->log_file);
            return IMAP_IOERROR;
        }

        /* Move sync_log to our work file */
        if (rename(slr->log_file, slr->work_file) < 0) {
            syslog(LOG_ERR, "Rename %s -> %s failed: %m",
                   slr->log_file, slr->work_file);
            return IMAP_IOERROR;
        }
    }

    if (slr->fd < 0) {
        int fd = open(slr->work_file, O_RDWR, 0);
        if (fd < 0) {
            syslog(LOG_ERR, "Failed to open %s: %m", slr->work_file);
            return IMAP_IOERROR;
        }

        if (lock_blocking(fd, slr->work_file) < 0) {
            syslog(LOG_ERR, "Failed to lock %s: %m", slr->work_file);
            close(fd);
            return IMAP_IOERROR;
        }

        slr->fd = fd;
        slr->fd_is_ours = 1;

        /* we can unlock immediately, since we have serialised
         * any process which held the lock over the rename.  All
         * future attempts to lock this inode will stat and notice
         * the rename, so they won't write any more */
        lock_unlock(slr->fd, slr->work_file);
    }

    slr->input = prot_new(slr->fd, /*write*/0);

    return 0;
}

EXPORTED const char *sync_log_reader_get_file_name(const sync_log_reader_t *slr)
{
    return slr->work_file;
}

/*
 * Finish reading a sync log file.  Closes the file (and, if the reader
 * is reading from a channel, unlinks the work file and prepares for the
 * next file).  Returns 0 on success or an IMAP error code on failure.
 */
EXPORTED int sync_log_reader_end(sync_log_reader_t *slr)
{
    if (!slr->input)
        return 0;

    if (slr->input) {
        prot_free(slr->input);
        slr->input = NULL;
    }

    if (slr->fd_is_ours && slr->fd >= 0) {
        lock_unlock(slr->fd, slr->work_file);
        close(slr->fd);
        slr->fd = -1;
    }

    if (slr->log_file) {
        /* We were initialised with a sync log channel, whose
         * log file we rename()d to the work file.  Now that
         * we've done with the work file we can unlink it.
         * Further checks at this point are just paranoia. */
        if (slr->work_file && unlink(slr->work_file) < 0) {
            syslog(LOG_ERR, "Unlink %s failed: %m", slr->work_file);
            return IMAP_IOERROR;
        }
    }

    return 0;
}

/*
 * Read a single log item from a sync log file.  The item will be
 * returned as three constant strings.  The first string is the type of
 * the item (e.g. "MAILBOX") and is always capitalised.  The second and
 * third strings are arguments.
 *
 * Returns 0 on success, EOF when the end of the file is reached, or an
 * IMAP error code on failure.
 */
EXPORTED int sync_log_reader_getitem(sync_log_reader_t *slr,
                                     const char *args[3])
{
    int c;
    const char *arg1s = NULL;
    const char *arg2s = NULL;

    if (!slr->input)
        return EOF;

    for (;;) {
        if ((c = getword(slr->input, &slr->type)) == EOF)
            return EOF;

        /* Ignore blank lines */
        if (c == '\r') c = prot_getc(slr->input);
        if (c == '\n')
            continue;

        if (c != ' ') {
            syslog(LOG_ERR, "Invalid input");
            eatline(slr->input, c);
            continue;
        }

        if ((c = getastring(slr->input, 0, &slr->arg1)) == EOF) return EOF;
        arg1s = slr->arg1.s;

        arg2s = NULL;
        if (c == ' ') {
            if ((c = getastring(slr->input, 0, &slr->arg2)) == EOF) return EOF;
            arg2s = slr->arg2.s;
        }

        if (c == '\r') c = prot_getc(slr->input);
        if (c != '\n') {
            syslog(LOG_ERR, "Garbage at end of input line");
            eatline(slr->input, c);
            continue;
        }

        break;
    }

    ucase(slr->type.s);
    args[0] = slr->type.s;
    args[1] = arg1s;
    args[2] = arg2s;
    return 0;
}