blob: 74d699ff2fb158bd314e91c02f8e6e5fd5f4bbfd [file] [log] [blame]
Damien Millereba71ba2000-04-29 23:57:08 +10001/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100012 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "includes.h"
Damien Miller280ecfb2003-05-14 13:46:00 +100026RCSID("$OpenBSD: sshconnect2.c,v 1.117 2003/05/12 16:55:37 markus Exp $");
Damien Millereba71ba2000-04-29 23:57:08 +100027
28#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000029#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100030#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100031#include "buffer.h"
32#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100033#include "compat.h"
Damien Millereba71ba2000-04-29 23:57:08 +100034#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "cipher.h"
Damien Millereba71ba2000-04-29 23:57:08 +100036#include "kex.h"
37#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100038#include "sshconnect.h"
39#include "authfile.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000040#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100041#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000042#include "log.h"
43#include "readconf.h"
44#include "readpass.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000045#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000046#include "dispatch.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000047#include "canohost.h"
Ben Lindstrom1bad2562002-06-06 19:57:33 +000048#include "msg.h"
49#include "pathnames.h"
Damien Miller874d77b2000-10-14 16:23:11 +110050
Damien Millereba71ba2000-04-29 23:57:08 +100051/* import */
52extern char *client_version_string;
53extern char *server_version_string;
54extern Options options;
55
56/*
57 * SSH2 key exchange
58 */
59
Ben Lindstrom46c16222000-12-22 01:43:59 +000060u_char *session_id2 = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100061int session_id2_len = 0;
62
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000063char *xxx_host;
64struct sockaddr *xxx_hostaddr;
65
Ben Lindstromf28f6342001-04-04 02:03:04 +000066Kex *xxx_kex = NULL;
67
Ben Lindstrombba81212001-06-25 05:01:22 +000068static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000069verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000070{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000071 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +100072 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000073 return 0;
74}
75
Damien Millereba71ba2000-04-29 23:57:08 +100076void
Damien Miller874d77b2000-10-14 16:23:11 +110077ssh_kex2(char *host, struct sockaddr *hostaddr)
78{
Damien Miller874d77b2000-10-14 16:23:11 +110079 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000080
81 xxx_host = host;
82 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +110083
Damien Millere39cacc2000-11-29 12:18:44 +110084 if (options.ciphers == (char *)-1) {
Damien Miller996acd22003-04-09 20:59:48 +100085 logit("No valid ciphers for protocol version 2 given, using defaults.");
Damien Millere39cacc2000-11-29 12:18:44 +110086 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +110087 }
88 if (options.ciphers != NULL) {
89 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
90 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
91 }
Damien Millera0ff4662001-03-30 10:49:35 +100092 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
93 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
94 myproposal[PROPOSAL_ENC_ALGS_STOC] =
95 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +110096 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +000097 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Ben Lindstrom343010a2002-07-04 00:16:25 +000098 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +110099 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000100 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Ben Lindstrom343010a2002-07-04 00:16:25 +0000101 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100102 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000103 if (options.macs != NULL) {
104 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
105 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
106 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000107 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100108 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000109 options.hostkeyalgorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100110
Damien Millera5539d22003-04-09 20:50:06 +1000111 if (options.rekey_limit)
112 packet_set_rekey_limit(options.rekey_limit);
113
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000114 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000115 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100116 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
117 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000118 kex->client_version_string=client_version_string;
119 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000120 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100121
Ben Lindstromf28f6342001-04-04 02:03:04 +0000122 xxx_kex = kex;
123
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000124 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100125
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000126 session_id2 = kex->session_id;
127 session_id2_len = kex->session_id_len;
128
Damien Miller874d77b2000-10-14 16:23:11 +1100129#ifdef DEBUG_KEXDH
130 /* send 1st encrypted/maced/compressed message */
131 packet_start(SSH2_MSG_IGNORE);
132 packet_put_cstring("markus");
133 packet_send();
134 packet_write_wait();
135#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000136}
Damien Millerb1715dc2000-05-30 13:44:51 +1000137
Damien Millereba71ba2000-04-29 23:57:08 +1000138/*
139 * Authenticate user
140 */
Damien Miller62cee002000-09-23 17:15:56 +1100141
142typedef struct Authctxt Authctxt;
143typedef struct Authmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000144typedef struct identity Identity;
145typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100146
Damien Miller280ecfb2003-05-14 13:46:00 +1000147struct identity {
148 TAILQ_ENTRY(identity) next;
149 AuthenticationConnection *ac; /* set if agent supports key */
150 Key *key; /* public/private key */
151 char *filename; /* comment for agent-only keys */
152 int tried;
153 int isprivate; /* key points to the private key */
154};
155TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100156
157struct Authctxt {
158 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000159 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100160 const char *host;
161 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100162 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100163 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000164 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000165 /* pubkey */
Damien Miller280ecfb2003-05-14 13:46:00 +1000166 Idlist keys;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000167 AuthenticationConnection *agent;
168 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000169 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000170 /* kbd-interactive */
171 int info_req_seen;
Damien Miller62cee002000-09-23 17:15:56 +1100172};
173struct Authmethod {
174 char *name; /* string to compare against server's list */
175 int (*userauth)(Authctxt *authctxt);
176 int *enabled; /* flag in option struct that enables method */
177 int *batch_flag; /* flag in option struct that disables method */
178};
179
Damien Miller630d6f42002-01-22 23:17:30 +1100180void input_userauth_success(int, u_int32_t, void *);
181void input_userauth_failure(int, u_int32_t, void *);
182void input_userauth_banner(int, u_int32_t, void *);
183void input_userauth_error(int, u_int32_t, void *);
184void input_userauth_info_req(int, u_int32_t, void *);
185void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000186void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100187
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000188int userauth_none(Authctxt *);
189int userauth_pubkey(Authctxt *);
190int userauth_passwd(Authctxt *);
191int userauth_kbdint(Authctxt *);
192int userauth_hostbased(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100193
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000194void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000195
Damien Miller280ecfb2003-05-14 13:46:00 +1000196static int sign_and_send_pubkey(Authctxt *, Identity *);
Ben Lindstrombba81212001-06-25 05:01:22 +0000197static void clear_auth_state(Authctxt *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000198static void pubkey_prepare(Authctxt *);
199static void pubkey_cleanup(Authctxt *);
200static Key *load_identity_file(char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000201
Ben Lindstrombba81212001-06-25 05:01:22 +0000202static Authmethod *authmethod_get(char *authlist);
203static Authmethod *authmethod_lookup(const char *name);
204static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100205
206Authmethod authmethods[] = {
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000207 {"hostbased",
208 userauth_hostbased,
209 &options.hostbased_authentication,
210 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000211 {"publickey",
212 userauth_pubkey,
213 &options.pubkey_authentication,
214 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100215 {"keyboard-interactive",
216 userauth_kbdint,
217 &options.kbd_interactive_authentication,
218 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000219 {"password",
220 userauth_passwd,
221 &options.password_authentication,
222 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100223 {"none",
224 userauth_none,
225 NULL,
226 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100227 {NULL, NULL, NULL, NULL}
228};
229
230void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000231ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000232 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100233{
234 Authctxt authctxt;
235 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100236
Ben Lindstrom551ea372001-06-05 18:56:16 +0000237 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000238 options.kbd_interactive_authentication = 1;
239
Damien Miller62cee002000-09-23 17:15:56 +1100240 packet_start(SSH2_MSG_SERVICE_REQUEST);
241 packet_put_cstring("ssh-userauth");
242 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000243 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100244 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100245 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000246 if (type != SSH2_MSG_SERVICE_ACCEPT)
247 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100248 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100249 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000250 debug2("service_accept: %s", reply);
Damien Miller62cee002000-09-23 17:15:56 +1100251 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100252 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000253 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100254 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100255 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000256 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100257
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000258 if (options.preferred_authentications == NULL)
259 options.preferred_authentications = authmethods_get();
260
Damien Miller62cee002000-09-23 17:15:56 +1100261 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000262 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000263 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100264 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000265 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100266 authctxt.host = host;
267 authctxt.service = "ssh-connection"; /* service name */
268 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100269 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000270 authctxt.authlist = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000271 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000272 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100273 if (authctxt.method == NULL)
274 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100275
276 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100277 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100278
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000279 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100280 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
281 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000282 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100283 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
284
Damien Miller280ecfb2003-05-14 13:46:00 +1000285 pubkey_cleanup(&authctxt);
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000286 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100287}
288void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000289userauth(Authctxt *authctxt, char *authlist)
290{
291 if (authlist == NULL) {
292 authlist = authctxt->authlist;
293 } else {
294 if (authctxt->authlist)
295 xfree(authctxt->authlist);
296 authctxt->authlist = authlist;
297 }
298 for (;;) {
299 Authmethod *method = authmethod_get(authlist);
300 if (method == NULL)
301 fatal("Permission denied (%s).", authlist);
302 authctxt->method = method;
303 if (method->userauth(authctxt) != 0) {
304 debug2("we sent a %s packet, wait for reply", method->name);
305 break;
306 } else {
307 debug2("we did not send a packet, disable method");
308 method->enabled = NULL;
309 }
310 }
311}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000312
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000313void
Damien Miller630d6f42002-01-22 23:17:30 +1100314input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100315{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000316 fatal("input_userauth_error: bad message during authentication: "
317 "type %d", type);
318}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000319
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000320void
Damien Miller630d6f42002-01-22 23:17:30 +1100321input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000322{
323 char *msg, *lang;
324 debug3("input_userauth_banner");
325 msg = packet_get_string(NULL);
326 lang = packet_get_string(NULL);
327 fprintf(stderr, "%s", msg);
328 xfree(msg);
329 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100330}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000331
Damien Miller62cee002000-09-23 17:15:56 +1100332void
Damien Miller630d6f42002-01-22 23:17:30 +1100333input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100334{
335 Authctxt *authctxt = ctxt;
336 if (authctxt == NULL)
337 fatal("input_userauth_success: no authentication context");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000338 if (authctxt->authlist)
339 xfree(authctxt->authlist);
340 clear_auth_state(authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100341 authctxt->success = 1; /* break out */
342}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000343
Damien Miller62cee002000-09-23 17:15:56 +1100344void
Damien Miller630d6f42002-01-22 23:17:30 +1100345input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100346{
Damien Miller62cee002000-09-23 17:15:56 +1100347 Authctxt *authctxt = ctxt;
348 char *authlist = NULL;
349 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100350
351 if (authctxt == NULL)
352 fatal("input_userauth_failure: no authentication context");
353
Damien Miller874d77b2000-10-14 16:23:11 +1100354 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100355 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100356 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100357
358 if (partial != 0)
Damien Miller996acd22003-04-09 20:59:48 +1000359 logit("Authenticated with partial success.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000360 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100361
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000362 clear_auth_state(authctxt);
363 userauth(authctxt, authlist);
364}
365void
Damien Miller630d6f42002-01-22 23:17:30 +1100366input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000367{
368 Authctxt *authctxt = ctxt;
369 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000370 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000371 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000372 int pktype, sent = 0;
373 u_int alen, blen;
374 char *pkalg, *fp;
375 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000376
377 if (authctxt == NULL)
378 fatal("input_userauth_pk_ok: no authentication context");
379 if (datafellows & SSH_BUG_PKOK) {
380 /* this is similar to SSH_BUG_PKAUTH */
381 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
382 pkblob = packet_get_string(&blen);
383 buffer_init(&b);
384 buffer_append(&b, pkblob, blen);
385 pkalg = buffer_get_string(&b, &alen);
386 buffer_free(&b);
387 } else {
388 pkalg = packet_get_string(&alen);
389 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000390 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100391 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000392
Damien Miller280ecfb2003-05-14 13:46:00 +1000393 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000394
Damien Miller280ecfb2003-05-14 13:46:00 +1000395 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
396 debug("unknown pkalg %s", pkalg);
397 goto done;
398 }
399 if ((key = key_from_blob(pkblob, blen)) == NULL) {
400 debug("no key from blob. pkalg %s", pkalg);
401 goto done;
402 }
403 if (key->type != pktype) {
404 error("input_userauth_pk_ok: type mismatch "
405 "for decoded key (received %d, expected %d)",
406 key->type, pktype);
407 goto done;
408 }
409 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
410 debug2("input_userauth_pk_ok: fp %s", fp);
411 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000412
Damien Miller280ecfb2003-05-14 13:46:00 +1000413 TAILQ_FOREACH(id, &authctxt->keys, next) {
414 if (key_equal(key, id->key)) {
415 sent = sign_and_send_pubkey(authctxt, id);
416 break;
417 }
418 }
419done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000420 if (key != NULL)
421 key_free(key);
422 xfree(pkalg);
423 xfree(pkblob);
424
425 /* unregister */
426 clear_auth_state(authctxt);
427 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
428
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000429 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000430 if (sent == 0)
431 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100432}
433
Damien Millereba71ba2000-04-29 23:57:08 +1000434int
Damien Miller874d77b2000-10-14 16:23:11 +1100435userauth_none(Authctxt *authctxt)
436{
437 /* initial userauth request */
438 packet_start(SSH2_MSG_USERAUTH_REQUEST);
439 packet_put_cstring(authctxt->server_user);
440 packet_put_cstring(authctxt->service);
441 packet_put_cstring(authctxt->method->name);
442 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100443 return 1;
444}
445
446int
Damien Miller62cee002000-09-23 17:15:56 +1100447userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000448{
Damien Millere247cc42000-05-07 12:03:14 +1000449 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000450 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000451 char *password;
452
Damien Millerd3a18572000-06-07 19:55:44 +1000453 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000454 return 0;
455
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000456 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000457 error("Permission denied, please try again.");
458
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000459 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100460 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000461 password = read_passphrase(prompt, 0);
462 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100463 packet_put_cstring(authctxt->server_user);
464 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100465 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000466 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000467 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000468 memset(password, 0, strlen(password));
469 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100470 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000471 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000472
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000473 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000474 &input_userauth_passwd_changereq);
475
Damien Millereba71ba2000-04-29 23:57:08 +1000476 return 1;
477}
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000478/*
479 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
480 */
481void
Tim Ricec8549622002-03-31 12:49:38 -0800482input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000483{
484 Authctxt *authctxt = ctxt;
485 char *info, *lang, *password = NULL, *retype = NULL;
486 char prompt[150];
487
488 debug2("input_userauth_passwd_changereq");
489
490 if (authctxt == NULL)
491 fatal("input_userauth_passwd_changereq: "
492 "no authentication context");
493
494 info = packet_get_string(NULL);
495 lang = packet_get_string(NULL);
496 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000497 logit("%s", info);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000498 xfree(info);
499 xfree(lang);
500 packet_start(SSH2_MSG_USERAUTH_REQUEST);
501 packet_put_cstring(authctxt->server_user);
502 packet_put_cstring(authctxt->service);
503 packet_put_cstring(authctxt->method->name);
504 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000505 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000506 "Enter %.30s@%.128s's old password: ",
507 authctxt->server_user, authctxt->host);
508 password = read_passphrase(prompt, 0);
509 packet_put_cstring(password);
510 memset(password, 0, strlen(password));
511 xfree(password);
512 password = NULL;
513 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000514 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000515 "Enter %.30s@%.128s's new password: ",
516 authctxt->server_user, authctxt->host);
517 password = read_passphrase(prompt, RP_ALLOW_EOF);
518 if (password == NULL) {
519 /* bail out */
520 return;
521 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000522 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000523 "Retype %.30s@%.128s's new password: ",
524 authctxt->server_user, authctxt->host);
525 retype = read_passphrase(prompt, 0);
526 if (strcmp(password, retype) != 0) {
527 memset(password, 0, strlen(password));
528 xfree(password);
Damien Miller996acd22003-04-09 20:59:48 +1000529 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000530 password = NULL;
531 }
532 memset(retype, 0, strlen(retype));
533 xfree(retype);
534 }
535 packet_put_cstring(password);
536 memset(password, 0, strlen(password));
537 xfree(password);
538 packet_add_padding(64);
539 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000540
541 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000542 &input_userauth_passwd_changereq);
543}
Damien Millereba71ba2000-04-29 23:57:08 +1000544
Ben Lindstrom79073822001-07-04 03:42:30 +0000545static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000546clear_auth_state(Authctxt *authctxt)
547{
548 /* XXX clear authentication state */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000549 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000550}
551
Ben Lindstrombba81212001-06-25 05:01:22 +0000552static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000553identity_sign(Identity *id, u_char **sigp, u_int *lenp,
554 u_char *data, u_int datalen)
555{
556 Key *prv;
557 int ret;
558
559 /* the agent supports this key */
560 if (id->ac)
561 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
562 data, datalen));
563 /*
564 * we have already loaded the private key or
565 * the private key is stored in external hardware
566 */
567 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
568 return (key_sign(id->key, sigp, lenp, data, datalen));
569 /* load the private key from the file */
570 if ((prv = load_identity_file(id->filename)) == NULL)
571 return (-1);
572 ret = key_sign(prv, sigp, lenp, data, datalen);
573 key_free(prv);
574 return (ret);
575}
576
577static int
578sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +1000579{
580 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000581 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000582 u_int bloblen, slen;
Damien Miller6536c7d2000-06-22 21:32:31 +1000583 int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000584 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100585 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000586
Ben Lindstromd121f612000-12-03 17:00:47 +0000587 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000588
Damien Miller280ecfb2003-05-14 13:46:00 +1000589 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100590 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000591 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100592 return 0;
593 }
Damien Millereba71ba2000-04-29 23:57:08 +1000594 /* data to be signed */
595 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100596 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000597 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000598 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100599 } else {
600 buffer_put_string(&b, session_id2, session_id2_len);
601 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000602 }
Damien Millereba71ba2000-04-29 23:57:08 +1000603 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100604 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000605 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000606 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000607 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100608 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000609 if (datafellows & SSH_BUG_PKAUTH) {
610 buffer_put_char(&b, have_sig);
611 } else {
612 buffer_put_cstring(&b, authctxt->method->name);
613 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +1000614 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +0000615 }
Damien Millereba71ba2000-04-29 23:57:08 +1000616 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000617
618 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +1000619 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000620 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000621 if (ret == -1) {
622 xfree(blob);
623 buffer_free(&b);
624 return 0;
625 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100626#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000627 buffer_dump(&b);
628#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000629 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000630 buffer_clear(&b);
631 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000632 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000633 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100634 buffer_put_cstring(&b, authctxt->server_user);
635 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100636 buffer_put_cstring(&b, authctxt->method->name);
637 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000638 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +1000639 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +1000640 buffer_put_string(&b, blob, bloblen);
641 }
642 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000643
Damien Millereba71ba2000-04-29 23:57:08 +1000644 /* append signature */
645 buffer_put_string(&b, signature, slen);
646 xfree(signature);
647
648 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000649 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100650 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000651 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000652
653 /* put remaining data from buffer into packet */
654 packet_start(SSH2_MSG_USERAUTH_REQUEST);
655 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
656 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +1000657 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000658
659 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000660}
661
Ben Lindstrombba81212001-06-25 05:01:22 +0000662static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000663send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +1000664{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000665 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000666 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000667
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000668 debug3("send_pubkey_test");
669
Damien Miller280ecfb2003-05-14 13:46:00 +1000670 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000671 /* we cannot handle this key */
672 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +1000673 return 0;
674 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000675 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000676 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +1000677
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000678 packet_start(SSH2_MSG_USERAUTH_REQUEST);
679 packet_put_cstring(authctxt->server_user);
680 packet_put_cstring(authctxt->service);
681 packet_put_cstring(authctxt->method->name);
682 packet_put_char(have_sig);
683 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +1000684 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000685 packet_put_string(blob, bloblen);
686 xfree(blob);
687 packet_send();
688 return 1;
689}
690
Ben Lindstrombba81212001-06-25 05:01:22 +0000691static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000692load_identity_file(char *filename)
693{
694 Key *private;
695 char prompt[300], *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000696 int quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000697 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000698
Ben Lindstrom329782e2001-03-10 17:08:59 +0000699 if (stat(filename, &st) < 0) {
700 debug3("no such identity: %s", filename);
701 return NULL;
702 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000703 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
704 if (private == NULL) {
705 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000706 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000707 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100708 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100709 for (i = 0; i < options.number_of_password_prompts; i++) {
710 passphrase = read_passphrase(prompt, 0);
711 if (strcmp(passphrase, "") != 0) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000712 private = key_load_private_type(KEY_UNSPEC, filename,
713 passphrase, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000714 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100715 } else {
716 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000717 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +1100718 }
719 memset(passphrase, 0, strlen(passphrase));
720 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000721 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +1100722 break;
723 debug2("bad passphrase given, try again...");
724 }
Damien Miller994cf142000-07-21 10:19:44 +1000725 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000726 return private;
727}
728
Damien Miller280ecfb2003-05-14 13:46:00 +1000729/*
730 * try keys in the following order:
731 * 1. agent keys that are found in the config file
732 * 2. other agent keys
733 * 3. keys that are only listed in the config file
734 */
735static void
736pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000737{
Damien Miller280ecfb2003-05-14 13:46:00 +1000738 Identity *id;
739 Idlist agent, files, *preferred;
740 Key *key;
741 AuthenticationConnection *ac;
Damien Millerad833b32000-08-23 10:46:23 +1000742 char *comment;
Damien Miller280ecfb2003-05-14 13:46:00 +1000743 int i, found;
Damien Millerad833b32000-08-23 10:46:23 +1000744
Damien Miller280ecfb2003-05-14 13:46:00 +1000745 TAILQ_INIT(&agent); /* keys from the agent */
746 TAILQ_INIT(&files); /* keys from the config file */
747 preferred = &authctxt->keys;
748 TAILQ_INIT(preferred); /* preferred order of keys */
749
750 /* list of keys stored in the filesystem */
751 for (i = 0; i < options.num_identity_files; i++) {
752 key = options.identity_keys[i];
753 if (key && key->type == KEY_RSA1)
754 continue;
755 options.identity_keys[i] = NULL;
756 id = xmalloc(sizeof(*id));
757 memset(id, 0, sizeof(*id));
758 id->key = key;
759 id->filename = xstrdup(options.identity_files[i]);
760 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +1000761 }
Damien Miller280ecfb2003-05-14 13:46:00 +1000762 /* list of keys supported by the agent */
763 if ((ac = ssh_get_authentication_connection())) {
764 for (key = ssh_get_first_identity(ac, &comment, 2);
765 key != NULL;
766 key = ssh_get_next_identity(ac, &comment, 2)) {
767 found = 0;
768 TAILQ_FOREACH(id, &files, next) {
769 /* agent keys from the config file are preferred */
770 if (key_equal(key, id->key)) {
771 key_free(key);
772 xfree(comment);
773 TAILQ_REMOVE(&files, id, next);
774 TAILQ_INSERT_TAIL(preferred, id, next);
775 id->ac = ac;
776 found = 1;
777 break;
778 }
779 }
780 if (!found) {
781 id = xmalloc(sizeof(*id));
782 memset(id, 0, sizeof(*id));
783 id->key = key;
784 id->filename = comment;
785 id->ac = ac;
786 TAILQ_INSERT_TAIL(&agent, id, next);
787 }
788 }
789 /* append remaining agent keys */
790 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
791 TAILQ_REMOVE(&agent, id, next);
792 TAILQ_INSERT_TAIL(preferred, id, next);
793 }
794 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +1100795 }
Damien Miller280ecfb2003-05-14 13:46:00 +1000796 /* append remaining keys from the config file */
797 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
798 TAILQ_REMOVE(&files, id, next);
799 TAILQ_INSERT_TAIL(preferred, id, next);
800 }
801 TAILQ_FOREACH(id, preferred, next) {
802 debug2("key: %s (%p)", id->filename, id->key);
803 }
804}
805
806static void
807pubkey_cleanup(Authctxt *authctxt)
808{
809 Identity *id;
810
811 if (authctxt->agent != NULL)
812 ssh_close_authentication_connection(authctxt->agent);
813 for (id = TAILQ_FIRST(&authctxt->keys); id;
814 id = TAILQ_FIRST(&authctxt->keys)) {
815 TAILQ_REMOVE(&authctxt->keys, id, next);
816 if (id->key)
817 key_free(id->key);
818 if (id->filename)
819 xfree(id->filename);
820 xfree(id);
821 }
Damien Millereba71ba2000-04-29 23:57:08 +1000822}
823
Damien Miller62cee002000-09-23 17:15:56 +1100824int
825userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000826{
Damien Miller280ecfb2003-05-14 13:46:00 +1000827 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +1100828 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000829
Damien Miller280ecfb2003-05-14 13:46:00 +1000830 while ((id = TAILQ_FIRST(&authctxt->keys))) {
831 if (id->tried++)
832 return (0);
833 TAILQ_REMOVE(&authctxt->keys, id, next);
834 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
835 /*
836 * send a test message if we have the public key. for
837 * encrypted keys we cannot do this and have to load the
838 * private key instead
839 */
840 if (id->key && id->key->type != KEY_RSA1) {
841 debug("Offering public key: %s", id->filename);
842 sent = send_pubkey_test(authctxt, id);
843 } else if (id->key == NULL) {
844 debug("Trying private key: %s", id->filename);
845 id->key = load_identity_file(id->filename);
846 if (id->key != NULL) {
847 id->isprivate = 1;
848 sent = sign_and_send_pubkey(authctxt, id);
849 key_free(id->key);
850 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000851 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000852 }
Damien Miller280ecfb2003-05-14 13:46:00 +1000853 if (sent)
854 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100855 }
Damien Miller280ecfb2003-05-14 13:46:00 +1000856 return (0);
Damien Miller62cee002000-09-23 17:15:56 +1100857}
Damien Millereba71ba2000-04-29 23:57:08 +1000858
Damien Miller874d77b2000-10-14 16:23:11 +1100859/*
860 * Send userauth request message specifying keyboard-interactive method.
861 */
862int
863userauth_kbdint(Authctxt *authctxt)
864{
865 static int attempt = 0;
866
867 if (attempt++ >= options.number_of_password_prompts)
868 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000869 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
870 if (attempt > 1 && !authctxt->info_req_seen) {
871 debug3("userauth_kbdint: disable: no info_req_seen");
872 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
873 return 0;
874 }
Damien Miller874d77b2000-10-14 16:23:11 +1100875
876 debug2("userauth_kbdint");
877 packet_start(SSH2_MSG_USERAUTH_REQUEST);
878 packet_put_cstring(authctxt->server_user);
879 packet_put_cstring(authctxt->service);
880 packet_put_cstring(authctxt->method->name);
881 packet_put_cstring(""); /* lang */
882 packet_put_cstring(options.kbd_interactive_devices ?
883 options.kbd_interactive_devices : "");
884 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100885
886 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
887 return 1;
888}
889
890/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000891 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +1100892 */
893void
Damien Miller630d6f42002-01-22 23:17:30 +1100894input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +1100895{
896 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000897 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000898 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +1100899 int echo = 0;
900
901 debug2("input_userauth_info_req");
902
903 if (authctxt == NULL)
904 fatal("input_userauth_info_req: no authentication context");
905
Ben Lindstrom7d199962001-09-12 18:29:00 +0000906 authctxt->info_req_seen = 1;
907
Damien Miller874d77b2000-10-14 16:23:11 +1100908 name = packet_get_string(NULL);
909 inst = packet_get_string(NULL);
910 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +1100911 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000912 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +1100913 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000914 logit("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000915 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +1100916 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000917 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100918
919 num_prompts = packet_get_int();
920 /*
921 * Begin to build info response packet based on prompts requested.
922 * We commit to providing the correct number of responses, so if
923 * further on we run into a problem that prevents this, we have to
924 * be sure and clean this up and send a correct error response.
925 */
926 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
927 packet_put_int(num_prompts);
928
Ben Lindstrom551ea372001-06-05 18:56:16 +0000929 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +1100930 for (i = 0; i < num_prompts; i++) {
931 prompt = packet_get_string(NULL);
932 echo = packet_get_char();
933
Ben Lindstrom949974b2001-06-25 05:20:31 +0000934 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +1100935
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000936 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +1100937 memset(response, 0, strlen(response));
938 xfree(response);
939 xfree(prompt);
940 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100941 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +1100942
Damien Miller9f643902001-11-12 11:02:52 +1100943 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +1100944 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100945}
Damien Miller62cee002000-09-23 17:15:56 +1100946
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000947static int
Ben Lindstrom5c385522002-06-23 21:23:20 +0000948ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000949 u_char *data, u_int datalen)
950{
951 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +0000952 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000953 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +0000954 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000955
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000956 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000957
Ben Lindstrom5206b952002-06-06 19:59:29 +0000958 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
959 error("ssh_keysign: no installed: %s", strerror(errno));
960 return -1;
961 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000962 if (fflush(stdout) != 0)
963 error("ssh_keysign: fflush: %s", strerror(errno));
964 if (pipe(to) < 0) {
965 error("ssh_keysign: pipe: %s", strerror(errno));
966 return -1;
967 }
968 if (pipe(from) < 0) {
969 error("ssh_keysign: pipe: %s", strerror(errno));
970 return -1;
971 }
972 if ((pid = fork()) < 0) {
973 error("ssh_keysign: fork: %s", strerror(errno));
974 return -1;
975 }
976 if (pid == 0) {
977 seteuid(getuid());
978 setuid(getuid());
979 close(from[0]);
980 if (dup2(from[1], STDOUT_FILENO) < 0)
981 fatal("ssh_keysign: dup2: %s", strerror(errno));
982 close(to[1]);
983 if (dup2(to[0], STDIN_FILENO) < 0)
984 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +0000985 close(from[1]);
986 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +0000987 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000988 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
989 strerror(errno));
990 }
991 close(from[1]);
992 close(to[0]);
993
994 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +0000995 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000996 buffer_put_string(&b, data, datalen);
Damien Miller901119b2002-10-04 11:10:04 +1000997 ssh_msg_send(to[1], version, &b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000998
Damien Miller901119b2002-10-04 11:10:04 +1000999 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001000 error("ssh_keysign: no reply");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001001 buffer_clear(&b);
1002 return -1;
1003 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001004 close(from[0]);
1005 close(to[1]);
1006
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001007 while (waitpid(pid, &status, 0) < 0)
1008 if (errno != EINTR)
1009 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001010
Ben Lindstrom5206b952002-06-06 19:59:29 +00001011 if (buffer_get_char(&b) != version) {
1012 error("ssh_keysign: bad version");
1013 buffer_clear(&b);
1014 return -1;
1015 }
1016 *sigp = buffer_get_string(&b, lenp);
1017 buffer_clear(&b);
1018
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001019 return 0;
1020}
1021
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001022int
1023userauth_hostbased(Authctxt *authctxt)
1024{
1025 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001026 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001027 Buffer b;
1028 u_char *signature, *blob;
1029 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001030 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001031 u_int blen, slen;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +00001032 int ok, i, len, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001033
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001034 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001035 for (i = 0; i < sensitive->nkeys; i++) {
1036 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001037 if (private && private->type != KEY_RSA1) {
1038 found = 1;
1039 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001040 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001041 break;
1042 }
1043 }
1044 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001045 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001046 return 0;
1047 }
1048 if (key_to_blob(private, &blob, &blen) == 0) {
1049 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001050 return 0;
1051 }
Damien Miller91c18472001-11-12 11:02:03 +11001052 /* figure out a name for the client host */
1053 p = get_local_name(packet_get_connection_in());
1054 if (p == NULL) {
1055 error("userauth_hostbased: cannot get local ipaddr/name");
1056 key_free(private);
1057 return 0;
1058 }
1059 len = strlen(p) + 2;
1060 chost = xmalloc(len);
1061 strlcpy(chost, p, len);
1062 strlcat(chost, ".", len);
1063 debug2("userauth_hostbased: chost %s", chost);
Damien Miller00111382003-03-10 11:21:17 +11001064 xfree(p);
Damien Miller91c18472001-11-12 11:02:03 +11001065
Ben Lindstrom671388f2001-04-19 20:40:45 +00001066 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1067 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001068 pkalg = xstrdup(key_ssh_name(private));
1069 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001070 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001071 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001072 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1073 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001074 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001075 buffer_put_cstring(&b, authctxt->method->name);
1076 buffer_put_cstring(&b, pkalg);
1077 buffer_put_string(&b, blob, blen);
1078 buffer_put_cstring(&b, chost);
1079 buffer_put_cstring(&b, authctxt->local_user);
1080#ifdef DEBUG_PK
1081 buffer_dump(&b);
1082#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001083 if (sensitive->external_keysign)
1084 ok = ssh_keysign(private, &signature, &slen,
1085 buffer_ptr(&b), buffer_len(&b));
1086 else
1087 ok = key_sign(private, &signature, &slen,
1088 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001089 key_free(private);
1090 buffer_free(&b);
1091 if (ok != 0) {
1092 error("key_sign failed");
1093 xfree(chost);
1094 xfree(pkalg);
1095 return 0;
1096 }
1097 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1098 packet_put_cstring(authctxt->server_user);
1099 packet_put_cstring(authctxt->service);
1100 packet_put_cstring(authctxt->method->name);
1101 packet_put_cstring(pkalg);
1102 packet_put_string(blob, blen);
1103 packet_put_cstring(chost);
1104 packet_put_cstring(authctxt->local_user);
1105 packet_put_string(signature, slen);
1106 memset(signature, 's', slen);
1107 xfree(signature);
1108 xfree(chost);
1109 xfree(pkalg);
1110
1111 packet_send();
1112 return 1;
1113}
1114
Damien Miller62cee002000-09-23 17:15:56 +11001115/* find auth method */
1116
Damien Miller62cee002000-09-23 17:15:56 +11001117/*
1118 * given auth method name, if configurable options permit this method fill
1119 * in auth_ident field and return true, otherwise return false.
1120 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001121static int
Damien Miller62cee002000-09-23 17:15:56 +11001122authmethod_is_enabled(Authmethod *method)
1123{
1124 if (method == NULL)
1125 return 0;
1126 /* return false if options indicate this method is disabled */
1127 if (method->enabled == NULL || *method->enabled == 0)
1128 return 0;
1129 /* return false if batch mode is enabled but method needs interactive mode */
1130 if (method->batch_flag != NULL && *method->batch_flag != 0)
1131 return 0;
1132 return 1;
1133}
1134
Ben Lindstrombba81212001-06-25 05:01:22 +00001135static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001136authmethod_lookup(const char *name)
1137{
1138 Authmethod *method = NULL;
1139 if (name != NULL)
1140 for (method = authmethods; method->name != NULL; method++)
1141 if (strcmp(name, method->name) == 0)
1142 return method;
1143 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1144 return NULL;
1145}
1146
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001147/* XXX internal state */
1148static Authmethod *current = NULL;
1149static char *supported = NULL;
1150static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001151
Damien Miller62cee002000-09-23 17:15:56 +11001152/*
1153 * Given the authentication method list sent by the server, return the
1154 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001155 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001156 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001157static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001158authmethod_get(char *authlist)
1159{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001160 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001161 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001162
Damien Miller62cee002000-09-23 17:15:56 +11001163 /* Use a suitable default if we're passed a nil list. */
1164 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001165 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001166
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001167 if (supported == NULL || strcmp(authlist, supported) != 0) {
1168 debug3("start over, passed a different list %s", authlist);
1169 if (supported != NULL)
1170 xfree(supported);
1171 supported = xstrdup(authlist);
1172 preferred = options.preferred_authentications;
1173 debug3("preferred %s", preferred);
1174 current = NULL;
1175 } else if (current != NULL && authmethod_is_enabled(current))
1176 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001177
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001178 for (;;) {
1179 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001180 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001181 current = NULL;
1182 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001183 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001184 preferred += next;
1185 debug3("authmethod_lookup %s", name);
1186 debug3("remaining preferred: %s", preferred);
1187 if ((current = authmethod_lookup(name)) != NULL &&
1188 authmethod_is_enabled(current)) {
1189 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001190 debug("Next authentication method: %s", name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001191 return current;
1192 }
Damien Millereba71ba2000-04-29 23:57:08 +10001193 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001194}
Damien Miller62cee002000-09-23 17:15:56 +11001195
Ben Lindstrom79073822001-07-04 03:42:30 +00001196static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001197authmethods_get(void)
1198{
1199 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001200 Buffer b;
1201 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001202
Damien Miller0e3b8722002-01-22 23:26:38 +11001203 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001204 for (method = authmethods; method->name != NULL; method++) {
1205 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001206 if (buffer_len(&b) > 0)
1207 buffer_append(&b, ",", 1);
1208 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001209 }
Damien Miller62cee002000-09-23 17:15:56 +11001210 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001211 buffer_append(&b, "\0", 1);
1212 list = xstrdup(buffer_ptr(&b));
1213 buffer_free(&b);
1214 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001215}