Add.
[gsasl.git] / lib / src / callback.c
blob1d775d2b60c509ad6e8d8cfa02aa591898317cd6
1 /* callback.c --- Callback handling.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006 Simon Josefsson
4 * This file is part of GNU SASL Library.
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.
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.
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.
23 #include "internal.h"
25 /**
26 * gsasl_callback_set:
27 * @ctx: handle received from gsasl_init().
28 * @cb: pointer to function implemented by application.
30 * Store the pointer to the application provided callback in the
31 * library handle. The callback will be used, via gsasl_callback(),
32 * by mechanisms to discover various parameters (such as username and
33 * passwords). The callback function will be called with a
34 * Gsasl_property value indicating the requested behaviour. For
35 * example, for GSASL_ANONYMOUS_TOKEN, the function is expected to
36 * invoke gsasl_property_set(CTX, GSASL_ANONYMOUS_TOKEN, "token")
37 * where "token" is the anonymous token the application wishes the
38 * SASL mechanism to use. See the manual for the meaning of all
39 * parameters.
41 * Since: 0.2.0
42 **/
43 void
44 gsasl_callback_set (Gsasl * ctx, Gsasl_callback_function cb)
46 ctx->cb = cb;
49 /**
50 * gsasl_callback:
51 * @ctx: handle received from gsasl_init(), may be NULL to derive it
52 * from @sctx.
53 * @sctx: session handle.
54 * @prop: enumerated value of Gsasl_property type.
56 * Invoke the application callback. The @prop value indicate what the
57 * callback is expected to do. For example, for
58 * GSASL_ANONYMOUS_TOKEN, the function is expected to invoke
59 * gsasl_property_set(SCTX, GSASL_ANONYMOUS_TOKEN, "token") where
60 * "token" is the anonymous token the application wishes the SASL
61 * mechanism to use. See the manual for the meaning of all
62 * parameters.
64 * Note that if no callback has been set by the application, but the
65 * obsolete callback interface has been used, this function will
66 * translate the old callback interface into the new. This interface
67 * should be sufficient to invoke all callbacks, both new and old.
69 * Return value: Returns whatever the application callback return, or
70 * GSASL_NO_CALLBACK if no application was known.
72 * Since: 0.2.0
73 **/
74 int
75 gsasl_callback (Gsasl * ctx, Gsasl_session * sctx, Gsasl_property prop)
77 if (ctx == NULL && sctx == NULL)
78 return GSASL_NO_CALLBACK;
80 if (ctx == NULL)
81 ctx = sctx->ctx;
83 if (ctx->cb)
84 return ctx->cb (ctx, sctx, prop);
86 #ifndef GSASL_NO_OBSOLETE
88 /* Call obsolete callbacks. Remove this when the obsolete
89 * callbacks are no longer supported. */
90 Gsasl_server_callback_anonymous cb_anonymous;
91 Gsasl_server_callback_external cb_external;
92 Gsasl_server_callback_securid cb_securid;
93 Gsasl_server_callback_gssapi cb_gssapi;
94 Gsasl_server_callback_validate cb_validate;
95 Gsasl_server_callback_retrieve cb_retrieve;
96 char buf[BUFSIZ];
97 size_t buflen = BUFSIZ - 1;
98 int res;
100 switch (prop)
102 case GSASL_VALIDATE_ANONYMOUS:
103 if (!sctx->anonymous_token)
104 break;
105 cb_anonymous = gsasl_server_callback_anonymous_get (sctx->ctx);
106 if (!cb_anonymous)
107 break;
108 res = cb_anonymous (sctx, sctx->anonymous_token);
109 return res;
110 break;
112 case GSASL_VALIDATE_EXTERNAL:
113 cb_external = gsasl_server_callback_external_get (sctx->ctx);
114 if (!cb_external)
115 break;
116 res = cb_external (sctx);
117 return res;
118 break;
120 case GSASL_VALIDATE_SECURID:
121 cb_securid = gsasl_server_callback_securid_get (sctx->ctx);
122 if (!cb_securid)
123 break;
124 res = cb_securid (sctx, sctx->authid, sctx->authzid, sctx->passcode,
125 sctx->pin, buf, &buflen);
126 if (buflen > 0 && buflen < BUFSIZ - 1)
128 buf[buflen] = '\0';
129 gsasl_property_set (sctx, GSASL_SUGGESTED_PIN, buf);
131 return res;
132 break;
134 case GSASL_VALIDATE_GSSAPI:
135 cb_gssapi = gsasl_server_callback_gssapi_get (sctx->ctx);
136 if (!cb_gssapi)
137 break;
138 res = cb_gssapi (sctx, sctx->gssapi_display_name, sctx->authzid);
139 return res;
140 break;
142 case GSASL_VALIDATE_SIMPLE:
143 cb_validate = gsasl_server_callback_validate_get (sctx->ctx);
144 if (!cb_validate)
145 break;
146 res = cb_validate (sctx, sctx->authzid, sctx->authid, sctx->password);
147 return res;
148 break;
150 case GSASL_PASSWORD:
151 cb_retrieve = gsasl_server_callback_retrieve_get (sctx->ctx);
152 if (!cb_retrieve)
153 break;
154 res = cb_retrieve (sctx, sctx->authid, sctx->authzid,
155 sctx->hostname, buf, &buflen);
156 if (res == GSASL_OK)
157 gsasl_property_set_raw (sctx, GSASL_PASSWORD, buf, buflen);
158 /* FIXME else if (res == GSASL_TOO_SMALL_BUFFER)... */
159 return res;
160 break;
162 default:
163 break;
166 #endif
168 return GSASL_NO_CALLBACK;
172 * gsasl_callback_hook_set:
173 * @ctx: libgsasl handle.
174 * @hook: opaque pointer to application specific data.
176 * Store application specific data in the libgsasl handle.
178 * The application data can be later (for instance, inside a callback)
179 * be retrieved by calling gsasl_callback_hook_get(). This is
180 * normally used by the application to maintain a global state between
181 * the main program and callbacks.
183 * Since: 0.2.0
185 void
186 gsasl_callback_hook_set (Gsasl * ctx, void *hook)
188 ctx->application_hook = hook;
192 * gsasl_callback_hook_get:
193 * @ctx: libgsasl handle.
195 * Retrieve application specific data from libgsasl handle.
197 * The application data is set using gsasl_callback_hook_set(). This
198 * is normally used by the application to maintain a global state
199 * between the main program and callbacks.
201 * Return value: Returns the application specific data, or NULL.
203 * Since: 0.2.0
205 void *
206 gsasl_callback_hook_get (Gsasl * ctx)
208 return ctx->application_hook;
212 * gsasl_session_hook_set:
213 * @sctx: libgsasl session handle.
214 * @hook: opaque pointer to application specific data.
216 * Store application specific data in the libgsasl session handle.
218 * The application data can be later (for instance, inside a callback)
219 * be retrieved by calling gsasl_session_hook_get(). This is normally
220 * used by the application to maintain a per-session state between the
221 * main program and callbacks.
223 * Since: 0.2.14
225 void
226 gsasl_session_hook_set (Gsasl_session * sctx, void *hook)
228 sctx->application_hook = hook;
232 * gsasl_session_hook_get:
233 * @sctx: libgsasl session handle.
235 * Retrieve application specific data from libgsasl session handle.
237 * The application data is set using gsasl_callback_hook_set(). This
238 * is normally used by the application to maintain a per-session state
239 * between the main program and callbacks.
241 * Return value: Returns the application specific data, or NULL.
243 * Since: 0.2.14
245 void *
246 gsasl_session_hook_get (Gsasl_session * sctx)
248 return sctx->application_hook;