Initial release, version 0.0.0.
[gsasl.git] / lib / xstart.c
blob25261e6a6078bc431def6c79e354669c1cfeccb3
1 /* xstart.c start session
2 * Copyright (C) 2002 Simon Josefsson
4 * This file is part of libgsasl.
6 * Libgsasl is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libgsasl 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with libgsasl; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "internal.h"
24 static int
25 _gsasl_session_start (Gsasl_ctx *ctx,
26 const char *mech,
27 Gsasl_session_ctx **xctx,
28 int clientp)
30 int i = 0;
31 int res;
33 *xctx = (Gsasl_session_ctx*) malloc(sizeof(**xctx));
34 if (*xctx == NULL)
35 return GSASL_MALLOC_ERROR;
37 memset(*xctx, 0, sizeof(**xctx));
39 for (i = 0; i < (clientp ? ctx->n_client_mechs : ctx->n_server_mechs); i++)
41 if (mech && ((clientp && strcmp(mech, ctx->client_mechs[i].name) == 0) ||
42 (!clientp && strcmp(mech, ctx->server_mechs[i].name) == 0)))
44 if (clientp)
45 (*xctx)->mech = &ctx->client_mechs[i];
46 else
47 (*xctx)->mech = &ctx->server_mechs[i];
48 break;
52 if ((*xctx)->mech == NULL)
54 free(*xctx);
55 *xctx = NULL;
56 return GSASL_UNKNOWN_MECHANISM;
59 (*xctx)->ctx = ctx;
60 (*xctx)->mech_data = NULL;
61 if (clientp)
62 res = (*xctx)->mech->client.start(*xctx, &(*xctx)->mech_data);
63 else
64 res = (*xctx)->mech->server.start(*xctx, &(*xctx)->mech_data);
66 if (res != GSASL_OK)
68 free(*xctx);
69 *xctx = NULL;
70 return res;
73 return GSASL_OK;
76 /**
77 * gsasl_client_start:
78 * @ctx: libgsasl handle.
79 * @mech: name of SASL mechanism.
80 * @xctx: pointer to client handle.
82 * This functions initiates a client SASL authentication. This
83 * function must be called before any other gsasl_client_*() function
84 * is called.
86 * Return value: Returns GSASL_OK if successful, or error code.
87 **/
88 int
89 gsasl_client_start (Gsasl_ctx *ctx,
90 const char *mech,
91 Gsasl_session_ctx **xctx)
93 return _gsasl_session_start (ctx, mech, xctx, 1);
96 /**
97 * gsasl_server_start:
98 * @ctx: libgsasl handle.
99 * @mech: name of SASL mechanism.
100 * @xctx: pointer to server handle.
102 * This functions initiates a server SASL authentication. This
103 * function must be called before any other gsasl_server_*() function
104 * is called.
106 * Return value: Returns GSASL_OK if successful, or error code.
109 gsasl_server_start (Gsasl_ctx *ctx,
110 const char *mech,
111 Gsasl_session_ctx **xctx)
113 return _gsasl_session_start (ctx, mech, xctx, 0);