source: npl/mailserver/dspam/dspam-3.10.2/src/config_shared.c @ c5c522c

gcc484ntopperl-5.22
Last change on this file since c5c522c was c5c522c, checked in by Edwin Eefting <edwin@datux.nl>, 8 years ago

initial commit, transferred from cleaned syn3 svn tree

  • Property mode set to 100644
File size: 4.4 KB
Line 
1/* $Id: config_shared.c,v 1.96 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_shared.c - attributes-based configuration functionsc
24 *
25 * DESCRIPTION
26 *   Attribtues are used by the agent and libdspam to control configuration
27 *   management. The included functions perform various operations on the
28 *   configuration structures supplied.
29 *
30 *   Because these functions are used by libdspam, they are prefixed with _ds_
31 */
32
33#ifdef HAVE_CONFIG_H
34#include <auto-config.h>
35#endif
36
37#include <stdio.h>
38#include <errno.h>
39#include <stdlib.h>
40#include <string.h>
41
42#include "config_shared.h"
43#include "error.h"
44#include "language.h"
45#include "libdspam.h"
46
47attribute_t _ds_find_attribute(config_t config, const char *key) {
48  int i;
49
50#ifdef VERBOSE
51  LOGDEBUG("find attribute '%s'", key);
52#endif
53
54  if (config == NULL) {
55#ifdef VERBOSE
56    LOGDEBUG("_ds_find_attribute(): NULL config");
57#endif
58    return NULL;
59  }
60
61  for(i=0;config[i];i++) {
62    attribute_t attr = config[i];
63    if (!strcasecmp(attr->key, key)) {
64#ifdef VERBOSE
65      LOGDEBUG(" -> found attribute '%s' with value '%s'", attr->key, attr->value);
66#endif
67      return attr;
68    }
69  }
70
71  return NULL;
72}
73
74int _ds_add_attribute(config_t config, const char *key, const char *val) {
75  attribute_t attr;
76
77#ifdef VERBOSE
78  LOGDEBUG("add attribute '%s' with value '%s'", key, val);
79#endif
80
81  attr = _ds_find_attribute(config, key);
82  if (!attr) {
83    int i;
84    for(i=0;config[i];i++) { }
85    config[i+1] = 0;
86    config[i] = malloc(sizeof(struct attribute));
87    if (!config[i]) {
88      LOG(LOG_CRIT, ERR_MEM_ALLOC);
89      return EUNKNOWN;
90    }
91    attr = config[i];
92  } else {
93    while(attr->next != NULL) {
94      attr = attr->next;
95    }
96    attr->next = malloc(sizeof(struct attribute));
97    if (!attr->next) {
98      LOG(LOG_CRIT, ERR_MEM_ALLOC);
99      return EUNKNOWN;
100    }
101    attr = attr->next;
102  }
103  attr->key = strdup(key);
104  attr->value = strdup(val);
105  attr->next = NULL;
106
107  return 0;
108}
109
110int _ds_overwrite_attribute(config_t config, const char *key, const char *val) {
111  attribute_t attr;
112
113#ifdef VERBOSE
114  LOGDEBUG("overwrite attribute '%s' with value '%s'", key, val);
115#endif
116
117  attr = _ds_find_attribute(config, key);
118  if (attr == NULL) {
119    return _ds_add_attribute(config, key, val);
120  }
121
122  free(attr->value);
123  attr->value = strdup(val);
124
125  return 0;
126}
127
128char *_ds_read_attribute(config_t config, const char *key) {
129#ifdef VERBOSE
130  LOGDEBUG("read attribute '%s'", key);
131#endif
132  attribute_t attr = _ds_find_attribute(config, key);
133
134  if (!attr) {
135#ifdef VERBOSE
136    LOGDEBUG(" -> read: not found attribute '%s'", key);
137#endif
138    return NULL;
139  }
140
141#ifdef VERBOSE
142  LOGDEBUG(" -> read attribute '%s' with value '%s'", key, attr->value);
143#endif
144  return attr->value;
145}
146
147int _ds_match_attribute(config_t config, const char *key, const char *val) {
148#ifdef VERBOSE
149  LOGDEBUG("match attribute '%s' with value '%s'", key, val);
150#endif
151  attribute_t attr;
152
153  attr = _ds_find_attribute(config, key);
154  if (!attr) {
155#ifdef VERBOSE
156    LOGDEBUG(" -> match: not found attribute '%s'", key);
157#endif
158    return 0;
159  }
160
161  while(strcasecmp(attr->value, val) && attr->next != NULL)
162    attr = attr->next;
163
164  if (!strcasecmp(attr->value, val)) {
165#ifdef VERBOSE
166    LOGDEBUG(" -> matched attribute '%s' with value '%s'", key, val);
167#endif
168    return 1;
169  }
170
171#ifdef VERBOSE
172  LOGDEBUG(" -> match: not found attribute '%s' with value '%s'", key, val);
173#endif
174  return 0;
175}
176
177void _ds_destroy_config(config_t config) {
178#ifdef VERBOSE
179  LOGDEBUG("destroying/freeing configuration");
180#endif
181  attribute_t x, y;
182  int i;
183
184  for(i=0;config[i];i++) {
185    x = config[i];
186
187    while(x) {
188      y = x->next;
189      free(x->key);
190      free(x->value);
191      free(x);
192      x = y;
193    }
194  }
195  free(config);
196  return;
197}
Note: See TracBrowser for help on using the repository browser.