Debian Codegraph @

contrib/mod_sftp/auth-hostbased.c

raw symbols SCIP
1 /*
2 * ProFTPD - mod_sftp 'hostbased' user authentication
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 #include "mod_sftp.h"
26 #include "ssh2.h"
27 #include "packet.h"
28 #include "msg.h"
29 #include "auth.h"
30 #include "session.h"
31 #include "keys.h"
32 #include "keystore.h"
33 #include "interop.h"
34 #include "blacklist.h"
35 #include "utf8.h"
36
37 static const char *trace_channel = "ssh2";
38
39 int sftp_auth_hostbased(struct ssh2_packet *pkt, cmd_rec *pass_cmd,
40 const char *orig_user, const char *user, const char *service,
41 unsigned char **buf, uint32_t *buflen, int *send_userauth_fail) {
42 struct passwd *pw;
43 char *hostkey_algo, *host_fqdn, *host_user, *host_user_utf8;
44 const char *fp = NULL, *fp_algo = NULL;
45 unsigned char *hostkey_data, *signature_data;
46 unsigned char *buf2, *ptr2;
47 const unsigned char *id;
48 uint32_t buflen2, bufsz2, hostkey_datalen, id_len, signature_len;
49 enum sftp_key_type_e pubkey_type;
50 struct sftp_verify_details *details = NULL;
51 pr_table_t *verify_notes = NULL;
52 int fp_algo_id;
53
54 if (pr_cmd_dispatch_phase(pass_cmd, PRE_CMD, 0) < 0) {
55 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
56 "authentication request for user '%s' blocked by '%s' handler",
57 orig_user, (char *) pass_cmd->argv[0]);
58
59 pr_log_auth(PR_LOG_NOTICE,
60 "USER %s (Login failed): blocked by '%s' handler", orig_user,
61 (char *) pass_cmd->argv[0]);
62
63 pr_cmd_dispatch_phase(pass_cmd, POST_CMD_ERR, 0);
64 pr_cmd_dispatch_phase(pass_cmd, LOG_CMD_ERR, 0);
65
66 *send_userauth_fail = TRUE;
67 errno = EPERM;
68 return 0;
69 }
70
71 hostkey_algo = sftp_msg_read_string(pkt->pool, buf, buflen);
72 if (hostkey_algo == NULL) {
73 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
74 "missing required host key algorithm, rejecting request");
75
76 *send_userauth_fail = TRUE;
77 errno = EINVAL;
78 return 0;
79 }
80
81 hostkey_datalen = sftp_msg_read_int(pkt->pool, buf, buflen);
82 hostkey_data = sftp_msg_read_data(pkt->pool, buf, buflen, hostkey_datalen);
83
84 host_fqdn = sftp_msg_read_string(pkt->pool, buf, buflen);
85
86 host_user_utf8 = sftp_msg_read_string(pkt->pool, buf, buflen);
87 host_user = sftp_utf8_decode_str(pkt->pool, host_user_utf8);
88
89 signature_len = sftp_msg_read_int(pkt->pool, buf, buflen);
90 signature_data = sftp_msg_read_data(pkt->pool, buf, buflen, signature_len);
91
92 pr_trace_msg(trace_channel, 9,
93 "client sent '%s' host key, FQDN %s, and remote user '%s'",
94 hostkey_algo, host_fqdn, host_user);
95
96 if (strcmp(hostkey_algo, "ssh-rsa") == 0) {
97 pubkey_type = SFTP_KEY_RSA;
98
99 #if defined(HAVE_SHA256_OPENSSL)
100 } else if (strcmp(hostkey_algo, "rsa-sha2-256") == 0) {
101 pubkey_type = SFTP_KEY_RSA_SHA256;
102 #endif /* HAVE_SHA256_OPENSSL */
103
104 #if defined(HAVE_SHA512_OPENSSL)
105 } else if (strcmp(hostkey_algo, "rsa-sha2-512") == 0) {
106 pubkey_type = SFTP_KEY_RSA_SHA512;
107 #endif /* HAVE_SHA512_OPENSSL */
108
109 } else if (strcmp(hostkey_algo, "ssh-dss") == 0) {
110 pubkey_type = SFTP_KEY_DSA;
111
112 #if defined(PR_USE_OPENSSL_ECC)
113 } else if (strcmp(hostkey_algo, "ecdsa-sha2-nistp256") == 0) {
114 pubkey_type = SFTP_KEY_ECDSA_256;
115
116 } else if (strcmp(hostkey_algo, "ecdsa-sha2-nistp384") == 0) {
117 pubkey_type = SFTP_KEY_ECDSA_384;
118
119 } else if (strcmp(hostkey_algo, "ecdsa-sha2-nistp521") == 0) {
120 pubkey_type = SFTP_KEY_ECDSA_521;
121
122 } else if (strcmp(hostkey_algo, "[email protected]") == 0) {
123 pubkey_type = SFTP_KEY_ECDSA_256_SK;
124 #endif /* PR_USE_OPENSSL_ECC */
125
126 #if defined(PR_USE_SODIUM)
127 } else if (strcmp(hostkey_algo, "ssh-ed25519") == 0) {
128 pubkey_type = SFTP_KEY_ED25519;
129
130 } else if (strcmp(hostkey_algo, "[email protected]") == 0) {
131 pubkey_type = SFTP_KEY_ED25519_SK;
132 #endif /* PR_USE_SODIUM */
133
134 #if defined(HAVE_X448_OPENSSL)
135 } else if (strcmp(hostkey_algo, "ssh-ed448") == 0) {
136 pubkey_type = SFTP_KEY_ED448;
137 #endif /* HAVE_X448_OPENSSL */
138
139 /* XXX Need to support X509v3 certs here */
140
141 } else {
142 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
143 "unsupported host key algorithm '%s' requested, rejecting request",
144 hostkey_algo);
145
146 pr_log_auth(PR_LOG_NOTICE,
147 "USER %s (Login failed): unsupported host key algorithm '%s' requested",
148 user, hostkey_algo);
149
150 *send_userauth_fail = TRUE;
151 errno = EINVAL;
152 return 0;
153 }
154
155 if (sftp_keys_verify_pubkey_type(pkt->pool, hostkey_data, hostkey_datalen,
156 pubkey_type) != TRUE) {
157 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
158 "unable to verify that given host key matches given '%s' algorithm",
159 hostkey_algo);
160
161 *send_userauth_fail = TRUE;
162 errno = EINVAL;
163 return 0;
164 }
165
166 #ifdef OPENSSL_FIPS
167 if (FIPS_mode()) {
168 # if defined(HAVE_SHA256_OPENSSL)
169 fp_algo_id = SFTP_KEYS_FP_DIGEST_SHA256;
170 fp_algo = "SHA256";
171 # else
172 fp_algo_id = SFTP_KEYS_FP_DIGEST_SHA1;
173 fp_algo = "SHA1";
174 # endif /* HAVE_SHA256_OPENSSL */
175
176 fp = sftp_keys_get_fingerprint(pkt->pool, hostkey_data, hostkey_datalen,
177 fp_algo_id);
178 if (fp != NULL) {
179 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
180 "public key %s fingerprint: %s", fp_algo, fp);
181
182 } else {
183 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
184 "error obtaining public key %s fingerprint: %s", fp_algo,
185 strerror(errno));
186 }
187
188 } else {
189 #endif /* OPENSSL_FIPS */
190 #if defined(HAVE_SHA256_OPENSSL)
191 fp_algo_id = SFTP_KEYS_FP_DIGEST_SHA256;
192 fp_algo = "SHA256";
193 #else
194 fp_algo_id = SFTP_KEYS_FP_DIGEST_MD5;
195 fp_algo = "MD5";
196 #endif /* HAVE_SHA256_OPENSSL */
197
198 fp = sftp_keys_get_fingerprint(pkt->pool, hostkey_data, hostkey_datalen,
199 fp_algo_id);
200 if (fp != NULL) {
201 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
202 "public key %s fingerprint: %s", fp_algo, fp);
203
204 } else {
205 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
206 "error obtaining public key %s fingerprint: %s", fp_algo,
207 strerror(errno));
208 }
209 #ifdef OPENSSL_FIPS
210 }
211 #endif /* OPENSSL_FIPS */
212
213 pw = pr_auth_getpwnam(pkt->pool, user);
214 if (pw == NULL) {
215 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
216 "no account for user '%s' found", user);
217
218 pr_log_auth(PR_LOG_NOTICE,
219 "USER %s: no such user found from %s [%s] to %s:%d", user,
220 session.c->remote_name, pr_netaddr_get_ipstr(session.c->remote_addr),
221 pr_netaddr_get_ipstr(session.c->local_addr), session.c->local_port);
222
223 *send_userauth_fail = TRUE;
224 errno = ENOENT;
225 return 0;
226 }
227
228 /* XXX Should we check the given FQDN here against the client's actual
229 * DNS name and/or IP address? Or leave that up to the keystore's
230 * verify_host_key() function?
231 */
232
233 if (sftp_blacklist_reject_key(pkt->pool, hostkey_data, hostkey_datalen)) {
234 pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): requested host "
235 "key is blacklisted", user);
236
237 *send_userauth_fail = TRUE;
238 errno = EACCES;
239 return 0;
240 }
241
242 /* The client signed the request as well; we need to authenticate the
243 * host with the given key now. If that succeeds, we use the signature to
244 * verify the request. And if that succeeds, then we're done authenticating.
245 */
246
247 verify_notes = pr_table_nalloc(pkt->pool, 0, 1);
248 if (sftp_keystore_verify_host_key(pkt->pool, user, host_fqdn, host_user,
249 hostkey_data, hostkey_datalen, verify_notes) < 0) {
250 pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): authentication "
251 "via '%s' host key failed", user, hostkey_algo);
252
253 *send_userauth_fail = TRUE;
254 errno = EACCES;
255 return 0;
256 }
257
258 /* Make sure the signature matches as well. */
259
260 id_len = sftp_session_get_id(&id);
261
262 /* XXX Is this buffer large enough? Too large? */
263 bufsz2 = buflen2 = 2048;
264 ptr2 = buf2 = sftp_msg_getbuf(pkt->pool, bufsz2);
265
266 sftp_msg_write_data(&buf2, &buflen2, id, id_len, TRUE);
267 sftp_msg_write_byte(&buf2, &buflen2, SFTP_SSH2_MSG_USER_AUTH_REQUEST);
268 sftp_msg_write_string(&buf2, &buflen2, orig_user);
269
270 if (sftp_interop_supports_feature(SFTP_SSH2_FEAT_SERVICE_IN_HOST_SIG)) {
271 sftp_msg_write_string(&buf2, &buflen2, service);
272
273 } else {
274 sftp_msg_write_string(&buf2, &buflen2, "ssh-userauth");
275 }
276
277 sftp_msg_write_string(&buf2, &buflen2, "hostbased");
278 sftp_msg_write_string(&buf2, &buflen2, hostkey_algo);
279 sftp_msg_write_data(&buf2, &buflen2, hostkey_data, hostkey_datalen, TRUE);
280 sftp_msg_write_string(&buf2, &buflen2, host_fqdn);
281 sftp_msg_write_string(&buf2, &buflen2, host_user_utf8);
282
283 details = pcalloc(pkt->pool, sizeof(struct sftp_verify_details));
284 if (sftp_keys_verify_signed_data(pkt->pool, hostkey_algo, hostkey_data,
285 hostkey_datalen, signature_data, signature_len, (unsigned char *) ptr2,
286 (bufsz2 - buflen2), details) < 0) {
287 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
288 "failed to verify '%s' signature on hostbased auth request for "
289 "user '%s', host %s", hostkey_algo, orig_user, host_fqdn);
290
291 pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): signature "
292 "verification of '%s' host key failed", user, hostkey_algo);
293
294 *send_userauth_fail = TRUE;
295 errno = EACCES;
296 return 0;
297 }
298
299 if (sftp_keys_permit_key(pkt->pool, hostkey_algo, orig_user, details,
300 verify_notes) < 0) {
301 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
302 "unable to permit '%s' signature on hostbased auth request for "
303 "user '%s', host %s due to security key policy", hostkey_algo, orig_user,
304 host_fqdn);
305
306 pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): signature "
307 "validation of '%s' host key failed", user, hostkey_algo);
308
309 *send_userauth_fail = TRUE;
310 errno = EACCES;
311 return 0;
312 }
313
314 /* Make sure the user is authorized to login. Normally this is checked
315 * as part of the password verification process, but in the case of
316 * hostbased authentication, there is no password to verify.
317 */
318
319 if (pr_auth_authorize(pkt->pool, user) != PR_AUTH_OK) {
320 (void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION,
321 "authentication for user '%s' failed: User not authorized", user);
322 pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): User not authorized "
323 "for login", user);
324 *send_userauth_fail = TRUE;
325 errno = EACCES;
326 return 0;
327 }
328
329 return 1;
330 }
331
332 int sftp_auth_hostbased_init(pool *p) {
333 return 0;
334 }