1 /* Anonymous SASL plugin
4 * $Id: anonymous.c,v 1.51 2004/09/08 11:10:52 mel Exp $
7 * Copyright (c) 1998-2003 Carnegie Mellon University. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. The name "Carnegie Mellon University" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For permission or any other legal
24 * details, please contact
25 * Office of Technology Transfer
26 * Carnegie Mellon University
28 * Pittsburgh, PA 15213-3890
29 * (412) 268-4387, fax: (412) 268-7395
30 * tech-transfer@andrew.cmu.edu
32 * 4. Redistributions of any form whatsoever must retain the following
34 * "This product includes software developed by Computing Services
35 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
37 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
38 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
39 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
40 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
41 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
42 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
43 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
55 #include "plugin_common.h"
58 #include <sasl_anonymous_plugin_decl.h>
61 /***************************** Common Section *****************************/
63 static const char plugin_id[] = "$Id: anonymous.c,v 1.51 2004/09/08 11:10:52 mel Exp $";
65 static const char anonymous_id[] = "anonymous";
67 /***************************** Server Section *****************************/
70 anonymous_server_mech_new(void *glob_context __attribute__((unused)),
71 sasl_server_params_t *sparams,
72 const char *challenge __attribute__((unused)),
73 unsigned challen __attribute__((unused)),
76 /* holds state are in */
78 PARAMERROR( sparams->utils );
88 anonymous_server_mech_step(void *conn_context __attribute__((unused)),
89 sasl_server_params_t *sparams,
92 const char **serverout,
93 unsigned *serveroutlen,
94 sasl_out_params_t *oparams)
103 PARAMERROR( sparams->utils );
104 return SASL_BADPARAM;
111 return SASL_CONTINUE;
114 /* We force a truncation 255 characters (specified by RFC 2245) */
115 if (clientinlen > 255) clientinlen = 255;
117 /* NULL-terminate the clientin... */
118 clientdata = sparams->utils->malloc(clientinlen + 1);
120 MEMERROR(sparams->utils);
124 strncpy(clientdata, clientin, clientinlen);
125 clientdata[clientinlen] = '\0';
127 sparams->utils->log(sparams->utils->conn,
129 "ANONYMOUS login: \"%s\"",
132 if (clientdata != clientin)
133 sparams->utils->free(clientdata);
135 result = sparams->canon_user(sparams->utils->conn,
137 SASL_CU_AUTHID | SASL_CU_AUTHZID, oparams);
139 if (result != SASL_OK) return result;
142 oparams->doneflag = 1;
143 oparams->mech_ssf = 0;
144 oparams->maxoutbuf = 0;
145 oparams->encode_context = NULL;
146 oparams->encode = NULL;
147 oparams->decode_context = NULL;
148 oparams->decode = NULL;
149 oparams->param_version = 0;
154 static sasl_server_plug_t anonymous_server_plugins[] =
157 "ANONYMOUS", /* mech_name */
159 SASL_SEC_NOPLAINTEXT, /* security_flags */
160 SASL_FEAT_WANT_CLIENT_FIRST, /* features */
161 NULL, /* glob_context */
162 &anonymous_server_mech_new, /* mech_new */
163 &anonymous_server_mech_step, /* mech_step */
164 NULL, /* mech_dispose */
165 NULL, /* mech_free */
167 NULL, /* user_query */
169 NULL, /* mech_avail */
174 int anonymous_server_plug_init(const sasl_utils_t *utils,
177 sasl_server_plug_t **pluglist,
180 if (maxversion < SASL_SERVER_PLUG_VERSION) {
181 SETERROR( utils, "ANONYMOUS version mismatch" );
185 *out_version = SASL_SERVER_PLUG_VERSION;
186 *pluglist = anonymous_server_plugins;
192 /***************************** Client Section *****************************/
194 typedef struct client_context {
196 unsigned out_buf_len;
200 anonymous_client_mech_new(void *glob_context __attribute__((unused)),
201 sasl_client_params_t *cparams,
204 client_context_t *text;
207 PARAMERROR(cparams->utils);
208 return SASL_BADPARAM;
211 /* holds state are in */
212 text = cparams->utils->malloc(sizeof(client_context_t));
214 MEMERROR(cparams->utils);
218 memset(text, 0, sizeof(client_context_t));
220 *conn_context = text;
226 anonymous_client_mech_step(void *conn_context,
227 sasl_client_params_t *cparams,
228 const char *serverin __attribute__((unused)),
229 unsigned serverinlen,
230 sasl_interact_t **prompt_need,
231 const char **clientout,
232 unsigned *clientoutlen,
233 sasl_out_params_t *oparams)
235 client_context_t *text = (client_context_t *) conn_context;
238 const char *user = NULL;
239 int user_result = SASL_OK;
246 PARAMERROR( cparams->utils );
247 return SASL_BADPARAM;
253 if (serverinlen != 0) {
254 SETERROR( cparams->utils,
255 "Nonzero serverinlen in ANONYMOUS continue_step" );
259 /* check if sec layer strong enough */
260 if (cparams->props.min_ssf > cparams->external_ssf) {
261 SETERROR( cparams->utils, "SSF requested of ANONYMOUS plugin");
265 /* try to get the trace info */
267 user_result = _plug_get_userid(cparams->utils, &user, prompt_need);
269 if ((user_result != SASL_OK) && (user_result != SASL_INTERACT)) {
274 /* free prompts we got */
275 if (prompt_need && *prompt_need) {
276 cparams->utils->free(*prompt_need);
280 /* if there are prompts not filled in */
281 if (user_result == SASL_INTERACT) {
282 /* make the prompt list */
284 _plug_make_prompts(cparams->utils, prompt_need,
285 user_result == SASL_INTERACT ?
286 "Please enter anonymous identification" : NULL,
292 if (result != SASL_OK) return result;
294 return SASL_INTERACT;
297 if (!user || !*user) {
300 userlen = strlen(user);
302 result = cparams->canon_user(cparams->utils->conn,
304 SASL_CU_AUTHID | SASL_CU_AUTHZID, oparams);
305 if (result != SASL_OK) return result;
307 memset(hostname, 0, sizeof(hostname));
308 gethostname(hostname, sizeof(hostname));
309 hostname[sizeof(hostname)-1] = '\0';
311 *clientoutlen = (unsigned) (userlen + strlen(hostname) + 1);
313 result = _plug_buf_alloc(cparams->utils, &text->out_buf,
314 &text->out_buf_len, *clientoutlen);
316 if (result != SASL_OK) return result;
318 strcpy(text->out_buf, user);
319 text->out_buf[userlen] = '@';
320 /* use memcpy() instead of strcpy() so we don't add the NUL */
321 memcpy(text->out_buf + userlen + 1, hostname, strlen(hostname));
323 *clientout = text->out_buf;
326 oparams->doneflag = 1;
327 oparams->mech_ssf = 0;
328 oparams->maxoutbuf = 0;
329 oparams->encode_context = NULL;
330 oparams->encode = NULL;
331 oparams->decode_context = NULL;
332 oparams->decode = NULL;
333 oparams->param_version = 0;
338 static void anonymous_client_dispose(void *conn_context,
339 const sasl_utils_t *utils)
341 client_context_t *text = (client_context_t *) conn_context;
345 if (text->out_buf) utils->free(text->out_buf);
350 static const long anonymous_required_prompts[] = {
354 static sasl_client_plug_t anonymous_client_plugins[] =
357 "ANONYMOUS", /* mech_name */
359 SASL_SEC_NOPLAINTEXT, /* security_flags */
360 SASL_FEAT_WANT_CLIENT_FIRST, /* features */
361 anonymous_required_prompts, /* required_prompts */
362 NULL, /* glob_context */
363 &anonymous_client_mech_new, /* mech_new */
364 &anonymous_client_mech_step, /* mech_step */
365 &anonymous_client_dispose, /* mech_dispose */
366 NULL, /* mech_free */
373 int anonymous_client_plug_init(const sasl_utils_t *utils,
376 sasl_client_plug_t **pluglist,
379 if (maxversion < SASL_CLIENT_PLUG_VERSION) {
380 SETERROR( utils, "ANONYMOUS version mismatch" );
384 *out_version = SASL_CLIENT_PLUG_VERSION;
385 *pluglist = anonymous_client_plugins;