blob: 04ea8dfc1d71887a9c416bb01b5a1abba1b98414 [file] [log] [blame]
Darren Tucker39972492006-07-12 22:22:46 +10001/* $OpenBSD: sshconnect2.c,v 1.156 2006/07/11 20:07:25 stevesk Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100013 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "includes.h"
Damien Miller2eb63402006-03-15 11:09:42 +110027
Damien Miller9cf6d072006-03-15 11:29:24 +110028#include <sys/types.h>
29#include <sys/wait.h>
Damien Millerf17883e2006-03-15 11:45:54 +110030#include <sys/stat.h>
Damien Millereba71ba2000-04-29 23:57:08 +100031
Darren Tucker39972492006-07-12 22:22:46 +100032#include <errno.h>
33
Damien Miller9c617692003-05-14 14:31:11 +100034#include "openbsd-compat/sys-queue.h"
35
Damien Millereba71ba2000-04-29 23:57:08 +100036#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000037#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100038#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100039#include "buffer.h"
40#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100041#include "compat.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000043#include "cipher.h"
Damien Millereba71ba2000-04-29 23:57:08 +100044#include "kex.h"
45#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100046#include "sshconnect.h"
47#include "authfile.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000048#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100049#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000050#include "log.h"
51#include "readconf.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100052#include "misc.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000053#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000054#include "dispatch.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000055#include "canohost.h"
Ben Lindstrom1bad2562002-06-06 19:57:33 +000056#include "msg.h"
57#include "pathnames.h"
Damien Miller6b4069a2006-06-13 13:05:15 +100058#include "uidswap.h"
Damien Miller874d77b2000-10-14 16:23:11 +110059
Darren Tucker0efd1552003-08-26 11:49:55 +100060#ifdef GSSAPI
61#include "ssh-gss.h"
62#endif
63
Damien Millereba71ba2000-04-29 23:57:08 +100064/* import */
65extern char *client_version_string;
66extern char *server_version_string;
67extern Options options;
68
69/*
70 * SSH2 key exchange
71 */
72
Ben Lindstrom46c16222000-12-22 01:43:59 +000073u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100074u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100075
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000076char *xxx_host;
77struct sockaddr *xxx_hostaddr;
78
Ben Lindstromf28f6342001-04-04 02:03:04 +000079Kex *xxx_kex = NULL;
80
Ben Lindstrombba81212001-06-25 05:01:22 +000081static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000082verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000083{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000084 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +100085 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000086 return 0;
87}
88
Damien Millereba71ba2000-04-29 23:57:08 +100089void
Damien Miller874d77b2000-10-14 16:23:11 +110090ssh_kex2(char *host, struct sockaddr *hostaddr)
91{
Damien Miller874d77b2000-10-14 16:23:11 +110092 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000093
94 xxx_host = host;
95 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +110096
Damien Millere39cacc2000-11-29 12:18:44 +110097 if (options.ciphers == (char *)-1) {
Damien Miller996acd22003-04-09 20:59:48 +100098 logit("No valid ciphers for protocol version 2 given, using defaults.");
Damien Millere39cacc2000-11-29 12:18:44 +110099 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100100 }
101 if (options.ciphers != NULL) {
102 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
103 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
104 }
Damien Millera0ff4662001-03-30 10:49:35 +1000105 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
106 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
107 myproposal[PROPOSAL_ENC_ALGS_STOC] =
108 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100109 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000110 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000111 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100112 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000113 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000114 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100115 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000116 if (options.macs != NULL) {
117 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
118 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
119 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000120 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100121 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000122 options.hostkeyalgorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100123
Damien Millera5539d22003-04-09 20:50:06 +1000124 if (options.rekey_limit)
125 packet_set_rekey_limit(options.rekey_limit);
126
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000127 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000128 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100129 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000130 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100131 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100132 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000133 kex->client_version_string=client_version_string;
134 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000135 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100136
Ben Lindstromf28f6342001-04-04 02:03:04 +0000137 xxx_kex = kex;
138
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000139 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100140
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000141 session_id2 = kex->session_id;
142 session_id2_len = kex->session_id_len;
143
Damien Miller874d77b2000-10-14 16:23:11 +1100144#ifdef DEBUG_KEXDH
145 /* send 1st encrypted/maced/compressed message */
146 packet_start(SSH2_MSG_IGNORE);
147 packet_put_cstring("markus");
148 packet_send();
149 packet_write_wait();
150#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000151}
Damien Millerb1715dc2000-05-30 13:44:51 +1000152
Damien Millereba71ba2000-04-29 23:57:08 +1000153/*
154 * Authenticate user
155 */
Damien Miller62cee002000-09-23 17:15:56 +1100156
157typedef struct Authctxt Authctxt;
158typedef struct Authmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000159typedef struct identity Identity;
160typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100161
Damien Miller280ecfb2003-05-14 13:46:00 +1000162struct identity {
163 TAILQ_ENTRY(identity) next;
164 AuthenticationConnection *ac; /* set if agent supports key */
165 Key *key; /* public/private key */
166 char *filename; /* comment for agent-only keys */
167 int tried;
168 int isprivate; /* key points to the private key */
169};
170TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100171
172struct Authctxt {
173 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000174 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100175 const char *host;
176 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100177 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100178 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000179 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000180 /* pubkey */
Damien Miller280ecfb2003-05-14 13:46:00 +1000181 Idlist keys;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000182 AuthenticationConnection *agent;
183 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000184 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000185 /* kbd-interactive */
186 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000187 /* generic */
188 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100189};
190struct Authmethod {
191 char *name; /* string to compare against server's list */
192 int (*userauth)(Authctxt *authctxt);
193 int *enabled; /* flag in option struct that enables method */
194 int *batch_flag; /* flag in option struct that disables method */
195};
196
Damien Miller630d6f42002-01-22 23:17:30 +1100197void input_userauth_success(int, u_int32_t, void *);
198void input_userauth_failure(int, u_int32_t, void *);
199void input_userauth_banner(int, u_int32_t, void *);
200void input_userauth_error(int, u_int32_t, void *);
201void input_userauth_info_req(int, u_int32_t, void *);
202void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000203void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100204
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000205int userauth_none(Authctxt *);
206int userauth_pubkey(Authctxt *);
207int userauth_passwd(Authctxt *);
208int userauth_kbdint(Authctxt *);
209int userauth_hostbased(Authctxt *);
Damien Miller3ab496b2003-05-14 13:47:37 +1000210int userauth_kerberos(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100211
Darren Tucker0efd1552003-08-26 11:49:55 +1000212#ifdef GSSAPI
213int userauth_gssapi(Authctxt *authctxt);
214void input_gssapi_response(int type, u_int32_t, void *);
215void input_gssapi_token(int type, u_int32_t, void *);
216void input_gssapi_hash(int type, u_int32_t, void *);
217void input_gssapi_error(int, u_int32_t, void *);
218void input_gssapi_errtok(int, u_int32_t, void *);
219#endif
220
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000221void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000222
Damien Miller280ecfb2003-05-14 13:46:00 +1000223static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000224static void pubkey_prepare(Authctxt *);
225static void pubkey_cleanup(Authctxt *);
226static Key *load_identity_file(char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000227
Ben Lindstrombba81212001-06-25 05:01:22 +0000228static Authmethod *authmethod_get(char *authlist);
229static Authmethod *authmethod_lookup(const char *name);
230static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100231
232Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000233#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100234 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000235 userauth_gssapi,
236 &options.gss_authentication,
237 NULL},
238#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000239 {"hostbased",
240 userauth_hostbased,
241 &options.hostbased_authentication,
242 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000243 {"publickey",
244 userauth_pubkey,
245 &options.pubkey_authentication,
246 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100247 {"keyboard-interactive",
248 userauth_kbdint,
249 &options.kbd_interactive_authentication,
250 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000251 {"password",
252 userauth_passwd,
253 &options.password_authentication,
254 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100255 {"none",
256 userauth_none,
257 NULL,
258 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100259 {NULL, NULL, NULL, NULL}
260};
261
262void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000263ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000264 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100265{
266 Authctxt authctxt;
267 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100268
Ben Lindstrom551ea372001-06-05 18:56:16 +0000269 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000270 options.kbd_interactive_authentication = 1;
271
Damien Miller62cee002000-09-23 17:15:56 +1100272 packet_start(SSH2_MSG_SERVICE_REQUEST);
273 packet_put_cstring("ssh-userauth");
274 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000275 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100276 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100277 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000278 if (type != SSH2_MSG_SERVICE_ACCEPT)
279 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100280 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100281 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000282 debug2("service_accept: %s", reply);
Damien Miller62cee002000-09-23 17:15:56 +1100283 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100284 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000285 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100286 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100287 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000288 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100289
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000290 if (options.preferred_authentications == NULL)
291 options.preferred_authentications = authmethods_get();
292
Damien Miller62cee002000-09-23 17:15:56 +1100293 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000294 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000295 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100296 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000297 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100298 authctxt.host = host;
299 authctxt.service = "ssh-connection"; /* service name */
300 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100301 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000302 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000303 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000304 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000305 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100306 if (authctxt.method == NULL)
307 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100308
309 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100310 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100311
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000312 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100313 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
314 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000315 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100316 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
317
Damien Miller280ecfb2003-05-14 13:46:00 +1000318 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000319 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
320
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000321 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100322}
Damien Millerf842fcb2003-05-15 12:01:28 +1000323
Damien Miller62cee002000-09-23 17:15:56 +1100324void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000325userauth(Authctxt *authctxt, char *authlist)
326{
Darren Tucker0efd1552003-08-26 11:49:55 +1000327 if (authctxt->methoddata) {
328 xfree(authctxt->methoddata);
329 authctxt->methoddata = NULL;
330 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000331 if (authlist == NULL) {
332 authlist = authctxt->authlist;
333 } else {
334 if (authctxt->authlist)
335 xfree(authctxt->authlist);
336 authctxt->authlist = authlist;
337 }
338 for (;;) {
339 Authmethod *method = authmethod_get(authlist);
340 if (method == NULL)
341 fatal("Permission denied (%s).", authlist);
342 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000343
344 /* reset the per method handler */
345 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
346 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
347
348 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000349 if (method->userauth(authctxt) != 0) {
350 debug2("we sent a %s packet, wait for reply", method->name);
351 break;
352 } else {
353 debug2("we did not send a packet, disable method");
354 method->enabled = NULL;
355 }
356 }
357}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000358
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000359void
Damien Miller630d6f42002-01-22 23:17:30 +1100360input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100361{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000362 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000363 "type %d", type);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000364}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000365
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000366void
Damien Miller630d6f42002-01-22 23:17:30 +1100367input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000368{
369 char *msg, *lang;
Darren Tucker79644822003-10-08 17:37:58 +1000370
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000371 debug3("input_userauth_banner");
372 msg = packet_get_string(NULL);
373 lang = packet_get_string(NULL);
Darren Tucker046dff22003-10-08 17:32:02 +1000374 if (options.log_level > SYSLOG_LEVEL_QUIET)
375 fprintf(stderr, "%s", msg);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000376 xfree(msg);
377 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100378}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000379
Damien Miller62cee002000-09-23 17:15:56 +1100380void
Damien Miller630d6f42002-01-22 23:17:30 +1100381input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100382{
383 Authctxt *authctxt = ctxt;
384 if (authctxt == NULL)
385 fatal("input_userauth_success: no authentication context");
Darren Tucker79644822003-10-08 17:37:58 +1000386 if (authctxt->authlist) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000387 xfree(authctxt->authlist);
Darren Tucker79644822003-10-08 17:37:58 +1000388 authctxt->authlist = NULL;
389 }
390 if (authctxt->methoddata) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000391 xfree(authctxt->methoddata);
Darren Tucker79644822003-10-08 17:37:58 +1000392 authctxt->methoddata = NULL;
393 }
Damien Miller62cee002000-09-23 17:15:56 +1100394 authctxt->success = 1; /* break out */
395}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000396
Damien Miller62cee002000-09-23 17:15:56 +1100397void
Damien Miller630d6f42002-01-22 23:17:30 +1100398input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100399{
Damien Miller62cee002000-09-23 17:15:56 +1100400 Authctxt *authctxt = ctxt;
401 char *authlist = NULL;
402 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100403
404 if (authctxt == NULL)
405 fatal("input_userauth_failure: no authentication context");
406
Damien Miller874d77b2000-10-14 16:23:11 +1100407 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100408 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100409 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100410
411 if (partial != 0)
Damien Miller996acd22003-04-09 20:59:48 +1000412 logit("Authenticated with partial success.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000413 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100414
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000415 userauth(authctxt, authlist);
416}
417void
Damien Miller630d6f42002-01-22 23:17:30 +1100418input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000419{
420 Authctxt *authctxt = ctxt;
421 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000422 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000423 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000424 int pktype, sent = 0;
425 u_int alen, blen;
426 char *pkalg, *fp;
427 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000428
429 if (authctxt == NULL)
430 fatal("input_userauth_pk_ok: no authentication context");
431 if (datafellows & SSH_BUG_PKOK) {
432 /* this is similar to SSH_BUG_PKAUTH */
433 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
434 pkblob = packet_get_string(&blen);
435 buffer_init(&b);
436 buffer_append(&b, pkblob, blen);
437 pkalg = buffer_get_string(&b, &alen);
438 buffer_free(&b);
439 } else {
440 pkalg = packet_get_string(&alen);
441 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000442 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100443 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000444
Damien Miller280ecfb2003-05-14 13:46:00 +1000445 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000446
Damien Miller280ecfb2003-05-14 13:46:00 +1000447 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
448 debug("unknown pkalg %s", pkalg);
449 goto done;
450 }
451 if ((key = key_from_blob(pkblob, blen)) == NULL) {
452 debug("no key from blob. pkalg %s", pkalg);
453 goto done;
454 }
455 if (key->type != pktype) {
456 error("input_userauth_pk_ok: type mismatch "
457 "for decoded key (received %d, expected %d)",
458 key->type, pktype);
459 goto done;
460 }
461 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
462 debug2("input_userauth_pk_ok: fp %s", fp);
463 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000464
Darren Tuckerd05b6012003-10-15 15:55:59 +1000465 /*
466 * search keys in the reverse order, because last candidate has been
467 * moved to the end of the queue. this also avoids confusion by
468 * duplicate keys
469 */
Damien Miller0b51a522004-04-20 20:07:19 +1000470 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000471 if (key_equal(key, id->key)) {
472 sent = sign_and_send_pubkey(authctxt, id);
473 break;
474 }
475 }
476done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000477 if (key != NULL)
478 key_free(key);
479 xfree(pkalg);
480 xfree(pkblob);
481
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000482 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000483 if (sent == 0)
484 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100485}
486
Darren Tucker0efd1552003-08-26 11:49:55 +1000487#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100488int
Darren Tucker0efd1552003-08-26 11:49:55 +1000489userauth_gssapi(Authctxt *authctxt)
490{
491 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100492 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000493 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000494 OM_uint32 min;
495 int ok = 0;
496
497 /* Try one GSSAPI method at a time, rather than sending them all at
498 * once. */
499
Darren Tucker56afe142003-11-03 20:06:14 +1100500 if (gss_supported == NULL)
501 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000502
503 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100504 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000505 if (gssctxt)
506 ssh_gssapi_delete_ctx(&gssctxt);
507 ssh_gssapi_build_ctx(&gssctxt);
Darren Tucker56afe142003-11-03 20:06:14 +1100508 ssh_gssapi_set_oid(gssctxt, &gss_supported->elements[mech]);
Darren Tucker0efd1552003-08-26 11:49:55 +1000509
510 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100511 if (gss_supported->elements[mech].length < 128 &&
Darren Tucker0efd1552003-08-26 11:49:55 +1000512 !GSS_ERROR(ssh_gssapi_import_name(gssctxt,
513 authctxt->host))) {
514 ok = 1; /* Mechanism works */
515 } else {
516 mech++;
517 }
518 }
519
Damien Miller5790b592006-03-26 13:54:03 +1100520 if (!ok) {
521 ssh_gssapi_delete_ctx(&gssctxt);
Damien Millereccb9de2005-06-17 12:59:34 +1000522 return 0;
Damien Miller5790b592006-03-26 13:54:03 +1100523 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000524
525 authctxt->methoddata=(void *)gssctxt;
526
527 packet_start(SSH2_MSG_USERAUTH_REQUEST);
528 packet_put_cstring(authctxt->server_user);
529 packet_put_cstring(authctxt->service);
530 packet_put_cstring(authctxt->method->name);
531
532 packet_put_int(1);
533
Darren Tucker655a5e02003-11-03 20:09:03 +1100534 packet_put_int((gss_supported->elements[mech].length) + 2);
535 packet_put_char(SSH_GSS_OIDTYPE);
536 packet_put_char(gss_supported->elements[mech].length);
537 packet_put_raw(gss_supported->elements[mech].elements,
538 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000539
540 packet_send();
541
542 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
543 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
544 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
545 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
546
547 mech++; /* Move along to next candidate */
548
549 return 1;
550}
551
Damien Miller91c6aa42003-11-17 21:20:18 +1100552static OM_uint32
553process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
554{
555 Authctxt *authctxt = ctxt;
556 Gssctxt *gssctxt = authctxt->methoddata;
557 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000558 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
559 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100560 OM_uint32 status, ms, flags;
561 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100562
Damien Miller91c6aa42003-11-17 21:20:18 +1100563 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100564 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100565
566 if (send_tok.length > 0) {
567 if (GSS_ERROR(status))
568 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
569 else
570 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100571
Damien Miller91c6aa42003-11-17 21:20:18 +1100572 packet_put_string(send_tok.value, send_tok.length);
573 packet_send();
574 gss_release_buffer(&ms, &send_tok);
575 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100576
Damien Miller91c6aa42003-11-17 21:20:18 +1100577 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100578 /* send either complete or MIC, depending on mechanism */
579 if (!(flags & GSS_C_INTEG_FLAG)) {
580 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
581 packet_send();
582 } else {
583 ssh_gssapi_buildmic(&b, authctxt->server_user,
584 authctxt->service, "gssapi-with-mic");
585
586 gssbuf.value = buffer_ptr(&b);
587 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100588
Damien Miller0425d402003-11-17 22:18:21 +1100589 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100590
Damien Miller0425d402003-11-17 22:18:21 +1100591 if (!GSS_ERROR(status)) {
592 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
593 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100594
Damien Miller0425d402003-11-17 22:18:21 +1100595 packet_send();
596 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100597
Damien Miller0425d402003-11-17 22:18:21 +1100598 buffer_free(&b);
599 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100600 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100601 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100602
Damien Miller91c6aa42003-11-17 21:20:18 +1100603 return status;
604}
605
Darren Tucker0efd1552003-08-26 11:49:55 +1000606void
607input_gssapi_response(int type, u_int32_t plen, void *ctxt)
608{
609 Authctxt *authctxt = ctxt;
610 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000611 int oidlen;
612 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000613
614 if (authctxt == NULL)
615 fatal("input_gssapi_response: no authentication context");
616 gssctxt = authctxt->methoddata;
617
618 /* Setup our OID */
619 oidv = packet_get_string(&oidlen);
620
Darren Tucker655a5e02003-11-03 20:09:03 +1100621 if (oidlen <= 2 ||
622 oidv[0] != SSH_GSS_OIDTYPE ||
623 oidv[1] != oidlen - 2) {
Damien Miller91c6aa42003-11-17 21:20:18 +1100624 xfree(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100625 debug("Badly encoded mechanism OID received");
626 userauth(authctxt, NULL);
Darren Tucker655a5e02003-11-03 20:09:03 +1100627 return;
Darren Tucker0efd1552003-08-26 11:49:55 +1000628 }
629
Darren Tucker655a5e02003-11-03 20:09:03 +1100630 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
631 fatal("Server returned different OID than expected");
632
Darren Tucker0efd1552003-08-26 11:49:55 +1000633 packet_check_eom();
634
635 xfree(oidv);
636
Damien Miller91c6aa42003-11-17 21:20:18 +1100637 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000638 /* Start again with next method on list */
639 debug("Trying to start again");
640 userauth(authctxt, NULL);
641 return;
642 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000643}
644
645void
646input_gssapi_token(int type, u_int32_t plen, void *ctxt)
647{
648 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000649 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100650 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000651 u_int slen;
652
653 if (authctxt == NULL)
654 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000655
656 recv_tok.value = packet_get_string(&slen);
657 recv_tok.length = slen; /* safe typecast */
658
659 packet_check_eom();
660
Damien Miller91c6aa42003-11-17 21:20:18 +1100661 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000662
663 xfree(recv_tok.value);
664
665 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000666 /* Start again with the next method in the list */
667 userauth(authctxt, NULL);
668 return;
669 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000670}
671
672void
673input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
674{
675 Authctxt *authctxt = ctxt;
676 Gssctxt *gssctxt;
677 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
678 gss_buffer_desc recv_tok;
679 OM_uint32 status, ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000680 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000681
682 if (authctxt == NULL)
683 fatal("input_gssapi_response: no authentication context");
684 gssctxt = authctxt->methoddata;
685
Darren Tucker600ad8d2003-08-26 12:10:48 +1000686 recv_tok.value = packet_get_string(&len);
687 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000688
689 packet_check_eom();
690
691 /* Stick it into GSSAPI and see what it says */
692 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000693 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000694
695 xfree(recv_tok.value);
696 gss_release_buffer(&ms, &send_tok);
697
698 /* Server will be returning a failed packet after this one */
699}
700
701void
702input_gssapi_error(int type, u_int32_t plen, void *ctxt)
703{
704 OM_uint32 maj, min;
705 char *msg;
706 char *lang;
707
708 maj=packet_get_int();
709 min=packet_get_int();
710 msg=packet_get_string(NULL);
711 lang=packet_get_string(NULL);
712
713 packet_check_eom();
714
Damien Miller15d72a02005-11-05 15:07:33 +1100715 debug("Server GSSAPI Error:\n%s", msg);
Darren Tucker0efd1552003-08-26 11:49:55 +1000716 xfree(msg);
717 xfree(lang);
718}
719#endif /* GSSAPI */
720
Damien Millereba71ba2000-04-29 23:57:08 +1000721int
Damien Miller874d77b2000-10-14 16:23:11 +1100722userauth_none(Authctxt *authctxt)
723{
724 /* initial userauth request */
725 packet_start(SSH2_MSG_USERAUTH_REQUEST);
726 packet_put_cstring(authctxt->server_user);
727 packet_put_cstring(authctxt->service);
728 packet_put_cstring(authctxt->method->name);
729 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100730 return 1;
731}
732
733int
Damien Miller62cee002000-09-23 17:15:56 +1100734userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000735{
Damien Millere247cc42000-05-07 12:03:14 +1000736 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000737 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000738 char *password;
739
Damien Millerd3a18572000-06-07 19:55:44 +1000740 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000741 return 0;
742
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000743 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000744 error("Permission denied, please try again.");
745
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000746 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100747 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000748 password = read_passphrase(prompt, 0);
749 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100750 packet_put_cstring(authctxt->server_user);
751 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100752 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000753 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000754 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000755 memset(password, 0, strlen(password));
756 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100757 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000758 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000759
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000760 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000761 &input_userauth_passwd_changereq);
762
Damien Millereba71ba2000-04-29 23:57:08 +1000763 return 1;
764}
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000765/*
766 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
767 */
768void
Tim Ricec8549622002-03-31 12:49:38 -0800769input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000770{
771 Authctxt *authctxt = ctxt;
772 char *info, *lang, *password = NULL, *retype = NULL;
773 char prompt[150];
774
775 debug2("input_userauth_passwd_changereq");
776
777 if (authctxt == NULL)
778 fatal("input_userauth_passwd_changereq: "
779 "no authentication context");
780
781 info = packet_get_string(NULL);
782 lang = packet_get_string(NULL);
783 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000784 logit("%s", info);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000785 xfree(info);
786 xfree(lang);
787 packet_start(SSH2_MSG_USERAUTH_REQUEST);
788 packet_put_cstring(authctxt->server_user);
789 packet_put_cstring(authctxt->service);
790 packet_put_cstring(authctxt->method->name);
791 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000792 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000793 "Enter %.30s@%.128s's old password: ",
794 authctxt->server_user, authctxt->host);
795 password = read_passphrase(prompt, 0);
796 packet_put_cstring(password);
797 memset(password, 0, strlen(password));
798 xfree(password);
799 password = NULL;
800 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000801 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000802 "Enter %.30s@%.128s's new password: ",
803 authctxt->server_user, authctxt->host);
804 password = read_passphrase(prompt, RP_ALLOW_EOF);
805 if (password == NULL) {
806 /* bail out */
807 return;
808 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000809 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000810 "Retype %.30s@%.128s's new password: ",
811 authctxt->server_user, authctxt->host);
812 retype = read_passphrase(prompt, 0);
813 if (strcmp(password, retype) != 0) {
814 memset(password, 0, strlen(password));
815 xfree(password);
Damien Miller996acd22003-04-09 20:59:48 +1000816 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000817 password = NULL;
818 }
819 memset(retype, 0, strlen(retype));
820 xfree(retype);
821 }
822 packet_put_cstring(password);
823 memset(password, 0, strlen(password));
824 xfree(password);
825 packet_add_padding(64);
826 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000827
828 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000829 &input_userauth_passwd_changereq);
830}
Damien Millereba71ba2000-04-29 23:57:08 +1000831
Ben Lindstrombba81212001-06-25 05:01:22 +0000832static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000833identity_sign(Identity *id, u_char **sigp, u_int *lenp,
834 u_char *data, u_int datalen)
835{
836 Key *prv;
837 int ret;
838
839 /* the agent supports this key */
840 if (id->ac)
841 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
842 data, datalen));
843 /*
844 * we have already loaded the private key or
845 * the private key is stored in external hardware
846 */
847 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
848 return (key_sign(id->key, sigp, lenp, data, datalen));
849 /* load the private key from the file */
850 if ((prv = load_identity_file(id->filename)) == NULL)
851 return (-1);
852 ret = key_sign(prv, sigp, lenp, data, datalen);
853 key_free(prv);
854 return (ret);
855}
856
857static int
858sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +1000859{
860 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000861 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000862 u_int bloblen, slen;
Darren Tucker502d3842003-06-28 12:38:01 +1000863 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000864 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100865 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000866
Ben Lindstromd121f612000-12-03 17:00:47 +0000867 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000868
Damien Miller280ecfb2003-05-14 13:46:00 +1000869 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100870 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000871 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100872 return 0;
873 }
Damien Millereba71ba2000-04-29 23:57:08 +1000874 /* data to be signed */
875 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100876 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000877 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000878 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100879 } else {
880 buffer_put_string(&b, session_id2, session_id2_len);
881 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000882 }
Damien Millereba71ba2000-04-29 23:57:08 +1000883 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100884 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000885 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000886 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000887 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100888 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000889 if (datafellows & SSH_BUG_PKAUTH) {
890 buffer_put_char(&b, have_sig);
891 } else {
892 buffer_put_cstring(&b, authctxt->method->name);
893 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +1000894 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +0000895 }
Damien Millereba71ba2000-04-29 23:57:08 +1000896 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000897
898 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +1000899 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000900 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000901 if (ret == -1) {
902 xfree(blob);
903 buffer_free(&b);
904 return 0;
905 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100906#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000907 buffer_dump(&b);
908#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000909 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000910 buffer_clear(&b);
911 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000912 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000913 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100914 buffer_put_cstring(&b, authctxt->server_user);
915 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100916 buffer_put_cstring(&b, authctxt->method->name);
917 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000918 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +1000919 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +1000920 buffer_put_string(&b, blob, bloblen);
921 }
922 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000923
Damien Millereba71ba2000-04-29 23:57:08 +1000924 /* append signature */
925 buffer_put_string(&b, signature, slen);
926 xfree(signature);
927
928 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000929 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100930 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000931 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000932
933 /* put remaining data from buffer into packet */
934 packet_start(SSH2_MSG_USERAUTH_REQUEST);
935 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
936 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +1000937 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000938
939 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000940}
941
Ben Lindstrombba81212001-06-25 05:01:22 +0000942static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000943send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +1000944{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000945 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000946 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000947
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000948 debug3("send_pubkey_test");
949
Damien Miller280ecfb2003-05-14 13:46:00 +1000950 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000951 /* we cannot handle this key */
952 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +1000953 return 0;
954 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000955 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000956 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +1000957
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000958 packet_start(SSH2_MSG_USERAUTH_REQUEST);
959 packet_put_cstring(authctxt->server_user);
960 packet_put_cstring(authctxt->service);
961 packet_put_cstring(authctxt->method->name);
962 packet_put_char(have_sig);
963 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +1000964 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000965 packet_put_string(blob, bloblen);
966 xfree(blob);
967 packet_send();
968 return 1;
969}
970
Ben Lindstrombba81212001-06-25 05:01:22 +0000971static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000972load_identity_file(char *filename)
973{
974 Key *private;
975 char prompt[300], *passphrase;
Darren Tucker232b76f2006-05-06 17:41:51 +1000976 int perm_ok, quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000977 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000978
Ben Lindstrom329782e2001-03-10 17:08:59 +0000979 if (stat(filename, &st) < 0) {
980 debug3("no such identity: %s", filename);
981 return NULL;
982 }
Darren Tucker232b76f2006-05-06 17:41:51 +1000983 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
984 if (!perm_ok)
985 return NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000986 if (private == NULL) {
987 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000988 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000989 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100990 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100991 for (i = 0; i < options.number_of_password_prompts; i++) {
992 passphrase = read_passphrase(prompt, 0);
993 if (strcmp(passphrase, "") != 0) {
Darren Tucker232b76f2006-05-06 17:41:51 +1000994 private = key_load_private_type(KEY_UNSPEC,
995 filename, passphrase, NULL, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000996 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100997 } else {
998 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000999 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +11001000 }
1001 memset(passphrase, 0, strlen(passphrase));
1002 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001003 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +11001004 break;
1005 debug2("bad passphrase given, try again...");
1006 }
Damien Miller994cf142000-07-21 10:19:44 +10001007 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001008 return private;
1009}
1010
Damien Miller280ecfb2003-05-14 13:46:00 +10001011/*
1012 * try keys in the following order:
1013 * 1. agent keys that are found in the config file
1014 * 2. other agent keys
1015 * 3. keys that are only listed in the config file
1016 */
1017static void
1018pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001019{
Damien Miller280ecfb2003-05-14 13:46:00 +10001020 Identity *id;
1021 Idlist agent, files, *preferred;
1022 Key *key;
1023 AuthenticationConnection *ac;
Damien Millerad833b32000-08-23 10:46:23 +10001024 char *comment;
Damien Miller280ecfb2003-05-14 13:46:00 +10001025 int i, found;
Damien Millerad833b32000-08-23 10:46:23 +10001026
Damien Miller280ecfb2003-05-14 13:46:00 +10001027 TAILQ_INIT(&agent); /* keys from the agent */
1028 TAILQ_INIT(&files); /* keys from the config file */
1029 preferred = &authctxt->keys;
1030 TAILQ_INIT(preferred); /* preferred order of keys */
1031
1032 /* list of keys stored in the filesystem */
1033 for (i = 0; i < options.num_identity_files; i++) {
1034 key = options.identity_keys[i];
1035 if (key && key->type == KEY_RSA1)
1036 continue;
1037 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001038 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001039 id->key = key;
1040 id->filename = xstrdup(options.identity_files[i]);
1041 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001042 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001043 /* list of keys supported by the agent */
1044 if ((ac = ssh_get_authentication_connection())) {
1045 for (key = ssh_get_first_identity(ac, &comment, 2);
1046 key != NULL;
1047 key = ssh_get_next_identity(ac, &comment, 2)) {
1048 found = 0;
1049 TAILQ_FOREACH(id, &files, next) {
Damien Millera8e06ce2003-11-21 23:48:55 +11001050 /* agent keys from the config file are preferred */
Damien Miller280ecfb2003-05-14 13:46:00 +10001051 if (key_equal(key, id->key)) {
1052 key_free(key);
1053 xfree(comment);
1054 TAILQ_REMOVE(&files, id, next);
1055 TAILQ_INSERT_TAIL(preferred, id, next);
1056 id->ac = ac;
1057 found = 1;
1058 break;
1059 }
1060 }
Damien Millerbd394c32004-03-08 23:12:36 +11001061 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001062 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001063 id->key = key;
1064 id->filename = comment;
1065 id->ac = ac;
1066 TAILQ_INSERT_TAIL(&agent, id, next);
1067 }
1068 }
1069 /* append remaining agent keys */
1070 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1071 TAILQ_REMOVE(&agent, id, next);
1072 TAILQ_INSERT_TAIL(preferred, id, next);
1073 }
1074 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +11001075 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001076 /* append remaining keys from the config file */
1077 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1078 TAILQ_REMOVE(&files, id, next);
1079 TAILQ_INSERT_TAIL(preferred, id, next);
1080 }
1081 TAILQ_FOREACH(id, preferred, next) {
1082 debug2("key: %s (%p)", id->filename, id->key);
1083 }
1084}
1085
1086static void
1087pubkey_cleanup(Authctxt *authctxt)
1088{
1089 Identity *id;
1090
1091 if (authctxt->agent != NULL)
1092 ssh_close_authentication_connection(authctxt->agent);
1093 for (id = TAILQ_FIRST(&authctxt->keys); id;
1094 id = TAILQ_FIRST(&authctxt->keys)) {
1095 TAILQ_REMOVE(&authctxt->keys, id, next);
1096 if (id->key)
1097 key_free(id->key);
1098 if (id->filename)
1099 xfree(id->filename);
1100 xfree(id);
1101 }
Damien Millereba71ba2000-04-29 23:57:08 +10001102}
1103
Damien Miller62cee002000-09-23 17:15:56 +11001104int
1105userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001106{
Damien Miller280ecfb2003-05-14 13:46:00 +10001107 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001108 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001109
Damien Miller280ecfb2003-05-14 13:46:00 +10001110 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1111 if (id->tried++)
1112 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001113 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001114 TAILQ_REMOVE(&authctxt->keys, id, next);
1115 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1116 /*
1117 * send a test message if we have the public key. for
1118 * encrypted keys we cannot do this and have to load the
1119 * private key instead
1120 */
1121 if (id->key && id->key->type != KEY_RSA1) {
1122 debug("Offering public key: %s", id->filename);
1123 sent = send_pubkey_test(authctxt, id);
1124 } else if (id->key == NULL) {
1125 debug("Trying private key: %s", id->filename);
1126 id->key = load_identity_file(id->filename);
1127 if (id->key != NULL) {
1128 id->isprivate = 1;
1129 sent = sign_and_send_pubkey(authctxt, id);
1130 key_free(id->key);
1131 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001132 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001133 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001134 if (sent)
1135 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001136 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001137 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001138}
Damien Millereba71ba2000-04-29 23:57:08 +10001139
Damien Miller874d77b2000-10-14 16:23:11 +11001140/*
1141 * Send userauth request message specifying keyboard-interactive method.
1142 */
1143int
1144userauth_kbdint(Authctxt *authctxt)
1145{
1146 static int attempt = 0;
1147
1148 if (attempt++ >= options.number_of_password_prompts)
1149 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001150 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1151 if (attempt > 1 && !authctxt->info_req_seen) {
1152 debug3("userauth_kbdint: disable: no info_req_seen");
1153 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1154 return 0;
1155 }
Damien Miller874d77b2000-10-14 16:23:11 +11001156
1157 debug2("userauth_kbdint");
1158 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1159 packet_put_cstring(authctxt->server_user);
1160 packet_put_cstring(authctxt->service);
1161 packet_put_cstring(authctxt->method->name);
1162 packet_put_cstring(""); /* lang */
1163 packet_put_cstring(options.kbd_interactive_devices ?
1164 options.kbd_interactive_devices : "");
1165 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001166
1167 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1168 return 1;
1169}
1170
1171/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001172 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001173 */
1174void
Damien Miller630d6f42002-01-22 23:17:30 +11001175input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001176{
1177 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001178 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001179 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001180 int echo = 0;
1181
1182 debug2("input_userauth_info_req");
1183
1184 if (authctxt == NULL)
1185 fatal("input_userauth_info_req: no authentication context");
1186
Ben Lindstrom7d199962001-09-12 18:29:00 +00001187 authctxt->info_req_seen = 1;
1188
Damien Miller874d77b2000-10-14 16:23:11 +11001189 name = packet_get_string(NULL);
1190 inst = packet_get_string(NULL);
1191 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001192 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001193 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001194 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001195 logit("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001196 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001197 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001198 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001199
1200 num_prompts = packet_get_int();
1201 /*
1202 * Begin to build info response packet based on prompts requested.
1203 * We commit to providing the correct number of responses, so if
1204 * further on we run into a problem that prevents this, we have to
1205 * be sure and clean this up and send a correct error response.
1206 */
1207 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1208 packet_put_int(num_prompts);
1209
Ben Lindstrom551ea372001-06-05 18:56:16 +00001210 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001211 for (i = 0; i < num_prompts; i++) {
1212 prompt = packet_get_string(NULL);
1213 echo = packet_get_char();
1214
Ben Lindstrom949974b2001-06-25 05:20:31 +00001215 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001216
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001217 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +11001218 memset(response, 0, strlen(response));
1219 xfree(response);
1220 xfree(prompt);
1221 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001222 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001223
Damien Miller9f643902001-11-12 11:02:52 +11001224 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001225 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001226}
Damien Miller62cee002000-09-23 17:15:56 +11001227
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001228static int
Ben Lindstrom5c385522002-06-23 21:23:20 +00001229ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001230 u_char *data, u_int datalen)
1231{
1232 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001233 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001234 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001235 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001236
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001237 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001238
Ben Lindstrom5206b952002-06-06 19:59:29 +00001239 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1240 error("ssh_keysign: no installed: %s", strerror(errno));
1241 return -1;
1242 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001243 if (fflush(stdout) != 0)
1244 error("ssh_keysign: fflush: %s", strerror(errno));
1245 if (pipe(to) < 0) {
1246 error("ssh_keysign: pipe: %s", strerror(errno));
1247 return -1;
1248 }
1249 if (pipe(from) < 0) {
1250 error("ssh_keysign: pipe: %s", strerror(errno));
1251 return -1;
1252 }
1253 if ((pid = fork()) < 0) {
1254 error("ssh_keysign: fork: %s", strerror(errno));
1255 return -1;
1256 }
1257 if (pid == 0) {
Damien Miller2e5fe882006-06-13 13:10:00 +10001258 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001259 close(from[0]);
1260 if (dup2(from[1], STDOUT_FILENO) < 0)
1261 fatal("ssh_keysign: dup2: %s", strerror(errno));
1262 close(to[1]);
1263 if (dup2(to[0], STDIN_FILENO) < 0)
1264 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001265 close(from[1]);
1266 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001267 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001268 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1269 strerror(errno));
1270 }
1271 close(from[1]);
1272 close(to[0]);
1273
1274 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001275 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001276 buffer_put_string(&b, data, datalen);
Damien Miller51bf11f2003-11-17 21:20:47 +11001277 if (ssh_msg_send(to[1], version, &b) == -1)
1278 fatal("ssh_keysign: couldn't send request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001279
Damien Miller901119b2002-10-04 11:10:04 +10001280 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001281 error("ssh_keysign: no reply");
Damien Millerfb1310e2004-01-21 11:02:50 +11001282 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001283 return -1;
1284 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001285 close(from[0]);
1286 close(to[1]);
1287
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001288 while (waitpid(pid, &status, 0) < 0)
1289 if (errno != EINTR)
1290 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001291
Ben Lindstrom5206b952002-06-06 19:59:29 +00001292 if (buffer_get_char(&b) != version) {
1293 error("ssh_keysign: bad version");
Damien Millerfb1310e2004-01-21 11:02:50 +11001294 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001295 return -1;
1296 }
1297 *sigp = buffer_get_string(&b, lenp);
Damien Millerfb1310e2004-01-21 11:02:50 +11001298 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001299
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001300 return 0;
1301}
1302
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001303int
1304userauth_hostbased(Authctxt *authctxt)
1305{
1306 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001307 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001308 Buffer b;
1309 u_char *signature, *blob;
1310 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001311 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001312 u_int blen, slen;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +00001313 int ok, i, len, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001314
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001315 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001316 for (i = 0; i < sensitive->nkeys; i++) {
1317 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001318 if (private && private->type != KEY_RSA1) {
1319 found = 1;
1320 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001321 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001322 break;
1323 }
1324 }
1325 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001326 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001327 return 0;
1328 }
1329 if (key_to_blob(private, &blob, &blen) == 0) {
1330 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001331 return 0;
1332 }
Damien Miller91c18472001-11-12 11:02:03 +11001333 /* figure out a name for the client host */
1334 p = get_local_name(packet_get_connection_in());
1335 if (p == NULL) {
1336 error("userauth_hostbased: cannot get local ipaddr/name");
1337 key_free(private);
Damien Miller5790b592006-03-26 13:54:03 +11001338 xfree(blob);
Damien Miller91c18472001-11-12 11:02:03 +11001339 return 0;
1340 }
1341 len = strlen(p) + 2;
Damien Miller07d86be2006-03-26 14:19:21 +11001342 xasprintf(&chost, "%s.", p);
Damien Miller91c18472001-11-12 11:02:03 +11001343 debug2("userauth_hostbased: chost %s", chost);
Damien Miller00111382003-03-10 11:21:17 +11001344 xfree(p);
Damien Miller91c18472001-11-12 11:02:03 +11001345
Ben Lindstrom671388f2001-04-19 20:40:45 +00001346 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1347 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001348 pkalg = xstrdup(key_ssh_name(private));
1349 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001350 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001351 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001352 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1353 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001354 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001355 buffer_put_cstring(&b, authctxt->method->name);
1356 buffer_put_cstring(&b, pkalg);
1357 buffer_put_string(&b, blob, blen);
1358 buffer_put_cstring(&b, chost);
1359 buffer_put_cstring(&b, authctxt->local_user);
1360#ifdef DEBUG_PK
1361 buffer_dump(&b);
1362#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001363 if (sensitive->external_keysign)
1364 ok = ssh_keysign(private, &signature, &slen,
1365 buffer_ptr(&b), buffer_len(&b));
1366 else
1367 ok = key_sign(private, &signature, &slen,
1368 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001369 key_free(private);
1370 buffer_free(&b);
1371 if (ok != 0) {
1372 error("key_sign failed");
1373 xfree(chost);
1374 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001375 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001376 return 0;
1377 }
1378 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1379 packet_put_cstring(authctxt->server_user);
1380 packet_put_cstring(authctxt->service);
1381 packet_put_cstring(authctxt->method->name);
1382 packet_put_cstring(pkalg);
1383 packet_put_string(blob, blen);
1384 packet_put_cstring(chost);
1385 packet_put_cstring(authctxt->local_user);
1386 packet_put_string(signature, slen);
1387 memset(signature, 's', slen);
1388 xfree(signature);
1389 xfree(chost);
1390 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001391 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001392
1393 packet_send();
1394 return 1;
1395}
1396
Damien Miller62cee002000-09-23 17:15:56 +11001397/* find auth method */
1398
Damien Miller62cee002000-09-23 17:15:56 +11001399/*
1400 * given auth method name, if configurable options permit this method fill
1401 * in auth_ident field and return true, otherwise return false.
1402 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001403static int
Damien Miller62cee002000-09-23 17:15:56 +11001404authmethod_is_enabled(Authmethod *method)
1405{
1406 if (method == NULL)
1407 return 0;
1408 /* return false if options indicate this method is disabled */
1409 if (method->enabled == NULL || *method->enabled == 0)
1410 return 0;
1411 /* return false if batch mode is enabled but method needs interactive mode */
1412 if (method->batch_flag != NULL && *method->batch_flag != 0)
1413 return 0;
1414 return 1;
1415}
1416
Ben Lindstrombba81212001-06-25 05:01:22 +00001417static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001418authmethod_lookup(const char *name)
1419{
1420 Authmethod *method = NULL;
1421 if (name != NULL)
1422 for (method = authmethods; method->name != NULL; method++)
1423 if (strcmp(name, method->name) == 0)
1424 return method;
1425 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1426 return NULL;
1427}
1428
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001429/* XXX internal state */
1430static Authmethod *current = NULL;
1431static char *supported = NULL;
1432static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001433
Damien Miller62cee002000-09-23 17:15:56 +11001434/*
1435 * Given the authentication method list sent by the server, return the
1436 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001437 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001438 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001439static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001440authmethod_get(char *authlist)
1441{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001442 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001443 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001444
Damien Miller62cee002000-09-23 17:15:56 +11001445 /* Use a suitable default if we're passed a nil list. */
1446 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001447 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001448
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001449 if (supported == NULL || strcmp(authlist, supported) != 0) {
1450 debug3("start over, passed a different list %s", authlist);
1451 if (supported != NULL)
1452 xfree(supported);
1453 supported = xstrdup(authlist);
1454 preferred = options.preferred_authentications;
1455 debug3("preferred %s", preferred);
1456 current = NULL;
1457 } else if (current != NULL && authmethod_is_enabled(current))
1458 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001459
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001460 for (;;) {
1461 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001462 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001463 current = NULL;
1464 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001465 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001466 preferred += next;
1467 debug3("authmethod_lookup %s", name);
1468 debug3("remaining preferred: %s", preferred);
1469 if ((current = authmethod_lookup(name)) != NULL &&
1470 authmethod_is_enabled(current)) {
1471 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001472 debug("Next authentication method: %s", name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001473 return current;
1474 }
Damien Millereba71ba2000-04-29 23:57:08 +10001475 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001476}
Damien Miller62cee002000-09-23 17:15:56 +11001477
Ben Lindstrom79073822001-07-04 03:42:30 +00001478static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001479authmethods_get(void)
1480{
1481 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001482 Buffer b;
1483 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001484
Damien Miller0e3b8722002-01-22 23:26:38 +11001485 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001486 for (method = authmethods; method->name != NULL; method++) {
1487 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001488 if (buffer_len(&b) > 0)
1489 buffer_append(&b, ",", 1);
1490 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001491 }
Damien Miller62cee002000-09-23 17:15:56 +11001492 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001493 buffer_append(&b, "\0", 1);
1494 list = xstrdup(buffer_ptr(&b));
1495 buffer_free(&b);
1496 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001497}