blob: 208df078cde3ac5c5e35d439748e300083ad5b36 [file] [log] [blame]
Darren Tuckerf520ea12007-05-20 15:11:33 +10001/* $OpenBSD: sshconnect2.c,v 1.164 2007/05/17 23:53:41 jolan 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>
Darren Tucker39972492006-07-12 22:22:46 +100041
Damien Miller9c617692003-05-14 14:31:11 +100042#include "openbsd-compat/sys-queue.h"
43
Damien Millerd7834352006-08-05 12:39:39 +100044#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100045#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100047#include "buffer.h"
48#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100049#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000050#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100051#include "key.h"
Damien Millereba71ba2000-04-29 23:57:08 +100052#include "kex.h"
53#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100054#include "sshconnect.h"
55#include "authfile.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000056#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100057#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058#include "log.h"
59#include "readconf.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100060#include "misc.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000061#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000062#include "dispatch.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000063#include "canohost.h"
Ben Lindstrom1bad2562002-06-06 19:57:33 +000064#include "msg.h"
65#include "pathnames.h"
Damien Miller6b4069a2006-06-13 13:05:15 +100066#include "uidswap.h"
Damien Miller874d77b2000-10-14 16:23:11 +110067
Darren Tucker0efd1552003-08-26 11:49:55 +100068#ifdef GSSAPI
69#include "ssh-gss.h"
70#endif
71
Damien Millereba71ba2000-04-29 23:57:08 +100072/* import */
73extern char *client_version_string;
74extern char *server_version_string;
75extern Options options;
76
77/*
78 * SSH2 key exchange
79 */
80
Ben Lindstrom46c16222000-12-22 01:43:59 +000081u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100082u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100083
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000084char *xxx_host;
85struct sockaddr *xxx_hostaddr;
86
Ben Lindstromf28f6342001-04-04 02:03:04 +000087Kex *xxx_kex = NULL;
88
Ben Lindstrombba81212001-06-25 05:01:22 +000089static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000090verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000091{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000092 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +100093 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000094 return 0;
95}
96
Damien Millereba71ba2000-04-29 23:57:08 +100097void
Damien Miller874d77b2000-10-14 16:23:11 +110098ssh_kex2(char *host, struct sockaddr *hostaddr)
99{
Damien Miller874d77b2000-10-14 16:23:11 +1100100 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000101
102 xxx_host = host;
103 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100104
Damien Millere39cacc2000-11-29 12:18:44 +1100105 if (options.ciphers == (char *)-1) {
Damien Miller996acd22003-04-09 20:59:48 +1000106 logit("No valid ciphers for protocol version 2 given, using defaults.");
Damien Millere39cacc2000-11-29 12:18:44 +1100107 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100108 }
109 if (options.ciphers != NULL) {
110 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
111 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
112 }
Damien Millera0ff4662001-03-30 10:49:35 +1000113 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
114 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
115 myproposal[PROPOSAL_ENC_ALGS_STOC] =
116 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100117 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000118 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000119 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100120 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000121 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000122 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100123 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000124 if (options.macs != NULL) {
125 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
126 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
127 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000128 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100129 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000130 options.hostkeyalgorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100131
Damien Millera5539d22003-04-09 20:50:06 +1000132 if (options.rekey_limit)
133 packet_set_rekey_limit(options.rekey_limit);
134
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000135 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000136 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100137 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000138 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100139 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100140 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000141 kex->client_version_string=client_version_string;
142 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000143 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100144
Ben Lindstromf28f6342001-04-04 02:03:04 +0000145 xxx_kex = kex;
146
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000147 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100148
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000149 session_id2 = kex->session_id;
150 session_id2_len = kex->session_id_len;
151
Damien Miller874d77b2000-10-14 16:23:11 +1100152#ifdef DEBUG_KEXDH
153 /* send 1st encrypted/maced/compressed message */
154 packet_start(SSH2_MSG_IGNORE);
155 packet_put_cstring("markus");
156 packet_send();
157 packet_write_wait();
158#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000159}
Damien Millerb1715dc2000-05-30 13:44:51 +1000160
Damien Millereba71ba2000-04-29 23:57:08 +1000161/*
162 * Authenticate user
163 */
Damien Miller62cee002000-09-23 17:15:56 +1100164
165typedef struct Authctxt Authctxt;
166typedef struct Authmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000167typedef struct identity Identity;
168typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100169
Damien Miller280ecfb2003-05-14 13:46:00 +1000170struct identity {
171 TAILQ_ENTRY(identity) next;
172 AuthenticationConnection *ac; /* set if agent supports key */
173 Key *key; /* public/private key */
174 char *filename; /* comment for agent-only keys */
175 int tried;
176 int isprivate; /* key points to the private key */
177};
178TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100179
180struct Authctxt {
181 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000182 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100183 const char *host;
184 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100185 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100186 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000187 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000188 /* pubkey */
Damien Miller280ecfb2003-05-14 13:46:00 +1000189 Idlist keys;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000190 AuthenticationConnection *agent;
191 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000192 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000193 /* kbd-interactive */
194 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000195 /* generic */
196 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100197};
198struct Authmethod {
199 char *name; /* string to compare against server's list */
200 int (*userauth)(Authctxt *authctxt);
201 int *enabled; /* flag in option struct that enables method */
202 int *batch_flag; /* flag in option struct that disables method */
203};
204
Damien Miller630d6f42002-01-22 23:17:30 +1100205void input_userauth_success(int, u_int32_t, void *);
206void input_userauth_failure(int, u_int32_t, void *);
207void input_userauth_banner(int, u_int32_t, void *);
208void input_userauth_error(int, u_int32_t, void *);
209void input_userauth_info_req(int, u_int32_t, void *);
210void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000211void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100212
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000213int userauth_none(Authctxt *);
214int userauth_pubkey(Authctxt *);
215int userauth_passwd(Authctxt *);
216int userauth_kbdint(Authctxt *);
217int userauth_hostbased(Authctxt *);
Damien Miller3ab496b2003-05-14 13:47:37 +1000218int userauth_kerberos(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100219
Darren Tucker0efd1552003-08-26 11:49:55 +1000220#ifdef GSSAPI
221int userauth_gssapi(Authctxt *authctxt);
222void input_gssapi_response(int type, u_int32_t, void *);
223void input_gssapi_token(int type, u_int32_t, void *);
224void input_gssapi_hash(int type, u_int32_t, void *);
225void input_gssapi_error(int, u_int32_t, void *);
226void input_gssapi_errtok(int, u_int32_t, void *);
227#endif
228
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000229void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000230
Damien Miller280ecfb2003-05-14 13:46:00 +1000231static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000232static void pubkey_prepare(Authctxt *);
233static void pubkey_cleanup(Authctxt *);
234static Key *load_identity_file(char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000235
Ben Lindstrombba81212001-06-25 05:01:22 +0000236static Authmethod *authmethod_get(char *authlist);
237static Authmethod *authmethod_lookup(const char *name);
238static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100239
240Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000241#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100242 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000243 userauth_gssapi,
244 &options.gss_authentication,
245 NULL},
246#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000247 {"hostbased",
248 userauth_hostbased,
249 &options.hostbased_authentication,
250 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000251 {"publickey",
252 userauth_pubkey,
253 &options.pubkey_authentication,
254 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100255 {"keyboard-interactive",
256 userauth_kbdint,
257 &options.kbd_interactive_authentication,
258 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000259 {"password",
260 userauth_passwd,
261 &options.password_authentication,
262 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100263 {"none",
264 userauth_none,
265 NULL,
266 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100267 {NULL, NULL, NULL, NULL}
268};
269
270void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000271ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000272 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100273{
274 Authctxt authctxt;
275 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100276
Ben Lindstrom551ea372001-06-05 18:56:16 +0000277 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000278 options.kbd_interactive_authentication = 1;
279
Damien Miller62cee002000-09-23 17:15:56 +1100280 packet_start(SSH2_MSG_SERVICE_REQUEST);
281 packet_put_cstring("ssh-userauth");
282 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000283 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100284 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100285 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000286 if (type != SSH2_MSG_SERVICE_ACCEPT)
287 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100288 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100289 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000290 debug2("service_accept: %s", reply);
Damien Miller62cee002000-09-23 17:15:56 +1100291 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100292 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000293 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100294 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100295 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000296 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100297
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000298 if (options.preferred_authentications == NULL)
299 options.preferred_authentications = authmethods_get();
300
Damien Miller62cee002000-09-23 17:15:56 +1100301 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000302 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000303 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100304 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000305 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100306 authctxt.host = host;
307 authctxt.service = "ssh-connection"; /* service name */
308 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100309 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000310 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000311 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000312 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000313 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100314 if (authctxt.method == NULL)
315 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100316
317 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100318 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100319
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000320 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100321 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
322 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000323 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100324 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
325
Damien Miller280ecfb2003-05-14 13:46:00 +1000326 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000327 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
328
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000329 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100330}
Damien Millerf842fcb2003-05-15 12:01:28 +1000331
Damien Miller62cee002000-09-23 17:15:56 +1100332void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000333userauth(Authctxt *authctxt, char *authlist)
334{
Darren Tucker0efd1552003-08-26 11:49:55 +1000335 if (authctxt->methoddata) {
336 xfree(authctxt->methoddata);
337 authctxt->methoddata = NULL;
338 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000339 if (authlist == NULL) {
340 authlist = authctxt->authlist;
341 } else {
342 if (authctxt->authlist)
343 xfree(authctxt->authlist);
344 authctxt->authlist = authlist;
345 }
346 for (;;) {
347 Authmethod *method = authmethod_get(authlist);
348 if (method == NULL)
349 fatal("Permission denied (%s).", authlist);
350 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000351
352 /* reset the per method handler */
353 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
354 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
355
356 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000357 if (method->userauth(authctxt) != 0) {
358 debug2("we sent a %s packet, wait for reply", method->name);
359 break;
360 } else {
361 debug2("we did not send a packet, disable method");
362 method->enabled = NULL;
363 }
364 }
365}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000366
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000367void
Damien Miller630d6f42002-01-22 23:17:30 +1100368input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100369{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000370 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000371 "type %d", type);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000372}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000373
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000374void
Damien Miller630d6f42002-01-22 23:17:30 +1100375input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000376{
377 char *msg, *lang;
Darren Tucker79644822003-10-08 17:37:58 +1000378
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000379 debug3("input_userauth_banner");
380 msg = packet_get_string(NULL);
381 lang = packet_get_string(NULL);
Damien Miller21258872006-08-30 11:08:33 +1000382 if (options.log_level >= SYSLOG_LEVEL_INFO)
Darren Tucker046dff22003-10-08 17:32:02 +1000383 fprintf(stderr, "%s", msg);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000384 xfree(msg);
385 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100386}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000387
Damien Miller62cee002000-09-23 17:15:56 +1100388void
Damien Miller630d6f42002-01-22 23:17:30 +1100389input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100390{
391 Authctxt *authctxt = ctxt;
392 if (authctxt == NULL)
393 fatal("input_userauth_success: no authentication context");
Darren Tucker79644822003-10-08 17:37:58 +1000394 if (authctxt->authlist) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000395 xfree(authctxt->authlist);
Darren Tucker79644822003-10-08 17:37:58 +1000396 authctxt->authlist = NULL;
397 }
398 if (authctxt->methoddata) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000399 xfree(authctxt->methoddata);
Darren Tucker79644822003-10-08 17:37:58 +1000400 authctxt->methoddata = NULL;
401 }
Damien Miller62cee002000-09-23 17:15:56 +1100402 authctxt->success = 1; /* break out */
403}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000404
Damien Miller62cee002000-09-23 17:15:56 +1100405void
Damien Miller630d6f42002-01-22 23:17:30 +1100406input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100407{
Damien Miller62cee002000-09-23 17:15:56 +1100408 Authctxt *authctxt = ctxt;
409 char *authlist = NULL;
410 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100411
412 if (authctxt == NULL)
413 fatal("input_userauth_failure: no authentication context");
414
Damien Miller874d77b2000-10-14 16:23:11 +1100415 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100416 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100417 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100418
419 if (partial != 0)
Damien Miller996acd22003-04-09 20:59:48 +1000420 logit("Authenticated with partial success.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000421 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100422
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000423 userauth(authctxt, authlist);
424}
425void
Damien Miller630d6f42002-01-22 23:17:30 +1100426input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000427{
428 Authctxt *authctxt = ctxt;
429 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000430 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000431 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000432 int pktype, sent = 0;
433 u_int alen, blen;
434 char *pkalg, *fp;
435 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000436
437 if (authctxt == NULL)
438 fatal("input_userauth_pk_ok: no authentication context");
439 if (datafellows & SSH_BUG_PKOK) {
440 /* this is similar to SSH_BUG_PKAUTH */
441 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
442 pkblob = packet_get_string(&blen);
443 buffer_init(&b);
444 buffer_append(&b, pkblob, blen);
445 pkalg = buffer_get_string(&b, &alen);
446 buffer_free(&b);
447 } else {
448 pkalg = packet_get_string(&alen);
449 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000450 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100451 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000452
Damien Miller280ecfb2003-05-14 13:46:00 +1000453 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000454
Damien Miller280ecfb2003-05-14 13:46:00 +1000455 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
456 debug("unknown pkalg %s", pkalg);
457 goto done;
458 }
459 if ((key = key_from_blob(pkblob, blen)) == NULL) {
460 debug("no key from blob. pkalg %s", pkalg);
461 goto done;
462 }
463 if (key->type != pktype) {
464 error("input_userauth_pk_ok: type mismatch "
465 "for decoded key (received %d, expected %d)",
466 key->type, pktype);
467 goto done;
468 }
469 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
470 debug2("input_userauth_pk_ok: fp %s", fp);
471 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000472
Darren Tuckerd05b6012003-10-15 15:55:59 +1000473 /*
474 * search keys in the reverse order, because last candidate has been
475 * moved to the end of the queue. this also avoids confusion by
476 * duplicate keys
477 */
Damien Miller0b51a522004-04-20 20:07:19 +1000478 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000479 if (key_equal(key, id->key)) {
480 sent = sign_and_send_pubkey(authctxt, id);
481 break;
482 }
483 }
484done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000485 if (key != NULL)
486 key_free(key);
487 xfree(pkalg);
488 xfree(pkblob);
489
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000490 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000491 if (sent == 0)
492 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100493}
494
Darren Tucker0efd1552003-08-26 11:49:55 +1000495#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100496int
Darren Tucker0efd1552003-08-26 11:49:55 +1000497userauth_gssapi(Authctxt *authctxt)
498{
499 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100500 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000501 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000502 OM_uint32 min;
503 int ok = 0;
504
505 /* Try one GSSAPI method at a time, rather than sending them all at
506 * once. */
507
Darren Tucker56afe142003-11-03 20:06:14 +1100508 if (gss_supported == NULL)
509 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000510
511 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100512 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000513 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100514 if (gss_supported->elements[mech].length < 128 &&
Damien Millera1cb9f32006-08-19 00:33:34 +1000515 ssh_gssapi_check_mechanism(&gssctxt,
516 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000517 ok = 1; /* Mechanism works */
518 } else {
519 mech++;
520 }
521 }
522
Damien Millera1cb9f32006-08-19 00:33:34 +1000523 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000524 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000525
526 authctxt->methoddata=(void *)gssctxt;
527
528 packet_start(SSH2_MSG_USERAUTH_REQUEST);
529 packet_put_cstring(authctxt->server_user);
530 packet_put_cstring(authctxt->service);
531 packet_put_cstring(authctxt->method->name);
532
533 packet_put_int(1);
534
Darren Tucker655a5e02003-11-03 20:09:03 +1100535 packet_put_int((gss_supported->elements[mech].length) + 2);
536 packet_put_char(SSH_GSS_OIDTYPE);
537 packet_put_char(gss_supported->elements[mech].length);
538 packet_put_raw(gss_supported->elements[mech].elements,
539 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000540
541 packet_send();
542
543 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
544 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
545 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
546 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
547
548 mech++; /* Move along to next candidate */
549
550 return 1;
551}
552
Damien Miller91c6aa42003-11-17 21:20:18 +1100553static OM_uint32
554process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
555{
556 Authctxt *authctxt = ctxt;
557 Gssctxt *gssctxt = authctxt->methoddata;
558 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000559 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
560 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100561 OM_uint32 status, ms, flags;
562 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100563
Damien Miller91c6aa42003-11-17 21:20:18 +1100564 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100565 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100566
567 if (send_tok.length > 0) {
568 if (GSS_ERROR(status))
569 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
570 else
571 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100572
Damien Miller91c6aa42003-11-17 21:20:18 +1100573 packet_put_string(send_tok.value, send_tok.length);
574 packet_send();
575 gss_release_buffer(&ms, &send_tok);
576 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100577
Damien Miller91c6aa42003-11-17 21:20:18 +1100578 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100579 /* send either complete or MIC, depending on mechanism */
580 if (!(flags & GSS_C_INTEG_FLAG)) {
581 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
582 packet_send();
583 } else {
584 ssh_gssapi_buildmic(&b, authctxt->server_user,
585 authctxt->service, "gssapi-with-mic");
586
587 gssbuf.value = buffer_ptr(&b);
588 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100589
Damien Miller0425d402003-11-17 22:18:21 +1100590 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100591
Damien Miller0425d402003-11-17 22:18:21 +1100592 if (!GSS_ERROR(status)) {
593 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
594 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100595
Damien Miller0425d402003-11-17 22:18:21 +1100596 packet_send();
597 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100598
Damien Miller0425d402003-11-17 22:18:21 +1100599 buffer_free(&b);
600 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100601 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100602 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100603
Damien Miller91c6aa42003-11-17 21:20:18 +1100604 return status;
605}
606
Darren Tucker0efd1552003-08-26 11:49:55 +1000607void
608input_gssapi_response(int type, u_int32_t plen, void *ctxt)
609{
610 Authctxt *authctxt = ctxt;
611 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000612 int oidlen;
613 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000614
615 if (authctxt == NULL)
616 fatal("input_gssapi_response: no authentication context");
617 gssctxt = authctxt->methoddata;
618
619 /* Setup our OID */
620 oidv = packet_get_string(&oidlen);
621
Darren Tucker655a5e02003-11-03 20:09:03 +1100622 if (oidlen <= 2 ||
623 oidv[0] != SSH_GSS_OIDTYPE ||
624 oidv[1] != oidlen - 2) {
Damien Miller91c6aa42003-11-17 21:20:18 +1100625 xfree(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100626 debug("Badly encoded mechanism OID received");
627 userauth(authctxt, NULL);
Darren Tucker655a5e02003-11-03 20:09:03 +1100628 return;
Darren Tucker0efd1552003-08-26 11:49:55 +1000629 }
630
Darren Tucker655a5e02003-11-03 20:09:03 +1100631 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
632 fatal("Server returned different OID than expected");
633
Darren Tucker0efd1552003-08-26 11:49:55 +1000634 packet_check_eom();
635
636 xfree(oidv);
637
Damien Miller91c6aa42003-11-17 21:20:18 +1100638 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000639 /* Start again with next method on list */
640 debug("Trying to start again");
641 userauth(authctxt, NULL);
642 return;
643 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000644}
645
646void
647input_gssapi_token(int type, u_int32_t plen, void *ctxt)
648{
649 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000650 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100651 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000652 u_int slen;
653
654 if (authctxt == NULL)
655 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000656
657 recv_tok.value = packet_get_string(&slen);
658 recv_tok.length = slen; /* safe typecast */
659
660 packet_check_eom();
661
Damien Miller91c6aa42003-11-17 21:20:18 +1100662 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000663
664 xfree(recv_tok.value);
665
666 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000667 /* Start again with the next method in the list */
668 userauth(authctxt, NULL);
669 return;
670 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000671}
672
673void
674input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
675{
676 Authctxt *authctxt = ctxt;
677 Gssctxt *gssctxt;
678 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
679 gss_buffer_desc recv_tok;
680 OM_uint32 status, ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000681 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000682
683 if (authctxt == NULL)
684 fatal("input_gssapi_response: no authentication context");
685 gssctxt = authctxt->methoddata;
686
Darren Tucker600ad8d2003-08-26 12:10:48 +1000687 recv_tok.value = packet_get_string(&len);
688 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000689
690 packet_check_eom();
691
692 /* Stick it into GSSAPI and see what it says */
693 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000694 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000695
696 xfree(recv_tok.value);
697 gss_release_buffer(&ms, &send_tok);
698
699 /* Server will be returning a failed packet after this one */
700}
701
702void
703input_gssapi_error(int type, u_int32_t plen, void *ctxt)
704{
705 OM_uint32 maj, min;
706 char *msg;
707 char *lang;
708
709 maj=packet_get_int();
710 min=packet_get_int();
711 msg=packet_get_string(NULL);
712 lang=packet_get_string(NULL);
713
714 packet_check_eom();
715
Damien Miller15d72a02005-11-05 15:07:33 +1100716 debug("Server GSSAPI Error:\n%s", msg);
Darren Tucker0efd1552003-08-26 11:49:55 +1000717 xfree(msg);
718 xfree(lang);
719}
720#endif /* GSSAPI */
721
Damien Millereba71ba2000-04-29 23:57:08 +1000722int
Damien Miller874d77b2000-10-14 16:23:11 +1100723userauth_none(Authctxt *authctxt)
724{
725 /* initial userauth request */
726 packet_start(SSH2_MSG_USERAUTH_REQUEST);
727 packet_put_cstring(authctxt->server_user);
728 packet_put_cstring(authctxt->service);
729 packet_put_cstring(authctxt->method->name);
730 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100731 return 1;
732}
733
734int
Damien Miller62cee002000-09-23 17:15:56 +1100735userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000736{
Damien Millere247cc42000-05-07 12:03:14 +1000737 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000738 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000739 char *password;
740
Damien Millerd3a18572000-06-07 19:55:44 +1000741 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000742 return 0;
743
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000744 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000745 error("Permission denied, please try again.");
746
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000747 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100748 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000749 password = read_passphrase(prompt, 0);
750 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100751 packet_put_cstring(authctxt->server_user);
752 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100753 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000754 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000755 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000756 memset(password, 0, strlen(password));
757 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100758 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000759 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000760
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000761 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000762 &input_userauth_passwd_changereq);
763
Damien Millereba71ba2000-04-29 23:57:08 +1000764 return 1;
765}
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000766/*
767 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
768 */
769void
Tim Ricec8549622002-03-31 12:49:38 -0800770input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000771{
772 Authctxt *authctxt = ctxt;
773 char *info, *lang, *password = NULL, *retype = NULL;
774 char prompt[150];
775
776 debug2("input_userauth_passwd_changereq");
777
778 if (authctxt == NULL)
779 fatal("input_userauth_passwd_changereq: "
780 "no authentication context");
781
782 info = packet_get_string(NULL);
783 lang = packet_get_string(NULL);
784 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000785 logit("%s", info);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000786 xfree(info);
787 xfree(lang);
788 packet_start(SSH2_MSG_USERAUTH_REQUEST);
789 packet_put_cstring(authctxt->server_user);
790 packet_put_cstring(authctxt->service);
791 packet_put_cstring(authctxt->method->name);
792 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000793 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000794 "Enter %.30s@%.128s's old password: ",
795 authctxt->server_user, authctxt->host);
796 password = read_passphrase(prompt, 0);
797 packet_put_cstring(password);
798 memset(password, 0, strlen(password));
799 xfree(password);
800 password = NULL;
801 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000802 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000803 "Enter %.30s@%.128s's new password: ",
804 authctxt->server_user, authctxt->host);
805 password = read_passphrase(prompt, RP_ALLOW_EOF);
806 if (password == NULL) {
807 /* bail out */
808 return;
809 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000810 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000811 "Retype %.30s@%.128s's new password: ",
812 authctxt->server_user, authctxt->host);
813 retype = read_passphrase(prompt, 0);
814 if (strcmp(password, retype) != 0) {
815 memset(password, 0, strlen(password));
816 xfree(password);
Damien Miller996acd22003-04-09 20:59:48 +1000817 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000818 password = NULL;
819 }
820 memset(retype, 0, strlen(retype));
821 xfree(retype);
822 }
823 packet_put_cstring(password);
824 memset(password, 0, strlen(password));
825 xfree(password);
826 packet_add_padding(64);
827 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000828
829 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000830 &input_userauth_passwd_changereq);
831}
Damien Millereba71ba2000-04-29 23:57:08 +1000832
Ben Lindstrombba81212001-06-25 05:01:22 +0000833static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000834identity_sign(Identity *id, u_char **sigp, u_int *lenp,
835 u_char *data, u_int datalen)
836{
837 Key *prv;
838 int ret;
839
840 /* the agent supports this key */
841 if (id->ac)
842 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
843 data, datalen));
844 /*
845 * we have already loaded the private key or
846 * the private key is stored in external hardware
847 */
848 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
849 return (key_sign(id->key, sigp, lenp, data, datalen));
850 /* load the private key from the file */
851 if ((prv = load_identity_file(id->filename)) == NULL)
852 return (-1);
853 ret = key_sign(prv, sigp, lenp, data, datalen);
854 key_free(prv);
855 return (ret);
856}
857
858static int
859sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +1000860{
861 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000862 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000863 u_int bloblen, slen;
Darren Tucker502d3842003-06-28 12:38:01 +1000864 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000865 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100866 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000867
Ben Lindstromd121f612000-12-03 17:00:47 +0000868 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000869
Damien Miller280ecfb2003-05-14 13:46:00 +1000870 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100871 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000872 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100873 return 0;
874 }
Damien Millereba71ba2000-04-29 23:57:08 +1000875 /* data to be signed */
876 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100877 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000878 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000879 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100880 } else {
881 buffer_put_string(&b, session_id2, session_id2_len);
882 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000883 }
Damien Millereba71ba2000-04-29 23:57:08 +1000884 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100885 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000886 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000887 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000888 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100889 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000890 if (datafellows & SSH_BUG_PKAUTH) {
891 buffer_put_char(&b, have_sig);
892 } else {
893 buffer_put_cstring(&b, authctxt->method->name);
894 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +1000895 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +0000896 }
Damien Millereba71ba2000-04-29 23:57:08 +1000897 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000898
899 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +1000900 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000901 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000902 if (ret == -1) {
903 xfree(blob);
904 buffer_free(&b);
905 return 0;
906 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100907#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000908 buffer_dump(&b);
909#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000910 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000911 buffer_clear(&b);
912 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000913 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000914 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100915 buffer_put_cstring(&b, authctxt->server_user);
916 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100917 buffer_put_cstring(&b, authctxt->method->name);
918 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000919 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +1000920 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +1000921 buffer_put_string(&b, blob, bloblen);
922 }
923 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000924
Damien Millereba71ba2000-04-29 23:57:08 +1000925 /* append signature */
926 buffer_put_string(&b, signature, slen);
927 xfree(signature);
928
929 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000930 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100931 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000932 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000933
934 /* put remaining data from buffer into packet */
935 packet_start(SSH2_MSG_USERAUTH_REQUEST);
936 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
937 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +1000938 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000939
940 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000941}
942
Ben Lindstrombba81212001-06-25 05:01:22 +0000943static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000944send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +1000945{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000946 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000947 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000948
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000949 debug3("send_pubkey_test");
950
Damien Miller280ecfb2003-05-14 13:46:00 +1000951 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000952 /* we cannot handle this key */
953 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +1000954 return 0;
955 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000956 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000957 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +1000958
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000959 packet_start(SSH2_MSG_USERAUTH_REQUEST);
960 packet_put_cstring(authctxt->server_user);
961 packet_put_cstring(authctxt->service);
962 packet_put_cstring(authctxt->method->name);
963 packet_put_char(have_sig);
964 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +1000965 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000966 packet_put_string(blob, bloblen);
967 xfree(blob);
968 packet_send();
969 return 1;
970}
971
Ben Lindstrombba81212001-06-25 05:01:22 +0000972static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000973load_identity_file(char *filename)
974{
975 Key *private;
976 char prompt[300], *passphrase;
Darren Tucker232b76f2006-05-06 17:41:51 +1000977 int perm_ok, quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000978 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000979
Ben Lindstrom329782e2001-03-10 17:08:59 +0000980 if (stat(filename, &st) < 0) {
981 debug3("no such identity: %s", filename);
982 return NULL;
983 }
Darren Tucker232b76f2006-05-06 17:41:51 +1000984 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
985 if (!perm_ok)
986 return NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000987 if (private == NULL) {
988 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000989 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000990 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100991 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100992 for (i = 0; i < options.number_of_password_prompts; i++) {
993 passphrase = read_passphrase(prompt, 0);
994 if (strcmp(passphrase, "") != 0) {
Darren Tucker232b76f2006-05-06 17:41:51 +1000995 private = key_load_private_type(KEY_UNSPEC,
996 filename, passphrase, NULL, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000997 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100998 } else {
999 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001000 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +11001001 }
1002 memset(passphrase, 0, strlen(passphrase));
1003 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001004 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +11001005 break;
1006 debug2("bad passphrase given, try again...");
1007 }
Damien Miller994cf142000-07-21 10:19:44 +10001008 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001009 return private;
1010}
1011
Damien Miller280ecfb2003-05-14 13:46:00 +10001012/*
1013 * try keys in the following order:
1014 * 1. agent keys that are found in the config file
1015 * 2. other agent keys
1016 * 3. keys that are only listed in the config file
1017 */
1018static void
1019pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001020{
Damien Miller280ecfb2003-05-14 13:46:00 +10001021 Identity *id;
1022 Idlist agent, files, *preferred;
1023 Key *key;
1024 AuthenticationConnection *ac;
Damien Millerad833b32000-08-23 10:46:23 +10001025 char *comment;
Damien Miller280ecfb2003-05-14 13:46:00 +10001026 int i, found;
Damien Millerad833b32000-08-23 10:46:23 +10001027
Damien Miller280ecfb2003-05-14 13:46:00 +10001028 TAILQ_INIT(&agent); /* keys from the agent */
1029 TAILQ_INIT(&files); /* keys from the config file */
1030 preferred = &authctxt->keys;
1031 TAILQ_INIT(preferred); /* preferred order of keys */
1032
1033 /* list of keys stored in the filesystem */
1034 for (i = 0; i < options.num_identity_files; i++) {
1035 key = options.identity_keys[i];
1036 if (key && key->type == KEY_RSA1)
1037 continue;
1038 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001039 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001040 id->key = key;
1041 id->filename = xstrdup(options.identity_files[i]);
1042 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001043 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001044 /* list of keys supported by the agent */
1045 if ((ac = ssh_get_authentication_connection())) {
1046 for (key = ssh_get_first_identity(ac, &comment, 2);
1047 key != NULL;
1048 key = ssh_get_next_identity(ac, &comment, 2)) {
1049 found = 0;
1050 TAILQ_FOREACH(id, &files, next) {
Damien Millera8e06ce2003-11-21 23:48:55 +11001051 /* agent keys from the config file are preferred */
Damien Miller280ecfb2003-05-14 13:46:00 +10001052 if (key_equal(key, id->key)) {
1053 key_free(key);
1054 xfree(comment);
1055 TAILQ_REMOVE(&files, id, next);
1056 TAILQ_INSERT_TAIL(preferred, id, next);
1057 id->ac = ac;
1058 found = 1;
1059 break;
1060 }
1061 }
Damien Millerbd394c32004-03-08 23:12:36 +11001062 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001063 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001064 id->key = key;
1065 id->filename = comment;
1066 id->ac = ac;
1067 TAILQ_INSERT_TAIL(&agent, id, next);
1068 }
1069 }
1070 /* append remaining agent keys */
1071 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1072 TAILQ_REMOVE(&agent, id, next);
1073 TAILQ_INSERT_TAIL(preferred, id, next);
1074 }
1075 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +11001076 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001077 /* append remaining keys from the config file */
1078 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1079 TAILQ_REMOVE(&files, id, next);
1080 TAILQ_INSERT_TAIL(preferred, id, next);
1081 }
1082 TAILQ_FOREACH(id, preferred, next) {
1083 debug2("key: %s (%p)", id->filename, id->key);
1084 }
1085}
1086
1087static void
1088pubkey_cleanup(Authctxt *authctxt)
1089{
1090 Identity *id;
1091
1092 if (authctxt->agent != NULL)
1093 ssh_close_authentication_connection(authctxt->agent);
1094 for (id = TAILQ_FIRST(&authctxt->keys); id;
1095 id = TAILQ_FIRST(&authctxt->keys)) {
1096 TAILQ_REMOVE(&authctxt->keys, id, next);
1097 if (id->key)
1098 key_free(id->key);
1099 if (id->filename)
1100 xfree(id->filename);
1101 xfree(id);
1102 }
Damien Millereba71ba2000-04-29 23:57:08 +10001103}
1104
Damien Miller62cee002000-09-23 17:15:56 +11001105int
1106userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001107{
Damien Miller280ecfb2003-05-14 13:46:00 +10001108 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001109 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001110
Damien Miller280ecfb2003-05-14 13:46:00 +10001111 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1112 if (id->tried++)
1113 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001114 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001115 TAILQ_REMOVE(&authctxt->keys, id, next);
1116 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1117 /*
1118 * send a test message if we have the public key. for
1119 * encrypted keys we cannot do this and have to load the
1120 * private key instead
1121 */
1122 if (id->key && id->key->type != KEY_RSA1) {
1123 debug("Offering public key: %s", id->filename);
1124 sent = send_pubkey_test(authctxt, id);
1125 } else if (id->key == NULL) {
1126 debug("Trying private key: %s", id->filename);
1127 id->key = load_identity_file(id->filename);
1128 if (id->key != NULL) {
1129 id->isprivate = 1;
1130 sent = sign_and_send_pubkey(authctxt, id);
1131 key_free(id->key);
1132 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001133 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001134 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001135 if (sent)
1136 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001137 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001138 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001139}
Damien Millereba71ba2000-04-29 23:57:08 +10001140
Damien Miller874d77b2000-10-14 16:23:11 +11001141/*
1142 * Send userauth request message specifying keyboard-interactive method.
1143 */
1144int
1145userauth_kbdint(Authctxt *authctxt)
1146{
1147 static int attempt = 0;
1148
1149 if (attempt++ >= options.number_of_password_prompts)
1150 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001151 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1152 if (attempt > 1 && !authctxt->info_req_seen) {
1153 debug3("userauth_kbdint: disable: no info_req_seen");
1154 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1155 return 0;
1156 }
Damien Miller874d77b2000-10-14 16:23:11 +11001157
1158 debug2("userauth_kbdint");
1159 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1160 packet_put_cstring(authctxt->server_user);
1161 packet_put_cstring(authctxt->service);
1162 packet_put_cstring(authctxt->method->name);
1163 packet_put_cstring(""); /* lang */
1164 packet_put_cstring(options.kbd_interactive_devices ?
1165 options.kbd_interactive_devices : "");
1166 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001167
1168 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1169 return 1;
1170}
1171
1172/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001173 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001174 */
1175void
Damien Miller630d6f42002-01-22 23:17:30 +11001176input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001177{
1178 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001179 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001180 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001181 int echo = 0;
1182
1183 debug2("input_userauth_info_req");
1184
1185 if (authctxt == NULL)
1186 fatal("input_userauth_info_req: no authentication context");
1187
Ben Lindstrom7d199962001-09-12 18:29:00 +00001188 authctxt->info_req_seen = 1;
1189
Damien Miller874d77b2000-10-14 16:23:11 +11001190 name = packet_get_string(NULL);
1191 inst = packet_get_string(NULL);
1192 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001193 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001194 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001195 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001196 logit("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001197 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001198 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001199 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001200
1201 num_prompts = packet_get_int();
1202 /*
1203 * Begin to build info response packet based on prompts requested.
1204 * We commit to providing the correct number of responses, so if
1205 * further on we run into a problem that prevents this, we have to
1206 * be sure and clean this up and send a correct error response.
1207 */
1208 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1209 packet_put_int(num_prompts);
1210
Ben Lindstrom551ea372001-06-05 18:56:16 +00001211 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001212 for (i = 0; i < num_prompts; i++) {
1213 prompt = packet_get_string(NULL);
1214 echo = packet_get_char();
1215
Ben Lindstrom949974b2001-06-25 05:20:31 +00001216 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001217
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001218 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +11001219 memset(response, 0, strlen(response));
1220 xfree(response);
1221 xfree(prompt);
1222 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001223 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001224
Damien Miller9f643902001-11-12 11:02:52 +11001225 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001226 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001227}
Damien Miller62cee002000-09-23 17:15:56 +11001228
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001229static int
Ben Lindstrom5c385522002-06-23 21:23:20 +00001230ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001231 u_char *data, u_int datalen)
1232{
1233 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001234 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001235 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001236 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001237
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001238 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001239
Ben Lindstrom5206b952002-06-06 19:59:29 +00001240 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1241 error("ssh_keysign: no installed: %s", strerror(errno));
1242 return -1;
1243 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001244 if (fflush(stdout) != 0)
1245 error("ssh_keysign: fflush: %s", strerror(errno));
1246 if (pipe(to) < 0) {
1247 error("ssh_keysign: pipe: %s", strerror(errno));
1248 return -1;
1249 }
1250 if (pipe(from) < 0) {
1251 error("ssh_keysign: pipe: %s", strerror(errno));
1252 return -1;
1253 }
1254 if ((pid = fork()) < 0) {
1255 error("ssh_keysign: fork: %s", strerror(errno));
1256 return -1;
1257 }
1258 if (pid == 0) {
Damien Miller2e5fe882006-06-13 13:10:00 +10001259 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001260 close(from[0]);
1261 if (dup2(from[1], STDOUT_FILENO) < 0)
1262 fatal("ssh_keysign: dup2: %s", strerror(errno));
1263 close(to[1]);
1264 if (dup2(to[0], STDIN_FILENO) < 0)
1265 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001266 close(from[1]);
1267 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001268 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001269 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1270 strerror(errno));
1271 }
1272 close(from[1]);
1273 close(to[0]);
1274
1275 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001276 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001277 buffer_put_string(&b, data, datalen);
Damien Miller51bf11f2003-11-17 21:20:47 +11001278 if (ssh_msg_send(to[1], version, &b) == -1)
1279 fatal("ssh_keysign: couldn't send request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001280
Damien Miller901119b2002-10-04 11:10:04 +10001281 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001282 error("ssh_keysign: no reply");
Damien Millerfb1310e2004-01-21 11:02:50 +11001283 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001284 return -1;
1285 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001286 close(from[0]);
1287 close(to[1]);
1288
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001289 while (waitpid(pid, &status, 0) < 0)
1290 if (errno != EINTR)
1291 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001292
Ben Lindstrom5206b952002-06-06 19:59:29 +00001293 if (buffer_get_char(&b) != version) {
1294 error("ssh_keysign: bad version");
Damien Millerfb1310e2004-01-21 11:02:50 +11001295 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001296 return -1;
1297 }
1298 *sigp = buffer_get_string(&b, lenp);
Damien Millerfb1310e2004-01-21 11:02:50 +11001299 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001300
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001301 return 0;
1302}
1303
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001304int
1305userauth_hostbased(Authctxt *authctxt)
1306{
1307 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001308 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001309 Buffer b;
1310 u_char *signature, *blob;
Darren Tucker26c66622007-05-20 15:09:42 +10001311 char *chost, *pkalg, *p, myname[NI_MAXHOST];
Ben Lindstrom671388f2001-04-19 20:40:45 +00001312 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001313 u_int blen, slen;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +00001314 int ok, i, len, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001315
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001316 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001317 for (i = 0; i < sensitive->nkeys; i++) {
1318 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001319 if (private && private->type != KEY_RSA1) {
1320 found = 1;
1321 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001322 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001323 break;
1324 }
1325 }
1326 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001327 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001328 return 0;
1329 }
1330 if (key_to_blob(private, &blob, &blen) == 0) {
1331 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001332 return 0;
1333 }
Damien Miller91c18472001-11-12 11:02:03 +11001334 /* figure out a name for the client host */
Darren Tucker26c66622007-05-20 15:09:42 +10001335 p = NULL;
1336 if (packet_connection_is_on_socket())
1337 p = get_local_name(packet_get_connection_in());
1338 if (p == NULL) {
1339 if (gethostname(myname, sizeof(myname)) == -1) {
1340 verbose("userauth_hostbased: gethostname: %s",
1341 strerror(errno));
1342 } else
1343 p = xstrdup(myname);
1344 }
Damien Miller91c18472001-11-12 11:02:03 +11001345 if (p == NULL) {
1346 error("userauth_hostbased: cannot get local ipaddr/name");
1347 key_free(private);
Damien Miller5790b592006-03-26 13:54:03 +11001348 xfree(blob);
Damien Miller91c18472001-11-12 11:02:03 +11001349 return 0;
1350 }
1351 len = strlen(p) + 2;
Damien Miller07d86be2006-03-26 14:19:21 +11001352 xasprintf(&chost, "%s.", p);
Damien Miller91c18472001-11-12 11:02:03 +11001353 debug2("userauth_hostbased: chost %s", chost);
Damien Miller00111382003-03-10 11:21:17 +11001354 xfree(p);
Damien Miller91c18472001-11-12 11:02:03 +11001355
Ben Lindstrom671388f2001-04-19 20:40:45 +00001356 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1357 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001358 pkalg = xstrdup(key_ssh_name(private));
1359 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001360 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001361 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001362 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1363 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001364 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001365 buffer_put_cstring(&b, authctxt->method->name);
1366 buffer_put_cstring(&b, pkalg);
1367 buffer_put_string(&b, blob, blen);
1368 buffer_put_cstring(&b, chost);
1369 buffer_put_cstring(&b, authctxt->local_user);
1370#ifdef DEBUG_PK
1371 buffer_dump(&b);
1372#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001373 if (sensitive->external_keysign)
1374 ok = ssh_keysign(private, &signature, &slen,
1375 buffer_ptr(&b), buffer_len(&b));
1376 else
1377 ok = key_sign(private, &signature, &slen,
1378 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001379 key_free(private);
1380 buffer_free(&b);
1381 if (ok != 0) {
1382 error("key_sign failed");
1383 xfree(chost);
1384 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001385 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001386 return 0;
1387 }
1388 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1389 packet_put_cstring(authctxt->server_user);
1390 packet_put_cstring(authctxt->service);
1391 packet_put_cstring(authctxt->method->name);
1392 packet_put_cstring(pkalg);
1393 packet_put_string(blob, blen);
1394 packet_put_cstring(chost);
1395 packet_put_cstring(authctxt->local_user);
1396 packet_put_string(signature, slen);
1397 memset(signature, 's', slen);
1398 xfree(signature);
1399 xfree(chost);
1400 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001401 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001402
1403 packet_send();
1404 return 1;
1405}
1406
Damien Miller62cee002000-09-23 17:15:56 +11001407/* find auth method */
1408
Damien Miller62cee002000-09-23 17:15:56 +11001409/*
1410 * given auth method name, if configurable options permit this method fill
1411 * in auth_ident field and return true, otherwise return false.
1412 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001413static int
Damien Miller62cee002000-09-23 17:15:56 +11001414authmethod_is_enabled(Authmethod *method)
1415{
1416 if (method == NULL)
1417 return 0;
1418 /* return false if options indicate this method is disabled */
1419 if (method->enabled == NULL || *method->enabled == 0)
1420 return 0;
1421 /* return false if batch mode is enabled but method needs interactive mode */
1422 if (method->batch_flag != NULL && *method->batch_flag != 0)
1423 return 0;
1424 return 1;
1425}
1426
Ben Lindstrombba81212001-06-25 05:01:22 +00001427static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001428authmethod_lookup(const char *name)
1429{
1430 Authmethod *method = NULL;
1431 if (name != NULL)
1432 for (method = authmethods; method->name != NULL; method++)
1433 if (strcmp(name, method->name) == 0)
1434 return method;
1435 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1436 return NULL;
1437}
1438
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001439/* XXX internal state */
1440static Authmethod *current = NULL;
1441static char *supported = NULL;
1442static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001443
Damien Miller62cee002000-09-23 17:15:56 +11001444/*
1445 * Given the authentication method list sent by the server, return the
1446 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001447 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001448 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001449static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001450authmethod_get(char *authlist)
1451{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001452 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001453 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001454
Damien Miller62cee002000-09-23 17:15:56 +11001455 /* Use a suitable default if we're passed a nil list. */
1456 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001457 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001458
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001459 if (supported == NULL || strcmp(authlist, supported) != 0) {
1460 debug3("start over, passed a different list %s", authlist);
1461 if (supported != NULL)
1462 xfree(supported);
1463 supported = xstrdup(authlist);
1464 preferred = options.preferred_authentications;
1465 debug3("preferred %s", preferred);
1466 current = NULL;
1467 } else if (current != NULL && authmethod_is_enabled(current))
1468 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001469
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001470 for (;;) {
1471 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001472 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001473 current = NULL;
1474 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001475 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001476 preferred += next;
1477 debug3("authmethod_lookup %s", name);
1478 debug3("remaining preferred: %s", preferred);
1479 if ((current = authmethod_lookup(name)) != NULL &&
1480 authmethod_is_enabled(current)) {
1481 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001482 debug("Next authentication method: %s", name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001483 return current;
1484 }
Damien Millereba71ba2000-04-29 23:57:08 +10001485 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001486}
Damien Miller62cee002000-09-23 17:15:56 +11001487
Ben Lindstrom79073822001-07-04 03:42:30 +00001488static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001489authmethods_get(void)
1490{
1491 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001492 Buffer b;
1493 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001494
Damien Miller0e3b8722002-01-22 23:26:38 +11001495 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001496 for (method = authmethods; method->name != NULL; method++) {
1497 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001498 if (buffer_len(&b) > 0)
1499 buffer_append(&b, ",", 1);
1500 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001501 }
Damien Miller62cee002000-09-23 17:15:56 +11001502 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001503 buffer_append(&b, "\0", 1);
1504 list = xstrdup(buffer_ptr(&b));
1505 buffer_free(&b);
1506 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001507}