[c5c522c] | 1 | /* $Id: config_api.c,v 1.21 2011/06/28 00:13:48 sbajic Exp $ */ |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | DSPAM |
---|
| 5 | COPYRIGHT (C) 2002-2012 DSPAM PROJECT |
---|
| 6 | |
---|
| 7 | This program is free software: you can redistribute it and/or modify |
---|
| 8 | it under the terms of the GNU Affero General Public License as |
---|
| 9 | published by the Free Software Foundation, either version 3 of the |
---|
| 10 | License, or (at your option) any later version. |
---|
| 11 | |
---|
| 12 | This program is distributed in the hope that it will be useful, |
---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | GNU Affero General Public License for more details. |
---|
| 16 | |
---|
| 17 | You should have received a copy of the GNU Affero General Public License |
---|
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 19 | |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | /* |
---|
| 23 | * config_api.c - configuration functions for populating libdspam properties |
---|
| 24 | * |
---|
| 25 | * DESCRIPTION |
---|
| 26 | * The functions in this section are used to manipulate the libdspam |
---|
| 27 | * context using various APIs. Specifically, the properties API and the |
---|
| 28 | * storage attach API are used here. |
---|
| 29 | */ |
---|
| 30 | |
---|
| 31 | #ifdef HAVE_CONFIG_H |
---|
| 32 | #include <auto-config.h> |
---|
| 33 | #endif |
---|
| 34 | |
---|
| 35 | #include <stdio.h> |
---|
| 36 | #include <errno.h> |
---|
| 37 | #include <stdlib.h> |
---|
| 38 | #ifdef HAVE_STRINGS_H |
---|
| 39 | #include <strings.h> |
---|
| 40 | #endif |
---|
| 41 | #include <string.h> |
---|
| 42 | |
---|
| 43 | #include "config_api.h" |
---|
| 44 | #include "read_config.h" |
---|
| 45 | #include "error.h" |
---|
| 46 | #include "language.h" |
---|
| 47 | #include "libdspam.h" |
---|
| 48 | #include "util.h" |
---|
| 49 | |
---|
| 50 | /* |
---|
| 51 | * set_libdspam_attributes(DSPAM_CTX *CTX) |
---|
| 52 | * |
---|
| 53 | * DESCRIPTION |
---|
| 54 | * populate libdspam properties with relevant configuration values from |
---|
| 55 | * dspam.conf. since dspam.conf is owned by the agent, libdspam doesn't read |
---|
| 56 | * it directly; we rely on this api to configure the context. |
---|
| 57 | * |
---|
| 58 | * INPUT ARGUMENTS |
---|
| 59 | * CTX dspam context to configure |
---|
| 60 | * |
---|
| 61 | * RETURN VALUES |
---|
| 62 | * returns 0 on success |
---|
| 63 | */ |
---|
| 64 | |
---|
| 65 | int set_libdspam_attributes(DSPAM_CTX *CTX) { |
---|
| 66 | attribute_t t; |
---|
| 67 | int i, ret = 0; |
---|
| 68 | char *profile; |
---|
| 69 | |
---|
| 70 | t = _ds_find_attribute(agent_config, "IgnoreHeader"); |
---|
| 71 | while(t) { |
---|
| 72 | ret += dspam_addattribute(CTX, t->key, t->value); |
---|
| 73 | t = t->next; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | profile = _ds_read_attribute(agent_config, "DefaultProfile"); |
---|
| 77 | |
---|
| 78 | for(i=0;agent_config[i];i++) { |
---|
| 79 | t = agent_config[i]; |
---|
| 80 | |
---|
| 81 | while(t) { |
---|
| 82 | |
---|
| 83 | if (!strncasecmp(t->key, "MySQL", 5) || |
---|
| 84 | !strncasecmp(t->key, "PgSQL", 5) || |
---|
| 85 | !strncasecmp(t->key, "SQLite", 6) || |
---|
| 86 | !strcasecmp(t->key, "LocalMX") || |
---|
| 87 | !strncasecmp(t->key, "Storage", 7) || |
---|
| 88 | !strncasecmp(t->key, "Processor", 9) || |
---|
| 89 | !strncasecmp(t->key, "Hash", 4)) |
---|
| 90 | { |
---|
| 91 | if (profile == NULL || profile[0] == 0) |
---|
| 92 | { |
---|
| 93 | ret += dspam_addattribute(CTX, t->key, t->value); |
---|
| 94 | } |
---|
| 95 | else if (strchr(t->key, '.')) |
---|
| 96 | { |
---|
| 97 | if (!strcasecmp((strchr(t->key, '.')+1), profile)) { |
---|
| 98 | char *x = strdup(t->key); |
---|
| 99 | char *y = strchr(x, '.'); |
---|
| 100 | y[0] = 0; |
---|
| 101 | |
---|
| 102 | ret += dspam_addattribute(CTX, x, t->value); |
---|
| 103 | free(x); |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | t = t->next; |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | ret += configure_algorithms(CTX); |
---|
| 112 | |
---|
| 113 | return ret; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | /* |
---|
| 117 | * attach_context(DSPAM_CTX *CTX, void *dbh) |
---|
| 118 | * |
---|
| 119 | * DESCRIPTION |
---|
| 120 | * attach a database handle to an initialized dspam context |
---|
| 121 | * |
---|
| 122 | * INPUT ARGUMENTS |
---|
| 123 | * CTX dspam context |
---|
| 124 | * dbh database handle to attach |
---|
| 125 | * |
---|
| 126 | * RETURN VALUES |
---|
| 127 | * returns 0 on success |
---|
| 128 | */ |
---|
| 129 | |
---|
| 130 | int attach_context(DSPAM_CTX *CTX, void *dbh) { |
---|
| 131 | int maxtries = 1, tries = 0; |
---|
| 132 | int r; |
---|
| 133 | |
---|
| 134 | if (!_ds_read_attribute(agent_config, "DefaultProfile")) |
---|
| 135 | return dspam_attach(CTX, dbh); |
---|
| 136 | |
---|
| 137 | /* Perform failover if an attach fails */ |
---|
| 138 | |
---|
| 139 | if (_ds_read_attribute(agent_config, "FailoverAttempts")) |
---|
| 140 | maxtries = atoi(_ds_read_attribute(agent_config, "FailoverAttempts")); |
---|
| 141 | |
---|
| 142 | r = dspam_attach(CTX, dbh); |
---|
| 143 | while (r && tries < maxtries) { |
---|
| 144 | char key[128]; |
---|
| 145 | char *failover; |
---|
| 146 | |
---|
| 147 | snprintf(key, sizeof(key), "Failover.%s", _ds_read_attribute(agent_config, "DefaultProfile")); |
---|
| 148 | |
---|
| 149 | failover = _ds_read_attribute(agent_config, key); |
---|
| 150 | |
---|
| 151 | if (!failover) { |
---|
| 152 | LOG(LOG_ERR, ERR_AGENT_FAILOVER_OUT); |
---|
| 153 | return r; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | LOG(LOG_WARNING, ERR_AGENT_FAILOVER, failover); |
---|
| 157 | _ds_overwrite_attribute(agent_config, "DefaultProfile", failover); |
---|
| 158 | |
---|
| 159 | if (dspam_clearattributes(CTX)) { |
---|
| 160 | LOG(LOG_ERR, ERR_AGENT_CLEAR_ATTRIB); |
---|
| 161 | return r; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | set_libdspam_attributes(CTX); |
---|
| 165 | |
---|
| 166 | tries++; |
---|
| 167 | r = dspam_attach(CTX, dbh); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | return r; |
---|
| 171 | } |
---|