1/* suggest.c --- Suggest client mechanism to use, from a set of mechanisms.
2 * Copyright (C) 2002-2012 Simon Josefsson
3 *
4 * This file is part of GNU SASL Library.
5 *
6 * GNU SASL Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * GNU SASL Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License License along with GNU SASL Library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#include "internal.h"
24
25/**
26 * gsasl_client_suggest_mechanism:
27 * @ctx: libgsasl handle.
28 * @mechlist: input character array with SASL mechanism names,
29 * separated by invalid characters (e.g. SPC).
30 *
31 * Given a list of mechanisms, suggest which to use.
32 *
33 * Return value: Returns name of "best" SASL mechanism supported by
34 * the libgsasl client which is present in the input string, or
35 * NULL if no supported mechanism is found.
36 **/
37const char *
38gsasl_client_suggest_mechanism (Gsasl * ctx, const char *mechlist)
39{
40 size_t mechlist_len, target_mech, i;
41
42 mechlist_len = mechlist ? strlen (mechlist) : 0;
43 target_mech = ctx->n_client_mechs; /* ~ no target */
44
45 for (i = 0; i < mechlist_len;)
46 {
47 size_t len;
48
49 len = strspn (mechlist + i, GSASL_VALID_MECHANISM_CHARACTERS);
50 if (!len)
51 ++i;
52 else
53 {
54 size_t j;
55
56 /* Assumption: the mechs array is sorted by preference
57 * from low security to high security. */
58 for (j = (target_mech < ctx->n_client_mechs ? target_mech + 1 : 0);
59 j < ctx->n_client_mechs; ++j)
60 {
61 if (strncmp (ctx->client_mechs[j].name, mechlist + i, len) == 0)
62 {
63 Gsasl_session *sctx;
64
65 if (gsasl_client_start (ctx, ctx->client_mechs[j].name,
66 &sctx) == GSASL_OK)
67 {
68 gsasl_finish (sctx);
69 target_mech = j;
70 }
71
72 break;
73 }
74 }
75 i += len + 1;
76 }
77 }
78
79 return target_mech < ctx->n_client_mechs ?
80 ctx->client_mechs[target_mech].name : NULL;
81}
82