Codebase list cyrus-imapd / debian/3.2.3-2 imap / mupdate-client.c
debian/3.2.3-2

Tree @debian/3.2.3-2 (Download .tar.gz)

mupdate-client.c @debian/3.2.3-2raw · 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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
/* mupdate-client.c -- cyrus murder database clients
 *
 * 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 <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <sasl/sasl.h>
#include <sasl/saslutil.h>
#include <sysexits.h>
#include <syslog.h>
#include <stdarg.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

#include "global.h"
#include "mupdate.h"
#include "prot.h"
#include "protocol.h"
#include "util.h"
#include "xmalloc.h"
#include "xstrlcpy.h"
#include "xstrlcat.h"

const char service_name[] = "mupdate";

static struct protocol_t mupdate_protocol =
{ "mupdate", "mupdate", TYPE_STD,
  { { { 1, "* OK" },
      { NULL, NULL, "* OK", NULL,
        CAPAF_ONE_PER_LINE|CAPAF_SKIP_FIRST_WORD,
        { { "AUTH", CAPA_AUTH },
          { "STARTTLS", CAPA_STARTTLS },
          { "COMPRESS=DEFLATE", CAPA_COMPRESS },
          { NULL, 0 } } },
      { "S01 STARTTLS", "S01 OK", "S01 NO", 1 },
      { "A01 AUTHENTICATE", USHRT_MAX, 1, "A01 OK", "A01 NO", "", "*", NULL, 0 },
      { "Z01 COMPRESS \"DEFLATE\"", NULL, "Z01 OK" },
      { "N01 NOOP", NULL, "N01 OK" },
      { "Q01 LOGOUT", NULL, "Q01 " } } }
};

EXPORTED int mupdate_connect(const char *server,
                    const char *port __attribute__((unused)),
                    mupdate_handle **handle,
                    sasl_callback_t *cbs)
{
    mupdate_handle *h = NULL;
    int local_cbs = 0;
    const char *status = NULL;

    if (!handle) {
        syslog(LOG_ERR, "%s: no mupdate_handle", __func__);
        return MUPDATE_BADPARAM;
    }

    /* open connection to 'server' */
    if (!server) {
        server = config_mupdate_server;
        if (server == NULL) {
            fatal("couldn't get mupdate server name", EX_UNAVAILABLE);
        }
    }

    h = xzmalloc(sizeof(mupdate_handle));
    *handle = h;

    if (!cbs) {
        local_cbs = 1;
        cbs = mysasl_callbacks(config_getstring(IMAPOPT_MUPDATE_USERNAME),
                               config_getstring(IMAPOPT_MUPDATE_AUTHNAME),
                               config_getstring(IMAPOPT_MUPDATE_REALM),
                               config_getstring(IMAPOPT_MUPDATE_PASSWORD));
    }

    h->conn = backend_connect(NULL, server, &mupdate_protocol,
                              "", cbs, &status, -1);

    /* xxx unclear that this is correct, but it prevents a memory leak */
    if (local_cbs) free_callbacks(cbs);

    if (!h->conn) {
        syslog(LOG_ERR, "mupdate_connect failed: %s", status ? status : "no auth status");
        return MUPDATE_NOCONN;
    }

    h->saslcompleted = 1;

    /* SUCCESS */
    return 0;
}

EXPORTED void mupdate_disconnect(mupdate_handle **hp)
{
    mupdate_handle *h;

    if (!hp || !(*hp)) return;
    h = *hp;

    backend_disconnect(h->conn);
    free(h->conn);

    buf_free(&(h->tag));
    buf_free(&(h->cmd));
    buf_free(&(h->arg1));
    buf_free(&(h->arg2));
    buf_free(&(h->arg3));

    free(h->acl);

    free(h);
    *hp = NULL;
}

/* We're really only looking for an OK or NO or BAD here -- and the callback
 * is never called in those cases.  So if the callback is called, we have
 * an error! */
static int mupdate_scarf_one(struct mupdate_mailboxdata *mdata __attribute__((unused)),
                             const char *cmd,
                             void *context __attribute__((unused)))
{
    syslog(LOG_ERR, "mupdate_scarf_one was called, but shouldn't be.  Command received was %s", cmd);
    return -1;
}

