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

Loading...
Searching...
No Matches
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-2022 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_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 **/
32 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
33
34/**
35 * gsasl_mechanism_name_p:
36 * @mech: input variable with mechanism name string.
37 *
38 * Check if the mechanism name string @mech follows syntactical rules.
39 * It does not check that the name is registered with IANA. It does not
40 * check that the mechanism name is actually implemented and supported.
41 *
42 * SASL mechanisms are named by strings, from 1 to 20 characters in
43 * length, consisting of upper-case letters, digits, hyphens, and/or
44 * underscores.
45 *
46 * Returns: non-zero when mechanism name string @mech conforms to
47 * rules, zero when it does not meet the requirements.
48 *
49 * Since: 2.0.0
50 **/
51int
52gsasl_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
74/**
75 * gsasl_client_suggest_mechanism:
76 * @ctx: libgsasl handle.
77 * @mechlist: input character array with SASL mechanism names,
78 * separated by invalid characters (e.g. SPC).
79 *
80 * Given a list of mechanisms, suggest which to use.
81 *
82 * Return value: Returns name of "best" SASL mechanism supported by
83 * the libgsasl client which is present in the input string, or
84 * NULL if no supported mechanism is found.
85 **/
86const char *
87gsasl_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}
void gsasl_finish(Gsasl_session *sctx)
Definition: xfinish.c:33
int gsasl_client_start(Gsasl *ctx, const char *mech, Gsasl_session **sctx)
Definition: xstart.c:119
@ GSASL_OK
Definition: gsasl.h:129
@ 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: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:87
int gsasl_mechanism_name_p(const char *mech)
Definition: suggest.c:52
const char * _GSASL_VALID_MECHANISM_CHARACTERS
Definition: suggest.c:31