Пример #1
0
json_item *get_json_object_channel(CHANNEL *chan)
{
	json_item *jstr = json_new_object();
	json_set_property_strN(jstr, "casttype", 8, "multi", 5);
	json_set_property_strN(jstr, "pubid", 5, chan->pipe->pubid, 32);

	json_item *jprop = json_new_object();
	json_set_property_strZ(jprop, "name", chan->name);

	extend *eTmp = chan->properties;
	
	while (eTmp != NULL) {
		if (eTmp->visibility == EXTEND_ISPUBLIC) {
			if (eTmp->type == EXTEND_JSON) {
				/*json *jcopy = json_copy(eTmp->val);
				
				set_json(eTmp->key, NULL, &jprop);
				
				json_attach(jprop, jcopy, JSON_OBJECT);*/
			} else {
				json_set_property_strZ(jprop, eTmp->key, eTmp->val);
			}
		}
		
		eTmp = eTmp->next;
	}
	json_set_property_objN(jstr, "properties", 10, jprop);
	
	//}
	
	return jstr;
}
Пример #2
0
void send_error(USERS *user, const char *msg, const char *code, acetables *g_ape)
{
	RAW *newraw;
	json_item *jlist = json_new_object();
	
	json_set_property_strZ(jlist, "code", code);
	json_set_property_strZ(jlist, "value", msg);
	
	newraw = forge_raw(RAW_ERR, jlist);
	
	post_raw(newraw, user, g_ape);	
}
Пример #3
0
json_item *get_json_object_user(USERS *user)
{
	json_item *jstr = NULL;
	
	if (user != NULL) {
		jstr = json_new_object();
		json_set_property_strN(jstr, "casttype", 8, "uni", 3);
		json_set_property_strN(jstr, "pubid", 5, user->pipe->pubid, 32);
		
		if (user->properties != NULL) {
			int has_prop = 0;
			
			json_item *jprop = NULL;
						
			extend *eTmp = user->properties;
			
			while (eTmp != NULL) {
				if (eTmp->visibility == EXTEND_ISPUBLIC) {
					if (!has_prop) {
						has_prop = 1;
						jprop = json_new_object();
					}
					if (eTmp->type == EXTEND_JSON) {
						json_item *jcopy = json_item_copy(eTmp->val, NULL);
						
						json_set_property_objZ(jprop, eTmp->key, jcopy);
					} else {
						json_set_property_strZ(jprop, eTmp->key, eTmp->val);

					}			
				}
				eTmp = eTmp->next;
			}
			if (has_prop) {
				json_set_property_objN(jstr, "properties", 10, jprop);
			}
		}

	} else {
		json_set_property_strZ(jstr, "pubid", SERVER_NAME);
	}
	return jstr;
}
Пример #4
0
void send_msg_sub(subuser *sub, const char *msg, const char *type, acetables *g_ape)
{
	RAW *newraw;
	json_item *jlist = json_new_object();
	
	json_set_property_strZ(jlist, "value", msg);
	
	newraw = forge_raw(type, jlist);
	
	post_raw_sub(newraw, sub, g_ape);		
}
Пример #5
0
void send_msg_channel(CHANNEL *chan, const char *msg, const char *type, acetables *g_ape)
{
	RAW *newraw;
	json_item *jlist = json_new_object();
	
	json_set_property_strZ(jlist, "value", msg);
	
	newraw = forge_raw(type, jlist);
	
	post_raw_channel(newraw, chan, g_ape);
}
Пример #6
0
unsigned int checkcmd(clientget *cget, transport_t transport, subuser **iuser, acetables *g_ape)
{	
	struct _cmd_process pc = {cget->hlines, NULL, NULL, cget->client, cget->host, cget->ip_get, transport};
	
	json_item *ijson, *ojson;
	
	unsigned int ret;

	ijson = ojson = init_json_parser(cget->get);
	if (ijson == NULL || ijson->jchild.child == NULL) {
		RAW *newraw;
		json_item *jlist = json_new_object();

		json_set_property_strZ(jlist, "code", "005");
		json_set_property_strZ(jlist, "value", "BAD_JSON");

		newraw = forge_raw(RAW_ERR, jlist);
		
		send_raw_inline(cget->client, transport, newraw, g_ape);
	} else {
		for (ijson = ijson->jchild.child; ijson != NULL; ijson = ijson->next) {
			
			if (pc.guser != NULL && pc.guser->istmp) { /* if "CONNECT" was delayed, push other cmd to the queue and stop execution */
				pc.guser->cmdqueue = json_item_copy(ijson, NULL);
				break;
			}		
			if ((ret = process_cmd(ijson, &pc, iuser, g_ape)) != -1) {
				free_json_item(ojson);
				return ret;
			}
			if (*iuser != NULL) {
				pc.sub = *iuser;
			}					
		}
		free_json_item(ojson);

		return (CONNECT_KEEPALIVE);
	}
	
	return (CONNECT_SHUTDOWN);
}
Пример #7
0
void send_msg(USERS *user, const char *msg, const char *type, acetables *g_ape)
{
	RAW *newraw;
	json_item *jlist = json_new_object();
	
	json_set_property_strZ(jlist, "value", msg);
	
	newraw = forge_raw(type, jlist);
	
	post_raw(newraw, user, g_ape);	
	POSTRAW_DONE(newraw);
}
Пример #8
0
void proxy_onevent(ape_proxy *proxy, char *event, acetables *g_ape)
{
	RAW *newraw;
	json_item *jlist = json_new_object();
	
	json_set_property_strZ(jlist, "event", event);
	json_set_property_objN(jlist, "pipe", 4, get_json_object_proxy(proxy));
	
	newraw = forge_raw("PROXY_EVENT", jlist);
	
	proxy_post_raw(newraw, proxy, g_ape);
}
Пример #9
0
json_item *get_json_object_proxy(ape_proxy *proxy)
{
	json_item *jstr = json_new_object();
	json_item *jprop = json_new_object();
	extend *eTmp = proxy->properties;

	json_set_property_strN(jstr, "pubid", 5, proxy->pipe->pubid, 32);
	json_set_property_strN(jstr, "casttype", 8, "proxy", 5);
	
	json_set_property_strZ(jprop, "host", proxy->sock.host->host);
	json_set_property_strZ(jprop, "ip", proxy->sock.host->ip);
	json_set_property_intZ(jprop, "port", proxy->sock.port);

	while (eTmp != NULL) {
		json_set_property_strZ(jprop, eTmp->key, eTmp->val);
		eTmp = eTmp->next;
	}
	
	json_set_property_objN(jstr, "properties", 10, jprop);

	return jstr;
}
Пример #10
0
void ban(CHANNEL *chan, USERS *banner, const char *ip, char *reason, unsigned int expire, acetables *g_ape) // Ban IP
{
	userslist *uTmp, *tUtmp;
	RAW *newraw;
	json_item *jlist;
	BANNED *blist, *bTmp;
	
	unsigned int isban = 0;
	
	long int nextime = (expire * 60)+time(NULL); // NOW !
	
	if (chan == NULL) {
		return;
	}
	
	uTmp = chan->head;
	bTmp = chan->banned;
	
	while (uTmp != NULL) {

		if (strcmp(ip, uTmp->userinfo->ip) == 0) { // We find somebody with the same IP
			jlist = json_new_object();
			
			json_set_property_strZ(jlist, "reason", reason);
			json_set_property_objN(jlist, "banner", 6, get_json_object_user(banner));
			json_set_property_objN(jlist, "pipe", 4, get_json_object_channel(chan));
			
			newraw = forge_raw(RAW_BAN, jlist);
			
			post_raw(newraw, uTmp->userinfo, g_ape);
			
			if (isban == 0) {
				blist = xmalloc(sizeof(*blist));
				
				memset(blist->reason, 0, 256);
				strncpy(blist->ip, ip, 16);
				strncpy(blist->reason, reason, 255);
				blist->expire = nextime;
				blist->next = bTmp;
				chan->banned = blist;
				isban = 1;
			}
			tUtmp = uTmp->next;
			left(uTmp->userinfo, chan, g_ape); // if the user is the last : "chan" is free (rmchan())
			uTmp = tUtmp;
			continue;
		}
		uTmp = uTmp->next;
	}

}
Пример #11
0
void proxy_process_eol(ape_socket *co, acetables *g_ape)
{
	char *b64;
	ape_proxy *proxy = co->attach;
	char *data = co->buffer_in.data;

	RAW *newraw;
	json_item *jlist = json_new_object();
	
	data[co->buffer_in.length] = '\0';
	b64 = base64_encode(data, strlen(data));
	
	json_set_property_strZ(jlist, "data", b64);
	json_set_property_strN(jlist, "event", 5, "READ", 4);
	json_set_property_objN(jlist, "pipe", 4, get_json_object_proxy(proxy));
	
	newraw = forge_raw("PROXY_EVENT", jlist);
	
	proxy_post_raw(newraw, proxy, g_ape);
	
	free(b64);	
}
Пример #12
0
void sendback_session(USERS *user, session *sess, acetables *g_ape)
{
	subuser *current = user->subuser;
	
	while (current != NULL) {
		if (current->need_update) {
			json_item *jlist = json_new_object(), *jobj_item = json_new_object();
			RAW *newraw;
			
			current->need_update = 0;
			
			json_set_property_strZ(jobj_item, sess->key, sess->val);
			json_set_property_objN(jlist, "sessions", 8, jobj_item);
			
			newraw = forge_raw("SESSIONS", jlist);
			newraw->priority = RAW_PRI_HI;
			
			post_raw_sub(copy_raw_z(newraw), current, g_ape);
		}
		current = current->next;
	}	
	
}
Пример #13
0
int process_cmd(json_item *ijson, struct _cmd_process *pc, subuser **iuser, acetables *g_ape)
{
	callback *cmdback, tmpback = {handle_bad_cmd, NEED_NOTHING};
	json_item *rjson = json_lookup(ijson->jchild.child, "cmd"), *jchl;
	subuser *sub = pc->sub;
	unsigned int flag;
	unsigned short int attach = 1;

	if (rjson != NULL && rjson->jval.vu.str.value != NULL) {
		callbackp cp;
		cp.client = NULL;
		cp.cmd 	= rjson->jval.vu.str.value;
		cp.data = NULL;
		cp.hlines = NULL;
		
		json_item *jsid;
		
		if ((cmdback = (callback *)hashtbl_seek(g_ape->hCallback, rjson->jval.vu.str.value)) == NULL) {
			cmdback = &tmpback;
		}
		
		if ((pc->guser == NULL && (jsid = json_lookup(ijson->jchild.child, "sessid")) != NULL && jsid->jval.vu.str.value != NULL)) {
			pc->guser = seek_user_id(jsid->jval.vu.str.value, g_ape);
		}

		if (cmdback->need != NEED_NOTHING || pc->guser != NULL) { // We process the connection like a "NEED_SESSID" if the user provide its key

			if (pc->guser == NULL) {
				
				RAW *newraw;
				json_item *jlist = json_new_object();

				json_set_property_strZ(jlist, "code", "004");
				json_set_property_strZ(jlist, "value", "BAD_SESSID");

				newraw = forge_raw(RAW_ERR, jlist);
				
				send_raw_inline(pc->client, pc->transport, newraw, g_ape);

				return (CONNECT_SHUTDOWN);
			} else if (sub == NULL) {
				
				sub = getsubuser(pc->guser, pc->host);
				if (sub != NULL && sub->client->fd != pc->client->fd && sub->state == ALIVE) {
					/* The user open a new connection while he already has one openned */
					struct _transport_open_same_host_p retval = transport_open_same_host(sub, pc->client, pc->guser->transport);				
			
					if (retval.client_close != NULL) {
						// Send CLOSE if no response has been sent yet
						if (!sub->headers.sent) {
							RAW *newraw;
							json_item *jlist = json_new_object();

							json_set_property_strZ(jlist, "value", "null");

							newraw = forge_raw("CLOSE", jlist);

							send_raw_inline((retval.client_close->fd == pc->client->fd ? pc->client : sub->client), pc->transport, newraw, g_ape);
						}
						
						// It's not safe to leave the subuser pointer in co->attach anymore
						// since subuser could subsequently be deleted, leaving a pointer into free heap.
						// So, let this socket finish up on its own and pretend its already finished.

						sub->state = ADIED;
						sub->headers.sent = 0;
						http_headers_free(sub->headers.content);
						sub->headers.content = NULL;
						sub->burn_after_writing = 0;

						g_ape->co[retval.client_close->fd]->attach = NULL;
						safe_shutdown(retval.client_close->fd, g_ape);
					}
					sub->client = cp.client = retval.client_listener;
					sub->state = retval.substate;
					attach = retval.attach;
			
				} else if (sub == NULL) {
					sub = addsubuser(pc->client, pc->host, pc->guser, g_ape);
					if (sub != NULL) {
						subuser_restor(sub, g_ape);
					}
				} else if (sub != NULL) {
					sub->client = pc->client;
				}
				pc->guser->idle = (long int)time(NULL); // update user idle

				sub->idle = pc->guser->idle; // Update subuser idle
				
			}

		}
		
		if (pc->guser != NULL && sub != NULL && (jchl = json_lookup(ijson->jchild.child, "chl")) != NULL /*&& jchl->jval.vu.integer_value > sub->current_chl*/) {
			sub->current_chl = jchl->jval.vu.integer_value;
		}
		#if 0 
		else if (pc->guser != NULL && sub != NULL) {
			/* if a bad challenge is detected, we are stoping walking on cmds */
			send_error(pc->guser, "BAD_CHL", "250", g_ape);

			sub->state = ALIVE;
			
			return (CONNECT_KEEPALIVE);
		}
		#endif
					
		cp.param = json_lookup(ijson->jchild.child, "params");
		cp.client = (cp.client != NULL ? cp.client : pc->client);
		cp.call_user = pc->guser;
		cp.call_subuser = sub;
		cp.g_ape = g_ape;
		cp.host = pc->host;
		cp.ip = pc->ip;
		cp.chl = (sub != NULL ? sub->current_chl : 0);
		cp.transport = pc->transport;
		cp.hlines = pc->hlines;
		
		/* Little hack to access user object on connect hook callback (preallocate an user) */
		if (strncasecmp(cp.cmd, "CONNECT", 7) == 0 && cp.cmd[7] == '\0') {
			pc->guser = cp.call_user = adduser(cp.client, cp.host, cp.ip, NULL, g_ape);
			pc->guser->transport = pc->transport;
			sub = cp.call_subuser = cp.call_user->subuser;
		}
		
		if ((flag = call_cmd_hook(cp.cmd, &cp, g_ape)) == RETURN_CONTINUE) {
			flag = cmdback->func(&cp);
		}
		
		if (flag & RETURN_NULL) {
			pc->guser = NULL;
		} else if (flag & RETURN_BAD_PARAMS) {
			RAW *newraw;
			json_item *jlist = json_new_object();
			
			if (cp.chl) {
				json_set_property_intN(jlist, "chl", 3, cp.chl);
			}
			json_set_property_strZ(jlist, "code", "001");
			json_set_property_strZ(jlist, "value", "BAD_PARAMS");

			newraw = forge_raw(RAW_ERR, jlist);
			
			if (cp.call_user != NULL) {
				//cp.call_user->istmp = 0;
				if (sub == NULL) {
					sub = getsubuser(pc->guser, pc->host);	
				}
				post_raw_sub(newraw, sub, g_ape);
			} else {
				send_raw_inline(pc->client, pc->transport, newraw, g_ape);
			}
			
			//guser = NULL;
		} else if (flag & RETURN_BAD_CMD) {
			RAW *newraw;
			json_item *jlist = json_new_object();

			if (cp.chl) {
				json_set_property_intN(jlist, "chl", 3, cp.chl);
			}
			json_set_property_strZ(jlist, "code", "003");
			json_set_property_strZ(jlist, "value", "BAD_CMD");

			newraw = forge_raw(RAW_ERR, jlist);
			
			if (cp.call_user != NULL) {	
				if (sub == NULL) {
					sub = getsubuser(pc->guser, pc->host);	
				}
				post_raw_sub(newraw, sub, g_ape);
			} else {
				send_raw_inline(pc->client, pc->transport, newraw, g_ape);
			}					
		}

		if (pc->guser != NULL) {
			if (sub == NULL) {
				sub = getsubuser(pc->guser, pc->host);	
			}
			if (iuser != NULL) {
				*iuser = (attach ? sub : NULL);
			}
			/* If tmpfd is set, we do not have any reasons to change its state */
			sub->state = ALIVE;
			
			if (flag & RETURN_HANG || flag & RETURN_BAD_PARAMS) {
				return (CONNECT_KEEPALIVE);
			}
			
		} else if (flag & RETURN_HANG) {
			/* Doesn't need sessid */
			return (CONNECT_KEEPALIVE);
		} else {
			return (CONNECT_SHUTDOWN);
		}
	} else {

		RAW *newraw;
		json_item *jlist = json_new_object();

		json_set_property_strZ(jlist, "code", "003");
		json_set_property_strZ(jlist, "value", "NO_CMD");

		newraw = forge_raw(RAW_ERR, jlist);

		send_raw_inline(pc->client, pc->transport, newraw, g_ape);
		//printf("Cant find %s\n", rjson->jval.vu.str.value);
		return (CONNECT_SHUTDOWN);
	}
	
	return -1;
}
Пример #14
0
void json_set_element_strZ(json_item *obj, const char *value)
{
	json_set_property_strZ(obj, NULL, value);
}