Initial release, version 0.0.0.
[gsasl.git] / lib / anonymous.c
blob2cb3c8c16de516fc38aacb9319b66f9914c6416e
1 /* anonymous.c implementation of SASL mechanism ANONYMOUS from RFC 2245
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 #ifdef USE_ANONYMOUS
26 int
27 _gsasl_anonymous_client_init (Gsasl_ctx *ctx)
29 return GSASL_OK;
32 void
33 _gsasl_anonymous_client_done (Gsasl_ctx *ctx)
35 return;
38 int
39 _gsasl_anonymous_client_start (Gsasl_session_ctx *cctx,
40 void **mech_data)
42 Gsasl_ctx *ctx;
43 int *step;
45 ctx = gsasl_client_ctx_get (cctx);
46 if (ctx == NULL)
47 return GSASL_CANNOT_GET_CTX;
49 if (gsasl_client_callback_anonymous_get (ctx) == NULL)
50 return GSASL_NEED_SERVER_ANONYMOUS_CALLBACK;
52 step = (int*) malloc(sizeof(*step));
53 if (step == NULL)
54 return GSASL_MALLOC_ERROR;
56 *step = 0;
58 *mech_data = step;
60 return GSASL_OK;
63 int
64 _gsasl_anonymous_client_step (Gsasl_session_ctx *cctx,
65 void *mech_data,
66 const char *input,
67 size_t input_len,
68 char *output,
69 size_t *output_len)
71 int *step = mech_data;
72 Gsasl_client_callback_anonymous cb_anonymous;
73 Gsasl_ctx *ctx;
74 int res;
76 if (*step > 0)
77 return GSASL_OK;
79 (*step)++;
81 ctx = gsasl_client_ctx_get (cctx);
82 if (ctx == NULL)
83 return GSASL_CANNOT_GET_CTX;
85 cb_anonymous = gsasl_client_callback_anonymous_get (ctx);
86 if (cb_anonymous == NULL)
87 return GSASL_NEED_SERVER_ANONYMOUS_CALLBACK;
89 res = cb_anonymous (cctx, output, output_len);
90 if (res != GSASL_OK)
91 return res;
93 *output_len = strlen(output);
95 return GSASL_NEEDS_MORE;
98 int
99 _gsasl_anonymous_client_finish (Gsasl_session_ctx *cctx,
100 void *mech_data)
102 int *step = mech_data;
104 free(step);
106 return GSASL_OK;
109 /* Server */
112 _gsasl_anonymous_server_init (Gsasl_ctx *ctx)
114 return GSASL_OK;
117 void
118 _gsasl_anonymous_server_done (Gsasl_ctx *ctx)
120 return;
124 _gsasl_anonymous_server_start (Gsasl_session_ctx *sctx,
125 void **mech_data)
127 Gsasl_ctx *ctx;
129 ctx = gsasl_server_ctx_get (sctx);
130 if (ctx == NULL)
131 return GSASL_CANNOT_GET_CTX;
133 if (gsasl_server_callback_anonymous_get (ctx) == NULL)
134 return GSASL_NEED_SERVER_ANONYMOUS_CALLBACK;
136 return GSASL_OK;
140 _gsasl_anonymous_server_step (Gsasl_session_ctx *sctx,
141 void *mech_data,
142 const char *input,
143 size_t input_len,
144 char *output,
145 size_t *output_len)
147 Gsasl_server_callback_anonymous cb_anonymous;
148 Gsasl_ctx *ctx;
149 char *token;
150 int res;
152 if (input_len == 0)
154 *output_len = 0;
155 return GSASL_NEEDS_MORE;
158 ctx = gsasl_server_ctx_get (sctx);
159 if (ctx == NULL)
160 return GSASL_CANNOT_GET_CTX;
162 cb_anonymous = gsasl_server_callback_anonymous_get (ctx);
163 if (cb_anonymous == NULL)
164 return GSASL_NEED_SERVER_ANONYMOUS_CALLBACK;
166 token = malloc(input_len + 1);
167 if (token == NULL)
168 return GSASL_MALLOC_ERROR;
170 memcpy(token, input, input_len);
171 token[input_len] = '\0';
173 res = cb_anonymous(sctx, token);
175 free(token);
177 return res;
181 _gsasl_anonymous_server_finish (Gsasl_session_ctx *sctx,
182 void *mech_data)
184 return GSASL_OK;
187 #endif /* USE_ANONYMOUS */