File: drac-plugin.c

package info (click to toggle)
dovecot 1:2.2.13-12~deb8u4
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 38,792 kB
  • sloc: ansic: 341,472; sh: 16,920; makefile: 5,393; cpp: 1,474; perl: 265; xml: 44; python: 34; pascal: 27
file content (108 lines) | stat: -rw-r--r-- 3,058 bytes parent folder | download | duplicates (3)
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
/*
 *
 * dovecot plugin for DRAC authentication
 *
 * Copyright (C) 2011 DesigNET, INC.
 *
 * $Id: drac-plugin.c,v 1.1.1.1 2011/09/09 08:04:34 usuda Exp $
 *
 * based:
 *   http://dovecot.org/patches/1.1/drac.c
 */
#include "lib.h"
#include "net.h"
#include "ioloop.h"
#include "mail-user.h"
#include "mail-storage-private.h"
#include <stdlib.h>
#include <string.h>

/* default parameters */
#define DRAC_TIMEOUT_SECS	(60)
#define DRAC_HOST		"localhost"


/* libdrac function */
int dracauth(char *, unsigned long, char **);

const char *drac_plugin_version = DOVECOT_VERSION;

static struct timeout *to_drac = NULL;
static const char *drachost = NULL; /* dracd host */
static unsigned long in_ip = 0; /* remote ip address */
static unsigned long dractout = 0; /* drac timeout secs */

static void drac_timeout(void *context ATTR_UNUSED)
{
    char *err;

    if(dracauth((char *)drachost, in_ip, &err) != 0) {
        i_error("%s: dracauth() failed: %s", __FUNCTION__, err);
    }
}

static void drac_mail_user_created(struct mail_user *user)
{
    const char *dractout_str;
    char addrname[256];
    char *ep;

    /* check address family */
    if(user->remote_ip->family != AF_INET) {
        if(inet_ntop(user->remote_ip->family, &user->remote_ip->u,
           addrname, sizeof(addrname)-1) == NULL) {
            strcpy(addrname, "<unknown>");
        }
        i_error("%s: Only IPv4 addresses are supported (%s)", __FUNCTION__,
                addrname);
    } else {
        /* get remote IPv4 address... uum... */
        memcpy(&in_ip, &user->remote_ip->u, sizeof(in_ip));

        /* get DRAC server name */
        drachost = mail_user_plugin_getenv(user, "dracdserver");
        if(drachost == NULL) {
            drachost = DRAC_HOST;
        }

        /* get timeout secs */
        dractout_str = mail_user_plugin_getenv(user, "dracdtimeout");
        if(dractout_str == NULL) {
            dractout = DRAC_TIMEOUT_SECS;
        } else {
            dractout = strtoul(dractout_str, &ep, 10);
            /* bad format -> use default value */
            if(ep != NULL && *ep != '\0') {
                i_warning("%s: bad dracdtimeout (%s). using default %d",
                          __FUNCTION__, dractout_str, DRAC_TIMEOUT_SECS);
                dractout = DRAC_TIMEOUT_SECS;
            }
        }
        i_debug("%s: dracdserver=%s, timeout=%ldsecs", __FUNCTION__,
               drachost, dractout);

        /* connect to DRAC server */
        drac_timeout(NULL);
        to_drac = timeout_add(1000*dractout, drac_timeout, NULL);
    }
}

static struct mail_storage_hooks drac_mail_storage_hooks = {
    .mail_user_created = drac_mail_user_created,
};

void drac_plugin_init(struct module *module)
{
    mail_storage_hooks_add(module, &drac_mail_storage_hooks);
    i_debug("%s called", __FUNCTION__);
}

void drac_plugin_deinit(void)
{
    if(to_drac != NULL) {
        timeout_remove(&to_drac);
        to_drac = NULL;
    }
    mail_storage_hooks_remove(&drac_mail_storage_hooks);
    i_debug("%s called", __FUNCTION__);
}