ssh2userauth-server.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Packet protocol layer for the server side of the SSH-2 userauth
  3. * protocol (RFC 4252).
  4. */
  5. #include <assert.h>
  6. #include "putty.h"
  7. #include "ssh.h"
  8. #include "sshbpp.h"
  9. #include "sshppl.h"
  10. #include "sshcr.h"
  11. #include "sshserver.h"
  12. #ifndef NO_GSSAPI
  13. #include "sshgssc.h"
  14. #include "sshgss.h"
  15. #endif
  16. struct ssh2_userauth_server_state {
  17. int crState;
  18. PacketProtocolLayer *transport_layer, *successor_layer;
  19. ptrlen session_id;
  20. AuthPolicy *authpolicy;
  21. const SshServerConfig *ssc;
  22. ptrlen username, service, method;
  23. unsigned methods, this_method;
  24. bool partial_success;
  25. AuthKbdInt *aki;
  26. PacketProtocolLayer ppl;
  27. };
  28. static void ssh2_userauth_server_free(PacketProtocolLayer *);
  29. static void ssh2_userauth_server_process_queue(PacketProtocolLayer *);
  30. static const PacketProtocolLayerVtable ssh2_userauth_server_vtable = {
  31. .free = ssh2_userauth_server_free,
  32. .process_queue = ssh2_userauth_server_process_queue,
  33. .queued_data_size = ssh_ppl_default_queued_data_size,
  34. .name = "ssh-userauth",
  35. /* other methods are NULL */
  36. };
  37. static void free_auth_kbdint(AuthKbdInt *aki)
  38. {
  39. int i;
  40. if (!aki)
  41. return;
  42. sfree(aki->title);
  43. sfree(aki->instruction);
  44. for (i = 0; i < aki->nprompts; i++)
  45. sfree(aki->prompts[i].prompt);
  46. sfree(aki->prompts);
  47. sfree(aki);
  48. }
  49. PacketProtocolLayer *ssh2_userauth_server_new(
  50. PacketProtocolLayer *successor_layer, AuthPolicy *authpolicy,
  51. const SshServerConfig *ssc)
  52. {
  53. struct ssh2_userauth_server_state *s =
  54. snew(struct ssh2_userauth_server_state);
  55. memset(s, 0, sizeof(*s));
  56. s->ppl.vt = &ssh2_userauth_server_vtable;
  57. s->successor_layer = successor_layer;
  58. s->authpolicy = authpolicy;
  59. s->ssc = ssc;
  60. return &s->ppl;
  61. }
  62. void ssh2_userauth_server_set_transport_layer(PacketProtocolLayer *userauth,
  63. PacketProtocolLayer *transport)
  64. {
  65. struct ssh2_userauth_server_state *s =
  66. container_of(userauth, struct ssh2_userauth_server_state, ppl);
  67. s->transport_layer = transport;
  68. }
  69. static void ssh2_userauth_server_free(PacketProtocolLayer *ppl)
  70. {
  71. struct ssh2_userauth_server_state *s =
  72. container_of(ppl, struct ssh2_userauth_server_state, ppl);
  73. if (s->successor_layer)
  74. ssh_ppl_free(s->successor_layer);
  75. free_auth_kbdint(s->aki);
  76. sfree(s);
  77. }
  78. static PktIn *ssh2_userauth_server_pop(struct ssh2_userauth_server_state *s)
  79. {
  80. return pq_pop(s->ppl.in_pq);
  81. }
  82. static void ssh2_userauth_server_add_session_id(
  83. struct ssh2_userauth_server_state *s, strbuf *sigdata)
  84. {
  85. if (s->ppl.remote_bugs & BUG_SSH2_PK_SESSIONID) {
  86. put_datapl(sigdata, s->session_id);
  87. } else {
  88. put_stringpl(sigdata, s->session_id);
  89. }
  90. }
  91. static void ssh2_userauth_server_process_queue(PacketProtocolLayer *ppl)
  92. {
  93. struct ssh2_userauth_server_state *s =
  94. container_of(ppl, struct ssh2_userauth_server_state, ppl);
  95. PktIn *pktin;
  96. PktOut *pktout;
  97. crBegin(s->crState);
  98. s->session_id = ssh2_transport_get_session_id(s->transport_layer);
  99. if (s->ssc->banner.ptr) {
  100. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_USERAUTH_BANNER);
  101. put_stringpl(pktout, s->ssc->banner);
  102. put_stringz(pktout, ""); /* language tag */
  103. pq_push(s->ppl.out_pq, pktout);
  104. }
  105. while (1) {
  106. crMaybeWaitUntilV((pktin = ssh2_userauth_server_pop(s)) != NULL);
  107. if (pktin->type != SSH2_MSG_USERAUTH_REQUEST) {
  108. ssh_proto_error(s->ppl.ssh, "Received unexpected packet when "
  109. "expecting USERAUTH_REQUEST, type %d (%s)",
  110. pktin->type,
  111. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  112. s->ppl.bpp->pls->actx, pktin->type));
  113. return;
  114. }
  115. s->username = get_string(pktin);
  116. s->service = get_string(pktin);
  117. s->method = get_string(pktin);
  118. if (!ptrlen_eq_string(s->service, s->successor_layer->vt->name)) {
  119. /*
  120. * Unconditionally reject authentication for any service
  121. * other than the one we're going to hand over to.
  122. */
  123. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_USERAUTH_FAILURE);
  124. put_stringz(pktout, "");
  125. put_bool(pktout, false);
  126. pq_push(s->ppl.out_pq, pktout);
  127. continue;
  128. }
  129. s->methods = auth_methods(s->authpolicy);
  130. s->partial_success = false;
  131. if (ptrlen_eq_string(s->method, "none")) {
  132. s->this_method = AUTHMETHOD_NONE;
  133. if (!(s->methods & s->this_method))
  134. goto failure;
  135. if (!auth_none(s->authpolicy, s->username))
  136. goto failure;
  137. } else if (ptrlen_eq_string(s->method, "password")) {
  138. bool changing;
  139. ptrlen password, new_password, *new_password_ptr;
  140. s->this_method = AUTHMETHOD_PASSWORD;
  141. if (!(s->methods & s->this_method))
  142. goto failure;
  143. changing = get_bool(pktin);
  144. password = get_string(pktin);
  145. if (changing) {
  146. new_password = get_string(pktin);
  147. new_password_ptr = &new_password;
  148. } else {
  149. new_password_ptr = NULL;
  150. }
  151. int result = auth_password(s->authpolicy, s->username,
  152. password, new_password_ptr);
  153. if (result == 2) {
  154. pktout = ssh_bpp_new_pktout(
  155. s->ppl.bpp, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ);
  156. put_stringz(pktout, "Please change your password");
  157. put_stringz(pktout, ""); /* language tag */
  158. pq_push(s->ppl.out_pq, pktout);
  159. continue; /* skip USERAUTH_{SUCCESS,FAILURE} epilogue */
  160. } else if (result != 1) {
  161. goto failure;
  162. }
  163. } else if (ptrlen_eq_string(s->method, "publickey")) {
  164. bool has_signature, success, send_pk_ok, key_really_ok;
  165. ptrlen algorithm, blob, signature;
  166. const ssh_keyalg *keyalg;
  167. ssh_key *key;
  168. strbuf *sigdata;
  169. s->this_method = AUTHMETHOD_PUBLICKEY;
  170. if (!(s->methods & s->this_method))
  171. goto failure;
  172. has_signature = get_bool(pktin);
  173. algorithm = get_string(pktin);
  174. blob = get_string(pktin);
  175. key_really_ok = auth_publickey(s->authpolicy, s->username, blob);
  176. send_pk_ok = key_really_ok ||
  177. s->ssc->stunt_pretend_to_accept_any_pubkey;
  178. if (!has_signature) {
  179. if (!send_pk_ok)
  180. goto failure;
  181. pktout = ssh_bpp_new_pktout(
  182. s->ppl.bpp, SSH2_MSG_USERAUTH_PK_OK);
  183. put_stringpl(pktout, algorithm);
  184. put_stringpl(pktout, blob);
  185. pq_push(s->ppl.out_pq, pktout);
  186. continue; /* skip USERAUTH_{SUCCESS,FAILURE} epilogue */
  187. }
  188. if (!key_really_ok)
  189. goto failure;
  190. keyalg = find_pubkey_alg_len(algorithm);
  191. if (!keyalg)
  192. goto failure;
  193. key = ssh_key_new_pub(keyalg, blob);
  194. if (!key)
  195. goto failure;
  196. sigdata = strbuf_new();
  197. ssh2_userauth_server_add_session_id(s, sigdata);
  198. put_byte(sigdata, SSH2_MSG_USERAUTH_REQUEST);
  199. put_stringpl(sigdata, s->username);
  200. put_stringpl(sigdata, s->service);
  201. put_stringpl(sigdata, s->method);
  202. put_bool(sigdata, has_signature);
  203. put_stringpl(sigdata, algorithm);
  204. put_stringpl(sigdata, blob);
  205. signature = get_string(pktin);
  206. success = ssh_key_verify(key, signature,
  207. ptrlen_from_strbuf(sigdata));
  208. ssh_key_free(key);
  209. strbuf_free(sigdata);
  210. if (!success)
  211. goto failure;
  212. } else if (ptrlen_eq_string(s->method, "keyboard-interactive")) {
  213. int i, ok;
  214. unsigned n;
  215. s->this_method = AUTHMETHOD_KBDINT;
  216. if (!(s->methods & s->this_method))
  217. goto failure;
  218. do {
  219. s->aki = auth_kbdint_prompts(s->authpolicy, s->username);
  220. if (!s->aki)
  221. goto failure;
  222. pktout = ssh_bpp_new_pktout(
  223. s->ppl.bpp, SSH2_MSG_USERAUTH_INFO_REQUEST);
  224. put_stringz(pktout, s->aki->title);
  225. put_stringz(pktout, s->aki->instruction);
  226. put_stringz(pktout, ""); /* language tag */
  227. put_uint32(pktout, s->aki->nprompts);
  228. for (i = 0; i < s->aki->nprompts; i++) {
  229. put_stringz(pktout, s->aki->prompts[i].prompt);
  230. put_bool(pktout, s->aki->prompts[i].echo);
  231. }
  232. pq_push(s->ppl.out_pq, pktout);
  233. crMaybeWaitUntilV(
  234. (pktin = ssh2_userauth_server_pop(s)) != NULL);
  235. if (pktin->type != SSH2_MSG_USERAUTH_INFO_RESPONSE) {
  236. ssh_proto_error(
  237. s->ppl.ssh, "Received unexpected packet when "
  238. "expecting USERAUTH_INFO_RESPONSE, type %d (%s)",
  239. pktin->type,
  240. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  241. s->ppl.bpp->pls->actx, pktin->type));
  242. return;
  243. }
  244. n = get_uint32(pktin);
  245. if (n != s->aki->nprompts) {
  246. ssh_proto_error(
  247. s->ppl.ssh, "Received %u keyboard-interactive "
  248. "responses after sending %u prompts",
  249. n, s->aki->nprompts);
  250. return;
  251. }
  252. {
  253. ptrlen *responses = snewn(s->aki->nprompts, ptrlen);
  254. for (i = 0; i < s->aki->nprompts; i++)
  255. responses[i] = get_string(pktin);
  256. ok = auth_kbdint_responses(s->authpolicy, responses);
  257. sfree(responses);
  258. }
  259. free_auth_kbdint(s->aki);
  260. s->aki = NULL;
  261. } while (ok == 0);
  262. if (ok <= 0)
  263. goto failure;
  264. } else {
  265. goto failure;
  266. }
  267. /*
  268. * If we get here, we've successfully completed this
  269. * authentication step.
  270. */
  271. if (auth_successful(s->authpolicy, s->username, s->this_method)) {
  272. /*
  273. * ... and it was the last one, so we're completely done.
  274. */
  275. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_USERAUTH_SUCCESS);
  276. pq_push(s->ppl.out_pq, pktout);
  277. break;
  278. } else {
  279. /*
  280. * ... but another is required, so fall through to
  281. * generation of USERAUTH_FAILURE, having first refreshed
  282. * the bit mask of available methods.
  283. */
  284. s->methods = auth_methods(s->authpolicy);
  285. }
  286. s->partial_success = true;
  287. failure:
  288. pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_USERAUTH_FAILURE);
  289. {
  290. strbuf *list = strbuf_new();
  291. if (s->methods & AUTHMETHOD_NONE)
  292. add_to_commasep(list, "none");
  293. if (s->methods & AUTHMETHOD_PASSWORD)
  294. add_to_commasep(list, "password");
  295. if (s->methods & AUTHMETHOD_PUBLICKEY)
  296. add_to_commasep(list, "publickey");
  297. if (s->methods & AUTHMETHOD_KBDINT)
  298. add_to_commasep(list, "keyboard-interactive");
  299. put_stringsb(pktout, list);
  300. }
  301. put_bool(pktout, s->partial_success);
  302. pq_push(s->ppl.out_pq, pktout);
  303. }
  304. /*
  305. * Finally, hand over to our successor layer, and return
  306. * immediately without reaching the crFinishV: ssh_ppl_replace
  307. * will have freed us, so crFinishV's zeroing-out of crState would
  308. * be a use-after-free bug.
  309. */
  310. {
  311. PacketProtocolLayer *successor = s->successor_layer;
  312. s->successor_layer = NULL; /* avoid freeing it ourself */
  313. ssh_ppl_replace(&s->ppl, successor);
  314. return; /* we've just freed s, so avoid even touching s->crState */
  315. }
  316. crFinishV;
  317. }