| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include "mod_sftp.h" |
| 26 |
#include "keystore.h" |
| 27 |
#include "rfc4716.h" |
| 28 |
|
| 29 |
extern int ServerUseReverseDNS; |
| 30 |
|
| 31 |
struct sftp_keystore_store { |
| 32 |
struct sftp_keystore_store *prev, *next; |
| 33 |
|
| 34 |
const char *store_type; |
| 35 |
sftp_keystore_t *(*store_open)(pool *, int, const char *, const char *); |
| 36 |
unsigned int store_ktypes; |
| 37 |
}; |
| 38 |
|
| 39 |
static pool *keystore_pool = NULL; |
| 40 |
static struct sftp_keystore_store *keystore_stores = NULL; |
| 41 |
static unsigned int keystore_nstores = 0; |
| 42 |
|
| 43 |
static const char *trace_channel = "ssh2"; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
static int rfc4716_header_keycmp(const void *key1, size_t keysz1, |
| 53 |
const void *key2, size_t keysz2) { |
| 54 |
const char *k1, *k2; |
| 55 |
|
| 56 |
if (keysz1 != keysz2) { |
| 57 |
return keysz1 < keysz2 ? -1 : 1; |
| 58 |
} |
| 59 |
|
| 60 |
k1 = key1; |
| 61 |
k2 = key2; |
| 62 |
|
| 63 |
return strncmp(k1, k2, keysz1); |
| 64 |
} |
| 65 |
|
| 66 |
static void prepare_verify_notes(pr_table_t *notes) { |
| 67 |
int res; |
| 68 |
|
| 69 |
res = pr_table_ctl(notes, PR_TABLE_CTL_SET_KEY_CMP, rfc4716_header_keycmp); |
| 70 |
if (res < 0) { |
| 71 |
pr_trace_msg(trace_channel, 1, |
| 72 |
"error setting case-insensitive comparator on notes: %s", |
| 73 |
strerror(errno)); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
static struct sftp_keystore_store *keystore_get_store(const char *store_type, |
| 80 |
unsigned int ktypes) { |
| 81 |
struct sftp_keystore_store *store; |
| 82 |
|
| 83 |
for (store = keystore_stores; store; store = store->next) { |
| 84 |
pr_signals_handle(); |
| 85 |
|
| 86 |
if ((store->store_ktypes & ktypes) && |
| 87 |
strcmp(store->store_type, store_type) == 0) { |
| 88 |
return store; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
errno = ENOENT; |
| 93 |
return NULL; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
int sftp_keystore_register_store(const char *store_type, |
| 99 |
sftp_keystore_t *(*store_open)(pool *, int, const char *, const char *), |
| 100 |
unsigned int store_ktypes) { |
| 101 |
struct sftp_keystore_store *store; |
| 102 |
|
| 103 |
if (store_type == NULL || |
| 104 |
store_open == NULL) { |
| 105 |
errno = EINVAL; |
| 106 |
return -1; |
| 107 |
} |
| 108 |
|
| 109 |
if (keystore_pool == NULL) { |
| 110 |
keystore_pool = make_sub_pool(permanent_pool); |
| 111 |
pr_pool_tag(keystore_pool, "SFTP Keystore Pool"); |
| 112 |
} |
| 113 |
|
| 114 |
store = keystore_get_store(store_type, store_ktypes); |
| 115 |
if (store != NULL) { |
| 116 |
errno = EEXIST; |
| 117 |
return -1; |
| 118 |
} |
| 119 |
|
| 120 |
store = pcalloc(keystore_pool, sizeof(struct sftp_keystore_store)); |
| 121 |
store->store_type = pstrdup(keystore_pool, store_type); |
| 122 |
store->store_open = store_open; |
| 123 |
store->store_ktypes = store_ktypes; |
| 124 |
|
| 125 |
store->next = keystore_stores; |
| 126 |
keystore_stores = store; |
| 127 |
keystore_nstores++; |
| 128 |
|
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
int sftp_keystore_unregister_store(const char *store_type, |
| 133 |
unsigned int store_ktypes) { |
| 134 |
struct sftp_keystore_store *store; |
| 135 |
|
| 136 |
if (store_type == NULL) { |
| 137 |
errno = EINVAL; |
| 138 |
return -1; |
| 139 |
} |
| 140 |
|
| 141 |
store = keystore_get_store(store_type, store_ktypes); |
| 142 |
if (store == NULL) { |
| 143 |
errno = ENOENT; |
| 144 |
return -1; |
| 145 |
} |
| 146 |
|
| 147 |
if (store->prev != NULL) { |
| 148 |
store->prev->next = store->next; |
| 149 |
|
| 150 |
} else { |
| 151 |
keystore_stores = store->next; |
| 152 |
} |
| 153 |
|
| 154 |
if (store->next != NULL) { |
| 155 |
store->next->prev = store->prev; |
| 156 |
} |
| 157 |
|
| 158 |
store->prev = store->next = NULL; |
| 159 |
keystore_nstores--; |
| 160 |
|
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
int sftp_keystore_init(void) { |
| 165 |
|
| 166 |
sftp_rfc4716_init(); |
| 167 |
|
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
int sftp_keystore_free(void) { |
| 172 |
sftp_rfc4716_free(); |
| 173 |
|
| 174 |
return 0; |
| 175 |
} |
| 176 |
|
| 177 |
int sftp_keystore_supports_store(const char *store_type, |
| 178 |
unsigned int store_ktype) { |
| 179 |
struct sftp_keystore_store *store; |
| 180 |
|
| 181 |
store = keystore_get_store(store_type, store_ktype); |
| 182 |
if (store != NULL) { |
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
errno = ENOENT; |
| 187 |
return -1; |
| 188 |
} |
| 189 |
|
| 190 |
int sftp_keystore_verify_host_key(pool *p, const char *user, |
| 191 |
const char *host_fqdn, const char *host_user, unsigned char *key_data, |
| 192 |
uint32_t key_len, pr_table_t *notes) { |
| 193 |
register unsigned int i; |
| 194 |
int res = -1; |
| 195 |
config_rec *c; |
| 196 |
|
| 197 |
if (host_fqdn == NULL || |
| 198 |
host_user == NULL || |
| 199 |
key_data == NULL || |
| 200 |
key_len == 0) { |
| 201 |
errno = EINVAL; |
| 202 |
return -1; |
| 203 |
} |
| 204 |
|
| 205 |
c = find_config(main_server->conf, CONF_PARAM, "SFTPAuthorizedHostKeys", |
| 206 |
FALSE); |
| 207 |
if (c == NULL) { |
| 208 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 209 |
"no SFTPAuthorizedHostKeys configured"); |
| 210 |
errno = EPERM; |
| 211 |
return -1; |
| 212 |
} |
| 213 |
|
| 214 |
if (ServerUseReverseDNS) { |
| 215 |
if (strcasecmp(host_fqdn, pr_netaddr_get_dnsstr(session.c->remote_addr)) != 0) { |
| 216 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 217 |
"client-sent FQDN '%s' DOES NOT match client DNS name '%s'", host_fqdn, |
| 218 |
pr_netaddr_get_dnsstr(session.c->remote_addr)); |
| 219 |
errno = EACCES; |
| 220 |
return -1; |
| 221 |
|
| 222 |
} else { |
| 223 |
pr_trace_msg(trace_channel, 9, "client-sent FQDN '%s' matches client " |
| 224 |
"DNS name '%s'", host_fqdn, |
| 225 |
pr_netaddr_get_dnsstr(session.c->remote_addr)); |
| 226 |
} |
| 227 |
|
| 228 |
} else { |
| 229 |
pr_trace_msg(trace_channel, 1, "unable to double-check client-sent " |
| 230 |
"FQDN '%s' against DNS: UseReverseDNS is off", host_fqdn); |
| 231 |
} |
| 232 |
|
| 233 |
for (i = 0; i < c->argc; i++) { |
| 234 |
struct sftp_keystore_store *sks; |
| 235 |
char *store_type, *ptr; |
| 236 |
|
| 237 |
res = -1; |
| 238 |
pr_signals_handle(); |
| 239 |
|
| 240 |
store_type = c->argv[i]; |
| 241 |
|
| 242 |
pr_trace_msg(trace_channel, 2, |
| 243 |
"using SFTPAuthorizedHostKeys '%s' for public key authentication for " |
| 244 |
"user '%s', host %s", store_type, user, host_fqdn); |
| 245 |
|
| 246 |
ptr = strchr(store_type, ':'); |
| 247 |
if (ptr == NULL) { |
| 248 |
pr_trace_msg(trace_channel, 2, |
| 249 |
"skipping badly formatted SFTPAuthorizedHostKeys '%s'", store_type); |
| 250 |
continue; |
| 251 |
} |
| 252 |
|
| 253 |
*ptr = '\0'; |
| 254 |
|
| 255 |
sks = keystore_get_store(store_type, SFTP_SSH2_HOST_KEY_STORE); |
| 256 |
if (sks != NULL) { |
| 257 |
sftp_keystore_t *store; |
| 258 |
|
| 259 |
store = (sks->store_open)(p, SFTP_SSH2_HOST_KEY_STORE, ptr + 1, user); |
| 260 |
if (store != NULL) { |
| 261 |
if (store->verify_host_key != NULL) { |
| 262 |
int xerrno; |
| 263 |
|
| 264 |
prepare_verify_notes(notes); |
| 265 |
res = (store->verify_host_key)(store, p, user, host_fqdn, host_user, |
| 266 |
key_data, key_len, notes); |
| 267 |
xerrno = errno; |
| 268 |
(store->store_close)(store); |
| 269 |
|
| 270 |
*ptr = ':'; |
| 271 |
if (res == 0) { |
| 272 |
break; |
| 273 |
} |
| 274 |
|
| 275 |
pr_trace_msg(trace_channel, 3, |
| 276 |
"error verifying host key for host '%s', user '%s' ('%s'): %s", |
| 277 |
host_fqdn, user, host_user, strerror(xerrno)); |
| 278 |
if (xerrno == ENOENT) { |
| 279 |
pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 280 |
"no matching public key found for host '%s' in '%s'; perhaps " |
| 281 |
"keys are not RFC4716-formatted", host_fqdn, ptr + 1); |
| 282 |
} |
| 283 |
|
| 284 |
} else { |
| 285 |
*ptr = ':'; |
| 286 |
pr_trace_msg(trace_channel, 7, |
| 287 |
"error using SFTPAuthorizedHostKeys '%s': %s", store_type, |
| 288 |
strerror(ENOSYS)); |
| 289 |
} |
| 290 |
|
| 291 |
continue; |
| 292 |
|
| 293 |
} else { |
| 294 |
*ptr = ':'; |
| 295 |
pr_trace_msg(trace_channel, 7, |
| 296 |
"error opening SFTPAuthorizedHostKeys '%s': %s", store_type, |
| 297 |
strerror(errno)); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
*ptr = ':'; |
| 302 |
} |
| 303 |
|
| 304 |
if (res == 0) { |
| 305 |
pr_trace_msg(trace_channel, 8, |
| 306 |
"verified host public key for user '%s', host '%s'", user, host_fqdn); |
| 307 |
return res; |
| 308 |
} |
| 309 |
|
| 310 |
errno = EACCES; |
| 311 |
return -1; |
| 312 |
} |
| 313 |
|
| 314 |
int sftp_keystore_verify_user_key(pool *p, const char *user, |
| 315 |
unsigned char *key_data, uint32_t key_len, pr_table_t *notes) { |
| 316 |
register unsigned int i; |
| 317 |
int res = -1; |
| 318 |
config_rec *c; |
| 319 |
|
| 320 |
if (key_data == NULL || |
| 321 |
key_len == 0) { |
| 322 |
errno = EINVAL; |
| 323 |
return -1; |
| 324 |
} |
| 325 |
|
| 326 |
c = find_config(main_server->conf, CONF_PARAM, "SFTPAuthorizedUserKeys", |
| 327 |
FALSE); |
| 328 |
if (c == NULL) { |
| 329 |
(void) pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 330 |
"no SFTPAuthorizedUserKeys configured"); |
| 331 |
errno = EPERM; |
| 332 |
return -1; |
| 333 |
} |
| 334 |
|
| 335 |
for (i = 0; i < c->argc; i++) { |
| 336 |
struct sftp_keystore_store *sks; |
| 337 |
const char *path, *sess_user; |
| 338 |
char *store_type, *ptr; |
| 339 |
|
| 340 |
pr_signals_handle(); |
| 341 |
|
| 342 |
res = -1; |
| 343 |
store_type = c->argv[i]; |
| 344 |
|
| 345 |
ptr = strchr(store_type, ':'); |
| 346 |
if (ptr == NULL) { |
| 347 |
pr_trace_msg(trace_channel, 2, |
| 348 |
"skipping badly formatted SFTPAuthorizedUserKeys '%s'", store_type); |
| 349 |
continue; |
| 350 |
} |
| 351 |
|
| 352 |
*ptr = '\0'; |
| 353 |
path = ptr + 1; |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
sess_user = session.user; |
| 361 |
session.user = user; |
| 362 |
path = path_subst_uservar(p, &path); |
| 363 |
session.user = sess_user; |
| 364 |
|
| 365 |
pr_trace_msg(trace_channel, 2, |
| 366 |
"using SFTPAuthorizedUserKeys '%s:%s' for public key authentication for " |
| 367 |
"user '%s'", store_type, path, user); |
| 368 |
|
| 369 |
sks = keystore_get_store(store_type, SFTP_SSH2_USER_KEY_STORE); |
| 370 |
if (sks != NULL) { |
| 371 |
sftp_keystore_t *store; |
| 372 |
|
| 373 |
store = (sks->store_open)(p, SFTP_SSH2_USER_KEY_STORE, path, user); |
| 374 |
if (store != NULL) { |
| 375 |
if (store->verify_user_key != NULL) { |
| 376 |
int xerrno; |
| 377 |
|
| 378 |
prepare_verify_notes(notes); |
| 379 |
res = (store->verify_user_key)(store, p, user, key_data, key_len, |
| 380 |
notes); |
| 381 |
xerrno = errno; |
| 382 |
(store->store_close)(store); |
| 383 |
|
| 384 |
*ptr = ':'; |
| 385 |
if (res == 0) { |
| 386 |
break; |
| 387 |
} |
| 388 |
|
| 389 |
pr_trace_msg(trace_channel, 3, |
| 390 |
"error verifying user key for user '%s': %s", user, |
| 391 |
strerror(xerrno)); |
| 392 |
if (xerrno == ENOENT) { |
| 393 |
pr_log_writefile(sftp_logfd, MOD_SFTP_VERSION, |
| 394 |
"no matching public key found for user '%s' in '%s'; perhaps " |
| 395 |
"keys are not RFC4716-formatted", user, path); |
| 396 |
} |
| 397 |
|
| 398 |
} else { |
| 399 |
*ptr = ':'; |
| 400 |
pr_trace_msg(trace_channel, 7, |
| 401 |
"error using SFTPAuthorizedUserKeys '%s': %s", store_type, |
| 402 |
strerror(ENOSYS)); |
| 403 |
} |
| 404 |
|
| 405 |
continue; |
| 406 |
|
| 407 |
} else { |
| 408 |
*ptr = ':'; |
| 409 |
pr_trace_msg(trace_channel, 7, |
| 410 |
"error opening SFTPAuthorizedUserKeys '%s': %s", store_type, |
| 411 |
strerror(errno)); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
*ptr = ':'; |
| 416 |
} |
| 417 |
|
| 418 |
if (res == 0) { |
| 419 |
pr_trace_msg(trace_channel, 8, |
| 420 |
"verified public key for user '%s'", user); |
| 421 |
return res; |
| 422 |
} |
| 423 |
|
| 424 |
errno = EACCES; |
| 425 |
return -1; |
| 426 |
} |