gsasl  2.2.2
xstart.c
Go to the documentation of this file.
1 /* xstart.c --- Start libgsasl session.
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 static Gsasl_mechanism *
26 find_mechanism (const char *mech, size_t n_mechs, Gsasl_mechanism *mechs)
27 {
28  size_t i;
29 
30  if (mech == NULL)
31  return NULL;
32 
33  for (i = 0; i < n_mechs; i++)
34  if (strcmp (mech, mechs[i].name) == 0)
35  return &mechs[i];
36 
37  return NULL;
38 }
39 
40 static int
41 setup (Gsasl *ctx,
42  const char *mech,
43  Gsasl_session *sctx,
44  size_t n_mechs, Gsasl_mechanism *mechs, int clientp)
45 {
46  Gsasl_mechanism *mechptr = NULL;
47  int res;
48 
49  mechptr = find_mechanism (mech, n_mechs, mechs);
50  if (mechptr == NULL)
52 
53  sctx->ctx = ctx;
54  sctx->mech = mechptr;
55  sctx->clientp = clientp;
56 
57  if (clientp)
58  {
59  if (sctx->mech->client.start)
60  res = sctx->mech->client.start (sctx, &sctx->mech_data);
61  else if (!sctx->mech->client.step)
63  else
64  res = GSASL_OK;
65  }
66  else
67  {
68  if (sctx->mech->server.start)
69  res = sctx->mech->server.start (sctx, &sctx->mech_data);
70  else if (!sctx->mech->server.step)
72  else
73  res = GSASL_OK;
74  }
75  if (res != GSASL_OK)
76  return res;
77 
78  return GSASL_OK;
79 }
80 
81 static int
82 start (Gsasl *ctx,
83  const char *mech,
84  Gsasl_session **sctx,
85  size_t n_mechs, Gsasl_mechanism *mechs, int clientp)
86 {
87  Gsasl_session *out;
88  int res;
89 
90  out = calloc (1, sizeof (*out));
91  if (out == NULL)
92  return GSASL_MALLOC_ERROR;
93 
94  res = setup (ctx, mech, out, n_mechs, mechs, clientp);
95  if (res != GSASL_OK)
96  {
97  gsasl_finish (out);
98  return res;
99  }
100 
101  *sctx = out;
102 
103  return GSASL_OK;
104 }
105 
118 int
119 gsasl_client_start (Gsasl *ctx, const char *mech, Gsasl_session **sctx)
120 {
121  return start (ctx, mech, sctx, ctx->n_client_mechs, ctx->client_mechs, 1);
122 }
123 
136 int
137 gsasl_server_start (Gsasl *ctx, const char *mech, Gsasl_session **sctx)
138 {
139  return start (ctx, mech, sctx, ctx->n_server_mechs, ctx->server_mechs, 0);
140 }
const char * name
Definition: error.c:37
@ GSASL_NO_CLIENT_CODE
Definition: gsasl.h:139
@ GSASL_OK
Definition: gsasl.h:128
@ GSASL_MALLOC_ERROR
Definition: gsasl.h:132
@ GSASL_NO_SERVER_CODE
Definition: gsasl.h:140
@ GSASL_UNKNOWN_MECHANISM
Definition: gsasl.h:130
_GSASL_API void gsasl_finish(Gsasl_session *sctx)
Definition: xfinish.c:33
Gsasl_start_function start
Definition: gsasl-mech.h:154
Gsasl_step_function step
Definition: gsasl-mech.h:155
struct Gsasl_mechanism_functions server
Definition: gsasl-mech.h:175
struct Gsasl_mechanism_functions client
Definition: gsasl-mech.h:174
void * mech_data
Definition: internal.h:52
Gsasl_mechanism * mech
Definition: internal.h:51
Gsasl * ctx
Definition: internal.h:49
Definition: internal.h:36
size_t n_client_mechs
Definition: internal.h:37
Gsasl_mechanism * server_mechs
Definition: internal.h:40
Gsasl_mechanism * client_mechs
Definition: internal.h:38
size_t n_server_mechs
Definition: internal.h:39
int gsasl_client_start(Gsasl *ctx, const char *mech, Gsasl_session **sctx)
Definition: xstart.c:119
int gsasl_server_start(Gsasl *ctx, const char *mech, Gsasl_session **sctx)
Definition: xstart.c:137