blob: 8b2e633c0b435a4e3edbf11c98bad50da858a25b [file] [log] [blame]
Damien Millera1cb9f32006-08-19 00:33:34 +10001/* $OpenBSD: sshconnect2.c,v 1.161 2006/08/18 13:54:54 djm Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100013 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "includes.h"
Damien Miller2eb63402006-03-15 11:09:42 +110027
Damien Miller9cf6d072006-03-15 11:29:24 +110028#include <sys/types.h>
Damien Millerd7834352006-08-05 12:39:39 +100029#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110030#include <sys/wait.h>
Damien Millerf17883e2006-03-15 11:45:54 +110031#include <sys/stat.h>
Damien Millereba71ba2000-04-29 23:57:08 +100032
Darren Tucker39972492006-07-12 22:22:46 +100033#include <errno.h>
Damien Millerd7834352006-08-05 12:39:39 +100034#include <pwd.h>
35#include <signal.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100036#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100037#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100038#include <unistd.h>
Darren Tucker39972492006-07-12 22:22:46 +100039
Damien Miller9c617692003-05-14 14:31:11 +100040#include "openbsd-compat/sys-queue.h"
41
Damien Millerd7834352006-08-05 12:39:39 +100042#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100043#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000044#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100045#include "buffer.h"
46#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100047#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000048#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100049#include "key.h"
Damien Millereba71ba2000-04-29 23:57:08 +100050#include "kex.h"
51#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100052#include "sshconnect.h"
53#include "authfile.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000054#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100055#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "log.h"
57#include "readconf.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100058#include "misc.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000059#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000060#include "dispatch.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000061#include "canohost.h"
Ben Lindstrom1bad2562002-06-06 19:57:33 +000062#include "msg.h"
63#include "pathnames.h"
Damien Miller6b4069a2006-06-13 13:05:15 +100064#include "uidswap.h"
Damien Miller874d77b2000-10-14 16:23:11 +110065
Darren Tucker0efd1552003-08-26 11:49:55 +100066#ifdef GSSAPI
67#include "ssh-gss.h"
68#endif
69
Damien Millereba71ba2000-04-29 23:57:08 +100070/* import */
71extern char *client_version_string;
72extern char *server_version_string;
73extern Options options;
74
75/*
76 * SSH2 key exchange
77 */
78
Ben Lindstrom46c16222000-12-22 01:43:59 +000079u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100080u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100081
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000082char *xxx_host;
83struct sockaddr *xxx_hostaddr;
84
Ben Lindstromf28f6342001-04-04 02:03:04 +000085Kex *xxx_kex = NULL;
86
Ben Lindstrombba81212001-06-25 05:01:22 +000087static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000088verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000089{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000090 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +100091 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000092 return 0;
93}
94
Damien Millereba71ba2000-04-29 23:57:08 +100095void
Damien Miller874d77b2000-10-14 16:23:11 +110096ssh_kex2(char *host, struct sockaddr *hostaddr)
97{
Damien Miller874d77b2000-10-14 16:23:11 +110098 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000099
100 xxx_host = host;
101 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100102
Damien Millere39cacc2000-11-29 12:18:44 +1100103 if (options.ciphers == (char *)-1) {
Damien Miller996acd22003-04-09 20:59:48 +1000104 logit("No valid ciphers for protocol version 2 given, using defaults.");
Damien Millere39cacc2000-11-29 12:18:44 +1100105 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100106 }
107 if (options.ciphers != NULL) {
108 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
109 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
110 }
Damien Millera0ff4662001-03-30 10:49:35 +1000111 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
112 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
113 myproposal[PROPOSAL_ENC_ALGS_STOC] =
114 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100115 if (options.compression) {
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] = "zlib@openssh.com,zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100118 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000119 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000120 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100121 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000122 if (options.macs != NULL) {
123 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
124 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
125 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000126 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100127 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000128 options.hostkeyalgorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100129
Damien Millera5539d22003-04-09 20:50:06 +1000130 if (options.rekey_limit)
131 packet_set_rekey_limit(options.rekey_limit);
132
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000133 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000134 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100135 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000136 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100137 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100138 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000139 kex->client_version_string=client_version_string;
140 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000141 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100142
Ben Lindstromf28f6342001-04-04 02:03:04 +0000143 xxx_kex = kex;
144
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000145 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100146
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000147 session_id2 = kex->session_id;
148 session_id2_len = kex->session_id_len;
149
Damien Miller874d77b2000-10-14 16:23:11 +1100150#ifdef DEBUG_KEXDH
151 /* send 1st encrypted/maced/compressed message */
152 packet_start(SSH2_MSG_IGNORE);
153 packet_put_cstring("markus");
154 packet_send();
155 packet_write_wait();
156#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000157}
Damien Millerb1715dc2000-05-30 13:44:51 +1000158
Damien Millereba71ba2000-04-29 23:57:08 +1000159/*
160 * Authenticate user
161 */
Damien Miller62cee002000-09-23 17:15:56 +1100162
163typedef struct Authctxt Authctxt;
164typedef struct Authmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000165typedef struct identity Identity;
166typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100167
Damien Miller280ecfb2003-05-14 13:46:00 +1000168struct identity {
169 TAILQ_ENTRY(identity) next;
170 AuthenticationConnection *ac; /* set if agent supports key */
171 Key *key; /* public/private key */
172 char *filename; /* comment for agent-only keys */
173 int tried;
174 int isprivate; /* key points to the private key */
175};
176TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100177
178struct Authctxt {
179 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000180 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100181 const char *host;
182 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100183 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100184 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000185 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000186 /* pubkey */
Damien Miller280ecfb2003-05-14 13:46:00 +1000187 Idlist keys;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000188 AuthenticationConnection *agent;
189 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000190 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000191 /* kbd-interactive */
192 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000193 /* generic */
194 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100195};
196struct Authmethod {
197 char *name; /* string to compare against server's list */
198 int (*userauth)(Authctxt *authctxt);
199 int *enabled; /* flag in option struct that enables method */
200 int *batch_flag; /* flag in option struct that disables method */
201};
202
Damien Miller630d6f42002-01-22 23:17:30 +1100203void input_userauth_success(int, u_int32_t, void *);
204void input_userauth_failure(int, u_int32_t, void *);
205void input_userauth_banner(int, u_int32_t, void *);
206void input_userauth_error(int, u_int32_t, void *);
207void input_userauth_info_req(int, u_int32_t, void *);
208void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000209void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100210
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000211int userauth_none(Authctxt *);
212int userauth_pubkey(Authctxt *);
213int userauth_passwd(Authctxt *);
214int userauth_kbdint(Authctxt *);
215int userauth_hostbased(Authctxt *);
Damien Miller3ab496b2003-05-14 13:47:37 +1000216int userauth_kerberos(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100217
Darren Tucker0efd1552003-08-26 11:49:55 +1000218#ifdef GSSAPI
219int userauth_gssapi(Authctxt *authctxt);
220void input_gssapi_response(int type, u_int32_t, void *);
221void input_gssapi_token(int type, u_int32_t, void *);
222void input_gssapi_hash(int type, u_int32_t, void *);
223void input_gssapi_error(int, u_int32_t, void *);
224void input_gssapi_errtok(int, u_int32_t, void *);
225#endif
226
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000227void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000228
Damien Miller280ecfb2003-05-14 13:46:00 +1000229static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000230static void pubkey_prepare(Authctxt *);
231static void pubkey_cleanup(Authctxt *);
232static Key *load_identity_file(char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000233
Ben Lindstrombba81212001-06-25 05:01:22 +0000234static Authmethod *authmethod_get(char *authlist);
235static Authmethod *authmethod_lookup(const char *name);
236static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100237
238Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000239#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100240 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000241 userauth_gssapi,
242 &options.gss_authentication,
243 NULL},
244#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000245 {"hostbased",
246 userauth_hostbased,
247 &options.hostbased_authentication,
248 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000249 {"publickey",
250 userauth_pubkey,
251 &options.pubkey_authentication,
252 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100253 {"keyboard-interactive",
254 userauth_kbdint,
255 &options.kbd_interactive_authentication,
256 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000257 {"password",
258 userauth_passwd,
259 &options.password_authentication,
260 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100261 {"none",
262 userauth_none,
263 NULL,
264 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100265 {NULL, NULL, NULL, NULL}
266};
267
268void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000269ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000270 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100271{
272 Authctxt authctxt;
273 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100274
Ben Lindstrom551ea372001-06-05 18:56:16 +0000275 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000276 options.kbd_interactive_authentication = 1;
277
Damien Miller62cee002000-09-23 17:15:56 +1100278 packet_start(SSH2_MSG_SERVICE_REQUEST);
279 packet_put_cstring("ssh-userauth");
280 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000281 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100282 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100283 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000284 if (type != SSH2_MSG_SERVICE_ACCEPT)
285 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100286 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100287 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000288 debug2("service_accept: %s", reply);
Damien Miller62cee002000-09-23 17:15:56 +1100289 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100290 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000291 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100292 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100293 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000294 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100295
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000296 if (options.preferred_authentications == NULL)
297 options.preferred_authentications = authmethods_get();
298
Damien Miller62cee002000-09-23 17:15:56 +1100299 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000300 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000301 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100302 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000303 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100304 authctxt.host = host;
305 authctxt.service = "ssh-connection"; /* service name */
306 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100307 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000308 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000309 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000310 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000311 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100312 if (authctxt.method == NULL)
313 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100314
315 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100316 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100317
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000318 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100319 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
320 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000321 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100322 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
323
Damien Miller280ecfb2003-05-14 13:46:00 +1000324 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000325 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
326
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000327 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100328}
Damien Millerf842fcb2003-05-15 12:01:28 +1000329
Damien Miller62cee002000-09-23 17:15:56 +1100330void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000331userauth(Authctxt *authctxt, char *authlist)
332{
Darren Tucker0efd1552003-08-26 11:49:55 +1000333 if (authctxt->methoddata) {
334 xfree(authctxt->methoddata);
335 authctxt->methoddata = NULL;
336 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000337 if (authlist == NULL) {
338 authlist = authctxt->authlist;
339 } else {
340 if (authctxt->authlist)
341 xfree(authctxt->authlist);
342 authctxt->authlist = authlist;
343 }
344 for (;;) {
345 Authmethod *method = authmethod_get(authlist);
346 if (method == NULL)
347 fatal("Permission denied (%s).", authlist);
348 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000349
350 /* reset the per method handler */
351 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
352 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
353
354 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000355 if (method->userauth(authctxt) != 0) {
356 debug2("we sent a %s packet, wait for reply", method->name);
357 break;
358 } else {
359 debug2("we did not send a packet, disable method");
360 method->enabled = NULL;
361 }
362 }
363}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000364
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000365void
Damien Miller630d6f42002-01-22 23:17:30 +1100366input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100367{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000368 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000369 "type %d", type);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000370}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000371
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000372void
Damien Miller630d6f42002-01-22 23:17:30 +1100373input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000374{
375 char *msg, *lang;
Darren Tucker79644822003-10-08 17:37:58 +1000376
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000377 debug3("input_userauth_banner");
378 msg = packet_get_string(NULL);
379 lang = packet_get_string(NULL);
Darren Tucker046dff22003-10-08 17:32:02 +1000380 if (options.log_level > SYSLOG_LEVEL_QUIET)
381 fprintf(stderr, "%s", msg);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000382 xfree(msg);
383 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100384}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000385
Damien Miller62cee002000-09-23 17:15:56 +1100386void
Damien Miller630d6f42002-01-22 23:17:30 +1100387input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100388{
389 Authctxt *authctxt = ctxt;
390 if (authctxt == NULL)
391 fatal("input_userauth_success: no authentication context");
Darren Tucker79644822003-10-08 17:37:58 +1000392 if (authctxt->authlist) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000393 xfree(authctxt->authlist);
Darren Tucker79644822003-10-08 17:37:58 +1000394 authctxt->authlist = NULL;
395 }
396 if (authctxt->methoddata) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000397 xfree(authctxt->methoddata);
Darren Tucker79644822003-10-08 17:37:58 +1000398 authctxt->methoddata = NULL;
399 }
Damien Miller62cee002000-09-23 17:15:56 +1100400 authctxt->success = 1; /* break out */
401}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000402
Damien Miller62cee002000-09-23 17:15:56 +1100403void
Damien Miller630d6f42002-01-22 23:17:30 +1100404input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100405{
Damien Miller62cee002000-09-23 17:15:56 +1100406 Authctxt *authctxt = ctxt;
407 char *authlist = NULL;
408 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100409
410 if (authctxt == NULL)
411 fatal("input_userauth_failure: no authentication context");
412
Damien Miller874d77b2000-10-14 16:23:11 +1100413 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100414 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100415 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100416
417 if (partial != 0)
Damien Miller996acd22003-04-09 20:59:48 +1000418 logit("Authenticated with partial success.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000419 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100420
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000421 userauth(authctxt, authlist);
422}
423void
Damien Miller630d6f42002-01-22 23:17:30 +1100424input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000425{
426 Authctxt *authctxt = ctxt;
427 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000428 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000429 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000430 int pktype, sent = 0;
431 u_int alen, blen;
432 char *pkalg, *fp;
433 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000434
435 if (authctxt == NULL)
436 fatal("input_userauth_pk_ok: no authentication context");
437 if (datafellows & SSH_BUG_PKOK) {
438 /* this is similar to SSH_BUG_PKAUTH */
439 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
440 pkblob = packet_get_string(&blen);
441 buffer_init(&b);
442 buffer_append(&b, pkblob, blen);
443 pkalg = buffer_get_string(&b, &alen);
444 buffer_free(&b);
445 } else {
446 pkalg = packet_get_string(&alen);
447 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000448 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100449 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000450
Damien Miller280ecfb2003-05-14 13:46:00 +1000451 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000452
Damien Miller280ecfb2003-05-14 13:46:00 +1000453 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
454 debug("unknown pkalg %s", pkalg);
455 goto done;
456 }
457 if ((key = key_from_blob(pkblob, blen)) == NULL) {
458 debug("no key from blob. pkalg %s", pkalg);
459 goto done;
460 }
461 if (key->type != pktype) {
462 error("input_userauth_pk_ok: type mismatch "
463 "for decoded key (received %d, expected %d)",
464 key->type, pktype);
465 goto done;
466 }
467 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
468 debug2("input_userauth_pk_ok: fp %s", fp);
469 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000470
Darren Tuckerd05b6012003-10-15 15:55:59 +1000471 /*
472 * search keys in the reverse order, because last candidate has been
473 * moved to the end of the queue. this also avoids confusion by
474 * duplicate keys
475 */
Damien Miller0b51a522004-04-20 20:07:19 +1000476 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000477 if (key_equal(key, id->key)) {
478 sent = sign_and_send_pubkey(authctxt, id);
479 break;
480 }
481 }
482done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000483 if (key != NULL)
484 key_free(key);
485 xfree(pkalg);
486 xfree(pkblob);
487
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000488 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000489 if (sent == 0)
490 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100491}
492
Darren Tucker0efd1552003-08-26 11:49:55 +1000493#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100494int
Darren Tucker0efd1552003-08-26 11:49:55 +1000495userauth_gssapi(Authctxt *authctxt)
496{
497 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100498 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000499 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000500 OM_uint32 min;
501 int ok = 0;
502
503 /* Try one GSSAPI method at a time, rather than sending them all at
504 * once. */
505
Darren Tucker56afe142003-11-03 20:06:14 +1100506 if (gss_supported == NULL)
507 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000508
509 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100510 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000511 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100512 if (gss_supported->elements[mech].length < 128 &&
Damien Millera1cb9f32006-08-19 00:33:34 +1000513 ssh_gssapi_check_mechanism(&gssctxt,
514 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000515 ok = 1; /* Mechanism works */
516 } else {
517 mech++;
518 }
519 }
520
Damien Millera1cb9f32006-08-19 00:33:34 +1000521 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000522 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000523
524 authctxt->methoddata=(void *)gssctxt;
525
526 packet_start(SSH2_MSG_USERAUTH_REQUEST);
527 packet_put_cstring(authctxt->server_user);
528 packet_put_cstring(authctxt->service);
529 packet_put_cstring(authctxt->method->name);
530
531 packet_put_int(1);
532
Darren Tucker655a5e02003-11-03 20:09:03 +1100533 packet_put_int((gss_supported->elements[mech].length) + 2);
534 packet_put_char(SSH_GSS_OIDTYPE);
535 packet_put_char(gss_supported->elements[mech].length);
536 packet_put_raw(gss_supported->elements[mech].elements,
537 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000538
539 packet_send();
540
541 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
542 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
543 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
544 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
545
546 mech++; /* Move along to next candidate */
547
548 return 1;
549}
550
Damien Miller91c6aa42003-11-17 21:20:18 +1100551static OM_uint32
552process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
553{
554 Authctxt *authctxt = ctxt;
555 Gssctxt *gssctxt = authctxt->methoddata;
556 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000557 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
558 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100559 OM_uint32 status, ms, flags;
560 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100561
Damien Miller91c6aa42003-11-17 21:20:18 +1100562 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100563 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100564
565 if (send_tok.length > 0) {
566 if (GSS_ERROR(status))
567 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
568 else
569 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100570
Damien Miller91c6aa42003-11-17 21:20:18 +1100571 packet_put_string(send_tok.value, send_tok.length);
572 packet_send();
573 gss_release_buffer(&ms, &send_tok);
574 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100575
Damien Miller91c6aa42003-11-17 21:20:18 +1100576 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100577 /* send either complete or MIC, depending on mechanism */
578 if (!(flags & GSS_C_INTEG_FLAG)) {
579 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
580 packet_send();
581 } else {
582 ssh_gssapi_buildmic(&b, authctxt->server_user,
583 authctxt->service, "gssapi-with-mic");
584
585 gssbuf.value = buffer_ptr(&b);
586 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100587
Damien Miller0425d402003-11-17 22:18:21 +1100588 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100589
Damien Miller0425d402003-11-17 22:18:21 +1100590 if (!GSS_ERROR(status)) {
591 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
592 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100593
Damien Miller0425d402003-11-17 22:18:21 +1100594 packet_send();
595 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100596
Damien Miller0425d402003-11-17 22:18:21 +1100597 buffer_free(&b);
598 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100599 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100600 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100601
Damien Miller91c6aa42003-11-17 21:20:18 +1100602 return status;
603}
604
Darren Tucker0efd1552003-08-26 11:49:55 +1000605void
606input_gssapi_response(int type, u_int32_t plen, void *ctxt)
607{
608 Authctxt *authctxt = ctxt;
609 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000610 int oidlen;
611 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000612
613 if (authctxt == NULL)
614 fatal("input_gssapi_response: no authentication context");
615 gssctxt = authctxt->methoddata;
616
617 /* Setup our OID */
618 oidv = packet_get_string(&oidlen);
619
Darren Tucker655a5e02003-11-03 20:09:03 +1100620 if (oidlen <= 2 ||
621 oidv[0] != SSH_GSS_OIDTYPE ||
622 oidv[1] != oidlen - 2) {
Damien Miller91c6aa42003-11-17 21:20:18 +1100623 xfree(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100624 debug("Badly encoded mechanism OID received");
625 userauth(authctxt, NULL);
Darren Tucker655a5e02003-11-03 20:09:03 +1100626 return;
Darren Tucker0efd1552003-08-26 11:49:55 +1000627 }
628
Darren Tucker655a5e02003-11-03 20:09:03 +1100629 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
630 fatal("Server returned different OID than expected");
631
Darren Tucker0efd1552003-08-26 11:49:55 +1000632 packet_check_eom();
633
634 xfree(oidv);
635
Damien Miller91c6aa42003-11-17 21:20:18 +1100636 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000637 /* Start again with next method on list */
638 debug("Trying to start again");
639 userauth(authctxt, NULL);
640 return;
641 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000642}
643
644void
645input_gssapi_token(int type, u_int32_t plen, void *ctxt)
646{
647 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000648 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100649 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000650 u_int slen;
651
652 if (authctxt == NULL)
653 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000654
655 recv_tok.value = packet_get_string(&slen);
656 recv_tok.length = slen; /* safe typecast */
657
658 packet_check_eom();
659
Damien Miller91c6aa42003-11-17 21:20:18 +1100660 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000661
662 xfree(recv_tok.value);
663
664 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000665 /* Start again with the next method in the list */
666 userauth(authctxt, NULL);
667 return;
668 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000669}
670
671void
672input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
673{
674 Authctxt *authctxt = ctxt;
675 Gssctxt *gssctxt;
676 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
677 gss_buffer_desc recv_tok;
678 OM_uint32 status, ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000679 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000680
681 if (authctxt == NULL)
682 fatal("input_gssapi_response: no authentication context");
683 gssctxt = authctxt->methoddata;
684
Darren Tucker600ad8d2003-08-26 12:10:48 +1000685 recv_tok.value = packet_get_string(&len);
686 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000687
688 packet_check_eom();
689
690 /* Stick it into GSSAPI and see what it says */
691 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000692 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000693
694 xfree(recv_tok.value);
695 gss_release_buffer(&ms, &send_tok);
696
697 /* Server will be returning a failed packet after this one */
698}
699
700void
701input_gssapi_error(int type, u_int32_t plen, void *ctxt)
702{
703 OM_uint32 maj, min;
704 char *msg;
705 char *lang;
706
707 maj=packet_get_int();
708 min=packet_get_int();
709 msg=packet_get_string(NULL);
710 lang=packet_get_string(NULL);
711
712 packet_check_eom();
713
Damien Miller15d72a02005-11-05 15:07:33 +1100714 debug("Server GSSAPI Error:\n%s", msg);
Darren Tucker0efd1552003-08-26 11:49:55 +1000715 xfree(msg);
716 xfree(lang);
717}
718#endif /* GSSAPI */
719
Damien Millereba71ba2000-04-29 23:57:08 +1000720int
Damien Miller874d77b2000-10-14 16:23:11 +1100721userauth_none(Authctxt *authctxt)
722{
723 /* initial userauth request */
724 packet_start(SSH2_MSG_USERAUTH_REQUEST);
725 packet_put_cstring(authctxt->server_user);
726 packet_put_cstring(authctxt->service);
727 packet_put_cstring(authctxt->method->name);
728 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100729 return 1;
730}
731
732int
Damien Miller62cee002000-09-23 17:15:56 +1100733userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000734{
Damien Millere247cc42000-05-07 12:03:14 +1000735 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000736 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000737 char *password;
738
Damien Millerd3a18572000-06-07 19:55:44 +1000739 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000740 return 0;
741
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000742 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000743 error("Permission denied, please try again.");
744
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000745 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100746 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000747 password = read_passphrase(prompt, 0);
748 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100749 packet_put_cstring(authctxt->server_user);
750 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100751 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000752 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000753 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000754 memset(password, 0, strlen(password));
755 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100756 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000757 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000758
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000759 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000760 &input_userauth_passwd_changereq);
761
Damien Millereba71ba2000-04-29 23:57:08 +1000762 return 1;
763}
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000764/*
765 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
766 */
767void
Tim Ricec8549622002-03-31 12:49:38 -0800768input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000769{
770 Authctxt *authctxt = ctxt;
771 char *info, *lang, *password = NULL, *retype = NULL;
772 char prompt[150];
773
774 debug2("input_userauth_passwd_changereq");
775
776 if (authctxt == NULL)
777 fatal("input_userauth_passwd_changereq: "
778 "no authentication context");
779
780 info = packet_get_string(NULL);
781 lang = packet_get_string(NULL);
782 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000783 logit("%s", info);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000784 xfree(info);
785 xfree(lang);
786 packet_start(SSH2_MSG_USERAUTH_REQUEST);
787 packet_put_cstring(authctxt->server_user);
788 packet_put_cstring(authctxt->service);
789 packet_put_cstring(authctxt->method->name);
790 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000791 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000792 "Enter %.30s@%.128s's old password: ",
793 authctxt->server_user, authctxt->host);
794 password = read_passphrase(prompt, 0);
795 packet_put_cstring(password);
796 memset(password, 0, strlen(password));
797 xfree(password);
798 password = NULL;
799 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000800 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000801 "Enter %.30s@%.128s's new password: ",
802 authctxt->server_user, authctxt->host);
803 password = read_passphrase(prompt, RP_ALLOW_EOF);
804 if (password == NULL) {
805 /* bail out */
806 return;
807 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000808 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000809 "Retype %.30s@%.128s's new password: ",
810 authctxt->server_user, authctxt->host);
811 retype = read_passphrase(prompt, 0);
812 if (strcmp(password, retype) != 0) {
813 memset(password, 0, strlen(password));
814 xfree(password);
Damien Miller996acd22003-04-09 20:59:48 +1000815 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000816 password = NULL;
817 }
818 memset(retype, 0, strlen(retype));
819 xfree(retype);
820 }
821 packet_put_cstring(password);
822 memset(password, 0, strlen(password));
823 xfree(password);
824 packet_add_padding(64);
825 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000826
827 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000828 &input_userauth_passwd_changereq);
829}
Damien Millereba71ba2000-04-29 23:57:08 +1000830
Ben Lindstrombba81212001-06-25 05:01:22 +0000831static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000832identity_sign(Identity *id, u_char **sigp, u_int *lenp,
833 u_char *data, u_int datalen)
834{
835 Key *prv;
836 int ret;
837
838 /* the agent supports this key */
839 if (id->ac)
840 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
841 data, datalen));
842 /*
843 * we have already loaded the private key or
844 * the private key is stored in external hardware
845 */
846 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
847 return (key_sign(id->key, sigp, lenp, data, datalen));
848 /* load the private key from the file */
849 if ((prv = load_identity_file(id->filename)) == NULL)
850 return (-1);
851 ret = key_sign(prv, sigp, lenp, data, datalen);
852 key_free(prv);
853 return (ret);
854}
855
856static int
857sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +1000858{
859 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000860 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000861 u_int bloblen, slen;
Darren Tucker502d3842003-06-28 12:38:01 +1000862 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000863 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100864 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000865
Ben Lindstromd121f612000-12-03 17:00:47 +0000866 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000867
Damien Miller280ecfb2003-05-14 13:46:00 +1000868 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100869 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000870 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100871 return 0;
872 }
Damien Millereba71ba2000-04-29 23:57:08 +1000873 /* data to be signed */
874 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100875 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000876 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000877 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100878 } else {
879 buffer_put_string(&b, session_id2, session_id2_len);
880 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000881 }
Damien Millereba71ba2000-04-29 23:57:08 +1000882 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100883 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000884 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000885 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000886 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100887 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000888 if (datafellows & SSH_BUG_PKAUTH) {
889 buffer_put_char(&b, have_sig);
890 } else {
891 buffer_put_cstring(&b, authctxt->method->name);
892 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +1000893 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +0000894 }
Damien Millereba71ba2000-04-29 23:57:08 +1000895 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000896
897 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +1000898 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000899 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000900 if (ret == -1) {
901 xfree(blob);
902 buffer_free(&b);
903 return 0;
904 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100905#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000906 buffer_dump(&b);
907#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000908 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000909 buffer_clear(&b);
910 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000911 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000912 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100913 buffer_put_cstring(&b, authctxt->server_user);
914 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100915 buffer_put_cstring(&b, authctxt->method->name);
916 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000917 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +1000918 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +1000919 buffer_put_string(&b, blob, bloblen);
920 }
921 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000922
Damien Millereba71ba2000-04-29 23:57:08 +1000923 /* append signature */
924 buffer_put_string(&b, signature, slen);
925 xfree(signature);
926
927 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000928 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100929 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000930 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000931
932 /* put remaining data from buffer into packet */
933 packet_start(SSH2_MSG_USERAUTH_REQUEST);
934 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
935 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +1000936 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000937
938 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000939}
940
Ben Lindstrombba81212001-06-25 05:01:22 +0000941static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000942send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +1000943{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000944 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000945 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000946
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000947 debug3("send_pubkey_test");
948
Damien Miller280ecfb2003-05-14 13:46:00 +1000949 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000950 /* we cannot handle this key */
951 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +1000952 return 0;
953 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000954 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000955 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +1000956
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000957 packet_start(SSH2_MSG_USERAUTH_REQUEST);
958 packet_put_cstring(authctxt->server_user);
959 packet_put_cstring(authctxt->service);
960 packet_put_cstring(authctxt->method->name);
961 packet_put_char(have_sig);
962 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +1000963 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000964 packet_put_string(blob, bloblen);
965 xfree(blob);
966 packet_send();
967 return 1;
968}
969
Ben Lindstrombba81212001-06-25 05:01:22 +0000970static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000971load_identity_file(char *filename)
972{
973 Key *private;
974 char prompt[300], *passphrase;
Darren Tucker232b76f2006-05-06 17:41:51 +1000975 int perm_ok, quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000976 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000977
Ben Lindstrom329782e2001-03-10 17:08:59 +0000978 if (stat(filename, &st) < 0) {
979 debug3("no such identity: %s", filename);
980 return NULL;
981 }
Darren Tucker232b76f2006-05-06 17:41:51 +1000982 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
983 if (!perm_ok)
984 return NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000985 if (private == NULL) {
986 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000987 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000988 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100989 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100990 for (i = 0; i < options.number_of_password_prompts; i++) {
991 passphrase = read_passphrase(prompt, 0);
992 if (strcmp(passphrase, "") != 0) {
Darren Tucker232b76f2006-05-06 17:41:51 +1000993 private = key_load_private_type(KEY_UNSPEC,
994 filename, passphrase, NULL, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000995 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100996 } else {
997 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000998 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +1100999 }
1000 memset(passphrase, 0, strlen(passphrase));
1001 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001002 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +11001003 break;
1004 debug2("bad passphrase given, try again...");
1005 }
Damien Miller994cf142000-07-21 10:19:44 +10001006 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001007 return private;
1008}
1009
Damien Miller280ecfb2003-05-14 13:46:00 +10001010/*
1011 * try keys in the following order:
1012 * 1. agent keys that are found in the config file
1013 * 2. other agent keys
1014 * 3. keys that are only listed in the config file
1015 */
1016static void
1017pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001018{
Damien Miller280ecfb2003-05-14 13:46:00 +10001019 Identity *id;
1020 Idlist agent, files, *preferred;
1021 Key *key;
1022 AuthenticationConnection *ac;
Damien Millerad833b32000-08-23 10:46:23 +10001023 char *comment;
Damien Miller280ecfb2003-05-14 13:46:00 +10001024 int i, found;
Damien Millerad833b32000-08-23 10:46:23 +10001025
Damien Miller280ecfb2003-05-14 13:46:00 +10001026 TAILQ_INIT(&agent); /* keys from the agent */
1027 TAILQ_INIT(&files); /* keys from the config file */
1028 preferred = &authctxt->keys;
1029 TAILQ_INIT(preferred); /* preferred order of keys */
1030
1031 /* list of keys stored in the filesystem */
1032 for (i = 0; i < options.num_identity_files; i++) {
1033 key = options.identity_keys[i];
1034 if (key && key->type == KEY_RSA1)
1035 continue;
1036 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001037 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001038 id->key = key;
1039 id->filename = xstrdup(options.identity_files[i]);
1040 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001041 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001042 /* list of keys supported by the agent */
1043 if ((ac = ssh_get_authentication_connection())) {
1044 for (key = ssh_get_first_identity(ac, &comment, 2);
1045 key != NULL;
1046 key = ssh_get_next_identity(ac, &comment, 2)) {
1047 found = 0;
1048 TAILQ_FOREACH(id, &files, next) {
Damien Millera8e06ce2003-11-21 23:48:55 +11001049 /* agent keys from the config file are preferred */
Damien Miller280ecfb2003-05-14 13:46:00 +10001050 if (key_equal(key, id->key)) {
1051 key_free(key);
1052 xfree(comment);
1053 TAILQ_REMOVE(&files, id, next);
1054 TAILQ_INSERT_TAIL(preferred, id, next);
1055 id->ac = ac;
1056 found = 1;
1057 break;
1058 }
1059 }
Damien Millerbd394c32004-03-08 23:12:36 +11001060 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001061 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001062 id->key = key;
1063 id->filename = comment;
1064 id->ac = ac;
1065 TAILQ_INSERT_TAIL(&agent, id, next);
1066 }
1067 }
1068 /* append remaining agent keys */
1069 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1070 TAILQ_REMOVE(&agent, id, next);
1071 TAILQ_INSERT_TAIL(preferred, id, next);
1072 }
1073 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +11001074 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001075 /* append remaining keys from the config file */
1076 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1077 TAILQ_REMOVE(&files, id, next);
1078 TAILQ_INSERT_TAIL(preferred, id, next);
1079 }
1080 TAILQ_FOREACH(id, preferred, next) {
1081 debug2("key: %s (%p)", id->filename, id->key);
1082 }
1083}
1084
1085static void
1086pubkey_cleanup(Authctxt *authctxt)
1087{
1088 Identity *id;
1089
1090 if (authctxt->agent != NULL)
1091 ssh_close_authentication_connection(authctxt->agent);
1092 for (id = TAILQ_FIRST(&authctxt->keys); id;
1093 id = TAILQ_FIRST(&authctxt->keys)) {
1094 TAILQ_REMOVE(&authctxt->keys, id, next);
1095 if (id->key)
1096 key_free(id->key);
1097 if (id->filename)
1098 xfree(id->filename);
1099 xfree(id);
1100 }
Damien Millereba71ba2000-04-29 23:57:08 +10001101}
1102
Damien Miller62cee002000-09-23 17:15:56 +11001103int
1104userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001105{
Damien Miller280ecfb2003-05-14 13:46:00 +10001106 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001107 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001108
Damien Miller280ecfb2003-05-14 13:46:00 +10001109 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1110 if (id->tried++)
1111 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001112 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001113 TAILQ_REMOVE(&authctxt->keys, id, next);
1114 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1115 /*
1116 * send a test message if we have the public key. for
1117 * encrypted keys we cannot do this and have to load the
1118 * private key instead
1119 */
1120 if (id->key && id->key->type != KEY_RSA1) {
1121 debug("Offering public key: %s", id->filename);
1122 sent = send_pubkey_test(authctxt, id);
1123 } else if (id->key == NULL) {
1124 debug("Trying private key: %s", id->filename);
1125 id->key = load_identity_file(id->filename);
1126 if (id->key != NULL) {
1127 id->isprivate = 1;
1128 sent = sign_and_send_pubkey(authctxt, id);
1129 key_free(id->key);
1130 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001131 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001132 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001133 if (sent)
1134 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001135 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001136 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001137}
Damien Millereba71ba2000-04-29 23:57:08 +10001138
Damien Miller874d77b2000-10-14 16:23:11 +11001139/*
1140 * Send userauth request message specifying keyboard-interactive method.
1141 */
1142int
1143userauth_kbdint(Authctxt *authctxt)
1144{
1145 static int attempt = 0;
1146
1147 if (attempt++ >= options.number_of_password_prompts)
1148 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001149 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1150 if (attempt > 1 && !authctxt->info_req_seen) {
1151 debug3("userauth_kbdint: disable: no info_req_seen");
1152 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1153 return 0;
1154 }
Damien Miller874d77b2000-10-14 16:23:11 +11001155
1156 debug2("userauth_kbdint");
1157 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1158 packet_put_cstring(authctxt->server_user);
1159 packet_put_cstring(authctxt->service);
1160 packet_put_cstring(authctxt->method->name);
1161 packet_put_cstring(""); /* lang */
1162 packet_put_cstring(options.kbd_interactive_devices ?
1163 options.kbd_interactive_devices : "");
1164 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001165
1166 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1167 return 1;
1168}
1169
1170/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001171 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001172 */
1173void
Damien Miller630d6f42002-01-22 23:17:30 +11001174input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001175{
1176 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001177 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001178 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001179 int echo = 0;
1180
1181 debug2("input_userauth_info_req");
1182
1183 if (authctxt == NULL)
1184 fatal("input_userauth_info_req: no authentication context");
1185
Ben Lindstrom7d199962001-09-12 18:29:00 +00001186 authctxt->info_req_seen = 1;
1187
Damien Miller874d77b2000-10-14 16:23:11 +11001188 name = packet_get_string(NULL);
1189 inst = packet_get_string(NULL);
1190 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001191 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001192 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001193 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001194 logit("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001195 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001196 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001197 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001198
1199 num_prompts = packet_get_int();
1200 /*
1201 * Begin to build info response packet based on prompts requested.
1202 * We commit to providing the correct number of responses, so if
1203 * further on we run into a problem that prevents this, we have to
1204 * be sure and clean this up and send a correct error response.
1205 */
1206 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1207 packet_put_int(num_prompts);
1208
Ben Lindstrom551ea372001-06-05 18:56:16 +00001209 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001210 for (i = 0; i < num_prompts; i++) {
1211 prompt = packet_get_string(NULL);
1212 echo = packet_get_char();
1213
Ben Lindstrom949974b2001-06-25 05:20:31 +00001214 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001215
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001216 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +11001217 memset(response, 0, strlen(response));
1218 xfree(response);
1219 xfree(prompt);
1220 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001221 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001222
Damien Miller9f643902001-11-12 11:02:52 +11001223 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001224 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001225}
Damien Miller62cee002000-09-23 17:15:56 +11001226
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001227static int
Ben Lindstrom5c385522002-06-23 21:23:20 +00001228ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001229 u_char *data, u_int datalen)
1230{
1231 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001232 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001233 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001234 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001235
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001236 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001237
Ben Lindstrom5206b952002-06-06 19:59:29 +00001238 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1239 error("ssh_keysign: no installed: %s", strerror(errno));
1240 return -1;
1241 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001242 if (fflush(stdout) != 0)
1243 error("ssh_keysign: fflush: %s", strerror(errno));
1244 if (pipe(to) < 0) {
1245 error("ssh_keysign: pipe: %s", strerror(errno));
1246 return -1;
1247 }
1248 if (pipe(from) < 0) {
1249 error("ssh_keysign: pipe: %s", strerror(errno));
1250 return -1;
1251 }
1252 if ((pid = fork()) < 0) {
1253 error("ssh_keysign: fork: %s", strerror(errno));
1254 return -1;
1255 }
1256 if (pid == 0) {
Damien Miller2e5fe882006-06-13 13:10:00 +10001257 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001258 close(from[0]);
1259 if (dup2(from[1], STDOUT_FILENO) < 0)
1260 fatal("ssh_keysign: dup2: %s", strerror(errno));
1261 close(to[1]);
1262 if (dup2(to[0], STDIN_FILENO) < 0)
1263 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001264 close(from[1]);
1265 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001266 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001267 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1268 strerror(errno));
1269 }
1270 close(from[1]);
1271 close(to[0]);
1272
1273 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001274 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001275 buffer_put_string(&b, data, datalen);
Damien Miller51bf11f2003-11-17 21:20:47 +11001276 if (ssh_msg_send(to[1], version, &b) == -1)
1277 fatal("ssh_keysign: couldn't send request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001278
Damien Miller901119b2002-10-04 11:10:04 +10001279 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001280 error("ssh_keysign: no reply");
Damien Millerfb1310e2004-01-21 11:02:50 +11001281 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001282 return -1;
1283 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001284 close(from[0]);
1285 close(to[1]);
1286
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001287 while (waitpid(pid, &status, 0) < 0)
1288 if (errno != EINTR)
1289 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001290
Ben Lindstrom5206b952002-06-06 19:59:29 +00001291 if (buffer_get_char(&b) != version) {
1292 error("ssh_keysign: bad version");
Damien Millerfb1310e2004-01-21 11:02:50 +11001293 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001294 return -1;
1295 }
1296 *sigp = buffer_get_string(&b, lenp);
Damien Millerfb1310e2004-01-21 11:02:50 +11001297 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001298
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001299 return 0;
1300}
1301
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001302int
1303userauth_hostbased(Authctxt *authctxt)
1304{
1305 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001306 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001307 Buffer b;
1308 u_char *signature, *blob;
1309 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001310 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001311 u_int blen, slen;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +00001312 int ok, i, len, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001313
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001314 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001315 for (i = 0; i < sensitive->nkeys; i++) {
1316 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001317 if (private && private->type != KEY_RSA1) {
1318 found = 1;
1319 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001320 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001321 break;
1322 }
1323 }
1324 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001325 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001326 return 0;
1327 }
1328 if (key_to_blob(private, &blob, &blen) == 0) {
1329 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001330 return 0;
1331 }
Damien Miller91c18472001-11-12 11:02:03 +11001332 /* figure out a name for the client host */
1333 p = get_local_name(packet_get_connection_in());
1334 if (p == NULL) {
1335 error("userauth_hostbased: cannot get local ipaddr/name");
1336 key_free(private);
Damien Miller5790b592006-03-26 13:54:03 +11001337 xfree(blob);
Damien Miller91c18472001-11-12 11:02:03 +11001338 return 0;
1339 }
1340 len = strlen(p) + 2;
Damien Miller07d86be2006-03-26 14:19:21 +11001341 xasprintf(&chost, "%s.", p);
Damien Miller91c18472001-11-12 11:02:03 +11001342 debug2("userauth_hostbased: chost %s", chost);
Damien Miller00111382003-03-10 11:21:17 +11001343 xfree(p);
Damien Miller91c18472001-11-12 11:02:03 +11001344
Ben Lindstrom671388f2001-04-19 20:40:45 +00001345 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1346 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001347 pkalg = xstrdup(key_ssh_name(private));
1348 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001349 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001350 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001351 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1352 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001353 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001354 buffer_put_cstring(&b, authctxt->method->name);
1355 buffer_put_cstring(&b, pkalg);
1356 buffer_put_string(&b, blob, blen);
1357 buffer_put_cstring(&b, chost);
1358 buffer_put_cstring(&b, authctxt->local_user);
1359#ifdef DEBUG_PK
1360 buffer_dump(&b);
1361#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001362 if (sensitive->external_keysign)
1363 ok = ssh_keysign(private, &signature, &slen,
1364 buffer_ptr(&b), buffer_len(&b));
1365 else
1366 ok = key_sign(private, &signature, &slen,
1367 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001368 key_free(private);
1369 buffer_free(&b);
1370 if (ok != 0) {
1371 error("key_sign failed");
1372 xfree(chost);
1373 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001374 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001375 return 0;
1376 }
1377 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1378 packet_put_cstring(authctxt->server_user);
1379 packet_put_cstring(authctxt->service);
1380 packet_put_cstring(authctxt->method->name);
1381 packet_put_cstring(pkalg);
1382 packet_put_string(blob, blen);
1383 packet_put_cstring(chost);
1384 packet_put_cstring(authctxt->local_user);
1385 packet_put_string(signature, slen);
1386 memset(signature, 's', slen);
1387 xfree(signature);
1388 xfree(chost);
1389 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001390 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001391
1392 packet_send();
1393 return 1;
1394}
1395
Damien Miller62cee002000-09-23 17:15:56 +11001396/* find auth method */
1397
Damien Miller62cee002000-09-23 17:15:56 +11001398/*
1399 * given auth method name, if configurable options permit this method fill
1400 * in auth_ident field and return true, otherwise return false.
1401 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001402static int
Damien Miller62cee002000-09-23 17:15:56 +11001403authmethod_is_enabled(Authmethod *method)
1404{
1405 if (method == NULL)
1406 return 0;
1407 /* return false if options indicate this method is disabled */
1408 if (method->enabled == NULL || *method->enabled == 0)
1409 return 0;
1410 /* return false if batch mode is enabled but method needs interactive mode */
1411 if (method->batch_flag != NULL && *method->batch_flag != 0)
1412 return 0;
1413 return 1;
1414}
1415
Ben Lindstrombba81212001-06-25 05:01:22 +00001416static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001417authmethod_lookup(const char *name)
1418{
1419 Authmethod *method = NULL;
1420 if (name != NULL)
1421 for (method = authmethods; method->name != NULL; method++)
1422 if (strcmp(name, method->name) == 0)
1423 return method;
1424 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1425 return NULL;
1426}
1427
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001428/* XXX internal state */
1429static Authmethod *current = NULL;
1430static char *supported = NULL;
1431static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001432
Damien Miller62cee002000-09-23 17:15:56 +11001433/*
1434 * Given the authentication method list sent by the server, return the
1435 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001436 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001437 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001438static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001439authmethod_get(char *authlist)
1440{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001441 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001442 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001443
Damien Miller62cee002000-09-23 17:15:56 +11001444 /* Use a suitable default if we're passed a nil list. */
1445 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001446 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001447
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001448 if (supported == NULL || strcmp(authlist, supported) != 0) {
1449 debug3("start over, passed a different list %s", authlist);
1450 if (supported != NULL)
1451 xfree(supported);
1452 supported = xstrdup(authlist);
1453 preferred = options.preferred_authentications;
1454 debug3("preferred %s", preferred);
1455 current = NULL;
1456 } else if (current != NULL && authmethod_is_enabled(current))
1457 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001458
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001459 for (;;) {
1460 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001461 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001462 current = NULL;
1463 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001464 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001465 preferred += next;
1466 debug3("authmethod_lookup %s", name);
1467 debug3("remaining preferred: %s", preferred);
1468 if ((current = authmethod_lookup(name)) != NULL &&
1469 authmethod_is_enabled(current)) {
1470 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001471 debug("Next authentication method: %s", name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001472 return current;
1473 }
Damien Millereba71ba2000-04-29 23:57:08 +10001474 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001475}
Damien Miller62cee002000-09-23 17:15:56 +11001476
Ben Lindstrom79073822001-07-04 03:42:30 +00001477static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001478authmethods_get(void)
1479{
1480 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001481 Buffer b;
1482 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001483
Damien Miller0e3b8722002-01-22 23:26:38 +11001484 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001485 for (method = authmethods; method->name != NULL; method++) {
1486 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001487 if (buffer_len(&b) > 0)
1488 buffer_append(&b, ",", 1);
1489 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001490 }
Damien Miller62cee002000-09-23 17:15:56 +11001491 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001492 buffer_append(&b, "\0", 1);
1493 list = xstrdup(buffer_ptr(&b));
1494 buffer_free(&b);
1495 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001496}