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