blob: 755be5cca07ef3c741c23842fb11884b60a6e883 [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 Lindstrom064496f2002-12-23 02:04:22 +000026RCSID("$OpenBSD: sshconnect2.c,v 1.108 2002/11/21 22:45:31 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) {
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] =
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
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
Damien Millereba71ba2000-04-29 23:57:08 +1000131}
Damien Millerb1715dc2000-05-30 13:44:51 +1000132
Damien Millereba71ba2000-04-29 23:57:08 +1000133/*
134 * Authenticate user
135 */
Damien Miller62cee002000-09-23 17:15:56 +1100136
137typedef struct Authctxt Authctxt;
138typedef struct Authmethod Authmethod;
139
140typedef int sign_cb_fn(
141 Authctxt *authctxt, Key *key,
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000142 u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
Damien Miller62cee002000-09-23 17:15:56 +1100143
144struct Authctxt {
145 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000146 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100147 const char *host;
148 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100149 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100150 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000151 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000152 /* pubkey */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000153 Key *last_key;
154 sign_cb_fn *last_key_sign;
155 int last_key_hint;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000156 AuthenticationConnection *agent;
157 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000158 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000159 /* kbd-interactive */
160 int info_req_seen;
Damien Miller62cee002000-09-23 17:15:56 +1100161};
162struct Authmethod {
163 char *name; /* string to compare against server's list */
164 int (*userauth)(Authctxt *authctxt);
165 int *enabled; /* flag in option struct that enables method */
166 int *batch_flag; /* flag in option struct that disables method */
167};
168
Damien Miller630d6f42002-01-22 23:17:30 +1100169void input_userauth_success(int, u_int32_t, void *);
170void input_userauth_failure(int, u_int32_t, void *);
171void input_userauth_banner(int, u_int32_t, void *);
172void input_userauth_error(int, u_int32_t, void *);
173void input_userauth_info_req(int, u_int32_t, void *);
174void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000175void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100176
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000177int userauth_none(Authctxt *);
178int userauth_pubkey(Authctxt *);
179int userauth_passwd(Authctxt *);
180int userauth_kbdint(Authctxt *);
181int userauth_hostbased(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100182
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000183void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000184
Ben Lindstrombba81212001-06-25 05:01:22 +0000185static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
186static void clear_auth_state(Authctxt *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000187
Ben Lindstrombba81212001-06-25 05:01:22 +0000188static Authmethod *authmethod_get(char *authlist);
189static Authmethod *authmethod_lookup(const char *name);
190static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100191
192Authmethod authmethods[] = {
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000193 {"hostbased",
194 userauth_hostbased,
195 &options.hostbased_authentication,
196 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000197 {"publickey",
198 userauth_pubkey,
199 &options.pubkey_authentication,
200 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100201 {"keyboard-interactive",
202 userauth_kbdint,
203 &options.kbd_interactive_authentication,
204 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000205 {"password",
206 userauth_passwd,
207 &options.password_authentication,
208 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100209 {"none",
210 userauth_none,
211 NULL,
212 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100213 {NULL, NULL, NULL, NULL}
214};
215
216void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000217ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000218 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100219{
220 Authctxt authctxt;
221 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100222
Ben Lindstrom551ea372001-06-05 18:56:16 +0000223 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000224 options.kbd_interactive_authentication = 1;
225
Damien Miller62cee002000-09-23 17:15:56 +1100226 packet_start(SSH2_MSG_SERVICE_REQUEST);
227 packet_put_cstring("ssh-userauth");
228 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000229 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100230 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100231 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000232 if (type != SSH2_MSG_SERVICE_ACCEPT)
233 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100234 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100235 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000236 debug2("service_accept: %s", reply);
Damien Miller62cee002000-09-23 17:15:56 +1100237 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100238 } else {
239 debug("buggy server: service_accept w/o service");
240 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100241 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000242 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100243
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000244 if (options.preferred_authentications == NULL)
245 options.preferred_authentications = authmethods_get();
246
Damien Miller62cee002000-09-23 17:15:56 +1100247 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000248 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller62cee002000-09-23 17:15:56 +1100249 authctxt.agent = ssh_get_authentication_connection();
250 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000251 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100252 authctxt.host = host;
253 authctxt.service = "ssh-connection"; /* service name */
254 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100255 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000256 authctxt.authlist = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000257 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000258 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100259 if (authctxt.method == NULL)
260 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100261
262 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100263 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100264
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000265 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100266 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
267 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000268 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100269 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
270
271 if (authctxt.agent != NULL)
272 ssh_close_authentication_connection(authctxt.agent);
273
Ben Lindstrom4dccfa52000-12-28 16:40:05 +0000274 debug("ssh-userauth2 successful: method %s", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100275}
276void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000277userauth(Authctxt *authctxt, char *authlist)
278{
279 if (authlist == NULL) {
280 authlist = authctxt->authlist;
281 } else {
282 if (authctxt->authlist)
283 xfree(authctxt->authlist);
284 authctxt->authlist = authlist;
285 }
286 for (;;) {
287 Authmethod *method = authmethod_get(authlist);
288 if (method == NULL)
289 fatal("Permission denied (%s).", authlist);
290 authctxt->method = method;
291 if (method->userauth(authctxt) != 0) {
292 debug2("we sent a %s packet, wait for reply", method->name);
293 break;
294 } else {
295 debug2("we did not send a packet, disable method");
296 method->enabled = NULL;
297 }
298 }
299}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000300
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000301void
Damien Miller630d6f42002-01-22 23:17:30 +1100302input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100303{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000304 fatal("input_userauth_error: bad message during authentication: "
305 "type %d", type);
306}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000307
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000308void
Damien Miller630d6f42002-01-22 23:17:30 +1100309input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000310{
311 char *msg, *lang;
312 debug3("input_userauth_banner");
313 msg = packet_get_string(NULL);
314 lang = packet_get_string(NULL);
315 fprintf(stderr, "%s", msg);
316 xfree(msg);
317 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100318}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000319
Damien Miller62cee002000-09-23 17:15:56 +1100320void
Damien Miller630d6f42002-01-22 23:17:30 +1100321input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100322{
323 Authctxt *authctxt = ctxt;
324 if (authctxt == NULL)
325 fatal("input_userauth_success: no authentication context");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000326 if (authctxt->authlist)
327 xfree(authctxt->authlist);
328 clear_auth_state(authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100329 authctxt->success = 1; /* break out */
330}
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_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100334{
Damien Miller62cee002000-09-23 17:15:56 +1100335 Authctxt *authctxt = ctxt;
336 char *authlist = NULL;
337 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100338
339 if (authctxt == NULL)
340 fatal("input_userauth_failure: no authentication context");
341
Damien Miller874d77b2000-10-14 16:23:11 +1100342 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100343 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100344 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100345
346 if (partial != 0)
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000347 log("Authenticated with partial success.");
Damien Miller62cee002000-09-23 17:15:56 +1100348 debug("authentications that can continue: %s", authlist);
349
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000350 clear_auth_state(authctxt);
351 userauth(authctxt, authlist);
352}
353void
Damien Miller630d6f42002-01-22 23:17:30 +1100354input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000355{
356 Authctxt *authctxt = ctxt;
357 Key *key = NULL;
358 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000359 int pktype, sent = 0;
360 u_int alen, blen;
361 char *pkalg, *fp;
362 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000363
364 if (authctxt == NULL)
365 fatal("input_userauth_pk_ok: no authentication context");
366 if (datafellows & SSH_BUG_PKOK) {
367 /* this is similar to SSH_BUG_PKAUTH */
368 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
369 pkblob = packet_get_string(&blen);
370 buffer_init(&b);
371 buffer_append(&b, pkblob, blen);
372 pkalg = buffer_get_string(&b, &alen);
373 buffer_free(&b);
374 } else {
375 pkalg = packet_get_string(&alen);
376 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000377 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100378 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000379
Ben Lindstrom5c385522002-06-23 21:23:20 +0000380 debug("input_userauth_pk_ok: pkalg %s blen %u lastkey %p hint %d",
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000381 pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
382
383 do {
384 if (authctxt->last_key == NULL ||
385 authctxt->last_key_sign == NULL) {
386 debug("no last key or no sign cb");
387 break;
388 }
Damien Miller68f45982002-02-05 12:23:32 +1100389 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000390 debug("unknown pkalg %s", pkalg);
391 break;
392 }
393 if ((key = key_from_blob(pkblob, blen)) == NULL) {
394 debug("no key from blob. pkalg %s", pkalg);
395 break;
396 }
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000397 if (key->type != pktype) {
Damien Miller68f45982002-02-05 12:23:32 +1100398 error("input_userauth_pk_ok: type mismatch "
399 "for decoded key (received %d, expected %d)",
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000400 key->type, pktype);
Damien Miller68f45982002-02-05 12:23:32 +1100401 break;
402 }
Ben Lindstromcfccef92001-03-13 04:57:58 +0000403 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
404 debug2("input_userauth_pk_ok: fp %s", fp);
405 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000406 if (!key_equal(key, authctxt->last_key)) {
407 debug("key != last_key");
408 break;
409 }
410 sent = sign_and_send_pubkey(authctxt, key,
411 authctxt->last_key_sign);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000412 } while (0);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000413
414 if (key != NULL)
415 key_free(key);
416 xfree(pkalg);
417 xfree(pkblob);
418
419 /* unregister */
420 clear_auth_state(authctxt);
421 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
422
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000423 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000424 if (sent == 0)
425 userauth(authctxt, NULL);
426
Damien Miller62cee002000-09-23 17:15:56 +1100427}
428
Damien Millereba71ba2000-04-29 23:57:08 +1000429int
Damien Miller874d77b2000-10-14 16:23:11 +1100430userauth_none(Authctxt *authctxt)
431{
432 /* initial userauth request */
433 packet_start(SSH2_MSG_USERAUTH_REQUEST);
434 packet_put_cstring(authctxt->server_user);
435 packet_put_cstring(authctxt->service);
436 packet_put_cstring(authctxt->method->name);
437 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100438 return 1;
439}
440
441int
Damien Miller62cee002000-09-23 17:15:56 +1100442userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000443{
Damien Millere247cc42000-05-07 12:03:14 +1000444 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000445 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000446 char *password;
447
Damien Millerd3a18572000-06-07 19:55:44 +1000448 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000449 return 0;
450
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000451 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000452 error("Permission denied, please try again.");
453
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000454 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100455 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000456 password = read_passphrase(prompt, 0);
457 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100458 packet_put_cstring(authctxt->server_user);
459 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100460 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000461 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000462 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000463 memset(password, 0, strlen(password));
464 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100465 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000466 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000467
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000468 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000469 &input_userauth_passwd_changereq);
470
Damien Millereba71ba2000-04-29 23:57:08 +1000471 return 1;
472}
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000473/*
474 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
475 */
476void
Tim Ricec8549622002-03-31 12:49:38 -0800477input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000478{
479 Authctxt *authctxt = ctxt;
480 char *info, *lang, *password = NULL, *retype = NULL;
481 char prompt[150];
482
483 debug2("input_userauth_passwd_changereq");
484
485 if (authctxt == NULL)
486 fatal("input_userauth_passwd_changereq: "
487 "no authentication context");
488
489 info = packet_get_string(NULL);
490 lang = packet_get_string(NULL);
491 if (strlen(info) > 0)
492 log("%s", info);
493 xfree(info);
494 xfree(lang);
495 packet_start(SSH2_MSG_USERAUTH_REQUEST);
496 packet_put_cstring(authctxt->server_user);
497 packet_put_cstring(authctxt->service);
498 packet_put_cstring(authctxt->method->name);
499 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000500 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000501 "Enter %.30s@%.128s's old password: ",
502 authctxt->server_user, authctxt->host);
503 password = read_passphrase(prompt, 0);
504 packet_put_cstring(password);
505 memset(password, 0, strlen(password));
506 xfree(password);
507 password = NULL;
508 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000509 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000510 "Enter %.30s@%.128s's new password: ",
511 authctxt->server_user, authctxt->host);
512 password = read_passphrase(prompt, RP_ALLOW_EOF);
513 if (password == NULL) {
514 /* bail out */
515 return;
516 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000517 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000518 "Retype %.30s@%.128s's new password: ",
519 authctxt->server_user, authctxt->host);
520 retype = read_passphrase(prompt, 0);
521 if (strcmp(password, retype) != 0) {
522 memset(password, 0, strlen(password));
523 xfree(password);
524 log("Mismatch; try again, EOF to quit.");
525 password = NULL;
526 }
527 memset(retype, 0, strlen(retype));
528 xfree(retype);
529 }
530 packet_put_cstring(password);
531 memset(password, 0, strlen(password));
532 xfree(password);
533 packet_add_padding(64);
534 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000535
536 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000537 &input_userauth_passwd_changereq);
538}
Damien Millereba71ba2000-04-29 23:57:08 +1000539
Ben Lindstrom79073822001-07-04 03:42:30 +0000540static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000541clear_auth_state(Authctxt *authctxt)
542{
543 /* XXX clear authentication state */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000544 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
545
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000546 if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
547 debug3("clear_auth_state: key_free %p", authctxt->last_key);
548 key_free(authctxt->last_key);
549 }
550 authctxt->last_key = NULL;
551 authctxt->last_key_hint = -2;
552 authctxt->last_key_sign = NULL;
553}
554
Ben Lindstrombba81212001-06-25 05:01:22 +0000555static int
Damien Miller62cee002000-09-23 17:15:56 +1100556sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
Damien Millereba71ba2000-04-29 23:57:08 +1000557{
558 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000559 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000560 u_int bloblen, slen;
Damien Miller6536c7d2000-06-22 21:32:31 +1000561 int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000562 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100563 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000564
Ben Lindstromd121f612000-12-03 17:00:47 +0000565 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000566
Damien Miller0bc1bd82000-11-13 22:57:25 +1100567 if (key_to_blob(k, &blob, &bloblen) == 0) {
568 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000569 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100570 return 0;
571 }
Damien Millereba71ba2000-04-29 23:57:08 +1000572 /* data to be signed */
573 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100574 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000575 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000576 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100577 } else {
578 buffer_put_string(&b, session_id2, session_id2_len);
579 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000580 }
Damien Millereba71ba2000-04-29 23:57:08 +1000581 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100582 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000583 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000584 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000585 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100586 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000587 if (datafellows & SSH_BUG_PKAUTH) {
588 buffer_put_char(&b, have_sig);
589 } else {
590 buffer_put_cstring(&b, authctxt->method->name);
591 buffer_put_char(&b, have_sig);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000592 buffer_put_cstring(&b, key_ssh_name(k));
Ben Lindstromd121f612000-12-03 17:00:47 +0000593 }
Damien Millereba71ba2000-04-29 23:57:08 +1000594 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000595
596 /* generate signature */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000597 ret = (*sign_callback)(authctxt, k, &signature, &slen,
598 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000599 if (ret == -1) {
600 xfree(blob);
601 buffer_free(&b);
602 return 0;
603 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100604#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000605 buffer_dump(&b);
606#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000607 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000608 buffer_clear(&b);
609 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000610 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000611 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100612 buffer_put_cstring(&b, authctxt->server_user);
613 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100614 buffer_put_cstring(&b, authctxt->method->name);
615 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000616 if (!(datafellows & SSH_BUG_PKAUTH))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000617 buffer_put_cstring(&b, key_ssh_name(k));
Damien Miller30c3d422000-05-09 11:02:59 +1000618 buffer_put_string(&b, blob, bloblen);
619 }
620 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000621
Damien Millereba71ba2000-04-29 23:57:08 +1000622 /* append signature */
623 buffer_put_string(&b, signature, slen);
624 xfree(signature);
625
626 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000627 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100628 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000629 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000630
631 /* put remaining data from buffer into packet */
632 packet_start(SSH2_MSG_USERAUTH_REQUEST);
633 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
634 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +1000635 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000636
637 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000638}
639
Ben Lindstrombba81212001-06-25 05:01:22 +0000640static int
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000641send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
642 int hint)
Damien Miller994cf142000-07-21 10:19:44 +1000643{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000644 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000645 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000646
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000647 debug3("send_pubkey_test");
648
649 if (key_to_blob(k, &blob, &bloblen) == 0) {
650 /* we cannot handle this key */
651 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +1000652 return 0;
653 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000654 /* register callback for USERAUTH_PK_OK message */
655 authctxt->last_key_sign = sign_callback;
656 authctxt->last_key_hint = hint;
657 authctxt->last_key = k;
658 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +1000659
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000660 packet_start(SSH2_MSG_USERAUTH_REQUEST);
661 packet_put_cstring(authctxt->server_user);
662 packet_put_cstring(authctxt->service);
663 packet_put_cstring(authctxt->method->name);
664 packet_put_char(have_sig);
665 if (!(datafellows & SSH_BUG_PKAUTH))
666 packet_put_cstring(key_ssh_name(k));
667 packet_put_string(blob, bloblen);
668 xfree(blob);
669 packet_send();
670 return 1;
671}
672
Ben Lindstrombba81212001-06-25 05:01:22 +0000673static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000674load_identity_file(char *filename)
675{
676 Key *private;
677 char prompt[300], *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000678 int quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000679 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000680
Ben Lindstrom329782e2001-03-10 17:08:59 +0000681 if (stat(filename, &st) < 0) {
682 debug3("no such identity: %s", filename);
683 return NULL;
684 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000685 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
686 if (private == NULL) {
687 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000688 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000689 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100690 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100691 for (i = 0; i < options.number_of_password_prompts; i++) {
692 passphrase = read_passphrase(prompt, 0);
693 if (strcmp(passphrase, "") != 0) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000694 private = key_load_private_type(KEY_UNSPEC, filename,
695 passphrase, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000696 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100697 } else {
698 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000699 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +1100700 }
701 memset(passphrase, 0, strlen(passphrase));
702 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000703 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +1100704 break;
705 debug2("bad passphrase given, try again...");
706 }
Damien Miller994cf142000-07-21 10:19:44 +1000707 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000708 return private;
709}
710
Ben Lindstrombba81212001-06-25 05:01:22 +0000711static int
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000712identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
713 u_char *data, u_int datalen)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000714{
715 Key *private;
716 int idx, ret;
717
718 idx = authctxt->last_key_hint;
719 if (idx < 0)
720 return -1;
Ben Lindstromc5b68002001-07-04 04:52:03 +0000721
722 /* private key is stored in external hardware */
Damien Miller9f0f5c62001-12-21 14:45:46 +1100723 if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
Ben Lindstromc5b68002001-07-04 04:52:03 +0000724 return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
725
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000726 private = load_identity_file(options.identity_files[idx]);
727 if (private == NULL)
728 return -1;
729 ret = key_sign(private, sigp, lenp, data, datalen);
730 key_free(private);
Damien Millerad833b32000-08-23 10:46:23 +1000731 return ret;
732}
733
Ben Lindstrombba81212001-06-25 05:01:22 +0000734static int
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000735agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
736 u_char *data, u_int datalen)
Damien Millerad833b32000-08-23 10:46:23 +1000737{
Damien Miller62cee002000-09-23 17:15:56 +1100738 return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
Damien Millerad833b32000-08-23 10:46:23 +1000739}
740
Ben Lindstrombba81212001-06-25 05:01:22 +0000741static int
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000742key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
743 u_char *data, u_int datalen)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000744{
Ben Lindstroma3700052001-04-05 23:26:32 +0000745 return key_sign(key, sigp, lenp, data, datalen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000746}
747
Ben Lindstrombba81212001-06-25 05:01:22 +0000748static int
Damien Miller62cee002000-09-23 17:15:56 +1100749userauth_pubkey_agent(Authctxt *authctxt)
Damien Millerad833b32000-08-23 10:46:23 +1000750{
751 static int called = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100752 int ret = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000753 char *comment;
754 Key *k;
Damien Millerad833b32000-08-23 10:46:23 +1000755
756 if (called == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100757 if (ssh_get_num_identities(authctxt->agent, 2) == 0)
758 debug2("userauth_pubkey_agent: no keys at all");
Damien Miller62cee002000-09-23 17:15:56 +1100759 called = 1;
Damien Millerad833b32000-08-23 10:46:23 +1000760 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100761 k = ssh_get_next_identity(authctxt->agent, &comment, 2);
Damien Miller62cee002000-09-23 17:15:56 +1100762 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100763 debug2("userauth_pubkey_agent: no more keys");
764 } else {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000765 debug("userauth_pubkey_agent: testing agent key %s", comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100766 xfree(comment);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000767 ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
768 if (ret == 0)
769 key_free(k);
Damien Miller62cee002000-09-23 17:15:56 +1100770 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100771 if (ret == 0)
772 debug2("userauth_pubkey_agent: no message sent");
Damien Millerad833b32000-08-23 10:46:23 +1000773 return ret;
Damien Millereba71ba2000-04-29 23:57:08 +1000774}
775
Damien Miller62cee002000-09-23 17:15:56 +1100776int
777userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000778{
Damien Miller62cee002000-09-23 17:15:56 +1100779 static int idx = 0;
780 int sent = 0;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000781 Key *key;
782 char *filename;
Damien Millereba71ba2000-04-29 23:57:08 +1000783
Damien Miller0bc1bd82000-11-13 22:57:25 +1100784 if (authctxt->agent != NULL) {
785 do {
786 sent = userauth_pubkey_agent(authctxt);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000787 } while (!sent && authctxt->agent->howmany > 0);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100788 }
789 while (!sent && idx < options.num_identity_files) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000790 key = options.identity_keys[idx];
791 filename = options.identity_files[idx];
792 if (key == NULL) {
793 debug("try privkey: %s", filename);
794 key = load_identity_file(filename);
795 if (key != NULL) {
796 sent = sign_and_send_pubkey(authctxt, key,
797 key_sign_cb);
798 key_free(key);
799 }
800 } else if (key->type != KEY_RSA1) {
801 debug("try pubkey: %s", filename);
802 sent = send_pubkey_test(authctxt, key,
803 identity_sign_cb, idx);
804 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100805 idx++;
806 }
Damien Miller62cee002000-09-23 17:15:56 +1100807 return sent;
808}
Damien Millereba71ba2000-04-29 23:57:08 +1000809
Damien Miller874d77b2000-10-14 16:23:11 +1100810/*
811 * Send userauth request message specifying keyboard-interactive method.
812 */
813int
814userauth_kbdint(Authctxt *authctxt)
815{
816 static int attempt = 0;
817
818 if (attempt++ >= options.number_of_password_prompts)
819 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000820 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
821 if (attempt > 1 && !authctxt->info_req_seen) {
822 debug3("userauth_kbdint: disable: no info_req_seen");
823 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
824 return 0;
825 }
Damien Miller874d77b2000-10-14 16:23:11 +1100826
827 debug2("userauth_kbdint");
828 packet_start(SSH2_MSG_USERAUTH_REQUEST);
829 packet_put_cstring(authctxt->server_user);
830 packet_put_cstring(authctxt->service);
831 packet_put_cstring(authctxt->method->name);
832 packet_put_cstring(""); /* lang */
833 packet_put_cstring(options.kbd_interactive_devices ?
834 options.kbd_interactive_devices : "");
835 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100836
837 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
838 return 1;
839}
840
841/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000842 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +1100843 */
844void
Damien Miller630d6f42002-01-22 23:17:30 +1100845input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +1100846{
847 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000848 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000849 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +1100850 int echo = 0;
851
852 debug2("input_userauth_info_req");
853
854 if (authctxt == NULL)
855 fatal("input_userauth_info_req: no authentication context");
856
Ben Lindstrom7d199962001-09-12 18:29:00 +0000857 authctxt->info_req_seen = 1;
858
Damien Miller874d77b2000-10-14 16:23:11 +1100859 name = packet_get_string(NULL);
860 inst = packet_get_string(NULL);
861 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +1100862 if (strlen(name) > 0)
Ben Lindstrom126c56a2001-06-25 05:22:53 +0000863 log("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +1100864 if (strlen(inst) > 0)
Ben Lindstrom126c56a2001-06-25 05:22:53 +0000865 log("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000866 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +1100867 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000868 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100869
870 num_prompts = packet_get_int();
871 /*
872 * Begin to build info response packet based on prompts requested.
873 * We commit to providing the correct number of responses, so if
874 * further on we run into a problem that prevents this, we have to
875 * be sure and clean this up and send a correct error response.
876 */
877 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
878 packet_put_int(num_prompts);
879
Ben Lindstrom551ea372001-06-05 18:56:16 +0000880 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +1100881 for (i = 0; i < num_prompts; i++) {
882 prompt = packet_get_string(NULL);
883 echo = packet_get_char();
884
Ben Lindstrom949974b2001-06-25 05:20:31 +0000885 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +1100886
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000887 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +1100888 memset(response, 0, strlen(response));
889 xfree(response);
890 xfree(prompt);
891 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100892 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +1100893
Damien Miller9f643902001-11-12 11:02:52 +1100894 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +1100895 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100896}
Damien Miller62cee002000-09-23 17:15:56 +1100897
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000898static int
Ben Lindstrom5c385522002-06-23 21:23:20 +0000899ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000900 u_char *data, u_int datalen)
901{
902 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +0000903 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000904 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +0000905 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000906
907 debug("ssh_keysign called");
908
Ben Lindstrom5206b952002-06-06 19:59:29 +0000909 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
910 error("ssh_keysign: no installed: %s", strerror(errno));
911 return -1;
912 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000913 if (fflush(stdout) != 0)
914 error("ssh_keysign: fflush: %s", strerror(errno));
915 if (pipe(to) < 0) {
916 error("ssh_keysign: pipe: %s", strerror(errno));
917 return -1;
918 }
919 if (pipe(from) < 0) {
920 error("ssh_keysign: pipe: %s", strerror(errno));
921 return -1;
922 }
923 if ((pid = fork()) < 0) {
924 error("ssh_keysign: fork: %s", strerror(errno));
925 return -1;
926 }
927 if (pid == 0) {
928 seteuid(getuid());
929 setuid(getuid());
930 close(from[0]);
931 if (dup2(from[1], STDOUT_FILENO) < 0)
932 fatal("ssh_keysign: dup2: %s", strerror(errno));
933 close(to[1]);
934 if (dup2(to[0], STDIN_FILENO) < 0)
935 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +0000936 close(from[1]);
937 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +0000938 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000939 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
940 strerror(errno));
941 }
942 close(from[1]);
943 close(to[0]);
944
945 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +0000946 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000947 buffer_put_string(&b, data, datalen);
Damien Miller901119b2002-10-04 11:10:04 +1000948 ssh_msg_send(to[1], version, &b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000949
Damien Miller901119b2002-10-04 11:10:04 +1000950 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +0000951 error("ssh_keysign: no reply");
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000952 buffer_clear(&b);
953 return -1;
954 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000955 close(from[0]);
956 close(to[1]);
957
Ben Lindstromcec2ea82002-06-06 20:51:04 +0000958 while (waitpid(pid, &status, 0) < 0)
959 if (errno != EINTR)
960 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000961
Ben Lindstrom5206b952002-06-06 19:59:29 +0000962 if (buffer_get_char(&b) != version) {
963 error("ssh_keysign: bad version");
964 buffer_clear(&b);
965 return -1;
966 }
967 *sigp = buffer_get_string(&b, lenp);
968 buffer_clear(&b);
969
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000970 return 0;
971}
972
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000973int
974userauth_hostbased(Authctxt *authctxt)
975{
976 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000977 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000978 Buffer b;
979 u_char *signature, *blob;
980 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +0000981 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000982 u_int blen, slen;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +0000983 int ok, i, len, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000984
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000985 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000986 for (i = 0; i < sensitive->nkeys; i++) {
987 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000988 if (private && private->type != KEY_RSA1) {
989 found = 1;
990 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000991 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000992 break;
993 }
994 }
995 if (!found) {
Damien Miller91c18472001-11-12 11:02:03 +1100996 debug("userauth_hostbased: no more client hostkeys");
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000997 return 0;
998 }
999 if (key_to_blob(private, &blob, &blen) == 0) {
1000 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001001 return 0;
1002 }
Damien Miller91c18472001-11-12 11:02:03 +11001003 /* figure out a name for the client host */
1004 p = get_local_name(packet_get_connection_in());
1005 if (p == NULL) {
1006 error("userauth_hostbased: cannot get local ipaddr/name");
1007 key_free(private);
1008 return 0;
1009 }
1010 len = strlen(p) + 2;
1011 chost = xmalloc(len);
1012 strlcpy(chost, p, len);
1013 strlcat(chost, ".", len);
1014 debug2("userauth_hostbased: chost %s", chost);
1015
Ben Lindstrom671388f2001-04-19 20:40:45 +00001016 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1017 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001018 pkalg = xstrdup(key_ssh_name(private));
1019 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001020 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001021 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001022 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1023 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001024 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001025 buffer_put_cstring(&b, authctxt->method->name);
1026 buffer_put_cstring(&b, pkalg);
1027 buffer_put_string(&b, blob, blen);
1028 buffer_put_cstring(&b, chost);
1029 buffer_put_cstring(&b, authctxt->local_user);
1030#ifdef DEBUG_PK
1031 buffer_dump(&b);
1032#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001033 if (sensitive->external_keysign)
1034 ok = ssh_keysign(private, &signature, &slen,
1035 buffer_ptr(&b), buffer_len(&b));
1036 else
1037 ok = key_sign(private, &signature, &slen,
1038 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001039 key_free(private);
1040 buffer_free(&b);
1041 if (ok != 0) {
1042 error("key_sign failed");
1043 xfree(chost);
1044 xfree(pkalg);
1045 return 0;
1046 }
1047 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1048 packet_put_cstring(authctxt->server_user);
1049 packet_put_cstring(authctxt->service);
1050 packet_put_cstring(authctxt->method->name);
1051 packet_put_cstring(pkalg);
1052 packet_put_string(blob, blen);
1053 packet_put_cstring(chost);
1054 packet_put_cstring(authctxt->local_user);
1055 packet_put_string(signature, slen);
1056 memset(signature, 's', slen);
1057 xfree(signature);
1058 xfree(chost);
1059 xfree(pkalg);
1060
1061 packet_send();
1062 return 1;
1063}
1064
Damien Miller62cee002000-09-23 17:15:56 +11001065/* find auth method */
1066
Damien Miller62cee002000-09-23 17:15:56 +11001067/*
1068 * given auth method name, if configurable options permit this method fill
1069 * in auth_ident field and return true, otherwise return false.
1070 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001071static int
Damien Miller62cee002000-09-23 17:15:56 +11001072authmethod_is_enabled(Authmethod *method)
1073{
1074 if (method == NULL)
1075 return 0;
1076 /* return false if options indicate this method is disabled */
1077 if (method->enabled == NULL || *method->enabled == 0)
1078 return 0;
1079 /* return false if batch mode is enabled but method needs interactive mode */
1080 if (method->batch_flag != NULL && *method->batch_flag != 0)
1081 return 0;
1082 return 1;
1083}
1084
Ben Lindstrombba81212001-06-25 05:01:22 +00001085static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001086authmethod_lookup(const char *name)
1087{
1088 Authmethod *method = NULL;
1089 if (name != NULL)
1090 for (method = authmethods; method->name != NULL; method++)
1091 if (strcmp(name, method->name) == 0)
1092 return method;
1093 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1094 return NULL;
1095}
1096
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001097/* XXX internal state */
1098static Authmethod *current = NULL;
1099static char *supported = NULL;
1100static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001101
Damien Miller62cee002000-09-23 17:15:56 +11001102/*
1103 * Given the authentication method list sent by the server, return the
1104 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001105 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001106 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001107static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001108authmethod_get(char *authlist)
1109{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001110
1111 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001112 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001113
Damien Miller62cee002000-09-23 17:15:56 +11001114 /* Use a suitable default if we're passed a nil list. */
1115 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001116 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001117
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001118 if (supported == NULL || strcmp(authlist, supported) != 0) {
1119 debug3("start over, passed a different list %s", authlist);
1120 if (supported != NULL)
1121 xfree(supported);
1122 supported = xstrdup(authlist);
1123 preferred = options.preferred_authentications;
1124 debug3("preferred %s", preferred);
1125 current = NULL;
1126 } else if (current != NULL && authmethod_is_enabled(current))
1127 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001128
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001129 for (;;) {
1130 if ((name = match_list(preferred, supported, &next)) == NULL) {
1131 debug("no more auth methods to try");
1132 current = NULL;
1133 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001134 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001135 preferred += next;
1136 debug3("authmethod_lookup %s", name);
1137 debug3("remaining preferred: %s", preferred);
1138 if ((current = authmethod_lookup(name)) != NULL &&
1139 authmethod_is_enabled(current)) {
1140 debug3("authmethod_is_enabled %s", name);
1141 debug("next auth method to try is %s", name);
1142 return current;
1143 }
Damien Millereba71ba2000-04-29 23:57:08 +10001144 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001145}
Damien Miller62cee002000-09-23 17:15:56 +11001146
Ben Lindstrom79073822001-07-04 03:42:30 +00001147static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001148authmethods_get(void)
1149{
1150 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001151 Buffer b;
1152 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001153
Damien Miller0e3b8722002-01-22 23:26:38 +11001154 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001155 for (method = authmethods; method->name != NULL; method++) {
1156 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001157 if (buffer_len(&b) > 0)
1158 buffer_append(&b, ",", 1);
1159 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001160 }
Damien Miller62cee002000-09-23 17:15:56 +11001161 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001162 buffer_append(&b, "\0", 1);
1163 list = xstrdup(buffer_ptr(&b));
1164 buffer_free(&b);
1165 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001166}