| 1 |
/* |
| 2 |
* ProFTPD - mod_sftp |
| 3 |
* Copyright (c) 2008-2023 TJ Saunders |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA. |
| 18 |
* |
| 19 |
* As a special exemption, TJ Saunders and other respective copyright holders |
| 20 |
* give permission to link this program with OpenSSL, and distribute the |
| 21 |
* resulting executable, without including the source code for OpenSSL in the |
| 22 |
* source distribution. |
| 23 |
*/ |
| 24 |
|
| 25 |
#ifndef MOD_SFTP_H |
| 26 |
#define MOD_SFTP_H |
| 27 |
|
| 28 |
#include "conf.h" |
| 29 |
#include "privs.h" |
| 30 |
|
| 31 |
#include <signal.h> |
| 32 |
|
| 33 |
#if HAVE_MLOCK |
| 34 |
# include <sys/mman.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#if HAVE_SYS_UIO_H |
| 38 |
# include <sys/uio.h> |
| 39 |
#endif |
| 40 |
|
| 41 |
#ifndef MAX |
| 42 |
# define MAX(x, y) (((x) > (y)) ? (x) : (y)) |
| 43 |
# define MIN(x, y) (((x) < (y)) ? (x) : (y)) |
| 44 |
#endif |
| 45 |
|
| 46 |
/* Define if you have the <zlib.h> header. */ |
| 47 |
#undef HAVE_ZLIB_H |
| 48 |
|
| 49 |
/* Define if you have OpenSSL with crippled AES support. */ |
| 50 |
#undef HAVE_AES_CRIPPLED_OPENSSL |
| 51 |
|
| 52 |
/* Define if you have OpenSSL with EVP_aes_128_ctr support. */ |
| 53 |
#undef HAVE_EVP_AES_128_CTR_OPENSSL |
| 54 |
|
| 55 |
/* Define if you have OpenSSL with EVP_aes_192_ctr support. */ |
| 56 |
#undef HAVE_EVP_AES_192_CTR_OPENSSL |
| 57 |
|
| 58 |
/* Define if you have OpenSSL with EVP_aes_256_ctr support. */ |
| 59 |
#undef HAVE_EVP_AES_256_CTR_OPENSSL |
| 60 |
|
| 61 |
/* Define if you have OpenSSL with EVP_aes_256_gcm support. */ |
| 62 |
#undef HAVE_EVP_AES_256_GCM_OPENSSL |
| 63 |
|
| 64 |
/* Define if you have OpenSSL with EVP_chacha20 support. |
| 65 |
* |
| 66 |
* Note, however, that older LibreSSL versions have broken ChaCha20 support. |
| 67 |
*/ |
| 68 |
#undef HAVE_EVP_CHACHA20_OPENSSL |
| 69 |
|
| 70 |
#if defined(HAVE_LIBRESSL) && \ |
| 71 |
LIBRESSL_VERSION_NUMBER < 0x3010000fL |
| 72 |
# undef HAVE_BROKEN_CHACHA20 |
| 73 |
#endif |
| 74 |
|
| 75 |
/* Define if you have OpenSSL with OSSL_PROVIDER_load support. */ |
| 76 |
#undef HAVE_OSSL_PROVIDER_LOAD_OPENSSL |
| 77 |
|
| 78 |
/* Define if you have OpenSSL with SHA256 support. */ |
| 79 |
#undef HAVE_SHA256_OPENSSL |
| 80 |
|
| 81 |
/* Define if you have OpenSSL with SHA512 support. */ |
| 82 |
#undef HAVE_SHA512_OPENSSL |
| 83 |
|
| 84 |
/* Define if you have OpenSSL with X448 support. */ |
| 85 |
#undef HAVE_X448_OPENSSL |
| 86 |
|
| 87 |
#define MOD_SFTP_VERSION "mod_sftp/1.2.0" |
| 88 |
|
| 89 |
/* Make sure the version of proftpd is as necessary. */ |
| 90 |
#if PROFTPD_VERSION_NUMBER < 0x0001030402 |
| 91 |
# error "ProFTPD 1.3.4rc2 or later required" |
| 92 |
#endif |
| 93 |
|
| 94 |
#include <openssl/bio.h> |
| 95 |
#if !defined(OPENSSL_NO_BF) |
| 96 |
# include <openssl/blowfish.h> |
| 97 |
#endif /* !OPENSSL_NO_BF */ |
| 98 |
#include <openssl/bn.h> |
| 99 |
#include <openssl/conf.h> |
| 100 |
#if !defined(OPENSSL_NO_DES) |
| 101 |
# include <openssl/des.h> |
| 102 |
#endif /* !OPENSSL_NO_DES */ |
| 103 |
#include <openssl/evp.h> |
| 104 |
#include <openssl/hmac.h> |
| 105 |
#include <openssl/x509v3.h> |
| 106 |
#include <openssl/err.h> |
| 107 |
#include <openssl/rand.h> |
| 108 |
#include <openssl/pem.h> |
| 109 |
#if !defined(OPENSSL_NO_DSA) |
| 110 |
# include <openssl/dsa.h> |
| 111 |
#endif /* !OPENSSL_NO_DSA */ |
| 112 |
#include <openssl/rsa.h> |
| 113 |
#if OPENSSL_VERSION_NUMBER > 0x000907000L |
| 114 |
# include <openssl/aes.h> |
| 115 |
# ifdef PR_USE_OPENSSL_ENGINE |
| 116 |
# include <openssl/engine.h> |
| 117 |
# endif /* PR_USE_OPENSSL_ENGINE */ |
| 118 |
# include <openssl/ocsp.h> |
| 119 |
#endif |
| 120 |
#if defined(PR_USE_OPENSSL_ECC) |
| 121 |
# include <openssl/ec.h> |
| 122 |
# include <openssl/ecdh.h> |
| 123 |
#endif /* PR_USE_OPENSSL_ECC */ |
| 124 |
#if defined(HAVE_OSSL_PROVIDER_LOAD_OPENSSL) |
| 125 |
# include <openssl/provider.h> |
| 126 |
#endif /* HAVE_OSSL_PROVIDER_LOAD_OPENSSL */ |
| 127 |
|
| 128 |
/* Define if you have the LibreSSL library. |
| 129 |
* |
| 130 |
* Note that in LibreSSL-3.5.0, the structs became opaque, as they are in |
| 131 |
* OpenSSL-1.1.0, and thus these version-dependent macros became more |
| 132 |
* complex. |
| 133 |
*/ |
| 134 |
#if defined(LIBRESSL_VERSION_NUMBER) |
| 135 |
# define HAVE_LIBRESSL 1 |
| 136 |
#endif |
| 137 |
|
| 138 |
#define SFTP_ID_PREFIX "SSH-2.0-" |
| 139 |
|
| 140 |
/* Omit the version information in the default banner. Sites wishing to use |
| 141 |
* or see that version information can configure it explicitly via ServerIdent. |
| 142 |
*/ |
| 143 |
#define SFTP_ID_DEFAULT_STRING SFTP_ID_PREFIX "mod_sftp" |
| 144 |
|
| 145 |
/* mod_sftp session state flags */ |
| 146 |
#define SFTP_SESS_STATE_HAVE_KEX 0x00001 |
| 147 |
#define SFTP_SESS_STATE_HAVE_SERVICE 0x00002 |
| 148 |
#define SFTP_SESS_STATE_HAVE_AUTH 0x00004 |
| 149 |
#define SFTP_SESS_STATE_REKEYING 0x00008 |
| 150 |
#define SFTP_SESS_STATE_HAVE_EXT_INFO 0x00010 |
| 151 |
|
| 152 |
/* mod_sftp option flags */ |
| 153 |
#define SFTP_OPT_IGNORE_SFTP_UPLOAD_PERMS 0x000001 |
| 154 |
#define SFTP_OPT_IGNORE_SCP_UPLOAD_PERMS 0x000002 |
| 155 |
#define SFTP_OPT_PESSIMISTIC_KEXINIT 0x000004 |
| 156 |
#define SFTP_OPT_OLD_PROTO_COMPAT 0x000008 |
| 157 |
#define SFTP_OPT_MATCH_KEY_SUBJECT 0x000010 |
| 158 |
#define SFTP_OPT_IGNORE_SFTP_SET_PERMS 0x000020 |
| 159 |
#define SFTP_OPT_IGNORE_SFTP_SET_TIMES 0x000040 |
| 160 |
#define SFTP_OPT_IGNORE_SFTP_SET_OWNERS 0x000080 |
| 161 |
#define SFTP_OPT_IGNORE_SCP_UPLOAD_TIMES 0x000100 |
| 162 |
#define SFTP_OPT_ALLOW_INSECURE_LOGIN 0x000200 |
| 163 |
#define SFTP_OPT_INSECURE_HOSTKEY_PERMS 0x000400 |
| 164 |
#define SFTP_OPT_ALLOW_WEAK_DH 0x000800 |
| 165 |
#define SFTP_OPT_IGNORE_FIFOS 0x001000 |
| 166 |
#define SFTP_OPT_IGNORE_SFTP_UPLOAD_XATTRS 0x002000 |
| 167 |
#define SFTP_OPT_IGNORE_SFTP_SET_XATTRS 0x004000 |
| 168 |
#define SFTP_OPT_INCLUDE_SFTP_TIMES 0x008000 |
| 169 |
#define SFTP_OPT_NO_EXT_INFO 0x010000 |
| 170 |
#define SFTP_OPT_NO_HOSTKEY_ROTATION 0x020000 |
| 171 |
#define SFTP_OPT_FIDO_TOUCH_REQUIRED 0x040000 |
| 172 |
#define SFTP_OPT_FIDO_VERIFY_REQUIRED 0x080000 |
| 173 |
#define SFTP_OPT_NO_STRICT_KEX 0x100000 |
| 174 |
|
| 175 |
/* mod_sftp service flags */ |
| 176 |
#define SFTP_SERVICE_FL_SFTP 0x0001 |
| 177 |
#define SFTP_SERVICE_FL_SCP 0x0002 |
| 178 |
#define SFTP_SERVICE_FL_DATE 0x0004 |
| 179 |
|
| 180 |
#define SFTP_SERVICE_DEFAULT \ |
| 181 |
(SFTP_SERVICE_FL_SFTP|SFTP_SERVICE_FL_SCP) |
| 182 |
|
| 183 |
/* mod_sftp roles */ |
| 184 |
#define SFTP_ROLE_SERVER 1 |
| 185 |
#define SFTP_ROLE_CLIENT 2 |
| 186 |
|
| 187 |
/* mod_sftp generic/fake "command ID" for logging */ |
| 188 |
#define SFTP_CMD_ID 128 |
| 189 |
|
| 190 |
/* Miscellaneous */ |
| 191 |
extern int sftp_logfd; |
| 192 |
extern const char *sftp_logname; |
| 193 |
extern pool *sftp_pool; |
| 194 |
extern conn_t *sftp_conn; |
| 195 |
extern unsigned int sftp_sess_state; |
| 196 |
extern unsigned long sftp_opts; |
| 197 |
extern unsigned int sftp_services; |
| 198 |
|
| 199 |
/* Used by other SFTP modules (e.g. mod_sftp_pam) for sending USERAUTH_BANNER |
| 200 |
* messages to the client. |
| 201 |
*/ |
| 202 |
int sftp_auth_send_banner(const char *); |
| 203 |
|
| 204 |
/* API for modules that which to register "keyboard-interactive" auth |
| 205 |
* drivers (see RFC4256). |
| 206 |
*/ |
| 207 |
|
| 208 |
typedef struct { |
| 209 |
const char *challenge; |
| 210 |
|
| 211 |
/* TRUE or FALSE, depending on whether the user's response should be |
| 212 |
* displayed back to them (TRUE), or hidden (FALSE). |
| 213 |
*/ |
| 214 |
char display_response; |
| 215 |
|
| 216 |
} sftp_kbdint_challenge_t; |
| 217 |
|
| 218 |
typedef struct kbdint_st { |
| 219 |
const char *driver_name; |
| 220 |
|
| 221 |
/* Memory pool for this driver */ |
| 222 |
pool *driver_pool; |
| 223 |
|
| 224 |
/* Arbitrary driver-specific data */ |
| 225 |
void *driver_data; |
| 226 |
|
| 227 |
/* Open the driver for authentication. The user argument is the name of the |
| 228 |
* user being authenticated. Returns zero on success, -1 otherwise |
| 229 |
* (with errno set appropriately). |
| 230 |
*/ |
| 231 |
int (*open)(struct kbdint_st *driver, const char *user); |
| 232 |
|
| 233 |
/* Authenticate the given user. The driver can use the following |
| 234 |
* functions for writing and reading the driver challenge/response data |
| 235 |
* to and from the client: |
| 236 |
* |
| 237 |
* sftp_kbdint_send_challenge() |
| 238 |
* sftp_kbdint_read_response() |
| 239 |
* |
| 240 |
* The array_header argument to the read_response() function will be |
| 241 |
* populated with strings from the client. |
| 242 |
* |
| 243 |
* Returns zero on success, -1 otherwise (with errno set appropriately). |
| 244 |
*/ |
| 245 |
int (*authenticate)(struct kbdint_st *driver, const char *user); |
| 246 |
|
| 247 |
/* Close the driver, clean up any allocated resources. Returns |
| 248 |
* zero on success, -1 otherwise (with errno set appropriately). |
| 249 |
*/ |
| 250 |
int (*close)(struct kbdint_st *driver); |
| 251 |
|
| 252 |
} sftp_kbdint_driver_t; |
| 253 |
|
| 254 |
int sftp_kbdint_register_driver(const char *name, sftp_kbdint_driver_t *driver); |
| 255 |
int sftp_kbdint_unregister_driver(const char *name); |
| 256 |
int sftp_kbdint_send_challenge(const char *, const char *, uint32_t, |
| 257 |
sftp_kbdint_challenge_t *); |
| 258 |
int sftp_kbdint_recv_response(pool *, uint32_t, uint32_t *, |
| 259 |
const char ***); |
| 260 |
|
| 261 |
/* API for modules that which to register keystores, for the |
| 262 |
* SFTPAuthorizedHostKeys and SFTPAuthorizedUserKeys directives. |
| 263 |
*/ |
| 264 |
|
| 265 |
typedef struct keystore_st { |
| 266 |
/* Memory pool for this keystore. */ |
| 267 |
pool *keystore_pool; |
| 268 |
|
| 269 |
/* Arbitrary keystore-specific data. */ |
| 270 |
void *keystore_data; |
| 271 |
|
| 272 |
/* The type of keys (host, user) that this store can handle. */ |
| 273 |
unsigned int store_ktypes; |
| 274 |
|
| 275 |
/* Verify the given host key. Returns zero on success, -1 otherwise. */ |
| 276 |
int (*verify_host_key)(struct keystore_st *store, pool *p, const char *user, |
| 277 |
const char *host_fqdn, const char *host_user, unsigned char *host_key, |
| 278 |
uint32_t host_keylen, pr_table_t *key_notes); |
| 279 |
|
| 280 |
/* Verify the given user key. Returns zero on success, -1 otherwise. */ |
| 281 |
int (*verify_user_key)(struct keystore_st *store, pool *p, const char *user, |
| 282 |
unsigned char *user_key, uint32_t user_keylen, pr_table_t *key_notes); |
| 283 |
|
| 284 |
/* Close this keystore, clean up any allocated resources. Returns |
| 285 |
* zero on success, -1 otherwise (with errno set appropriately). |
| 286 |
*/ |
| 287 |
int (*store_close)(struct keystore_st *store); |
| 288 |
|
| 289 |
} sftp_keystore_t; |
| 290 |
|
| 291 |
#define SFTP_SSH2_HOST_KEY_STORE 0x01 |
| 292 |
#define SFTP_SSH2_USER_KEY_STORE 0x02 |
| 293 |
|
| 294 |
/* Known/supported RFC4716 headers. */ |
| 295 |
#define SFTP_KEYSTORE_HEADER_COMMENT "Comment" |
| 296 |
#define SFTP_KEYSTORE_HEADER_SUBJECT "Subject" |
| 297 |
#define SFTP_KEYSTORE_HEADER_FIDO_TOUCH_REQUIRED "X-FIDO-Touch-Required" |
| 298 |
#define SFTP_KEYSTORE_HEADER_FIDO_VERIFY_REQUIRED "X-FIDO-Verify-Required" |
| 299 |
|
| 300 |
int sftp_keystore_register_store(const char *, |
| 301 |
sftp_keystore_t *(*store_open)(pool *, int, const char *, const char *), |
| 302 |
unsigned int); |
| 303 |
int sftp_keystore_unregister_store(const char *, unsigned int); |
| 304 |
|
| 305 |
/* For use by keystore backend modules. */ |
| 306 |
int sftp_keys_compare_keys(pool *, unsigned char *, uint32_t, unsigned char *, |
| 307 |
uint32_t); |
| 308 |
|
| 309 |
/* These strings are part of any RFC4716 key; thus they will be needed by |
| 310 |
* any keystore backend modules. |
| 311 |
*/ |
| 312 |
#define SFTP_SSH2_PUBKEY_BEGIN_MARKER "---- BEGIN SSH2 PUBLIC KEY ----" |
| 313 |
#define SFTP_SSH2_PUBKEY_END_MARKER "---- END SSH2 PUBLIC KEY ----" |
| 314 |
|
| 315 |
/* For use by other SFTP modules. */ |
| 316 |
const char *sftp_crypto_get_errors(void); |
| 317 |
|
| 318 |
/* For use by other modules to register 'exec' channel request handlers. |
| 319 |
* |
| 320 |
* Note that the implementation currently assumes that the registering module |
| 321 |
* is registering its callbacks from a session process, i.e. post-fork(2). |
| 322 |
* That is why there is no corresponding unregister function at present. |
| 323 |
*/ |
| 324 |
int sftp_channel_register_exec_handler(module *, const char *, |
| 325 |
int (*set_params)(pool *, uint32_t, array_header *), |
| 326 |
int (*prep_chan)(uint32_t), |
| 327 |
int (*postopen_chan)(uint32_t), |
| 328 |
int (*handle_pkt)(pool *, void *, uint32_t, unsigned char *, uint32_t), |
| 329 |
int (*finish_chan)(uint32_t), |
| 330 |
int (**write_data)(pool *, uint32_t, unsigned char *, uint32_t)); |
| 331 |
|
| 332 |
#endif |