1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-07-28 01:41:48 +03:00

Reformat sftpserver examples and tests and remove unused code

there were unused structure members and some code formatted not following our
code guidelines.

Signed-off-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
Reviewed-by: Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
This commit is contained in:
Jakub Jelen
2023-01-23 09:59:19 +01:00
parent f1f766f14f
commit af60e23081
2 changed files with 20 additions and 63 deletions

View File

@ -1215,20 +1215,8 @@ static struct argp argp = {options, parse_opt, args_doc, doc, NULL, NULL, NULL};
/* A userdata struct for channel. */ /* A userdata struct for channel. */
struct channel_data_struct struct channel_data_struct
{ {
/* pid of the child process the channel will spawn. */
pid_t pid;
/* For PTY allocation */
socket_t pty_master;
socket_t pty_slave;
/* For communication with the child process. */
socket_t child_stdin;
socket_t child_stdout;
/* Only used for subsystem and exec requests. */
socket_t child_stderr;
/* Event which is used to poll the above descriptors. */ /* Event which is used to poll the above descriptors. */
ssh_event event; ssh_event event;
/* Terminal size struct. */
struct winsize *winsize;
sftp_session sftp; sftp_session sftp;
}; };
@ -1358,32 +1346,18 @@ static ssh_channel channel_open(ssh_session session, void *userdata)
static void handle_session(ssh_event event, ssh_session session) static void handle_session(ssh_event event, ssh_session session)
{ {
int n; int n;
int rc = 0;
/* Structure for storing the pty size. */
struct winsize wsize = {
.ws_row = 0,
.ws_col = 0,
.ws_xpixel = 0,
.ws_ypixel = 0};
/* Our struct holding information about the channel. */ /* Our struct holding information about the channel. */
struct channel_data_struct cdata = { struct channel_data_struct cdata = {
.pid = 0, .sftp = NULL,
.pty_master = -1, };
.pty_slave = -1,
.child_stdin = -1,
.child_stdout = -1,
.child_stderr = -1,
.event = NULL,
.winsize = &wsize,
.sftp = NULL};
/* Our struct holding information about the session. */ /* Our struct holding information about the session. */
struct session_data_struct sdata = { struct session_data_struct sdata = {
.channel = NULL, .channel = NULL,
.auth_attempts = 0, .auth_attempts = 0,
.authenticated = 0}; .authenticated = 0,
};
struct ssh_channel_callbacks_struct channel_cb = { struct ssh_channel_callbacks_struct channel_cb = {
.userdata = &cdata, .userdata = &cdata,
@ -1418,17 +1392,14 @@ static void handle_session(ssh_event event, ssh_session session)
ssh_event_add_session(event, session); ssh_event_add_session(event, session);
n = 0; n = 0;
while (sdata.authenticated == 0 || sdata.channel == NULL) while (sdata.authenticated == 0 || sdata.channel == NULL) {
{
/* If the user has used up all attempts, or if he hasn't been able to /* If the user has used up all attempts, or if he hasn't been able to
* authenticate in 10 seconds (n * 100ms), disconnect. */ * authenticate in 10 seconds (n * 100ms), disconnect. */
if (sdata.auth_attempts >= 3 || n >= 100) if (sdata.auth_attempts >= 3 || n >= 100) {
{
return; return;
} }
if (ssh_event_dopoll(event, 100) == SSH_ERROR) if (ssh_event_dopoll(event, 100) == SSH_ERROR) {
{
fprintf(stderr, "%s\n", ssh_get_error(session)); fprintf(stderr, "%s\n", ssh_get_error(session));
return; return;
} }
@ -1437,33 +1408,29 @@ static void handle_session(ssh_event event, ssh_session session)
ssh_set_channel_callbacks(sdata.channel, &channel_cb); ssh_set_channel_callbacks(sdata.channel, &channel_cb);
do do {
{
/* Poll the main event which takes care of the session, the channel and /* Poll the main event which takes care of the session, the channel and
* even our child process's stdout/stderr (once it's started). */ * even our child process's stdout/stderr (once it's started). */
if (ssh_event_dopoll(event, -1) == SSH_ERROR) if (ssh_event_dopoll(event, -1) == SSH_ERROR) {
{
ssh_channel_close(sdata.channel); ssh_channel_close(sdata.channel);
} }
/* If child process's stdout/stderr has been registered with the event, /* If child process's stdout/stderr has been registered with the event,
* or the child process hasn't started yet, continue. */ * or the child process hasn't started yet, continue. */
if (cdata.event != NULL || cdata.pid == 0) if (cdata.event != NULL) {
{
continue; continue;
} }
/* FIXME The server keeps hanging in the poll above when the client
* closes the channel */
} while (ssh_channel_is_open(sdata.channel));
} while (ssh_channel_is_open(sdata.channel) && sftp_server_free_handles();
(cdata.pid == 0 || waitpid(cdata.pid, &rc, WNOHANG) == 0));
free_handles();
ssh_channel_send_eof(sdata.channel); ssh_channel_send_eof(sdata.channel);
ssh_channel_close(sdata.channel); ssh_channel_close(sdata.channel);
/* Wait up to 5 seconds for the client to terminate the session. */ /* Wait up to 5 seconds for the client to terminate the session. */
for (n = 0; n < 50 && (ssh_get_status(session) & SESSION_END) == 0; n++) for (n = 0; n < 50 && (ssh_get_status(session) & SESSION_END) == 0; n++) {
{
ssh_event_dopoll(event, 100); ssh_event_dopoll(event, 100);
} }
} }

View File

@ -1186,8 +1186,8 @@ end:
}; };
void sftp_handle_session_cb(ssh_event event, void sftp_handle_session_cb(ssh_event event,
ssh_session session, ssh_session session,
struct server_state_st *state) struct server_state_st *state)
{ {
int n; int n;
int rc = 0; int rc = 0;
@ -1202,12 +1202,6 @@ void sftp_handle_session_cb(ssh_event event,
/* Our struct holding information about the channel. */ /* Our struct holding information about the channel. */
struct channel_data_st cdata = { struct channel_data_st cdata = {
.pid = 0,
.pty_master = -1,
.pty_slave = -1,
.child_stdin = -1,
.child_stdout = -1,
.child_stderr = -1,
.event = NULL, .event = NULL,
.winsize = &wsize, .winsize = &wsize,
.sftp = NULL .sftp = NULL
@ -1257,7 +1251,6 @@ void sftp_handle_session_cb(ssh_event event,
} }
sdata.server_state = (void *)state; sdata.server_state = (void *)state;
cdata.server_state = (void *)state;
#ifdef WITH_PCAP #ifdef WITH_PCAP
set_pcap(&sdata, session, state->pcap_file); set_pcap(&sdata, session, state->pcap_file);
@ -1329,19 +1322,16 @@ void sftp_handle_session_cb(ssh_event event,
/* Poll the main event which takes care of the session, the channel and /* Poll the main event which takes care of the session, the channel and
* even our child process's stdout/stderr (once it's started). */ * even our child process's stdout/stderr (once it's started). */
if (ssh_event_dopoll(event, -1) == SSH_ERROR) { if (ssh_event_dopoll(event, -1) == SSH_ERROR) {
ssh_channel_close(sdata.channel); ssh_channel_close(sdata.channel);
} }
/* If child process's stdout/stderr has been registered with the event, /* If child process's stdout/stderr has been registered with the event,
* or the child process hasn't started yet, continue. */ * or the child process hasn't started yet, continue. */
if (cdata.event != NULL || cdata.pid == 0) { if (cdata.event != NULL) {
continue; continue;
} }
} while(ssh_channel_is_open(sdata.channel) && } while (ssh_channel_is_open(sdata.channel));
(cdata.pid == 0 || waitpid(cdata.pid, &rc, WNOHANG) == 0));
free_handles(MAX_HANDLE_NUM);
ssh_channel_send_eof(sdata.channel); ssh_channel_send_eof(sdata.channel);
ssh_channel_close(sdata.channel); ssh_channel_close(sdata.channel);