Debian Codegraph @

src/_bonsai/wldap-utf8.c

raw symbols SCIP
1 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
2
3 #include <stdio.h>
4
5 #include "wldap-utf8.h"
6
7 /* Get size of a list by reaching the terminating NULL. */
8 static int
9 get_size(void **list) {
10 int size = 0;
11
12 if (list == NULL) return 0;
13
14 while (list[size] != NULL) size++;
15
16 return size;
17 }
18
19 /* Copy the items of a list into a new list using the `copyfunc` function. */
20 static int
21 copy_list(void **list, void **newlist, int(*copyfunc)(void *, void**)) {
22 int i = 0;
23 int rc = 0;
24
25 if (list == NULL || newlist == NULL) return LDAP_PARAM_ERROR;
26
27 for (i = 0; list[i] != NULL; i++) {
28 rc = (copyfunc)(list[i], &newlist[i]);
29 if (rc != LDAP_SUCCESS) {
30 /* Copy is failed, break the cycle and set the failed
31 item to NULL thus the successfully converted items
32 can be freed without overrunning the list. */
33 break;
34 }
35 }
36 newlist[i] = NULL;
37 return rc;
38 }
39
40 /* Free the list element using the `freefunc` function. */
41 static void
42 free_list(void **list, void freefunc(void*)) {
43 int i = 0;
44
45 if (list != NULL) {
46 for (i = 0; list[i] != NULL; i++) {
47 (freefunc)(list[i]);
48 }
49 free(list);
50 }
51 }
52
53
54 /* Convert a wide character string into a UTF-8 (narrow) string. */
55 static int
56 convert_to_mbs(wchar_t *tmp, char **str) {
57 int size = 0;
58 int rc = 0;
59
60 if (tmp == NULL) return LDAP_SUCCESS;
61
62 /* Get necessary size for the new UTF-8 encoded char*. */
63 size = WideCharToMultiByte(CP_UTF8, 0, tmp, -1, NULL, 0, NULL, NULL);
64 *str = (char *)malloc(sizeof(char) * size);
65 if (*str == NULL) return LDAP_NO_MEMORY;
66
67 rc = WideCharToMultiByte(CP_UTF8, 0, tmp, -1, *str, size, NULL, NULL);
68 if (rc == 0) {
69 free(*str);
70 *str = NULL;
71 return LDAP_ENCODING_ERROR;
72 }
73
74 return LDAP_SUCCESS;
75 }
76
77 /* Convert an narrow character string into a UTF-16 (wide) string. */
78 static int
79 convert_to_wcs(char *tmp, wchar_t **str) {
80 int size = 0;
81 int rc = 0;
82
83 if (tmp == NULL) return LDAP_SUCCESS;
84
85 /* Get necessary size for the new wchar_t*. */
86 size = MultiByteToWideChar(CP_UTF8, 0, tmp, -1, NULL, 0);
87 *str = (wchar_t *)malloc(sizeof(wchar_t) * size);
88 if (*str == NULL) return LDAP_NO_MEMORY;
89
90 rc = MultiByteToWideChar(CP_UTF8, 0, tmp, -1, *str, size);
91 if (rc == 0) {
92 free(*str);
93 *str = NULL;
94 return LDAP_DECODING_ERROR;
95 }
96
97 return LDAP_SUCCESS;
98 }
99
100
101 /* Convert a list of narrow character strings into a list of UTF-16 (wide)
102 strings. */
103 static int
104 convert_char_list(char **list, wchar_t ***wlist) {
105 int size = 0;
106 int rc = 0;
107
108 if (list == NULL) return LDAP_SUCCESS;
109
110 size = get_size(list);
111
112 *wlist = (wchar_t **)malloc(sizeof(wchar_t*) * (size + 1));
113 if (*wlist == NULL) return LDAP_NO_MEMORY;
114
115 rc = copy_list(list, *wlist, ((int(*)(void*, void**))convert_to_wcs));
116
117 if (rc != LDAP_SUCCESS) {
118 /* At least one of the item's conversion is failed,
119 thus the list is not converted properly. */
120 free_list(*wlist, &free);
121 }
122
123 return rc;
124 }
125
126 /* Convert a list of UTf-16 (wide) character strings into a list of narrow
127 strings. */
128 static int
129 convert_wchar_list(wchar_t **wlist, char ***list) {
130 int size = 0;
131 int rc = 0;
132
133 if (wlist == NULL) return LDAP_SUCCESS;
134
135 size = get_size(wlist);
136
137 *list = (char **)malloc(sizeof(char *) * (size + 1));
138 if (*list == NULL) return LDAP_NO_MEMORY;
139
140 rc = copy_list(wlist, *list, ((int(*)(void*, void**))convert_to_mbs));
141
142 if (rc != LDAP_SUCCESS) {
143 /* At least one of the item's conversion is failed,
144 thus the list is not converted properly. */
145 free_list(*list, &free);
146 }
147
148 return rc;
149 }
150
151 /* Convert an "ANSI" LDAPControl struct into an UTF-16 wide LDAPControl. */
152 static int
153 convert_ctrl(LDAPControlA *ctrl, LDAPControlW **wctrl) {
154 int rc = 0;
155 wchar_t *woid = NULL;
156
157 if (ctrl == NULL) return LDAP_SUCCESS;
158
159 *wctrl = (LDAPControlW *)malloc(sizeof(LDAPControlW));
160 if (*wctrl == NULL) return LDAP_NO_MEMORY;
161
162 (*wctrl)->ldctl_iscritical = ctrl->ldctl_iscritical;
163 (*wctrl)->ldctl_value = ctrl->ldctl_value;
164
165 if (rc = convert_to_wcs(ctrl->ldctl_oid, &woid) != LDAP_SUCCESS) {
166 free(*wctrl);
167 return rc;
168 }
169
170 (*wctrl)->ldctl_oid = woid;
171
172 return LDAP_SUCCESS;
173 }
174
175 /* Free a converted wide LDAPControl. */
176 static void
177 free_ctrl(LDAPControlW *ctrl) {
178 if (ctrl != NULL) {
179 free(ctrl->ldctl_oid);
180 free(ctrl);
181 }
182 }
183
184 /* Convert a list of "ANSI" LDAPControl struct into a list of UTF-16
185 wide LDAPControl. */
186 static int
187 convert_ctrl_list(LDAPControlA **ctrls, LDAPControlW ***wctrls) {
188 int size = 0;
189 int rc = 0;
190
191 if (ctrls == NULL) return LDAP_SUCCESS;
192
193 size = get_size(ctrls);
194 *wctrls = (LDAPControlW **)malloc(sizeof(LDAPControlW*) * (size + 1));
195 if (*wctrls == NULL) return LDAP_NO_MEMORY;
196
197 rc = copy_list(ctrls, *wctrls, ((int(*)(void*, void**))convert_ctrl));
198
199 if (rc != LDAP_SUCCESS) {
200 /* At least one of the item's conversion is failed,
201 thus the list is not converted properly. */
202 free_list(*wctrls, &free_ctrl);
203 }
204
205 return rc;
206 }
207
208 /* Convert an "ANSI" LDAPMod struct into an UTF-16 wide LDAPMod. */
209 static int
210 convert_mod(LDAPModA *mod, LDAPModW **wmod) {
211 int rc = 0;
212
213 if (mod == NULL) return LDAP_SUCCESS;
214
215 *wmod = (LDAPModW *)malloc(sizeof(LDAPModW));
216 if (*wmod == NULL) return LDAP_NO_MEMORY;
217
218 (*wmod)->mod_op = mod->mod_op;
219
220 if (((*wmod)->mod_op & LDAP_MOD_BVALUES) == LDAP_MOD_BVALUES) {
221 (*wmod)->mod_vals.modv_bvals = mod->mod_vals.modv_bvals;
222 } else {
223 rc = convert_char_list(mod->mod_vals.modv_strvals, &((*wmod)->mod_vals.modv_strvals));
224 if (rc != LDAP_SUCCESS) {
225 free(*wmod);
226 return rc;
227 }
228 }
229
230 rc = convert_to_wcs(mod->mod_type, &((*wmod)->mod_type));
231 if (rc != LDAP_SUCCESS) {
232 if ((mod->mod_op & LDAP_MOD_BVALUES) != LDAP_MOD_BVALUES) {
233 free_list((*wmod)->mod_vals.modv_strvals, &free);
234 }
235 free(*wmod);
236 }
237
238 return rc;
239 }
240
241 /* Free a converted wide LDAPMod. */
242 static void
243 free_mod(LDAPModW *mod) {
244 if (mod != NULL) {
245 free(mod->mod_type);
246 if ((mod->mod_op & LDAP_MOD_BVALUES) != LDAP_MOD_BVALUES) {
247 if (mod->mod_vals.modv_strvals) free_list(mod->mod_vals.modv_strvals, &free);
248 }
249 free(mod);
250 }
251 }
252
253 /* Convert a list of "ANSI" LDAPMod struct into a list of UTF-16
254 wide LDAPMod. */
255 static int
256 convert_mod_list(LDAPModA **mods, LDAPModW ***wmods) {
257 int rc = 0;
258 int size = 0;
259
260 if (mods == NULL) return LDAP_SUCCESS;
261
262 size = get_size(mods);
263 *wmods = (LDAPModW **)malloc(sizeof(LDAPModW *) * (size + 1));
264 if (*wmods == NULL) return LDAP_NO_MEMORY;
265
266 rc = copy_list(mods, *wmods, (void *)&convert_mod);
267
268 if (rc != LDAP_SUCCESS) {
269 /* At least one of the item's conversion is failed,
270 thus the list is not converted properly. */
271 free_list(*wmods, &free_mod);
272 }
273
274 return rc;
275 }
276
277 /******************************************************************************
278 * All of the following functions behave just like they documented in the WinLDAP
279 * or OpenLDAP documentations (except where the comment says otherwise), but they
280 * can return with encoding or decoding error code, if the conversation is failed.
281 ******************************************************************************/
282
283 int
284 ldap_unbind_ext(LDAP *ld, LDAPControlA **sctrls, LDAPControlA **cctrls) {
285 return ldap_unbind(ld);
286 }
287
288 int
289 ldap_abandon_ext(LDAP *ld, int msgid, LDAPControlA **sctrls, LDAPControlA **cctrls) {
290 return ldap_abandon(ld, msgid);
291 }
292
293 char *
294 ldap_get_dnU(LDAP *ld, LDAPMessage *entry) {
295 char *dn = NULL;
296 wchar_t *wdn = NULL;
297
298 wdn = ldap_get_dnW(ld, entry);
299
300 convert_to_mbs(wdn, &dn);
301
302 ldap_memfreeW(wdn);
303
304 return dn;
305 }
306
307 int
308 ldap_add_extU(LDAP *ld, char *dn, LDAPModA **attrs, LDAPControlA **sctrls, LDAPControlA **cctrls,
309 int *msgidp) {
310
311 int rc = 0;
312 wchar_t *wdn = NULL;
313 LDAPModW **wattrs = NULL;
314 LDAPControlW **wsctrls = NULL;
315 LDAPControlW **wcctrls = NULL;
316
317 if (rc = convert_to_wcs(dn, &wdn) != LDAP_SUCCESS) goto end;
318 if (rc = convert_ctrl_list(sctrls, &wsctrls) != LDAP_SUCCESS) goto end;
319 if (rc = convert_ctrl_list(cctrls, &wcctrls) != LDAP_SUCCESS) goto end;
320 if (rc = convert_mod_list(attrs, &wattrs) != LDAP_SUCCESS) goto end;
321
322 rc = ldap_add_extW(ld, wdn, wattrs, wsctrls, wcctrls, msgidp);
323
324 end:
325 free(wdn);
326 free_list((void **)wsctrls, (void *)free_ctrl);
327 free_list((void **)wcctrls, (void *)free_ctrl);
328 free_list((void **)wattrs, (void *)free_mod);
329
330 return rc;
331 }
332
333 int
334 ldap_modify_extU(LDAP *ld, char *dn, LDAPModA **attrs, LDAPControlA **sctrls, LDAPControlA **cctrls,
335 int *msgidp) {
336
337 int rc = 0;
338 wchar_t *wdn = NULL;
339 LDAPModW **wattrs = NULL;
340 LDAPControlW **wsctrls = NULL;
341 LDAPControlW **wcctrls = NULL;
342
343 if (rc = convert_to_wcs(dn, &wdn) != LDAP_SUCCESS) goto end;
344 if (rc = convert_ctrl_list(sctrls, &wsctrls) != LDAP_SUCCESS) goto end;
345 if (rc = convert_ctrl_list(cctrls, &wcctrls) != LDAP_SUCCESS) goto end;
346 if (rc = convert_mod_list(attrs, &wattrs) != LDAP_SUCCESS) goto end;
347
348 rc = ldap_modify_extW(ld, wdn, wattrs, wsctrls, wcctrls, msgidp);
349
350 end:
351 free(wdn);
352 free_list((void **)wsctrls, (void *)free_ctrl);
353 free_list((void **)wcctrls, (void *)free_ctrl);
354 free_list((void **)wattrs, (void *)free_mod);
355
356 return rc;
357 }
358
359 int
360 ldap_delete_extU(LDAP *ld, char *dn, LDAPControlA **sctrls, LDAPControlA **cctrls, int *msgidp) {
361 int rc = 0;
362 wchar_t *wdn = NULL;
363 LDAPControlW **wsctrls = NULL;
364 LDAPControlW **wcctrls = NULL;
365
366 if (rc = convert_to_wcs(dn, &wdn) != LDAP_SUCCESS) goto end;
367 if (rc = convert_ctrl_list(sctrls, &wsctrls) != LDAP_SUCCESS) goto end;
368 if (rc = convert_ctrl_list(cctrls, &wcctrls) != LDAP_SUCCESS) goto end;
369
370 rc = ldap_delete_extW(ld, wdn, wsctrls, wcctrls, msgidp);
371
372 end:
373 free(wdn);
374 free_list((void **)wsctrls, (void *)free_ctrl);
375 free_list((void **)wcctrls, (void *)free_ctrl);
376
377 return rc;
378 }
379
380 char *
381 ldap_first_attributeU(LDAP *ld, LDAPMessage *entry, BerElement **ber) {
382 char *attr = NULL;
383 wchar_t *wattr = NULL;
384
385 wattr = ldap_first_attributeW(ld, entry, ber);
386
387 convert_to_mbs(wattr, &attr);
388
389 ldap_memfreeW(wattr);
390
391 return attr;
392 }
393
394 char *
395 ldap_next_attributeU(LDAP *ld, LDAPMessage *entry, BerElement *ber) {
396 char *attr = NULL;
397 wchar_t *wattr = NULL;
398
399 wattr = ldap_next_attributeW(ld, entry, ber);
400
401 convert_to_mbs(wattr, &attr);
402
403 ldap_memfreeW(wattr);
404
405 return attr;
406 }
407
408 struct berval **
409 ldap_get_values_lenU(LDAP *ld, LDAPMessage *entry, char *target) {
410 struct berval **ret = NULL;
411 wchar_t *wtarget = NULL;
412
413 convert_to_wcs(target, &wtarget);
414
415 ret = ldap_get_values_lenW(ld, entry, wtarget);
416
417 free(wtarget);
418
419 return ret;
420 }
421
422 int
423 ldap_renameU(LDAP *ld, char *dn, char *newrdn, char *newSuperior, int deleteoldrdn,
424 LDAPControlA **sctrls, LDAPControlA **cctrls, int *msgidp) {
425
426 int rc = 0;
427 wchar_t *wdn = NULL;
428 wchar_t *wnewrdn = NULL;
429 wchar_t *wnewSuperior = NULL;
430 LDAPControlW **wsctrls = NULL;
431 LDAPControlW **wcctrls = NULL;
432
433 if (rc = convert_to_wcs(dn, &wdn) != LDAP_SUCCESS) goto end;
434 if (rc = convert_to_wcs(newrdn, &wnewrdn) != LDAP_SUCCESS) goto end;
435 if (rc = convert_to_wcs(newSuperior, &wnewSuperior) != LDAP_SUCCESS) goto end;
436 if (rc = convert_ctrl_list(sctrls, &wsctrls) != LDAP_SUCCESS) goto end;
437 if (rc = convert_ctrl_list(cctrls, &wcctrls) != LDAP_SUCCESS) goto end;
438
439 rc = ldap_rename_extW(ld, wdn, wnewrdn, wnewSuperior, deleteoldrdn, wsctrls, wcctrls, msgidp);
440
441 end:
442 free(wdn);
443 free(wnewrdn);
444 free(wnewSuperior);
445 free_list((void **)wsctrls, (void *)free_ctrl);
446 free_list((void **)wcctrls, (void *)free_ctrl);
447
448 return rc;
449 }
450
451 int
452 ldap_search_extU(LDAP *ld, char *base, int scope, char *filter, char **attrs, int attrsonly,
453 LDAPControlA **sctrls, LDAPControlA **cctrls, struct timeval *timeout, int sizelimit, int *msgidp) {
454
455 int rc = 0;
456 wchar_t *wbase = NULL;
457 wchar_t *wfilter = NULL;
458 wchar_t **wattrs = NULL;
459 LDAPControlW **wsctrls = NULL;
460 LDAPControlW **wcctrls = NULL;
461 unsigned long timelimit = 0;
462
463 if (timeout != NULL) {
464 timelimit = (unsigned long)timeout->tv_sec;
465 } else {
466 timelimit = 0;
467 }
468
469 if (rc = convert_to_wcs(base, &wbase) != LDAP_SUCCESS) goto end;
470 if (rc = convert_to_wcs(filter, &wfilter) != LDAP_SUCCESS) goto end;
471 if (rc = convert_char_list(attrs, &wattrs) != LDAP_SUCCESS) goto end;
472 if (rc = convert_ctrl_list(sctrls, &wsctrls) != LDAP_SUCCESS) goto end;
473 if (rc = convert_ctrl_list(cctrls, &wcctrls) != LDAP_SUCCESS) goto end;
474
475 rc = ldap_search_extW(ld, wbase, scope, wfilter, wattrs, attrsonly, wsctrls, wcctrls, timelimit,
476 sizelimit, msgidp);
477
478 end:
479 free(wbase);
480 free(wfilter);
481 free_list((void **)wattrs, (void *)free);
482 free_list((void **)wsctrls, (void *)free_ctrl);
483 free_list((void **)wcctrls, (void *)free_ctrl);
484
485 return rc;
486 }
487
488 int
489 ldap_extended_operationU(LDAP *ld, char *reqoid, struct berval *reqdata, LDAPControlA **sctrls,
490 LDAPControlA **cctrls, int *msgidp) {
491
492 int rc = 0;
493 wchar_t *woid = NULL;
494 LDAPControlW **wsctrls = NULL;
495 LDAPControlW **wcctrls = NULL;
496
497 if (rc = convert_to_wcs(reqoid, &woid) != LDAP_SUCCESS) goto end;
498 if (rc = convert_ctrl_list(sctrls, &wsctrls) != LDAP_SUCCESS) goto end;
499 if (rc = convert_ctrl_list(cctrls, &wcctrls) != LDAP_SUCCESS) goto end;
500
501 rc = ldap_extended_operationW(ld, woid, reqdata, wsctrls, wcctrls, msgidp);
502
503 end:
504 free(woid);
505 free_list((void **)wsctrls, (void *)free_ctrl);
506 free_list((void **)wcctrls, (void *)free_ctrl);
507
508 return rc;
509 }
510
511 int
512 ldap_parse_extended_resultU(LDAP *ld, LDAPMessage *res, char **retoidp, struct berval **retdatap,
513 int freeit) {
514
515 int rc = 0;
516 char *oid = NULL;
517 wchar_t *wretoid = NULL;
518
519 rc = ldap_parse_extended_resultW(ld, res, &wretoid, retdatap, freeit);
520 if (rc != LDAP_SUCCESS) return rc;
521 if (rc = convert_to_mbs(wretoid, &oid) != LDAP_SUCCESS) return rc;
522
523 if (wretoid) ldap_memfreeW(wretoid);
524
525 *retoidp = oid;
526
527 return rc;
528 }
529
530 /* This function receive a list of LDAPControl instead of one LDAPControl,
531 because the corresponding WinLDAP function will search the page control
532 object internally. */
533 int
534 ldap_parse_pageresponse_controlU(LDAP *ld, LDAPControlA **ctrls, ber_int_t *count,
535 struct berval **cookie) {
536
537 int rc = 0;
538 LDAPControlW **wctrls = NULL;
539
540 if (rc = convert_ctrl_list(ctrls, &wctrls) != LDAP_SUCCESS) goto end;
541
542 if (*cookie != NULL && (*cookie)->bv_val != NULL) {
543 ber_bvfree(*cookie);
544 }
545
546 rc = ldap_parse_page_controlW(ld, wctrls, (unsigned long *)count, cookie);
547 end:
548 free_list((void **)wctrls, (void *)free_ctrl);
549
550 return rc;
551 }
552
553 /* Create an LDAPControl struct. If `dupval` is non-zero, the berval struct `value`
554 is copied. */
555 int ldap_control_createU(char *requestOID, int iscritical, struct berval *value,
556 int dupval, LDAPControlA **ctrlp) {
557 LDAPControlA *tmpControl = NULL;
558
559 tmpControl = (LDAPControlA *)malloc(sizeof(LDAPControlA));
560 if (tmpControl == NULL) return LDAP_NO_MEMORY;
561
562 tmpControl->ldctl_oid = requestOID;
563 tmpControl->ldctl_iscritical = iscritical;
564
565 if (dupval) {
566 if (value == NULL) {
567 tmpControl->ldctl_value.bv_val = NULL;
568 tmpControl->ldctl_value.bv_len = 0;
569 } else {
570 tmpControl->ldctl_value.bv_val = (char *)malloc(sizeof(char) * value->bv_len);
571 if (tmpControl->ldctl_value.bv_val == NULL) {
572 free(tmpControl);
573 return LDAP_NO_MEMORY;
574 }
575
576 memcpy(tmpControl->ldctl_value.bv_val, value->bv_val, value->bv_len);
577 tmpControl->ldctl_value.bv_len = value->bv_len;
578 }
579 } else {
580 tmpControl->ldctl_value = *value;
581 }
582
583 *ctrlp = tmpControl;
584 return LDAP_SUCCESS;
585 }
586
587 /* This function is a dummy function for keeping compatibility with OpenLDAP. */
588 LDAPControlA **
589 ldap_control_findU(char *oid, LDAPControlA **ctrls, LDAPControlA ***nextctrlp) {
590 return ctrls;
591 }
592
593 int
594 ldap_parse_resultU(LDAP *ld, LDAPMessage *res, int *errcodep, char **matcheddnp, char **errmsgp,
595 char ***referralsp, LDAPControlA ***sctrls, int freeit) {
596
597 int i = 0;
598 int rc = 0;
599 int size = 0;
600 char *err = NULL;
601 char **refs = NULL;
602 wchar_t *wmatcheddnp = NULL;
603 wchar_t *werrmsgp = NULL;
604 wchar_t **wreferralsp = NULL;
605 LDAPControlW **wsctrls = NULL;
606 LDAPControlA **ctrls = NULL;
607 LDAPControlA *ctrla = NULL;
608
609 rc = ldap_parse_resultW(ld, res, errcodep, &wmatcheddnp, &werrmsgp, &wreferralsp, &wsctrls, freeit);
610 if (rc != LDAP_SUCCESS) return rc;
611
612 /* Convert and assign parameters just if they are required. */
613 if (matcheddnp != NULL) rc = convert_to_mbs(wmatcheddnp, matcheddnp);
614 if (errmsgp != NULL) rc = convert_to_mbs(werrmsgp, errmsgp);
615
616 if (wreferralsp != NULL && referralsp != NULL) {
617 /* Copy and convert the referral strings, if it's required. */
618 size = get_size(wreferralsp);
619 refs = (char **)malloc(sizeof(char*) * (size + 1));
620 if (refs == NULL) {
621 rc = LDAP_NO_MEMORY;
622 goto end;
623 }
624 for (i = 0; wreferralsp[i] != NULL; i++) {
625 convert_to_mbs(wreferralsp[i], &refs[i]);
626 }
627 refs[i] = NULL;
628 *referralsp = refs;
629 }
630
631 if (wsctrls != NULL && sctrls != NULL) {
632 /* Copy and convert the server controls, if it's required. */
633 size = get_size(wsctrls);
634 ctrls = (LDAPControlA **)malloc(sizeof(LDAPControlA*) * (size + 1));
635 if (ctrls == NULL) {
636 rc = LDAP_NO_MEMORY;
637 goto end;
638 }
639
640 for (i = 0; wsctrls[i] != NULL; i++) {
641 ctrla = (LDAPControlA *)malloc(sizeof(LDAPControlA));
642 if (ctrla == NULL) return LDAP_NO_MEMORY;
643
644 ctrla->ldctl_iscritical = wsctrls[i]->ldctl_iscritical;
645 rc = convert_to_mbs(wsctrls[i]->ldctl_oid, &(ctrla->ldctl_oid));
646 if (rc != LDAP_SUCCESS) goto end;
647 ctrla->ldctl_value.bv_val = (char *)malloc(wsctrls[i]->ldctl_value.bv_len);
648 if (ctrla->ldctl_value.bv_val == NULL) {
649 rc = LDAP_NO_MEMORY;
650 goto end;
651 }
652 memcpy(ctrla->ldctl_value.bv_val, wsctrls[i]->ldctl_value.bv_val, wsctrls[i]->ldctl_value.bv_len);
653 ctrla->ldctl_value.bv_len = wsctrls[i]->ldctl_value.bv_len;
654 ctrls[i] = ctrla;
655 }
656 ctrls[i] = NULL;
657
658 *sctrls = ctrls;
659 }
660
661 end:
662 ldap_memfreeW(wmatcheddnp);
663 ldap_memfreeW(werrmsgp);
664 ldap_value_freeW(wreferralsp);
665 ldap_controls_freeW(wsctrls);
666
667 return rc;
668 }
669
670 char *
671 ldap_err2stringU(int err) {
672 char *errmsg = NULL;
673 wchar_t *werr = NULL;
674
675 /* Mustn't free the returning string. */
676 werr = ldap_err2stringW(err);
677
678 convert_to_mbs(werr, &errmsg);
679 return errmsg;
680 }
681
682 int
683 ldap_initializeU(LDAP **ldp, char *url) {
684 int rc = 0;
685 int err = 0;
686 int chunk_num = 0;
687 int port = 389;
688 int ssl = 0;
689 int size = 0;
690 char *host = NULL;
691 wchar_t *whost = NULL;
692 char *chunk = NULL;
693 const char *token;
694 char *nxtoken = NULL;
695
696 /* Check for IPv6 address. */
697 if (strchr(url, '[') != NULL) token = "[]";
698 else token = ":/";
699
700 /* Parse string address. */
701 chunk = strtok_s(url, token, &nxtoken);
702 while (chunk != NULL) {
703 switch (chunk_num) {
704 case 0:
705 /* Check scheme. */
706 if (strstr(chunk, "ldaps") != NULL) {
707 ssl = 1;
708 } else {
709 ssl = 0;
710 }
711 chunk_num++;
712 break;
713 case 1:
714 /* Copy hostname. */
715 size = (int)strlen(chunk) + 1;
716 host = (char *)malloc(sizeof(char) * size);
717 if (host == NULL) return LDAP_NO_MEMORY;
718 strcpy_s(host, size, chunk);
719 chunk_num++;
720 break;
721 case 2:
722 /* Convert the port. */
723 if (strcmp(token, "[]") == 0) {
724 /* If it's an IPv6 address the ':' needs to be cut. */
725 chunk += 1;
726 }
727 port = (int)strtol(chunk, NULL, 10);
728 if (port <= 0) {
729 if (host) free(host);
730 return LDAP_PARAM_ERROR;
731 }
732 /* Shortcut: no useful data left. */
733 goto init;
734 default:
735 break;
736 }
737 chunk = strtok_s(NULL, token, &nxtoken);
738 }
739 init:
740 if (rc = convert_to_wcs(host, &whost) != LDAP_SUCCESS) return rc;
741
742 *ldp = ldap_sslinitW(whost, port, ssl);
743
744 free(host);
745 free(whost);
746
747 if (*ldp == NULL) {
748 err = LdapGetLastError();
749 if (err != 0) return err;
750 else return LDAP_LOCAL_ERROR;
751 }
752
753 return ldap_connect(*ldp, NULL);
754 }
755
756 int
757 ldap_start_tls_sU(LDAP *ld, LDAPControlA **sctrls, LDAPControlA **cctrls) {
758 int rc = 0;
759 LDAPControlW **wsctrls = NULL;
760 LDAPControlW **wcctrls = NULL;
761
762 if (rc = convert_ctrl_list(sctrls, &wsctrls) != LDAP_SUCCESS) goto end;
763 if (rc = convert_ctrl_list(cctrls, &wcctrls) != LDAP_SUCCESS) goto end;
764
765 rc = ldap_start_tls_sW(ld, NULL, NULL, wsctrls, wcctrls);
766
767 end:
768 free_list((void **)wsctrls, (void *)free_ctrl);
769 free_list((void **)wcctrls, (void *)free_ctrl);
770
771 return rc;
772 }
773
774 int
775 ldap_simple_bind_sU(LDAP *ld, char *who, char *passwd) {
776 int rc = 0;
777 wchar_t *wwho = NULL;
778 wchar_t *wpsw = NULL;
779
780 if (rc = convert_to_wcs(who, &wwho) != LDAP_SUCCESS) goto end;
781 if (rc = convert_to_wcs(passwd, &wpsw) != LDAP_SUCCESS) goto end;
782
783 rc = ldap_simple_bind_sW(ld, wwho, wpsw);
784
785 end:
786 free(wwho);
787 free(wpsw);
788
789 return rc;
790 }
791
792 /* The manually created LDAPControls (like the ones in the ldap_parse_resultU)
793 can not be freed with original ldap_controls_freeA without causing heap
794 corruption. */
795 void
796 ldap_controls_freeU(LDAPControlA **ctrls) {
797 int i = 0;
798
799 if (ctrls != NULL) {
800 for (i = 0; ctrls[i] != NULL; i++) {
801 if (ctrls[i]->ldctl_oid) free(ctrls[i]->ldctl_oid);
802 if (ctrls[i]->ldctl_value.bv_val != NULL) {
803 free(ctrls[i]->ldctl_value.bv_val);
804 }
805 free(ctrls[i]);
806 }
807 }
808 }
809
810 /* Extended ldap_get_option function with TLS package name and API info
811 support. */
812 int
813 ldap_get_optionU(LDAP *ld, int option, void *outvalue) {
814 if (option == LDAP_OPT_X_TLS_PACKAGE) {
815 *(char **)outvalue = strdup("SChannel");
816 return LDAP_SUCCESS;
817 }
818 if (option == LDAP_OPT_API_INFO) {
819 /* By default ldap_get_option returns with protocol error,
820 simpler to fill out the struct manually. */
821 LDAPAPIInfo *info = (LDAPAPIInfo *)outvalue;
822 if (info == NULL) return LDAP_OTHER;
823
824 info->ldapai_vendor_name = LDAP_VENDOR_NAME;
825 info->ldapai_vendor_version = LDAP_VENDOR_VERSION;
826 info->ldapai_api_version = LDAP_API_VERSION;
827 info->ldapai_protocol_version = LDAP_VERSION3;
828 info->ldapai_extensions = NULL;
829
830 return LDAP_SUCCESS;
831 }
832
833 return ldap_get_optionW(ld, option, outvalue);
834 }
835
836 int
837 ldap_parse_sortresponse_controlU(LDAP *ld, LDAPControlA **ctrls, ber_int_t *result,
838 char **attribute) {
839
840 int rc = 0;
841 wchar_t *wattr = NULL;
842 LDAPControlW **wctrls = NULL;
843
844 if (rc = convert_ctrl_list(ctrls, &wctrls) != LDAP_SUCCESS) goto end;
845
846 rc = ldap_parse_sort_controlW(ld, wctrls, result, &wattr);
847 convert_to_mbs(wattr, attribute);
848 if (rc == LDAP_CONTROL_NOT_FOUND) {
849 rc = LDAP_SUCCESS;
850 }
851 end:
852 free_list((void **)wctrls, (void *)free_ctrl);
853 ldap_memfreeW(wattr);
854 return rc;
855 }
856
857 int
858 ldap_create_vlv_controlU(LDAP *ld, LDAPVLVInfo *vlvinfo, LDAPControlA **ctrl) {
859 return ldap_create_vlv_controlA(ld, vlvinfo, FALSE, ctrl);
860 }
861
862 int
863 ldap_parse_vlvresponse_controlU(LDAP *ld, LDAPControlA **ctrls, long int *target_posp,
864 long int *list_countp, struct berval **contextp, int *errcodep) {
865 int rc = 0;
866 LDAPControlW **wctrls = NULL;
867
868 rc = convert_ctrl_list(ctrls, &wctrls);
869 if (rc != LDAP_SUCCESS) goto end;
870
871 rc = ldap_parse_vlv_controlW(ld, wctrls, target_posp, list_countp, contextp, errcodep);
872
873 end:
874 free_list((void **)wctrls, (void *)free_ctrl);
875 return rc;
876 }
877
878 int
879 ldap_parse_referenceU(LDAP *ld, LDAPMessage *reference, char ***referralsp,
880 LDAPControlA ***serverctrlsp, int freeit) {
881 int rc = 0;
882 LDAPControlW **wctrls = NULL;
883 wchar_t **wreferrals = NULL;
884
885 rc = ldap_parse_referenceW(ld, reference, &wreferrals);
886 if (rc != LDAP_SUCCESS) goto end;
887 rc = convert_wchar_list(wreferrals, referralsp);
888 if (freeit) ldap_msgfree(reference);
889
890 end:
891 ldap_value_freeW(wreferrals);
892 return rc;
893
894 }
895
896 /******************************************************************************
897 Dummy functions for password policy control.
898 WinLDAP fails with protocol error when the empty password policy control is
899 attached to an LDAP operation.
900 *******************************************************************************/
901 int
902 ldap_create_passwordpolicy_controlU(LDAP *ld, LDAPControlA **ctrlp) {
903 return LDAP_SUCCESS;
904 }
905
906 int
907 ldap_parse_passwordpolicy_controlU(LDAP *ld, LDAPControlA **ctrls, ber_int_t *expirep,
908 ber_int_t *gracep, unsigned int *errorp) {
909 return LDAP_CONTROL_NOT_FOUND;
910 }
911
912 /******************************************************************************
913 * The following functions are for SASL binding, using SSPI.
914 ******************************************************************************/
915
916
917 /* Decrypt the message of a GSSAPI (Kerberos) response from the server. */
918 static int
919 decrypt_response(CtxtHandle *handle, char *inToken, int inLen, char **outToken, int *outLen) {
920 SecBufferDesc buff_desc;
921 SecBuffer bufs[2];
922 unsigned long qop = 0;
923 int res;
924
925 buff_desc.ulVersion = SECBUFFER_VERSION;
926 buff_desc.cBuffers = 2;
927 buff_desc.pBuffers = bufs;
928
929 /* This buffer is for SSPI. */
930 bufs[0].BufferType = SECBUFFER_STREAM;
931 bufs[0].pvBuffer = inToken;
932 bufs[0].cbBuffer = inLen;
933
934 /* This buffer holds the application data. */
935 bufs[1].BufferType = SECBUFFER_DATA;
936 bufs[1].cbBuffer = 0;
937 bufs[1].pvBuffer = NULL;
938
939 res = DecryptMessage(handle, &buff_desc, 0, &qop);
940
941 if (res == SEC_E_OK) {
942 int maxlen = bufs[1].cbBuffer;
943 char *p = (char *)malloc(maxlen);
944 *outToken = p;
945 *outLen = maxlen;
946 memcpy(p, bufs[1].pvBuffer, bufs[1].cbBuffer);
947 }
948
949 return res;
950 }
951
952 /* Encrypt the message of a GSSAPI (Kerberos) reply to the server. */
953 static int
954 encrypt_reply(CtxtHandle *handle, char *inToken, int inLen, char **outToken, int *outLen) {
955 SecBufferDesc buff_desc;
956 SecBuffer bufs[3];
957 SecPkgContext_Sizes sizes;
958 int res;
959
960 res = QueryContextAttributesW(handle, SECPKG_ATTR_SIZES, &sizes);
961
962 buff_desc.ulVersion = SECBUFFER_VERSION;
963 buff_desc.cBuffers = 3;
964 buff_desc.pBuffers = bufs;
965
966 /* This buffer is for SSPI. */
967 bufs[0].BufferType = SECBUFFER_TOKEN;
968 bufs[0].cbBuffer = sizes.cbSecurityTrailer;
969 bufs[0].pvBuffer = malloc(sizes.cbSecurityTrailer);
970 if (bufs[0].pvBuffer == NULL) {
971 res = LDAP_NO_MEMORY;
972 goto error;
973 }
974
975 /* This buffer holds the application data. */
976 bufs[1].BufferType = SECBUFFER_DATA;
977 bufs[1].cbBuffer = inLen;
978 bufs[1].pvBuffer = malloc(inLen);
979 if (bufs[1].pvBuffer == NULL) {
980 res = LDAP_NO_MEMORY;
981 goto error;
982 }
983
984 memcpy(bufs[1].pvBuffer, inToken, inLen);
985
986 /* This buffer is for SSPI. */
987 bufs[2].BufferType = SECBUFFER_PADDING;
988 bufs[2].cbBuffer = sizes.cbBlockSize;
989 bufs[2].pvBuffer = malloc(sizes.cbBlockSize);
990 if (bufs[2].pvBuffer == NULL) {
991 res = LDAP_NO_MEMORY;
992 goto error;
993 }
994
995 res = EncryptMessage(handle, SECQOP_WRAP_NO_ENCRYPT, &buff_desc, 0);
996
997 if (res == SEC_E_OK) {
998 int maxlen = bufs[0].cbBuffer + bufs[1].cbBuffer + bufs[2].cbBuffer;
999 char *tokp = (char *)malloc(maxlen);
1000 if (tokp == NULL) {
1001 res = LDAP_NO_MEMORY;
1002 goto error;
1003 }
1004 *outToken = tokp;
1005 *outLen = maxlen;
1006 memcpy(tokp, bufs[0].pvBuffer, bufs[0].cbBuffer);
1007 tokp += bufs[0].cbBuffer;
1008 memcpy(tokp, bufs[1].pvBuffer, bufs[1].cbBuffer);
1009 tokp += bufs[1].cbBuffer;
1010 memcpy(tokp, bufs[2].pvBuffer, bufs[2].cbBuffer);
1011 }
1012 return res;
1013 error:
1014 free(bufs[0].pvBuffer);
1015 free(bufs[1].pvBuffer);
1016 free(bufs[2].pvBuffer);
1017 return res;
1018 }
1019
1020 /* Get maximal message length for certain mechanism. */
1021 static int
1022 init_package(wchar_t *package_name, unsigned long *max_mgslen) {
1023 int rc;
1024 PSecPkgInfo pkginfo;
1025
1026 rc = QuerySecurityPackageInfo(package_name, &pkginfo);
1027 if (rc != SEC_E_OK) {
1028 return -1;
1029 }
1030
1031 *max_mgslen = pkginfo->cbMaxToken;
1032 FreeContextBuffer(pkginfo);
1033 return 0;
1034 }
1035
1036 /* Supplement the DIGEST-MD5 server response with an authorization ID. */
1037 static char *
1038 create_authzid_digest_str(char *authzid, char *chunk, int *length) {
1039 int len = 12;
1040 char *concat = NULL;
1041
1042 if (authzid != NULL) len += (int)strlen(authzid);
1043 if (chunk != NULL) len += (int)strlen(chunk);
1044
1045 concat = (char *)malloc(sizeof(char) * len);
1046 if (concat == NULL) return NULL;
1047
1048 if (sprintf_s(concat, len, "%s,authzid=\"%s\"", chunk, authzid) == -1) {
1049 return NULL;
1050 }
1051
1052 *length = len - 1;
1053 return concat;
1054 }
1055
1056 /* Concat the decrypted GSSAPI server response with an authorization ID. */
1057 static char *
1058 create_authzid_gssapi_str(char *authzid, char *chunk, int chunklen, int *length) {
1059 int len = chunklen;
1060 char *concat = NULL;
1061 char *p;
1062
1063 if (chunk == NULL) return NULL;
1064 if (authzid == NULL) return chunk;
1065
1066 len += (int)strlen(authzid);
1067 concat = (char *)malloc(sizeof(char) * len);
1068 if (concat == NULL) return NULL;
1069 p = concat;
1070
1071 /* Copy the authzid after the server response. */
1072 memcpy(p, chunk, chunklen);
1073 p += chunklen;
1074 memcpy(p, authzid, strlen(authzid));
1075 free(chunk);
1076
1077 *length = len;
1078 return concat;
1079 }
1080
1081 /* Create the credentials handle for the certain authentication. */
1082 static int
1083 create_credentials(CredHandle *hcred, wchar_t *package_name, sasl_defaults_t *defs) {
1084 int rc = 0;
1085 SEC_WINNT_AUTH_IDENTITY_W wincreds;
1086 wchar_t *wauthcid = NULL;
1087 wchar_t *wpasswd = NULL;
1088 wchar_t *wrealm = NULL;
1089
1090 if (strcmp(defs->authcid, "") == 0 && strcmp(defs->passwd, "") == 0) {
1091 /* If authentication id and password are not set, then
1092 use NULL SEC_WINNT_AUTH_IDENTITY for logon user credentials. */
1093 return AcquireCredentialsHandleW(NULL, package_name, SECPKG_CRED_OUTBOUND,
1094 NULL, NULL, NULL, NULL, hcred, NULL);
1095 }
1096
1097 memset(&wincreds, 0, sizeof(wincreds));
1098
1099 if (rc = convert_to_wcs(defs->authcid, &wauthcid) != LDAP_SUCCESS) goto end;
1100 if (rc = convert_to_wcs(defs->passwd, &wpasswd) != LDAP_SUCCESS) goto end;
1101 if (rc = convert_to_wcs(defs->realm, &wrealm) != LDAP_SUCCESS) goto end;
1102
1103 wincreds.User = (unsigned short *)wauthcid;
1104 if (wincreds.User != NULL) wincreds.UserLength = (unsigned long)wcslen(wauthcid);
1105 else wincreds.UserLength = 0;
1106 wincreds.Password = (unsigned short *)wpasswd;
1107 if (wincreds.Password != NULL) wincreds.PasswordLength = (unsigned long)wcslen(wpasswd);
1108 else wincreds.PasswordLength = 0;
1109 wincreds.Domain = (unsigned short *)wrealm;
1110 if (wincreds.Domain != NULL) wincreds.DomainLength = (unsigned long)wcslen(wrealm);
1111 else wincreds.DomainLength = 0;
1112 wincreds.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
1113
1114 rc = AcquireCredentialsHandleW(NULL, package_name, SECPKG_CRED_OUTBOUND, NULL,
1115 &wincreds, NULL, NULL, hcred, NULL);
1116 end:
1117 free(wauthcid);
1118 free(wpasswd);
1119 free(wrealm);
1120 return rc;
1121 }
1122
1123 /* Generate the client context that will be used as a response to the server
1124 during the authetication porcess. */
1125 static int
1126 generate_client_ctxt(CredHandle *hcred, SecHandle *hctxt, wchar_t *package_name,
1127 wchar_t *target_name, sasl_defaults_t *defs, char *input, int inlen,
1128 char *output, int *outlen, int *crypted, char **crypted_msg) {
1129 int rc = 0, newctxt = 0, len = 0, isauthz = 0;
1130 unsigned long ctxtattr;
1131 char *data = NULL;
1132 SecBufferDesc out_buff_desc;
1133 SecBuffer out_buff;
1134 SecBufferDesc in_buff_desc;
1135 SecBuffer in_buff;
1136
1137 if (!input) {
1138 newctxt = 1;
1139
1140 rc = create_credentials(hcred, package_name, defs);
1141 if (rc < 0) goto end;
1142 }
1143
1144 /* Prepare output buffer. */
1145 out_buff_desc.ulVersion = SECBUFFER_VERSION;
1146 out_buff_desc.cBuffers = 1;
1147 out_buff_desc.pBuffers = &out_buff;
1148
1149 out_buff.cbBuffer = *outlen;
1150 out_buff.BufferType = SECBUFFER_TOKEN;
1151 out_buff.pvBuffer = output;
1152
1153 if (!newctxt) {
1154 if (wcscmp(package_name, L"WDigest") == 0 &&
1155 defs->authzid != NULL && strlen(defs->authzid) != 0
1156 && input != NULL) {
1157 /* Authzid for DIGEST-MD5 authentication. */
1158 input[inlen] = '\0';
1159 input = create_authzid_digest_str(defs->authzid, input, &inlen);
1160 if (input == NULL) return -1;
1161 isauthz = 1;
1162 }
1163
1164 /* Prepare intput buffer. */
1165 in_buff_desc.ulVersion = SECBUFFER_VERSION;
1166 in_buff_desc.cBuffers = 1;
1167 in_buff_desc.pBuffers = &in_buff;
1168
1169 in_buff.cbBuffer = inlen;
1170 in_buff.BufferType = SECBUFFER_TOKEN;
1171 in_buff.pvBuffer = input;
1172 }
1173
1174 if (*crypted == 1) {
1175 /* Crypting for Kerberos. */
1176 rc = decrypt_response(hctxt, input, inlen, &data, &len);
1177 data = create_authzid_gssapi_str(defs->authzid, data, len, &len);
1178 if (data == NULL) return LDAP_NO_MEMORY;
1179 rc = encrypt_reply(hctxt, data, len, crypted_msg, outlen);
1180 free(data);
1181 return 1;
1182 } else {
1183 rc = InitializeSecurityContextW(hcred, newctxt ? NULL : hctxt,
1184 target_name, ISC_REQ_MUTUAL_AUTH | ISC_REQ_NO_INTEGRITY,
1185 0, SECURITY_NATIVE_DREP, newctxt ? NULL : &in_buff_desc,
1186 0, hctxt, &out_buff_desc, &ctxtattr, NULL);
1187 if (isauthz) free(input);
1188 }
1189 if (rc < 0) goto end;
1190
1191 if (rc == SEC_E_OK) *crypted = 1;
1192
1193 if ((rc == SEC_I_COMPLETE_NEEDED) || (rc == SEC_I_COMPLETE_AND_CONTINUE)) {
1194 /* Complete token. */
1195 rc = CompleteAuthToken(hctxt, &out_buff_desc);
1196 if (rc < 0) goto end;
1197 }
1198
1199 *outlen = out_buff.cbBuffer;
1200 end:
1201 return rc;
1202 }
1203
1204 /* Get the SPN target name from the LDAP struct's hostname. */
1205 static wchar_t *
1206 get_target_name(LDAP *ld) {
1207 size_t len = 0;
1208 wchar_t *hostname = NULL;
1209 wchar_t *target_name = NULL;
1210
1211 convert_to_wcs(ld->ld_host, &hostname);
1212 if (hostname == NULL) return NULL;
1213
1214 /* The new string starts with ldap/ (5 char) + 1 terminating NULL. */
1215 len = wcslen(hostname) + 6;
1216 target_name = (wchar_t *)malloc(sizeof(wchar_t) * len);
1217 if (target_name == NULL) return NULL;
1218 /* Copy hostname from LDAP struct to create a valid targetName(SPN). */
1219 swprintf_s(target_name, len, L"ldap/%ls", hostname);
1220
1221 return target_name;
1222 }
1223
1224 /* Execute a synchronous SASL binding to the directory server.
1225 No asynchronous function can be used because there is some bug in the
1226 WinLDAP library that makes impossible to process the result of the
1227 server with the ldap_result function. */
1228 int
1229 ldap_sasl_sspi_bind_sU(LDAP *ld, char *dn, char *mechanism, LDAPControlA **sctrls,
1230 LDAPControlA **cctrls, void *defaults) {
1231 int i = 0, rc = 0, maxlen = 0;
1232 int outlen = 0, inlen = 0, crypted = 0;
1233 char *output = NULL, *input = NULL, *crypted_msg = NULL;
1234 wchar_t *package_name = NULL, *target_name = NULL;
1235 wchar_t *wdn = NULL, *wmech = NULL, *error_msg = NULL;
1236 struct berval cred;
1237 struct berval *response = NULL;
1238 LDAPControlW **wsctrls = NULL;
1239 LDAPControlW **wcctrls = NULL;
1240 CredHandle credhandle;
1241 CtxtHandle ctxhandle;
1242 sasl_defaults_t *defs = (sasl_defaults_t *)defaults;
1243 /* Supported mechanisms, order matters. */
1244 char *mechs[] = { "DIGEST-MD5", "NTLM", "GSSAPI", "GSS-SPNEGO", "EXTERNAL", NULL };
1245 wchar_t *secpacks[] = { L"WDigest", L"NTLM", L"Kerberos", L"Negotiate", L"EXTERNAL", NULL };
1246
1247 /* Get security package name from the mechanism. */
1248 for (i = 0; mechs[i] != NULL; i++) {
1249 if (strcmp(mechanism, mechs[i]) == 0) {
1250 package_name = secpacks[i];
1251 break;
1252 }
1253 }
1254 if (package_name == NULL) return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1255
1256 if (rc = convert_to_wcs(dn, &wdn) != LDAP_SUCCESS) goto end;
1257 if (rc = convert_to_wcs(mechanism, &wmech) != LDAP_SUCCESS) goto end;
1258 if (rc = convert_ctrl_list(sctrls, &wsctrls) != LDAP_SUCCESS) goto end;
1259 if (rc = convert_ctrl_list(cctrls, &wcctrls) != LDAP_SUCCESS) goto end;
1260
1261 if (init_package(package_name, &maxlen) != 0) return LDAP_PARAM_ERROR;
1262
1263 output = (char *)malloc(maxlen);
1264 if (output == NULL) return LDAP_NO_MEMORY;
1265 /* Fill with zeros (crtical for DIGEST-MD5). */
1266 memset(output, 0, maxlen);
1267
1268 SecInvalidateHandle(&credhandle);
1269 SecInvalidateHandle(&ctxhandle);
1270
1271 target_name = get_target_name(ld);
1272 if (target_name == NULL) return LDAP_PARAM_ERROR;
1273
1274 if (strcmp(mechanism, "EXTERNAL") == 0) {
1275 /* Set authorization ID for EXTERNAL. */
1276 cred.bv_val = defs->authzid;
1277 if (defs->authzid != NULL) cred.bv_len = (unsigned long)strlen(defs->authzid);
1278 else cred.bv_len = 0;
1279 }
1280
1281 do {
1282 outlen = maxlen;
1283 if (response) {
1284 input = response->bv_val;
1285 inlen = response->bv_len;
1286 } else {
1287 input = NULL;
1288 inlen = 0;
1289 }
1290
1291 rc = generate_client_ctxt(&credhandle, &ctxhandle, package_name,
1292 target_name, defs, input, inlen, output, &outlen, &crypted,
1293 &crypted_msg);
1294 if (rc < 0) {
1295 ldap_set_option(ld, LDAP_OPT_ERROR_NUMBER, &rc);
1296 rc = LDAP_INVALID_CREDENTIALS;
1297 goto end;
1298 }
1299
1300 if (strcmp(mechanism, "EXTERNAL") != 0) {
1301 cred.bv_len = outlen;
1302 if (crypted_msg != NULL) {
1303 cred.bv_val = crypted_msg;
1304 } else {
1305 cred.bv_val = output;
1306 }
1307 }
1308
1309 if (response != NULL) ber_bvfree(response);
1310 rc = ldap_sasl_bind_sW(ld, wdn, wmech, &cred, wsctrls, wcctrls, &response);
1311 if (crypted_msg != NULL) free(crypted_msg);
1312 /* Get the last error code from the LDAP struct. */
1313 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &rc);
1314 } while (rc == LDAP_SASL_BIND_IN_PROGRESS);
1315
1316 end:
1317 free(wdn);
1318 free(wmech);
1319 free(target_name);
1320 free(output);
1321 free_list((void **)wsctrls, (void *)free_ctrl);
1322 free_list((void **)wcctrls, (void *)free_ctrl);
1323 DeleteSecurityContext(&ctxhandle);
1324 FreeCredentialHandle(&credhandle);
1325 return rc;
1326 }
1327
1328 static int WINAPI
1329 ldap_thread_tls(void *thread_data) {
1330 int rc = 0;
1331 ldap_tls_data_t *data = (ldap_tls_data_t *)thread_data;
1332
1333 rc = ldap_start_tls_sU(data->ld, data->serverctrls, data->clientctrls);
1334 free(data);
1335
1336 return rc;
1337 }
1338
1339 int
1340 ldap_start_tlsU(LDAP *ld, LDAPControlA **serverctrls, LDAPControlA **clientctrls, HANDLE *msgidp) {
1341 ldap_tls_data_t *data = NULL;
1342
1343 data = (ldap_tls_data_t *)malloc(sizeof(ldap_tls_data_t));
1344 if (data == NULL) return LDAP_NO_MEMORY;
1345
1346 data->ld = ld;
1347 data->clientctrls = clientctrls;
1348 data->serverctrls = serverctrls;
1349
1350 *msgidp = CreateThread(NULL, 0, ldap_thread_tls, (void *)data, 0, NULL);
1351 if (*msgidp == NULL) return LDAP_LOCAL_ERROR;
1352
1353 return LDAP_SUCCESS;
1354 }
1355
1356 /* Get the optional error message. */
1357 char *
1358 _ldap_get_opt_errormsgU(LDAP *ld) {
1359 int code = 0;
1360 char *opt = NULL;
1361 wchar_t *wopt = NULL;
1362
1363 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &code);
1364
1365 if (code <= 0x80090367L && code >= 0x80090300L) {
1366 opt = (char *)malloc(sizeof(char) * 72);
1367 if (opt == NULL) return opt;
1368 sprintf_s(opt, 72, "SSPI authentication procedure is failed"
1369 " with error code: 0x%x", code);
1370 } else {
1371 /* Get additional error message from the session. */
1372 ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &wopt);
1373 convert_to_mbs(wopt, &opt);
1374 }
1375
1376 return opt;
1377 }
1378
1379 #endif