auth-bsdauth.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* $OpenBSD: auth-bsdauth.c,v 1.15 2018/07/09 21:35:50 markus Exp $ */
  2. /*
  3. * Copyright (c) 2001 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 <stdarg.h>
  28. #include <stdio.h>
  29. #ifdef BSD_AUTH
  30. #include "xmalloc.h"
  31. #include "sshkey.h"
  32. #include "sshbuf.h"
  33. #include "hostfile.h"
  34. #include "auth.h"
  35. #include "log.h"
  36. #ifdef GSSAPI
  37. #include "ssh-gss.h"
  38. #endif
  39. #include "monitor_wrap.h"
  40. static void *
  41. bsdauth_init_ctx(Authctxt *authctxt)
  42. {
  43. return authctxt;
  44. }
  45. int
  46. bsdauth_query(void *ctx, char **name, char **infotxt,
  47. u_int *numprompts, char ***prompts, u_int **echo_on)
  48. {
  49. Authctxt *authctxt = ctx;
  50. char *challenge = NULL;
  51. *infotxt = NULL;
  52. *numprompts = 0;
  53. *prompts = NULL;
  54. *echo_on = NULL;
  55. if (authctxt->as != NULL) {
  56. debug2("bsdauth_query: try reuse session");
  57. challenge = auth_getitem(authctxt->as, AUTHV_CHALLENGE);
  58. if (challenge == NULL) {
  59. auth_close(authctxt->as);
  60. authctxt->as = NULL;
  61. }
  62. }
  63. if (challenge == NULL) {
  64. debug2("bsdauth_query: new bsd auth session");
  65. debug3("bsdauth_query: style %s",
  66. authctxt->style ? authctxt->style : "<default>");
  67. authctxt->as = auth_userchallenge(authctxt->user,
  68. authctxt->style, "auth-ssh", &challenge);
  69. if (authctxt->as == NULL)
  70. challenge = NULL;
  71. debug2("bsdauth_query: <%s>", challenge ? challenge : "empty");
  72. }
  73. if (challenge == NULL)
  74. return -1;
  75. *name = xstrdup("");
  76. *infotxt = xstrdup("");
  77. *numprompts = 1;
  78. *prompts = xcalloc(*numprompts, sizeof(char *));
  79. *echo_on = xcalloc(*numprompts, sizeof(u_int));
  80. (*prompts)[0] = xstrdup(challenge);
  81. return 0;
  82. }
  83. int
  84. bsdauth_respond(void *ctx, u_int numresponses, char **responses)
  85. {
  86. Authctxt *authctxt = ctx;
  87. int authok;
  88. if (!authctxt->valid)
  89. return -1;
  90. if (authctxt->as == NULL)
  91. error("bsdauth_respond: no bsd auth session");
  92. if (numresponses != 1)
  93. return -1;
  94. authok = auth_userresponse(authctxt->as, responses[0], 0);
  95. authctxt->as = NULL;
  96. debug3("bsdauth_respond: <%s> = <%d>", responses[0], authok);
  97. return (authok == 0) ? -1 : 0;
  98. }
  99. static void
  100. bsdauth_free_ctx(void *ctx)
  101. {
  102. Authctxt *authctxt = ctx;
  103. if (authctxt && authctxt->as) {
  104. auth_close(authctxt->as);
  105. authctxt->as = NULL;
  106. }
  107. }
  108. KbdintDevice bsdauth_device = {
  109. "bsdauth",
  110. bsdauth_init_ctx,
  111. bsdauth_query,
  112. bsdauth_respond,
  113. bsdauth_free_ctx
  114. };
  115. KbdintDevice mm_bsdauth_device = {
  116. "bsdauth",
  117. bsdauth_init_ctx,
  118. mm_bsdauth_query,
  119. mm_bsdauth_respond,
  120. bsdauth_free_ctx
  121. };
  122. #endif