Exemplo n.º 1
0
ESL_DECLARE(esl_status_t) esl_event_set_body(esl_event_t *event, const char *body)
{
	esl_safe_free(event->body);

	if (body) {
		event->body = DUP(body);
	}
	
	return ESL_SUCCESS;
}
Exemplo n.º 2
0
ESLevent::ESLevent(ESLevent *me)
{
	/* workaround for silly php thing */
	event = me->event;
	mine = me->mine;
	serialized_string = NULL;
	me->event = NULL;
	me->mine = 0;
	esl_safe_free(me->serialized_string);
}
Exemplo n.º 3
0
const char *ESLevent::serialize(const char *format)
{
	this_check("");

	esl_safe_free(serialized_string);
	
	if (!event) {
		return "";
	}

	if (esl_event_serialize(event, &serialized_string, ESL_TRUE) == ESL_SUCCESS) {
		return serialized_string;
	}

	return "";

}
Exemplo n.º 4
0
ESL_DECLARE(esl_status_t) esl_event_add_body(esl_event_t *event, const char *fmt, ...)
{
	int ret = 0;
	char *data;

	va_list ap;
	if (fmt) {
		va_start(ap, fmt);
		ret = esl_vasprintf(&data, fmt, ap);
		va_end(ap);

		if (ret == -1) {
			return ESL_FAIL;
		} else {
			esl_safe_free(event->body);
			event->body = data;
			return ESL_SUCCESS;
		}
	} else {
		return ESL_FAIL;
	}
}
Exemplo n.º 5
0
ESL_DECLARE(esl_status_t) esl_event_serialize(esl_event_t *event, char **str, esl_bool_t encode)
{
	size_t len = 0;
	esl_event_header_t *hp;
	size_t llen = 0, dlen = 0, blocksize = 512, encode_len = 1536, new_len = 0;
	char *buf;
	char *encode_buf = NULL;	/* used for url encoding of variables to make sure unsafe things stay out of the serialized copy */
	int clen = 0;

	if (!event || !event->headers)
		return ESL_FAIL;

	*str = NULL;

	dlen = blocksize * 2;

	if (!(buf = malloc(dlen))) {
		return ESL_FAIL;
	}

	/* go ahead and give ourselves some space to work with, should save a few reallocs */
	if (!(encode_buf = malloc(encode_len))) {
		esl_safe_free(buf);
		return ESL_FAIL;
	}

	/* esl_log_printf(ESL_CHANNEL_LOG, ESL_LOG_INFO, "hit serialized!.\n"); */
	for (hp = event->headers; hp; hp = hp->next) {
		/*
		 * grab enough memory to store 3x the string (url encode takes one char and turns it into %XX)
		 * so we could end up with a string that is 3 times the originals length, unlikely but rather
		 * be safe than destroy the string, also add one for the null.  And try to be smart about using 
		 * the memory, allocate and only reallocate if we need more.  This avoids an alloc, free CPU
		 * destroying loop.
		 */

		if (!strcasecmp(hp->name, "content-length")) {
			clen++;
		}


		new_len = (strlen(hp->value) * 3) + 1;

		if (encode_len < new_len) {
			char *tmp;
			/* esl_log_printf(ESL_CHANNEL_LOG, ESL_LOG_INFO, "Allocing %d was %d.\n", ((strlen(hp->value) * 3) + 1), encode_len); */
			/* we can use realloc for initial alloc as well, if encode_buf is zero it treats it as a malloc */

			/* keep track of the size of our allocation */
			encode_len = new_len;

			if (!(tmp = realloc(encode_buf, encode_len))) {
				/* oh boy, ram's gone, give back what little we grabbed and bail */
				esl_safe_free(buf);
				esl_safe_free(encode_buf);
				return ESL_FAIL;
			}

			encode_buf = tmp;
		}

		/* handle any bad things in the string like newlines : etc that screw up the serialized format */
		if (encode) {
			esl_url_encode(hp->value, encode_buf, encode_len);
		} else {
			esl_snprintf(encode_buf, encode_len, "%s", hp->value);
		}

		llen = strlen(hp->name) + strlen(encode_buf) + 8;

		if ((len + llen) > dlen) {
			char *m;
			dlen += (blocksize + (len + llen));
			if ((m = realloc(buf, dlen))) {
				buf = m;
			} else {
				/* we seem to be out of memory trying to resize the serialize string, give back what we already have and give up */
				esl_safe_free(buf);
				esl_safe_free(encode_buf);
				return ESL_FAIL;
			}
		}

		snprintf(buf + len, dlen - len, "%s: %s\n", hp->name, *encode_buf == '\0' ? "_undef_" : encode_buf);
		len = strlen(buf);
	}

	/* we are done with the memory we used for encoding, give it back */
	esl_safe_free(encode_buf);

	if (event->body) {
		int blen = (int) strlen(event->body);
		llen = blen;

		if (blen) {
			llen += 25;
		} else {
			llen += 5;
		}

		if ((len + llen) > dlen) {
			char *m;
			dlen += (blocksize + (len + llen));
			if ((m = realloc(buf, dlen))) {
				buf = m;
			} else {
				esl_safe_free(buf);
				return ESL_FAIL;
			}
		}
		
		if (blen) {
			if (clen) {
				snprintf(buf + len, dlen - len, "\n%s", event->body);
			} else {
				snprintf(buf + len, dlen - len, "Content-Length: %d\n\n%s", (int)strlen(event->body), event->body);
			
			}
		}
	} else {
		snprintf(buf + len, dlen - len, "\n");
	}


	*str = buf;

	return ESL_SUCCESS;
}
Exemplo n.º 6
0
ESL_DECLARE(esl_status_t) esl_event_serialize(esl_event_t *event, char **str, esl_bool_t encode)
{
	esl_size_t len = 0;
	esl_event_header_t *hp;
	esl_size_t llen = 0, dlen = 0, blocksize = 512, encode_len = 1536, new_len = 0;
	char *buf;
	char *encode_buf = NULL;	/* used for url encoding of variables to make sure unsafe things stay out of the serialized copy */

	*str = NULL;

	dlen = blocksize * 2;

	if (!(buf = malloc(dlen))) {
		abort();
	}

	/* go ahead and give ourselves some space to work with, should save a few reallocs */
	if (!(encode_buf = malloc(encode_len))) {
		abort();
	}

	/* esl_log_printf(ESL_CHANNEL_LOG, ESL_LOG_INFO, "hit serialized!.\n"); */
	for (hp = event->headers; hp; hp = hp->next) {
		/*
		 * grab enough memory to store 3x the string (url encode takes one char and turns it into %XX)
		 * so we could end up with a string that is 3 times the originals length, unlikely but rather
		 * be safe than destroy the string, also add one for the null.  And try to be smart about using 
		 * the memory, allocate and only reallocate if we need more.  This avoids an alloc, free CPU
		 * destroying loop.
		 */

		if (hp->idx) {
			int i;
			new_len = 0;
			for(i = 0; i < hp->idx; i++) {
				new_len += (strlen(hp->array[i]) * 3) + 1;
			}
		} else {
			new_len = (strlen(hp->value) * 3) + 1;
		}

		if (encode_len < new_len) {
			char *tmp;

			/* keep track of the size of our allocation */
			encode_len = new_len;

			if (!(tmp = realloc(encode_buf, encode_len))) {
				abort();
			}

			encode_buf = tmp;
		}

		/* handle any bad things in the string like newlines : etc that screw up the serialized format */


		if (encode) {
			esl_url_encode(hp->value, encode_buf, encode_len);
		} else {
			esl_snprintf(encode_buf, encode_len, "[%s]", hp->value);
		}


		llen = strlen(hp->name) + strlen(encode_buf) + 8;

		if ((len + llen) > dlen) {
			char *m;
			char *old = buf;
			dlen += (blocksize + (len + llen));
			if ((m = realloc(buf, dlen))) {
				buf = m;
			} else {
				buf = old;
				abort();
			}
		}

		esl_snprintf(buf + len, dlen - len, "%s: %s\n", hp->name, *encode_buf == '\0' ? "_undef_" : encode_buf);
		len = strlen(buf);
	}

	/* we are done with the memory we used for encoding, give it back */
	esl_safe_free(encode_buf);

	if (event->body) {
		int blen = (int) strlen(event->body);
		llen = blen;

		if (blen) {
			llen += 25;
		} else {
			llen += 5;
		}

		if ((len + llen) > dlen) {
			char *m;
			char *old = buf;
			dlen += (blocksize + (len + llen));
			if ((m = realloc(buf, dlen))) {
				buf = m;
			} else {
				buf = old;
				abort();
			}
		}

		if (blen) {
			esl_snprintf(buf + len, dlen - len, "Content-Length: %d\n\n%s", blen, event->body);
		} else {
			esl_snprintf(buf + len, dlen - len, "\n");
		}
	} else {
		esl_snprintf(buf + len, dlen - len, "\n");
	}

	*str = buf;

	return ESL_SUCCESS;
}
Exemplo n.º 7
0
static esl_status_t esl_event_base_add_header(esl_event_t *event, esl_stack_t stack, const char *header_name, char *data)
{
	esl_event_header_t *header = NULL;
	esl_ssize_t hlen = -1;
	int exists = 0, fly = 0;
	char *index_ptr;
	int index = 0;
	char *real_header_name = NULL;

	if ((index_ptr = strchr(header_name, '['))) {
		index_ptr++;
		index = atoi(index_ptr);
		real_header_name = DUP(header_name);
		if ((index_ptr = strchr(real_header_name, '['))) {
			*index_ptr++ = '\0';
		}
		header_name = real_header_name;
	}
	
	if (index_ptr || (stack & ESL_STACK_PUSH) || (stack & ESL_STACK_UNSHIFT)) {
		
		if (!(header = esl_event_get_header_ptr(event, header_name)) && index_ptr) {

			header = new_header(header_name);

			if (esl_test_flag(event, ESL_EF_UNIQ_HEADERS)) {
				esl_event_del_header(event, header_name);
			}

			fly++;
		}
		
		if ((header = esl_event_get_header_ptr(event, header_name))) {
			
			if (index_ptr) {
				if (index > -1 && index <= 4000) {
					if (index < header->idx) {
						FREE(header->array[index]);
						header->array[index] = DUP(data);
					} else {
						int i;
						char **m;
					
						m = realloc(header->array, sizeof(char *) * (index + 1));
						esl_assert(m);
						header->array = m;
						for (i = header->idx; i < index; i++) {
							m[i] = DUP("");
						}
						m[index] = DUP(data);
						header->idx = index + 1;
						if (!fly) {
							exists = 1;
						}

						goto redraw;
					}
				}
				goto end;
			} else {
				if ((stack & ESL_STACK_PUSH) || (stack & ESL_STACK_UNSHIFT)) {
					exists++;
					stack &= ~(ESL_STACK_TOP | ESL_STACK_BOTTOM);
				} else {
					header = NULL;
				}
			}
		}
	}


	if (!header) {

		if (esl_strlen_zero(data)) {
			esl_event_del_header(event, header_name);
			FREE(data);
			goto end;
		}

		if (esl_test_flag(event, ESL_EF_UNIQ_HEADERS)) {
			esl_event_del_header(event, header_name);
		}

		if (strstr(data, "ARRAY::")) {
			esl_event_add_array(event, header_name, data);
			FREE(data);
			goto end;
		}


		header = new_header(header_name);
	}
	
	if ((stack & ESL_STACK_PUSH) || (stack & ESL_STACK_UNSHIFT)) {
		char **m = NULL;
		esl_size_t len = 0;
		char *hv;
		int i = 0, j = 0;

		if (header->value && !header->idx) {
			m = malloc(sizeof(char *));
			esl_assert(m);
			m[0] = header->value;
			header->value = NULL;
			header->array = m;
			header->idx++;
			m = NULL;
		}

		i = header->idx + 1;
		m = realloc(header->array, sizeof(char *) * i); 
		esl_assert(m);

		if ((stack & ESL_STACK_PUSH)) {
			m[header->idx] = data;
		} else if ((stack & ESL_STACK_UNSHIFT)) {
			for (j = header->idx; j > 0; j--) {
				m[j] = m[j-1];
			}
			m[0] = data;
		}

		header->idx++;		
		header->array = m;

	redraw:
		len = 0;
		for(j = 0; j < header->idx; j++) {
			len += strlen(header->array[j]) + 2;
		}

		if (len) {
			len += 8;
			hv = realloc(header->value, len);
			esl_assert(hv);
			header->value = hv;

			esl_snprintf(header->value, len, "ARRAY::");
			for(j = 0; j < header->idx; j++) {
				esl_snprintf(header->value + strlen(header->value), len - strlen(header->value), "%s%s", j == 0 ? "" : "|:", header->array[j]);
			}
		}

	} else {
		header->value = data;
	}

	if (!exists) {
		header->hash = esl_ci_hashfunc_default(header->name, &hlen);

		if ((stack & ESL_STACK_TOP)) {
			header->next = event->headers;
			event->headers = header;
			if (!event->last_header) {
				event->last_header = header;
			}
		} else {
			if (event->last_header) {
				event->last_header->next = header;
			} else {
				event->headers = header;
				header->next = NULL;
			}
			event->last_header = header;
		}
	}

 end:

	esl_safe_free(real_header_name);

	return ESL_SUCCESS;
}
Exemplo n.º 8
0
static unsigned char esl_console_complete(const char *buffer, const char *cursor)
{
	char cmd_str[2048] = "";
	unsigned char ret = CC_REDISPLAY;
	char *dup = strdup(buffer);
	char *buf = dup;
	int pos = 0, sc = 0;
	char *p;

	if (!esl_strlen_zero(cursor) && !esl_strlen_zero(buffer)) {
		pos = (int)(cursor - buffer);
	}
	if (pos > 0) {
		*(buf + pos) = '\0';
	}

	if ((p = strchr(buf, '\r')) || (p = strchr(buf, '\n'))) {
		*p = '\0';
	}

	while (*buf == ' ') {
		buf++;
		sc++;
	}

#ifdef HAVE_EDITLINE
	if (!*buf && sc) {
		el_deletestr(el, sc);
	}
#endif

	sc = 0;

	p = end_of_p(buf);
	while(p >= buf && *p == ' ') {
		sc++;
		p--;
	}

#ifdef HAVE_EDITLINE
	if (sc > 1) {
		el_deletestr(el, sc - 1);
		*(p + 2) = '\0';
	}
#endif
	

	if (*cursor) {
		snprintf(cmd_str, sizeof(cmd_str), "api console_complete c=%ld;%s\n\n", (long)pos, buf);
	} else {
		snprintf(cmd_str, sizeof(cmd_str), "api console_complete %s\n\n", buf);
	}

	esl_send_recv(global_handle, cmd_str);


	if (global_handle->last_sr_event && global_handle->last_sr_event->body) {
		char *r = global_handle->last_sr_event->body;
		char *w, *p1;
		
		if (r) {
			if ((w = strstr(r, "\n\nwrite="))) {
				int len = 0;
				*w = '\0';
				w += 8;

				len = atoi(w);

				if ((p1= strchr(w, ':'))) {
					w = p1+ 1;
				}
				
				printf("%s\n\n\n", r);

#ifdef HAVE_EDITLINE
				el_deletestr(el, len);
				el_insertstr(el, w);
#else
#ifdef _MSC_VER
				console_bufferInput(0, len, (char*)buffer, DELETE_REFRESH_OP);
				console_bufferInput(w, (int)strlen(w), (char*)buffer, 0);
#endif
#endif
				
			} else {
				printf("%s\n", r);
#ifdef _MSC_VER
				console_bufferInput(0, 0, (char*)buffer, DELETE_REFRESH_OP);
#endif
			}
		}

		fflush(stdout);
	}	

	esl_safe_free(dup);

	return ret;
}
Exemplo n.º 9
0
static int process_command(esl_handle_t *handle, const char *cmd) 
{
	if ((*cmd == '/' && cmd++) || !strncasecmp(cmd, "...", 3)) {
		
		if (!strcasecmp(cmd, "help")) {
			printf(
				   "Command                    \tDescription\n"
				   "-----------------------------------------------\n"
				   "/help                      \tHelp\n"
				   "/exit, /quit, /bye, ...    \tExit the program.\n"
				   "/event, /noevent, /nixevent\tEvent commands.\n"
				   "/log, /nolog               \tLog commands.\n"
				   "/uuid                      \tFilter logs for a single call uuid\n"
				   "/filter                    \tFilter commands.\n"
				   "/debug [0-7]               \tSet debug level.\n"
				   "\n"
				   );

			goto end;
		}

		if (
			!strcasecmp(cmd, "exit") ||
			!strcasecmp(cmd, "quit") ||
			!strcasecmp(cmd, "...") ||
			!strcasecmp(cmd, "bye")
			) {
			esl_log(ESL_LOG_INFO, "Goodbye!\nSee you at ClueCon http://www.cluecon.com/\n");
			return -1;
		} else if (!strncasecmp(cmd, "uuid", 4)) {
			cmd += 4;
			
			while (*cmd && *cmd == ' ') {
				cmd++;
			}

			if (!esl_strlen_zero(cmd)) {
				filter_uuid = strdup(cmd);
			} else {
				esl_safe_free(filter_uuid);
			}
			
			printf("UUID filtering %s\n", filter_uuid ? "enabled" : "disabled");

		} else if (
			!strncasecmp(cmd, "event", 5) || 
			!strncasecmp(cmd, "noevent", 7) ||
			!strncasecmp(cmd, "nixevent", 8) ||
			!strncasecmp(cmd, "log", 3) || 
			!strncasecmp(cmd, "nolog", 5) || 
			!strncasecmp(cmd, "filter", 6)
			) {

			esl_send_recv(handle, cmd);	

			printf("%s\n", handle->last_sr_reply);
		} else if (!strncasecmp(cmd, "debug", 5)){
			int tmp_debug = atoi(cmd+6);
			if (tmp_debug > -1 && tmp_debug < 8){
				esl_global_set_default_logger(tmp_debug);
				printf("fs_cli debug level set to %d\n", tmp_debug);
			} else {
				printf("fs_cli debug level must be 0 - 7\n");
			}
		} else {
			printf("Unknown command [%s]\n", cmd);	
		}
	} else {
		char cmd_str[1024] = "";
		const char *err = NULL;
		
		snprintf(cmd_str, sizeof(cmd_str), "api %s\nconsole_execute: true\n\n", cmd);
		if (esl_send_recv(handle, cmd_str)) {
			printf("Socket interrupted, bye!\n");
			return 1;
		}
		if (handle->last_sr_event) {
			if (handle->last_sr_event->body) {
				printf("%s\n", handle->last_sr_event->body);
			} else if ((err = esl_event_get_header(handle->last_sr_event, "reply-text")) && !strncasecmp(err, "-err", 3)) {
				printf("Error: %s!\n", err + 4);
			}
		}
	}
	
 end:

	return 0;

}
Exemplo n.º 10
0
static int process_command(esl_handle_t *handle, const char *cmd)
{
	while (*cmd == ' ') cmd++;


	if ((*cmd == '/' && cmd++) || !strncasecmp(cmd, "...", 3)) {
		if (!strcasecmp(cmd, "help")) {
			output_printf("%s", cli_usage);
			goto end;
		}
		if (!strcasecmp(cmd, "exit") ||
			!strcasecmp(cmd, "quit") ||
			!strcasecmp(cmd, "...") ||
			!strcasecmp(cmd, "bye")
			) {
			esl_log(ESL_LOG_INFO, "Goodbye!\nSee you at ClueCon http://www.cluecon.com/\n");
			return -1;
		} else if (!strncasecmp(cmd, "logfilter", 9)) {
			cmd += 9;
			while (*cmd && *cmd == ' ') {
				cmd++;
			}
			if (!esl_strlen_zero(cmd)) {
				esl_safe_free(logfilter);
				logfilter = strdup(cmd);
			} else {
				esl_safe_free(logfilter);
			}
			output_printf("Logfilter %s\n", logfilter ? "enabled" : "disabled");
		} else if (!strncasecmp(cmd, "uuid", 4)) {
			cmd += 4;
			while (*cmd && *cmd == ' ') {
				cmd++;
			}
			if (!esl_strlen_zero(cmd)) {
				filter_uuid = strdup(cmd);
			} else {
				esl_safe_free(filter_uuid);
			}
			output_printf("UUID filtering %s\n", filter_uuid ? "enabled" : "disabled");
		} else if (!strncasecmp(cmd, "event", 5) ||
				   !strncasecmp(cmd, "noevents", 8) ||
				   !strncasecmp(cmd, "nixevent", 8) ||
				   !strncasecmp(cmd, "log", 3) ||
				   !strncasecmp(cmd, "nolog", 5) ||
				   !strncasecmp(cmd, "filter", 6)
				   ) {
			esl_send_recv(handle, cmd);
			printf("%s\n", handle->last_sr_reply);
		} else if (!strncasecmp(cmd, "debug", 5)) {
			int tmp_debug = atoi(cmd+6);
			if (tmp_debug > -1 && tmp_debug < 8) {
				esl_global_set_default_logger(tmp_debug);
				output_printf("fs_cli debug level set to %d\n", tmp_debug);
			} else {
				output_printf("fs_cli debug level must be 0 - 7\n");
			}
		} else {
			output_printf("Unknown command [%s]\n", cmd);
		}
	} else {
		char cmd_str[1024] = "";
		const char *err = NULL;

		if (!strncasecmp(cmd, "console loglevel ", 17)) { 
			snprintf(cmd_str, sizeof(cmd_str), "log %s", cmd + 17);
			esl_send_recv(handle, cmd_str);
			printf("%s\n", handle->last_sr_reply);
		}

		snprintf(cmd_str, sizeof(cmd_str), "api %s\nconsole_execute: true\n\n", cmd);
		if (esl_send_recv(handle, cmd_str)) {
			output_printf("Socket interrupted, bye!\n");
			return -1;
		}
		if (handle->last_sr_event) {
			if (handle->last_sr_event->body) {
				output_printf("%s\n", handle->last_sr_event->body);
			} else if ((err = esl_event_get_header(handle->last_sr_event, "reply-text")) && !strncasecmp(err, "-err", 3)) {
				output_printf("Error: %s!\n", err + 4);
			}
		}
	}
 end:
	return 0;
}