blob: 6a0bd409ac65be1e77e8e81165637d9729cd01f8 [file] [log] [blame]
Damien Millereba71ba2000-04-29 23:57:08 +10001/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
Damien Millereba71ba2000-04-29 23:57:08 +100012 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "includes.h"
Darren Tucker502d3842003-06-28 12:38:01 +100026RCSID("$OpenBSD: sshconnect2.c,v 1.120 2003/06/24 08:23:46 markus Exp $");
Damien Miller3ab496b2003-05-14 13:47:37 +100027
28#ifdef KRB5
29#include <krb5.h>
Damien Miller9c617692003-05-14 14:31:11 +100030#ifndef HEIMDAL
31#define krb5_get_err_text(context,code) error_message(code)
32#endif /* !HEIMDAL */
Damien Miller3ab496b2003-05-14 13:47:37 +100033#endif
Damien Millereba71ba2000-04-29 23:57:08 +100034
Damien Miller9c617692003-05-14 14:31:11 +100035#include "openbsd-compat/sys-queue.h"
36
Damien Millereba71ba2000-04-29 23:57:08 +100037#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000038#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100039#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#include "buffer.h"
41#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042#include "compat.h"
Damien Millereba71ba2000-04-29 23:57:08 +100043#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000044#include "cipher.h"
Damien Millereba71ba2000-04-29 23:57:08 +100045#include "kex.h"
46#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100047#include "sshconnect.h"
48#include "authfile.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000049#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100050#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "log.h"
52#include "readconf.h"
53#include "readpass.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000054#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000055#include "dispatch.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000056#include "canohost.h"
Ben Lindstrom1bad2562002-06-06 19:57:33 +000057#include "msg.h"
58#include "pathnames.h"
Damien Miller874d77b2000-10-14 16:23:11 +110059
Damien Millereba71ba2000-04-29 23:57:08 +100060/* import */
61extern char *client_version_string;
62extern char *server_version_string;
63extern Options options;
64
65/*
66 * SSH2 key exchange
67 */
68
Ben Lindstrom46c16222000-12-22 01:43:59 +000069u_char *session_id2 = NULL;
Darren Tucker502d3842003-06-28 12:38:01 +100070u_int session_id2_len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +100071
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000072char *xxx_host;
73struct sockaddr *xxx_hostaddr;
74
Ben Lindstromf28f6342001-04-04 02:03:04 +000075Kex *xxx_kex = NULL;
76
Ben Lindstrombba81212001-06-25 05:01:22 +000077static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000078verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000079{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000080 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +100081 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000082 return 0;
83}
84
Damien Millereba71ba2000-04-29 23:57:08 +100085void
Damien Miller874d77b2000-10-14 16:23:11 +110086ssh_kex2(char *host, struct sockaddr *hostaddr)
87{
Damien Miller874d77b2000-10-14 16:23:11 +110088 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000089
90 xxx_host = host;
91 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +110092
Damien Millere39cacc2000-11-29 12:18:44 +110093 if (options.ciphers == (char *)-1) {
Damien Miller996acd22003-04-09 20:59:48 +100094 logit("No valid ciphers for protocol version 2 given, using defaults.");
Damien Millere39cacc2000-11-29 12:18:44 +110095 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +110096 }
97 if (options.ciphers != NULL) {
98 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
99 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
100 }
Damien Millera0ff4662001-03-30 10:49:35 +1000101 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
102 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
103 myproposal[PROPOSAL_ENC_ALGS_STOC] =
104 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100105 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000106 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Ben Lindstrom343010a2002-07-04 00:16:25 +0000107 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100108 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000109 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Ben Lindstrom343010a2002-07-04 00:16:25 +0000110 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100111 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000112 if (options.macs != NULL) {
113 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
114 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
115 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000116 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100117 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000118 options.hostkeyalgorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100119
Damien Millera5539d22003-04-09 20:50:06 +1000120 if (options.rekey_limit)
121 packet_set_rekey_limit(options.rekey_limit);
122
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000123 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000124 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100125 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
126 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000127 kex->client_version_string=client_version_string;
128 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000129 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100130
Ben Lindstromf28f6342001-04-04 02:03:04 +0000131 xxx_kex = kex;
132
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000133 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100134
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000135 session_id2 = kex->session_id;
136 session_id2_len = kex->session_id_len;
137
Damien Miller874d77b2000-10-14 16:23:11 +1100138#ifdef DEBUG_KEXDH
139 /* send 1st encrypted/maced/compressed message */
140 packet_start(SSH2_MSG_IGNORE);
141 packet_put_cstring("markus");
142 packet_send();
143 packet_write_wait();
144#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000145}
Damien Millerb1715dc2000-05-30 13:44:51 +1000146
Damien Millereba71ba2000-04-29 23:57:08 +1000147/*
148 * Authenticate user
149 */
Damien Miller62cee002000-09-23 17:15:56 +1100150
151typedef struct Authctxt Authctxt;
152typedef struct Authmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000153typedef struct identity Identity;
154typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100155
Damien Miller280ecfb2003-05-14 13:46:00 +1000156struct identity {
157 TAILQ_ENTRY(identity) next;
158 AuthenticationConnection *ac; /* set if agent supports key */
159 Key *key; /* public/private key */
160 char *filename; /* comment for agent-only keys */
161 int tried;
162 int isprivate; /* key points to the private key */
163};
164TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100165
166struct Authctxt {
167 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000168 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100169 const char *host;
170 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100171 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100172 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000173 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000174 /* pubkey */
Damien Miller280ecfb2003-05-14 13:46:00 +1000175 Idlist keys;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000176 AuthenticationConnection *agent;
177 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000178 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000179 /* kbd-interactive */
180 int info_req_seen;
Damien Miller62cee002000-09-23 17:15:56 +1100181};
182struct Authmethod {
183 char *name; /* string to compare against server's list */
184 int (*userauth)(Authctxt *authctxt);
185 int *enabled; /* flag in option struct that enables method */
186 int *batch_flag; /* flag in option struct that disables method */
187};
188
Damien Miller630d6f42002-01-22 23:17:30 +1100189void input_userauth_success(int, u_int32_t, void *);
190void input_userauth_failure(int, u_int32_t, void *);
191void input_userauth_banner(int, u_int32_t, void *);
192void input_userauth_error(int, u_int32_t, void *);
193void input_userauth_info_req(int, u_int32_t, void *);
194void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000195void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100196
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000197int userauth_none(Authctxt *);
198int userauth_pubkey(Authctxt *);
199int userauth_passwd(Authctxt *);
200int userauth_kbdint(Authctxt *);
201int userauth_hostbased(Authctxt *);
Damien Miller3ab496b2003-05-14 13:47:37 +1000202int userauth_kerberos(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100203
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000204void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000205
Damien Miller280ecfb2003-05-14 13:46:00 +1000206static int sign_and_send_pubkey(Authctxt *, Identity *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000207static void pubkey_prepare(Authctxt *);
208static void pubkey_cleanup(Authctxt *);
209static Key *load_identity_file(char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000210
Ben Lindstrombba81212001-06-25 05:01:22 +0000211static Authmethod *authmethod_get(char *authlist);
212static Authmethod *authmethod_lookup(const char *name);
213static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100214
215Authmethod authmethods[] = {
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000216 {"hostbased",
217 userauth_hostbased,
218 &options.hostbased_authentication,
219 NULL},
Damien Miller3ab496b2003-05-14 13:47:37 +1000220#if KRB5
221 {"kerberos-2@ssh.com",
222 userauth_kerberos,
223 &options.kerberos_authentication,
224 NULL},
225#endif
Ben Lindstrom45350e82001-08-06 20:57:11 +0000226 {"publickey",
227 userauth_pubkey,
228 &options.pubkey_authentication,
229 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100230 {"keyboard-interactive",
231 userauth_kbdint,
232 &options.kbd_interactive_authentication,
233 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000234 {"password",
235 userauth_passwd,
236 &options.password_authentication,
237 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100238 {"none",
239 userauth_none,
240 NULL,
241 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100242 {NULL, NULL, NULL, NULL}
243};
244
245void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000246ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000247 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100248{
249 Authctxt authctxt;
250 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100251
Ben Lindstrom551ea372001-06-05 18:56:16 +0000252 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000253 options.kbd_interactive_authentication = 1;
254
Damien Miller62cee002000-09-23 17:15:56 +1100255 packet_start(SSH2_MSG_SERVICE_REQUEST);
256 packet_put_cstring("ssh-userauth");
257 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000258 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100259 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100260 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000261 if (type != SSH2_MSG_SERVICE_ACCEPT)
262 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100263 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100264 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000265 debug2("service_accept: %s", reply);
Damien Miller62cee002000-09-23 17:15:56 +1100266 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100267 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000268 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100269 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100270 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000271 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100272
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000273 if (options.preferred_authentications == NULL)
274 options.preferred_authentications = authmethods_get();
275
Damien Miller62cee002000-09-23 17:15:56 +1100276 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000277 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000278 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100279 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000280 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100281 authctxt.host = host;
282 authctxt.service = "ssh-connection"; /* service name */
283 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100284 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000285 authctxt.authlist = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000286 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000287 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100288 if (authctxt.method == NULL)
289 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100290
291 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100292 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100293
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000294 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100295 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
296 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000297 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100298 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
299
Damien Miller280ecfb2003-05-14 13:46:00 +1000300 pubkey_cleanup(&authctxt);
Damien Millerf842fcb2003-05-15 12:01:28 +1000301 dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
302
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000303 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100304}
Damien Millerf842fcb2003-05-15 12:01:28 +1000305
Damien Miller62cee002000-09-23 17:15:56 +1100306void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000307userauth(Authctxt *authctxt, char *authlist)
308{
309 if (authlist == NULL) {
310 authlist = authctxt->authlist;
311 } else {
312 if (authctxt->authlist)
313 xfree(authctxt->authlist);
314 authctxt->authlist = authlist;
315 }
316 for (;;) {
317 Authmethod *method = authmethod_get(authlist);
318 if (method == NULL)
319 fatal("Permission denied (%s).", authlist);
320 authctxt->method = method;
Damien Millerf842fcb2003-05-15 12:01:28 +1000321
322 /* reset the per method handler */
323 dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
324 SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
325
326 /* and try new method */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000327 if (method->userauth(authctxt) != 0) {
328 debug2("we sent a %s packet, wait for reply", method->name);
329 break;
330 } else {
331 debug2("we did not send a packet, disable method");
332 method->enabled = NULL;
333 }
334 }
335}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000336
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000337void
Damien Miller630d6f42002-01-22 23:17:30 +1100338input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100339{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000340 fatal("input_userauth_error: bad message during authentication: "
341 "type %d", type);
342}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000343
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000344void
Damien Miller630d6f42002-01-22 23:17:30 +1100345input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000346{
347 char *msg, *lang;
348 debug3("input_userauth_banner");
349 msg = packet_get_string(NULL);
350 lang = packet_get_string(NULL);
351 fprintf(stderr, "%s", msg);
352 xfree(msg);
353 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100354}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000355
Damien Miller62cee002000-09-23 17:15:56 +1100356void
Damien Miller630d6f42002-01-22 23:17:30 +1100357input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100358{
359 Authctxt *authctxt = ctxt;
360 if (authctxt == NULL)
361 fatal("input_userauth_success: no authentication context");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000362 if (authctxt->authlist)
363 xfree(authctxt->authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100364 authctxt->success = 1; /* break out */
365}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000366
Damien Miller62cee002000-09-23 17:15:56 +1100367void
Damien Miller630d6f42002-01-22 23:17:30 +1100368input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100369{
Damien Miller62cee002000-09-23 17:15:56 +1100370 Authctxt *authctxt = ctxt;
371 char *authlist = NULL;
372 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100373
374 if (authctxt == NULL)
375 fatal("input_userauth_failure: no authentication context");
376
Damien Miller874d77b2000-10-14 16:23:11 +1100377 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100378 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100379 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100380
381 if (partial != 0)
Damien Miller996acd22003-04-09 20:59:48 +1000382 logit("Authenticated with partial success.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000383 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100384
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000385 userauth(authctxt, authlist);
386}
387void
Damien Miller630d6f42002-01-22 23:17:30 +1100388input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000389{
390 Authctxt *authctxt = ctxt;
391 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000392 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000393 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000394 int pktype, sent = 0;
395 u_int alen, blen;
396 char *pkalg, *fp;
397 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000398
399 if (authctxt == NULL)
400 fatal("input_userauth_pk_ok: no authentication context");
401 if (datafellows & SSH_BUG_PKOK) {
402 /* this is similar to SSH_BUG_PKAUTH */
403 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
404 pkblob = packet_get_string(&blen);
405 buffer_init(&b);
406 buffer_append(&b, pkblob, blen);
407 pkalg = buffer_get_string(&b, &alen);
408 buffer_free(&b);
409 } else {
410 pkalg = packet_get_string(&alen);
411 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000412 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100413 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000414
Damien Miller280ecfb2003-05-14 13:46:00 +1000415 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000416
Damien Miller280ecfb2003-05-14 13:46:00 +1000417 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
418 debug("unknown pkalg %s", pkalg);
419 goto done;
420 }
421 if ((key = key_from_blob(pkblob, blen)) == NULL) {
422 debug("no key from blob. pkalg %s", pkalg);
423 goto done;
424 }
425 if (key->type != pktype) {
426 error("input_userauth_pk_ok: type mismatch "
427 "for decoded key (received %d, expected %d)",
428 key->type, pktype);
429 goto done;
430 }
431 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
432 debug2("input_userauth_pk_ok: fp %s", fp);
433 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000434
Damien Miller280ecfb2003-05-14 13:46:00 +1000435 TAILQ_FOREACH(id, &authctxt->keys, next) {
436 if (key_equal(key, id->key)) {
437 sent = sign_and_send_pubkey(authctxt, id);
438 break;
439 }
440 }
441done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000442 if (key != NULL)
443 key_free(key);
444 xfree(pkalg);
445 xfree(pkblob);
446
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000447 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000448 if (sent == 0)
449 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100450}
451
Damien Millereba71ba2000-04-29 23:57:08 +1000452int
Damien Miller874d77b2000-10-14 16:23:11 +1100453userauth_none(Authctxt *authctxt)
454{
455 /* initial userauth request */
456 packet_start(SSH2_MSG_USERAUTH_REQUEST);
457 packet_put_cstring(authctxt->server_user);
458 packet_put_cstring(authctxt->service);
459 packet_put_cstring(authctxt->method->name);
460 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100461 return 1;
462}
463
464int
Damien Miller62cee002000-09-23 17:15:56 +1100465userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000466{
Damien Millere247cc42000-05-07 12:03:14 +1000467 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000468 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000469 char *password;
470
Damien Millerd3a18572000-06-07 19:55:44 +1000471 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000472 return 0;
473
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000474 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000475 error("Permission denied, please try again.");
476
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000477 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100478 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000479 password = read_passphrase(prompt, 0);
480 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100481 packet_put_cstring(authctxt->server_user);
482 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100483 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000484 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000485 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000486 memset(password, 0, strlen(password));
487 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100488 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000489 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000490
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000491 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000492 &input_userauth_passwd_changereq);
493
Damien Millereba71ba2000-04-29 23:57:08 +1000494 return 1;
495}
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000496/*
497 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
498 */
499void
Tim Ricec8549622002-03-31 12:49:38 -0800500input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000501{
502 Authctxt *authctxt = ctxt;
503 char *info, *lang, *password = NULL, *retype = NULL;
504 char prompt[150];
505
506 debug2("input_userauth_passwd_changereq");
507
508 if (authctxt == NULL)
509 fatal("input_userauth_passwd_changereq: "
510 "no authentication context");
511
512 info = packet_get_string(NULL);
513 lang = packet_get_string(NULL);
514 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000515 logit("%s", info);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000516 xfree(info);
517 xfree(lang);
518 packet_start(SSH2_MSG_USERAUTH_REQUEST);
519 packet_put_cstring(authctxt->server_user);
520 packet_put_cstring(authctxt->service);
521 packet_put_cstring(authctxt->method->name);
522 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000523 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000524 "Enter %.30s@%.128s's old password: ",
525 authctxt->server_user, authctxt->host);
526 password = read_passphrase(prompt, 0);
527 packet_put_cstring(password);
528 memset(password, 0, strlen(password));
529 xfree(password);
530 password = NULL;
531 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000532 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000533 "Enter %.30s@%.128s's new password: ",
534 authctxt->server_user, authctxt->host);
535 password = read_passphrase(prompt, RP_ALLOW_EOF);
536 if (password == NULL) {
537 /* bail out */
538 return;
539 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000540 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000541 "Retype %.30s@%.128s's new password: ",
542 authctxt->server_user, authctxt->host);
543 retype = read_passphrase(prompt, 0);
544 if (strcmp(password, retype) != 0) {
545 memset(password, 0, strlen(password));
546 xfree(password);
Damien Miller996acd22003-04-09 20:59:48 +1000547 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000548 password = NULL;
549 }
550 memset(retype, 0, strlen(retype));
551 xfree(retype);
552 }
553 packet_put_cstring(password);
554 memset(password, 0, strlen(password));
555 xfree(password);
556 packet_add_padding(64);
557 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000558
559 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000560 &input_userauth_passwd_changereq);
561}
Damien Millereba71ba2000-04-29 23:57:08 +1000562
Ben Lindstrombba81212001-06-25 05:01:22 +0000563static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000564identity_sign(Identity *id, u_char **sigp, u_int *lenp,
565 u_char *data, u_int datalen)
566{
567 Key *prv;
568 int ret;
569
570 /* the agent supports this key */
571 if (id->ac)
572 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
573 data, datalen));
574 /*
575 * we have already loaded the private key or
576 * the private key is stored in external hardware
577 */
578 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
579 return (key_sign(id->key, sigp, lenp, data, datalen));
580 /* load the private key from the file */
581 if ((prv = load_identity_file(id->filename)) == NULL)
582 return (-1);
583 ret = key_sign(prv, sigp, lenp, data, datalen);
584 key_free(prv);
585 return (ret);
586}
587
588static int
589sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +1000590{
591 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000592 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000593 u_int bloblen, slen;
Darren Tucker502d3842003-06-28 12:38:01 +1000594 u_int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000595 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100596 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000597
Ben Lindstromd121f612000-12-03 17:00:47 +0000598 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000599
Damien Miller280ecfb2003-05-14 13:46:00 +1000600 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100601 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000602 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100603 return 0;
604 }
Damien Millereba71ba2000-04-29 23:57:08 +1000605 /* data to be signed */
606 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100607 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000608 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000609 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100610 } else {
611 buffer_put_string(&b, session_id2, session_id2_len);
612 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000613 }
Damien Millereba71ba2000-04-29 23:57:08 +1000614 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100615 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000616 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000617 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000618 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100619 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000620 if (datafellows & SSH_BUG_PKAUTH) {
621 buffer_put_char(&b, have_sig);
622 } else {
623 buffer_put_cstring(&b, authctxt->method->name);
624 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +1000625 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +0000626 }
Damien Millereba71ba2000-04-29 23:57:08 +1000627 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000628
629 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +1000630 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000631 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000632 if (ret == -1) {
633 xfree(blob);
634 buffer_free(&b);
635 return 0;
636 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100637#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000638 buffer_dump(&b);
639#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000640 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000641 buffer_clear(&b);
642 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000643 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000644 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100645 buffer_put_cstring(&b, authctxt->server_user);
646 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100647 buffer_put_cstring(&b, authctxt->method->name);
648 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000649 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +1000650 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +1000651 buffer_put_string(&b, blob, bloblen);
652 }
653 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000654
Damien Millereba71ba2000-04-29 23:57:08 +1000655 /* append signature */
656 buffer_put_string(&b, signature, slen);
657 xfree(signature);
658
659 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000660 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100661 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000662 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000663
664 /* put remaining data from buffer into packet */
665 packet_start(SSH2_MSG_USERAUTH_REQUEST);
666 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
667 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +1000668 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000669
670 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000671}
672
Ben Lindstrombba81212001-06-25 05:01:22 +0000673static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000674send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +1000675{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000676 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000677 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000678
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000679 debug3("send_pubkey_test");
680
Damien Miller280ecfb2003-05-14 13:46:00 +1000681 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000682 /* we cannot handle this key */
683 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +1000684 return 0;
685 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000686 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000687 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +1000688
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000689 packet_start(SSH2_MSG_USERAUTH_REQUEST);
690 packet_put_cstring(authctxt->server_user);
691 packet_put_cstring(authctxt->service);
692 packet_put_cstring(authctxt->method->name);
693 packet_put_char(have_sig);
694 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +1000695 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000696 packet_put_string(blob, bloblen);
697 xfree(blob);
698 packet_send();
699 return 1;
700}
701
Ben Lindstrombba81212001-06-25 05:01:22 +0000702static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000703load_identity_file(char *filename)
704{
705 Key *private;
706 char prompt[300], *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000707 int quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000708 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000709
Ben Lindstrom329782e2001-03-10 17:08:59 +0000710 if (stat(filename, &st) < 0) {
711 debug3("no such identity: %s", filename);
712 return NULL;
713 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000714 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
715 if (private == NULL) {
716 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000717 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000718 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100719 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100720 for (i = 0; i < options.number_of_password_prompts; i++) {
721 passphrase = read_passphrase(prompt, 0);
722 if (strcmp(passphrase, "") != 0) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000723 private = key_load_private_type(KEY_UNSPEC, filename,
724 passphrase, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000725 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100726 } else {
727 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000728 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +1100729 }
730 memset(passphrase, 0, strlen(passphrase));
731 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000732 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +1100733 break;
734 debug2("bad passphrase given, try again...");
735 }
Damien Miller994cf142000-07-21 10:19:44 +1000736 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000737 return private;
738}
739
Damien Miller280ecfb2003-05-14 13:46:00 +1000740/*
741 * try keys in the following order:
742 * 1. agent keys that are found in the config file
743 * 2. other agent keys
744 * 3. keys that are only listed in the config file
745 */
746static void
747pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000748{
Damien Miller280ecfb2003-05-14 13:46:00 +1000749 Identity *id;
750 Idlist agent, files, *preferred;
751 Key *key;
752 AuthenticationConnection *ac;
Damien Millerad833b32000-08-23 10:46:23 +1000753 char *comment;
Damien Miller280ecfb2003-05-14 13:46:00 +1000754 int i, found;
Damien Millerad833b32000-08-23 10:46:23 +1000755
Damien Miller280ecfb2003-05-14 13:46:00 +1000756 TAILQ_INIT(&agent); /* keys from the agent */
757 TAILQ_INIT(&files); /* keys from the config file */
758 preferred = &authctxt->keys;
759 TAILQ_INIT(preferred); /* preferred order of keys */
760
761 /* list of keys stored in the filesystem */
762 for (i = 0; i < options.num_identity_files; i++) {
763 key = options.identity_keys[i];
764 if (key && key->type == KEY_RSA1)
765 continue;
766 options.identity_keys[i] = NULL;
767 id = xmalloc(sizeof(*id));
768 memset(id, 0, sizeof(*id));
769 id->key = key;
770 id->filename = xstrdup(options.identity_files[i]);
771 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +1000772 }
Damien Miller280ecfb2003-05-14 13:46:00 +1000773 /* list of keys supported by the agent */
774 if ((ac = ssh_get_authentication_connection())) {
775 for (key = ssh_get_first_identity(ac, &comment, 2);
776 key != NULL;
777 key = ssh_get_next_identity(ac, &comment, 2)) {
778 found = 0;
779 TAILQ_FOREACH(id, &files, next) {
780 /* agent keys from the config file are preferred */
781 if (key_equal(key, id->key)) {
782 key_free(key);
783 xfree(comment);
784 TAILQ_REMOVE(&files, id, next);
785 TAILQ_INSERT_TAIL(preferred, id, next);
786 id->ac = ac;
787 found = 1;
788 break;
789 }
790 }
791 if (!found) {
792 id = xmalloc(sizeof(*id));
793 memset(id, 0, sizeof(*id));
794 id->key = key;
795 id->filename = comment;
796 id->ac = ac;
797 TAILQ_INSERT_TAIL(&agent, id, next);
798 }
799 }
800 /* append remaining agent keys */
801 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
802 TAILQ_REMOVE(&agent, id, next);
803 TAILQ_INSERT_TAIL(preferred, id, next);
804 }
805 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +1100806 }
Damien Miller280ecfb2003-05-14 13:46:00 +1000807 /* append remaining keys from the config file */
808 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
809 TAILQ_REMOVE(&files, id, next);
810 TAILQ_INSERT_TAIL(preferred, id, next);
811 }
812 TAILQ_FOREACH(id, preferred, next) {
813 debug2("key: %s (%p)", id->filename, id->key);
814 }
815}
816
817static void
818pubkey_cleanup(Authctxt *authctxt)
819{
820 Identity *id;
821
822 if (authctxt->agent != NULL)
823 ssh_close_authentication_connection(authctxt->agent);
824 for (id = TAILQ_FIRST(&authctxt->keys); id;
825 id = TAILQ_FIRST(&authctxt->keys)) {
826 TAILQ_REMOVE(&authctxt->keys, id, next);
827 if (id->key)
828 key_free(id->key);
829 if (id->filename)
830 xfree(id->filename);
831 xfree(id);
832 }
Damien Millereba71ba2000-04-29 23:57:08 +1000833}
834
Damien Miller62cee002000-09-23 17:15:56 +1100835int
836userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000837{
Damien Miller280ecfb2003-05-14 13:46:00 +1000838 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +1100839 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000840
Damien Miller280ecfb2003-05-14 13:46:00 +1000841 while ((id = TAILQ_FIRST(&authctxt->keys))) {
842 if (id->tried++)
843 return (0);
844 TAILQ_REMOVE(&authctxt->keys, id, next);
845 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
846 /*
847 * send a test message if we have the public key. for
848 * encrypted keys we cannot do this and have to load the
849 * private key instead
850 */
851 if (id->key && id->key->type != KEY_RSA1) {
852 debug("Offering public key: %s", id->filename);
853 sent = send_pubkey_test(authctxt, id);
854 } else if (id->key == NULL) {
855 debug("Trying private key: %s", id->filename);
856 id->key = load_identity_file(id->filename);
857 if (id->key != NULL) {
858 id->isprivate = 1;
859 sent = sign_and_send_pubkey(authctxt, id);
860 key_free(id->key);
861 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000862 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000863 }
Damien Miller280ecfb2003-05-14 13:46:00 +1000864 if (sent)
865 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100866 }
Damien Miller280ecfb2003-05-14 13:46:00 +1000867 return (0);
Damien Miller62cee002000-09-23 17:15:56 +1100868}
Damien Millereba71ba2000-04-29 23:57:08 +1000869
Damien Miller874d77b2000-10-14 16:23:11 +1100870/*
871 * Send userauth request message specifying keyboard-interactive method.
872 */
873int
874userauth_kbdint(Authctxt *authctxt)
875{
876 static int attempt = 0;
877
878 if (attempt++ >= options.number_of_password_prompts)
879 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000880 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
881 if (attempt > 1 && !authctxt->info_req_seen) {
882 debug3("userauth_kbdint: disable: no info_req_seen");
883 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
884 return 0;
885 }
Damien Miller874d77b2000-10-14 16:23:11 +1100886
887 debug2("userauth_kbdint");
888 packet_start(SSH2_MSG_USERAUTH_REQUEST);
889 packet_put_cstring(authctxt->server_user);
890 packet_put_cstring(authctxt->service);
891 packet_put_cstring(authctxt->method->name);
892 packet_put_cstring(""); /* lang */
893 packet_put_cstring(options.kbd_interactive_devices ?
894 options.kbd_interactive_devices : "");
895 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100896
897 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
898 return 1;
899}
900
901/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000902 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +1100903 */
904void
Damien Miller630d6f42002-01-22 23:17:30 +1100905input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +1100906{
907 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000908 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000909 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +1100910 int echo = 0;
911
912 debug2("input_userauth_info_req");
913
914 if (authctxt == NULL)
915 fatal("input_userauth_info_req: no authentication context");
916
Ben Lindstrom7d199962001-09-12 18:29:00 +0000917 authctxt->info_req_seen = 1;
918
Damien Miller874d77b2000-10-14 16:23:11 +1100919 name = packet_get_string(NULL);
920 inst = packet_get_string(NULL);
921 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +1100922 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000923 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +1100924 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000925 logit("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000926 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +1100927 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000928 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100929
930 num_prompts = packet_get_int();
931 /*
932 * Begin to build info response packet based on prompts requested.
933 * We commit to providing the correct number of responses, so if
934 * further on we run into a problem that prevents this, we have to
935 * be sure and clean this up and send a correct error response.
936 */
937 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
938 packet_put_int(num_prompts);
939
Ben Lindstrom551ea372001-06-05 18:56:16 +0000940 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +1100941 for (i = 0; i < num_prompts; i++) {
942 prompt = packet_get_string(NULL);
943 echo = packet_get_char();
944
Ben Lindstrom949974b2001-06-25 05:20:31 +0000945 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +1100946
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000947 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +1100948 memset(response, 0, strlen(response));
949 xfree(response);
950 xfree(prompt);
951 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100952 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +1100953
Damien Miller9f643902001-11-12 11:02:52 +1100954 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +1100955 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100956}
Damien Miller62cee002000-09-23 17:15:56 +1100957
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000958static int
Ben Lindstrom5c385522002-06-23 21:23:20 +0000959ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000960 u_char *data, u_int datalen)
961{
962 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +0000963 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000964 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +0000965 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000966
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000967 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000968
Ben Lindstrom5206b952002-06-06 19:59:29 +0000969 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
970 error("ssh_keysign: no installed: %s", strerror(errno));
971 return -1;
972 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000973 if (fflush(stdout) != 0)
974 error("ssh_keysign: fflush: %s", strerror(errno));
975 if (pipe(to) < 0) {
976 error("ssh_keysign: pipe: %s", strerror(errno));
977 return -1;
978 }
979 if (pipe(from) < 0) {
980 error("ssh_keysign: pipe: %s", strerror(errno));
981 return -1;
982 }
983 if ((pid = fork()) < 0) {
984 error("ssh_keysign: fork: %s", strerror(errno));
985 return -1;
986 }
987 if (pid == 0) {
988 seteuid(getuid());
989 setuid(getuid());
990 close(from[0]);
991 if (dup2(from[1], STDOUT_FILENO) < 0)
992 fatal("ssh_keysign: dup2: %s", strerror(errno));
993 close(to[1]);
994 if (dup2(to[0], STDIN_FILENO) < 0)
995 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +0000996 close(from[1]);
997 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +0000998 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000999 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1000 strerror(errno));
1001 }
1002 close(from[1]);
1003 close(to[0]);
1004
1005 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001006 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001007 buffer_put_string(&b, data, datalen);
Damien Miller901119b2002-10-04 11:10:04 +10001008 ssh_msg_send(to[1], version, &b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001009
Damien Miller901119b2002-10-04 11:10:04 +10001010 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001011 error("ssh_keysign: no reply");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001012 buffer_clear(&b);
1013 return -1;
1014 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001015 close(from[0]);
1016 close(to[1]);
1017
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001018 while (waitpid(pid, &status, 0) < 0)
1019 if (errno != EINTR)
1020 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001021
Ben Lindstrom5206b952002-06-06 19:59:29 +00001022 if (buffer_get_char(&b) != version) {
1023 error("ssh_keysign: bad version");
1024 buffer_clear(&b);
1025 return -1;
1026 }
1027 *sigp = buffer_get_string(&b, lenp);
1028 buffer_clear(&b);
1029
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001030 return 0;
1031}
1032
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001033int
1034userauth_hostbased(Authctxt *authctxt)
1035{
1036 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001037 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001038 Buffer b;
1039 u_char *signature, *blob;
1040 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001041 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001042 u_int blen, slen;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +00001043 int ok, i, len, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001044
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001045 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001046 for (i = 0; i < sensitive->nkeys; i++) {
1047 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001048 if (private && private->type != KEY_RSA1) {
1049 found = 1;
1050 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001051 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001052 break;
1053 }
1054 }
1055 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001056 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001057 return 0;
1058 }
1059 if (key_to_blob(private, &blob, &blen) == 0) {
1060 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001061 return 0;
1062 }
Damien Miller91c18472001-11-12 11:02:03 +11001063 /* figure out a name for the client host */
1064 p = get_local_name(packet_get_connection_in());
1065 if (p == NULL) {
1066 error("userauth_hostbased: cannot get local ipaddr/name");
1067 key_free(private);
1068 return 0;
1069 }
1070 len = strlen(p) + 2;
1071 chost = xmalloc(len);
1072 strlcpy(chost, p, len);
1073 strlcat(chost, ".", len);
1074 debug2("userauth_hostbased: chost %s", chost);
Damien Miller00111382003-03-10 11:21:17 +11001075 xfree(p);
Damien Miller91c18472001-11-12 11:02:03 +11001076
Ben Lindstrom671388f2001-04-19 20:40:45 +00001077 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1078 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001079 pkalg = xstrdup(key_ssh_name(private));
1080 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001081 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001082 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001083 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1084 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001085 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001086 buffer_put_cstring(&b, authctxt->method->name);
1087 buffer_put_cstring(&b, pkalg);
1088 buffer_put_string(&b, blob, blen);
1089 buffer_put_cstring(&b, chost);
1090 buffer_put_cstring(&b, authctxt->local_user);
1091#ifdef DEBUG_PK
1092 buffer_dump(&b);
1093#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001094 if (sensitive->external_keysign)
1095 ok = ssh_keysign(private, &signature, &slen,
1096 buffer_ptr(&b), buffer_len(&b));
1097 else
1098 ok = key_sign(private, &signature, &slen,
1099 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001100 key_free(private);
1101 buffer_free(&b);
1102 if (ok != 0) {
1103 error("key_sign failed");
1104 xfree(chost);
1105 xfree(pkalg);
1106 return 0;
1107 }
1108 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1109 packet_put_cstring(authctxt->server_user);
1110 packet_put_cstring(authctxt->service);
1111 packet_put_cstring(authctxt->method->name);
1112 packet_put_cstring(pkalg);
1113 packet_put_string(blob, blen);
1114 packet_put_cstring(chost);
1115 packet_put_cstring(authctxt->local_user);
1116 packet_put_string(signature, slen);
1117 memset(signature, 's', slen);
1118 xfree(signature);
1119 xfree(chost);
1120 xfree(pkalg);
1121
1122 packet_send();
1123 return 1;
1124}
1125
Damien Miller3ab496b2003-05-14 13:47:37 +10001126#if KRB5
1127static int
Damien Miller4d995192003-05-14 19:23:56 +10001128ssh_krb5_helper(krb5_data *ap, krb5_context *context)
Damien Miller3ab496b2003-05-14 13:47:37 +10001129{
1130 krb5_context xcontext = NULL; /* XXX share with ssh1 */
1131 krb5_auth_context xauth_context = NULL;
Damien Miller3ab496b2003-05-14 13:47:37 +10001132 krb5_auth_context *auth_context;
1133 krb5_error_code problem;
1134 const char *tkfile;
1135 struct stat buf;
1136 krb5_ccache ccache = NULL;
1137 const char *remotehost;
1138 int ret;
1139
1140 memset(ap, 0, sizeof(*ap));
1141
1142 context = &xcontext;
1143 auth_context = &xauth_context;
1144
1145 problem = krb5_init_context(context);
1146 if (problem) {
1147 debug("Kerberos v5: krb5_init_context failed");
1148 ret = 0;
1149 goto out;
1150 }
1151
1152 tkfile = krb5_cc_default_name(*context);
1153 if (strncmp(tkfile, "FILE:", 5) == 0)
1154 tkfile += 5;
1155
1156 if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) {
1157 debug("Kerberos v5: could not get default ccache (permission denied).");
1158 ret = 0;
1159 goto out;
1160 }
1161
1162 problem = krb5_cc_default(*context, &ccache);
1163 if (problem) {
1164 debug("Kerberos v5: krb5_cc_default failed: %s",
1165 krb5_get_err_text(*context, problem));
1166 ret = 0;
1167 goto out;
1168 }
1169
1170 remotehost = get_canonical_hostname(1);
1171
1172 problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED,
1173 "host", remotehost, NULL, ccache, ap);
1174 if (problem) {
1175 debug("Kerberos v5: krb5_mk_req failed: %s",
1176 krb5_get_err_text(*context, problem));
1177 ret = 0;
1178 goto out;
1179 }
1180 ret = 1;
1181
1182 out:
1183 if (ccache != NULL)
1184 krb5_cc_close(*context, ccache);
1185 if (*auth_context)
1186 krb5_auth_con_free(*context, *auth_context);
Damien Miller3ab496b2003-05-14 13:47:37 +10001187 return (ret);
1188}
1189
1190int
1191userauth_kerberos(Authctxt *authctxt)
1192{
1193 krb5_data ap;
Damien Miller4d995192003-05-14 19:23:56 +10001194 krb5_context *context;
1195 int ret = 0;
Damien Miller3ab496b2003-05-14 13:47:37 +10001196
Damien Miller4d995192003-05-14 19:23:56 +10001197 if (ssh_krb5_helper(&ap, context) == 0)
1198 goto out;
Damien Miller3ab496b2003-05-14 13:47:37 +10001199
1200 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1201 packet_put_cstring(authctxt->server_user);
1202 packet_put_cstring(authctxt->service);
1203 packet_put_cstring(authctxt->method->name);
1204 packet_put_string(ap.data, ap.length);
1205 packet_send();
1206
Damien Miller9c617692003-05-14 14:31:11 +10001207#ifdef HEIMDAL
Damien Miller3ab496b2003-05-14 13:47:37 +10001208 krb5_data_free(&ap);
Damien Miller9c617692003-05-14 14:31:11 +10001209#else
Damien Miller4d995192003-05-14 19:23:56 +10001210 krb5_free_data_contents(*context, &ap);
Damien Miller9c617692003-05-14 14:31:11 +10001211#endif
Damien Miller4d995192003-05-14 19:23:56 +10001212 ret = 1;
Damien Miller9c617692003-05-14 14:31:11 +10001213
Damien Miller4d995192003-05-14 19:23:56 +10001214out:
1215 if (*context)
1216 krb5_free_context(*context);
1217 return ret;
Damien Miller3ab496b2003-05-14 13:47:37 +10001218}
1219#endif
1220
Damien Miller62cee002000-09-23 17:15:56 +11001221/* find auth method */
1222
Damien Miller62cee002000-09-23 17:15:56 +11001223/*
1224 * given auth method name, if configurable options permit this method fill
1225 * in auth_ident field and return true, otherwise return false.
1226 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001227static int
Damien Miller62cee002000-09-23 17:15:56 +11001228authmethod_is_enabled(Authmethod *method)
1229{
1230 if (method == NULL)
1231 return 0;
1232 /* return false if options indicate this method is disabled */
1233 if (method->enabled == NULL || *method->enabled == 0)
1234 return 0;
1235 /* return false if batch mode is enabled but method needs interactive mode */
1236 if (method->batch_flag != NULL && *method->batch_flag != 0)
1237 return 0;
1238 return 1;
1239}
1240
Ben Lindstrombba81212001-06-25 05:01:22 +00001241static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001242authmethod_lookup(const char *name)
1243{
1244 Authmethod *method = NULL;
1245 if (name != NULL)
1246 for (method = authmethods; method->name != NULL; method++)
1247 if (strcmp(name, method->name) == 0)
1248 return method;
1249 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1250 return NULL;
1251}
1252
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001253/* XXX internal state */
1254static Authmethod *current = NULL;
1255static char *supported = NULL;
1256static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001257
Damien Miller62cee002000-09-23 17:15:56 +11001258/*
1259 * Given the authentication method list sent by the server, return the
1260 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001261 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001262 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001263static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001264authmethod_get(char *authlist)
1265{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001266 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001267 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001268
Damien Miller62cee002000-09-23 17:15:56 +11001269 /* Use a suitable default if we're passed a nil list. */
1270 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001271 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001272
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001273 if (supported == NULL || strcmp(authlist, supported) != 0) {
1274 debug3("start over, passed a different list %s", authlist);
1275 if (supported != NULL)
1276 xfree(supported);
1277 supported = xstrdup(authlist);
1278 preferred = options.preferred_authentications;
1279 debug3("preferred %s", preferred);
1280 current = NULL;
1281 } else if (current != NULL && authmethod_is_enabled(current))
1282 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001283
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001284 for (;;) {
1285 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001286 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001287 current = NULL;
1288 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001289 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001290 preferred += next;
1291 debug3("authmethod_lookup %s", name);
1292 debug3("remaining preferred: %s", preferred);
1293 if ((current = authmethod_lookup(name)) != NULL &&
1294 authmethod_is_enabled(current)) {
1295 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001296 debug("Next authentication method: %s", name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001297 return current;
1298 }
Damien Millereba71ba2000-04-29 23:57:08 +10001299 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001300}
Damien Miller62cee002000-09-23 17:15:56 +11001301
Ben Lindstrom79073822001-07-04 03:42:30 +00001302static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001303authmethods_get(void)
1304{
1305 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001306 Buffer b;
1307 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001308
Damien Miller0e3b8722002-01-22 23:26:38 +11001309 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001310 for (method = authmethods; method->name != NULL; method++) {
1311 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001312 if (buffer_len(&b) > 0)
1313 buffer_append(&b, ",", 1);
1314 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001315 }
Damien Miller62cee002000-09-23 17:15:56 +11001316 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001317 buffer_append(&b, "\0", 1);
1318 list = xstrdup(buffer_ptr(&b));
1319 buffer_free(&b);
1320 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001321}