Debian Codegraph @

tests/server/test_server/sftpserver_cb.c

raw
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 #include "test_server.h"
26 #include "default_cb.h"
27
28 #include <libssh/callbacks.h>
29 #include <libssh/server.h>
30 #include <libssh/priv.h>
31 #include <libssh/sftp.h>
32 #include <libssh/sftpserver.h>
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <stdbool.h>
38 #include <poll.h>
39 #include <signal.h>
40 #include <sys/wait.h>
41 #include <sys/ioctl.h>
42 #include <sys/stat.h>
43
44 #ifdef HAVE_LIBUTIL_H
45 #include <libutil.h>
46 #endif
47 #ifdef HAVE_PTY_H
48 #include <pty.h>
49 #endif
50 #ifdef HAVE_UTMP_H
51 #include <utmp.h>
52 #endif
53 #ifdef HAVE_UTIL_H
54 #include <util.h>
55 #endif
56
57 /* below are for sftp */
58 #include <sys/types.h>
59 #include <sys/statvfs.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <errno.h>
63
64 #include <fcntl.h>
65 #include <unistd.h>
66 #include <dirent.h>
67 #include <time.h>
68
69
70 #define BUF_SIZE 1048576
71 #define SESSION_END (SSH_CLOSED | SSH_CLOSED_ERROR)
72
73
74 /* TODO implement proper pam authentication cb */
75 static int sftp_auth_password_cb(UNUSED_PARAM(ssh_session session),
76 const char *user,
77 const char *password,
78 void *userdata)
79 {
80 bool known_user = false;
81 bool valid_password = false;
82
83 struct session_data_st *sdata;
84
85 sdata = (struct session_data_st *)userdata;
86
87 if (sdata == NULL) {
88 fprintf(stderr, "Error: NULL userdata\n");
89 goto null_userdata;
90 }
91
92 if (sdata->username == NULL) {
93 fprintf(stderr, "Error: expected username not set\n");
94 goto denied;
95 }
96
97 if (sdata->password == NULL) {
98 fprintf(stderr, "Error: expected password not set\n");
99 goto denied;
100 }
101
102 printf("Password authentication of user %s\n", user);
103
104 known_user = !(strcmp(user, sdata->username));
105 valid_password = !(strcmp(password, sdata->password));
106
107 if (known_user && valid_password) {
108 sdata->authenticated = 1;
109 sdata->auth_attempts = 0;
110 printf("Authenticated\n");
111 return SSH_AUTH_SUCCESS;
112 }
113
114 denied:
115 sdata->auth_attempts++;
116 null_userdata:
117 return SSH_AUTH_DENIED;
118 }
119
120 static ssh_channel sftp_channel_new_session_cb(ssh_session session, void *userdata)
121 {
122 struct session_data_st *sdata = NULL;
123 ssh_channel chan = NULL;
124
125 sdata = (struct session_data_st *)userdata;
126
127 if (sdata == NULL) {
128 fprintf(stderr, "NULL userdata");
129 goto end;
130 }
131
132 chan = ssh_channel_new(session);
133 if (chan == NULL) {
134 fprintf(stderr, "Error creating channel: %s\n",
135 ssh_get_error(session));
136 goto end;
137 }
138
139 sdata->channel = chan;
140
141 end:
142 return chan;
143 }
144
145 #ifdef WITH_PCAP
146 static void set_pcap(struct session_data_st *sdata,
147 ssh_session session,
148 char *pcap_file)
149 {
150 int rc = 0;
151
152 if (sdata == NULL) {
153 return;
154 }
155
156 if (pcap_file == NULL) {
157 return;
158 }
159
160 sdata->pcap = ssh_pcap_file_new();
161 if (sdata->pcap == NULL) {
162 return;
163 }
164
165 rc = ssh_pcap_file_open(sdata->pcap, pcap_file);
166 if (rc == SSH_ERROR) {
167 fprintf(stderr, "Error opening pcap file\n");
168 ssh_pcap_file_free(sdata->pcap);
169 sdata->pcap = NULL;
170 return;
171 }
172 ssh_set_pcap_file(session, sdata->pcap);
173 }
174
175 static void cleanup_pcap(struct session_data_st *sdata)
176 {
177 if (sdata == NULL) {
178 return;
179 }
180
181 if (sdata->pcap == NULL) {
182 return;
183 }
184
185 ssh_pcap_file_free(sdata->pcap);
186 sdata->pcap = NULL;
187 }
188 #endif
189
190
191 /* The caller is responsible to set the userdata to be provided to the callback
192 * The caller is responsible to free the allocated structure
193 */
194 struct ssh_server_callbacks_struct *get_sftp_server_cb(void)
195 {
196
197 struct ssh_server_callbacks_struct *cb;
198
199 cb = (struct ssh_server_callbacks_struct *)calloc(1,
200 sizeof(struct ssh_server_callbacks_struct));
201
202 if (cb == NULL) {
203 fprintf(stderr, "Out of memory\n");
204 goto end;
205 }
206
207 cb->auth_password_function = sftp_auth_password_cb;
208 cb->channel_open_request_session_function = sftp_channel_new_session_cb;
209
210 end:
211 return cb;
212 }
213
214 /* The caller is responsible to set the userdata to be provided to the callback
215 * The caller is responsible to free the allocated structure
216 * */
217 struct ssh_channel_callbacks_struct *get_sftp_channel_cb(void)
218 {
219 struct ssh_channel_callbacks_struct *cb;
220
221 cb = (struct ssh_channel_callbacks_struct *)calloc(1,
222 sizeof(struct ssh_channel_callbacks_struct));
223 if (cb == NULL) {
224 fprintf(stderr, "Out of memory\n");
225 goto end;
226 }
227
228 cb->channel_data_function = sftp_channel_default_data_callback;
229 cb->channel_subsystem_request_function = sftp_channel_default_subsystem_request;
230
231 end:
232 return cb;
233 };
234
235 void sftp_handle_session_cb(ssh_event event,
236 ssh_session session,
237 struct server_state_st *state)
238 {
239 int n;
240 int rc = 0;
241
242 /* Our struct holding information about the channel. */
243 struct channel_data_st cdata = {
244 .sftp = NULL,
245 };
246
247 /* Our struct holding information about the session. */
248 struct session_data_st sdata = {
249 .channel = NULL,
250 .auth_attempts = 0,
251 .authenticated = 0,
252 .username = SSHD_DEFAULT_USER,
253 .password = SSHD_DEFAULT_PASSWORD,
254 };
255
256 struct ssh_channel_callbacks_struct *channel_cb = NULL;
257 struct ssh_server_callbacks_struct *server_cb = NULL;
258
259 if (state == NULL) {
260 fprintf(stderr, "NULL server state provided\n");
261 goto end;
262 }
263
264 /* If callbacks were provided use them. Otherwise, use default callbacks */
265 if (state->server_cb != NULL) {
266 /* This is a macro, it does not return a value */
267 ssh_callbacks_init(state->server_cb);
268
269 rc = ssh_set_server_callbacks(session, state->server_cb);
270 if (rc) {
271 goto end;
272 }
273 } else {
274 server_cb = get_sftp_server_cb();
275 if (server_cb == NULL) {
276 goto end;
277 }
278
279 server_cb->userdata = &sdata;
280
281 /* This is a macro, it does not return a value */
282 ssh_callbacks_init(server_cb);
283
284 rc = ssh_set_server_callbacks(session, server_cb);
285 if (rc) {
286 goto end;
287 }
288 }
289
290 sdata.server_state = (void *)state;
291
292 #ifdef WITH_PCAP
293 set_pcap(&sdata, session, state->pcap_file);
294 #endif
295
296 if (state->expected_username != NULL) {
297 sdata.username = state->expected_username;
298 }
299
300 if (state->expected_password != NULL) {
301 sdata.password = state->expected_password;
302 }
303
304 if (ssh_handle_key_exchange(session) != SSH_OK) {
305 fprintf(stderr, "%s\n", ssh_get_error(session));
306 goto end;
307 }
308
309 /* Set the supported authentication methods */
310 if (state->auth_methods) {
311 ssh_set_auth_methods(session, state->auth_methods);
312 } else {
313 ssh_set_auth_methods(session,
314 SSH_AUTH_METHOD_PASSWORD |
315 SSH_AUTH_METHOD_PUBLICKEY);
316 }
317
318 ssh_event_add_session(event, session);
319
320 n = 0;
321 while (sdata.authenticated == 0 || sdata.channel == NULL) {
322 /* If the user has used up all attempts, or if he hasn't been able to
323 * authenticate in 10 seconds (n * 100ms), disconnect. */
324 if (sdata.auth_attempts >= state->max_tries || n >= 100) {
325 goto end;
326 }
327
328 if (ssh_event_dopoll(event, 100) == SSH_ERROR) {
329 fprintf(stderr, "do_poll error: %s\n", ssh_get_error(session));
330 goto end;
331 }
332 n++;
333 }
334
335 /* TODO check return values */
336 if (state->channel_cb != NULL) {
337 ssh_callbacks_init(state->channel_cb);
338
339 rc = ssh_set_channel_callbacks(sdata.channel, state->channel_cb);
340 if (rc) {
341 goto end;
342 }
343 } else {
344 channel_cb = get_sftp_channel_cb();
345 if (channel_cb == NULL) {
346 goto end;
347 }
348
349 channel_cb->userdata = &(cdata.sftp);
350
351 ssh_callbacks_init(channel_cb);
352 rc = ssh_set_channel_callbacks(sdata.channel, channel_cb);
353 if (rc) {
354 goto end;
355 }
356 }
357
358 do {
359 /* Poll the main event which takes care of the session, the channel and
360 * even our child process's stdout/stderr (once it's started). */
361 if (ssh_event_dopoll(event, 100) == SSH_ERROR) {
362 ssh_channel_close(sdata.channel);
363 }
364 } while (ssh_channel_is_open(sdata.channel) &&
365 !ssh_channel_is_eof(sdata.channel));
366
367 ssh_channel_send_eof(sdata.channel);
368 ssh_channel_close(sdata.channel);
369 sftp_server_free(cdata.sftp);
370
371 /* Wait up to 5 seconds for the client to terminate the session. */
372 for (n = 0; n < 50 && (ssh_get_status(session) & SESSION_END) == 0; n++) {
373 ssh_event_dopoll(event, 100);
374 }
375
376 end:
377 #ifdef WITH_PCAP
378 cleanup_pcap(&sdata);
379 #endif
380 if (channel_cb != NULL) {
381 free(channel_cb);
382 }
383 if (server_cb != NULL) {
384 free(server_cb);
385 }
386 return;
387 }