Branch data Line data Source code
1 : : /* $OpenBSD: sshconnect2.c,v 1.204 2014/02/02 03:44:32 djm Exp $ */
2 : : /*
3 : : * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 : : * Copyright (c) 2008 Damien Miller. All rights reserved.
5 : : *
6 : : * Redistribution and use in source and binary forms, with or without
7 : : * modification, are permitted provided that the following conditions
8 : : * are met:
9 : : * 1. Redistributions of source code must retain the above copyright
10 : : * notice, this list of conditions and the following disclaimer.
11 : : * 2. Redistributions in binary form must reproduce the above copyright
12 : : * notice, this list of conditions and the following disclaimer in the
13 : : * documentation and/or other materials provided with the distribution.
14 : : *
15 : : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 : : * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 : : * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 : : * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 : : * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 : : * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 : : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 : : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 : : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 : : * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 : : */
26 : :
27 : : #include "includes.h"
28 : :
29 : : #include <sys/types.h>
30 : : #include <sys/socket.h>
31 : : #include <sys/wait.h>
32 : : #include <sys/stat.h>
33 : :
34 : : #include <errno.h>
35 : : #include <fcntl.h>
36 : : #include <netdb.h>
37 : : #include <pwd.h>
38 : : #include <signal.h>
39 : : #include <stdarg.h>
40 : : #include <stdio.h>
41 : : #include <string.h>
42 : : #include <unistd.h>
43 : : #if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H) && !defined(BROKEN_STRNVIS)
44 : : #include <vis.h>
45 : : #endif
46 : :
47 : : #include "openbsd-compat/sys-queue.h"
48 : :
49 : : #include "xmalloc.h"
50 : : #include "ssh.h"
51 : : #include "ssh2.h"
52 : : #include "buffer.h"
53 : : #include "packet.h"
54 : : #include "compat.h"
55 : : #include "cipher.h"
56 : : #include "key.h"
57 : : #include "kex.h"
58 : : #include "myproposal.h"
59 : : #include "sshconnect.h"
60 : : #include "authfile.h"
61 : : #include "dh.h"
62 : : #include "authfd.h"
63 : : #include "log.h"
64 : : #include "readconf.h"
65 : : #include "misc.h"
66 : : #include "match.h"
67 : : #include "dispatch.h"
68 : : #include "canohost.h"
69 : : #include "msg.h"
70 : : #include "pathnames.h"
71 : : #include "uidswap.h"
72 : : #include "hostfile.h"
73 : :
74 : : #ifdef GSSAPI
75 : : #include "ssh-gss.h"
76 : : #endif
77 : :
78 : : /* import */
79 : : extern char *client_version_string;
80 : : extern char *server_version_string;
81 : : extern Options options;
82 : :
83 : : /*
84 : : * SSH2 key exchange
85 : : */
86 : :
87 : : u_char *session_id2 = NULL;
88 : : u_int session_id2_len = 0;
89 : :
90 : : char *xxx_host;
91 : : struct sockaddr *xxx_hostaddr;
92 : :
93 : : Kex *xxx_kex = NULL;
94 : :
95 : : static int
96 : 1138 : verify_host_key_callback(Key *hostkey)
97 : : {
98 [ - + ]: 1138 : if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
99 : 0 : fatal("Host key verification failed.");
100 : 1138 : return 0;
101 : : }
102 : :
103 : : static char *
104 : 688 : order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
105 : : {
106 : : char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
107 : : size_t maxlen;
108 : : struct hostkeys *hostkeys;
109 : : int ktype;
110 : : u_int i;
111 : :
112 : : /* Find all hostkeys for this hostname */
113 : 688 : get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
114 : 688 : hostkeys = init_hostkeys();
115 [ + + ]: 1376 : for (i = 0; i < options.num_user_hostfiles; i++)
116 : 688 : load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
117 [ + + ]: 1376 : for (i = 0; i < options.num_system_hostfiles; i++)
118 : 688 : load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
119 : :
120 : 688 : oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
121 : 688 : maxlen = strlen(avail) + 1;
122 : 688 : first = xmalloc(maxlen);
123 : 688 : last = xmalloc(maxlen);
124 : 688 : *first = *last = '\0';
125 : :
126 : : #define ALG_APPEND(to, from) \
127 : : do { \
128 : : if (*to != '\0') \
129 : : strlcat(to, ",", maxlen); \
130 : : strlcat(to, from, maxlen); \
131 : : } while (0)
132 : :
133 [ + + ][ + - ]: 11008 : while ((alg = strsep(&avail, ",")) && *alg != '\0') {
134 [ - + ]: 9632 : if ((ktype = key_type_from_name(alg)) == KEY_UNSPEC)
135 : 0 : fatal("%s: unknown alg %s", __func__, alg);
136 [ + + ]: 9632 : if (lookup_key_in_hostkeys_by_type(hostkeys,
137 : : key_type_plain(ktype), NULL))
138 [ + + ]: 2028 : ALG_APPEND(first, alg);
139 : : else
140 [ + + ]: 9632 : ALG_APPEND(last, alg);
141 : : }
142 : : #undef ALG_APPEND
143 [ + + ]: 688 : xasprintf(&ret, "%s%s%s", first, *first == '\0' ? "" : ",", last);
144 [ + + ]: 688 : if (*first != '\0')
145 : 668 : debug3("%s: prefer hostkeyalgs: %s", __func__, first);
146 : :
147 : 688 : free(first);
148 : 688 : free(last);
149 : 688 : free(hostname);
150 : 688 : free(oavail);
151 : 688 : free_hostkeys(hostkeys);
152 : :
153 : 688 : return ret;
154 : : }
155 : :
156 : : void
157 : 688 : ssh_kex2(char *host, struct sockaddr *hostaddr, u_short port)
158 : : {
159 : : Kex *kex;
160 : :
161 : 688 : xxx_host = host;
162 : 688 : xxx_hostaddr = hostaddr;
163 : :
164 [ - + ]: 688 : if (options.ciphers == (char *)-1) {
165 : 0 : logit("No valid ciphers for protocol version 2 given, using defaults.");
166 : 0 : options.ciphers = NULL;
167 : : }
168 [ + + ]: 688 : if (options.ciphers != NULL) {
169 : 293 : myproposal[PROPOSAL_ENC_ALGS_CTOS] =
170 : 293 : myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
171 : : }
172 : 688 : myproposal[PROPOSAL_ENC_ALGS_CTOS] =
173 : 688 : compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
174 : 688 : myproposal[PROPOSAL_ENC_ALGS_STOC] =
175 : 688 : compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
176 [ - + ]: 688 : if (options.compression) {
177 : 0 : myproposal[PROPOSAL_COMP_ALGS_CTOS] =
178 : 0 : myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib@openssh.com,zlib,none";
179 : : } else {
180 : 688 : myproposal[PROPOSAL_COMP_ALGS_CTOS] =
181 : 688 : myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib@openssh.com,zlib";
182 : : }
183 [ + + ]: 688 : if (options.macs != NULL) {
184 : 269 : myproposal[PROPOSAL_MAC_ALGS_CTOS] =
185 : 269 : myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
186 : : }
187 [ - + ]: 688 : if (options.hostkeyalgorithms != NULL)
188 : 0 : myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
189 : 0 : compat_pkalg_proposal(options.hostkeyalgorithms);
190 : : else {
191 : : /* Prefer algorithms that we already have keys for */
192 : 688 : myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
193 : 688 : compat_pkalg_proposal(
194 : : order_hostkeyalgs(host, hostaddr, port));
195 : : }
196 [ + + ]: 688 : if (options.kex_algorithms != NULL)
197 : 72 : myproposal[PROPOSAL_KEX_ALGS] = options.kex_algorithms;
198 : :
199 [ + + ][ + + ]: 688 : if (options.rekey_limit || options.rekey_interval)
200 : 79 : packet_set_rekey_limits((u_int32_t)options.rekey_limit,
201 : 79 : (time_t)options.rekey_interval);
202 : :
203 : : /* start key exchange */
204 : 688 : kex = kex_setup(myproposal);
205 : 688 : kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
206 : 688 : kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
207 : 688 : kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
208 : 688 : kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
209 : 688 : kex->kex[KEX_ECDH_SHA2] = kexecdh_client;
210 : 688 : kex->kex[KEX_C25519_SHA256] = kexc25519_client;
211 : 688 : kex->client_version_string=client_version_string;
212 : 688 : kex->server_version_string=server_version_string;
213 : 688 : kex->verify_host_key=&verify_host_key_callback;
214 : :
215 : 688 : xxx_kex = kex;
216 : :
217 : 688 : dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
218 : :
219 [ + - ][ + - ]: 688 : if (options.use_roaming && !kex->roaming) {
220 : 688 : debug("Roaming not allowed by server");
221 : 688 : options.use_roaming = 0;
222 : : }
223 : :
224 : 688 : session_id2 = kex->session_id;
225 : 688 : session_id2_len = kex->session_id_len;
226 : :
227 : : #ifdef DEBUG_KEXDH
228 : : /* send 1st encrypted/maced/compressed message */
229 : : packet_start(SSH2_MSG_IGNORE);
230 : : packet_put_cstring("markus");
231 : : packet_send();
232 : : packet_write_wait();
233 : : #endif
234 : 688 : }
235 : :
236 : : /*
237 : : * Authenticate user
238 : : */
239 : :
240 : : typedef struct Authctxt Authctxt;
241 : : typedef struct Authmethod Authmethod;
242 : : typedef struct identity Identity;
243 : : typedef struct idlist Idlist;
244 : :
245 : : struct identity {
246 : : TAILQ_ENTRY(identity) next;
247 : : AuthenticationConnection *ac; /* set if agent supports key */
248 : : Key *key; /* public/private key */
249 : : char *filename; /* comment for agent-only keys */
250 : : int tried;
251 : : int isprivate; /* key points to the private key */
252 : : int userprovided;
253 : : };
254 : : TAILQ_HEAD(idlist, identity);
255 : :
256 : : struct Authctxt {
257 : : const char *server_user;
258 : : const char *local_user;
259 : : const char *host;
260 : : const char *service;
261 : : Authmethod *method;
262 : : sig_atomic_t success;
263 : : char *authlist;
264 : : /* pubkey */
265 : : Idlist keys;
266 : : AuthenticationConnection *agent;
267 : : /* hostbased */
268 : : Sensitive *sensitive;
269 : : /* kbd-interactive */
270 : : int info_req_seen;
271 : : /* generic */
272 : : void *methoddata;
273 : : };
274 : : struct Authmethod {
275 : : char *name; /* string to compare against server's list */
276 : : int (*userauth)(Authctxt *authctxt);
277 : : void (*cleanup)(Authctxt *authctxt);
278 : : int *enabled; /* flag in option struct that enables method */
279 : : int *batch_flag; /* flag in option struct that disables method */
280 : : };
281 : :
282 : : void input_userauth_success(int, u_int32_t, void *);
283 : : void input_userauth_success_unexpected(int, u_int32_t, void *);
284 : : void input_userauth_failure(int, u_int32_t, void *);
285 : : void input_userauth_banner(int, u_int32_t, void *);
286 : : void input_userauth_error(int, u_int32_t, void *);
287 : : void input_userauth_info_req(int, u_int32_t, void *);
288 : : void input_userauth_pk_ok(int, u_int32_t, void *);
289 : : void input_userauth_passwd_changereq(int, u_int32_t, void *);
290 : :
291 : : int userauth_none(Authctxt *);
292 : : int userauth_pubkey(Authctxt *);
293 : : int userauth_passwd(Authctxt *);
294 : : int userauth_kbdint(Authctxt *);
295 : : int userauth_hostbased(Authctxt *);
296 : :
297 : : #ifdef GSSAPI
298 : : int userauth_gssapi(Authctxt *authctxt);
299 : : void input_gssapi_response(int type, u_int32_t, void *);
300 : : void input_gssapi_token(int type, u_int32_t, void *);
301 : : void input_gssapi_hash(int type, u_int32_t, void *);
302 : : void input_gssapi_error(int, u_int32_t, void *);
303 : : void input_gssapi_errtok(int, u_int32_t, void *);
304 : : #endif
305 : :
306 : : void userauth(Authctxt *, char *);
307 : :
308 : : static int sign_and_send_pubkey(Authctxt *, Identity *);
309 : : static void pubkey_prepare(Authctxt *);
310 : : static void pubkey_cleanup(Authctxt *);
311 : : static Key *load_identity_file(char *, int);
312 : :
313 : : static Authmethod *authmethod_get(char *authlist);
314 : : static Authmethod *authmethod_lookup(const char *name);
315 : : static char *authmethods_get(void);
316 : :
317 : : Authmethod authmethods[] = {
318 : : #ifdef GSSAPI
319 : : {"gssapi-with-mic",
320 : : userauth_gssapi,
321 : : NULL,
322 : : &options.gss_authentication,
323 : : NULL},
324 : : #endif
325 : : {"hostbased",
326 : : userauth_hostbased,
327 : : NULL,
328 : : &options.hostbased_authentication,
329 : : NULL},
330 : : {"publickey",
331 : : userauth_pubkey,
332 : : NULL,
333 : : &options.pubkey_authentication,
334 : : NULL},
335 : : {"keyboard-interactive",
336 : : userauth_kbdint,
337 : : NULL,
338 : : &options.kbd_interactive_authentication,
339 : : &options.batch_mode},
340 : : {"password",
341 : : userauth_passwd,
342 : : NULL,
343 : : &options.password_authentication,
344 : : &options.batch_mode},
345 : : {"none",
346 : : userauth_none,
347 : : NULL,
348 : : NULL,
349 : : NULL},
350 : : {NULL, NULL, NULL, NULL, NULL}
351 : : };
352 : :
353 : : void
354 : 688 : ssh_userauth2(const char *local_user, const char *server_user, char *host,
355 : : Sensitive *sensitive)
356 : : {
357 : : Authctxt authctxt;
358 : : int type;
359 : :
360 [ - + ]: 688 : if (options.challenge_response_authentication)
361 : 0 : options.kbd_interactive_authentication = 1;
362 : :
363 : 688 : packet_start(SSH2_MSG_SERVICE_REQUEST);
364 : 688 : packet_put_cstring("ssh-userauth");
365 : 688 : packet_send();
366 : 688 : debug("SSH2_MSG_SERVICE_REQUEST sent");
367 : 688 : packet_write_wait();
368 : 688 : type = packet_read();
369 [ - + ]: 688 : if (type != SSH2_MSG_SERVICE_ACCEPT)
370 : 0 : fatal("Server denied authentication request: %d", type);
371 [ + - ]: 688 : if (packet_remaining() > 0) {
372 : 688 : char *reply = packet_get_string(NULL);
373 : 688 : debug2("service_accept: %s", reply);
374 : 688 : free(reply);
375 : : } else {
376 : 0 : debug2("buggy server: service_accept w/o service");
377 : : }
378 [ - + ]: 688 : packet_check_eom();
379 : 688 : debug("SSH2_MSG_SERVICE_ACCEPT received");
380 : :
381 [ + - ]: 688 : if (options.preferred_authentications == NULL)
382 : 688 : options.preferred_authentications = authmethods_get();
383 : :
384 : : /* setup authentication context */
385 : : memset(&authctxt, 0, sizeof(authctxt));
386 : 688 : pubkey_prepare(&authctxt);
387 : 688 : authctxt.server_user = server_user;
388 : 688 : authctxt.local_user = local_user;
389 : 688 : authctxt.host = host;
390 : 688 : authctxt.service = "ssh-connection"; /* service name */
391 : 688 : authctxt.success = 0;
392 : 688 : authctxt.method = authmethod_lookup("none");
393 : 688 : authctxt.authlist = NULL;
394 : 688 : authctxt.methoddata = NULL;
395 : 688 : authctxt.sensitive = sensitive;
396 : 688 : authctxt.info_req_seen = 0;
397 [ - + ]: 688 : if (authctxt.method == NULL)
398 : 0 : fatal("ssh_userauth2: internal error: cannot send userauth none request");
399 : :
400 : : /* initial userauth request */
401 : 688 : userauth_none(&authctxt);
402 : :
403 : 688 : dispatch_init(&input_userauth_error);
404 : 688 : dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
405 : 688 : dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
406 : 688 : dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
407 : 688 : dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
408 : :
409 : 688 : pubkey_cleanup(&authctxt);
410 : 688 : dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
411 : :
412 : 688 : debug("Authentication succeeded (%s).", authctxt.method->name);
413 : 688 : }
414 : :
415 : : void
416 : 834 : userauth(Authctxt *authctxt, char *authlist)
417 : : {
418 [ + - ][ - + ]: 834 : if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
419 : 0 : authctxt->method->cleanup(authctxt);
420 : :
421 : 834 : free(authctxt->methoddata);
422 : 834 : authctxt->methoddata = NULL;
423 [ - + ]: 834 : if (authlist == NULL) {
424 : 0 : authlist = authctxt->authlist;
425 : : } else {
426 : 834 : free(authctxt->authlist);
427 : 834 : authctxt->authlist = authlist;
428 : : }
429 : : for (;;) {
430 : 834 : Authmethod *method = authmethod_get(authlist);
431 [ - + ]: 834 : if (method == NULL)
432 : 0 : fatal("Permission denied (%s).", authlist);
433 : 834 : authctxt->method = method;
434 : :
435 : : /* reset the per method handler */
436 : 834 : dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
437 : : SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
438 : :
439 : : /* and try new method */
440 [ + - ]: 834 : if (method->userauth(authctxt) != 0) {
441 : 834 : debug2("we sent a %s packet, wait for reply", method->name);
442 : : break;
443 : : } else {
444 : 0 : debug2("we did not send a packet, disable method");
445 : 0 : method->enabled = NULL;
446 : : }
447 : 0 : }
448 : 834 : }
449 : :
450 : : /* ARGSUSED */
451 : : void
452 : 0 : input_userauth_error(int type, u_int32_t seq, void *ctxt)
453 : : {
454 : 0 : fatal("input_userauth_error: bad message during authentication: "
455 : : "type %d", type);
456 : : }
457 : :
458 : : /* ARGSUSED */
459 : : void
460 : 6 : input_userauth_banner(int type, u_int32_t seq, void *ctxt)
461 : : {
462 : : char *msg, *raw, *lang;
463 : : u_int len;
464 : :
465 : 6 : debug3("input_userauth_banner");
466 : 6 : raw = packet_get_string(&len);
467 : 6 : lang = packet_get_string(NULL);
468 [ + - ][ + + ]: 6 : if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
469 [ + + ]: 5 : if (len > 65536)
470 : 1 : len = 65536;
471 : 5 : msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
472 : 5 : strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
473 : 5 : fprintf(stderr, "%s", msg);
474 : 5 : free(msg);
475 : : }
476 : 6 : free(raw);
477 : 6 : free(lang);
478 : 6 : }
479 : :
480 : : /* ARGSUSED */
481 : : void
482 : 688 : input_userauth_success(int type, u_int32_t seq, void *ctxt)
483 : : {
484 : 688 : Authctxt *authctxt = ctxt;
485 : :
486 [ - + ]: 688 : if (authctxt == NULL)
487 : 0 : fatal("input_userauth_success: no authentication context");
488 : 688 : free(authctxt->authlist);
489 : 688 : authctxt->authlist = NULL;
490 [ + - ][ - + ]: 688 : if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
491 : 0 : authctxt->method->cleanup(authctxt);
492 : 688 : free(authctxt->methoddata);
493 : 688 : authctxt->methoddata = NULL;
494 : 688 : authctxt->success = 1; /* break out */
495 : 688 : }
496 : :
497 : : void
498 : 0 : input_userauth_success_unexpected(int type, u_int32_t seq, void *ctxt)
499 : : {
500 : 0 : Authctxt *authctxt = ctxt;
501 : :
502 [ # # ]: 0 : if (authctxt == NULL)
503 : 0 : fatal("%s: no authentication context", __func__);
504 : :
505 : 0 : fatal("Unexpected authentication success during %s.",
506 : 0 : authctxt->method->name);
507 : : }
508 : :
509 : : /* ARGSUSED */
510 : : void
511 : 834 : input_userauth_failure(int type, u_int32_t seq, void *ctxt)
512 : : {
513 : 834 : Authctxt *authctxt = ctxt;
514 : 834 : char *authlist = NULL;
515 : : int partial;
516 : :
517 [ - + ]: 834 : if (authctxt == NULL)
518 : 0 : fatal("input_userauth_failure: no authentication context");
519 : :
520 : 834 : authlist = packet_get_string(NULL);
521 : 834 : partial = packet_get_char();
522 [ - + ]: 834 : packet_check_eom();
523 : :
524 [ - + ]: 834 : if (partial != 0) {
525 : 0 : logit("Authenticated with partial success.");
526 : : /* reset state */
527 : 0 : pubkey_cleanup(authctxt);
528 : 0 : pubkey_prepare(authctxt);
529 : : }
530 : 834 : debug("Authentications that can continue: %s", authlist);
531 : :
532 : 834 : userauth(authctxt, authlist);
533 : 834 : }
534 : :
535 : : /* ARGSUSED */
536 : : void
537 : 688 : input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
538 : : {
539 : 688 : Authctxt *authctxt = ctxt;
540 : 688 : Key *key = NULL;
541 : 688 : Identity *id = NULL;
542 : : Buffer b;
543 : 688 : int pktype, sent = 0;
544 : : u_int alen, blen;
545 : : char *pkalg, *fp;
546 : : u_char *pkblob;
547 : :
548 [ - + ]: 688 : if (authctxt == NULL)
549 : 0 : fatal("input_userauth_pk_ok: no authentication context");
550 [ - + ]: 688 : if (datafellows & SSH_BUG_PKOK) {
551 : : /* this is similar to SSH_BUG_PKAUTH */
552 : 0 : debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
553 : 0 : pkblob = packet_get_string(&blen);
554 : 0 : buffer_init(&b);
555 : 0 : buffer_append(&b, pkblob, blen);
556 : 0 : pkalg = buffer_get_string(&b, &alen);
557 : 0 : buffer_free(&b);
558 : : } else {
559 : 688 : pkalg = packet_get_string(&alen);
560 : 688 : pkblob = packet_get_string(&blen);
561 : : }
562 [ - + ]: 688 : packet_check_eom();
563 : :
564 : 688 : debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
565 : :
566 [ - + ]: 688 : if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
567 : 0 : debug("unknown pkalg %s", pkalg);
568 : 0 : goto done;
569 : : }
570 [ - + ]: 688 : if ((key = key_from_blob(pkblob, blen)) == NULL) {
571 : 0 : debug("no key from blob. pkalg %s", pkalg);
572 : 0 : goto done;
573 : : }
574 [ - + ]: 688 : if (key->type != pktype) {
575 : 0 : error("input_userauth_pk_ok: type mismatch "
576 : : "for decoded key (received %d, expected %d)",
577 : : key->type, pktype);
578 : 0 : goto done;
579 : : }
580 : 688 : fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
581 : 688 : debug2("input_userauth_pk_ok: fp %s", fp);
582 : 688 : free(fp);
583 : :
584 : : /*
585 : : * search keys in the reverse order, because last candidate has been
586 : : * moved to the end of the queue. this also avoids confusion by
587 : : * duplicate keys
588 : : */
589 [ + - ]: 688 : TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
590 [ + - ]: 688 : if (key_equal(key, id->key)) {
591 : 688 : sent = sign_and_send_pubkey(authctxt, id);
592 : 688 : break;
593 : : }
594 : : }
595 : : done:
596 [ + - ]: 688 : if (key != NULL)
597 : 688 : key_free(key);
598 : 688 : free(pkalg);
599 : 688 : free(pkblob);
600 : :
601 : : /* try another method if we did not send a packet */
602 [ - + ]: 688 : if (sent == 0)
603 : 0 : userauth(authctxt, NULL);
604 : 688 : }
605 : :
606 : : #ifdef GSSAPI
607 : : int
608 : : userauth_gssapi(Authctxt *authctxt)
609 : : {
610 : : Gssctxt *gssctxt = NULL;
611 : : static gss_OID_set gss_supported = NULL;
612 : : static u_int mech = 0;
613 : : OM_uint32 min;
614 : : int ok = 0;
615 : :
616 : : /* Try one GSSAPI method at a time, rather than sending them all at
617 : : * once. */
618 : :
619 : : if (gss_supported == NULL)
620 : : gss_indicate_mechs(&min, &gss_supported);
621 : :
622 : : /* Check to see if the mechanism is usable before we offer it */
623 : : while (mech < gss_supported->count && !ok) {
624 : : /* My DER encoding requires length<128 */
625 : : if (gss_supported->elements[mech].length < 128 &&
626 : : ssh_gssapi_check_mechanism(&gssctxt,
627 : : &gss_supported->elements[mech], authctxt->host)) {
628 : : ok = 1; /* Mechanism works */
629 : : } else {
630 : : mech++;
631 : : }
632 : : }
633 : :
634 : : if (!ok)
635 : : return 0;
636 : :
637 : : authctxt->methoddata=(void *)gssctxt;
638 : :
639 : : packet_start(SSH2_MSG_USERAUTH_REQUEST);
640 : : packet_put_cstring(authctxt->server_user);
641 : : packet_put_cstring(authctxt->service);
642 : : packet_put_cstring(authctxt->method->name);
643 : :
644 : : packet_put_int(1);
645 : :
646 : : packet_put_int((gss_supported->elements[mech].length) + 2);
647 : : packet_put_char(SSH_GSS_OIDTYPE);
648 : : packet_put_char(gss_supported->elements[mech].length);
649 : : packet_put_raw(gss_supported->elements[mech].elements,
650 : : gss_supported->elements[mech].length);
651 : :
652 : : packet_send();
653 : :
654 : : dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
655 : : dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
656 : : dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
657 : : dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
658 : :
659 : : mech++; /* Move along to next candidate */
660 : :
661 : : return 1;
662 : : }
663 : :
664 : : static OM_uint32
665 : : process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
666 : : {
667 : : Authctxt *authctxt = ctxt;
668 : : Gssctxt *gssctxt = authctxt->methoddata;
669 : : gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
670 : : gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
671 : : gss_buffer_desc gssbuf;
672 : : OM_uint32 status, ms, flags;
673 : : Buffer b;
674 : :
675 : : status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
676 : : recv_tok, &send_tok, &flags);
677 : :
678 : : if (send_tok.length > 0) {
679 : : if (GSS_ERROR(status))
680 : : packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
681 : : else
682 : : packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
683 : :
684 : : packet_put_string(send_tok.value, send_tok.length);
685 : : packet_send();
686 : : gss_release_buffer(&ms, &send_tok);
687 : : }
688 : :
689 : : if (status == GSS_S_COMPLETE) {
690 : : /* send either complete or MIC, depending on mechanism */
691 : : if (!(flags & GSS_C_INTEG_FLAG)) {
692 : : packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
693 : : packet_send();
694 : : } else {
695 : : ssh_gssapi_buildmic(&b, authctxt->server_user,
696 : : authctxt->service, "gssapi-with-mic");
697 : :
698 : : gssbuf.value = buffer_ptr(&b);
699 : : gssbuf.length = buffer_len(&b);
700 : :
701 : : status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
702 : :
703 : : if (!GSS_ERROR(status)) {
704 : : packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
705 : : packet_put_string(mic.value, mic.length);
706 : :
707 : : packet_send();
708 : : }
709 : :
710 : : buffer_free(&b);
711 : : gss_release_buffer(&ms, &mic);
712 : : }
713 : : }
714 : :
715 : : return status;
716 : : }
717 : :
718 : : /* ARGSUSED */
719 : : void
720 : : input_gssapi_response(int type, u_int32_t plen, void *ctxt)
721 : : {
722 : : Authctxt *authctxt = ctxt;
723 : : Gssctxt *gssctxt;
724 : : int oidlen;
725 : : char *oidv;
726 : :
727 : : if (authctxt == NULL)
728 : : fatal("input_gssapi_response: no authentication context");
729 : : gssctxt = authctxt->methoddata;
730 : :
731 : : /* Setup our OID */
732 : : oidv = packet_get_string(&oidlen);
733 : :
734 : : if (oidlen <= 2 ||
735 : : oidv[0] != SSH_GSS_OIDTYPE ||
736 : : oidv[1] != oidlen - 2) {
737 : : free(oidv);
738 : : debug("Badly encoded mechanism OID received");
739 : : userauth(authctxt, NULL);
740 : : return;
741 : : }
742 : :
743 : : if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
744 : : fatal("Server returned different OID than expected");
745 : :
746 : : packet_check_eom();
747 : :
748 : : free(oidv);
749 : :
750 : : if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
751 : : /* Start again with next method on list */
752 : : debug("Trying to start again");
753 : : userauth(authctxt, NULL);
754 : : return;
755 : : }
756 : : }
757 : :
758 : : /* ARGSUSED */
759 : : void
760 : : input_gssapi_token(int type, u_int32_t plen, void *ctxt)
761 : : {
762 : : Authctxt *authctxt = ctxt;
763 : : gss_buffer_desc recv_tok;
764 : : OM_uint32 status;
765 : : u_int slen;
766 : :
767 : : if (authctxt == NULL)
768 : : fatal("input_gssapi_response: no authentication context");
769 : :
770 : : recv_tok.value = packet_get_string(&slen);
771 : : recv_tok.length = slen; /* safe typecast */
772 : :
773 : : packet_check_eom();
774 : :
775 : : status = process_gssapi_token(ctxt, &recv_tok);
776 : :
777 : : free(recv_tok.value);
778 : :
779 : : if (GSS_ERROR(status)) {
780 : : /* Start again with the next method in the list */
781 : : userauth(authctxt, NULL);
782 : : return;
783 : : }
784 : : }
785 : :
786 : : /* ARGSUSED */
787 : : void
788 : : input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
789 : : {
790 : : Authctxt *authctxt = ctxt;
791 : : Gssctxt *gssctxt;
792 : : gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
793 : : gss_buffer_desc recv_tok;
794 : : OM_uint32 ms;
795 : : u_int len;
796 : :
797 : : if (authctxt == NULL)
798 : : fatal("input_gssapi_response: no authentication context");
799 : : gssctxt = authctxt->methoddata;
800 : :
801 : : recv_tok.value = packet_get_string(&len);
802 : : recv_tok.length = len;
803 : :
804 : : packet_check_eom();
805 : :
806 : : /* Stick it into GSSAPI and see what it says */
807 : : (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
808 : : &recv_tok, &send_tok, NULL);
809 : :
810 : : free(recv_tok.value);
811 : : gss_release_buffer(&ms, &send_tok);
812 : :
813 : : /* Server will be returning a failed packet after this one */
814 : : }
815 : :
816 : : /* ARGSUSED */
817 : : void
818 : : input_gssapi_error(int type, u_int32_t plen, void *ctxt)
819 : : {
820 : : char *msg;
821 : : char *lang;
822 : :
823 : : /* maj */(void)packet_get_int();
824 : : /* min */(void)packet_get_int();
825 : : msg=packet_get_string(NULL);
826 : : lang=packet_get_string(NULL);
827 : :
828 : : packet_check_eom();
829 : :
830 : : debug("Server GSSAPI Error:\n%s", msg);
831 : : free(msg);
832 : : free(lang);
833 : : }
834 : : #endif /* GSSAPI */
835 : :
836 : : int
837 : 688 : userauth_none(Authctxt *authctxt)
838 : : {
839 : : /* initial userauth request */
840 : 688 : packet_start(SSH2_MSG_USERAUTH_REQUEST);
841 : 688 : packet_put_cstring(authctxt->server_user);
842 : 688 : packet_put_cstring(authctxt->service);
843 : 688 : packet_put_cstring(authctxt->method->name);
844 : 688 : packet_send();
845 : 688 : return 1;
846 : : }
847 : :
848 : : int
849 : 0 : userauth_passwd(Authctxt *authctxt)
850 : : {
851 : : static int attempt = 0;
852 : : char prompt[150];
853 : : char *password;
854 [ # # ]: 0 : const char *host = options.host_key_alias ? options.host_key_alias :
855 : : authctxt->host;
856 : :
857 [ # # ]: 0 : if (attempt++ >= options.number_of_password_prompts)
858 : : return 0;
859 : :
860 [ # # ]: 0 : if (attempt != 1)
861 : 0 : error("Permission denied, please try again.");
862 : :
863 : 0 : snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
864 : : authctxt->server_user, host);
865 : 0 : password = read_passphrase(prompt, 0);
866 : 0 : packet_start(SSH2_MSG_USERAUTH_REQUEST);
867 : 0 : packet_put_cstring(authctxt->server_user);
868 : 0 : packet_put_cstring(authctxt->service);
869 : 0 : packet_put_cstring(authctxt->method->name);
870 : 0 : packet_put_char(0);
871 : 0 : packet_put_cstring(password);
872 : 0 : explicit_bzero(password, strlen(password));
873 : 0 : free(password);
874 : 0 : packet_add_padding(64);
875 : 0 : packet_send();
876 : :
877 : 0 : dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
878 : : &input_userauth_passwd_changereq);
879 : :
880 : 0 : return 1;
881 : : }
882 : :
883 : : /*
884 : : * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
885 : : */
886 : : /* ARGSUSED */
887 : : void
888 : 0 : input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
889 : : {
890 : 0 : Authctxt *authctxt = ctxt;
891 : 0 : char *info, *lang, *password = NULL, *retype = NULL;
892 : : char prompt[150];
893 [ # # ]: 0 : const char *host = options.host_key_alias ? options.host_key_alias :
894 : : authctxt->host;
895 : :
896 : 0 : debug2("input_userauth_passwd_changereq");
897 : :
898 [ # # ]: 0 : if (authctxt == NULL)
899 : 0 : fatal("input_userauth_passwd_changereq: "
900 : : "no authentication context");
901 : :
902 : 0 : info = packet_get_string(NULL);
903 : 0 : lang = packet_get_string(NULL);
904 [ # # ]: 0 : if (strlen(info) > 0)
905 : 0 : logit("%s", info);
906 : 0 : free(info);
907 : 0 : free(lang);
908 : 0 : packet_start(SSH2_MSG_USERAUTH_REQUEST);
909 : 0 : packet_put_cstring(authctxt->server_user);
910 : 0 : packet_put_cstring(authctxt->service);
911 : 0 : packet_put_cstring(authctxt->method->name);
912 : 0 : packet_put_char(1); /* additional info */
913 : 0 : snprintf(prompt, sizeof(prompt),
914 : : "Enter %.30s@%.128s's old password: ",
915 : : authctxt->server_user, host);
916 : 0 : password = read_passphrase(prompt, 0);
917 : 0 : packet_put_cstring(password);
918 : 0 : explicit_bzero(password, strlen(password));
919 : 0 : free(password);
920 : 0 : password = NULL;
921 [ # # ]: 0 : while (password == NULL) {
922 : 0 : snprintf(prompt, sizeof(prompt),
923 : : "Enter %.30s@%.128s's new password: ",
924 : : authctxt->server_user, host);
925 : 0 : password = read_passphrase(prompt, RP_ALLOW_EOF);
926 [ # # ]: 0 : if (password == NULL) {
927 : : /* bail out */
928 : 0 : return;
929 : : }
930 : 0 : snprintf(prompt, sizeof(prompt),
931 : : "Retype %.30s@%.128s's new password: ",
932 : : authctxt->server_user, host);
933 : 0 : retype = read_passphrase(prompt, 0);
934 [ # # ]: 0 : if (strcmp(password, retype) != 0) {
935 : 0 : explicit_bzero(password, strlen(password));
936 : 0 : free(password);
937 : 0 : logit("Mismatch; try again, EOF to quit.");
938 : 0 : password = NULL;
939 : : }
940 : 0 : explicit_bzero(retype, strlen(retype));
941 : 0 : free(retype);
942 : : }
943 : 0 : packet_put_cstring(password);
944 : 0 : explicit_bzero(password, strlen(password));
945 : 0 : free(password);
946 : 0 : packet_add_padding(64);
947 : 0 : packet_send();
948 : :
949 : 0 : dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
950 : : &input_userauth_passwd_changereq);
951 : : }
952 : :
953 : : static int
954 : 688 : identity_sign(Identity *id, u_char **sigp, u_int *lenp,
955 : : u_char *data, u_int datalen)
956 : : {
957 : : Key *prv;
958 : : int ret;
959 : :
960 : : /* the agent supports this key */
961 [ + + ]: 688 : if (id->ac)
962 : 4 : return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
963 : : data, datalen));
964 : : /*
965 : : * we have already loaded the private key or
966 : : * the private key is stored in external hardware
967 : : */
968 [ + - ][ - + ]: 684 : if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
969 : 0 : return (key_sign(id->key, sigp, lenp, data, datalen));
970 : : /* load the private key from the file */
971 [ + - ]: 684 : if ((prv = load_identity_file(id->filename, id->userprovided)) == NULL)
972 : : return (-1);
973 : 684 : ret = key_sign(prv, sigp, lenp, data, datalen);
974 : 684 : key_free(prv);
975 : 684 : return (ret);
976 : : }
977 : :
978 : : static int
979 : 688 : sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
980 : : {
981 : : Buffer b;
982 : : u_char *blob, *signature;
983 : : u_int bloblen, slen;
984 : 688 : u_int skip = 0;
985 : 688 : int ret = -1;
986 : 688 : int have_sig = 1;
987 : : char *fp;
988 : :
989 : 688 : fp = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
990 : 688 : debug3("sign_and_send_pubkey: %s %s", key_type(id->key), fp);
991 : 688 : free(fp);
992 : :
993 [ - + ]: 688 : if (key_to_blob(id->key, &blob, &bloblen) == 0) {
994 : : /* we cannot handle this key */
995 : 0 : debug3("sign_and_send_pubkey: cannot handle key");
996 : 0 : return 0;
997 : : }
998 : : /* data to be signed */
999 : 688 : buffer_init(&b);
1000 [ - + ]: 688 : if (datafellows & SSH_OLD_SESSIONID) {
1001 : 0 : buffer_append(&b, session_id2, session_id2_len);
1002 : 0 : skip = session_id2_len;
1003 : : } else {
1004 : 688 : buffer_put_string(&b, session_id2, session_id2_len);
1005 : 688 : skip = buffer_len(&b);
1006 : : }
1007 : 688 : buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1008 : 688 : buffer_put_cstring(&b, authctxt->server_user);
1009 [ + - ]: 688 : buffer_put_cstring(&b,
1010 : 688 : datafellows & SSH_BUG_PKSERVICE ?
1011 : : "ssh-userauth" :
1012 : : authctxt->service);
1013 [ - + ]: 688 : if (datafellows & SSH_BUG_PKAUTH) {
1014 : 0 : buffer_put_char(&b, have_sig);
1015 : : } else {
1016 : 688 : buffer_put_cstring(&b, authctxt->method->name);
1017 : 688 : buffer_put_char(&b, have_sig);
1018 : 688 : buffer_put_cstring(&b, key_ssh_name(id->key));
1019 : : }
1020 : 688 : buffer_put_string(&b, blob, bloblen);
1021 : :
1022 : : /* generate signature */
1023 : 1376 : ret = identity_sign(id, &signature, &slen,
1024 : 688 : buffer_ptr(&b), buffer_len(&b));
1025 [ - + ]: 688 : if (ret == -1) {
1026 : 0 : free(blob);
1027 : 0 : buffer_free(&b);
1028 : 0 : return 0;
1029 : : }
1030 : : #ifdef DEBUG_PK
1031 : : buffer_dump(&b);
1032 : : #endif
1033 [ - + ]: 688 : if (datafellows & SSH_BUG_PKSERVICE) {
1034 : 0 : buffer_clear(&b);
1035 : 0 : buffer_append(&b, session_id2, session_id2_len);
1036 : 0 : skip = session_id2_len;
1037 : 0 : buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1038 : 0 : buffer_put_cstring(&b, authctxt->server_user);
1039 : 0 : buffer_put_cstring(&b, authctxt->service);
1040 : 0 : buffer_put_cstring(&b, authctxt->method->name);
1041 : 0 : buffer_put_char(&b, have_sig);
1042 [ # # ]: 0 : if (!(datafellows & SSH_BUG_PKAUTH))
1043 : 0 : buffer_put_cstring(&b, key_ssh_name(id->key));
1044 : 0 : buffer_put_string(&b, blob, bloblen);
1045 : : }
1046 : 688 : free(blob);
1047 : :
1048 : : /* append signature */
1049 : 688 : buffer_put_string(&b, signature, slen);
1050 : 688 : free(signature);
1051 : :
1052 : : /* skip session id and packet type */
1053 [ - + ]: 688 : if (buffer_len(&b) < skip + 1)
1054 : 0 : fatal("userauth_pubkey: internal error");
1055 : 688 : buffer_consume(&b, skip + 1);
1056 : :
1057 : : /* put remaining data from buffer into packet */
1058 : 688 : packet_start(SSH2_MSG_USERAUTH_REQUEST);
1059 : 688 : packet_put_raw(buffer_ptr(&b), buffer_len(&b));
1060 : 688 : buffer_free(&b);
1061 : 688 : packet_send();
1062 : :
1063 : 688 : return 1;
1064 : : }
1065 : :
1066 : : static int
1067 : 1668 : send_pubkey_test(Authctxt *authctxt, Identity *id)
1068 : : {
1069 : : u_char *blob;
1070 : 834 : u_int bloblen, have_sig = 0;
1071 : :
1072 : 834 : debug3("send_pubkey_test");
1073 : :
1074 [ - + ]: 834 : if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1075 : : /* we cannot handle this key */
1076 : 0 : debug3("send_pubkey_test: cannot handle key");
1077 : : return 0;
1078 : : }
1079 : : /* register callback for USERAUTH_PK_OK message */
1080 : 834 : dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1081 : :
1082 : 834 : packet_start(SSH2_MSG_USERAUTH_REQUEST);
1083 : 834 : packet_put_cstring(authctxt->server_user);
1084 : 834 : packet_put_cstring(authctxt->service);
1085 : 834 : packet_put_cstring(authctxt->method->name);
1086 : 834 : packet_put_char(have_sig);
1087 [ + - ]: 834 : if (!(datafellows & SSH_BUG_PKAUTH))
1088 : 834 : packet_put_cstring(key_ssh_name(id->key));
1089 : 834 : packet_put_string(blob, bloblen);
1090 : 834 : free(blob);
1091 : 834 : packet_send();
1092 : : return 1;
1093 : : }
1094 : :
1095 : : static Key *
1096 : 684 : load_identity_file(char *filename, int userprovided)
1097 : : {
1098 : : Key *private;
1099 : : char prompt[300], *passphrase;
1100 : 684 : int perm_ok = 0, quit, i;
1101 : : struct stat st;
1102 : :
1103 [ - + ]: 684 : if (stat(filename, &st) < 0) {
1104 [ # # ]: 0 : (userprovided ? logit : debug3)("no such identity: %s: %s",
1105 : 0 : filename, strerror(errno));
1106 : 0 : return NULL;
1107 : : }
1108 : 684 : private = key_load_private_type(KEY_UNSPEC, filename, "", NULL, &perm_ok);
1109 [ - + ]: 684 : if (!perm_ok) {
1110 [ # # ]: 0 : if (private != NULL)
1111 : 0 : key_free(private);
1112 : : return NULL;
1113 : : }
1114 [ - + ]: 684 : if (private == NULL) {
1115 [ # # ]: 0 : if (options.batch_mode)
1116 : : return NULL;
1117 : : snprintf(prompt, sizeof prompt,
1118 : : "Enter passphrase for key '%.100s': ", filename);
1119 [ # # ]: 0 : for (i = 0; i < options.number_of_password_prompts; i++) {
1120 : 0 : passphrase = read_passphrase(prompt, 0);
1121 [ # # ]: 0 : if (strcmp(passphrase, "") != 0) {
1122 : 0 : private = key_load_private_type(KEY_UNSPEC,
1123 : : filename, passphrase, NULL, NULL);
1124 : 0 : quit = 0;
1125 : : } else {
1126 : 0 : debug2("no passphrase given, try next key");
1127 : 0 : quit = 1;
1128 : : }
1129 : 0 : explicit_bzero(passphrase, strlen(passphrase));
1130 : 0 : free(passphrase);
1131 [ # # ]: 0 : if (private != NULL || quit)
1132 : : break;
1133 : 0 : debug2("bad passphrase given, try again...");
1134 : : }
1135 : : }
1136 : 684 : return private;
1137 : : }
1138 : :
1139 : : /*
1140 : : * try keys in the following order:
1141 : : * 1. agent keys that are found in the config file
1142 : : * 2. other agent keys
1143 : : * 3. keys that are only listed in the config file
1144 : : */
1145 : : static void
1146 : 688 : pubkey_prepare(Authctxt *authctxt)
1147 : : {
1148 : : Identity *id, *id2, *tmp;
1149 : : Idlist agent, files, *preferred;
1150 : : Key *key;
1151 : : AuthenticationConnection *ac;
1152 : : char *comment;
1153 : : int i, found;
1154 : :
1155 : 688 : TAILQ_INIT(&agent); /* keys from the agent */
1156 : 688 : TAILQ_INIT(&files); /* keys from the config file */
1157 : 688 : preferred = &authctxt->keys;
1158 : 688 : TAILQ_INIT(preferred); /* preferred order of keys */
1159 : :
1160 : : /* list of keys stored in the filesystem and PKCS#11 */
1161 [ + + ]: 3690 : for (i = 0; i < options.num_identity_files; i++) {
1162 : 3002 : key = options.identity_keys[i];
1163 [ + - ][ + + ]: 3002 : if (key && key->type == KEY_RSA1)
1164 : 1334 : continue;
1165 [ + - ][ + + ]: 1668 : if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
[ - + ]
1166 : 0 : continue;
1167 : 1668 : options.identity_keys[i] = NULL;
1168 : 1668 : id = xcalloc(1, sizeof(*id));
1169 : 1668 : id->key = key;
1170 : 1668 : id->filename = xstrdup(options.identity_files[i]);
1171 : 1668 : id->userprovided = options.identity_file_userprovided[i];
1172 : 1668 : TAILQ_INSERT_TAIL(&files, id, next);
1173 : : }
1174 : : /* Prefer PKCS11 keys that are explicitly listed */
1175 [ + + ]: 2356 : TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
1176 [ + - ][ + - ]: 1668 : if (id->key == NULL || (id->key->flags & KEY_FLAG_EXT) == 0)
1177 : 1668 : continue;
1178 : 0 : found = 0;
1179 [ # # ]: 0 : TAILQ_FOREACH(id2, &files, next) {
1180 [ # # ][ # # ]: 0 : if (id2->key == NULL ||
1181 : 0 : (id2->key->flags & KEY_FLAG_EXT) != 0)
1182 : 0 : continue;
1183 [ # # ]: 0 : if (key_equal(id->key, id2->key)) {
1184 [ # # ]: 0 : TAILQ_REMOVE(&files, id, next);
1185 : 0 : TAILQ_INSERT_TAIL(preferred, id, next);
1186 : 0 : found = 1;
1187 : 0 : break;
1188 : : }
1189 : : }
1190 : : /* If IdentitiesOnly set and key not found then don't use it */
1191 [ # # ][ # # ]: 0 : if (!found && options.identities_only) {
1192 [ # # ]: 0 : TAILQ_REMOVE(&files, id, next);
1193 : 0 : explicit_bzero(id, sizeof(*id));
1194 : 0 : free(id);
1195 : : }
1196 : : }
1197 : : /* list of keys supported by the agent */
1198 [ + + ]: 688 : if ((ac = ssh_get_authentication_connection())) {
1199 [ + + ]: 12 : for (key = ssh_get_first_identity(ac, &comment, 2);
1200 : : key != NULL;
1201 : 8 : key = ssh_get_next_identity(ac, &comment, 2)) {
1202 : 8 : found = 0;
1203 [ + + ]: 24 : TAILQ_FOREACH(id, &files, next) {
1204 : : /* agent keys from the config file are preferred */
1205 [ - + ]: 16 : if (key_equal(key, id->key)) {
1206 : 0 : key_free(key);
1207 : 0 : free(comment);
1208 [ # # ]: 0 : TAILQ_REMOVE(&files, id, next);
1209 : 0 : TAILQ_INSERT_TAIL(preferred, id, next);
1210 : 0 : id->ac = ac;
1211 : 0 : found = 1;
1212 : 0 : break;
1213 : : }
1214 : : }
1215 [ + - ][ + - ]: 8 : if (!found && !options.identities_only) {
1216 : 8 : id = xcalloc(1, sizeof(*id));
1217 : 8 : id->key = key;
1218 : 8 : id->filename = comment;
1219 : 8 : id->ac = ac;
1220 : 8 : TAILQ_INSERT_TAIL(&agent, id, next);
1221 : : }
1222 : : }
1223 : : /* append remaining agent keys */
1224 [ + + ]: 12 : for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
1225 [ + + ]: 8 : TAILQ_REMOVE(&agent, id, next);
1226 : 8 : TAILQ_INSERT_TAIL(preferred, id, next);
1227 : : }
1228 : 4 : authctxt->agent = ac;
1229 : : }
1230 : : /* append remaining keys from the config file */
1231 [ + + ]: 2356 : for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
1232 [ + + ]: 1668 : TAILQ_REMOVE(&files, id, next);
1233 : 1668 : TAILQ_INSERT_TAIL(preferred, id, next);
1234 : : }
1235 [ + + ]: 2364 : TAILQ_FOREACH(id, preferred, next) {
1236 [ + + ]: 1676 : debug2("key: %s (%p),%s", id->filename, id->key,
1237 : 1676 : id->userprovided ? " explicit" : "");
1238 : : }
1239 : 688 : }
1240 : :
1241 : : static void
1242 : 688 : pubkey_cleanup(Authctxt *authctxt)
1243 : : {
1244 : : Identity *id;
1245 : :
1246 [ + + ]: 688 : if (authctxt->agent != NULL)
1247 : 4 : ssh_close_authentication_connection(authctxt->agent);
1248 [ + + ]: 2364 : for (id = TAILQ_FIRST(&authctxt->keys); id;
1249 : 1676 : id = TAILQ_FIRST(&authctxt->keys)) {
1250 [ + + ]: 1676 : TAILQ_REMOVE(&authctxt->keys, id, next);
1251 [ + - ]: 1676 : if (id->key)
1252 : 1676 : key_free(id->key);
1253 : 1676 : free(id->filename);
1254 : 1676 : free(id);
1255 : : }
1256 : 688 : }
1257 : :
1258 : : int
1259 : 834 : userauth_pubkey(Authctxt *authctxt)
1260 : : {
1261 : : Identity *id;
1262 : 834 : int sent = 0;
1263 : :
1264 [ + - ]: 834 : while ((id = TAILQ_FIRST(&authctxt->keys))) {
1265 [ + - ]: 834 : if (id->tried++)
1266 : : return (0);
1267 : : /* move key to the end of the queue */
1268 [ + - ]: 834 : TAILQ_REMOVE(&authctxt->keys, id, next);
1269 : 834 : TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
1270 : : /*
1271 : : * send a test message if we have the public key. for
1272 : : * encrypted keys we cannot do this and have to load the
1273 : : * private key instead
1274 : : */
1275 [ + - ]: 834 : if (id->key != NULL) {
1276 [ + + ][ - + ]: 834 : if (key_type_plain(id->key->type) == KEY_RSA &&
1277 : 623 : (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1278 : 0 : debug("Skipped %s key %s for RSA/MD5 server",
1279 : 0 : key_type(id->key), id->filename);
1280 [ + - ]: 834 : } else if (id->key->type != KEY_RSA1) {
1281 : 834 : debug("Offering %s public key: %s",
1282 : : key_type(id->key), id->filename);
1283 : 834 : sent = send_pubkey_test(authctxt, id);
1284 : : }
1285 : : } else {
1286 : 0 : debug("Trying private key: %s", id->filename);
1287 : 0 : id->key = load_identity_file(id->filename,
1288 : : id->userprovided);
1289 [ # # ]: 0 : if (id->key != NULL) {
1290 : 0 : id->isprivate = 1;
1291 [ # # ][ # # ]: 0 : if (key_type_plain(id->key->type) == KEY_RSA &&
1292 : 0 : (datafellows & SSH_BUG_RSASIGMD5) != 0) {
1293 : 0 : debug("Skipped %s key %s for RSA/MD5 "
1294 : 0 : "server", key_type(id->key),
1295 : : id->filename);
1296 : : } else {
1297 : 0 : sent = sign_and_send_pubkey(
1298 : : authctxt, id);
1299 : : }
1300 : 0 : key_free(id->key);
1301 : 0 : id->key = NULL;
1302 : : }
1303 : : }
1304 [ - + ]: 834 : if (sent)
1305 : : return (sent);
1306 : : }
1307 : : return (0);
1308 : : }
1309 : :
1310 : : /*
1311 : : * Send userauth request message specifying keyboard-interactive method.
1312 : : */
1313 : : int
1314 : 0 : userauth_kbdint(Authctxt *authctxt)
1315 : : {
1316 : : static int attempt = 0;
1317 : :
1318 [ # # ]: 0 : if (attempt++ >= options.number_of_password_prompts)
1319 : : return 0;
1320 : : /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1321 [ # # ][ # # ]: 0 : if (attempt > 1 && !authctxt->info_req_seen) {
1322 : 0 : debug3("userauth_kbdint: disable: no info_req_seen");
1323 : 0 : dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1324 : 0 : return 0;
1325 : : }
1326 : :
1327 : 0 : debug2("userauth_kbdint");
1328 : 0 : packet_start(SSH2_MSG_USERAUTH_REQUEST);
1329 : 0 : packet_put_cstring(authctxt->server_user);
1330 : 0 : packet_put_cstring(authctxt->service);
1331 : 0 : packet_put_cstring(authctxt->method->name);
1332 : 0 : packet_put_cstring(""); /* lang */
1333 [ # # ]: 0 : packet_put_cstring(options.kbd_interactive_devices ?
1334 : : options.kbd_interactive_devices : "");
1335 : 0 : packet_send();
1336 : :
1337 : 0 : dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1338 : 0 : return 1;
1339 : : }
1340 : :
1341 : : /*
1342 : : * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1343 : : */
1344 : : void
1345 : 0 : input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1346 : : {
1347 : 0 : Authctxt *authctxt = ctxt;
1348 : : char *name, *inst, *lang, *prompt, *response;
1349 : : u_int num_prompts, i;
1350 : 0 : int echo = 0;
1351 : :
1352 : 0 : debug2("input_userauth_info_req");
1353 : :
1354 [ # # ]: 0 : if (authctxt == NULL)
1355 : 0 : fatal("input_userauth_info_req: no authentication context");
1356 : :
1357 : 0 : authctxt->info_req_seen = 1;
1358 : :
1359 : 0 : name = packet_get_string(NULL);
1360 : 0 : inst = packet_get_string(NULL);
1361 : 0 : lang = packet_get_string(NULL);
1362 [ # # ]: 0 : if (strlen(name) > 0)
1363 : 0 : logit("%s", name);
1364 [ # # ]: 0 : if (strlen(inst) > 0)
1365 : 0 : logit("%s", inst);
1366 : 0 : free(name);
1367 : 0 : free(inst);
1368 : 0 : free(lang);
1369 : :
1370 : 0 : num_prompts = packet_get_int();
1371 : : /*
1372 : : * Begin to build info response packet based on prompts requested.
1373 : : * We commit to providing the correct number of responses, so if
1374 : : * further on we run into a problem that prevents this, we have to
1375 : : * be sure and clean this up and send a correct error response.
1376 : : */
1377 : 0 : packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
1378 : 0 : packet_put_int(num_prompts);
1379 : :
1380 : 0 : debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1381 [ # # ]: 0 : for (i = 0; i < num_prompts; i++) {
1382 : 0 : prompt = packet_get_string(NULL);
1383 : 0 : echo = packet_get_char();
1384 : :
1385 : 0 : response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1386 : :
1387 : 0 : packet_put_cstring(response);
1388 : 0 : explicit_bzero(response, strlen(response));
1389 : 0 : free(response);
1390 : 0 : free(prompt);
1391 : : }
1392 [ # # ]: 0 : packet_check_eom(); /* done with parsing incoming message. */
1393 : :
1394 : 0 : packet_add_padding(64);
1395 : 0 : packet_send();
1396 : 0 : }
1397 : :
1398 : : static int
1399 : 0 : ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1400 : : u_char *data, u_int datalen)
1401 : : {
1402 : : Buffer b;
1403 : : struct stat st;
1404 : : pid_t pid;
1405 : 0 : int to[2], from[2], status, version = 2;
1406 : :
1407 : 0 : debug2("ssh_keysign called");
1408 : :
1409 [ # # ]: 0 : if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
1410 : 0 : error("ssh_keysign: not installed: %s", strerror(errno));
1411 : : return -1;
1412 : : }
1413 [ # # ]: 0 : if (fflush(stdout) != 0)
1414 : 0 : error("ssh_keysign: fflush: %s", strerror(errno));
1415 [ # # ]: 0 : if (pipe(to) < 0) {
1416 : 0 : error("ssh_keysign: pipe: %s", strerror(errno));
1417 : : return -1;
1418 : : }
1419 [ # # ]: 0 : if (pipe(from) < 0) {
1420 : 0 : error("ssh_keysign: pipe: %s", strerror(errno));
1421 : : return -1;
1422 : : }
1423 [ # # ]: 0 : if ((pid = fork()) < 0) {
1424 : 0 : error("ssh_keysign: fork: %s", strerror(errno));
1425 : : return -1;
1426 : : }
1427 [ # # ]: 0 : if (pid == 0) {
1428 : : /* keep the socket on exec */
1429 : 0 : fcntl(packet_get_connection_in(), F_SETFD, 0);
1430 : 0 : permanently_drop_suid(getuid());
1431 : 0 : close(from[0]);
1432 [ # # ]: 0 : if (dup2(from[1], STDOUT_FILENO) < 0)
1433 : 0 : fatal("ssh_keysign: dup2: %s", strerror(errno));
1434 : 0 : close(to[1]);
1435 [ # # ]: 0 : if (dup2(to[0], STDIN_FILENO) < 0)
1436 : 0 : fatal("ssh_keysign: dup2: %s", strerror(errno));
1437 : 0 : close(from[1]);
1438 : 0 : close(to[0]);
1439 : 0 : execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1440 : 0 : fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
1441 : 0 : strerror(errno));
1442 : : }
1443 : 0 : close(from[1]);
1444 : 0 : close(to[0]);
1445 : :
1446 : 0 : buffer_init(&b);
1447 : 0 : buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1448 : 0 : buffer_put_string(&b, data, datalen);
1449 [ # # ]: 0 : if (ssh_msg_send(to[1], version, &b) == -1)
1450 : 0 : fatal("ssh_keysign: couldn't send request");
1451 : :
1452 [ # # ]: 0 : if (ssh_msg_recv(from[0], &b) < 0) {
1453 : 0 : error("ssh_keysign: no reply");
1454 : 0 : buffer_free(&b);
1455 : : return -1;
1456 : : }
1457 : 0 : close(from[0]);
1458 : 0 : close(to[1]);
1459 : :
1460 [ # # ]: 0 : while (waitpid(pid, &status, 0) < 0)
1461 [ # # ]: 0 : if (errno != EINTR)
1462 : : break;
1463 : :
1464 [ # # ]: 0 : if (buffer_get_char(&b) != version) {
1465 : 0 : error("ssh_keysign: bad version");
1466 : 0 : buffer_free(&b);
1467 : : return -1;
1468 : : }
1469 : 0 : *sigp = buffer_get_string(&b, lenp);
1470 : 0 : buffer_free(&b);
1471 : :
1472 : : return 0;
1473 : : }
1474 : :
1475 : : int
1476 : 0 : userauth_hostbased(Authctxt *authctxt)
1477 : : {
1478 : 0 : Key *private = NULL;
1479 : 0 : Sensitive *sensitive = authctxt->sensitive;
1480 : : Buffer b;
1481 : : u_char *signature, *blob;
1482 : : char *chost, *pkalg, *p;
1483 : : const char *service;
1484 : : u_int blen, slen;
1485 : 0 : int ok, i, found = 0;
1486 : :
1487 : : /* check for a useful key */
1488 [ # # ]: 0 : for (i = 0; i < sensitive->nkeys; i++) {
1489 : 0 : private = sensitive->keys[i];
1490 [ # # ][ # # ]: 0 : if (private && private->type != KEY_RSA1) {
1491 : 0 : found = 1;
1492 : : /* we take and free the key */
1493 : 0 : sensitive->keys[i] = NULL;
1494 : 0 : break;
1495 : : }
1496 : : }
1497 [ # # ]: 0 : if (!found) {
1498 : 0 : debug("No more client hostkeys for hostbased authentication.");
1499 : 0 : return 0;
1500 : : }
1501 [ # # ]: 0 : if (key_to_blob(private, &blob, &blen) == 0) {
1502 : 0 : key_free(private);
1503 : 0 : return 0;
1504 : : }
1505 : : /* figure out a name for the client host */
1506 : 0 : p = get_local_name(packet_get_connection_in());
1507 [ # # ]: 0 : if (p == NULL) {
1508 : 0 : error("userauth_hostbased: cannot get local ipaddr/name");
1509 : 0 : key_free(private);
1510 : 0 : free(blob);
1511 : 0 : return 0;
1512 : : }
1513 : 0 : xasprintf(&chost, "%s.", p);
1514 : 0 : debug2("userauth_hostbased: chost %s", chost);
1515 : 0 : free(p);
1516 : :
1517 [ # # ]: 0 : service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
1518 : : authctxt->service;
1519 : 0 : pkalg = xstrdup(key_ssh_name(private));
1520 : 0 : buffer_init(&b);
1521 : : /* construct data */
1522 : 0 : buffer_put_string(&b, session_id2, session_id2_len);
1523 : 0 : buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1524 : 0 : buffer_put_cstring(&b, authctxt->server_user);
1525 : 0 : buffer_put_cstring(&b, service);
1526 : 0 : buffer_put_cstring(&b, authctxt->method->name);
1527 : 0 : buffer_put_cstring(&b, pkalg);
1528 : 0 : buffer_put_string(&b, blob, blen);
1529 : 0 : buffer_put_cstring(&b, chost);
1530 : 0 : buffer_put_cstring(&b, authctxt->local_user);
1531 : : #ifdef DEBUG_PK
1532 : : buffer_dump(&b);
1533 : : #endif
1534 [ # # ]: 0 : if (sensitive->external_keysign)
1535 : 0 : ok = ssh_keysign(private, &signature, &slen,
1536 : 0 : buffer_ptr(&b), buffer_len(&b));
1537 : : else
1538 : 0 : ok = key_sign(private, &signature, &slen,
1539 : 0 : buffer_ptr(&b), buffer_len(&b));
1540 : 0 : key_free(private);
1541 : 0 : buffer_free(&b);
1542 [ # # ]: 0 : if (ok != 0) {
1543 : 0 : error("key_sign failed");
1544 : 0 : free(chost);
1545 : 0 : free(pkalg);
1546 : 0 : free(blob);
1547 : 0 : return 0;
1548 : : }
1549 : 0 : packet_start(SSH2_MSG_USERAUTH_REQUEST);
1550 : 0 : packet_put_cstring(authctxt->server_user);
1551 : 0 : packet_put_cstring(authctxt->service);
1552 : 0 : packet_put_cstring(authctxt->method->name);
1553 : 0 : packet_put_cstring(pkalg);
1554 : 0 : packet_put_string(blob, blen);
1555 : 0 : packet_put_cstring(chost);
1556 : 0 : packet_put_cstring(authctxt->local_user);
1557 : 0 : packet_put_string(signature, slen);
1558 : 0 : explicit_bzero(signature, slen);
1559 : 0 : free(signature);
1560 : 0 : free(chost);
1561 : 0 : free(pkalg);
1562 : 0 : free(blob);
1563 : :
1564 : 0 : packet_send();
1565 : 0 : return 1;
1566 : : }
1567 : :
1568 : : /* find auth method */
1569 : :
1570 : : /*
1571 : : * given auth method name, if configurable options permit this method fill
1572 : : * in auth_ident field and return true, otherwise return false.
1573 : : */
1574 : : static int
1575 : : authmethod_is_enabled(Authmethod *method)
1576 : : {
1577 [ + - ][ + - ]: 4274 : if (method == NULL)
[ + - ]
1578 : : return 0;
1579 : : /* return false if options indicate this method is disabled */
1580 [ + + ][ + + ]: 4274 : if (method->enabled == NULL || *method->enabled == 0)
[ + - ][ + - ]
[ + - ][ + - ]
1581 : : return 0;
1582 : : /* return false if batch mode is enabled but method needs interactive mode */
1583 [ + + ][ - + ]: 2210 : if (method->batch_flag != NULL && *method->batch_flag != 0)
[ - + ][ # # ]
[ - + ][ # # ]
1584 : : return 0;
1585 : : return 1;
1586 : : }
1587 : :
1588 : : static Authmethod *
1589 : 1376 : authmethod_lookup(const char *name)
1590 : : {
1591 : 1376 : Authmethod *method = NULL;
1592 [ + - ]: 1376 : if (name != NULL)
1593 [ + - ]: 4816 : for (method = authmethods; method->name != NULL; method++)
1594 [ + + ]: 4816 : if (strcmp(name, method->name) == 0)
1595 : : return method;
1596 [ # # ]: 0 : debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
1597 : 0 : return NULL;
1598 : : }
1599 : :
1600 : : /* XXX internal state */
1601 : : static Authmethod *current = NULL;
1602 : : static char *supported = NULL;
1603 : : static char *preferred = NULL;
1604 : :
1605 : : /*
1606 : : * Given the authentication method list sent by the server, return the
1607 : : * next method we should try. If the server initially sends a nil list,
1608 : : * use a built-in default list.
1609 : : */
1610 : : static Authmethod *
1611 : 834 : authmethod_get(char *authlist)
1612 : : {
1613 : 834 : char *name = NULL;
1614 : : u_int next;
1615 : :
1616 : : /* Use a suitable default if we're passed a nil list. */
1617 [ + - ][ - + ]: 834 : if (authlist == NULL || strlen(authlist) == 0)
1618 : 0 : authlist = options.preferred_authentications;
1619 : :
1620 [ + + ][ - + ]: 834 : if (supported == NULL || strcmp(authlist, supported) != 0) {
1621 : 688 : debug3("start over, passed a different list %s", authlist);
1622 : 688 : free(supported);
1623 : 688 : supported = xstrdup(authlist);
1624 : 688 : preferred = options.preferred_authentications;
1625 : 688 : debug3("preferred %s", preferred);
1626 : 688 : current = NULL;
1627 [ - + ][ - + ]: 292 : } else if (current != NULL && authmethod_is_enabled(current))
1628 : : return current;
1629 : :
1630 : : for (;;) {
1631 [ - + ]: 688 : if ((name = match_list(preferred, supported, &next)) == NULL) {
1632 : 0 : debug("No more authentication methods to try.");
1633 : 0 : current = NULL;
1634 : 0 : return NULL;
1635 : : }
1636 : 688 : preferred += next;
1637 : 688 : debug3("authmethod_lookup %s", name);
1638 : 688 : debug3("remaining preferred: %s", preferred);
1639 [ + - ][ + - ]: 1376 : if ((current = authmethod_lookup(name)) != NULL &&
1640 : 688 : authmethod_is_enabled(current)) {
1641 : 688 : debug3("authmethod_is_enabled %s", name);
1642 : 688 : debug("Next authentication method: %s", name);
1643 : 688 : free(name);
1644 : 688 : return current;
1645 : : }
1646 : 0 : free(name);
1647 : 0 : }
1648 : : }
1649 : :
1650 : : static char *
1651 : 688 : authmethods_get(void)
1652 : : {
1653 : 688 : Authmethod *method = NULL;
1654 : : Buffer b;
1655 : : char *list;
1656 : :
1657 : 688 : buffer_init(&b);
1658 [ + + ]: 4128 : for (method = authmethods; method->name != NULL; method++) {
1659 [ + + ]: 3440 : if (authmethod_is_enabled(method)) {
1660 [ - + ]: 688 : if (buffer_len(&b) > 0)
1661 : 0 : buffer_append(&b, ",", 1);
1662 : 688 : buffer_append(&b, method->name, strlen(method->name));
1663 : : }
1664 : : }
1665 : 688 : buffer_append(&b, "\0", 1);
1666 : 688 : list = xstrdup(buffer_ptr(&b));
1667 : 688 : buffer_free(&b);
1668 : 688 : return list;
1669 : : }
1670 : :
|