blob: 0605e4e5f6a19df308bbe374e62d403d7eafb397 [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"
Damien Miller3ab496b2003-05-14 13:47:37 +100026RCSID("$OpenBSD: sshconnect2.c,v 1.118 2003/05/14 02:15:47 markus Exp $");
27
28#ifdef KRB5
29#include <krb5.h>
30#endif
Damien Millereba71ba2000-04-29 23:57:08 +100031
32#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000033#include "ssh2.h"
Damien Millereba71ba2000-04-29 23:57:08 +100034#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100035#include "buffer.h"
36#include "packet.h"
Damien Millereba71ba2000-04-29 23:57:08 +100037#include "compat.h"
Damien Millereba71ba2000-04-29 23:57:08 +100038#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "cipher.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#include "kex.h"
41#include "myproposal.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042#include "sshconnect.h"
43#include "authfile.h"
Ben Lindstromdf221392001-03-29 00:36:16 +000044#include "dh.h"
Damien Millerad833b32000-08-23 10:46:23 +100045#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include "log.h"
47#include "readconf.h"
48#include "readpass.h"
Ben Lindstromb9be60a2001-03-11 01:49:19 +000049#include "match.h"
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000050#include "dispatch.h"
Ben Lindstrom5eabda32001-04-12 23:34:34 +000051#include "canohost.h"
Ben Lindstrom1bad2562002-06-06 19:57:33 +000052#include "msg.h"
53#include "pathnames.h"
Damien Miller874d77b2000-10-14 16:23:11 +110054
Damien Millereba71ba2000-04-29 23:57:08 +100055/* import */
56extern char *client_version_string;
57extern char *server_version_string;
58extern Options options;
59
60/*
61 * SSH2 key exchange
62 */
63
Ben Lindstrom46c16222000-12-22 01:43:59 +000064u_char *session_id2 = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +100065int session_id2_len = 0;
66
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000067char *xxx_host;
68struct sockaddr *xxx_hostaddr;
69
Ben Lindstromf28f6342001-04-04 02:03:04 +000070Kex *xxx_kex = NULL;
71
Ben Lindstrombba81212001-06-25 05:01:22 +000072static int
Ben Lindstromd6481ea2001-06-25 04:37:41 +000073verify_host_key_callback(Key *hostkey)
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000074{
Ben Lindstromd6481ea2001-06-25 04:37:41 +000075 if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
Damien Miller59d9fb92001-10-10 15:03:11 +100076 fatal("Host key verification failed.");
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000077 return 0;
78}
79
Damien Millereba71ba2000-04-29 23:57:08 +100080void
Damien Miller874d77b2000-10-14 16:23:11 +110081ssh_kex2(char *host, struct sockaddr *hostaddr)
82{
Damien Miller874d77b2000-10-14 16:23:11 +110083 Kex *kex;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +000084
85 xxx_host = host;
86 xxx_hostaddr = hostaddr;
Damien Miller874d77b2000-10-14 16:23:11 +110087
Damien Millere39cacc2000-11-29 12:18:44 +110088 if (options.ciphers == (char *)-1) {
Damien Miller996acd22003-04-09 20:59:48 +100089 logit("No valid ciphers for protocol version 2 given, using defaults.");
Damien Millere39cacc2000-11-29 12:18:44 +110090 options.ciphers = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +110091 }
92 if (options.ciphers != NULL) {
93 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
94 myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
95 }
Damien Millera0ff4662001-03-30 10:49:35 +100096 myproposal[PROPOSAL_ENC_ALGS_CTOS] =
97 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
98 myproposal[PROPOSAL_ENC_ALGS_STOC] =
99 compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
Damien Miller874d77b2000-10-14 16:23:11 +1100100 if (options.compression) {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000101 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Ben Lindstrom343010a2002-07-04 00:16:25 +0000102 myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
Damien Miller874d77b2000-10-14 16:23:11 +1100103 } else {
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000104 myproposal[PROPOSAL_COMP_ALGS_CTOS] =
Ben Lindstrom343010a2002-07-04 00:16:25 +0000105 myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
Damien Miller874d77b2000-10-14 16:23:11 +1100106 }
Ben Lindstrom06b33aa2001-02-15 03:01:59 +0000107 if (options.macs != NULL) {
108 myproposal[PROPOSAL_MAC_ALGS_CTOS] =
109 myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
110 }
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000111 if (options.hostkeyalgorithms != NULL)
Damien Miller9f0f5c62001-12-21 14:45:46 +1100112 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000113 options.hostkeyalgorithms;
Damien Miller874d77b2000-10-14 16:23:11 +1100114
Damien Millera5539d22003-04-09 20:50:06 +1000115 if (options.rekey_limit)
116 packet_set_rekey_limit(options.rekey_limit);
117
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000118 /* start key exchange */
Ben Lindstrom238abf62001-04-04 17:52:53 +0000119 kex = kex_setup(myproposal);
Damien Miller8e7fb332003-02-24 12:03:03 +1100120 kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
121 kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
Ben Lindstrom20d7c7b2001-04-04 01:56:17 +0000122 kex->client_version_string=client_version_string;
123 kex->server_version_string=server_version_string;
Ben Lindstromd6481ea2001-06-25 04:37:41 +0000124 kex->verify_host_key=&verify_host_key_callback;
Damien Miller874d77b2000-10-14 16:23:11 +1100125
Ben Lindstromf28f6342001-04-04 02:03:04 +0000126 xxx_kex = kex;
127
Ben Lindstrombe2cc432001-04-04 23:46:07 +0000128 dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
Damien Miller874d77b2000-10-14 16:23:11 +1100129
Ben Lindstrom2d90e002001-04-04 02:00:54 +0000130 session_id2 = kex->session_id;
131 session_id2_len = kex->session_id_len;
132
Damien Miller874d77b2000-10-14 16:23:11 +1100133#ifdef DEBUG_KEXDH
134 /* send 1st encrypted/maced/compressed message */
135 packet_start(SSH2_MSG_IGNORE);
136 packet_put_cstring("markus");
137 packet_send();
138 packet_write_wait();
139#endif
Damien Millereba71ba2000-04-29 23:57:08 +1000140}
Damien Millerb1715dc2000-05-30 13:44:51 +1000141
Damien Millereba71ba2000-04-29 23:57:08 +1000142/*
143 * Authenticate user
144 */
Damien Miller62cee002000-09-23 17:15:56 +1100145
146typedef struct Authctxt Authctxt;
147typedef struct Authmethod Authmethod;
Damien Miller280ecfb2003-05-14 13:46:00 +1000148typedef struct identity Identity;
149typedef struct idlist Idlist;
Damien Miller62cee002000-09-23 17:15:56 +1100150
Damien Miller280ecfb2003-05-14 13:46:00 +1000151struct identity {
152 TAILQ_ENTRY(identity) next;
153 AuthenticationConnection *ac; /* set if agent supports key */
154 Key *key; /* public/private key */
155 char *filename; /* comment for agent-only keys */
156 int tried;
157 int isprivate; /* key points to the private key */
158};
159TAILQ_HEAD(idlist, identity);
Damien Miller62cee002000-09-23 17:15:56 +1100160
161struct Authctxt {
162 const char *server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000163 const char *local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100164 const char *host;
165 const char *service;
Damien Miller62cee002000-09-23 17:15:56 +1100166 Authmethod *method;
Damien Miller874d77b2000-10-14 16:23:11 +1100167 int success;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000168 char *authlist;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000169 /* pubkey */
Damien Miller280ecfb2003-05-14 13:46:00 +1000170 Idlist keys;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000171 AuthenticationConnection *agent;
172 /* hostbased */
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000173 Sensitive *sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000174 /* kbd-interactive */
175 int info_req_seen;
Damien Miller62cee002000-09-23 17:15:56 +1100176};
177struct Authmethod {
178 char *name; /* string to compare against server's list */
179 int (*userauth)(Authctxt *authctxt);
180 int *enabled; /* flag in option struct that enables method */
181 int *batch_flag; /* flag in option struct that disables method */
182};
183
Damien Miller630d6f42002-01-22 23:17:30 +1100184void input_userauth_success(int, u_int32_t, void *);
185void input_userauth_failure(int, u_int32_t, void *);
186void input_userauth_banner(int, u_int32_t, void *);
187void input_userauth_error(int, u_int32_t, void *);
188void input_userauth_info_req(int, u_int32_t, void *);
189void input_userauth_pk_ok(int, u_int32_t, void *);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000190void input_userauth_passwd_changereq(int, u_int32_t, void *);
Damien Miller874d77b2000-10-14 16:23:11 +1100191
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000192int userauth_none(Authctxt *);
193int userauth_pubkey(Authctxt *);
194int userauth_passwd(Authctxt *);
195int userauth_kbdint(Authctxt *);
196int userauth_hostbased(Authctxt *);
Damien Miller3ab496b2003-05-14 13:47:37 +1000197int userauth_kerberos(Authctxt *);
Damien Miller62cee002000-09-23 17:15:56 +1100198
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000199void userauth(Authctxt *, char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000200
Damien Miller280ecfb2003-05-14 13:46:00 +1000201static int sign_and_send_pubkey(Authctxt *, Identity *);
Ben Lindstrombba81212001-06-25 05:01:22 +0000202static void clear_auth_state(Authctxt *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000203static void pubkey_prepare(Authctxt *);
204static void pubkey_cleanup(Authctxt *);
205static Key *load_identity_file(char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000206
Ben Lindstrombba81212001-06-25 05:01:22 +0000207static Authmethod *authmethod_get(char *authlist);
208static Authmethod *authmethod_lookup(const char *name);
209static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100210
211Authmethod authmethods[] = {
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000212 {"hostbased",
213 userauth_hostbased,
214 &options.hostbased_authentication,
215 NULL},
Damien Miller3ab496b2003-05-14 13:47:37 +1000216#if KRB5
217 {"kerberos-2@ssh.com",
218 userauth_kerberos,
219 &options.kerberos_authentication,
220 NULL},
221#endif
Ben Lindstrom45350e82001-08-06 20:57:11 +0000222 {"publickey",
223 userauth_pubkey,
224 &options.pubkey_authentication,
225 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100226 {"keyboard-interactive",
227 userauth_kbdint,
228 &options.kbd_interactive_authentication,
229 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000230 {"password",
231 userauth_passwd,
232 &options.password_authentication,
233 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100234 {"none",
235 userauth_none,
236 NULL,
237 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100238 {NULL, NULL, NULL, NULL}
239};
240
241void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000242ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000243 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100244{
245 Authctxt authctxt;
246 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100247
Ben Lindstrom551ea372001-06-05 18:56:16 +0000248 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000249 options.kbd_interactive_authentication = 1;
250
Damien Miller62cee002000-09-23 17:15:56 +1100251 packet_start(SSH2_MSG_SERVICE_REQUEST);
252 packet_put_cstring("ssh-userauth");
253 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000254 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100255 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100256 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000257 if (type != SSH2_MSG_SERVICE_ACCEPT)
258 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100259 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100260 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000261 debug2("service_accept: %s", reply);
Damien Miller62cee002000-09-23 17:15:56 +1100262 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100263 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000264 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100265 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100266 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000267 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100268
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000269 if (options.preferred_authentications == NULL)
270 options.preferred_authentications = authmethods_get();
271
Damien Miller62cee002000-09-23 17:15:56 +1100272 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000273 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000274 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100275 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000276 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100277 authctxt.host = host;
278 authctxt.service = "ssh-connection"; /* service name */
279 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100280 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000281 authctxt.authlist = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000282 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000283 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100284 if (authctxt.method == NULL)
285 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100286
287 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100288 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100289
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000290 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100291 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
292 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000293 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100294 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
295
Damien Miller280ecfb2003-05-14 13:46:00 +1000296 pubkey_cleanup(&authctxt);
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000297 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100298}
299void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000300userauth(Authctxt *authctxt, char *authlist)
301{
302 if (authlist == NULL) {
303 authlist = authctxt->authlist;
304 } else {
305 if (authctxt->authlist)
306 xfree(authctxt->authlist);
307 authctxt->authlist = authlist;
308 }
309 for (;;) {
310 Authmethod *method = authmethod_get(authlist);
311 if (method == NULL)
312 fatal("Permission denied (%s).", authlist);
313 authctxt->method = method;
314 if (method->userauth(authctxt) != 0) {
315 debug2("we sent a %s packet, wait for reply", method->name);
316 break;
317 } else {
318 debug2("we did not send a packet, disable method");
319 method->enabled = NULL;
320 }
321 }
322}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000323
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000324void
Damien Miller630d6f42002-01-22 23:17:30 +1100325input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100326{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000327 fatal("input_userauth_error: bad message during authentication: "
328 "type %d", type);
329}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000330
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000331void
Damien Miller630d6f42002-01-22 23:17:30 +1100332input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000333{
334 char *msg, *lang;
335 debug3("input_userauth_banner");
336 msg = packet_get_string(NULL);
337 lang = packet_get_string(NULL);
338 fprintf(stderr, "%s", msg);
339 xfree(msg);
340 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100341}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000342
Damien Miller62cee002000-09-23 17:15:56 +1100343void
Damien Miller630d6f42002-01-22 23:17:30 +1100344input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100345{
346 Authctxt *authctxt = ctxt;
347 if (authctxt == NULL)
348 fatal("input_userauth_success: no authentication context");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000349 if (authctxt->authlist)
350 xfree(authctxt->authlist);
351 clear_auth_state(authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100352 authctxt->success = 1; /* break out */
353}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000354
Damien Miller62cee002000-09-23 17:15:56 +1100355void
Damien Miller630d6f42002-01-22 23:17:30 +1100356input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100357{
Damien Miller62cee002000-09-23 17:15:56 +1100358 Authctxt *authctxt = ctxt;
359 char *authlist = NULL;
360 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100361
362 if (authctxt == NULL)
363 fatal("input_userauth_failure: no authentication context");
364
Damien Miller874d77b2000-10-14 16:23:11 +1100365 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100366 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100367 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100368
369 if (partial != 0)
Damien Miller996acd22003-04-09 20:59:48 +1000370 logit("Authenticated with partial success.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000371 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100372
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000373 clear_auth_state(authctxt);
374 userauth(authctxt, authlist);
375}
376void
Damien Miller630d6f42002-01-22 23:17:30 +1100377input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000378{
379 Authctxt *authctxt = ctxt;
380 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000381 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000382 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000383 int pktype, sent = 0;
384 u_int alen, blen;
385 char *pkalg, *fp;
386 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000387
388 if (authctxt == NULL)
389 fatal("input_userauth_pk_ok: no authentication context");
390 if (datafellows & SSH_BUG_PKOK) {
391 /* this is similar to SSH_BUG_PKAUTH */
392 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
393 pkblob = packet_get_string(&blen);
394 buffer_init(&b);
395 buffer_append(&b, pkblob, blen);
396 pkalg = buffer_get_string(&b, &alen);
397 buffer_free(&b);
398 } else {
399 pkalg = packet_get_string(&alen);
400 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000401 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100402 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000403
Damien Miller280ecfb2003-05-14 13:46:00 +1000404 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000405
Damien Miller280ecfb2003-05-14 13:46:00 +1000406 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
407 debug("unknown pkalg %s", pkalg);
408 goto done;
409 }
410 if ((key = key_from_blob(pkblob, blen)) == NULL) {
411 debug("no key from blob. pkalg %s", pkalg);
412 goto done;
413 }
414 if (key->type != pktype) {
415 error("input_userauth_pk_ok: type mismatch "
416 "for decoded key (received %d, expected %d)",
417 key->type, pktype);
418 goto done;
419 }
420 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
421 debug2("input_userauth_pk_ok: fp %s", fp);
422 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000423
Damien Miller280ecfb2003-05-14 13:46:00 +1000424 TAILQ_FOREACH(id, &authctxt->keys, next) {
425 if (key_equal(key, id->key)) {
426 sent = sign_and_send_pubkey(authctxt, id);
427 break;
428 }
429 }
430done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000431 if (key != NULL)
432 key_free(key);
433 xfree(pkalg);
434 xfree(pkblob);
435
436 /* unregister */
437 clear_auth_state(authctxt);
438 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
439
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000440 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000441 if (sent == 0)
442 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100443}
444
Damien Millereba71ba2000-04-29 23:57:08 +1000445int
Damien Miller874d77b2000-10-14 16:23:11 +1100446userauth_none(Authctxt *authctxt)
447{
448 /* initial userauth request */
449 packet_start(SSH2_MSG_USERAUTH_REQUEST);
450 packet_put_cstring(authctxt->server_user);
451 packet_put_cstring(authctxt->service);
452 packet_put_cstring(authctxt->method->name);
453 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100454 return 1;
455}
456
457int
Damien Miller62cee002000-09-23 17:15:56 +1100458userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000459{
Damien Millere247cc42000-05-07 12:03:14 +1000460 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000461 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000462 char *password;
463
Damien Millerd3a18572000-06-07 19:55:44 +1000464 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000465 return 0;
466
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000467 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000468 error("Permission denied, please try again.");
469
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000470 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100471 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000472 password = read_passphrase(prompt, 0);
473 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100474 packet_put_cstring(authctxt->server_user);
475 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100476 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000477 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000478 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000479 memset(password, 0, strlen(password));
480 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100481 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000482 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000483
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000484 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000485 &input_userauth_passwd_changereq);
486
Damien Millereba71ba2000-04-29 23:57:08 +1000487 return 1;
488}
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000489/*
490 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
491 */
492void
Tim Ricec8549622002-03-31 12:49:38 -0800493input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000494{
495 Authctxt *authctxt = ctxt;
496 char *info, *lang, *password = NULL, *retype = NULL;
497 char prompt[150];
498
499 debug2("input_userauth_passwd_changereq");
500
501 if (authctxt == NULL)
502 fatal("input_userauth_passwd_changereq: "
503 "no authentication context");
504
505 info = packet_get_string(NULL);
506 lang = packet_get_string(NULL);
507 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000508 logit("%s", info);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000509 xfree(info);
510 xfree(lang);
511 packet_start(SSH2_MSG_USERAUTH_REQUEST);
512 packet_put_cstring(authctxt->server_user);
513 packet_put_cstring(authctxt->service);
514 packet_put_cstring(authctxt->method->name);
515 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000516 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000517 "Enter %.30s@%.128s's old password: ",
518 authctxt->server_user, authctxt->host);
519 password = read_passphrase(prompt, 0);
520 packet_put_cstring(password);
521 memset(password, 0, strlen(password));
522 xfree(password);
523 password = NULL;
524 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000525 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000526 "Enter %.30s@%.128s's new password: ",
527 authctxt->server_user, authctxt->host);
528 password = read_passphrase(prompt, RP_ALLOW_EOF);
529 if (password == NULL) {
530 /* bail out */
531 return;
532 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000533 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000534 "Retype %.30s@%.128s's new password: ",
535 authctxt->server_user, authctxt->host);
536 retype = read_passphrase(prompt, 0);
537 if (strcmp(password, retype) != 0) {
538 memset(password, 0, strlen(password));
539 xfree(password);
Damien Miller996acd22003-04-09 20:59:48 +1000540 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000541 password = NULL;
542 }
543 memset(retype, 0, strlen(retype));
544 xfree(retype);
545 }
546 packet_put_cstring(password);
547 memset(password, 0, strlen(password));
548 xfree(password);
549 packet_add_padding(64);
550 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000551
552 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000553 &input_userauth_passwd_changereq);
554}
Damien Millereba71ba2000-04-29 23:57:08 +1000555
Ben Lindstrom79073822001-07-04 03:42:30 +0000556static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000557clear_auth_state(Authctxt *authctxt)
558{
559 /* XXX clear authentication state */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000560 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000561}
562
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;
Damien Miller6536c7d2000-06-22 21:32:31 +1000594 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
1128ssh_krb5_helper(krb5_data *ap)
1129{
1130 krb5_context xcontext = NULL; /* XXX share with ssh1 */
1131 krb5_auth_context xauth_context = NULL;
1132
1133 krb5_context *context;
1134 krb5_auth_context *auth_context;
1135 krb5_error_code problem;
1136 const char *tkfile;
1137 struct stat buf;
1138 krb5_ccache ccache = NULL;
1139 const char *remotehost;
1140 int ret;
1141
1142 memset(ap, 0, sizeof(*ap));
1143
1144 context = &xcontext;
1145 auth_context = &xauth_context;
1146
1147 problem = krb5_init_context(context);
1148 if (problem) {
1149 debug("Kerberos v5: krb5_init_context failed");
1150 ret = 0;
1151 goto out;
1152 }
1153
1154 tkfile = krb5_cc_default_name(*context);
1155 if (strncmp(tkfile, "FILE:", 5) == 0)
1156 tkfile += 5;
1157
1158 if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) {
1159 debug("Kerberos v5: could not get default ccache (permission denied).");
1160 ret = 0;
1161 goto out;
1162 }
1163
1164 problem = krb5_cc_default(*context, &ccache);
1165 if (problem) {
1166 debug("Kerberos v5: krb5_cc_default failed: %s",
1167 krb5_get_err_text(*context, problem));
1168 ret = 0;
1169 goto out;
1170 }
1171
1172 remotehost = get_canonical_hostname(1);
1173
1174 problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED,
1175 "host", remotehost, NULL, ccache, ap);
1176 if (problem) {
1177 debug("Kerberos v5: krb5_mk_req failed: %s",
1178 krb5_get_err_text(*context, problem));
1179 ret = 0;
1180 goto out;
1181 }
1182 ret = 1;
1183
1184 out:
1185 if (ccache != NULL)
1186 krb5_cc_close(*context, ccache);
1187 if (*auth_context)
1188 krb5_auth_con_free(*context, *auth_context);
1189 if (*context)
1190 krb5_free_context(*context);
1191 return (ret);
1192}
1193
1194int
1195userauth_kerberos(Authctxt *authctxt)
1196{
1197 krb5_data ap;
1198
1199 if (ssh_krb5_helper(&ap) == 0)
1200 return (0);
1201
1202 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1203 packet_put_cstring(authctxt->server_user);
1204 packet_put_cstring(authctxt->service);
1205 packet_put_cstring(authctxt->method->name);
1206 packet_put_string(ap.data, ap.length);
1207 packet_send();
1208
1209 krb5_data_free(&ap);
1210 return (1);
1211}
1212#endif
1213
Damien Miller62cee002000-09-23 17:15:56 +11001214/* find auth method */
1215
Damien Miller62cee002000-09-23 17:15:56 +11001216/*
1217 * given auth method name, if configurable options permit this method fill
1218 * in auth_ident field and return true, otherwise return false.
1219 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001220static int
Damien Miller62cee002000-09-23 17:15:56 +11001221authmethod_is_enabled(Authmethod *method)
1222{
1223 if (method == NULL)
1224 return 0;
1225 /* return false if options indicate this method is disabled */
1226 if (method->enabled == NULL || *method->enabled == 0)
1227 return 0;
1228 /* return false if batch mode is enabled but method needs interactive mode */
1229 if (method->batch_flag != NULL && *method->batch_flag != 0)
1230 return 0;
1231 return 1;
1232}
1233
Ben Lindstrombba81212001-06-25 05:01:22 +00001234static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001235authmethod_lookup(const char *name)
1236{
1237 Authmethod *method = NULL;
1238 if (name != NULL)
1239 for (method = authmethods; method->name != NULL; method++)
1240 if (strcmp(name, method->name) == 0)
1241 return method;
1242 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1243 return NULL;
1244}
1245
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001246/* XXX internal state */
1247static Authmethod *current = NULL;
1248static char *supported = NULL;
1249static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001250
Damien Miller62cee002000-09-23 17:15:56 +11001251/*
1252 * Given the authentication method list sent by the server, return the
1253 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001254 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001255 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001256static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001257authmethod_get(char *authlist)
1258{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001259 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001260 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001261
Damien Miller62cee002000-09-23 17:15:56 +11001262 /* Use a suitable default if we're passed a nil list. */
1263 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001264 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001265
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001266 if (supported == NULL || strcmp(authlist, supported) != 0) {
1267 debug3("start over, passed a different list %s", authlist);
1268 if (supported != NULL)
1269 xfree(supported);
1270 supported = xstrdup(authlist);
1271 preferred = options.preferred_authentications;
1272 debug3("preferred %s", preferred);
1273 current = NULL;
1274 } else if (current != NULL && authmethod_is_enabled(current))
1275 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001276
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001277 for (;;) {
1278 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001279 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001280 current = NULL;
1281 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001282 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001283 preferred += next;
1284 debug3("authmethod_lookup %s", name);
1285 debug3("remaining preferred: %s", preferred);
1286 if ((current = authmethod_lookup(name)) != NULL &&
1287 authmethod_is_enabled(current)) {
1288 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001289 debug("Next authentication method: %s", name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001290 return current;
1291 }
Damien Millereba71ba2000-04-29 23:57:08 +10001292 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001293}
Damien Miller62cee002000-09-23 17:15:56 +11001294
Ben Lindstrom79073822001-07-04 03:42:30 +00001295static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001296authmethods_get(void)
1297{
1298 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001299 Buffer b;
1300 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001301
Damien Miller0e3b8722002-01-22 23:26:38 +11001302 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001303 for (method = authmethods; method->name != NULL; method++) {
1304 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001305 if (buffer_len(&b) > 0)
1306 buffer_append(&b, ",", 1);
1307 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001308 }
Damien Miller62cee002000-09-23 17:15:56 +11001309 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001310 buffer_append(&b, "\0", 1);
1311 list = xstrdup(buffer_ptr(&b));
1312 buffer_free(&b);
1313 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001314}