blob: 067fad54522c977adbac72ebfa0a367cda56eb0e [file] [log] [blame]
Damien Miller7ba0ca72008-07-17 18:57:06 +10001/* $OpenBSD: sshconnect2.c,v 1.166 2008/07/17 08:48:00 djm Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100013 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "includes.h"
Damien Miller2eb63402006-03-15 11:09:42 +110027
Damien Miller9cf6d072006-03-15 11:29:24 +110028#include <sys/types.h>
Damien Millerd7834352006-08-05 12:39:39 +100029#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110030#include <sys/wait.h>
Damien Millerf17883e2006-03-15 11:45:54 +110031#include <sys/stat.h>
Damien Millereba71ba2000-04-29 23:57:08 +100032
Darren Tucker39972492006-07-12 22:22:46 +100033#include <errno.h>
Darren Tuckerf520ea12007-05-20 15:11:33 +100034#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100035#include <pwd.h>
36#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100037#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100038#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100039#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100040#include <unistd.h>
Damien Miller7ba0ca72008-07-17 18:57:06 +100041#include <vis.h>
Darren Tucker39972492006-07-12 22:22:46 +100042
Damien Miller9c617692003-05-14 14:31:11 +100043#include "openbsd-compat/sys-queue.h"
44
Damien Millerd7834352006-08-05 12:39:39 +100045#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100046#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000047#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100048#include "buffer.h"
49#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100050#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100052#include "key.h"
Damien Millereba71ba2000-04-29 23:57:08 +100053#include "kex.h"
54#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100055#include "sshconnect.h"
56#include "authfile.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000057#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100058#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000059#include "log.h"
60#include "readconf.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100061#include "misc.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000062#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000063#include "dispatch.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000064#include "canohost.h"
Ben Lindstrom1bad2562002-06-06 19:57:33 +000065#include "msg.h"
66#include "pathnames.h"
Damien Miller6b4069a2006-06-13 13:05:15 +100067#include "uidswap.h"
Damien Miller874d77b2000-10-14 16:23:11 +110068
Darren Tucker0efd1552003-08-26 11:49:55 +100069#ifdef GSSAPI
70#include "ssh-gss.h"
71#endif
72
Damien Millereba71ba2000-04-29 23:57:08 +100073/* import */
74extern char *client_version_string;
75extern char *server_version_string;
76extern Options options;
77
78/*
79 * SSH2 key exchange
80 */
81
Ben Lindstrom46c16222000-12-22 01:43:59 +000082u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100083u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100084
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000085char *xxx_host;
86struct sockaddr *xxx_hostaddr;
87
Ben Lindstromf28f6342001-04-04 02:03:04 +000088Kex *xxx_kex = NULL;
89
Ben Lindstrombba81212001-06-25 05:01:22 +000090static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000091verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000092{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000093 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +100094 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000095 return 0;
96}
97
Damien Millereba71ba2000-04-29 23:57:08 +100098void
Damien Miller874d77b2000-10-14 16:23:11 +110099ssh_kex2(char *host, struct sockaddr *hostaddr)
100{
Damien Miller874d77b2000-10-14 16:23:11 +1100101 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000102
103 xxx_host = host;
104 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100105
Damien Millere39cacc2000-11-29 12:18:44 +1100106 if (options.ciphers == (char *)-1) {
Damien Miller996acd22003-04-09 20:59:48 +1000107 logit("No valid ciphers for protocol version 2 given, using defaults.");
Damien Millere39cacc2000-11-29 12:18:44 +1100108 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100109 }
110 if (options.ciphers != NULL) {
111 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
112 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
113 }
Damien Millera0ff4662001-03-30 10:49:35 +1000114 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
115 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
116 myproposal[PROPOSAL_ENC_ALGS_STOC] =
117 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100118 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000119 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000120 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100121 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000122 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000123 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100124 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000125 if (options.macs != NULL) {
126 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
127 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
128 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000129 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100130 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000131 options.hostkeyalgorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100132
Damien Millera5539d22003-04-09 20:50:06 +1000133 if (options.rekey_limit)
Damien Miller3dff1762008-02-10 22:25:52 +1100134 packet_set_rekey_limit((u_int32_t)options.rekey_limit);
Damien Millera5539d22003-04-09 20:50:06 +1000135
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000136 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000137 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100138 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000139 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100140 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100141 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000142 kex->client_version_string=client_version_string;
143 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000144 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100145
Ben Lindstromf28f6342001-04-04 02:03:04 +0000146 xxx_kex = kex;
147
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000148 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100149
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000150 session_id2 = kex->session_id;
151 session_id2_len = kex->session_id_len;
152
Damien Miller874d77b2000-10-14 16:23:11 +1100153#ifdef DEBUG_KEXDH
154 /* send 1st encrypted/maced/compressed message */
155 packet_start(SSH2_MSG_IGNORE);
156 packet_put_cstring("markus");
157 packet_send();
158 packet_write_wait();
159#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000160}
Damien Millerb1715dc2000-05-30 13:44:51 +1000161
Damien Millereba71ba2000-04-29 23:57:08 +1000162/*
163 * Authenticate user
164 */
Damien Miller62cee002000-09-23 17:15:56 +1100165
166typedef struct Authctxt Authctxt;
167typedef struct Authmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000168typedef struct identity Identity;
169typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100170
Damien Miller280ecfb2003-05-14 13:46:00 +1000171struct identity {
172 TAILQ_ENTRY(identity) next;
173 AuthenticationConnection *ac; /* set if agent supports key */
174 Key *key; /* public/private key */
175 char *filename; /* comment for agent-only keys */
176 int tried;
177 int isprivate; /* key points to the private key */
178};
179TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100180
181struct Authctxt {
182 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000183 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100184 const char *host;
185 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100186 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100187 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000188 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000189 /* pubkey */
Damien Miller280ecfb2003-05-14 13:46:00 +1000190 Idlist keys;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000191 AuthenticationConnection *agent;
192 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000193 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000194 /* kbd-interactive */
195 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000196 /* generic */
197 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100198};
199struct Authmethod {
200 char *name; /* string to compare against server's list */
201 int (*userauth)(Authctxt *authctxt);
202 int *enabled; /* flag in option struct that enables method */
203 int *batch_flag; /* flag in option struct that disables method */
204};
205
Damien Miller630d6f42002-01-22 23:17:30 +1100206void input_userauth_success(int, u_int32_t, void *);
207void input_userauth_failure(int, u_int32_t, void *);
208void input_userauth_banner(int, u_int32_t, void *);
209void input_userauth_error(int, u_int32_t, void *);
210void input_userauth_info_req(int, u_int32_t, void *);
211void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000212void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100213
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000214int userauth_none(Authctxt *);
215int userauth_pubkey(Authctxt *);
216int userauth_passwd(Authctxt *);
217int userauth_kbdint(Authctxt *);
218int userauth_hostbased(Authctxt *);
Damien Miller3ab496b2003-05-14 13:47:37 +1000219int userauth_kerberos(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100220
Darren Tucker0efd1552003-08-26 11:49:55 +1000221#ifdef GSSAPI
222int userauth_gssapi(Authctxt *authctxt);
223void input_gssapi_response(int type, u_int32_t, void *);
224void input_gssapi_token(int type, u_int32_t, void *);
225void input_gssapi_hash(int type, u_int32_t, void *);
226void input_gssapi_error(int, u_int32_t, void *);
227void input_gssapi_errtok(int, u_int32_t, void *);
228#endif
229
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000230void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000231
Damien Miller280ecfb2003-05-14 13:46:00 +1000232static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000233static void pubkey_prepare(Authctxt *);
234static void pubkey_cleanup(Authctxt *);
235static Key *load_identity_file(char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000236
Ben Lindstrombba81212001-06-25 05:01:22 +0000237static Authmethod *authmethod_get(char *authlist);
238static Authmethod *authmethod_lookup(const char *name);
239static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100240
241Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000242#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100243 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000244 userauth_gssapi,
245 &options.gss_authentication,
246 NULL},
247#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000248 {"hostbased",
249 userauth_hostbased,
250 &options.hostbased_authentication,
251 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000252 {"publickey",
253 userauth_pubkey,
254 &options.pubkey_authentication,
255 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100256 {"keyboard-interactive",
257 userauth_kbdint,
258 &options.kbd_interactive_authentication,
259 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000260 {"password",
261 userauth_passwd,
262 &options.password_authentication,
263 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100264 {"none",
265 userauth_none,
266 NULL,
267 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100268 {NULL, NULL, NULL, NULL}
269};
270
271void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000272ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000273 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100274{
275 Authctxt authctxt;
276 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100277
Ben Lindstrom551ea372001-06-05 18:56:16 +0000278 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000279 options.kbd_interactive_authentication = 1;
280
Damien Miller62cee002000-09-23 17:15:56 +1100281 packet_start(SSH2_MSG_SERVICE_REQUEST);
282 packet_put_cstring("ssh-userauth");
283 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000284 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100285 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100286 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000287 if (type != SSH2_MSG_SERVICE_ACCEPT)
288 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100289 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100290 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000291 debug2("service_accept: %s", reply);
Damien Miller62cee002000-09-23 17:15:56 +1100292 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100293 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000294 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100295 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100296 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000297 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100298
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000299 if (options.preferred_authentications == NULL)
300 options.preferred_authentications = authmethods_get();
301
Damien Miller62cee002000-09-23 17:15:56 +1100302 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000303 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000304 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100305 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000306 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100307 authctxt.host = host;
308 authctxt.service = "ssh-connection"; /* service name */
309 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100310 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000311 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000312 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000313 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000314 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100315 if (authctxt.method == NULL)
316 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100317
318 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100319 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100320
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000321 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100322 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
323 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000324 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100325 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
326
Damien Miller280ecfb2003-05-14 13:46:00 +1000327 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000328 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
329
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000330 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100331}
Damien Millerf842fcb2003-05-15 12:01:28 +1000332
Damien Miller62cee002000-09-23 17:15:56 +1100333void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000334userauth(Authctxt *authctxt, char *authlist)
335{
Darren Tucker0efd1552003-08-26 11:49:55 +1000336 if (authctxt->methoddata) {
337 xfree(authctxt->methoddata);
338 authctxt->methoddata = NULL;
339 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000340 if (authlist == NULL) {
341 authlist = authctxt->authlist;
342 } else {
343 if (authctxt->authlist)
344 xfree(authctxt->authlist);
345 authctxt->authlist = authlist;
346 }
347 for (;;) {
348 Authmethod *method = authmethod_get(authlist);
349 if (method == NULL)
350 fatal("Permission denied (%s).", authlist);
351 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000352
353 /* reset the per method handler */
354 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
355 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
356
357 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000358 if (method->userauth(authctxt) != 0) {
359 debug2("we sent a %s packet, wait for reply", method->name);
360 break;
361 } else {
362 debug2("we did not send a packet, disable method");
363 method->enabled = NULL;
364 }
365 }
366}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000367
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000368void
Damien Miller630d6f42002-01-22 23:17:30 +1100369input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100370{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000371 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000372 "type %d", type);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000373}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000374
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000375void
Damien Miller630d6f42002-01-22 23:17:30 +1100376input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000377{
Damien Miller7ba0ca72008-07-17 18:57:06 +1000378 char *msg, *raw, *lang;
379 u_int len;
Darren Tucker79644822003-10-08 17:37:58 +1000380
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000381 debug3("input_userauth_banner");
Damien Miller7ba0ca72008-07-17 18:57:06 +1000382 raw = packet_get_string(&len);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000383 lang = packet_get_string(NULL);
Damien Miller7ba0ca72008-07-17 18:57:06 +1000384 if (options.log_level >= SYSLOG_LEVEL_INFO) {
385 if (len > 65536)
386 len = 65536;
387 msg = xmalloc(len * 4); /* max expansion from strnvis() */
388 strnvis(msg, raw, len * 4, VIS_SAFE|VIS_OCTAL);
Darren Tucker046dff22003-10-08 17:32:02 +1000389 fprintf(stderr, "%s", msg);
Damien Miller7ba0ca72008-07-17 18:57:06 +1000390 xfree(msg);
391 }
392 xfree(raw);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000393 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100394}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000395
Damien Miller62cee002000-09-23 17:15:56 +1100396void
Damien Miller630d6f42002-01-22 23:17:30 +1100397input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100398{
399 Authctxt *authctxt = ctxt;
400 if (authctxt == NULL)
401 fatal("input_userauth_success: no authentication context");
Darren Tucker79644822003-10-08 17:37:58 +1000402 if (authctxt->authlist) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000403 xfree(authctxt->authlist);
Darren Tucker79644822003-10-08 17:37:58 +1000404 authctxt->authlist = NULL;
405 }
406 if (authctxt->methoddata) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000407 xfree(authctxt->methoddata);
Darren Tucker79644822003-10-08 17:37:58 +1000408 authctxt->methoddata = NULL;
409 }
Damien Miller62cee002000-09-23 17:15:56 +1100410 authctxt->success = 1; /* break out */
411}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000412
Damien Miller62cee002000-09-23 17:15:56 +1100413void
Damien Miller630d6f42002-01-22 23:17:30 +1100414input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100415{
Damien Miller62cee002000-09-23 17:15:56 +1100416 Authctxt *authctxt = ctxt;
417 char *authlist = NULL;
418 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100419
420 if (authctxt == NULL)
421 fatal("input_userauth_failure: no authentication context");
422
Damien Miller874d77b2000-10-14 16:23:11 +1100423 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100424 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100425 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100426
427 if (partial != 0)
Damien Miller996acd22003-04-09 20:59:48 +1000428 logit("Authenticated with partial success.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000429 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100430
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000431 userauth(authctxt, authlist);
432}
433void
Damien Miller630d6f42002-01-22 23:17:30 +1100434input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000435{
436 Authctxt *authctxt = ctxt;
437 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000438 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000439 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000440 int pktype, sent = 0;
441 u_int alen, blen;
442 char *pkalg, *fp;
443 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000444
445 if (authctxt == NULL)
446 fatal("input_userauth_pk_ok: no authentication context");
447 if (datafellows & SSH_BUG_PKOK) {
448 /* this is similar to SSH_BUG_PKAUTH */
449 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
450 pkblob = packet_get_string(&blen);
451 buffer_init(&b);
452 buffer_append(&b, pkblob, blen);
453 pkalg = buffer_get_string(&b, &alen);
454 buffer_free(&b);
455 } else {
456 pkalg = packet_get_string(&alen);
457 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000458 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100459 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000460
Damien Miller280ecfb2003-05-14 13:46:00 +1000461 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000462
Damien Miller280ecfb2003-05-14 13:46:00 +1000463 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
464 debug("unknown pkalg %s", pkalg);
465 goto done;
466 }
467 if ((key = key_from_blob(pkblob, blen)) == NULL) {
468 debug("no key from blob. pkalg %s", pkalg);
469 goto done;
470 }
471 if (key->type != pktype) {
472 error("input_userauth_pk_ok: type mismatch "
473 "for decoded key (received %d, expected %d)",
474 key->type, pktype);
475 goto done;
476 }
477 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
478 debug2("input_userauth_pk_ok: fp %s", fp);
479 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000480
Darren Tuckerd05b6012003-10-15 15:55:59 +1000481 /*
482 * search keys in the reverse order, because last candidate has been
483 * moved to the end of the queue. this also avoids confusion by
484 * duplicate keys
485 */
Damien Miller0b51a522004-04-20 20:07:19 +1000486 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000487 if (key_equal(key, id->key)) {
488 sent = sign_and_send_pubkey(authctxt, id);
489 break;
490 }
491 }
492done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000493 if (key != NULL)
494 key_free(key);
495 xfree(pkalg);
496 xfree(pkblob);
497
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000498 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000499 if (sent == 0)
500 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100501}
502
Darren Tucker0efd1552003-08-26 11:49:55 +1000503#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100504int
Darren Tucker0efd1552003-08-26 11:49:55 +1000505userauth_gssapi(Authctxt *authctxt)
506{
507 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100508 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000509 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000510 OM_uint32 min;
511 int ok = 0;
512
513 /* Try one GSSAPI method at a time, rather than sending them all at
514 * once. */
515
Darren Tucker56afe142003-11-03 20:06:14 +1100516 if (gss_supported == NULL)
517 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000518
519 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100520 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000521 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100522 if (gss_supported->elements[mech].length < 128 &&
Damien Millera1cb9f32006-08-19 00:33:34 +1000523 ssh_gssapi_check_mechanism(&gssctxt,
524 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000525 ok = 1; /* Mechanism works */
526 } else {
527 mech++;
528 }
529 }
530
Damien Millera1cb9f32006-08-19 00:33:34 +1000531 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000532 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000533
534 authctxt->methoddata=(void *)gssctxt;
535
536 packet_start(SSH2_MSG_USERAUTH_REQUEST);
537 packet_put_cstring(authctxt->server_user);
538 packet_put_cstring(authctxt->service);
539 packet_put_cstring(authctxt->method->name);
540
541 packet_put_int(1);
542
Darren Tucker655a5e02003-11-03 20:09:03 +1100543 packet_put_int((gss_supported->elements[mech].length) + 2);
544 packet_put_char(SSH_GSS_OIDTYPE);
545 packet_put_char(gss_supported->elements[mech].length);
546 packet_put_raw(gss_supported->elements[mech].elements,
547 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000548
549 packet_send();
550
551 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
552 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
553 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
554 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
555
556 mech++; /* Move along to next candidate */
557
558 return 1;
559}
560
Damien Miller91c6aa42003-11-17 21:20:18 +1100561static OM_uint32
562process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
563{
564 Authctxt *authctxt = ctxt;
565 Gssctxt *gssctxt = authctxt->methoddata;
566 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000567 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
568 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100569 OM_uint32 status, ms, flags;
570 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100571
Damien Miller91c6aa42003-11-17 21:20:18 +1100572 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100573 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100574
575 if (send_tok.length > 0) {
576 if (GSS_ERROR(status))
577 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
578 else
579 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100580
Damien Miller91c6aa42003-11-17 21:20:18 +1100581 packet_put_string(send_tok.value, send_tok.length);
582 packet_send();
583 gss_release_buffer(&ms, &send_tok);
584 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100585
Damien Miller91c6aa42003-11-17 21:20:18 +1100586 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100587 /* send either complete or MIC, depending on mechanism */
588 if (!(flags & GSS_C_INTEG_FLAG)) {
589 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
590 packet_send();
591 } else {
592 ssh_gssapi_buildmic(&b, authctxt->server_user,
593 authctxt->service, "gssapi-with-mic");
594
595 gssbuf.value = buffer_ptr(&b);
596 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100597
Damien Miller0425d402003-11-17 22:18:21 +1100598 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100599
Damien Miller0425d402003-11-17 22:18:21 +1100600 if (!GSS_ERROR(status)) {
601 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
602 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100603
Damien Miller0425d402003-11-17 22:18:21 +1100604 packet_send();
605 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100606
Damien Miller0425d402003-11-17 22:18:21 +1100607 buffer_free(&b);
608 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100609 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100610 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100611
Damien Miller91c6aa42003-11-17 21:20:18 +1100612 return status;
613}
614
Darren Tucker0efd1552003-08-26 11:49:55 +1000615void
616input_gssapi_response(int type, u_int32_t plen, void *ctxt)
617{
618 Authctxt *authctxt = ctxt;
619 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000620 int oidlen;
621 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000622
623 if (authctxt == NULL)
624 fatal("input_gssapi_response: no authentication context");
625 gssctxt = authctxt->methoddata;
626
627 /* Setup our OID */
628 oidv = packet_get_string(&oidlen);
629
Darren Tucker655a5e02003-11-03 20:09:03 +1100630 if (oidlen <= 2 ||
631 oidv[0] != SSH_GSS_OIDTYPE ||
632 oidv[1] != oidlen - 2) {
Damien Miller91c6aa42003-11-17 21:20:18 +1100633 xfree(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100634 debug("Badly encoded mechanism OID received");
635 userauth(authctxt, NULL);
Darren Tucker655a5e02003-11-03 20:09:03 +1100636 return;
Darren Tucker0efd1552003-08-26 11:49:55 +1000637 }
638
Darren Tucker655a5e02003-11-03 20:09:03 +1100639 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
640 fatal("Server returned different OID than expected");
641
Darren Tucker0efd1552003-08-26 11:49:55 +1000642 packet_check_eom();
643
644 xfree(oidv);
645
Damien Miller91c6aa42003-11-17 21:20:18 +1100646 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000647 /* Start again with next method on list */
648 debug("Trying to start again");
649 userauth(authctxt, NULL);
650 return;
651 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000652}
653
654void
655input_gssapi_token(int type, u_int32_t plen, void *ctxt)
656{
657 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000658 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100659 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000660 u_int slen;
661
662 if (authctxt == NULL)
663 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000664
665 recv_tok.value = packet_get_string(&slen);
666 recv_tok.length = slen; /* safe typecast */
667
668 packet_check_eom();
669
Damien Miller91c6aa42003-11-17 21:20:18 +1100670 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000671
672 xfree(recv_tok.value);
673
674 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000675 /* Start again with the next method in the list */
676 userauth(authctxt, NULL);
677 return;
678 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000679}
680
681void
682input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
683{
684 Authctxt *authctxt = ctxt;
685 Gssctxt *gssctxt;
686 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
687 gss_buffer_desc recv_tok;
688 OM_uint32 status, ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000689 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000690
691 if (authctxt == NULL)
692 fatal("input_gssapi_response: no authentication context");
693 gssctxt = authctxt->methoddata;
694
Darren Tucker600ad8d2003-08-26 12:10:48 +1000695 recv_tok.value = packet_get_string(&len);
696 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000697
698 packet_check_eom();
699
700 /* Stick it into GSSAPI and see what it says */
701 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000702 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000703
704 xfree(recv_tok.value);
705 gss_release_buffer(&ms, &send_tok);
706
707 /* Server will be returning a failed packet after this one */
708}
709
710void
711input_gssapi_error(int type, u_int32_t plen, void *ctxt)
712{
713 OM_uint32 maj, min;
714 char *msg;
715 char *lang;
716
717 maj=packet_get_int();
718 min=packet_get_int();
719 msg=packet_get_string(NULL);
720 lang=packet_get_string(NULL);
721
722 packet_check_eom();
723
Damien Miller15d72a02005-11-05 15:07:33 +1100724 debug("Server GSSAPI Error:\n%s", msg);
Darren Tucker0efd1552003-08-26 11:49:55 +1000725 xfree(msg);
726 xfree(lang);
727}
728#endif /* GSSAPI */
729
Damien Millereba71ba2000-04-29 23:57:08 +1000730int
Damien Miller874d77b2000-10-14 16:23:11 +1100731userauth_none(Authctxt *authctxt)
732{
733 /* initial userauth request */
734 packet_start(SSH2_MSG_USERAUTH_REQUEST);
735 packet_put_cstring(authctxt->server_user);
736 packet_put_cstring(authctxt->service);
737 packet_put_cstring(authctxt->method->name);
738 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100739 return 1;
740}
741
742int
Damien Miller62cee002000-09-23 17:15:56 +1100743userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000744{
Damien Millere247cc42000-05-07 12:03:14 +1000745 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000746 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000747 char *password;
748
Damien Millerd3a18572000-06-07 19:55:44 +1000749 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000750 return 0;
751
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000752 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000753 error("Permission denied, please try again.");
754
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000755 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100756 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000757 password = read_passphrase(prompt, 0);
758 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100759 packet_put_cstring(authctxt->server_user);
760 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100761 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000762 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000763 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000764 memset(password, 0, strlen(password));
765 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100766 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000767 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000768
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000769 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000770 &input_userauth_passwd_changereq);
771
Damien Millereba71ba2000-04-29 23:57:08 +1000772 return 1;
773}
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000774/*
775 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
776 */
777void
Tim Ricec8549622002-03-31 12:49:38 -0800778input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000779{
780 Authctxt *authctxt = ctxt;
781 char *info, *lang, *password = NULL, *retype = NULL;
782 char prompt[150];
783
784 debug2("input_userauth_passwd_changereq");
785
786 if (authctxt == NULL)
787 fatal("input_userauth_passwd_changereq: "
788 "no authentication context");
789
790 info = packet_get_string(NULL);
791 lang = packet_get_string(NULL);
792 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000793 logit("%s", info);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000794 xfree(info);
795 xfree(lang);
796 packet_start(SSH2_MSG_USERAUTH_REQUEST);
797 packet_put_cstring(authctxt->server_user);
798 packet_put_cstring(authctxt->service);
799 packet_put_cstring(authctxt->method->name);
800 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000801 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000802 "Enter %.30s@%.128s's old password: ",
803 authctxt->server_user, authctxt->host);
804 password = read_passphrase(prompt, 0);
805 packet_put_cstring(password);
806 memset(password, 0, strlen(password));
807 xfree(password);
808 password = NULL;
809 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000810 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000811 "Enter %.30s@%.128s's new password: ",
812 authctxt->server_user, authctxt->host);
813 password = read_passphrase(prompt, RP_ALLOW_EOF);
814 if (password == NULL) {
815 /* bail out */
816 return;
817 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000818 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000819 "Retype %.30s@%.128s's new password: ",
820 authctxt->server_user, authctxt->host);
821 retype = read_passphrase(prompt, 0);
822 if (strcmp(password, retype) != 0) {
823 memset(password, 0, strlen(password));
824 xfree(password);
Damien Miller996acd22003-04-09 20:59:48 +1000825 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000826 password = NULL;
827 }
828 memset(retype, 0, strlen(retype));
829 xfree(retype);
830 }
831 packet_put_cstring(password);
832 memset(password, 0, strlen(password));
833 xfree(password);
834 packet_add_padding(64);
835 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000836
837 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000838 &input_userauth_passwd_changereq);
839}
Damien Millereba71ba2000-04-29 23:57:08 +1000840
Ben Lindstrombba81212001-06-25 05:01:22 +0000841static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000842identity_sign(Identity *id, u_char **sigp, u_int *lenp,
843 u_char *data, u_int datalen)
844{
845 Key *prv;
846 int ret;
847
848 /* the agent supports this key */
849 if (id->ac)
850 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
851 data, datalen));
852 /*
853 * we have already loaded the private key or
854 * the private key is stored in external hardware
855 */
856 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
857 return (key_sign(id->key, sigp, lenp, data, datalen));
858 /* load the private key from the file */
859 if ((prv = load_identity_file(id->filename)) == NULL)
860 return (-1);
861 ret = key_sign(prv, sigp, lenp, data, datalen);
862 key_free(prv);
863 return (ret);
864}
865
866static int
867sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +1000868{
869 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000870 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000871 u_int bloblen, slen;
Darren Tucker502d3842003-06-28 12:38:01 +1000872 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000873 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100874 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000875
Ben Lindstromd121f612000-12-03 17:00:47 +0000876 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000877
Damien Miller280ecfb2003-05-14 13:46:00 +1000878 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100879 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000880 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100881 return 0;
882 }
Damien Millereba71ba2000-04-29 23:57:08 +1000883 /* data to be signed */
884 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100885 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000886 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000887 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100888 } else {
889 buffer_put_string(&b, session_id2, session_id2_len);
890 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000891 }
Damien Millereba71ba2000-04-29 23:57:08 +1000892 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100893 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000894 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000895 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000896 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100897 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000898 if (datafellows & SSH_BUG_PKAUTH) {
899 buffer_put_char(&b, have_sig);
900 } else {
901 buffer_put_cstring(&b, authctxt->method->name);
902 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +1000903 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +0000904 }
Damien Millereba71ba2000-04-29 23:57:08 +1000905 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000906
907 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +1000908 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000909 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000910 if (ret == -1) {
911 xfree(blob);
912 buffer_free(&b);
913 return 0;
914 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100915#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000916 buffer_dump(&b);
917#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000918 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000919 buffer_clear(&b);
920 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000921 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000922 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100923 buffer_put_cstring(&b, authctxt->server_user);
924 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100925 buffer_put_cstring(&b, authctxt->method->name);
926 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000927 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +1000928 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +1000929 buffer_put_string(&b, blob, bloblen);
930 }
931 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000932
Damien Millereba71ba2000-04-29 23:57:08 +1000933 /* append signature */
934 buffer_put_string(&b, signature, slen);
935 xfree(signature);
936
937 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000938 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100939 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000940 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000941
942 /* put remaining data from buffer into packet */
943 packet_start(SSH2_MSG_USERAUTH_REQUEST);
944 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
945 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +1000946 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000947
948 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000949}
950
Ben Lindstrombba81212001-06-25 05:01:22 +0000951static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000952send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +1000953{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000954 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000955 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000956
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000957 debug3("send_pubkey_test");
958
Damien Miller280ecfb2003-05-14 13:46:00 +1000959 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000960 /* we cannot handle this key */
961 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +1000962 return 0;
963 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000964 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000965 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +1000966
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000967 packet_start(SSH2_MSG_USERAUTH_REQUEST);
968 packet_put_cstring(authctxt->server_user);
969 packet_put_cstring(authctxt->service);
970 packet_put_cstring(authctxt->method->name);
971 packet_put_char(have_sig);
972 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +1000973 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000974 packet_put_string(blob, bloblen);
975 xfree(blob);
976 packet_send();
977 return 1;
978}
979
Ben Lindstrombba81212001-06-25 05:01:22 +0000980static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000981load_identity_file(char *filename)
982{
983 Key *private;
984 char prompt[300], *passphrase;
Darren Tucker232b76f2006-05-06 17:41:51 +1000985 int perm_ok, quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000986 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000987
Ben Lindstrom329782e2001-03-10 17:08:59 +0000988 if (stat(filename, &st) < 0) {
989 debug3("no such identity: %s", filename);
990 return NULL;
991 }
Darren Tucker232b76f2006-05-06 17:41:51 +1000992 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
993 if (!perm_ok)
994 return NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000995 if (private == NULL) {
996 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000997 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000998 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100999 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +11001000 for (i = 0; i < options.number_of_password_prompts; i++) {
1001 passphrase = read_passphrase(prompt, 0);
1002 if (strcmp(passphrase, "") != 0) {
Darren Tucker232b76f2006-05-06 17:41:51 +10001003 private = key_load_private_type(KEY_UNSPEC,
1004 filename, passphrase, NULL, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001005 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001006 } else {
1007 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001008 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +11001009 }
1010 memset(passphrase, 0, strlen(passphrase));
1011 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001012 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +11001013 break;
1014 debug2("bad passphrase given, try again...");
1015 }
Damien Miller994cf142000-07-21 10:19:44 +10001016 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001017 return private;
1018}
1019
Damien Miller280ecfb2003-05-14 13:46:00 +10001020/*
1021 * try keys in the following order:
1022 * 1. agent keys that are found in the config file
1023 * 2. other agent keys
1024 * 3. keys that are only listed in the config file
1025 */
1026static void
1027pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001028{
Damien Miller280ecfb2003-05-14 13:46:00 +10001029 Identity *id;
1030 Idlist agent, files, *preferred;
1031 Key *key;
1032 AuthenticationConnection *ac;
Damien Millerad833b32000-08-23 10:46:23 +10001033 char *comment;
Damien Miller280ecfb2003-05-14 13:46:00 +10001034 int i, found;
Damien Millerad833b32000-08-23 10:46:23 +10001035
Damien Miller280ecfb2003-05-14 13:46:00 +10001036 TAILQ_INIT(&agent); /* keys from the agent */
1037 TAILQ_INIT(&files); /* keys from the config file */
1038 preferred = &authctxt->keys;
1039 TAILQ_INIT(preferred); /* preferred order of keys */
1040
1041 /* list of keys stored in the filesystem */
1042 for (i = 0; i < options.num_identity_files; i++) {
1043 key = options.identity_keys[i];
1044 if (key && key->type == KEY_RSA1)
1045 continue;
1046 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001047 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001048 id->key = key;
1049 id->filename = xstrdup(options.identity_files[i]);
1050 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001051 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001052 /* list of keys supported by the agent */
1053 if ((ac = ssh_get_authentication_connection())) {
1054 for (key = ssh_get_first_identity(ac, &comment, 2);
1055 key != NULL;
1056 key = ssh_get_next_identity(ac, &comment, 2)) {
1057 found = 0;
1058 TAILQ_FOREACH(id, &files, next) {
Damien Millera8e06ce2003-11-21 23:48:55 +11001059 /* agent keys from the config file are preferred */
Damien Miller280ecfb2003-05-14 13:46:00 +10001060 if (key_equal(key, id->key)) {
1061 key_free(key);
1062 xfree(comment);
1063 TAILQ_REMOVE(&files, id, next);
1064 TAILQ_INSERT_TAIL(preferred, id, next);
1065 id->ac = ac;
1066 found = 1;
1067 break;
1068 }
1069 }
Damien Millerbd394c32004-03-08 23:12:36 +11001070 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001071 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001072 id->key = key;
1073 id->filename = comment;
1074 id->ac = ac;
1075 TAILQ_INSERT_TAIL(&agent, id, next);
1076 }
1077 }
1078 /* append remaining agent keys */
1079 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1080 TAILQ_REMOVE(&agent, id, next);
1081 TAILQ_INSERT_TAIL(preferred, id, next);
1082 }
1083 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +11001084 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001085 /* append remaining keys from the config file */
1086 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1087 TAILQ_REMOVE(&files, id, next);
1088 TAILQ_INSERT_TAIL(preferred, id, next);
1089 }
1090 TAILQ_FOREACH(id, preferred, next) {
1091 debug2("key: %s (%p)", id->filename, id->key);
1092 }
1093}
1094
1095static void
1096pubkey_cleanup(Authctxt *authctxt)
1097{
1098 Identity *id;
1099
1100 if (authctxt->agent != NULL)
1101 ssh_close_authentication_connection(authctxt->agent);
1102 for (id = TAILQ_FIRST(&authctxt->keys); id;
1103 id = TAILQ_FIRST(&authctxt->keys)) {
1104 TAILQ_REMOVE(&authctxt->keys, id, next);
1105 if (id->key)
1106 key_free(id->key);
1107 if (id->filename)
1108 xfree(id->filename);
1109 xfree(id);
1110 }
Damien Millereba71ba2000-04-29 23:57:08 +10001111}
1112
Damien Miller62cee002000-09-23 17:15:56 +11001113int
1114userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001115{
Damien Miller280ecfb2003-05-14 13:46:00 +10001116 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001117 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001118
Damien Miller280ecfb2003-05-14 13:46:00 +10001119 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1120 if (id->tried++)
1121 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001122 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001123 TAILQ_REMOVE(&authctxt->keys, id, next);
1124 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1125 /*
1126 * send a test message if we have the public key. for
1127 * encrypted keys we cannot do this and have to load the
1128 * private key instead
1129 */
1130 if (id->key && id->key->type != KEY_RSA1) {
1131 debug("Offering public key: %s", id->filename);
1132 sent = send_pubkey_test(authctxt, id);
1133 } else if (id->key == NULL) {
1134 debug("Trying private key: %s", id->filename);
1135 id->key = load_identity_file(id->filename);
1136 if (id->key != NULL) {
1137 id->isprivate = 1;
1138 sent = sign_and_send_pubkey(authctxt, id);
1139 key_free(id->key);
1140 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001141 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001142 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001143 if (sent)
1144 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001145 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001146 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001147}
Damien Millereba71ba2000-04-29 23:57:08 +10001148
Damien Miller874d77b2000-10-14 16:23:11 +11001149/*
1150 * Send userauth request message specifying keyboard-interactive method.
1151 */
1152int
1153userauth_kbdint(Authctxt *authctxt)
1154{
1155 static int attempt = 0;
1156
1157 if (attempt++ >= options.number_of_password_prompts)
1158 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001159 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1160 if (attempt > 1 && !authctxt->info_req_seen) {
1161 debug3("userauth_kbdint: disable: no info_req_seen");
1162 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1163 return 0;
1164 }
Damien Miller874d77b2000-10-14 16:23:11 +11001165
1166 debug2("userauth_kbdint");
1167 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1168 packet_put_cstring(authctxt->server_user);
1169 packet_put_cstring(authctxt->service);
1170 packet_put_cstring(authctxt->method->name);
1171 packet_put_cstring(""); /* lang */
1172 packet_put_cstring(options.kbd_interactive_devices ?
1173 options.kbd_interactive_devices : "");
1174 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001175
1176 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1177 return 1;
1178}
1179
1180/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001181 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001182 */
1183void
Damien Miller630d6f42002-01-22 23:17:30 +11001184input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001185{
1186 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001187 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001188 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001189 int echo = 0;
1190
1191 debug2("input_userauth_info_req");
1192
1193 if (authctxt == NULL)
1194 fatal("input_userauth_info_req: no authentication context");
1195
Ben Lindstrom7d199962001-09-12 18:29:00 +00001196 authctxt->info_req_seen = 1;
1197
Damien Miller874d77b2000-10-14 16:23:11 +11001198 name = packet_get_string(NULL);
1199 inst = packet_get_string(NULL);
1200 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001201 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001202 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001203 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001204 logit("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001205 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001206 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001207 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001208
1209 num_prompts = packet_get_int();
1210 /*
1211 * Begin to build info response packet based on prompts requested.
1212 * We commit to providing the correct number of responses, so if
1213 * further on we run into a problem that prevents this, we have to
1214 * be sure and clean this up and send a correct error response.
1215 */
1216 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1217 packet_put_int(num_prompts);
1218
Ben Lindstrom551ea372001-06-05 18:56:16 +00001219 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001220 for (i = 0; i < num_prompts; i++) {
1221 prompt = packet_get_string(NULL);
1222 echo = packet_get_char();
1223
Ben Lindstrom949974b2001-06-25 05:20:31 +00001224 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001225
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001226 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +11001227 memset(response, 0, strlen(response));
1228 xfree(response);
1229 xfree(prompt);
1230 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001231 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001232
Damien Miller9f643902001-11-12 11:02:52 +11001233 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001234 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001235}
Damien Miller62cee002000-09-23 17:15:56 +11001236
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001237static int
Ben Lindstrom5c385522002-06-23 21:23:20 +00001238ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001239 u_char *data, u_int datalen)
1240{
1241 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001242 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001243 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001244 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001245
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001246 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001247
Ben Lindstrom5206b952002-06-06 19:59:29 +00001248 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1249 error("ssh_keysign: no installed: %s", strerror(errno));
1250 return -1;
1251 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001252 if (fflush(stdout) != 0)
1253 error("ssh_keysign: fflush: %s", strerror(errno));
1254 if (pipe(to) < 0) {
1255 error("ssh_keysign: pipe: %s", strerror(errno));
1256 return -1;
1257 }
1258 if (pipe(from) < 0) {
1259 error("ssh_keysign: pipe: %s", strerror(errno));
1260 return -1;
1261 }
1262 if ((pid = fork()) < 0) {
1263 error("ssh_keysign: fork: %s", strerror(errno));
1264 return -1;
1265 }
1266 if (pid == 0) {
Damien Miller2e5fe882006-06-13 13:10:00 +10001267 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001268 close(from[0]);
1269 if (dup2(from[1], STDOUT_FILENO) < 0)
1270 fatal("ssh_keysign: dup2: %s", strerror(errno));
1271 close(to[1]);
1272 if (dup2(to[0], STDIN_FILENO) < 0)
1273 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001274 close(from[1]);
1275 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001276 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001277 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1278 strerror(errno));
1279 }
1280 close(from[1]);
1281 close(to[0]);
1282
1283 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001284 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001285 buffer_put_string(&b, data, datalen);
Damien Miller51bf11f2003-11-17 21:20:47 +11001286 if (ssh_msg_send(to[1], version, &b) == -1)
1287 fatal("ssh_keysign: couldn't send request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001288
Damien Miller901119b2002-10-04 11:10:04 +10001289 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001290 error("ssh_keysign: no reply");
Damien Millerfb1310e2004-01-21 11:02:50 +11001291 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001292 return -1;
1293 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001294 close(from[0]);
1295 close(to[1]);
1296
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001297 while (waitpid(pid, &status, 0) < 0)
1298 if (errno != EINTR)
1299 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001300
Ben Lindstrom5206b952002-06-06 19:59:29 +00001301 if (buffer_get_char(&b) != version) {
1302 error("ssh_keysign: bad version");
Damien Millerfb1310e2004-01-21 11:02:50 +11001303 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001304 return -1;
1305 }
1306 *sigp = buffer_get_string(&b, lenp);
Damien Millerfb1310e2004-01-21 11:02:50 +11001307 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001308
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001309 return 0;
1310}
1311
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001312int
1313userauth_hostbased(Authctxt *authctxt)
1314{
1315 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001316 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001317 Buffer b;
1318 u_char *signature, *blob;
Darren Tucker26c66622007-05-20 15:09:42 +10001319 char *chost, *pkalg, *p, myname[NI_MAXHOST];
Ben Lindstrom671388f2001-04-19 20:40:45 +00001320 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001321 u_int blen, slen;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +00001322 int ok, i, len, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001323
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001324 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001325 for (i = 0; i < sensitive->nkeys; i++) {
1326 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001327 if (private && private->type != KEY_RSA1) {
1328 found = 1;
1329 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001330 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001331 break;
1332 }
1333 }
1334 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001335 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001336 return 0;
1337 }
1338 if (key_to_blob(private, &blob, &blen) == 0) {
1339 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001340 return 0;
1341 }
Damien Miller91c18472001-11-12 11:02:03 +11001342 /* figure out a name for the client host */
Darren Tucker26c66622007-05-20 15:09:42 +10001343 p = NULL;
1344 if (packet_connection_is_on_socket())
1345 p = get_local_name(packet_get_connection_in());
1346 if (p == NULL) {
1347 if (gethostname(myname, sizeof(myname)) == -1) {
1348 verbose("userauth_hostbased: gethostname: %s",
1349 strerror(errno));
1350 } else
1351 p = xstrdup(myname);
1352 }
Damien Miller91c18472001-11-12 11:02:03 +11001353 if (p == NULL) {
1354 error("userauth_hostbased: cannot get local ipaddr/name");
1355 key_free(private);
Damien Miller5790b592006-03-26 13:54:03 +11001356 xfree(blob);
Damien Miller91c18472001-11-12 11:02:03 +11001357 return 0;
1358 }
1359 len = strlen(p) + 2;
Damien Miller07d86be2006-03-26 14:19:21 +11001360 xasprintf(&chost, "%s.", p);
Damien Miller91c18472001-11-12 11:02:03 +11001361 debug2("userauth_hostbased: chost %s", chost);
Damien Miller00111382003-03-10 11:21:17 +11001362 xfree(p);
Damien Miller91c18472001-11-12 11:02:03 +11001363
Ben Lindstrom671388f2001-04-19 20:40:45 +00001364 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1365 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001366 pkalg = xstrdup(key_ssh_name(private));
1367 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001368 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001369 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001370 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1371 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001372 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001373 buffer_put_cstring(&b, authctxt->method->name);
1374 buffer_put_cstring(&b, pkalg);
1375 buffer_put_string(&b, blob, blen);
1376 buffer_put_cstring(&b, chost);
1377 buffer_put_cstring(&b, authctxt->local_user);
1378#ifdef DEBUG_PK
1379 buffer_dump(&b);
1380#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001381 if (sensitive->external_keysign)
1382 ok = ssh_keysign(private, &signature, &slen,
1383 buffer_ptr(&b), buffer_len(&b));
1384 else
1385 ok = key_sign(private, &signature, &slen,
1386 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001387 key_free(private);
1388 buffer_free(&b);
1389 if (ok != 0) {
1390 error("key_sign failed");
1391 xfree(chost);
1392 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001393 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001394 return 0;
1395 }
1396 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1397 packet_put_cstring(authctxt->server_user);
1398 packet_put_cstring(authctxt->service);
1399 packet_put_cstring(authctxt->method->name);
1400 packet_put_cstring(pkalg);
1401 packet_put_string(blob, blen);
1402 packet_put_cstring(chost);
1403 packet_put_cstring(authctxt->local_user);
1404 packet_put_string(signature, slen);
1405 memset(signature, 's', slen);
1406 xfree(signature);
1407 xfree(chost);
1408 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001409 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001410
1411 packet_send();
1412 return 1;
1413}
1414
Damien Miller62cee002000-09-23 17:15:56 +11001415/* find auth method */
1416
Damien Miller62cee002000-09-23 17:15:56 +11001417/*
1418 * given auth method name, if configurable options permit this method fill
1419 * in auth_ident field and return true, otherwise return false.
1420 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001421static int
Damien Miller62cee002000-09-23 17:15:56 +11001422authmethod_is_enabled(Authmethod *method)
1423{
1424 if (method == NULL)
1425 return 0;
1426 /* return false if options indicate this method is disabled */
1427 if (method->enabled == NULL || *method->enabled == 0)
1428 return 0;
1429 /* return false if batch mode is enabled but method needs interactive mode */
1430 if (method->batch_flag != NULL && *method->batch_flag != 0)
1431 return 0;
1432 return 1;
1433}
1434
Ben Lindstrombba81212001-06-25 05:01:22 +00001435static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001436authmethod_lookup(const char *name)
1437{
1438 Authmethod *method = NULL;
1439 if (name != NULL)
1440 for (method = authmethods; method->name != NULL; method++)
1441 if (strcmp(name, method->name) == 0)
1442 return method;
1443 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1444 return NULL;
1445}
1446
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001447/* XXX internal state */
1448static Authmethod *current = NULL;
1449static char *supported = NULL;
1450static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001451
Damien Miller62cee002000-09-23 17:15:56 +11001452/*
1453 * Given the authentication method list sent by the server, return the
1454 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001455 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001456 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001457static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001458authmethod_get(char *authlist)
1459{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001460 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001461 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001462
Damien Miller62cee002000-09-23 17:15:56 +11001463 /* Use a suitable default if we're passed a nil list. */
1464 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001465 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001466
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001467 if (supported == NULL || strcmp(authlist, supported) != 0) {
1468 debug3("start over, passed a different list %s", authlist);
1469 if (supported != NULL)
1470 xfree(supported);
1471 supported = xstrdup(authlist);
1472 preferred = options.preferred_authentications;
1473 debug3("preferred %s", preferred);
1474 current = NULL;
1475 } else if (current != NULL && authmethod_is_enabled(current))
1476 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001477
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001478 for (;;) {
1479 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001480 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001481 current = NULL;
1482 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001483 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001484 preferred += next;
1485 debug3("authmethod_lookup %s", name);
1486 debug3("remaining preferred: %s", preferred);
1487 if ((current = authmethod_lookup(name)) != NULL &&
1488 authmethod_is_enabled(current)) {
1489 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001490 debug("Next authentication method: %s", name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001491 return current;
1492 }
Damien Millereba71ba2000-04-29 23:57:08 +10001493 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001494}
Damien Miller62cee002000-09-23 17:15:56 +11001495
Ben Lindstrom79073822001-07-04 03:42:30 +00001496static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001497authmethods_get(void)
1498{
1499 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001500 Buffer b;
1501 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001502
Damien Miller0e3b8722002-01-22 23:26:38 +11001503 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001504 for (method = authmethods; method->name != NULL; method++) {
1505 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001506 if (buffer_len(&b) > 0)
1507 buffer_append(&b, ",", 1);
1508 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001509 }
Damien Miller62cee002000-09-23 17:15:56 +11001510 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001511 buffer_append(&b, "\0", 1);
1512 list = xstrdup(buffer_ptr(&b));
1513 buffer_free(&b);
1514 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001515}