| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 | Infrastructure for async ldap client requests
|
|---|
| 4 | Copyright (C) Volker Lendecke 2009
|
|---|
| 5 |
|
|---|
| 6 | This program is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 3 of the License, or
|
|---|
| 9 | (at your option) any later version.
|
|---|
| 10 |
|
|---|
| 11 | This program 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
|
|---|
| 14 | GNU General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 18 | */
|
|---|
| 19 |
|
|---|
| 20 | #include "includes.h"
|
|---|
| 21 | #include "tldap.h"
|
|---|
| 22 | #include "tldap_util.h"
|
|---|
| 23 | #include "../libcli/security/security.h"
|
|---|
| 24 | #include "../lib/util/asn1.h"
|
|---|
| 25 | #include "../librpc/ndr/libndr.h"
|
|---|
| 26 |
|
|---|
| 27 | bool tldap_entry_values(struct tldap_message *msg, const char *attribute,
|
|---|
| 28 | DATA_BLOB **values, int *num_values)
|
|---|
| 29 | {
|
|---|
| 30 | struct tldap_attribute *attributes;
|
|---|
| 31 | int i, num_attributes;
|
|---|
| 32 |
|
|---|
| 33 | if (!tldap_entry_attributes(msg, &attributes, &num_attributes)) {
|
|---|
| 34 | return false;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | for (i=0; i<num_attributes; i++) {
|
|---|
| 38 | if (strequal(attribute, attributes[i].name)) {
|
|---|
| 39 | break;
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 | if (i == num_attributes) {
|
|---|
| 43 | return false;
|
|---|
| 44 | }
|
|---|
| 45 | *num_values = attributes[i].num_values;
|
|---|
| 46 | *values = attributes[i].values;
|
|---|
| 47 | return true;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | bool tldap_get_single_valueblob(struct tldap_message *msg,
|
|---|
| 51 | const char *attribute, DATA_BLOB *blob)
|
|---|
| 52 | {
|
|---|
| 53 | int num_values;
|
|---|
| 54 | DATA_BLOB *values;
|
|---|
| 55 |
|
|---|
| 56 | if (attribute == NULL) {
|
|---|
| 57 | return NULL;
|
|---|
| 58 | }
|
|---|
| 59 | if (!tldap_entry_values(msg, attribute, &values, &num_values)) {
|
|---|
| 60 | return NULL;
|
|---|
| 61 | }
|
|---|
| 62 | if (num_values != 1) {
|
|---|
| 63 | return NULL;
|
|---|
| 64 | }
|
|---|
| 65 | *blob = values[0];
|
|---|
| 66 | return true;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | char *tldap_talloc_single_attribute(struct tldap_message *msg,
|
|---|
| 70 | const char *attribute,
|
|---|
| 71 | TALLOC_CTX *mem_ctx)
|
|---|
| 72 | {
|
|---|
| 73 | DATA_BLOB val;
|
|---|
| 74 | char *result;
|
|---|
| 75 | size_t len;
|
|---|
| 76 |
|
|---|
| 77 | if (!tldap_get_single_valueblob(msg, attribute, &val)) {
|
|---|
| 78 | return false;
|
|---|
| 79 | }
|
|---|
| 80 | if (!convert_string_talloc(mem_ctx, CH_UTF8, CH_UNIX,
|
|---|
| 81 | val.data, val.length,
|
|---|
| 82 | &result, &len, false)) {
|
|---|
| 83 | return NULL;
|
|---|
| 84 | }
|
|---|
| 85 | return result;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | bool tldap_pull_binsid(struct tldap_message *msg, const char *attribute,
|
|---|
| 89 | struct dom_sid *sid)
|
|---|
| 90 | {
|
|---|
| 91 | DATA_BLOB val;
|
|---|
| 92 |
|
|---|
| 93 | if (!tldap_get_single_valueblob(msg, attribute, &val)) {
|
|---|
| 94 | return false;
|
|---|
| 95 | }
|
|---|
| 96 | return sid_parse((char *)val.data, val.length, sid);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | bool tldap_pull_guid(struct tldap_message *msg, const char *attribute,
|
|---|
| 100 | struct GUID *guid)
|
|---|
| 101 | {
|
|---|
| 102 | DATA_BLOB val;
|
|---|
| 103 |
|
|---|
| 104 | if (!tldap_get_single_valueblob(msg, attribute, &val)) {
|
|---|
| 105 | return false;
|
|---|
| 106 | }
|
|---|
| 107 | return NT_STATUS_IS_OK(GUID_from_data_blob(&val, guid));
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | static bool tldap_add_blob_vals(TALLOC_CTX *mem_ctx, struct tldap_mod *mod,
|
|---|
| 111 | DATA_BLOB *newvals, int num_newvals)
|
|---|
| 112 | {
|
|---|
| 113 | int num_values = talloc_array_length(mod->values);
|
|---|
| 114 | int i;
|
|---|
| 115 | DATA_BLOB *tmp;
|
|---|
| 116 |
|
|---|
| 117 | tmp = talloc_realloc(mem_ctx, mod->values, DATA_BLOB,
|
|---|
| 118 | num_values + num_newvals);
|
|---|
| 119 | if (tmp == NULL) {
|
|---|
| 120 | return false;
|
|---|
| 121 | }
|
|---|
| 122 | mod->values = tmp;
|
|---|
| 123 |
|
|---|
| 124 | for (i=0; i<num_newvals; i++) {
|
|---|
| 125 | mod->values[i+num_values].data = (uint8_t *)talloc_memdup(
|
|---|
| 126 | mod->values, newvals[i].data, newvals[i].length);
|
|---|
| 127 | if (mod->values[i+num_values].data == NULL) {
|
|---|
| 128 | return false;
|
|---|
| 129 | }
|
|---|
| 130 | mod->values[i+num_values].length = newvals[i].length;
|
|---|
| 131 | }
|
|---|
| 132 | mod->num_values = num_values + num_newvals;
|
|---|
| 133 | return true;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | bool tldap_add_mod_blobs(TALLOC_CTX *mem_ctx,
|
|---|
| 137 | struct tldap_mod **pmods, int *pnum_mods,
|
|---|
| 138 | int mod_op, const char *attrib,
|
|---|
| 139 | DATA_BLOB *newvals, int num_newvals)
|
|---|
| 140 | {
|
|---|
| 141 | struct tldap_mod new_mod;
|
|---|
| 142 | struct tldap_mod *mods = *pmods;
|
|---|
| 143 | struct tldap_mod *mod = NULL;
|
|---|
| 144 | int i, num_mods;
|
|---|
| 145 |
|
|---|
| 146 | if (mods == NULL) {
|
|---|
| 147 | mods = talloc_array(mem_ctx, struct tldap_mod, 0);
|
|---|
| 148 | }
|
|---|
| 149 | if (mods == NULL) {
|
|---|
| 150 | return false;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | num_mods = *pnum_mods;
|
|---|
| 154 |
|
|---|
| 155 | for (i=0; i<num_mods; i++) {
|
|---|
| 156 | if ((mods[i].mod_op == mod_op)
|
|---|
| 157 | && strequal(mods[i].attribute, attrib)) {
|
|---|
| 158 | mod = &mods[i];
|
|---|
| 159 | break;
|
|---|
| 160 | }
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | if (mod == NULL) {
|
|---|
| 164 | new_mod.mod_op = mod_op;
|
|---|
| 165 | new_mod.attribute = talloc_strdup(mods, attrib);
|
|---|
| 166 | if (new_mod.attribute == NULL) {
|
|---|
| 167 | return false;
|
|---|
| 168 | }
|
|---|
| 169 | new_mod.num_values = 0;
|
|---|
| 170 | new_mod.values = NULL;
|
|---|
| 171 | mod = &new_mod;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | if ((num_newvals != 0)
|
|---|
| 175 | && !tldap_add_blob_vals(mods, mod, newvals, num_newvals)) {
|
|---|
| 176 | return false;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | if ((i == num_mods) && (talloc_array_length(mods) < num_mods + 1)) {
|
|---|
| 180 | mods = talloc_realloc(talloc_tos(), mods, struct tldap_mod,
|
|---|
| 181 | num_mods+1);
|
|---|
| 182 | if (mods == NULL) {
|
|---|
| 183 | return false;
|
|---|
| 184 | }
|
|---|
| 185 | mods[num_mods] = *mod;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | *pmods = mods;
|
|---|
| 189 | *pnum_mods += 1;
|
|---|
| 190 | return true;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | bool tldap_add_mod_str(TALLOC_CTX *mem_ctx,
|
|---|
| 194 | struct tldap_mod **pmods, int *pnum_mods,
|
|---|
| 195 | int mod_op, const char *attrib, const char *str)
|
|---|
| 196 | {
|
|---|
| 197 | DATA_BLOB utf8;
|
|---|
| 198 | bool ret;
|
|---|
| 199 |
|
|---|
| 200 | if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_UTF8, str,
|
|---|
| 201 | strlen(str), &utf8.data, &utf8.length,
|
|---|
| 202 | false)) {
|
|---|
| 203 | return false;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | ret = tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods, mod_op, attrib,
|
|---|
| 207 | &utf8, 1);
|
|---|
| 208 | TALLOC_FREE(utf8.data);
|
|---|
| 209 | return ret;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | static bool tldap_make_mod_blob_int(struct tldap_message *existing,
|
|---|
| 213 | TALLOC_CTX *mem_ctx,
|
|---|
| 214 | struct tldap_mod **pmods, int *pnum_mods,
|
|---|
| 215 | const char *attrib, DATA_BLOB newval,
|
|---|
| 216 | int (*comparison)(const DATA_BLOB *d1,
|
|---|
| 217 | const DATA_BLOB *d2))
|
|---|
| 218 | {
|
|---|
| 219 | int num_values = 0;
|
|---|
| 220 | DATA_BLOB *values = NULL;
|
|---|
| 221 | DATA_BLOB oldval = data_blob_null;
|
|---|
| 222 |
|
|---|
| 223 | if ((existing != NULL)
|
|---|
| 224 | && tldap_entry_values(existing, attrib, &values, &num_values)) {
|
|---|
| 225 |
|
|---|
| 226 | if (num_values > 1) {
|
|---|
| 227 | /* can't change multivalue attributes atm */
|
|---|
| 228 | return false;
|
|---|
| 229 | }
|
|---|
| 230 | if (num_values == 1) {
|
|---|
| 231 | oldval = values[0];
|
|---|
| 232 | }
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | if ((oldval.data != NULL) && (newval.data != NULL)
|
|---|
| 236 | && (comparison(&oldval, &newval) == 0)) {
|
|---|
| 237 | /* Believe it or not, but LDAP will deny a delete and
|
|---|
| 238 | an add at the same time if the values are the
|
|---|
| 239 | same... */
|
|---|
| 240 | DEBUG(10,("tldap_make_mod_blob_int: attribute |%s| not "
|
|---|
| 241 | "changed.\n", attrib));
|
|---|
| 242 | return true;
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | if (oldval.data != NULL) {
|
|---|
| 246 | /* By deleting exactly the value we found in the entry this
|
|---|
| 247 | * should be race-free in the sense that the LDAP-Server will
|
|---|
| 248 | * deny the complete operation if somebody changed the
|
|---|
| 249 | * attribute behind our back. */
|
|---|
| 250 | /* This will also allow modifying single valued attributes in
|
|---|
| 251 | * Novell NDS. In NDS you have to first remove attribute and
|
|---|
| 252 | * then you could add new value */
|
|---|
| 253 |
|
|---|
| 254 | DEBUG(10, ("tldap_make_mod_blob_int: deleting attribute |%s|\n",
|
|---|
| 255 | attrib));
|
|---|
| 256 | if (!tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods,
|
|---|
| 257 | TLDAP_MOD_DELETE,
|
|---|
| 258 | attrib, &oldval, 1)) {
|
|---|
| 259 | return false;
|
|---|
| 260 | }
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | /* Regardless of the real operation (add or modify)
|
|---|
| 264 | we add the new value here. We rely on deleting
|
|---|
| 265 | the old value, should it exist. */
|
|---|
| 266 |
|
|---|
| 267 | if (newval.data != NULL) {
|
|---|
| 268 | DEBUG(10, ("tldap_make_mod_blob_int: adding attribute |%s| value len "
|
|---|
| 269 | "%d\n", attrib, (int)newval.length));
|
|---|
| 270 | if (!tldap_add_mod_blobs(mem_ctx, pmods, pnum_mods,
|
|---|
| 271 | TLDAP_MOD_ADD,
|
|---|
| 272 | attrib, &newval, 1)) {
|
|---|
| 273 | return false;
|
|---|
| 274 | }
|
|---|
| 275 | }
|
|---|
| 276 | return true;
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | bool tldap_make_mod_blob(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
|
|---|
| 280 | struct tldap_mod **pmods, int *pnum_mods,
|
|---|
| 281 | const char *attrib, DATA_BLOB newval)
|
|---|
| 282 | {
|
|---|
| 283 | return tldap_make_mod_blob_int(existing, mem_ctx, pmods, pnum_mods,
|
|---|
| 284 | attrib, newval, data_blob_cmp);
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | static int compare_utf8_blobs(const DATA_BLOB *d1, const DATA_BLOB *d2)
|
|---|
| 288 | {
|
|---|
| 289 | char *s1, *s2;
|
|---|
| 290 | size_t s1len, s2len;
|
|---|
| 291 | int ret;
|
|---|
| 292 |
|
|---|
| 293 | if (!convert_string_talloc(talloc_tos(), CH_UTF8, CH_UNIX, d1->data,
|
|---|
| 294 | d1->length, &s1, &s1len, false)) {
|
|---|
| 295 | /* can't do much here */
|
|---|
| 296 | return 0;
|
|---|
| 297 | }
|
|---|
| 298 | if (!convert_string_talloc(talloc_tos(), CH_UTF8, CH_UNIX, d2->data,
|
|---|
| 299 | d2->length, &s2, &s2len, false)) {
|
|---|
| 300 | /* can't do much here */
|
|---|
| 301 | TALLOC_FREE(s1);
|
|---|
| 302 | return 0;
|
|---|
| 303 | }
|
|---|
| 304 | ret = StrCaseCmp(s1, s2);
|
|---|
| 305 | TALLOC_FREE(s2);
|
|---|
| 306 | TALLOC_FREE(s1);
|
|---|
| 307 | return ret;
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | bool tldap_make_mod_fmt(struct tldap_message *existing, TALLOC_CTX *mem_ctx,
|
|---|
| 311 | struct tldap_mod **pmods, int *pnum_mods,
|
|---|
| 312 | const char *attrib, const char *fmt, ...)
|
|---|
| 313 | {
|
|---|
| 314 | va_list ap;
|
|---|
| 315 | char *newval;
|
|---|
| 316 | bool ret;
|
|---|
| 317 | DATA_BLOB blob = data_blob_null;
|
|---|
| 318 |
|
|---|
| 319 | va_start(ap, fmt);
|
|---|
| 320 | newval = talloc_vasprintf(talloc_tos(), fmt, ap);
|
|---|
| 321 | va_end(ap);
|
|---|
| 322 |
|
|---|
| 323 | if (newval == NULL) {
|
|---|
| 324 | return false;
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | blob.length = strlen(newval);
|
|---|
| 328 | if (blob.length != 0) {
|
|---|
| 329 | blob.data = CONST_DISCARD(uint8_t *, newval);
|
|---|
| 330 | }
|
|---|
| 331 | ret = tldap_make_mod_blob_int(existing, mem_ctx, pmods, pnum_mods,
|
|---|
| 332 | attrib, blob, compare_utf8_blobs);
|
|---|
| 333 | TALLOC_FREE(newval);
|
|---|
| 334 | return ret;
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | const char *tldap_errstr(TALLOC_CTX *mem_ctx, struct tldap_context *ld, int rc)
|
|---|
| 338 | {
|
|---|
| 339 | const char *ld_error = NULL;
|
|---|
| 340 | char *res;
|
|---|
| 341 |
|
|---|
| 342 | if (ld != NULL) {
|
|---|
| 343 | ld_error = tldap_msg_diagnosticmessage(tldap_ctx_lastmsg(ld));
|
|---|
| 344 | }
|
|---|
| 345 | res = talloc_asprintf(mem_ctx, "LDAP error %d (%s), %s", rc,
|
|---|
| 346 | tldap_err2string(rc),
|
|---|
| 347 | ld_error ? ld_error : "unknown");
|
|---|
| 348 | return res;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | int tldap_search_va(struct tldap_context *ld, const char *base, int scope,
|
|---|
| 352 | const char *attrs[], int num_attrs, int attrsonly,
|
|---|
| 353 | TALLOC_CTX *mem_ctx, struct tldap_message ***res,
|
|---|
| 354 | const char *fmt, va_list ap)
|
|---|
| 355 | {
|
|---|
| 356 | char *filter;
|
|---|
| 357 | int ret;
|
|---|
| 358 |
|
|---|
| 359 | filter = talloc_vasprintf(talloc_tos(), fmt, ap);
|
|---|
| 360 | if (filter == NULL) {
|
|---|
| 361 | return TLDAP_NO_MEMORY;
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | ret = tldap_search(ld, base, scope, filter,
|
|---|
| 365 | attrs, num_attrs, attrsonly,
|
|---|
| 366 | NULL /*sctrls*/, 0, NULL /*cctrls*/, 0,
|
|---|
| 367 | 0 /*timelimit*/, 0 /*sizelimit*/, 0 /*deref*/,
|
|---|
| 368 | mem_ctx, res, NULL);
|
|---|
| 369 | TALLOC_FREE(filter);
|
|---|
| 370 | return ret;
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | int tldap_search_fmt(struct tldap_context *ld, const char *base, int scope,
|
|---|
| 374 | const char *attrs[], int num_attrs, int attrsonly,
|
|---|
| 375 | TALLOC_CTX *mem_ctx, struct tldap_message ***res,
|
|---|
| 376 | const char *fmt, ...)
|
|---|
| 377 | {
|
|---|
| 378 | va_list ap;
|
|---|
| 379 | int ret;
|
|---|
| 380 |
|
|---|
| 381 | va_start(ap, fmt);
|
|---|
| 382 | ret = tldap_search_va(ld, base, scope, attrs, num_attrs, attrsonly,
|
|---|
| 383 | mem_ctx, res, fmt, ap);
|
|---|
| 384 | va_end(ap);
|
|---|
| 385 | return ret;
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | bool tldap_pull_uint64(struct tldap_message *msg, const char *attr,
|
|---|
| 389 | uint64_t *presult)
|
|---|
| 390 | {
|
|---|
| 391 | char *str;
|
|---|
| 392 | uint64_t result;
|
|---|
| 393 |
|
|---|
| 394 | str = tldap_talloc_single_attribute(msg, attr, talloc_tos());
|
|---|
| 395 | if (str == NULL) {
|
|---|
| 396 | DEBUG(10, ("Could not find attribute %s\n", attr));
|
|---|
| 397 | return false;
|
|---|
| 398 | }
|
|---|
| 399 | result = strtoull(str, NULL, 10);
|
|---|
| 400 | TALLOC_FREE(str);
|
|---|
| 401 | *presult = result;
|
|---|
| 402 | return true;
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | bool tldap_pull_uint32(struct tldap_message *msg, const char *attr,
|
|---|
| 406 | uint32_t *presult)
|
|---|
| 407 | {
|
|---|
| 408 | uint64_t result;
|
|---|
| 409 |
|
|---|
| 410 | if (!tldap_pull_uint64(msg, attr, &result)) {
|
|---|
| 411 | return false;
|
|---|
| 412 | }
|
|---|
| 413 | *presult = (uint32_t)result;
|
|---|
| 414 | return true;
|
|---|
| 415 | }
|
|---|
| 416 |
|
|---|
| 417 | struct tldap_fetch_rootdse_state {
|
|---|
| 418 | struct tldap_context *ld;
|
|---|
| 419 | struct tldap_message *rootdse;
|
|---|
| 420 | };
|
|---|
| 421 |
|
|---|
| 422 | static void tldap_fetch_rootdse_done(struct tevent_req *subreq);
|
|---|
| 423 |
|
|---|
| 424 | struct tevent_req *tldap_fetch_rootdse_send(TALLOC_CTX *mem_ctx,
|
|---|
| 425 | struct tevent_context *ev,
|
|---|
| 426 | struct tldap_context *ld)
|
|---|
| 427 | {
|
|---|
| 428 | struct tevent_req *req, *subreq;
|
|---|
| 429 | struct tldap_fetch_rootdse_state *state;
|
|---|
| 430 | static const char *attrs[2] = { "*", "+" };
|
|---|
| 431 |
|
|---|
| 432 | req = tevent_req_create(mem_ctx, &state,
|
|---|
| 433 | struct tldap_fetch_rootdse_state);
|
|---|
| 434 | if (req == NULL) {
|
|---|
| 435 | return NULL;
|
|---|
| 436 | }
|
|---|
| 437 | state->ld = ld;
|
|---|
| 438 | state->rootdse = NULL;
|
|---|
| 439 |
|
|---|
| 440 | subreq = tldap_search_send(
|
|---|
| 441 | mem_ctx, ev, ld, "", TLDAP_SCOPE_BASE, "(objectclass=*)",
|
|---|
| 442 | attrs, ARRAY_SIZE(attrs), 0, NULL, 0, NULL, 0, 0, 0, 0);
|
|---|
| 443 | if (tevent_req_nomem(subreq, req)) {
|
|---|
| 444 | return tevent_req_post(req, ev);
|
|---|
| 445 | }
|
|---|
| 446 | tevent_req_set_callback(subreq, tldap_fetch_rootdse_done, req);
|
|---|
| 447 | return req;
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | static void tldap_fetch_rootdse_done(struct tevent_req *subreq)
|
|---|
| 451 | {
|
|---|
| 452 | struct tevent_req *req = tevent_req_callback_data(
|
|---|
| 453 | subreq, struct tevent_req);
|
|---|
| 454 | struct tldap_fetch_rootdse_state *state = tevent_req_data(
|
|---|
| 455 | req, struct tldap_fetch_rootdse_state);
|
|---|
| 456 | struct tldap_message *msg;
|
|---|
| 457 | int rc;
|
|---|
| 458 |
|
|---|
| 459 | rc = tldap_search_recv(subreq, state, &msg);
|
|---|
| 460 | if (rc != TLDAP_SUCCESS) {
|
|---|
| 461 | TALLOC_FREE(subreq);
|
|---|
| 462 | tevent_req_error(req, rc);
|
|---|
| 463 | return;
|
|---|
| 464 | }
|
|---|
| 465 |
|
|---|
| 466 | switch (tldap_msg_type(msg)) {
|
|---|
| 467 | case TLDAP_RES_SEARCH_ENTRY:
|
|---|
| 468 | if (state->rootdse != NULL) {
|
|---|
| 469 | goto protocol_error;
|
|---|
| 470 | }
|
|---|
| 471 | state->rootdse = msg;
|
|---|
| 472 | break;
|
|---|
| 473 | case TLDAP_RES_SEARCH_RESULT:
|
|---|
| 474 | TALLOC_FREE(subreq);
|
|---|
| 475 | if (state->rootdse == NULL) {
|
|---|
| 476 | goto protocol_error;
|
|---|
| 477 | }
|
|---|
| 478 | tevent_req_done(req);
|
|---|
| 479 | break;
|
|---|
| 480 | default:
|
|---|
| 481 | goto protocol_error;
|
|---|
| 482 | }
|
|---|
| 483 | return;
|
|---|
| 484 |
|
|---|
| 485 | protocol_error:
|
|---|
| 486 | tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
|
|---|
| 487 | return;
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | int tldap_fetch_rootdse_recv(struct tevent_req *req)
|
|---|
| 491 | {
|
|---|
| 492 | struct tldap_fetch_rootdse_state *state = tevent_req_data(
|
|---|
| 493 | req, struct tldap_fetch_rootdse_state);
|
|---|
| 494 | int err;
|
|---|
| 495 | char *dn;
|
|---|
| 496 |
|
|---|
| 497 | if (tevent_req_is_ldap_error(req, &err)) {
|
|---|
| 498 | return err;
|
|---|
| 499 | }
|
|---|
| 500 | /* Trigger parsing the dn, just to make sure it's ok */
|
|---|
| 501 | if (!tldap_entry_dn(state->rootdse, &dn)) {
|
|---|
| 502 | return TLDAP_DECODING_ERROR;
|
|---|
| 503 | }
|
|---|
| 504 | if (!tldap_context_setattr(state->ld, "tldap:rootdse",
|
|---|
| 505 | &state->rootdse)) {
|
|---|
| 506 | return TLDAP_NO_MEMORY;
|
|---|
| 507 | }
|
|---|
| 508 | return 0;
|
|---|
| 509 | }
|
|---|
| 510 |
|
|---|
| 511 | int tldap_fetch_rootdse(struct tldap_context *ld)
|
|---|
| 512 | {
|
|---|
| 513 | TALLOC_CTX *frame = talloc_stackframe();
|
|---|
| 514 | struct tevent_context *ev;
|
|---|
| 515 | struct tevent_req *req;
|
|---|
| 516 | int result;
|
|---|
| 517 |
|
|---|
| 518 | ev = event_context_init(frame);
|
|---|
| 519 | if (ev == NULL) {
|
|---|
| 520 | result = TLDAP_NO_MEMORY;
|
|---|
| 521 | goto fail;
|
|---|
| 522 | }
|
|---|
| 523 |
|
|---|
| 524 | req = tldap_fetch_rootdse_send(frame, ev, ld);
|
|---|
| 525 | if (req == NULL) {
|
|---|
| 526 | result = TLDAP_NO_MEMORY;
|
|---|
| 527 | goto fail;
|
|---|
| 528 | }
|
|---|
| 529 |
|
|---|
| 530 | if (!tevent_req_poll(req, ev)) {
|
|---|
| 531 | result = TLDAP_OPERATIONS_ERROR;
|
|---|
| 532 | goto fail;
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| 535 | result = tldap_fetch_rootdse_recv(req);
|
|---|
| 536 | fail:
|
|---|
| 537 | TALLOC_FREE(frame);
|
|---|
| 538 | return result;
|
|---|
| 539 | }
|
|---|
| 540 |
|
|---|
| 541 | struct tldap_message *tldap_rootdse(struct tldap_context *ld)
|
|---|
| 542 | {
|
|---|
| 543 | return talloc_get_type(tldap_context_getattr(ld, "tldap:rootdse"),
|
|---|
| 544 | struct tldap_message);
|
|---|
| 545 | }
|
|---|
| 546 |
|
|---|
| 547 | bool tldap_entry_has_attrvalue(struct tldap_message *msg,
|
|---|
| 548 | const char *attribute,
|
|---|
| 549 | const DATA_BLOB blob)
|
|---|
| 550 | {
|
|---|
| 551 | int i, num_values;
|
|---|
| 552 | DATA_BLOB *values;
|
|---|
| 553 |
|
|---|
| 554 | if (!tldap_entry_values(msg, attribute, &values, &num_values)) {
|
|---|
| 555 | return false;
|
|---|
| 556 | }
|
|---|
| 557 | for (i=0; i<num_values; i++) {
|
|---|
| 558 | if (data_blob_cmp(&values[i], &blob) == 0) {
|
|---|
| 559 | return true;
|
|---|
| 560 | }
|
|---|
| 561 | }
|
|---|
| 562 | return false;
|
|---|
| 563 | }
|
|---|
| 564 |
|
|---|
| 565 | bool tldap_supports_control(struct tldap_context *ld, const char *oid)
|
|---|
| 566 | {
|
|---|
| 567 | struct tldap_message *rootdse = tldap_rootdse(ld);
|
|---|
| 568 |
|
|---|
| 569 | if (rootdse == NULL) {
|
|---|
| 570 | return false;
|
|---|
| 571 | }
|
|---|
| 572 | return tldap_entry_has_attrvalue(rootdse, "supportedControl",
|
|---|
| 573 | data_blob_const(oid, strlen(oid)));
|
|---|
| 574 | }
|
|---|
| 575 |
|
|---|
| 576 | struct tldap_control *tldap_add_control(TALLOC_CTX *mem_ctx,
|
|---|
| 577 | struct tldap_control *ctrls,
|
|---|
| 578 | int num_ctrls,
|
|---|
| 579 | struct tldap_control *ctrl)
|
|---|
| 580 | {
|
|---|
| 581 | struct tldap_control *result;
|
|---|
| 582 |
|
|---|
| 583 | result = talloc_array(mem_ctx, struct tldap_control, num_ctrls+1);
|
|---|
| 584 | if (result == NULL) {
|
|---|
| 585 | return NULL;
|
|---|
| 586 | }
|
|---|
| 587 | memcpy(result, ctrls, sizeof(struct tldap_control) * num_ctrls);
|
|---|
| 588 | result[num_ctrls] = *ctrl;
|
|---|
| 589 | return result;
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | /*
|
|---|
| 593 | * Find a control returned by the server
|
|---|
| 594 | */
|
|---|
| 595 | struct tldap_control *tldap_msg_findcontrol(struct tldap_message *msg,
|
|---|
| 596 | const char *oid)
|
|---|
| 597 | {
|
|---|
| 598 | struct tldap_control *controls;
|
|---|
| 599 | int i, num_controls;
|
|---|
| 600 |
|
|---|
| 601 | tldap_msg_sctrls(msg, &num_controls, &controls);
|
|---|
| 602 |
|
|---|
| 603 | for (i=0; i<num_controls; i++) {
|
|---|
| 604 | if (strcmp(controls[i].oid, oid) == 0) {
|
|---|
| 605 | return &controls[i];
|
|---|
| 606 | }
|
|---|
| 607 | }
|
|---|
| 608 | return NULL;
|
|---|
| 609 | }
|
|---|
| 610 |
|
|---|
| 611 | struct tldap_search_paged_state {
|
|---|
| 612 | struct tevent_context *ev;
|
|---|
| 613 | struct tldap_context *ld;
|
|---|
| 614 | const char *base;
|
|---|
| 615 | const char *filter;
|
|---|
| 616 | int scope;
|
|---|
| 617 | const char **attrs;
|
|---|
| 618 | int num_attrs;
|
|---|
| 619 | int attrsonly;
|
|---|
| 620 | struct tldap_control *sctrls;
|
|---|
| 621 | int num_sctrls;
|
|---|
| 622 | struct tldap_control *cctrls;
|
|---|
| 623 | int num_cctrls;
|
|---|
| 624 | int timelimit;
|
|---|
| 625 | int sizelimit;
|
|---|
| 626 | int deref;
|
|---|
| 627 |
|
|---|
| 628 | int page_size;
|
|---|
| 629 | struct asn1_data *asn1;
|
|---|
| 630 | DATA_BLOB cookie;
|
|---|
| 631 | struct tldap_message *result;
|
|---|
| 632 | };
|
|---|
| 633 |
|
|---|
| 634 | static struct tevent_req *tldap_ship_paged_search(
|
|---|
| 635 | TALLOC_CTX *mem_ctx,
|
|---|
| 636 | struct tldap_search_paged_state *state)
|
|---|
| 637 | {
|
|---|
| 638 | struct tldap_control *pgctrl;
|
|---|
| 639 | struct asn1_data *asn1;
|
|---|
| 640 |
|
|---|
| 641 | asn1 = asn1_init(state);
|
|---|
| 642 | if (asn1 == NULL) {
|
|---|
| 643 | return NULL;
|
|---|
| 644 | }
|
|---|
| 645 | asn1_push_tag(asn1, ASN1_SEQUENCE(0));
|
|---|
| 646 | asn1_write_Integer(asn1, state->page_size);
|
|---|
| 647 | asn1_write_OctetString(asn1, state->cookie.data, state->cookie.length);
|
|---|
| 648 | asn1_pop_tag(asn1);
|
|---|
| 649 | if (asn1->has_error) {
|
|---|
| 650 | TALLOC_FREE(asn1);
|
|---|
| 651 | return NULL;
|
|---|
| 652 | }
|
|---|
| 653 | state->asn1 = asn1;
|
|---|
| 654 |
|
|---|
| 655 | pgctrl = &state->sctrls[state->num_sctrls-1];
|
|---|
| 656 | pgctrl->oid = TLDAP_CONTROL_PAGEDRESULTS;
|
|---|
| 657 | pgctrl->critical = true;
|
|---|
| 658 | if (!asn1_blob(state->asn1, &pgctrl->value)) {
|
|---|
| 659 | TALLOC_FREE(asn1);
|
|---|
| 660 | return NULL;
|
|---|
| 661 | }
|
|---|
| 662 | return tldap_search_send(mem_ctx, state->ev, state->ld, state->base,
|
|---|
| 663 | state->scope, state->filter, state->attrs,
|
|---|
| 664 | state->num_attrs, state->attrsonly,
|
|---|
| 665 | state->sctrls, state->num_sctrls,
|
|---|
| 666 | state->cctrls, state->num_cctrls,
|
|---|
| 667 | state->timelimit, state->sizelimit,
|
|---|
| 668 | state->deref);
|
|---|
| 669 | }
|
|---|
| 670 |
|
|---|
| 671 | static void tldap_search_paged_done(struct tevent_req *subreq);
|
|---|
| 672 |
|
|---|
| 673 | struct tevent_req *tldap_search_paged_send(TALLOC_CTX *mem_ctx,
|
|---|
| 674 | struct tevent_context *ev,
|
|---|
| 675 | struct tldap_context *ld,
|
|---|
| 676 | const char *base, int scope,
|
|---|
| 677 | const char *filter,
|
|---|
| 678 | const char **attrs,
|
|---|
| 679 | int num_attrs,
|
|---|
| 680 | int attrsonly,
|
|---|
| 681 | struct tldap_control *sctrls,
|
|---|
| 682 | int num_sctrls,
|
|---|
| 683 | struct tldap_control *cctrls,
|
|---|
| 684 | int num_cctrls,
|
|---|
| 685 | int timelimit,
|
|---|
| 686 | int sizelimit,
|
|---|
| 687 | int deref,
|
|---|
| 688 | int page_size)
|
|---|
| 689 | {
|
|---|
| 690 | struct tevent_req *req, *subreq;
|
|---|
| 691 | struct tldap_search_paged_state *state;
|
|---|
| 692 | struct tldap_control empty_control;
|
|---|
| 693 |
|
|---|
| 694 | req = tevent_req_create(mem_ctx, &state,
|
|---|
| 695 | struct tldap_search_paged_state);
|
|---|
| 696 | if (req == NULL) {
|
|---|
| 697 | return NULL;
|
|---|
| 698 | }
|
|---|
| 699 | state->ev = ev;
|
|---|
| 700 | state->ld = ld;
|
|---|
| 701 | state->base = base;
|
|---|
| 702 | state->filter = filter;
|
|---|
| 703 | state->scope = scope;
|
|---|
| 704 | state->attrs = attrs;
|
|---|
| 705 | state->num_attrs = num_attrs;
|
|---|
| 706 | state->attrsonly = attrsonly;
|
|---|
| 707 | state->cctrls = cctrls;
|
|---|
| 708 | state->num_cctrls = num_cctrls;
|
|---|
| 709 | state->timelimit = timelimit;
|
|---|
| 710 | state->sizelimit = sizelimit;
|
|---|
| 711 | state->deref = deref;
|
|---|
| 712 |
|
|---|
| 713 | state->page_size = page_size;
|
|---|
| 714 | state->asn1 = NULL;
|
|---|
| 715 | state->cookie = data_blob_null;
|
|---|
| 716 |
|
|---|
| 717 | ZERO_STRUCT(empty_control);
|
|---|
| 718 |
|
|---|
| 719 | state->sctrls = tldap_add_control(state, sctrls, num_sctrls,
|
|---|
| 720 | &empty_control);
|
|---|
| 721 | if (tevent_req_nomem(state->sctrls, req)) {
|
|---|
| 722 | return tevent_req_post(req, ev);
|
|---|
| 723 | }
|
|---|
| 724 | state->num_sctrls = num_sctrls+1;
|
|---|
| 725 |
|
|---|
| 726 | subreq = tldap_ship_paged_search(state, state);
|
|---|
| 727 | if (tevent_req_nomem(subreq, req)) {
|
|---|
| 728 | return tevent_req_post(req, ev);
|
|---|
| 729 | }
|
|---|
| 730 | tevent_req_set_callback(subreq, tldap_search_paged_done, req);
|
|---|
| 731 |
|
|---|
| 732 | return req;
|
|---|
| 733 | }
|
|---|
| 734 |
|
|---|
| 735 | static void tldap_search_paged_done(struct tevent_req *subreq)
|
|---|
| 736 | {
|
|---|
| 737 | struct tevent_req *req = tevent_req_callback_data(
|
|---|
| 738 | subreq, struct tevent_req);
|
|---|
| 739 | struct tldap_search_paged_state *state = tevent_req_data(
|
|---|
| 740 | req, struct tldap_search_paged_state);
|
|---|
| 741 | struct asn1_data *asn1;
|
|---|
| 742 | struct tldap_control *pgctrl;
|
|---|
| 743 | int rc, size;
|
|---|
| 744 |
|
|---|
| 745 | rc = tldap_search_recv(subreq, state, &state->result);
|
|---|
| 746 | if (rc != TLDAP_SUCCESS) {
|
|---|
| 747 | TALLOC_FREE(subreq);
|
|---|
| 748 | tevent_req_error(req, rc);
|
|---|
| 749 | return;
|
|---|
| 750 | }
|
|---|
| 751 |
|
|---|
| 752 | TALLOC_FREE(state->asn1);
|
|---|
| 753 |
|
|---|
| 754 | switch (tldap_msg_type(state->result)) {
|
|---|
| 755 | case TLDAP_RES_SEARCH_ENTRY:
|
|---|
| 756 | case TLDAP_RES_SEARCH_REFERENCE:
|
|---|
| 757 | tevent_req_notify_callback(req);
|
|---|
| 758 | return;
|
|---|
| 759 | case TLDAP_RES_SEARCH_RESULT:
|
|---|
| 760 | break;
|
|---|
| 761 | default:
|
|---|
| 762 | TALLOC_FREE(subreq);
|
|---|
| 763 | tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
|
|---|
| 764 | return;
|
|---|
| 765 | }
|
|---|
| 766 |
|
|---|
| 767 | TALLOC_FREE(subreq);
|
|---|
| 768 |
|
|---|
| 769 | /* We've finished one paged search, fire the next */
|
|---|
| 770 |
|
|---|
| 771 | pgctrl = tldap_msg_findcontrol(state->result,
|
|---|
| 772 | TLDAP_CONTROL_PAGEDRESULTS);
|
|---|
| 773 | if (pgctrl == NULL) {
|
|---|
| 774 | /* RFC2696 requires the server to return the control */
|
|---|
| 775 | tevent_req_error(req, TLDAP_PROTOCOL_ERROR);
|
|---|
| 776 | return;
|
|---|
| 777 | }
|
|---|
| 778 |
|
|---|
| 779 | TALLOC_FREE(state->cookie.data);
|
|---|
| 780 |
|
|---|
| 781 | asn1 = asn1_init(talloc_tos());
|
|---|
| 782 | if (asn1 == NULL) {
|
|---|
| 783 | tevent_req_error(req, TLDAP_NO_MEMORY);
|
|---|
| 784 | return;
|
|---|
| 785 | }
|
|---|
| 786 |
|
|---|
| 787 | asn1_load_nocopy(asn1, pgctrl->value.data, pgctrl->value.length);
|
|---|
| 788 | asn1_start_tag(asn1, ASN1_SEQUENCE(0));
|
|---|
| 789 | asn1_read_Integer(asn1, &size);
|
|---|
| 790 | asn1_read_OctetString(asn1, state, &state->cookie);
|
|---|
| 791 | asn1_end_tag(asn1);
|
|---|
| 792 | if (asn1->has_error) {
|
|---|
| 793 | tevent_req_error(req, TLDAP_DECODING_ERROR);
|
|---|
| 794 | return;
|
|---|
| 795 | }
|
|---|
| 796 | TALLOC_FREE(asn1);
|
|---|
| 797 |
|
|---|
| 798 | if (state->cookie.length == 0) {
|
|---|
| 799 | /* We're done, no cookie anymore */
|
|---|
| 800 | tevent_req_done(req);
|
|---|
| 801 | return;
|
|---|
| 802 | }
|
|---|
| 803 |
|
|---|
| 804 | TALLOC_FREE(state->result);
|
|---|
| 805 |
|
|---|
| 806 | subreq = tldap_ship_paged_search(state, state);
|
|---|
| 807 | if (tevent_req_nomem(subreq, req)) {
|
|---|
| 808 | return;
|
|---|
| 809 | }
|
|---|
| 810 | tevent_req_set_callback(subreq, tldap_search_paged_done, req);
|
|---|
| 811 | }
|
|---|
| 812 |
|
|---|
| 813 | int tldap_search_paged_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
|
|---|
| 814 | struct tldap_message **pmsg)
|
|---|
| 815 | {
|
|---|
| 816 | struct tldap_search_paged_state *state = tevent_req_data(
|
|---|
| 817 | req, struct tldap_search_paged_state);
|
|---|
| 818 | int err;
|
|---|
| 819 |
|
|---|
| 820 | if (!tevent_req_is_in_progress(req)
|
|---|
| 821 | && tevent_req_is_ldap_error(req, &err)) {
|
|---|
| 822 | return err;
|
|---|
| 823 | }
|
|---|
| 824 | if (tevent_req_is_in_progress(req)) {
|
|---|
| 825 | switch (tldap_msg_type(state->result)) {
|
|---|
| 826 | case TLDAP_RES_SEARCH_ENTRY:
|
|---|
| 827 | case TLDAP_RES_SEARCH_REFERENCE:
|
|---|
| 828 | break;
|
|---|
| 829 | default:
|
|---|
| 830 | return TLDAP_PROTOCOL_ERROR;
|
|---|
| 831 | }
|
|---|
| 832 | }
|
|---|
| 833 | *pmsg = talloc_move(mem_ctx, &state->result);
|
|---|
| 834 | return TLDAP_SUCCESS;
|
|---|
| 835 | }
|
|---|