Beispiel #1
0
void subuser_restor(subuser *sub, acetables *g_ape)
{
	CHANLIST *chanl;
	CHANNEL *chan;
	
	json_item *jlist;
	RAW *newraw;
	USERS *user = sub->user;
	userslist *ulist;

	chanl = user->chan_foot;

	while (chanl != NULL) {
		chan = chanl->chaninfo;
		/*
		 * quiet channel won't be posted on subuser_restor 
		 */
		if (!(chan->flags & CHANNEL_QUIET)) {
			jlist = json_new_object();
			if ( !(chan->flags & CHANNEL_NONINTERACTIVE) && chan->head != NULL ) {
				json_item *user_list = json_new_array();
			
				ulist = chan->head;
			
				while (ulist != NULL) {
	
					json_item *juser = get_json_object_user(ulist->userinfo);
		
					if (ulist->userinfo != user) {
						//make_link(user, ulist->userinfo);
					}
				
					json_set_property_intN(juser, "level", 5, ulist->level);
				
					json_set_element_obj(user_list, juser);

					ulist = ulist->next;
				}
			
				json_set_property_objN(jlist, "users", 5, user_list);
			}
			json_set_property_objN(jlist, "pipe", 4, get_json_object_channel(chan));

			newraw = forge_raw(RAW_CHANNEL, jlist);
			newraw->priority = RAW_PRI_HI;
			post_raw_sub(newraw, sub, g_ape);
			POSTRAW_DONE(newraw);
		}
		chanl = chanl->next;
	}

	jlist = json_new_object();
	json_set_property_objN(jlist, "user", 4, get_json_object_user(user));	
	
	newraw = forge_raw("IDENT", jlist);
	newraw->priority = RAW_PRI_HI;
	post_raw_sub(newraw, sub, g_ape);
	POSTRAW_DONE(newraw);
	
}
Beispiel #2
0
unsigned int cmd_connect(callbackp *callbacki)
{
	USERS *nuser;
	RAW *newraw;
	json_item *jstr = NULL;

	nuser = adduser(NULL, NULL, NULL, callbacki->call_user, callbacki->g_ape);
	
	callbacki->call_user = nuser;

	jstr = json_new_object();
	json_set_property_objN(jstr, "user", 4, get_json_object_user(callbacki->call_user));	
	
	newraw = forge_raw("IDENT", jstr);
	newraw->priority = RAW_PRI_HI;
	post_raw_sub(newraw, callbacki->call_subuser, callbacki->g_ape);	

	jstr = json_new_object();	
	json_set_property_strN(jstr, "sessid", 6, nuser->sessid, 32);
	
	newraw = forge_raw(RAW_LOGIN, jstr);
	newraw->priority = RAW_PRI_HI;
	
	post_raw(newraw, nuser, callbacki->g_ape);	
	
	return (RETURN_NOTHING);
}
Beispiel #3
0
/* Post raw to a user and propagate it to all of it's subuser */
void post_raw(RAW *raw, USERS *user, acetables *g_ape)
{
	subuser *sub = user->subuser;
	while (sub != NULL) {
		post_raw_sub(copy_raw_z(raw), sub, g_ape);
		sub = sub->next;
	}
}
Beispiel #4
0
void send_msg_sub(subuser *sub, const char *msg, const char *type, acetables *g_ape)
{
	RAW *newraw;
	json *jlist = NULL;
	
	set_json("value", msg, &jlist);
	
	newraw = forge_raw(type, jlist);
	
	post_raw_sub(newraw, sub, g_ape);		
}
Beispiel #5
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);		
}
Beispiel #6
0
/* Post raw to a user and propagate it to all of it's subuser with *sub exception */
void post_raw_restricted(RAW *raw, USERS *user, subuser *sub, acetables *g_ape)
{
	subuser *tSub = user->subuser;
	
	if (sub == NULL) {
		return;
	}
	while (tSub != NULL) {
		if (sub != tSub) {
			post_raw_sub(copy_raw_z(raw), tSub, g_ape);
		}
		tSub = tSub->next;
	}
}
Beispiel #7
0
subuser *addsubuser(int fd, const char *channel, USERS *user, acetables *g_ape)
{
	subuser *sub;
		
	if (getsubuser(user, channel) != NULL || strlen(channel) > MAX_HOST_LENGTH) {
		return NULL;
	}

	sub = xmalloc(sizeof(*sub));
	sub->fd = fd;
	sub->state = ADIED;
	sub->user = user;
	
	memcpy(sub->channel, channel, strlen(channel)+1);
	sub->next = user->subuser;
	
	sub->rawhead = NULL;
	sub->rawfoot = NULL;
	sub->nraw = 0;
	sub->wait_for_free = 0;
	sub->headers_sent = 0;
	
	sub->burn_after_writing = 0;
	
	sub->idle = time(NULL);
	sub->need_update = 0;

	
	(user->nsub)++;
	
	user->subuser = sub;
	
	/* if the previous subuser have some messages in queue, copy them to the new subuser */
	if (sub->next != NULL && sub->next->nraw) {
		RAW *rTmp;
		
		for (rTmp = sub->next->rawhead; rTmp != NULL; rTmp = rTmp->next) {
			if (rTmp->priority == 1) {
				continue;
			}
			post_raw_sub(copy_raw(rTmp), sub, g_ape);
		}

	}

	return sub;
}
Beispiel #8
0
/* This is usefull to ask all subuser to update their sessions */
unsigned int cmd_pong(callbackp *callbacki)
{
	if (strcmp(callbacki->param[2], callbacki->call_user->lastping) == 0) {
		RAW *newraw;
				
		callbacki->call_user->lastping[0] = '\0';

		json *jlist = NULL;
	
		set_json("value", callbacki->param[2], &jlist);
	
		newraw = forge_raw("UPDATE", jlist);
	
		post_raw_sub(newraw, getsubuser(callbacki->call_user, callbacki->host), callbacki->g_ape);
	}
	return (FOR_NOTHING);
}
Beispiel #9
0
void sendback_session(USERS *user, session *sess, acetables *g_ape)
{
	subuser *current = user->subuser;
	
	while (current != NULL) {
		if (current->need_update) {
			json *jlist = NULL, *jobj = NULL;
			RAW *newraw;
			
			current->need_update = 0;
			set_json("sessions", NULL, &jlist);
			set_json(sess->key, (sess != NULL ? sess->val : NULL), &jobj);
			json_attach(jlist, jobj, JSON_OBJECT);
			newraw = forge_raw("SESSIONS", jlist);
			newraw->priority = 1;
			post_raw_sub(newraw, current, g_ape);
		}
		current = current->next;
	}	
	
}
Beispiel #10
0
unsigned int cmd_session(callbackp *callbacki)
{
	if (strcmp(callbacki->param[2], "set") == 0 && (callbacki->nParam == 4 || callbacki->nParam == 5)) {
		if (callbacki->nParam == 5) {
			subuser *tmpSub = getsubuser(callbacki->call_user, callbacki->host);
		
			if (tmpSub != NULL) {
				tmpSub->need_update = 0;
			}
		}
		if (set_session(callbacki->call_user, callbacki->param[3], callbacki->param[4], (callbacki->nParam == 4 ? 0 : 1), callbacki->g_ape) == NULL) {
			send_error(callbacki->call_user, "SESSION_ERROR", "203", callbacki->g_ape);
		}
	} else if (strcmp(callbacki->param[2], "get") == 0 && callbacki->nParam >= 3) {
		int i;
		json *jlist = NULL, *jobj = NULL;
		RAW *newraw;
		
		set_json("sessions", NULL, &jlist);
		
		for (i = 3; i <= callbacki->nParam; i++) {
			if (strlen(callbacki->param[i]) > 32) {
				continue;
			}
			session *sTmp = get_session(callbacki->call_user, callbacki->param[i]);

			set_json(callbacki->param[i], (sTmp != NULL ? sTmp->val : NULL), &jobj);

		}
		json_attach(jlist, jobj, JSON_OBJECT);
		newraw = forge_raw("SESSIONS", jlist);
		newraw->priority = 1;
		/* Only sending to current subuser */
		post_raw_sub(newraw, getsubuser(callbacki->call_user, callbacki->host), callbacki->g_ape);

	} else {
		send_error(callbacki->call_user, "SESSION_ERROR_PARAMS", "108", callbacki->g_ape);
	}
	return (FOR_NOTHING);
}
Beispiel #11
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;
	}	
	
}
Beispiel #12
0
subuser *addsubuser(ape_socket *client, const char *channel, USERS *user, acetables *g_ape)
{
	subuser *sub = NULL;

	FIRE_EVENT(addsubuser, sub, g_ape);
	
	if (getsubuser(user, channel) != NULL || strlen(channel) > MAX_HOST_LENGTH) {
		return NULL;
	}

	sub = xmalloc(sizeof(*sub));
	sub->client = client;
	sub->state = ADIED;
	sub->user = user;
	
	memcpy(sub->channel, channel, strlen(channel)+1);
	sub->next = user->subuser;
	
	sub->nraw = 0;
	sub->wait_for_free = 0;
	
	sub->properties = NULL;
	
	sub->headers.sent = 0;
	sub->headers.content = NULL;
	
	sub->burn_after_writing = 0;
	
	sub->idle = time(NULL);
	sub->need_update = 0;
	sub->current_chl = 0;

	sub->raw_pools.nraw = 0;
	
	/* Pre-allocate a pool of raw to reduce the number of malloc calls */
	
	/* Low priority raws */
	sub->raw_pools.low.nraw = 0;
	sub->raw_pools.low.size = 32;
	sub->raw_pools.low.rawhead = init_raw_pool(sub->raw_pools.low.size);
	sub->raw_pools.low.rawfoot = sub->raw_pools.low.rawhead;
	
	/* High priority raws */
	sub->raw_pools.high.nraw = 0;
	sub->raw_pools.high.size = 8;
	sub->raw_pools.high.rawhead = init_raw_pool(sub->raw_pools.high.size);
	sub->raw_pools.high.rawfoot = sub->raw_pools.high.rawhead;
	
	(user->nsub)++;
	
	user->subuser = sub;
	
	/* if the previous subuser have some messages in queue, copy them to the new subuser */
	if (sub->next != NULL && sub->next->raw_pools.low.nraw) {
		struct _raw_pool *rTmp;
		for (rTmp = sub->next->raw_pools.low.rawhead; rTmp->raw != NULL; rTmp = rTmp->next) {
			post_raw_sub(rTmp->raw, sub, g_ape);
		}

	}
	
	HOOK_EVENT(addsubuser, sub, g_ape);
	
	return sub;
}
Beispiel #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;
}
Beispiel #14
0
void subuser_restor(subuser *sub, acetables *g_ape)
{
	CHANLIST *chanl;
	CHANNEL *chan;
	
	json *jlist = NULL;
	RAW *newraw;
	USERS *user = sub->user;
	userslist *ulist;
	
	char level[8];
	
	chanl = user->chan_foot;

	while (chanl != NULL) {
		jlist = NULL;
		chan = chanl->chaninfo;
		
		if (chan->interactive) {

			ulist = chan->head;
			set_json("users", NULL, &jlist);
			
			while (ulist != NULL) {
	
				struct json *juser = NULL;
		
				if (ulist->userinfo != user) {
					//make_link(user, ulist->userinfo);
				}
		
				sprintf(level, "%i", ulist->level);
				set_json("level", level, &juser);
				
				json_concat(juser, get_json_object_user(ulist->userinfo));
	
				json_attach(jlist, juser, JSON_ARRAY);

				ulist = ulist->next;
			}
		}
		set_json("pipe", NULL, &jlist);
		
		json_attach(jlist, get_json_object_channel(chan), JSON_OBJECT);

		newraw = forge_raw(RAW_CHANNEL, jlist);
		newraw->priority = 1;
		post_raw_sub(newraw, sub, g_ape);
		chanl = chanl->next;
	}

	
	jlist = NULL;
	
	set_json("user", NULL, &jlist);
	json_attach(jlist, get_json_object_user(user), JSON_OBJECT);	
	
	newraw = forge_raw("IDENT", jlist);
	newraw->priority = 1;
	post_raw_sub(newraw, sub, g_ape);
	
}