blob: 36d592b4291919ccb0cf9ac2d171e73c880654fc [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>
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;
Damien Millereba71ba2000-04-29 23:57:08 +100070int session_id2_len = 0;
71
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 *);
Ben Lindstrombba81212001-06-25 05:01:22 +0000207static void clear_auth_state(Authctxt *);
Damien Miller280ecfb2003-05-14 13:46:00 +1000208static void pubkey_prepare(Authctxt *);
209static void pubkey_cleanup(Authctxt *);
210static Key *load_identity_file(char *);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000211
Ben Lindstrombba81212001-06-25 05:01:22 +0000212static Authmethod *authmethod_get(char *authlist);
213static Authmethod *authmethod_lookup(const char *name);
214static char *authmethods_get(void);
Damien Miller62cee002000-09-23 17:15:56 +1100215
216Authmethod authmethods[] = {
Ben Lindstrom1bfe2912001-06-05 19:37:25 +0000217 {"hostbased",
218 userauth_hostbased,
219 &options.hostbased_authentication,
220 NULL},
Damien Miller3ab496b2003-05-14 13:47:37 +1000221#if KRB5
222 {"kerberos-2@ssh.com",
223 userauth_kerberos,
224 &options.kerberos_authentication,
225 NULL},
226#endif
Ben Lindstrom45350e82001-08-06 20:57:11 +0000227 {"publickey",
228 userauth_pubkey,
229 &options.pubkey_authentication,
230 NULL},
Damien Miller874d77b2000-10-14 16:23:11 +1100231 {"keyboard-interactive",
232 userauth_kbdint,
233 &options.kbd_interactive_authentication,
234 &options.batch_mode},
Ben Lindstrom45350e82001-08-06 20:57:11 +0000235 {"password",
236 userauth_passwd,
237 &options.password_authentication,
238 &options.batch_mode},
Damien Miller874d77b2000-10-14 16:23:11 +1100239 {"none",
240 userauth_none,
241 NULL,
242 NULL},
Damien Miller62cee002000-09-23 17:15:56 +1100243 {NULL, NULL, NULL, NULL}
244};
245
246void
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000247ssh_userauth2(const char *local_user, const char *server_user, char *host,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000248 Sensitive *sensitive)
Damien Miller62cee002000-09-23 17:15:56 +1100249{
250 Authctxt authctxt;
251 int type;
Damien Miller62cee002000-09-23 17:15:56 +1100252
Ben Lindstrom551ea372001-06-05 18:56:16 +0000253 if (options.challenge_response_authentication)
Ben Lindstrom95fb2dd2001-01-23 03:12:10 +0000254 options.kbd_interactive_authentication = 1;
255
Damien Miller62cee002000-09-23 17:15:56 +1100256 packet_start(SSH2_MSG_SERVICE_REQUEST);
257 packet_put_cstring("ssh-userauth");
258 packet_send();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000259 debug("SSH2_MSG_SERVICE_REQUEST sent");
Damien Miller62cee002000-09-23 17:15:56 +1100260 packet_write_wait();
Damien Millerdff50992002-01-22 23:16:32 +1100261 type = packet_read();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000262 if (type != SSH2_MSG_SERVICE_ACCEPT)
263 fatal("Server denied authentication request: %d", type);
Damien Miller62cee002000-09-23 17:15:56 +1100264 if (packet_remaining() > 0) {
Damien Millerdff50992002-01-22 23:16:32 +1100265 char *reply = packet_get_string(NULL);
Ben Lindstrom064496f2002-12-23 02:04:22 +0000266 debug2("service_accept: %s", reply);
Damien Miller62cee002000-09-23 17:15:56 +1100267 xfree(reply);
Damien Miller62cee002000-09-23 17:15:56 +1100268 } else {
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000269 debug2("buggy server: service_accept w/o service");
Damien Miller62cee002000-09-23 17:15:56 +1100270 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100271 packet_check_eom();
Ben Lindstrom064496f2002-12-23 02:04:22 +0000272 debug("SSH2_MSG_SERVICE_ACCEPT received");
Damien Miller62cee002000-09-23 17:15:56 +1100273
Ben Lindstromb9be60a2001-03-11 01:49:19 +0000274 if (options.preferred_authentications == NULL)
275 options.preferred_authentications = authmethods_get();
276
Damien Miller62cee002000-09-23 17:15:56 +1100277 /* setup authentication context */
Ben Lindstrom7d199962001-09-12 18:29:00 +0000278 memset(&authctxt, 0, sizeof(authctxt));
Damien Miller280ecfb2003-05-14 13:46:00 +1000279 pubkey_prepare(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100280 authctxt.server_user = server_user;
Ben Lindstrom5eabda32001-04-12 23:34:34 +0000281 authctxt.local_user = local_user;
Damien Miller62cee002000-09-23 17:15:56 +1100282 authctxt.host = host;
283 authctxt.service = "ssh-connection"; /* service name */
284 authctxt.success = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100285 authctxt.method = authmethod_lookup("none");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000286 authctxt.authlist = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000287 authctxt.sensitive = sensitive;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000288 authctxt.info_req_seen = 0;
Damien Miller874d77b2000-10-14 16:23:11 +1100289 if (authctxt.method == NULL)
290 fatal("ssh_userauth2: internal error: cannot send userauth none request");
Damien Miller62cee002000-09-23 17:15:56 +1100291
292 /* initial userauth request */
Damien Miller874d77b2000-10-14 16:23:11 +1100293 userauth_none(&authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100294
Ben Lindstrom8ac91062001-04-04 17:57:54 +0000295 dispatch_init(&input_userauth_error);
Damien Miller62cee002000-09-23 17:15:56 +1100296 dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
297 dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000298 dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
Damien Miller62cee002000-09-23 17:15:56 +1100299 dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
300
Damien Miller280ecfb2003-05-14 13:46:00 +1000301 pubkey_cleanup(&authctxt);
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000302 debug("Authentication succeeded (%s).", authctxt.method->name);
Damien Miller62cee002000-09-23 17:15:56 +1100303}
304void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000305userauth(Authctxt *authctxt, char *authlist)
306{
307 if (authlist == NULL) {
308 authlist = authctxt->authlist;
309 } else {
310 if (authctxt->authlist)
311 xfree(authctxt->authlist);
312 authctxt->authlist = authlist;
313 }
314 for (;;) {
315 Authmethod *method = authmethod_get(authlist);
316 if (method == NULL)
317 fatal("Permission denied (%s).", authlist);
318 authctxt->method = method;
319 if (method->userauth(authctxt) != 0) {
320 debug2("we sent a %s packet, wait for reply", method->name);
321 break;
322 } else {
323 debug2("we did not send a packet, disable method");
324 method->enabled = NULL;
325 }
326 }
327}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000328
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000329void
Damien Miller630d6f42002-01-22 23:17:30 +1100330input_userauth_error(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100331{
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000332 fatal("input_userauth_error: bad message during authentication: "
333 "type %d", type);
334}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000335
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000336void
Damien Miller630d6f42002-01-22 23:17:30 +1100337input_userauth_banner(int type, u_int32_t seq, void *ctxt)
Ben Lindstromd26dcf32001-01-06 15:18:16 +0000338{
339 char *msg, *lang;
340 debug3("input_userauth_banner");
341 msg = packet_get_string(NULL);
342 lang = packet_get_string(NULL);
343 fprintf(stderr, "%s", msg);
344 xfree(msg);
345 xfree(lang);
Damien Miller62cee002000-09-23 17:15:56 +1100346}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000347
Damien Miller62cee002000-09-23 17:15:56 +1100348void
Damien Miller630d6f42002-01-22 23:17:30 +1100349input_userauth_success(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100350{
351 Authctxt *authctxt = ctxt;
352 if (authctxt == NULL)
353 fatal("input_userauth_success: no authentication context");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000354 if (authctxt->authlist)
355 xfree(authctxt->authlist);
356 clear_auth_state(authctxt);
Damien Miller62cee002000-09-23 17:15:56 +1100357 authctxt->success = 1; /* break out */
358}
Ben Lindstrom5c385522002-06-23 21:23:20 +0000359
Damien Miller62cee002000-09-23 17:15:56 +1100360void
Damien Miller630d6f42002-01-22 23:17:30 +1100361input_userauth_failure(int type, u_int32_t seq, void *ctxt)
Damien Miller62cee002000-09-23 17:15:56 +1100362{
Damien Miller62cee002000-09-23 17:15:56 +1100363 Authctxt *authctxt = ctxt;
364 char *authlist = NULL;
365 int partial;
Damien Miller62cee002000-09-23 17:15:56 +1100366
367 if (authctxt == NULL)
368 fatal("input_userauth_failure: no authentication context");
369
Damien Miller874d77b2000-10-14 16:23:11 +1100370 authlist = packet_get_string(NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100371 partial = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100372 packet_check_eom();
Damien Miller62cee002000-09-23 17:15:56 +1100373
374 if (partial != 0)
Damien Miller996acd22003-04-09 20:59:48 +1000375 logit("Authenticated with partial success.");
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000376 debug("Authentications that can continue: %s", authlist);
Damien Miller62cee002000-09-23 17:15:56 +1100377
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000378 clear_auth_state(authctxt);
379 userauth(authctxt, authlist);
380}
381void
Damien Miller630d6f42002-01-22 23:17:30 +1100382input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000383{
384 Authctxt *authctxt = ctxt;
385 Key *key = NULL;
Damien Miller280ecfb2003-05-14 13:46:00 +1000386 Identity *id = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000387 Buffer b;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000388 int pktype, sent = 0;
389 u_int alen, blen;
390 char *pkalg, *fp;
391 u_char *pkblob;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000392
393 if (authctxt == NULL)
394 fatal("input_userauth_pk_ok: no authentication context");
395 if (datafellows & SSH_BUG_PKOK) {
396 /* this is similar to SSH_BUG_PKAUTH */
397 debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
398 pkblob = packet_get_string(&blen);
399 buffer_init(&b);
400 buffer_append(&b, pkblob, blen);
401 pkalg = buffer_get_string(&b, &alen);
402 buffer_free(&b);
403 } else {
404 pkalg = packet_get_string(&alen);
405 pkblob = packet_get_string(&blen);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000406 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100407 packet_check_eom();
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000408
Damien Miller280ecfb2003-05-14 13:46:00 +1000409 debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000410
Damien Miller280ecfb2003-05-14 13:46:00 +1000411 if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
412 debug("unknown pkalg %s", pkalg);
413 goto done;
414 }
415 if ((key = key_from_blob(pkblob, blen)) == NULL) {
416 debug("no key from blob. pkalg %s", pkalg);
417 goto done;
418 }
419 if (key->type != pktype) {
420 error("input_userauth_pk_ok: type mismatch "
421 "for decoded key (received %d, expected %d)",
422 key->type, pktype);
423 goto done;
424 }
425 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
426 debug2("input_userauth_pk_ok: fp %s", fp);
427 xfree(fp);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000428
Damien Miller280ecfb2003-05-14 13:46:00 +1000429 TAILQ_FOREACH(id, &authctxt->keys, next) {
430 if (key_equal(key, id->key)) {
431 sent = sign_and_send_pubkey(authctxt, id);
432 break;
433 }
434 }
435done:
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000436 if (key != NULL)
437 key_free(key);
438 xfree(pkalg);
439 xfree(pkblob);
440
441 /* unregister */
442 clear_auth_state(authctxt);
443 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
444
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000445 /* try another method if we did not send a packet */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000446 if (sent == 0)
447 userauth(authctxt, NULL);
Damien Miller62cee002000-09-23 17:15:56 +1100448}
449
Damien Millereba71ba2000-04-29 23:57:08 +1000450int
Damien Miller874d77b2000-10-14 16:23:11 +1100451userauth_none(Authctxt *authctxt)
452{
453 /* initial userauth request */
454 packet_start(SSH2_MSG_USERAUTH_REQUEST);
455 packet_put_cstring(authctxt->server_user);
456 packet_put_cstring(authctxt->service);
457 packet_put_cstring(authctxt->method->name);
458 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100459 return 1;
460}
461
462int
Damien Miller62cee002000-09-23 17:15:56 +1100463userauth_passwd(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000464{
Damien Millere247cc42000-05-07 12:03:14 +1000465 static int attempt = 0;
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000466 char prompt[150];
Damien Millereba71ba2000-04-29 23:57:08 +1000467 char *password;
468
Damien Millerd3a18572000-06-07 19:55:44 +1000469 if (attempt++ >= options.number_of_password_prompts)
Damien Millere247cc42000-05-07 12:03:14 +1000470 return 0;
471
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000472 if (attempt != 1)
Damien Millerd3a18572000-06-07 19:55:44 +1000473 error("Permission denied, please try again.");
474
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000475 snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
Damien Miller62cee002000-09-23 17:15:56 +1100476 authctxt->server_user, authctxt->host);
Damien Millereba71ba2000-04-29 23:57:08 +1000477 password = read_passphrase(prompt, 0);
478 packet_start(SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100479 packet_put_cstring(authctxt->server_user);
480 packet_put_cstring(authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100481 packet_put_cstring(authctxt->method->name);
Damien Millereba71ba2000-04-29 23:57:08 +1000482 packet_put_char(0);
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000483 packet_put_cstring(password);
Damien Millereba71ba2000-04-29 23:57:08 +1000484 memset(password, 0, strlen(password));
485 xfree(password);
Damien Miller9f643902001-11-12 11:02:52 +1100486 packet_add_padding(64);
Damien Millereba71ba2000-04-29 23:57:08 +1000487 packet_send();
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000488
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000489 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000490 &input_userauth_passwd_changereq);
491
Damien Millereba71ba2000-04-29 23:57:08 +1000492 return 1;
493}
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000494/*
495 * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
496 */
497void
Tim Ricec8549622002-03-31 12:49:38 -0800498input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000499{
500 Authctxt *authctxt = ctxt;
501 char *info, *lang, *password = NULL, *retype = NULL;
502 char prompt[150];
503
504 debug2("input_userauth_passwd_changereq");
505
506 if (authctxt == NULL)
507 fatal("input_userauth_passwd_changereq: "
508 "no authentication context");
509
510 info = packet_get_string(NULL);
511 lang = packet_get_string(NULL);
512 if (strlen(info) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000513 logit("%s", info);
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000514 xfree(info);
515 xfree(lang);
516 packet_start(SSH2_MSG_USERAUTH_REQUEST);
517 packet_put_cstring(authctxt->server_user);
518 packet_put_cstring(authctxt->service);
519 packet_put_cstring(authctxt->method->name);
520 packet_put_char(1); /* additional info */
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000521 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000522 "Enter %.30s@%.128s's old password: ",
523 authctxt->server_user, authctxt->host);
524 password = read_passphrase(prompt, 0);
525 packet_put_cstring(password);
526 memset(password, 0, strlen(password));
527 xfree(password);
528 password = NULL;
529 while (password == NULL) {
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000530 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000531 "Enter %.30s@%.128s's new password: ",
532 authctxt->server_user, authctxt->host);
533 password = read_passphrase(prompt, RP_ALLOW_EOF);
534 if (password == NULL) {
535 /* bail out */
536 return;
537 }
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000538 snprintf(prompt, sizeof(prompt),
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000539 "Retype %.30s@%.128s's new password: ",
540 authctxt->server_user, authctxt->host);
541 retype = read_passphrase(prompt, 0);
542 if (strcmp(password, retype) != 0) {
543 memset(password, 0, strlen(password));
544 xfree(password);
Damien Miller996acd22003-04-09 20:59:48 +1000545 logit("Mismatch; try again, EOF to quit.");
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000546 password = NULL;
547 }
548 memset(retype, 0, strlen(retype));
549 xfree(retype);
550 }
551 packet_put_cstring(password);
552 memset(password, 0, strlen(password));
553 xfree(password);
554 packet_add_padding(64);
555 packet_send();
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000556
557 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000558 &input_userauth_passwd_changereq);
559}
Damien Millereba71ba2000-04-29 23:57:08 +1000560
Ben Lindstrom79073822001-07-04 03:42:30 +0000561static void
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000562clear_auth_state(Authctxt *authctxt)
563{
564 /* XXX clear authentication state */
Ben Lindstrom38a69e62002-03-27 17:28:46 +0000565 dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000566}
567
Ben Lindstrombba81212001-06-25 05:01:22 +0000568static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000569identity_sign(Identity *id, u_char **sigp, u_int *lenp,
570 u_char *data, u_int datalen)
571{
572 Key *prv;
573 int ret;
574
575 /* the agent supports this key */
576 if (id->ac)
577 return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
578 data, datalen));
579 /*
580 * we have already loaded the private key or
581 * the private key is stored in external hardware
582 */
583 if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
584 return (key_sign(id->key, sigp, lenp, data, datalen));
585 /* load the private key from the file */
586 if ((prv = load_identity_file(id->filename)) == NULL)
587 return (-1);
588 ret = key_sign(prv, sigp, lenp, data, datalen);
589 key_free(prv);
590 return (ret);
591}
592
593static int
594sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
Damien Millereba71ba2000-04-29 23:57:08 +1000595{
596 Buffer b;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000597 u_char *blob, *signature;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000598 u_int bloblen, slen;
Damien Miller6536c7d2000-06-22 21:32:31 +1000599 int skip = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000600 int ret = -1;
Damien Miller874d77b2000-10-14 16:23:11 +1100601 int have_sig = 1;
Damien Millereba71ba2000-04-29 23:57:08 +1000602
Ben Lindstromd121f612000-12-03 17:00:47 +0000603 debug3("sign_and_send_pubkey");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000604
Damien Miller280ecfb2003-05-14 13:46:00 +1000605 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100606 /* we cannot handle this key */
Ben Lindstromd121f612000-12-03 17:00:47 +0000607 debug3("sign_and_send_pubkey: cannot handle key");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100608 return 0;
609 }
Damien Millereba71ba2000-04-29 23:57:08 +1000610 /* data to be signed */
611 buffer_init(&b);
Damien Miller50a41ed2000-10-16 12:14:42 +1100612 if (datafellows & SSH_OLD_SESSIONID) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000613 buffer_append(&b, session_id2, session_id2_len);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000614 skip = session_id2_len;
Damien Miller50a41ed2000-10-16 12:14:42 +1100615 } else {
616 buffer_put_string(&b, session_id2, session_id2_len);
617 skip = buffer_len(&b);
Damien Miller6536c7d2000-06-22 21:32:31 +1000618 }
Damien Millereba71ba2000-04-29 23:57:08 +1000619 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100620 buffer_put_cstring(&b, authctxt->server_user);
Damien Miller30c3d422000-05-09 11:02:59 +1000621 buffer_put_cstring(&b,
Ben Lindstromd121f612000-12-03 17:00:47 +0000622 datafellows & SSH_BUG_PKSERVICE ?
Damien Miller30c3d422000-05-09 11:02:59 +1000623 "ssh-userauth" :
Damien Miller62cee002000-09-23 17:15:56 +1100624 authctxt->service);
Ben Lindstromd121f612000-12-03 17:00:47 +0000625 if (datafellows & SSH_BUG_PKAUTH) {
626 buffer_put_char(&b, have_sig);
627 } else {
628 buffer_put_cstring(&b, authctxt->method->name);
629 buffer_put_char(&b, have_sig);
Damien Miller280ecfb2003-05-14 13:46:00 +1000630 buffer_put_cstring(&b, key_ssh_name(id->key));
Ben Lindstromd121f612000-12-03 17:00:47 +0000631 }
Damien Millereba71ba2000-04-29 23:57:08 +1000632 buffer_put_string(&b, blob, bloblen);
Damien Millereba71ba2000-04-29 23:57:08 +1000633
634 /* generate signature */
Damien Miller280ecfb2003-05-14 13:46:00 +1000635 ret = identity_sign(id, &signature, &slen,
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000636 buffer_ptr(&b), buffer_len(&b));
Damien Millerad833b32000-08-23 10:46:23 +1000637 if (ret == -1) {
638 xfree(blob);
639 buffer_free(&b);
640 return 0;
641 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100642#ifdef DEBUG_PK
Damien Millereba71ba2000-04-29 23:57:08 +1000643 buffer_dump(&b);
644#endif
Ben Lindstromd121f612000-12-03 17:00:47 +0000645 if (datafellows & SSH_BUG_PKSERVICE) {
Damien Miller30c3d422000-05-09 11:02:59 +1000646 buffer_clear(&b);
647 buffer_append(&b, session_id2, session_id2_len);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000648 skip = session_id2_len;
Damien Miller30c3d422000-05-09 11:02:59 +1000649 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller62cee002000-09-23 17:15:56 +1100650 buffer_put_cstring(&b, authctxt->server_user);
651 buffer_put_cstring(&b, authctxt->service);
Damien Miller874d77b2000-10-14 16:23:11 +1100652 buffer_put_cstring(&b, authctxt->method->name);
653 buffer_put_char(&b, have_sig);
Ben Lindstromd121f612000-12-03 17:00:47 +0000654 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +1000655 buffer_put_cstring(&b, key_ssh_name(id->key));
Damien Miller30c3d422000-05-09 11:02:59 +1000656 buffer_put_string(&b, blob, bloblen);
657 }
658 xfree(blob);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000659
Damien Millereba71ba2000-04-29 23:57:08 +1000660 /* append signature */
661 buffer_put_string(&b, signature, slen);
662 xfree(signature);
663
664 /* skip session id and packet type */
Damien Miller6536c7d2000-06-22 21:32:31 +1000665 if (buffer_len(&b) < skip + 1)
Damien Miller62cee002000-09-23 17:15:56 +1100666 fatal("userauth_pubkey: internal error");
Damien Miller6536c7d2000-06-22 21:32:31 +1000667 buffer_consume(&b, skip + 1);
Damien Millereba71ba2000-04-29 23:57:08 +1000668
669 /* put remaining data from buffer into packet */
670 packet_start(SSH2_MSG_USERAUTH_REQUEST);
671 packet_put_raw(buffer_ptr(&b), buffer_len(&b));
672 buffer_free(&b);
Damien Millereba71ba2000-04-29 23:57:08 +1000673 packet_send();
Damien Millerad833b32000-08-23 10:46:23 +1000674
675 return 1;
Damien Miller994cf142000-07-21 10:19:44 +1000676}
677
Ben Lindstrombba81212001-06-25 05:01:22 +0000678static int
Damien Miller280ecfb2003-05-14 13:46:00 +1000679send_pubkey_test(Authctxt *authctxt, Identity *id)
Damien Miller994cf142000-07-21 10:19:44 +1000680{
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000681 u_char *blob;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000682 u_int bloblen, have_sig = 0;
Damien Miller994cf142000-07-21 10:19:44 +1000683
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000684 debug3("send_pubkey_test");
685
Damien Miller280ecfb2003-05-14 13:46:00 +1000686 if (key_to_blob(id->key, &blob, &bloblen) == 0) {
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000687 /* we cannot handle this key */
688 debug3("send_pubkey_test: cannot handle key");
Damien Miller994cf142000-07-21 10:19:44 +1000689 return 0;
690 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000691 /* register callback for USERAUTH_PK_OK message */
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000692 dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
Damien Miller994cf142000-07-21 10:19:44 +1000693
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000694 packet_start(SSH2_MSG_USERAUTH_REQUEST);
695 packet_put_cstring(authctxt->server_user);
696 packet_put_cstring(authctxt->service);
697 packet_put_cstring(authctxt->method->name);
698 packet_put_char(have_sig);
699 if (!(datafellows & SSH_BUG_PKAUTH))
Damien Miller280ecfb2003-05-14 13:46:00 +1000700 packet_put_cstring(key_ssh_name(id->key));
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000701 packet_put_string(blob, bloblen);
702 xfree(blob);
703 packet_send();
704 return 1;
705}
706
Ben Lindstrombba81212001-06-25 05:01:22 +0000707static Key *
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000708load_identity_file(char *filename)
709{
710 Key *private;
711 char prompt[300], *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000712 int quit, i;
Ben Lindstrom329782e2001-03-10 17:08:59 +0000713 struct stat st;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000714
Ben Lindstrom329782e2001-03-10 17:08:59 +0000715 if (stat(filename, &st) < 0) {
716 debug3("no such identity: %s", filename);
717 return NULL;
718 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000719 private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
720 if (private == NULL) {
721 if (options.batch_mode)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000722 return NULL;
Damien Miller994cf142000-07-21 10:19:44 +1000723 snprintf(prompt, sizeof prompt,
Damien Miller9f0f5c62001-12-21 14:45:46 +1100724 "Enter passphrase for key '%.100s': ", filename);
Damien Miller62cee002000-09-23 17:15:56 +1100725 for (i = 0; i < options.number_of_password_prompts; i++) {
726 passphrase = read_passphrase(prompt, 0);
727 if (strcmp(passphrase, "") != 0) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000728 private = key_load_private_type(KEY_UNSPEC, filename,
729 passphrase, NULL);
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000730 quit = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100731 } else {
732 debug2("no passphrase given, try next key");
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000733 quit = 1;
Damien Miller62cee002000-09-23 17:15:56 +1100734 }
735 memset(passphrase, 0, strlen(passphrase));
736 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000737 if (private != NULL || quit)
Damien Miller62cee002000-09-23 17:15:56 +1100738 break;
739 debug2("bad passphrase given, try again...");
740 }
Damien Miller994cf142000-07-21 10:19:44 +1000741 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000742 return private;
743}
744
Damien Miller280ecfb2003-05-14 13:46:00 +1000745/*
746 * try keys in the following order:
747 * 1. agent keys that are found in the config file
748 * 2. other agent keys
749 * 3. keys that are only listed in the config file
750 */
751static void
752pubkey_prepare(Authctxt *authctxt)
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000753{
Damien Miller280ecfb2003-05-14 13:46:00 +1000754 Identity *id;
755 Idlist agent, files, *preferred;
756 Key *key;
757 AuthenticationConnection *ac;
Damien Millerad833b32000-08-23 10:46:23 +1000758 char *comment;
Damien Miller280ecfb2003-05-14 13:46:00 +1000759 int i, found;
Damien Millerad833b32000-08-23 10:46:23 +1000760
Damien Miller280ecfb2003-05-14 13:46:00 +1000761 TAILQ_INIT(&agent); /* keys from the agent */
762 TAILQ_INIT(&files); /* keys from the config file */
763 preferred = &authctxt->keys;
764 TAILQ_INIT(preferred); /* preferred order of keys */
765
766 /* list of keys stored in the filesystem */
767 for (i = 0; i < options.num_identity_files; i++) {
768 key = options.identity_keys[i];
769 if (key && key->type == KEY_RSA1)
770 continue;
771 options.identity_keys[i] = NULL;
772 id = xmalloc(sizeof(*id));
773 memset(id, 0, sizeof(*id));
774 id->key = key;
775 id->filename = xstrdup(options.identity_files[i]);
776 TAILQ_INSERT_TAIL(&files, id, next);
Damien Millerad833b32000-08-23 10:46:23 +1000777 }
Damien Miller280ecfb2003-05-14 13:46:00 +1000778 /* list of keys supported by the agent */
779 if ((ac = ssh_get_authentication_connection())) {
780 for (key = ssh_get_first_identity(ac, &comment, 2);
781 key != NULL;
782 key = ssh_get_next_identity(ac, &comment, 2)) {
783 found = 0;
784 TAILQ_FOREACH(id, &files, next) {
785 /* agent keys from the config file are preferred */
786 if (key_equal(key, id->key)) {
787 key_free(key);
788 xfree(comment);
789 TAILQ_REMOVE(&files, id, next);
790 TAILQ_INSERT_TAIL(preferred, id, next);
791 id->ac = ac;
792 found = 1;
793 break;
794 }
795 }
796 if (!found) {
797 id = xmalloc(sizeof(*id));
798 memset(id, 0, sizeof(*id));
799 id->key = key;
800 id->filename = comment;
801 id->ac = ac;
802 TAILQ_INSERT_TAIL(&agent, id, next);
803 }
804 }
805 /* append remaining agent keys */
806 for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
807 TAILQ_REMOVE(&agent, id, next);
808 TAILQ_INSERT_TAIL(preferred, id, next);
809 }
810 authctxt->agent = ac;
Damien Miller62cee002000-09-23 17:15:56 +1100811 }
Damien Miller280ecfb2003-05-14 13:46:00 +1000812 /* append remaining keys from the config file */
813 for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
814 TAILQ_REMOVE(&files, id, next);
815 TAILQ_INSERT_TAIL(preferred, id, next);
816 }
817 TAILQ_FOREACH(id, preferred, next) {
818 debug2("key: %s (%p)", id->filename, id->key);
819 }
820}
821
822static void
823pubkey_cleanup(Authctxt *authctxt)
824{
825 Identity *id;
826
827 if (authctxt->agent != NULL)
828 ssh_close_authentication_connection(authctxt->agent);
829 for (id = TAILQ_FIRST(&authctxt->keys); id;
830 id = TAILQ_FIRST(&authctxt->keys)) {
831 TAILQ_REMOVE(&authctxt->keys, id, next);
832 if (id->key)
833 key_free(id->key);
834 if (id->filename)
835 xfree(id->filename);
836 xfree(id);
837 }
Damien Millereba71ba2000-04-29 23:57:08 +1000838}
839
Damien Miller62cee002000-09-23 17:15:56 +1100840int
841userauth_pubkey(Authctxt *authctxt)
Damien Millereba71ba2000-04-29 23:57:08 +1000842{
Damien Miller280ecfb2003-05-14 13:46:00 +1000843 Identity *id;
Damien Miller62cee002000-09-23 17:15:56 +1100844 int sent = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000845
Damien Miller280ecfb2003-05-14 13:46:00 +1000846 while ((id = TAILQ_FIRST(&authctxt->keys))) {
847 if (id->tried++)
848 return (0);
849 TAILQ_REMOVE(&authctxt->keys, id, next);
850 TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
851 /*
852 * send a test message if we have the public key. for
853 * encrypted keys we cannot do this and have to load the
854 * private key instead
855 */
856 if (id->key && id->key->type != KEY_RSA1) {
857 debug("Offering public key: %s", id->filename);
858 sent = send_pubkey_test(authctxt, id);
859 } else if (id->key == NULL) {
860 debug("Trying private key: %s", id->filename);
861 id->key = load_identity_file(id->filename);
862 if (id->key != NULL) {
863 id->isprivate = 1;
864 sent = sign_and_send_pubkey(authctxt, id);
865 key_free(id->key);
866 id->key = NULL;
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000867 }
Ben Lindstrom266dfdf2001-03-09 00:12:22 +0000868 }
Damien Miller280ecfb2003-05-14 13:46:00 +1000869 if (sent)
870 return (sent);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100871 }
Damien Miller280ecfb2003-05-14 13:46:00 +1000872 return (0);
Damien Miller62cee002000-09-23 17:15:56 +1100873}
Damien Millereba71ba2000-04-29 23:57:08 +1000874
Damien Miller874d77b2000-10-14 16:23:11 +1100875/*
876 * Send userauth request message specifying keyboard-interactive method.
877 */
878int
879userauth_kbdint(Authctxt *authctxt)
880{
881 static int attempt = 0;
882
883 if (attempt++ >= options.number_of_password_prompts)
884 return 0;
Ben Lindstrom7d199962001-09-12 18:29:00 +0000885 /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
886 if (attempt > 1 && !authctxt->info_req_seen) {
887 debug3("userauth_kbdint: disable: no info_req_seen");
888 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
889 return 0;
890 }
Damien Miller874d77b2000-10-14 16:23:11 +1100891
892 debug2("userauth_kbdint");
893 packet_start(SSH2_MSG_USERAUTH_REQUEST);
894 packet_put_cstring(authctxt->server_user);
895 packet_put_cstring(authctxt->service);
896 packet_put_cstring(authctxt->method->name);
897 packet_put_cstring(""); /* lang */
898 packet_put_cstring(options.kbd_interactive_devices ?
899 options.kbd_interactive_devices : "");
900 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100901
902 dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
903 return 1;
904}
905
906/*
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000907 * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
Damien Miller874d77b2000-10-14 16:23:11 +1100908 */
909void
Damien Miller630d6f42002-01-22 23:17:30 +1100910input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
Damien Miller874d77b2000-10-14 16:23:11 +1100911{
912 Authctxt *authctxt = ctxt;
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000913 char *name, *inst, *lang, *prompt, *response;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000914 u_int num_prompts, i;
Damien Miller874d77b2000-10-14 16:23:11 +1100915 int echo = 0;
916
917 debug2("input_userauth_info_req");
918
919 if (authctxt == NULL)
920 fatal("input_userauth_info_req: no authentication context");
921
Ben Lindstrom7d199962001-09-12 18:29:00 +0000922 authctxt->info_req_seen = 1;
923
Damien Miller874d77b2000-10-14 16:23:11 +1100924 name = packet_get_string(NULL);
925 inst = packet_get_string(NULL);
926 lang = packet_get_string(NULL);
Damien Miller874d77b2000-10-14 16:23:11 +1100927 if (strlen(name) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000928 logit("%s", name);
Damien Miller874d77b2000-10-14 16:23:11 +1100929 if (strlen(inst) > 0)
Damien Miller996acd22003-04-09 20:59:48 +1000930 logit("%s", inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000931 xfree(name);
Damien Miller874d77b2000-10-14 16:23:11 +1100932 xfree(inst);
Ben Lindstrom03df5bd2001-02-10 22:16:41 +0000933 xfree(lang);
Damien Miller874d77b2000-10-14 16:23:11 +1100934
935 num_prompts = packet_get_int();
936 /*
937 * Begin to build info response packet based on prompts requested.
938 * We commit to providing the correct number of responses, so if
939 * further on we run into a problem that prevents this, we have to
940 * be sure and clean this up and send a correct error response.
941 */
942 packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
943 packet_put_int(num_prompts);
944
Ben Lindstrom551ea372001-06-05 18:56:16 +0000945 debug2("input_userauth_info_req: num_prompts %d", num_prompts);
Damien Miller874d77b2000-10-14 16:23:11 +1100946 for (i = 0; i < num_prompts; i++) {
947 prompt = packet_get_string(NULL);
948 echo = packet_get_char();
949
Ben Lindstrom949974b2001-06-25 05:20:31 +0000950 response = read_passphrase(prompt, echo ? RP_ECHO : 0);
Damien Miller874d77b2000-10-14 16:23:11 +1100951
Ben Lindstrom5699c5f2001-03-05 06:17:49 +0000952 packet_put_cstring(response);
Damien Miller874d77b2000-10-14 16:23:11 +1100953 memset(response, 0, strlen(response));
954 xfree(response);
955 xfree(prompt);
956 }
Damien Miller48b03fc2002-01-22 23:11:40 +1100957 packet_check_eom(); /* done with parsing incoming message. */
Damien Miller874d77b2000-10-14 16:23:11 +1100958
Damien Miller9f643902001-11-12 11:02:52 +1100959 packet_add_padding(64);
Damien Miller874d77b2000-10-14 16:23:11 +1100960 packet_send();
Damien Miller874d77b2000-10-14 16:23:11 +1100961}
Damien Miller62cee002000-09-23 17:15:56 +1100962
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000963static int
Ben Lindstrom5c385522002-06-23 21:23:20 +0000964ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000965 u_char *data, u_int datalen)
966{
967 Buffer b;
Ben Lindstrom5206b952002-06-06 19:59:29 +0000968 struct stat st;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000969 pid_t pid;
Ben Lindstromcec2ea82002-06-06 20:51:04 +0000970 int to[2], from[2], status, version = 2;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000971
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000972 debug2("ssh_keysign called");
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000973
Ben Lindstrom5206b952002-06-06 19:59:29 +0000974 if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
975 error("ssh_keysign: no installed: %s", strerror(errno));
976 return -1;
977 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000978 if (fflush(stdout) != 0)
979 error("ssh_keysign: fflush: %s", strerror(errno));
980 if (pipe(to) < 0) {
981 error("ssh_keysign: pipe: %s", strerror(errno));
982 return -1;
983 }
984 if (pipe(from) < 0) {
985 error("ssh_keysign: pipe: %s", strerror(errno));
986 return -1;
987 }
988 if ((pid = fork()) < 0) {
989 error("ssh_keysign: fork: %s", strerror(errno));
990 return -1;
991 }
992 if (pid == 0) {
993 seteuid(getuid());
994 setuid(getuid());
995 close(from[0]);
996 if (dup2(from[1], STDOUT_FILENO) < 0)
997 fatal("ssh_keysign: dup2: %s", strerror(errno));
998 close(to[1]);
999 if (dup2(to[0], STDIN_FILENO) < 0)
1000 fatal("ssh_keysign: dup2: %s", strerror(errno));
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001001 close(from[1]);
1002 close(to[0]);
Ben Lindstrom4887da22002-06-06 20:05:57 +00001003 execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001004 fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1005 strerror(errno));
1006 }
1007 close(from[1]);
1008 close(to[0]);
1009
1010 buffer_init(&b);
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001011 buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001012 buffer_put_string(&b, data, datalen);
Damien Miller901119b2002-10-04 11:10:04 +10001013 ssh_msg_send(to[1], version, &b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001014
Damien Miller901119b2002-10-04 11:10:04 +10001015 if (ssh_msg_recv(from[0], &b) < 0) {
Ben Lindstrom5206b952002-06-06 19:59:29 +00001016 error("ssh_keysign: no reply");
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001017 buffer_clear(&b);
1018 return -1;
1019 }
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001020 close(from[0]);
1021 close(to[1]);
1022
Ben Lindstromcec2ea82002-06-06 20:51:04 +00001023 while (waitpid(pid, &status, 0) < 0)
1024 if (errno != EINTR)
1025 break;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001026
Ben Lindstrom5206b952002-06-06 19:59:29 +00001027 if (buffer_get_char(&b) != version) {
1028 error("ssh_keysign: bad version");
1029 buffer_clear(&b);
1030 return -1;
1031 }
1032 *sigp = buffer_get_string(&b, lenp);
1033 buffer_clear(&b);
1034
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001035 return 0;
1036}
1037
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001038int
1039userauth_hostbased(Authctxt *authctxt)
1040{
1041 Key *private = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001042 Sensitive *sensitive = authctxt->sensitive;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001043 Buffer b;
1044 u_char *signature, *blob;
1045 char *chost, *pkalg, *p;
Ben Lindstrom671388f2001-04-19 20:40:45 +00001046 const char *service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001047 u_int blen, slen;
Ben Lindstrom2bffd6f2001-04-19 20:35:40 +00001048 int ok, i, len, found = 0;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001049
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001050 /* check for a useful key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001051 for (i = 0; i < sensitive->nkeys; i++) {
1052 private = sensitive->keys[i];
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001053 if (private && private->type != KEY_RSA1) {
1054 found = 1;
1055 /* we take and free the key */
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001056 sensitive->keys[i] = NULL;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001057 break;
1058 }
1059 }
1060 if (!found) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001061 debug("No more client hostkeys for hostbased authentication.");
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001062 return 0;
1063 }
1064 if (key_to_blob(private, &blob, &blen) == 0) {
1065 key_free(private);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001066 return 0;
1067 }
Damien Miller91c18472001-11-12 11:02:03 +11001068 /* figure out a name for the client host */
1069 p = get_local_name(packet_get_connection_in());
1070 if (p == NULL) {
1071 error("userauth_hostbased: cannot get local ipaddr/name");
1072 key_free(private);
1073 return 0;
1074 }
1075 len = strlen(p) + 2;
1076 chost = xmalloc(len);
1077 strlcpy(chost, p, len);
1078 strlcat(chost, ".", len);
1079 debug2("userauth_hostbased: chost %s", chost);
Damien Miller00111382003-03-10 11:21:17 +11001080 xfree(p);
Damien Miller91c18472001-11-12 11:02:03 +11001081
Ben Lindstrom671388f2001-04-19 20:40:45 +00001082 service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1083 authctxt->service;
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001084 pkalg = xstrdup(key_ssh_name(private));
1085 buffer_init(&b);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001086 /* construct data */
Ben Lindstrom671388f2001-04-19 20:40:45 +00001087 buffer_put_string(&b, session_id2, session_id2_len);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001088 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1089 buffer_put_cstring(&b, authctxt->server_user);
Ben Lindstrom671388f2001-04-19 20:40:45 +00001090 buffer_put_cstring(&b, service);
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001091 buffer_put_cstring(&b, authctxt->method->name);
1092 buffer_put_cstring(&b, pkalg);
1093 buffer_put_string(&b, blob, blen);
1094 buffer_put_cstring(&b, chost);
1095 buffer_put_cstring(&b, authctxt->local_user);
1096#ifdef DEBUG_PK
1097 buffer_dump(&b);
1098#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001099 if (sensitive->external_keysign)
1100 ok = ssh_keysign(private, &signature, &slen,
1101 buffer_ptr(&b), buffer_len(&b));
1102 else
1103 ok = key_sign(private, &signature, &slen,
1104 buffer_ptr(&b), buffer_len(&b));
Ben Lindstrom5eabda32001-04-12 23:34:34 +00001105 key_free(private);
1106 buffer_free(&b);
1107 if (ok != 0) {
1108 error("key_sign failed");
1109 xfree(chost);
1110 xfree(pkalg);
1111 return 0;
1112 }
1113 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1114 packet_put_cstring(authctxt->server_user);
1115 packet_put_cstring(authctxt->service);
1116 packet_put_cstring(authctxt->method->name);
1117 packet_put_cstring(pkalg);
1118 packet_put_string(blob, blen);
1119 packet_put_cstring(chost);
1120 packet_put_cstring(authctxt->local_user);
1121 packet_put_string(signature, slen);
1122 memset(signature, 's', slen);
1123 xfree(signature);
1124 xfree(chost);
1125 xfree(pkalg);
1126
1127 packet_send();
1128 return 1;
1129}
1130
Damien Miller3ab496b2003-05-14 13:47:37 +10001131#if KRB5
1132static int
Damien Miller4d995192003-05-14 19:23:56 +10001133ssh_krb5_helper(krb5_data *ap, krb5_context *context)
Damien Miller3ab496b2003-05-14 13:47:37 +10001134{
1135 krb5_context xcontext = NULL; /* XXX share with ssh1 */
1136 krb5_auth_context xauth_context = NULL;
Damien Miller3ab496b2003-05-14 13:47:37 +10001137 krb5_auth_context *auth_context;
1138 krb5_error_code problem;
1139 const char *tkfile;
1140 struct stat buf;
1141 krb5_ccache ccache = NULL;
1142 const char *remotehost;
1143 int ret;
1144
1145 memset(ap, 0, sizeof(*ap));
1146
1147 context = &xcontext;
1148 auth_context = &xauth_context;
1149
1150 problem = krb5_init_context(context);
1151 if (problem) {
1152 debug("Kerberos v5: krb5_init_context failed");
1153 ret = 0;
1154 goto out;
1155 }
1156
1157 tkfile = krb5_cc_default_name(*context);
1158 if (strncmp(tkfile, "FILE:", 5) == 0)
1159 tkfile += 5;
1160
1161 if (stat(tkfile, &buf) == 0 && getuid() != buf.st_uid) {
1162 debug("Kerberos v5: could not get default ccache (permission denied).");
1163 ret = 0;
1164 goto out;
1165 }
1166
1167 problem = krb5_cc_default(*context, &ccache);
1168 if (problem) {
1169 debug("Kerberos v5: krb5_cc_default failed: %s",
1170 krb5_get_err_text(*context, problem));
1171 ret = 0;
1172 goto out;
1173 }
1174
1175 remotehost = get_canonical_hostname(1);
1176
1177 problem = krb5_mk_req(*context, auth_context, AP_OPTS_MUTUAL_REQUIRED,
1178 "host", remotehost, NULL, ccache, ap);
1179 if (problem) {
1180 debug("Kerberos v5: krb5_mk_req failed: %s",
1181 krb5_get_err_text(*context, problem));
1182 ret = 0;
1183 goto out;
1184 }
1185 ret = 1;
1186
1187 out:
1188 if (ccache != NULL)
1189 krb5_cc_close(*context, ccache);
1190 if (*auth_context)
1191 krb5_auth_con_free(*context, *auth_context);
Damien Miller3ab496b2003-05-14 13:47:37 +10001192 return (ret);
1193}
1194
1195int
1196userauth_kerberos(Authctxt *authctxt)
1197{
1198 krb5_data ap;
Damien Miller4d995192003-05-14 19:23:56 +10001199 krb5_context *context;
1200 int ret = 0;
Damien Miller3ab496b2003-05-14 13:47:37 +10001201
Damien Miller4d995192003-05-14 19:23:56 +10001202 if (ssh_krb5_helper(&ap, context) == 0)
1203 goto out;
Damien Miller3ab496b2003-05-14 13:47:37 +10001204
1205 packet_start(SSH2_MSG_USERAUTH_REQUEST);
1206 packet_put_cstring(authctxt->server_user);
1207 packet_put_cstring(authctxt->service);
1208 packet_put_cstring(authctxt->method->name);
1209 packet_put_string(ap.data, ap.length);
1210 packet_send();
1211
Damien Miller9c617692003-05-14 14:31:11 +10001212#ifdef HEIMDAL
Damien Miller3ab496b2003-05-14 13:47:37 +10001213 krb5_data_free(&ap);
Damien Miller9c617692003-05-14 14:31:11 +10001214#else
Damien Miller4d995192003-05-14 19:23:56 +10001215 krb5_free_data_contents(*context, &ap);
Damien Miller9c617692003-05-14 14:31:11 +10001216#endif
Damien Miller4d995192003-05-14 19:23:56 +10001217 ret = 1;
Damien Miller9c617692003-05-14 14:31:11 +10001218
Damien Miller4d995192003-05-14 19:23:56 +10001219out:
1220 if (*context)
1221 krb5_free_context(*context);
1222 return ret;
Damien Miller3ab496b2003-05-14 13:47:37 +10001223}
1224#endif
1225
Damien Miller62cee002000-09-23 17:15:56 +11001226/* find auth method */
1227
Damien Miller62cee002000-09-23 17:15:56 +11001228/*
1229 * given auth method name, if configurable options permit this method fill
1230 * in auth_ident field and return true, otherwise return false.
1231 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001232static int
Damien Miller62cee002000-09-23 17:15:56 +11001233authmethod_is_enabled(Authmethod *method)
1234{
1235 if (method == NULL)
1236 return 0;
1237 /* return false if options indicate this method is disabled */
1238 if (method->enabled == NULL || *method->enabled == 0)
1239 return 0;
1240 /* return false if batch mode is enabled but method needs interactive mode */
1241 if (method->batch_flag != NULL && *method->batch_flag != 0)
1242 return 0;
1243 return 1;
1244}
1245
Ben Lindstrombba81212001-06-25 05:01:22 +00001246static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001247authmethod_lookup(const char *name)
1248{
1249 Authmethod *method = NULL;
1250 if (name != NULL)
1251 for (method = authmethods; method->name != NULL; method++)
1252 if (strcmp(name, method->name) == 0)
1253 return method;
1254 debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1255 return NULL;
1256}
1257
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001258/* XXX internal state */
1259static Authmethod *current = NULL;
1260static char *supported = NULL;
1261static char *preferred = NULL;
Ben Lindstrom5c385522002-06-23 21:23:20 +00001262
Damien Miller62cee002000-09-23 17:15:56 +11001263/*
1264 * Given the authentication method list sent by the server, return the
1265 * next method we should try. If the server initially sends a nil list,
Ben Lindstroma3700052001-04-05 23:26:32 +00001266 * use a built-in default list.
Kevin Stevesef4eea92001-02-05 12:42:17 +00001267 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001268static Authmethod *
Damien Miller62cee002000-09-23 17:15:56 +11001269authmethod_get(char *authlist)
1270{
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001271 char *name = NULL;
Ben Lindstromc58ab022002-02-26 18:15:09 +00001272 u_int next;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001273
Damien Miller62cee002000-09-23 17:15:56 +11001274 /* Use a suitable default if we're passed a nil list. */
1275 if (authlist == NULL || strlen(authlist) == 0)
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001276 authlist = options.preferred_authentications;
Damien Miller62cee002000-09-23 17:15:56 +11001277
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001278 if (supported == NULL || strcmp(authlist, supported) != 0) {
1279 debug3("start over, passed a different list %s", authlist);
1280 if (supported != NULL)
1281 xfree(supported);
1282 supported = xstrdup(authlist);
1283 preferred = options.preferred_authentications;
1284 debug3("preferred %s", preferred);
1285 current = NULL;
1286 } else if (current != NULL && authmethod_is_enabled(current))
1287 return current;
Damien Millereba71ba2000-04-29 23:57:08 +10001288
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001289 for (;;) {
1290 if ((name = match_list(preferred, supported, &next)) == NULL) {
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001291 debug("No more authentication methods to try.");
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001292 current = NULL;
1293 return NULL;
Damien Miller874d77b2000-10-14 16:23:11 +11001294 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001295 preferred += next;
1296 debug3("authmethod_lookup %s", name);
1297 debug3("remaining preferred: %s", preferred);
1298 if ((current = authmethod_lookup(name)) != NULL &&
1299 authmethod_is_enabled(current)) {
1300 debug3("authmethod_is_enabled %s", name);
Ben Lindstrom1d568f92002-12-23 02:44:36 +00001301 debug("Next authentication method: %s", name);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001302 return current;
1303 }
Damien Millereba71ba2000-04-29 23:57:08 +10001304 }
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001305}
Damien Miller62cee002000-09-23 17:15:56 +11001306
Ben Lindstrom79073822001-07-04 03:42:30 +00001307static char *
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001308authmethods_get(void)
1309{
1310 Authmethod *method = NULL;
Damien Miller0e3b8722002-01-22 23:26:38 +11001311 Buffer b;
1312 char *list;
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001313
Damien Miller0e3b8722002-01-22 23:26:38 +11001314 buffer_init(&b);
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001315 for (method = authmethods; method->name != NULL; method++) {
1316 if (authmethod_is_enabled(method)) {
Damien Miller0e3b8722002-01-22 23:26:38 +11001317 if (buffer_len(&b) > 0)
1318 buffer_append(&b, ",", 1);
1319 buffer_append(&b, method->name, strlen(method->name));
Ben Lindstromb9be60a2001-03-11 01:49:19 +00001320 }
Damien Miller62cee002000-09-23 17:15:56 +11001321 }
Damien Miller0e3b8722002-01-22 23:26:38 +11001322 buffer_append(&b, "\0", 1);
1323 list = xstrdup(buffer_ptr(&b));
1324 buffer_free(&b);
1325 return list;
Damien Millereba71ba2000-04-29 23:57:08 +10001326}