Debian Codegraph @

contrib/mod_sftp/interop.c

raw symbols SCIP
1 /*
2 * ProFTPD - mod_sftp interoperability
3 * Copyright (c) 2008-2024 TJ Saunders
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, TJ Saunders and other respective copyright holders
20 * give permission to link this program with OpenSSL, and distribute the
21 * resulting executable, without including the source code for OpenSSL in the
22 * source distribution.
23 */
24
25 #include "mod_sftp.h"
26 #include "ssh2.h"
27 #include "channel.h"
28 #include "disconnect.h"
29 #include "interop.h"
30 #include "fxp.h"
31
32 extern module sftp_module;
33
34 /* By default, each client is assumed to support all of the features in
35 * which we are interested.
36 */
37 static unsigned int default_flags =
38 SFTP_SSH2_FEAT_IGNORE_MSG |
39 SFTP_SSH2_FEAT_MAC_LEN |
40 SFTP_SSH2_FEAT_CIPHER_USE_K |
41 SFTP_SSH2_FEAT_REKEYING |
42 SFTP_SSH2_FEAT_USERAUTH_BANNER |
43 SFTP_SSH2_FEAT_HAVE_PUBKEY_ALGO |
44 SFTP_SSH2_FEAT_SERVICE_IN_HOST_SIG |
45 SFTP_SSH2_FEAT_SERVICE_IN_PUBKEY_SIG |
46 SFTP_SSH2_FEAT_HAVE_PUBKEY_ALGO_IN_DSA_SIG |
47 SFTP_SSH2_FEAT_NO_DATA_WHILE_REKEYING |
48 SFTP_SSH2_FEAT_HOSTKEYS |
49 SFTP_SSH2_FEAT_USE_FULL_FXP_LIMITS;
50
51 struct sftp_version_pattern {
52 const char *pattern;
53 int disabled_flags;
54 pr_regex_t *pre;
55 };
56
57 static struct sftp_version_pattern known_versions[] = {
58 { "^Cisco-1\\..*", SFTP_SSH2_FEAT_HOSTKEYS, NULL },
59
60 { "^OpenSSH-2\\.0.*|"
61 "^OpenSSH-2\\.1.*|"
62 "^OpenSSH_2\\.1.*|"
63 "^OpenSSH_2\\.2.*|"
64 "^OpenSSH_2\\.3\\.0.*", SFTP_SSH2_FEAT_USERAUTH_BANNER|
65 SFTP_SSH2_FEAT_REKEYING, NULL },
66
67 { "^OpenSSH_2\\.3\\..*|"
68 "^OpenSSH_2\\.5\\.0p1.*|"
69 "^OpenSSH_2\\.5\\.1p1.*|"
70 "^OpenSSH_2\\.5\\.0.*|"
71 "^OpenSSH_2\\.5\\.1.*|"
72 "^OpenSSH_2\\.5\\.2.*|"
73 "^OpenSSH_2\\.5\\.3.*", SFTP_SSH2_FEAT_REKEYING, NULL },
74
75 { "^OpenSSH_8\\..*|"
76 "^OpenSSH_9\\.0.*|"
77 "^OpenSSH_9\\.1.*", SFTP_SSH2_FEAT_USE_FULL_FXP_LIMITS, NULL },
78
79 { "^OpenSSH.*", 0, NULL },
80
81 { ".*J2SSH_Maverick.*", SFTP_SSH2_FEAT_REKEYING, NULL },
82
83 { ".*MindTerm.*", 0, NULL },
84
85 { "^Sun_SSH_1\\.0.*", SFTP_SSH2_FEAT_REKEYING, NULL },
86
87 { "^TeraTerm SSH.*|"
88 "^TTSSH/1\\.5\\..*|"
89 "^TTSSH/2\\.1.*|"
90 "^TTSSH/2\\.2.*|"
91 "^TTSSH/2\\.3.*|"
92 "^TTSSH/2\\.4.*|"
93 "^TTSSH/2\\.5.*|"
94 "^TTSSH/2\\.6.*|"
95 "^TTSSH/2\\.70.*|"
96 "^TTSSH/2\\.71.*|"
97 "^TTSSH/2\\.72.*", SFTP_SSH2_FEAT_HOSTKEYS, NULL },
98
99 { "^2\\.1\\.0.*|"
100 "^2\\.1 .*", SFTP_SSH2_FEAT_HAVE_PUBKEY_ALGO_IN_DSA_SIG|
101 SFTP_SSH2_FEAT_SERVICE_IN_HOST_SIG|
102 SFTP_SSH2_FEAT_MAC_LEN, NULL },
103
104 { "^2\\.0\\.13.*|"
105 "^2\\.0\\.14.*|"
106 "^2\\.0\\.15.*|"
107 "^2\\.0\\.16.*|"
108 "^2\\.0\\.17.*|"
109 "^2\\.0\\.18.*|"
110 "^2\\.0\\.19.*", SFTP_SSH2_FEAT_HAVE_PUBKEY_ALGO_IN_DSA_SIG|
111 SFTP_SSH2_FEAT_SERVICE_IN_HOST_SIG|
112 SFTP_SSH2_FEAT_SERVICE_IN_PUBKEY_SIG|
113 SFTP_SSH2_FEAT_MAC_LEN, NULL },
114
115 { "^2\\.0\\.11.*|"
116 "^2\\.0\\.12.*", SFTP_SSH2_FEAT_HAVE_PUBKEY_ALGO_IN_DSA_SIG|
117 SFTP_SSH2_FEAT_SERVICE_IN_PUBKEY_SIG|
118 SFTP_SSH2_FEAT_HAVE_PUBKEY_ALGO|
119 SFTP_SSH2_FEAT_MAC_LEN, NULL },
120
121 { "^2\\.0\\..*", SFTP_SSH2_FEAT_HAVE_PUBKEY_ALGO_IN_DSA_SIG|
122 SFTP_SSH2_FEAT_SERVICE_IN_PUBKEY_SIG|
123 SFTP_SSH2_FEAT_HAVE_PUBKEY_ALGO|
124 SFTP_SSH2_FEAT_CIPHER_USE_K|
125 SFTP_SSH2_FEAT_MAC_LEN, NULL },
126
127 { "^2\\.2\\.0.*|"
128 "^2\\.3\\.0.*", SFTP_SSH2_FEAT_MAC_LEN, NULL },
129
130
131 { "^1\\.2\\.18.*|"
132 "^1\\.2\\.19.*|"
133 "^1\\.2\\.20.*|"
134 "^1\\.2\\.21.*|"
135 "^1\\.2\\.22.*|"
136 "^1\\.3\\.2.*|"
137 "^3\\.2\\.9.*", SFTP_SSH2_FEAT_IGNORE_MSG, NULL },
138
139 { ".*PuTTY.*|"
140 ".*PUTTY.*|"
141 ".*WinSCP.*", SFTP_SSH2_FEAT_NO_DATA_WHILE_REKEYING, NULL },
142
143 { ".*SSH_Version_Mapper.*", SFTP_SSH2_FEAT_SCANNER, NULL },
144
145 { "^Probe-.*", SFTP_SSH2_FEAT_PROBE, NULL },
146
147 { NULL, 0, NULL },
148 };
149
150 static const char *trace_channel = "ssh2";
151
152 int sftp_interop_handle_version(pool *p, const char *client_version) {
153 register unsigned int i;
154 size_t version_len;
155 const char *version = NULL;
156 char *ptr = NULL;
157 int is_probe = FALSE, is_scan = FALSE;
158 config_rec *c;
159
160 if (client_version == NULL) {
161 errno = EINVAL;
162 return -1;
163 }
164
165 version_len = strlen(client_version);
166
167 /* The version string MUST conform to the following, as per Section 4.2
168 * of RFC4253:
169 *
170 * SSH-protoversion-softwareversion [SP comments]
171 *
172 * The 'comments' field is optional. The 'protoversion' MUST be "2.0".
173 * The 'softwareversion' field MUST be printable ASCII characters and
174 * cannot contain SP or the '-' character.
175 */
176
177 for (i = 0; i < version_len; i++) {
178 if (!PR_ISPRINT(client_version[i]) &&
179 client_version[i] != '-' &&
180 client_version[i] != ' ') {
181 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
182 "client-sent version contains non-printable or illegal characters, "
183 "disconnecting client");
184 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED,
185 NULL);
186 }
187 }
188
189 /* Skip past the leading "SSH-2.0-" (or "SSH-1.99-") to get the actual
190 * client info.
191 */
192 if (strncmp(client_version, "SSH-2.0-", 8) == 0) {
193 version = pstrdup(p, client_version + 8);
194
195 } else if (strncmp(client_version, "SSH-1.99-", 9) == 0) {
196 version = pstrdup(p, client_version + 9);
197
198 } else {
199 /* An illegally formatted client version. How did it get here? */
200 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
201 "client-sent version (%s) is illegally formmated, disconnecting client",
202 client_version);
203 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED,
204 NULL);
205 }
206
207 /* Look for the optional comments field in the received client version; if
208 * present, trim it out, so that we do not try to match on it.
209 */
210 ptr = strchr(version, ' ');
211 if (ptr != NULL) {
212 pr_trace_msg(trace_channel, 11, "read client version with comments: '%s'",
213 version);
214 *ptr = '\0';
215 }
216
217 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
218 "handling connection from SSH2 client '%s'", version);
219 pr_trace_msg(trace_channel, 5, "handling connection from SSH2 client '%s'",
220 version);
221
222 /* First matching pattern wins. */
223 for (i = 0; known_versions[i].pattern != NULL; i++) {
224 int res;
225
226 pr_signals_handle();
227
228 pr_trace_msg(trace_channel, 18,
229 "checking client version '%s' against regex '%s'", version,
230 known_versions[i].pattern);
231
232 res = pr_regexp_exec(known_versions[i].pre, version, 0, NULL, 0, 0, 0);
233 if (res == 0) {
234 pr_trace_msg(trace_channel, 18,
235 "client version '%s' matched against regex '%s'", version,
236 known_versions[i].pattern);
237
238 /* We have a match. */
239 default_flags &= ~(known_versions[i].disabled_flags);
240
241 if (known_versions[i].disabled_flags == SFTP_SSH2_FEAT_PROBE) {
242 is_probe = TRUE;
243 }
244
245 if (known_versions[i].disabled_flags == SFTP_SSH2_FEAT_SCANNER) {
246 is_scan = TRUE;
247 }
248
249 break;
250
251 } else {
252 pr_trace_msg(trace_channel, 18,
253 "client version '%s' did not match regex '%s'", version,
254 known_versions[i].pattern);
255 }
256 }
257
258 /* Disconnect probes/scans here. */
259 if (is_probe) {
260 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
261 "SSH2 probe from '%s', disconnecting", version);
262
263 /* We should use the PROTOCOL_VERSION_NOT_SUPPORTED disconnect code,
264 * but for probes/scans, simply hanging up on the client seems better.
265 */
266 pr_session_disconnect(&sftp_module, PR_SESS_DISCONNECT_BY_APPLICATION,
267 NULL);
268 }
269
270 if (is_scan) {
271 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
272 "SSH2 scan from '%s', disconnecting", version);
273
274 /* We should use the PROTOCOL_VERSION_NOT_SUPPORTED disconnect code,
275 * but for probes/scans, simply hanging up on the client seems better.
276 */
277 pr_session_disconnect(&sftp_module, PR_SESS_DISCONNECT_BY_APPLICATION,
278 NULL);
279 }
280
281 /* Now iterate through any SFTPClientMatch rules. */
282
283 c = find_config(main_server->conf, CONF_PARAM, "SFTPClientMatch", FALSE);
284 while (c != NULL) {
285 int res;
286 char *pattern;
287 pr_regex_t *pre;
288
289 pr_signals_handle();
290
291 pattern = c->argv[0];
292 pre = c->argv[1];
293
294 pr_trace_msg(trace_channel, 18,
295 "checking client version '%s' against SFTPClientMatch regex '%s'",
296 version, pattern);
297
298 res = pr_regexp_exec(pre, version, 0, NULL, 0, 0, 0);
299 if (res == 0) {
300 pr_table_t *tab;
301 const void *v, *v2;
302
303 /* We have a match. */
304
305 tab = c->argv[2];
306
307 /* Look for the following keys:
308 *
309 * channelWindowSize
310 * channelPacketSize
311 * pessimisticNewkeys
312 * sftpCiphers
313 * sftpDigests
314 * sftpHostKeys
315 * sftpKeyExchanges
316 * sftpMinProtocolVersion
317 * sftpMaxProtocolVersion
318 * sftpUTF8ProtocolVersion (only if NLS support is enabled)
319 */
320
321 v = pr_table_get(tab, "channelWindowSize", NULL);
322 if (v != NULL) {
323 uint32_t window_size;
324
325 window_size = *((uint32_t *) v);
326
327 pr_trace_msg(trace_channel, 16, "setting max server channel window "
328 "size to %lu bytes, as per SFTPClientMatch",
329 (unsigned long) window_size);
330
331 sftp_channel_set_max_windowsz(window_size);
332 }
333
334 v = pr_table_get(tab, "channelPacketSize", NULL);
335 if (v != NULL) {
336 uint32_t packet_size;
337
338 packet_size = *((uint32_t *) v);
339
340 pr_trace_msg(trace_channel, 16, "setting max server channel packet "
341 "size to %lu bytes, as per SFTPClientMatch",
342 (unsigned long) packet_size);
343
344 sftp_channel_set_max_packetsz(packet_size);
345 }
346
347 v = pr_table_get(tab, "pessimisticNewkeys", NULL);
348 if (v != NULL) {
349 int pessimistic_newkeys;
350
351 pessimistic_newkeys = *((int *) v);
352
353 pr_trace_msg(trace_channel, 16,
354 "setting pessimistic NEWKEYS behavior to %s, as per SFTPClientMatch",
355 pessimistic_newkeys ? "true" : "false");
356
357 if (pessimistic_newkeys) {
358 default_flags |= SFTP_SSH2_FEAT_PESSIMISTIC_NEWKEYS;
359 }
360 }
361
362 v = pr_table_get(tab, "sftpCiphers", NULL);
363 if (v != NULL) {
364 config_rec *ciphers;
365
366 ciphers = *((config_rec **) v);
367
368 pr_trace_msg(trace_channel, 16,
369 "setting new SSH ciphers, per SFTPClientMatch");
370
371 remove_config(main_server->conf, "SFTPCiphers", FALSE);
372 pr_config_add_config_to_set(main_server->conf, ciphers, 0);
373 }
374
375 v = pr_table_get(tab, "sftpDigests", NULL);
376 if (v != NULL) {
377 config_rec *digests;
378
379 digests = *((config_rec **) v);
380
381 pr_trace_msg(trace_channel, 16,
382 "setting new SSH digests, per SFTPClientMatch");
383
384 remove_config(main_server->conf, "SFTPDigests", FALSE);
385 pr_config_add_config_to_set(main_server->conf, digests, 0);
386 }
387
388 v = pr_table_get(tab, "sftpHostKeys", NULL);
389 if (v != NULL) {
390 config_rec *hostkeys;
391
392 hostkeys = *((config_rec **) v);
393
394 pr_trace_msg(trace_channel, 16,
395 "setting new SSH host key algorithms, per SFTPClientMatch");
396
397 remove_config(main_server->conf, "SFTPHostKeys", FALSE);
398 pr_config_add_config_to_set(main_server->conf, hostkeys, 0);
399 }
400
401 v = pr_table_get(tab, "sftpKeyExchanges", NULL);
402 if (v != NULL) {
403 config_rec *key_exchanges;
404
405 key_exchanges = *((config_rec **) v);
406
407 pr_trace_msg(trace_channel, 16,
408 "setting new SSH key exchanges, per SFTPClientMatch");
409
410 remove_config(main_server->conf, "SFTPKeyExchanges", FALSE);
411 pr_config_add_config_to_set(main_server->conf, key_exchanges, 0);
412 }
413
414 v = pr_table_get(tab, "sftpMinProtocolVersion", NULL);
415 v2 = pr_table_get(tab, "sftpMaxProtocolVersion", NULL);
416 if (v != NULL &&
417 v2 != NULL) {
418 unsigned int min_version, max_version;
419
420 min_version = *((unsigned int *) v);
421 max_version = *((unsigned int *) v2);
422
423 if (min_version != max_version) {
424 pr_trace_msg(trace_channel, 16, "setting SFTP protocol version "
425 "range %u-%u, as per SFTPClientMatch", min_version,
426 max_version);
427
428 } else {
429 pr_trace_msg(trace_channel, 16, "setting SFTP protocol version "
430 "%u, as per SFTPClientMatch", min_version);
431 }
432
433 sftp_fxp_set_protocol_version(min_version, max_version);
434 }
435
436 #ifdef PR_USE_NLS
437 v = pr_table_get(tab, "sftpUTF8ProtocolVersion", NULL);
438 if (v != NULL) {
439 unsigned int protocol_version;
440
441 protocol_version = *((unsigned int *) v);
442 pr_trace_msg(trace_channel, 16, "setting SFTP UTF8 protocol version "
443 "%u, as per SFTPClientMatch", protocol_version);
444
445 sftp_fxp_set_utf8_protocol_version(protocol_version);
446 }
447 #endif /* PR_USE_NLS */
448
449 /* Once we're done, we can destroy the table. */
450 (void) pr_table_empty(tab);
451 (void) pr_table_free(tab);
452 c->argv[2] = NULL;
453
454 } else {
455 pr_trace_msg(trace_channel, 18,
456 "client version '%s' did not match SFTPClientMatch regex '%s'", version,
457 pattern);
458 }
459
460 c = find_config_next(c, c->next, CONF_PARAM, "SFTPClientMatch", FALSE);
461 }
462
463 return 0;
464 }
465
466 int sftp_interop_supports_feature(int feat_flag) {
467 switch (feat_flag) {
468 case SFTP_SSH2_FEAT_PROBE:
469 case SFTP_SSH2_FEAT_SCANNER:
470 /* Scanners and probes would have been disconnected by now. */
471 return FALSE;
472
473 default:
474 if (!(default_flags & feat_flag)) {
475 return FALSE;
476 }
477 }
478
479 return TRUE;
480 }
481
482 int sftp_interop_init(void) {
483 register unsigned int i;
484
485 /* Compile the regexps for all of the known client versions, to save the
486 * time when a client connects.
487 */
488 for (i = 0; known_versions[i].pattern != NULL; i++) {
489 pr_regex_t *pre;
490 int res;
491
492 pr_signals_handle();
493
494 pre = pr_regexp_alloc(&sftp_module);
495
496 res = pr_regexp_compile(pre, known_versions[i].pattern,
497 REG_EXTENDED|REG_NOSUB);
498 if (res != 0) {
499 char errmsg[256];
500
501 memset(errmsg, '\0', sizeof(errmsg));
502 pr_regexp_error(res, pre, errmsg, sizeof(errmsg));
503 pr_regexp_free(NULL, pre);
504
505 pr_log_debug(DEBUG0, MOD_SFTP_VERSION
506 ": error compiling regex pattern '%s' (known_versions[%u]): %s",
507 known_versions[i].pattern, i, errmsg);
508 continue;
509 }
510
511 known_versions[i].pre = pre;
512 }
513
514 return 0;
515 }
516
517 int sftp_interop_free(void) {
518 register unsigned int i;
519
520 for (i = 0; known_versions[i].pattern != NULL; i++) {
521 if (known_versions[i].pre != NULL) {
522 pr_regexp_free(NULL, known_versions[i].pre);
523 known_versions[i].pre = NULL;
524 }
525 }
526
527 return 0;
528 }