ssh2userauth.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769
  1. /*
  2. * Packet protocol layer for the client 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. #ifndef NO_GSSAPI
  12. #include "sshgssc.h"
  13. #include "sshgss.h"
  14. #endif
  15. #define BANNER_LIMIT 131072
  16. struct ssh2_userauth_state {
  17. int crState;
  18. PacketProtocolLayer *transport_layer, *successor_layer;
  19. Filename *keyfile;
  20. bool tryagent, change_username;
  21. char *hostname, *fullhostname;
  22. char *default_username;
  23. bool try_ki_auth, try_gssapi_auth, try_gssapi_kex_auth, gssapi_fwd;
  24. char *loghost; // WINSCP
  25. bool change_password; // WINSCP
  26. ptrlen session_id;
  27. enum {
  28. AUTH_TYPE_NONE,
  29. AUTH_TYPE_PUBLICKEY,
  30. AUTH_TYPE_PUBLICKEY_OFFER_LOUD,
  31. AUTH_TYPE_PUBLICKEY_OFFER_QUIET,
  32. AUTH_TYPE_PASSWORD,
  33. AUTH_TYPE_GSSAPI, /* always QUIET */
  34. AUTH_TYPE_KEYBOARD_INTERACTIVE,
  35. AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET
  36. } type;
  37. bool need_pw, can_pubkey, can_passwd, can_keyb_inter;
  38. int userpass_ret;
  39. bool tried_pubkey_config, done_agent;
  40. struct ssh_connection_shared_gss_state *shgss;
  41. #ifndef NO_GSSAPI
  42. bool can_gssapi;
  43. bool can_gssapi_keyex_auth;
  44. bool tried_gssapi;
  45. bool tried_gssapi_keyex_auth;
  46. time_t gss_cred_expiry;
  47. Ssh_gss_buf gss_buf;
  48. Ssh_gss_buf gss_rcvtok, gss_sndtok;
  49. Ssh_gss_stat gss_stat;
  50. #endif
  51. bool suppress_wait_for_response_packet;
  52. strbuf *last_methods_string;
  53. bool kbd_inter_refused;
  54. prompts_t *cur_prompt;
  55. int num_prompts;
  56. char *username;
  57. char *password;
  58. bool got_username;
  59. strbuf *publickey_blob;
  60. bool privatekey_available, privatekey_encrypted;
  61. char *publickey_algorithm;
  62. char *publickey_comment;
  63. void *agent_response_to_free;
  64. ptrlen agent_response;
  65. BinarySource asrc[1]; /* for reading SSH agent response */
  66. size_t pkblob_pos_in_agent;
  67. int keyi, nkeys;
  68. ptrlen pk, alg, comment;
  69. int len;
  70. PktOut *pktout;
  71. bool want_user_input;
  72. agent_pending_query *auth_agent_query;
  73. bufchain banner;
  74. PacketProtocolLayer ppl;
  75. };
  76. static void ssh2_userauth_free(PacketProtocolLayer *);
  77. static void ssh2_userauth_process_queue(PacketProtocolLayer *);
  78. static bool ssh2_userauth_get_specials(
  79. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx);
  80. static void ssh2_userauth_special_cmd(PacketProtocolLayer *ppl,
  81. SessionSpecialCode code, int arg);
  82. static bool ssh2_userauth_want_user_input(PacketProtocolLayer *ppl);
  83. static void ssh2_userauth_got_user_input(PacketProtocolLayer *ppl);
  84. static void ssh2_userauth_reconfigure(PacketProtocolLayer *ppl, Conf *conf);
  85. static void ssh2_userauth_agent_query(struct ssh2_userauth_state *, strbuf *);
  86. static void ssh2_userauth_agent_callback(void *, void *, int);
  87. static void ssh2_userauth_add_sigblob(
  88. struct ssh2_userauth_state *s, PktOut *pkt, ptrlen pkblob, ptrlen sigblob);
  89. static void ssh2_userauth_add_session_id(
  90. struct ssh2_userauth_state *s, strbuf *sigdata);
  91. #ifndef NO_GSSAPI
  92. static PktOut *ssh2_userauth_gss_packet(
  93. struct ssh2_userauth_state *s, const char *authtype);
  94. #endif
  95. static const struct PacketProtocolLayerVtable ssh2_userauth_vtable = {
  96. ssh2_userauth_free,
  97. ssh2_userauth_process_queue,
  98. ssh2_userauth_get_specials,
  99. ssh2_userauth_special_cmd,
  100. ssh2_userauth_want_user_input,
  101. ssh2_userauth_got_user_input,
  102. ssh2_userauth_reconfigure,
  103. "ssh-userauth",
  104. };
  105. PacketProtocolLayer *ssh2_userauth_new(
  106. PacketProtocolLayer *successor_layer,
  107. const char *hostname, const char *fullhostname,
  108. Filename *keyfile, bool tryagent,
  109. const char *default_username, bool change_username,
  110. bool try_ki_auth, bool try_gssapi_auth, bool try_gssapi_kex_auth,
  111. bool gssapi_fwd, struct ssh_connection_shared_gss_state *shgss,
  112. const char * loghost, bool change_password) // WINSCP
  113. {
  114. struct ssh2_userauth_state *s = snew(struct ssh2_userauth_state);
  115. memset(s, 0, sizeof(*s));
  116. s->ppl.vt = &ssh2_userauth_vtable;
  117. s->successor_layer = successor_layer;
  118. s->hostname = dupstr(hostname);
  119. s->fullhostname = dupstr(fullhostname);
  120. s->keyfile = filename_copy(keyfile);
  121. s->tryagent = tryagent;
  122. s->default_username = dupstr(default_username);
  123. s->change_username = change_username;
  124. s->try_ki_auth = try_ki_auth;
  125. s->try_gssapi_auth = try_gssapi_auth;
  126. s->try_gssapi_kex_auth = try_gssapi_kex_auth;
  127. s->gssapi_fwd = gssapi_fwd;
  128. s->shgss = shgss;
  129. s->last_methods_string = strbuf_new();
  130. s->loghost = dupstr(loghost); // WINSCP
  131. s->change_password = change_password;
  132. bufchain_init(&s->banner);
  133. return &s->ppl;
  134. }
  135. void ssh2_userauth_set_transport_layer(PacketProtocolLayer *userauth,
  136. PacketProtocolLayer *transport)
  137. {
  138. struct ssh2_userauth_state *s =
  139. container_of(userauth, struct ssh2_userauth_state, ppl);
  140. s->transport_layer = transport;
  141. }
  142. static void ssh2_userauth_free(PacketProtocolLayer *ppl)
  143. {
  144. struct ssh2_userauth_state *s =
  145. container_of(ppl, struct ssh2_userauth_state, ppl);
  146. bufchain_clear(&s->banner);
  147. if (s->successor_layer)
  148. ssh_ppl_free(s->successor_layer);
  149. sfree(s->agent_response_to_free);
  150. if (s->auth_agent_query)
  151. agent_cancel_query(s->auth_agent_query);
  152. filename_free(s->keyfile);
  153. sfree(s->default_username);
  154. sfree(s->hostname);
  155. sfree(s->fullhostname);
  156. strbuf_free(s->last_methods_string);
  157. sfree(s->loghost);
  158. sfree(s);
  159. }
  160. static void ssh2_userauth_filter_queue(struct ssh2_userauth_state *s)
  161. {
  162. PktIn *pktin;
  163. ptrlen string;
  164. while ((pktin = pq_peek(s->ppl.in_pq)) != NULL) {
  165. switch (pktin->type) {
  166. case SSH2_MSG_USERAUTH_BANNER:
  167. string = get_string(pktin);
  168. if (string.len > BANNER_LIMIT - bufchain_size(&s->banner))
  169. string.len = BANNER_LIMIT - bufchain_size(&s->banner);
  170. sanitise_term_data(&s->banner, string.ptr, string.len);
  171. pq_pop(s->ppl.in_pq);
  172. break;
  173. default:
  174. return;
  175. }
  176. }
  177. }
  178. static PktIn *ssh2_userauth_pop(struct ssh2_userauth_state *s)
  179. {
  180. ssh2_userauth_filter_queue(s);
  181. return pq_pop(s->ppl.in_pq);
  182. }
  183. static void ssh2_userauth_process_queue(PacketProtocolLayer *ppl)
  184. {
  185. struct ssh2_userauth_state *s =
  186. container_of(ppl, struct ssh2_userauth_state, ppl);
  187. PktIn *pktin;
  188. ssh2_userauth_filter_queue(s); /* no matter why we were called */
  189. crBegin(s->crState);
  190. #ifndef NO_GSSAPI
  191. s->tried_gssapi = false;
  192. s->tried_gssapi_keyex_auth = false;
  193. #endif
  194. /*
  195. * Misc one-time setup for authentication.
  196. */
  197. s->publickey_blob = NULL;
  198. s->session_id = ssh2_transport_get_session_id(s->transport_layer);
  199. /*
  200. * Load the public half of any configured public key file for
  201. * later use.
  202. */
  203. if (!filename_is_null(s->keyfile)) {
  204. int keytype;
  205. ppl_logevent(WINSCP_BOM "Reading key file \"%s\"",
  206. filename_to_str(s->keyfile));
  207. keytype = key_type(s->keyfile);
  208. if (keytype == SSH_KEYTYPE_SSH2 ||
  209. keytype == SSH_KEYTYPE_SSH2_PUBLIC_RFC4716 ||
  210. keytype == SSH_KEYTYPE_SSH2_PUBLIC_OPENSSH) {
  211. const char *error;
  212. s->publickey_blob = strbuf_new();
  213. if (ssh2_userkey_loadpub(s->keyfile,
  214. &s->publickey_algorithm,
  215. BinarySink_UPCAST(s->publickey_blob),
  216. &s->publickey_comment, &error)) {
  217. s->privatekey_available = (keytype == SSH_KEYTYPE_SSH2);
  218. if (!s->privatekey_available)
  219. ppl_logevent("Key file contains public key only");
  220. s->privatekey_encrypted =
  221. ssh2_userkey_encrypted(s->keyfile, NULL);
  222. } else {
  223. ppl_logevent("Unable to load key (%s)", error);
  224. ppl_printf(WINSCP_BOM "Unable to load key file \"%s\" (%s)\r\n",
  225. filename_to_str(s->keyfile), error);
  226. strbuf_free(s->publickey_blob);
  227. s->publickey_blob = NULL;
  228. }
  229. } else {
  230. ppl_logevent("Unable to use this key file (%s)",
  231. key_type_to_str(keytype));
  232. ppl_printf(WINSCP_BOM "Unable to use key file \"%s\" (%s)\r\n",
  233. filename_to_str(s->keyfile),
  234. key_type_to_str(keytype));
  235. s->publickey_blob = NULL;
  236. }
  237. }
  238. /*
  239. * Find out about any keys Pageant has (but if there's a public
  240. * key configured, filter out all others).
  241. */
  242. s->nkeys = 0;
  243. s->pkblob_pos_in_agent = 0;
  244. if (s->tryagent && agent_exists()) {
  245. ppl_logevent("Pageant is running. Requesting keys.");
  246. /* Request the keys held by the agent. */
  247. {
  248. strbuf *request = strbuf_new_for_agent_query();
  249. put_byte(request, SSH2_AGENTC_REQUEST_IDENTITIES);
  250. ssh2_userauth_agent_query(s, request);
  251. strbuf_free(request);
  252. crWaitUntilV(!s->auth_agent_query);
  253. }
  254. BinarySource_BARE_INIT(
  255. s->asrc, s->agent_response.ptr, s->agent_response.len);
  256. get_uint32(s->asrc); /* skip length field */
  257. if (get_byte(s->asrc) == SSH2_AGENT_IDENTITIES_ANSWER) {
  258. int keyi;
  259. s->nkeys = toint(get_uint32(s->asrc));
  260. /*
  261. * Vet the Pageant response to ensure that the key count
  262. * and blob lengths make sense.
  263. */
  264. if (s->nkeys < 0) {
  265. ppl_logevent("Pageant response contained a negative"
  266. " key count %d", s->nkeys);
  267. s->nkeys = 0;
  268. goto done_agent_query;
  269. } else {
  270. ppl_logevent("Pageant has %d SSH-2 keys", s->nkeys);
  271. /* See if configured key is in agent. */
  272. for (keyi = 0; keyi < s->nkeys; keyi++) {
  273. size_t pos = s->asrc->pos;
  274. ptrlen blob = get_string(s->asrc);
  275. get_string(s->asrc); /* skip comment */
  276. if (get_err(s->asrc)) {
  277. ppl_logevent("Pageant response was truncated");
  278. s->nkeys = 0;
  279. goto done_agent_query;
  280. }
  281. if (s->publickey_blob &&
  282. blob.len == s->publickey_blob->len &&
  283. !memcmp(blob.ptr, s->publickey_blob->s,
  284. s->publickey_blob->len)) {
  285. ppl_logevent("Pageant key #%d matches "
  286. "configured key file", keyi);
  287. s->keyi = keyi;
  288. s->pkblob_pos_in_agent = pos;
  289. break;
  290. }
  291. }
  292. if (s->publickey_blob && !s->pkblob_pos_in_agent) {
  293. ppl_logevent("Configured key file not in Pageant");
  294. s->nkeys = 0;
  295. }
  296. }
  297. } else {
  298. ppl_logevent("Failed to get reply from Pageant");
  299. }
  300. done_agent_query:;
  301. }
  302. /*
  303. * We repeat this whole loop, including the username prompt,
  304. * until we manage a successful authentication. If the user
  305. * types the wrong _password_, they can be sent back to the
  306. * beginning to try another username, if this is configured on.
  307. * (If they specify a username in the config, they are never
  308. * asked, even if they do give a wrong password.)
  309. *
  310. * I think this best serves the needs of
  311. *
  312. * - the people who have no configuration, no keys, and just
  313. * want to try repeated (username,password) pairs until they
  314. * type both correctly
  315. *
  316. * - people who have keys and configuration but occasionally
  317. * need to fall back to passwords
  318. *
  319. * - people with a key held in Pageant, who might not have
  320. * logged in to a particular machine before; so they want to
  321. * type a username, and then _either_ their key will be
  322. * accepted, _or_ they will type a password. If they mistype
  323. * the username they will want to be able to get back and
  324. * retype it!
  325. */
  326. s->got_username = false;
  327. while (1) {
  328. /*
  329. * Get a username.
  330. */
  331. if (s->got_username && s->change_username) {
  332. /*
  333. * We got a username last time round this loop, and
  334. * with change_username turned off we don't try to get
  335. * it again.
  336. */
  337. } else if ((s->username = s->default_username) == NULL) {
  338. s->cur_prompt = new_prompts();
  339. s->cur_prompt->to_server = true;
  340. s->cur_prompt->name = dupstr("SSH login name");
  341. add_prompt(s->cur_prompt, dupstr("login as: "), true);
  342. s->userpass_ret = seat_get_userpass_input(
  343. s->ppl.seat, s->cur_prompt, NULL);
  344. while (1) {
  345. while (s->userpass_ret < 0 &&
  346. bufchain_size(s->ppl.user_input) > 0)
  347. s->userpass_ret = seat_get_userpass_input(
  348. s->ppl.seat, s->cur_prompt, s->ppl.user_input);
  349. if (s->userpass_ret >= 0)
  350. break;
  351. s->want_user_input = true;
  352. crReturnV;
  353. s->want_user_input = false;
  354. }
  355. if (!s->userpass_ret) {
  356. /*
  357. * seat_get_userpass_input() failed to get a username.
  358. * Terminate.
  359. */
  360. free_prompts(s->cur_prompt);
  361. ssh_user_close(s->ppl.ssh, "No username provided");
  362. return;
  363. }
  364. s->username = dupstr(s->cur_prompt->prompts[0]->result);
  365. free_prompts(s->cur_prompt);
  366. } else {
  367. if ((flags & FLAG_VERBOSE) || (flags & FLAG_INTERACTIVE))
  368. ppl_printf(WINSCP_BOM "Using username \"%s\".\r\n", s->username);
  369. }
  370. s->got_username = true;
  371. /*
  372. * Send an authentication request using method "none": (a)
  373. * just in case it succeeds, and (b) so that we know what
  374. * authentication methods we can usefully try next.
  375. */
  376. s->ppl.bpp->pls->actx = SSH2_PKTCTX_NOAUTH;
  377. s->pktout = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  378. put_stringz(s->pktout, s->username);
  379. put_stringz(s->pktout, s->successor_layer->vt->name);
  380. put_stringz(s->pktout, "none"); /* method */
  381. pq_push(s->ppl.out_pq, s->pktout);
  382. s->type = AUTH_TYPE_NONE;
  383. s->tried_pubkey_config = false;
  384. s->kbd_inter_refused = false;
  385. /* Reset agent request state. */
  386. s->done_agent = false;
  387. if (s->agent_response.ptr) {
  388. if (s->pkblob_pos_in_agent) {
  389. s->asrc->pos = s->pkblob_pos_in_agent;
  390. } else {
  391. s->asrc->pos = 9; /* skip length + type + key count */
  392. s->keyi = 0;
  393. }
  394. }
  395. while (1) {
  396. /*
  397. * Wait for the result of the last authentication request,
  398. * unless the request terminated for some reason on our
  399. * own side.
  400. */
  401. if (s->suppress_wait_for_response_packet) {
  402. pktin = NULL;
  403. s->suppress_wait_for_response_packet = false;
  404. } else {
  405. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  406. }
  407. /*
  408. * Now is a convenient point to spew any banner material
  409. * that we've accumulated. (This should ensure that when
  410. * we exit the auth loop, we haven't any left to deal
  411. * with.)
  412. */
  413. {
  414. /*
  415. * Don't show the banner if we're operating in
  416. * non-verbose non-interactive mode. (It's probably
  417. * a script, which means nobody will read the
  418. * banner _anyway_, and moreover the printing of
  419. * the banner will screw up processing on the
  420. * output of (say) plink.)
  421. *
  422. * The banner data has been sanitised already by this
  423. * point, so we can safely pass it straight to
  424. * seat_stderr.
  425. */
  426. if (bufchain_size(&s->banner) &&
  427. (flags & (FLAG_VERBOSE | FLAG_INTERACTIVE))) {
  428. while (bufchain_size(&s->banner) > 0) {
  429. void *data;
  430. int len;
  431. bufchain_prefix(&s->banner, &data, &len);
  432. display_banner(s->ppl.seat, &s->banner, len); // WINSCP
  433. seat_stderr(s->ppl.seat, data, len);
  434. bufchain_consume(&s->banner, len);
  435. }
  436. }
  437. bufchain_clear(&s->banner);
  438. }
  439. if (pktin && pktin->type == SSH2_MSG_USERAUTH_SUCCESS) {
  440. ppl_logevent("Access granted");
  441. goto userauth_success;
  442. }
  443. if (pktin && pktin->type != SSH2_MSG_USERAUTH_FAILURE &&
  444. s->type != AUTH_TYPE_GSSAPI) {
  445. ssh_proto_error(s->ppl.ssh, "Received unexpected packet "
  446. "in response to authentication request, "
  447. "type %d (%s)", pktin->type,
  448. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  449. s->ppl.bpp->pls->actx,
  450. pktin->type));
  451. return;
  452. }
  453. /*
  454. * OK, we're now sitting on a USERAUTH_FAILURE message, so
  455. * we can look at the string in it and know what we can
  456. * helpfully try next.
  457. */
  458. if (pktin && pktin->type == SSH2_MSG_USERAUTH_FAILURE) {
  459. ptrlen methods = get_string(pktin);
  460. bool partial_success = get_bool(pktin);
  461. if (!partial_success) {
  462. /*
  463. * We have received an unequivocal Access
  464. * Denied. This can translate to a variety of
  465. * messages, or no message at all.
  466. *
  467. * For forms of authentication which are attempted
  468. * implicitly, by which I mean without printing
  469. * anything in the window indicating that we're
  470. * trying them, we should never print 'Access
  471. * denied'.
  472. *
  473. * If we do print a message saying that we're
  474. * attempting some kind of authentication, it's OK
  475. * to print a followup message saying it failed -
  476. * but the message may sometimes be more specific
  477. * than simply 'Access denied'.
  478. *
  479. * Additionally, if we'd just tried password
  480. * authentication, we should break out of this
  481. * whole loop so as to go back to the username
  482. * prompt (iff we're configured to allow
  483. * username change attempts).
  484. */
  485. if (s->type == AUTH_TYPE_NONE) {
  486. /* do nothing */
  487. } else if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD ||
  488. s->type == AUTH_TYPE_PUBLICKEY_OFFER_QUIET) {
  489. if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD)
  490. ppl_printf("Server refused our key\r\n");
  491. ppl_logevent("Server refused our key");
  492. } else if (s->type == AUTH_TYPE_PUBLICKEY) {
  493. /* This _shouldn't_ happen except by a
  494. * protocol bug causing client and server to
  495. * disagree on what is a correct signature. */
  496. ppl_printf("Server refused public-key signature"
  497. " despite accepting key!\r\n");
  498. ppl_logevent("Server refused public-key signature"
  499. " despite accepting key!");
  500. } else if (s->type==AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET) {
  501. /* quiet, so no ppl_printf */
  502. ppl_logevent("Server refused keyboard-interactive "
  503. "authentication");
  504. } else if (s->type==AUTH_TYPE_GSSAPI) {
  505. /* always quiet, so no ppl_printf */
  506. /* also, the code down in the GSSAPI block has
  507. * already logged this in the Event Log */
  508. } else if (s->type == AUTH_TYPE_KEYBOARD_INTERACTIVE) {
  509. ppl_logevent("Keyboard-interactive authentication "
  510. "failed");
  511. ppl_printf("Access denied\r\n");
  512. } else {
  513. assert(s->type == AUTH_TYPE_PASSWORD);
  514. ppl_logevent("Password authentication failed");
  515. ppl_printf("Access denied\r\n");
  516. if (s->change_username) {
  517. /* XXX perhaps we should allow
  518. * keyboard-interactive to do this too? */
  519. goto try_new_username;
  520. }
  521. }
  522. } else {
  523. ppl_printf("Further authentication required\r\n");
  524. ppl_logevent("Further authentication required");
  525. }
  526. /*
  527. * Save the methods string for use in error messages.
  528. */
  529. s->last_methods_string->len = 0;
  530. put_data(s->last_methods_string, methods.ptr, methods.len);
  531. #ifdef WINSCP
  532. ppl_logevent("Server offered these authentication methods: %s", s->last_methods_string->s);
  533. #endif
  534. /*
  535. * Scan it for method identifiers we know about.
  536. */
  537. { // WINSCP
  538. bool srv_pubkey = false, srv_passwd = false;
  539. bool srv_keyb_inter = false, srv_gssapi = false;
  540. bool srv_gssapi_keyex_auth = false;
  541. ptrlen method; // WINSCP
  542. for (; get_commasep_word(&methods, &method) ;) {
  543. if (ptrlen_eq_string(method, "publickey"))
  544. srv_pubkey = true;
  545. else if (ptrlen_eq_string(method, "password"))
  546. srv_passwd = true;
  547. else if (ptrlen_eq_string(method, "keyboard-interactive"))
  548. srv_keyb_inter = true;
  549. else if (ptrlen_eq_string(method, "gssapi-with-mic"))
  550. srv_gssapi = true;
  551. else if (ptrlen_eq_string(method, "gssapi-keyex"))
  552. srv_gssapi_keyex_auth = true;
  553. }
  554. /*
  555. * And combine those flags with our own configuration
  556. * and context to set the main can_foo variables.
  557. */
  558. s->can_pubkey = srv_pubkey;
  559. s->can_passwd = srv_passwd;
  560. s->can_keyb_inter = s->try_ki_auth && srv_keyb_inter;
  561. #ifndef NO_GSSAPI
  562. s->can_gssapi = s->try_gssapi_auth && srv_gssapi &&
  563. s->shgss->libs->nlibraries > 0;
  564. s->can_gssapi_keyex_auth = s->try_gssapi_kex_auth &&
  565. srv_gssapi_keyex_auth &&
  566. s->shgss->libs->nlibraries > 0 && s->shgss->ctx;
  567. #endif
  568. } // WINSCP
  569. }
  570. s->ppl.bpp->pls->actx = SSH2_PKTCTX_NOAUTH;
  571. #ifndef NO_GSSAPI
  572. if (s->can_gssapi_keyex_auth && !s->tried_gssapi_keyex_auth) {
  573. /* gssapi-keyex authentication */
  574. s->type = AUTH_TYPE_GSSAPI;
  575. s->tried_gssapi_keyex_auth = true;
  576. s->ppl.bpp->pls->actx = SSH2_PKTCTX_GSSAPI;
  577. if (s->shgss->lib->gsslogmsg)
  578. ppl_logevent("%s", s->shgss->lib->gsslogmsg);
  579. ppl_logevent("Trying gssapi-keyex...");
  580. s->pktout = ssh2_userauth_gss_packet(s, "gssapi-keyex");
  581. pq_push(s->ppl.out_pq, s->pktout);
  582. s->shgss->lib->release_cred(s->shgss->lib, &s->shgss->ctx);
  583. s->shgss->ctx = NULL;
  584. continue;
  585. } else
  586. #endif /* NO_GSSAPI */
  587. if (s->can_pubkey && !s->done_agent && s->nkeys) {
  588. /*
  589. * Attempt public-key authentication using a key from Pageant.
  590. */
  591. s->ppl.bpp->pls->actx = SSH2_PKTCTX_PUBLICKEY;
  592. ppl_logevent("Trying Pageant key #%d", s->keyi);
  593. /* Unpack key from agent response */
  594. s->pk = get_string(s->asrc);
  595. s->comment = get_string(s->asrc);
  596. {
  597. BinarySource src[1];
  598. BinarySource_BARE_INIT(src, s->pk.ptr, s->pk.len);
  599. s->alg = get_string(src);
  600. }
  601. /* See if server will accept it */
  602. s->pktout = ssh_bpp_new_pktout(
  603. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  604. put_stringz(s->pktout, s->username);
  605. put_stringz(s->pktout, s->successor_layer->vt->name);
  606. put_stringz(s->pktout, "publickey");
  607. /* method */
  608. put_bool(s->pktout, false); /* no signature included */
  609. put_stringpl(s->pktout, s->alg);
  610. put_stringpl(s->pktout, s->pk);
  611. pq_push(s->ppl.out_pq, s->pktout);
  612. s->type = AUTH_TYPE_PUBLICKEY_OFFER_QUIET;
  613. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  614. if (pktin->type != SSH2_MSG_USERAUTH_PK_OK) {
  615. /* Offer of key refused, presumably via
  616. * USERAUTH_FAILURE. Requeue for the next iteration. */
  617. pq_push_front(s->ppl.in_pq, pktin);
  618. } else {
  619. strbuf *agentreq, *sigdata;
  620. if (flags & FLAG_VERBOSE)
  621. ppl_printf("Authenticating with public key "
  622. "\"%.*s\" from agent\r\n",
  623. PTRLEN_PRINTF(s->comment));
  624. /*
  625. * Server is willing to accept the key.
  626. * Construct a SIGN_REQUEST.
  627. */
  628. s->pktout = ssh_bpp_new_pktout(
  629. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  630. put_stringz(s->pktout, s->username);
  631. put_stringz(s->pktout, s->successor_layer->vt->name);
  632. put_stringz(s->pktout, "publickey");
  633. /* method */
  634. put_bool(s->pktout, true); /* signature included */
  635. put_stringpl(s->pktout, s->alg);
  636. put_stringpl(s->pktout, s->pk);
  637. /* Ask agent for signature. */
  638. agentreq = strbuf_new_for_agent_query();
  639. put_byte(agentreq, SSH2_AGENTC_SIGN_REQUEST);
  640. put_stringpl(agentreq, s->pk);
  641. /* Now the data to be signed... */
  642. sigdata = strbuf_new();
  643. ssh2_userauth_add_session_id(s, sigdata);
  644. put_data(sigdata, s->pktout->data + 5,
  645. s->pktout->length - 5);
  646. put_stringsb(agentreq, sigdata);
  647. /* And finally the (zero) flags word. */
  648. put_uint32(agentreq, 0);
  649. ssh2_userauth_agent_query(s, agentreq);
  650. strbuf_free(agentreq);
  651. crWaitUntilV(!s->auth_agent_query);
  652. if (s->agent_response.ptr) {
  653. ptrlen sigblob;
  654. BinarySource src[1];
  655. BinarySource_BARE_INIT(src, s->agent_response.ptr,
  656. s->agent_response.len);
  657. get_uint32(src); /* skip length field */
  658. if (get_byte(src) == SSH2_AGENT_SIGN_RESPONSE &&
  659. (sigblob = get_string(src), !get_err(src))) {
  660. ppl_logevent("Sending Pageant's response");
  661. ssh2_userauth_add_sigblob(s, s->pktout,
  662. s->pk, sigblob);
  663. pq_push(s->ppl.out_pq, s->pktout);
  664. s->type = AUTH_TYPE_PUBLICKEY;
  665. } else {
  666. /* FIXME: less drastic response */
  667. ssh_sw_abort(s->ppl.ssh, "Pageant failed to "
  668. "provide a signature");
  669. return;
  670. }
  671. }
  672. }
  673. /* Do we have any keys left to try? */
  674. if (s->pkblob_pos_in_agent) {
  675. s->done_agent = true;
  676. s->tried_pubkey_config = true;
  677. } else {
  678. s->keyi++;
  679. if (s->keyi >= s->nkeys)
  680. s->done_agent = true;
  681. }
  682. } else if (s->can_pubkey && s->publickey_blob &&
  683. s->privatekey_available && !s->tried_pubkey_config) {
  684. struct ssh2_userkey *key; /* not live over crReturn */
  685. char *passphrase; /* not live over crReturn */
  686. s->ppl.bpp->pls->actx = SSH2_PKTCTX_PUBLICKEY;
  687. s->tried_pubkey_config = true;
  688. /*
  689. * Try the public key supplied in the configuration.
  690. *
  691. * First, offer the public blob to see if the server is
  692. * willing to accept it.
  693. */
  694. s->pktout = ssh_bpp_new_pktout(
  695. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  696. put_stringz(s->pktout, s->username);
  697. put_stringz(s->pktout, s->successor_layer->vt->name);
  698. put_stringz(s->pktout, "publickey"); /* method */
  699. put_bool(s->pktout, false);
  700. /* no signature included */
  701. put_stringz(s->pktout, s->publickey_algorithm);
  702. put_string(s->pktout, s->publickey_blob->s,
  703. s->publickey_blob->len);
  704. pq_push(s->ppl.out_pq, s->pktout);
  705. ppl_logevent("Offered public key");
  706. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  707. if (pktin->type != SSH2_MSG_USERAUTH_PK_OK) {
  708. /* Key refused. Give up. */
  709. pq_push_front(s->ppl.in_pq, pktin);
  710. s->type = AUTH_TYPE_PUBLICKEY_OFFER_LOUD;
  711. continue; /* process this new message */
  712. }
  713. ppl_logevent("Offer of public key accepted");
  714. /*
  715. * Actually attempt a serious authentication using
  716. * the key.
  717. */
  718. if (flags & FLAG_VERBOSE)
  719. ppl_printf("Authenticating with public key \"%s\"\r\n",
  720. s->publickey_comment);
  721. key = NULL;
  722. while (!key) {
  723. const char *error; /* not live over crReturn */
  724. if (s->privatekey_encrypted) {
  725. /*
  726. * Get a passphrase from the user.
  727. */
  728. s->cur_prompt = new_prompts();
  729. s->cur_prompt->to_server = false;
  730. s->cur_prompt->name = dupstr("SSH key passphrase");
  731. add_prompt(s->cur_prompt,
  732. dupprintf("Passphrase for key \"%s\": ",
  733. s->publickey_comment),
  734. false);
  735. s->userpass_ret = seat_get_userpass_input(
  736. s->ppl.seat, s->cur_prompt, NULL);
  737. while (1) {
  738. while (s->userpass_ret < 0 &&
  739. bufchain_size(s->ppl.user_input) > 0)
  740. s->userpass_ret = seat_get_userpass_input(
  741. s->ppl.seat, s->cur_prompt,
  742. s->ppl.user_input);
  743. if (s->userpass_ret >= 0)
  744. break;
  745. s->want_user_input = true;
  746. crReturnV;
  747. s->want_user_input = false;
  748. }
  749. if (!s->userpass_ret) {
  750. /* Failed to get a passphrase. Terminate. */
  751. free_prompts(s->cur_prompt);
  752. ssh_bpp_queue_disconnect(
  753. s->ppl.bpp, "Unable to authenticate",
  754. SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
  755. ssh_user_close(s->ppl.ssh, "User aborted at "
  756. "passphrase prompt");
  757. return;
  758. }
  759. passphrase =
  760. dupstr(s->cur_prompt->prompts[0]->result);
  761. free_prompts(s->cur_prompt);
  762. } else {
  763. passphrase = NULL; /* no passphrase needed */
  764. }
  765. /*
  766. * Try decrypting the key.
  767. */
  768. key = ssh2_load_userkey(s->keyfile, passphrase, &error);
  769. if (passphrase) {
  770. /* burn the evidence */
  771. smemclr(passphrase, strlen(passphrase));
  772. sfree(passphrase);
  773. }
  774. if (key == SSH2_WRONG_PASSPHRASE || key == NULL) {
  775. if (passphrase &&
  776. (key == SSH2_WRONG_PASSPHRASE)) {
  777. ppl_printf("Wrong passphrase\r\n");
  778. key = NULL;
  779. /* and loop again */
  780. } else {
  781. ppl_printf("Unable to load private key (%s)\r\n",
  782. error);
  783. key = NULL;
  784. break; /* try something else */
  785. }
  786. }
  787. }
  788. if (key) {
  789. strbuf *pkblob, *sigdata, *sigblob;
  790. /*
  791. * We have loaded the private key and the server
  792. * has announced that it's willing to accept it.
  793. * Hallelujah. Generate a signature and send it.
  794. */
  795. s->pktout = ssh_bpp_new_pktout(
  796. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  797. put_stringz(s->pktout, s->username);
  798. put_stringz(s->pktout, s->successor_layer->vt->name);
  799. put_stringz(s->pktout, "publickey"); /* method */
  800. put_bool(s->pktout, true); /* signature follows */
  801. put_stringz(s->pktout, ssh_key_ssh_id(key->key));
  802. pkblob = strbuf_new();
  803. ssh_key_public_blob(key->key, BinarySink_UPCAST(pkblob));
  804. put_string(s->pktout, pkblob->s, pkblob->len);
  805. /*
  806. * The data to be signed is:
  807. *
  808. * string session-id
  809. *
  810. * followed by everything so far placed in the
  811. * outgoing packet.
  812. */
  813. sigdata = strbuf_new();
  814. ssh2_userauth_add_session_id(s, sigdata);
  815. put_data(sigdata, s->pktout->data + 5,
  816. s->pktout->length - 5);
  817. sigblob = strbuf_new();
  818. ssh_key_sign(key->key, sigdata->s, sigdata->len, 0,
  819. BinarySink_UPCAST(sigblob));
  820. strbuf_free(sigdata);
  821. ssh2_userauth_add_sigblob(
  822. s, s->pktout, ptrlen_from_strbuf(pkblob),
  823. ptrlen_from_strbuf(sigblob));
  824. strbuf_free(pkblob);
  825. strbuf_free(sigblob);
  826. pq_push(s->ppl.out_pq, s->pktout);
  827. ppl_logevent("Sent public key signature");
  828. s->type = AUTH_TYPE_PUBLICKEY;
  829. ssh_key_free(key->key);
  830. sfree(key->comment);
  831. sfree(key);
  832. }
  833. #ifndef NO_GSSAPI
  834. } else if (s->can_gssapi && !s->tried_gssapi) {
  835. /* gssapi-with-mic authentication */
  836. ptrlen data;
  837. s->type = AUTH_TYPE_GSSAPI;
  838. s->tried_gssapi = true;
  839. s->ppl.bpp->pls->actx = SSH2_PKTCTX_GSSAPI;
  840. if (s->shgss->lib->gsslogmsg)
  841. ppl_logevent("%s", s->shgss->lib->gsslogmsg);
  842. /* Sending USERAUTH_REQUEST with "gssapi-with-mic" method */
  843. ppl_logevent("Trying gssapi-with-mic...");
  844. s->pktout = ssh_bpp_new_pktout(
  845. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  846. put_stringz(s->pktout, s->username);
  847. put_stringz(s->pktout, s->successor_layer->vt->name);
  848. put_stringz(s->pktout, "gssapi-with-mic");
  849. ppl_logevent("Attempting GSSAPI authentication");
  850. /* add mechanism info */
  851. s->shgss->lib->indicate_mech(s->shgss->lib, &s->gss_buf);
  852. /* number of GSSAPI mechanisms */
  853. put_uint32(s->pktout, 1);
  854. /* length of OID + 2 */
  855. put_uint32(s->pktout, s->gss_buf.length + 2);
  856. put_byte(s->pktout, SSH2_GSS_OIDTYPE);
  857. /* length of OID */
  858. put_byte(s->pktout, s->gss_buf.length);
  859. put_data(s->pktout, s->gss_buf.value, s->gss_buf.length);
  860. pq_push(s->ppl.out_pq, s->pktout);
  861. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  862. if (pktin->type != SSH2_MSG_USERAUTH_GSSAPI_RESPONSE) {
  863. ppl_logevent("GSSAPI authentication request refused");
  864. pq_push_front(s->ppl.in_pq, pktin);
  865. continue;
  866. }
  867. /* check returned packet ... */
  868. data = get_string(pktin);
  869. s->gss_rcvtok.value = (char *)data.ptr;
  870. s->gss_rcvtok.length = data.len;
  871. if (s->gss_rcvtok.length != s->gss_buf.length + 2 ||
  872. ((char *)s->gss_rcvtok.value)[0] != SSH2_GSS_OIDTYPE ||
  873. ((char *)s->gss_rcvtok.value)[1] != s->gss_buf.length ||
  874. memcmp((char *)s->gss_rcvtok.value + 2,
  875. s->gss_buf.value,s->gss_buf.length) ) {
  876. ppl_logevent("GSSAPI authentication - wrong response "
  877. "from server");
  878. continue;
  879. }
  880. /* Import server name if not cached from KEX */
  881. if (s->shgss->srv_name == GSS_C_NO_NAME) {
  882. // WINSCP
  883. const char * fullhostname = s->fullhostname;
  884. if (s->loghost[0] != '\0')
  885. {
  886. fullhostname = s->loghost;
  887. }
  888. // /WINSCP
  889. s->gss_stat = s->shgss->lib->import_name(
  890. s->shgss->lib, fullhostname, &s->shgss->srv_name); // WINSCP
  891. if (s->gss_stat != SSH_GSS_OK) {
  892. if (s->gss_stat == SSH_GSS_BAD_HOST_NAME)
  893. ppl_logevent("GSSAPI import name failed -"
  894. " Bad service name");
  895. else
  896. ppl_logevent("GSSAPI import name failed");
  897. continue;
  898. }
  899. }
  900. /* Allocate our gss_ctx */
  901. s->gss_stat = s->shgss->lib->acquire_cred(
  902. s->shgss->lib, &s->shgss->ctx, NULL);
  903. if (s->gss_stat != SSH_GSS_OK) {
  904. ppl_logevent("GSSAPI authentication failed to get "
  905. "credentials");
  906. /* The failure was on our side, so the server
  907. * won't be sending a response packet indicating
  908. * failure. Avoid waiting for it next time round
  909. * the loop. */
  910. s->suppress_wait_for_response_packet = true;
  911. continue;
  912. }
  913. /* initial tokens are empty */
  914. SSH_GSS_CLEAR_BUF(&s->gss_rcvtok);
  915. SSH_GSS_CLEAR_BUF(&s->gss_sndtok);
  916. /* now enter the loop */
  917. do {
  918. /*
  919. * When acquire_cred yields no useful expiration, go with
  920. * the service ticket expiration.
  921. */
  922. s->gss_stat = s->shgss->lib->init_sec_context
  923. (s->shgss->lib,
  924. &s->shgss->ctx,
  925. s->shgss->srv_name,
  926. s->gssapi_fwd,
  927. &s->gss_rcvtok,
  928. &s->gss_sndtok,
  929. NULL,
  930. NULL);
  931. if (s->gss_stat!=SSH_GSS_S_COMPLETE &&
  932. s->gss_stat!=SSH_GSS_S_CONTINUE_NEEDED) {
  933. ppl_logevent("GSSAPI authentication initialisation "
  934. "failed");
  935. if (s->shgss->lib->display_status(s->shgss->lib,
  936. s->shgss->ctx, &s->gss_buf) == SSH_GSS_OK) {
  937. ppl_logevent("%s", (char *)s->gss_buf.value);
  938. sfree(s->gss_buf.value);
  939. }
  940. pq_push_front(s->ppl.in_pq, pktin);
  941. break;
  942. }
  943. ppl_logevent("GSSAPI authentication initialised");
  944. /*
  945. * Client and server now exchange tokens until GSSAPI
  946. * no longer says CONTINUE_NEEDED
  947. */
  948. if (s->gss_sndtok.length != 0) {
  949. s->pktout =
  950. ssh_bpp_new_pktout(
  951. s->ppl.bpp, SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
  952. put_string(s->pktout,
  953. s->gss_sndtok.value, s->gss_sndtok.length);
  954. pq_push(s->ppl.out_pq, s->pktout);
  955. s->shgss->lib->free_tok(s->shgss->lib, &s->gss_sndtok);
  956. }
  957. if (s->gss_stat == SSH_GSS_S_CONTINUE_NEEDED) {
  958. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  959. if (pktin->type == SSH2_MSG_USERAUTH_GSSAPI_ERRTOK) {
  960. /*
  961. * Per RFC 4462 section 3.9, this packet
  962. * type MUST immediately precede an
  963. * ordinary USERAUTH_FAILURE.
  964. *
  965. * We currently don't know how to do
  966. * anything with the GSSAPI error token
  967. * contained in this packet, so we ignore
  968. * it and just wait for the following
  969. * FAILURE.
  970. */
  971. crMaybeWaitUntilV(
  972. (pktin = ssh2_userauth_pop(s)) != NULL);
  973. if (pktin->type != SSH2_MSG_USERAUTH_FAILURE) {
  974. ssh_proto_error(
  975. s->ppl.ssh, "Received unexpected packet "
  976. "after SSH_MSG_USERAUTH_GSSAPI_ERRTOK "
  977. "(expected SSH_MSG_USERAUTH_FAILURE): "
  978. "type %d (%s)", pktin->type,
  979. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  980. s->ppl.bpp->pls->actx,
  981. pktin->type));
  982. return;
  983. }
  984. }
  985. if (pktin->type == SSH2_MSG_USERAUTH_FAILURE) {
  986. ppl_logevent("GSSAPI authentication failed");
  987. s->gss_stat = SSH_GSS_FAILURE;
  988. pq_push_front(s->ppl.in_pq, pktin);
  989. break;
  990. } else if (pktin->type !=
  991. SSH2_MSG_USERAUTH_GSSAPI_TOKEN) {
  992. ppl_logevent("GSSAPI authentication -"
  993. " bad server response");
  994. s->gss_stat = SSH_GSS_FAILURE;
  995. break;
  996. }
  997. data = get_string(pktin);
  998. s->gss_rcvtok.value = (char *)data.ptr;
  999. s->gss_rcvtok.length = data.len;
  1000. }
  1001. } while (s-> gss_stat == SSH_GSS_S_CONTINUE_NEEDED);
  1002. if (s->gss_stat != SSH_GSS_OK) {
  1003. s->shgss->lib->release_cred(s->shgss->lib, &s->shgss->ctx);
  1004. continue;
  1005. }
  1006. ppl_logevent("GSSAPI authentication loop finished OK");
  1007. /* Now send the MIC */
  1008. s->pktout = ssh2_userauth_gss_packet(s, "gssapi-with-mic");
  1009. pq_push(s->ppl.out_pq, s->pktout);
  1010. s->shgss->lib->release_cred(s->shgss->lib, &s->shgss->ctx);
  1011. continue;
  1012. #endif
  1013. } else if (s->can_keyb_inter && !s->kbd_inter_refused) {
  1014. /*
  1015. * Keyboard-interactive authentication.
  1016. */
  1017. s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE;
  1018. s->ppl.bpp->pls->actx = SSH2_PKTCTX_KBDINTER;
  1019. s->pktout = ssh_bpp_new_pktout(
  1020. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  1021. put_stringz(s->pktout, s->username);
  1022. put_stringz(s->pktout, s->successor_layer->vt->name);
  1023. put_stringz(s->pktout, "keyboard-interactive");
  1024. /* method */
  1025. put_stringz(s->pktout, ""); /* lang */
  1026. put_stringz(s->pktout, ""); /* submethods */
  1027. pq_push(s->ppl.out_pq, s->pktout);
  1028. ppl_logevent("Attempting keyboard-interactive authentication");
  1029. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  1030. if (pktin->type != SSH2_MSG_USERAUTH_INFO_REQUEST) {
  1031. /* Server is not willing to do keyboard-interactive
  1032. * at all (or, bizarrely but legally, accepts the
  1033. * user without actually issuing any prompts).
  1034. * Give up on it entirely. */
  1035. pq_push_front(s->ppl.in_pq, pktin);
  1036. s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET;
  1037. s->kbd_inter_refused = true; /* don't try it again */
  1038. continue;
  1039. }
  1040. /*
  1041. * Loop while the server continues to send INFO_REQUESTs.
  1042. */
  1043. while (pktin->type == SSH2_MSG_USERAUTH_INFO_REQUEST) {
  1044. ptrlen name, inst;
  1045. int i;
  1046. /*
  1047. * We've got a fresh USERAUTH_INFO_REQUEST.
  1048. * Get the preamble and start building a prompt.
  1049. */
  1050. name = get_string(pktin);
  1051. inst = get_string(pktin);
  1052. get_string(pktin); /* skip language tag */
  1053. s->cur_prompt = new_prompts();
  1054. s->cur_prompt->to_server = true;
  1055. /*
  1056. * Get any prompt(s) from the packet.
  1057. */
  1058. s->num_prompts = get_uint32(pktin);
  1059. for (i = 0; i < s->num_prompts; i++) {
  1060. ptrlen prompt;
  1061. bool echo;
  1062. static char noprompt[] =
  1063. "<server failed to send prompt>: ";
  1064. prompt = get_string(pktin);
  1065. echo = get_bool(pktin);
  1066. if (!prompt.len) {
  1067. prompt.ptr = noprompt;
  1068. prompt.len = lenof(noprompt)-1;
  1069. }
  1070. add_prompt(s->cur_prompt, mkstr(prompt), echo);
  1071. }
  1072. if (name.len) {
  1073. /* FIXME: better prefix to distinguish from
  1074. * local prompts? */
  1075. s->cur_prompt->name =
  1076. dupprintf("SSH server: %.*s", PTRLEN_PRINTF(name));
  1077. s->cur_prompt->name_reqd = true;
  1078. } else {
  1079. s->cur_prompt->name =
  1080. dupstr("SSH server authentication");
  1081. s->cur_prompt->name_reqd = false;
  1082. }
  1083. /* We add a prefix to try to make it clear that a prompt
  1084. * has come from the server.
  1085. * FIXME: ugly to print "Using..." in prompt _every_
  1086. * time round. Can this be done more subtly? */
  1087. /* Special case: for reasons best known to themselves,
  1088. * some servers send k-i requests with no prompts and
  1089. * nothing to display. Keep quiet in this case. */
  1090. if (s->num_prompts || name.len || inst.len) {
  1091. s->cur_prompt->instruction =
  1092. dupprintf("Using keyboard-interactive "
  1093. "authentication.%s%.*s",
  1094. inst.len ? "\n" : "",
  1095. PTRLEN_PRINTF(inst));
  1096. s->cur_prompt->instr_reqd = true;
  1097. } else {
  1098. s->cur_prompt->instr_reqd = false;
  1099. }
  1100. /*
  1101. * Display any instructions, and get the user's
  1102. * response(s).
  1103. */
  1104. s->userpass_ret = seat_get_userpass_input(
  1105. s->ppl.seat, s->cur_prompt, NULL);
  1106. while (1) {
  1107. while (s->userpass_ret < 0 &&
  1108. bufchain_size(s->ppl.user_input) > 0)
  1109. s->userpass_ret = seat_get_userpass_input(
  1110. s->ppl.seat, s->cur_prompt, s->ppl.user_input);
  1111. if (s->userpass_ret >= 0)
  1112. break;
  1113. s->want_user_input = true;
  1114. crReturnV;
  1115. s->want_user_input = false;
  1116. }
  1117. if (!s->userpass_ret) {
  1118. /*
  1119. * Failed to get responses. Terminate.
  1120. */
  1121. free_prompts(s->cur_prompt);
  1122. ssh_bpp_queue_disconnect(
  1123. s->ppl.bpp, "Unable to authenticate",
  1124. SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
  1125. ssh_user_close(s->ppl.ssh, "User aborted during "
  1126. "keyboard-interactive authentication");
  1127. return;
  1128. }
  1129. /*
  1130. * Send the response(s) to the server.
  1131. */
  1132. s->pktout = ssh_bpp_new_pktout(
  1133. s->ppl.bpp, SSH2_MSG_USERAUTH_INFO_RESPONSE);
  1134. put_uint32(s->pktout, s->num_prompts);
  1135. for (i=0; i < s->num_prompts; i++) {
  1136. put_stringz(s->pktout,
  1137. s->cur_prompt->prompts[i]->result);
  1138. }
  1139. s->pktout->minlen = 256;
  1140. pq_push(s->ppl.out_pq, s->pktout);
  1141. /*
  1142. * Free the prompts structure from this iteration.
  1143. * If there's another, a new one will be allocated
  1144. * when we return to the top of this while loop.
  1145. */
  1146. free_prompts(s->cur_prompt);
  1147. /*
  1148. * Get the next packet in case it's another
  1149. * INFO_REQUEST.
  1150. */
  1151. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  1152. }
  1153. /*
  1154. * We should have SUCCESS or FAILURE now.
  1155. */
  1156. pq_push_front(s->ppl.in_pq, pktin);
  1157. } else if (s->can_passwd) {
  1158. /*
  1159. * Plain old password authentication.
  1160. */
  1161. bool changereq_first_time; /* not live over crReturn */
  1162. s->ppl.bpp->pls->actx = SSH2_PKTCTX_PASSWORD;
  1163. // WINSCP
  1164. if (s->change_password)
  1165. {
  1166. s->password = dupstr("");
  1167. s->type = AUTH_TYPE_PASSWORD;
  1168. }
  1169. else
  1170. {
  1171. // no indentation to ease merges
  1172. // /WINSCP
  1173. s->cur_prompt = new_prompts();
  1174. s->cur_prompt->to_server = true;
  1175. s->cur_prompt->name = dupstr("SSH password");
  1176. add_prompt(s->cur_prompt, dupprintf("%s@%s's password: ",
  1177. s->username, s->hostname),
  1178. false);
  1179. s->userpass_ret = seat_get_userpass_input(
  1180. s->ppl.seat, s->cur_prompt, NULL);
  1181. while (1) {
  1182. while (s->userpass_ret < 0 &&
  1183. bufchain_size(s->ppl.user_input) > 0)
  1184. s->userpass_ret = seat_get_userpass_input(
  1185. s->ppl.seat, s->cur_prompt, s->ppl.user_input);
  1186. if (s->userpass_ret >= 0)
  1187. break;
  1188. s->want_user_input = true;
  1189. crReturnV;
  1190. s->want_user_input = false;
  1191. }
  1192. if (!s->userpass_ret) {
  1193. /*
  1194. * Failed to get responses. Terminate.
  1195. */
  1196. free_prompts(s->cur_prompt);
  1197. ssh_bpp_queue_disconnect(
  1198. s->ppl.bpp, "Unable to authenticate",
  1199. SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
  1200. ssh_user_close(s->ppl.ssh, "User aborted during password "
  1201. "authentication");
  1202. return;
  1203. }
  1204. /*
  1205. * Squirrel away the password. (We may need it later if
  1206. * asked to change it.)
  1207. */
  1208. s->password = dupstr(s->cur_prompt->prompts[0]->result);
  1209. free_prompts(s->cur_prompt);
  1210. /*
  1211. * Send the password packet.
  1212. *
  1213. * We pad out the password packet to 256 bytes to make
  1214. * it harder for an attacker to find the length of the
  1215. * user's password.
  1216. *
  1217. * Anyone using a password longer than 256 bytes
  1218. * probably doesn't have much to worry about from
  1219. * people who find out how long their password is!
  1220. */
  1221. s->pktout = ssh_bpp_new_pktout(
  1222. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  1223. put_stringz(s->pktout, s->username);
  1224. put_stringz(s->pktout, s->successor_layer->vt->name);
  1225. put_stringz(s->pktout, "password");
  1226. put_bool(s->pktout, false);
  1227. put_stringz(s->pktout, s->password);
  1228. s->pktout->minlen = 256;
  1229. pq_push(s->ppl.out_pq, s->pktout);
  1230. ppl_logevent("Sent password");
  1231. s->type = AUTH_TYPE_PASSWORD;
  1232. /*
  1233. * Wait for next packet, in case it's a password change
  1234. * request.
  1235. */
  1236. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  1237. } // WINSCP
  1238. changereq_first_time = true;
  1239. while ((pktin->type == SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ) ||
  1240. s->change_password) { // WINSCP
  1241. /*
  1242. * We're being asked for a new password
  1243. * (perhaps not for the first time).
  1244. * Loop until the server accepts it.
  1245. */
  1246. bool got_new = false; /* not live over crReturn */
  1247. ptrlen prompt; /* not live over crReturn */
  1248. if (!s->change_password) // WINSCP
  1249. {
  1250. const char *msg;
  1251. if (changereq_first_time)
  1252. msg = "Server requested password change";
  1253. else
  1254. msg = "Server rejected new password";
  1255. ppl_logevent("%s", msg);
  1256. ppl_printf("%s\r\n", msg);
  1257. }
  1258. s->change_password = false; // WINSCP
  1259. prompt = get_string(pktin);
  1260. s->cur_prompt = new_prompts();
  1261. s->cur_prompt->to_server = true;
  1262. s->cur_prompt->name = dupstr("New SSH password");
  1263. s->cur_prompt->instruction = mkstr(prompt);
  1264. s->cur_prompt->instr_reqd = true;
  1265. /*
  1266. * There's no explicit requirement in the protocol
  1267. * for the "old" passwords in the original and
  1268. * password-change messages to be the same, and
  1269. * apparently some Cisco kit supports password change
  1270. * by the user entering a blank password originally
  1271. * and the real password subsequently, so,
  1272. * reluctantly, we prompt for the old password again.
  1273. *
  1274. * (On the other hand, some servers don't even bother
  1275. * to check this field.)
  1276. */
  1277. add_prompt(s->cur_prompt,
  1278. dupstr("Current password (blank for previously entered password): "),
  1279. false);
  1280. add_prompt(s->cur_prompt, dupstr("Enter new password: "),
  1281. false);
  1282. add_prompt(s->cur_prompt, dupstr("Confirm new password: "),
  1283. false);
  1284. /*
  1285. * Loop until the user manages to enter the same
  1286. * password twice.
  1287. */
  1288. while (!got_new) {
  1289. s->userpass_ret = seat_get_userpass_input(
  1290. s->ppl.seat, s->cur_prompt, NULL);
  1291. while (1) {
  1292. while (s->userpass_ret < 0 &&
  1293. bufchain_size(s->ppl.user_input) > 0)
  1294. s->userpass_ret = seat_get_userpass_input(
  1295. s->ppl.seat, s->cur_prompt,
  1296. s->ppl.user_input);
  1297. if (s->userpass_ret >= 0)
  1298. break;
  1299. s->want_user_input = true;
  1300. crReturnV;
  1301. s->want_user_input = false;
  1302. }
  1303. if (!s->userpass_ret) {
  1304. /*
  1305. * Failed to get responses. Terminate.
  1306. */
  1307. /* burn the evidence */
  1308. free_prompts(s->cur_prompt);
  1309. smemclr(s->password, strlen(s->password));
  1310. sfree(s->password);
  1311. ssh_bpp_queue_disconnect(
  1312. s->ppl.bpp, "Unable to authenticate",
  1313. SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
  1314. ssh_user_close(s->ppl.ssh, "User aborted during "
  1315. "password changing");
  1316. return;
  1317. }
  1318. /*
  1319. * If the user specified a new original password
  1320. * (IYSWIM), overwrite any previously specified
  1321. * one.
  1322. * (A side effect is that the user doesn't have to
  1323. * re-enter it if they louse up the new password.)
  1324. */
  1325. if (s->cur_prompt->prompts[0]->result[0]) {
  1326. smemclr(s->password, strlen(s->password));
  1327. /* burn the evidence */
  1328. sfree(s->password);
  1329. s->password =
  1330. dupstr(s->cur_prompt->prompts[0]->result);
  1331. }
  1332. /*
  1333. * Check the two new passwords match.
  1334. */
  1335. got_new = (strcmp(s->cur_prompt->prompts[1]->result,
  1336. s->cur_prompt->prompts[2]->result)
  1337. == 0);
  1338. if (!got_new)
  1339. /* They don't. Silly user. */
  1340. ppl_printf("Passwords do not match\r\n");
  1341. }
  1342. /*
  1343. * Send the new password (along with the old one).
  1344. * (see above for padding rationale)
  1345. */
  1346. s->pktout = ssh_bpp_new_pktout(
  1347. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  1348. put_stringz(s->pktout, s->username);
  1349. put_stringz(s->pktout, s->successor_layer->vt->name);
  1350. put_stringz(s->pktout, "password");
  1351. put_bool(s->pktout, true);
  1352. put_stringz(s->pktout, s->password);
  1353. put_stringz(s->pktout,
  1354. s->cur_prompt->prompts[1]->result);
  1355. free_prompts(s->cur_prompt);
  1356. s->pktout->minlen = 256;
  1357. pq_push(s->ppl.out_pq, s->pktout);
  1358. ppl_logevent("Sent new password");
  1359. /*
  1360. * Now see what the server has to say about it.
  1361. * (If it's CHANGEREQ again, it's not happy with the
  1362. * new password.)
  1363. */
  1364. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  1365. changereq_first_time = false;
  1366. }
  1367. /*
  1368. * We need to reexamine the current pktin at the top
  1369. * of the loop. Either:
  1370. * - we weren't asked to change password at all, in
  1371. * which case it's a SUCCESS or FAILURE with the
  1372. * usual meaning
  1373. * - we sent a new password, and the server was
  1374. * either OK with it (SUCCESS or FAILURE w/partial
  1375. * success) or unhappy with the _old_ password
  1376. * (FAILURE w/o partial success)
  1377. * In any of these cases, we go back to the top of
  1378. * the loop and start again.
  1379. */
  1380. pq_push_front(s->ppl.in_pq, pktin);
  1381. /*
  1382. * We don't need the old password any more, in any
  1383. * case. Burn the evidence.
  1384. */
  1385. smemclr(s->password, strlen(s->password));
  1386. sfree(s->password);
  1387. } else {
  1388. ssh_bpp_queue_disconnect(
  1389. s->ppl.bpp,
  1390. "No supported authentication methods available",
  1391. SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE);
  1392. ssh_sw_abort(s->ppl.ssh, "No supported authentication methods "
  1393. "available (server sent: %s)",
  1394. s->last_methods_string->s);
  1395. return;
  1396. }
  1397. }
  1398. try_new_username:;
  1399. }
  1400. userauth_success:
  1401. /*
  1402. * We've just received USERAUTH_SUCCESS, and we haven't sent
  1403. * any packets since. Signal the transport layer to consider
  1404. * doing an immediate rekey, if it has any reason to want to.
  1405. */
  1406. ssh2_transport_notify_auth_done(s->transport_layer);
  1407. /*
  1408. * Finally, hand over to our successor layer, and return
  1409. * immediately without reaching the crFinishV: ssh_ppl_replace
  1410. * will have freed us, so crFinishV's zeroing-out of crState would
  1411. * be a use-after-free bug.
  1412. */
  1413. {
  1414. PacketProtocolLayer *successor = s->successor_layer;
  1415. s->successor_layer = NULL; /* avoid freeing it ourself */
  1416. ssh_ppl_replace(&s->ppl, successor);
  1417. return; /* we've just freed s, so avoid even touching s->crState */
  1418. }
  1419. crFinishV;
  1420. }
  1421. static void ssh2_userauth_add_session_id(
  1422. struct ssh2_userauth_state *s, strbuf *sigdata)
  1423. {
  1424. if (s->ppl.remote_bugs & BUG_SSH2_PK_SESSIONID) {
  1425. put_data(sigdata, s->session_id.ptr, s->session_id.len);
  1426. } else {
  1427. put_stringpl(sigdata, s->session_id);
  1428. }
  1429. }
  1430. static void ssh2_userauth_agent_query(
  1431. struct ssh2_userauth_state *s, strbuf *req)
  1432. {
  1433. void *response;
  1434. int response_len;
  1435. sfree(s->agent_response_to_free);
  1436. s->agent_response_to_free = NULL;
  1437. s->auth_agent_query = agent_query(req, &response, &response_len,
  1438. ssh2_userauth_agent_callback, s);
  1439. if (!s->auth_agent_query)
  1440. ssh2_userauth_agent_callback(s, response, response_len);
  1441. }
  1442. static void ssh2_userauth_agent_callback(void *uav, void *reply, int replylen)
  1443. {
  1444. struct ssh2_userauth_state *s = (struct ssh2_userauth_state *)uav;
  1445. s->auth_agent_query = NULL;
  1446. s->agent_response_to_free = reply;
  1447. s->agent_response = make_ptrlen(reply, replylen);
  1448. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1449. }
  1450. /*
  1451. * Helper function to add an SSH-2 signature blob to a packet. Expects
  1452. * to be shown the public key blob as well as the signature blob.
  1453. * Normally just appends the sig blob unmodified as a string, except
  1454. * that it optionally breaks it open and fiddle with it to work around
  1455. * BUG_SSH2_RSA_PADDING.
  1456. */
  1457. static void ssh2_userauth_add_sigblob(
  1458. struct ssh2_userauth_state *s, PktOut *pkt, ptrlen pkblob, ptrlen sigblob)
  1459. {
  1460. BinarySource pk[1], sig[1];
  1461. BinarySource_BARE_INIT(pk, pkblob.ptr, pkblob.len);
  1462. BinarySource_BARE_INIT(sig, sigblob.ptr, sigblob.len);
  1463. /* dmemdump(pkblob, pkblob_len); */
  1464. /* dmemdump(sigblob, sigblob_len); */
  1465. /*
  1466. * See if this is in fact an ssh-rsa signature and a buggy
  1467. * server; otherwise we can just do this the easy way.
  1468. */
  1469. if ((s->ppl.remote_bugs & BUG_SSH2_RSA_PADDING) &&
  1470. ptrlen_eq_string(get_string(pk), "ssh-rsa") &&
  1471. ptrlen_eq_string(get_string(sig), "ssh-rsa")) {
  1472. ptrlen mod_mp, sig_mp;
  1473. size_t sig_prefix_len;
  1474. /*
  1475. * Find the modulus and signature integers.
  1476. */
  1477. get_string(pk); /* skip over exponent */
  1478. mod_mp = get_string(pk); /* remember modulus */
  1479. sig_prefix_len = sig->pos;
  1480. sig_mp = get_string(sig);
  1481. if (get_err(pk) || get_err(sig))
  1482. goto give_up;
  1483. /*
  1484. * Find the byte length of the modulus, not counting leading
  1485. * zeroes.
  1486. */
  1487. while (mod_mp.len > 0 && *(const char *)mod_mp.ptr == 0) {
  1488. mod_mp.len--;
  1489. mod_mp.ptr = (const char *)mod_mp.ptr + 1;
  1490. }
  1491. /* debug("modulus length is %d\n", len); */
  1492. /* debug("signature length is %d\n", siglen); */
  1493. if (mod_mp.len != sig_mp.len) {
  1494. strbuf *substr = strbuf_new();
  1495. put_data(substr, sigblob.ptr, sig_prefix_len);
  1496. put_uint32(substr, mod_mp.len);
  1497. put_padding(substr, mod_mp.len - sig_mp.len, 0);
  1498. put_data(substr, sig_mp.ptr, sig_mp.len);
  1499. put_stringsb(pkt, substr);
  1500. return;
  1501. }
  1502. /* Otherwise fall through and do it the easy way. We also come
  1503. * here as a fallback if we discover above that the key blob
  1504. * is misformatted in some way. */
  1505. give_up:;
  1506. }
  1507. put_stringpl(pkt, sigblob);
  1508. }
  1509. #ifndef NO_GSSAPI
  1510. static PktOut *ssh2_userauth_gss_packet(
  1511. struct ssh2_userauth_state *s, const char *authtype)
  1512. {
  1513. strbuf *sb;
  1514. PktOut *p;
  1515. Ssh_gss_buf buf;
  1516. Ssh_gss_buf mic;
  1517. /*
  1518. * The mic is computed over the session id + intended
  1519. * USERAUTH_REQUEST packet.
  1520. */
  1521. sb = strbuf_new();
  1522. put_stringpl(sb, s->session_id);
  1523. put_byte(sb, SSH2_MSG_USERAUTH_REQUEST);
  1524. put_stringz(sb, s->username);
  1525. put_stringz(sb, s->successor_layer->vt->name);
  1526. put_stringz(sb, authtype);
  1527. /* Compute the mic */
  1528. buf.value = sb->s;
  1529. buf.length = sb->len;
  1530. s->shgss->lib->get_mic(s->shgss->lib, s->shgss->ctx, &buf, &mic);
  1531. strbuf_free(sb);
  1532. /* Now we can build the real packet */
  1533. if (strcmp(authtype, "gssapi-with-mic") == 0) {
  1534. p = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_USERAUTH_GSSAPI_MIC);
  1535. } else {
  1536. p = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  1537. put_stringz(p, s->username);
  1538. put_stringz(p, s->successor_layer->vt->name);
  1539. put_stringz(p, authtype);
  1540. }
  1541. put_string(p, mic.value, mic.length);
  1542. return p;
  1543. }
  1544. #endif
  1545. static bool ssh2_userauth_get_specials(
  1546. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx)
  1547. {
  1548. /* No specials provided by this layer. */
  1549. return false;
  1550. }
  1551. static void ssh2_userauth_special_cmd(PacketProtocolLayer *ppl,
  1552. SessionSpecialCode code, int arg)
  1553. {
  1554. /* No specials provided by this layer. */
  1555. }
  1556. static bool ssh2_userauth_want_user_input(PacketProtocolLayer *ppl)
  1557. {
  1558. struct ssh2_userauth_state *s =
  1559. container_of(ppl, struct ssh2_userauth_state, ppl);
  1560. return s->want_user_input;
  1561. }
  1562. static void ssh2_userauth_got_user_input(PacketProtocolLayer *ppl)
  1563. {
  1564. struct ssh2_userauth_state *s =
  1565. container_of(ppl, struct ssh2_userauth_state, ppl);
  1566. if (s->want_user_input)
  1567. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1568. }
  1569. static void ssh2_userauth_reconfigure(PacketProtocolLayer *ppl, Conf *conf)
  1570. {
  1571. struct ssh2_userauth_state *s =
  1572. container_of(ppl, struct ssh2_userauth_state, ppl);
  1573. ssh_ppl_reconfigure(s->successor_layer, conf);
  1574. }