Bump versions.
[gsasl.git] / lib / src / property.c
blobe495ca1124dde48bc41e0cd4dfaafe53caf915ef
1 /* property.c --- Callback property handling.
2 * Copyright (C) 2004, 2005 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 static char **
26 map (Gsasl_session * sctx, Gsasl_property prop)
28 char **p = NULL;
30 if (!sctx)
31 return NULL;
33 switch (prop)
35 case GSASL_ANONYMOUS_TOKEN:
36 p = &sctx->anonymous_token;
37 break;
39 case GSASL_SERVICE:
40 p = &sctx->service;
41 break;
43 case GSASL_HOSTNAME:
44 p = &sctx->hostname;
45 break;
47 case GSASL_AUTHID:
48 p = &sctx->authid;
49 break;
51 case GSASL_AUTHZID:
52 p = &sctx->authzid;
53 break;
55 case GSASL_PASSWORD:
56 p = &sctx->password;
57 break;
59 case GSASL_PASSCODE:
60 p = &sctx->passcode;
61 break;
63 case GSASL_PIN:
64 p = &sctx->pin;
65 break;
67 case GSASL_SUGGESTED_PIN:
68 p = &sctx->suggestedpin;
69 break;
71 case GSASL_GSSAPI_DISPLAY_NAME:
72 p = &sctx->gssapi_display_name;
73 break;
75 case GSASL_REALM:
76 p = &sctx->realm;
77 break;
79 default:
80 break;
83 return p;
86 /**
87 * gsasl_property_set:
88 * @sctx: session handle.
89 * @prop: enumerated value of Gsasl_property type, indicating the
90 * type of data in @data.
91 * @data: zero terminated character string to store.
93 * Make a copy of @data and store it in the session handle for the
94 * indicated property @prop.
96 * You can immediately deallocate @data after calling this function,
97 * without affecting the data stored in the session handle.
99 * Since: 0.2.0
101 void
102 gsasl_property_set (Gsasl_session * sctx, Gsasl_property prop,
103 const char *data)
105 gsasl_property_set_raw (sctx, prop, data, data ? strlen (data) : 0);
109 * gsasl_property_set_raw:
110 * @sctx: session handle.
111 * @prop: enumerated value of Gsasl_property type, indicating the
112 * type of data in @data.
113 * @data: character string to store.
114 * @len: length of character string to store.
116 * Make a copy of @len sized @data and store a zero terminated version
117 * of it in the session handle for the indicated property @prop.
119 * You can immediately deallocate @data after calling this function,
120 * without affecting the data stored in the session handle.
122 * Except for the length indicator, this function is identical to
123 * gsasl_property_set.
125 * Since: 0.2.0
127 void
128 gsasl_property_set_raw (Gsasl_session * sctx, Gsasl_property prop,
129 const char *data, size_t len)
131 char **p = map (sctx, prop);
133 if (p)
135 if (*p)
136 free (*p);
137 if (data)
139 *p = malloc (len + 1);
140 if (*p)
142 memcpy (*p, data, len);
143 (*p)[len] = '\0';
146 else
147 *p = NULL;
152 * gsasl_property_fast:
153 * @sctx: session handle.
154 * @prop: enumerated value of Gsasl_property type, indicating the
155 * type of data in @data.
157 * Retrieve the data stored in the session handle for given property
158 * @prop.
160 * The pointer is to live data, and must not be deallocated or
161 * modified in any way.
163 * This function will not invoke the application callback.
165 * Return value: Return property value, if known, or %NULL if no value
166 * known.
168 * Since: 0.2.0
170 const char *
171 gsasl_property_fast (Gsasl_session * sctx, Gsasl_property prop)
173 char **p = map (sctx, prop);
175 if (p)
176 return *p;
178 return NULL;
182 * gsasl_property_get:
183 * @sctx: session handle.
184 * @prop: enumerated value of Gsasl_property type, indicating the
185 * type of data in @data.
187 * Retrieve the data stored in the session handle for given property
188 * @prop, possibly invoking the application callback to get the value.
190 * The pointer is to live data, and must not be deallocated or
191 * modified in any way.
193 * This function will invoke the application callback, using
194 * gsasl_callback(), when a property value is not known.
196 * If no value is known, and no callback is specified or if the
197 * callback fail to return data, and if any obsolete callback
198 * functions has been set by the application, this function will try
199 * to call these obsolete callbacks, and store the returned data as
200 * the corresponding property. This behaviour of this function will
201 * be removed when the obsolete callback interfaces are removed.
203 * Return value: Return data for property, or NULL if no value known.
205 * Since: 0.2.0
207 const char *
208 gsasl_property_get (Gsasl_session * sctx, Gsasl_property prop)
210 const char *p = gsasl_property_fast (sctx, prop);
212 if (!p)
214 gsasl_callback (NULL, sctx, prop);
215 p = gsasl_property_fast (sctx, prop);
218 #ifndef GSASL_NO_OBSOLETE
219 if (!p)
221 Gsasl_client_callback_anonymous cb_anonymous;
222 Gsasl_client_callback_authorization_id cb_authorization_id;
223 Gsasl_client_callback_authentication_id cb_authentication_id;
224 Gsasl_client_callback_password cb_password;
225 Gsasl_client_callback_passcode cb_passcode;
226 Gsasl_client_callback_pin cb_pin;
227 Gsasl_client_callback_service cb_service;
228 Gsasl_client_callback_realm cb_realm;
229 char buf[BUFSIZ];
230 size_t buflen = BUFSIZ - 1;
231 int res;
233 buf[0] = '\0';
235 /* Call obsolete callbacks to get properties. Remove this when
236 * the obsolete callbacks are no longer supported. */
238 switch (prop)
240 case GSASL_SERVICE:
241 cb_service = gsasl_client_callback_service_get (sctx->ctx);
242 if (!cb_service)
243 break;
244 res = cb_service (sctx, buf, &buflen, NULL, 0, NULL, 0);
245 if (res != GSASL_OK)
246 break;
247 buf[buflen] = '\0';
248 gsasl_property_set (sctx, prop, buf);
249 break;
251 case GSASL_HOSTNAME:
252 cb_service = gsasl_client_callback_service_get (sctx->ctx);
253 if (!cb_service)
254 break;
255 res = cb_service (sctx, NULL, 0, buf, &buflen, NULL, 0);
256 if (res != GSASL_OK)
257 break;
258 buf[buflen] = '\0';
259 gsasl_property_set (sctx, prop, buf);
260 break;
262 case GSASL_ANONYMOUS_TOKEN:
263 cb_anonymous = gsasl_client_callback_anonymous_get (sctx->ctx);
264 if (!cb_anonymous)
265 break;
266 res = cb_anonymous (sctx, buf, &buflen);
267 if (res != GSASL_OK)
268 break;
269 buf[buflen] = '\0';
270 gsasl_property_set (sctx, prop, buf);
271 break;
273 case GSASL_AUTHID:
274 cb_authentication_id =
275 gsasl_client_callback_authentication_id_get (sctx->ctx);
276 if (!cb_authentication_id)
277 break;
278 res = cb_authentication_id (sctx, buf, &buflen);
279 if (res != GSASL_OK)
280 break;
281 buf[buflen] = '\0';
282 gsasl_property_set (sctx, prop, buf);
283 break;
285 case GSASL_AUTHZID:
286 cb_authorization_id =
287 gsasl_client_callback_authorization_id_get (sctx->ctx);
288 if (!cb_authorization_id)
289 break;
290 res = cb_authorization_id (sctx, buf, &buflen);
291 if (res != GSASL_OK)
292 break;
293 buf[buflen] = '\0';
294 gsasl_property_set (sctx, prop, buf);
295 break;
297 case GSASL_PASSWORD:
298 cb_password = gsasl_client_callback_password_get (sctx->ctx);
299 if (!cb_password)
300 break;
301 res = cb_password (sctx, buf, &buflen);
302 if (res != GSASL_OK)
303 break;
304 buf[buflen] = '\0';
305 gsasl_property_set (sctx, prop, buf);
306 break;
308 case GSASL_PASSCODE:
309 cb_passcode = gsasl_client_callback_passcode_get (sctx->ctx);
310 if (!cb_passcode)
311 break;
312 res = cb_passcode (sctx, buf, &buflen);
313 if (res != GSASL_OK)
314 break;
315 buf[buflen] = '\0';
316 gsasl_property_set (sctx, prop, buf);
317 break;
319 case GSASL_PIN:
320 cb_pin = gsasl_client_callback_pin_get (sctx->ctx);
321 if (!cb_pin)
322 break;
323 res = cb_pin (sctx, sctx->suggestedpin, buf, &buflen);
324 if (res != GSASL_OK)
325 break;
326 buf[buflen] = '\0';
327 gsasl_property_set (sctx, prop, buf);
328 break;
330 case GSASL_REALM:
331 cb_realm = gsasl_client_callback_realm_get (sctx->ctx);
332 if (!cb_realm)
333 break;
334 res = cb_realm (sctx, buf, &buflen);
335 if (res != GSASL_OK)
336 break;
337 buf[buflen] = '\0';
338 gsasl_property_set (sctx, prop, buf);
339 break;
342 default:
343 break;
345 p = gsasl_property_fast (sctx, prop);
347 #endif
349 return p;