示例#1
0
/**
 * Pretty print peer name.
 */
static int
lbox_session_peer(struct lua_State *L)
{
	if (lua_gettop(L) > 1)
		luaL_error(L, "session.peer(sid): bad arguments");

	int fd;
	struct session *session;
	if (lua_gettop(L) == 1)
		session = session_find(luaL_checkint(L, 1));
	else
		session = current_session();
	if (session == NULL)
		luaL_error(L, "session.peer(): session does not exist");
	fd = session->fd;
	if (fd < 0) {
		lua_pushnil(L); /* no associated peer */
		return 1;
	}

	struct sockaddr_storage addr;
	socklen_t addrlen = sizeof(addr);
	if (sio_getpeername(fd, (struct sockaddr *)&addr, &addrlen) < 0)
		luaL_error(L, "session.peer(): getpeername() failed");

	lua_pushstring(L, sio_strfaddr((struct sockaddr *)&addr, addrlen));
	return 1;
}
示例#2
0
/** Session user id. */
static int
lbox_session_su(struct lua_State *L)
{
	int top = lua_gettop(L);
	if (top < 1)
		luaL_error(L, "session.su(): bad arguments");
	struct session *session = current_session();
	if (session == NULL)
		luaL_error(L, "session.su(): session does not exist");
	struct user *user;
	if (lua_type(L, 1) == LUA_TSTRING) {
		size_t len;
		const char *name = lua_tolstring(L, 1, &len);
		user = user_find_by_name(name, len);
	} else {
		user = user_find(lua_tointeger(L, 1));
	}
	if (user == NULL)
		lbox_error(L);
	struct credentials orig_cr;
	credentials_copy(&orig_cr, &session->credentials);
	credentials_init(&session->credentials, user);
	if (top == 1)
		return 0; /* su */

	/* sudo */
	int error = lua_pcall(L, top - 2, LUA_MULTRET, 0);
	credentials_copy(&session->credentials, &orig_cr);
	if (error)
		lbox_error(L);
	return lua_gettop(L) - 1;
}
示例#3
0
gboolean mbb_session_is_http(void)
{
	struct mbb_session *ss;

	if ((ss = current_session()) == NULL)
		return FALSE;

	return ss->type == MBB_SESSION_HTTP;
}
示例#4
0
/**
 * Session user name.
 * Note: effective user name may be different in
 * a setuid function.
 */
static int
lbox_session_user(struct lua_State *L)
{
	struct user *user = user_by_id(current_session()->credentials.uid);
	if (user)
		lua_pushstring(L, user->def.name);
	else
		lua_pushnil(L);
	return 1;
}
示例#5
0
/**
 * Session user id.
 * Note: effective user id (current_user()->uid)
 * may be different in a setuid function.
 */
static int
lbox_session_uid(struct lua_State *L)
{
	/*
	 * Sic: push session user, not the current user,
	 * which may differ inside a setuid function.
	 */
	lua_pushnumber(L, current_session()->credentials.uid);
	return 1;
}
示例#6
0
gpointer mbb_session_var_get_data(struct mbb_var *var)
{
	struct mbb_session *ss;
	gpointer data;

	if ((ss = current_session()) == NULL)
		return NULL;

	if (session_lookup_var(ss, var, &data) == FALSE)
		data = session_insert_var(ss, var);

	return data;
}
示例#7
0
/**
 * Return the id of currently executed request.
 * Many requests share the same session so this is only
 * valid at session start. 0 for non-iproto sessions.
 */
static int
lbox_session_sync(struct lua_State *L)
{
	lua_pushnumber(L, current_session()->sync);
	return 1;
}