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
|
/* $Id: macro.c,v 1.9 2008/11/18 11:57:29 manu Exp $ */
/*
* Copyright (c) 2006 Emmanuel Dreyfus
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Emmanuel Dreyfus
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#ifdef HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#ifdef __RCSID
__RCSID("$Id: macro.c,v 1.9 2008/11/18 11:57:29 manu Exp $");
#endif
#endif
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <sysexits.h>
#include <regex.h>
#ifdef HAVE_OLD_QUEUE_H
#include "queue.h"
#else
#include <sys/queue.h>
#endif
#include "milter-greylist.h"
#include "pending.h"
#include "conf.h"
#include "spf.h"
#include "macro.h"
#ifdef USE_DMALLOC
#include <dmalloc.h>
#endif
/*
* locking is done through the same lock as acllist: both are static
* configuration, which are readen or changed at the same times.
*/
struct macrolist macro_head;
void
macro_init(void) {
LIST_INIT(¯o_head);
return;
}
int
macro_check(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
SMFICTX *ctx;
struct macro_entry *me;
char *value;
int retval = 0;
ctx = priv->priv_ctx;
me = ad->macro;
value = smfi_getsymval(ctx, me->m_macro);
switch (me->m_type) {
case M_UNSET:
if (value == NULL)
retval = 1;
break;
case M_STRING:
if (value != NULL && strcmp(value, me->m_string) == 0)
retval = 1;
break;
case M_REGEX:
if (value != NULL &&
regexec(me->m_regex, value, 0, NULL, 0) == 0)
retval = 1;
break;
default:
mg_log(LOG_ERR, "unexpecte me->m_type = %d", me->m_type);
exit(EX_SOFTWARE);
break;
}
if (conf.c_debug) {
mg_log(LOG_DEBUG, "sm_macro \"%s\" %s=%s %s", me->m_name,
me->m_macro, value ? value : "(null)",
retval ? "match" : "nomatch");
}
return retval;
}
void
macro_add_unset(name, macro)
char *name;
char *macro;
{
struct macro_entry *me;
if (macro_byname(name) != NULL) {
mg_log(LOG_ERR, "macro \"%s\" defined twice at line %d",
name, conf_line - 1);
exit(EX_DATAERR);
}
if ((me = malloc(sizeof(*me))) == NULL) {
mg_log(LOG_ERR, "malloc failed: %s", strerror(errno));
exit(EX_OSERR);
}
me->m_type = M_UNSET;
if ((me->m_name = strdup(name)) == NULL) {
mg_log(LOG_ERR, "strdup failed: %s", strerror(errno));
exit(EX_OSERR);
}
if ((me->m_macro = strdup(macro)) == NULL) {
mg_log(LOG_ERR, "strdup failed: %s", strerror(errno));
exit(EX_OSERR);
}
me->m_string = NULL;
LIST_INSERT_HEAD(¯o_head, me, m_list);
if (conf.c_debug || conf.c_acldebug) {
mg_log(LOG_DEBUG, "load sm_macro \"%s\" \"%s\" unset",
me->m_name, me->m_macro);
}
return;
}
void
macro_add_string(name, macro, string)
char *name;
char *macro;
char *string;
{
struct macro_entry *me;
if ((me = malloc(sizeof(*me))) == NULL) {
mg_log(LOG_ERR, "malloc failed: %s", strerror(errno));
exit(EX_OSERR);
}
me->m_type = M_STRING;
if ((me->m_name = strdup(name)) == NULL) {
mg_log(LOG_ERR, "strdup failed: %s", strerror(errno));
exit(EX_OSERR);
}
if ((me->m_macro = strdup(macro)) == NULL) {
mg_log(LOG_ERR, "strdup failed: %s", strerror(errno));
exit(EX_OSERR);
}
if ((me->m_string = strdup(string)) == NULL) {
mg_log(LOG_ERR, "strdup failed: %s", strerror(errno));
exit(EX_OSERR);
}
LIST_INSERT_HEAD(¯o_head, me, m_list);
if (conf.c_debug || conf.c_acldebug) {
mg_log(LOG_DEBUG, "load sm_macro \"%s\" \"%s\" \"%s\"",
me->m_name, me->m_macro, me->m_string);
}
return;
}
#define ERRLEN 1024
void
macro_add_regex(name, macro, regex)
char *name;
char *macro;
char *regex;
{
struct macro_entry *me;
char errstr[ERRLEN + 1];
int error;
size_t len;
/* Strip slashes */
len = strlen(regex);
if (len > 0)
regex[len - 1] = '\0';
regex++;
if ((me = malloc(sizeof(*me))) == NULL) {
mg_log(LOG_ERR, "malloc failed: %s", strerror(errno));
exit(EX_OSERR);
}
me->m_type = M_REGEX;
if ((me->m_name = strdup(name)) == NULL) {
mg_log(LOG_ERR, "strdup failed: %s", strerror(errno));
exit(EX_OSERR);
}
if ((me->m_macro = strdup(macro)) == NULL) {
mg_log(LOG_ERR, "strdup failed: %s", strerror(errno));
exit(EX_OSERR);
}
if ((me->m_regex = malloc(sizeof(*me->m_regex))) == NULL) {
mg_log(LOG_ERR, "malloc failed: %s", strerror(errno));
exit(EX_OSERR);
}
if ((error = regcomp(me->m_regex, regex,
(conf.c_extendedregex ? REG_EXTENDED : 0) | REG_ICASE)) != 0) {
regerror(error, me->m_regex, errstr, ERRLEN);
mg_log(LOG_ERR, "bad regular expression \"%s\": %s",
regex, errstr);
exit(EX_OSERR);
}
LIST_INSERT_HEAD(¯o_head, me, m_list);
if (conf.c_debug || conf.c_acldebug) {
mg_log(LOG_DEBUG, "load sm_macro \"%s\" \"%s\" /%s/",
me->m_name, me->m_macro, regex);
}
return;
}
struct macro_entry *
macro_byname(macro) /* acllist must be read locked */
char *macro;
{
struct macro_entry *me;
LIST_FOREACH(me, ¯o_head, m_list) {
if (strcmp(me->m_name, macro) == 0)
break;
}
return me;
}
void
macro_clear(void) /* acllist must be write locked */
{
struct macro_entry *me;
while(!LIST_EMPTY(¯o_head)) {
me = LIST_FIRST(¯o_head);
LIST_REMOVE(me, m_list);
free(me->m_name);
free(me->m_macro);
switch (me->m_type) {
case M_UNSET:
break;
case M_STRING:
free(me->m_string);
break;
case M_REGEX:
regfree(me->m_regex);
free(me->m_regex);
break;
default:
mg_log(LOG_ERR,
"unexpecte me->m_type = %d", me->m_type);
exit(EX_SOFTWARE);
}
}
macro_init();
return;
}
|