agent: Reformat the rest of the file

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Jakub Jelen
2025-08-01 12:04:29 +02:00
parent cc667021e5
commit 737f9ecc3c

View File

@@ -67,7 +67,9 @@
(((x) == SSH_AGENT_FAILURE) || ((x) == SSH_COM_AGENT2_FAILURE) || \ (((x) == SSH_AGENT_FAILURE) || ((x) == SSH_COM_AGENT2_FAILURE) || \
((x) == SSH2_AGENT_FAILURE)) ((x) == SSH2_AGENT_FAILURE))
static uint32_t atomicio(struct ssh_agent_struct *agent, void *buf, uint32_t n, int do_read) { static uint32_t
atomicio(struct ssh_agent_struct *agent, void *buf, uint32_t n, int do_read)
{
char *b = buf; char *b = buf;
uint32_t pos = 0; uint32_t pos = 0;
ssize_t res; ssize_t res;
@@ -113,21 +115,25 @@ static uint32_t atomicio(struct ssh_agent_struct *agent, void *buf, uint32_t n,
} else { } else {
/* using an SSH channel */ /* using an SSH channel */
while (n > pos) { while (n > pos) {
if (do_read) if (do_read) {
res = ssh_channel_read(channel, b + pos, n - pos, 0); res = ssh_channel_read(channel, b + pos, n - pos, 0);
else } else {
res = ssh_channel_write(channel, b + pos, n - pos); res = ssh_channel_write(channel, b + pos, n - pos);
if (res == SSH_AGAIN) }
if (res == SSH_AGAIN) {
continue; continue;
if (res == SSH_ERROR) }
if (res == SSH_ERROR) {
return 0; return 0;
}
pos += (uint32_t)res; pos += (uint32_t)res;
} }
return pos; return pos;
} }
} }
ssh_agent ssh_agent_new(struct ssh_session_struct *session) { ssh_agent ssh_agent_new(struct ssh_session_struct *session)
{
ssh_agent agent = NULL; ssh_agent agent = NULL;
agent = malloc(sizeof(struct ssh_agent_struct)); agent = malloc(sizeof(struct ssh_agent_struct));
@@ -146,7 +152,9 @@ ssh_agent ssh_agent_new(struct ssh_session_struct *session) {
return agent; return agent;
} }
static void agent_set_channel(struct ssh_agent_struct *agent, ssh_channel channel){ static void agent_set_channel(struct ssh_agent_struct *agent,
ssh_channel channel)
{
agent->channel = channel; agent->channel = channel;
} }
@@ -168,11 +176,15 @@ static void agent_set_channel(struct ssh_agent_struct *agent, ssh_channel channe
* @returns SSH_OK in case of success * @returns SSH_OK in case of success
* SSH_ERROR in case of an error * SSH_ERROR in case of an error
*/ */
int ssh_set_agent_channel(ssh_session session, ssh_channel channel){ int ssh_set_agent_channel(ssh_session session, ssh_channel channel)
if (!session) {
if (!session) {
return SSH_ERROR; return SSH_ERROR;
}
if (!session->agent) { if (!session->agent) {
ssh_set_error(session, SSH_REQUEST_DENIED, "Session has no active agent"); ssh_set_error(session,
SSH_REQUEST_DENIED,
"Session has no active agent");
return SSH_ERROR; return SSH_ERROR;
} }
agent_set_channel(session->agent, channel); agent_set_channel(session->agent, channel);
@@ -187,11 +199,15 @@ int ssh_set_agent_channel(ssh_session session, ssh_channel channel){
* @returns SSH_OK in case of success * @returns SSH_OK in case of success
* SSH_ERROR in case of an error * SSH_ERROR in case of an error
*/ */
int ssh_set_agent_socket(ssh_session session, socket_t fd){ int ssh_set_agent_socket(ssh_session session, socket_t fd)
if (!session) {
if (!session) {
return SSH_ERROR; return SSH_ERROR;
}
if (!session->agent) { if (!session->agent) {
ssh_set_error(session, SSH_REQUEST_DENIED, "Session has no active agent"); ssh_set_error(session,
SSH_REQUEST_DENIED,
"Session has no active agent");
return SSH_ERROR; return SSH_ERROR;
} }
@@ -203,7 +219,8 @@ int ssh_set_agent_socket(ssh_session session, socket_t fd){
* @} * @}
*/ */
void ssh_agent_close(struct ssh_agent_struct *agent) { void ssh_agent_close(struct ssh_agent_struct *agent)
{
if (agent == NULL) { if (agent == NULL) {
return; return;
} }
@@ -211,7 +228,8 @@ void ssh_agent_close(struct ssh_agent_struct *agent) {
ssh_socket_close(agent->sock); ssh_socket_close(agent->sock);
} }
void ssh_agent_free(ssh_agent agent) { void ssh_agent_free(ssh_agent agent)
{
if (agent) { if (agent) {
if (agent->ident) { if (agent->ident) {
SSH_BUFFER_FREE(agent->ident); SSH_BUFFER_FREE(agent->ident);
@@ -224,18 +242,20 @@ void ssh_agent_free(ssh_agent agent) {
} }
} }
static int agent_connect(ssh_session session) { static int agent_connect(ssh_session session)
{
const char *auth_sock = NULL; const char *auth_sock = NULL;
if (session == NULL || session->agent == NULL) { if (session == NULL || session->agent == NULL) {
return -1; return -1;
} }
if (session->agent->channel != NULL) if (session->agent->channel != NULL) {
return 0; return 0;
}
auth_sock = session->opts.agent_socket ? auth_sock = session->opts.agent_socket ? session->opts.agent_socket
session->opts.agent_socket : getenv("SSH_AUTH_SOCK"); : getenv("SSH_AUTH_SOCK");
if (auth_sock && *auth_sock) { if (auth_sock && *auth_sock) {
if (ssh_socket_unix(session->agent->sock, auth_sock) < 0) { if (ssh_socket_unix(session->agent->sock, auth_sock) < 0) {
@@ -268,7 +288,9 @@ static int agent_decode_reply(struct ssh_session_struct *session, int type) {
#endif #endif
static int agent_talk(struct ssh_session_struct *session, static int agent_talk(struct ssh_session_struct *session,
struct ssh_buffer_struct *request, struct ssh_buffer_struct *reply) { struct ssh_buffer_struct *request,
struct ssh_buffer_struct *reply)
{
uint32_t len = 0; uint32_t len = 0;
uint8_t tmpbuf[4]; uint8_t tmpbuf[4];
uint8_t *payload = tmpbuf; uint8_t *payload = tmpbuf;
@@ -280,10 +302,10 @@ static int agent_talk(struct ssh_session_struct *session,
/* send length and then the request packet */ /* send length and then the request packet */
if (atomicio(session->agent, payload, 4, 0) == 4) { if (atomicio(session->agent, payload, 4, 0) == 4) {
if (atomicio(session->agent, ssh_buffer_get(request), len, 0) if (atomicio(session->agent, ssh_buffer_get(request), len, 0) != len) {
!= len) { SSH_LOG(SSH_LOG_TRACE,
SSH_LOG(SSH_LOG_TRACE, "atomicio sending request failed: %s", "atomicio sending request failed: %s",
strerror(errno)); ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX));
return -1; return -1;
} }
} else { } else {
@@ -295,15 +317,18 @@ static int agent_talk(struct ssh_session_struct *session,
/* wait for response, read the length of the response packet */ /* wait for response, read the length of the response packet */
if (atomicio(session->agent, payload, 4, 1) != 4) { if (atomicio(session->agent, payload, 4, 1) != 4) {
SSH_LOG(SSH_LOG_TRACE, "atomicio read response length failed: %s", SSH_LOG(SSH_LOG_TRACE,
strerror(errno)); "atomicio read response length failed: %s",
ssh_strerror(errno, err_msg, SSH_ERRNO_MSG_MAX));
return -1; return -1;
} }
len = PULL_BE_U32(payload, 0); len = PULL_BE_U32(payload, 0);
if (len > 256 * 1024) { if (len > 256 * 1024) {
ssh_set_error(session, SSH_FATAL, ssh_set_error(session,
"Authentication response too long: %" PRIu32, len); SSH_FATAL,
"Authentication response too long: %" PRIu32,
len);
return -1; return -1;
} }
SSH_LOG(SSH_LOG_TRACE, "Response length: %" PRIu32, len); SSH_LOG(SSH_LOG_TRACE, "Response length: %" PRIu32, len);
@@ -471,7 +496,8 @@ ssh_key ssh_agent_get_next_ident(struct ssh_session_struct *session,
return key; return key;
} }
int ssh_agent_is_running(ssh_session session) { int ssh_agent_is_running(ssh_session session)
{
if (session == NULL || session->agent == NULL) { if (session == NULL || session->agent == NULL) {
return 0; return 0;
} }