From cc7cdf2d5bc06756156da296f66440d533c3ca00 Mon Sep 17 00:00:00 2001 From: Mathieu Parent Date: Thu, 15 Sep 2016 09:22:04 +0200 Subject: [PATCH] SO_PASSCRED and SO_PEERCRED options --- src/options.c | 27 +++++++++++++++++++++++++++ src/options.h | 3 +++ src/unixtcp.c | 17 +++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/src/options.c b/src/options.c index 20f4c28..6f66e65 100644 --- a/src/options.c +++ b/src/options.c @@ -251,6 +251,33 @@ int opt_set_ip6_v6only(lua_State *L, p_socket ps) return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY); } +int opt_set_passcred(lua_State *L, p_socket ps) +{ + return opt_setboolean(L, ps, SOL_SOCKET, SO_PASSCRED); +} + +int opt_get_passcred(lua_State *L, p_socket ps) +{ + return opt_getboolean(L, ps, SOL_SOCKET, SO_PASSCRED); +} + +int opt_get_peercred(lua_State *L, p_socket ps) +{ + struct ucred cred; + int len = sizeof(cred); + int err = opt_get(L, ps, SOL_SOCKET, SO_PEERCRED, (char *) &cred, &len); + if (err < 0) + return err; + lua_newtable(L); + lua_pushinteger(L, (long) cred.pid); + lua_setfield(L, -2, "pid"); + lua_pushinteger(L, (long) cred.uid); + lua_setfield(L, -2, "uid"); + lua_pushinteger(L, (long) cred.gid); + lua_setfield(L, -2, "gid"); + return 1; +} + /*=========================================================================*\ * Auxiliar functions \*=========================================================================*/ diff --git a/src/options.h b/src/options.h index 19ba0df..13fbc77 100644 --- a/src/options.h +++ b/src/options.h @@ -37,6 +37,7 @@ int opt_set_ip6_multicast_loop(lua_State *L, p_socket ps); int opt_set_ip6_add_membership(lua_State *L, p_socket ps); int opt_set_ip6_drop_membersip(lua_State *L, p_socket ps); int opt_set_ip6_v6only(lua_State *L, p_socket ps); +int opt_set_passcred(lua_State *L, p_socket ps); /* supported options for getoption */ int opt_get_dontroute(lua_State *L, p_socket ps); @@ -54,6 +55,8 @@ int opt_get_ip6_multicast_hops(lua_State *L, p_socket ps); int opt_get_ip6_unicast_hops(lua_State *L, p_socket ps); int opt_get_ip6_v6only(lua_State *L, p_socket ps); int opt_get_reuseport(lua_State *L, p_socket ps); +int opt_get_passcred(lua_State *L, p_socket ps); +int opt_get_peercred(lua_State *L, p_socket ps); /* invokes the appropriate option handler */ int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps); diff --git a/src/unixtcp.c b/src/unixtcp.c index 747ef5f..4cbdb0f 100644 --- a/src/unixtcp.c +++ b/src/unixtcp.c @@ -27,6 +27,7 @@ static int meth_receive(lua_State *L); static int meth_accept(lua_State *L); static int meth_close(lua_State *L); static int meth_setoption(lua_State *L); +static int meth_getoption(lua_State *L); static int meth_settimeout(lua_State *L); static int meth_getfd(lua_State *L); static int meth_setfd(lua_State *L); @@ -55,6 +56,7 @@ static luaL_Reg unixtcp_methods[] = { {"send", meth_send}, {"setfd", meth_setfd}, {"setoption", meth_setoption}, + {"getoption", meth_getoption}, {"setpeername", meth_connect}, {"setsockname", meth_bind}, {"getsockname", meth_getsockname}, @@ -68,6 +70,13 @@ static t_opt optset[] = { {"keepalive", opt_set_keepalive}, {"reuseaddr", opt_set_reuseaddr}, {"linger", opt_set_linger}, + {"passcred", opt_set_passcred}, + {NULL, NULL} +}; + +static t_opt optget[] = { + {"passcred", opt_get_passcred}, + {"peercred", opt_get_peercred}, {NULL, NULL} }; @@ -130,6 +139,14 @@ static int meth_setoption(lua_State *L) { return opt_meth_setoption(L, optset, &un->sock); } +/*-------------------------------------------------------------------------*\ +* Just call option handler +\*-------------------------------------------------------------------------*/ +static int meth_getoption(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkgroup(L, "unixudp{any}", 1); + return opt_meth_getoption(L, optget, &un->sock); +} + /*-------------------------------------------------------------------------*\ * Select support methods \*-------------------------------------------------------------------------*/