Example #1
0
static int load_config_file_stream ( FILE *f, const char *name )
{
	char buffer[1024];
	int lineno = 1;
	while (fgets(buffer, sizeof(buffer), f)) {
		char *key, *val;
		char orig_line[sizeof(buffer)];
		if (strlen(buffer) > sizeof(buffer) - 4) {
			ERROR_WINDOW("Config file %s has too long line at lineno %d", name, lineno);
			return 1;
		}
		strcpy(orig_line, buffer);
		str_rstrip(orig_line);
		if (parse_cfgfile_line(buffer, &key, &val)) {
			ERROR_WINDOW("Config file %s has syntax error at lineno %d line \"%s\"", name, lineno, orig_line);
			return 1;
		}
		if (key && val) {
			char cleankey[sizeof(buffer)];
			int subopt = separate_key(buffer, cleankey);
			//str_rstrip(buffer);
			//printf("[%d][%s]%s = %s\n", lineno, orig_line, key, val);
			DEBUG("CONFIG: FILE: %s@%d = %s" NL, cleankey, subopt, val);
			if (subopt < -1 || config_set_user(cleankey, subopt, val, 1)) {
				ERROR_WINDOW("Config file %s has invalid option key/value at lineno %d: %s = %s", name, lineno, key, val);
				return 1;
			}
		}
		lineno++;
	}
	return 0;
}
char *
str_trim (char *str)
{
    assert (str != NULL);

    return str_lstrip (str_rstrip(str));
}
Example #3
0
static int parse_cfgfile_line ( char *buffer, char **key, char **value )
{
	char *p;
	*key = *value = NULL;
	while (*buffer == ' ' || *buffer == '\t') buffer++;
	p = strchr(buffer, '#');
	if (p) *p = 0;
	str_rstrip(buffer);
	if (!*buffer) return 0;	// empty line, or only comment in this line
	p = strchr(buffer, '=');
	if (!p) return 1;
	*(p++) = 0;
	while (*p == ' ' || *p == '\t') p++;
	*value = p;
	*key = buffer;
	//str_rstrip(p);
	str_rstrip(buffer);
	return !*p || !*buffer;
}
Example #4
0
gboolean server_handle(GIOChannel *source, GIOCondition cond, gpointer user)
{
        int fd;
        struct layout *l = (void *)user;
        struct named_element *e;
        struct ui_msg *msg;
        int ret;

        fd = g_io_channel_unix_get_fd(source);

        ret = ui_get_msg(fd, &msg);
        if (ret < 0) {
                printf("Failed to receive message: %s\n", strerror(-ret));
                return TRUE;
        } else if (ret == 0) {
                printf("Removed client\n");
                close(fd);
                return FALSE;
        }

#if 0
        printf("Setting %s->%s\n", ui_get_msg_name(msg), ui_get_msg_valu(msg));
#endif

        e = get_element(l, ui_get_msg_name(msg));
        if (!e) {
                printf("Unknown element `%s'\n", ui_get_msg_name(msg));
                goto out;
        }

        str_rstrip(ui_get_msg_valu(msg));
        e->update_fn(e, ui_get_msg_valu(msg));
 out:
        free(msg);

        return TRUE;
}
Example #5
0
static const response* message_end(int fd)
{
  const char* hostname;
  const char* tmp;
  ipv4port cmdport;
  ipv4addr ips[MAX_IPS];
  int ip_count;
  int i;
  int offset;
  int result;
  struct timeval tv;
  int sock;
  unsigned long timeout;
  unsigned long connect_timeout;
  unsigned long send_timeout;
  unsigned long maxsize;
  ibuf netin;
  obuf netout;
  struct stat st;
  
  if ((hostname = session_getenv("CLAMAV_HOST")) != 0
      || (hostname = session_getenv("CLAMD_HOST")) != 0) {

    if (fstat(fd, &st) != 0)
      return &resp_internal;
    /* For simplicity, this plugin just sends a single chunk, but each
     * chunk is limited to 2^32 bytes by the protocol. */
    if (st.st_size > 0xffffffffLL) {
      warn1("ClamAV scanning skipped: message larger than chunk size");
      return 0;
    }
    if ((tmp = session_getenv("CLAMAV_MAXSIZE")) != 0
	&& (maxsize = strtoul(tmp, (char**)&tmp, 10)) != 0
	&& *tmp == 0) {
      if (st.st_size > (ssize_t)maxsize){
	warn1("ClamAV scanning skipped: message larger than maximum");
	return 0;
      }
    }

    if (((tmp = session_getenv("CLAMAV_PORT")) == 0
	 && (tmp = session_getenv("CLAMD_PORT")) == 0)
	|| (cmdport = strtoul(tmp, (char**)&tmp, 10)) == 0
	|| *tmp != 0)
      cmdport = 3310;
    if (((tmp = session_getenv("CLAMAV_TIMEOUT")) == 0
	 && (tmp = session_getenv("CLAMD_TIMEOUT")) == 0)
	|| (timeout = strtoul(tmp, (char**)&tmp, 10)) == 0
	|| *tmp != 0)
      timeout = 5000;
    if ((tmp = session_getenv("CLAMAV_CONNECT_TIMEOUT")) == 0
	|| (connect_timeout = strtoul(tmp, (char**)&tmp, 10)) == 0
	|| *tmp != 0)
      connect_timeout = timeout;
    if ((tmp = session_getenv("CLAMAV_SEND_TIMEOUT")) == 0
	|| (send_timeout = strtoul(tmp, (char**)&tmp, 10)) == 0
	|| *tmp != 0)
      send_timeout = timeout;
    if ((ip_count = resolve_ipv4name_n(hostname, ips, MAX_IPS)) <= 0)
      return &resp_no_hostname;

    gettimeofday(&tv, 0);
    offset = (tv.tv_sec ^ tv.tv_usec) % ip_count;
    for (i = 0; i < ip_count; ++i) {
      const ipv4addr* addr = &ips[(i + offset) % ip_count];
      if (lseek(fd, 0, SEEK_SET) != 0)
	return &resp_internal;
      if ((sock = try_connect(addr, cmdport, connect_timeout)) < 0)
	continue;

      if (obuf_init(&netout, sock, 0, 0, 0)) {
	netout.io.timeout = send_timeout;
	result = obuf_puts(&netout, "nINSTREAM\n")
	  && copystream(fd, st.st_size, &netout)
	  && obuf_close(&netout);
	obuf_close(&netout);
	if (result) {
	  if (ibuf_init(&netin, sock, 0, IOBUF_NEEDSCLOSE, 0)) {
	    netin.io.timeout = timeout;
	    result = ibuf_getstr(&netin, &line, LF);
	    ibuf_close(&netin);
	    sock = -1;
	    if (result) {
	      if (memcmp(line.s, "stream: ", 8) == 0) {
		str_lcut(&line, 8);
		str_rstrip(&line);
		if (str_diffs(&line, "OK") == 0)
		  return 0;
		str_splices(&line, 0, 0, "5.7.0 Virus scan failed: ");
		resp_virus.message = line.s;
		return &resp_virus;
	      }
	    }
	  }
	}
      }
      if (sock >= 0)
	close(sock);
    }
  }
  return &resp_no_scan;
}