Menu

[r178]: / trunk / ext / cyrus-sasl / plugins / sasldb.c  Maximize  Restore  History

Download this file

241 lines (203 with data), 7.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* SASL server API implementation
* Rob Siemborski
* Tim Martin
* $Id: sasldb.c,v 1.11 2006/04/03 10:58:19 mel Exp $
*/
/*
* Copyright (c) 1998-2003 Carnegie Mellon University. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The name "Carnegie Mellon University" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For permission or any other legal
* details, please contact
* Office of Technology Transfer
* Carnegie Mellon University
* 5000 Forbes Avenue
* Pittsburgh, PA 15213-3890
* (412) 268-4387, fax: (412) 268-7395
* tech-transfer@andrew.cmu.edu
*
* 4. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by Computing Services
* at Carnegie Mellon University (http://www.cmu.edu/computing/)."
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <config.h>
/* sasldb stuff */
#include <stdio.h>
#include "sasl.h"
#include "saslutil.h"
#include "saslplug.h"
#include "../sasldb/sasldb.h"
#include "plugin_common.h"
static void sasldb_auxprop_lookup(void *glob_context __attribute__((unused)),
sasl_server_params_t *sparams,
unsigned flags,
const char *user,
unsigned ulen)
{
char *userid = NULL;
char *realm = NULL;
const char *user_realm = NULL;
int ret;
const struct propval *to_fetch, *cur;
char value[8192];
size_t value_len;
char *user_buf;
if(!sparams || !user) return;
user_buf = sparams->utils->malloc(ulen + 1);
if(!user_buf) {
goto done;
}
memcpy(user_buf, user, ulen);
user_buf[ulen] = '\0';
if(sparams->user_realm) {
user_realm = sparams->user_realm;
} else {
user_realm = sparams->serverFQDN;
}
ret = _plug_parseuser(sparams->utils, &userid, &realm, user_realm,
sparams->serverFQDN, user_buf);
if(ret != SASL_OK) goto done;
to_fetch = sparams->utils->prop_get(sparams->propctx);
if(!to_fetch) goto done;
for(cur = to_fetch; cur->name; cur++) {
const char *realname = cur->name;
/* Only look up properties that apply to this lookup! */
if(cur->name[0] == '*' && (flags & SASL_AUXPROP_AUTHZID)) continue;
if(!(flags & SASL_AUXPROP_AUTHZID)) {
if(cur->name[0] != '*') continue;
else realname = cur->name + 1;
}
/* If it's there already, we want to see if it needs to be
* overridden */
if(cur->values && !(flags & SASL_AUXPROP_OVERRIDE))
continue;
else if(cur->values)
sparams->utils->prop_erase(sparams->propctx, cur->name);
ret = _sasldb_getdata(sparams->utils,
sparams->utils->conn, userid, realm,
realname, value, sizeof(value), &value_len);
if(ret != SASL_OK) {
/* We didn't find it, leave it as not found */
continue;
}
sparams->utils->prop_set(sparams->propctx, cur->name,
value, (unsigned) value_len);
}
done:
if (userid) sparams->utils->free(userid);
if (realm) sparams->utils->free(realm);
if (user_buf) sparams->utils->free(user_buf);
}
static int sasldb_auxprop_store(void *glob_context __attribute__((unused)),
sasl_server_params_t *sparams,
struct propctx *ctx,
const char *user,
unsigned ulen)
{
char *userid = NULL;
char *realm = NULL;
const char *user_realm = NULL;
int ret = SASL_FAIL;
int tmp_res;
const struct propval *to_store, *cur;
char *user_buf;
/* just checking if we are enabled */
if(!ctx) return SASL_OK;
if(!sparams || !user) return SASL_BADPARAM;
user_buf = sparams->utils->malloc(ulen + 1);
if(!user_buf) {
ret = SASL_NOMEM;
goto done;
}
memcpy(user_buf, user, ulen);
user_buf[ulen] = '\0';
if(sparams->user_realm) {
user_realm = sparams->user_realm;
} else {
user_realm = sparams->serverFQDN;
}
ret = _plug_parseuser(sparams->utils, &userid, &realm, user_realm,
sparams->serverFQDN, user_buf);
if(ret != SASL_OK) goto done;
to_store = sparams->utils->prop_get(ctx);
if(!to_store) {
ret = SASL_BADPARAM;
goto done;
}
/* All iterations return SASL_NOUSER ==> ret = SASL_NOUSER
Some iterations return SASL_OK and some SASL_NOUSER ==> ret = SASL_OK
At least one iteration returns any other error ==> ret = the error */
ret = SASL_NOUSER;
for(cur = to_store; cur->name; cur++) {
/* We only support one value at a time right now. */
tmp_res = _sasldb_putdata(sparams->utils, sparams->utils->conn,
userid, realm, cur->name,
cur->values && cur->values[0] ?
cur->values[0] : NULL,
cur->values && cur->values[0] ?
strlen(cur->values[0]) : 0);
/* SASL_NOUSER is returned when _sasldb_putdata fails to delete
a non-existent entry, which should not be treated as an error */
if ((tmp_res != SASL_NOUSER) &&
(ret == SASL_NOUSER || ret == SASL_OK)) {
ret = tmp_res;
}
/* Abort the loop if an error has occurred */
if (ret != SASL_NOUSER && ret != SASL_OK) {
break;
}
}
done:
if (userid) sparams->utils->free(userid);
if (realm) sparams->utils->free(realm);
if (user_buf) sparams->utils->free(user_buf);
return ret;
}
static sasl_auxprop_plug_t sasldb_auxprop_plugin = {
0, /* Features */
0, /* spare */
NULL, /* glob_context */
sasldb_auxprop_free, /* auxprop_free */
sasldb_auxprop_lookup, /* auxprop_lookup */
"sasldb", /* name */
sasldb_auxprop_store /* auxprop_store */
};
int sasldb_auxprop_plug_init(const sasl_utils_t *utils,
int max_version,
int *out_version,
sasl_auxprop_plug_t **plug,
const char *plugname __attribute__((unused)))
{
if(!out_version || !plug) return SASL_BADPARAM;
/* Do we have database support? */
/* Note that we can use a NULL sasl_conn_t because our
* sasl_utils_t is "blessed" with the global callbacks */
if(_sasl_check_db(utils, NULL) != SASL_OK)
return SASL_NOMECH;
if(max_version < SASL_AUXPROP_PLUG_VERSION) return SASL_BADVERS;
*out_version = SASL_AUXPROP_PLUG_VERSION;
*plug = &sasldb_auxprop_plugin;
return SASL_OK;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.