File: notifyd.c

package info (click to toggle)
cyrus-imapd 3.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 60,760 kB
  • sloc: ansic: 280,403; perl: 146,834; javascript: 9,624; sh: 5,730; yacc: 2,660; cpp: 2,263; makefile: 2,103; lex: 675; xml: 621; awk: 303; python: 273; asm: 262
file content (275 lines) | stat: -rw-r--r-- 7,604 bytes parent folder | download | duplicates (2)
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
/* notifyd.c -- main file for notifyd (notify script notification program)
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sysexits.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <signal.h>
#include <string.h>

#include "notifyd.h"

#include "imap/global.h"
#include "libconfig.h"
#include "imap/notify.h"
#include "xmalloc.h"
#include "strarray.h"

#include "master/service.h"

/* global state */
const int config_need_data = 0;

static int soc = 0; /* master has handed us the port as stdin */

static notifymethod_t *default_method;  /* default method daemon is using */


/* Cleanly shut down and exit */
void shut_down(int code) __attribute__ ((noreturn));
EXPORTED void shut_down(int code)
{
    in_shutdown = 1;

    cyrus_done();

    /* done */
    exit(code);
}

static char *fetch_arg(char *head, char* tail)
{
    char *cp;

    for (cp = head; *cp && cp < tail; cp++);
    return (cp == tail ? NULL : cp + 1);
}

static int do_notify(void)
{
    struct sockaddr_un sun_data;
    socklen_t sunlen = sizeof(sun_data);
    char buf[NOTIFY_MAXSIZE+1], *cp, *tail;
    int r, i;
    char *method, *class, *priority, *user, *mailbox, *message;
    strarray_t options = STRARRAY_INITIALIZER;
    long nopt;
    char *reply;
    char *fname = NULL;
    notifymethod_t *nmethod;
    unsigned bufsiz;
    socklen_t optlen;

    /* Get receive buffer size */
    optlen = sizeof(bufsiz);
    r = getsockopt(soc, SOL_SOCKET, SO_RCVBUF, &bufsiz, &optlen);
    if (r == -1) {
        syslog(LOG_ERR, "unable to getsockopt(SO_RCVBUF) on notify socket: %m");
        return (errno);
    }

    /* Use minimum of 1/10 of receive buffer size (-overhead) NOTIFY_MAXSIZE */
    bufsiz = MIN(bufsiz / 10 - 32, NOTIFY_MAXSIZE);

    while (1) {
        method = class = priority = user = mailbox = message = reply = NULL;
        nopt = 0;

        if (signals_poll() == SIGHUP) {
            /* caught a SIGHUP, return */
            return 0;
        }
        r = recvfrom(soc, buf, bufsiz, 0,
                     (struct sockaddr *) &sun_data, &sunlen);
        if (r == -1) {
            return (errno);
        }
        buf[r] = '\0';

        tail = buf + r - 1;

        /*
         * parse request of the form:
         *
         * method NUL class NUL priority NUL user NUL mailbox NUL
         *   nopt NUL N(option NUL) message NUL
         */
        method = (cp = buf);

        if (cp) class = (cp = fetch_arg(cp, tail));
        if (cp) priority = (cp = fetch_arg(cp, tail));
        if (cp) user = (cp = fetch_arg(cp, tail));
        if (cp) mailbox = (cp = fetch_arg(cp, tail));

        if (cp) cp = fetch_arg(cp, tail); /* skip to nopt */
        if (cp) nopt = strtol(cp, NULL, 10);
        if (nopt < 0 || errno == ERANGE) cp = NULL;

        for (i = 0; cp && i < nopt; i++)
            strarray_append(&options, cp = fetch_arg(cp, tail));

        if (cp) message = (cp = fetch_arg(cp, tail));
        if (cp) fname = (cp = fetch_arg(cp, tail));

        if (!message) {
            syslog(LOG_ERR, "malformed notify request");
            strarray_fini(&options);
            return 0;
        }

        if (!*method)
            nmethod = default_method;
        else {
            nmethod = methods;
            while (nmethod->name) {
                if (!strcasecmp(nmethod->name, method)) break;
                nmethod++;
            }
        }

        syslog(LOG_DEBUG, "do_notify using method '%s'",
               nmethod->name ? nmethod->name: "unknown");

        if (nmethod->name) {
            reply = nmethod->notify(class, priority, user, mailbox,
                                    nopt, options.data, message, fname);
        }
#if 0  /* we don't care about responses right now */
        else {
            reply = strdup("NO unknown notification method");
            if (!reply) {
                fatal("strdup failed", EX_OSERR);
            }
        }
#endif

        free(reply);
        strarray_fini(&options);
    }

    /* never reached */
}


EXPORTED void fatal(const char *s, int code)
{
    static int recurse_code = 0;

    if (recurse_code) {
        /* We were called recursively. Just give up */
        exit(recurse_code);
    }
    recurse_code = code;

    syslog(LOG_ERR, "Fatal error: %s", s);

    if (code != EX_PROTOCOL && config_fatals_abort) abort();

    shut_down(code);
}

static void usage(void)
{
    syslog(LOG_ERR, "usage: notifyd [-C <alt_config>] [-U <max usage>] [-T timeout] [-D] [-X] [-m method].  method defaults to null");
    exit(EX_USAGE);
}

EXPORTED int service_init(int argc, char **argv, char **envp __attribute__((unused)))
{
    int opt;
    const char *method = "null";

    if (geteuid() == 0) fatal("must run as the Cyrus user", EX_USAGE);

    while ((opt = getopt(argc, argv, "m:")) != EOF) {
        switch(opt) {
        case 'm':
            method = optarg;
            break;
        default:
            usage();
        }
    }

    default_method = methods;
    while (default_method->name) {
        if (!strcasecmp(default_method->name, method)) break;
        default_method++;
    }

    if (!default_method) fatal("unknown notification method %s", EX_USAGE);

    signals_set_shutdown(&shut_down);

    return 0;
}

/* Called by service API to shut down the service */
EXPORTED void service_abort(int error)
{
    shut_down(error);
}

EXPORTED int service_main(int argc __attribute__((unused)),
                 char **argv __attribute__((unused)),
                 char **envp __attribute__((unused)))
{
    int r = 0;

    r = do_notify();

    shut_down(r);
    return 0;
}