File: read_config.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 (382 lines) | stat: -rw-r--r-- 9,784 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* $Id: read_config.c,v 1.197 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/>.

*/

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

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#ifdef SPLIT_CONFIG
#include <dirent.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <string.h>

#include "config_shared.h"
#include "read_config.h"
#include "config.h"
#include "error.h"
#include "language.h"
#include "libdspam.h"
#include "pref.h"
#include "util.h"

#ifdef SPLIT_CONFIG
long dirread(const char *path, config_t *attrib, long num_root);
long fileread(const char *path, config_t *attrib, long num_root);
#endif

static char *next_normal_token(char **p)
{
  char *start = *p;
  while (**p) {
    if (**p == ' ' || **p == '\t') {
      **p = 0;
      *p += 1;
      break;
    }
    *p += 1;
  }
  return start;
}

static char *next_quoted_token(char **p)
{
  char *start = *p;
  while (**p) {
    if (**p == '\"') {
      **p = 0;
      *p += 1;
      break;
    }
    *p += 1;
  }
  return start;
}

static char *tokenize(char *text, char **next)
{
  /* Initialize */
  if (text)
    *next = text;

  while (**next) {
    /* Skip leading whitespace */
    if (**next == ' ' || **next == '\t') {
      *next += 1;
      continue;
    }

    /* Strip off one token */
    if (**next == '\"') {
      *next += 1;
      return next_quoted_token(next);
    } else
      return next_normal_token(next);
  }

  return NULL;
}

#ifdef SPLIT_CONFIG
// Read the files in the directory and pass it to fileread
// or if it is a file, pass it to fileread.
long dirread(const char *path, config_t *attrib, long num_root) {
  DIR *dir_p;
  char *fulldir = NULL;
  struct dirent *dir_entry_p;

  if (path == NULL) return 0;

  // Strip "\n"
  char *ptr = strrchr(path, '\n');
  if (ptr)
    *ptr = '\0';

  if ((dir_p = opendir(path)) == NULL) {
    LOG(LOG_ERR, ERR_IO_FILE_OPEN, path, strerror(errno));
    return 0;
  }

  if ((dir_p = opendir(path))) {
    while((dir_entry_p = readdir(dir_p)))
    {
      // We don't need the . and ..
      if (strcmp(dir_entry_p->d_name, ".") == 0 ||
          strcmp(dir_entry_p->d_name, "..") == 0)
        continue;

      int n = strlen(dir_entry_p->d_name);

      // only use files which end in .conf:
      if (n < 5) continue;
      if (strncmp(dir_entry_p->d_name + n - 5, ".conf", 5) != 0) continue;

      int m = strlen(path);

      fulldir = (char *)malloc(n+m+2);
      if (fulldir == NULL) {
        LOG(LOG_CRIT, ERR_MEM_ALLOC);
        return 0;
      }

      strcpy(fulldir, (char *)path);
      strcat(fulldir, "/");
      strcat(fulldir, dir_entry_p->d_name);
      num_root = fileread((const char *)fulldir, attrib, num_root);
      free(fulldir);
    }
    closedir(dir_p);
  } else {
    // Could be a file.
    return fileread((const char *)path, attrib, num_root);
  }

  return num_root;
}

