blob: 0d339b9c5c347b25ed7b5e763560f3f14c19ac57 [file] [log] [blame]
Damien Miller324541e2013-12-31 12:25:40 +11001/* $OpenBSD: sshconnect2.c,v 1.200 2013/12/30 23:52:28 djm Exp $ */
Damien Millereba71ba2000-04-29 23:57:08 +10002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Miller01ed2272008-11-05 16:20:46 +11004 * Copyright (c) 2008 Damien Miller. All rights reserved.
Damien Millereba71ba2000-04-29 23:57:08 +10005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100014 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "includes.h"
Damien Miller2eb63402006-03-15 11:09:42 +110028
Damien Miller9cf6d072006-03-15 11:29:24 +110029#include <sys/types.h>
Damien Millerd7834352006-08-05 12:39:39 +100030#include <sys/socket.h>
Damien Miller9cf6d072006-03-15 11:29:24 +110031#include <sys/wait.h>
Damien Millerf17883e2006-03-15 11:45:54 +110032#include <sys/stat.h>
Damien Millereba71ba2000-04-29 23:57:08 +100033
Darren Tucker39972492006-07-12 22:22:46 +100034#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110035#include <fcntl.h>
Darren Tuckerf520ea12007-05-20 15:11:33 +100036#include <netdb.h>
Damien Millerd7834352006-08-05 12:39:39 +100037#include <pwd.h>
38#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100039#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100040#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100041#include <string.h>
Damien Miller1cdde6f2006-07-24 14:07:35 +100042#include <unistd.h>
Damien Miller63b4bcd2013-03-20 12:55:14 +110043#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
Damien Miller7ba0ca72008-07-17 18:57:06 +100044#include <vis.h>
Damien Miller2e28d862008-07-17 19:15:43 +100045#endif
Darren Tucker39972492006-07-12 22:22:46 +100046
Damien Miller9c617692003-05-14 14:31:11 +100047#include "openbsd-compat/sys-queue.h"
48
Damien Millerd7834352006-08-05 12:39:39 +100049#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100050#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100052#include "buffer.h"
53#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100054#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include "cipher.h"
Damien Millerd7834352006-08-05 12:39:39 +100056#include "key.h"
Damien Millereba71ba2000-04-29 23:57:08 +100057#include "kex.h"
58#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100059#include "sshconnect.h"
60#include "authfile.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000061#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100062#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "log.h"
64#include "readconf.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100065#include "misc.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000066#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000067#include "dispatch.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000068#include "canohost.h"
Ben Lindstrom1bad2562002-06-06 19:57:33 +000069#include "msg.h"
70#include "pathnames.h"
Damien Miller6b4069a2006-06-13 13:05:15 +100071#include "uidswap.h"
Damien Millerd925dcd2010-12-01 12:21:51 +110072#include "hostfile.h"
Damien Millercee85232009-03-06 00:58:22 +110073#include "schnorr.h"
Damien Miller01ed2272008-11-05 16:20:46 +110074#include "jpake.h"
Damien Miller874d77b2000-10-14 16:23:11 +110075
Darren Tucker0efd1552003-08-26 11:49:55 +100076#ifdef GSSAPI
77#include "ssh-gss.h"
78#endif
79
Damien Millereba71ba2000-04-29 23:57:08 +100080/* import */
81extern char *client_version_string;
82extern char *server_version_string;
83extern Options options;
84
85/*
86 * SSH2 key exchange
87 */
88
Ben Lindstrom46c16222000-12-22 01:43:59 +000089u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100090u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100091
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000092char *xxx_host;
93struct sockaddr *xxx_hostaddr;
94
Ben Lindstromf28f6342001-04-04 02:03:04 +000095Kex *xxx_kex = NULL;
96
Ben Lindstrombba81212001-06-25 05:01:22 +000097static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000098verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000099{
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000100 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +1000101 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000102 return 0;
103}
104
Damien Millerd925dcd2010-12-01 12:21:51 +1100105static char *
106order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
107{
108 char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
109 size_t maxlen;
110 struct hostkeys *hostkeys;
111 int ktype;
Damien Miller295ee632011-05-29 21:42:31 +1000112 u_int i;
Damien Millerd925dcd2010-12-01 12:21:51 +1100113
114 /* Find all hostkeys for this hostname */
115 get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
116 hostkeys = init_hostkeys();
Damien Miller295ee632011-05-29 21:42:31 +1000117 for (i = 0; i < options.num_user_hostfiles; i++)
118 load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
119 for (i = 0; i < options.num_system_hostfiles; i++)
120 load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
Damien Millerd925dcd2010-12-01 12:21:51 +1100121
122 oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
123 maxlen = strlen(avail) + 1;
124 first = xmalloc(maxlen);
125 last = xmalloc(maxlen);
126 *first = *last = '\0';
127
128#define ALG_APPEND(to, from) \
129 do { \
130 if (*to != '\0') \
131 strlcat(to, ",", maxlen); \
132 strlcat(to, from, maxlen); \
133 } while (0)
134
135 while ((alg = strsep(&avail, ",")) && *alg != '\0') {
136 if ((ktype = key_type_from_name(alg)) == KEY_UNSPEC)
137 fatal("%s: unknown alg %s", __func__, alg);
138 if (lookup_key_in_hostkeys_by_type(hostkeys,
139 key_type_plain(ktype), NULL))
140 ALG_APPEND(first, alg);
141 else
142 ALG_APPEND(last, alg);
143 }
144#undef ALG_APPEND
145 xasprintf(&ret, "%s%s%s", first, *first == '\0' ? "" : ",", last);
146 if (*first != '\0')
147 debug3("%s: prefer hostkeyalgs: %s", __func__, first);
148
Darren Tuckera627d422013-06-02 07:31:17 +1000149 free(first);
150 free(last);
151 free(hostname);
152 free(oavail);
Damien Millerd925dcd2010-12-01 12:21:51 +1100153 free_hostkeys(hostkeys);
154
155 return ret;
156}
157
Damien Millereba71ba2000-04-29 23:57:08 +1000158void
Damien Millerd925dcd2010-12-01 12:21:51 +1100159ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
Damien Miller874d77b2000-10-14 16:23:11 +1100160{
Damien Miller874d77b2000-10-14 16:23:11 +1100161 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000162
163 xxx_host = host;
164 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +1100165
Damien Millere39cacc2000-11-29 12:18:44 +1100166 if (options.ciphers == (char *)-1) {
Damien Miller996acd22003-04-09 20:59:48 +1000167 logit("No valid ciphers for protocol version 2 given, using defaults.");
Damien Millere39cacc2000-11-29 12:18:44 +1100168 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100169 }
170 if (options.ciphers != NULL) {
171 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
172 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
173 }
Damien Millera0ff4662001-03-30 10:49:35 +1000174 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
175 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
176 myproposal[PROPOSAL_ENC_ALGS_STOC] =
177 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100178 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000179 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000180 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100181 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000182 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Damien Miller9786e6e2005-07-26 21:54:56 +1000183 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100184 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000185 if (options.macs != NULL) {
186 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
187 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
188 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000189 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100190 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100191 compat_pkalg_proposal(options.hostkeyalgorithms);
Damien Millerd925dcd2010-12-01 12:21:51 +1100192 else {
193 /* Prefer algorithms that we already have keys for */
194 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Damien Miller324541e2013-12-31 12:25:40 +1100195 compat_pkalg_proposal(
196 order_hostkeyalgs(host, hostaddr, port));
Damien Millerd925dcd2010-12-01 12:21:51 +1100197 }
Damien Millerd5f62bf2010-09-24 22:11:14 +1000198 if (options.kex_algorithms != NULL)
199 myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100200
Darren Tuckerc53c2af2013-05-16 20:28:16 +1000201 if (options.rekey_limit || options.rekey_interval)
202 packet_set_rekey_limits((u_int32_t)options.rekey_limit,
203 (time_t)options.rekey_interval);
Damien Millera5539d22003-04-09 20:50:06 +1000204
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000205 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000206 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100207 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
Damien Millerf675fc42004-06-15 10:30:09 +1000208 kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
Damien Miller8e7fb332003-02-24 12:03:03 +1100209 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Damien Millera63128d2006-03-15 12:08:28 +1100210 kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
Damien Millereb8b60e2010-08-31 22:41:14 +1000211 kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
Damien Miller1e124262013-11-04 08:26:52 +1100212 kex->kex[KEX_C25519_SHA256] = kexc25519_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000213 kex->client_version_string=client_version_string;
214 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000215 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100216
Ben Lindstromf28f6342001-04-04 02:03:04 +0000217 xxx_kex = kex;
218
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000219 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100220
Darren Tucker36331b52010-01-08 16:50:41 +1100221 if (options.use_roaming && !kex->roaming) {
222 debug("Roaming not allowed by server");
223 options.use_roaming = 0;
224 }
225
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000226 session_id2 = kex->session_id;
227 session_id2_len = kex->session_id_len;
228
Damien Miller874d77b2000-10-14 16:23:11 +1100229#ifdef DEBUG_KEXDH
230 /* send 1st encrypted/maced/compressed message */
231 packet_start(SSH2_MSG_IGNORE);
232 packet_put_cstring("markus");
233 packet_send();
234 packet_write_wait();
235#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000236}
Damien Millerb1715dc2000-05-30 13:44:51 +1000237
Damien Millereba71ba2000-04-29 23:57:08 +1000238/*
239 * Authenticate user
240 */
Damien Miller62cee002000-09-23 17:15:56 +1100241
242typedef struct Authctxt Authctxt;
243typedef struct Authmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000244typedef struct identity Identity;
245typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100246
Damien Miller280ecfb2003-05-14 13:46:00 +1000247struct identity {
248 TAILQ_ENTRY(identity) next;
249 AuthenticationConnection *ac; /* set if agent supports key */
250 Key *key; /* public/private key */
251 char *filename; /* comment for agent-only keys */
252 int tried;
253 int isprivate; /* key points to the private key */
Damien Miller5ceddc32013-02-15 12:18:32 +1100254 int userprovided;
Damien Miller280ecfb2003-05-14 13:46:00 +1000255};
256TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100257
258struct Authctxt {
259 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000260 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100261 const char *host;
262 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100263 Authmethod *method;
Damien Miller79442c02010-05-10 11:55:38 +1000264 sig_atomic_t success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000265 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000266 /* pubkey */
Damien Miller280ecfb2003-05-14 13:46:00 +1000267 Idlist keys;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000268 AuthenticationConnection *agent;
269 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000270 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000271 /* kbd-interactive */
272 int info_req_seen;
Darren Tucker0efd1552003-08-26 11:49:55 +1000273 /* generic */
274 void *methoddata;
Damien Miller62cee002000-09-23 17:15:56 +1100275};
276struct Authmethod {
277 char *name; /* string to compare against server's list */
278 int (*userauth)(Authctxt *authctxt);
Damien Miller01ed2272008-11-05 16:20:46 +1100279 void (*cleanup)(Authctxt *authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100280 int *enabled; /* flag in option struct that enables method */
281 int *batch_flag; /* flag in option struct that disables method */
282};
283
Damien Miller630d6f42002-01-22 23:17:30 +1100284void input_userauth_success(int, u_int32_t, void *);
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100285void input_userauth_success_unexpected(int, u_int32_t, void *);
Damien Miller630d6f42002-01-22 23:17:30 +1100286void input_userauth_failure(int, u_int32_t, void *);
287void input_userauth_banner(int, u_int32_t, void *);
288void input_userauth_error(int, u_int32_t, void *);
289void input_userauth_info_req(int, u_int32_t, void *);
290void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000291void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller01ed2272008-11-05 16:20:46 +1100292void input_userauth_jpake_server_step1(int, u_int32_t, void *);
293void input_userauth_jpake_server_step2(int, u_int32_t, void *);
294void input_userauth_jpake_server_confirm(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100295
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000296int userauth_none(Authctxt *);
297int userauth_pubkey(Authctxt *);
298int userauth_passwd(Authctxt *);
299int userauth_kbdint(Authctxt *);
300int userauth_hostbased(Authctxt *);
Damien Miller01ed2272008-11-05 16:20:46 +1100301int userauth_jpake(Authctxt *);
302
303void userauth_jpake_cleanup(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100304
Darren Tucker0efd1552003-08-26 11:49:55 +1000305#ifdef GSSAPI
306int userauth_gssapi(Authctxt *authctxt);
307void input_gssapi_response(int type, u_int32_t, void *);
308void input_gssapi_token(int type, u_int32_t, void *);
309void input_gssapi_hash(int type, u_int32_t, void *);
310void input_gssapi_error(int, u_int32_t, void *);
311void input_gssapi_errtok(int, u_int32_t, void *);
312#endif
313
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000314void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000315
Damien Miller280ecfb2003-05-14 13:46:00 +1000316static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000317static void pubkey_prepare(Authctxt *);
318static void pubkey_cleanup(Authctxt *);
Damien Miller5ceddc32013-02-15 12:18:32 +1100319static Key *load_identity_file(char *, int);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000320
Ben Lindstrombba81212001-06-25 05:01:22 +0000321static Authmethod *authmethod_get(char *authlist);
322static Authmethod *authmethod_lookup(const char *name);
323static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100324
325Authmethod authmethods[] = {
Darren Tucker0efd1552003-08-26 11:49:55 +1000326#ifdef GSSAPI
Damien Miller0425d402003-11-17 22:18:21 +1100327 {"gssapi-with-mic",
Darren Tucker0efd1552003-08-26 11:49:55 +1000328 userauth_gssapi,
Damien Miller01ed2272008-11-05 16:20:46 +1100329 NULL,
Darren Tucker0efd1552003-08-26 11:49:55 +1000330 &options.gss_authentication,
331 NULL},
332#endif
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000333 {"hostbased",
334 userauth_hostbased,
Damien Miller01ed2272008-11-05 16:20:46 +1100335 NULL,
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000336 &options.hostbased_authentication,
337 NULL},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000338 {"publickey",
339 userauth_pubkey,
Damien Miller01ed2272008-11-05 16:20:46 +1100340 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000341 &options.pubkey_authentication,
342 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100343#ifdef JPAKE
344 {"jpake-01@openssh.com",
345 userauth_jpake,
346 userauth_jpake_cleanup,
347 &options.zero_knowledge_password_authentication,
348 &options.batch_mode},
349#endif
Damien Miller874d77b2000-10-14 16:23:11 +1100350 {"keyboard-interactive",
351 userauth_kbdint,
Damien Miller01ed2272008-11-05 16:20:46 +1100352 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100353 &options.kbd_interactive_authentication,
354 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000355 {"password",
356 userauth_passwd,
Damien Miller01ed2272008-11-05 16:20:46 +1100357 NULL,
Ben Lindstrom45350e82001-08-06 20:57:11 +0000358 &options.password_authentication,
359 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100360 {"none",
361 userauth_none,
362 NULL,
Damien Miller01ed2272008-11-05 16:20:46 +1100363 NULL,
Damien Miller874d77b2000-10-14 16:23:11 +1100364 NULL},
Damien Miller01ed2272008-11-05 16:20:46 +1100365 {NULL, NULL, NULL, NULL, NULL}
Damien Miller62cee002000-09-23 17:15:56 +1100366};
367
368void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000369ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000370 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100371{
372 Authctxt authctxt;
373 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100374
Ben Lindstrom551ea372001-06-05 18:56:16 +0000375 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000376 options.kbd_interactive_authentication = 1;
377
Damien Miller62cee002000-09-23 17:15:56 +1100378 packet_start(SSH2_MSG_SERVICE_REQUEST);
379 packet_put_cstring("ssh-userauth");
380 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000381 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100382 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100383 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000384 if (type != SSH2_MSG_SERVICE_ACCEPT)
385 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100386 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100387 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000388 debug2("service_accept: %s", reply);
Darren Tuckera627d422013-06-02 07:31:17 +1000389 free(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100390 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000391 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100392 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100393 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000394 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100395
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000396 if (options.preferred_authentications == NULL)
397 options.preferred_authentications = authmethods_get();
398
Damien Miller62cee002000-09-23 17:15:56 +1100399 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000400 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000401 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100402 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000403 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100404 authctxt.host = host;
405 authctxt.service = "ssh-connection"; /* service name */
406 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100407 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000408 authctxt.authlist = NULL;
Darren Tucker0efd1552003-08-26 11:49:55 +1000409 authctxt.methoddata = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000410 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000411 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100412 if (authctxt.method == NULL)
413 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100414
415 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100416 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100417
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000418 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100419 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
420 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000421 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100422 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
423
Damien Miller280ecfb2003-05-14 13:46:00 +1000424 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000425 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
426
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000427 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100428}
Damien Millerf842fcb2003-05-15 12:01:28 +1000429
Damien Miller62cee002000-09-23 17:15:56 +1100430void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000431userauth(Authctxt *authctxt, char *authlist)
432{
Damien Miller01ed2272008-11-05 16:20:46 +1100433 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
434 authctxt->method->cleanup(authctxt);
435
Darren Tuckera627d422013-06-02 07:31:17 +1000436 free(authctxt->methoddata);
437 authctxt->methoddata = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000438 if (authlist == NULL) {
439 authlist = authctxt->authlist;
440 } else {
Darren Tuckera627d422013-06-02 07:31:17 +1000441 free(authctxt->authlist);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000442 authctxt->authlist = authlist;
443 }
444 for (;;) {
445 Authmethod *method = authmethod_get(authlist);
446 if (method == NULL)
447 fatal("Permission denied (%s).", authlist);
448 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000449
450 /* reset the per method handler */
451 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
452 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
453
454 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000455 if (method->userauth(authctxt) != 0) {
456 debug2("we sent a %s packet, wait for reply", method->name);
457 break;
458 } else {
459 debug2("we did not send a packet, disable method");
460 method->enabled = NULL;
461 }
462 }
463}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000464
Damien Millerf7475d72008-11-03 19:26:18 +1100465/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000466void
Damien Miller630d6f42002-01-22 23:17:30 +1100467input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100468{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000469 fatal("input_userauth_error: bad message during authentication: "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000470 "type %d", type);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000471}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000472
Damien Millerf7475d72008-11-03 19:26:18 +1100473/* ARGSUSED */
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000474void
Damien Miller630d6f42002-01-22 23:17:30 +1100475input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000476{
Damien Miller7ba0ca72008-07-17 18:57:06 +1000477 char *msg, *raw, *lang;
478 u_int len;
Darren Tucker79644822003-10-08 17:37:58 +1000479
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000480 debug3("input_userauth_banner");
Damien Miller7ba0ca72008-07-17 18:57:06 +1000481 raw = packet_get_string(&len);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000482 lang = packet_get_string(NULL);
Damien Millerc674d582008-11-03 19:16:57 +1100483 if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
Damien Miller7ba0ca72008-07-17 18:57:06 +1000484 if (len > 65536)
485 len = 65536;
Damien Millerc4d1b362008-11-03 19:22:09 +1100486 msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
Darren Tucker0c348f52010-01-08 18:58:05 +1100487 strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
Darren Tucker046dff22003-10-08 17:32:02 +1000488 fprintf(stderr, "%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000489 free(msg);
Damien Miller7ba0ca72008-07-17 18:57:06 +1000490 }
Darren Tuckera627d422013-06-02 07:31:17 +1000491 free(raw);
492 free(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100493}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000494
Damien Millerf7475d72008-11-03 19:26:18 +1100495/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100496void
Damien Miller630d6f42002-01-22 23:17:30 +1100497input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100498{
499 Authctxt *authctxt = ctxt;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100500
Damien Miller62cee002000-09-23 17:15:56 +1100501 if (authctxt == NULL)
502 fatal("input_userauth_success: no authentication context");
Darren Tuckera627d422013-06-02 07:31:17 +1000503 free(authctxt->authlist);
504 authctxt->authlist = NULL;
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100505 if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
506 authctxt->method->cleanup(authctxt);
Darren Tuckera627d422013-06-02 07:31:17 +1000507 free(authctxt->methoddata);
508 authctxt->methoddata = NULL;
Damien Miller62cee002000-09-23 17:15:56 +1100509 authctxt->success = 1; /* break out */
510}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000511
Darren Tucker2f29a8c2009-10-24 11:47:58 +1100512void
513input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
514{
515 Authctxt *authctxt = ctxt;
516
517 if (authctxt == NULL)
518 fatal("%s: no authentication context", __func__);
519
520 fatal("Unexpected authentication success during %s.",
521 authctxt->method->name);
522}
523
Damien Millerf7475d72008-11-03 19:26:18 +1100524/* ARGSUSED */
Damien Miller62cee002000-09-23 17:15:56 +1100525void
Damien Miller630d6f42002-01-22 23:17:30 +1100526input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100527{
Damien Miller62cee002000-09-23 17:15:56 +1100528 Authctxt *authctxt = ctxt;
529 char *authlist = NULL;
530 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100531
532 if (authctxt == NULL)
533 fatal("input_userauth_failure: no authentication context");
534
Damien Miller874d77b2000-10-14 16:23:11 +1100535 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100536 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100537 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100538
Damien Miller62e9c4f2013-04-23 15:15:49 +1000539 if (partial != 0) {
Damien Miller996acd22003-04-09 20:59:48 +1000540 logit("Authenticated with partial success.");
Damien Miller62e9c4f2013-04-23 15:15:49 +1000541 /* reset state */
542 pubkey_cleanup(authctxt);
543 pubkey_prepare(authctxt);
544 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000545 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100546
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000547 userauth(authctxt, authlist);
548}
Damien Millerf7475d72008-11-03 19:26:18 +1100549
550/* ARGSUSED */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000551void
Damien Miller630d6f42002-01-22 23:17:30 +1100552input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000553{
554 Authctxt *authctxt = ctxt;
555 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000556 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000557 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000558 int pktype, sent = 0;
559 u_int alen, blen;
560 char *pkalg, *fp;
561 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000562
563 if (authctxt == NULL)
564 fatal("input_userauth_pk_ok: no authentication context");
565 if (datafellows & SSH_BUG_PKOK) {
566 /* this is similar to SSH_BUG_PKAUTH */
567 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
568 pkblob = packet_get_string(&blen);
569 buffer_init(&b);
570 buffer_append(&b, pkblob, blen);
571 pkalg = buffer_get_string(&b, &alen);
572 buffer_free(&b);
573 } else {
574 pkalg = packet_get_string(&alen);
575 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000576 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100577 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000578
Damien Miller280ecfb2003-05-14 13:46:00 +1000579 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000580
Damien Miller280ecfb2003-05-14 13:46:00 +1000581 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
582 debug("unknown pkalg %s", pkalg);
583 goto done;
584 }
585 if ((key = key_from_blob(pkblob, blen)) == NULL) {
586 debug("no key from blob. pkalg %s", pkalg);
587 goto done;
588 }
589 if (key->type != pktype) {
590 error("input_userauth_pk_ok: type mismatch "
591 "for decoded key (received %d, expected %d)",
592 key->type, pktype);
593 goto done;
594 }
595 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
596 debug2("input_userauth_pk_ok: fp %s", fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000597 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000598
Darren Tuckerd05b6012003-10-15 15:55:59 +1000599 /*
600 * search keys in the reverse order, because last candidate has been
601 * moved to the end of the queue. this also avoids confusion by
602 * duplicate keys
603 */
Damien Miller0b51a522004-04-20 20:07:19 +1000604 TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
Damien Miller280ecfb2003-05-14 13:46:00 +1000605 if (key_equal(key, id->key)) {
606 sent = sign_and_send_pubkey(authctxt, id);
607 break;
608 }
609 }
610done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000611 if (key != NULL)
612 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000613 free(pkalg);
614 free(pkblob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000615
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000616 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000617 if (sent == 0)
618 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100619}
620
Darren Tucker0efd1552003-08-26 11:49:55 +1000621#ifdef GSSAPI
Damien Millera8e06ce2003-11-21 23:48:55 +1100622int
Darren Tucker0efd1552003-08-26 11:49:55 +1000623userauth_gssapi(Authctxt *authctxt)
624{
625 Gssctxt *gssctxt = NULL;
Darren Tucker56afe142003-11-03 20:06:14 +1100626 static gss_OID_set gss_supported = NULL;
Damien Millereccb9de2005-06-17 12:59:34 +1000627 static u_int mech = 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000628 OM_uint32 min;
629 int ok = 0;
630
631 /* Try one GSSAPI method at a time, rather than sending them all at
632 * once. */
633
Darren Tucker56afe142003-11-03 20:06:14 +1100634 if (gss_supported == NULL)
635 gss_indicate_mechs(&min, &gss_supported);
Darren Tucker0efd1552003-08-26 11:49:55 +1000636
637 /* Check to see if the mechanism is usable before we offer it */
Darren Tucker56afe142003-11-03 20:06:14 +1100638 while (mech < gss_supported->count && !ok) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000639 /* My DER encoding requires length<128 */
Darren Tucker56afe142003-11-03 20:06:14 +1100640 if (gss_supported->elements[mech].length < 128 &&
Damien Millera1cb9f32006-08-19 00:33:34 +1000641 ssh_gssapi_check_mechanism(&gssctxt,
642 &gss_supported->elements[mech], authctxt->host)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000643 ok = 1; /* Mechanism works */
644 } else {
645 mech++;
646 }
647 }
648
Damien Millera1cb9f32006-08-19 00:33:34 +1000649 if (!ok)
Damien Millereccb9de2005-06-17 12:59:34 +1000650 return 0;
Darren Tucker0efd1552003-08-26 11:49:55 +1000651
652 authctxt->methoddata=(void *)gssctxt;
653
654 packet_start(SSH2_MSG_USERAUTH_REQUEST);
655 packet_put_cstring(authctxt->server_user);
656 packet_put_cstring(authctxt->service);
657 packet_put_cstring(authctxt->method->name);
658
659 packet_put_int(1);
660
Darren Tucker655a5e02003-11-03 20:09:03 +1100661 packet_put_int((gss_supported->elements[mech].length) + 2);
662 packet_put_char(SSH_GSS_OIDTYPE);
663 packet_put_char(gss_supported->elements[mech].length);
664 packet_put_raw(gss_supported->elements[mech].elements,
665 gss_supported->elements[mech].length);
Darren Tucker0efd1552003-08-26 11:49:55 +1000666
667 packet_send();
668
669 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
670 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
671 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
672 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
673
674 mech++; /* Move along to next candidate */
675
676 return 1;
677}
678
Damien Miller91c6aa42003-11-17 21:20:18 +1100679static OM_uint32
680process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
681{
682 Authctxt *authctxt = ctxt;
683 Gssctxt *gssctxt = authctxt->methoddata;
684 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
Damien Millerda9984f2005-08-31 19:46:26 +1000685 gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
686 gss_buffer_desc gssbuf;
Damien Miller0425d402003-11-17 22:18:21 +1100687 OM_uint32 status, ms, flags;
688 Buffer b;
Damien Miller787b2ec2003-11-21 23:56:47 +1100689
Damien Miller91c6aa42003-11-17 21:20:18 +1100690 status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0425d402003-11-17 22:18:21 +1100691 recv_tok, &send_tok, &flags);
Damien Miller91c6aa42003-11-17 21:20:18 +1100692
693 if (send_tok.length > 0) {
694 if (GSS_ERROR(status))
695 packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
696 else
697 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
Damien Miller787b2ec2003-11-21 23:56:47 +1100698
Damien Miller91c6aa42003-11-17 21:20:18 +1100699 packet_put_string(send_tok.value, send_tok.length);
700 packet_send();
701 gss_release_buffer(&ms, &send_tok);
702 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100703
Damien Miller91c6aa42003-11-17 21:20:18 +1100704 if (status == GSS_S_COMPLETE) {
Damien Miller0425d402003-11-17 22:18:21 +1100705 /* send either complete or MIC, depending on mechanism */
706 if (!(flags & GSS_C_INTEG_FLAG)) {
707 packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
708 packet_send();
709 } else {
710 ssh_gssapi_buildmic(&b, authctxt->server_user,
711 authctxt->service, "gssapi-with-mic");
712
713 gssbuf.value = buffer_ptr(&b);
714 gssbuf.length = buffer_len(&b);
Damien Miller787b2ec2003-11-21 23:56:47 +1100715
Damien Miller0425d402003-11-17 22:18:21 +1100716 status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100717
Damien Miller0425d402003-11-17 22:18:21 +1100718 if (!GSS_ERROR(status)) {
719 packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
720 packet_put_string(mic.value, mic.length);
Damien Miller787b2ec2003-11-21 23:56:47 +1100721
Damien Miller0425d402003-11-17 22:18:21 +1100722 packet_send();
723 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100724
Damien Miller0425d402003-11-17 22:18:21 +1100725 buffer_free(&b);
726 gss_release_buffer(&ms, &mic);
Damien Miller787b2ec2003-11-21 23:56:47 +1100727 }
Damien Miller91c6aa42003-11-17 21:20:18 +1100728 }
Damien Miller787b2ec2003-11-21 23:56:47 +1100729
Damien Miller91c6aa42003-11-17 21:20:18 +1100730 return status;
731}
732
Damien Millerf7475d72008-11-03 19:26:18 +1100733/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000734void
735input_gssapi_response(int type, u_int32_t plen, void *ctxt)
736{
737 Authctxt *authctxt = ctxt;
738 Gssctxt *gssctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000739 int oidlen;
740 char *oidv;
Darren Tucker0efd1552003-08-26 11:49:55 +1000741
742 if (authctxt == NULL)
743 fatal("input_gssapi_response: no authentication context");
744 gssctxt = authctxt->methoddata;
745
746 /* Setup our OID */
747 oidv = packet_get_string(&oidlen);
748
Darren Tucker655a5e02003-11-03 20:09:03 +1100749 if (oidlen <= 2 ||
750 oidv[0] != SSH_GSS_OIDTYPE ||
751 oidv[1] != oidlen - 2) {
Darren Tuckera627d422013-06-02 07:31:17 +1000752 free(oidv);
Darren Tucker655a5e02003-11-03 20:09:03 +1100753 debug("Badly encoded mechanism OID received");
754 userauth(authctxt, NULL);
Darren Tucker655a5e02003-11-03 20:09:03 +1100755 return;
Darren Tucker0efd1552003-08-26 11:49:55 +1000756 }
757
Darren Tucker655a5e02003-11-03 20:09:03 +1100758 if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
759 fatal("Server returned different OID than expected");
760
Darren Tucker0efd1552003-08-26 11:49:55 +1000761 packet_check_eom();
762
Darren Tuckera627d422013-06-02 07:31:17 +1000763 free(oidv);
Darren Tucker0efd1552003-08-26 11:49:55 +1000764
Damien Miller91c6aa42003-11-17 21:20:18 +1100765 if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000766 /* Start again with next method on list */
767 debug("Trying to start again");
768 userauth(authctxt, NULL);
769 return;
770 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000771}
772
Damien Millerf7475d72008-11-03 19:26:18 +1100773/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000774void
775input_gssapi_token(int type, u_int32_t plen, void *ctxt)
776{
777 Authctxt *authctxt = ctxt;
Darren Tucker0efd1552003-08-26 11:49:55 +1000778 gss_buffer_desc recv_tok;
Damien Miller91c6aa42003-11-17 21:20:18 +1100779 OM_uint32 status;
Darren Tucker0efd1552003-08-26 11:49:55 +1000780 u_int slen;
781
782 if (authctxt == NULL)
783 fatal("input_gssapi_response: no authentication context");
Darren Tucker0efd1552003-08-26 11:49:55 +1000784
785 recv_tok.value = packet_get_string(&slen);
786 recv_tok.length = slen; /* safe typecast */
787
788 packet_check_eom();
789
Damien Miller91c6aa42003-11-17 21:20:18 +1100790 status = process_gssapi_token(ctxt, &recv_tok);
Darren Tucker0efd1552003-08-26 11:49:55 +1000791
Darren Tuckera627d422013-06-02 07:31:17 +1000792 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000793
794 if (GSS_ERROR(status)) {
Darren Tucker0efd1552003-08-26 11:49:55 +1000795 /* Start again with the next method in the list */
796 userauth(authctxt, NULL);
797 return;
798 }
Darren Tucker0efd1552003-08-26 11:49:55 +1000799}
800
Damien Millerf7475d72008-11-03 19:26:18 +1100801/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000802void
803input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
804{
805 Authctxt *authctxt = ctxt;
806 Gssctxt *gssctxt;
807 gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
808 gss_buffer_desc recv_tok;
Damien Millerd677ad12013-04-23 15:18:51 +1000809 OM_uint32 ms;
Darren Tucker600ad8d2003-08-26 12:10:48 +1000810 u_int len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000811
812 if (authctxt == NULL)
813 fatal("input_gssapi_response: no authentication context");
814 gssctxt = authctxt->methoddata;
815
Darren Tucker600ad8d2003-08-26 12:10:48 +1000816 recv_tok.value = packet_get_string(&len);
817 recv_tok.length = len;
Darren Tucker0efd1552003-08-26 11:49:55 +1000818
819 packet_check_eom();
820
821 /* Stick it into GSSAPI and see what it says */
Damien Millerd677ad12013-04-23 15:18:51 +1000822 (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
Damien Miller0dc1bef2005-07-17 17:22:45 +1000823 &recv_tok, &send_tok, NULL);
Darren Tucker0efd1552003-08-26 11:49:55 +1000824
Darren Tuckera627d422013-06-02 07:31:17 +1000825 free(recv_tok.value);
Darren Tucker0efd1552003-08-26 11:49:55 +1000826 gss_release_buffer(&ms, &send_tok);
827
828 /* Server will be returning a failed packet after this one */
829}
830
Damien Millerf7475d72008-11-03 19:26:18 +1100831/* ARGSUSED */
Darren Tucker0efd1552003-08-26 11:49:55 +1000832void
833input_gssapi_error(int type, u_int32_t plen, void *ctxt)
834{
Darren Tucker0efd1552003-08-26 11:49:55 +1000835 char *msg;
836 char *lang;
837
Damien Millerd677ad12013-04-23 15:18:51 +1000838 /* maj */(void)packet_get_int();
839 /* min */(void)packet_get_int();
Darren Tucker0efd1552003-08-26 11:49:55 +1000840 msg=packet_get_string(NULL);
841 lang=packet_get_string(NULL);
842
843 packet_check_eom();
844
Damien Miller15d72a02005-11-05 15:07:33 +1100845 debug("Server GSSAPI Error:\n%s", msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000846 free(msg);
847 free(lang);
Darren Tucker0efd1552003-08-26 11:49:55 +1000848}
849#endif /* GSSAPI */
850
Damien Millereba71ba2000-04-29 23:57:08 +1000851int
Damien Miller874d77b2000-10-14 16:23:11 +1100852userauth_none(Authctxt *authctxt)
853{
854 /* initial userauth request */
855 packet_start(SSH2_MSG_USERAUTH_REQUEST);
856 packet_put_cstring(authctxt->server_user);
857 packet_put_cstring(authctxt->service);
858 packet_put_cstring(authctxt->method->name);
859 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100860 return 1;
861}
862
863int
Damien Miller62cee002000-09-23 17:15:56 +1100864userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000865{
Damien Millere247cc42000-05-07 12:03:14 +1000866 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000867 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000868 char *password;
Darren Tuckerab791692010-01-08 18:48:02 +1100869 const char *host = options.host_key_alias ? options.host_key_alias :
870 authctxt->host;
Damien Millereba71ba2000-04-29 23:57:08 +1000871
Damien Millerd3a18572000-06-07 19:55:44 +1000872 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000873 return 0;
874
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000875 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000876 error("Permission denied, please try again.");
877
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000878 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100879 authctxt->server_user, host);
Damien Millereba71ba2000-04-29 23:57:08 +1000880 password = read_passphrase(prompt, 0);
881 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100882 packet_put_cstring(authctxt->server_user);
883 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100884 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000885 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000886 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000887 memset(password, 0, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000888 free(password);
Damien Miller9f643902001-11-12 11:02:52 +1100889 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000890 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000891
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000892 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000893 &input_userauth_passwd_changereq);
894
Damien Millereba71ba2000-04-29 23:57:08 +1000895 return 1;
896}
Damien Millerf7475d72008-11-03 19:26:18 +1100897
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000898/*
899 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
900 */
Damien Millerf7475d72008-11-03 19:26:18 +1100901/* ARGSUSED */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000902void
Tim Ricec8549622002-03-31 12:49:38 -0800903input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000904{
905 Authctxt *authctxt = ctxt;
906 char *info, *lang, *password = NULL, *retype = NULL;
907 char prompt[150];
Darren Tuckerab791692010-01-08 18:48:02 +1100908 const char *host = options.host_key_alias ? options.host_key_alias :
909 authctxt->host;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000910
911 debug2("input_userauth_passwd_changereq");
912
913 if (authctxt == NULL)
914 fatal("input_userauth_passwd_changereq: "
915 "no authentication context");
916
917 info = packet_get_string(NULL);
918 lang = packet_get_string(NULL);
919 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000920 logit("%s", info);
Darren Tuckera627d422013-06-02 07:31:17 +1000921 free(info);
922 free(lang);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000923 packet_start(SSH2_MSG_USERAUTH_REQUEST);
924 packet_put_cstring(authctxt->server_user);
925 packet_put_cstring(authctxt->service);
926 packet_put_cstring(authctxt->method->name);
927 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000928 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000929 "Enter %.30s@%.128s's old password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100930 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000931 password = read_passphrase(prompt, 0);
932 packet_put_cstring(password);
933 memset(password, 0, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000934 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000935 password = NULL;
936 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000937 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000938 "Enter %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100939 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000940 password = read_passphrase(prompt, RP_ALLOW_EOF);
941 if (password == NULL) {
942 /* bail out */
943 return;
944 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000945 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000946 "Retype %.30s@%.128s's new password: ",
Darren Tuckerab791692010-01-08 18:48:02 +1100947 authctxt->server_user, host);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000948 retype = read_passphrase(prompt, 0);
949 if (strcmp(password, retype) != 0) {
950 memset(password, 0, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000951 free(password);
Damien Miller996acd22003-04-09 20:59:48 +1000952 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000953 password = NULL;
954 }
955 memset(retype, 0, strlen(retype));
Darren Tuckera627d422013-06-02 07:31:17 +1000956 free(retype);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000957 }
958 packet_put_cstring(password);
959 memset(password, 0, strlen(password));
Darren Tuckera627d422013-06-02 07:31:17 +1000960 free(password);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000961 packet_add_padding(64);
962 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000963
964 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000965 &input_userauth_passwd_changereq);
966}
Damien Millereba71ba2000-04-29 23:57:08 +1000967
Damien Miller01ed2272008-11-05 16:20:46 +1100968#ifdef JPAKE
969static char *
970pw_encrypt(const char *password, const char *crypt_scheme, const char *salt)
971{
972 /* OpenBSD crypt(3) handles all of these */
973 if (strcmp(crypt_scheme, "crypt") == 0 ||
974 strcmp(crypt_scheme, "bcrypt") == 0 ||
975 strcmp(crypt_scheme, "md5crypt") == 0 ||
976 strcmp(crypt_scheme, "crypt-extended") == 0)
977 return xstrdup(crypt(password, salt));
978 error("%s: unsupported password encryption scheme \"%.100s\"",
979 __func__, crypt_scheme);
980 return NULL;
981}
982
983static BIGNUM *
984jpake_password_to_secret(Authctxt *authctxt, const char *crypt_scheme,
985 const char *salt)
986{
987 char prompt[256], *password, *crypted;
988 u_char *secret;
989 u_int secret_len;
990 BIGNUM *ret;
991
992 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password (JPAKE): ",
993 authctxt->server_user, authctxt->host);
994 password = read_passphrase(prompt, 0);
995
996 if ((crypted = pw_encrypt(password, crypt_scheme, salt)) == NULL) {
997 logit("Disabling %s authentication", authctxt->method->name);
998 authctxt->method->enabled = NULL;
999 /* Continue with an empty password to fail gracefully */
1000 crypted = xstrdup("");
1001 }
1002
1003#ifdef JPAKE_DEBUG
1004 debug3("%s: salt = %s", __func__, salt);
1005 debug3("%s: scheme = %s", __func__, crypt_scheme);
1006 debug3("%s: crypted = %s", __func__, crypted);
1007#endif
1008
1009 if (hash_buffer(crypted, strlen(crypted), EVP_sha256(),
1010 &secret, &secret_len) != 0)
1011 fatal("%s: hash_buffer", __func__);
1012
1013 bzero(password, strlen(password));
1014 bzero(crypted, strlen(crypted));
Darren Tuckera627d422013-06-02 07:31:17 +10001015 free(password);
1016 free(crypted);
Damien Miller01ed2272008-11-05 16:20:46 +11001017
1018 if ((ret = BN_bin2bn(secret, secret_len, NULL)) == NULL)
1019 fatal("%s: BN_bin2bn (secret)", __func__);
1020 bzero(secret, secret_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001021 free(secret);
Damien Miller01ed2272008-11-05 16:20:46 +11001022
1023 return ret;
1024}
1025
1026/* ARGSUSED */
1027void
1028input_userauth_jpake_server_step1(int type, u_int32_t seq, void *ctxt)
1029{
1030 Authctxt *authctxt = ctxt;
1031 struct jpake_ctx *pctx = authctxt->methoddata;
1032 u_char *x3_proof, *x4_proof, *x2_s_proof;
1033 u_int x3_proof_len, x4_proof_len, x2_s_proof_len;
1034 char *crypt_scheme, *salt;
1035
1036 /* Disable this message */
1037 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1, NULL);
1038
1039 if ((pctx->g_x3 = BN_new()) == NULL ||
1040 (pctx->g_x4 = BN_new()) == NULL)
1041 fatal("%s: BN_new", __func__);
1042
1043 /* Fetch step 1 values */
1044 crypt_scheme = packet_get_string(NULL);
1045 salt = packet_get_string(NULL);
1046 pctx->server_id = packet_get_string(&pctx->server_id_len);
1047 packet_get_bignum2(pctx->g_x3);
1048 packet_get_bignum2(pctx->g_x4);
1049 x3_proof = packet_get_string(&x3_proof_len);
1050 x4_proof = packet_get_string(&x4_proof_len);
1051 packet_check_eom();
1052
1053 JPAKE_DEBUG_CTX((pctx, "step 1 received in %s", __func__));
1054
1055 /* Obtain password and derive secret */
1056 pctx->s = jpake_password_to_secret(authctxt, crypt_scheme, salt);
1057 bzero(crypt_scheme, strlen(crypt_scheme));
1058 bzero(salt, strlen(salt));
Darren Tuckera627d422013-06-02 07:31:17 +10001059 free(crypt_scheme);
1060 free(salt);
Damien Miller01ed2272008-11-05 16:20:46 +11001061 JPAKE_DEBUG_BN((pctx->s, "%s: s = ", __func__));
1062
1063 /* Calculate step 2 values */
1064 jpake_step2(pctx->grp, pctx->s, pctx->g_x1,
1065 pctx->g_x3, pctx->g_x4, pctx->x2,
1066 pctx->server_id, pctx->server_id_len,
1067 pctx->client_id, pctx->client_id_len,
1068 x3_proof, x3_proof_len,
1069 x4_proof, x4_proof_len,
1070 &pctx->a,
1071 &x2_s_proof, &x2_s_proof_len);
1072
1073 bzero(x3_proof, x3_proof_len);
1074 bzero(x4_proof, x4_proof_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001075 free(x3_proof);
1076 free(x4_proof);
Damien Miller01ed2272008-11-05 16:20:46 +11001077
1078 JPAKE_DEBUG_CTX((pctx, "step 2 sending in %s", __func__));
1079
1080 /* Send values for step 2 */
1081 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP2);
1082 packet_put_bignum2(pctx->a);
1083 packet_put_string(x2_s_proof, x2_s_proof_len);
1084 packet_send();
1085
1086 bzero(x2_s_proof, x2_s_proof_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001087 free(x2_s_proof);
Damien Miller01ed2272008-11-05 16:20:46 +11001088
1089 /* Expect step 2 packet from peer */
1090 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2,
1091 input_userauth_jpake_server_step2);
1092}
1093
1094/* ARGSUSED */
1095void
1096input_userauth_jpake_server_step2(int type, u_int32_t seq, void *ctxt)
1097{
1098 Authctxt *authctxt = ctxt;
1099 struct jpake_ctx *pctx = authctxt->methoddata;
1100 u_char *x4_s_proof;
1101 u_int x4_s_proof_len;
1102
1103 /* Disable this message */
1104 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP2, NULL);
1105
1106 if ((pctx->b = BN_new()) == NULL)
1107 fatal("%s: BN_new", __func__);
1108
1109 /* Fetch step 2 values */
1110 packet_get_bignum2(pctx->b);
1111 x4_s_proof = packet_get_string(&x4_s_proof_len);
1112 packet_check_eom();
1113
1114 JPAKE_DEBUG_CTX((pctx, "step 2 received in %s", __func__));
1115
1116 /* Derive shared key and calculate confirmation hash */
1117 jpake_key_confirm(pctx->grp, pctx->s, pctx->b,
1118 pctx->x2, pctx->g_x1, pctx->g_x2, pctx->g_x3, pctx->g_x4,
1119 pctx->client_id, pctx->client_id_len,
1120 pctx->server_id, pctx->server_id_len,
1121 session_id2, session_id2_len,
1122 x4_s_proof, x4_s_proof_len,
1123 &pctx->k,
1124 &pctx->h_k_cid_sessid, &pctx->h_k_cid_sessid_len);
1125
1126 bzero(x4_s_proof, x4_s_proof_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001127 free(x4_s_proof);
Damien Miller01ed2272008-11-05 16:20:46 +11001128
1129 JPAKE_DEBUG_CTX((pctx, "confirm sending in %s", __func__));
1130
1131 /* Send key confirmation proof */
1132 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_CONFIRM);
1133 packet_put_string(pctx->h_k_cid_sessid, pctx->h_k_cid_sessid_len);
1134 packet_send();
1135
1136 /* Expect confirmation from peer */
1137 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM,
1138 input_userauth_jpake_server_confirm);
1139}
1140
1141/* ARGSUSED */
1142void
1143input_userauth_jpake_server_confirm(int type, u_int32_t seq, void *ctxt)
1144{
1145 Authctxt *authctxt = ctxt;
1146 struct jpake_ctx *pctx = authctxt->methoddata;
1147
1148 /* Disable this message */
1149 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_CONFIRM, NULL);
1150
1151 pctx->h_k_sid_sessid = packet_get_string(&pctx->h_k_sid_sessid_len);
1152 packet_check_eom();
1153
1154 JPAKE_DEBUG_CTX((pctx, "confirm received in %s", __func__));
1155
1156 /* Verify expected confirmation hash */
1157 if (jpake_check_confirm(pctx->k,
1158 pctx->server_id, pctx->server_id_len,
1159 session_id2, session_id2_len,
1160 pctx->h_k_sid_sessid, pctx->h_k_sid_sessid_len) == 1)
1161 debug("%s: %s success", __func__, authctxt->method->name);
1162 else {
1163 debug("%s: confirmation mismatch", __func__);
1164 /* XXX stash this so if auth succeeds then we can warn/kill */
1165 }
1166
1167 userauth_jpake_cleanup(authctxt);
1168}
1169#endif /* JPAKE */
1170
Ben Lindstrombba81212001-06-25 05:01:22 +00001171static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001172identity_sign(Identity *id, u_char **sigp, u_int *lenp,
1173 u_char *data, u_int datalen)
1174{
1175 Key *prv;
1176 int ret;
1177
1178 /* the agent supports this key */
1179 if (id->ac)
1180 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
1181 data, datalen));
1182 /*
1183 * we have already loaded the private key or
1184 * the private key is stored in external hardware
1185 */
1186 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
1187 return (key_sign(id->key, sigp, lenp, data, datalen));
1188 /* load the private key from the file */
Damien Miller5ceddc32013-02-15 12:18:32 +11001189 if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
Damien Miller280ecfb2003-05-14 13:46:00 +10001190 return (-1);
1191 ret = key_sign(prv, sigp, lenp, data, datalen);
1192 key_free(prv);
1193 return (ret);
1194}
1195
1196static int
1197sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +10001198{
1199 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001200 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +00001201 u_int bloblen, slen;
Darren Tucker502d3842003-06-28 12:38:01 +10001202 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +10001203 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +11001204 int have_sig = 1;
Damien Miller4e270b02010-04-16 15:56:21 +10001205 char *fp;
Damien Millereba71ba2000-04-29 23:57:08 +10001206
Damien Miller4e270b02010-04-16 15:56:21 +10001207 fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
1208 debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
Darren Tuckera627d422013-06-02 07:31:17 +10001209 free(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001210
Damien Miller280ecfb2003-05-14 13:46:00 +10001211 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001212 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +00001213 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001214 return 0;
1215 }
Damien Millereba71ba2000-04-29 23:57:08 +10001216 /* data to be signed */
1217 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +11001218 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +10001219 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001220 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +11001221 } else {
1222 buffer_put_string(&b, session_id2, session_id2_len);
1223 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +10001224 }
Damien Millereba71ba2000-04-29 23:57:08 +10001225 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001226 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +10001227 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +00001228 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +10001229 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +11001230 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +00001231 if (datafellows & SSH_BUG_PKAUTH) {
1232 buffer_put_char(&b, have_sig);
1233 } else {
1234 buffer_put_cstring(&b, authctxt->method->name);
1235 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +10001236 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +00001237 }
Damien Millereba71ba2000-04-29 23:57:08 +10001238 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +10001239
1240 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +10001241 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001242 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +10001243 if (ret == -1) {
Darren Tuckera627d422013-06-02 07:31:17 +10001244 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +10001245 buffer_free(&b);
1246 return 0;
1247 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001248#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +10001249 buffer_dump(&b);
1250#endif
Ben Lindstromd121f612000-12-03 17:00:47 +00001251 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +10001252 buffer_clear(&b);
1253 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001254 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +10001255 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +11001256 buffer_put_cstring(&b, authctxt->server_user);
1257 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +11001258 buffer_put_cstring(&b, authctxt->method->name);
1259 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +00001260 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001261 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +10001262 buffer_put_string(&b, blob, bloblen);
1263 }
Darren Tuckera627d422013-06-02 07:31:17 +10001264 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001265
Damien Millereba71ba2000-04-29 23:57:08 +10001266 /* append signature */
1267 buffer_put_string(&b, signature, slen);
Darren Tuckera627d422013-06-02 07:31:17 +10001268 free(signature);
Damien Millereba71ba2000-04-29 23:57:08 +10001269
1270 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +10001271 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +11001272 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +10001273 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +10001274
1275 /* put remaining data from buffer into packet */
1276 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1277 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1278 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +10001279 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +10001280
1281 return 1;
Damien Miller994cf142000-07-21 10:19:44 +10001282}
1283
Ben Lindstrombba81212001-06-25 05:01:22 +00001284static int
Damien Miller280ecfb2003-05-14 13:46:00 +10001285send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +10001286{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001287 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001288 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +10001289
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001290 debug3("send_pubkey_test");
1291
Damien Miller280ecfb2003-05-14 13:46:00 +10001292 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001293 /* we cannot handle this key */
1294 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +10001295 return 0;
1296 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001297 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001298 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +10001299
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001300 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1301 packet_put_cstring(authctxt->server_user);
1302 packet_put_cstring(authctxt->service);
1303 packet_put_cstring(authctxt->method->name);
1304 packet_put_char(have_sig);
1305 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +10001306 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001307 packet_put_string(blob, bloblen);
Darren Tuckera627d422013-06-02 07:31:17 +10001308 free(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001309 packet_send();
1310 return 1;
1311}
1312
Ben Lindstrombba81212001-06-25 05:01:22 +00001313static Key *
Damien Miller5ceddc32013-02-15 12:18:32 +11001314load_identity_file(char *filename, int userprovided)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001315{
1316 Key *private;
1317 char prompt[300], *passphrase;
Darren Tuckerd4c86b12010-01-12 19:41:22 +11001318 int perm_ok = 0, quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +00001319 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001320
Ben Lindstrom329782e2001-03-10 17:08:59 +00001321 if (stat(filename, &st) < 0) {
Damien Miller5ceddc32013-02-15 12:18:32 +11001322 (userprovided ? logit : debug3)("no such identity: %s: %s",
1323 filename, strerror(errno));
Ben Lindstrom329782e2001-03-10 17:08:59 +00001324 return NULL;
1325 }
Darren Tucker232b76f2006-05-06 17:41:51 +10001326 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
Darren Tuckere52a2602013-06-06 08:22:05 +10001327 if (!perm_ok) {
1328 if (private != NULL)
1329 key_free(private);
Darren Tucker232b76f2006-05-06 17:41:51 +10001330 return NULL;
Darren Tuckere52a2602013-06-06 08:22:05 +10001331 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001332 if (private == NULL) {
1333 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001334 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +10001335 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +11001336 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +11001337 for (i = 0; i < options.number_of_password_prompts; i++) {
1338 passphrase = read_passphrase(prompt, 0);
1339 if (strcmp(passphrase, "") != 0) {
Darren Tucker232b76f2006-05-06 17:41:51 +10001340 private = key_load_private_type(KEY_UNSPEC,
1341 filename, passphrase, NULL, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001342 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +11001343 } else {
1344 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001345 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +11001346 }
1347 memset(passphrase, 0, strlen(passphrase));
Darren Tuckera627d422013-06-02 07:31:17 +10001348 free(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001349 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +11001350 break;
1351 debug2("bad passphrase given, try again...");
1352 }
Damien Miller994cf142000-07-21 10:19:44 +10001353 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001354 return private;
1355}
1356
Damien Miller280ecfb2003-05-14 13:46:00 +10001357/*
1358 * try keys in the following order:
1359 * 1. agent keys that are found in the config file
1360 * 2. other agent keys
1361 * 3. keys that are only listed in the config file
1362 */
1363static void
1364pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001365{
Damien Millercb6b68b2012-12-03 09:49:52 +11001366 Identity *id, *id2, *tmp;
Damien Miller280ecfb2003-05-14 13:46:00 +10001367 Idlist agent, files, *preferred;
1368 Key *key;
1369 AuthenticationConnection *ac;
Damien Millerad833b32000-08-23 10:46:23 +10001370 char *comment;
Damien Miller280ecfb2003-05-14 13:46:00 +10001371 int i, found;
Damien Millerad833b32000-08-23 10:46:23 +10001372
Damien Miller280ecfb2003-05-14 13:46:00 +10001373 TAILQ_INIT(&agent); /* keys from the agent */
1374 TAILQ_INIT(&files); /* keys from the config file */
1375 preferred = &authctxt->keys;
1376 TAILQ_INIT(preferred); /* preferred order of keys */
1377
Damien Millercb6b68b2012-12-03 09:49:52 +11001378 /* list of keys stored in the filesystem and PKCS#11 */
Damien Miller280ecfb2003-05-14 13:46:00 +10001379 for (i = 0; i < options.num_identity_files; i++) {
1380 key = options.identity_keys[i];
1381 if (key && key->type == KEY_RSA1)
1382 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +11001383 if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1384 continue;
Damien Miller280ecfb2003-05-14 13:46:00 +10001385 options.identity_keys[i] = NULL;
Damien Miller07d86be2006-03-26 14:19:21 +11001386 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001387 id->key = key;
1388 id->filename = xstrdup(options.identity_files[i]);
Darren Tucker19104782013-04-05 11:13:08 +11001389 id->userprovided = options.identity_file_userprovided[i];
Damien Miller280ecfb2003-05-14 13:46:00 +10001390 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +10001391 }
Damien Millercb6b68b2012-12-03 09:49:52 +11001392 /* Prefer PKCS11 keys that are explicitly listed */
1393 TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1394 if (id->key == NULL || (id->key->flags & KEY_FLAG_EXT) == 0)
1395 continue;
1396 found = 0;
1397 TAILQ_FOREACH(id2, &files, next) {
1398 if (id2->key == NULL ||
1399 (id2->key->flags & KEY_FLAG_EXT) != 0)
1400 continue;
1401 if (key_equal(id->key, id2->key)) {
1402 TAILQ_REMOVE(&files, id, next);
1403 TAILQ_INSERT_TAIL(preferred, id, next);
1404 found = 1;
1405 break;
1406 }
1407 }
1408 /* If IdentitiesOnly set and key not found then don't use it */
1409 if (!found && options.identities_only) {
1410 TAILQ_REMOVE(&files, id, next);
Darren Tucker54da6be2013-05-16 20:25:04 +10001411 bzero(id, sizeof(*id));
Damien Millercb6b68b2012-12-03 09:49:52 +11001412 free(id);
1413 }
1414 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001415 /* list of keys supported by the agent */
1416 if ((ac = ssh_get_authentication_connection())) {
1417 for (key = ssh_get_first_identity(ac, &comment, 2);
1418 key != NULL;
1419 key = ssh_get_next_identity(ac, &comment, 2)) {
1420 found = 0;
1421 TAILQ_FOREACH(id, &files, next) {
Damien Millera8e06ce2003-11-21 23:48:55 +11001422 /* agent keys from the config file are preferred */
Damien Miller280ecfb2003-05-14 13:46:00 +10001423 if (key_equal(key, id->key)) {
1424 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +10001425 free(comment);
Damien Miller280ecfb2003-05-14 13:46:00 +10001426 TAILQ_REMOVE(&files, id, next);
1427 TAILQ_INSERT_TAIL(preferred, id, next);
1428 id->ac = ac;
1429 found = 1;
1430 break;
1431 }
1432 }
Damien Millerbd394c32004-03-08 23:12:36 +11001433 if (!found && !options.identities_only) {
Damien Miller07d86be2006-03-26 14:19:21 +11001434 id = xcalloc(1, sizeof(*id));
Damien Miller280ecfb2003-05-14 13:46:00 +10001435 id->key = key;
1436 id->filename = comment;
1437 id->ac = ac;
1438 TAILQ_INSERT_TAIL(&agent, id, next);
1439 }
1440 }
1441 /* append remaining agent keys */
1442 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1443 TAILQ_REMOVE(&agent, id, next);
1444 TAILQ_INSERT_TAIL(preferred, id, next);
1445 }
1446 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +11001447 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001448 /* append remaining keys from the config file */
1449 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1450 TAILQ_REMOVE(&files, id, next);
1451 TAILQ_INSERT_TAIL(preferred, id, next);
1452 }
1453 TAILQ_FOREACH(id, preferred, next) {
Damien Miller5ceddc32013-02-15 12:18:32 +11001454 debug2("key: %s (%p),%s", id->filename, id->key,
1455 id->userprovided ? " explicit" : "");
Damien Miller280ecfb2003-05-14 13:46:00 +10001456 }
1457}
1458
1459static void
1460pubkey_cleanup(Authctxt *authctxt)
1461{
1462 Identity *id;
1463
1464 if (authctxt->agent != NULL)
1465 ssh_close_authentication_connection(authctxt->agent);
1466 for (id = TAILQ_FIRST(&authctxt->keys); id;
1467 id = TAILQ_FIRST(&authctxt->keys)) {
1468 TAILQ_REMOVE(&authctxt->keys, id, next);
1469 if (id->key)
1470 key_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +10001471 free(id->filename);
1472 free(id);
Damien Miller280ecfb2003-05-14 13:46:00 +10001473 }
Damien Millereba71ba2000-04-29 23:57:08 +10001474}
1475
Damien Miller62cee002000-09-23 17:15:56 +11001476int
1477userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +10001478{
Damien Miller280ecfb2003-05-14 13:46:00 +10001479 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +11001480 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +10001481
Damien Miller280ecfb2003-05-14 13:46:00 +10001482 while ((id = TAILQ_FIRST(&authctxt->keys))) {
1483 if (id->tried++)
1484 return (0);
Darren Tuckerd05b6012003-10-15 15:55:59 +10001485 /* move key to the end of the queue */
Damien Miller280ecfb2003-05-14 13:46:00 +10001486 TAILQ_REMOVE(&authctxt->keys, id, next);
1487 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1488 /*
1489 * send a test message if we have the public key. for
1490 * encrypted keys we cannot do this and have to load the
1491 * private key instead
1492 */
Damien Miller324541e2013-12-31 12:25:40 +11001493 if (id->key != NULL) {
1494 if (key_type_plain(id->key->type) == KEY_RSA &&
1495 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1496 debug("Skipped %s key %s for RSA/MD5 server",
1497 key_type(id->key), id->filename);
1498 } else if (id->key->type != KEY_RSA1) {
1499 debug("Offering %s public key: %s",
1500 key_type(id->key), id->filename);
1501 sent = send_pubkey_test(authctxt, id);
1502 }
1503 } else {
Damien Miller280ecfb2003-05-14 13:46:00 +10001504 debug("Trying private key: %s", id->filename);
Damien Miller5ceddc32013-02-15 12:18:32 +11001505 id->key = load_identity_file(id->filename,
1506 id->userprovided);
Damien Miller280ecfb2003-05-14 13:46:00 +10001507 if (id->key != NULL) {
1508 id->isprivate = 1;
Damien Miller324541e2013-12-31 12:25:40 +11001509 if (key_type_plain(id->key->type) == KEY_RSA &&
1510 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1511 debug("Skipped %s key %s for RSA/MD5 "
1512 "server", key_type(id->key),
1513 id->filename);
1514 } else {
1515 sent = sign_and_send_pubkey(
1516 authctxt, id);
1517 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001518 key_free(id->key);
1519 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001520 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +00001521 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001522 if (sent)
1523 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001524 }
Damien Miller280ecfb2003-05-14 13:46:00 +10001525 return (0);
Damien Miller62cee002000-09-23 17:15:56 +11001526}
Damien Millereba71ba2000-04-29 23:57:08 +10001527
Damien Miller874d77b2000-10-14 16:23:11 +11001528/*
1529 * Send userauth request message specifying keyboard-interactive method.
1530 */
1531int
1532userauth_kbdint(Authctxt *authctxt)
1533{
1534 static int attempt = 0;
1535
1536 if (attempt++ >= options.number_of_password_prompts)
1537 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +00001538 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1539 if (attempt > 1 && !authctxt->info_req_seen) {
1540 debug3("userauth_kbdint: disable: no info_req_seen");
1541 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1542 return 0;
1543 }
Damien Miller874d77b2000-10-14 16:23:11 +11001544
1545 debug2("userauth_kbdint");
1546 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1547 packet_put_cstring(authctxt->server_user);
1548 packet_put_cstring(authctxt->service);
1549 packet_put_cstring(authctxt->method->name);
1550 packet_put_cstring(""); /* lang */
1551 packet_put_cstring(options.kbd_interactive_devices ?
1552 options.kbd_interactive_devices : "");
1553 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001554
1555 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1556 return 1;
1557}
1558
1559/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001560 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +11001561 */
1562void
Damien Miller630d6f42002-01-22 23:17:30 +11001563input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +11001564{
1565 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +00001566 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001567 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +11001568 int echo = 0;
1569
1570 debug2("input_userauth_info_req");
1571
1572 if (authctxt == NULL)
1573 fatal("input_userauth_info_req: no authentication context");
1574
Ben Lindstrom7d199962001-09-12 18:29:00 +00001575 authctxt->info_req_seen = 1;
1576
Damien Miller874d77b2000-10-14 16:23:11 +11001577 name = packet_get_string(NULL);
1578 inst = packet_get_string(NULL);
1579 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +11001580 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001581 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +11001582 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +10001583 logit("%s", inst);
Darren Tuckera627d422013-06-02 07:31:17 +10001584 free(name);
1585 free(inst);
1586 free(lang);
Damien Miller874d77b2000-10-14 16:23:11 +11001587
1588 num_prompts = packet_get_int();
1589 /*
1590 * Begin to build info response packet based on prompts requested.
1591 * We commit to providing the correct number of responses, so if
1592 * further on we run into a problem that prevents this, we have to
1593 * be sure and clean this up and send a correct error response.
1594 */
1595 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1596 packet_put_int(num_prompts);
1597
Ben Lindstrom551ea372001-06-05 18:56:16 +00001598 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +11001599 for (i = 0; i < num_prompts; i++) {
1600 prompt = packet_get_string(NULL);
1601 echo = packet_get_char();
1602
Ben Lindstrom949974b2001-06-25 05:20:31 +00001603 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +11001604
Ben Lindstrom5699c5f2001-03-05 06:17:49 +00001605 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +11001606 memset(response, 0, strlen(response));
Darren Tuckera627d422013-06-02 07:31:17 +10001607 free(response);
1608 free(prompt);
Damien Miller874d77b2000-10-14 16:23:11 +11001609 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001610 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +11001611
Damien Miller9f643902001-11-12 11:02:52 +11001612 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +11001613 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +11001614}
Damien Miller62cee002000-09-23 17:15:56 +11001615
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001616static int
Ben Lindstrom5c385522002-06-23 21:23:20 +00001617ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001618 u_char *data, u_int datalen)
1619{
1620 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +00001621 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001622 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001623 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001624
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001625 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001626
Ben Lindstrom5206b952002-06-06 19:59:29 +00001627 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001628 error("ssh_keysign: not installed: %s", strerror(errno));
Ben Lindstrom5206b952002-06-06 19:59:29 +00001629 return -1;
1630 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001631 if (fflush(stdout) != 0)
1632 error("ssh_keysign: fflush: %s", strerror(errno));
1633 if (pipe(to) < 0) {
1634 error("ssh_keysign: pipe: %s", strerror(errno));
1635 return -1;
1636 }
1637 if (pipe(from) < 0) {
1638 error("ssh_keysign: pipe: %s", strerror(errno));
1639 return -1;
1640 }
1641 if ((pid = fork()) < 0) {
1642 error("ssh_keysign: fork: %s", strerror(errno));
1643 return -1;
1644 }
1645 if (pid == 0) {
Darren Tucker6e7fe1c2010-01-08 17:07:22 +11001646 /* keep the socket on exec */
1647 fcntl(packet_get_connection_in(), F_SETFD, 0);
Damien Miller2e5fe882006-06-13 13:10:00 +10001648 permanently_drop_suid(getuid());
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001649 close(from[0]);
1650 if (dup2(from[1], STDOUT_FILENO) < 0)
1651 fatal("ssh_keysign: dup2: %s", strerror(errno));
1652 close(to[1]);
1653 if (dup2(to[0], STDIN_FILENO) < 0)
1654 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001655 close(from[1]);
1656 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001657 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001658 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1659 strerror(errno));
1660 }
1661 close(from[1]);
1662 close(to[0]);
1663
1664 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001665 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001666 buffer_put_string(&b, data, datalen);
Damien Miller51bf11f2003-11-17 21:20:47 +11001667 if (ssh_msg_send(to[1], version, &b) == -1)
1668 fatal("ssh_keysign: couldn't send request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001669
Damien Miller901119b2002-10-04 11:10:04 +10001670 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001671 error("ssh_keysign: no reply");
Damien Millerfb1310e2004-01-21 11:02:50 +11001672 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001673 return -1;
1674 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001675 close(from[0]);
1676 close(to[1]);
1677
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001678 while (waitpid(pid, &status, 0) < 0)
1679 if (errno != EINTR)
1680 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001681
Ben Lindstrom5206b952002-06-06 19:59:29 +00001682 if (buffer_get_char(&b) != version) {
1683 error("ssh_keysign: bad version");
Damien Millerfb1310e2004-01-21 11:02:50 +11001684 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001685 return -1;
1686 }
1687 *sigp = buffer_get_string(&b, lenp);
Damien Millerfb1310e2004-01-21 11:02:50 +11001688 buffer_free(&b);
Ben Lindstrom5206b952002-06-06 19:59:29 +00001689
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001690 return 0;
1691}
1692
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001693int
1694userauth_hostbased(Authctxt *authctxt)
1695{
1696 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001697 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001698 Buffer b;
1699 u_char *signature, *blob;
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001700 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001701 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001702 u_int blen, slen;
Darren Tucker5246df42010-01-08 18:50:46 +11001703 int ok, i, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001704
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001705 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001706 for (i = 0; i < sensitive->nkeys; i++) {
1707 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001708 if (private && private->type != KEY_RSA1) {
1709 found = 1;
1710 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001711 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001712 break;
1713 }
1714 }
1715 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001716 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001717 return 0;
1718 }
1719 if (key_to_blob(private, &blob, &blen) == 0) {
1720 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001721 return 0;
1722 }
Damien Miller91c18472001-11-12 11:02:03 +11001723 /* figure out a name for the client host */
Darren Tuckerdaaa4502010-01-13 22:43:33 +11001724 p = get_local_name(packet_get_connection_in());
Damien Miller91c18472001-11-12 11:02:03 +11001725 if (p == NULL) {
1726 error("userauth_hostbased: cannot get local ipaddr/name");
1727 key_free(private);
Darren Tuckera627d422013-06-02 07:31:17 +10001728 free(blob);
Damien Miller91c18472001-11-12 11:02:03 +11001729 return 0;
1730 }
Damien Miller07d86be2006-03-26 14:19:21 +11001731 xasprintf(&chost, "%s.", p);
Damien Miller91c18472001-11-12 11:02:03 +11001732 debug2("userauth_hostbased: chost %s", chost);
Darren Tuckera627d422013-06-02 07:31:17 +10001733 free(p);
Damien Miller91c18472001-11-12 11:02:03 +11001734
Ben Lindstrom671388f2001-04-19 20:40:45 +00001735 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1736 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001737 pkalg = xstrdup(key_ssh_name(private));
1738 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001739 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001740 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001741 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1742 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001743 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001744 buffer_put_cstring(&b, authctxt->method->name);
1745 buffer_put_cstring(&b, pkalg);
1746 buffer_put_string(&b, blob, blen);
1747 buffer_put_cstring(&b, chost);
1748 buffer_put_cstring(&b, authctxt->local_user);
1749#ifdef DEBUG_PK
1750 buffer_dump(&b);
1751#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001752 if (sensitive->external_keysign)
1753 ok = ssh_keysign(private, &signature, &slen,
1754 buffer_ptr(&b), buffer_len(&b));
1755 else
1756 ok = key_sign(private, &signature, &slen,
1757 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001758 key_free(private);
1759 buffer_free(&b);
1760 if (ok != 0) {
1761 error("key_sign failed");
Darren Tuckera627d422013-06-02 07:31:17 +10001762 free(chost);
1763 free(pkalg);
1764 free(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001765 return 0;
1766 }
1767 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1768 packet_put_cstring(authctxt->server_user);
1769 packet_put_cstring(authctxt->service);
1770 packet_put_cstring(authctxt->method->name);
1771 packet_put_cstring(pkalg);
1772 packet_put_string(blob, blen);
1773 packet_put_cstring(chost);
1774 packet_put_cstring(authctxt->local_user);
1775 packet_put_string(signature, slen);
1776 memset(signature, 's', slen);
Darren Tuckera627d422013-06-02 07:31:17 +10001777 free(signature);
1778 free(chost);
1779 free(pkalg);
1780 free(blob);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001781
1782 packet_send();
1783 return 1;
1784}
1785
Damien Miller01ed2272008-11-05 16:20:46 +11001786#ifdef JPAKE
1787int
1788userauth_jpake(Authctxt *authctxt)
1789{
1790 struct jpake_ctx *pctx;
1791 u_char *x1_proof, *x2_proof;
1792 u_int x1_proof_len, x2_proof_len;
1793 static int attempt = 0; /* XXX share with userauth_password's? */
1794
1795 if (attempt++ >= options.number_of_password_prompts)
1796 return 0;
1797 if (attempt != 1)
1798 error("Permission denied, please try again.");
1799
1800 if (authctxt->methoddata != NULL)
1801 fatal("%s: authctxt->methoddata already set (%p)",
1802 __func__, authctxt->methoddata);
1803
1804 authctxt->methoddata = pctx = jpake_new();
1805
1806 /*
1807 * Send request immediately, to get the protocol going while
1808 * we do the initial computations.
1809 */
1810 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1811 packet_put_cstring(authctxt->server_user);
1812 packet_put_cstring(authctxt->service);
1813 packet_put_cstring(authctxt->method->name);
1814 packet_send();
1815 packet_write_wait();
1816
1817 jpake_step1(pctx->grp,
1818 &pctx->client_id, &pctx->client_id_len,
1819 &pctx->x1, &pctx->x2, &pctx->g_x1, &pctx->g_x2,
1820 &x1_proof, &x1_proof_len,
1821 &x2_proof, &x2_proof_len);
1822
1823 JPAKE_DEBUG_CTX((pctx, "step 1 sending in %s", __func__));
1824
1825 packet_start(SSH2_MSG_USERAUTH_JPAKE_CLIENT_STEP1);
1826 packet_put_string(pctx->client_id, pctx->client_id_len);
1827 packet_put_bignum2(pctx->g_x1);
1828 packet_put_bignum2(pctx->g_x2);
1829 packet_put_string(x1_proof, x1_proof_len);
1830 packet_put_string(x2_proof, x2_proof_len);
1831 packet_send();
1832
1833 bzero(x1_proof, x1_proof_len);
1834 bzero(x2_proof, x2_proof_len);
Darren Tuckera627d422013-06-02 07:31:17 +10001835 free(x1_proof);
1836 free(x2_proof);
Damien Miller01ed2272008-11-05 16:20:46 +11001837
1838 /* Expect step 1 packet from peer */
1839 dispatch_set(SSH2_MSG_USERAUTH_JPAKE_SERVER_STEP1,
1840 input_userauth_jpake_server_step1);
Darren Tucker2f29a8c2009-10-24 11:47:58 +11001841 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS,
1842 &input_userauth_success_unexpected);
Damien Miller01ed2272008-11-05 16:20:46 +11001843
1844 return 1;
1845}
1846
1847void
1848userauth_jpake_cleanup(Authctxt *authctxt)
1849{
1850 debug3("%s: clean up", __func__);
1851 if (authctxt->methoddata != NULL) {
1852 jpake_free(authctxt->methoddata);
1853 authctxt->methoddata = NULL;
1854 }
Darren Tucker2f29a8c2009-10-24 11:47:58 +11001855 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
Damien Miller01ed2272008-11-05 16:20:46 +11001856}
1857#endif /* JPAKE */
1858
Damien Miller62cee002000-09-23 17:15:56 +11001859/* find auth method */
1860
Damien Miller62cee002000-09-23 17:15:56 +11001861/*
1862 * given auth method name, if configurable options permit this method fill
1863 * in auth_ident field and return true, otherwise return false.
1864 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001865static int
Damien Miller62cee002000-09-23 17:15:56 +11001866authmethod_is_enabled(Authmethod *method)
1867{
1868 if (method == NULL)
1869 return 0;
1870 /* return false if options indicate this method is disabled */
1871 if (method->enabled == NULL || *method->enabled == 0)
1872 return 0;
1873 /* return false if batch mode is enabled but method needs interactive mode */
1874 if (method->batch_flag != NULL && *method->batch_flag != 0)
1875 return 0;
1876 return 1;
1877}
1878
Ben Lindstrombba81212001-06-25 05:01:22 +00001879static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001880authmethod_lookup(const char *name)
1881{
1882 Authmethod *method = NULL;
1883 if (name != NULL)
1884 for (method = authmethods; method->name != NULL; method++)
1885 if (strcmp(name, method->name) == 0)
1886 return method;
1887 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1888 return NULL;
1889}
1890
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001891/* XXX internal state */
1892static Authmethod *current = NULL;
1893static char *supported = NULL;
1894static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001895
Damien Miller62cee002000-09-23 17:15:56 +11001896/*
1897 * Given the authentication method list sent by the server, return the
1898 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001899 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001900 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001901static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001902authmethod_get(char *authlist)
1903{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001904 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001905 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001906
Damien Miller62cee002000-09-23 17:15:56 +11001907 /* Use a suitable default if we're passed a nil list. */
1908 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001909 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001910
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001911 if (supported == NULL || strcmp(authlist, supported) != 0) {
1912 debug3("start over, passed a different list %s", authlist);
Darren Tuckera627d422013-06-02 07:31:17 +10001913 free(supported);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001914 supported = xstrdup(authlist);
1915 preferred = options.preferred_authentications;
1916 debug3("preferred %s", preferred);
1917 current = NULL;
1918 } else if (current != NULL && authmethod_is_enabled(current))
1919 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001920
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001921 for (;;) {
1922 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001923 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001924 current = NULL;
1925 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001926 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001927 preferred += next;
1928 debug3("authmethod_lookup %s", name);
1929 debug3("remaining preferred: %s", preferred);
1930 if ((current = authmethod_lookup(name)) != NULL &&
1931 authmethod_is_enabled(current)) {
1932 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001933 debug("Next authentication method: %s", name);
Darren Tuckera627d422013-06-02 07:31:17 +10001934 free(name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001935 return current;
1936 }
Darren Tuckere52a2602013-06-06 08:22:05 +10001937 free(name);
Damien Millereba71ba2000-04-29 23:57:08 +10001938 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001939}
Damien Miller62cee002000-09-23 17:15:56 +11001940
Ben Lindstrom79073822001-07-04 03:42:30 +00001941static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001942authmethods_get(void)
1943{
1944 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001945 Buffer b;
1946 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001947
Damien Miller0e3b8722002-01-22 23:26:38 +11001948 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001949 for (method = authmethods; method->name != NULL; method++) {
1950 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001951 if (buffer_len(&b) > 0)
1952 buffer_append(&b, ",", 1);
1953 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001954 }
Damien Miller62cee002000-09-23 17:15:56 +11001955 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001956 buffer_append(&b, "\0", 1);
1957 list = xstrdup(buffer_ptr(&b));
1958 buffer_free(&b);
1959 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001960}
Damien Miller01ed2272008-11-05 16:20:46 +11001961