auth2.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /* $OpenBSD: auth2.c,v 1.158 2020/03/06 18:16:21 markus Exp $ */
  2. /*
  3. * Copyright (c) 2000 Markus Friedl. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "includes.h"
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <sys/uio.h>
  29. #include <fcntl.h>
  30. #include <limits.h>
  31. #include <pwd.h>
  32. #include <stdarg.h>
  33. #include <string.h>
  34. #include <unistd.h>
  35. #include <time.h>
  36. #include "stdlib.h"
  37. #include "atomicio.h"
  38. #include "xmalloc.h"
  39. #include "ssh2.h"
  40. #include "packet.h"
  41. #include "log.h"
  42. #include "sshbuf.h"
  43. #include "misc.h"
  44. #include "servconf.h"
  45. #include "compat.h"
  46. #include "sshkey.h"
  47. #include "hostfile.h"
  48. #include "auth.h"
  49. #include "dispatch.h"
  50. #include "pathnames.h"
  51. #include "ssherr.h"
  52. #include "canohost.h"
  53. #ifdef GSSAPI
  54. #include "ssh-gss.h"
  55. #endif
  56. #include "monitor_wrap.h"
  57. #include "digest.h"
  58. /* import */
  59. extern ServerOptions options;
  60. extern struct sshbuf *loginmsg;
  61. /* methods */
  62. extern Authmethod method_none;
  63. extern Authmethod method_pubkey;
  64. extern Authmethod method_passwd;
  65. extern Authmethod method_kbdint;
  66. extern Authmethod method_hostbased;
  67. #ifdef GSSAPI
  68. extern Authmethod method_gssapi;
  69. #endif
  70. static int log_flag = 0;
  71. Authmethod *authmethods[] = {
  72. &method_none,
  73. &method_pubkey,
  74. #ifdef GSSAPI
  75. &method_gssapi,
  76. #endif
  77. &method_passwd,
  78. &method_kbdint,
  79. &method_hostbased,
  80. NULL
  81. };
  82. /* protocol */
  83. static int input_service_request(int, u_int32_t, struct ssh *);
  84. static int input_userauth_request(int, u_int32_t, struct ssh *);
  85. /* helper */
  86. static Authmethod *authmethod_lookup(Authctxt *, const char *);
  87. static char *authmethods_get(Authctxt *authctxt);
  88. #define MATCH_NONE 0 /* method or submethod mismatch */
  89. #define MATCH_METHOD 1 /* method matches (no submethod specified) */
  90. #define MATCH_BOTH 2 /* method and submethod match */
  91. #define MATCH_PARTIAL 3 /* method matches, submethod can't be checked */
  92. static int list_starts_with(const char *, const char *, const char *);
  93. char *
  94. auth2_read_banner(void)
  95. {
  96. struct stat st;
  97. char *banner = NULL;
  98. size_t len, n;
  99. int fd;
  100. if ((fd = open(options.banner, O_RDONLY)) == -1)
  101. return (NULL);
  102. if (fstat(fd, &st) == -1) {
  103. close(fd);
  104. return (NULL);
  105. }
  106. if (st.st_size <= 0 || st.st_size > 1*1024*1024) {
  107. close(fd);
  108. return (NULL);
  109. }
  110. len = (size_t)st.st_size; /* truncate */
  111. banner = xmalloc(len + 1);
  112. n = atomicio(read, fd, banner, len);
  113. close(fd);
  114. if (n != len) {
  115. free(banner);
  116. return (NULL);
  117. }
  118. banner[n] = '\0';
  119. return (banner);
  120. }
  121. static void
  122. userauth_send_banner(struct ssh *ssh, const char *msg)
  123. {
  124. int r;
  125. if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_BANNER)) != 0 ||
  126. (r = sshpkt_put_cstring(ssh, msg)) != 0 ||
  127. (r = sshpkt_put_cstring(ssh, "")) != 0 || /* language, unused */
  128. (r = sshpkt_send(ssh)) != 0)
  129. fatal("%s: %s", __func__, ssh_err(r));
  130. debug("%s: sent", __func__);
  131. }
  132. static void
  133. userauth_banner(struct ssh *ssh)
  134. {
  135. char *banner = NULL;
  136. if (options.banner == NULL)
  137. return;
  138. if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
  139. goto done;
  140. userauth_send_banner(ssh, banner);
  141. done:
  142. free(banner);
  143. }
  144. /*
  145. * loop until authctxt->success == TRUE
  146. */
  147. void
  148. do_authentication2(struct ssh *ssh)
  149. {
  150. Authctxt *authctxt = ssh->authctxt;
  151. ssh_dispatch_init(ssh, &dispatch_protocol_error);
  152. ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_REQUEST, &input_service_request);
  153. ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt->success);
  154. ssh->authctxt = NULL;
  155. }
  156. /*ARGSUSED*/
  157. static int
  158. input_service_request(int type, u_int32_t seq, struct ssh *ssh)
  159. {
  160. Authctxt *authctxt = ssh->authctxt;
  161. char *service = NULL;
  162. int r, acceptit = 0;
  163. if ((r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 ||
  164. (r = sshpkt_get_end(ssh)) != 0)
  165. goto out;
  166. if (authctxt == NULL)
  167. fatal("input_service_request: no authctxt");
  168. if (strcmp(service, "ssh-userauth") == 0) {
  169. if (!authctxt->success) {
  170. acceptit = 1;
  171. /* now we can handle user-auth requests */
  172. ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST,
  173. &input_userauth_request);
  174. }
  175. }
  176. /* XXX all other service requests are denied */
  177. if (acceptit) {
  178. if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_ACCEPT)) != 0 ||
  179. (r = sshpkt_put_cstring(ssh, service)) != 0 ||
  180. (r = sshpkt_send(ssh)) != 0 ||
  181. (r = ssh_packet_write_wait(ssh)) != 0)
  182. goto out;
  183. } else {
  184. debug("bad service request %s", service);
  185. ssh_packet_disconnect(ssh, "bad service request %s", service);
  186. }
  187. r = 0;
  188. out:
  189. free(service);
  190. return r;
  191. }
  192. #define MIN_FAIL_DELAY_SECONDS 0.005
  193. static double
  194. user_specific_delay(const char *user)
  195. {
  196. char b[512];
  197. size_t len = ssh_digest_bytes(SSH_DIGEST_SHA512);
  198. u_char *hash = xmalloc(len);
  199. double delay;
  200. (void)snprintf(b, sizeof b, "%llu%s",
  201. (unsigned long long)options.timing_secret, user);
  202. if (ssh_digest_memory(SSH_DIGEST_SHA512, b, strlen(b), hash, len) != 0)
  203. fatal("%s: ssh_digest_memory", __func__);
  204. /* 0-4.2 ms of delay */
  205. delay = (double)PEEK_U32(hash) / 1000 / 1000 / 1000 / 1000;
  206. freezero(hash, len);
  207. debug3("%s: user specific delay %0.3lfms", __func__, delay/1000);
  208. return MIN_FAIL_DELAY_SECONDS + delay;
  209. }
  210. static void
  211. ensure_minimum_time_since(double start, double seconds)
  212. {
  213. struct timespec ts;
  214. double elapsed = monotime_double() - start, req = seconds, remain;
  215. /* if we've already passed the requested time, scale up */
  216. while ((remain = seconds - elapsed) < 0.0)
  217. seconds *= 2;
  218. ts.tv_sec = remain;
  219. ts.tv_nsec = (remain - ts.tv_sec) * 1000000000;
  220. debug3("%s: elapsed %0.3lfms, delaying %0.3lfms (requested %0.3lfms)",
  221. __func__, elapsed*1000, remain*1000, req*1000);
  222. nanosleep(&ts, NULL);
  223. }
  224. /*ARGSUSED*/
  225. static int
  226. input_userauth_request(int type, u_int32_t seq, struct ssh *ssh)
  227. {
  228. Authctxt *authctxt = ssh->authctxt;
  229. Authmethod *m = NULL;
  230. char *user = NULL, *service = NULL, *method = NULL, *style = NULL;
  231. #ifdef WITH_SELINUX
  232. char *role = NULL;
  233. #endif
  234. int r, authenticated = 0;
  235. double tstart = monotime_double();
  236. if (authctxt == NULL)
  237. fatal("input_userauth_request: no authctxt");
  238. if ((r = sshpkt_get_cstring(ssh, &user, NULL)) != 0 ||
  239. (r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 ||
  240. (r = sshpkt_get_cstring(ssh, &method, NULL)) != 0)
  241. goto out;
  242. debug("userauth-request for user %s service %s method %s", user, service, method);
  243. if (!log_flag) {
  244. logit("SSH: Server;Ltype: Authname;Remote: %s-%d;Name: %s",
  245. ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), user);
  246. log_flag = 1;
  247. }
  248. debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
  249. #ifdef WITH_SELINUX
  250. if ((role = strchr(user, '/')) != NULL)
  251. *role++ = 0;
  252. #endif
  253. if ((style = strchr(user, ':')) != NULL)
  254. *style++ = 0;
  255. if (authctxt->attempt++ == 0) {
  256. /* setup auth context */
  257. authctxt->pw = PRIVSEP(getpwnamallow(ssh, user));
  258. authctxt->user = xstrdup(user);
  259. if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
  260. authctxt->valid = 1;
  261. debug2("%s: setting up authctxt for %s",
  262. __func__, user);
  263. } else {
  264. /* Invalid user, fake password information */
  265. authctxt->pw = fakepw();
  266. }
  267. #ifdef USE_PAM
  268. if (options.use_pam)
  269. PRIVSEP(start_pam(ssh));
  270. #endif
  271. ssh_packet_set_log_preamble(ssh, "%suser %s",
  272. authctxt->valid ? "authenticating " : "invalid ", user);
  273. setproctitle("%s%s", authctxt->valid ? user : "unknown",
  274. use_privsep ? " [net]" : "");
  275. authctxt->service = xstrdup(service);
  276. authctxt->style = style ? xstrdup(style) : NULL;
  277. #ifdef WITH_SELINUX
  278. authctxt->role = role ? xstrdup(role) : NULL;
  279. #endif
  280. if (use_privsep) {
  281. mm_inform_authserv(service, style);
  282. #ifdef WITH_SELINUX
  283. mm_inform_authrole(role);
  284. #endif
  285. }
  286. userauth_banner(ssh);
  287. if (auth2_setup_methods_lists(authctxt) != 0)
  288. ssh_packet_disconnect(ssh,
  289. "no authentication methods enabled");
  290. } else if (strcmp(user, authctxt->user) != 0 ||
  291. strcmp(service, authctxt->service) != 0) {
  292. ssh_packet_disconnect(ssh, "Change of username or service "
  293. "not allowed: (%s,%s) -> (%s,%s)",
  294. authctxt->user, authctxt->service, user, service);
  295. }
  296. /* reset state */
  297. auth2_challenge_stop(ssh);
  298. #ifdef GSSAPI
  299. /* XXX move to auth2_gssapi_stop() */
  300. ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
  301. ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
  302. #endif
  303. auth2_authctxt_reset_info(authctxt);
  304. authctxt->postponed = 0;
  305. authctxt->server_caused_failure = 0;
  306. /* try to authenticate user */
  307. m = authmethod_lookup(authctxt, method);
  308. if (m != NULL && authctxt->failures < options.max_authtries) {
  309. debug2("input_userauth_request: try method %s", method);
  310. authenticated = m->userauth(ssh);
  311. }
  312. if (!authctxt->authenticated)
  313. ensure_minimum_time_since(tstart,
  314. user_specific_delay(authctxt->user));
  315. userauth_finish(ssh, authenticated, method, NULL);
  316. r = 0;
  317. out:
  318. free(service);
  319. free(user);
  320. free(method);
  321. return r;
  322. }
  323. void
  324. userauth_finish(struct ssh *ssh, int authenticated, const char *method,
  325. const char *submethod)
  326. {
  327. Authctxt *authctxt = ssh->authctxt;
  328. char *methods;
  329. int r, partial = 0;
  330. if (!authctxt->valid && authenticated)
  331. fatal("INTERNAL ERROR: authenticated invalid user %s",
  332. authctxt->user);
  333. if (authenticated && authctxt->postponed)
  334. fatal("INTERNAL ERROR: authenticated and postponed");
  335. /* Special handling for root */
  336. if (authenticated && authctxt->pw->pw_uid == 0 &&
  337. !auth_root_allowed(ssh, method)) {
  338. authenticated = 0;
  339. #ifdef SSH_AUDIT_EVENTS
  340. PRIVSEP(audit_event(ssh, SSH_LOGIN_ROOT_DENIED));
  341. #endif
  342. }
  343. if (authenticated && options.num_auth_methods != 0) {
  344. if (!auth2_update_methods_lists(authctxt, method, submethod)) {
  345. authenticated = 0;
  346. partial = 1;
  347. }
  348. }
  349. /* Log before sending the reply */
  350. auth_log(ssh, authenticated, partial, method, submethod);
  351. /* Update information exposed to session */
  352. if (authenticated || partial)
  353. auth2_update_session_info(authctxt, method, submethod);
  354. if (authctxt->postponed)
  355. return;
  356. #ifdef USE_PAM
  357. if (options.use_pam && authenticated) {
  358. int r, success = PRIVSEP(do_pam_account());
  359. /* If PAM returned a message, send it to the user. */
  360. if (sshbuf_len(loginmsg) > 0) {
  361. if ((r = sshbuf_put(loginmsg, "\0", 1)) != 0)
  362. fatal("%s: buffer error: %s",
  363. __func__, ssh_err(r));
  364. userauth_send_banner(ssh, sshbuf_ptr(loginmsg));
  365. if ((r = ssh_packet_write_wait(ssh)) != 0) {
  366. sshpkt_fatal(ssh, r,
  367. "%s: send PAM banner", __func__);
  368. }
  369. }
  370. if (!success) {
  371. fatal("Access denied for user %s by PAM account "
  372. "configuration", authctxt->user);
  373. }
  374. }
  375. #endif
  376. if (authenticated == 1) {
  377. /* turn off userauth */
  378. ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST,
  379. &dispatch_protocol_ignore);
  380. if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_SUCCESS)) != 0 ||
  381. (r = sshpkt_send(ssh)) != 0 ||
  382. (r = ssh_packet_write_wait(ssh)) != 0)
  383. fatal("%s: %s", __func__, ssh_err(r));
  384. /* now we can break out */
  385. authctxt->success = 1;
  386. ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
  387. } else {
  388. /* Allow initial try of "none" auth without failure penalty */
  389. if (!partial && !authctxt->server_caused_failure &&
  390. (authctxt->attempt > 1 || strcmp(method, "none") != 0))
  391. authctxt->failures++;
  392. if (authctxt->failures >= options.max_authtries) {
  393. #ifdef SSH_AUDIT_EVENTS
  394. PRIVSEP(audit_event(ssh, SSH_LOGIN_EXCEED_MAXTRIES));
  395. #endif
  396. auth_maxtries_exceeded(ssh);
  397. }
  398. methods = authmethods_get(authctxt);
  399. debug3("%s: failure partial=%d next methods=\"%s\"", __func__,
  400. partial, methods);
  401. if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_FAILURE)) != 0 ||
  402. (r = sshpkt_put_cstring(ssh, methods)) != 0 ||
  403. (r = sshpkt_put_u8(ssh, partial)) != 0 ||
  404. (r = sshpkt_send(ssh)) != 0 ||
  405. (r = ssh_packet_write_wait(ssh)) != 0)
  406. fatal("%s: %s", __func__, ssh_err(r));
  407. free(methods);
  408. }
  409. }
  410. /*
  411. * Checks whether method is allowed by at least one AuthenticationMethods
  412. * methods list. Returns 1 if allowed, or no methods lists configured.
  413. * 0 otherwise.
  414. */
  415. int
  416. auth2_method_allowed(Authctxt *authctxt, const char *method,
  417. const char *submethod)
  418. {
  419. u_int i;
  420. /*
  421. * NB. authctxt->num_auth_methods might be zero as a result of
  422. * auth2_setup_methods_lists(), so check the configuration.
  423. */
  424. if (options.num_auth_methods == 0)
  425. return 1;
  426. for (i = 0; i < authctxt->num_auth_methods; i++) {
  427. if (list_starts_with(authctxt->auth_methods[i], method,
  428. submethod) != MATCH_NONE)
  429. return 1;
  430. }
  431. return 0;
  432. }
  433. static char *
  434. authmethods_get(Authctxt *authctxt)
  435. {
  436. struct sshbuf *b;
  437. char *list;
  438. int i, r;
  439. if ((b = sshbuf_new()) == NULL)
  440. fatal("%s: sshbuf_new failed", __func__);
  441. for (i = 0; authmethods[i] != NULL; i++) {
  442. if (strcmp(authmethods[i]->name, "none") == 0)
  443. continue;
  444. if (authmethods[i]->enabled == NULL ||
  445. *(authmethods[i]->enabled) == 0)
  446. continue;
  447. if (!auth2_method_allowed(authctxt, authmethods[i]->name,
  448. NULL))
  449. continue;
  450. if ((r = sshbuf_putf(b, "%s%s", sshbuf_len(b) ? "," : "",
  451. authmethods[i]->name)) != 0)
  452. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  453. }
  454. if ((list = sshbuf_dup_string(b)) == NULL)
  455. fatal("%s: sshbuf_dup_string failed", __func__);
  456. sshbuf_free(b);
  457. return list;
  458. }
  459. static Authmethod *
  460. authmethod_lookup(Authctxt *authctxt, const char *name)
  461. {
  462. int i;
  463. if (name != NULL)
  464. for (i = 0; authmethods[i] != NULL; i++)
  465. if (authmethods[i]->enabled != NULL &&
  466. *(authmethods[i]->enabled) != 0 &&
  467. strcmp(name, authmethods[i]->name) == 0 &&
  468. auth2_method_allowed(authctxt,
  469. authmethods[i]->name, NULL))
  470. return authmethods[i];
  471. debug2("Unrecognized authentication method name: %s",
  472. name ? name : "NULL");
  473. return NULL;
  474. }
  475. /*
  476. * Check a comma-separated list of methods for validity. Is need_enable is
  477. * non-zero, then also require that the methods are enabled.
  478. * Returns 0 on success or -1 if the methods list is invalid.
  479. */
  480. int
  481. auth2_methods_valid(const char *_methods, int need_enable)
  482. {
  483. char *methods, *omethods, *method, *p;
  484. u_int i, found;
  485. int ret = -1;
  486. if (*_methods == '\0') {
  487. error("empty authentication method list");
  488. return -1;
  489. }
  490. omethods = methods = xstrdup(_methods);
  491. while ((method = strsep(&methods, ",")) != NULL) {
  492. for (found = i = 0; !found && authmethods[i] != NULL; i++) {
  493. if ((p = strchr(method, ':')) != NULL)
  494. *p = '\0';
  495. if (strcmp(method, authmethods[i]->name) != 0)
  496. continue;
  497. if (need_enable) {
  498. if (authmethods[i]->enabled == NULL ||
  499. *(authmethods[i]->enabled) == 0) {
  500. error("Disabled method \"%s\" in "
  501. "AuthenticationMethods list \"%s\"",
  502. method, _methods);
  503. goto out;
  504. }
  505. }
  506. found = 1;
  507. break;
  508. }
  509. if (!found) {
  510. error("Unknown authentication method \"%s\" in list",
  511. method);
  512. goto out;
  513. }
  514. }
  515. ret = 0;
  516. out:
  517. free(omethods);
  518. return ret;
  519. }
  520. /*
  521. * Prune the AuthenticationMethods supplied in the configuration, removing
  522. * any methods lists that include disabled methods. Note that this might
  523. * leave authctxt->num_auth_methods == 0, even when multiple required auth
  524. * has been requested. For this reason, all tests for whether multiple is
  525. * enabled should consult options.num_auth_methods directly.
  526. */
  527. int
  528. auth2_setup_methods_lists(Authctxt *authctxt)
  529. {
  530. u_int i;
  531. /* First, normalise away the "any" pseudo-method */
  532. if (options.num_auth_methods == 1 &&
  533. strcmp(options.auth_methods[0], "any") == 0) {
  534. free(options.auth_methods[0]);
  535. options.auth_methods[0] = NULL;
  536. options.num_auth_methods = 0;
  537. }
  538. if (options.num_auth_methods == 0)
  539. return 0;
  540. debug3("%s: checking methods", __func__);
  541. authctxt->auth_methods = xcalloc(options.num_auth_methods,
  542. sizeof(*authctxt->auth_methods));
  543. authctxt->num_auth_methods = 0;
  544. for (i = 0; i < options.num_auth_methods; i++) {
  545. if (auth2_methods_valid(options.auth_methods[i], 1) != 0) {
  546. logit("Authentication methods list \"%s\" contains "
  547. "disabled method, skipping",
  548. options.auth_methods[i]);
  549. continue;
  550. }
  551. debug("authentication methods list %d: %s",
  552. authctxt->num_auth_methods, options.auth_methods[i]);
  553. authctxt->auth_methods[authctxt->num_auth_methods++] =
  554. xstrdup(options.auth_methods[i]);
  555. }
  556. if (authctxt->num_auth_methods == 0) {
  557. error("No AuthenticationMethods left after eliminating "
  558. "disabled methods");
  559. return -1;
  560. }
  561. return 0;
  562. }
  563. static int
  564. list_starts_with(const char *methods, const char *method,
  565. const char *submethod)
  566. {
  567. size_t l = strlen(method);
  568. int match;
  569. const char *p;
  570. if (strncmp(methods, method, l) != 0)
  571. return MATCH_NONE;
  572. p = methods + l;
  573. match = MATCH_METHOD;
  574. if (*p == ':') {
  575. if (!submethod)
  576. return MATCH_PARTIAL;
  577. l = strlen(submethod);
  578. p += 1;
  579. if (strncmp(submethod, p, l))
  580. return MATCH_NONE;
  581. p += l;
  582. match = MATCH_BOTH;
  583. }
  584. if (*p != ',' && *p != '\0')
  585. return MATCH_NONE;
  586. return match;
  587. }
  588. /*
  589. * Remove method from the start of a comma-separated list of methods.
  590. * Returns 0 if the list of methods did not start with that method or 1
  591. * if it did.
  592. */
  593. static int
  594. remove_method(char **methods, const char *method, const char *submethod)
  595. {
  596. char *omethods = *methods, *p;
  597. size_t l = strlen(method);
  598. int match;
  599. match = list_starts_with(omethods, method, submethod);
  600. if (match != MATCH_METHOD && match != MATCH_BOTH)
  601. return 0;
  602. p = omethods + l;
  603. if (submethod && match == MATCH_BOTH)
  604. p += 1 + strlen(submethod); /* include colon */
  605. if (*p == ',')
  606. p++;
  607. *methods = xstrdup(p);
  608. free(omethods);
  609. return 1;
  610. }
  611. /*
  612. * Called after successful authentication. Will remove the successful method
  613. * from the start of each list in which it occurs. If it was the last method
  614. * in any list, then authentication is deemed successful.
  615. * Returns 1 if the method completed any authentication list or 0 otherwise.
  616. */
  617. int
  618. auth2_update_methods_lists(Authctxt *authctxt, const char *method,
  619. const char *submethod)
  620. {
  621. u_int i, found = 0;
  622. debug3("%s: updating methods list after \"%s\"", __func__, method);
  623. for (i = 0; i < authctxt->num_auth_methods; i++) {
  624. if (!remove_method(&(authctxt->auth_methods[i]), method,
  625. submethod))
  626. continue;
  627. found = 1;
  628. if (*authctxt->auth_methods[i] == '\0') {
  629. debug2("authentication methods list %d complete", i);
  630. return 1;
  631. }
  632. debug3("authentication methods list %d remaining: \"%s\"",
  633. i, authctxt->auth_methods[i]);
  634. }
  635. /* This should not happen, but would be bad if it did */
  636. if (!found)
  637. fatal("%s: method not in AuthenticationMethods", __func__);
  638. return 0;
  639. }
  640. /* Reset method-specific information */
  641. void auth2_authctxt_reset_info(Authctxt *authctxt)
  642. {
  643. sshkey_free(authctxt->auth_method_key);
  644. free(authctxt->auth_method_info);
  645. authctxt->auth_method_key = NULL;
  646. authctxt->auth_method_info = NULL;
  647. }
  648. /* Record auth method-specific information for logs */
  649. void
  650. auth2_record_info(Authctxt *authctxt, const char *fmt, ...)
  651. {
  652. va_list ap;
  653. int i;
  654. free(authctxt->auth_method_info);
  655. authctxt->auth_method_info = NULL;
  656. va_start(ap, fmt);
  657. i = vasprintf(&authctxt->auth_method_info, fmt, ap);
  658. va_end(ap);
  659. if (i == -1)
  660. fatal("%s: vasprintf failed", __func__);
  661. }
  662. /*
  663. * Records a public key used in authentication. This is used for logging
  664. * and to ensure that the same key is not subsequently accepted again for
  665. * multiple authentication.
  666. */
  667. void
  668. auth2_record_key(Authctxt *authctxt, int authenticated,
  669. const struct sshkey *key)
  670. {
  671. struct sshkey **tmp, *dup;
  672. int r;
  673. if ((r = sshkey_from_private(key, &dup)) != 0)
  674. fatal("%s: copy key: %s", __func__, ssh_err(r));
  675. sshkey_free(authctxt->auth_method_key);
  676. authctxt->auth_method_key = dup;
  677. if (!authenticated)
  678. return;
  679. /* If authenticated, make sure we don't accept this key again */
  680. if ((r = sshkey_from_private(key, &dup)) != 0)
  681. fatal("%s: copy key: %s", __func__, ssh_err(r));
  682. if (authctxt->nprev_keys >= INT_MAX ||
  683. (tmp = recallocarray(authctxt->prev_keys, authctxt->nprev_keys,
  684. authctxt->nprev_keys + 1, sizeof(*authctxt->prev_keys))) == NULL)
  685. fatal("%s: reallocarray failed", __func__);
  686. authctxt->prev_keys = tmp;
  687. authctxt->prev_keys[authctxt->nprev_keys] = dup;
  688. authctxt->nprev_keys++;
  689. }
  690. /* Checks whether a key has already been previously used for authentication */
  691. int
  692. auth2_key_already_used(Authctxt *authctxt, const struct sshkey *key)
  693. {
  694. u_int i;
  695. char *fp;
  696. for (i = 0; i < authctxt->nprev_keys; i++) {
  697. if (sshkey_equal_public(key, authctxt->prev_keys[i])) {
  698. fp = sshkey_fingerprint(authctxt->prev_keys[i],
  699. options.fingerprint_hash, SSH_FP_DEFAULT);
  700. debug3("%s: key already used: %s %s", __func__,
  701. sshkey_type(authctxt->prev_keys[i]),
  702. fp == NULL ? "UNKNOWN" : fp);
  703. free(fp);
  704. return 1;
  705. }
  706. }
  707. return 0;
  708. }
  709. /*
  710. * Updates authctxt->session_info with details of authentication. Should be
  711. * whenever an authentication method succeeds.
  712. */
  713. void
  714. auth2_update_session_info(Authctxt *authctxt, const char *method,
  715. const char *submethod)
  716. {
  717. int r;
  718. if (authctxt->session_info == NULL) {
  719. if ((authctxt->session_info = sshbuf_new()) == NULL)
  720. fatal("%s: sshbuf_new", __func__);
  721. }
  722. /* Append method[/submethod] */
  723. if ((r = sshbuf_putf(authctxt->session_info, "%s%s%s",
  724. method, submethod == NULL ? "" : "/",
  725. submethod == NULL ? "" : submethod)) != 0)
  726. fatal("%s: append method: %s", __func__, ssh_err(r));
  727. /* Append key if present */
  728. if (authctxt->auth_method_key != NULL) {
  729. if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 ||
  730. (r = sshkey_format_text(authctxt->auth_method_key,
  731. authctxt->session_info)) != 0)
  732. fatal("%s: append key: %s", __func__, ssh_err(r));
  733. }
  734. if (authctxt->auth_method_info != NULL) {
  735. /* Ensure no ambiguity here */
  736. if (strchr(authctxt->auth_method_info, '\n') != NULL)
  737. fatal("%s: auth_method_info contains \\n", __func__);
  738. if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 ||
  739. (r = sshbuf_putf(authctxt->session_info, "%s",
  740. authctxt->auth_method_info)) != 0) {
  741. fatal("%s: append method info: %s",
  742. __func__, ssh_err(r));
  743. }
  744. }
  745. if ((r = sshbuf_put_u8(authctxt->session_info, '\n')) != 0)
  746. fatal("%s: append: %s", __func__, ssh_err(r));
  747. }