ssh2userauth.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768
  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. bufchain_consume(&s->banner, len);
  434. }
  435. }
  436. bufchain_clear(&s->banner);
  437. }
  438. if (pktin && pktin->type == SSH2_MSG_USERAUTH_SUCCESS) {
  439. ppl_logevent("Access granted");
  440. goto userauth_success;
  441. }
  442. if (pktin && pktin->type != SSH2_MSG_USERAUTH_FAILURE &&
  443. s->type != AUTH_TYPE_GSSAPI) {
  444. ssh_proto_error(s->ppl.ssh, "Received unexpected packet "
  445. "in response to authentication request, "
  446. "type %d (%s)", pktin->type,
  447. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  448. s->ppl.bpp->pls->actx,
  449. pktin->type));
  450. return;
  451. }
  452. /*
  453. * OK, we're now sitting on a USERAUTH_FAILURE message, so
  454. * we can look at the string in it and know what we can
  455. * helpfully try next.
  456. */
  457. if (pktin && pktin->type == SSH2_MSG_USERAUTH_FAILURE) {
  458. ptrlen methods = get_string(pktin);
  459. bool partial_success = get_bool(pktin);
  460. if (!partial_success) {
  461. /*
  462. * We have received an unequivocal Access
  463. * Denied. This can translate to a variety of
  464. * messages, or no message at all.
  465. *
  466. * For forms of authentication which are attempted
  467. * implicitly, by which I mean without printing
  468. * anything in the window indicating that we're
  469. * trying them, we should never print 'Access
  470. * denied'.
  471. *
  472. * If we do print a message saying that we're
  473. * attempting some kind of authentication, it's OK
  474. * to print a followup message saying it failed -
  475. * but the message may sometimes be more specific
  476. * than simply 'Access denied'.
  477. *
  478. * Additionally, if we'd just tried password
  479. * authentication, we should break out of this
  480. * whole loop so as to go back to the username
  481. * prompt (iff we're configured to allow
  482. * username change attempts).
  483. */
  484. if (s->type == AUTH_TYPE_NONE) {
  485. /* do nothing */
  486. } else if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD ||
  487. s->type == AUTH_TYPE_PUBLICKEY_OFFER_QUIET) {
  488. if (s->type == AUTH_TYPE_PUBLICKEY_OFFER_LOUD)
  489. ppl_printf("Server refused our key\r\n");
  490. ppl_logevent("Server refused our key");
  491. } else if (s->type == AUTH_TYPE_PUBLICKEY) {
  492. /* This _shouldn't_ happen except by a
  493. * protocol bug causing client and server to
  494. * disagree on what is a correct signature. */
  495. ppl_printf("Server refused public-key signature"
  496. " despite accepting key!\r\n");
  497. ppl_logevent("Server refused public-key signature"
  498. " despite accepting key!");
  499. } else if (s->type==AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET) {
  500. /* quiet, so no ppl_printf */
  501. ppl_logevent("Server refused keyboard-interactive "
  502. "authentication");
  503. } else if (s->type==AUTH_TYPE_GSSAPI) {
  504. /* always quiet, so no ppl_printf */
  505. /* also, the code down in the GSSAPI block has
  506. * already logged this in the Event Log */
  507. } else if (s->type == AUTH_TYPE_KEYBOARD_INTERACTIVE) {
  508. ppl_logevent("Keyboard-interactive authentication "
  509. "failed");
  510. ppl_printf("Access denied\r\n");
  511. } else {
  512. assert(s->type == AUTH_TYPE_PASSWORD);
  513. ppl_logevent("Password authentication failed");
  514. ppl_printf("Access denied\r\n");
  515. if (s->change_username) {
  516. /* XXX perhaps we should allow
  517. * keyboard-interactive to do this too? */
  518. goto try_new_username;
  519. }
  520. }
  521. } else {
  522. ppl_printf("Further authentication required\r\n");
  523. ppl_logevent("Further authentication required");
  524. }
  525. /*
  526. * Save the methods string for use in error messages.
  527. */
  528. s->last_methods_string->len = 0;
  529. put_datapl(s->last_methods_string, methods);
  530. #ifdef WINSCP
  531. ppl_logevent("Server offered these authentication methods: %s", s->last_methods_string->s);
  532. #endif
  533. /*
  534. * Scan it for method identifiers we know about.
  535. */
  536. { // WINSCP
  537. bool srv_pubkey = false, srv_passwd = false;
  538. bool srv_keyb_inter = false, srv_gssapi = false;
  539. bool srv_gssapi_keyex_auth = false;
  540. ptrlen method; // WINSCP
  541. for (; get_commasep_word(&methods, &method) ;) {
  542. if (ptrlen_eq_string(method, "publickey"))
  543. srv_pubkey = true;
  544. else if (ptrlen_eq_string(method, "password"))
  545. srv_passwd = true;
  546. else if (ptrlen_eq_string(method, "keyboard-interactive"))
  547. srv_keyb_inter = true;
  548. else if (ptrlen_eq_string(method, "gssapi-with-mic"))
  549. srv_gssapi = true;
  550. else if (ptrlen_eq_string(method, "gssapi-keyex"))
  551. srv_gssapi_keyex_auth = true;
  552. }
  553. /*
  554. * And combine those flags with our own configuration
  555. * and context to set the main can_foo variables.
  556. */
  557. s->can_pubkey = srv_pubkey;
  558. s->can_passwd = srv_passwd;
  559. s->can_keyb_inter = s->try_ki_auth && srv_keyb_inter;
  560. #ifndef NO_GSSAPI
  561. s->can_gssapi = s->try_gssapi_auth && srv_gssapi &&
  562. s->shgss->libs->nlibraries > 0;
  563. s->can_gssapi_keyex_auth = s->try_gssapi_kex_auth &&
  564. srv_gssapi_keyex_auth &&
  565. s->shgss->libs->nlibraries > 0 && s->shgss->ctx;
  566. #endif
  567. } // WINSCP
  568. }
  569. s->ppl.bpp->pls->actx = SSH2_PKTCTX_NOAUTH;
  570. #ifndef NO_GSSAPI
  571. if (s->can_gssapi_keyex_auth && !s->tried_gssapi_keyex_auth) {
  572. /* gssapi-keyex authentication */
  573. s->type = AUTH_TYPE_GSSAPI;
  574. s->tried_gssapi_keyex_auth = true;
  575. s->ppl.bpp->pls->actx = SSH2_PKTCTX_GSSAPI;
  576. if (s->shgss->lib->gsslogmsg)
  577. ppl_logevent("%s", s->shgss->lib->gsslogmsg);
  578. ppl_logevent("Trying gssapi-keyex...");
  579. s->pktout = ssh2_userauth_gss_packet(s, "gssapi-keyex");
  580. pq_push(s->ppl.out_pq, s->pktout);
  581. s->shgss->lib->release_cred(s->shgss->lib, &s->shgss->ctx);
  582. s->shgss->ctx = NULL;
  583. continue;
  584. } else
  585. #endif /* NO_GSSAPI */
  586. if (s->can_pubkey && !s->done_agent && s->nkeys) {
  587. /*
  588. * Attempt public-key authentication using a key from Pageant.
  589. */
  590. s->ppl.bpp->pls->actx = SSH2_PKTCTX_PUBLICKEY;
  591. ppl_logevent("Trying Pageant key #%d", s->keyi);
  592. /* Unpack key from agent response */
  593. s->pk = get_string(s->asrc);
  594. s->comment = get_string(s->asrc);
  595. {
  596. BinarySource src[1];
  597. BinarySource_BARE_INIT(src, s->pk.ptr, s->pk.len);
  598. s->alg = get_string(src);
  599. }
  600. /* See if server will accept it */
  601. s->pktout = ssh_bpp_new_pktout(
  602. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  603. put_stringz(s->pktout, s->username);
  604. put_stringz(s->pktout, s->successor_layer->vt->name);
  605. put_stringz(s->pktout, "publickey");
  606. /* method */
  607. put_bool(s->pktout, false); /* no signature included */
  608. put_stringpl(s->pktout, s->alg);
  609. put_stringpl(s->pktout, s->pk);
  610. pq_push(s->ppl.out_pq, s->pktout);
  611. s->type = AUTH_TYPE_PUBLICKEY_OFFER_QUIET;
  612. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  613. if (pktin->type != SSH2_MSG_USERAUTH_PK_OK) {
  614. /* Offer of key refused, presumably via
  615. * USERAUTH_FAILURE. Requeue for the next iteration. */
  616. pq_push_front(s->ppl.in_pq, pktin);
  617. } else {
  618. strbuf *agentreq, *sigdata;
  619. if (flags & FLAG_VERBOSE)
  620. ppl_printf("Authenticating with public key "
  621. "\"%.*s\" from agent\r\n",
  622. PTRLEN_PRINTF(s->comment));
  623. /*
  624. * Server is willing to accept the key.
  625. * Construct a SIGN_REQUEST.
  626. */
  627. s->pktout = ssh_bpp_new_pktout(
  628. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  629. put_stringz(s->pktout, s->username);
  630. put_stringz(s->pktout, s->successor_layer->vt->name);
  631. put_stringz(s->pktout, "publickey");
  632. /* method */
  633. put_bool(s->pktout, true); /* signature included */
  634. put_stringpl(s->pktout, s->alg);
  635. put_stringpl(s->pktout, s->pk);
  636. /* Ask agent for signature. */
  637. agentreq = strbuf_new_for_agent_query();
  638. put_byte(agentreq, SSH2_AGENTC_SIGN_REQUEST);
  639. put_stringpl(agentreq, s->pk);
  640. /* Now the data to be signed... */
  641. sigdata = strbuf_new();
  642. ssh2_userauth_add_session_id(s, sigdata);
  643. put_data(sigdata, s->pktout->data + 5,
  644. s->pktout->length - 5);
  645. put_stringsb(agentreq, sigdata);
  646. /* And finally the (zero) flags word. */
  647. put_uint32(agentreq, 0);
  648. ssh2_userauth_agent_query(s, agentreq);
  649. strbuf_free(agentreq);
  650. crWaitUntilV(!s->auth_agent_query);
  651. if (s->agent_response.ptr) {
  652. ptrlen sigblob;
  653. BinarySource src[1];
  654. BinarySource_BARE_INIT(src, s->agent_response.ptr,
  655. s->agent_response.len);
  656. get_uint32(src); /* skip length field */
  657. if (get_byte(src) == SSH2_AGENT_SIGN_RESPONSE &&
  658. (sigblob = get_string(src), !get_err(src))) {
  659. ppl_logevent("Sending Pageant's response");
  660. ssh2_userauth_add_sigblob(s, s->pktout,
  661. s->pk, sigblob);
  662. pq_push(s->ppl.out_pq, s->pktout);
  663. s->type = AUTH_TYPE_PUBLICKEY;
  664. } else {
  665. /* FIXME: less drastic response */
  666. ssh_sw_abort(s->ppl.ssh, "Pageant failed to "
  667. "provide a signature");
  668. return;
  669. }
  670. }
  671. }
  672. /* Do we have any keys left to try? */
  673. if (s->pkblob_pos_in_agent) {
  674. s->done_agent = true;
  675. s->tried_pubkey_config = true;
  676. } else {
  677. s->keyi++;
  678. if (s->keyi >= s->nkeys)
  679. s->done_agent = true;
  680. }
  681. } else if (s->can_pubkey && s->publickey_blob &&
  682. s->privatekey_available && !s->tried_pubkey_config) {
  683. ssh2_userkey *key; /* not live over crReturn */
  684. char *passphrase; /* not live over crReturn */
  685. s->ppl.bpp->pls->actx = SSH2_PKTCTX_PUBLICKEY;
  686. s->tried_pubkey_config = true;
  687. /*
  688. * Try the public key supplied in the configuration.
  689. *
  690. * First, offer the public blob to see if the server is
  691. * willing to accept it.
  692. */
  693. s->pktout = ssh_bpp_new_pktout(
  694. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  695. put_stringz(s->pktout, s->username);
  696. put_stringz(s->pktout, s->successor_layer->vt->name);
  697. put_stringz(s->pktout, "publickey"); /* method */
  698. put_bool(s->pktout, false);
  699. /* no signature included */
  700. put_stringz(s->pktout, s->publickey_algorithm);
  701. put_string(s->pktout, s->publickey_blob->s,
  702. s->publickey_blob->len);
  703. pq_push(s->ppl.out_pq, s->pktout);
  704. ppl_logevent("Offered public key");
  705. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  706. if (pktin->type != SSH2_MSG_USERAUTH_PK_OK) {
  707. /* Key refused. Give up. */
  708. pq_push_front(s->ppl.in_pq, pktin);
  709. s->type = AUTH_TYPE_PUBLICKEY_OFFER_LOUD;
  710. continue; /* process this new message */
  711. }
  712. ppl_logevent("Offer of public key accepted");
  713. /*
  714. * Actually attempt a serious authentication using
  715. * the key.
  716. */
  717. if (flags & FLAG_VERBOSE)
  718. ppl_printf("Authenticating with public key \"%s\"\r\n",
  719. s->publickey_comment);
  720. key = NULL;
  721. while (!key) {
  722. const char *error; /* not live over crReturn */
  723. if (s->privatekey_encrypted) {
  724. /*
  725. * Get a passphrase from the user.
  726. */
  727. s->cur_prompt = new_prompts();
  728. s->cur_prompt->to_server = false;
  729. s->cur_prompt->name = dupstr("SSH key passphrase");
  730. add_prompt(s->cur_prompt,
  731. dupprintf("Passphrase for key \"%s\": ",
  732. s->publickey_comment),
  733. false);
  734. s->userpass_ret = seat_get_userpass_input(
  735. s->ppl.seat, s->cur_prompt, NULL);
  736. while (1) {
  737. while (s->userpass_ret < 0 &&
  738. bufchain_size(s->ppl.user_input) > 0)
  739. s->userpass_ret = seat_get_userpass_input(
  740. s->ppl.seat, s->cur_prompt,
  741. s->ppl.user_input);
  742. if (s->userpass_ret >= 0)
  743. break;
  744. s->want_user_input = true;
  745. crReturnV;
  746. s->want_user_input = false;
  747. }
  748. if (!s->userpass_ret) {
  749. /* Failed to get a passphrase. Terminate. */
  750. free_prompts(s->cur_prompt);
  751. ssh_bpp_queue_disconnect(
  752. s->ppl.bpp, "Unable to authenticate",
  753. SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
  754. ssh_user_close(s->ppl.ssh, "User aborted at "
  755. "passphrase prompt");
  756. return;
  757. }
  758. passphrase =
  759. dupstr(s->cur_prompt->prompts[0]->result);
  760. free_prompts(s->cur_prompt);
  761. } else {
  762. passphrase = NULL; /* no passphrase needed */
  763. }
  764. /*
  765. * Try decrypting the key.
  766. */
  767. key = ssh2_load_userkey(s->keyfile, passphrase, &error);
  768. if (passphrase) {
  769. /* burn the evidence */
  770. smemclr(passphrase, strlen(passphrase));
  771. sfree(passphrase);
  772. }
  773. if (key == SSH2_WRONG_PASSPHRASE || key == NULL) {
  774. if (passphrase &&
  775. (key == SSH2_WRONG_PASSPHRASE)) {
  776. ppl_printf("Wrong passphrase\r\n");
  777. key = NULL;
  778. /* and loop again */
  779. } else {
  780. ppl_printf("Unable to load private key (%s)\r\n",
  781. error);
  782. key = NULL;
  783. break; /* try something else */
  784. }
  785. }
  786. }
  787. if (key) {
  788. strbuf *pkblob, *sigdata, *sigblob;
  789. /*
  790. * We have loaded the private key and the server
  791. * has announced that it's willing to accept it.
  792. * Hallelujah. Generate a signature and send it.
  793. */
  794. s->pktout = ssh_bpp_new_pktout(
  795. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  796. put_stringz(s->pktout, s->username);
  797. put_stringz(s->pktout, s->successor_layer->vt->name);
  798. put_stringz(s->pktout, "publickey"); /* method */
  799. put_bool(s->pktout, true); /* signature follows */
  800. put_stringz(s->pktout, ssh_key_ssh_id(key->key));
  801. pkblob = strbuf_new();
  802. ssh_key_public_blob(key->key, BinarySink_UPCAST(pkblob));
  803. put_string(s->pktout, pkblob->s, pkblob->len);
  804. /*
  805. * The data to be signed is:
  806. *
  807. * string session-id
  808. *
  809. * followed by everything so far placed in the
  810. * outgoing packet.
  811. */
  812. sigdata = strbuf_new();
  813. ssh2_userauth_add_session_id(s, sigdata);
  814. put_data(sigdata, s->pktout->data + 5,
  815. s->pktout->length - 5);
  816. sigblob = strbuf_new();
  817. ssh_key_sign(key->key, ptrlen_from_strbuf(sigdata), 0,
  818. BinarySink_UPCAST(sigblob));
  819. strbuf_free(sigdata);
  820. ssh2_userauth_add_sigblob(
  821. s, s->pktout, ptrlen_from_strbuf(pkblob),
  822. ptrlen_from_strbuf(sigblob));
  823. strbuf_free(pkblob);
  824. strbuf_free(sigblob);
  825. pq_push(s->ppl.out_pq, s->pktout);
  826. ppl_logevent("Sent public key signature");
  827. s->type = AUTH_TYPE_PUBLICKEY;
  828. ssh_key_free(key->key);
  829. sfree(key->comment);
  830. sfree(key);
  831. }
  832. #ifndef NO_GSSAPI
  833. } else if (s->can_gssapi && !s->tried_gssapi) {
  834. /* gssapi-with-mic authentication */
  835. ptrlen data;
  836. s->type = AUTH_TYPE_GSSAPI;
  837. s->tried_gssapi = true;
  838. s->ppl.bpp->pls->actx = SSH2_PKTCTX_GSSAPI;
  839. if (s->shgss->lib->gsslogmsg)
  840. ppl_logevent("%s", s->shgss->lib->gsslogmsg);
  841. /* Sending USERAUTH_REQUEST with "gssapi-with-mic" method */
  842. ppl_logevent("Trying gssapi-with-mic...");
  843. s->pktout = ssh_bpp_new_pktout(
  844. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  845. put_stringz(s->pktout, s->username);
  846. put_stringz(s->pktout, s->successor_layer->vt->name);
  847. put_stringz(s->pktout, "gssapi-with-mic");
  848. ppl_logevent("Attempting GSSAPI authentication");
  849. /* add mechanism info */
  850. s->shgss->lib->indicate_mech(s->shgss->lib, &s->gss_buf);
  851. /* number of GSSAPI mechanisms */
  852. put_uint32(s->pktout, 1);
  853. /* length of OID + 2 */
  854. put_uint32(s->pktout, s->gss_buf.length + 2);
  855. put_byte(s->pktout, SSH2_GSS_OIDTYPE);
  856. /* length of OID */
  857. put_byte(s->pktout, s->gss_buf.length);
  858. put_data(s->pktout, s->gss_buf.value, s->gss_buf.length);
  859. pq_push(s->ppl.out_pq, s->pktout);
  860. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  861. if (pktin->type != SSH2_MSG_USERAUTH_GSSAPI_RESPONSE) {
  862. ppl_logevent("GSSAPI authentication request refused");
  863. pq_push_front(s->ppl.in_pq, pktin);
  864. continue;
  865. }
  866. /* check returned packet ... */
  867. data = get_string(pktin);
  868. s->gss_rcvtok.value = (char *)data.ptr;
  869. s->gss_rcvtok.length = data.len;
  870. if (s->gss_rcvtok.length != s->gss_buf.length + 2 ||
  871. ((char *)s->gss_rcvtok.value)[0] != SSH2_GSS_OIDTYPE ||
  872. ((char *)s->gss_rcvtok.value)[1] != s->gss_buf.length ||
  873. memcmp((char *)s->gss_rcvtok.value + 2,
  874. s->gss_buf.value,s->gss_buf.length) ) {
  875. ppl_logevent("GSSAPI authentication - wrong response "
  876. "from server");
  877. continue;
  878. }
  879. /* Import server name if not cached from KEX */
  880. if (s->shgss->srv_name == GSS_C_NO_NAME) {
  881. // WINSCP
  882. const char * fullhostname = s->fullhostname;
  883. if (s->loghost[0] != '\0')
  884. {
  885. fullhostname = s->loghost;
  886. }
  887. // /WINSCP
  888. s->gss_stat = s->shgss->lib->import_name(
  889. s->shgss->lib, fullhostname, &s->shgss->srv_name); // WINSCP
  890. if (s->gss_stat != SSH_GSS_OK) {
  891. if (s->gss_stat == SSH_GSS_BAD_HOST_NAME)
  892. ppl_logevent("GSSAPI import name failed -"
  893. " Bad service name");
  894. else
  895. ppl_logevent("GSSAPI import name failed");
  896. continue;
  897. }
  898. }
  899. /* Allocate our gss_ctx */
  900. s->gss_stat = s->shgss->lib->acquire_cred(
  901. s->shgss->lib, &s->shgss->ctx, NULL);
  902. if (s->gss_stat != SSH_GSS_OK) {
  903. ppl_logevent("GSSAPI authentication failed to get "
  904. "credentials");
  905. /* The failure was on our side, so the server
  906. * won't be sending a response packet indicating
  907. * failure. Avoid waiting for it next time round
  908. * the loop. */
  909. s->suppress_wait_for_response_packet = true;
  910. continue;
  911. }
  912. /* initial tokens are empty */
  913. SSH_GSS_CLEAR_BUF(&s->gss_rcvtok);
  914. SSH_GSS_CLEAR_BUF(&s->gss_sndtok);
  915. /* now enter the loop */
  916. do {
  917. /*
  918. * When acquire_cred yields no useful expiration, go with
  919. * the service ticket expiration.
  920. */
  921. s->gss_stat = s->shgss->lib->init_sec_context
  922. (s->shgss->lib,
  923. &s->shgss->ctx,
  924. s->shgss->srv_name,
  925. s->gssapi_fwd,
  926. &s->gss_rcvtok,
  927. &s->gss_sndtok,
  928. NULL,
  929. NULL);
  930. if (s->gss_stat!=SSH_GSS_S_COMPLETE &&
  931. s->gss_stat!=SSH_GSS_S_CONTINUE_NEEDED) {
  932. ppl_logevent("GSSAPI authentication initialisation "
  933. "failed");
  934. if (s->shgss->lib->display_status(s->shgss->lib,
  935. s->shgss->ctx, &s->gss_buf) == SSH_GSS_OK) {
  936. ppl_logevent("%s", (char *)s->gss_buf.value);
  937. sfree(s->gss_buf.value);
  938. }
  939. pq_push_front(s->ppl.in_pq, pktin);
  940. break;
  941. }
  942. ppl_logevent("GSSAPI authentication initialised");
  943. /*
  944. * Client and server now exchange tokens until GSSAPI
  945. * no longer says CONTINUE_NEEDED
  946. */
  947. if (s->gss_sndtok.length != 0) {
  948. s->pktout =
  949. ssh_bpp_new_pktout(
  950. s->ppl.bpp, SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
  951. put_string(s->pktout,
  952. s->gss_sndtok.value, s->gss_sndtok.length);
  953. pq_push(s->ppl.out_pq, s->pktout);
  954. s->shgss->lib->free_tok(s->shgss->lib, &s->gss_sndtok);
  955. }
  956. if (s->gss_stat == SSH_GSS_S_CONTINUE_NEEDED) {
  957. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  958. if (pktin->type == SSH2_MSG_USERAUTH_GSSAPI_ERRTOK) {
  959. /*
  960. * Per RFC 4462 section 3.9, this packet
  961. * type MUST immediately precede an
  962. * ordinary USERAUTH_FAILURE.
  963. *
  964. * We currently don't know how to do
  965. * anything with the GSSAPI error token
  966. * contained in this packet, so we ignore
  967. * it and just wait for the following
  968. * FAILURE.
  969. */
  970. crMaybeWaitUntilV(
  971. (pktin = ssh2_userauth_pop(s)) != NULL);
  972. if (pktin->type != SSH2_MSG_USERAUTH_FAILURE) {
  973. ssh_proto_error(
  974. s->ppl.ssh, "Received unexpected packet "
  975. "after SSH_MSG_USERAUTH_GSSAPI_ERRTOK "
  976. "(expected SSH_MSG_USERAUTH_FAILURE): "
  977. "type %d (%s)", pktin->type,
  978. ssh2_pkt_type(s->ppl.bpp->pls->kctx,
  979. s->ppl.bpp->pls->actx,
  980. pktin->type));
  981. return;
  982. }
  983. }
  984. if (pktin->type == SSH2_MSG_USERAUTH_FAILURE) {
  985. ppl_logevent("GSSAPI authentication failed");
  986. s->gss_stat = SSH_GSS_FAILURE;
  987. pq_push_front(s->ppl.in_pq, pktin);
  988. break;
  989. } else if (pktin->type !=
  990. SSH2_MSG_USERAUTH_GSSAPI_TOKEN) {
  991. ppl_logevent("GSSAPI authentication -"
  992. " bad server response");
  993. s->gss_stat = SSH_GSS_FAILURE;
  994. break;
  995. }
  996. data = get_string(pktin);
  997. s->gss_rcvtok.value = (char *)data.ptr;
  998. s->gss_rcvtok.length = data.len;
  999. }
  1000. } while (s-> gss_stat == SSH_GSS_S_CONTINUE_NEEDED);
  1001. if (s->gss_stat != SSH_GSS_OK) {
  1002. s->shgss->lib->release_cred(s->shgss->lib, &s->shgss->ctx);
  1003. continue;
  1004. }
  1005. ppl_logevent("GSSAPI authentication loop finished OK");
  1006. /* Now send the MIC */
  1007. s->pktout = ssh2_userauth_gss_packet(s, "gssapi-with-mic");
  1008. pq_push(s->ppl.out_pq, s->pktout);
  1009. s->shgss->lib->release_cred(s->shgss->lib, &s->shgss->ctx);
  1010. continue;
  1011. #endif
  1012. } else if (s->can_keyb_inter && !s->kbd_inter_refused) {
  1013. /*
  1014. * Keyboard-interactive authentication.
  1015. */
  1016. s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE;
  1017. s->ppl.bpp->pls->actx = SSH2_PKTCTX_KBDINTER;
  1018. s->pktout = ssh_bpp_new_pktout(
  1019. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  1020. put_stringz(s->pktout, s->username);
  1021. put_stringz(s->pktout, s->successor_layer->vt->name);
  1022. put_stringz(s->pktout, "keyboard-interactive");
  1023. /* method */
  1024. put_stringz(s->pktout, ""); /* lang */
  1025. put_stringz(s->pktout, ""); /* submethods */
  1026. pq_push(s->ppl.out_pq, s->pktout);
  1027. ppl_logevent("Attempting keyboard-interactive authentication");
  1028. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  1029. if (pktin->type != SSH2_MSG_USERAUTH_INFO_REQUEST) {
  1030. /* Server is not willing to do keyboard-interactive
  1031. * at all (or, bizarrely but legally, accepts the
  1032. * user without actually issuing any prompts).
  1033. * Give up on it entirely. */
  1034. pq_push_front(s->ppl.in_pq, pktin);
  1035. s->type = AUTH_TYPE_KEYBOARD_INTERACTIVE_QUIET;
  1036. s->kbd_inter_refused = true; /* don't try it again */
  1037. continue;
  1038. }
  1039. /*
  1040. * Loop while the server continues to send INFO_REQUESTs.
  1041. */
  1042. while (pktin->type == SSH2_MSG_USERAUTH_INFO_REQUEST) {
  1043. ptrlen name, inst;
  1044. int i;
  1045. /*
  1046. * We've got a fresh USERAUTH_INFO_REQUEST.
  1047. * Get the preamble and start building a prompt.
  1048. */
  1049. name = get_string(pktin);
  1050. inst = get_string(pktin);
  1051. get_string(pktin); /* skip language tag */
  1052. s->cur_prompt = new_prompts();
  1053. s->cur_prompt->to_server = true;
  1054. /*
  1055. * Get any prompt(s) from the packet.
  1056. */
  1057. s->num_prompts = get_uint32(pktin);
  1058. for (i = 0; i < s->num_prompts; i++) {
  1059. ptrlen prompt;
  1060. bool echo;
  1061. static char noprompt[] =
  1062. "<server failed to send prompt>: ";
  1063. prompt = get_string(pktin);
  1064. echo = get_bool(pktin);
  1065. if (!prompt.len) {
  1066. prompt.ptr = noprompt;
  1067. prompt.len = lenof(noprompt)-1;
  1068. }
  1069. add_prompt(s->cur_prompt, mkstr(prompt), echo);
  1070. }
  1071. if (name.len) {
  1072. /* FIXME: better prefix to distinguish from
  1073. * local prompts? */
  1074. s->cur_prompt->name =
  1075. dupprintf("SSH server: %.*s", PTRLEN_PRINTF(name));
  1076. s->cur_prompt->name_reqd = true;
  1077. } else {
  1078. s->cur_prompt->name =
  1079. dupstr("SSH server authentication");
  1080. s->cur_prompt->name_reqd = false;
  1081. }
  1082. /* We add a prefix to try to make it clear that a prompt
  1083. * has come from the server.
  1084. * FIXME: ugly to print "Using..." in prompt _every_
  1085. * time round. Can this be done more subtly? */
  1086. /* Special case: for reasons best known to themselves,
  1087. * some servers send k-i requests with no prompts and
  1088. * nothing to display. Keep quiet in this case. */
  1089. if (s->num_prompts || name.len || inst.len) {
  1090. s->cur_prompt->instruction =
  1091. dupprintf("Using keyboard-interactive "
  1092. "authentication.%s%.*s",
  1093. inst.len ? "\n" : "",
  1094. PTRLEN_PRINTF(inst));
  1095. s->cur_prompt->instr_reqd = true;
  1096. } else {
  1097. s->cur_prompt->instr_reqd = false;
  1098. }
  1099. /*
  1100. * Display any instructions, and get the user's
  1101. * response(s).
  1102. */
  1103. s->userpass_ret = seat_get_userpass_input(
  1104. s->ppl.seat, s->cur_prompt, NULL);
  1105. while (1) {
  1106. while (s->userpass_ret < 0 &&
  1107. bufchain_size(s->ppl.user_input) > 0)
  1108. s->userpass_ret = seat_get_userpass_input(
  1109. s->ppl.seat, s->cur_prompt, s->ppl.user_input);
  1110. if (s->userpass_ret >= 0)
  1111. break;
  1112. s->want_user_input = true;
  1113. crReturnV;
  1114. s->want_user_input = false;
  1115. }
  1116. if (!s->userpass_ret) {
  1117. /*
  1118. * Failed to get responses. Terminate.
  1119. */
  1120. free_prompts(s->cur_prompt);
  1121. ssh_bpp_queue_disconnect(
  1122. s->ppl.bpp, "Unable to authenticate",
  1123. SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
  1124. ssh_user_close(s->ppl.ssh, "User aborted during "
  1125. "keyboard-interactive authentication");
  1126. return;
  1127. }
  1128. /*
  1129. * Send the response(s) to the server.
  1130. */
  1131. s->pktout = ssh_bpp_new_pktout(
  1132. s->ppl.bpp, SSH2_MSG_USERAUTH_INFO_RESPONSE);
  1133. put_uint32(s->pktout, s->num_prompts);
  1134. for (i=0; i < s->num_prompts; i++) {
  1135. put_stringz(s->pktout,
  1136. s->cur_prompt->prompts[i]->result);
  1137. }
  1138. s->pktout->minlen = 256;
  1139. pq_push(s->ppl.out_pq, s->pktout);
  1140. /*
  1141. * Free the prompts structure from this iteration.
  1142. * If there's another, a new one will be allocated
  1143. * when we return to the top of this while loop.
  1144. */
  1145. free_prompts(s->cur_prompt);
  1146. /*
  1147. * Get the next packet in case it's another
  1148. * INFO_REQUEST.
  1149. */
  1150. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  1151. }
  1152. /*
  1153. * We should have SUCCESS or FAILURE now.
  1154. */
  1155. pq_push_front(s->ppl.in_pq, pktin);
  1156. } else if (s->can_passwd) {
  1157. /*
  1158. * Plain old password authentication.
  1159. */
  1160. bool changereq_first_time; /* not live over crReturn */
  1161. s->ppl.bpp->pls->actx = SSH2_PKTCTX_PASSWORD;
  1162. // WINSCP
  1163. if (s->change_password)
  1164. {
  1165. s->password = dupstr("");
  1166. s->type = AUTH_TYPE_PASSWORD;
  1167. }
  1168. else
  1169. {
  1170. // no indentation to ease merges
  1171. // /WINSCP
  1172. s->cur_prompt = new_prompts();
  1173. s->cur_prompt->to_server = true;
  1174. s->cur_prompt->name = dupstr("SSH password");
  1175. add_prompt(s->cur_prompt, dupprintf("%s@%s's password: ",
  1176. s->username, s->hostname),
  1177. false);
  1178. s->userpass_ret = seat_get_userpass_input(
  1179. s->ppl.seat, s->cur_prompt, NULL);
  1180. while (1) {
  1181. while (s->userpass_ret < 0 &&
  1182. bufchain_size(s->ppl.user_input) > 0)
  1183. s->userpass_ret = seat_get_userpass_input(
  1184. s->ppl.seat, s->cur_prompt, s->ppl.user_input);
  1185. if (s->userpass_ret >= 0)
  1186. break;
  1187. s->want_user_input = true;
  1188. crReturnV;
  1189. s->want_user_input = false;
  1190. }
  1191. if (!s->userpass_ret) {
  1192. /*
  1193. * Failed to get responses. Terminate.
  1194. */
  1195. free_prompts(s->cur_prompt);
  1196. ssh_bpp_queue_disconnect(
  1197. s->ppl.bpp, "Unable to authenticate",
  1198. SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
  1199. ssh_user_close(s->ppl.ssh, "User aborted during password "
  1200. "authentication");
  1201. return;
  1202. }
  1203. /*
  1204. * Squirrel away the password. (We may need it later if
  1205. * asked to change it.)
  1206. */
  1207. s->password = dupstr(s->cur_prompt->prompts[0]->result);
  1208. free_prompts(s->cur_prompt);
  1209. /*
  1210. * Send the password packet.
  1211. *
  1212. * We pad out the password packet to 256 bytes to make
  1213. * it harder for an attacker to find the length of the
  1214. * user's password.
  1215. *
  1216. * Anyone using a password longer than 256 bytes
  1217. * probably doesn't have much to worry about from
  1218. * people who find out how long their password is!
  1219. */
  1220. s->pktout = ssh_bpp_new_pktout(
  1221. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  1222. put_stringz(s->pktout, s->username);
  1223. put_stringz(s->pktout, s->successor_layer->vt->name);
  1224. put_stringz(s->pktout, "password");
  1225. put_bool(s->pktout, false);
  1226. put_stringz(s->pktout, s->password);
  1227. s->pktout->minlen = 256;
  1228. pq_push(s->ppl.out_pq, s->pktout);
  1229. ppl_logevent("Sent password");
  1230. s->type = AUTH_TYPE_PASSWORD;
  1231. /*
  1232. * Wait for next packet, in case it's a password change
  1233. * request.
  1234. */
  1235. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  1236. } // WINSCP
  1237. changereq_first_time = true;
  1238. while ((pktin->type == SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ) ||
  1239. s->change_password) { // WINSCP
  1240. /*
  1241. * We're being asked for a new password
  1242. * (perhaps not for the first time).
  1243. * Loop until the server accepts it.
  1244. */
  1245. bool got_new = false; /* not live over crReturn */
  1246. ptrlen prompt; /* not live over crReturn */
  1247. if (!s->change_password) // WINSCP
  1248. {
  1249. const char *msg;
  1250. if (changereq_first_time)
  1251. msg = "Server requested password change";
  1252. else
  1253. msg = "Server rejected new password";
  1254. ppl_logevent("%s", msg);
  1255. ppl_printf("%s\r\n", msg);
  1256. }
  1257. s->change_password = false; // WINSCP
  1258. prompt = get_string(pktin);
  1259. s->cur_prompt = new_prompts();
  1260. s->cur_prompt->to_server = true;
  1261. s->cur_prompt->name = dupstr("New SSH password");
  1262. s->cur_prompt->instruction = mkstr(prompt);
  1263. s->cur_prompt->instr_reqd = true;
  1264. /*
  1265. * There's no explicit requirement in the protocol
  1266. * for the "old" passwords in the original and
  1267. * password-change messages to be the same, and
  1268. * apparently some Cisco kit supports password change
  1269. * by the user entering a blank password originally
  1270. * and the real password subsequently, so,
  1271. * reluctantly, we prompt for the old password again.
  1272. *
  1273. * (On the other hand, some servers don't even bother
  1274. * to check this field.)
  1275. */
  1276. add_prompt(s->cur_prompt,
  1277. dupstr("Current password (blank for previously entered password): "),
  1278. false);
  1279. add_prompt(s->cur_prompt, dupstr("Enter new password: "),
  1280. false);
  1281. add_prompt(s->cur_prompt, dupstr("Confirm new password: "),
  1282. false);
  1283. /*
  1284. * Loop until the user manages to enter the same
  1285. * password twice.
  1286. */
  1287. while (!got_new) {
  1288. s->userpass_ret = seat_get_userpass_input(
  1289. s->ppl.seat, s->cur_prompt, NULL);
  1290. while (1) {
  1291. while (s->userpass_ret < 0 &&
  1292. bufchain_size(s->ppl.user_input) > 0)
  1293. s->userpass_ret = seat_get_userpass_input(
  1294. s->ppl.seat, s->cur_prompt,
  1295. s->ppl.user_input);
  1296. if (s->userpass_ret >= 0)
  1297. break;
  1298. s->want_user_input = true;
  1299. crReturnV;
  1300. s->want_user_input = false;
  1301. }
  1302. if (!s->userpass_ret) {
  1303. /*
  1304. * Failed to get responses. Terminate.
  1305. */
  1306. /* burn the evidence */
  1307. free_prompts(s->cur_prompt);
  1308. smemclr(s->password, strlen(s->password));
  1309. sfree(s->password);
  1310. ssh_bpp_queue_disconnect(
  1311. s->ppl.bpp, "Unable to authenticate",
  1312. SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER);
  1313. ssh_user_close(s->ppl.ssh, "User aborted during "
  1314. "password changing");
  1315. return;
  1316. }
  1317. /*
  1318. * If the user specified a new original password
  1319. * (IYSWIM), overwrite any previously specified
  1320. * one.
  1321. * (A side effect is that the user doesn't have to
  1322. * re-enter it if they louse up the new password.)
  1323. */
  1324. if (s->cur_prompt->prompts[0]->result[0]) {
  1325. smemclr(s->password, strlen(s->password));
  1326. /* burn the evidence */
  1327. sfree(s->password);
  1328. s->password =
  1329. dupstr(s->cur_prompt->prompts[0]->result);
  1330. }
  1331. /*
  1332. * Check the two new passwords match.
  1333. */
  1334. got_new = (strcmp(s->cur_prompt->prompts[1]->result,
  1335. s->cur_prompt->prompts[2]->result)
  1336. == 0);
  1337. if (!got_new)
  1338. /* They don't. Silly user. */
  1339. ppl_printf("Passwords do not match\r\n");
  1340. }
  1341. /*
  1342. * Send the new password (along with the old one).
  1343. * (see above for padding rationale)
  1344. */
  1345. s->pktout = ssh_bpp_new_pktout(
  1346. s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  1347. put_stringz(s->pktout, s->username);
  1348. put_stringz(s->pktout, s->successor_layer->vt->name);
  1349. put_stringz(s->pktout, "password");
  1350. put_bool(s->pktout, true);
  1351. put_stringz(s->pktout, s->password);
  1352. put_stringz(s->pktout,
  1353. s->cur_prompt->prompts[1]->result);
  1354. free_prompts(s->cur_prompt);
  1355. s->pktout->minlen = 256;
  1356. pq_push(s->ppl.out_pq, s->pktout);
  1357. ppl_logevent("Sent new password");
  1358. /*
  1359. * Now see what the server has to say about it.
  1360. * (If it's CHANGEREQ again, it's not happy with the
  1361. * new password.)
  1362. */
  1363. crMaybeWaitUntilV((pktin = ssh2_userauth_pop(s)) != NULL);
  1364. changereq_first_time = false;
  1365. }
  1366. /*
  1367. * We need to reexamine the current pktin at the top
  1368. * of the loop. Either:
  1369. * - we weren't asked to change password at all, in
  1370. * which case it's a SUCCESS or FAILURE with the
  1371. * usual meaning
  1372. * - we sent a new password, and the server was
  1373. * either OK with it (SUCCESS or FAILURE w/partial
  1374. * success) or unhappy with the _old_ password
  1375. * (FAILURE w/o partial success)
  1376. * In any of these cases, we go back to the top of
  1377. * the loop and start again.
  1378. */
  1379. pq_push_front(s->ppl.in_pq, pktin);
  1380. /*
  1381. * We don't need the old password any more, in any
  1382. * case. Burn the evidence.
  1383. */
  1384. smemclr(s->password, strlen(s->password));
  1385. sfree(s->password);
  1386. } else {
  1387. ssh_bpp_queue_disconnect(
  1388. s->ppl.bpp,
  1389. "No supported authentication methods available",
  1390. SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE);
  1391. ssh_sw_abort(s->ppl.ssh, "No supported authentication methods "
  1392. "available (server sent: %s)",
  1393. s->last_methods_string->s);
  1394. return;
  1395. }
  1396. }
  1397. try_new_username:;
  1398. }
  1399. userauth_success:
  1400. /*
  1401. * We've just received USERAUTH_SUCCESS, and we haven't sent
  1402. * any packets since. Signal the transport layer to consider
  1403. * doing an immediate rekey, if it has any reason to want to.
  1404. */
  1405. ssh2_transport_notify_auth_done(s->transport_layer);
  1406. /*
  1407. * Finally, hand over to our successor layer, and return
  1408. * immediately without reaching the crFinishV: ssh_ppl_replace
  1409. * will have freed us, so crFinishV's zeroing-out of crState would
  1410. * be a use-after-free bug.
  1411. */
  1412. {
  1413. PacketProtocolLayer *successor = s->successor_layer;
  1414. s->successor_layer = NULL; /* avoid freeing it ourself */
  1415. ssh_ppl_replace(&s->ppl, successor);
  1416. return; /* we've just freed s, so avoid even touching s->crState */
  1417. }
  1418. crFinishV;
  1419. }
  1420. static void ssh2_userauth_add_session_id(
  1421. struct ssh2_userauth_state *s, strbuf *sigdata)
  1422. {
  1423. if (s->ppl.remote_bugs & BUG_SSH2_PK_SESSIONID) {
  1424. put_datapl(sigdata, s->session_id);
  1425. } else {
  1426. put_stringpl(sigdata, s->session_id);
  1427. }
  1428. }
  1429. static void ssh2_userauth_agent_query(
  1430. struct ssh2_userauth_state *s, strbuf *req)
  1431. {
  1432. void *response;
  1433. int response_len;
  1434. sfree(s->agent_response_to_free);
  1435. s->agent_response_to_free = NULL;
  1436. s->auth_agent_query = agent_query(req, &response, &response_len,
  1437. ssh2_userauth_agent_callback, s);
  1438. if (!s->auth_agent_query)
  1439. ssh2_userauth_agent_callback(s, response, response_len);
  1440. }
  1441. static void ssh2_userauth_agent_callback(void *uav, void *reply, int replylen)
  1442. {
  1443. struct ssh2_userauth_state *s = (struct ssh2_userauth_state *)uav;
  1444. s->auth_agent_query = NULL;
  1445. s->agent_response_to_free = reply;
  1446. s->agent_response = make_ptrlen(reply, replylen);
  1447. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1448. }
  1449. /*
  1450. * Helper function to add an SSH-2 signature blob to a packet. Expects
  1451. * to be shown the public key blob as well as the signature blob.
  1452. * Normally just appends the sig blob unmodified as a string, except
  1453. * that it optionally breaks it open and fiddle with it to work around
  1454. * BUG_SSH2_RSA_PADDING.
  1455. */
  1456. static void ssh2_userauth_add_sigblob(
  1457. struct ssh2_userauth_state *s, PktOut *pkt, ptrlen pkblob, ptrlen sigblob)
  1458. {
  1459. BinarySource pk[1], sig[1];
  1460. BinarySource_BARE_INIT(pk, pkblob.ptr, pkblob.len);
  1461. BinarySource_BARE_INIT(sig, sigblob.ptr, sigblob.len);
  1462. /* dmemdump(pkblob, pkblob_len); */
  1463. /* dmemdump(sigblob, sigblob_len); */
  1464. /*
  1465. * See if this is in fact an ssh-rsa signature and a buggy
  1466. * server; otherwise we can just do this the easy way.
  1467. */
  1468. if ((s->ppl.remote_bugs & BUG_SSH2_RSA_PADDING) &&
  1469. ptrlen_eq_string(get_string(pk), "ssh-rsa") &&
  1470. ptrlen_eq_string(get_string(sig), "ssh-rsa")) {
  1471. ptrlen mod_mp, sig_mp;
  1472. size_t sig_prefix_len;
  1473. /*
  1474. * Find the modulus and signature integers.
  1475. */
  1476. get_string(pk); /* skip over exponent */
  1477. mod_mp = get_string(pk); /* remember modulus */
  1478. sig_prefix_len = sig->pos;
  1479. sig_mp = get_string(sig);
  1480. if (get_err(pk) || get_err(sig))
  1481. goto give_up;
  1482. /*
  1483. * Find the byte length of the modulus, not counting leading
  1484. * zeroes.
  1485. */
  1486. while (mod_mp.len > 0 && *(const char *)mod_mp.ptr == 0) {
  1487. mod_mp.len--;
  1488. mod_mp.ptr = (const char *)mod_mp.ptr + 1;
  1489. }
  1490. /* debug("modulus length is %d\n", len); */
  1491. /* debug("signature length is %d\n", siglen); */
  1492. if (mod_mp.len != sig_mp.len) {
  1493. strbuf *substr = strbuf_new();
  1494. put_data(substr, sigblob.ptr, sig_prefix_len);
  1495. put_uint32(substr, mod_mp.len);
  1496. put_padding(substr, mod_mp.len - sig_mp.len, 0);
  1497. put_datapl(substr, sig_mp);
  1498. put_stringsb(pkt, substr);
  1499. return;
  1500. }
  1501. /* Otherwise fall through and do it the easy way. We also come
  1502. * here as a fallback if we discover above that the key blob
  1503. * is misformatted in some way. */
  1504. give_up:;
  1505. }
  1506. put_stringpl(pkt, sigblob);
  1507. }
  1508. #ifndef NO_GSSAPI
  1509. static PktOut *ssh2_userauth_gss_packet(
  1510. struct ssh2_userauth_state *s, const char *authtype)
  1511. {
  1512. strbuf *sb;
  1513. PktOut *p;
  1514. Ssh_gss_buf buf;
  1515. Ssh_gss_buf mic;
  1516. /*
  1517. * The mic is computed over the session id + intended
  1518. * USERAUTH_REQUEST packet.
  1519. */
  1520. sb = strbuf_new();
  1521. put_stringpl(sb, s->session_id);
  1522. put_byte(sb, SSH2_MSG_USERAUTH_REQUEST);
  1523. put_stringz(sb, s->username);
  1524. put_stringz(sb, s->successor_layer->vt->name);
  1525. put_stringz(sb, authtype);
  1526. /* Compute the mic */
  1527. buf.value = sb->s;
  1528. buf.length = sb->len;
  1529. s->shgss->lib->get_mic(s->shgss->lib, s->shgss->ctx, &buf, &mic);
  1530. strbuf_free(sb);
  1531. /* Now we can build the real packet */
  1532. if (strcmp(authtype, "gssapi-with-mic") == 0) {
  1533. p = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_USERAUTH_GSSAPI_MIC);
  1534. } else {
  1535. p = ssh_bpp_new_pktout(s->ppl.bpp, SSH2_MSG_USERAUTH_REQUEST);
  1536. put_stringz(p, s->username);
  1537. put_stringz(p, s->successor_layer->vt->name);
  1538. put_stringz(p, authtype);
  1539. }
  1540. put_string(p, mic.value, mic.length);
  1541. return p;
  1542. }
  1543. #endif
  1544. static bool ssh2_userauth_get_specials(
  1545. PacketProtocolLayer *ppl, add_special_fn_t add_special, void *ctx)
  1546. {
  1547. /* No specials provided by this layer. */
  1548. return false;
  1549. }
  1550. static void ssh2_userauth_special_cmd(PacketProtocolLayer *ppl,
  1551. SessionSpecialCode code, int arg)
  1552. {
  1553. /* No specials provided by this layer. */
  1554. }
  1555. static bool ssh2_userauth_want_user_input(PacketProtocolLayer *ppl)
  1556. {
  1557. struct ssh2_userauth_state *s =
  1558. container_of(ppl, struct ssh2_userauth_state, ppl);
  1559. return s->want_user_input;
  1560. }
  1561. static void ssh2_userauth_got_user_input(PacketProtocolLayer *ppl)
  1562. {
  1563. struct ssh2_userauth_state *s =
  1564. container_of(ppl, struct ssh2_userauth_state, ppl);
  1565. if (s->want_user_input)
  1566. queue_idempotent_callback(&s->ppl.ic_process_queue);
  1567. }
  1568. static void ssh2_userauth_reconfigure(PacketProtocolLayer *ppl, Conf *conf)
  1569. {
  1570. struct ssh2_userauth_state *s =
  1571. container_of(ppl, struct ssh2_userauth_state, ppl);
  1572. ssh_ppl_reconfigure(s->successor_layer, conf);
  1573. }