gsasl  1.10.0
About: GNU SASL is an implementation of the Simple Authentication and Security Layer (SASL). Development version.
  Fossies Dox: gsasl-1.10.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

suggest.c
Go to the documentation of this file.
1 /* suggest.c --- Suggest client mechanism to use, from a set of mechanisms.
2  * Copyright (C) 2002-2021 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  **/
37 const char *
38 gsasl_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 }
#define NULL
Definition: stddef.in.h:72
void gsasl_finish(Gsasl_session *sctx)
Definition: xfinish.c:33
const char * GSASL_VALID_MECHANISM_CHARACTERS
Definition: init.c:51
int gsasl_client_start(Gsasl *ctx, const char *mech, Gsasl_session **sctx)
Definition: xstart.c:119
@ GSASL_OK
Definition: gsasl.h:171
const char * name
Definition: gsasl-mech.h:54
Definition: internal.h:41
size_t n_client_mechs
Definition: internal.h:42
Gsasl_mechanism * client_mechs
Definition: internal.h:43
const char * gsasl_client_suggest_mechanism(Gsasl *ctx, const char *mechlist)
Definition: suggest.c:38