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