EXPORTED int mupdate_activate(mupdate_handle *handle,
                     const char *mailbox, const char *location,
                     const char *acl)
{
    int ret;
    enum mupdate_cmd_response response;
    const char *p;

    if (!handle) {
        syslog(LOG_ERR, "%s: no mupdate_handle", __func__);
        return MUPDATE_BADPARAM;
    }

    if (!mailbox) {
        syslog(LOG_ERR, "%s: no mailbox", __func__);
        return MUPDATE_BADPARAM;
    }

    if (!location) {
        syslog(LOG_ERR, "%s: no location", __func__);
        return MUPDATE_BADPARAM;
    }

    if (!handle->saslcompleted) return MUPDATE_NOAUTH;

    /* make sure we don't have a double server!partition */
    if ((p = strchr(location, '!')) && strchr(p+1, '!')) {
        syslog(
                LOG_ERR,
                "%s: double ! detected in location '%s'",
                __func__,
                location
            );

        return MUPDATE_BADPARAM;
    }

    if (config_mupdate_config == IMAP_ENUM_MUPDATE_CONFIG_REPLICATED) {
        /* we don't care about the server part, everything is local */
        if (p) location = p + 1;
    }

    prot_printf(handle->conn->out,
                "X%u ACTIVATE "
                "{" SIZE_T_FMT "+}\r\n%s "
                "{" SIZE_T_FMT "+}\r\n%s "
                "{" SIZE_T_FMT "+}\r\n%s\r\n",
                handle->tagn++,
                strlen(mailbox), mailbox,
                strlen(location), location,
                (acl ? strlen(acl): 0), (acl ? acl : "")
        );

    ret = mupdate_scarf(handle, mupdate_scarf_one, NULL, 1, &response);
    if (ret) {
        return ret;
    } else if (response != MUPDATE_OK) {
        return MUPDATE_FAIL;
    } else {
        return 0;
    }
}

HIDDEN int mupdate_reserve(mupdate_handle *handle,
                    const char *mailbox, const char *location)
{
    int ret;
    enum mupdate_cmd_response response;
    const char *p;

    if (!handle) {
        syslog(LOG_ERR, "%s: no mupdate_handle", __func__);
        return MUPDATE_BADPARAM;
    }

    if (!mailbox) {
        syslog(LOG_ERR, "%s: no mailbox", __func__);
        return MUPDATE_BADPARAM;
    }

    if (!location) {
        syslog(LOG_ERR, "%s: no location", __func__);
        return MUPDATE_BADPARAM;
    }

    if (!handle->saslcompleted) return MUPDATE_NOAUTH;

    /* make sure we don't have a double server!partition */
    if ((p = strchr(location, '!')) && strchr(p+1, '!')) {
        syslog(
                LOG_ERR,
                "%s: double ! detected in location '%s'",
                __func__,
                location
            );

        return MUPDATE_BADPARAM;
    }

    if (config_mupdate_config == IMAP_ENUM_MUPDATE_CONFIG_REPLICATED) {
        /* we don't care about the location part, everything is local */
        if (p) location = p + 1;
    }

    prot_printf(handle->conn->out,
                "X%u RESERVE "
                "{" SIZE_T_FMT "+}\r\n%s "
                "{" SIZE_T_FMT "+}\r\n%s\r\n",
                handle->tagn++,
                strlen(mailbox), mailbox,
                strlen(location), location
        );

    ret = mupdate_scarf(handle, mupdate_scarf_one, NULL, 1, &response);
    if (ret) {
        return ret;
    } else if (response != MUPDATE_OK) {
        return MUPDATE_FAIL_RESERVE;
    } else {
        return 0;
    }
}

