int set_libdspam_attributes(DSPAM_CTX *CTX) { attribute_t t; int i, ret = 0; char *profile; t = _ds_find_attribute(agent_config, "IgnoreHeader"); while(t) { ret += dspam_addattribute(CTX, t->key, t->value); t = t->next; } profile = _ds_read_attribute(agent_config, "DefaultProfile"); for(i=0;agent_config[i];i++) { t = agent_config[i]; while(t) { if (!strncasecmp(t->key, "MySQL", 5) || !strncasecmp(t->key, "PgSQL", 5) || !strncasecmp(t->key, "Ora", 3) || !strncasecmp(t->key, "SQLite", 6) || !strcasecmp(t->key, "LocalMX") || !strncasecmp(t->key, "LDAP", 4) || !strncasecmp(t->key, "Storage", 7) || !strncasecmp(t->key, "Processor", 9) || !strncasecmp(t->key, "Hash", 4)) { if (profile == NULL || profile[0] == 0) { ret += dspam_addattribute(CTX, t->key, t->value); } else if (strchr(t->key, '.')) { if (!strcasecmp((strchr(t->key, '.')+1), profile)) { char *x = strdup(t->key); char *y = strchr(x, '.'); y[0] = 0; ret += dspam_addattribute(CTX, x, t->value); free(x); } } } t = t->next; } } ret += configure_algorithms(CTX); return ret; }
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; 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; }
config_t read_config(const char *path) { config_t attrib, ptr; FILE *file; long attrib_size = 128, num_root = 0; char buffer[1024]; char *a, *c, *v, *bufptr = buffer; attrib = calloc(1, attrib_size*sizeof(attribute_t)); if (attrib == NULL) { LOG(LOG_CRIT, ERR_MEM_ALLOC); return NULL; } if (path == NULL) file = fopen(CONFIG_DEFAULT, "r"); else file = fopen(path, "r"); if (file == NULL) { LOG(LOG_ERR, ERR_IO_FILE_OPEN, CONFIG_DEFAULT, strerror(errno)); free(attrib); return NULL; } while(fgets(buffer, sizeof(buffer), file)!=NULL) { chomp(buffer); /* Remove comments */ c = strchr(buffer, '#'); if (!c) c = strchr(buffer, ';'); if (c) *c = 0; /* Parse attribute name */ a = tokenize(buffer, &bufptr); if (!a) continue; /* Ignore whitespace-only lines */ while ((v = tokenize(NULL, &bufptr)) != NULL) { 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); } _ds_add_attribute(attrib, a, v); } } } fclose(file); ptr = realloc(attrib, ((num_root+1)*sizeof(attribute_t))+1); if (ptr) return ptr; LOG(LOG_CRIT, ERR_MEM_ALLOC); return attrib; }
int _ds_init_storage (DSPAM_CTX * CTX, void *dbh) { struct _sqlite_drv_storage *s; FILE *file; char buff[1024]; char filename[MAX_FILENAME_LENGTH]; char *err=NULL; struct stat st; int noexist; buff[0] = 0; if (CTX == NULL) return EINVAL; if (!CTX->home) { LOG(LOG_ERR, ERR_AGENT_DSPAM_HOME); return EINVAL; } if (CTX->flags & DSF_MERGED) { LOG(LOG_ERR, ERR_DRV_NO_MERGED); return EINVAL; } /* don't init if we're already initted */ if (CTX->storage != NULL) { LOGDEBUG ("_ds_init_storage: storage already initialized"); return EINVAL; } s = malloc (sizeof (struct _sqlite_drv_storage)); if (s == NULL) { LOG (LOG_CRIT, ERR_MEM_ALLOC); return EUNKNOWN; } s->dbh = NULL; s->control_token = 0; s->iter_token = NULL; s->iter_sig = NULL; s->control_token = 0; s->control_sh = 0; s->control_ih = 0; s->dbh_attached = (dbh) ? 1 : 0; if (CTX->group == NULL || CTX->group[0] == 0) _ds_userdir_path (filename, CTX->home, CTX->username, "sdb"); else _ds_userdir_path (filename, CTX->home, CTX->group, "sdb"); _ds_prepare_path_for (filename); noexist = stat(filename, &st); if (dbh) s->dbh = dbh; else s->dbh = sqlite_open(filename, 0660, &err); if (s->dbh == NULL) { LOGDEBUG ("_ds_init_storage: sqlite_open: unable to initialize database: %s", err); return EUNKNOWN; } /* Commit timeout of 20 minutes */ sqlite_busy_timeout(s->dbh, 1000 * 60 * 20); /* Create database objects */ if (noexist) { sqlite_exec(s->dbh, "create table dspam_token_data (token char(20) primary key, " "spam_hits int, innocent_hits int, last_hit date)", NULL, NULL, &err); sqlite_exec(s->dbh, "create index id_token_data_02 on dspam_token_data" "(innocent_hits)", NULL, NULL, &err); sqlite_exec(s->dbh, "create table dspam_signature_data (" "signature char(128) primary key, data blob, created_on date)", NULL, NULL, &err); sqlite_exec(s->dbh, "create table dspam_stats (dspam_stat_id int primary key, " "spam_learned int, innocent_learned int, " "spam_misclassified int, innocent_misclassified int, " "spam_corpusfed int, innocent_corpusfed int, " "spam_classified int, innocent_classified int)", NULL, NULL, &err); } if (_ds_read_attribute(CTX->config->attributes, "SQLitePragma")) { char pragma[1024]; attribute_t t = _ds_find_attribute(CTX->config->attributes, "SQLitePragma"); while(t != NULL) { snprintf(pragma, sizeof(pragma), "PRAGMA %s", t->value); if ((sqlite_exec(s->dbh, pragma, NULL, NULL, &err))!=SQLITE_OK) { LOG(LOG_WARNING, "sqlite.pragma function error: %s: %s", err, pragma); _sqlite_drv_query_error (err, pragma); } t = t->next; } } else if (CTX->home) { snprintf(filename, MAX_FILENAME_LENGTH, "%s/sqlite.pragma", CTX->home); file = fopen(filename, "r"); if (file != NULL) { while((fgets(buff, sizeof(buff), file))!=NULL) { chomp(buff); if ((sqlite_exec(s->dbh, buff, NULL, NULL, &err))!=SQLITE_OK) { LOG(LOG_WARNING, "sqlite.pragma function error: %s: %s", err, buff); _sqlite_drv_query_error (err, buff); } } fclose(file); } } CTX->storage = s; s->dir_handles = nt_create (NT_INDEX); s->control_token = 0; s->control_ih = 0; s->control_sh = 0; /* get spam totals on successful init */ if (CTX->username != NULL) { if (_sqlite_drv_get_spamtotals (CTX)) { LOGDEBUG ("unable to load totals. using zero values."); } } else { memset (&CTX->totals, 0, sizeof (struct _ds_spam_totals)); memset (&s->control_totals, 0, sizeof (struct _ds_spam_totals)); } return 0; }