Codebase list cyrus-imapd / lintian-fixes/main imap / proc.c
lintian-fixes/main

Tree @lintian-fixes/main (Download .tar.gz)

proc.c @lintian-fixes/mainraw · 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
/* proc.c -- Server process registry
 *
 * 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 <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <signal.h>
#include <sysexits.h>
#include <syslog.h>
#include <string.h>
#include <sys/stat.h>

#include "assert.h"
#include "global.h"
#include "proc.h"
#include "retry.h"
#include "util.h"
#include "xmalloc.h"

#ifdef HAVE_DIRENT_H
# include <dirent.h>
# define NAMLEN(dirent) strlen((dirent)->d_name)
#else
# define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen
# if HAVE_SYS_NDIR_H
#  include <sys/ndir.h>
# endif
# if HAVE_SYS_DIR_H
#  include <sys/dir.h>
# endif
# if HAVE_NDIR_H
#  include <ndir.h>
# endif
#endif


#define FNAME_PROCDIR "/proc"

static char *procfname = 0;

static char *proc_getpath(pid_t pid, int isnew)
{
    struct buf buf = BUF_INITIALIZER;

    if (config_getstring(IMAPOPT_PROC_PATH)) {
        const char *procpath = config_getstring(IMAPOPT_PROC_PATH);

        if (procpath[0] != '/')
            fatal("proc path must be fully qualified", EX_CONFIG);

        if (strlen(procpath) < 2)
            fatal("proc path must not be '/'", EX_CONFIG);

        buf_setcstr(&buf, procpath);

        if (buf.s[buf.len-1] != '/')
            buf_putc(&buf, '/');
    }
    else {
        buf_setcstr(&buf, config_dir);
        buf_appendcstr(&buf, FNAME_PROCDIR);
        buf_putc(&buf, '/');
    }

    if (pid)
        buf_printf(&buf, "%u", pid);

    if (isnew)
        buf_appendcstr(&buf, ".new");

    return buf_release(&buf);
}

static char *proc_getdir(void)
{
    return proc_getpath(0, 0);
}

EXPORTED int proc_register(const char *servicename, const char *clienthost,
                  const char *userid, const char *mailbox, const char *cmd)
{
    pid_t pid = getpid();
    FILE *procfile = NULL;
    char *newfname = NULL;

    if (!procfname)
        procfname = proc_getpath(pid, /*isnew*/0);

    newfname = proc_getpath(pid, /*isnew*/1);

    procfile = fopen(newfname, "w+");
    if (!procfile) {
        if (cyrus_mkdir(newfname, 0755) == -1) {
            fatal("couldn't create proc directory", EX_IOERR);
        }
        else {
            syslog(LOG_NOTICE, "created proc directory");
            procfile = fopen(newfname, "w+");
            if (!procfile) {
                syslog(LOG_ERR, "IOERROR: creating %s: %m", newfname);
                fatal("can't write proc file", EX_IOERR);
            }
        }
    }

    if (!servicename) servicename = "";
    if (!clienthost) clienthost = "";
    if (!userid) userid = "";
    if (!mailbox) mailbox = "";
    if (!cmd) cmd = "";
    fprintf(procfile, "%s\t%s\t%s\t%s\t%s\n", servicename, clienthost, userid, mailbox, cmd);
    fclose(procfile);

    if (rename(newfname, procfname)) {
        syslog(LOG_ERR, "IOERROR: renaming %s to %s: %m", newfname, procfname);
        unlink(newfname);
        fatal("can't write proc file", EX_IOERR);
    }

    setproctitle("%s: %s %s %s %s", servicename, clienthost, userid, mailbox, cmd);

    free(newfname);
    return 0;
}

EXPORTED void proc_cleanup(void)
{
    if (procfname) {
        unlink(procfname);
        free(procfname);
        procfname = NULL;
    }
}

static int proc_foreach_helper(pid_t pid, procdata_t *func, void *rock)
{
    int r = 0;
    char *buf = NULL;
    char *path = NULL;
    int fd = -1;

    path = proc_getpath(pid, /*isnew*/0);

    fd = open(path, O_RDONLY, 0);
    if (fd != -1) {
        /* yay, got a file */
        int n;
        struct stat sbuf;
        char *p;
        char *service = NULL;
        char *host = NULL;
        char *user = NULL;
        char *mailbox = NULL;
        char *cmd = NULL;

        if (fstat(fd, &sbuf))
            goto done;
        if (!S_ISREG(sbuf.st_mode))
            goto done;

        /* grab a copy of the file contents */
        buf = xmalloc(sbuf.st_size+1);
        n = retry_read(fd, buf, sbuf.st_size);
        if (n != sbuf.st_size)
            goto done;

        buf[sbuf.st_size] = '\0';

        /* remove any endline characters */
        p = strchr(buf, '\r');
        if (p) *p = '\0';
        p = strchr(buf, '\n');
        if (p) *p = '\0';

        /* parse the fields */
        service = buf;
        host = strchr(service, '\t');
        if (!host) goto done;
        *host++ = '\0';
        user = strchr(host, '\t');
        if (user) {
            *user++ = '\0';
            mailbox = strchr(user, '\t');
        }
        if (mailbox) {
            *mailbox++ = '\0';
            cmd = strchr(mailbox, '\t');
        }
        if (cmd) {
            *cmd++ = '\0';
        }

        (*func)(pid, service, host, user, mailbox, cmd, rock);
    }

done:
    xclose(fd);
    free(buf);
    free(path);
    return r;
}

