File: config_shared.c

package info (click to toggle)
dspam 3.10.1+dfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,656 kB
  • sloc: ansic: 26,034; sh: 12,546; perl: 5,469; makefile: 690; sql: 379
file content (197 lines) | stat: -rw-r--r-- 4,461 bytes parent folder | download
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
/* $Id: config_shared.c,v 1.96 2011/06/28 00:13:48 sbajic Exp $ */

/*
 DSPAM
 COPYRIGHT (C) 2002-2011 DSPAM PROJECT

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
 published by the Free Software Foundation, either version 3 of the
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

/*
 * config_shared.c - attributes-based configuration functionsc
 *
 * DESCRIPTION
 *   Attribtues are used by the agent and libdspam to control configuration
 *   management. The included functions perform various operations on the
 *   configuration structures supplied.
 *
 *   Because these functions are used by libdspam, they are prefixed with _ds_
 */

#ifdef HAVE_CONFIG_H
#include <auto-config.h>
#endif

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include "config_shared.h"
#include "error.h"
#include "language.h"
#include "libdspam.h"

attribute_t _ds_find_attribute(config_t config, const char *key) {
  int i;

#ifdef VERBOSE
  LOGDEBUG("find attribute '%s'", key);
#endif

  if (config == NULL) {
#ifdef VERBOSE
    LOGDEBUG("_ds_find_attribute(): NULL config");
#endif
    return NULL;
  }

  for(i=0;config[i];i++) {
    attribute_t attr = config[i];
    if (!strcasecmp(attr->key, key)) {
#ifdef VERBOSE
      LOGDEBUG(" -> found attribute '%s' with value '%s'", attr->key, attr->value);
#endif
      return attr;
    }
  }

  return NULL;
}

int _ds_add_attribute(config_t config, const char *key, const char *val) {
  attribute_t attr;

#ifdef VERBOSE
  LOGDEBUG("add attribute '%s' with value '%s'", key, val);
#endif

  attr = _ds_find_attribute(config, key);
  if (!attr) {
    int i;
    for(i=0;config[i];i++) { }
    config[i+1] = 0;
    config[i] = malloc(sizeof(struct attribute));
    if (!config[i]) {
      LOG(LOG_CRIT, ERR_MEM_ALLOC);
      return EUNKNOWN;
    }
    attr = config[i];
  } else {
    while(attr->next != NULL) {
      attr = attr->next;
    }
    attr->next = malloc(sizeof(struct attribute));
    if (!attr->next) {
      LOG(LOG_CRIT, ERR_MEM_ALLOC);
      return EUNKNOWN;
    }
    attr = attr->next;
  }
  attr->key = strdup(key);
  attr->value = strdup(val);
  attr->next = NULL;

  return 0;
}

int _ds_overwrite_attribute(config_t config, const char *key, const char *val) {
  attribute_t attr;

#ifdef VERBOSE
  LOGDEBUG("overwrite attribute '%s' with value '%s'", key, val);
#endif

  attr = _ds_find_attribute(config, key);
  if (attr == NULL) {
    return _ds_add_attribute(config, key, val);
  }

  free(attr->value);
  attr->value = strdup(val);

  return 0;
}

char *_ds_read_attribute(config_t config, const char *key) {
#ifdef VERBOSE
  LOGDEBUG("read attribute '%s'", key);
#endif
  attribute_t attr = _ds_find_attribute(config, key);

  if (!attr) {
#ifdef VERBOSE
    LOGDEBUG(" -> read: not found attribute '%s'", key);
#endif
    return NULL;
  }

#ifdef VERBOSE
  LOGDEBUG(" -> read attribute '%s' with value '%s'", key, attr->value);
#endif
  return attr->value;
}

int _ds_match_attribute(config_t config, const char *key, const char *val) {
#ifdef VERBOSE
  LOGDEBUG("match attribute '%s' with value '%s'", key, val);
#endif
  attribute_t attr;

  attr = _ds_find_attribute(config, key);
  if (!attr) {
#ifdef VERBOSE
    LOGDEBUG(" -> match: not found attribute '%s'", key);
#endif
    return 0;
  }

  while(strcasecmp(attr->value, val) && attr->next != NULL)
    attr = attr->next;

  if (!strcasecmp(attr->value, val)) {
#ifdef VERBOSE
    LOGDEBUG(" -> matched attribute '%s' with value '%s'", key, val);
#endif
    return 1;
  }

#ifdef VERBOSE
  LOGDEBUG(" -> match: not found attribute '%s' with value '%s'", key, val);
#endif
  return 0;
}

void _ds_destroy_config(config_t config) {
#ifdef VERBOSE
  LOGDEBUG("destroying/freeing configuration");
#endif
  attribute_t x, y;
  int i;

  for(i=0;config[i];i++) {
    x = config[i];

    while(x) {
      y = x->next;
      free(x->key);
      free(x->value);
      free(x);
      x = y;
    }
  }
  free(config);
  return;
}