blob: 27e5442e96876002bad473c6bb5f93e5a7e3891c [file] [log] [blame]
Damien Millera7a73ee2006-08-05 11:37:59 +10001/* $OpenBSD: sshconnect2.c,v 1.159 2006/08/01 23:22:48 stevesk Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100013 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "includes.h"
Damien Miller2eb63402006-03-15 11:09:42 +110027
Damien Miller9cf6d072006-03-15 11:29:24 +110028#include <sys/types.h>
29#include <sys/wait.h>
Damien Millerf17883e2006-03-15 11:45:54 +110030#include <sys/stat.h>
Damien Millereba71ba2000-04-29 23:57:08 +100031
Darren Tucker39972492006-07-12 22:22:46 +100032#include <errno.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100033#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100034#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100035#include <unistd.h>
Darren Tucker39972492006-07-12 22:22:46 +100036
Damien Miller9c617692003-05-14 14:31:11 +100037#include "openbsd-compat/sys-queue.h"
38
Damien Millereba71ba2000-04-29 23:57:08 +100039#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100041#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042#include "buffer.h"
43#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100044#include "compat.h"
Damien Millereba71ba2000-04-29 23:57:08 +100045#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include "cipher.h"
Damien Millereba71ba2000-04-29 23:57:08 +100047#include "kex.h"
48#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100049#include "sshconnect.h"
50#include "authfile.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000051#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100052#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000053#include "log.h"
54#include "readconf.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100055#include "misc.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000056#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000057#include "dispatch.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000058#include "canohost.h"
Ben Lindstrom1bad2562002-06-06 19:57:33 +000059#include "msg.h"
60#include "pathnames.h"
Damien Miller6b4069a2006-06-13 13:05:15 +100061#include "uidswap.h"
Damien Miller874d77b2000-10-14 16:23:11 +110062
Darren Tucker0efd1552003-08-26 11:49:55 +100063#ifdef GSSAPI
64#include "ssh-gss.h"
65#endif
66
Damien Millereba71ba2000-04-29 23:57:08 +100067/* import */
68extern char *client_version_string;
69extern char *server_version_string;
70extern Options options;
71
72/*
73 * SSH2 key exchange
74 */
75
Ben Lindstrom46c16222000-12-22 01:43:59 +000076u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100077u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100078
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000079char *xxx_host;
80struct sockaddr *xxx_hostaddr;
81
Ben Lindstromf28f6342001-04-04 02:03:04 +000082Kex *xxx_kex = NULL;
83
Ben Lindstrombba81212001-06-25 05:01:22 +000084static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000085verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000086{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000087 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +100088 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000089 return 0;
90}
91
Damien Millereba71ba2000-04-29 23:57:08 +100092void
Damien Miller874d77b2000-10-14 16:23:11 +110093ssh_kex2(char *host, struct sockaddr *hostaddr)
94{
Damien Miller874d77b2000-10-14 16:23:11 +110095 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000096
97 xxx_host = host;
98 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +110099
Damien Millere39cacc2000-11-29 12:18:44 +1100100 if (options.ciphers == (char *)-1) {
Damien Miller996acd22003-04-09 20:59:48 +1000101 logit("No valid ciphers for protocol version 2 given, using defaults.");
Damien Millere39cacc2000-11-29 12:18:44 +1100102 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100103 }
104 if (options.ciphers != NULL) {
105 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
106 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
107 }
Damien Millera0ff4662001-03-30 10:49:35 +1000108 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
109 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
110 myproposal[PROPOSAL_ENC_ALGS_STOC] =
111 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100112 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000113 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000114 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100115 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000116 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000117 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100118 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000119 if (options.macs != NULL) {
120 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
121 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
122 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000123 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100124 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000125 options.hostkeyalgorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100126
Damien Millera5539d22003-04-09 20:50:06 +1000127 if (options.rekey_limit)
128 packet_set_rekey_limit(options.rekey_limit);
129
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000130 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000131 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100132 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000133 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100134 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100135 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000136 kex->client_version_string=client_version_string;
137 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000138 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100139
Ben Lindstromf28f6342001-04-04 02:03:04 +0000140 xxx_kex = kex;
141
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000142 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100143
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000144 session_id2 = kex->session_id;
145 session_id2_len = kex->session_id_len;
146
Damien Miller874d77b2000-10-14 16:23:11 +1100147#ifdef DEBUG_KEXDH
148 /* send 1st encrypted/maced/compressed message */
149 packet_start(SSH2_MSG_IGNORE);
150 packet_put_cstring("markus");
151 packet_send();
152 packet_write_wait();
153#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000154}
Damien Millerb1715dc2000-05-30 13:44:51 +1000155
Damien Millereba71ba2000-04-29 23:57:08 +1000156/*
157 * Authenticate user
158 */
Damien Miller62cee002000-09-23 17:15:56 +1100159
160typedef struct Authctxt Authctxt;
161typedef struct Authmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000162typedef struct identity Identity;
163typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100164
Damien Miller280ecfb2003-05-14 13:46:00 +1000165struct identity {
166 TAILQ_ENTRY(identity) next;
167 AuthenticationConnection *ac; /* set if agent supports key */
168 Key *key; /* public/private key */
169 char *filename; /* comment for agent-only keys */
170 int tried;
171 int isprivate; /* key points to the private key */
172};
173TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100174
175struct Authctxt {
176 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000177 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100178 const char *host;
179 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100180 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100181 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000182 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000183 /* pubkey */
Damien Miller280ecfb2003-05-14 13:46:00 +1000184 Idlist keys;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000185 AuthenticationConnection *agent;
186 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000187 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000188 /* kbd-interactive */
189 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000190 /* generic */
191 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100192};
193struct Authmethod {
194 char *name; /* string to compare against server's list */
195 int (*userauth)(Authctxt *authctxt);
196 int *enabled; /* flag in option struct that enables method */
197 int *batch_flag; /* flag in option struct that disables method */
198};
199
Damien Miller630d6f42002-01-22 23:17:30 +1100200void input_userauth_success(int, u_int32_t, void *);
201void input_userauth_failure(int, u_int32_t, void *);
202void input_userauth_banner(int, u_int32_t, void *);
203void input_userauth_error(int, u_int32_t, void *);
204void input_userauth_info_req(int, u_int32_t, void *);
205void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000206void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100207
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000208int userauth_none(Authctxt *);
209int userauth_pubkey(Authctxt *);
210int userauth_passwd(Authctxt *);
211int userauth_kbdint(Authctxt *);
212int userauth_hostbased(Authctxt *);
Damien Miller3ab496b2003-05-14 13:47:37 +1000213int userauth_kerberos(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100214
Darren Tucker0efd1552003-08-26 11:49:55 +1000215#ifdef GSSAPI
216int userauth_gssapi(Authctxt *authctxt);
217void input_gssapi_response(int type, u_int32_t, void *);
218void input_gssapi_token(int type, u_int32_t, void *);
219void input_gssapi_hash(int type, u_int32_t, void *);
220void input_gssapi_error(int, u_int32_t, void *);
221void input_gssapi_errtok(int, u_int32_t, void *);
222#endif
223
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000224void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000225
Damien Miller280ecfb2003-05-14 13:46:00 +1000226static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000227static void pubkey_prepare(Authctxt *);
228static void pubkey_cleanup(Authctxt *);
229static Key *load_identity_file(char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000230
Ben Lindstrombba81212001-06-25 05:01:22 +0000231static Authmethod *authmethod_get(char *authlist);
232static Authmethod *authmethod_lookup(const char *name);
233static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100234
235Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000236#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100237 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000238 userauth_gssapi,
239 &options.gss_authentication,
240 NULL},
241#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000242 {"hostbased",
243 userauth_hostbased,
244 &options.hostbased_authentication,
245 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000246 {"publickey",
247 userauth_pubkey,
248 &options.pubkey_authentication,
249 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100250 {"keyboard-interactive",
251 userauth_kbdint,
252 &options.kbd_interactive_authentication,
253 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000254 {"password",
255 userauth_passwd,
256 &options.password_authentication,
257 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100258 {"none",
259 userauth_none,
260 NULL,
261 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100262 {NULL, NULL, NULL, NULL}
263};
264
265void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000266ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000267 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100268{
269 Authctxt authctxt;
270 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100271
Ben Lindstrom551ea372001-06-05 18:56:16 +0000272 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000273 options.kbd_interactive_authentication = 1;
274
Damien Miller62cee002000-09-23 17:15:56 +1100275 packet_start(SSH2_MSG_SERVICE_REQUEST);
276 packet_put_cstring("ssh-userauth");
277 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000278 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100279 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100280 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000281 if (type != SSH2_MSG_SERVICE_ACCEPT)
282 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100283 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100284 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000285 debug2("service_accept: %s", reply);
Damien Miller62cee002000-09-23 17:15:56 +1100286 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100287 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000288 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100289 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100290 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000291 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100292
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000293 if (options.preferred_authentications == NULL)
294 options.preferred_authentications = authmethods_get();
295
Damien Miller62cee002000-09-23 17:15:56 +1100296 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000297 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000298 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100299 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000300 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100301 authctxt.host = host;
302 authctxt.service = "ssh-connection"; /* service name */
303 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100304 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000305 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000306 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000307 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000308 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100309 if (authctxt.method == NULL)
310 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100311
312 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100313 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100314
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000315 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100316 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
317 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000318 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100319 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
320
Damien Miller280ecfb2003-05-14 13:46:00 +1000321 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000322 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
323
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000324 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100325}
Damien Millerf842fcb2003-05-15 12:01:28 +1000326
Damien Miller62cee002000-09-23 17:15:56 +1100327void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000328userauth(Authctxt *authctxt, char *authlist)
329{
Darren Tucker0efd1552003-08-26 11:49:55 +1000330 if (authctxt->methoddata) {
331 xfree(authctxt->methoddata);
332 authctxt->methoddata = NULL;
333 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000334 if (authlist == NULL) {
335 authlist = authctxt->authlist;
336 } else {
337 if (authctxt->authlist)
338 xfree(authctxt->authlist);
339 authctxt->authlist = authlist;
340 }
341 for (;;) {
342 Authmethod *method = authmethod_get(authlist);
343 if (method == NULL)
344 fatal("Permission denied (%s).", authlist);
345 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000346
347 /* reset the per method handler */
348 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
349 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
350
351 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000352 if (method->userauth(authctxt) != 0) {
353 debug2("we sent a %s packet, wait for reply", method->name);
354 break;
355 } else {
356 debug2("we did not send a packet, disable method");
357 method->enabled = NULL;
358 }
359 }
360}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000361
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000362void
Damien Miller630d6f42002-01-22 23:17:30 +1100363input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100364{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000365 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000366 "type %d", type);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000367}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000368
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000369void
Damien Miller630d6f42002-01-22 23:17:30 +1100370input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000371{
372 char *msg, *lang;
Darren Tucker79644822003-10-08 17:37:58 +1000373
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000374 debug3("input_userauth_banner");
375 msg = packet_get_string(NULL);
376 lang = packet_get_string(NULL);
Darren Tucker046dff22003-10-08 17:32:02 +1000377 if (options.log_level > SYSLOG_LEVEL_QUIET)
378 fprintf(stderr, "%s", msg);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000379 xfree(msg);
380 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100381}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000382
Damien Miller62cee002000-09-23 17:15:56 +1100383void
Damien Miller630d6f42002-01-22 23:17:30 +1100384input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100385{
386 Authctxt *authctxt = ctxt;
387 if (authctxt == NULL)
388 fatal("input_userauth_success: no authentication context");
Darren Tucker79644822003-10-08 17:37:58 +1000389 if (authctxt->authlist) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000390 xfree(authctxt->authlist);
Darren Tucker79644822003-10-08 17:37:58 +1000391 authctxt->authlist = NULL;
392 }
393 if (authctxt->methoddata) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000394 xfree(authctxt->methoddata);
Darren Tucker79644822003-10-08 17:37:58 +1000395 authctxt->methoddata = NULL;
396 }
Damien Miller62cee002000-09-23 17:15:56 +1100397 authctxt->success = 1; /* break out */
398}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000399
Damien Miller62cee002000-09-23 17:15:56 +1100400void
Damien Miller630d6f42002-01-22 23:17:30 +1100401input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100402{
Damien Miller62cee002000-09-23 17:15:56 +1100403 Authctxt *authctxt = ctxt;
404 char *authlist = NULL;
405 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100406
407 if (authctxt == NULL)
408 fatal("input_userauth_failure: no authentication context");
409
Damien Miller874d77b2000-10-14 16:23:11 +1100410 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100411 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100412 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100413
414 if (partial != 0)
Damien Miller996acd22003-04-09 20:59:48 +1000415 logit("Authenticated with partial success.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000416 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100417
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000418 userauth(authctxt, authlist);
419}
420void
Damien Miller630d6f42002-01-22 23:17:30 +1100421input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000422{
423 Authctxt *authctxt = ctxt;
424 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000425 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000426 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000427 int pktype, sent = 0;
428 u_int alen, blen;
429 char *pkalg, *fp;
430 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000431
432 if (authctxt == NULL)
433 fatal("input_userauth_pk_ok: no authentication context");
434 if (datafellows & SSH_BUG_PKOK) {
435 /* this is similar to SSH_BUG_PKAUTH */
436 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
437 pkblob = packet_get_string(&blen);
438 buffer_init(&b);
439 buffer_append(&b, pkblob, blen);
440 pkalg = buffer_get_string(&b, &alen);
441 buffer_free(&b);
442 } else {
443 pkalg = packet_get_string(&alen);
444 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000445 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100446 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000447
Damien Miller280ecfb2003-05-14 13:46:00 +1000448 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000449
Damien Miller280ecfb2003-05-14 13:46:00 +1000450 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
451 debug("unknown pkalg %s", pkalg);
452 goto done;
453 }
454 if ((key = key_from_blob(pkblob, blen)) == NULL) {
455 debug("no key from blob. pkalg %s", pkalg);
456 goto done;
457 }
458 if (key->type != pktype) {
459 error("input_userauth_pk_ok: type mismatch "
460 "for decoded key (received %d, expected %d)",
461 key->type, pktype);
462 goto done;
463 }
464 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
465 debug2("input_userauth_pk_ok: fp %s", fp);
466 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000467
Darren Tuckerd05b6012003-10-15 15:55:59 +1000468 /*
469 * search keys in the reverse order, because last candidate has been
470 * moved to the end of the queue. this also avoids confusion by
471 * duplicate keys
472 */
Damien Miller0b51a522004-04-20 20:07:19 +1000473 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000474 if (key_equal(key, id->key)) {
475 sent = sign_and_send_pubkey(authctxt, id);
476 break;
477 }
478 }
479done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000480 if (key != NULL)
481 key_free(key);
482 xfree(pkalg);
483 xfree(pkblob);
484
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000485 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000486 if (sent == 0)
487 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100488}
489
Darren Tucker0efd1552003-08-26 11:49:55 +1000490#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100491int
Darren Tucker0efd1552003-08-26 11:49:55 +1000492userauth_gssapi(Authctxt *authctxt)
493{
494 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100495 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000496 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000497 OM_uint32 min;
498 int ok = 0;
499
500 /* Try one GSSAPI method at a time, rather than sending them all at
501 * once. */
502
Darren Tucker56afe142003-11-03 20:06:14 +1100503 if (gss_supported == NULL)
504 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000505
506 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100507 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000508 if (gssctxt)
509 ssh_gssapi_delete_ctx(&gssctxt);
510 ssh_gssapi_build_ctx(&gssctxt);
Darren Tucker56afe142003-11-03 20:06:14 +1100511 ssh_gssapi_set_oid(gssctxt, &gss_supported->elements[mech]);
Darren Tucker0efd1552003-08-26 11:49:55 +1000512
513 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100514 if (gss_supported->elements[mech].length < 128 &&
Darren Tucker0efd1552003-08-26 11:49:55 +1000515 !GSS_ERROR(ssh_gssapi_import_name(gssctxt,
516 authctxt->host))) {
517 ok = 1; /* Mechanism works */
518 } else {
519 mech++;
520 }
521 }
522
Damien Miller5790b592006-03-26 13:54:03 +1100523 if (!ok) {
524 ssh_gssapi_delete_ctx(&gssctxt);
Damien Millereccb9de2005-06-17 12:59:34 +1000525 return 0;
Damien Miller5790b592006-03-26 13:54:03 +1100526 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000527
528 authctxt->methoddata=(void *)gssctxt;
529
530 packet_start(SSH2_MSG_USERAUTH_REQUEST);
531 packet_put_cstring(authctxt->server_user);
532 packet_put_cstring(authctxt->service);
533 packet_put_cstring(authctxt->method->name);
534
535 packet_put_int(1);
536
Darren Tucker655a5e02003-11-03 20:09:03 +1100537 packet_put_int((gss_supported->elements[mech].length) + 2);
538 packet_put_char(SSH_GSS_OIDTYPE);
539 packet_put_char(gss_supported->elements[mech].length);
540 packet_put_raw(gss_supported->elements[mech].elements,
541 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000542
543 packet_send();
544
545 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
546 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
547 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
548 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
549
550 mech++; /* Move along to next candidate */
551
552 return 1;
553}
554
Damien Miller91c6aa42003-11-17 21:20:18 +1100555static OM_uint32
556process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
557{
558 Authctxt *authctxt = ctxt;
559 Gssctxt *gssctxt = authctxt->methoddata;
560 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000561 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
562 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100563 OM_uint32 status, ms, flags;
564 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100565
Damien Miller91c6aa42003-11-17 21:20:18 +1100566 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100567 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100568
569 if (send_tok.length > 0) {
570 if (GSS_ERROR(status))
571 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
572 else
573 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100574
Damien Miller91c6aa42003-11-17 21:20:18 +1100575 packet_put_string(send_tok.value, send_tok.length);
576 packet_send();
577 gss_release_buffer(&ms, &send_tok);
578 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100579
Damien Miller91c6aa42003-11-17 21:20:18 +1100580 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100581 /* send either complete or MIC, depending on mechanism */
582 if (!(flags & GSS_C_INTEG_FLAG)) {
583 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
584 packet_send();
585 } else {
586 ssh_gssapi_buildmic(&b, authctxt->server_user,
587 authctxt->service, "gssapi-with-mic");
588
589 gssbuf.value = buffer_ptr(&b);
590 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100591
Damien Miller0425d402003-11-17 22:18:21 +1100592 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100593
Damien Miller0425d402003-11-17 22:18:21 +1100594 if (!GSS_ERROR(status)) {
595 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
596 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100597
Damien Miller0425d402003-11-17 22:18:21 +1100598 packet_send();
599 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100600
Damien Miller0425d402003-11-17 22:18:21 +1100601 buffer_free(&b);
602 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100603 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100604 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100605
Damien Miller91c6aa42003-11-17 21:20:18 +1100606 return status;
607}
608
Darren Tucker0efd1552003-08-26 11:49:55 +1000609void
610input_gssapi_response(int type, u_int32_t plen, void *ctxt)
611{
612 Authctxt *authctxt = ctxt;
613 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000614 int oidlen;
615 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000616
617 if (authctxt == NULL)
618 fatal("input_gssapi_response: no authentication context");
619 gssctxt = authctxt->methoddata;
620
621 /* Setup our OID */
622 oidv = packet_get_string(&oidlen);
623
Darren Tucker655a5e02003-11-03 20:09:03 +1100624 if (oidlen <= 2 ||
625 oidv[0] != SSH_GSS_OIDTYPE ||
626 oidv[1] != oidlen - 2) {
Damien Miller91c6aa42003-11-17 21:20:18 +1100627 xfree(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100628 debug("Badly encoded mechanism OID received");
629 userauth(authctxt, NULL);
Darren Tucker655a5e02003-11-03 20:09:03 +1100630 return;
Darren Tucker0efd1552003-08-26 11:49:55 +1000631 }
632
Darren Tucker655a5e02003-11-03 20:09:03 +1100633 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
634 fatal("Server returned different OID than expected");
635
Darren Tucker0efd1552003-08-26 11:49:55 +1000636 packet_check_eom();
637
638 xfree(oidv);
639
Damien Miller91c6aa42003-11-17 21:20:18 +1100640 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000641 /* Start again with next method on list */
642 debug("Trying to start again");
643 userauth(authctxt, NULL);
644 return;
645 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000646}
647
648void
649input_gssapi_token(int type, u_int32_t plen, void *ctxt)
650{
651 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000652 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100653 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000654 u_int slen;
655
656 if (authctxt == NULL)
657 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000658
659 recv_tok.value = packet_get_string(&slen);
660 recv_tok.length = slen; /* safe typecast */
661
662 packet_check_eom();
663
Damien Miller91c6aa42003-11-17 21:20:18 +1100664 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000665
666 xfree(recv_tok.value);
667
668 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000669 /* Start again with the next method in the list */
670 userauth(authctxt, NULL);
671 return;
672 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000673}
674
675void
676input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
677{
678 Authctxt *authctxt = ctxt;
679 Gssctxt *gssctxt;
680 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
681 gss_buffer_desc recv_tok;
682 OM_uint32 status, ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000683 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000684
685 if (authctxt == NULL)
686 fatal("input_gssapi_response: no authentication context");
687 gssctxt = authctxt->methoddata;
688
Darren Tucker600ad8d2003-08-26 12:10:48 +1000689 recv_tok.value = packet_get_string(&len);
690 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000691
692 packet_check_eom();
693
694 /* Stick it into GSSAPI and see what it says */
695 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000696 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000697
698 xfree(recv_tok.value);
699 gss_release_buffer(&ms, &send_tok);
700
701 /* Server will be returning a failed packet after this one */
702}
703
704void
705input_gssapi_error(int type, u_int32_t plen, void *ctxt)
706{
707 OM_uint32 maj, min;
708 char *msg;
709 char *lang;
710
711 maj=packet_get_int();
712 min=packet_get_int();
713 msg=packet_get_string(NULL);
714 lang=packet_get_string(NULL);
715
716 packet_check_eom();
717
Damien Miller15d72a02005-11-05 15:07:33 +1100718 debug("Server GSSAPI Error:\n%s", msg);
Darren Tucker0efd1552003-08-26 11:49:55 +1000719 xfree(msg);
720 xfree(lang);
721}
722#endif /* GSSAPI */
723
Damien Millereba71ba2000-04-29 23:57:08 +1000724int
Damien Miller874d77b2000-10-14 16:23:11 +1100725userauth_none(Authctxt *authctxt)
726{
727 /* initial userauth request */
728 packet_start(SSH2_MSG_USERAUTH_REQUEST);
729 packet_put_cstring(authctxt->server_user);
730 packet_put_cstring(authctxt->service);
731 packet_put_cstring(authctxt->method->name);
732 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100733 return 1;
734}
735
736int
Damien Miller62cee002000-09-23 17:15:56 +1100737userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000738{
Damien Millere247cc42000-05-07 12:03:14 +1000739 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000740 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000741 char *password;
742
Damien Millerd3a18572000-06-07 19:55:44 +1000743 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000744 return 0;
745
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000746 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000747 error("Permission denied, please try again.");
748
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000749 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100750 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000751 password = read_passphrase(prompt, 0);
752 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100753 packet_put_cstring(authctxt->server_user);
754 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100755 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000756 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000757 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000758 memset(password, 0, strlen(password));
759 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100760 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000761 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000762
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000763 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000764 &input_userauth_passwd_changereq);
765
Damien Millereba71ba2000-04-29 23:57:08 +1000766 return 1;
767}
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000768/*
769 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
770 */
771void
Tim Ricec8549622002-03-31 12:49:38 -0800772input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000773{
774 Authctxt *authctxt = ctxt;
775 char *info, *lang, *password = NULL, *retype = NULL;
776 char prompt[150];
777
778 debug2("input_userauth_passwd_changereq");
779
780 if (authctxt == NULL)
781 fatal("input_userauth_passwd_changereq: "
782 "no authentication context");
783
784 info = packet_get_string(NULL);
785 lang = packet_get_string(NULL);
786 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000787 logit("%s", info);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000788 xfree(info);
789 xfree(lang);
790 packet_start(SSH2_MSG_USERAUTH_REQUEST);
791 packet_put_cstring(authctxt->server_user);
792 packet_put_cstring(authctxt->service);
793 packet_put_cstring(authctxt->method->name);
794 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000795 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000796 "Enter %.30s@%.128s's old password: ",
797 authctxt->server_user, authctxt->host);
798 password = read_passphrase(prompt, 0);
799 packet_put_cstring(password);
800 memset(password, 0, strlen(password));
801 xfree(password);
802 password = NULL;
803 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000804 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000805 "Enter %.30s@%.128s's new password: ",
806 authctxt->server_user, authctxt->host);
807 password = read_passphrase(prompt, RP_ALLOW_EOF);
808 if (password == NULL) {
809 /* bail out */
810 return;
811 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000812 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000813 "Retype %.30s@%.128s's new password: ",
814 authctxt->server_user, authctxt->host);
815 retype = read_passphrase(prompt, 0);
816 if (strcmp(password, retype) != 0) {
817 memset(password, 0, strlen(password));
818 xfree(password);
Damien Miller996acd22003-04-09 20:59:48 +1000819 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000820 password = NULL;
821 }
822 memset(retype, 0, strlen(retype));
823 xfree(retype);
824 }
825 packet_put_cstring(password);
826 memset(password, 0, strlen(password));
827 xfree(password);
828 packet_add_padding(64);
829 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000830
831 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000832 &input_userauth_passwd_changereq);
833}
Damien Millereba71ba2000-04-29 23:57:08 +1000834
Ben Lindstrombba81212001-06-25 05:01:22 +0000835static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000836identity_sign(Identity *id, u_char **sigp, u_int *lenp,
837 u_char *data, u_int datalen)
838{
839 Key *prv;
840 int ret;
841
842 /* the agent supports this key */
843 if (id->ac)
844 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
845 data, datalen));
846 /*
847 * we have already loaded the private key or
848 * the private key is stored in external hardware
849 */
850 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
851 return (key_sign(id->key, sigp, lenp, data, datalen));
852 /* load the private key from the file */
853 if ((prv = load_identity_file(id->filename)) == NULL)
854 return (-1);
855 ret = key_sign(prv, sigp, lenp, data, datalen);
856 key_free(prv);
857 return (ret);
858}
859
860static int
861sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +1000862{
863 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000864 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000865 u_int bloblen, slen;
Darren Tucker502d3842003-06-28 12:38:01 +1000866 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000867 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100868 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000869
Ben Lindstromd121f612000-12-03 17:00:47 +0000870 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000871
Damien Miller280ecfb2003-05-14 13:46:00 +1000872 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100873 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000874 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100875 return 0;
876 }
Damien Millereba71ba2000-04-29 23:57:08 +1000877 /* data to be signed */
878 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100879 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000880 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000881 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100882 } else {
883 buffer_put_string(&b, session_id2, session_id2_len);
884 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000885 }
Damien Millereba71ba2000-04-29 23:57:08 +1000886 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100887 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000888 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000889 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000890 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100891 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000892 if (datafellows & SSH_BUG_PKAUTH) {
893 buffer_put_char(&b, have_sig);
894 } else {
895 buffer_put_cstring(&b, authctxt->method->name);
896 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +1000897 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +0000898 }
Damien Millereba71ba2000-04-29 23:57:08 +1000899 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000900
901 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +1000902 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000903 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000904 if (ret == -1) {
905 xfree(blob);
906 buffer_free(&b);
907 return 0;
908 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100909#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000910 buffer_dump(&b);
911#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000912 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000913 buffer_clear(&b);
914 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000915 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000916 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100917 buffer_put_cstring(&b, authctxt->server_user);
918 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100919 buffer_put_cstring(&b, authctxt->method->name);
920 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000921 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +1000922 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +1000923 buffer_put_string(&b, blob, bloblen);
924 }
925 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000926
Damien Millereba71ba2000-04-29 23:57:08 +1000927 /* append signature */
928 buffer_put_string(&b, signature, slen);
929 xfree(signature);
930
931 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000932 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100933 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000934 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000935
936 /* put remaining data from buffer into packet */
937 packet_start(SSH2_MSG_USERAUTH_REQUEST);
938 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
939 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +1000940 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000941
942 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000943}
944
Ben Lindstrombba81212001-06-25 05:01:22 +0000945static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000946send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +1000947{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000948 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000949 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000950
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000951 debug3("send_pubkey_test");
952
Damien Miller280ecfb2003-05-14 13:46:00 +1000953 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000954 /* we cannot handle this key */
955 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +1000956 return 0;
957 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000958 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000959 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +1000960
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000961 packet_start(SSH2_MSG_USERAUTH_REQUEST);
962 packet_put_cstring(authctxt->server_user);
963 packet_put_cstring(authctxt->service);
964 packet_put_cstring(authctxt->method->name);
965 packet_put_char(have_sig);
966 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +1000967 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000968 packet_put_string(blob, bloblen);
969 xfree(blob);
970 packet_send();
971 return 1;
972}
973
Ben Lindstrombba81212001-06-25 05:01:22 +0000974static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000975load_identity_file(char *filename)
976{
977 Key *private;
978 char prompt[300], *passphrase;
Darren Tucker232b76f2006-05-06 17:41:51 +1000979 int perm_ok, quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000980 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000981
Ben Lindstrom329782e2001-03-10 17:08:59 +0000982 if (stat(filename, &st) < 0) {
983 debug3("no such identity: %s", filename);
984 return NULL;
985 }
Darren Tucker232b76f2006-05-06 17:41:51 +1000986 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
987 if (!perm_ok)
988 return NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000989 if (private == NULL) {
990 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000991 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000992 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100993 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100994 for (i = 0; i < options.number_of_password_prompts; i++) {
995 passphrase = read_passphrase(prompt, 0);
996 if (strcmp(passphrase, "") != 0) {
Darren Tucker232b76f2006-05-06 17:41:51 +1000997 private = key_load_private_type(KEY_UNSPEC,
998 filename, passphrase, NULL, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000999 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001000 } else {
1001 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001002 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +11001003 }
1004 memset(passphrase, 0, strlen(passphrase));
1005 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001006 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +11001007 break;
1008 debug2("bad passphrase given, try again...");
1009 }
Damien Miller994cf142000-07-21 10:19:44 +10001010 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001011 return private;
1012}
1013
Damien Miller280ecfb2003-05-14 13:46:00 +10001014/*
1015 * try keys in the following order:
1016 * 1. agent keys that are found in the config file
1017 * 2. other agent keys
1018 * 3. keys that are only listed in the config file
1019 */
1020static void
1021pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001022{
Damien Miller280ecfb2003-05-14 13:46:00 +10001023 Identity *id;
1024 Idlist agent, files, *preferred;
1025 Key *key;
1026 AuthenticationConnection *ac;
Damien Millerad833b32000-08-23 10:46:23 +10001027 char *comment;
Damien Miller280ecfb2003-05-14 13:46:00 +10001028 int i, found;
Damien Millerad833b32000-08-23 10:46:23 +10001029
Damien Miller280ecfb2003-05-14 13:46:00 +10001030 TAILQ_INIT(&agent); /* keys from the agent */
1031 TAILQ_INIT(&files); /* keys from the config file */
1032 preferred = &authctxt->keys;
1033 TAILQ_INIT(preferred); /* preferred order of keys */
1034
1035 /* list of keys stored in the filesystem */
1036 for (i = 0; i < options.num_identity_files; i++) {
1037 key = options.identity_keys[i];
1038 if (key && key->type == KEY_RSA1)
1039 continue;
1040 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001041 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001042 id->key = key;
1043 id->filename = xstrdup(options.identity_files[i]);
1044 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001045 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001046 /* list of keys supported by the agent */
1047 if ((ac = ssh_get_authentication_connection())) {
1048 for (key = ssh_get_first_identity(ac, &comment, 2);
1049 key != NULL;
1050 key = ssh_get_next_identity(ac, &comment, 2)) {
1051 found = 0;
1052 TAILQ_FOREACH(id, &files, next) {
Damien Millera8e06ce2003-11-21 23:48:55 +11001053 /* agent keys from the config file are preferred */
Damien Miller280ecfb2003-05-14 13:46:00 +10001054 if (key_equal(key, id->key)) {
1055 key_free(key);
1056 xfree(comment);
1057 TAILQ_REMOVE(&files, id, next);
1058 TAILQ_INSERT_TAIL(preferred, id, next);
1059 id->ac = ac;
1060 found = 1;
1061 break;
1062 }
1063 }
Damien Millerbd394c32004-03-08 23:12:36 +11001064 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001065 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001066 id->key = key;
1067 id->filename = comment;
1068 id->ac = ac;
1069 TAILQ_INSERT_TAIL(&agent, id, next);
1070 }
1071 }
1072 /* append remaining agent keys */
1073 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1074 TAILQ_REMOVE(&agent, id, next);
1075 TAILQ_INSERT_TAIL(preferred, id, next);
1076 }
1077 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +11001078 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001079 /* append remaining keys from the config file */
1080 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1081 TAILQ_REMOVE(&files, id, next);
1082 TAILQ_INSERT_TAIL(preferred, id, next);
1083 }
1084 TAILQ_FOREACH(id, preferred, next) {
1085 debug2("key: %s (%p)", id->filename, id->key);
1086 }
1087}
1088
1089static void
1090pubkey_cleanup(Authctxt *authctxt)
1091{
1092 Identity *id;
1093
1094 if (authctxt->agent != NULL)
1095 ssh_close_authentication_connection(authctxt->agent);
1096 for (id = TAILQ_FIRST(&authctxt->keys); id;
1097 id = TAILQ_FIRST(&authctxt->keys)) {
1098 TAILQ_REMOVE(&authctxt->keys, id, next);
1099 if (id->key)
1100 key_free(id->key);
1101 if (id->filename)
1102 xfree(id->filename);
1103 xfree(id);
1104 }
Damien Millereba71ba2000-04-29 23:57:08 +10001105}
1106
Damien Miller62cee002000-09-23 17:15:56 +11001107int
1108userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001109{
Damien Miller280ecfb2003-05-14 13:46:00 +10001110 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001111 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001112
Damien Miller280ecfb2003-05-14 13:46:00 +10001113 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1114 if (id->tried++)
1115 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001116 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001117 TAILQ_REMOVE(&authctxt->keys, id, next);
1118 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1119 /*
1120 * send a test message if we have the public key. for
1121 * encrypted keys we cannot do this and have to load the
1122 * private key instead
1123 */
1124 if (id->key && id->key->type != KEY_RSA1) {
1125 debug("Offering public key: %s", id->filename);
1126 sent = send_pubkey_test(authctxt, id);
1127 } else if (id->key == NULL) {
1128 debug("Trying private key: %s", id->filename);
1129 id->key = load_identity_file(id->filename);
1130 if (id->key != NULL) {
1131 id->isprivate = 1;
1132 sent = sign_and_send_pubkey(authctxt, id);
1133 key_free(id->key);
1134 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001135 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001136 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001137 if (sent)
1138 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001139 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001140 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001141}
Damien Millereba71ba2000-04-29 23:57:08 +10001142
Damien Miller874d77b2000-10-14 16:23:11 +11001143/*
1144 * Send userauth request message specifying keyboard-interactive method.
1145 */
1146int
1147userauth_kbdint(Authctxt *authctxt)
1148{
1149 static int attempt = 0;
1150
1151 if (attempt++ >= options.number_of_password_prompts)
1152 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001153 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1154 if (attempt > 1 && !authctxt->info_req_seen) {
1155 debug3("userauth_kbdint: disable: no info_req_seen");
1156 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1157 return 0;
1158 }
Damien Miller874d77b2000-10-14 16:23:11 +11001159
1160 debug2("userauth_kbdint");
1161 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1162 packet_put_cstring(authctxt->server_user);
1163 packet_put_cstring(authctxt->service);
1164 packet_put_cstring(authctxt->method->name);
1165 packet_put_cstring(""); /* lang */
1166 packet_put_cstring(options.kbd_interactive_devices ?
1167 options.kbd_interactive_devices : "");
1168 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001169
1170 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1171 return 1;
1172}
1173
1174/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001175 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001176 */
1177void
Damien Miller630d6f42002-01-22 23:17:30 +11001178input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001179{
1180 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001181 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001182 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001183 int echo = 0;
1184
1185 debug2("input_userauth_info_req");
1186
1187 if (authctxt == NULL)
1188 fatal("input_userauth_info_req: no authentication context");
1189
Ben Lindstrom7d199962001-09-12 18:29:00 +00001190 authctxt->info_req_seen = 1;
1191
Damien Miller874d77b2000-10-14 16:23:11 +11001192 name = packet_get_string(NULL);
1193 inst = packet_get_string(NULL);
1194 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001195 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001196 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001197 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001198 logit("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001199 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001200 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001201 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001202
1203 num_prompts = packet_get_int();
1204 /*
1205 * Begin to build info response packet based on prompts requested.
1206 * We commit to providing the correct number of responses, so if
1207 * further on we run into a problem that prevents this, we have to
1208 * be sure and clean this up and send a correct error response.
1209 */
1210 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1211 packet_put_int(num_prompts);
1212
Ben Lindstrom551ea372001-06-05 18:56:16 +00001213 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001214 for (i = 0; i < num_prompts; i++) {
1215 prompt = packet_get_string(NULL);
1216 echo = packet_get_char();
1217
Ben Lindstrom949974b2001-06-25 05:20:31 +00001218 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001219
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001220 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +11001221 memset(response, 0, strlen(response));
1222 xfree(response);
1223 xfree(prompt);
1224 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001225 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001226
Damien Miller9f643902001-11-12 11:02:52 +11001227 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001228 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001229}
Damien Miller62cee002000-09-23 17:15:56 +11001230
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001231static int
Ben Lindstrom5c385522002-06-23 21:23:20 +00001232ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001233 u_char *data, u_int datalen)
1234{
1235 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001236 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001237 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001238 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001239
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001240 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001241
Ben Lindstrom5206b952002-06-06 19:59:29 +00001242 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1243 error("ssh_keysign: no installed: %s", strerror(errno));
1244 return -1;
1245 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001246 if (fflush(stdout) != 0)
1247 error("ssh_keysign: fflush: %s", strerror(errno));
1248 if (pipe(to) < 0) {
1249 error("ssh_keysign: pipe: %s", strerror(errno));
1250 return -1;
1251 }
1252 if (pipe(from) < 0) {
1253 error("ssh_keysign: pipe: %s", strerror(errno));
1254 return -1;
1255 }
1256 if ((pid = fork()) < 0) {
1257 error("ssh_keysign: fork: %s", strerror(errno));
1258 return -1;
1259 }
1260 if (pid == 0) {
Damien Miller2e5fe882006-06-13 13:10:00 +10001261 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001262 close(from[0]);
1263 if (dup2(from[1], STDOUT_FILENO) < 0)
1264 fatal("ssh_keysign: dup2: %s", strerror(errno));
1265 close(to[1]);
1266 if (dup2(to[0], STDIN_FILENO) < 0)
1267 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001268 close(from[1]);
1269 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001270 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001271 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1272 strerror(errno));
1273 }
1274 close(from[1]);
1275 close(to[0]);
1276
1277 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001278 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001279 buffer_put_string(&b, data, datalen);
Damien Miller51bf11f2003-11-17 21:20:47 +11001280 if (ssh_msg_send(to[1], version, &b) == -1)
1281 fatal("ssh_keysign: couldn't send request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001282
Damien Miller901119b2002-10-04 11:10:04 +10001283 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001284 error("ssh_keysign: no reply");
Damien Millerfb1310e2004-01-21 11:02:50 +11001285 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001286 return -1;
1287 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001288 close(from[0]);
1289 close(to[1]);
1290
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001291 while (waitpid(pid, &status, 0) < 0)
1292 if (errno != EINTR)
1293 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001294
Ben Lindstrom5206b952002-06-06 19:59:29 +00001295 if (buffer_get_char(&b) != version) {
1296 error("ssh_keysign: bad version");
Damien Millerfb1310e2004-01-21 11:02:50 +11001297 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001298 return -1;
1299 }
1300 *sigp = buffer_get_string(&b, lenp);
Damien Millerfb1310e2004-01-21 11:02:50 +11001301 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001302
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001303 return 0;
1304}
1305
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001306int
1307userauth_hostbased(Authctxt *authctxt)
1308{
1309 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001310 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001311 Buffer b;
1312 u_char *signature, *blob;
1313 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001314 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001315 u_int blen, slen;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +00001316 int ok, i, len, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001317
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001318 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001319 for (i = 0; i < sensitive->nkeys; i++) {
1320 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001321 if (private && private->type != KEY_RSA1) {
1322 found = 1;
1323 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001324 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001325 break;
1326 }
1327 }
1328 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001329 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001330 return 0;
1331 }
1332 if (key_to_blob(private, &blob, &blen) == 0) {
1333 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001334 return 0;
1335 }
Damien Miller91c18472001-11-12 11:02:03 +11001336 /* figure out a name for the client host */
1337 p = get_local_name(packet_get_connection_in());
1338 if (p == NULL) {
1339 error("userauth_hostbased: cannot get local ipaddr/name");
1340 key_free(private);
Damien Miller5790b592006-03-26 13:54:03 +11001341 xfree(blob);
Damien Miller91c18472001-11-12 11:02:03 +11001342 return 0;
1343 }
1344 len = strlen(p) + 2;
Damien Miller07d86be2006-03-26 14:19:21 +11001345 xasprintf(&chost, "%s.", p);
Damien Miller91c18472001-11-12 11:02:03 +11001346 debug2("userauth_hostbased: chost %s", chost);
Damien Miller00111382003-03-10 11:21:17 +11001347 xfree(p);
Damien Miller91c18472001-11-12 11:02:03 +11001348
Ben Lindstrom671388f2001-04-19 20:40:45 +00001349 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1350 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001351 pkalg = xstrdup(key_ssh_name(private));
1352 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001353 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001354 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001355 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1356 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001357 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001358 buffer_put_cstring(&b, authctxt->method->name);
1359 buffer_put_cstring(&b, pkalg);
1360 buffer_put_string(&b, blob, blen);
1361 buffer_put_cstring(&b, chost);
1362 buffer_put_cstring(&b, authctxt->local_user);
1363#ifdef DEBUG_PK
1364 buffer_dump(&b);
1365#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001366 if (sensitive->external_keysign)
1367 ok = ssh_keysign(private, &signature, &slen,
1368 buffer_ptr(&b), buffer_len(&b));
1369 else
1370 ok = key_sign(private, &signature, &slen,
1371 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001372 key_free(private);
1373 buffer_free(&b);
1374 if (ok != 0) {
1375 error("key_sign failed");
1376 xfree(chost);
1377 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001378 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001379 return 0;
1380 }
1381 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1382 packet_put_cstring(authctxt->server_user);
1383 packet_put_cstring(authctxt->service);
1384 packet_put_cstring(authctxt->method->name);
1385 packet_put_cstring(pkalg);
1386 packet_put_string(blob, blen);
1387 packet_put_cstring(chost);
1388 packet_put_cstring(authctxt->local_user);
1389 packet_put_string(signature, slen);
1390 memset(signature, 's', slen);
1391 xfree(signature);
1392 xfree(chost);
1393 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001394 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001395
1396 packet_send();
1397 return 1;
1398}
1399
Damien Miller62cee002000-09-23 17:15:56 +11001400/* find auth method */
1401
Damien Miller62cee002000-09-23 17:15:56 +11001402/*
1403 * given auth method name, if configurable options permit this method fill
1404 * in auth_ident field and return true, otherwise return false.
1405 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001406static int
Damien Miller62cee002000-09-23 17:15:56 +11001407authmethod_is_enabled(Authmethod *method)
1408{
1409 if (method == NULL)
1410 return 0;
1411 /* return false if options indicate this method is disabled */
1412 if (method->enabled == NULL || *method->enabled == 0)
1413 return 0;
1414 /* return false if batch mode is enabled but method needs interactive mode */
1415 if (method->batch_flag != NULL && *method->batch_flag != 0)
1416 return 0;
1417 return 1;
1418}
1419
Ben Lindstrombba81212001-06-25 05:01:22 +00001420static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001421authmethod_lookup(const char *name)
1422{
1423 Authmethod *method = NULL;
1424 if (name != NULL)
1425 for (method = authmethods; method->name != NULL; method++)
1426 if (strcmp(name, method->name) == 0)
1427 return method;
1428 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1429 return NULL;
1430}
1431
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001432/* XXX internal state */
1433static Authmethod *current = NULL;
1434static char *supported = NULL;
1435static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001436
Damien Miller62cee002000-09-23 17:15:56 +11001437/*
1438 * Given the authentication method list sent by the server, return the
1439 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001440 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001441 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001442static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001443authmethod_get(char *authlist)
1444{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001445 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001446 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001447
Damien Miller62cee002000-09-23 17:15:56 +11001448 /* Use a suitable default if we're passed a nil list. */
1449 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001450 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001451
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001452 if (supported == NULL || strcmp(authlist, supported) != 0) {
1453 debug3("start over, passed a different list %s", authlist);
1454 if (supported != NULL)
1455 xfree(supported);
1456 supported = xstrdup(authlist);
1457 preferred = options.preferred_authentications;
1458 debug3("preferred %s", preferred);
1459 current = NULL;
1460 } else if (current != NULL && authmethod_is_enabled(current))
1461 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001462
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001463 for (;;) {
1464 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001465 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001466 current = NULL;
1467 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001468 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001469 preferred += next;
1470 debug3("authmethod_lookup %s", name);
1471 debug3("remaining preferred: %s", preferred);
1472 if ((current = authmethod_lookup(name)) != NULL &&
1473 authmethod_is_enabled(current)) {
1474 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001475 debug("Next authentication method: %s", name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001476 return current;
1477 }
Damien Millereba71ba2000-04-29 23:57:08 +10001478 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001479}
Damien Miller62cee002000-09-23 17:15:56 +11001480
Ben Lindstrom79073822001-07-04 03:42:30 +00001481static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001482authmethods_get(void)
1483{
1484 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001485 Buffer b;
1486 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001487
Damien Miller0e3b8722002-01-22 23:26:38 +11001488 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001489 for (method = authmethods; method->name != NULL; method++) {
1490 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001491 if (buffer_len(&b) > 0)
1492 buffer_append(&b, ",", 1);
1493 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001494 }
Damien Miller62cee002000-09-23 17:15:56 +11001495 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001496 buffer_append(&b, "\0", 1);
1497 list = xstrdup(buffer_ptr(&b));
1498 buffer_free(&b);
1499 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001500}