EXPORTED int proc_foreach(procdata_t *func, void *rock)
{
    DIR *dirp;
    struct dirent *dirent;
    char *path;
    const char *p;
    pid_t pid;
    char *end = NULL;
    size_t len;
    int r = 0;

    path = proc_getdir();
    dirp = opendir(path);

    if (dirp) {
        while ((dirent = readdir(dirp)) != NULL) {
            p = dirent->d_name;
            if (*p == '.') continue; /* dot files */
            len = strlen(p);
            if (len > 4 && !strcmp(p + len - 4, ".new")) continue; /* temporary new file */
            pid = strtoul(p, &end, 10);
            if (pid == 0 || end == NULL || *end || end == p) {
                syslog(LOG_ERR, "IOERROR: bogus filename \"%s/%s\" in proc_foreach",
                                path, p);
                continue;
            }
            r = proc_foreach_helper(pid, func, rock);
            if (r) break;
        }
        closedir(dirp);
    }

    free(path);
    return r;
}

static int procusage_cb(pid_t pid __attribute__((unused)),
                        const char *servicename __attribute__((unused)),
                        const char *clienthost,
                        const char *userid,
                        const char *mboxname __attribute__((unused)),
                        const char *cmd __attribute__((unused)),
                        void *rock)
{
    struct proc_limits *limitsp = (struct proc_limits *)rock;

    /* we only count logged in sessions */
    if (!userid) return 0;

    if (limitsp->clienthost && !strcmp(clienthost, limitsp->clienthost))
        limitsp->host++;
    if (limitsp->userid && !strcmp(userid, limitsp->userid))
        limitsp->user++;

    return 0;
}

EXPORTED int proc_checklimits(struct proc_limits *limitsp)
{
    limitsp->maxhost = config_getint(IMAPOPT_MAXLOGINS_PER_HOST);
    limitsp->maxuser = config_getint(IMAPOPT_MAXLOGINS_PER_USER);

    if (!limitsp->maxuser && !limitsp->maxhost)
        return 0;

    limitsp->host = 0;
    limitsp->user = 0;
    proc_foreach(procusage_cb, limitsp);

    if (limitsp->maxhost && limitsp->host >= limitsp->maxhost) return 1;
    if (limitsp->maxuser && limitsp->user >= limitsp->maxuser) return 1;

    return 0;
}

struct prockill_data {
    const char *servicename;
    const char *clienthost;
    const char *userid;
    const char *mboxname;
    const char *cmd;
    int sig;
};

#define PROCKILL_INIT { NULL, NULL, NULL, NULL, NULL, 0 }

static int prockill_cb(pid_t pid,
                       const char *servicename,
                       const char *clienthost,
                       const char *userid,
                       const char *mboxname,
                       const char *cmd,
                       void *rock)
{
    struct prockill_data *dat = (struct prockill_data *)rock;
    pid_t mypid = getpid();

    /* don't kill myself */
    if (mypid == pid)
        return 0;

    if (dat->servicename && strcmpsafe(servicename, dat->servicename))
        return 0;

    if (dat->clienthost && strcmpsafe(clienthost, dat->clienthost))
        return 0;

    if (dat->userid && strcmpsafe(userid, dat->userid))
        return 0;

    if (dat->mboxname && strcmpsafe(mboxname, dat->mboxname))
        return 0;

    if (dat->cmd && strcmpsafe(cmd, dat->cmd))
        return 0;

    if (dat->sig)
        kill(pid, dat->sig);
    else
        kill(pid, SIGTERM);

    return 0;
}


EXPORTED void proc_killuser(const char *userid)
{
    struct prockill_data rock = PROCKILL_INIT;

    /* can't kill all non-connected, that's evil */
    assert(userid && userid[0]);

    rock.userid = userid;

    proc_foreach(prockill_cb, &rock);
}

EXPORTED void proc_killmbox(const char *mboxname)
{
    struct prockill_data rock = PROCKILL_INIT;

    /* can't kill all non-selected, that's evil */;
    assert(mboxname && mboxname[0]);

    rock.mboxname = mboxname;

    proc_foreach(prockill_cb, &rock);
}

EXPORTED void proc_killusercmd(const char *userid, const char *cmd, int sig)
{
    struct prockill_data rock = PROCKILL_INIT;

    /* can't kill all non-selected, that's evil */;
    assert(userid && userid[0]);
    /* or all cmd either... use proc_killuser if you want that */
    assert(cmd && cmd[0]);

    rock.userid = userid;
    rock.cmd = cmd;
    rock.sig = sig;

    proc_foreach(prockill_cb, &rock);
}