// Read the file and check if there is an Include directive, if so then pass
// it to dirread.
long fileread(const char *path, config_t *attrib, long num_root) {
  config_t ptr;
#else
config_t read_config(const char *path) {
  config_t attrib, ptr;
#endif
  FILE *file;
#ifdef SPLIT_CONFIG
  long attrib_size = 128;
#else
  long attrib_size = 128, num_root = 0;
#endif
  char buffer[1024];
  char *a, *c, *v, *bufptr = buffer;

#ifndef SPLIT_CONFIG
  attrib = calloc(1, attrib_size*sizeof(attribute_t));
  if (attrib == NULL) {
    LOG(LOG_CRIT, ERR_MEM_ALLOC);
    return NULL;
  }
#endif

  if (path == NULL)
    file = fopen(CONFIG_DEFAULT, "r");
  else
    file = fopen(path, "r");

  if (file == NULL) {
#ifdef SPLIT_CONFIG
    if (path == NULL) {
      LOG(LOG_ERR, ERR_IO_FILE_OPEN, CONFIG_DEFAULT, strerror(errno));
    } else {
      LOG(LOG_ERR, ERR_IO_FILE_OPEN, path, strerror(errno));
    }
    return 0;
#else
    LOG(LOG_ERR, ERR_IO_FILE_OPEN, CONFIG_DEFAULT, strerror(errno));
    free(attrib);
    return NULL;
#endif
  }

  while(fgets(buffer, sizeof(buffer), file)!=NULL) {

    chomp(buffer);

    /* Remove comments */
    if ((c = strchr(buffer, '#')) || (c = strchr(buffer, ';')))
      *c = 0;

    /* Parse attribute name */
    if (!(a = tokenize(buffer, &bufptr)))
      continue; /* Ignore whitespace-only lines */

    while ((v = tokenize(NULL, &bufptr)) != NULL) {
#ifdef SPLIT_CONFIG
      // Check for include directive
      if (strcmp(a, "Include") == 0) {
        // Give v (value) to dirraed
        num_root = dirread(v, attrib, num_root);
      } else {
        if (_ds_find_attribute((*attrib), a)!=NULL) {
          _ds_add_attribute((*attrib), a, v);
        }
        else {
          num_root++;
          if (num_root >= attrib_size) {
            attrib_size *=2;
            ptr = realloc((*attrib), attrib_size*sizeof(attribute_t));
            if (ptr)
              *attrib = ptr;
            else {
              LOG(LOG_CRIT, ERR_MEM_ALLOC);
              fclose(file);
              return 0;
            }
          }
          _ds_add_attribute((*attrib), a, v);
        }
#else
      if (_ds_find_attribute(attrib, a)!=NULL) {
        _ds_add_attribute(attrib, a, v);
      }
      else {
        num_root++;
        if (num_root >= attrib_size) {
          attrib_size *=2;
          ptr = realloc(attrib, attrib_size*sizeof(attribute_t));
          if (ptr)
            attrib = ptr;
          else {
            LOG(LOG_CRIT, ERR_MEM_ALLOC);
            fclose(file);
            return NULL;
          }
        }
        _ds_add_attribute(attrib, a, v);
#endif
      }
    }
  }

  fclose(file);

#ifdef SPLIT_CONFIG
  return num_root;
}

config_t read_config(const char *path) {
  config_t attrib;
  long attrib_size = 128, num_root = 0;

  attrib = calloc(1, attrib_size*sizeof(attribute_t));
  if (attrib == NULL) {
    LOG(LOG_CRIT, ERR_MEM_ALLOC);
    return NULL;
  }

  if (fileread(path, &attrib, num_root) == 0)
    return NULL;
#else
  ptr = realloc(attrib, ((num_root+1)*sizeof(attribute_t))+1);
  if (ptr)
    return ptr;
  LOG(LOG_CRIT, ERR_MEM_ALLOC);
#endif

  return attrib;
}

int configure_algorithms(DSPAM_CTX *CTX) {
  if (_ds_read_attribute(agent_config, "Algorithm"))
    CTX->algorithms = 0;
                                                                                
  if (_ds_match_attribute(agent_config, "Algorithm", "graham"))
    CTX->algorithms |= DSA_GRAHAM;
                                                                                
  if (_ds_match_attribute(agent_config, "Algorithm", "burton"))
    CTX->algorithms |= DSA_BURTON;
                                                                                
  if (_ds_match_attribute(agent_config, "Algorithm", "robinson"))
    CTX->algorithms |= DSA_ROBINSON;

  if (_ds_match_attribute(agent_config, "Algorithm", "naive"))
    CTX->algorithms |= DSA_NAIVE;
                                                                                
  if (_ds_match_attribute(agent_config, "PValue", "robinson"))
    CTX->algorithms |= DSP_ROBINSON;
  else if (_ds_match_attribute(agent_config, "PValue", "markov"))
    CTX->algorithms |= DSP_MARKOV;
  else
    CTX->algorithms |= DSP_GRAHAM;

  if (_ds_match_attribute(agent_config, "Tokenizer", "word")) 
    CTX->tokenizer = DSZ_WORD;
  else if (_ds_match_attribute(agent_config, "Tokenizer", "chain") ||
           _ds_match_attribute(agent_config, "Tokenizer", "chained"))
    CTX->tokenizer = DSZ_CHAIN;
  else if (_ds_match_attribute(agent_config, "Tokenizer", "sbph"))
    CTX->tokenizer = DSZ_SBPH;
  else if (_ds_match_attribute(agent_config, "Tokenizer", "osb"))
    CTX->tokenizer = DSZ_OSB;
 
  if (_ds_match_attribute(agent_config, "Algorithm", "chi-square"))
  {
    if (CTX->algorithms != 0 && CTX->algorithms != DSP_ROBINSON) {
      LOG(LOG_WARNING, "Warning: Chi-Square algorithm enabled with other algorithms. False positives may ensue.");
    }
    CTX->algorithms |= DSA_CHI_SQUARE;
  }

  return 0;
}

agent_pref_t pref_config(void)
{
  agent_pref_t PTX = malloc(sizeof(agent_attrib_t)*PREF_MAX);
  agent_pref_t ptr;
  attribute_t attrib;
  char *p, *q;
  char *ptrptr = NULL;
  int i = 0;

  if (PTX == NULL) {
    LOG(LOG_CRIT, ERR_MEM_ALLOC);
    return NULL;
  }
  PTX[0] = NULL;

  /* Apply default preferences from dspam.conf */
                                                                                
  attrib = _ds_find_attribute(agent_config, "Preference");
                                                                                
  LOGDEBUG("Loading preferences from dspam.conf");
                                                                                
  while(attrib != NULL) {
    char *pcopy = strdup(attrib->value);
                                                                              
    p = strtok_r(pcopy, "=", &ptrptr);
    if (p == NULL) {
      free(pcopy);
      continue;
    }
    q = p + strlen(p)+1;

    PTX[i] = _ds_pref_new(p, q);
    PTX[i+1] = NULL;

    i++;
    attrib = attrib->next;
    free(pcopy);
  }

  ptr = realloc(PTX, sizeof(agent_attrib_t)*(i+1));
  if (ptr)
    return ptr;
  
  LOG(LOG_CRIT, ERR_MEM_ALLOC);
  return PTX;
}