EXPORTED int mupdate_deactivate(mupdate_handle *handle,
                       const char *mailbox, const char *location)
{
    int ret;
    enum mupdate_cmd_response response;
    const char *p;

    if (!handle) {
        syslog(LOG_ERR, "%s: no mupdate_handle", __func__);
        return MUPDATE_BADPARAM;
    }

    if (!mailbox) {
        syslog(LOG_ERR, "%s: no mailbox", __func__);
        return MUPDATE_BADPARAM;
    }

    if (!location) {
        syslog(LOG_ERR, "%s: no location", __func__);
        return MUPDATE_BADPARAM;
    }

    if (!handle->saslcompleted) return MUPDATE_NOAUTH;

    /* make sure we don't have a double server!partition */
    if ((p = strchr(location, '!')) && strchr(p+1, '!')) {
        syslog(
                LOG_ERR,
                "%s: double ! detected in location '%s'",
                __func__,
                location
            );

        return MUPDATE_BADPARAM;
    }

    if (config_mupdate_config == IMAP_ENUM_MUPDATE_CONFIG_REPLICATED) {
        /* we don't care about the server part, everything is local */
        if (p) location = p + 1;
    }

    prot_printf(handle->conn->out,
            "X%u DEACTIVATE "
            "{" SIZE_T_FMT "+}\r\n%s "
            "{" SIZE_T_FMT "+}\r\n%s\r\n",
            handle->tagn++,
            strlen(mailbox), mailbox,
            strlen(location), location
        );

    ret = mupdate_scarf(handle, mupdate_scarf_one, NULL, 1, &response);
    if (ret) {
        return ret;
    } else if (response != MUPDATE_OK) {
        return MUPDATE_FAIL_RESERVE;
    } else {
        return 0;
    }
}

EXPORTED int mupdate_delete(mupdate_handle *handle,
                   const char *mailbox)
{
    int ret;
    enum mupdate_cmd_response response;

    if (!handle) {
        syslog(LOG_ERR, "%s: no mupdate_handle", __func__);
        return MUPDATE_BADPARAM;
    }

    if (!mailbox) {
        syslog(LOG_ERR, "%s: no mailbox", __func__);
        return MUPDATE_BADPARAM;
    }

    if (!handle->saslcompleted) return MUPDATE_NOAUTH;

    prot_printf(handle->conn->out,
                "X%u DELETE {" SIZE_T_FMT "+}\r\n%s\r\n", handle->tagn++,
                strlen(mailbox), mailbox);

    ret = mupdate_scarf(handle, mupdate_scarf_one, NULL, 1, &response);
    if (ret) {
        return ret;
    } else if (response != MUPDATE_OK) {
        return MUPDATE_FAIL;
    } else {
        return 0;
    }
}


static int mupdate_find_cb(struct mupdate_mailboxdata *mdata,
                           const char *cmd, void *context)
{
    struct mupdate_handle_s *h = (struct mupdate_handle_s *)context;

    if (!h || !cmd || !mdata) return 1;

    /* coyp the data to the handle storage */
    /* xxx why can't we just point to the 'mdata' buffers? */
    strlcpy(h->mailbox_buf, mdata->mailbox, sizeof(h->mailbox_buf));
    strlcpy(h->location_buf, mdata->location, sizeof(h->location_buf));

    if (!strncmp(cmd, "MAILBOX", 7)) {
        h->mailboxdata_buf.t = ACTIVE;

        free(h->acl);
        h->acl = xstrdup(mdata->acl);
    } else if (!strncmp(cmd, "RESERVE", 7)) {
        h->mailboxdata_buf.t = RESERVE;
        free(h->acl);
        h->acl = xstrdup("");
    } else {
        /* Bad command */
        return 1;
    }

    h->mailboxdata_buf.mailbox = h->mailbox_buf;
    h->mailboxdata_buf.location = h->location_buf;
    h->mailboxdata_buf.acl = h->acl;

    return 0;
}

EXPORTED int mupdate_find(mupdate_handle *handle, const char *mailbox,
                 struct mupdate_mailboxdata **target)
{
    int ret;
    enum mupdate_cmd_response response;

    if (!handle) {
        syslog(LOG_ERR, "%s: no mupdate_handle", __func__);
        return MUPDATE_BADPARAM;
    }

    if (!mailbox) {
        syslog(LOG_ERR, "%s: no mailbox", __func__);
        return MUPDATE_BADPARAM;
    }

