gsasl  2.2.2
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-2025 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 along with GNU SASL Library; if not, see
18  * <https://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include <config.h>
23 #include "internal.h"
24 
25 /*
26  * _GSASL_VALID_MECHANISM_CHARACTERS:
27  *
28  * A zero-terminated character array, or string, with all ASCII
29  * characters that may be used within a SASL mechanism name.
30  **/
31 static const char *_GSASL_VALID_MECHANISM_CHARACTERS =
32  "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
33 
51 int
52 gsasl_mechanism_name_p (const char *mech)
53 {
54  size_t l;
55 
56  if (mech == NULL)
57  return 0;
58 
59  l = strlen (mech);
60 
62  return 0;
63 
65  return 0;
66 
67  while (*mech)
68  if (strchr (_GSASL_VALID_MECHANISM_CHARACTERS, *mech++) == NULL)
69  return 0;
70 
71  return 1;
72 }
73 
86 const char *
87 gsasl_client_suggest_mechanism (Gsasl *ctx, const char *mechlist)
88 {
89  size_t mechlist_len, target_mech, i;
90 
91  mechlist_len = mechlist ? strlen (mechlist) : 0;
92  target_mech = ctx->n_client_mechs; /* ~ no target */
93 
94  for (i = 0; i < mechlist_len;)
95  {
96  size_t len;
97 
98  len = strspn (mechlist + i, _GSASL_VALID_MECHANISM_CHARACTERS);
99  if (!len)
100  ++i;
101  else
102  {
103  size_t j;
104 
105  /* Assumption: the mechs array is sorted by preference
106  * from low security to high security. */
107  for (j = (target_mech < ctx->n_client_mechs ? target_mech + 1 : 0);
108  j < ctx->n_client_mechs; ++j)
109  {
110  if ((strlen (ctx->client_mechs[j].name) == len)
111  && (strncmp (ctx->client_mechs[j].name, mechlist + i,
112  len) == 0))
113  {
114  Gsasl_session *sctx;
115 
116  if (gsasl_client_start (ctx, ctx->client_mechs[j].name,
117  &sctx) == GSASL_OK)
118  {
119  gsasl_finish (sctx);
120  target_mech = j;
121  }
122 
123  break;
124  }
125  }
126  i += len + 1;
127  }
128  }
129 
130  return target_mech < ctx->n_client_mechs ?
131  ctx->client_mechs[target_mech].name : NULL;
132 }
@ GSASL_OK
Definition: gsasl.h:128
_GSASL_API int gsasl_client_start(Gsasl *ctx, const char *mech, Gsasl_session **sctx)
Definition: xstart.c:119
_GSASL_API void gsasl_finish(Gsasl_session *sctx)
Definition: xfinish.c:33
@ GSASL_MIN_MECHANISM_SIZE
Definition: gsasl.h:299
@ GSASL_MAX_MECHANISM_SIZE
Definition: gsasl.h:300
const char * name
Definition: gsasl-mech.h:172
Definition: internal.h:36
size_t n_client_mechs
Definition: internal.h:37
Gsasl_mechanism * client_mechs
Definition: internal.h:38
const char * gsasl_client_suggest_mechanism(Gsasl *ctx, const char *mechlist)
Definition: suggest.c:87
int gsasl_mechanism_name_p(const char *mech)
Definition: suggest.c:52