diff --git a/src/auth.c b/src/auth.c index ea0aab0c..5ab2e0fa 100644 --- a/src/auth.c +++ b/src/auth.c @@ -66,7 +66,8 @@ * @returns SSH_AGAIN on nonblocking mode, if calling that function * again is necessary */ -static int ssh_userauth_request_service(ssh_session session) { +static int ssh_userauth_request_service(ssh_session session) +{ int rc; rc = ssh_service_request(session, "ssh-userauth"); @@ -78,7 +79,8 @@ static int ssh_userauth_request_service(ssh_session session) { return rc; } -static int ssh_auth_response_termination(void *user) { +static int ssh_auth_response_termination(void *user) +{ ssh_session session = (ssh_session)user; switch (session->auth.state) { case SSH_AUTH_STATE_NONE: @@ -139,7 +141,8 @@ static const char *ssh_auth_get_current_method(ssh_session session) * SSH_AUTH_AGAIN In nonblocking mode, call has to be made again * SSH_AUTH_ERROR Error during the process. */ -static int ssh_userauth_get_response(ssh_session session) { +static int ssh_userauth_get_response(ssh_session session) +{ int rc = SSH_AUTH_ERROR; rc = ssh_handle_packets_termination(session, SSH_TIMEOUT_USER, @@ -403,10 +406,11 @@ int ssh_userauth_list(ssh_session session, const char *username) * authentication. The username should only be set with ssh_options_set() only * before you connect to the server. */ -int ssh_userauth_none(ssh_session session, const char *username) { +int ssh_userauth_none(ssh_session session, const char *username) +{ int rc; - switch(session->pending_call_state) { + switch (session->pending_call_state) { case SSH_PENDING_CALL_NONE: break; case SSH_PENDING_CALL_AUTH_NONE: @@ -865,7 +869,8 @@ struct ssh_agent_state_struct { }; /* Internal function */ -void ssh_agent_state_free(void *data) { +void ssh_agent_state_free(void *data) +{ struct ssh_agent_state_struct *state = data; if (state) { @@ -898,7 +903,8 @@ void ssh_agent_state_free(void *data) { * before you connect to the server. */ int ssh_userauth_agent(ssh_session session, - const char *username) { + const char *username) +{ int rc = SSH_AUTH_ERROR; struct ssh_agent_state_struct *state; @@ -1326,10 +1332,11 @@ int ssh_userauth_publickey_auto(ssh_session session, */ int ssh_userauth_password(ssh_session session, const char *username, - const char *password) { + const char *password) +{ int rc; - switch(session->pending_call_state) { + switch (session->pending_call_state) { case SSH_PENDING_CALL_NONE: break; case SSH_PENDING_CALL_AUTH_PASSWORD: @@ -1417,7 +1424,8 @@ int ssh_userauth_agent_pubkey(ssh_session session, } #endif /* _WIN32 */ -ssh_kbdint ssh_kbdint_new(void) { +ssh_kbdint ssh_kbdint_new(void) +{ ssh_kbdint kbd; kbd = calloc(1, sizeof(struct ssh_kbdint_struct)); @@ -1429,7 +1437,8 @@ ssh_kbdint ssh_kbdint_new(void) { } -void ssh_kbdint_free(ssh_kbdint kbd) { +void ssh_kbdint_free(ssh_kbdint kbd) +{ size_t i, n; if (kbd == NULL) { @@ -1465,7 +1474,8 @@ void ssh_kbdint_free(ssh_kbdint kbd) { SAFE_FREE(kbd); } -void ssh_kbdint_clean(ssh_kbdint kbd) { +void ssh_kbdint_clean(ssh_kbdint kbd) +{ size_t i, n; if (kbd == NULL) { @@ -1743,7 +1753,8 @@ SSH_PACKET_CALLBACK(ssh_packet_userauth_info_request) { * @see ssh_userauth_kbdint_setanswer() */ int ssh_userauth_kbdint(ssh_session session, const char *user, - const char *submethods) { + const char *submethods) +{ int rc = SSH_AUTH_ERROR; if (session == NULL) { @@ -1785,7 +1796,8 @@ int ssh_userauth_kbdint(ssh_session session, const char *user, * * @returns The number of prompts. */ -int ssh_userauth_kbdint_getnprompts(ssh_session session) { +int ssh_userauth_kbdint_getnprompts(ssh_session session) +{ if (session == NULL) { return SSH_ERROR; } @@ -1807,7 +1819,8 @@ int ssh_userauth_kbdint_getnprompts(ssh_session session) { * * @returns The name of the message block. Do not free it. */ -const char *ssh_userauth_kbdint_getname(ssh_session session) { +const char *ssh_userauth_kbdint_getname(ssh_session session) +{ if (session == NULL) { return NULL; } @@ -1830,7 +1843,8 @@ const char *ssh_userauth_kbdint_getname(ssh_session session) { * @returns The instruction of the message block. */ -const char *ssh_userauth_kbdint_getinstruction(ssh_session session) { +const char *ssh_userauth_kbdint_getinstruction(ssh_session session) +{ if (session == NULL) return NULL; if (session->kbdint == NULL) { @@ -1865,8 +1879,9 @@ const char *ssh_userauth_kbdint_getinstruction(ssh_session session) { * if (echo) ... * @endcode */ -const char *ssh_userauth_kbdint_getprompt(ssh_session session, unsigned int i, - char *echo) { +const char * +ssh_userauth_kbdint_getprompt(ssh_session session, unsigned int i, char *echo) +{ if (session == NULL) return NULL; if (session->kbdint == NULL) { @@ -1893,7 +1908,8 @@ const char *ssh_userauth_kbdint_getprompt(ssh_session session, unsigned int i, * * @returns The number of answers. */ -int ssh_userauth_kbdint_getnanswers(ssh_session session) { +int ssh_userauth_kbdint_getnanswers(ssh_session session) +{ if (session == NULL || session->kbdint == NULL) { return SSH_ERROR; } @@ -1909,7 +1925,8 @@ int ssh_userauth_kbdint_getnanswers(ssh_session session) { * * @return 0 on success, < 0 on error. */ -const char *ssh_userauth_kbdint_getanswer(ssh_session session, unsigned int i) { +const char *ssh_userauth_kbdint_getanswer(ssh_session session, unsigned int i) +{ if (session == NULL || session->kbdint == NULL || session->kbdint->answers == NULL) { return NULL; @@ -1940,8 +1957,10 @@ const char *ssh_userauth_kbdint_getanswer(ssh_session session, unsigned int i) { * * @return 0 on success, < 0 on error. */ -int ssh_userauth_kbdint_setanswer(ssh_session session, unsigned int i, - const char *answer) { +int +ssh_userauth_kbdint_setanswer(ssh_session session, unsigned int i, + const char *answer) +{ if (session == NULL) { return -1; } @@ -1987,7 +2006,8 @@ int ssh_userauth_kbdint_setanswer(ssh_session session, unsigned int i, * SSH_AUTH_AGAIN: In nonblocking mode, you've got to call this again * later. */ -int ssh_userauth_gssapi(ssh_session session) { +int ssh_userauth_gssapi(ssh_session session) +{ int rc = SSH_AUTH_DENIED; #ifdef WITH_GSSAPI switch(session->pending_call_state) { diff --git a/src/channels.c b/src/channels.c index 3b82603d..93f79c9c 100644 --- a/src/channels.c +++ b/src/channels.c @@ -151,8 +151,9 @@ ssh_channel ssh_channel_new(ssh_session session) * * @return The new channel identifier. */ -uint32_t ssh_channel_new_id(ssh_session session) { - return ++(session->maxchannel); +uint32_t ssh_channel_new_id(ssh_session session) +{ + return ++(session->maxchannel); } /** @@ -267,7 +268,8 @@ error: return SSH_PACKET_USED; } -static int ssh_channel_open_termination(void *c){ +static int ssh_channel_open_termination(void *c) +{ ssh_channel channel = (ssh_channel) c; if (channel->state != SSH_CHANNEL_STATE_OPENING || channel->session->session_state == SSH_SESSION_STATE_ERROR) @@ -414,7 +416,7 @@ static int grow_window(ssh_session session, uint32_t new_window = minimumsize > WINDOWBASE ? minimumsize : WINDOWBASE; int rc; - if(new_window <= channel->local_window){ + if (new_window <= channel->local_window) { SSH_LOG(SSH_LOG_PROTOCOL, "growing window (channel %d:%d) to %d bytes : not needed (%d bytes)", channel->local_channel, channel->remote_channel, new_window, @@ -467,7 +469,8 @@ error: * @return The related ssh_channel, or NULL if the channel is * unknown or the packet is invalid. */ -static ssh_channel channel_from_msg(ssh_session session, ssh_buffer packet) { +static ssh_channel channel_from_msg(ssh_session session, ssh_buffer packet) +{ ssh_channel channel; uint32_t chan; int rc; @@ -968,8 +971,9 @@ int channel_default_bufferize(ssh_channel channel, * @see ssh_channel_request_shell() * @see ssh_channel_request_exec() */ -int ssh_channel_open_session(ssh_channel channel) { - if(channel == NULL) { +int ssh_channel_open_session(ssh_channel channel) +{ + if (channel == NULL) { return SSH_ERROR; } @@ -995,8 +999,9 @@ int ssh_channel_open_session(ssh_channel channel) { * * @see ssh_channel_open_forward() */ -int ssh_channel_open_auth_agent(ssh_channel channel){ - if(channel == NULL) { +int ssh_channel_open_auth_agent(ssh_channel channel) +{ + if (channel == NULL) { return SSH_ERROR; } @@ -1034,13 +1039,14 @@ int ssh_channel_open_auth_agent(ssh_channel channel){ * use channel_read and channel_write for this. */ int ssh_channel_open_forward(ssh_channel channel, const char *remotehost, - int remoteport, const char *sourcehost, int localport) { + int remoteport, const char *sourcehost, int localport) +{ ssh_session session; ssh_buffer payload = NULL; ssh_string str = NULL; int rc = SSH_ERROR; - if(channel == NULL) { + if (channel == NULL) { return rc; } @@ -1387,7 +1393,8 @@ error: } /* this termination function waits for a window growing condition */ -static int ssh_channel_waitwindow_termination(void *c){ +static int ssh_channel_waitwindow_termination(void *c) +{ ssh_channel channel = (ssh_channel) c; if (channel->remote_window > 0 || channel->session->session_state == SSH_SESSION_STATE_ERROR || @@ -1400,7 +1407,8 @@ static int ssh_channel_waitwindow_termination(void *c){ /* This termination function waits until the session is not in blocked status * anymore, e.g. because of a key re-exchange. */ -static int ssh_waitsession_unblocked(void *s){ +static int ssh_waitsession_unblocked(void *s) +{ ssh_session session = (ssh_session)s; switch (session->session_state){ case SSH_SESSION_STATE_DH: @@ -1420,8 +1428,9 @@ static int ssh_waitsession_unblocked(void *s){ * SSH_ERROR On error. * SSH_AGAIN Timeout elapsed (or in nonblocking mode). */ -int ssh_channel_flush(ssh_channel channel){ - return ssh_blocking_flush(channel->session, SSH_TIMEOUT_DEFAULT); +int ssh_channel_flush(ssh_channel channel) +{ + return ssh_blocking_flush(channel->session, SSH_TIMEOUT_DEFAULT); } static int channel_write_common(ssh_channel channel, @@ -1584,7 +1593,8 @@ error: * @warning A zero return value means ssh_channel_write (default settings) * will block until the window grows back. */ -uint32_t ssh_channel_window_size(ssh_channel channel) { +uint32_t ssh_channel_window_size(ssh_channel channel) +{ return channel->remote_window; } @@ -1601,8 +1611,9 @@ uint32_t ssh_channel_window_size(ssh_channel channel) { * * @see ssh_channel_read() */ -int ssh_channel_write(ssh_channel channel, const void *data, uint32_t len) { - return channel_write_common(channel, data, len, 0); +int ssh_channel_write(ssh_channel channel, const void *data, uint32_t len) +{ + return channel_write_common(channel, data, len, 0); } /** @@ -1614,8 +1625,9 @@ int ssh_channel_write(ssh_channel channel, const void *data, uint32_t len) { * * @see ssh_channel_is_closed() */ -int ssh_channel_is_open(ssh_channel channel) { - if(channel == NULL) { +int ssh_channel_is_open(ssh_channel channel) +{ + if (channel == NULL) { return 0; } return (channel->state == SSH_CHANNEL_STATE_OPEN && channel->session->alive != 0); @@ -1630,8 +1642,9 @@ int ssh_channel_is_open(ssh_channel channel) { * * @see ssh_channel_is_open() */ -int ssh_channel_is_closed(ssh_channel channel) { - if(channel == NULL) { +int ssh_channel_is_closed(ssh_channel channel) +{ + if (channel == NULL) { return SSH_ERROR; } return (channel->state != SSH_CHANNEL_STATE_OPEN || channel->session->alive == 0); @@ -1644,15 +1657,16 @@ int ssh_channel_is_closed(ssh_channel channel) { * * @return 0 if there is no EOF, nonzero otherwise. */ -int ssh_channel_is_eof(ssh_channel channel) { - if(channel == NULL) { - return SSH_ERROR; - } - if (ssh_channel_has_unread_data(channel)) { - return 0; - } +int ssh_channel_is_eof(ssh_channel channel) +{ + if (channel == NULL) { + return SSH_ERROR; + } + if (ssh_channel_has_unread_data(channel)) { + return 0; + } - return (channel->remote_eof != 0); + return (channel->remote_eof != 0); } /** @@ -1666,11 +1680,12 @@ int ssh_channel_is_eof(ssh_channel channel) { * in non-blocking mode. * @see ssh_set_blocking() */ -void ssh_channel_set_blocking(ssh_channel channel, int blocking) { - if(channel == NULL) { - return; - } - ssh_set_blocking(channel->session,blocking); +void ssh_channel_set_blocking(ssh_channel channel, int blocking) +{ + if (channel == NULL) { + return; + } + ssh_set_blocking(channel->session, blocking); } /** @@ -1734,7 +1749,8 @@ SSH_PACKET_CALLBACK(ssh_packet_channel_failure){ return SSH_PACKET_USED; } -static int ssh_channel_request_termination(void *c){ +static int ssh_channel_request_termination(void *c) +{ ssh_channel channel = (ssh_channel)c; if(channel->request_state != SSH_CHANNEL_REQ_STATE_PENDING || channel->session->session_state == SSH_SESSION_STATE_ERROR) @@ -1744,7 +1760,8 @@ static int ssh_channel_request_termination(void *c){ } static int channel_request(ssh_channel channel, const char *request, - ssh_buffer buffer, int reply) { + ssh_buffer buffer, int reply) +{ ssh_session session = channel->session; int rc = SSH_ERROR; int ret; @@ -1844,7 +1861,8 @@ error: * to be done again. */ int ssh_channel_request_pty_size(ssh_channel channel, const char *terminal, - int col, int row) { + int col, int row) +{ ssh_session session; ssh_buffer buffer = NULL; int rc = SSH_ERROR; @@ -1906,7 +1924,8 @@ error: * * @see ssh_channel_request_pty_size() */ -int ssh_channel_request_pty(ssh_channel channel) { +int ssh_channel_request_pty(ssh_channel channel) +{ return ssh_channel_request_pty_size(channel, "xterm", 80, 24); } @@ -1925,7 +1944,8 @@ int ssh_channel_request_pty(ssh_channel channel) { * libssh function using the same channel/session is running at the * same time (not 100% threadsafe). */ -int ssh_channel_change_pty_size(ssh_channel channel, int cols, int rows) { +int ssh_channel_change_pty_size(ssh_channel channel, int cols, int rows) +{ ssh_session session = channel->session; ssh_buffer buffer = NULL; int rc = SSH_ERROR; @@ -1964,8 +1984,9 @@ error: * SSH_AGAIN if in nonblocking mode and call has * to be done again. */ -int ssh_channel_request_shell(ssh_channel channel) { - if(channel == NULL) { +int ssh_channel_request_shell(ssh_channel channel) +{ + if (channel == NULL) { return SSH_ERROR; } @@ -1986,7 +2007,8 @@ int ssh_channel_request_shell(ssh_channel channel) { * * @warning You normally don't have to call it for sftp, see sftp_new(). */ -int ssh_channel_request_subsystem(ssh_channel channel, const char *subsys) { +int ssh_channel_request_subsystem(ssh_channel channel, const char *subsys) +{ ssh_buffer buffer = NULL; int rc = SSH_ERROR; @@ -2035,14 +2057,16 @@ error: * * @note You should use sftp_new() which does this for you. */ -int ssh_channel_request_sftp( ssh_channel channel){ +int ssh_channel_request_sftp( ssh_channel channel) +{ if(channel == NULL) { return SSH_ERROR; } return ssh_channel_request_subsystem(channel, "sftp"); } -static char *generate_cookie(void) { +static char *generate_cookie(void) +{ static const char *hex = "0123456789abcdef"; char s[36]; unsigned char rnd[16]; @@ -2088,7 +2112,8 @@ static char *generate_cookie(void) { * to be done again. */ int ssh_channel_request_x11(ssh_channel channel, int single_connection, const char *protocol, - const char *cookie, int screen_number) { + const char *cookie, int screen_number) +{ ssh_buffer buffer = NULL; char *c = NULL; int rc = SSH_ERROR; @@ -2139,7 +2164,8 @@ error: } static ssh_channel ssh_channel_accept(ssh_session session, int channeltype, - int timeout_ms, int *destination_port, char **originator, int *originator_port) { + int timeout_ms, int *destination_port, char **originator, int *originator_port) +{ #ifndef _WIN32 static const struct timespec ts = { .tv_sec = 0, @@ -2209,8 +2235,9 @@ static ssh_channel ssh_channel_accept(ssh_session session, int channeltype, * @return A newly created channel, or NULL if no X11 request from * the server. */ -ssh_channel ssh_channel_accept_x11(ssh_channel channel, int timeout_ms) { - return ssh_channel_accept(channel->session, SSH_CHANNEL_X11, timeout_ms, NULL, NULL, NULL); +ssh_channel ssh_channel_accept_x11(ssh_channel channel, int timeout_ms) +{ + return ssh_channel_accept(channel->session, SSH_CHANNEL_X11, timeout_ms, NULL, NULL, NULL); } /** @@ -2279,7 +2306,8 @@ SSH_PACKET_CALLBACK(ssh_request_denied){ } -static int ssh_global_request_termination(void *s){ +static int ssh_global_request_termination(void *s) +{ ssh_session session = (ssh_session) s; if (session->global_req_state != SSH_CHANNEL_REQ_STATE_PENDING || session->session_state == SSH_SESSION_STATE_ERROR) @@ -2458,13 +2486,15 @@ error: } /* DEPRECATED */ -int ssh_forward_listen(ssh_session session, const char *address, int port, int *bound_port) { +int ssh_forward_listen(ssh_session session, const char *address, int port, int *bound_port) +{ return ssh_channel_listen_forward(session, address, port, bound_port); } /* DEPRECATED */ -ssh_channel ssh_forward_accept(ssh_session session, int timeout_ms) { - return ssh_channel_accept(session, SSH_CHANNEL_FORWARDED_TCPIP, timeout_ms, NULL, NULL, NULL); +ssh_channel ssh_forward_accept(ssh_session session, int timeout_ms) +{ + return ssh_channel_accept(session, SSH_CHANNEL_FORWARDED_TCPIP, timeout_ms, NULL, NULL, NULL); } /** @@ -2553,7 +2583,8 @@ error: } /* DEPRECATED */ -int ssh_forward_cancel(ssh_session session, const char *address, int port) { +int ssh_forward_cancel(ssh_session session, const char *address, int port) +{ return ssh_channel_cancel_forward(session, address, port); } @@ -2572,7 +2603,8 @@ int ssh_forward_cancel(ssh_session session, const char *address, int port) { * to be done again. * @warning Some environment variables may be refused by security reasons. */ -int ssh_channel_request_env(ssh_channel channel, const char *name, const char *value) { +int ssh_channel_request_env(ssh_channel channel, const char *name, const char *value) +{ ssh_buffer buffer = NULL; int rc = SSH_ERROR; @@ -2642,7 +2674,8 @@ error: * * @see ssh_channel_request_shell() */ -int ssh_channel_request_exec(ssh_channel channel, const char *cmd) { +int ssh_channel_request_exec(ssh_channel channel, const char *cmd) +{ ssh_buffer buffer = NULL; int rc = SSH_ERROR; @@ -2707,14 +2740,15 @@ error: * * @return SSH_OK on success, SSH_ERROR if an error occurred. */ -int ssh_channel_request_send_signal(ssh_channel channel, const char *sig) { +int ssh_channel_request_send_signal(ssh_channel channel, const char *sig) +{ ssh_buffer buffer = NULL; int rc = SSH_ERROR; - if(channel == NULL) { + if (channel == NULL) { return SSH_ERROR; } - if(sig == NULL) { + if (sig == NULL) { ssh_set_error_invalid(channel->session); return rc; } @@ -2751,7 +2785,8 @@ error: * * @return SSH_OK on success, SSH_ERROR if an error occurred */ -int ssh_channel_request_send_break(ssh_channel channel, uint32_t length) { +int ssh_channel_request_send_break(ssh_channel channel, uint32_t length) +{ ssh_buffer buffer = NULL; int rc = SSH_ERROR; @@ -2784,7 +2819,7 @@ error: * * @param[in] channel The channel to read from. * - * @param[in] buffer The buffer which will get the data. + * @param[out] buffer The buffer which will get the data. * * @param[in] count The count of bytes to be read. If it is bigger than 0, * the exact size will be read, else (bytes=0) it will @@ -2799,7 +2834,8 @@ error: * @see ssh_channel_read */ int channel_read_buffer(ssh_channel channel, ssh_buffer buffer, uint32_t count, - int is_stderr) { + int is_stderr) +{ ssh_session session; char *buffer_tmp = NULL; int r; @@ -2874,7 +2910,8 @@ struct ssh_channel_read_termination_struct { ssh_buffer buffer; }; -static int ssh_channel_read_termination(void *s){ +static int ssh_channel_read_termination(void *s) +{ struct ssh_channel_read_termination_struct *ctx = s; if (ssh_buffer_get_len(ctx->buffer) >= ctx->count || ctx->channel->remote_eof || @@ -2891,14 +2928,14 @@ static int ssh_channel_read_termination(void *s){ * * @param[in] channel The channel to read from. * - * @param[in] dest The destination buffer which will get the data. + * @param[out] dest The destination buffer which will get the data. * * @param[in] count The count of bytes to be read. * * @param[in] is_stderr A boolean value to mark reading from the stderr flow. * * @return The number of bytes read, 0 on end of file or SSH_ERROR - * on error. In nonblocking mode it Can return 0 if no data + * on error. In nonblocking mode it can return 0 if no data * is available or SSH_AGAIN. * * @warning This function may return less than count bytes of data, and won't @@ -2920,7 +2957,7 @@ int ssh_channel_read(ssh_channel channel, void *dest, uint32_t count, int is_std * * @param[in] channel The channel to read from. * - * @param[in] dest The destination buffer which will get the data. + * @param[out] dest The destination buffer which will get the data. * * @param[in] count The count of bytes to be read. * @@ -3050,7 +3087,7 @@ int ssh_channel_read_timeout(ssh_channel channel, * * @param[in] channel The channel to read from. * - * @param[in] dest A pointer to a destination buffer. + * @param[out] dest A pointer to a destination buffer. * * @param[in] count The count of bytes of data to be read. * @@ -3120,7 +3157,8 @@ int ssh_channel_read_nonblocking(ssh_channel channel, * * @see ssh_channel_is_eof() */ -int ssh_channel_poll(ssh_channel channel, int is_stderr){ +int ssh_channel_poll(ssh_channel channel, int is_stderr) +{ ssh_buffer stdbuf; if ((channel == NULL) || (channel->flags & SSH_CHANNEL_FLAG_FREED_LOCAL)) { @@ -3233,15 +3271,17 @@ out: * * @return The session pointer. */ -ssh_session ssh_channel_get_session(ssh_channel channel) { - if(channel == NULL) { +ssh_session ssh_channel_get_session(ssh_channel channel) +{ + if (channel == NULL) { return NULL; } return channel->session; } -static int ssh_channel_exit_status_termination(void *c){ +static int ssh_channel_exit_status_termination(void *c) +{ ssh_channel channel = c; if(channel->exit_status != -1 || /* When a channel is closed, no exit status message can @@ -3271,7 +3311,8 @@ static int ssh_channel_exit_status_termination(void *c){ * * @see ssh_channel_exit_status_callback */ -int ssh_channel_get_exit_status(ssh_channel channel) { +int ssh_channel_get_exit_status(ssh_channel channel) +{ int rc; if ((channel == NULL) || (channel->flags & SSH_CHANNEL_FLAG_FREED_LOCAL)) { return SSH_ERROR; @@ -3295,8 +3336,11 @@ int ssh_channel_get_exit_status(ssh_channel channel) { * This is made in two parts: protocol select and network select. The protocol * select does not use the network functions at all */ -static int channel_protocol_select(ssh_channel *rchans, ssh_channel *wchans, - ssh_channel *echans, ssh_channel *rout, ssh_channel *wout, ssh_channel *eout) { +static int +channel_protocol_select(ssh_channel *rchans, ssh_channel *wchans, + ssh_channel *echans, ssh_channel *rout, + ssh_channel *wout, ssh_channel *eout) +{ ssh_channel chan; int i; int j = 0; @@ -3376,7 +3420,8 @@ static size_t count_ptrs(ssh_channel *ptrs) * function, or SSH_ERROR on error. */ int ssh_channel_select(ssh_channel *readchans, ssh_channel *writechans, - ssh_channel *exceptchans, struct timeval * timeout) { + ssh_channel *exceptchans, struct timeval * timeout) +{ ssh_channel *rchans, *wchans, *echans; ssh_channel dummy = NULL; ssh_event event = NULL; @@ -3510,7 +3555,8 @@ int ssh_channel_select(ssh_channel *readchans, ssh_channel *writechans, * @param[in] counter Counter for bytes handled by the channel. */ void ssh_channel_set_counter(ssh_channel channel, - ssh_counter counter) { + ssh_counter counter) +{ if (channel != NULL) { channel->counter = counter; } @@ -3529,8 +3575,9 @@ void ssh_channel_set_counter(ssh_channel channel, * * @see ssh_channel_read() */ -int ssh_channel_write_stderr(ssh_channel channel, const void *data, uint32_t len) { - return channel_write_common(channel, data, len, 1); +int ssh_channel_write_stderr(ssh_channel channel, const void *data, uint32_t len) +{ + return channel_write_common(channel, data, len, 1); } #if WITH_SERVER @@ -3560,7 +3607,8 @@ int ssh_channel_write_stderr(ssh_channel channel, const void *data, uint32_t len * use channel_read and channel_write for this. */ int ssh_channel_open_reverse_forward(ssh_channel channel, const char *remotehost, - int remoteport, const char *sourcehost, int localport) { + int remoteport, const char *sourcehost, int localport) +{ ssh_session session; ssh_buffer payload = NULL; int rc = SSH_ERROR; @@ -3623,7 +3671,8 @@ error: * use channel_read and channel_write for this. */ int ssh_channel_open_x11(ssh_channel channel, - const char *orig_addr, int orig_port) { + const char *orig_addr, int orig_port) +{ ssh_session session; ssh_buffer payload = NULL; int rc = SSH_ERROR; @@ -3679,7 +3728,8 @@ error: * * @return SSH_OK on success, SSH_ERROR if an error occurred. */ -int ssh_channel_request_send_exit_status(ssh_channel channel, int exit_status) { +int ssh_channel_request_send_exit_status(ssh_channel channel, int exit_status) +{ ssh_buffer buffer = NULL; int rc = SSH_ERROR; @@ -3723,7 +3773,8 @@ error: * @return SSH_OK on success, SSH_ERROR if an error occurred */ int ssh_channel_request_send_exit_signal(ssh_channel channel, const char *sig, - int core, const char *errmsg, const char *lang) { + int core, const char *errmsg, const char *lang) +{ ssh_buffer buffer = NULL; int rc = SSH_ERROR; diff --git a/src/client.c b/src/client.c index 43ce1b0d..98c8f803 100644 --- a/src/client.c +++ b/src/client.c @@ -60,7 +60,8 @@ * @param code one of SSH_SOCKET_CONNECTED_OK or SSH_SOCKET_CONNECTED_ERROR * @param user is a pointer to session */ -static void socket_callback_connected(int code, int errno_code, void *user){ +static void socket_callback_connected(int code, int errno_code, void *user) +{ ssh_session session=(ssh_session)user; if (session->session_state != SSH_SESSION_STATE_CONNECTING && @@ -96,10 +97,10 @@ static void socket_callback_connected(int code, int errno_code, void *user){ static int callback_receive_banner(const void *data, size_t len, void *user) { char *buffer = (char *)data; - ssh_session session=(ssh_session) user; + ssh_session session = (ssh_session) user; char *str = NULL; size_t i; - int ret=0; + int ret = 0; if (session->session_state != SSH_SESSION_STATE_SOCKET_CONNECTED) { ssh_set_error(session,SSH_FATAL, @@ -243,8 +244,8 @@ end: * @warning this function returning is no proof that DH handshake is * completed */ -static int dh_handshake(ssh_session session) { - +static int dh_handshake(ssh_session session) +{ int rc = SSH_AGAIN; switch (session->dh_handshake_state) { @@ -299,13 +300,15 @@ static int dh_handshake(ssh_session session) { return rc; } -static int ssh_service_request_termination(void *s){ - ssh_session session = (ssh_session)s; - if(session->session_state == SSH_SESSION_STATE_ERROR || - session->auth.service_state != SSH_AUTH_SERVICE_SENT) - return 1; - else - return 0; +static int ssh_service_request_termination(void *s) +{ + ssh_session session = (ssh_session)s; + + if (session->session_state == SSH_SESSION_STATE_ERROR || + session->auth.service_state != SSH_AUTH_SERVICE_SENT) + return 1; + else + return 0; } /** @@ -323,8 +326,9 @@ static int ssh_service_request_termination(void *s){ * @return SSH_AGAIN No response received yet * @bug actually only works with ssh-userauth */ -int ssh_service_request(ssh_session session, const char *service) { - int rc=SSH_ERROR; +int ssh_service_request(ssh_session session, const char *service) +{ + int rc = SSH_ERROR; if(session->auth.service_state != SSH_AUTH_SERVICE_NONE) goto pending; @@ -481,16 +485,18 @@ error: /** @internal * @brief describe under which conditions the ssh_connect function may stop */ -static int ssh_connect_termination(void *user){ - ssh_session session = (ssh_session)user; - switch(session->session_state){ +static int ssh_connect_termination(void *user) +{ + ssh_session session = (ssh_session)user; + + switch (session->session_state) { case SSH_SESSION_STATE_ERROR: case SSH_SESSION_STATE_AUTHENTICATING: case SSH_SESSION_STATE_DISCONNECTED: - return 1; + return 1; default: - return 0; - } + return 0; + } } /** @@ -649,12 +655,13 @@ pending: * * @return A newly allocated string with the banner, NULL on error. */ -char *ssh_get_issue_banner(ssh_session session) { - if (session == NULL || session->banner == NULL) { - return NULL; - } +char *ssh_get_issue_banner(ssh_session session) +{ + if (session == NULL || session->banner == NULL) { + return NULL; + } - return ssh_string_to_char(session->banner); + return ssh_string_to_char(session->banner); } /** @@ -675,12 +682,13 @@ char *ssh_get_issue_banner(ssh_session session) { * } * @endcode */ -int ssh_get_openssh_version(ssh_session session) { - if (session == NULL) { - return 0; - } +int ssh_get_openssh_version(ssh_session session) +{ + if (session == NULL) { + return 0; + } - return session->openssh; + return session->openssh; } /** @@ -774,7 +782,8 @@ error: } } -const char *ssh_copyright(void) { +const char *ssh_copyright(void) +{ return SSH_STRINGIFY(LIBSSH_VERSION) " (c) 2003-2022 " "Aris Adamantiadis, Andreas Schneider " "and libssh contributors. " diff --git a/src/getpass.c b/src/getpass.c index c00d0f54..6be33c77 100644 --- a/src/getpass.c +++ b/src/getpass.c @@ -44,7 +44,8 @@ * * @return 1 on success, 0 on error. */ -static int ssh_gets(const char *prompt, char *buf, size_t len, int verify) { +static int ssh_gets(const char *prompt, char *buf, size_t len, int verify) +{ char *tmp; char *ptr = NULL; int ok = 0; @@ -121,7 +122,8 @@ int ssh_getpass(const char *prompt, char *buf, size_t len, int echo, - int verify) { + int verify) +{ HANDLE h; DWORD mode = 0; int ok; @@ -213,7 +215,8 @@ int ssh_getpass(const char *prompt, char *buf, size_t len, int echo, - int verify) { + int verify) +{ struct termios attr; struct termios old_attr; int ok = 0; diff --git a/src/gssapi.c b/src/gssapi.c index 2cbfba1a..bbfcb6e6 100644 --- a/src/gssapi.c +++ b/src/gssapi.c @@ -68,7 +68,8 @@ struct ssh_gssapi_struct{ /** @internal * @initializes a gssapi context for authentication */ -static int ssh_gssapi_init(ssh_session session){ +static int ssh_gssapi_init(ssh_session session) +{ if (session->gssapi != NULL) return SSH_OK; session->gssapi = malloc(sizeof(struct ssh_gssapi_struct)); @@ -87,7 +88,8 @@ static int ssh_gssapi_init(ssh_session session){ /** @internal * @frees a gssapi context */ -static void ssh_gssapi_free(ssh_session session){ +static void ssh_gssapi_free(ssh_session session) +{ OM_uint32 min; if (session->gssapi == NULL) return; @@ -114,7 +116,8 @@ SSH_PACKET_CALLBACK(ssh_packet_userauth_gssapi_token){ * @brief sends a SSH_MSG_USERAUTH_GSSAPI_RESPONSE packet * @param[in] oid the OID that was selected for authentication */ -static int ssh_gssapi_send_response(ssh_session session, ssh_string oid){ +static int ssh_gssapi_send_response(ssh_session session, ssh_string oid) +{ if (ssh_buffer_add_u8(session->out_buffer, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE) < 0 || ssh_buffer_add_ssh_string(session->out_buffer,oid) < 0) { ssh_set_error_oom(session); @@ -184,8 +187,11 @@ out: /** @internal * @brief handles an user authentication using GSSAPI */ -int ssh_gssapi_handle_userauth(ssh_session session, const char *user, uint32_t n_oid, ssh_string *oids){ - char service_name[]="host"; +int +ssh_gssapi_handle_userauth(ssh_session session, const char *user, + uint32_t n_oid, ssh_string *oids) +{ + char service_name[] = "host"; gss_buffer_desc name_buf; gss_name_t server_name; /* local server fqdn */ OM_uint32 maj_stat, min_stat; @@ -327,7 +333,8 @@ int ssh_gssapi_handle_userauth(ssh_session session, const char *user, uint32_t n return ssh_gssapi_send_response(session, oids[i]); } -static char *ssh_gssapi_name_to_char(gss_name_t name){ +static char *ssh_gssapi_name_to_char(gss_name_t name) +{ gss_buffer_desc buffer; OM_uint32 maj_stat, min_stat; char *ptr; @@ -572,7 +579,8 @@ end: * @returns gssapi credentials handle. * @returns NULL if no forwardable token is available. */ -ssh_gssapi_creds ssh_gssapi_get_creds(ssh_session session){ +ssh_gssapi_creds ssh_gssapi_get_creds(ssh_session session) +{ if (!session || !session->gssapi || session->gssapi->client_creds == GSS_C_NO_CREDENTIAL) return NULL; return (ssh_gssapi_creds)session->gssapi->client_creds; @@ -602,7 +610,9 @@ void ssh_gssapi_set_creds(ssh_session session, const ssh_gssapi_creds creds) session->gssapi->client.client_deleg_creds = (gss_cred_id_t)creds; } -static int ssh_gssapi_send_auth_mic(ssh_session session, ssh_string *oid_set, int n_oid){ +static int +ssh_gssapi_send_auth_mic(ssh_session session, ssh_string *oid_set, int n_oid) +{ int rc; int i; @@ -711,7 +721,8 @@ end: * SSH_AUTH_AGAIN: In nonblocking mode, you've got to call this again * later. */ -int ssh_gssapi_auth_mic(ssh_session session){ +int ssh_gssapi_auth_mic(ssh_session session) +{ size_t i; gss_OID_set selected; /* oid selected for authentication */ ssh_string *oids = NULL; @@ -902,7 +913,8 @@ error: return SSH_PACKET_USED; } -static int ssh_gssapi_send_mic(ssh_session session){ +static int ssh_gssapi_send_mic(ssh_session session) +{ OM_uint32 maj_stat, min_stat; gss_buffer_desc mic_buf = GSS_C_EMPTY_BUFFER; gss_buffer_desc mic_token_buf = GSS_C_EMPTY_BUFFER; diff --git a/src/misc.c b/src/misc.c index 0d455df2..f7efb9df 100644 --- a/src/misc.c +++ b/src/misc.c @@ -104,7 +104,8 @@ */ #ifdef _WIN32 -char *ssh_get_user_home_dir(void) { +char *ssh_get_user_home_dir(void) +{ char tmp[PATH_MAX] = {0}; char *szPath = NULL; @@ -122,12 +123,13 @@ char *ssh_get_user_home_dir(void) { } /* we have read access on file */ -int ssh_file_readaccess_ok(const char *file) { - if (_access(file, 4) < 0) { - return 0; - } +int ssh_file_readaccess_ok(const char *file) +{ + if (_access(file, 4) < 0) { + return 0; + } - return 1; + return 1; } /** @@ -158,7 +160,8 @@ int ssh_dir_writeable(const char *path) #define SSH_USEC_IN_SEC 1000000LL #define SSH_SECONDS_SINCE_1601 11644473600LL -int gettimeofday(struct timeval *__p, void *__t) { +int gettimeofday(struct timeval *__p, void *__t) +{ union { unsigned long long ns100; /* time since 1 Jan 1601 in 100ns units */ FILETIME ft; @@ -171,7 +174,8 @@ int gettimeofday(struct timeval *__p, void *__t) { return (0); } -char *ssh_get_local_username(void) { +char *ssh_get_local_username(void) +{ DWORD size = 0; char *user; @@ -190,7 +194,8 @@ char *ssh_get_local_username(void) { return NULL; } -int ssh_is_ipaddr_v4(const char *str) { +int ssh_is_ipaddr_v4(const char *str) +{ struct sockaddr_storage ss; int sslen = sizeof(ss); int rc = SOCKET_ERROR; @@ -212,7 +217,8 @@ int ssh_is_ipaddr_v4(const char *str) { return 0; } -int ssh_is_ipaddr(const char *str) { +int ssh_is_ipaddr(const char *str) +{ int rc = SOCKET_ERROR; if (strchr(str, ':')) { @@ -319,7 +325,8 @@ char *ssh_get_local_username(void) return name; } -int ssh_is_ipaddr_v4(const char *str) { +int ssh_is_ipaddr_v4(const char *str) +{ int rc = -1; struct in_addr dest; @@ -331,7 +338,8 @@ int ssh_is_ipaddr_v4(const char *str) { return 0; } -int ssh_is_ipaddr(const char *str) { +int ssh_is_ipaddr(const char *str) +{ int rc = -1; if (strchr(str, ':')) { @@ -349,7 +357,8 @@ int ssh_is_ipaddr(const char *str) { #endif /* _WIN32 */ -char *ssh_lowercase(const char* str) { +char *ssh_lowercase(const char* str) +{ char *new, *p; if (str == NULL) { @@ -392,15 +401,17 @@ char *ssh_hostport(const char *host, int port) * @brief Convert a buffer into a colon separated hex string. * The caller has to free the memory. * - * @param what What should be converted to a hex string. + * @param[in] what What should be converted to a hex string. * - * @param len Length of the buffer to convert. + * @param[in] len Length of the buffer to convert. * - * @return The hex string or NULL on error. + * @return The hex string or NULL on error. The memory needs + * to be freed using ssh_string_free_char(). * * @see ssh_string_free_char() */ -char *ssh_get_hexa(const unsigned char *what, size_t len) { +char *ssh_get_hexa(const unsigned char *what, size_t len) +{ const char h[] = "0123456789abcdef"; char *hexa; size_t i; @@ -428,7 +439,8 @@ char *ssh_get_hexa(const unsigned char *what, size_t len) { /** * @deprecated Please use ssh_print_hash() instead */ -void ssh_print_hexa(const char *descr, const unsigned char *what, size_t len) { +void ssh_print_hexa(const char *descr, const unsigned char *what, size_t len) +{ char *hexa = ssh_get_hexa(what, len); if (hexa == NULL) { @@ -649,48 +661,54 @@ error: * } * @endcode */ -const char *ssh_version(int req_version) { - if (req_version <= LIBSSH_VERSION_INT) { - return SSH_STRINGIFY(LIBSSH_VERSION) GCRYPT_STRING CRYPTO_STRING MBED_STRING - ZLIB_STRING; - } +const char *ssh_version(int req_version) +{ + if (req_version <= LIBSSH_VERSION_INT) { + return SSH_STRINGIFY(LIBSSH_VERSION) GCRYPT_STRING CRYPTO_STRING + MBED_STRING ZLIB_STRING; + } - return NULL; -} - -struct ssh_list *ssh_list_new(void) { - struct ssh_list *ret=malloc(sizeof(struct ssh_list)); - if(!ret) return NULL; - ret->root=ret->end=NULL; - return ret; } -void ssh_list_free(struct ssh_list *list){ - struct ssh_iterator *ptr,*next; - if(!list) - return; - ptr=list->root; - while(ptr){ - next=ptr->next; - SAFE_FREE(ptr); - ptr=next; - } - SAFE_FREE(list); +struct ssh_list *ssh_list_new(void) +{ + struct ssh_list *ret = malloc(sizeof(struct ssh_list)); + if (!ret) + return NULL; + ret->root = ret->end = NULL; + return ret; } -struct ssh_iterator *ssh_list_get_iterator(const struct ssh_list *list){ - if(!list) +void ssh_list_free(struct ssh_list *list) +{ + struct ssh_iterator *ptr, *next; + if (!list) + return; + ptr = list->root; + while (ptr) { + next = ptr->next; + SAFE_FREE(ptr); + ptr = next; + } + SAFE_FREE(list); +} + +struct ssh_iterator *ssh_list_get_iterator(const struct ssh_list *list) +{ + if (!list) + return NULL; + return list->root; +} + +struct ssh_iterator *ssh_list_find(const struct ssh_list *list, void *value) +{ + struct ssh_iterator *it; + + for (it = ssh_list_get_iterator(list); it != NULL ; it = it->next) + if (it->data == value) + return it; return NULL; - return list->root; -} - -struct ssh_iterator *ssh_list_find(const struct ssh_list *list, void *value){ - struct ssh_iterator *it; - for(it = ssh_list_get_iterator(list); it != NULL ;it=it->next) - if(it->data==value) - return it; - return NULL; } /** @@ -712,16 +730,19 @@ size_t ssh_list_count(const struct ssh_list *list) return count; } -static struct ssh_iterator *ssh_iterator_new(const void *data){ - struct ssh_iterator *iterator=malloc(sizeof(struct ssh_iterator)); - if(!iterator) - return NULL; - iterator->next=NULL; - iterator->data=data; - return iterator; +static struct ssh_iterator *ssh_iterator_new(const void *data) +{ + struct ssh_iterator *iterator = malloc(sizeof(struct ssh_iterator)); + + if (!iterator) + return NULL; + iterator->next = NULL; + iterator->data = data; + return iterator; } -int ssh_list_append(struct ssh_list *list,const void *data){ +int ssh_list_append(struct ssh_list *list,const void *data) +{ struct ssh_iterator *iterator = NULL; if (list == NULL) { @@ -744,7 +765,8 @@ int ssh_list_append(struct ssh_list *list,const void *data){ return SSH_OK; } -int ssh_list_prepend(struct ssh_list *list, const void *data){ +int ssh_list_prepend(struct ssh_list *list, const void *data) +{ struct ssh_iterator *it = NULL; if (list == NULL) { @@ -768,8 +790,9 @@ int ssh_list_prepend(struct ssh_list *list, const void *data){ return SSH_OK; } -void ssh_list_remove(struct ssh_list *list, struct ssh_iterator *iterator){ - struct ssh_iterator *ptr,*prev; +void ssh_list_remove(struct ssh_list *list, struct ssh_iterator *iterator) +{ + struct ssh_iterator *ptr, *prev; if (list == NULL) { return; @@ -808,7 +831,8 @@ void ssh_list_remove(struct ssh_list *list, struct ssh_iterator *iterator){ * @returns A pointer to the element being stored in head, or NULL * if the list is empty. */ -const void *_ssh_list_pop_head(struct ssh_list *list){ +const void *_ssh_list_pop_head(struct ssh_list *list) +{ struct ssh_iterator *iterator = NULL; const void *data = NULL; @@ -847,7 +871,8 @@ const void *_ssh_list_pop_head(struct ssh_list *list){ * * @see ssh_string_free_char() */ -char *ssh_dirname (const char *path) { +char *ssh_dirname (const char *path) +{ char *new = NULL; size_t len; @@ -905,7 +930,8 @@ char *ssh_dirname (const char *path) { * * @see ssh_string_free_char() */ -char *ssh_basename (const char *path) { +char *ssh_basename (const char *path) +{ char *new = NULL; const char *s; size_t len; @@ -1043,7 +1069,8 @@ int ssh_mkdirs(const char *pathname, mode_t mode) * * @see ssh_string_free_char() */ -char *ssh_path_expand_tilde(const char *d) { +char *ssh_path_expand_tilde(const char *d) +{ char *h = NULL, *r; const char *p; size_t ld; @@ -1115,7 +1142,8 @@ char *ssh_path_expand_tilde(const char *d) { * * @see ssh_string_free_char() */ -char *ssh_path_expand_escape(ssh_session session, const char *s) { +char *ssh_path_expand_escape(ssh_session session, const char *s) +{ char host[NI_MAXHOST] = {0}; char *buf = NULL; char *r = NULL; @@ -1376,7 +1404,8 @@ done: * @brief initializes a timestamp to the current time * @param[out] ts pointer to an allocated ssh_timestamp structure */ -void ssh_timestamp_init(struct ssh_timestamp *ts){ +void ssh_timestamp_init(struct ssh_timestamp *ts) +{ #ifdef HAVE_CLOCK_GETTIME struct timespec tp; clock_gettime(CLOCK, &tp); @@ -1399,17 +1428,18 @@ void ssh_timestamp_init(struct ssh_timestamp *ts){ * @returns difference in milliseconds */ -static int ssh_timestamp_difference(struct ssh_timestamp *old, - struct ssh_timestamp *new){ - long seconds, usecs, msecs; - seconds = new->seconds - old->seconds; - usecs = new->useconds - old->useconds; - if (usecs < 0){ - seconds--; - usecs += 1000000; - } - msecs = seconds * 1000 + usecs/1000; - return msecs; +static int +ssh_timestamp_difference(struct ssh_timestamp *old, struct ssh_timestamp *new) +{ + long seconds, usecs, msecs; + seconds = new->seconds - old->seconds; + usecs = new->useconds - old->useconds; + if (usecs < 0){ + seconds--; + usecs += 1000000; + } + msecs = seconds * 1000 + usecs/1000; + return msecs; } /** @@ -1420,14 +1450,15 @@ static int ssh_timestamp_difference(struct ssh_timestamp *old, * @param[in] usec number of microseconds * @returns milliseconds, or 10000 if user supplied values are equal to zero */ -int ssh_make_milliseconds(long sec, long usec) { - int res = usec ? (usec / 1000) : 0; - res += (sec * 1000); - if (res == 0) { - res = 10 * 1000; /* use a reasonable default value in case - * SSH_OPTIONS_TIMEOUT is not set in options. */ - } - return res; +int ssh_make_milliseconds(long sec, long usec) +{ + int res = usec ? (usec / 1000) : 0; + res += (sec * 1000); + if (res == 0) { + res = 10 * 1000; /* use a reasonable default value in case + * SSH_OPTIONS_TIMEOUT is not set in options. */ + } + return res; } /** @@ -1440,7 +1471,8 @@ int ssh_make_milliseconds(long sec, long usec) { * @returns 1 if timeout is elapsed * 0 otherwise */ -int ssh_timeout_elapsed(struct ssh_timestamp *ts, int timeout) { +int ssh_timeout_elapsed(struct ssh_timestamp *ts, int timeout) +{ struct ssh_timestamp now; switch(timeout) { @@ -1472,7 +1504,8 @@ int ssh_timeout_elapsed(struct ssh_timestamp *ts, int timeout) { * timeout * @returns remaining time in milliseconds, 0 if elapsed, -1 if never. */ -int ssh_timeout_update(struct ssh_timestamp *ts, int timeout){ +int ssh_timeout_update(struct ssh_timestamp *ts, int timeout) +{ struct ssh_timestamp now; int ms, ret; if (timeout <= 0) { @@ -1795,7 +1828,7 @@ int ssh_newline_vis(const char *string, char *buf, size_t buf_len) * * @brief Replaces the last 6 characters of a string from 'X' to 6 random hexdigits. * - * @param[in] template Any input string with last 6 characters as 'X'. + * @param[in,out] template Any input string with last 6 characters as 'X'. * @returns -1 as error when the last 6 characters of the input to be replaced are not 'X' * 0 otherwise. */ diff --git a/src/options.c b/src/options.c index e6bd920b..d6c06776 100644 --- a/src/options.c +++ b/src/options.c @@ -483,7 +483,8 @@ int ssh_options_set_algo(ssh_session session, * @return 0 on success, < 0 on error. */ int ssh_options_set(ssh_session session, enum ssh_options_e type, - const void *value) { + const void *value) +{ const char *v; char *p, *q; long int i; @@ -1406,7 +1407,8 @@ int ssh_options_getopt(ssh_session session, int *argcptr, char **argv) * * @see ssh_options_set() */ -int ssh_options_parse_config(ssh_session session, const char *filename) { +int ssh_options_parse_config(ssh_session session, const char *filename) +{ char *expanded_filename; int r; @@ -1451,7 +1453,8 @@ out: return r; } -int ssh_options_apply(ssh_session session) { +int ssh_options_apply(ssh_session session) +{ struct ssh_iterator *it; char *tmp; int rc; @@ -1544,8 +1547,9 @@ static bool ssh_bind_key_size_allowed(ssh_bind sshbind, ssh_key key) * @addtogroup libssh_server * @{ */ -static int ssh_bind_set_key(ssh_bind sshbind, char **key_loc, - const void *value) { +static int +ssh_bind_set_key(ssh_bind sshbind, char **key_loc, const void *value) +{ if (value == NULL) { ssh_set_error_invalid(sshbind); return -1; diff --git a/src/pki.c b/src/pki.c index 9f1b392d..09756fd9 100644 --- a/src/pki.c +++ b/src/pki.c @@ -122,15 +122,17 @@ const char *ssh_pki_key_ecdsa_name(const ssh_key key) /** * @brief creates a new empty SSH key + * * @returns an empty ssh_key handle, or NULL on error. */ -ssh_key ssh_key_new (void) { - ssh_key ptr = malloc (sizeof (struct ssh_key_struct)); - if (ptr == NULL) { - return NULL; - } - ZERO_STRUCTP(ptr); - return ptr; +ssh_key ssh_key_new (void) +{ + ssh_key ptr = malloc (sizeof (struct ssh_key_struct)); + if (ptr == NULL) { + return NULL; + } + ZERO_STRUCTP(ptr); + return ptr; } ssh_key ssh_key_dup(const ssh_key key) @@ -146,8 +148,9 @@ ssh_key ssh_key_dup(const ssh_key key) * @brief clean up the key and deallocate all existing keys * @param[in] key ssh_key to clean */ -void ssh_key_clean (ssh_key key){ - if(key == NULL) +void ssh_key_clean (ssh_key key) +{ + if (key == NULL) return; #ifdef HAVE_LIBGCRYPT if(key->dsa) gcry_sexp_release(key->dsa); @@ -206,8 +209,9 @@ void ssh_key_clean (ssh_key key){ * @brief deallocate a SSH key * @param[in] key ssh_key handle to free */ -void ssh_key_free (ssh_key key){ - if(key){ +void ssh_key_free (ssh_key key) +{ + if (key) { ssh_key_clean(key); SAFE_FREE(key); } @@ -224,7 +228,8 @@ void ssh_key_free (ssh_key key){ * SSH_KEYTYPE_ED25519_CERT01. * @returns SSH_KEYTYPE_UNKNOWN if the type is unknown */ -enum ssh_keytypes_e ssh_key_type(const ssh_key key){ +enum ssh_keytypes_e ssh_key_type(const ssh_key key) +{ if (key == NULL) { return SSH_KEYTYPE_UNKNOWN; } @@ -573,7 +578,8 @@ enum ssh_keytypes_e ssh_key_type_from_signature_name(const char *name) { * * @return The enum ssh key type. */ -enum ssh_keytypes_e ssh_key_type_from_name(const char *name) { +enum ssh_keytypes_e ssh_key_type_from_name(const char *name) +{ if (name == NULL) { return SSH_KEYTYPE_UNKNOWN; } @@ -628,7 +634,8 @@ enum ssh_keytypes_e ssh_key_type_from_name(const char *name) { * * @return The matching public key type. */ -enum ssh_keytypes_e ssh_key_type_plain(enum ssh_keytypes_e type) { +enum ssh_keytypes_e ssh_key_type_plain(enum ssh_keytypes_e type) +{ switch (type) { case SSH_KEYTYPE_DSS_CERT01: return SSH_KEYTYPE_DSS; @@ -658,7 +665,8 @@ enum ssh_keytypes_e ssh_key_type_plain(enum ssh_keytypes_e type) { * * @return 1 if it is a public key, 0 if not. */ -int ssh_key_is_public(const ssh_key k) { +int ssh_key_is_public(const ssh_key k) +{ if (k == NULL) { return 0; } @@ -1091,7 +1099,8 @@ int ssh_pki_export_privkey_file(const ssh_key privkey, } /* temporary function to migrate seemlessly to ssh_key */ -ssh_public_key ssh_pki_convert_key_to_publickey(const ssh_key key) { +ssh_public_key ssh_pki_convert_key_to_publickey(const ssh_key key) +{ ssh_public_key pub; ssh_key tmp; @@ -1124,7 +1133,8 @@ ssh_public_key ssh_pki_convert_key_to_publickey(const ssh_key key) { return pub; } -ssh_private_key ssh_pki_convert_key_to_privatekey(const ssh_key key) { +ssh_private_key ssh_pki_convert_key_to_privatekey(const ssh_key key) +{ ssh_private_key privkey; privkey = malloc(sizeof(struct ssh_private_key_struct)); @@ -1326,7 +1336,8 @@ fail: static int pki_import_pubkey_buffer(ssh_buffer buffer, enum ssh_keytypes_e type, - ssh_key *pkey) { + ssh_key *pkey) +{ ssh_key key = NULL; int rc; @@ -1508,7 +1519,8 @@ fail: static int pki_import_cert_buffer(ssh_buffer buffer, enum ssh_keytypes_e type, - ssh_key *pkey) { + ssh_key *pkey) +{ ssh_buffer cert; ssh_string tmp_s; const char *type_c; @@ -1610,7 +1622,8 @@ fail: */ int ssh_pki_import_pubkey_base64(const char *b64_key, enum ssh_keytypes_e type, - ssh_key *pkey) { + ssh_key *pkey) +{ ssh_buffer buffer = NULL; ssh_string type_s = NULL; int rc; @@ -1657,7 +1670,8 @@ int ssh_pki_import_pubkey_base64(const char *b64_key, * @see ssh_key_free() */ int ssh_pki_import_pubkey_blob(const ssh_string key_blob, - ssh_key *pkey) { + ssh_key *pkey) +{ ssh_buffer buffer = NULL; ssh_string type_s = NULL; enum ssh_keytypes_e type; @@ -1891,7 +1905,8 @@ int ssh_pki_import_pubkey_file(const char *filename, ssh_key *pkey) */ int ssh_pki_import_cert_base64(const char *b64_cert, enum ssh_keytypes_e type, - ssh_key *pkey) { + ssh_key *pkey) +{ return ssh_pki_import_pubkey_base64(b64_cert, type, pkey); } @@ -1911,7 +1926,8 @@ int ssh_pki_import_cert_base64(const char *b64_cert, * @see ssh_key_free() */ int ssh_pki_import_cert_blob(const ssh_string cert_blob, - ssh_key *pkey) { + ssh_key *pkey) +{ return ssh_pki_import_pubkey_blob(cert_blob, pkey); } @@ -1951,7 +1967,8 @@ int ssh_pki_import_cert_file(const char *filename, ssh_key *pkey) * @see ssh_key_free() */ int ssh_pki_generate(enum ssh_keytypes_e type, int parameter, - ssh_key *pkey){ + ssh_key *pkey) +{ int rc; ssh_key key = ssh_key_new(); diff --git a/src/poll.c b/src/poll.c index f9499b07..55285555 100644 --- a/src/poll.c +++ b/src/poll.c @@ -84,15 +84,18 @@ struct ssh_poll_ctx_struct { #ifdef HAVE_POLL #include -void ssh_poll_init(void) { +void ssh_poll_init(void) +{ return; } -void ssh_poll_cleanup(void) { +void ssh_poll_cleanup(void) +{ return; } -int ssh_poll(ssh_pollfd_t *fds, nfds_t nfds, int timeout) { +int ssh_poll(ssh_pollfd_t *fds, nfds_t nfds, int timeout) +{ return poll((struct pollfd *) fds, nfds, timeout); } @@ -333,21 +336,24 @@ int ssh_poll(ssh_pollfd_t *fds, nfds_t nfds, int timeout) { /** * @brief Allocate a new poll object, which could be used within a poll context. * - * @param fd Socket that will be polled. - * @param events Poll events that will be monitored for the socket. i.e. - * POLLIN, POLLPRI, POLLOUT - * @param cb Function to be called if any of the events are set. - * The prototype of cb is: - * int (*ssh_poll_callback)(ssh_poll_handle p, socket_t fd, - * int revents, void *userdata); - * @param userdata Userdata to be passed to the callback function. NULL if - * not needed. + * @param[in] fd Socket that will be polled. + * @param[in] events Poll events that will be monitored for the socket. + * i.e. POLLIN, POLLPRI, POLLOUT + * @param[in] cb Function to be called if any of the events are set. + * The prototype of cb is: + * int (*ssh_poll_callback)(ssh_poll_handle p, + * socket_t fd, + * int revents, + * void *userdata); + * @param[in] userdata Userdata to be passed to the callback function. + * NULL if not needed. * - * @return A new poll object, NULL on error + * @return A new poll object, NULL on error */ -ssh_poll_handle ssh_poll_new(socket_t fd, short events, ssh_poll_callback cb, - void *userdata) { +ssh_poll_handle +ssh_poll_new(socket_t fd, short events, ssh_poll_callback cb, void *userdata) +{ ssh_poll_handle p; p = malloc(sizeof(struct ssh_poll_handle_struct)); @@ -371,12 +377,13 @@ ssh_poll_handle ssh_poll_new(socket_t fd, short events, ssh_poll_callback cb, * @param p Pointer to an already allocated poll object. */ -void ssh_poll_free(ssh_poll_handle p) { - if(p->ctx != NULL){ - ssh_poll_ctx_remove(p->ctx,p); - p->ctx=NULL; - } - SAFE_FREE(p); +void ssh_poll_free(ssh_poll_handle p) +{ + if (p->ctx != NULL) { + ssh_poll_ctx_remove(p->ctx, p); + p->ctx = NULL; + } + SAFE_FREE(p); } /** @@ -386,8 +393,9 @@ void ssh_poll_free(ssh_poll_handle p) { * * @return Poll context or NULL if the poll object isn't attached. */ -ssh_poll_ctx ssh_poll_get_ctx(ssh_poll_handle p) { - return p->ctx; +ssh_poll_ctx ssh_poll_get_ctx(ssh_poll_handle p) +{ + return p->ctx; } /** @@ -397,8 +405,9 @@ ssh_poll_ctx ssh_poll_get_ctx(ssh_poll_handle p) { * * @return Poll events. */ -short ssh_poll_get_events(ssh_poll_handle p) { - return p->events; +short ssh_poll_get_events(ssh_poll_handle p) +{ + return p->events; } /** @@ -408,11 +417,12 @@ short ssh_poll_get_events(ssh_poll_handle p) { * @param p Pointer to an already allocated poll object. * @param events Poll events. */ -void ssh_poll_set_events(ssh_poll_handle p, short events) { - p->events = events; - if (p->ctx != NULL && !p->lock) { - p->ctx->pollfds[p->x.idx].events = events; - } +void ssh_poll_set_events(ssh_poll_handle p, short events) +{ + p->events = events; + if (p->ctx != NULL && !p->lock) { + p->ctx->pollfds[p->x.idx].events = events; + } } /** @@ -422,12 +432,13 @@ void ssh_poll_set_events(ssh_poll_handle p, short events) { * @param p Pointer to an already allocated poll object. * @param fd New file descriptor. */ -void ssh_poll_set_fd(ssh_poll_handle p, socket_t fd) { - if (p->ctx != NULL) { - p->ctx->pollfds[p->x.idx].fd = fd; - } else { - p->x.fd = fd; - } +void ssh_poll_set_fd(ssh_poll_handle p, socket_t fd) +{ + if (p->ctx != NULL) { + p->ctx->pollfds[p->x.idx].fd = fd; + } else { + p->x.fd = fd; + } } /** @@ -437,8 +448,9 @@ void ssh_poll_set_fd(ssh_poll_handle p, socket_t fd) { * @param p Pointer to an already allocated poll object. * @param events Poll events. */ -void ssh_poll_add_events(ssh_poll_handle p, short events) { - ssh_poll_set_events(p, ssh_poll_get_events(p) | events); +void ssh_poll_add_events(ssh_poll_handle p, short events) +{ + ssh_poll_set_events(p, ssh_poll_get_events(p) | events); } /** @@ -448,8 +460,9 @@ void ssh_poll_add_events(ssh_poll_handle p, short events) { * @param p Pointer to an already allocated poll object. * @param events Poll events. */ -void ssh_poll_remove_events(ssh_poll_handle p, short events) { - ssh_poll_set_events(p, ssh_poll_get_events(p) & ~events); +void ssh_poll_remove_events(ssh_poll_handle p, short events) +{ + ssh_poll_set_events(p, ssh_poll_get_events(p) & ~events); } /** @@ -460,12 +473,13 @@ void ssh_poll_remove_events(ssh_poll_handle p, short events) { * @return Raw socket. */ -socket_t ssh_poll_get_fd(ssh_poll_handle p) { - if (p->ctx != NULL) { - return p->ctx->pollfds[p->x.idx].fd; - } +socket_t ssh_poll_get_fd(ssh_poll_handle p) +{ + if (p->ctx != NULL) { + return p->ctx->pollfds[p->x.idx].fd; + } - return p->x.fd; + return p->x.fd; } /** * @brief Set the callback of a poll object. @@ -475,11 +489,12 @@ socket_t ssh_poll_get_fd(ssh_poll_handle p) { * @param userdata Userdata to be passed to the callback function. NULL if * not needed. */ -void ssh_poll_set_callback(ssh_poll_handle p, ssh_poll_callback cb, void *userdata) { - if (cb != NULL) { - p->cb = cb; - p->cb_data = userdata; - } +void ssh_poll_set_callback(ssh_poll_handle p, ssh_poll_callback cb, void *userdata) +{ + if (cb != NULL) { + p->cb = cb; + p->cb_data = userdata; + } } /** @@ -493,7 +508,8 @@ void ssh_poll_set_callback(ssh_poll_handle p, ssh_poll_callback cb, void *userda * for the next 5. Set it to 0 if you want to use the * library's default value. */ -ssh_poll_ctx ssh_poll_ctx_new(size_t chunk_size) { +ssh_poll_ctx ssh_poll_ctx_new(size_t chunk_size) +{ ssh_poll_ctx ctx; ctx = malloc(sizeof(struct ssh_poll_ctx_struct)); @@ -516,25 +532,27 @@ ssh_poll_ctx ssh_poll_ctx_new(size_t chunk_size) { * * @param ctx Pointer to an already allocated poll context. */ -void ssh_poll_ctx_free(ssh_poll_ctx ctx) { - if (ctx->polls_allocated > 0) { - while (ctx->polls_used > 0){ - ssh_poll_handle p = ctx->pollptrs[0]; - /* - * The free function calls ssh_poll_ctx_remove() and decrements - * ctx->polls_used - */ - ssh_poll_free(p); +void ssh_poll_ctx_free(ssh_poll_ctx ctx) +{ + if (ctx->polls_allocated > 0) { + while (ctx->polls_used > 0){ + ssh_poll_handle p = ctx->pollptrs[0]; + /* + * The free function calls ssh_poll_ctx_remove() and decrements + * ctx->polls_used + */ + ssh_poll_free(p); + } + + SAFE_FREE(ctx->pollptrs); + SAFE_FREE(ctx->pollfds); } - SAFE_FREE(ctx->pollptrs); - SAFE_FREE(ctx->pollfds); - } - - SAFE_FREE(ctx); + SAFE_FREE(ctx); } -static int ssh_poll_ctx_resize(ssh_poll_ctx ctx, size_t new_size) { +static int ssh_poll_ctx_resize(ssh_poll_ctx ctx, size_t new_size) +{ ssh_poll_handle *pollptrs; ssh_pollfd_t *pollfds; @@ -568,7 +586,8 @@ static int ssh_poll_ctx_resize(ssh_poll_ctx ctx, size_t new_size) { * * @return 0 on success, < 0 on error */ -int ssh_poll_ctx_add(ssh_poll_ctx ctx, ssh_poll_handle p) { +int ssh_poll_ctx_add(ssh_poll_ctx ctx, ssh_poll_handle p) +{ socket_t fd; if (p->ctx != NULL) { @@ -620,7 +639,8 @@ int ssh_poll_ctx_add_socket (ssh_poll_ctx ctx, ssh_socket s) * @param ctx Pointer to an already allocated poll context. * @param p Pointer to an already allocated poll object. */ -void ssh_poll_ctx_remove(ssh_poll_ctx ctx, ssh_poll_handle p) { +void ssh_poll_ctx_remove(ssh_poll_ctx ctx, ssh_poll_handle p) +{ size_t i; i = p->x.idx; @@ -725,12 +745,13 @@ int ssh_poll_ctx_dopoll(ssh_poll_ctx ctx, int timeout) * @param session SSH session * @returns the default ssh_poll_ctx */ -ssh_poll_ctx ssh_poll_get_default_ctx(ssh_session session){ - if(session->default_poll_ctx != NULL) - return session->default_poll_ctx; - /* 2 is enough for the default one */ - session->default_poll_ctx = ssh_poll_ctx_new(2); - return session->default_poll_ctx; +ssh_poll_ctx ssh_poll_get_default_ctx(ssh_session session) +{ + if(session->default_poll_ctx != NULL) + return session->default_poll_ctx; + /* 2 is enough for the default one */ + session->default_poll_ctx = ssh_poll_ctx_new(2); + return session->default_poll_ctx; } /* public event API */ @@ -755,7 +776,8 @@ struct ssh_event_struct { * * @return The ssh_event object on success, NULL on failure. */ -ssh_event ssh_event_new(void) { +ssh_event ssh_event_new(void) +{ ssh_event event; event = malloc(sizeof(struct ssh_event_struct)); @@ -782,12 +804,14 @@ ssh_event ssh_event_new(void) { return event; } -static int ssh_event_fd_wrapper_callback(ssh_poll_handle p, socket_t fd, int revents, - void *userdata) { +static int +ssh_event_fd_wrapper_callback(ssh_poll_handle p, socket_t fd, int revents, + void *userdata) +{ struct ssh_event_fd_wrapper *pw = (struct ssh_event_fd_wrapper *)userdata; (void)p; - if(pw->cb != NULL) { + if (pw->cb != NULL) { return pw->cb(fd, revents, pw->userdata); } return 0; @@ -810,8 +834,10 @@ static int ssh_event_fd_wrapper_callback(ssh_poll_handle p, socket_t fd, int rev * @returns SSH_OK on success * SSH_ERROR on failure */ -int ssh_event_add_fd(ssh_event event, socket_t fd, short events, - ssh_event_callback cb, void *userdata) { +int +ssh_event_add_fd(ssh_event event, socket_t fd, short events, + ssh_event_callback cb, void *userdata) +{ ssh_poll_handle p; struct ssh_event_fd_wrapper *pw; @@ -879,7 +905,8 @@ void ssh_event_remove_poll(ssh_event event, ssh_poll_handle p) * @returns SSH_OK on success * SSH_ERROR on failure */ -int ssh_event_add_session(ssh_event event, ssh_session session) { +int ssh_event_add_session(ssh_event event, ssh_session session) +{ ssh_poll_handle p; #ifdef WITH_SERVER struct ssh_iterator *iterator; @@ -931,7 +958,8 @@ int ssh_event_add_session(ssh_event event, ssh_session session) { * * @return SSH_ERROR in case of error */ -int ssh_event_add_connector(ssh_event event, ssh_connector connector){ +int ssh_event_add_connector(ssh_event event, ssh_connector connector) +{ return ssh_connector_set_event(connector, event); } @@ -952,10 +980,11 @@ int ssh_event_add_connector(ssh_event event, ssh_connector connector){ * SSH_ERROR Error happened during the poll. * SSH_AGAIN Timeout occured */ -int ssh_event_dopoll(ssh_event event, int timeout) { +int ssh_event_dopoll(ssh_event event, int timeout) +{ int rc; - if(event == NULL || event->ctx == NULL) { + if (event == NULL || event->ctx == NULL) { return SSH_ERROR; } rc = ssh_poll_ctx_dopoll(event->ctx, timeout); @@ -971,7 +1000,8 @@ int ssh_event_dopoll(ssh_event event, int timeout) { * @returns SSH_OK on success * SSH_ERROR on failure */ -int ssh_event_remove_fd(ssh_event event, socket_t fd) { +int ssh_event_remove_fd(ssh_event event, socket_t fd) +{ register size_t i, used; int rc = SSH_ERROR; @@ -1017,7 +1047,8 @@ int ssh_event_remove_fd(ssh_event event, socket_t fd) { * @returns SSH_OK on success * SSH_ERROR on failure */ -int ssh_event_remove_session(ssh_event event, ssh_session session) { +int ssh_event_remove_session(ssh_event event, ssh_session session) +{ ssh_poll_handle p; register size_t i, used; int rc = SSH_ERROR; @@ -1025,14 +1056,14 @@ int ssh_event_remove_session(ssh_event event, ssh_session session) { struct ssh_iterator *iterator; #endif - if(event == NULL || event->ctx == NULL || session == NULL) { + if (event == NULL || event->ctx == NULL || session == NULL) { return SSH_ERROR; } used = event->ctx->polls_used; - for(i = 0; i < used; i++) { - p = event->ctx->pollptrs[i]; - if(p->session == session){ + for (i = 0; i < used; i++) { + p = event->ctx->pollptrs[i]; + if (p->session == session) { /* * ssh_poll_ctx_remove() decrements * event->ctx->polls_used @@ -1052,8 +1083,8 @@ int ssh_event_remove_session(ssh_event event, ssh_session session) { } #ifdef WITH_SERVER iterator = ssh_list_get_iterator(event->sessions); - while(iterator != NULL) { - if((ssh_session)iterator->data == session) { + while (iterator != NULL) { + if ((ssh_session)iterator->data == session) { ssh_list_remove(event->sessions, iterator); /* there should be only one instance of this session */ break; @@ -1071,7 +1102,8 @@ int ssh_event_remove_session(ssh_event event, ssh_session session) { * @return SSH_OK on success * @return SSH_ERROR on failure */ -int ssh_event_remove_connector(ssh_event event, ssh_connector connector){ +int ssh_event_remove_connector(ssh_event event, ssh_connector connector) +{ (void)event; return ssh_connector_remove_event(connector); } @@ -1089,13 +1121,13 @@ void ssh_event_free(ssh_event event) size_t used, i; ssh_poll_handle p; - if(event == NULL) { + if (event == NULL) { return; } if (event->ctx != NULL) { used = event->ctx->polls_used; - for(i = 0; i < used; i++) { + for (i = 0; i < used; i++) { p = event->ctx->pollptrs[i]; if (p->session != NULL) { ssh_poll_ctx_remove(event->ctx, p); @@ -1108,7 +1140,7 @@ void ssh_event_free(ssh_event event) ssh_poll_ctx_free(event->ctx); } #ifdef WITH_SERVER - if(event->sessions != NULL) { + if (event->sessions != NULL) { ssh_list_free(event->sessions); } #endif diff --git a/src/session.c b/src/session.c index 9b6248b7..61882a50 100644 --- a/src/session.c +++ b/src/session.c @@ -1047,7 +1047,8 @@ int ssh_get_pubkey_hash(ssh_session session, unsigned char **hash) * * @see ssh_get_pubkey_hash() */ -void ssh_clean_pubkey_hash(unsigned char **hash) { +void ssh_clean_pubkey_hash(unsigned char **hash) +{ SAFE_FREE(*hash); }