    if (!target) {
        syslog(LOG_ERR, "%s: no target", __func__);
        return MUPDATE_BADPARAM;
    }

    prot_printf(handle->conn->out,
                "X%u FIND {" SIZE_T_FMT "+}\r\n%s\r\n", handle->tagn++,
                strlen(mailbox), mailbox);

    memset(&(handle->mailboxdata_buf), 0, sizeof(handle->mailboxdata_buf));

    ret = mupdate_scarf(handle, mupdate_find_cb, handle, 1, &response);

    /* note that the response is still OK even if there was no data returned,
     * so we have to make sure we actually filled in the data too */
    if (!ret && response == MUPDATE_OK && handle->mailboxdata_buf.mailbox) {
        *target = &(handle->mailboxdata_buf);
        return 0;
    } else if (!ret && response == MUPDATE_OK) {
        /* it looked okay, but we didn't get a mailbox */
        *target = NULL;
        return MUPDATE_MAILBOX_UNKNOWN;
    } else {
        /* Something Bad happened */
        *target = NULL;
        return ret ? ret : MUPDATE_FAIL;
    }
}

EXPORTED int mupdate_list(mupdate_handle *handle, mupdate_callback callback,
                 const char *prefix, void *context)
{
    int ret;
    enum mupdate_cmd_response response;

    if (!handle) {
        syslog(LOG_ERR, "%s: no mupdate_handle", __func__);
        return MUPDATE_BADPARAM;
    }

    if (!callback) {
        syslog(LOG_ERR, "%s: no callback", __func__);
        return MUPDATE_BADPARAM;
    }

    if (prefix) {
        prot_printf(handle->conn->out,
                    "X%u LIST {" SIZE_T_FMT "+}\r\n%s\r\n", handle->tagn++,
                    strlen(prefix), prefix);
    } else {
        prot_printf(handle->conn->out,
                    "X%u LIST\r\n", handle->tagn++);
    }

    ret = mupdate_scarf(handle, callback, context, 1, &response);

    if (ret) {
        return ret;
    } else if (response != MUPDATE_OK) {
        return MUPDATE_FAIL;
    } else {
        return 0;
    }
}


EXPORTED int mupdate_noop(mupdate_handle *handle, mupdate_callback callback,
                 void *context)
{
    int ret;
    enum mupdate_cmd_response response;

    if (!handle) {
        syslog(LOG_ERR, "%s: no mupdate_handle", __func__);
        return MUPDATE_BADPARAM;
    }

    if (!callback) {
        syslog(LOG_ERR, "%s: no callback", __func__);
        return MUPDATE_BADPARAM;
    }

    prot_printf(handle->conn->out,
                "X%u NOOP\r\n", handle->tagn++);

    ret = mupdate_scarf(handle, callback, context, 1, &response);

    if (!ret && response == MUPDATE_OK) {
        return 0;
    } else {
        return ret ? ret : MUPDATE_FAIL;
    }
}

#define CHECKNEWLINE(c, ch) do { if ((ch) == '\r') (ch)=prot_getc((c)->conn->in); \
                                 if ((ch) != '\n') { syslog(LOG_ERR, \
                             "extra arguments received, aborting connection");\
                                 r = MUPDATE_PROTOCOL_ERROR;\
                                 goto done; }} while(0)

/* Scarf up the incoming data and perform the requested operations */
EXPORTED int mupdate_scarf(mupdate_handle *handle,
                  mupdate_callback callback,
                  void *context,
                  int wait_for_ok,
                  enum mupdate_cmd_response *response)
{
    struct mupdate_mailboxdata box;
    int r = 0;

    if (!handle) {
        syslog(LOG_ERR, "%s: no mupdate_handle", __func__);
        return MUPDATE_BADPARAM;
    }

    if (!callback) {
        syslog(LOG_ERR, "%s: no callback", __func__);
        return MUPDATE_BADPARAM;
    }

