Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getpeercred operation on unix request socket #117

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,14 @@ ngx_feature_test='setsockopt(1, SOL_SOCKET, SO_PASSCRED, NULL, 0);'

#CFLAGS=$"$CFLAGS -DLUA_DEFAULT_PATH='\"/usr/local/openresty/lualib/?.lua\"'"
#CFLAGS=$"$CFLAGS -DLUA_DEFAULT_CPATH='\"/usr/local/openresty/lualib/?.so\"'"

ngx_feature="SO_PEERCRED"
ngx_feature_libs=
ngx_feature_name="NGX_STREAM_LUA_HAVE_SO_PEERCRED"
ngx_feature_run=no
ngx_feature_incs="#include <sys/types.h>
#include <sys/socket.h>"
ngx_feature_path=
ngx_feature_test='getsockopt(1, SOL_SOCKET, SO_PEERCRED, NULL, 0);'

. auto/feature
96 changes: 96 additions & 0 deletions src/ngx_stream_lua_socket_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ static int ngx_stream_lua_socket_tcp_shutdown(lua_State *L);
static int ngx_stream_lua_socket_tcp_setoption(lua_State *L);
static int ngx_stream_lua_socket_tcp_settimeout(lua_State *L);
static int ngx_stream_lua_socket_tcp_settimeouts(lua_State *L);
#if (NGX_STREAM_LUA_HAVE_SO_PEERCRED)
static int ngx_stream_lua_socket_tcp_getpeercred(lua_State *L);
#endif
static void ngx_stream_lua_socket_tcp_handler(ngx_event_t *ev);
static ngx_int_t ngx_stream_lua_socket_tcp_get_peer(ngx_peer_connection_t *pc,
void *data);
Expand Down Expand Up @@ -266,6 +269,11 @@ ngx_stream_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L)
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_shutdown);
lua_setfield(L, -2, "shutdown");

#if (NGX_STREAM_LUA_HAVE_SO_PEERCRED)
lua_pushcfunction(L, ngx_stream_lua_socket_tcp_getpeercred);
lua_setfield(L, -2, "getpeercred");
#endif

lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");

Expand Down Expand Up @@ -4453,6 +4461,94 @@ ngx_stream_lua_socket_tcp_getreusedtimes(lua_State *L)
}


#if (NGX_STREAM_LUA_HAVE_SO_PEERCRED)
static int
ngx_stream_lua_socket_tcp_getpeercred(lua_State *L)
{
ngx_stream_lua_request_t *r;
ngx_connection_t *c;
struct ucred ucred;
socklen_t len;

ngx_stream_lua_loc_conf_t *llcf;
ngx_stream_lua_socket_tcp_upstream_t *u;

if (lua_gettop(L) != 1) {
return luaL_error(L, "expecting 1 arguments (the object), "
"but got %d", lua_gettop(L));
}

r = ngx_stream_lua_get_req(L);
if (r == NULL) {
return luaL_error(L, "no request found");
}

luaL_checktype(L, 1, LUA_TTABLE);

lua_rawgeti(L, 1, SOCKET_CTX_INDEX);
u = lua_touserdata(L, -1);
lua_pop(L, 1);

if (u == NULL || u->peer.connection == NULL || u->write_closed) {
llcf = ngx_stream_lua_get_module_loc_conf(r, ngx_stream_lua_module);

if (llcf->log_socket_errors) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"attempt to get peercred on a closed socket: u:%p, "
"c:%p, ft:%d eof:%d",
u, u ? u->peer.connection : NULL,
u ? (int) u->ft_type : 0, u ? (int) u->eof : 0);
}

lua_pushnil(L);
lua_pushliteral(L, "closed");
return 2;
}

c = u->peer.connection;

if (c->sockaddr->sa_family != AF_UNIX) {
llcf = ngx_stream_lua_get_module_loc_conf(r, ngx_stream_lua_module);

if (llcf->log_socket_errors) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"peercred available for unix sockets only: u:%p, "
"c:%p, ft:%d eof:%d",
u, u ? u->peer.connection : NULL,
u ? (int) u->ft_type : 0, u ? (int) u->eof : 0);
}

lua_pushnil(L);
lua_pushliteral(L, "invalid");
return 2;
}

len = sizeof(struct ucred);
if (getsockopt(c->fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1) {
llcf = ngx_stream_lua_get_module_loc_conf(r, ngx_stream_lua_module);

if (llcf->log_socket_errors) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"attempt to get peercred failed: u:%p, "
"c:%p, ft:%d eof:%d",
u, u ? u->peer.connection : NULL,
u ? (int) u->ft_type : 0, u ? (int) u->eof : 0);
}

lua_pushnil(L);
lua_pushliteral(L, "failed");
return 2;
}

lua_pushnumber(L, ucred.uid);
lua_pushnumber(L, ucred.gid);
lua_pushnumber(L, ucred.pid);

return 3;
}
#endif


static int
ngx_stream_lua_socket_tcp_setkeepalive(lua_State *L)
{
Expand Down
4 changes: 2 additions & 2 deletions t/062-count.t
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ n = 10
assert(ngx.say("n = ", n))
}
--- stream_response
n = 7
n = 8
--- no_error_log
[error]

Expand Down Expand Up @@ -276,6 +276,6 @@ n = 6
end
}
--- stream_response
n = 7
n = 8
--- no_error_log
[error]