blob: e58d078c4ac23c321d6b84a91f0ac0c2252a8e34 [file] [log] [blame]
Damien Millerd7834352006-08-05 12:39:39 +10001/* $OpenBSD: sshconnect2.c,v 1.160 2006/08/03 03:34:42 deraadt 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 if (gssctxt)
512 ssh_gssapi_delete_ctx(&gssctxt);
513 ssh_gssapi_build_ctx(&gssctxt);
Darren Tucker56afe142003-11-03 20:06:14 +1100514 ssh_gssapi_set_oid(gssctxt, &gss_supported->elements[mech]);
Darren Tucker0efd1552003-08-26 11:49:55 +1000515
516 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100517 if (gss_supported->elements[mech].length < 128 &&
Darren Tucker0efd1552003-08-26 11:49:55 +1000518 !GSS_ERROR(ssh_gssapi_import_name(gssctxt,
519 authctxt->host))) {
520 ok = 1; /* Mechanism works */
521 } else {
522 mech++;
523 }
524 }
525
Damien Miller5790b592006-03-26 13:54:03 +1100526 if (!ok) {
527 ssh_gssapi_delete_ctx(&gssctxt);
Damien Millereccb9de2005-06-17 12:59:34 +1000528 return 0;
Damien Miller5790b592006-03-26 13:54:03 +1100529 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000530
531 authctxt->methoddata=(void *)gssctxt;
532
533 packet_start(SSH2_MSG_USERAUTH_REQUEST);
534 packet_put_cstring(authctxt->server_user);
535 packet_put_cstring(authctxt->service);
536 packet_put_cstring(authctxt->method->name);
537
538 packet_put_int(1);
539
Darren Tucker655a5e02003-11-03 20:09:03 +1100540 packet_put_int((gss_supported->elements[mech].length) + 2);
541 packet_put_char(SSH_GSS_OIDTYPE);
542 packet_put_char(gss_supported->elements[mech].length);
543 packet_put_raw(gss_supported->elements[mech].elements,
544 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000545
546 packet_send();
547
548 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
549 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
550 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
551 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
552
553 mech++; /* Move along to next candidate */
554
555 return 1;
556}
557
Damien Miller91c6aa42003-11-17 21:20:18 +1100558static OM_uint32
559process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
560{
561 Authctxt *authctxt = ctxt;
562 Gssctxt *gssctxt = authctxt->methoddata;
563 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000564 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
565 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100566 OM_uint32 status, ms, flags;
567 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100568
Damien Miller91c6aa42003-11-17 21:20:18 +1100569 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100570 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100571
572 if (send_tok.length > 0) {
573 if (GSS_ERROR(status))
574 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
575 else
576 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100577
Damien Miller91c6aa42003-11-17 21:20:18 +1100578 packet_put_string(send_tok.value, send_tok.length);
579 packet_send();
580 gss_release_buffer(&ms, &send_tok);
581 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100582
Damien Miller91c6aa42003-11-17 21:20:18 +1100583 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100584 /* send either complete or MIC, depending on mechanism */
585 if (!(flags & GSS_C_INTEG_FLAG)) {
586 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
587 packet_send();
588 } else {
589 ssh_gssapi_buildmic(&b, authctxt->server_user,
590 authctxt->service, "gssapi-with-mic");
591
592 gssbuf.value = buffer_ptr(&b);
593 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100594
Damien Miller0425d402003-11-17 22:18:21 +1100595 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100596
Damien Miller0425d402003-11-17 22:18:21 +1100597 if (!GSS_ERROR(status)) {
598 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
599 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100600
Damien Miller0425d402003-11-17 22:18:21 +1100601 packet_send();
602 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100603
Damien Miller0425d402003-11-17 22:18:21 +1100604 buffer_free(&b);
605 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100606 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100607 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100608
Damien Miller91c6aa42003-11-17 21:20:18 +1100609 return status;
610}
611
Darren Tucker0efd1552003-08-26 11:49:55 +1000612void
613input_gssapi_response(int type, u_int32_t plen, void *ctxt)
614{
615 Authctxt *authctxt = ctxt;
616 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000617 int oidlen;
618 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000619
620 if (authctxt == NULL)
621 fatal("input_gssapi_response: no authentication context");
622 gssctxt = authctxt->methoddata;
623
624 /* Setup our OID */
625 oidv = packet_get_string(&oidlen);
626
Darren Tucker655a5e02003-11-03 20:09:03 +1100627 if (oidlen <= 2 ||
628 oidv[0] != SSH_GSS_OIDTYPE ||
629 oidv[1] != oidlen - 2) {
Damien Miller91c6aa42003-11-17 21:20:18 +1100630 xfree(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100631 debug("Badly encoded mechanism OID received");
632 userauth(authctxt, NULL);
Darren Tucker655a5e02003-11-03 20:09:03 +1100633 return;
Darren Tucker0efd1552003-08-26 11:49:55 +1000634 }
635
Darren Tucker655a5e02003-11-03 20:09:03 +1100636 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
637 fatal("Server returned different OID than expected");
638
Darren Tucker0efd1552003-08-26 11:49:55 +1000639 packet_check_eom();
640
641 xfree(oidv);
642
Damien Miller91c6aa42003-11-17 21:20:18 +1100643 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000644 /* Start again with next method on list */
645 debug("Trying to start again");
646 userauth(authctxt, NULL);
647 return;
648 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000649}
650
651void
652input_gssapi_token(int type, u_int32_t plen, void *ctxt)
653{
654 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000655 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100656 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000657 u_int slen;
658
659 if (authctxt == NULL)
660 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000661
662 recv_tok.value = packet_get_string(&slen);
663 recv_tok.length = slen; /* safe typecast */
664
665 packet_check_eom();
666
Damien Miller91c6aa42003-11-17 21:20:18 +1100667 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000668
669 xfree(recv_tok.value);
670
671 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000672 /* Start again with the next method in the list */
673 userauth(authctxt, NULL);
674 return;
675 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000676}
677
678void
679input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
680{
681 Authctxt *authctxt = ctxt;
682 Gssctxt *gssctxt;
683 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
684 gss_buffer_desc recv_tok;
685 OM_uint32 status, ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000686 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000687
688 if (authctxt == NULL)
689 fatal("input_gssapi_response: no authentication context");
690 gssctxt = authctxt->methoddata;
691
Darren Tucker600ad8d2003-08-26 12:10:48 +1000692 recv_tok.value = packet_get_string(&len);
693 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000694
695 packet_check_eom();
696
697 /* Stick it into GSSAPI and see what it says */
698 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000699 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000700
701 xfree(recv_tok.value);
702 gss_release_buffer(&ms, &send_tok);
703
704 /* Server will be returning a failed packet after this one */
705}
706
707void
708input_gssapi_error(int type, u_int32_t plen, void *ctxt)
709{
710 OM_uint32 maj, min;
711 char *msg;
712 char *lang;
713
714 maj=packet_get_int();
715 min=packet_get_int();
716 msg=packet_get_string(NULL);
717 lang=packet_get_string(NULL);
718
719 packet_check_eom();
720
Damien Miller15d72a02005-11-05 15:07:33 +1100721 debug("Server GSSAPI Error:\n%s", msg);
Darren Tucker0efd1552003-08-26 11:49:55 +1000722 xfree(msg);
723 xfree(lang);
724}
725#endif /* GSSAPI */
726
Damien Millereba71ba2000-04-29 23:57:08 +1000727int
Damien Miller874d77b2000-10-14 16:23:11 +1100728userauth_none(Authctxt *authctxt)
729{
730 /* initial userauth request */
731 packet_start(SSH2_MSG_USERAUTH_REQUEST);
732 packet_put_cstring(authctxt->server_user);
733 packet_put_cstring(authctxt->service);
734 packet_put_cstring(authctxt->method->name);
735 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100736 return 1;
737}
738
739int
Damien Miller62cee002000-09-23 17:15:56 +1100740userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000741{
Damien Millere247cc42000-05-07 12:03:14 +1000742 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000743 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000744 char *password;
745
Damien Millerd3a18572000-06-07 19:55:44 +1000746 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000747 return 0;
748
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000749 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000750 error("Permission denied, please try again.");
751
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000752 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100753 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000754 password = read_passphrase(prompt, 0);
755 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100756 packet_put_cstring(authctxt->server_user);
757 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100758 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000759 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000760 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000761 memset(password, 0, strlen(password));
762 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100763 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000764 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000765
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000766 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000767 &input_userauth_passwd_changereq);
768
Damien Millereba71ba2000-04-29 23:57:08 +1000769 return 1;
770}
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000771/*
772 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
773 */
774void
Tim Ricec8549622002-03-31 12:49:38 -0800775input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000776{
777 Authctxt *authctxt = ctxt;
778 char *info, *lang, *password = NULL, *retype = NULL;
779 char prompt[150];
780
781 debug2("input_userauth_passwd_changereq");
782
783 if (authctxt == NULL)
784 fatal("input_userauth_passwd_changereq: "
785 "no authentication context");
786
787 info = packet_get_string(NULL);
788 lang = packet_get_string(NULL);
789 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000790 logit("%s", info);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000791 xfree(info);
792 xfree(lang);
793 packet_start(SSH2_MSG_USERAUTH_REQUEST);
794 packet_put_cstring(authctxt->server_user);
795 packet_put_cstring(authctxt->service);
796 packet_put_cstring(authctxt->method->name);
797 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000798 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000799 "Enter %.30s@%.128s's old password: ",
800 authctxt->server_user, authctxt->host);
801 password = read_passphrase(prompt, 0);
802 packet_put_cstring(password);
803 memset(password, 0, strlen(password));
804 xfree(password);
805 password = NULL;
806 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000807 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000808 "Enter %.30s@%.128s's new password: ",
809 authctxt->server_user, authctxt->host);
810 password = read_passphrase(prompt, RP_ALLOW_EOF);
811 if (password == NULL) {
812 /* bail out */
813 return;
814 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000815 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000816 "Retype %.30s@%.128s's new password: ",
817 authctxt->server_user, authctxt->host);
818 retype = read_passphrase(prompt, 0);
819 if (strcmp(password, retype) != 0) {
820 memset(password, 0, strlen(password));
821 xfree(password);
Damien Miller996acd22003-04-09 20:59:48 +1000822 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000823 password = NULL;
824 }
825 memset(retype, 0, strlen(retype));
826 xfree(retype);
827 }
828 packet_put_cstring(password);
829 memset(password, 0, strlen(password));
830 xfree(password);
831 packet_add_padding(64);
832 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000833
834 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000835 &input_userauth_passwd_changereq);
836}
Damien Millereba71ba2000-04-29 23:57:08 +1000837
Ben Lindstrombba81212001-06-25 05:01:22 +0000838static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000839identity_sign(Identity *id, u_char **sigp, u_int *lenp,
840 u_char *data, u_int datalen)
841{
842 Key *prv;
843 int ret;
844
845 /* the agent supports this key */
846 if (id->ac)
847 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
848 data, datalen));
849 /*
850 * we have already loaded the private key or
851 * the private key is stored in external hardware
852 */
853 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
854 return (key_sign(id->key, sigp, lenp, data, datalen));
855 /* load the private key from the file */
856 if ((prv = load_identity_file(id->filename)) == NULL)
857 return (-1);
858 ret = key_sign(prv, sigp, lenp, data, datalen);
859 key_free(prv);
860 return (ret);
861}
862
863static int
864sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +1000865{
866 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000867 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000868 u_int bloblen, slen;
Darren Tucker502d3842003-06-28 12:38:01 +1000869 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000870 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100871 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000872
Ben Lindstromd121f612000-12-03 17:00:47 +0000873 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000874
Damien Miller280ecfb2003-05-14 13:46:00 +1000875 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100876 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000877 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100878 return 0;
879 }
Damien Millereba71ba2000-04-29 23:57:08 +1000880 /* data to be signed */
881 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100882 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000883 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000884 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100885 } else {
886 buffer_put_string(&b, session_id2, session_id2_len);
887 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000888 }
Damien Millereba71ba2000-04-29 23:57:08 +1000889 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100890 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000891 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000892 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000893 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100894 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000895 if (datafellows & SSH_BUG_PKAUTH) {
896 buffer_put_char(&b, have_sig);
897 } else {
898 buffer_put_cstring(&b, authctxt->method->name);
899 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +1000900 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +0000901 }
Damien Millereba71ba2000-04-29 23:57:08 +1000902 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000903
904 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +1000905 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000906 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000907 if (ret == -1) {
908 xfree(blob);
909 buffer_free(&b);
910 return 0;
911 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100912#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000913 buffer_dump(&b);
914#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000915 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000916 buffer_clear(&b);
917 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000918 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000919 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100920 buffer_put_cstring(&b, authctxt->server_user);
921 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100922 buffer_put_cstring(&b, authctxt->method->name);
923 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000924 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +1000925 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +1000926 buffer_put_string(&b, blob, bloblen);
927 }
928 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000929
Damien Millereba71ba2000-04-29 23:57:08 +1000930 /* append signature */
931 buffer_put_string(&b, signature, slen);
932 xfree(signature);
933
934 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000935 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100936 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000937 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000938
939 /* put remaining data from buffer into packet */
940 packet_start(SSH2_MSG_USERAUTH_REQUEST);
941 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
942 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +1000943 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000944
945 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000946}
947
Ben Lindstrombba81212001-06-25 05:01:22 +0000948static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000949send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +1000950{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000951 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000952 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000953
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000954 debug3("send_pubkey_test");
955
Damien Miller280ecfb2003-05-14 13:46:00 +1000956 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000957 /* we cannot handle this key */
958 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +1000959 return 0;
960 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000961 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000962 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +1000963
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000964 packet_start(SSH2_MSG_USERAUTH_REQUEST);
965 packet_put_cstring(authctxt->server_user);
966 packet_put_cstring(authctxt->service);
967 packet_put_cstring(authctxt->method->name);
968 packet_put_char(have_sig);
969 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +1000970 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000971 packet_put_string(blob, bloblen);
972 xfree(blob);
973 packet_send();
974 return 1;
975}
976
Ben Lindstrombba81212001-06-25 05:01:22 +0000977static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000978load_identity_file(char *filename)
979{
980 Key *private;
981 char prompt[300], *passphrase;
Darren Tucker232b76f2006-05-06 17:41:51 +1000982 int perm_ok, quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000983 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000984
Ben Lindstrom329782e2001-03-10 17:08:59 +0000985 if (stat(filename, &st) < 0) {
986 debug3("no such identity: %s", filename);
987 return NULL;
988 }
Darren Tucker232b76f2006-05-06 17:41:51 +1000989 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
990 if (!perm_ok)
991 return NULL;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000992 if (private == NULL) {
993 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000994 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000995 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100996 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100997 for (i = 0; i < options.number_of_password_prompts; i++) {
998 passphrase = read_passphrase(prompt, 0);
999 if (strcmp(passphrase, "") != 0) {
Darren Tucker232b76f2006-05-06 17:41:51 +10001000 private = key_load_private_type(KEY_UNSPEC,
1001 filename, passphrase, NULL, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001002 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001003 } else {
1004 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001005 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +11001006 }
1007 memset(passphrase, 0, strlen(passphrase));
1008 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001009 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +11001010 break;
1011 debug2("bad passphrase given, try again...");
1012 }
Damien Miller994cf142000-07-21 10:19:44 +10001013 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001014 return private;
1015}
1016
Damien Miller280ecfb2003-05-14 13:46:00 +10001017/*
1018 * try keys in the following order:
1019 * 1. agent keys that are found in the config file
1020 * 2. other agent keys
1021 * 3. keys that are only listed in the config file
1022 */
1023static void
1024pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001025{
Damien Miller280ecfb2003-05-14 13:46:00 +10001026 Identity *id;
1027 Idlist agent, files, *preferred;
1028 Key *key;
1029 AuthenticationConnection *ac;
Damien Millerad833b32000-08-23 10:46:23 +10001030 char *comment;
Damien Miller280ecfb2003-05-14 13:46:00 +10001031 int i, found;
Damien Millerad833b32000-08-23 10:46:23 +10001032
Damien Miller280ecfb2003-05-14 13:46:00 +10001033 TAILQ_INIT(&agent); /* keys from the agent */
1034 TAILQ_INIT(&files); /* keys from the config file */
1035 preferred = &authctxt->keys;
1036 TAILQ_INIT(preferred); /* preferred order of keys */
1037
1038 /* list of keys stored in the filesystem */
1039 for (i = 0; i < options.num_identity_files; i++) {
1040 key = options.identity_keys[i];
1041 if (key && key->type == KEY_RSA1)
1042 continue;
1043 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001044 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001045 id->key = key;
1046 id->filename = xstrdup(options.identity_files[i]);
1047 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001048 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001049 /* list of keys supported by the agent */
1050 if ((ac = ssh_get_authentication_connection())) {
1051 for (key = ssh_get_first_identity(ac, &comment, 2);
1052 key != NULL;
1053 key = ssh_get_next_identity(ac, &comment, 2)) {
1054 found = 0;
1055 TAILQ_FOREACH(id, &files, next) {
Damien Millera8e06ce2003-11-21 23:48:55 +11001056 /* agent keys from the config file are preferred */
Damien Miller280ecfb2003-05-14 13:46:00 +10001057 if (key_equal(key, id->key)) {
1058 key_free(key);
1059 xfree(comment);
1060 TAILQ_REMOVE(&files, id, next);
1061 TAILQ_INSERT_TAIL(preferred, id, next);
1062 id->ac = ac;
1063 found = 1;
1064 break;
1065 }
1066 }
Damien Millerbd394c32004-03-08 23:12:36 +11001067 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001068 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001069 id->key = key;
1070 id->filename = comment;
1071 id->ac = ac;
1072 TAILQ_INSERT_TAIL(&agent, id, next);
1073 }
1074 }
1075 /* append remaining agent keys */
1076 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1077 TAILQ_REMOVE(&agent, id, next);
1078 TAILQ_INSERT_TAIL(preferred, id, next);
1079 }
1080 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +11001081 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001082 /* append remaining keys from the config file */
1083 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1084 TAILQ_REMOVE(&files, id, next);
1085 TAILQ_INSERT_TAIL(preferred, id, next);
1086 }
1087 TAILQ_FOREACH(id, preferred, next) {
1088 debug2("key: %s (%p)", id->filename, id->key);
1089 }
1090}
1091
1092static void
1093pubkey_cleanup(Authctxt *authctxt)
1094{
1095 Identity *id;
1096
1097 if (authctxt->agent != NULL)
1098 ssh_close_authentication_connection(authctxt->agent);
1099 for (id = TAILQ_FIRST(&authctxt->keys); id;
1100 id = TAILQ_FIRST(&authctxt->keys)) {
1101 TAILQ_REMOVE(&authctxt->keys, id, next);
1102 if (id->key)
1103 key_free(id->key);
1104 if (id->filename)
1105 xfree(id->filename);
1106 xfree(id);
1107 }
Damien Millereba71ba2000-04-29 23:57:08 +10001108}
1109
Damien Miller62cee002000-09-23 17:15:56 +11001110int
1111userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001112{
Damien Miller280ecfb2003-05-14 13:46:00 +10001113 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001114 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001115
Damien Miller280ecfb2003-05-14 13:46:00 +10001116 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1117 if (id->tried++)
1118 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001119 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001120 TAILQ_REMOVE(&authctxt->keys, id, next);
1121 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1122 /*
1123 * send a test message if we have the public key. for
1124 * encrypted keys we cannot do this and have to load the
1125 * private key instead
1126 */
1127 if (id->key && id->key->type != KEY_RSA1) {
1128 debug("Offering public key: %s", id->filename);
1129 sent = send_pubkey_test(authctxt, id);
1130 } else if (id->key == NULL) {
1131 debug("Trying private key: %s", id->filename);
1132 id->key = load_identity_file(id->filename);
1133 if (id->key != NULL) {
1134 id->isprivate = 1;
1135 sent = sign_and_send_pubkey(authctxt, id);
1136 key_free(id->key);
1137 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001138 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001139 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001140 if (sent)
1141 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001142 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001143 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001144}
Damien Millereba71ba2000-04-29 23:57:08 +10001145
Damien Miller874d77b2000-10-14 16:23:11 +11001146/*
1147 * Send userauth request message specifying keyboard-interactive method.
1148 */
1149int
1150userauth_kbdint(Authctxt *authctxt)
1151{
1152 static int attempt = 0;
1153
1154 if (attempt++ >= options.number_of_password_prompts)
1155 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001156 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1157 if (attempt > 1 && !authctxt->info_req_seen) {
1158 debug3("userauth_kbdint: disable: no info_req_seen");
1159 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1160 return 0;
1161 }
Damien Miller874d77b2000-10-14 16:23:11 +11001162
1163 debug2("userauth_kbdint");
1164 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1165 packet_put_cstring(authctxt->server_user);
1166 packet_put_cstring(authctxt->service);
1167 packet_put_cstring(authctxt->method->name);
1168 packet_put_cstring(""); /* lang */
1169 packet_put_cstring(options.kbd_interactive_devices ?
1170 options.kbd_interactive_devices : "");
1171 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001172
1173 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1174 return 1;
1175}
1176
1177/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001178 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001179 */
1180void
Damien Miller630d6f42002-01-22 23:17:30 +11001181input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001182{
1183 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001184 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001185 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001186 int echo = 0;
1187
1188 debug2("input_userauth_info_req");
1189
1190 if (authctxt == NULL)
1191 fatal("input_userauth_info_req: no authentication context");
1192
Ben Lindstrom7d199962001-09-12 18:29:00 +00001193 authctxt->info_req_seen = 1;
1194
Damien Miller874d77b2000-10-14 16:23:11 +11001195 name = packet_get_string(NULL);
1196 inst = packet_get_string(NULL);
1197 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001198 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001199 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001200 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001201 logit("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001202 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +11001203 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001204 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001205
1206 num_prompts = packet_get_int();
1207 /*
1208 * Begin to build info response packet based on prompts requested.
1209 * We commit to providing the correct number of responses, so if
1210 * further on we run into a problem that prevents this, we have to
1211 * be sure and clean this up and send a correct error response.
1212 */
1213 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1214 packet_put_int(num_prompts);
1215
Ben Lindstrom551ea372001-06-05 18:56:16 +00001216 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001217 for (i = 0; i < num_prompts; i++) {
1218 prompt = packet_get_string(NULL);
1219 echo = packet_get_char();
1220
Ben Lindstrom949974b2001-06-25 05:20:31 +00001221 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001222
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001223 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +11001224 memset(response, 0, strlen(response));
1225 xfree(response);
1226 xfree(prompt);
1227 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001228 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001229
Damien Miller9f643902001-11-12 11:02:52 +11001230 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001231 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001232}
Damien Miller62cee002000-09-23 17:15:56 +11001233
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001234static int
Ben Lindstrom5c385522002-06-23 21:23:20 +00001235ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001236 u_char *data, u_int datalen)
1237{
1238 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001239 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001240 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001241 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001242
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001243 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001244
Ben Lindstrom5206b952002-06-06 19:59:29 +00001245 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1246 error("ssh_keysign: no installed: %s", strerror(errno));
1247 return -1;
1248 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001249 if (fflush(stdout) != 0)
1250 error("ssh_keysign: fflush: %s", strerror(errno));
1251 if (pipe(to) < 0) {
1252 error("ssh_keysign: pipe: %s", strerror(errno));
1253 return -1;
1254 }
1255 if (pipe(from) < 0) {
1256 error("ssh_keysign: pipe: %s", strerror(errno));
1257 return -1;
1258 }
1259 if ((pid = fork()) < 0) {
1260 error("ssh_keysign: fork: %s", strerror(errno));
1261 return -1;
1262 }
1263 if (pid == 0) {
Damien Miller2e5fe882006-06-13 13:10:00 +10001264 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001265 close(from[0]);
1266 if (dup2(from[1], STDOUT_FILENO) < 0)
1267 fatal("ssh_keysign: dup2: %s", strerror(errno));
1268 close(to[1]);
1269 if (dup2(to[0], STDIN_FILENO) < 0)
1270 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001271 close(from[1]);
1272 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001273 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001274 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1275 strerror(errno));
1276 }
1277 close(from[1]);
1278 close(to[0]);
1279
1280 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001281 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001282 buffer_put_string(&b, data, datalen);
Damien Miller51bf11f2003-11-17 21:20:47 +11001283 if (ssh_msg_send(to[1], version, &b) == -1)
1284 fatal("ssh_keysign: couldn't send request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001285
Damien Miller901119b2002-10-04 11:10:04 +10001286 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001287 error("ssh_keysign: no reply");
Damien Millerfb1310e2004-01-21 11:02:50 +11001288 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001289 return -1;
1290 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001291 close(from[0]);
1292 close(to[1]);
1293
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001294 while (waitpid(pid, &status, 0) < 0)
1295 if (errno != EINTR)
1296 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001297
Ben Lindstrom5206b952002-06-06 19:59:29 +00001298 if (buffer_get_char(&b) != version) {
1299 error("ssh_keysign: bad version");
Damien Millerfb1310e2004-01-21 11:02:50 +11001300 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001301 return -1;
1302 }
1303 *sigp = buffer_get_string(&b, lenp);
Damien Millerfb1310e2004-01-21 11:02:50 +11001304 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001305
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001306 return 0;
1307}
1308
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001309int
1310userauth_hostbased(Authctxt *authctxt)
1311{
1312 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001313 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001314 Buffer b;
1315 u_char *signature, *blob;
1316 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001317 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001318 u_int blen, slen;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +00001319 int ok, i, len, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001320
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001321 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001322 for (i = 0; i < sensitive->nkeys; i++) {
1323 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001324 if (private && private->type != KEY_RSA1) {
1325 found = 1;
1326 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001327 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001328 break;
1329 }
1330 }
1331 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001332 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001333 return 0;
1334 }
1335 if (key_to_blob(private, &blob, &blen) == 0) {
1336 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001337 return 0;
1338 }
Damien Miller91c18472001-11-12 11:02:03 +11001339 /* figure out a name for the client host */
1340 p = get_local_name(packet_get_connection_in());
1341 if (p == NULL) {
1342 error("userauth_hostbased: cannot get local ipaddr/name");
1343 key_free(private);
Damien Miller5790b592006-03-26 13:54:03 +11001344 xfree(blob);
Damien Miller91c18472001-11-12 11:02:03 +11001345 return 0;
1346 }
1347 len = strlen(p) + 2;
Damien Miller07d86be2006-03-26 14:19:21 +11001348 xasprintf(&chost, "%s.", p);
Damien Miller91c18472001-11-12 11:02:03 +11001349 debug2("userauth_hostbased: chost %s", chost);
Damien Miller00111382003-03-10 11:21:17 +11001350 xfree(p);
Damien Miller91c18472001-11-12 11:02:03 +11001351
Ben Lindstrom671388f2001-04-19 20:40:45 +00001352 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1353 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001354 pkalg = xstrdup(key_ssh_name(private));
1355 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001356 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001357 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001358 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1359 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001360 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001361 buffer_put_cstring(&b, authctxt->method->name);
1362 buffer_put_cstring(&b, pkalg);
1363 buffer_put_string(&b, blob, blen);
1364 buffer_put_cstring(&b, chost);
1365 buffer_put_cstring(&b, authctxt->local_user);
1366#ifdef DEBUG_PK
1367 buffer_dump(&b);
1368#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001369 if (sensitive->external_keysign)
1370 ok = ssh_keysign(private, &signature, &slen,
1371 buffer_ptr(&b), buffer_len(&b));
1372 else
1373 ok = key_sign(private, &signature, &slen,
1374 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001375 key_free(private);
1376 buffer_free(&b);
1377 if (ok != 0) {
1378 error("key_sign failed");
1379 xfree(chost);
1380 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001381 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001382 return 0;
1383 }
1384 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1385 packet_put_cstring(authctxt->server_user);
1386 packet_put_cstring(authctxt->service);
1387 packet_put_cstring(authctxt->method->name);
1388 packet_put_cstring(pkalg);
1389 packet_put_string(blob, blen);
1390 packet_put_cstring(chost);
1391 packet_put_cstring(authctxt->local_user);
1392 packet_put_string(signature, slen);
1393 memset(signature, 's', slen);
1394 xfree(signature);
1395 xfree(chost);
1396 xfree(pkalg);
Damien Miller5790b592006-03-26 13:54:03 +11001397 xfree(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001398
1399 packet_send();
1400 return 1;
1401}
1402
Damien Miller62cee002000-09-23 17:15:56 +11001403/* find auth method */
1404
Damien Miller62cee002000-09-23 17:15:56 +11001405/*
1406 * given auth method name, if configurable options permit this method fill
1407 * in auth_ident field and return true, otherwise return false.
1408 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001409static int
Damien Miller62cee002000-09-23 17:15:56 +11001410authmethod_is_enabled(Authmethod *method)
1411{
1412 if (method == NULL)
1413 return 0;
1414 /* return false if options indicate this method is disabled */
1415 if (method->enabled == NULL || *method->enabled == 0)
1416 return 0;
1417 /* return false if batch mode is enabled but method needs interactive mode */
1418 if (method->batch_flag != NULL && *method->batch_flag != 0)
1419 return 0;
1420 return 1;
1421}
1422
Ben Lindstrombba81212001-06-25 05:01:22 +00001423static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001424authmethod_lookup(const char *name)
1425{
1426 Authmethod *method = NULL;
1427 if (name != NULL)
1428 for (method = authmethods; method->name != NULL; method++)
1429 if (strcmp(name, method->name) == 0)
1430 return method;
1431 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1432 return NULL;
1433}
1434
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001435/* XXX internal state */
1436static Authmethod *current = NULL;
1437static char *supported = NULL;
1438static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001439
Damien Miller62cee002000-09-23 17:15:56 +11001440/*
1441 * Given the authentication method list sent by the server, return the
1442 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001443 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001444 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001445static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001446authmethod_get(char *authlist)
1447{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001448 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001449 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001450
Damien Miller62cee002000-09-23 17:15:56 +11001451 /* Use a suitable default if we're passed a nil list. */
1452 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001453 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001454
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001455 if (supported == NULL || strcmp(authlist, supported) != 0) {
1456 debug3("start over, passed a different list %s", authlist);
1457 if (supported != NULL)
1458 xfree(supported);
1459 supported = xstrdup(authlist);
1460 preferred = options.preferred_authentications;
1461 debug3("preferred %s", preferred);
1462 current = NULL;
1463 } else if (current != NULL && authmethod_is_enabled(current))
1464 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001465
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001466 for (;;) {
1467 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001468 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001469 current = NULL;
1470 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001471 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001472 preferred += next;
1473 debug3("authmethod_lookup %s", name);
1474 debug3("remaining preferred: %s", preferred);
1475 if ((current = authmethod_lookup(name)) != NULL &&
1476 authmethod_is_enabled(current)) {
1477 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001478 debug("Next authentication method: %s", name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001479 return current;
1480 }
Damien Millereba71ba2000-04-29 23:57:08 +10001481 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001482}
Damien Miller62cee002000-09-23 17:15:56 +11001483
Ben Lindstrom79073822001-07-04 03:42:30 +00001484static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001485authmethods_get(void)
1486{
1487 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001488 Buffer b;
1489 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001490
Damien Miller0e3b8722002-01-22 23:26:38 +11001491 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001492 for (method = authmethods; method->name != NULL; method++) {
1493 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001494 if (buffer_len(&b) > 0)
1495 buffer_append(&b, ",", 1);
1496 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001497 }
Damien Miller62cee002000-09-23 17:15:56 +11001498 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001499 buffer_append(&b, "\0", 1);
1500 list = xstrdup(buffer_ptr(&b));
1501 buffer_free(&b);
1502 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001503}