    /* keep going while we have input or if we're waiting for an OK */
    while (!r) {
        int ch;
        char *p;

        if (wait_for_ok) {
            prot_BLOCK(handle->conn->in);
        } else {
            /* check for input */
            prot_NONBLOCK(handle->conn->in);
            ch = prot_getc(handle->conn->in);

            if (ch == EOF && errno == EAGAIN) {
                /* this was just "no input" we return 0 */
                goto done;
            } else if (ch == EOF) {
                /* this was a fatal error */
                r = MUPDATE_NOCONN;
                goto done;
            } else {
                /* there's input waiting, put back our character */
                prot_ungetc(ch, handle->conn->in);
            }

            /* Set it back to blocking so we don't get half a word */
            prot_BLOCK(handle->conn->in);
        }

        ch = getword(handle->conn->in, &(handle->tag));
        if (ch == EOF) {
            /* this was a fatal error */
            r = MUPDATE_NOCONN;
            goto done;
        }

        if (ch != ' ') {
            /* We always have a command */
            syslog(LOG_ERR, "Protocol error from master: no tag");
            r = MUPDATE_PROTOCOL_ERROR;
            goto done;
        }
        ch = getword(handle->conn->in, &(handle->cmd));
        if (ch != ' ') {
            /* We always have an argument */
            syslog(LOG_ERR, "Protocol error from master: no keyword");
            r = MUPDATE_PROTOCOL_ERROR;
            break;
        }

        if (Uislower(handle->cmd.s[0])) {
            handle->cmd.s[0] = toupper((unsigned char) handle->cmd.s[0]);
        }
        for (p = &(handle->cmd.s[1]); *p; p++) {
            if (Uislower(*p))
                *p = toupper((unsigned char) *p);
        }

        switch(handle->cmd.s[0]) {
        case 'B':
            if (!strncmp(handle->cmd.s, "BAD", 3)) {
                ch = getstring(handle->conn->in, handle->conn->out, &(handle->arg1));
                CHECKNEWLINE(handle, ch);

                syslog(LOG_ERR, "mupdate BAD response: %s", handle->arg1.s);
                if (wait_for_ok && response) {
                    *response = MUPDATE_BAD;
                }
                goto done;
            } else if (!strncmp(handle->cmd.s, "BYE", 3)) {
                ch = getstring(handle->conn->in, handle->conn->out, &(handle->arg1));
                CHECKNEWLINE(handle, ch);

                syslog(LOG_ERR, "mupdate BYE response: %s", handle->arg1.s);
                if (wait_for_ok && response) {
                    *response = MUPDATE_BYE;
                }
                goto done;
            }
            goto badcmd;

        case 'D':
            if (!strncmp(handle->cmd.s, "DELETE", 6)) {
                ch = getstring(handle->conn->in, handle->conn->out, &(handle->arg1));
                CHECKNEWLINE(handle, ch);

                memset(&box, 0, sizeof(box));
                box.mailbox = handle->arg1.s;

                /* Handle delete command */
                r = callback(&box, handle->cmd.s, context);
                if (r) {
                    syslog(LOG_ERR,
                           "error deleting mailbox: callback returned %d", r);
                    goto done;
                }
                break;
            }
            goto badcmd;

        case 'M':
            if (!strncmp(handle->cmd.s, "MAILBOX", 7)) {
                /* Mailbox Name */
                ch = getstring(handle->conn->in, handle->conn->out, &(handle->arg1));
                if (ch != ' ') {
                    r = MUPDATE_PROTOCOL_ERROR;
                    goto done;
                }

                /* Server */
                ch = getstring(handle->conn->in, handle->conn->out, &(handle->arg2));
                if (ch != ' ') {
                    r = MUPDATE_PROTOCOL_ERROR;
                    goto done;
                }

                /* ACL */
                ch = getstring(handle->conn->in, handle->conn->out, &(handle->arg3));
                CHECKNEWLINE(handle, ch);

                /* Handle mailbox command */
                memset(&box, 0, sizeof(box));
                box.mailbox = handle->arg1.s;
                box.location = handle->arg2.s;
                box.acl = handle->arg3.s;
                r = callback(&box, handle->cmd.s, context);
                if (r) { /* callback error ? */
                    syslog(LOG_ERR,
                           "error activating mailbox: callback returned %d", r);
                    goto done;
                }
                break;
            }
            goto badcmd;
        case 'N':
            if (!strncmp(handle->cmd.s, "NO", 2)) {
                ch = getstring(handle->conn->in, handle->conn->out, &(handle->arg1));
                CHECKNEWLINE(handle, ch);

                syslog(LOG_DEBUG, "mupdate NO response: %s", handle->arg1.s);
                if (wait_for_ok) {
                    if (response) *response = MUPDATE_NO;
                    goto done;
                }
                break;
            }
            goto badcmd;
        case 'O':
            if (!strncmp(handle->cmd.s, "OK", 2)) {
                /* It's all good, grab the attached string and move on */
                ch = getstring(handle->conn->in, handle->conn->out, &(handle->arg1));

                CHECKNEWLINE(handle, ch);
                if (wait_for_ok) {
                    if (response) *response = MUPDATE_OK;
                    goto done;
                }
                break;
            }
            goto badcmd;
        case 'R':
            if (!strncmp(handle->cmd.s, "RESERVE", 7)) {
                /* Mailbox Name */
                ch = getstring(handle->conn->in, handle->conn->out, &(handle->arg1));
                if (ch != ' ') {
                    r = MUPDATE_PROTOCOL_ERROR;
                    goto done;
                }

                /* Server */
                ch = getstring(handle->conn->in, handle->conn->out, &(handle->arg2));
                CHECKNEWLINE(handle, ch);

                /* Handle reserve command */
                memset(&box, 0, sizeof(box));
                box.mailbox = handle->arg1.s;
                box.location = handle->arg2.s;
                r = callback(&box, handle->cmd.s, context);
                if (r) { /* callback error ? */
                    syslog(LOG_ERR,
                           "error reserving mailbox: callback returned %d", r);
                    goto done;
                }

                break;
            }
            goto badcmd;

        default:
        badcmd:
            /* Bad Command */
            syslog(LOG_ERR, "bad/unexpected command from master: %s",
                   handle->cmd.s);
            r = MUPDATE_PROTOCOL_ERROR;
            goto done;
        }
    }

