blob: 215f76ca2e3673f7258d7b36d0499d4194f370b1 [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"
Ben Lindstrom5c385522002-06-23 21:23:20 +000026RCSID("$OpenBSD: sshconnect2.c,v 1.105 2002/06/23 03:30:17 deraadt 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) {
85 log("No valid ciphers for protocol version 2 given, using defaults.");
86 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] =
Damien Miller874d77b2000-10-14 16:23:11 +110098 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib";
99 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000100 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller874d77b2000-10-14 16:23:11 +1100101 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
102 }
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
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000111 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000112 kex = kex_setup(myproposal);
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000113 kex->client_version_string=client_version_string;
114 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000115 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100116
Ben Lindstromf28f6342001-04-04 02:03:04 +0000117 xxx_kex = kex;
118
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000119 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100120
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000121 session_id2 = kex->session_id;
122 session_id2_len = kex->session_id_len;
123
Damien Miller874d77b2000-10-14 16:23:11 +1100124#ifdef DEBUG_KEXDH
125 /* send 1st encrypted/maced/compressed message */
126 packet_start(SSH2_MSG_IGNORE);
127 packet_put_cstring("markus");
128 packet_send();
129 packet_write_wait();
130#endif
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000131 debug("done: ssh_kex2.");
Damien Millereba71ba2000-04-29 23:57:08 +1000132}
Damien Millerb1715dc2000-05-30 13:44:51 +1000133
Damien Millereba71ba2000-04-29 23:57:08 +1000134/*
135 * Authenticate user
136 */
Damien Miller62cee002000-09-23 17:15:56 +1100137
138typedef struct Authctxt Authctxt;
139typedef struct Authmethod Authmethod;
140
141typedef int sign_cb_fn(
142 Authctxt *authctxt, Key *key,
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000143 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
Damien Miller62cee002000-09-23 17:15:56 +1100144
145struct Authctxt {
146 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000147 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100148 const char *host;
149 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100150 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100151 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000152 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000153 /* pubkey */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000154 Key *last_key;
155 sign_cb_fn *last_key_sign;
156 int last_key_hint;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000157 AuthenticationConnection *agent;
158 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000159 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000160 /* kbd-interactive */
161 int info_req_seen;
Damien Miller62cee002000-09-23 17:15:56 +1100162};
163struct Authmethod {
164 char *name; /* string to compare against server's list */
165 int (*userauth)(Authctxt *authctxt);
166 int *enabled; /* flag in option struct that enables method */
167 int *batch_flag; /* flag in option struct that disables method */
168};
169
Damien Miller630d6f42002-01-22 23:17:30 +1100170void input_userauth_success(int, u_int32_t, void *);
171void input_userauth_failure(int, u_int32_t, void *);
172void input_userauth_banner(int, u_int32_t, void *);
173void input_userauth_error(int, u_int32_t, void *);
174void input_userauth_info_req(int, u_int32_t, void *);
175void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000176void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100177
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000178int userauth_none(Authctxt *);
179int userauth_pubkey(Authctxt *);
180int userauth_passwd(Authctxt *);
181int userauth_kbdint(Authctxt *);
182int userauth_hostbased(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100183
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000184void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000185
Ben Lindstrombba81212001-06-25 05:01:22 +0000186static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
187static void clear_auth_state(Authctxt *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000188
Ben Lindstrombba81212001-06-25 05:01:22 +0000189static Authmethod *authmethod_get(char *authlist);
190static Authmethod *authmethod_lookup(const char *name);
191static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100192
193Authmethod authmethods[] = {
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000194 {"hostbased",
195 userauth_hostbased,
196 &options.hostbased_authentication,
197 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000198 {"publickey",
199 userauth_pubkey,
200 &options.pubkey_authentication,
201 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100202 {"keyboard-interactive",
203 userauth_kbdint,
204 &options.kbd_interactive_authentication,
205 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000206 {"password",
207 userauth_passwd,
208 &options.password_authentication,
209 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100210 {"none",
211 userauth_none,
212 NULL,
213 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100214 {NULL, NULL, NULL, NULL}
215};
216
217void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000218ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000219 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100220{
221 Authctxt authctxt;
222 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100223
Ben Lindstrom551ea372001-06-05 18:56:16 +0000224 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000225 options.kbd_interactive_authentication = 1;
226
Damien Miller62cee002000-09-23 17:15:56 +1100227 debug("send SSH2_MSG_SERVICE_REQUEST");
228 packet_start(SSH2_MSG_SERVICE_REQUEST);
229 packet_put_cstring("ssh-userauth");
230 packet_send();
231 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100232 type = packet_read();
Damien Miller62cee002000-09-23 17:15:56 +1100233 if (type != SSH2_MSG_SERVICE_ACCEPT) {
234 fatal("denied SSH2_MSG_SERVICE_ACCEPT: %d", type);
235 }
236 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100237 char *reply = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100238 debug("service_accept: %s", reply);
239 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100240 } else {
241 debug("buggy server: service_accept w/o service");
242 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100243 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100244 debug("got SSH2_MSG_SERVICE_ACCEPT");
245
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000246 if (options.preferred_authentications == NULL)
247 options.preferred_authentications = authmethods_get();
248
Damien Miller62cee002000-09-23 17:15:56 +1100249 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000250 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller62cee002000-09-23 17:15:56 +1100251 authctxt.agent = ssh_get_authentication_connection();
252 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000253 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100254 authctxt.host = host;
255 authctxt.service = "ssh-connection"; /* service name */
256 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100257 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000258 authctxt.authlist = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000259 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000260 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100261 if (authctxt.method == NULL)
262 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100263
264 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100265 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100266
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000267 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100268 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
269 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000270 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100271 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
272
273 if (authctxt.agent != NULL)
274 ssh_close_authentication_connection(authctxt.agent);
275
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000276 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100277}
278void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000279userauth(Authctxt *authctxt, char *authlist)
280{
281 if (authlist == NULL) {
282 authlist = authctxt->authlist;
283 } else {
284 if (authctxt->authlist)
285 xfree(authctxt->authlist);
286 authctxt->authlist = authlist;
287 }
288 for (;;) {
289 Authmethod *method = authmethod_get(authlist);
290 if (method == NULL)
291 fatal("Permission denied (%s).", authlist);
292 authctxt->method = method;
293 if (method->userauth(authctxt) != 0) {
294 debug2("we sent a %s packet, wait for reply", method->name);
295 break;
296 } else {
297 debug2("we did not send a packet, disable method");
298 method->enabled = NULL;
299 }
300 }
301}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000302
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000303void
Damien Miller630d6f42002-01-22 23:17:30 +1100304input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100305{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000306 fatal("input_userauth_error: bad message during authentication: "
307 "type %d", type);
308}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000309
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000310void
Damien Miller630d6f42002-01-22 23:17:30 +1100311input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000312{
313 char *msg, *lang;
314 debug3("input_userauth_banner");
315 msg = packet_get_string(NULL);
316 lang = packet_get_string(NULL);
317 fprintf(stderr, "%s", msg);
318 xfree(msg);
319 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100320}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000321
Damien Miller62cee002000-09-23 17:15:56 +1100322void
Damien Miller630d6f42002-01-22 23:17:30 +1100323input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100324{
325 Authctxt *authctxt = ctxt;
326 if (authctxt == NULL)
327 fatal("input_userauth_success: no authentication context");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000328 if (authctxt->authlist)
329 xfree(authctxt->authlist);
330 clear_auth_state(authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100331 authctxt->success = 1; /* break out */
332}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000333
Damien Miller62cee002000-09-23 17:15:56 +1100334void
Damien Miller630d6f42002-01-22 23:17:30 +1100335input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100336{
Damien Miller62cee002000-09-23 17:15:56 +1100337 Authctxt *authctxt = ctxt;
338 char *authlist = NULL;
339 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100340
341 if (authctxt == NULL)
342 fatal("input_userauth_failure: no authentication context");
343
Damien Miller874d77b2000-10-14 16:23:11 +1100344 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100345 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100346 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100347
348 if (partial != 0)
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000349 log("Authenticated with partial success.");
Damien Miller62cee002000-09-23 17:15:56 +1100350 debug("authentications that can continue: %s", authlist);
351
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000352 clear_auth_state(authctxt);
353 userauth(authctxt, authlist);
354}
355void
Damien Miller630d6f42002-01-22 23:17:30 +1100356input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000357{
358 Authctxt *authctxt = ctxt;
359 Key *key = NULL;
360 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000361 int pktype, sent = 0;
362 u_int alen, blen;
363 char *pkalg, *fp;
364 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000365
366 if (authctxt == NULL)
367 fatal("input_userauth_pk_ok: no authentication context");
368 if (datafellows & SSH_BUG_PKOK) {
369 /* this is similar to SSH_BUG_PKAUTH */
370 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
371 pkblob = packet_get_string(&blen);
372 buffer_init(&b);
373 buffer_append(&b, pkblob, blen);
374 pkalg = buffer_get_string(&b, &alen);
375 buffer_free(&b);
376 } else {
377 pkalg = packet_get_string(&alen);
378 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000379 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100380 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000381
Ben Lindstrom5c385522002-06-23 21:23:20 +0000382 debug("input_userauth_pk_ok: pkalg %s blen %u lastkey %p hint %d",
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000383 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
384
385 do {
386 if (authctxt->last_key == NULL ||
387 authctxt->last_key_sign == NULL) {
388 debug("no last key or no sign cb");
389 break;
390 }
Damien Miller68f45982002-02-05 12:23:32 +1100391 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000392 debug("unknown pkalg %s", pkalg);
393 break;
394 }
395 if ((key = key_from_blob(pkblob, blen)) == NULL) {
396 debug("no key from blob. pkalg %s", pkalg);
397 break;
398 }
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000399 if (key->type != pktype) {
Damien Miller68f45982002-02-05 12:23:32 +1100400 error("input_userauth_pk_ok: type mismatch "
401 "for decoded key (received %d, expected %d)",
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000402 key->type, pktype);
Damien Miller68f45982002-02-05 12:23:32 +1100403 break;
404 }
Ben Lindstromcfccef92001-03-13 04:57:58 +0000405 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
406 debug2("input_userauth_pk_ok: fp %s", fp);
407 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000408 if (!key_equal(key, authctxt->last_key)) {
409 debug("key != last_key");
410 break;
411 }
412 sent = sign_and_send_pubkey(authctxt, key,
413 authctxt->last_key_sign);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000414 } while (0);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000415
416 if (key != NULL)
417 key_free(key);
418 xfree(pkalg);
419 xfree(pkblob);
420
421 /* unregister */
422 clear_auth_state(authctxt);
423 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
424
425 /* try another method if we did not send a packet*/
426 if (sent == 0)
427 userauth(authctxt, NULL);
428
Damien Miller62cee002000-09-23 17:15:56 +1100429}
430
Damien Millereba71ba2000-04-29 23:57:08 +1000431int
Damien Miller874d77b2000-10-14 16:23:11 +1100432userauth_none(Authctxt *authctxt)
433{
434 /* initial userauth request */
435 packet_start(SSH2_MSG_USERAUTH_REQUEST);
436 packet_put_cstring(authctxt->server_user);
437 packet_put_cstring(authctxt->service);
438 packet_put_cstring(authctxt->method->name);
439 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100440 return 1;
441}
442
443int
Damien Miller62cee002000-09-23 17:15:56 +1100444userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000445{
Damien Millere247cc42000-05-07 12:03:14 +1000446 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000447 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000448 char *password;
449
Damien Millerd3a18572000-06-07 19:55:44 +1000450 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000451 return 0;
452
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000453 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000454 error("Permission denied, please try again.");
455
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000456 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100457 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000458 password = read_passphrase(prompt, 0);
459 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100460 packet_put_cstring(authctxt->server_user);
461 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100462 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000463 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000464 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000465 memset(password, 0, strlen(password));
466 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100467 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000468 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000469
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000470 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000471 &input_userauth_passwd_changereq);
472
Damien Millereba71ba2000-04-29 23:57:08 +1000473 return 1;
474}
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000475/*
476 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
477 */
478void
Tim Ricec8549622002-03-31 12:49:38 -0800479input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000480{
481 Authctxt *authctxt = ctxt;
482 char *info, *lang, *password = NULL, *retype = NULL;
483 char prompt[150];
484
485 debug2("input_userauth_passwd_changereq");
486
487 if (authctxt == NULL)
488 fatal("input_userauth_passwd_changereq: "
489 "no authentication context");
490
491 info = packet_get_string(NULL);
492 lang = packet_get_string(NULL);
493 if (strlen(info) > 0)
494 log("%s", info);
495 xfree(info);
496 xfree(lang);
497 packet_start(SSH2_MSG_USERAUTH_REQUEST);
498 packet_put_cstring(authctxt->server_user);
499 packet_put_cstring(authctxt->service);
500 packet_put_cstring(authctxt->method->name);
501 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000502 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000503 "Enter %.30s@%.128s's old password: ",
504 authctxt->server_user, authctxt->host);
505 password = read_passphrase(prompt, 0);
506 packet_put_cstring(password);
507 memset(password, 0, strlen(password));
508 xfree(password);
509 password = NULL;
510 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000511 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000512 "Enter %.30s@%.128s's new password: ",
513 authctxt->server_user, authctxt->host);
514 password = read_passphrase(prompt, RP_ALLOW_EOF);
515 if (password == NULL) {
516 /* bail out */
517 return;
518 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000519 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000520 "Retype %.30s@%.128s's new password: ",
521 authctxt->server_user, authctxt->host);
522 retype = read_passphrase(prompt, 0);
523 if (strcmp(password, retype) != 0) {
524 memset(password, 0, strlen(password));
525 xfree(password);
526 log("Mismatch; try again, EOF to quit.");
527 password = NULL;
528 }
529 memset(retype, 0, strlen(retype));
530 xfree(retype);
531 }
532 packet_put_cstring(password);
533 memset(password, 0, strlen(password));
534 xfree(password);
535 packet_add_padding(64);
536 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000537
538 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000539 &input_userauth_passwd_changereq);
540}
Damien Millereba71ba2000-04-29 23:57:08 +1000541
Ben Lindstrom79073822001-07-04 03:42:30 +0000542static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000543clear_auth_state(Authctxt *authctxt)
544{
545 /* XXX clear authentication state */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000546 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
547
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000548 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
549 debug3("clear_auth_state: key_free %p", authctxt->last_key);
550 key_free(authctxt->last_key);
551 }
552 authctxt->last_key = NULL;
553 authctxt->last_key_hint = -2;
554 authctxt->last_key_sign = NULL;
555}
556
Ben Lindstrombba81212001-06-25 05:01:22 +0000557static int
Damien Miller62cee002000-09-23 17:15:56 +1100558sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
Damien Millereba71ba2000-04-29 23:57:08 +1000559{
560 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000561 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000562 u_int bloblen, slen;
Damien Miller6536c7d2000-06-22 21:32:31 +1000563 int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000564 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100565 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000566
Ben Lindstromd121f612000-12-03 17:00:47 +0000567 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000568
Damien Miller0bc1bd82000-11-13 22:57:25 +1100569 if (key_to_blob(k, &blob, &bloblen) == 0) {
570 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000571 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100572 return 0;
573 }
Damien Millereba71ba2000-04-29 23:57:08 +1000574 /* data to be signed */
575 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100576 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000577 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000578 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100579 } else {
580 buffer_put_string(&b, session_id2, session_id2_len);
581 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000582 }
Damien Millereba71ba2000-04-29 23:57:08 +1000583 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100584 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000585 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000586 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000587 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100588 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000589 if (datafellows & SSH_BUG_PKAUTH) {
590 buffer_put_char(&b, have_sig);
591 } else {
592 buffer_put_cstring(&b, authctxt->method->name);
593 buffer_put_char(&b, have_sig);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000594 buffer_put_cstring(&b, key_ssh_name(k));
Ben Lindstromd121f612000-12-03 17:00:47 +0000595 }
Damien Millereba71ba2000-04-29 23:57:08 +1000596 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000597
598 /* generate signature */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000599 ret = (*sign_callback)(authctxt, k, &signature, &slen,
600 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000601 if (ret == -1) {
602 xfree(blob);
603 buffer_free(&b);
604 return 0;
605 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100606#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000607 buffer_dump(&b);
608#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000609 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000610 buffer_clear(&b);
611 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000612 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000613 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100614 buffer_put_cstring(&b, authctxt->server_user);
615 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100616 buffer_put_cstring(&b, authctxt->method->name);
617 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000618 if (!(datafellows & SSH_BUG_PKAUTH))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000619 buffer_put_cstring(&b, key_ssh_name(k));
Damien Miller30c3d422000-05-09 11:02:59 +1000620 buffer_put_string(&b, blob, bloblen);
621 }
622 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000623
Damien Millereba71ba2000-04-29 23:57:08 +1000624 /* append signature */
625 buffer_put_string(&b, signature, slen);
626 xfree(signature);
627
628 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000629 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100630 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000631 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000632
633 /* put remaining data from buffer into packet */
634 packet_start(SSH2_MSG_USERAUTH_REQUEST);
635 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
636 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +1000637 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000638
639 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000640}
641
Ben Lindstrombba81212001-06-25 05:01:22 +0000642static int
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000643send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
644 int hint)
Damien Miller994cf142000-07-21 10:19:44 +1000645{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000646 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000647 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000648
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000649 debug3("send_pubkey_test");
650
651 if (key_to_blob(k, &blob, &bloblen) == 0) {
652 /* we cannot handle this key */
653 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +1000654 return 0;
655 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000656 /* register callback for USERAUTH_PK_OK message */
657 authctxt->last_key_sign = sign_callback;
658 authctxt->last_key_hint = hint;
659 authctxt->last_key = k;
660 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +1000661
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000662 packet_start(SSH2_MSG_USERAUTH_REQUEST);
663 packet_put_cstring(authctxt->server_user);
664 packet_put_cstring(authctxt->service);
665 packet_put_cstring(authctxt->method->name);
666 packet_put_char(have_sig);
667 if (!(datafellows & SSH_BUG_PKAUTH))
668 packet_put_cstring(key_ssh_name(k));
669 packet_put_string(blob, bloblen);
670 xfree(blob);
671 packet_send();
672 return 1;
673}
674
Ben Lindstrombba81212001-06-25 05:01:22 +0000675static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000676load_identity_file(char *filename)
677{
678 Key *private;
679 char prompt[300], *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000680 int quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000681 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000682
Ben Lindstrom329782e2001-03-10 17:08:59 +0000683 if (stat(filename, &st) < 0) {
684 debug3("no such identity: %s", filename);
685 return NULL;
686 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000687 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
688 if (private == NULL) {
689 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000690 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000691 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100692 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100693 for (i = 0; i < options.number_of_password_prompts; i++) {
694 passphrase = read_passphrase(prompt, 0);
695 if (strcmp(passphrase, "") != 0) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000696 private = key_load_private_type(KEY_UNSPEC, filename,
697 passphrase, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000698 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100699 } else {
700 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000701 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +1100702 }
703 memset(passphrase, 0, strlen(passphrase));
704 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000705 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +1100706 break;
707 debug2("bad passphrase given, try again...");
708 }
Damien Miller994cf142000-07-21 10:19:44 +1000709 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000710 return private;
711}
712
Ben Lindstrombba81212001-06-25 05:01:22 +0000713static int
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000714identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
715 u_char *data, u_int datalen)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000716{
717 Key *private;
718 int idx, ret;
719
720 idx = authctxt->last_key_hint;
721 if (idx < 0)
722 return -1;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000723
724 /* private key is stored in external hardware */
Damien Miller9f0f5c62001-12-21 14:45:46 +1100725 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
Ben Lindstromc5b68002001-07-04 04:52:03 +0000726 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
727
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000728 private = load_identity_file(options.identity_files[idx]);
729 if (private == NULL)
730 return -1;
731 ret = key_sign(private, sigp, lenp, data, datalen);
732 key_free(private);
Damien Millerad833b32000-08-23 10:46:23 +1000733 return ret;
734}
735
Ben Lindstrombba81212001-06-25 05:01:22 +0000736static int
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000737agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
738 u_char *data, u_int datalen)
Damien Millerad833b32000-08-23 10:46:23 +1000739{
Damien Miller62cee002000-09-23 17:15:56 +1100740 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
Damien Millerad833b32000-08-23 10:46:23 +1000741}
742
Ben Lindstrombba81212001-06-25 05:01:22 +0000743static int
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000744key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
745 u_char *data, u_int datalen)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000746{
Ben Lindstroma3700052001-04-05 23:26:32 +0000747 return key_sign(key, sigp, lenp, data, datalen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000748}
749
Ben Lindstrombba81212001-06-25 05:01:22 +0000750static int
Damien Miller62cee002000-09-23 17:15:56 +1100751userauth_pubkey_agent(Authctxt *authctxt)
Damien Millerad833b32000-08-23 10:46:23 +1000752{
753 static int called = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100754 int ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000755 char *comment;
756 Key *k;
Damien Millerad833b32000-08-23 10:46:23 +1000757
758 if (called == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100759 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
760 debug2("userauth_pubkey_agent: no keys at all");
Damien Miller62cee002000-09-23 17:15:56 +1100761 called = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000762 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100763 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
Damien Miller62cee002000-09-23 17:15:56 +1100764 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100765 debug2("userauth_pubkey_agent: no more keys");
766 } else {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000767 debug("userauth_pubkey_agent: testing agent key %s", comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100768 xfree(comment);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000769 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
770 if (ret == 0)
771 key_free(k);
Damien Miller62cee002000-09-23 17:15:56 +1100772 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100773 if (ret == 0)
774 debug2("userauth_pubkey_agent: no message sent");
Damien Millerad833b32000-08-23 10:46:23 +1000775 return ret;
Damien Millereba71ba2000-04-29 23:57:08 +1000776}
777
Damien Miller62cee002000-09-23 17:15:56 +1100778int
779userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000780{
Damien Miller62cee002000-09-23 17:15:56 +1100781 static int idx = 0;
782 int sent = 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000783 Key *key;
784 char *filename;
Damien Millereba71ba2000-04-29 23:57:08 +1000785
Damien Miller0bc1bd82000-11-13 22:57:25 +1100786 if (authctxt->agent != NULL) {
787 do {
788 sent = userauth_pubkey_agent(authctxt);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000789 } while (!sent && authctxt->agent->howmany > 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100790 }
791 while (!sent && idx < options.num_identity_files) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000792 key = options.identity_keys[idx];
793 filename = options.identity_files[idx];
794 if (key == NULL) {
795 debug("try privkey: %s", filename);
796 key = load_identity_file(filename);
797 if (key != NULL) {
798 sent = sign_and_send_pubkey(authctxt, key,
799 key_sign_cb);
800 key_free(key);
801 }
802 } else if (key->type != KEY_RSA1) {
803 debug("try pubkey: %s", filename);
804 sent = send_pubkey_test(authctxt, key,
805 identity_sign_cb, idx);
806 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100807 idx++;
808 }
Damien Miller62cee002000-09-23 17:15:56 +1100809 return sent;
810}
Damien Millereba71ba2000-04-29 23:57:08 +1000811
Damien Miller874d77b2000-10-14 16:23:11 +1100812/*
813 * Send userauth request message specifying keyboard-interactive method.
814 */
815int
816userauth_kbdint(Authctxt *authctxt)
817{
818 static int attempt = 0;
819
820 if (attempt++ >= options.number_of_password_prompts)
821 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000822 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
823 if (attempt > 1 && !authctxt->info_req_seen) {
824 debug3("userauth_kbdint: disable: no info_req_seen");
825 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
826 return 0;
827 }
Damien Miller874d77b2000-10-14 16:23:11 +1100828
829 debug2("userauth_kbdint");
830 packet_start(SSH2_MSG_USERAUTH_REQUEST);
831 packet_put_cstring(authctxt->server_user);
832 packet_put_cstring(authctxt->service);
833 packet_put_cstring(authctxt->method->name);
834 packet_put_cstring(""); /* lang */
835 packet_put_cstring(options.kbd_interactive_devices ?
836 options.kbd_interactive_devices : "");
837 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100838
839 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
840 return 1;
841}
842
843/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000844 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +1100845 */
846void
Damien Miller630d6f42002-01-22 23:17:30 +1100847input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +1100848{
849 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000850 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000851 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +1100852 int echo = 0;
853
854 debug2("input_userauth_info_req");
855
856 if (authctxt == NULL)
857 fatal("input_userauth_info_req: no authentication context");
858
Ben Lindstrom7d199962001-09-12 18:29:00 +0000859 authctxt->info_req_seen = 1;
860
Damien Miller874d77b2000-10-14 16:23:11 +1100861 name = packet_get_string(NULL);
862 inst = packet_get_string(NULL);
863 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +1100864 if (strlen(name) > 0)
Ben Lindstrom126c56a2001-06-25 05:22:53 +0000865 log("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +1100866 if (strlen(inst) > 0)
Ben Lindstrom126c56a2001-06-25 05:22:53 +0000867 log("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000868 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +1100869 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000870 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100871
872 num_prompts = packet_get_int();
873 /*
874 * Begin to build info response packet based on prompts requested.
875 * We commit to providing the correct number of responses, so if
876 * further on we run into a problem that prevents this, we have to
877 * be sure and clean this up and send a correct error response.
878 */
879 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
880 packet_put_int(num_prompts);
881
Ben Lindstrom551ea372001-06-05 18:56:16 +0000882 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +1100883 for (i = 0; i < num_prompts; i++) {
884 prompt = packet_get_string(NULL);
885 echo = packet_get_char();
886
Ben Lindstrom949974b2001-06-25 05:20:31 +0000887 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +1100888
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000889 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +1100890 memset(response, 0, strlen(response));
891 xfree(response);
892 xfree(prompt);
893 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100894 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +1100895
Damien Miller9f643902001-11-12 11:02:52 +1100896 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +1100897 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100898}
Damien Miller62cee002000-09-23 17:15:56 +1100899
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000900static int
Ben Lindstrom5c385522002-06-23 21:23:20 +0000901ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000902 u_char *data, u_int datalen)
903{
904 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +0000905 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000906 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +0000907 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000908
909 debug("ssh_keysign called");
910
Ben Lindstrom5206b952002-06-06 19:59:29 +0000911 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
912 error("ssh_keysign: no installed: %s", strerror(errno));
913 return -1;
914 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000915 if (fflush(stdout) != 0)
916 error("ssh_keysign: fflush: %s", strerror(errno));
917 if (pipe(to) < 0) {
918 error("ssh_keysign: pipe: %s", strerror(errno));
919 return -1;
920 }
921 if (pipe(from) < 0) {
922 error("ssh_keysign: pipe: %s", strerror(errno));
923 return -1;
924 }
925 if ((pid = fork()) < 0) {
926 error("ssh_keysign: fork: %s", strerror(errno));
927 return -1;
928 }
929 if (pid == 0) {
930 seteuid(getuid());
931 setuid(getuid());
932 close(from[0]);
933 if (dup2(from[1], STDOUT_FILENO) < 0)
934 fatal("ssh_keysign: dup2: %s", strerror(errno));
935 close(to[1]);
936 if (dup2(to[0], STDIN_FILENO) < 0)
937 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +0000938 close(from[1]);
939 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +0000940 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000941 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
942 strerror(errno));
943 }
944 close(from[1]);
945 close(to[0]);
946
947 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +0000948 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000949 buffer_put_string(&b, data, datalen);
950 msg_send(to[1], version, &b);
951
952 if (msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +0000953 error("ssh_keysign: no reply");
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000954 buffer_clear(&b);
955 return -1;
956 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000957 close(from[0]);
958 close(to[1]);
959
Ben Lindstromcec2ea82002-06-06 20:51:04 +0000960 while (waitpid(pid, &status, 0) < 0)
961 if (errno != EINTR)
962 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000963
Ben Lindstrom5206b952002-06-06 19:59:29 +0000964 if (buffer_get_char(&b) != version) {
965 error("ssh_keysign: bad version");
966 buffer_clear(&b);
967 return -1;
968 }
969 *sigp = buffer_get_string(&b, lenp);
970 buffer_clear(&b);
971
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000972 return 0;
973}
974
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000975int
976userauth_hostbased(Authctxt *authctxt)
977{
978 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000979 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000980 Buffer b;
981 u_char *signature, *blob;
982 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +0000983 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000984 u_int blen, slen;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000985 int ok, i, len, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000986
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000987 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000988 for (i = 0; i < sensitive->nkeys; i++) {
989 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000990 if (private && private->type != KEY_RSA1) {
991 found = 1;
992 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000993 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000994 break;
995 }
996 }
997 if (!found) {
Damien Miller91c18472001-11-12 11:02:03 +1100998 debug("userauth_hostbased: no more client hostkeys");
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000999 return 0;
1000 }
1001 if (key_to_blob(private, &blob, &blen) == 0) {
1002 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001003 return 0;
1004 }
Damien Miller91c18472001-11-12 11:02:03 +11001005 /* figure out a name for the client host */
1006 p = get_local_name(packet_get_connection_in());
1007 if (p == NULL) {
1008 error("userauth_hostbased: cannot get local ipaddr/name");
1009 key_free(private);
1010 return 0;
1011 }
1012 len = strlen(p) + 2;
1013 chost = xmalloc(len);
1014 strlcpy(chost, p, len);
1015 strlcat(chost, ".", len);
1016 debug2("userauth_hostbased: chost %s", chost);
1017
Ben Lindstrom671388f2001-04-19 20:40:45 +00001018 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1019 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001020 pkalg = xstrdup(key_ssh_name(private));
1021 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001022 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001023 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001024 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1025 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001026 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001027 buffer_put_cstring(&b, authctxt->method->name);
1028 buffer_put_cstring(&b, pkalg);
1029 buffer_put_string(&b, blob, blen);
1030 buffer_put_cstring(&b, chost);
1031 buffer_put_cstring(&b, authctxt->local_user);
1032#ifdef DEBUG_PK
1033 buffer_dump(&b);
1034#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001035 if (sensitive->external_keysign)
1036 ok = ssh_keysign(private, &signature, &slen,
1037 buffer_ptr(&b), buffer_len(&b));
1038 else
1039 ok = key_sign(private, &signature, &slen,
1040 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001041 key_free(private);
1042 buffer_free(&b);
1043 if (ok != 0) {
1044 error("key_sign failed");
1045 xfree(chost);
1046 xfree(pkalg);
1047 return 0;
1048 }
1049 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1050 packet_put_cstring(authctxt->server_user);
1051 packet_put_cstring(authctxt->service);
1052 packet_put_cstring(authctxt->method->name);
1053 packet_put_cstring(pkalg);
1054 packet_put_string(blob, blen);
1055 packet_put_cstring(chost);
1056 packet_put_cstring(authctxt->local_user);
1057 packet_put_string(signature, slen);
1058 memset(signature, 's', slen);
1059 xfree(signature);
1060 xfree(chost);
1061 xfree(pkalg);
1062
1063 packet_send();
1064 return 1;
1065}
1066
Damien Miller62cee002000-09-23 17:15:56 +11001067/* find auth method */
1068
Damien Miller62cee002000-09-23 17:15:56 +11001069/*
1070 * given auth method name, if configurable options permit this method fill
1071 * in auth_ident field and return true, otherwise return false.
1072 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001073static int
Damien Miller62cee002000-09-23 17:15:56 +11001074authmethod_is_enabled(Authmethod *method)
1075{
1076 if (method == NULL)
1077 return 0;
1078 /* return false if options indicate this method is disabled */
1079 if (method->enabled == NULL || *method->enabled == 0)
1080 return 0;
1081 /* return false if batch mode is enabled but method needs interactive mode */
1082 if (method->batch_flag != NULL && *method->batch_flag != 0)
1083 return 0;
1084 return 1;
1085}
1086
Ben Lindstrombba81212001-06-25 05:01:22 +00001087static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001088authmethod_lookup(const char *name)
1089{
1090 Authmethod *method = NULL;
1091 if (name != NULL)
1092 for (method = authmethods; method->name != NULL; method++)
1093 if (strcmp(name, method->name) == 0)
1094 return method;
1095 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1096 return NULL;
1097}
1098
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001099/* XXX internal state */
1100static Authmethod *current = NULL;
1101static char *supported = NULL;
1102static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001103
Damien Miller62cee002000-09-23 17:15:56 +11001104/*
1105 * Given the authentication method list sent by the server, return the
1106 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001107 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001108 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001109static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001110authmethod_get(char *authlist)
1111{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001112
1113 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001114 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001115
Damien Miller62cee002000-09-23 17:15:56 +11001116 /* Use a suitable default if we're passed a nil list. */
1117 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001118 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001119
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001120 if (supported == NULL || strcmp(authlist, supported) != 0) {
1121 debug3("start over, passed a different list %s", authlist);
1122 if (supported != NULL)
1123 xfree(supported);
1124 supported = xstrdup(authlist);
1125 preferred = options.preferred_authentications;
1126 debug3("preferred %s", preferred);
1127 current = NULL;
1128 } else if (current != NULL && authmethod_is_enabled(current))
1129 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001130
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001131 for (;;) {
1132 if ((name = match_list(preferred, supported, &next)) == NULL) {
1133 debug("no more auth methods to try");
1134 current = NULL;
1135 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001136 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001137 preferred += next;
1138 debug3("authmethod_lookup %s", name);
1139 debug3("remaining preferred: %s", preferred);
1140 if ((current = authmethod_lookup(name)) != NULL &&
1141 authmethod_is_enabled(current)) {
1142 debug3("authmethod_is_enabled %s", name);
1143 debug("next auth method to try is %s", name);
1144 return current;
1145 }
Damien Millereba71ba2000-04-29 23:57:08 +10001146 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001147}
Damien Miller62cee002000-09-23 17:15:56 +11001148
Ben Lindstrom79073822001-07-04 03:42:30 +00001149static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001150authmethods_get(void)
1151{
1152 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001153 Buffer b;
1154 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001155
Damien Miller0e3b8722002-01-22 23:26:38 +11001156 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001157 for (method = authmethods; method->name != NULL; method++) {
1158 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001159 if (buffer_len(&b) > 0)
1160 buffer_append(&b, ",", 1);
1161 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001162 }
Damien Miller62cee002000-09-23 17:15:56 +11001163 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001164 buffer_append(&b, "\0", 1);
1165 list = xstrdup(buffer_ptr(&b));
1166 buffer_free(&b);
1167 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001168}