Debian Codegraph @

contrib/mod_sftp/mod_sftp.c

raw symbols SCIP
1 /*
2 * ProFTPD - mod_sftp
3 * Copyright (c) 2008-2025 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 * -----DO NOT EDIT BELOW THIS LINE-----
25 * $Archive: mod_sftp.a$
26 * $Libraries: -lcrypto$
27 */
28
29 #include "mod_sftp.h"
30 #include "ssh2.h"
31 #include "packet.h"
32 #include "interop.h"
33 #include "crypto.h"
34 #include "cipher.h"
35 #include "mac.h"
36 #include "keys.h"
37 #include "keystore.h"
38 #include "disconnect.h"
39 #include "kex.h"
40 #include "blacklist.h"
41 #include "service.h"
42 #include "auth.h"
43 #include "channel.h"
44 #include "tap.h"
45 #include "kbdint.h"
46 #include "fxp.h"
47 #include "utf8.h"
48
49 #if defined(HAVE_SODIUM_H)
50 # include <sodium.h>
51 #endif /* HAVE_SODIUM_H */
52
53 extern xaset_t *server_list;
54
55 module sftp_module;
56
57 int sftp_logfd = -1;
58 const char *sftp_logname = NULL;
59 pool *sftp_pool = NULL;
60 conn_t *sftp_conn = NULL;
61 unsigned int sftp_sess_state = 0;
62 unsigned long sftp_opts = 0UL;
63 unsigned int sftp_services = SFTP_SERVICE_DEFAULT;
64
65 static int sftp_engine = 0;
66 static const char *sftp_client_version = NULL;
67 static const char *sftp_server_version = SFTP_ID_DEFAULT_STRING;
68
69 /* Flags for changing how hostkeys are handled. */
70 #define SFTP_HOSTKEY_FL_CLEAR_RSA_KEY 0x001
71 #define SFTP_HOSTKEY_FL_CLEAR_DSA_KEY 0x002
72 #define SFTP_HOSTKEY_FL_CLEAR_ECDSA_KEY 0x004
73 #define SFTP_HOSTKEY_FL_CLEAR_ED25519_KEY 0x008
74
75 #if defined(HAVE_OSSL_PROVIDER_LOAD_OPENSSL)
76 static OSSL_PROVIDER *legacy_provider = NULL;
77 #endif /* HAVE_OSSL_PROVIDER_LOAD_OPENSSL */
78
79 static const char *trace_channel = "ssh2";
80
81 static int sftp_have_authenticated(cmd_rec *cmd) {
82 return (sftp_sess_state & SFTP_SESS_STATE_HAVE_AUTH);
83 }
84
85 static int sftp_get_client_version(conn_t *conn) {
86 int res;
87
88 /* 255 is the RFC-defined maximum banner/ID string size */
89 char buf[256], *banner = NULL;
90 size_t buflen = 0;
91
92 /* Read client version. This looks ugly, reading one byte at a time.
93 * It is necessary, though. The banner sent by the client is not of any
94 * guaranteed length. The client might also send the next SSH packet in
95 * the exchange, such that both messages are in the socket buffer. If
96 * we read too much of the banner, we'll read into the KEXINIT, for example,
97 * and cause problems later.
98 */
99
100 while (TRUE) {
101 register unsigned int i;
102 int bad_proto = FALSE;
103
104 pr_signals_handle();
105
106 memset(buf, '\0', sizeof(buf));
107
108 for (i = 0; i < sizeof(buf) - 1; i++) {
109 res = sftp_ssh2_packet_sock_read(conn->rfd, &buf[i], 1, 0);
110 while (res <= 0) {
111 int xerrno = errno;
112
113 if (xerrno == EINTR) {
114 pr_signals_handle();
115
116 res = sftp_ssh2_packet_sock_read(conn->rfd, &buf[i], 1, 0);
117 continue;
118 }
119
120 if (res < 0) {
121 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
122 "error reading from client rfd %d: %s", conn->rfd,
123 strerror(xerrno));
124 }
125
126 errno = xerrno;
127 return res;
128 }
129
130 /* We continue reading until the client has sent the terminating
131 * CRLF sequence.
132 */
133 if (buf[i] == '\r') {
134 buf[i] = '\0';
135 continue;
136 }
137
138 if (buf[i] == '\n') {
139 buf[i] = '\0';
140 break;
141 }
142 }
143
144 if (i == sizeof(buf)-1) {
145 bad_proto = TRUE;
146
147 } else {
148 buf[sizeof(buf)-1] = '\0';
149 buflen = strlen(buf);
150 }
151
152 /* If the line does not begin with "SSH-2.0-", skip it. RFC4253, Section
153 * 4.2 does not specify what should happen if the client sends data
154 * other than the proper version string initially.
155 *
156 * If we have been configured for compatibility with old protocol
157 * implementations, check for "SSH-1.99-" as well.
158 *
159 * OpenSSH simply disconnects the client after saying "Protocol mismatch"
160 * if the client's version string does not begin with "SSH-2.0-"
161 * (or "SSH-1.99-"). Works for me.
162 */
163 if (bad_proto == FALSE) {
164 if (strncmp(buf, "SSH-2.0-", 8) != 0) {
165 bad_proto = TRUE;
166
167 if (sftp_opts & SFTP_OPT_OLD_PROTO_COMPAT) {
168 if (strncmp(buf, "SSH-1.99-", 9) == 0) {
169 if (buflen == 9) {
170 /* The client sent ONLY "SSH-1.99-". OpenSSH handles this as a
171 * "Protocol mismatch", so shall we.
172 */
173 bad_proto = TRUE;
174
175 } else {
176 banner = buf + 9;
177 bad_proto = FALSE;
178 }
179 }
180 }
181
182 } else {
183 if (buflen == 8) {
184 /* The client sent ONLY "SSH-2.0-". OpenSSH handles this as a
185 * "Protocol mismatch", so shall we.
186 */
187 bad_proto = TRUE;
188
189 } else {
190 banner = buf + 8;
191 }
192 }
193 }
194
195 if (banner != NULL) {
196 char *k, *v;
197
198 k = pstrdup(session.pool, "SFTP_CLIENT_BANNER");
199 v = pstrdup(session.pool, banner);
200 pr_env_unset(session.pool, k);
201 pr_env_set(session.pool, k, v);
202 (void) pr_table_add(session.notes, k, v, 0);
203 }
204
205 if (bad_proto) {
206 const char *errstr = "Protocol mismatch.\n";
207
208 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
209 "Bad protocol version '%.100s' from %s", buf,
210 pr_netaddr_get_ipstr(session.c->remote_addr));
211
212 if (write(conn->wfd, errstr, strlen(errstr)) < 0) {
213 pr_trace_msg("ssh2", 9,
214 "error sending 'Protocol mismatch' message to client: %s",
215 strerror(errno));
216 }
217
218 errno = EINVAL;
219 return -1;
220 }
221
222 break;
223 }
224
225 sftp_client_version = pstrdup(sftp_pool, buf);
226 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
227 "received client version '%s'", sftp_client_version);
228
229 if (sftp_interop_handle_version(sftp_pool, sftp_client_version) < 0) {
230 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
231 "error checking client version '%s' for interoperability: %s",
232 sftp_client_version, strerror(errno));
233 }
234
235 return 0;
236 }
237
238 static void sftp_cmd_loop(server_rec *s, conn_t *conn) {
239 int res;
240 char buf[256];
241 const char *k, *v;
242
243 /* Include the client, server IP addresses, ports in the SFTPLog; add the
244 * session ID, if present, as well.
245 */
246 k = "UNIQUE_ID";
247 v = pr_table_get(session.notes, k, NULL);
248 if (v != NULL) {
249 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
250 "client (%s:%d) connected to server (%s:%d) [session ID %s]",
251 pr_netaddr_get_ipstr(session.c->remote_addr), session.c->remote_port,
252 pr_netaddr_get_ipstr(session.c->local_addr), session.c->local_port, v);
253
254 } else {
255 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
256 "client (%s:%d) connected to server (%s:%d)",
257 pr_netaddr_get_ipstr(session.c->remote_addr), session.c->remote_port,
258 pr_netaddr_get_ipstr(session.c->local_addr), session.c->local_port);
259 }
260
261 sftp_ssh2_packet_set_handler(NULL);
262 sftp_conn = conn;
263 pr_session_set_protocol("ssh2");
264
265 if (sftp_opts & SFTP_OPT_PESSIMISTIC_KEXINIT) {
266 /* If we are being pessimistic, we will send our version string to the
267 * client now, and send our KEXINIT message later.
268 */
269 res = sftp_ssh2_packet_send_version();
270
271 } else {
272 /* If we are being optimistic, we can reduce the connection latency
273 * by sending our KEXINIT message now; this will have the server version
274 * string automatically prepended.
275 */
276 res = sftp_kex_send_first_kexinit();
277 }
278
279 if (res < 0) {
280 pr_session_disconnect(&sftp_module, PR_SESS_DISCONNECT_BY_APPLICATION,
281 NULL);
282 }
283
284 res = sftp_get_client_version(conn);
285 if (res < 0) {
286 pr_session_disconnect(&sftp_module, PR_SESS_DISCONNECT_BY_APPLICATION,
287 NULL);
288 }
289
290 sftp_kex_init(sftp_client_version, sftp_server_version);
291 sftp_service_init();
292 sftp_auth_init();
293 sftp_channel_init();
294
295 /* Set the initial timeout for reading packets from clients. Using
296 * a value of zero sets the default timeout value (i.e. TimeoutIdle).
297 */
298 sftp_ssh2_packet_set_poll_timeout(0);
299
300 k = pstrdup(session.pool, "SFTP");
301 v = pstrdup(session.pool, "1");
302 pr_env_set(session.pool, k, v);
303 (void) pr_table_add(session.notes, k, v, 0);
304
305 k = pstrdup(session.pool, "SFTP_LIBRARY_VERSION");
306 v = pstrdup(session.pool, OPENSSL_VERSION_TEXT);
307 pr_env_set(session.pool, k, v);
308 (void) pr_table_add(session.notes, k, v, 0);
309
310 memset(buf, '\0', sizeof(buf));
311 k = pstrdup(session.pool, "SSH_CONNECTION");
312 pr_snprintf(buf, sizeof(buf)-1, "%.50s %d %.50s %d",
313 pr_netaddr_get_ipstr(conn->remote_addr), conn->remote_port,
314 pr_netaddr_get_ipstr(conn->local_addr), conn->local_port);
315 v = pstrdup(session.pool, buf);
316 pr_env_set(session.pool, k, v);
317 (void) pr_table_add(session.notes, k, v, 0);
318
319 /* If we didn't send our KEXINIT earlier, send it now. */
320 if (sftp_opts & SFTP_OPT_PESSIMISTIC_KEXINIT) {
321 res = sftp_kex_send_first_kexinit();
322 if (res < 0) {
323 pr_session_disconnect(&sftp_module, PR_SESS_DISCONNECT_BY_APPLICATION,
324 NULL);
325 }
326 }
327
328 while (TRUE) {
329 pr_signals_handle();
330
331 res = sftp_ssh2_packet_process(sftp_pool);
332 if (res < 0) {
333 break;
334 }
335 }
336
337 return;
338 }
339
340 /* Configuration handlers
341 */
342
343 static config_rec *create_config(pool *p, const char *name, int argc) {
344 config_rec *c;
345 pool *conf_pool;
346
347 /* Now, make a subpool for the config_rec to be allocated. The default
348 * pool size (PR_TUNABLE_NEW_POOL_SIZE, 512 by default) is a bit large
349 * for config_rec pools; use a smaller size.
350 */
351 conf_pool = pr_pool_create_sz(p, 128);
352
353 c = pr_config_alloc(conf_pool, name, CONF_PARAM);
354 c->argc = argc;
355 c->argv = pcalloc(c->pool, (c->argc + 1) * sizeof(char *));
356
357 return c;
358 }
359
360 /* usage: SFTPAcceptEnv env1 ... envN */
361 MODRET set_sftpacceptenv(cmd_rec *cmd) {
362 register unsigned int i;
363 config_rec *c;
364 array_header *accepted_envs;
365
366 if (cmd->argc < 2) {
367 CONF_ERROR(cmd, "wrong number of parameters");
368 }
369
370 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
371
372 c = add_config_param(cmd->argv[0], 1, NULL);
373 accepted_envs = make_array(c->pool, 0, sizeof(char *));
374
375 for (i = 1; i < cmd->argc; i++) {
376 *((char **) push_array(accepted_envs)) = pstrdup(c->pool, cmd->argv[i]);
377 }
378 c->argv[0] = (void *) accepted_envs;
379
380 if (pr_module_exists("mod_ifsession.c")) {
381 /* These are needed in case this directive is used with mod_ifsession
382 * configuration.
383 */
384 c->flags |= CF_MULTI;
385 }
386
387 return PR_HANDLED(cmd);
388 }
389
390 /* usage: SFTPAuthMethods method-list1 ... method-listN */
391 MODRET set_sftpauthmeths(cmd_rec *cmd) {
392 register unsigned int i;
393 config_rec *c;
394 array_header *auth_chains;
395
396 if (cmd->argc < 2) {
397 CONF_ERROR(cmd, "Wrong number of parameters");
398 }
399
400 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
401
402 c = add_config_param(cmd->argv[0], 1, NULL);
403 auth_chains = make_array(c->pool, 0, sizeof(struct sftp_auth_chain *));
404
405 for (i = 1; i < cmd->argc; i++) {
406 array_header *method_names;
407 register unsigned int j;
408 struct sftp_auth_chain *auth_chain;
409
410 method_names = sftp_auth_chain_parse_method_chain(cmd->tmp_pool,
411 cmd->argv[i]);
412 if (method_names == NULL) {
413 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
414 "invalid authentication parameter: ", (char *) cmd->argv[i], NULL));
415 }
416
417 auth_chain = sftp_auth_chain_alloc(c->pool);
418 for (j = 0; j < method_names->nelts; j++) {
419 int res;
420 char *name;
421 unsigned int method_id = 0;
422 const char *method_name = NULL, *submethod_name = NULL;
423
424 name = ((char **) method_names->elts)[j];
425
426 res = sftp_auth_chain_parse_method(c->pool, name, &method_id,
427 &method_name, &submethod_name);
428 if (res < 0) {
429 /* Make for a slightly better/more informative error message. */
430 if (method_id == SFTP_AUTH_FL_METH_KBDINT) {
431 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
432 "unsupported authentication method '", name,
433 "': No drivers loaded", NULL));
434
435 } else {
436 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
437 "unsupported authentication method '", name, "': ",
438 strerror(errno), NULL));
439 }
440 }
441
442 sftp_auth_chain_add_method(auth_chain, method_id, method_name,
443 submethod_name);
444 }
445
446 if (sftp_auth_chain_isvalid(auth_chain) < 0) {
447 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
448 "unsupportable chain of authentication methods '",
449 (char *) cmd->argv[i], "': ", strerror(errno), NULL));
450 }
451
452 *((struct sftp_auth_chain **) push_array(auth_chains)) = auth_chain;
453 }
454
455 c->argv[0] = auth_chains;
456 return PR_HANDLED(cmd);
457 }
458
459 /* usage: SFTPAuthPublicKeys list */
460 MODRET set_sftpauthpublickeys(cmd_rec *cmd) {
461 register unsigned int i;
462 config_rec *c;
463
464 if (cmd->argc < 2) {
465 CONF_ERROR(cmd, "Wrong number of parameters");
466 }
467
468 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
469
470 for (i = 1; i < cmd->argc; i++) {
471 if (sftp_auth_publickey_isvalid(cmd->argv[i], NULL) < 0) {
472 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
473 "unsupported auth public key algorithm: ", (char *) cmd->argv[i],
474 NULL));
475 }
476 }
477
478 c = add_config_param(cmd->argv[0], cmd->argc-1, NULL);
479 for (i = 1; i < cmd->argc; i++) {
480 c->argv[i-1] = pstrdup(c->pool, cmd->argv[i]);
481 }
482
483 return PR_HANDLED(cmd);
484 }
485
486 /* usage: SFTPAuthorized{Host,User}Keys store1 ... */
487 MODRET set_sftpauthorizedkeys(cmd_rec *cmd) {
488 register unsigned int i;
489 int requested_key_type = 0;
490 config_rec *c;
491
492 if (cmd->argc < 2) {
493 CONF_ERROR(cmd, "wrong number of parameters");
494 }
495
496 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
497
498 if (strncasecmp(cmd->argv[0], "SFTPAuthorizedHostKeys", 23) == 0) {
499 requested_key_type = SFTP_SSH2_HOST_KEY_STORE;
500
501 } else if (strncasecmp(cmd->argv[0], "SFTPAuthorizedUserKeys", 23) == 0) {
502 requested_key_type = SFTP_SSH2_USER_KEY_STORE;
503 }
504
505 for (i = 1; i < cmd->argc; i++) {
506 char *ptr;
507
508 /* Separate the parameter into its separate store-type:store-info pieces. */
509 ptr = strchr(cmd->argv[i], ':');
510 if (ptr == NULL) {
511 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "badly formatted parameter: '",
512 (char *) cmd->argv[i], "'", NULL));
513 }
514 *ptr = '\0';
515
516 /* Verify that the requested store type has been registered, and supports
517 * the type of keystore requested (host or user key).
518 */
519 if (sftp_keystore_supports_store(cmd->argv[i], requested_key_type) < 0) {
520 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unsupported key store: '",
521 (char *) cmd->argv[i], "'", NULL));
522 }
523
524 *ptr = ':';
525 }
526
527 c = add_config_param(cmd->argv[0], 0);
528 c->argc = cmd->argc-1;
529 c->argv = pcalloc(c->pool, c->argc * (sizeof(char *)));
530 for (i = 1; i < cmd->argc; i++) {
531 c->argv[i-1] = pstrdup(c->pool, cmd->argv[i]);
532 }
533
534 return PR_HANDLED(cmd);
535 }
536
537 /* usage: SFTPCiphers list */
538 MODRET set_sftpciphers(cmd_rec *cmd) {
539 register unsigned int i;
540 config_rec *c;
541
542 if (cmd->argc < 2) {
543 CONF_ERROR(cmd, "Wrong number of parameters");
544 }
545
546 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
547
548 for (i = 1; i < cmd->argc; i++) {
549 if (sftp_crypto_get_cipher(cmd->argv[i], NULL, NULL, NULL) == NULL) {
550 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
551 "unsupported cipher algorithm: ", (char *) cmd->argv[i], NULL));
552 }
553 }
554
555 c = add_config_param(cmd->argv[0], cmd->argc-1, NULL);
556 for (i = 1; i < cmd->argc; i++) {
557 c->argv[i-1] = pstrdup(c->pool, cmd->argv[i]);
558 }
559
560 return PR_HANDLED(cmd);
561 }
562
563 /* usage: SFTPClientAlive count interval */
564 MODRET set_sftpclientalive(cmd_rec *cmd) {
565 int count, interval;
566 config_rec *c;
567
568 CHECK_ARGS(cmd, 2);
569 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
570
571 count = atoi(cmd->argv[1]);
572 if (count < 0) {
573 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "max count '",
574 (char *) cmd->argv[1], "' must be equal to or greater than zero", NULL));
575 }
576
577 interval = atoi(cmd->argv[2]);
578 if (interval < 0) {
579 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "interval '", (char *) cmd->argv[2],
580 "' must be equal to or greater than zero", NULL));
581 }
582
583 c = add_config_param(cmd->argv[0], 2, NULL, NULL);
584 c->argv[0] = palloc(c->pool, sizeof(unsigned int));
585 *((unsigned int *) c->argv[0]) = count;
586 c->argv[1] = palloc(c->pool, sizeof(unsigned int));
587 *((unsigned int *) c->argv[1]) = interval;
588
589 return PR_HANDLED(cmd);
590 }
591
592 #if defined(PR_USE_REGEX)
593 /* Ensure that the PessimisticKexinit SFTPOption is in effect. If
594 * there is an existing SFTPOption config, then add the flag to that,
595 * otherwise, add a new config.
596 */
597 static void set_pessimistic_kexinit_opt(server_rec *s) {
598 config_rec *c;
599
600 c = find_config(s->conf, CONF_PARAM, "SFTPOptions", FALSE);
601 if (c != NULL) {
602 unsigned long opts;
603
604 opts = *((unsigned long *) c->argv[0]);
605 opts |= SFTP_OPT_PESSIMISTIC_KEXINIT;
606 *((unsigned long *) c->argv[0]) = opts;
607
608 } else {
609 c = add_config_param("SFTPOptions", 1, NULL);
610 c->argv[0] = pcalloc(c->pool, sizeof(unsigned long));
611 *((unsigned long *) c->argv[0]) = SFTP_OPT_PESSIMISTIC_KEXINIT;
612 }
613 }
614 #endif /* PR_USE_REGEX */
615
616 /* usage: SFTPClientMatch pattern key1 val1 ... */
617 MODRET set_sftpclientmatch(cmd_rec *cmd) {
618 #if defined(PR_USE_REGEX)
619 register unsigned int i;
620 config_rec *c;
621 pr_table_t *tab;
622 pr_regex_t *pre;
623 int res;
624
625 if (cmd->argc < 4) {
626 CONF_ERROR(cmd, "Wrong number of parameters");
627
628 } else {
629 int npairs;
630
631 /* Make sure we have an even number of args for the key/value pairs. */
632
633 npairs = cmd->argc - 2;
634 if (npairs % 2 != 0) {
635 CONF_ERROR(cmd, "Wrong number of parameters");
636 }
637 }
638
639 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
640
641 pre = pr_regexp_alloc(&sftp_module);
642
643 res = pr_regexp_compile(pre, cmd->argv[1], REG_EXTENDED|REG_NOSUB);
644 if (res != 0) {
645 char errstr[200];
646
647 memset(errstr, '\0', sizeof(errstr));
648 pr_regexp_error(res, pre, errstr, sizeof(errstr));
649 pr_regexp_free(NULL, pre);
650
651 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "'", (char *) cmd->argv[1],
652 "' failed regex compilation: ", errstr, NULL));
653 }
654
655 c = add_config_param(cmd->argv[0], 3, NULL, NULL, NULL);
656 c->argv[0] = pstrdup(c->pool, cmd->argv[1]);
657 c->argv[1] = pre;
658
659 tab = pr_table_alloc(c->pool, 0);
660
661 c->argv[2] = tab;
662
663 for (i = 2; i < cmd->argc; i++) {
664 if (strcmp(cmd->argv[i], "channelWindowSize") == 0) {
665 off_t window_size;
666 void *value;
667 char *arg, units[3];
668 size_t arglen;
669
670 arg = pstrdup(cmd->tmp_pool, cmd->argv[i+1]);
671 arglen = strlen(arg);
672
673 memset(units, '\0', sizeof(units));
674
675 if (arglen >= 3) {
676 /* Look for any possible "GB", "MB", "KB", "B" suffixes. */
677
678 if ((arg[arglen-2] == 'G' || arg[arglen-2] == 'g') &&
679 (arg[arglen-1] == 'B' || arg[arglen-1] == 'b')) {
680 units[0] = 'G';
681 units[1] = 'B';
682 arg[arglen-2] = '\0';
683 arg[arglen-1] = '\0';
684 arglen -= 2;
685
686 } else if ((arg[arglen-2] == 'M' || arg[arglen-2] == 'm') &&
687 (arg[arglen-1] == 'B' || arg[arglen-1] == 'b')) {
688 units[0] = 'M';
689 units[1] = 'B';
690 arg[arglen-2] = '\0';
691 arg[arglen-1] = '\0';
692 arglen -= 2;
693
694 } else if ((arg[arglen-2] == 'K' || arg[arglen-2] == 'k') &&
695 (arg[arglen-1] == 'B' || arg[arglen-1] == 'b')) {
696 units[0] = 'K';
697 units[1] = 'B';
698 arg[arglen-2] = '\0';
699 arg[arglen-1] = '\0';
700 arglen -= 2;
701
702 } else if (arg[arglen-1] == 'B' || arg[arglen-1] == 'b') {
703 units[0] = 'B';
704 arg[arglen-1] = '\0';
705 arglen--;
706 }
707
708 } else if (arglen >= 2) {
709 /* Look for any possible "B" suffix. */
710 if (arg[arglen-1] == 'B' || arg[arglen-1] == 'b') {
711 units[0] = 'B';
712 arg[arglen-1] = '\0';
713 arglen--;
714 }
715 }
716
717 if (pr_str_get_nbytes(arg, units, &window_size) < 0) {
718 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
719 "error parsing 'channelWindowSize' value ", cmd->argv[i+1], ": ",
720 strerror(errno), NULL));
721 }
722
723 value = palloc(c->pool, sizeof(uint32_t));
724 *((uint32_t *) value) = (uint32_t) window_size;
725
726 if (pr_table_add(tab, pstrdup(c->pool, "channelWindowSize"), value,
727 sizeof(uint32_t)) < 0) {
728 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
729 "error storing 'channelWindowSize' value: ", strerror(errno), NULL));
730 }
731
732 /* Don't forget to advance i past the value. */
733 i++;
734
735 } else if (strcmp(cmd->argv[i], "channelPacketSize") == 0) {
736 off_t packet_size;
737 void *value;
738 char *arg, units[3];
739 size_t arglen;
740
741 arg = pstrdup(cmd->tmp_pool, cmd->argv[i+1]);
742 arglen = strlen(arg);
743
744 memset(units, '\0', sizeof(units));
745
746 if (arglen >= 3) {
747 /* Look for any possible "GB", "MB", "KB", "B" suffixes. */
748
749 if ((arg[arglen-2] == 'G' || arg[arglen-2] == 'g') &&
750 (arg[arglen-1] == 'B' || arg[arglen-1] == 'b')) {
751 units[0] = 'G';
752 units[1] = 'B';
753 arg[arglen-2] = '\0';
754 arg[arglen-1] = '\0';
755 arglen -= 2;
756
757 } else if ((arg[arglen-2] == 'M' || arg[arglen-2] == 'm') &&
758 (arg[arglen-1] == 'B' || arg[arglen-1] == 'b')) {
759 units[0] = 'M';
760 units[1] = 'B';
761 arg[arglen-2] = '\0';
762 arg[arglen-1] = '\0';
763 arglen -= 2;
764
765 } else if ((arg[arglen-2] == 'K' || arg[arglen-2] == 'k') &&
766 (arg[arglen-1] == 'B' || arg[arglen-1] == 'b')) {
767 units[0] = 'K';
768 units[1] = 'B';
769 arg[arglen-2] = '\0';
770 arg[arglen-1] = '\0';
771 arglen -= 2;
772
773 } else if (arg[arglen-1] == 'B' || arg[arglen-1] == 'b') {
774 units[0] = 'B';
775 arg[arglen-1] = '\0';
776 arglen--;
777 }
778
779 } else if (arglen >= 2) {
780 /* Look for any possible "B" suffix. */
781 if (arg[arglen-1] == 'B' || arg[arglen-1] == 'b') {
782 units[0] = 'B';
783 arg[arglen-1] = '\0';
784 arglen--;
785 }
786 }
787
788 if (pr_str_get_nbytes(arg, units, &packet_size) < 0) {
789 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
790 "error parsing 'channelPacketSize' value ", cmd->argv[i+1], ": ",
791 strerror(errno), NULL));
792 }
793
794 if (packet_size > SFTP_MAX_PACKET_LEN) {
795 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
796 "'channelPacketSize' value ", cmd->argv[i+1], " too large, must be "
797 "less than 35000B", NULL));
798 }
799
800 value = palloc(c->pool, sizeof(uint32_t));
801 *((uint32_t *) value) = (uint32_t) packet_size;
802
803 if (pr_table_add(tab, pstrdup(c->pool, "channelPacketSize"), value,
804 sizeof(uint32_t)) < 0) {
805 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
806 "error storing 'channelPacketSize' value: ", strerror(errno), NULL));
807 }
808
809 /* Don't forget to advance i past the value. */
810 i++;
811
812 } else if (strcmp(cmd->argv[i], "pessimisticNewkeys") == 0) {
813 int pessimistic_newkeys;
814 void *value;
815
816 pessimistic_newkeys = get_boolean(cmd, i+1);
817 if (pessimistic_newkeys == -1) {
818 CONF_ERROR(cmd, "expected Boolean parameter");
819 }
820
821 value = palloc(c->pool, sizeof(int));
822 *((int *) value) = pessimistic_newkeys;
823
824 if (pr_table_add(tab, pstrdup(c->pool, "pessimisticNewkeys"), value,
825 sizeof(int)) < 0) {
826 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
827 "error storing 'pessimisticNewkeys' value: ", strerror(errno), NULL));
828 }
829
830 /* Don't forget to advance i past the value. */
831 i++;
832
833 } else if (strcmp(cmd->argv[i], "sftpCiphers") == 0) {
834 register unsigned int j;
835 array_header *algos = NULL;
836 config_rec *ciphers = NULL;
837 void *value = NULL;
838
839 algos = pr_str_text_to_array(cmd->tmp_pool, cmd->argv[i + 1], ',');
840 if (algos == NULL) {
841 modret_t *mr;
842
843 mr = mod_create_ret(cmd, TRUE, NULL, pstrcat(cmd->tmp_pool,
844 (char *) cmd->argv[0], ": error parsing cipher list '",
845 (char *) cmd->argv[i+1], ": ", strerror(errno), NULL));
846 return mr;
847 }
848
849 ciphers = create_config(c->pool, "SFTPCiphers", algos->nelts);
850 for (j = 0; j < algos->nelts; j++) {
851 const char *algo;
852
853 algo = ((char **) algos->elts)[j];
854 if (sftp_crypto_get_cipher(algo, NULL, NULL, NULL) == NULL) {
855 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
856 "unsupported cipher algorithm: ", algo, NULL));
857 }
858
859 ciphers->argv[j] = pstrdup(ciphers->pool, algo);
860 }
861
862 value = palloc(c->pool, sizeof(config_rec *));
863 *((config_rec **) value) = ciphers;
864
865 if (pr_table_add(tab, pstrdup(c->pool, "sftpCiphers"), value,
866 sizeof(config_rec *)) < 0) {
867 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
868 "error storing 'sftpCiphers' value: ", strerror(errno), NULL));
869 }
870
871 /* NOTE: In order for these banner-specific ciphers to take effect,
872 * we need to wait until we've seen the client banner, which means
873 * automatically enabling the PessimisticKexinit SFTPOption.
874 */
875 set_pessimistic_kexinit_opt(cmd->server);
876
877 /* Don't forget to advance i past the key/values */
878 i++;
879
880 } else if (strcmp(cmd->argv[i], "sftpDigests") == 0) {
881 register unsigned int j;
882 array_header *algos = NULL;
883 config_rec *digests = NULL;
884 void *value = NULL;
885
886 algos = pr_str_text_to_array(cmd->tmp_pool, cmd->argv[i + 1], ',');
887 if (algos == NULL) {
888 modret_t *mr;
889
890 mr = mod_create_ret(cmd, TRUE, NULL, pstrcat(cmd->tmp_pool,
891 (char *) cmd->argv[0], ": error parsing digest list '",
892 (char *) cmd->argv[i+1], ": ", strerror(errno), NULL));
893 return mr;
894 }
895
896 digests = create_config(c->pool, "SFTPDigests", algos->nelts);
897 for (j = 0; j < algos->nelts; j++) {
898 const EVP_MD *md;
899 const char *algo;
900 int free_md = FALSE;
901
902 algo = ((char **) algos->elts)[j];
903 md = sftp_crypto_get_digest(algo, NULL, &free_md);
904 if (md == NULL) {
905 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
906 "unsupported digest algorithm: ", algo, NULL));
907 }
908
909 if (free_md == TRUE) {
910 sftp_crypto_free_digest(md);
911 }
912
913 digests->argv[j] = pstrdup(digests->pool, algo);
914 }
915
916 value = palloc(c->pool, sizeof(config_rec *));
917 *((config_rec **) value) = digests;
918
919 if (pr_table_add(tab, pstrdup(c->pool, "sftpDigests"), value,
920 sizeof(config_rec *)) < 0) {
921 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
922 "error storing 'sftpDigests' value: ", strerror(errno), NULL));
923 }
924
925 /* NOTE: In order for these banner-specific digests to take effect,
926 * we need to wait until we've seen the client banner, which means
927 * automatically enabling the PessimisticKexinit SFTPOption.
928 */
929 set_pessimistic_kexinit_opt(cmd->server);
930
931 /* Don't forget to advance i past the key/values */
932 i++;
933
934 } else if (strcmp(cmd->argv[i], "sftpHostKeys") == 0) {
935 register unsigned int j;
936 array_header *algos = NULL;
937 config_rec *hostkeys = NULL;
938 void *value = NULL;
939
940 algos = pr_str_text_to_array(cmd->tmp_pool, cmd->argv[i + 1], ',');
941 if (algos == NULL) {
942 modret_t *mr;
943
944 mr = mod_create_ret(cmd, TRUE, NULL, pstrcat(cmd->tmp_pool,
945 (char *) cmd->argv[0], ": error parsing host key list '",
946 (char *) cmd->argv[i+1], ": ", strerror(errno), NULL));
947 return mr;
948 }
949
950 hostkeys = create_config(c->pool, "SFTPHostKeys", algos->nelts);
951 for (j = 0; j < algos->nelts; j++) {
952 const char *algo;
953
954 algo = ((char **) algos->elts)[j];
955 if (sftp_crypto_is_hostkey(algo) < 0) {
956 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
957 "unsupported host key algorithm: ", algo, NULL));
958 }
959
960 hostkeys->argv[j] = pstrdup(hostkeys->pool, algo);
961 }
962
963 value = palloc(c->pool, sizeof(config_rec *));
964 *((config_rec **) value) = hostkeys;
965
966 if (pr_table_add(tab, pstrdup(c->pool, "sftpHostKeys"), value,
967 sizeof(config_rec *)) < 0) {
968 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
969 "error storing 'sftpHostKeys' value: ", strerror(errno), NULL));
970 }
971
972 /* NOTE: In order for these banner-specific ciphers to take effect,
973 * we need to wait until we've seen the client banner, which means
974 * automatically enabling the PessimisticKexinit SFTPOption.
975 */
976 set_pessimistic_kexinit_opt(cmd->server);
977
978 /* Don't forget to advance i past the key/values */
979 i++;
980
981 } else if (strcmp(cmd->argv[i], "sftpKeyExchanges") == 0) {
982 register unsigned int j;
983 array_header *algos = NULL;
984 config_rec *key_exchanges = NULL;
985 void *value = NULL;
986
987 algos = pr_str_text_to_array(cmd->tmp_pool, cmd->argv[i + 1], ',');
988 if (algos == NULL) {
989 modret_t *mr;
990
991 mr = mod_create_ret(cmd, TRUE, NULL, pstrcat(cmd->tmp_pool,
992 (char *) cmd->argv[0], ": error parsing key exchange list '",
993 (char *) cmd->argv[i+1], ": ", strerror(errno), NULL));
994 return mr;
995 }
996
997 key_exchanges = create_config(c->pool, "SFTPKeyExchanges", 1);
998 for (j = 0; j < algos->nelts; j++) {
999 const char *algo;
1000
1001 algo = ((char **) algos->elts)[j];
1002 if (sftp_crypto_is_key_exchange(algo) < 0) {
1003 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1004 "unsupported key exyhange algorithm: ", algo, NULL));
1005 }
1006 }
1007
1008 key_exchanges->argv[0] = pstrdup(key_exchanges->pool, cmd->argv[i+1]);
1009
1010 value = palloc(c->pool, sizeof(config_rec *));
1011 *((config_rec **) value) = key_exchanges;
1012
1013 if (pr_table_add(tab, pstrdup(c->pool, "sftpKeyExchanges"), value,
1014 sizeof(config_rec *)) < 0) {
1015 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1016 "error storing 'sftpKeyExchanges' value: ", strerror(errno), NULL));
1017 }
1018
1019 /* NOTE: In order for these banner-specific key exchanges to take effect,
1020 * we need to wait until we've seen the client banner, which means
1021 * automatically enabling the PessimisticKexinit SFTPOption.
1022 */
1023 set_pessimistic_kexinit_opt(cmd->server);
1024
1025 /* Don't forget to advance i past the key/values */
1026 i++;
1027
1028 } else if (strcmp(cmd->argv[i], "sftpProtocolVersion") == 0) {
1029 void *min_value, *max_value;
1030 char *ptr = NULL;
1031
1032 /* Check for a range of values. */
1033 ptr = strchr(cmd->argv[i+1], '-');
1034
1035 if (ptr == NULL) {
1036 long protocol_version;
1037
1038 /* Just a single value. */
1039
1040 protocol_version = strtol(cmd->argv[i+1], &ptr, 10);
1041 if (ptr && *ptr) {
1042 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1043 "badly formatted 'sftpProtocolVersion' value: ", cmd->argv[i+1],
1044 NULL));
1045 }
1046
1047 if (protocol_version < 1 ||
1048 protocol_version > 6) {
1049 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1050 "'sftpProtocolVersion' value ", cmd->argv[i+1],
1051 " must be between 1 and 6: ", strerror(errno), NULL));
1052 }
1053
1054 #if !defined(PR_USE_NLS)
1055 /* If NLS supported was enabled in the proftpd build, then we can
1056 * support UTF8, and thus every other version of SFTP. Otherwise, we
1057 * can only support up to version 3.
1058 */
1059 if (protocol_version > 3) {
1060 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1061 "'sftpProtocolVersion' value ", cmd->argv[i+1],
1062 " cannot be higher than 3, due to lack of UTF8 support "
1063 "(requires --enable-nls)", NULL));
1064 }
1065 #endif /* PR_USE_NLS */
1066
1067 min_value = palloc(c->pool, sizeof(unsigned int));
1068 *((unsigned int *) min_value) = (unsigned int) protocol_version;
1069
1070 max_value = palloc(c->pool, sizeof(unsigned int));
1071 *((unsigned int *) max_value) = (unsigned int) protocol_version;
1072
1073 } else {
1074 long min_version, max_version;
1075 char *tmp = NULL;
1076
1077 /* We have a range of values. */
1078
1079 *ptr = '\0';
1080 min_version = strtol(cmd->argv[i+1], &tmp, 10);
1081
1082 if (tmp && *tmp) {
1083 *ptr = '-';
1084 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1085 "badly formatted 'sftpProtocolVersion' value: ", cmd->argv[i+1],
1086 NULL));
1087 }
1088 *ptr = '-';
1089
1090 if (min_version < 1 ||
1091 min_version > 6) {
1092 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1093 "'sftpProtocolVersion' value ", cmd->argv[i+1],
1094 " must be between 1 and 6: ", strerror(errno), NULL));
1095 }
1096
1097 #if !defined(PR_USE_NLS)
1098 /* If NLS supported was enabled in the proftpd build, then we can
1099 * support UTF8, and thus every other version of SFTP. Otherwise, we
1100 * can only support up to version 3.
1101 */
1102 if (min_version > 3) {
1103 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1104 "'sftpProtocolVersion' value ", cmd->argv[i+1],
1105 " cannot be higher than 3, due to lack of UTF8 support "
1106 "(requires --enable-nls)", NULL));
1107 }
1108 #endif /* PR_USE_NLS */
1109
1110 min_value = palloc(c->pool, sizeof(unsigned int));
1111 *((unsigned int *) min_value) = (unsigned int) min_version;
1112
1113 *ptr = '\0';
1114 tmp = NULL;
1115 max_version = strtol(ptr + 1, &tmp, 10);
1116
1117 if (tmp && *tmp) {
1118 *ptr = '-';
1119 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1120 "badly formatted 'sftpProtocolVersion' value: ", cmd->argv[i+1],
1121 NULL));
1122 }
1123 *ptr = '-';
1124
1125 if (max_version < 1 ||
1126 max_version > 6) {
1127 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1128 "'sftpProtocolVersion' value ", cmd->argv[i+1],
1129 " must be between 1 and 6: ", strerror(errno), NULL));
1130 }
1131
1132 #if !defined(PR_USE_NLS)
1133 /* If NLS supported was enabled in the proftpd build, then we can
1134 * support UTF8, and thus every other version of SFTP. Otherwise, we
1135 * can only support up to version 3.
1136 */
1137 if (max_version > 3) {
1138 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1139 "'sftpProtocolVersion' value ", cmd->argv[i+1],
1140 " cannot be higher than 3, due to lack of UTF8 support "
1141 "(requires --enable-nls)", NULL));
1142 }
1143 #endif /* PR_USE_NLS */
1144
1145 max_value = palloc(c->pool, sizeof(unsigned int));
1146 *((unsigned int *) max_value) = (unsigned int) max_version;
1147 }
1148
1149 if (pr_table_add(tab, pstrdup(c->pool, "sftpMinProtocolVersion"),
1150 min_value, sizeof(unsigned int)) < 0) {
1151 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1152 "error storing 'sftpProtocolVersion' value: ", strerror(errno),
1153 NULL));
1154 }
1155
1156 if (pr_table_add(tab, pstrdup(c->pool, "sftpMaxProtocolVersion"),
1157 max_value, sizeof(unsigned int)) < 0) {
1158 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1159 "error storing 'sftpProtocolVersion' value: ", strerror(errno),
1160 NULL));
1161 }
1162
1163 /* Don't forget to advance i past the value. */
1164 i++;
1165
1166 } else if (strcmp(cmd->argv[i], "sftpUTF8ProtocolVersion") == 0) {
1167 #if defined(PR_USE_NLS)
1168 char *ptr = NULL;
1169 void *value;
1170 long protocol_version;
1171
1172 protocol_version = strtol(cmd->argv[i+1], &ptr, 10);
1173 if (ptr && *ptr) {
1174 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1175 "badly formatted 'sftpUTF8ProtocolVersion' value: ", cmd->argv[i+1],
1176 NULL));
1177 }
1178
1179 if (protocol_version < 1 ||
1180 protocol_version > 6) {
1181 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1182 "'sftpUTF8ProtocolVersion' value ", cmd->argv[i+1], NULL));
1183 }
1184
1185 value = palloc(c->pool, sizeof(unsigned int));
1186 *((unsigned int *) value) = (unsigned int) protocol_version;
1187
1188 if (pr_table_add(tab, pstrdup(c->pool, "sftpUTF8ProtocolVersion"),
1189 value, sizeof(unsigned int)) < 0) {
1190 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1191 "error storing 'sftpUTF8ProtocolVersion' value: ", strerror(errno),
1192 NULL));
1193 }
1194
1195 /* Don't forget to advance i past the value. */
1196 i++;
1197 #else
1198 CONF_ERROR(cmd, "'sftpUTF8ProtocolVersion' requires NLS support (--enable-nls)");
1199 #endif /* PR_USE_NLS */
1200 } else {
1201 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unknown SFTPClientMatch key: '",
1202 cmd->argv[i], "'", NULL));
1203 }
1204 }
1205
1206 if (pr_module_exists("mod_ifsession.c")) {
1207 /* These are needed in case this directive is used with mod_ifsession
1208 * configuration.
1209 */
1210 c->flags |= CF_MULTI;
1211 }
1212
1213 return PR_HANDLED(cmd);
1214
1215 #else /* no regular expression support at the moment */
1216 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "The ", cmd->argv[0],
1217 " directive cannot be used on this system, as you do not have POSIX "
1218 "compliant regex support", NULL));
1219 #endif
1220 }
1221
1222 /* usage: SFTPCompression on|off|delayed */
1223 MODRET set_sftpcompression(cmd_rec *cmd) {
1224 config_rec *c;
1225 int use_compression = FALSE;
1226
1227 if (cmd->argc != 2) {
1228 CONF_ERROR(cmd, "Wrong number of parameters");
1229 }
1230
1231 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1232
1233 #if defined(HAVE_ZLIB_H)
1234 use_compression = get_boolean(cmd, 1);
1235 if (use_compression == -1) {
1236 if (strcasecmp(cmd->argv[1], "delayed") != 0) {
1237 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1238 "unknown compression setting: ", cmd->argv[1], NULL));
1239 }
1240
1241 use_compression = 2;
1242 }
1243 #else
1244 pr_log_debug(DEBUG0, MOD_SFTP_VERSION ": platform lacks zlib support, ignoring SFTPCompression");
1245 use_compression = 0;
1246 #endif /* !HAVE_ZLIB_H */
1247
1248 c = add_config_param(cmd->argv[0], 1, NULL);
1249 c->argv[0] = pcalloc(c->pool, sizeof(int));
1250 *((int *) c->argv[0]) = use_compression;
1251
1252 return PR_HANDLED(cmd);
1253 }
1254
1255 /* usage: SFTPCryptoDevice engine|"ALL" */
1256 MODRET set_sftpcryptodevice(cmd_rec *cmd) {
1257 #if OPENSSL_VERSION_NUMBER > 0x000907000L
1258 CHECK_ARGS(cmd, 1);
1259 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1260
1261 (void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
1262 return PR_HANDLED(cmd);
1263
1264 #else /* OpenSSL is too old for ENGINE support */
1265 CONF_ERROR(cmd, "unsupportable (OpenSSL version is too old");
1266 #endif
1267 }
1268
1269 /* usage: SFTPDHParamFile path */
1270 MODRET set_sftpdhparamfile(cmd_rec *cmd) {
1271 CHECK_ARGS(cmd, 1);
1272 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1273
1274 if (pr_fs_valid_path(cmd->argv[1]) < 0) {
1275 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1276 "unable to use '", cmd->argv[1], "'", NULL));
1277 }
1278
1279 (void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
1280 return PR_HANDLED(cmd);
1281 }
1282
1283 /* usage: SFTPDigests list */
1284 MODRET set_sftpdigests(cmd_rec *cmd) {
1285 register unsigned int i;
1286 config_rec *c;
1287
1288 if (cmd->argc < 2) {
1289 CONF_ERROR(cmd, "Wrong number of parameters");
1290 }
1291
1292 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1293
1294 for (i = 1; i < cmd->argc; i++) {
1295 const EVP_MD *md;
1296 int free_md = FALSE;
1297
1298 md = sftp_crypto_get_digest(cmd->argv[i], NULL, &free_md);
1299 if (md == NULL) {
1300 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1301 "unsupported digest algorithm: ", cmd->argv[i], NULL));
1302 }
1303
1304 if (free_md == TRUE) {
1305 sftp_crypto_free_digest(md);
1306 }
1307 }
1308
1309 c = add_config_param(cmd->argv[0], cmd->argc-1, NULL);
1310 for (i = 1; i < cmd->argc; i++) {
1311 c->argv[i-1] = pstrdup(c->pool, cmd->argv[i]);
1312 }
1313
1314 return PR_HANDLED(cmd);
1315 }
1316
1317 /* usage: SFTPDisplayBanner path */
1318 MODRET set_sftpdisplaybanner(cmd_rec *cmd) {
1319 CHECK_ARGS(cmd, 1);
1320 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1321
1322 add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
1323 return PR_HANDLED(cmd);
1324 }
1325
1326 /* usage: SFTPEngine on|off */
1327 MODRET set_sftpengine(cmd_rec *cmd) {
1328 int engine = 1;
1329 config_rec *c;
1330
1331 CHECK_ARGS(cmd, 1);
1332 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1333
1334 engine = get_boolean(cmd, 1);
1335 if (engine == -1) {
1336 CONF_ERROR(cmd, "expected Boolean parameter");
1337 }
1338
1339 c = add_config_param(cmd->argv[0], 1, NULL);
1340 c->argv[0] = pcalloc(c->pool, sizeof(int));
1341 *((int *) c->argv[0]) = engine;
1342
1343 return PR_HANDLED(cmd);
1344 }
1345
1346 /* usage: SFTPExtensions ext1 ... extN */
1347 MODRET set_sftpextensions(cmd_rec *cmd) {
1348 register unsigned int i;
1349 config_rec *c;
1350 unsigned long ext_flags = SFTP_FXP_EXT_DEFAULT;
1351
1352 if (cmd->argc < 2) {
1353 CONF_ERROR(cmd, "wrong number of parameters");
1354 }
1355
1356 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1357
1358 for (i = 1; i < cmd->argc; i++) {
1359 char action, *ext;
1360
1361 ext = cmd->argv[i];
1362 action = *ext;
1363
1364 if (action != '-' &&
1365 action != '+') {
1366 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "bad option: '", ext, "'",
1367 NULL));
1368 }
1369
1370 ext++;
1371
1372 if (strcasecmp(ext, "checkFile") == 0) {
1373 switch (action) {
1374 case '-':
1375 ext_flags &= ~SFTP_FXP_EXT_CHECK_FILE;
1376 break;
1377
1378 case '+':
1379 ext_flags |= SFTP_FXP_EXT_CHECK_FILE;
1380 break;
1381 }
1382
1383 } else if (strcasecmp(ext, "copyFile") == 0) {
1384 switch (action) {
1385 case '-':
1386 ext_flags &= ~SFTP_FXP_EXT_COPY_FILE;
1387 break;
1388
1389 case '+':
1390 ext_flags |= SFTP_FXP_EXT_COPY_FILE;
1391 break;
1392 }
1393
1394 } else if (strcasecmp(ext, "fsync") == 0) {
1395 switch (action) {
1396 case '-':
1397 ext_flags &= ~SFTP_FXP_EXT_FSYNC;
1398 break;
1399
1400 case '+':
1401 ext_flags |= SFTP_FXP_EXT_FSYNC;
1402 break;
1403 }
1404
1405 } else if (strcasecmp(ext, "vendorID") == 0) {
1406 switch (action) {
1407 case '-':
1408 ext_flags &= ~SFTP_FXP_EXT_VENDOR_ID;
1409 break;
1410
1411 case '+':
1412 ext_flags |= SFTP_FXP_EXT_VENDOR_ID;
1413 break;
1414 }
1415
1416 } else if (strcasecmp(ext, "versionSelect") == 0) {
1417 switch (action) {
1418 case '-':
1419 ext_flags &= ~SFTP_FXP_EXT_VERSION_SELECT;
1420 break;
1421
1422 case '+':
1423 ext_flags |= SFTP_FXP_EXT_VERSION_SELECT;
1424 break;
1425 }
1426
1427 } else if (strcasecmp(ext, "posixRename") == 0) {
1428 switch (action) {
1429 case '-':
1430 ext_flags &= ~SFTP_FXP_EXT_POSIX_RENAME;
1431 break;
1432
1433 case '+':
1434 ext_flags |= SFTP_FXP_EXT_POSIX_RENAME;
1435 break;
1436 }
1437
1438 } else if (strcasecmp(ext, "spaceAvailable") == 0) {
1439 #if defined(HAVE_SYS_STATVFS_H)
1440 switch (action) {
1441 case '-':
1442 ext_flags &= ~SFTP_FXP_EXT_SPACE_AVAIL;
1443 break;
1444
1445 case '+':
1446 ext_flags |= SFTP_FXP_EXT_SPACE_AVAIL;
1447 break;
1448 }
1449 #else
1450 pr_log_debug(DEBUG0, "%s: spaceAvailable extension not supported "
1451 "on this system; requires statvfs(3) support", cmd->argv[0]);
1452 #endif /* !HAVE_SYS_STATVFS_H */
1453
1454 } else if (strcasecmp(ext, "statvfs") == 0) {
1455 #if defined(HAVE_SYS_STATVFS_H)
1456 switch (action) {
1457 case '-':
1458 ext_flags &= ~SFTP_FXP_EXT_STATVFS;
1459 break;
1460
1461 case '+':
1462 ext_flags |= SFTP_FXP_EXT_STATVFS;
1463 break;
1464 }
1465 #else
1466 pr_log_debug(DEBUG0, "%s: [email protected] extension not supported "
1467 "on this system; requires statvfs(3) support", cmd->argv[0]);
1468 #endif /* !HAVE_SYS_STATVFS_H */
1469
1470 } else if (strcasecmp(ext, "hardlink") == 0) {
1471 switch (action) {
1472 case '-':
1473 ext_flags &= ~SFTP_FXP_EXT_HARDLINK;
1474 break;
1475
1476 case '+':
1477 ext_flags |= SFTP_FXP_EXT_HARDLINK;
1478 break;
1479 }
1480
1481 } else if (strcasecmp(ext, "homeDirectory") == 0) {
1482 switch (action) {
1483 case '-':
1484 ext_flags &= ~SFTP_FXP_EXT_HOMEDIR;
1485 break;
1486
1487 case '+':
1488 ext_flags |= SFTP_FXP_EXT_HOMEDIR;
1489 break;
1490 }
1491
1492 } else if (strcasecmp(ext, "limits") == 0) {
1493 switch (action) {
1494 case '-':
1495 ext_flags &= ~SFTP_FXP_EXT_LIMITS;
1496 break;
1497
1498 case '+':
1499 ext_flags |= SFTP_FXP_EXT_LIMITS;
1500 break;
1501 }
1502
1503 } else if (strcasecmp(ext, "userGroupNames") == 0) {
1504 switch (action) {
1505 case '-':
1506 ext_flags &= ~SFTP_FXP_EXT_USERGROUPNAMES;
1507 break;
1508
1509 case '+':
1510 ext_flags |= SFTP_FXP_EXT_USERGROUPNAMES;
1511 break;
1512 }
1513
1514 } else if (strcasecmp(ext, "xattr") == 0) {
1515 #ifdef HAVE_SYS_XATTR_H
1516 switch (action) {
1517 case '-':
1518 ext_flags &= ~SFTP_FXP_EXT_XATTR;
1519 break;
1520
1521 case '+':
1522 ext_flags |= SFTP_FXP_EXT_XATTR;
1523 break;
1524 }
1525 #else
1526 pr_log_debug(DEBUG0, "%s: [email protected] extension not supported "
1527 "on this system; requires extended attribute support",
1528 (char *) cmd->argv[0]);
1529 #endif /* HAVE_SYS_XATTR_H */
1530
1531 } else {
1532 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unknown extension: '",
1533 ext, "'", NULL));
1534 }
1535 }
1536
1537 c = add_config_param(cmd->argv[0], 1, NULL);
1538 c->argv[0] = palloc(c->pool, sizeof(unsigned long));
1539 *((unsigned long *) c->argv[0]) = ext_flags;
1540
1541 return PR_HANDLED(cmd);
1542 }
1543
1544 /* usage: SFTPHostKey path|"agent:/..."|"NoRSA"|"NoDSA"|"NoECDSA"|"NoED25519" */
1545 MODRET set_sftphostkey(cmd_rec *cmd) {
1546 struct stat st;
1547 int flags = 0;
1548 config_rec *c;
1549 const char *path = NULL;
1550
1551 CHECK_ARGS(cmd, 1);
1552 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1553
1554 if (strncasecmp(cmd->argv[1], "NoRSA", 6) == 0) {
1555 flags |= SFTP_HOSTKEY_FL_CLEAR_RSA_KEY;
1556
1557 } else if (strncasecmp(cmd->argv[1], "NoDSA", 6) == 0) {
1558 flags |= SFTP_HOSTKEY_FL_CLEAR_DSA_KEY;
1559
1560 } else if (strncasecmp(cmd->argv[1], "NoECDSA", 8) == 0) {
1561 flags |= SFTP_HOSTKEY_FL_CLEAR_ECDSA_KEY;
1562
1563 } else if (strncasecmp(cmd->argv[1], "NoED25519", 10) == 0) {
1564 flags |= SFTP_HOSTKEY_FL_CLEAR_ED25519_KEY;
1565 }
1566
1567 if (strncmp(cmd->argv[1], "agent:", 6) != 0 &&
1568 flags == 0) {
1569 int res, xerrno;
1570
1571 path = cmd->argv[1];
1572 if (*path != '/') {
1573 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "must be an absolute path: ",
1574 path, NULL));
1575 }
1576
1577 PRIVS_ROOT
1578 res = stat(path, &st);
1579 xerrno = errno;
1580 PRIVS_RELINQUISH
1581
1582 if (res < 0) {
1583 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to check '", path,
1584 "': ", strerror(xerrno), NULL));
1585 }
1586
1587 if ((st.st_mode & S_IRWXG) ||
1588 (st.st_mode & S_IRWXO)) {
1589 int insecure_hostkey_perms = FALSE;
1590
1591 /* Check for the InsecureHostKeyPerms SFTPOption. */
1592 c = find_config(cmd->server->conf, CONF_PARAM, "SFTPOptions", FALSE);
1593 while (c != NULL) {
1594 unsigned long opts;
1595
1596 pr_signals_handle();
1597
1598 opts = *((unsigned long *) c->argv[0]);
1599 if (opts & SFTP_OPT_INSECURE_HOSTKEY_PERMS) {
1600 insecure_hostkey_perms = TRUE;
1601 break;
1602 }
1603
1604 c = find_config_next(c, c->next, CONF_PARAM, "SFTPOptions", FALSE);
1605 }
1606
1607 if (insecure_hostkey_perms) {
1608 pr_log_pri(PR_LOG_NOTICE, MOD_SFTP_VERSION ": unable to use '%s' "
1609 "as host key, as it is group- or world-accessible", path);
1610
1611 } else {
1612 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to use '", path,
1613 "' as host key, as it is group- or world-accessible", NULL));
1614 }
1615 }
1616 }
1617
1618 c = add_config_param_str(cmd->argv[0], 2, NULL, NULL);
1619 c->argv[0] = pstrdup(c->pool, path);
1620 c->argv[1] = palloc(c->pool, sizeof(int));
1621 *((int *) c->argv[1]) = flags;
1622 return PR_HANDLED(cmd);
1623 }
1624
1625 /* usage: SFTPHostKeys list */
1626 MODRET set_sftphostkeys(cmd_rec *cmd) {
1627 register unsigned int i;
1628 config_rec *c;
1629
1630 if (cmd->argc < 2) {
1631 CONF_ERROR(cmd, "Wrong number of parameters");
1632 }
1633
1634 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1635
1636 for (i = 1; i < cmd->argc; i++) {
1637 if (sftp_crypto_is_hostkey(cmd->argv[i]) < 0) {
1638 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1639 "unsupported host key algorithm: ", (char *) cmd->argv[i], NULL));
1640 }
1641 }
1642
1643 c = add_config_param(cmd->argv[0], cmd->argc-1, NULL);
1644 for (i = 1; i < cmd->argc; i++) {
1645 c->argv[i-1] = pstrdup(c->pool, cmd->argv[i]);
1646 }
1647
1648 return PR_HANDLED(cmd);
1649 }
1650
1651 /* usage: SFTPKeyBlacklist "none"|path */
1652 MODRET set_sftpkeyblacklist(cmd_rec *cmd) {
1653 CHECK_ARGS(cmd, 1);
1654 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1655
1656 if (strncasecmp(cmd->argv[1], "none", 5) != 0) {
1657 if (pr_fs_valid_path(cmd->argv[1]) < 0) {
1658 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "path '", cmd->argv[1],
1659 "' not an absolute path", NULL));
1660 }
1661
1662 if (!exists2(cmd->tmp_pool, cmd->argv[1])) {
1663 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "path '", cmd->argv[1],
1664 "' not found", NULL));
1665 }
1666 }
1667
1668 add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
1669 return PR_HANDLED(cmd);
1670 }
1671
1672 /* usage: SFTPKeyExchanges list */
1673 MODRET set_sftpkeyexchanges(cmd_rec *cmd) {
1674 register unsigned int i;
1675 config_rec *c;
1676 char *exchanges = "";
1677
1678 if (cmd->argc < 2) {
1679 CONF_ERROR(cmd, "Wrong number of parameters");
1680 }
1681
1682 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1683
1684 for (i = 1; i < cmd->argc; i++) {
1685 if (sftp_crypto_is_key_exchange(cmd->argv[i]) < 0) {
1686 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
1687 "unsupported key exchange algorithm: ", cmd->argv[i], NULL));
1688 }
1689 }
1690
1691 c = add_config_param(cmd->argv[0], 1, NULL);
1692 for (i = 1; i < cmd->argc; i++) {
1693 exchanges = pstrcat(c->pool, exchanges, *exchanges ? "," : "", cmd->argv[i],
1694 NULL);
1695 }
1696 c->argv[0] = exchanges;
1697
1698 return PR_HANDLED(cmd);
1699 }
1700
1701 /* usage: SFTPKeyLimits limit1 ... limitN */
1702 MODRET set_sftpkeylimits(cmd_rec *cmd) {
1703 register unsigned int i;
1704 config_rec *c;
1705
1706 if (cmd->argc < 3 ||
1707 ((cmd->argc-1) % 2 != 0)) {
1708 CONF_ERROR(cmd, "wrong number of parameters");
1709 }
1710 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1711
1712 c = add_config_param(cmd->argv[0], 3, NULL, NULL, NULL);
1713
1714 for (i = 1; i < cmd->argc; i++) {
1715 if (strcasecmp(cmd->argv[i], "MinimumRSASize") == 0) {
1716 int nbits;
1717
1718 nbits = atoi(cmd->argv[++i]);
1719 if (nbits < 0) {
1720 CONF_ERROR(cmd, "minimum key size must be zero or greater");
1721 }
1722
1723 c->argv[0] = palloc(c->pool, sizeof(int));
1724 *((int *) c->argv[0]) = nbits;
1725
1726 } else if (strcasecmp(cmd->argv[i], "MinimumDSASize") == 0) {
1727 int nbits;
1728
1729 nbits = atoi(cmd->argv[++i]);
1730 if (nbits < 0) {
1731 CONF_ERROR(cmd, "minimum key size must be zero or greater");
1732 }
1733
1734 c->argv[1] = palloc(c->pool, sizeof(int));
1735 *((int *) c->argv[1]) = nbits;
1736
1737 } else if (strcasecmp(cmd->argv[i], "MinimumECSize") == 0) {
1738 int nbits;
1739
1740 nbits = atoi(cmd->argv[++i]);
1741 if (nbits < 0) {
1742 CONF_ERROR(cmd, "minimum key size must be zero or greater");
1743 }
1744
1745 c->argv[2] = palloc(c->pool, sizeof(int));
1746 *((int *) c->argv[2]) = nbits;
1747
1748 } else {
1749 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown SFTPKeyLimit '",
1750 cmd->argv[i], "'", NULL));
1751 }
1752 }
1753
1754 return PR_HANDLED(cmd);
1755 }
1756
1757 /* usage: SFTPLog path|"none" */
1758 MODRET set_sftplog(cmd_rec *cmd) {
1759 CHECK_ARGS(cmd, 1);
1760 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1761
1762 (void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
1763 return PR_HANDLED(cmd);
1764 }
1765
1766 /* usage: SFTPMaxChannels max */
1767 MODRET set_sftpmaxchannels(cmd_rec *cmd) {
1768 config_rec *c;
1769 unsigned int max;
1770 char *ptr = NULL;
1771
1772 CHECK_ARGS(cmd, 1);
1773 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1774
1775 max = strtoul(cmd->argv[1], &ptr, 10);
1776
1777 if (ptr && *ptr) {
1778 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "maximum channel count '",
1779 cmd->argv[1], "' must be numeric", NULL));
1780 }
1781
1782 if (max == 0) {
1783 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "maximum channel count '",
1784 cmd->argv[1], "' must be greater than zero", NULL));
1785 }
1786
1787 c = add_config_param(cmd->argv[0], 1, NULL);
1788 c->argv[0] = palloc(c->pool, sizeof(unsigned int));
1789 *((unsigned int *) c->argv[0]) = max;
1790
1791 return PR_HANDLED(cmd);
1792 }
1793
1794 /* usage: SFTPOptions opt1 ... optN */
1795 MODRET set_sftpoptions(cmd_rec *cmd) {
1796 register unsigned int i;
1797 config_rec *c;
1798 unsigned long opts = 0UL;
1799
1800 if (cmd->argc-1 == 0) {
1801 CONF_ERROR(cmd, "wrong number of parameters");
1802 }
1803
1804 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1805
1806 c = add_config_param(cmd->argv[0], 1, NULL);
1807
1808 for (i = 1; i < cmd->argc; i++) {
1809 if (strcmp(cmd->argv[i], "IgnoreSFTPUploadPerms") == 0) {
1810 opts |= SFTP_OPT_IGNORE_SFTP_UPLOAD_PERMS;
1811
1812 } else if (strcmp(cmd->argv[i], "IgnoreSFTPSetOwners") == 0) {
1813 opts |= SFTP_OPT_IGNORE_SFTP_SET_OWNERS;
1814
1815 } else if (strcmp(cmd->argv[i], "IgnoreSFTPSetPerms") == 0) {
1816 opts |= SFTP_OPT_IGNORE_SFTP_SET_PERMS;
1817
1818 } else if (strcmp(cmd->argv[i], "IgnoreSFTPSetTimes") == 0) {
1819 opts |= SFTP_OPT_IGNORE_SFTP_SET_TIMES;
1820
1821 } else if (strcmp(cmd->argv[i], "IgnoreSCPUploadPerms") == 0) {
1822 opts |= SFTP_OPT_IGNORE_SCP_UPLOAD_PERMS;
1823
1824 } else if (strcmp(cmd->argv[i], "IgnoreSCPUploadTimes") == 0) {
1825 opts |= SFTP_OPT_IGNORE_SCP_UPLOAD_TIMES;
1826
1827 } else if (strcmp(cmd->argv[i], "OldProtocolCompat") == 0) {
1828 opts |= SFTP_OPT_OLD_PROTO_COMPAT;
1829
1830 /* This option also automatically enables PessimisticKexint,
1831 * as per the comments in RFC4253, Section 5.1.
1832 */
1833 opts |= SFTP_OPT_PESSIMISTIC_KEXINIT;
1834
1835 } else if (strcmp(cmd->argv[i], "PessimisticKexinit") == 0) {
1836 opts |= SFTP_OPT_PESSIMISTIC_KEXINIT;
1837
1838 } else if (strcmp(cmd->argv[i], "MatchKeySubject") == 0) {
1839 opts |= SFTP_OPT_MATCH_KEY_SUBJECT;
1840
1841 } else if (strcmp(cmd->argv[i], "AllowInsecureLogin") == 0) {
1842 opts |= SFTP_OPT_ALLOW_INSECURE_LOGIN;
1843
1844 } else if (strcmp(cmd->argv[i], "InsecureHostKeyPerms") == 0) {
1845 opts |= SFTP_OPT_INSECURE_HOSTKEY_PERMS;
1846
1847 } else if (strcmp(cmd->argv[i], "AllowWeakDH") == 0) {
1848 opts |= SFTP_OPT_ALLOW_WEAK_DH;
1849
1850 } else if (strcmp(cmd->argv[i], "IgnoreFIFOs") == 0) {
1851 opts |= SFTP_OPT_IGNORE_FIFOS;
1852
1853 } else if (strcmp(cmd->argv[i],
1854 "IgnoreSFTPUploadExtendedAttributes") == 0) {
1855 opts |= SFTP_OPT_IGNORE_SFTP_UPLOAD_XATTRS;
1856
1857 } else if (strcmp(cmd->argv[i], "IgnoreSFTPSetExtendedAttributes") == 0) {
1858 opts |= SFTP_OPT_IGNORE_SFTP_SET_XATTRS;
1859
1860 } else if (strcmp(cmd->argv[i], "IncludeSFTPTimes") == 0) {
1861 opts |= SFTP_OPT_INCLUDE_SFTP_TIMES;
1862
1863 } else if (strcmp(cmd->argv[i], "NoExtensionNegotiation") == 0) {
1864 opts |= SFTP_OPT_NO_EXT_INFO;
1865
1866 } else if (strcmp(cmd->argv[i], "NoHostkeyRotation") == 0) {
1867 opts |= SFTP_OPT_NO_HOSTKEY_ROTATION;
1868
1869 } else if (strcmp(cmd->argv[i], "FIDOTouchRequired") == 0) {
1870 opts |= SFTP_OPT_FIDO_TOUCH_REQUIRED;
1871
1872 } else if (strcmp(cmd->argv[i], "FIDOVerifyRequired") == 0) {
1873 opts |= SFTP_OPT_FIDO_VERIFY_REQUIRED;
1874
1875 } else if (strcmp(cmd->argv[i], "NoStrictKex") == 0) {
1876 opts |= SFTP_OPT_NO_STRICT_KEX;
1877
1878 } else {
1879 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown SFTPOption '",
1880 cmd->argv[i], "'", NULL));
1881 }
1882 }
1883
1884 c->argv[0] = pcalloc(c->pool, sizeof(unsigned long));
1885 *((unsigned long *) c->argv[0]) = opts;
1886
1887 if (pr_module_exists("mod_ifsession.c")) {
1888 /* These are needed in case this directive is used with mod_ifsession
1889 * configuration.
1890 */
1891 c->flags |= CF_MULTI;
1892 }
1893
1894 return PR_HANDLED(cmd);
1895 }
1896
1897 /* usage: SFTPPassPhraseProvider path */
1898 MODRET set_sftppassphraseprovider(cmd_rec *cmd) {
1899 struct stat st;
1900 char *path;
1901
1902 CHECK_ARGS(cmd, 1);
1903 CHECK_CONF(cmd, CONF_ROOT);
1904
1905 path = cmd->argv[1];
1906
1907 if (*path != '/') {
1908 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "must be a full path: '", path, "'",
1909 NULL));
1910 }
1911
1912 if (stat(path, &st) < 0) {
1913 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error checking '", path, "': ",
1914 strerror(errno), NULL));
1915 }
1916
1917 if (!S_ISREG(st.st_mode)) {
1918 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to use '", path,
1919 ": Not a regular file", NULL));
1920 }
1921
1922 add_config_param_str(cmd->argv[0], 1, path);
1923 return PR_HANDLED(cmd);
1924 }
1925
1926 /* usage: SFTPRekey "none"|"required" [interval bytes [timeout]] */
1927 MODRET set_sftprekey(cmd_rec *cmd) {
1928 config_rec *c;
1929 int rekey_interval;
1930 unsigned long rekey_mbytes;
1931 char *ptr = NULL;
1932
1933 if (cmd->argc-1 < 1 || cmd->argc-1 > 4) {
1934 CONF_ERROR(cmd, "wrong number of parameters");
1935 }
1936
1937 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
1938
1939 if (strncasecmp(cmd->argv[1], "none", 5) == 0) {
1940 c = add_config_param(cmd->argv[0], 1, NULL);
1941 c->argv[0] = pcalloc(c->pool, sizeof(int));
1942 *((int *) c->argv[0]) = FALSE;
1943
1944 return PR_HANDLED(cmd);
1945 }
1946
1947 if (strncasecmp(cmd->argv[1], "required", 9) != 0) {
1948 CONF_ERROR(cmd, "expected either 'none' or 'required'");
1949 }
1950
1951 if (cmd->argc-1 == 4) {
1952 /* The admin specified a rekey timeout as well. Nice. */
1953 c = add_config_param(cmd->argv[0], 4, NULL, NULL, NULL, NULL);
1954
1955 } else {
1956 /* The admin did not specify rekey timeout as well. Oh well. */
1957 c = add_config_param(cmd->argv[0], 3, NULL, NULL, NULL);
1958 }
1959
1960 c->argv[0] = pcalloc(c->pool, sizeof(int));
1961 *((int *) c->argv[0]) = TRUE;
1962
1963 if (cmd->argc-1 >= 2) {
1964 rekey_interval = atoi(cmd->argv[2]);
1965
1966 } else {
1967 /* Default: one hour. */
1968 rekey_interval = 3600;
1969 }
1970
1971 if (rekey_interval > 0) {
1972 c->argv[1] = pcalloc(c->pool, sizeof(int));
1973 *((int *) c->argv[1]) = rekey_interval;
1974
1975 } else {
1976 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "rekey interval '", cmd->argv[2],
1977 "' must be greater than zero", NULL));
1978 }
1979
1980 if (cmd->argc-1 >= 3) {
1981 rekey_mbytes = strtoul(cmd->argv[3], &ptr, 10);
1982 if (ptr && *ptr) {
1983 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "rekey MB '", cmd->argv[3],
1984 "' must be numeric", NULL));
1985 }
1986
1987 } else {
1988 /* Default: 2 GB */
1989 rekey_mbytes = (2 * 1024);
1990 }
1991
1992 c->argv[2] = pcalloc(c->pool, sizeof(off_t));
1993 *((off_t *) c->argv[2]) = (off_t) rekey_mbytes * 1024 * 1024;
1994
1995 if (cmd->argc-1 == 4) {
1996 int rekey_timeout;
1997
1998 rekey_timeout = atoi(cmd->argv[4]);
1999 if (rekey_timeout > 0) {
2000 c->argv[3] = pcalloc(c->pool, sizeof(int));
2001 *((int *) c->argv[3]) = rekey_timeout;
2002
2003 } else {
2004 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "rekey timeout '", cmd->argv[4],
2005 "' must be greater than zero", NULL));
2006 }
2007 }
2008
2009 return PR_HANDLED(cmd);
2010 }
2011
2012 /* usage: SFTPTrafficPolicy policy */
2013 MODRET set_sftptrafficpolicy(cmd_rec *cmd) {
2014 CHECK_ARGS(cmd, 1);
2015 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
2016
2017 if (sftp_tap_have_policy(cmd->argv[1]) < 0) {
2018 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "'", cmd->argv[1],
2019 "' is not a recognized policy", NULL));
2020 }
2021
2022 (void) add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
2023 return PR_HANDLED(cmd);
2024 }
2025
2026 /* Command handlers
2027 */
2028
2029 MODRET sftp_post_pass(cmd_rec *cmd) {
2030 config_rec *c;
2031
2032 if (sftp_engine == FALSE) {
2033 return PR_DECLINED(cmd);
2034 }
2035
2036 /* We look up SFTPMaxChannels here, post-authentication, to honor any
2037 * mod_ifsession changes (see Issue #1500).
2038 */
2039 c = find_config(main_server->conf, CONF_PARAM, "SFTPMaxChannels", FALSE);
2040 if (c != NULL) {
2041 sftp_channel_set_max_count(*((unsigned int *) c->argv[0]));
2042 }
2043
2044 c = find_config(main_server->conf, CONF_PARAM, "SFTPExtensions", FALSE);
2045 if (c != NULL) {
2046 sftp_fxp_set_extensions(*((unsigned long *) c->argv[0]));
2047 }
2048
2049 /* Check for any rekey policy. */
2050 c = find_config(main_server->conf, CONF_PARAM, "SFTPRekey", FALSE);
2051 if (c != NULL) {
2052 int rekey;
2053
2054 /* The possible int values here are:
2055 *
2056 * 0 (disable rekeying)
2057 * 1 (enable rekeying, with parameters)
2058 */
2059
2060 rekey = *((int *) c->argv[0]);
2061 if (rekey) {
2062 int rekey_interval;
2063 off_t rekey_size;
2064
2065 rekey_interval = *((int *) c->argv[1]);
2066 rekey_size = *((off_t *) c->argv[2]);
2067
2068 pr_trace_msg("ssh2", 6, "SSH2 rekeys requested after %d secs "
2069 "or %" PR_LU " bytes", rekey_interval, (pr_off_t) rekey_size);
2070 sftp_kex_rekey_set_interval(rekey_interval);
2071 sftp_ssh2_packet_rekey_set_size(rekey_size);
2072
2073 if (c->argc == 4) {
2074 int rekey_timeout;
2075
2076 rekey_timeout = *((int *) c->argv[3]);
2077
2078 pr_trace_msg("ssh2", 6, "SSH2 rekeying has %d %s to complete",
2079 rekey_timeout, rekey_timeout != 1 ? "secs" : "sec");
2080 sftp_kex_rekey_set_timeout(rekey_timeout);
2081 }
2082
2083 } else {
2084 sftp_kex_rekey_set_interval(0);
2085 sftp_kex_rekey_set_timeout(0);
2086 sftp_ssh2_packet_rekey_set_seqno(0);
2087 sftp_ssh2_packet_rekey_set_size(0);
2088
2089 pr_trace_msg("ssh2", 6,
2090 "SSH2 server-requested rekeys disabled by SFTPRekey");
2091 }
2092
2093 } else {
2094
2095 /* Set the default rekey values: 1 hour (3600 secs) and 2 GB.
2096 * Also, as per RFC4344, rekeys will be requested whenever the
2097 * packet sequence numbers reach rollover; these are handled by
2098 * default in packet.c.
2099 */
2100 sftp_kex_rekey_set_interval(3600);
2101 sftp_ssh2_packet_rekey_set_size((off_t) 2147483648UL);
2102 }
2103
2104 return PR_DECLINED(cmd);
2105 }
2106
2107 /* Hook handlers
2108 */
2109
2110 MODRET sftp_hook_get_packet_write(cmd_rec *cmd) {
2111 if (cmd->argc != 1) {
2112 return PR_ERROR_MSG(cmd, NULL, "wrong number of arguments");
2113 }
2114
2115 return mod_create_data(cmd, sftp_ssh2_packet_write);
2116 }
2117
2118 MODRET sftp_hook_set_auth_success_handler(cmd_rec *cmd) {
2119 if (cmd->argc != 1) {
2120 return PR_ERROR_MSG(cmd, NULL, "wrong number of arguments");
2121 }
2122
2123 sftp_auth_set_success_handler(cmd->argv[0]);
2124 return PR_HANDLED(cmd);
2125 }
2126
2127 MODRET sftp_hook_set_packet_handler(cmd_rec *cmd) {
2128 if (cmd->argc != 1) {
2129 return PR_ERROR_MSG(cmd, NULL, "wrong number of arguments");
2130 }
2131
2132 sftp_ssh2_packet_set_handler(cmd->argv[0]);
2133 return PR_HANDLED(cmd);
2134 }
2135
2136 /* Event handlers
2137 */
2138
2139 static void sftp_chroot_ev(const void *event_data, void *user_data) {
2140 (void) pr_table_add_dup(session.notes, "mod_sftp.chroot-path", event_data, 0);
2141 }
2142
2143 extern pid_t mpid;
2144
2145 static void sftp_exit_ev(const void *event_data, void *user_data) {
2146
2147 /* Close any channels/sessions that remain open. */
2148 sftp_channel_free();
2149
2150 sftp_keys_free();
2151 sftp_kex_free();
2152
2153 sftp_crypto_free(0);
2154 sftp_utf8_free();
2155
2156 if (sftp_logfd >= 0) {
2157 (void) close(sftp_logfd);
2158 sftp_logfd = -1;
2159 }
2160 }
2161
2162 static void sftp_ban_class_ev(const void *event_data, void *user_data) {
2163 const char *proto;
2164
2165 proto = pr_session_get_protocol(PR_SESS_PROTO_FL_LOGOUT);
2166
2167 /* Only send an SSH2 DISCONNECT if we're dealing with an SSH2 client. */
2168 if (strncmp(proto, "SSH2", 5) == 0) {
2169 sftp_disconnect_send(SFTP_SSH2_DISCONNECT_BY_APPLICATION, "Banned",
2170 __FILE__, __LINE__, "");
2171 }
2172 }
2173
2174 static void sftp_ban_host_ev(const void *event_data, void *user_data) {
2175 const char *proto;
2176
2177 proto = pr_session_get_protocol(PR_SESS_PROTO_FL_LOGOUT);
2178
2179 /* Only send an SSH2 DISCONNECT if we're dealing with an SSH2 client. */
2180 if (strncmp(proto, "SSH2", 5) == 0) {
2181 char *ban_msg = "Banned", *name;
2182
2183 name = user_data;
2184 if (name != NULL) {
2185 ban_msg = pstrcat(sftp_pool, "Host ", name, " has been banned", NULL);
2186 }
2187
2188 sftp_disconnect_send(SFTP_SSH2_DISCONNECT_BY_APPLICATION, ban_msg,
2189 __FILE__, __LINE__, "");
2190 }
2191 }
2192
2193 static void sftp_ban_user_ev(const void *event_data, void *user_data) {
2194 const char *proto;
2195
2196 proto = pr_session_get_protocol(PR_SESS_PROTO_FL_LOGOUT);
2197
2198 /* Only send an SSH2 DISCONNECT if we're dealing with an SSH2 client. */
2199 if (strncmp(proto, "SSH2", 5) == 0) {
2200 char *ban_msg = "Banned", *name;
2201
2202 name = user_data;
2203 if (name != NULL) {
2204 ban_msg = pstrcat(sftp_pool, "User ", name, " has been banned", NULL);
2205 }
2206
2207 sftp_disconnect_send(SFTP_SSH2_DISCONNECT_BY_APPLICATION, ban_msg,
2208 __FILE__, __LINE__, "");
2209 }
2210 }
2211
2212 static void sftp_max_conns_ev(const void *event_data, void *user_data) {
2213 const char *proto;
2214
2215 proto = pr_session_get_protocol(PR_SESS_PROTO_FL_LOGOUT);
2216
2217 /* Only send an SSH2 DISCONNECT if we're dealing with an SSH2 client. */
2218 if (strncmp(proto, "SSH2", 5) == 0) {
2219 sftp_disconnect_send(SFTP_SSH2_DISCONNECT_TOO_MANY_CONNECTIONS,
2220 "Maximum connections for host/user reached", __FILE__, __LINE__, "");
2221 }
2222 }
2223
2224 #if defined(PR_SHARED_MODULE)
2225 static void sftp_mod_unload_ev(const void *event_data, void *user_data) {
2226 if (strncmp((const char *) event_data, "mod_sftp.c", 11) == 0) {
2227 /* Unregister ourselves from all events. */
2228 pr_event_unregister(&sftp_module, NULL, NULL);
2229
2230 sftp_interop_free();
2231 sftp_keystore_free();
2232 sftp_keys_free();
2233 sftp_cipher_free();
2234 sftp_mac_free();
2235 pr_response_block(FALSE);
2236 sftp_utf8_free();
2237
2238 /* Clean up the OpenSSL stuff. */
2239 sftp_crypto_free(0);
2240
2241 destroy_pool(sftp_pool);
2242 sftp_pool = NULL;
2243
2244 close(sftp_logfd);
2245 sftp_logfd = -1;
2246 }
2247 }
2248 #endif
2249
2250 static void sftp_postparse_ev(const void *event_data, void *user_data) {
2251 config_rec *c;
2252 server_rec *s;
2253
2254 /* Initialize OpenSSL. */
2255 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
2256 defined(HAVE_LIBRESSL)
2257 OPENSSL_config(NULL);
2258 ERR_load_crypto_strings();
2259 OpenSSL_add_all_algorithms();
2260 #endif /* prior to OpenSSL-1.1.x */
2261
2262 c = find_config(main_server->conf, CONF_PARAM, "SFTPPassPhraseProvider",
2263 FALSE);
2264 if (c != NULL) {
2265 sftp_keys_set_passphrase_provider(c->argv[0]);
2266 }
2267
2268 sftp_keys_get_passphrases();
2269
2270 /* Initialize the interoperability checks here, so that all session
2271 * processes share the compiled regexes in memory.
2272 */
2273 if (sftp_interop_init() < 0) {
2274 pr_log_pri(PR_LOG_NOTICE, MOD_SFTP_VERSION
2275 ": error preparing interoperability checks: %s", strerror(errno));
2276 }
2277
2278 /* Check for incompatible SFTPAuthMethods configurations. For example,
2279 * configuring:
2280 *
2281 * SFTPAuthMethods hostbased+password
2282 *
2283 * without also configuring SFTPAuthorizedHostKeys means that authentication
2284 * will never succeed.
2285 *
2286 * While here, we also check for unsupported configuration directives, and
2287 * warn if found. And we also look to see if the same vhost has both
2288 * SFTP and TLS (via mod_tls) enabled; that is an invalid configuration.
2289 */
2290 for (s = (server_rec *) server_list->xas_list; s; s = s->next) {
2291 int supports_hostbased = FALSE, supports_publickey = FALSE;
2292 int use_sftp = FALSE, use_tls = FALSE;
2293
2294 c = find_config(s->conf, CONF_PARAM, "SFTPEngine", FALSE);
2295 if (c != NULL) {
2296 use_sftp = *((int *) c->argv[0]);
2297 }
2298
2299 if (use_sftp == FALSE) {
2300 /* No need to check further if mod_sftp is not enabled. */
2301 continue;
2302 }
2303
2304 c = find_config(s->conf, CONF_PARAM, "TLSEngine", FALSE);
2305 if (c != NULL) {
2306 use_tls = *((unsigned char *) c->argv[0]);
2307 }
2308
2309 if (use_tls == TRUE) {
2310 pr_log_pri(PR_LOG_WARNING, MOD_SFTP_VERSION
2311 ": Server '%s': cannot support both FTPS (TLSEngine on) and "
2312 "SFTP (SFTPEngine on) for the same host", s->ServerName);
2313 pr_session_disconnect(&sftp_module, PR_SESS_DISCONNECT_BAD_CONFIG, NULL);
2314 }
2315
2316 c = find_config(s->conf, CONF_PARAM, "SFTPAuthorizedHostKeys", FALSE);
2317 if (c != NULL) {
2318 supports_hostbased = TRUE;
2319 }
2320
2321 c = find_config(s->conf, CONF_PARAM, "SFTPAuthorizedUserKeys", FALSE);
2322 if (c != NULL) {
2323 supports_publickey = TRUE;
2324 }
2325
2326 c = find_config(s->conf, CONF_PARAM, "SFTPAuthMethods", FALSE);
2327 if (c != NULL) {
2328 register unsigned int i;
2329 array_header *auth_chains;
2330
2331 auth_chains = c->argv[0];
2332
2333 for (i = 0; i < auth_chains->nelts; i++) {
2334 register unsigned int j;
2335 struct sftp_auth_chain *auth_chain;
2336
2337 auth_chain = ((struct sftp_auth_chain **) auth_chains->elts)[i];
2338 for (j = 0; j < auth_chain->methods->nelts; j++) {
2339 struct sftp_auth_method *meth;
2340
2341 meth = ((struct sftp_auth_method **) auth_chain->methods->elts)[j];
2342
2343 if (meth->method_id == SFTP_AUTH_FL_METH_HOSTBASED &&
2344 supports_hostbased == FALSE) {
2345 pr_log_pri(PR_LOG_NOTICE, MOD_SFTP_VERSION
2346 ": Server '%s': cannot support authentication method '%s' "
2347 "without SFTPAuthorizedHostKeys configuration", s->ServerName,
2348 meth->method_name);
2349 pr_session_disconnect(&sftp_module, PR_SESS_DISCONNECT_BAD_CONFIG,
2350 NULL);
2351 }
2352
2353 if (meth->method_id == SFTP_AUTH_FL_METH_PUBLICKEY &&
2354 supports_publickey == FALSE) {
2355 pr_log_pri(PR_LOG_NOTICE, MOD_SFTP_VERSION
2356 ": Server '%s': cannot support authentication method '%s' "
2357 "without SFTPAuthorizedUserKeys configuration", s->ServerName,
2358 meth->method_name);
2359 pr_session_disconnect(&sftp_module, PR_SESS_DISCONNECT_BAD_CONFIG,
2360 NULL);
2361 }
2362 }
2363 }
2364 }
2365
2366 /* The following directives are documented as not supported:
2367 * <Anonymous>
2368 * ListOptions
2369 * MaxRetrieveFileSize
2370 */
2371
2372 c = find_config(s->conf, CONF_ANON, NULL, FALSE);
2373 if (c != NULL) {
2374 pr_log_pri(PR_LOG_WARNING, MOD_SFTP_VERSION
2375 ": Server '%s': <Anonymous> configuration is not supported by "
2376 "mod_sftp, and will be ignored", s->ServerName);
2377 }
2378
2379 c = find_config(s->conf, CONF_PARAM, "ListOptions", TRUE);
2380 if (c != NULL) {
2381 pr_log_pri(PR_LOG_WARNING, MOD_SFTP_VERSION
2382 ": Server '%s': ListOptions directive is not supported by mod_sftp, "
2383 "and will be ignored", s->ServerName);
2384 }
2385
2386 c = find_config(s->conf, CONF_PARAM, "MaxRetrieveFileSize", TRUE);
2387 if (c != NULL) {
2388 pr_log_pri(PR_LOG_WARNING, MOD_SFTP_VERSION
2389 ": Server '%s': MaxRetrieveFileSize directive is not supported by "
2390 "mod_sftp, and will be ignored", s->ServerName);
2391 }
2392 }
2393 }
2394
2395 static void sftp_restart_ev(const void *event_data, void *user_data) {
2396
2397 /* Clear the host keys. */
2398 sftp_keys_free();
2399
2400 /* Clear the client banner regexes. */
2401 sftp_interop_free();
2402 }
2403
2404 static void sftp_shutdown_ev(const void *event_data, void *user_data) {
2405 sftp_interop_free();
2406 sftp_keystore_free();
2407 sftp_keys_free();
2408 sftp_cipher_free();
2409 sftp_mac_free();
2410 sftp_utf8_free();
2411
2412 /* Clean up the OpenSSL stuff. */
2413 sftp_crypto_free(0);
2414
2415 #if defined(HAVE_OSSL_PROVIDER_LOAD_OPENSSL)
2416 if (legacy_provider != NULL) {
2417 OSSL_PROVIDER_unload(legacy_provider);
2418 legacy_provider = NULL;
2419 }
2420 #endif /* HAVE_OSSL_PROVIDER_LOAD_OPENSSL */
2421
2422 destroy_pool(sftp_pool);
2423 sftp_pool = NULL;
2424
2425 if (sftp_logfd >= 0) {
2426 close(sftp_logfd);
2427 sftp_logfd = -1;
2428 }
2429 }
2430
2431 static void sftp_timeoutlogin_ev(const void *event_data, void *user_data) {
2432 if (sftp_sess_state & SFTP_SESS_STATE_HAVE_KEX) {
2433 SFTP_DISCONNECT_CONN(SFTP_SSH2_DISCONNECT_BY_APPLICATION, NULL);
2434 }
2435 }
2436
2437 #ifdef PR_USE_DEVEL
2438 static void pool_printf(const char *fmt, ...) {
2439 char buf[PR_TUNABLE_BUFFER_SIZE];
2440 va_list msg;
2441
2442 memset(buf, '\0', sizeof(buf));
2443
2444 va_start(msg, fmt);
2445 pr_vsnprintf(buf, sizeof(buf), fmt, msg);
2446 va_end(msg);
2447
2448 buf[sizeof(buf)-1] = '\0';
2449 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, "%s", buf);
2450 }
2451
2452 static void sftp_sigusr2_ev(const void *event_data, void *user_data) {
2453 /* Note: the mod_shaper module deliberately uses the SIGUSR2 signal
2454 * for handling shaping. Thus we only want to dump out the pools
2455 * IFF mod_shaper is NOT present.
2456 */
2457 if (pr_module_exists("mod_shaper.c") == FALSE) {
2458 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, "%s",
2459 "-----BEGIN POOL DUMP-----");
2460 pr_pool_debug_memory(pool_printf);
2461 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, "%s",
2462 "-----END POOL DUMP-----");
2463 }
2464 }
2465 #endif /* PR_USE_DEVEL */
2466
2467 static void sftp_wrap_conn_denied_ev(const void *event_data, void *user_data) {
2468 const char *proto;
2469
2470 proto = pr_session_get_protocol(PR_SESS_PROTO_FL_LOGOUT);
2471
2472 /* Only send an SSH2 DISCONNECT if we're dealing with an SSH2 client. */
2473 if (strncmp(proto, "SSH2", 5) == 0) {
2474 const char *msg;
2475
2476 msg = get_param_ptr(main_server->conf, "WrapDenyMsg", FALSE);
2477 if (msg != NULL) {
2478 const char *user;
2479
2480 user = session.user;
2481 if (user == NULL) {
2482 user = pr_table_get(session.notes, "mod_auth.orig-user", NULL);
2483 }
2484
2485 /* If the client has authenticated, we can interpolate any '%u'
2486 * variable in the configured deny message.
2487 */
2488 msg = sreplace(sftp_pool, msg, "%u", user, NULL);
2489
2490 } else {
2491 /* XXX This needs to be properly localized. However, trying to use
2492 * the _("") construction here causes mod_sftp.c to fail compilation
2493 * (see Bug#3677), so leave it hardcoded for now.
2494 */
2495 msg = "Access denied";
2496 }
2497
2498 /* If the client has completed the KEXINIT, we can simply use
2499 * sftp_disconnect_send().
2500 */
2501 if (sftp_sess_state & SFTP_SESS_STATE_HAVE_KEX) {
2502 sftp_disconnect_send(SFTP_SSH2_DISCONNECT_BY_APPLICATION, msg,
2503 __FILE__, __LINE__, "");
2504
2505 } else {
2506 /* If the client has not completed the KEXINIT, then just send the
2507 * disconnected message, if any, directly. Make sure to terminate
2508 * the message with a newline character.
2509 */
2510 msg = pstrcat(sftp_pool, msg, "\n", NULL);
2511
2512 /* Make sure we block the Response API, otherwise mod_wrap/mod_wrap2 will
2513 * also be sending its response, and the SSH client may be confused.
2514 */
2515 pr_response_block(TRUE);
2516
2517 if (write(session.c->wfd, msg, strlen(msg)) < 0) {
2518 pr_trace_msg("ssh2", 9,
2519 "error sending mod_wrap2 connection denied message to client: %s",
2520 strerror(errno));
2521 }
2522 }
2523 }
2524 }
2525
2526 /* Initialization routines
2527 */
2528
2529 static int sftp_init(void) {
2530 unsigned long openssl_version;
2531
2532 /* Check that the OpenSSL headers used match the version of the
2533 * OpenSSL library used.
2534 *
2535 * For now, we only log if there is a difference.
2536 */
2537
2538 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
2539 defined(HAVE_LIBRESSL)
2540 openssl_version = SSLeay();
2541 #else
2542 openssl_version = OpenSSL_version_num();
2543 #endif /* prior to OpenSSL-1.1.x */
2544
2545 if (openssl_version != OPENSSL_VERSION_NUMBER) {
2546 int unexpected_version_mismatch = TRUE;
2547
2548 if (OPENSSL_VERSION_NUMBER >= 0x1000000fL) {
2549 /* OpenSSL versions after 1.0.0 try to maintain ABI compatibility.
2550 * So we will warn about header/library version mismatches only if
2551 * the library is older than the headers.
2552 */
2553 if (openssl_version >= OPENSSL_VERSION_NUMBER) {
2554 unexpected_version_mismatch = FALSE;
2555 }
2556 }
2557
2558 if (unexpected_version_mismatch == TRUE) {
2559 const char *version_text;
2560
2561 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
2562 defined(HAVE_LIBRESSL)
2563 version_text = SSLeay_version(SSLEAY_VERSION);
2564 #else
2565 version_text = OpenSSL_version(OPENSSL_VERSION);
2566 #endif /* prior to OpenSSL-1.1.x */
2567
2568 pr_log_pri(PR_LOG_WARNING, MOD_SFTP_VERSION
2569 ": compiled using OpenSSL version '%s' headers, but linked to "
2570 "OpenSSL version '%s' library", OPENSSL_VERSION_TEXT, version_text);
2571 }
2572 }
2573
2574 pr_log_debug(DEBUG2, MOD_SFTP_VERSION ": using " OPENSSL_VERSION_TEXT);
2575
2576 #if defined(HAVE_SODIUM_H)
2577 if (sodium_init() < 0) {
2578 pr_log_pri(PR_LOG_NOTICE, MOD_SFTP_VERSION
2579 ": error initializing libsodium");
2580
2581 } else {
2582 const char *sodium_version;
2583
2584 sodium_version = sodium_version_string();
2585 pr_log_debug(DEBUG2, MOD_SFTP_VERSION ": using libsodium-%s",
2586 sodium_version);
2587 }
2588 #endif /* HAVE_SODIUM_H */
2589
2590 #if defined(HAVE_OSSL_PROVIDER_LOAD_OPENSSL)
2591 /* Load the "legacy" OpenSSL algorithm provider, for those SSH algorithms
2592 * that require support of algorithms that OpenSSL deemed "legacy".
2593 */
2594 legacy_provider = OSSL_PROVIDER_load(NULL, "legacy");
2595 #endif /* HAVE_OSSL_PROVIDER_LOAD_OPENSSL */
2596
2597 sftp_keystore_init();
2598 sftp_crypto_init();
2599 sftp_cipher_init();
2600 sftp_mac_init();
2601
2602 pr_event_register(&sftp_module, "mod_ban.ban-class", sftp_ban_class_ev, NULL);
2603 pr_event_register(&sftp_module, "mod_ban.ban-host", sftp_ban_host_ev, NULL);
2604 pr_event_register(&sftp_module, "mod_ban.ban-user", sftp_ban_user_ev, NULL);
2605
2606 /* Listen for mod_wrap/mod_wrap2 connection denied events, so that we can
2607 * attempt to display any deny messages from those modules to the connecting
2608 * SSH2 client.
2609 */
2610 pr_event_register(&sftp_module, "mod_wrap.connection-denied",
2611 sftp_wrap_conn_denied_ev, NULL);
2612
2613 #if defined(PR_SHARED_MODULE)
2614 pr_event_register(&sftp_module, "core.module-unload", sftp_mod_unload_ev,
2615 NULL);
2616 #endif
2617 pr_event_register(&sftp_module, "core.postparse", sftp_postparse_ev, NULL);
2618 pr_event_register(&sftp_module, "core.restart", sftp_restart_ev, NULL);
2619 pr_event_register(&sftp_module, "core.shutdown", sftp_shutdown_ev, NULL);
2620 pr_event_register(&sftp_module, "core.timeout-login", sftp_timeoutlogin_ev,
2621 NULL);
2622
2623 return 0;
2624 }
2625
2626 static int sftp_sess_init(void) {
2627 config_rec *c;
2628 int times_gmt = TRUE;
2629
2630 c = find_config(main_server->conf, CONF_PARAM, "SFTPEngine", FALSE);
2631 if (c != NULL) {
2632 sftp_engine = *((int *) c->argv[0]);
2633 }
2634
2635 if (sftp_engine == FALSE) {
2636 return 0;
2637 }
2638
2639 pr_event_register(&sftp_module, "core.chroot", sftp_chroot_ev, NULL);
2640 pr_event_register(&sftp_module, "core.exit", sftp_exit_ev, NULL);
2641 #ifdef PR_USE_DEVEL
2642 pr_event_register(&sftp_module, "core.signal.USR2", sftp_sigusr2_ev, NULL);
2643 #endif /* PR_USE_DEVEL */
2644 pr_event_register(&sftp_module, "mod_auth.max-clients",
2645 sftp_max_conns_ev, NULL);
2646 pr_event_register(&sftp_module, "mod_auth.max-clients-per-class",
2647 sftp_max_conns_ev, NULL);
2648 pr_event_register(&sftp_module, "mod_auth.max-clients-per-host",
2649 sftp_max_conns_ev, NULL);
2650 pr_event_register(&sftp_module, "mod_auth.max-clients-per-user",
2651 sftp_max_conns_ev, NULL);
2652 pr_event_register(&sftp_module, "mod_auth.max-connections-per-host",
2653 sftp_max_conns_ev, NULL);
2654 pr_event_register(&sftp_module, "mod_auth.max-hosts-per-user",
2655 sftp_max_conns_ev, NULL);
2656
2657 c = find_config(main_server->conf, CONF_PARAM, "SFTPLog", FALSE);
2658 if (c != NULL) {
2659 sftp_logname = c->argv[0];
2660
2661 if (strcasecmp(sftp_logname, "none") != 0) {
2662 int res, xerrno;
2663
2664 pr_signals_block();
2665 PRIVS_ROOT
2666 res = pr_log_openfile(sftp_logname, &sftp_logfd, PR_LOG_SYSTEM_MODE);
2667 xerrno = errno;
2668 PRIVS_RELINQUISH
2669 pr_signals_unblock();
2670
2671 if (res < 0) {
2672 if (res == -1) {
2673 pr_log_pri(PR_LOG_NOTICE, MOD_SFTP_VERSION
2674 ": notice: unable to open SFTPLog '%s': %s", sftp_logname,
2675 strerror(xerrno));
2676
2677 } else if (res == PR_LOG_WRITABLE_DIR) {
2678 pr_log_pri(PR_LOG_WARNING, MOD_SFTP_VERSION
2679 ": notice: unable to open SFTPLog '%s': parent directory is "
2680 "world-writable", sftp_logname);
2681
2682 } else if (res == PR_LOG_SYMLINK) {
2683 pr_log_pri(PR_LOG_WARNING, MOD_SFTP_VERSION
2684 ": notice: unable to open SFTPLog '%s': cannot log to a symlink",
2685 sftp_logname);
2686 }
2687 }
2688 }
2689 }
2690
2691 if (pr_define_exists("SFTP_USE_FIPS")) {
2692 #ifdef OPENSSL_FIPS
2693 if (!FIPS_mode()) {
2694
2695 /* Make sure OpenSSL is set to use the default RNG, as per an email
2696 * discussion on the OpenSSL developer list:
2697 *
2698 * "The internal FIPS logic uses the default RNG to see the FIPS RNG
2699 * as part of the self test process..."
2700 */
2701 RAND_set_rand_method(NULL);
2702
2703 if (!FIPS_mode_set(1)) {
2704 const char *errstr;
2705
2706 errstr = sftp_crypto_get_errors();
2707 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2708 "unable to use FIPS mode: %s", errstr);
2709 pr_log_pri(PR_LOG_ERR, MOD_SFTP_VERSION ": unable to use FIPS mode: %s",
2710 errstr);
2711
2712 errno = EACCES;
2713 return -1;
2714
2715 } else {
2716 pr_log_pri(PR_LOG_NOTICE, MOD_SFTP_VERSION ": FIPS mode enabled");
2717 }
2718
2719 } else {
2720 pr_log_pri(PR_LOG_DEBUG, MOD_SFTP_VERSION ": FIPS mode already enabled");
2721 }
2722 #else
2723 pr_log_pri(PR_LOG_WARNING, MOD_SFTP_VERSION ": FIPS mode requested, but " OPENSSL_VERSION_TEXT " not built with FIPS support");
2724 #endif /* OPENSSL_FIPS */
2725 }
2726
2727 #if OPENSSL_VERSION_NUMBER > 0x000907000L
2728 /* Handle any requested crypto accelerators/drivers. */
2729 c = find_config(main_server->conf, CONF_PARAM, "SFTPCryptoDevice", FALSE);
2730 if (c != NULL) {
2731 if (sftp_crypto_set_driver(c->argv[0]) < 0) {
2732 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2733 "unable use SFTPCryptoDevice '%s': %s", (const char *) c->argv[0],
2734 strerror(errno));
2735 }
2736 }
2737 #endif
2738
2739 sftp_pool = make_sub_pool(session.pool);
2740 pr_pool_tag(sftp_pool, MOD_SFTP_VERSION);
2741
2742 /* Clear out FTP-isms. */
2743 session.data_port = 0;
2744
2745 c = find_config(main_server->conf, CONF_PARAM, "SFTPOptions", FALSE);
2746 while (c != NULL) {
2747 unsigned long opts;
2748
2749 pr_signals_handle();
2750
2751 opts = *((unsigned long *) c->argv[0]);
2752 sftp_opts |= opts;
2753
2754 c = find_config_next(c, c->next, CONF_PARAM, "SFTPOptions", FALSE);
2755 }
2756
2757 /* We do two passes through the configured hostkeys. On the first pass,
2758 * we focus on loading all of the configured keys. On the second pass,
2759 * we focus on handling any of the hostkey flags that would e.g. clear the
2760 * previously loaded keys.
2761 */
2762
2763 c = find_config(main_server->conf, CONF_PARAM, "SFTPHostKey", FALSE);
2764 while (c != NULL) {
2765 const char *path = c->argv[0];
2766 int flags = *((int *) c->argv[1]);
2767
2768 if (path != NULL &&
2769 flags == 0) {
2770 /* This pool needs to have the lifetime of the session, since the hostkey
2771 * data is needed for rekeying, and rekeying can happen at any time
2772 * during the session.
2773 */
2774 if (sftp_keys_get_hostkey(sftp_pool, path) < 0) {
2775 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2776 "error loading hostkey '%s', skipping key", path);
2777 }
2778 }
2779
2780 c = find_config_next(c, c->next, CONF_PARAM, "SFTPHostKey", FALSE);
2781 }
2782
2783 c = find_config(main_server->conf, CONF_PARAM, "SFTPHostKey", FALSE);
2784 while (c != NULL) {
2785 int flags = *((int *) c->argv[1]);
2786
2787 if (flags != 0) {
2788 /* Handle any flags, such as for clearing previous host keys. */
2789 if (flags & SFTP_HOSTKEY_FL_CLEAR_RSA_KEY) {
2790 if (sftp_keys_clear_rsa_hostkey() < 0) {
2791 pr_trace_msg(trace_channel, 13,
2792 "error clearing RSA hostkey: %s", strerror(errno));
2793
2794 } else {
2795 pr_trace_msg(trace_channel, 9, "cleared RSA hostkey");
2796 }
2797
2798 } else if (flags & SFTP_HOSTKEY_FL_CLEAR_DSA_KEY) {
2799 if (sftp_keys_clear_dsa_hostkey() < 0) {
2800 pr_trace_msg(trace_channel, 13,
2801 "error clearing DSA hostkey: %s", strerror(errno));
2802
2803 } else {
2804 pr_trace_msg(trace_channel, 9, "cleared DSA hostkey");
2805 }
2806
2807 } else if (flags & SFTP_HOSTKEY_FL_CLEAR_ECDSA_KEY) {
2808 if (sftp_keys_clear_ecdsa_hostkey() < 0) {
2809 pr_trace_msg(trace_channel, 13,
2810 "error clearing ECDSA hostkey(s): %s", strerror(errno));
2811
2812 } else {
2813 pr_trace_msg(trace_channel, 9, "cleared ECDSA hostkey(s)");
2814 }
2815
2816 } else if (flags & SFTP_HOSTKEY_FL_CLEAR_ED25519_KEY) {
2817 if (sftp_keys_clear_ed25519_hostkey() < 0) {
2818 pr_trace_msg(trace_channel, 13,
2819 "error clearing ED25519 hostkey(s): %s", strerror(errno));
2820
2821 } else {
2822 pr_trace_msg(trace_channel, 9, "cleared ED25519 hostkey(s)");
2823 }
2824 }
2825 }
2826
2827 c = find_config_next(c, c->next, CONF_PARAM, "SFTPHostKey", FALSE);
2828 }
2829
2830 /* Support having either an RSA hostkey, a DSA hostkey, an ECDSA hostkey,
2831 * an ED25519 hostkey, or any combination thereof. But we have to have at
2832 * least one hostkey.
2833 */
2834 if (sftp_keys_have_dsa_hostkey() < 0 &&
2835 sftp_keys_have_rsa_hostkey() < 0 &&
2836 sftp_keys_have_ecdsa_hostkey(sftp_pool, NULL) < 0 &&
2837 sftp_keys_have_ed25519_hostkey()) {
2838 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2839 "no available host keys, unable to handle session");
2840 errno = EACCES;
2841 return -1;
2842 }
2843
2844 c = find_config(main_server->conf, CONF_PARAM, "SFTPKeyLimits", FALSE);
2845 if (c != NULL) {
2846 int rsa_min = -1, dsa_min = -1, ec_min = -1;
2847
2848 if (c->argv[0] != NULL) {
2849 rsa_min = *((int *) c->argv[0]);
2850 }
2851
2852 if (c->argv[1] != NULL) {
2853 dsa_min = *((int *) c->argv[1]);
2854 }
2855
2856 if (c->argv[2] != NULL) {
2857 ec_min = *((int *) c->argv[2]);
2858 }
2859
2860 if (rsa_min > -1 ||
2861 dsa_min > -1 ||
2862 ec_min > -1) {
2863 if (sftp_keys_set_key_limits(rsa_min, dsa_min, ec_min) < 0) {
2864 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2865 "error setting SFTPKeyLimits: %s", strerror(errno));
2866 }
2867 }
2868 }
2869
2870 c = find_config(main_server->conf, CONF_PARAM, "SFTPKeyBlacklist", FALSE);
2871 if (c != NULL) {
2872 if (strncasecmp((char *) c->argv[0], "none", 5) != 0) {
2873 sftp_blacklist_set_file(c->argv[0]);
2874
2875 } else {
2876 /* Admin explicitly requested no checking of a key blacklist. */
2877 sftp_blacklist_set_file(NULL);
2878 }
2879 }
2880
2881 c = find_config(main_server->conf, CONF_PARAM, "DisplayLogin", FALSE);
2882 if (c != NULL) {
2883 const char *path;
2884
2885 path = c->argv[0];
2886 if (sftp_fxp_set_displaylogin(path) < 0) {
2887 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2888 "error using DisplayLogin '%s': %s", path, strerror(errno));
2889 }
2890 }
2891
2892 c = find_config(main_server->conf, CONF_PARAM, "ServerIdent", FALSE);
2893 if (c != NULL) {
2894 if (*((unsigned char *) c->argv[0]) == FALSE) {
2895 /* The admin configured "ServerIdent off". Set the version string to
2896 * just "mod_sftp", and that's it, no version.
2897 */
2898 sftp_server_version = pstrcat(sftp_pool, SFTP_ID_PREFIX, "mod_sftp",
2899 NULL);
2900 sftp_ssh2_packet_set_version(sftp_server_version);
2901
2902 } else {
2903 /* The admin configured "ServerIdent on", and possibly some custom
2904 * string.
2905 */
2906 if (c->argc > 1) {
2907 sftp_server_version = pstrcat(sftp_pool, SFTP_ID_PREFIX, c->argv[1],
2908 NULL);
2909 sftp_ssh2_packet_set_version(sftp_server_version);
2910 }
2911 }
2912 }
2913
2914 c = find_config(main_server->conf, CONF_PARAM, "TimesGMT", FALSE);
2915 if (c != NULL) {
2916 times_gmt = *((unsigned char *) c->argv[0]);
2917 }
2918
2919 sftp_fxp_use_gmt(times_gmt);
2920
2921 pr_response_block(TRUE);
2922
2923 c = find_config(main_server->conf, CONF_PARAM, "SFTPClientAlive", FALSE);
2924 if (c != NULL) {
2925 unsigned int count, interval;
2926
2927 count = *((unsigned int *) c->argv[0]);
2928 interval = *((unsigned int *) c->argv[1]);
2929
2930 (void) sftp_ssh2_packet_set_client_alive(count, interval);
2931
2932 pr_trace_msg("ssh2", 7,
2933 "client alive checks requested after %u secs, up to %u times",
2934 interval, count);
2935 }
2936
2937 /* Enable traffic analysis protection (TAP) after keys have been
2938 * exchanged, based on the configured policy.
2939 */
2940 c = find_config(main_server->conf, CONF_PARAM, "SFTPTrafficPolicy", FALSE);
2941 if (c != NULL) {
2942 const char *policy = c->argv[0];
2943
2944 if (sftp_tap_set_policy(policy) < 0) {
2945 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2946 "error setting TrafficPolicy '%s': %s", policy, strerror(errno));
2947
2948 } else {
2949 pr_trace_msg("ssh2", 9, "using TAP policy '%s'", policy);
2950 }
2951 }
2952
2953 pr_session_set_protocol("ssh2");
2954
2955 /* Use our own "authenticated yet?" check. */
2956 set_auth_check(sftp_have_authenticated);
2957
2958 pr_cmd_set_handler(sftp_cmd_loop);
2959
2960 /* Check for any UseEncoding directives. Specifically, we're interested
2961 * in the charset portion; the encoding is always UTF8 for SFTP clients
2962 * (when applicable).
2963 */
2964
2965 c = find_config(main_server->conf, CONF_PARAM, "UseEncoding", FALSE);
2966 if (c != NULL) {
2967 if (c->argc == 2) {
2968 char *charset;
2969
2970 charset = c->argv[0];
2971
2972 if (sftp_utf8_set_charset(charset) < 0) {
2973 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
2974 "error setting local charset '%s': %s", charset, strerror(errno));
2975
2976 /* Re-initialize the UTF8 conversion handles. */
2977 (void) sftp_utf8_free();
2978 sftp_utf8_init();
2979 }
2980
2981 } else {
2982 sftp_utf8_init();
2983 }
2984
2985 } else {
2986 sftp_utf8_init();
2987 }
2988
2989 return 0;
2990 }
2991
2992 /* Module API tables
2993 */
2994
2995 static conftable sftp_conftab[] = {
2996 { "SFTPAcceptEnv", set_sftpacceptenv, NULL },
2997 { "SFTPAuthMethods", set_sftpauthmeths, NULL },
2998 { "SFTPAuthPublicKeys", set_sftpauthpublickeys, NULL },
2999 { "SFTPAuthorizedHostKeys", set_sftpauthorizedkeys, NULL },
3000 { "SFTPAuthorizedUserKeys", set_sftpauthorizedkeys, NULL },
3001 { "SFTPCiphers", set_sftpciphers, NULL },
3002 { "SFTPClientAlive", set_sftpclientalive, NULL },
3003 { "SFTPClientMatch", set_sftpclientmatch, NULL },
3004 { "SFTPCompression", set_sftpcompression, NULL },
3005 { "SFTPCryptoDevice", set_sftpcryptodevice, NULL },
3006 { "SFTPDHParamFile", set_sftpdhparamfile, NULL },
3007 { "SFTPDigests", set_sftpdigests, NULL },
3008 { "SFTPDisplayBanner", set_sftpdisplaybanner, NULL },
3009 { "SFTPEngine", set_sftpengine, NULL },
3010 { "SFTPExtensions", set_sftpextensions, NULL },
3011 { "SFTPHostKey", set_sftphostkey, NULL },
3012 { "SFTPHostKeys", set_sftphostkeys, NULL },
3013 { "SFTPKeyBlacklist", set_sftpkeyblacklist, NULL },
3014 { "SFTPKeyExchanges", set_sftpkeyexchanges, NULL },
3015 { "SFTPKeyLimits", set_sftpkeylimits, NULL },
3016 { "SFTPLog", set_sftplog, NULL },
3017 { "SFTPMaxChannels", set_sftpmaxchannels, NULL },
3018 { "SFTPOptions", set_sftpoptions, NULL },
3019 { "SFTPPassPhraseProvider", set_sftppassphraseprovider, NULL },
3020 { "SFTPRekey", set_sftprekey, NULL },
3021 { "SFTPTrafficPolicy", set_sftptrafficpolicy, NULL },
3022 { NULL }
3023 };
3024
3025 static cmdtable sftp_cmdtab[] = {
3026 { POST_CMD, C_PASS, G_NONE, sftp_post_pass, FALSE, FALSE },
3027
3028 /* Module hooks */
3029 { HOOK, "sftp_get_packet_write", G_NONE, sftp_hook_get_packet_write, FALSE, FALSE },
3030 { HOOK, "sftp_set_auth_success_handler", G_NONE, sftp_hook_set_auth_success_handler, FALSE, FALSE },
3031 { HOOK, "sftp_set_packet_handler", G_NONE, sftp_hook_set_packet_handler, FALSE, FALSE },
3032
3033 { 0, NULL }
3034 };
3035
3036 module sftp_module = {
3037 /* Always NULL */
3038 NULL, NULL,
3039
3040 /* Module API version */
3041 0x20,
3042
3043 /* Module name */
3044 "sftp",
3045
3046 /* Module configuration handler table */
3047 sftp_conftab,
3048
3049 /* Module command handler table */
3050 sftp_cmdtab,
3051
3052 /* Module authentication handler table */
3053 NULL,
3054
3055 /* Module initialization */
3056 sftp_init,
3057
3058 /* Session initialization */
3059 sftp_sess_init,
3060
3061 /* Module version */
3062 MOD_SFTP_VERSION
3063 };