-
Notifications
You must be signed in to change notification settings - Fork 0
/
out_channel.c
272 lines (206 loc) · 7.48 KB
/
out_channel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <stdio.h>
#include <libssh/libssh.h>
#include <libssh/server.h>
#include <libssh/callbacks.h>
#include "state.h"
#include "out_channel.h"
#include "in_channel.h"
int out_data (ssh_session session, ssh_channel channel, void *data, uint32_t len, int is_stderr, void *userdata)
{
(void)session;
(void)channel;
stateptr state = (stateptr) userdata;
int fwlen;
fprintf(stdout, "out_data callback called with %d bytes (stderr %d)\n", len, is_stderr);
// Forward data to in channel
if (is_stderr) {
fwlen = ssh_channel_write_stderr(state->in_channel, data, len);
} else {
fwlen = ssh_channel_write(state->in_channel, data, len);
}
fprintf(stdout, "out_data callback wrote %d bytes\n", fwlen);
return fwlen;
}
void out_eof (ssh_session session, ssh_channel channel, void *userdata)
{
(void)session;
(void)channel;
stateptr state = (stateptr) userdata;
fprintf(stdout, "out_eof callback called\n");
// Forward eof to in channel
ssh_channel_send_eof(state->in_channel);
}
void out_close (ssh_session session, ssh_channel channel, void *userdata)
{
(void)session;
(void)channel;
stateptr state = (stateptr) userdata;
fprintf(stdout, "out_close callback called\n");
// Close in channel
destroy_in_channel(state);
}
void out_signal (ssh_session session, ssh_channel channel, const char *signal, void *userdata)
{
(void)session;
(void)channel;
stateptr state = (stateptr) userdata;
fprintf(stdout, "out_signal callback called with signal %s\n", signal);
// Forward to out channel
ssh_channel_request_send_signal(state->in_channel, signal);
}
void out_exit_status (ssh_session session, ssh_channel channel, int exit_status, void *userdata)
{
(void)session;
(void)channel;
stateptr state = (stateptr) userdata;
fprintf(stdout, "out_exit_status callback called with status %d\n", exit_status);
// Forward to in channel
ssh_channel_request_send_exit_status(state->in_channel, exit_status);
}
void out_exit_signal (ssh_session session, ssh_channel channel, const char *signal, int core, const char *errmsg,
const char *lang, void *userdata)
{
(void)session;
(void)channel;
stateptr state = (stateptr) userdata;
fprintf(stdout, "out_exit_signal callback called with signal %s, core %d, error %s, lang %s\n",
signal, core, errmsg, lang);
// Forward to in channel
ssh_channel_request_send_exit_signal(state->in_channel, signal, core, errmsg, lang);
}
int out_pty_request (ssh_session session, ssh_channel channel, const char *term, int width, int height,
int pxwidth, int pxheight, void *userdata)
{
(void)session;
(void)channel;
(void)userdata;
fprintf(stdout, "out_pty_request_callback callback called with term %s, char dim %dx%d, px dim %dx%d (UNEXPECTED)\n",
term, width, height, pxwidth, pxheight);
return -1;
}
int out_shell_request (ssh_session session, ssh_channel channel, void *userdata)
{
(void)session;
(void)channel;
(void)userdata;
fprintf(stdout, "out_shell_request callback called (UNEXPECTED)\n");
return 1;
}
void out_auth_agent_req (ssh_session session, ssh_channel channel, void *userdata)
{
(void)session;
(void)channel;
(void)userdata;
fprintf(stdout, "out_auth_agent_req callback called (UNEXPECTED)\n");
}
void out_x11_req (ssh_session session, ssh_channel channel, int single_connection, const char *auth_protocol,
const char *auth_cookie, uint32_t screen_number, void *userdata)
{
(void)session;
(void)channel;
(void)userdata;
fprintf(stdout, "out_x11_req callback called, single_connection %d, auth protocol %s, auth cookie %s, screen %d (UNEXPECTED)\n",
single_connection, auth_protocol, auth_cookie, screen_number);
}
int out_pty_window_change (ssh_session session, ssh_channel channel, int width, int height, int pxwidth, int pxheight,
void *userdata)
{
(void)session;
(void)channel;
(void)userdata;
fprintf(stdout, "out_pty_window_change callback called with char dim %dx%d, px dim %dx%d (UNEXPECTED)\n",
width, height, pxwidth, pxheight);
return -1;
}
int out_exec_request (ssh_session session, ssh_channel channel, const char *command, void *userdata)
{
(void)session;
(void)channel;
(void)userdata;
fprintf(stdout, "out_exec_request callback called, command %s (UNEXPECTED)\n", command);
return 1;
}
int out_env_request (ssh_session session, ssh_channel channel, const char *env_name, const char *env_value, void *userdata)
{
(void)session;
(void)channel;
(void)userdata;
fprintf(stdout, "out_env_request callback called, %s = '%s' (UNEXPECTED)\n", env_name, env_value);
return 1;
}
int out_subsystem_request (ssh_session session, ssh_channel channel, const char *subsystem, void *userdata)
{
(void)session;
(void)channel;
(void)userdata;
fprintf(stdout, "out_subsystem_request callback called for %s (UNEXPECTED)\n", subsystem);
return 1;
}
int out_write_wontblock (ssh_session session, ssh_channel channel, uint32_t bytes, void *userdata)
{
(void)session;
(void)channel;
(void)userdata;
fprintf(stdout, "out_write_wontblock callback called with bytes = %d\n", bytes);
return 0;
}
struct ssh_channel_callbacks_struct out_channel_callbacks = {
.channel_data_function = out_data,
.channel_eof_function = out_eof,
.channel_close_function = out_close,
.channel_signal_function = out_signal,
.channel_exit_status_function = out_exit_status,
.channel_exit_signal_function = out_exit_signal,
.channel_pty_request_function = out_pty_request,
.channel_shell_request_function = out_shell_request,
.channel_auth_agent_req_function = out_auth_agent_req,
.channel_x11_req_function = out_x11_req,
.channel_pty_window_change_function = out_pty_window_change,
.channel_exec_request_function = out_exec_request,
.channel_env_request_function = out_env_request,
.channel_subsystem_request_function = out_subsystem_request,
.channel_write_wontblock_function = out_write_wontblock
};
ssh_channel create_out_channel(stateptr state)
{
int ok = 0;
// Initialise the callbacks struct
ssh_callbacks_init(&out_channel_callbacks);
out_channel_callbacks.userdata = state;
// Already have one?
if (state->out_channel) {
fprintf(stderr, "create_out_channel: outbound channel already active\n");
} else {
// Create the channel
state->out_channel = ssh_channel_new(state->out_session);
if (state->out_channel == NULL) {
fprintf(stderr, "create_out_channel: unable to create outbound channel\n");
} else {
// Set callbacks
ssh_set_channel_callbacks(state->out_channel, &out_channel_callbacks);
// Open the session
if (ssh_channel_open_session(state->out_channel) != SSH_OK){
fprintf(stderr, "create_out_channel: outbound channel could not be established\n");
} else {
// Finished successfully
ok = 1;
}
}
}
if (!ok) {
// Make sure out channel is destroyed
destroy_out_channel(state);
}
return state->out_channel;
}
void destroy_out_channel(stateptr state)
{
if (state->out_channel) {
ssh_channel_close(state->out_channel);
ssh_channel_free(state->out_channel);
state->out_channel = NULL;
if (state->in_channel == NULL) {
state->finished = 1;
}
}
}