| 1 |
/* |
| 2 |
* This file is part of the SSH Library |
| 3 |
* |
| 4 |
* Copyright (c) 2018 by Red Hat, Inc. |
| 5 |
* |
| 6 |
* Author: Anderson Toshiyuki Sasaki <[email protected]> |
| 7 |
* |
| 8 |
* The SSH Library is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2.1 of the License, or (at your |
| 11 |
* option) any later version. |
| 12 |
* |
| 13 |
* The SSH Library is distributed in the hope that it will be useful, but |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 16 |
* License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with the SSH Library; see the file COPYING. If not, write to |
| 20 |
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 21 |
* MA 02111-1307, USA. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "config.h" |
| 25 |
|
| 26 |
#include <libssh/libssh.h> |
| 27 |
#include <libssh/sftp.h> |
| 28 |
#include <libssh/callbacks.h> |
| 29 |
|
| 30 |
#define SSHD_DEFAULT_USER "libssh" |
| 31 |
#define SSHD_DEFAULT_PASSWORD "libssh" |
| 32 |
#define SSHD_DEFAULT_PORT 2222 |
| 33 |
#define SSHD_DEFAULT_ADDRESS "127.0.0.1" |
| 34 |
#define SSHD_DEFAULT_PCAP_FILE "debug.server.pcap" |
| 35 |
|
| 36 |
#define SSHD_BANNER_MESSAGE "Test Banner Message\nlibssh-send-banner\n" |
| 37 |
|
| 38 |
#ifndef KEYS_FOLDER |
| 39 |
#ifdef _WIN32 |
| 40 |
#define KEYS_FOLDER |
| 41 |
#else |
| 42 |
#define KEYS_FOLDER "/etc/ssh/" |
| 43 |
#endif |
| 44 |
#endif |
| 45 |
|
| 46 |
#define BUF_SIZE 1048576 |
| 47 |
#define SESSION_END (SSH_CLOSED | SSH_CLOSED_ERROR) |
| 48 |
#define SFTP_SERVER_PATH "/usr/lib/sftp-server" |
| 49 |
|
| 50 |
#ifdef HAVE_PTY_H |
| 51 |
#include <pty.h> |
| 52 |
#endif |
| 53 |
|
| 54 |
/* A userdata struct for channel. */ |
| 55 |
struct channel_data_st { |
| 56 |
/* pid of the child process the channel will spawn. */ |
| 57 |
pid_t pid; |
| 58 |
/* For PTY allocation */ |
| 59 |
socket_t pty_master; |
| 60 |
socket_t pty_slave; |
| 61 |
/* For communication with the child process. */ |
| 62 |
socket_t child_stdin; |
| 63 |
socket_t child_stdout; |
| 64 |
/* Only used for subsystem and exec requests. */ |
| 65 |
socket_t child_stderr; |
| 66 |
/* Event which is used to poll the above descriptors. */ |
| 67 |
ssh_event event; |
| 68 |
/* Terminal size struct. */ |
| 69 |
struct winsize *winsize; |
| 70 |
/* This pointer will hold the server state for default callbacks */ |
| 71 |
void *server_state; |
| 72 |
/* This pointer is useful to set data for custom callbacks */ |
| 73 |
void *extra_data; |
| 74 |
sftp_session sftp; |
| 75 |
}; |
| 76 |
|
| 77 |
/* A userdata struct for session. */ |
| 78 |
struct session_data_st { |
| 79 |
/* Pointer to the channel the session will allocate. */ |
| 80 |
ssh_channel channel; |
| 81 |
int auth_attempts; |
| 82 |
int authenticated; |
| 83 |
const char *username; |
| 84 |
const char *password; |
| 85 |
#ifdef WITH_PCAP |
| 86 |
ssh_pcap_file pcap; |
| 87 |
#endif |
| 88 |
/* This pointer will hold the server state for default callbacks */ |
| 89 |
void *server_state; |
| 90 |
/* This pointer is useful to set data for custom callbacks */ |
| 91 |
void *extra_data; |
| 92 |
}; |
| 93 |
|
| 94 |
int auth_password_cb(ssh_session session, const char *user, |
| 95 |
const char *password, void *userdata); |
| 96 |
|
| 97 |
#if WITH_GSSAPI |
| 98 |
int auth_gssapi_mic_cb(ssh_session session, const char *user, |
| 99 |
const char *principal, void *userdata); |
| 100 |
#endif |
| 101 |
|
| 102 |
int channel_data_cb(ssh_session session, ssh_channel channel, |
| 103 |
void *data, uint32_t len, int is_stderr, void *userdata); |
| 104 |
|
| 105 |
void channel_eof_cb(ssh_session session, ssh_channel channel, |
| 106 |
void *userdata); |
| 107 |
|
| 108 |
void channel_close_cb(ssh_session session, ssh_channel channel, |
| 109 |
void *userdata); |
| 110 |
|
| 111 |
void channel_signal_cb (ssh_session session, |
| 112 |
ssh_channel channel, |
| 113 |
const char *signal, |
| 114 |
void *userdata); |
| 115 |
|
| 116 |
void channel_exit_status_cb (ssh_session session, |
| 117 |
ssh_channel channel, |
| 118 |
int exit_status, |
| 119 |
void *userdata); |
| 120 |
|
| 121 |
void channel_exit_signal_cb(ssh_session session, |
| 122 |
ssh_channel channel, |
| 123 |
const char *signal, |
| 124 |
int core, |
| 125 |
const char *errmsg, |
| 126 |
const char *lang, |
| 127 |
void *userdata); |
| 128 |
|
| 129 |
int channel_pty_request_cb(ssh_session session, ssh_channel channel, |
| 130 |
const char *term, int cols, int rows, int py, int px, void *userdata); |
| 131 |
|
| 132 |
int channel_pty_resize_cb(ssh_session session, ssh_channel channel, |
| 133 |
int cols, int rows, int py, int px, void *userdata); |
| 134 |
|
| 135 |
int channel_shell_request_cb(ssh_session session, ssh_channel channel, |
| 136 |
void *userdata); |
| 137 |
|
| 138 |
void channel_auth_agent_req_callback(ssh_session session, |
| 139 |
ssh_channel channel, void *userdata); |
| 140 |
|
| 141 |
void channel_x11_req_callback(ssh_session session, |
| 142 |
ssh_channel channel, |
| 143 |
int single_connection, |
| 144 |
const char *auth_protocol, |
| 145 |
const char *auth_cookie, |
| 146 |
uint32_t screen_number, |
| 147 |
void *userdata); |
| 148 |
|
| 149 |
int channel_exec_request_cb(ssh_session session, |
| 150 |
ssh_channel channel, |
| 151 |
const char *command, |
| 152 |
void *userdata); |
| 153 |
|
| 154 |
int channel_env_request_cb(ssh_session session, |
| 155 |
ssh_channel channel, const char *env_name, const char *env_value, |
| 156 |
void *userdata); |
| 157 |
|
| 158 |
int channel_subsystem_request_cb(ssh_session session, |
| 159 |
ssh_channel channel, const char *subsystem, |
| 160 |
void *userdata); |
| 161 |
|
| 162 |
int channel_write_wontblock_cb(ssh_session session, |
| 163 |
ssh_channel channel, |
| 164 |
size_t bytes, |
| 165 |
void *userdata); |
| 166 |
|
| 167 |
ssh_channel channel_new_session_cb(ssh_session session, void *userdata); |
| 168 |
|
| 169 |
/* The caller is responsible to set the userdata to be provided to the callback |
| 170 |
* The caller is responsible to free the allocated structure |
| 171 |
* */ |
| 172 |
struct ssh_server_callbacks_struct *get_default_server_cb(void); |
| 173 |
|
| 174 |
/* The caller is responsible to set the userdata to be provided to the callback |
| 175 |
* The caller is responsible to free the allocated structure |
| 176 |
* */ |
| 177 |
struct ssh_channel_callbacks_struct *get_default_channel_cb(void); |
| 178 |
|
| 179 |
void default_handle_session_cb(ssh_event event, ssh_session session, |
| 180 |
struct server_state_st *state); |