 done:
    /* reset blocking */
    prot_NONBLOCK(handle->conn->in);

    return r;
}

EXPORTED void kick_mupdate(void)
{
    char buf[2048];
    struct buf addrbuf = BUF_INITIALIZER;
    struct sockaddr_un srvaddr;
    int s, r;
    int len;

    /* no server? drop out */
    if (!config_mupdate_server)
        return;

    /* don't kick on standard backends */
    if (config_mupdate_config == IMAP_ENUM_MUPDATE_CONFIG_STANDARD
        && config_getstring(IMAPOPT_PROXYSERVERS))
        return;

    s = socket(AF_UNIX, SOCK_STREAM, 0);
    if (s == -1) {
        syslog(LOG_ERR, "socket: %m");
        return;
    }

    buf_appendcstr(&addrbuf, config_dir);
    buf_appendcstr(&addrbuf, FNAME_MUPDATE_TARGET_SOCK);
    if (buf_len(&addrbuf) >= sizeof(srvaddr.sun_path)) {
        syslog(LOG_ERR, "kick_mupdate: configured socket path '%s' is too long"
                        " (maximum length is " SIZE_T_FMT ")",
                        buf_cstring(&addrbuf), sizeof(srvaddr.sun_path) - 1);
        fatal("socket path too long", EX_CONFIG);
    }

    memset((char *)&srvaddr, 0, sizeof(srvaddr));
    srvaddr.sun_family = AF_UNIX;
    strlcpy(srvaddr.sun_path, buf_cstring(&addrbuf), sizeof(srvaddr.sun_path));
    len = sizeof(srvaddr.sun_family) + strlen(srvaddr.sun_path) + 1;

    r = connect(s, (struct sockaddr *)&srvaddr, len);
    if (r == -1) {
        syslog(LOG_ERR, "kick_mupdate: can't connect to target: %m");
        goto done;
    }

    r = read(s, buf, sizeof(buf));
    if (r <= 0) {
        syslog(LOG_ERR, "kick_mupdate: can't read from target: %m");
    }

 done:
    buf_free(&addrbuf);
    close(s);
    return;
}