1 /* db_berkeley.c--SASL berkeley db interface
4 * $Id: allockey.c,v 1.8 2006/04/10 13:26:51 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.
58 int _sasldb_alloc_key(const sasl_utils_t *utils,
59 const char *auth_identity,
65 size_t auth_id_len, realm_len, prop_len;
67 if(!utils || !auth_identity || !realm || !propName || !key || !key_len)
70 auth_id_len = strlen(auth_identity);
71 realm_len = strlen(realm);
72 prop_len = strlen(propName);
74 *key_len = auth_id_len + realm_len + prop_len + 2;
75 *key = utils->malloc(*key_len);
78 memcpy(*key, auth_identity, auth_id_len);
79 (*key)[auth_id_len] = '\0';
80 memcpy(*key + auth_id_len + 1, realm, realm_len);
81 (*key)[auth_id_len + realm_len + 1] = '\0';
82 memcpy(*key + auth_id_len + realm_len + 2, propName, prop_len);
90 int _sasldb_parse_key(const char *key, const size_t key_len,
91 char *authid, const size_t max_authid,
92 char *realm, const size_t max_realm,
93 char *propName, const size_t max_propname)
96 unsigned numnulls = 0;
97 size_t alen = 0, rlen = 0, pnlen = 0;
100 || (authid && !max_authid)
101 || (realm && !max_realm)
102 || (propName && !max_propname))
103 return SASL_BADPARAM;
105 for(i=0; i<key_len; i++) {
106 if(key[i] == '\0') numnulls++;
109 if(numnulls != 2) return SASL_BADPARAM;
112 rlen = strlen(key + alen + 1);
113 pnlen = key_len - alen - rlen - 2;
117 if(alen >= max_authid)
119 strncpy(authid, key, max_authid);
123 if(rlen >= max_realm)
125 strncpy(realm, key + alen + 1, max_realm);
129 if(pnlen >= max_propname)
131 strncpy(propName, key + alen + rlen + 2, pnlen);
133 /* Have to add the missing NULL */
134 propName[pnlen] = '\0';
140 /* These are more or less aliases to the correct functions */
141 int _sasldb_getsecret(const sasl_utils_t *utils,
142 sasl_conn_t *context,
145 sasl_secret_t ** secret)
151 const char *param = SASL_AUX_PASSWORD;
155 utils->seterror(context, 0, "No secret pointer in _sasldb_getsecret");
156 return SASL_BADPARAM;
159 ret = _sasldb_getdata(utils, context, authid, realm, param,
166 out = utils->malloc(sizeof(sasl_secret_t) + len);
168 utils->seterror(context, 0, "Out of Memory in _sasldb_getsecret");
172 out->len = (unsigned) len;
173 memcpy(out->data, buf, len);
181 int _sasldb_putsecret(const sasl_utils_t *utils,
182 sasl_conn_t *context,
185 const sasl_secret_t * secret)
187 const char *param = SASL_AUX_PASSWORD;
188 param++; /* skip leading * */
189 return _sasldb_putdata(utils, context, authid, realm, param,
190 (secret ? secret->data : NULL),
191 (secret ? secret->len : 0));
194 int __sasldb_internal_list (const char *authid,
196 const char *property,
197 void *rock __attribute__((unused)))
199 printf("%s@%s: %s\n", authid, realm, property);
204 /* List all users in database */
205 int _sasldb_listusers (const sasl_utils_t *utils,
206 sasl_conn_t *context,
207 sasldb_list_callback_t callback,
215 if (callback == NULL) {
216 callback = &__sasldb_internal_list;
217 callback_rock = NULL;
220 dbh = _sasldb_getkeyhandle(utils, context);
223 utils->log (context, SASL_LOG_ERR, "_sasldb_getkeyhandle has failed");
227 result = _sasldb_getnextkey(utils,
233 while (result == SASL_CONTINUE)
235 char authid_buf[16384];
236 char realm_buf[16384];
237 char property_buf[16384];
240 ret = _sasldb_parse_key(key_buf, key_len,
243 property_buf, 16384);
245 if(ret == SASL_BUFOVER) {
246 utils->log (context, SASL_LOG_ERR, "Key is too large in _sasldb_parse_key");
248 } else if(ret != SASL_OK) {
249 utils->log (context, SASL_LOG_ERR, "Bad Key in _sasldb_parse_key");
253 result = callback (authid_buf,
258 if (result != SASL_OK && result != SASL_CONTINUE) {
262 result = _sasldb_getnextkey(utils,
269 if (result == SASL_BUFOVER) {
270 utils->log (context, SASL_LOG_ERR, "Key is too large in _sasldb_getnextkey");
271 } else if (result != SASL_OK) {
272 utils->log (context, SASL_LOG_ERR, "DB failure in _sasldb_getnextkey");
275 return _sasldb_releasekeyhandle(utils, dbh);