Skip to content

Commit 7355678

Browse files
committed
session server REFACTOR clean unix accept
1 parent 1022f92 commit 7355678

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/session_server.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,14 +2055,20 @@ nc_get_uid(int sock, uid_t *uid)
20552055

20562056
#endif
20572057

2058+
/**
2059+
* @brief Fully accept a session on a connected UNIX socket.
2060+
*
2061+
* @param[in] session Session to use.
2062+
* @param[in] sock Connected socket.
2063+
* @return 1 on success.
2064+
* @return -1 on error.
2065+
*/
20582066
static int
2059-
nc_accept_unix(struct nc_session *session, int sock)
2067+
nc_accept_unix_session(struct nc_session *session, int sock)
20602068
{
20612069
#if defined (SO_PEERCRED) || defined (HAVE_GETPEEREID)
20622070
struct passwd *pw, pw_buf;
20632071
char *username;
2064-
2065-
session->ti_type = NC_TI_UNIX;
20662072
uid_t uid = 0;
20672073
char *buf = NULL;
20682074
size_t buf_len = 0;
@@ -2074,7 +2080,7 @@ nc_accept_unix(struct nc_session *session, int sock)
20742080

20752081
pw = nc_getpw(uid, NULL, &pw_buf, &buf, &buf_len);
20762082
if (pw == NULL) {
2077-
ERR(NULL, "Failed to find username for uid=%u (%s).\n", uid, strerror(errno));
2083+
ERR(session, "Failed to find username for uid=%u (%s).", uid, strerror(errno));
20782084
close(sock);
20792085
return -1;
20802086
}
@@ -2086,15 +2092,17 @@ nc_accept_unix(struct nc_session *session, int sock)
20862092
close(sock);
20872093
return -1;
20882094
}
2089-
session->username = username;
20902095

2096+
session->username = username;
2097+
session->ti_type = NC_TI_UNIX;
20912098
session->ti.unixsock.sock = sock;
20922099

20932100
return 1;
20942101
#else
20952102
(void)session;
20962103
(void)sock;
20972104

2105+
ERR(session, "Unable to learn the identity of the client connected to the UNIX socket, terminating.");
20982106
return -1;
20992107
#endif
21002108
}
@@ -2317,7 +2325,7 @@ nc_accept(int timeout, const struct ly_ctx *ctx, struct nc_session **session)
23172325
#endif /* NC_ENABLED_SSH_TLS */
23182326
if (server_opts.endpts[bind_idx].ti == NC_TI_UNIX) {
23192327
(*session)->data = server_opts.endpts[bind_idx].opts.unixsock;
2320-
ret = nc_accept_unix(*session, sock);
2328+
ret = nc_accept_unix_session(*session, sock);
23212329
sock = -1;
23222330
if (ret < 0) {
23232331
msgtype = NC_MSG_ERROR;

0 commit comments

Comments
 (0)