int parse_header(FILE *f, llist *l) { msg_header h; char s[MAX_FBUFFER_SIZE]; if (!feof(f)) { fgets((char *)s, MAX_FBUFFER_SIZE, f); strchop(s); } while ((strcmp(s,"")!=0) && (!feof(f))) { if (strstr(s, "From: ")) { char *tmp = strstr(s,": ")+2; if (tmp) strncpy(h.from, tmp, MAX_HINFO_SIZE); } else if (strstr(s, "Subject: ")) { char *tmp = strstr(s,": ")+2; if (tmp) strncpy(h.subject, tmp, MAX_HINFO_SIZE); } else if (strstr(s, "To: ")) { char *tmp = strstr(s,": ")+2; if (tmp) strncpy(h.to, tmp, MAX_HINFO_SIZE); } else if (strstr(s, "Date: ")) { char *tmp = strstr(s,": ")+2; if (tmp) strncpy(h.date, tmp, MAX_HINFO_SIZE); } fgets((char *)s, MAX_FBUFFER_SIZE, f); strchop(s); } h.body_offset = ftell(f); return(lpush(l, &h)); }
llist *read_mbox(char *fname) { FILE *f; llist *headers; char *buf = (char *) malloc(MAX_FBUFFER_SIZE); f = fopen(fname, "r"); if (!f) return(NULL); headers = lmake(sizeof(msg_header)); if (!headers) return(NULL); while (!feof(f)) { fgets(buf, MAX_FBUFFER_SIZE, f); strchop(buf); if (ishead(buf)) { parse_header(f, headers); /* put_it("found header %d", headers->length);*/ } } fclose(f); return(headers); }
int basenamecpy (const char * str1, char * str2, int str2len) { char *p; if (!str1 || !strlen(str1)) return 0; if (strlen(str1) >= str2len) return 0; if (!str2) return 0; p = (char *)str1 + (strlen(str1) - 1); while (*--p != '/' && p != str1) continue; if (p != str1) p++; strncpy(str2, p, str2len); str2[str2len - 1] = '\0'; strchop(str2); return strlen(str2); }
void String::strip(const char *chars) { size_t len = strtrim(chars, getText(), getLength()); if(!len) { setLength(len); return; } setLength(strchop(chars, getText(), len)); }
int get_word (char * sentence, char ** word) { char * p; int len; int skip = 0; if (word) *word = NULL; while (*sentence == ' ') { sentence++; skip++; } if (*sentence == '\0') return 0; p = sentence; while (*p != ' ' && *p != '\0') p++; len = (int) (p - sentence); if (!word) return skip + len; *word = MALLOC(len + 1); if (!*word) { condlog(0, "get_word : oom"); return 0; } strncpy(*word, sentence, len); strchop(*word); condlog(4, "*word = %s, len = %i", *word, len); if (*p == '\0') return 0; return skip + len; }
int read_config(char *file, srv_t * srv) { FILE *fp; char s[LINEBUF], *cp, *sp, pluginname[256]; char section = 0, *dbname = 0, *dbload = 0; unsigned int n = 0; long lval; int i; plugin_t *plugins, *pl = 0; dback_t *dbp = 0; const conf_com_t *ccp; spocp_result_t r; /* * should never be necessary if (!srv->root) srv->root = ruleset_new(0); */ if (!srv->root->db) srv->root->db = db_new(); plugins = srv->plugin; if ((fp = fopen(file, "r")) == NULL) { traceLog(LOG_ERR, "Could not find or open the configuration file \"%s\"", file); return 0; } while (fgets(s, LINEBUF, fp)) { n++; rmcrlf(s); if (*s == 0 || *s == '#') continue; /* * New section */ if (*s == '[') { cp = find_balancing(s + 1, '[', ']'); if (cp == 0) { traceLog(LOG_ERR, err_msg, n, "Section specification"); return 0; } *cp = 0; sp = s + 1; if (strcasecmp(sp, "server") == 0) section = SYSTEM; else if (strcasecmp(sp, "dback") == 0) section = DBACK; else { section = PLUGIN; strlcpy(pluginname, sp, sizeof( pluginname)); pl = 0; } continue; } /* * Within a section The directives are of the form: * key *SP * "=" *SP val *(*SP val) * val = 1*nonspacechar / '"' char '"' */ rm_lt_sp(s, 1); /* remove leading and trailing blanks */ /* * empty line or comment */ if (*s == 0 || *s == '#') continue; cp = strchr(s, '='); if (cp == 0) { traceLog(LOG_ERR, err_msg, n, "syntax error"); continue; } sp = cp; for (*cp++ = '\0'; *cp && (*cp == ' ' || *cp == '\t'); cp++) *cp = '\0'; for (sp--; sp >= s && (*sp == ' ' || *sp == '\t'); sp--) *sp = '\0'; /* * no key, not good */ if (*s == '\0') continue; switch (section) { case SYSTEM: for (i = 1; keyword[i]; i++) if (strcasecmp(keyword[i], s) == 0) break; if (keyword[i] == 0) { #ifdef HAVE_SASL if((strncmp("sasl_", s, 5) == 0)) add_overflow_directive(s, cp); else #endif traceLog(LOG_ERR, err_msg, n, "Unknown keyword"); continue; } switch (i) { case RULEFILE: if (srv->rulefile) free(srv->rulefile); srv->rulefile = Strdup(cp); break; case CERTIFICATE: if (srv->certificateFile) free(srv->certificateFile); srv->certificateFile = Strdup(cp); break; case PRIVATEKEY: if (srv->privateKey) free(srv->privateKey); srv->privateKey = Strdup(cp); break; case CALIST: if (srv->caList) free(srv->caList); srv->caList = Strdup(cp); break; case DHFILE: if (srv->dhFile) free(srv->dhFile); srv->dhFile = Strdup(cp); break; case ENTROPYFILE: if (srv->SslEntropyFile) free(srv->SslEntropyFile); srv->SslEntropyFile = Strdup(cp); break; case PASSWD: if (srv->passwd) free(srv->passwd); srv->passwd = Strdup(cp); break; case LOGGING: if (srv->logfile) free(srv->logfile); srv->logfile = Strdup(cp); break; case TIMEOUT: if (numstr(cp, &lval) == SPOCP_SUCCESS) { if (lval >= 0 && lval <= YEAR) srv->timeout = (unsigned int) lval; else { traceLog(LOG_ERR, err_msg, n, "Value out of range"); srv->timeout = DEFAULT_TIMEOUT; } } else { traceLog(LOG_ERR, err_msg, n, "Non numeric value"); srv->timeout = DEFAULT_TIMEOUT; } break; case UNIXDOMAINSOCKET: if (srv->uds) free(srv->uds); srv->uds = Strdup(cp); break; case PORT: if (numstr(cp, &lval) == SPOCP_SUCCESS) { if (lval > 0L && lval < 65536) { srv->port = (unsigned int) lval; } else { traceLog(LOG_ERR, err_msg, n, "Number out of range"); srv->port = DEFAULT_PORT; } } else { traceLog(LOG_ERR, err_msg, n, "Non numeric value"); } break; case NTHREADS: if (numstr(cp, &lval) == SPOCP_SUCCESS) { if (lval <= 0) { traceLog(LOG_ERR, err_msg, n, "Value out of range"); return 0; } else { int level = (int) lval; srv->threads = level; } } else { traceLog(LOG_ERR, err_msg, n, "Non numeric specification"); return 0; } break; case SSLVERIFYDEPTH: if (numstr(cp, &lval) == SPOCP_SUCCESS) { if (lval > 0L) { srv->sslverifydepth = (unsigned int) lval; } else { traceLog(LOG_ERR, err_msg, n, "number out of range"); srv->sslverifydepth = 0; } } else { traceLog(LOG_ERR, err_msg, n, "Non numeric value"); } break; case PIDFILE: if (srv->pidfile) Free(srv->pidfile); srv->pidfile = Strdup(cp); break; case MAXCONN: if (numstr(cp, &lval) == SPOCP_SUCCESS) { if (lval > 0L) { srv->nconn = (unsigned int) lval; } else { traceLog(LOG_ERR, err_msg, n, "Number out of range"); srv->sslverifydepth = 0; } } else { traceLog(LOG_ERR, err_msg, n, "Non numeric value"); } break; #ifdef HAVE_SSL case CLIENTCERT: if (strcasecmp(cp, "none") == 0) srv->clientcert = NONE; else if (strcasecmp(cp, "demand") == 0) srv->clientcert = DEMAND; else if (strcasecmp(cp, "hard") == 0) srv->clientcert = HARD; break; #endif case NAME: if (srv->name) Free(srv->name); srv->name = Strdup(cp); break; } break; case PLUGIN: if (pl == 0) { if (strcmp(s, "load") != 0) { traceLog(LOG_ERR, err_msg, n, "First directive in plugin sector has to be \"load\""); section = 0; } if ((pl = plugin_load(plugins, pluginname, cp)) == 0) section = 0; else { /* * The last one is placed last */ for (; pl->next; pl = pl->next); } if (plugins == 0) plugins = pl; } else { if (strcmp(s, "poolsize") == 0) { if (numstr(cp, &lval) == SPOCP_SUCCESS) { if (lval <= 0) { traceLog(LOG_ERR, err_msg, n, "Value out of range"); } else { int level = (int) lval; if (pl->dyn == 0) pl->dyn = pdyn_new (level); if (pl->dyn->size == 0) pl->dyn->size = level; } } else { traceLog(LOG_ERR, err_msg, n, "Non numeric specification"); } } else if (strcmp(s, "cachetime") == 0) { if (plugin_add_cachedef(pl, cp) == FALSE ) traceLog(LOG_ERR, err_msg, n, "Cachetime def"); } else if (pl->ccmds == 0) { /* No * directives * allowed */ traceLog(LOG_ERR, err_msg, n, "Directive where there should not be one"); } else { for (ccp = pl->ccmds; ccp; ccp++) { int np=0, j; char **arr; arr = strchop(cp,&np); for (j=0; j<np; j++) traceLog(LOG_ERR, "%s:%s", cp, arr[j]); if (strcmp(ccp->name, s) == 0) { r = ccp->func(&pl-> conf, ccp-> cmd_data, np, arr); if (r != SPOCP_SUCCESS) { traceLog (LOG_ERR, err_msg, n, ccp-> errmsg); } charmatrix_free( arr ); break; } } if (ccp == 0) { traceLog(LOG_ERR,err_msg, n, "Unknown directive"); } } } break; case DBACK: if (dbp == 0) { if (strcmp(s, "name") == 0) { dbname = Strdup(cp); if (dbname && dbload) { dbp = dback_load(dbname, dbload); free(dbname); free(dbload); } } else if (strcmp(s, "load") == 0) { dbload = Strdup(cp); if (dbname && dbload) { dbp = dback_load(dbname, dbload); free(dbname); free(dbload); } } else traceLog(LOG_ERR,err_msg, n, "Unknown directive"); } else { for (ccp = dbp->ccmds; ccp && *ccp->name; ccp++) { if (strcmp(ccp->name, s) == 0) { r = ccp->func(&dbp->conf, ccp->cmd_data, 1, &cp); if (r != SPOCP_SUCCESS) { traceLog(LOG_ERR,err_msg, n, ccp->errmsg); } break; } } if (ccp == 0) { traceLog(LOG_ERR,err_msg, n, "Unknown directive"); } } break; } } fclose(fp); if (srv->pidfile == 0) srv->pidfile = Strdup("spocd.pid"); if (srv->timeout == 0) srv->timeout = DEFAULT_TIMEOUT; if (srv->threads == 0) srv->threads = DEFAULT_NTHREADS; if (srv->sslverifydepth == 0) srv->sslverifydepth = DEFAULT_SSL_DEPTH; srv->plugin = plugins; srv->dback = dbp; return 1; }