2 Unix SMB/CIFS implementation.
3 ads (active directory) utility library
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Remus Koos 2001
6 Copyright (C) Jim McDonough 2002
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * @brief basic ldap client-side routines for ads server communications
31 * The routines contained here should do the necessary ldap calls for
34 * Important note: attribute names passed into ads_ routines must
35 * already be in UTF-8 format. We do not convert them because in almost
36 * all cases, they are just ascii (which is represented with the same
37 * codepoints in UTF-8). This may have to change at some point
42 try a connection to a given ldap server, returning True and setting the servers IP
43 in the ads struct if successful
45 static BOOL ads_try_connect(ADS_STRUCT *ads, const char *server, unsigned port)
49 if (!server || !*server) {
53 DEBUG(5,("ads_try_connect: trying ldap server '%s' port %u\n", server, port));
55 /* this copes with inet_ntoa brokenness */
58 ads->ld = ldap_open(srv, port);
63 ads->ldap_port = port;
64 ads->ldap_ip = *interpret_addr2(srv);
71 try a connection to a given ldap server, based on URL, returning True if successful
73 static BOOL ads_try_connect_uri(ADS_STRUCT *ads)
75 #if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
76 DEBUG(5,("ads_try_connect: trying ldap server at URI '%s'\n",
77 ads->server.ldap_uri));
80 if (ldap_initialize((LDAP**)&(ads->ld), ads->server.ldap_uri) == LDAP_SUCCESS) {
83 DEBUG(0, ("ldap_initialize: %s\n", strerror(errno)));
87 DEBUG(1, ("no URL support in LDAP libs!\n"));
93 /* used by the IP comparison function */
99 /* compare 2 ldap IPs by nearness to our interfaces - used in qsort */
100 static int ldap_ip_compare(struct ldap_ip *ip1, struct ldap_ip *ip2)
102 return ip_compare(&ip1->ip, &ip2->ip);
105 /* try connecting to a ldap server via DNS */
106 static BOOL ads_try_dns(ADS_STRUCT *ads)
113 struct ldap_ip *ip_list;
116 c_realm = ads->server.realm;
117 if (!c_realm || !*c_realm) {
118 c_realm = lp_realm();
120 if (!c_realm || !*c_realm) {
121 c_realm = ads->server.workgroup;
123 if (!c_realm || !*c_realm) {
124 c_realm = lp_workgroup();
129 realm = smb_xstrdup(c_realm);
131 DEBUG(6,("ads_try_dns: looking for realm '%s'\n", realm));
132 if (ldap_domain2hostlist(realm, &list) != LDAP_SUCCESS) {
137 DEBUG(6,("ads_try_dns: ldap realm '%s' host list '%s'\n", realm, list));
140 count = count_chars(list, ' ') + 1;
141 ip_list = malloc(count * sizeof(struct ldap_ip));
147 while (next_token(&ptr, tok, " ", sizeof(tok))) {
148 unsigned port = LDAP_PORT;
149 char *p = strchr(tok, ':');
154 ip_list[i].ip = *interpret_addr2(tok);
155 ip_list[i].port = port;
156 if (!is_zero_ip(ip_list[i].ip)) {
164 /* we sort the list of addresses by closeness to our interfaces. This
165 tries to prevent us using a DC on the other side of the country */
167 qsort(ip_list, count, sizeof(struct ldap_ip),
168 QSORT_CAST ldap_ip_compare);
171 for (i=0;i<count;i++) {
172 if (ads_try_connect(ads, inet_ntoa(ip_list[i].ip), ip_list[i].port)) {
182 /* try connecting to a ldap server via netbios */
183 static BOOL ads_try_netbios(ADS_STRUCT *ads)
185 struct in_addr *ip_list, pdc_ip;
188 const char *workgroup = ads->server.workgroup;
192 workgroup = lp_workgroup();
195 DEBUG(6,("ads_try_netbios: looking for workgroup '%s'\n", workgroup));
197 /* try the PDC first */
198 if (get_pdc_ip(workgroup, &pdc_ip)) {
199 DEBUG(6,("ads_try_netbios: trying server '%s'\n",
201 if (ads_try_connect(ads, inet_ntoa(pdc_ip), LDAP_PORT))
205 /* now any DC, including backups */
206 if (get_dc_list(workgroup, &ip_list, &count, &list_ordered)) {
207 for (i=0;i<count;i++) {
208 DEBUG(6,("ads_try_netbios: trying server '%s'\n",
209 inet_ntoa(ip_list[i])));
210 if (ads_try_connect(ads, inet_ntoa(ip_list[i]), LDAP_PORT)) {
222 * Connect to the LDAP server
223 * @param ads Pointer to an existing ADS_STRUCT
224 * @return status of connection
226 ADS_STATUS ads_connect(ADS_STRUCT *ads)
228 int version = LDAP_VERSION3;
231 ads->last_attempt = time(NULL);
234 /* try with a URL based server */
236 if (ads->server.ldap_uri &&
237 ads_try_connect_uri(ads)) {
241 /* try with a user specified server */
242 if (ads->server.ldap_server &&
243 ads_try_connect(ads, ads->server.ldap_server, LDAP_PORT)) {
247 /* try with a smb.conf ads server setting if we are connecting
248 to the primary workgroup or realm */
249 if (!ads->server.foreign &&
250 ads_try_connect(ads, lp_ads_server(), LDAP_PORT)) {
255 if (ads_try_dns(ads)) {
259 /* try via netbios lookups */
260 if (!lp_disable_netbios() && ads_try_netbios(ads)) {
264 return ADS_ERROR_SYSTEM(errno?errno:ENOENT);
267 DEBUG(3,("Connected to LDAP server %s\n", inet_ntoa(ads->ldap_ip)));
269 status = ads_server_info(ads);
270 if (!ADS_ERR_OK(status)) {
271 DEBUG(1,("Failed to get ldap server info\n"));
275 ldap_set_option(ads->ld, LDAP_OPT_PROTOCOL_VERSION, &version);
277 if (!ads->auth.user_name) {
278 /* by default use the machine account */
280 fstrcpy(myname, global_myname());
282 asprintf(&ads->auth.user_name, "HOST/%s", myname);
285 if (!ads->auth.realm) {
286 ads->auth.realm = strdup(ads->config.realm);
289 if (!ads->auth.kdc_server) {
290 ads->auth.kdc_server = strdup(inet_ntoa(ads->ldap_ip));
294 /* this is a really nasty hack to avoid ADS DNS problems. It needs a patch
295 to MIT kerberos to work (tridge) */
298 asprintf(&env, "KRB5_KDC_ADDRESS_%s", ads->config.realm);
299 setenv(env, ads->auth.kdc_server, 1);
304 if (ads->auth.flags & ADS_AUTH_NO_BIND) {
308 if (ads->auth.flags & ADS_AUTH_ANON_BIND) {
309 return ADS_ERROR(ldap_simple_bind_s( ads->ld, NULL, NULL));
312 if (ads->auth.flags & ADS_AUTH_SIMPLE_BIND) {
313 return ADS_ERROR(ldap_simple_bind_s( ads->ld, ads->auth.user_name, ads->auth.password));
316 return ads_sasl_bind(ads);
320 Duplicate a struct berval into talloc'ed memory
322 static struct berval *dup_berval(TALLOC_CTX *ctx, const struct berval *in_val)
324 struct berval *value;
326 if (!in_val) return NULL;
328 value = talloc_zero(ctx, sizeof(struct berval));
331 if (in_val->bv_len == 0) return value;
333 value->bv_len = in_val->bv_len;
334 value->bv_val = talloc_memdup(ctx, in_val->bv_val, in_val->bv_len);
339 Make a values list out of an array of (struct berval *)
341 static struct berval **ads_dup_values(TALLOC_CTX *ctx,
342 const struct berval **in_vals)
344 struct berval **values;
347 if (!in_vals) return NULL;
348 for (i=0; in_vals[i]; i++); /* count values */
349 values = (struct berval **) talloc_zero(ctx,
350 (i+1)*sizeof(struct berval *));
351 if (!values) return NULL;
353 for (i=0; in_vals[i]; i++) {
354 values[i] = dup_berval(ctx, in_vals[i]);
360 UTF8-encode a values list out of an array of (char *)
362 static char **ads_push_strvals(TALLOC_CTX *ctx, const char **in_vals)
367 if (!in_vals) return NULL;
368 for (i=0; in_vals[i]; i++); /* count values */
369 values = (char ** ) talloc_zero(ctx, (i+1)*sizeof(char *));
370 if (!values) return NULL;
372 for (i=0; in_vals[i]; i++) {
373 push_utf8_talloc(ctx, &values[i], in_vals[i]);
379 Pull a (char *) array out of a UTF8-encoded values list
381 static char **ads_pull_strvals(TALLOC_CTX *ctx, const char **in_vals)
386 if (!in_vals) return NULL;
387 for (i=0; in_vals[i]; i++); /* count values */
388 values = (char **) talloc_zero(ctx, (i+1)*sizeof(char *));
389 if (!values) return NULL;
391 for (i=0; in_vals[i]; i++) {
392 pull_utf8_talloc(ctx, &values[i], in_vals[i]);
398 * Do a search with paged results. cookie must be null on the first
399 * call, and then returned on each subsequent call. It will be null
400 * again when the entire search is complete
401 * @param ads connection to ads server
402 * @param bind_path Base dn for the search
403 * @param scope Scope of search (LDAP_BASE | LDAP_ONE | LDAP_SUBTREE)
404 * @param exp Search expression - specified in local charset
405 * @param attrs Attributes to retrieve - specified in utf8 or ascii
406 * @param res ** which will contain results - free res* with ads_msgfree()
407 * @param count Number of entries retrieved on this page
408 * @param cookie The paged results cookie to be returned on subsequent calls
409 * @return status of search
411 ADS_STATUS ads_do_paged_search(ADS_STRUCT *ads, const char *bind_path,
412 int scope, const char *exp,
413 const char **attrs, void **res,
414 int *count, void **cookie)
417 char *utf8_exp, *utf8_path, **search_attrs;
418 LDAPControl PagedResults, NoReferrals, *controls[3], **rcontrols;
419 BerElement *cookie_be = NULL;
420 struct berval *cookie_bv= NULL;
425 if (!(ctx = talloc_init("ads_do_paged_search")))
426 return ADS_ERROR(LDAP_NO_MEMORY);
428 /* 0 means the conversion worked but the result was empty
429 so we only fail if it's negative. In any case, it always
430 at least nulls out the dest */
431 if ((push_utf8_talloc(ctx, &utf8_exp, exp) < 0) ||
432 (push_utf8_talloc(ctx, &utf8_path, bind_path) < 0)) {
437 if (!attrs || !(*attrs))
440 /* This would be the utf8-encoded version...*/
441 /* if (!(search_attrs = ads_push_strvals(ctx, attrs))) */
442 if (!(str_list_copy(&search_attrs, attrs))) {
449 /* Paged results only available on ldap v3 or later */
450 ldap_get_option(ads->ld, LDAP_OPT_PROTOCOL_VERSION, &version);
451 if (version < LDAP_VERSION3) {
452 rc = LDAP_NOT_SUPPORTED;
456 cookie_be = ber_alloc_t(LBER_USE_DER);
457 if (cookie && *cookie) {
458 ber_printf(cookie_be, "{iO}", (ber_int_t) 1000, *cookie);
459 ber_bvfree(*cookie); /* don't need it from last time */
462 ber_printf(cookie_be, "{io}", (ber_int_t) 1000, "", 0);
464 ber_flatten(cookie_be, &cookie_bv);
465 PagedResults.ldctl_oid = ADS_PAGE_CTL_OID;
466 PagedResults.ldctl_iscritical = (char) 1;
467 PagedResults.ldctl_value.bv_len = cookie_bv->bv_len;
468 PagedResults.ldctl_value.bv_val = cookie_bv->bv_val;
470 NoReferrals.ldctl_oid = ADS_NO_REFERRALS_OID;
471 NoReferrals.ldctl_iscritical = (char) 0;
472 NoReferrals.ldctl_value.bv_len = 0;
473 NoReferrals.ldctl_value.bv_val = "";
476 controls[0] = &NoReferrals;
477 controls[1] = &PagedResults;
482 /* we need to disable referrals as the openldap libs don't
483 handle them and paged results at the same time. Using them
484 together results in the result record containing the server
485 page control being removed from the result list (tridge/jmcd)
487 leaving this in despite the control that says don't generate
488 referrals, in case the server doesn't support it (jmcd)
490 ldap_set_option(ads->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
492 rc = ldap_search_ext_s(ads->ld, utf8_path, scope, utf8_exp,
493 search_attrs, 0, controls,
494 NULL, NULL, LDAP_NO_LIMIT, (LDAPMessage **)res);
496 ber_free(cookie_be, 1);
497 ber_bvfree(cookie_bv);
500 DEBUG(3,("ldap_search_ext_s(%s) -> %s\n", exp, ldap_err2string(rc)));
504 rc = ldap_parse_result(ads->ld, *res, NULL, NULL, NULL,
505 NULL, &rcontrols, 0);
511 for (i=0; rcontrols[i]; i++) {
512 if (strcmp(ADS_PAGE_CTL_OID, rcontrols[i]->ldctl_oid) == 0) {
513 cookie_be = ber_init(&rcontrols[i]->ldctl_value);
514 ber_scanf(cookie_be,"{iO}", (ber_int_t *) count,
516 /* the berval is the cookie, but must be freed when
518 if (cookie_bv->bv_len) /* still more to do */
519 *cookie=ber_bvdup(cookie_bv);
522 ber_bvfree(cookie_bv);
523 ber_free(cookie_be, 1);
527 ldap_controls_free(rcontrols);
531 /* if/when we decide to utf8-encode attrs, take out this next line */
532 str_list_free(&search_attrs);
534 return ADS_ERROR(rc);
539 * Get all results for a search. This uses ads_do_paged_search() to return
540 * all entries in a large search.
541 * @param ads connection to ads server
542 * @param bind_path Base dn for the search
543 * @param scope Scope of search (LDAP_BASE | LDAP_ONE | LDAP_SUBTREE)
544 * @param exp Search expression
545 * @param attrs Attributes to retrieve
546 * @param res ** which will contain results - free res* with ads_msgfree()
547 * @return status of search
549 ADS_STATUS ads_do_search_all(ADS_STRUCT *ads, const char *bind_path,
550 int scope, const char *exp,
551 const char **attrs, void **res)
557 status = ads_do_paged_search(ads, bind_path, scope, exp, attrs, res,
560 if (!ADS_ERR_OK(status)) return status;
565 LDAPMessage *msg, *next;
567 status2 = ads_do_paged_search(ads, bind_path, scope, exp,
568 attrs, &res2, &count, &cookie);
570 if (!ADS_ERR_OK(status2)) break;
572 /* this relies on the way that ldap_add_result_entry() works internally. I hope
573 that this works on all ldap libs, but I have only tested with openldap */
574 for (msg = ads_first_entry(ads, res2); msg; msg = next) {
575 next = ads_next_entry(ads, msg);
576 ldap_add_result_entry((LDAPMessage **)res, msg);
578 /* note that we do not free res2, as the memory is now
579 part of the main returned list */
586 * Run a function on all results for a search. Uses ads_do_paged_search() and
587 * runs the function as each page is returned, using ads_process_results()
588 * @param ads connection to ads server
589 * @param bind_path Base dn for the search
590 * @param scope Scope of search (LDAP_BASE | LDAP_ONE | LDAP_SUBTREE)
591 * @param exp Search expression - specified in local charset
592 * @param attrs Attributes to retrieve - specified in UTF-8 or ascii
593 * @param fn Function which takes attr name, values list, and data_area
594 * @param data_area Pointer which is passed to function on each call
595 * @return status of search
597 ADS_STATUS ads_do_search_all_fn(ADS_STRUCT *ads, const char *bind_path,
598 int scope, const char *exp, const char **attrs,
599 BOOL(*fn)(char *, void **, void *),
607 status = ads_do_paged_search(ads, bind_path, scope, exp, attrs, &res,
610 if (!ADS_ERR_OK(status)) return status;
612 ads_process_results(ads, res, fn, data_area);
613 ads_msgfree(ads, res);
616 status = ads_do_paged_search(ads, bind_path, scope, exp, attrs,
617 &res, &count, &cookie);
619 if (!ADS_ERR_OK(status)) break;
621 ads_process_results(ads, res, fn, data_area);
622 ads_msgfree(ads, res);
629 * Do a search with a timeout.
630 * @param ads connection to ads server
631 * @param bind_path Base dn for the search
632 * @param scope Scope of search (LDAP_BASE | LDAP_ONE | LDAP_SUBTREE)
633 * @param exp Search expression
634 * @param attrs Attributes to retrieve
635 * @param res ** which will contain results - free res* with ads_msgfree()
636 * @return status of search
638 ADS_STATUS ads_do_search(ADS_STRUCT *ads, const char *bind_path, int scope,
640 const char **attrs, void **res)
642 struct timeval timeout;
644 char *utf8_exp, *utf8_path, **search_attrs = NULL;
647 if (!(ctx = talloc_init("ads_do_search"))) {
648 DEBUG(1,("ads_do_search: talloc_init() failed!"));
649 return ADS_ERROR(LDAP_NO_MEMORY);
652 /* 0 means the conversion worked but the result was empty
653 so we only fail if it's negative. In any case, it always
654 at least nulls out the dest */
655 if ((push_utf8_talloc(ctx, &utf8_exp, exp) < 0) ||
656 (push_utf8_talloc(ctx, &utf8_path, bind_path) < 0)) {
657 DEBUG(1,("ads_do_search: push_utf8_talloc() failed!"));
662 if (!attrs || !(*attrs))
665 /* This would be the utf8-encoded version...*/
666 /* if (!(search_attrs = ads_push_strvals(ctx, attrs))) */
667 if (!(str_list_copy(&search_attrs, attrs)))
669 DEBUG(1,("ads_do_search: str_list_copy() failed!"));
675 timeout.tv_sec = ADS_SEARCH_TIMEOUT;
679 /* see the note in ads_do_paged_search - we *must* disable referrals */
680 ldap_set_option(ads->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
682 rc = ldap_search_ext_s(ads->ld, utf8_path, scope, utf8_exp,
683 search_attrs, 0, NULL, NULL,
684 &timeout, LDAP_NO_LIMIT, (LDAPMessage **)res);
686 if (rc == LDAP_SIZELIMIT_EXCEEDED) {
687 DEBUG(3,("Warning! sizelimit exceeded in ldap. Truncating.\n"));
693 /* if/when we decide to utf8-encode attrs, take out this next line */
694 str_list_free(&search_attrs);
695 return ADS_ERROR(rc);
698 * Do a general ADS search
699 * @param ads connection to ads server
700 * @param res ** which will contain results - free res* with ads_msgfree()
701 * @param exp Search expression
702 * @param attrs Attributes to retrieve
703 * @return status of search
705 ADS_STATUS ads_search(ADS_STRUCT *ads, void **res,
709 return ads_do_search(ads, ads->config.bind_path, LDAP_SCOPE_SUBTREE,
714 * Do a search on a specific DistinguishedName
715 * @param ads connection to ads server
716 * @param res ** which will contain results - free res* with ads_msgfree()
717 * @param dn DistinguishName to search
718 * @param attrs Attributes to retrieve
719 * @return status of search
721 ADS_STATUS ads_search_dn(ADS_STRUCT *ads, void **res,
725 return ads_do_search(ads, dn, LDAP_SCOPE_BASE, "(objectclass=*)", attrs, res);
729 * Free up memory from a ads_search
730 * @param ads connection to ads server
731 * @param msg Search results to free
733 void ads_msgfree(ADS_STRUCT *ads, void *msg)
740 * Free up memory from various ads requests
741 * @param ads connection to ads server
742 * @param mem Area to free
744 void ads_memfree(ADS_STRUCT *ads, void *mem)
750 * Get a dn from search results
751 * @param ads connection to ads server
752 * @param res Search results
755 char *ads_get_dn(ADS_STRUCT *ads, void *res)
757 char *utf8_dn, *unix_dn;
759 utf8_dn = ldap_get_dn(ads->ld, res);
760 pull_utf8_allocate((void **) &unix_dn, utf8_dn);
761 ldap_memfree(utf8_dn);
766 * Find a machine account given a hostname
767 * @param ads connection to ads server
768 * @param res ** which will contain results - free res* with ads_msgfree()
769 * @param host Hostname to search for
770 * @return status of search
772 ADS_STATUS ads_find_machine_acct(ADS_STRUCT *ads, void **res, const char *host)
776 const char *attrs[] = {"*", "nTSecurityDescriptor", NULL};
778 /* the easiest way to find a machine account anywhere in the tree
779 is to look for hostname$ */
780 if (asprintf(&exp, "(samAccountName=%s$)", host) == -1) {
781 DEBUG(1, ("asprintf failed!\n"));
782 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
785 status = ads_search(ads, res, exp, attrs);
791 * Initialize a list of mods to be used in a modify request
792 * @param ctx An initialized TALLOC_CTX
793 * @return allocated ADS_MODLIST
795 ADS_MODLIST ads_init_mods(TALLOC_CTX *ctx)
797 #define ADS_MODLIST_ALLOC_SIZE 10
800 if ((mods = (LDAPMod **) talloc_zero(ctx, sizeof(LDAPMod *) *
801 (ADS_MODLIST_ALLOC_SIZE + 1))))
802 /* -1 is safety to make sure we don't go over the end.
803 need to reset it to NULL before doing ldap modify */
804 mods[ADS_MODLIST_ALLOC_SIZE] = (LDAPMod *) -1;
811 add an attribute to the list, with values list already constructed
813 static ADS_STATUS ads_modlist_add(TALLOC_CTX *ctx, ADS_MODLIST *mods,
814 int mod_op, const char *name,
818 LDAPMod **modlist = (LDAPMod **) *mods;
823 mod_op = LDAP_MOD_DELETE;
825 if (mod_op & LDAP_MOD_BVALUES)
826 values = (void **) ads_dup_values(ctx,
827 (const struct berval **)invals);
829 values = (void **) ads_push_strvals(ctx,
830 (const char **) invals);
833 /* find the first empty slot */
834 for (curmod=0; modlist[curmod] && modlist[curmod] != (LDAPMod *) -1;
836 if (modlist[curmod] == (LDAPMod *) -1) {
837 if (!(modlist = talloc_realloc(ctx, modlist,
838 (curmod+ADS_MODLIST_ALLOC_SIZE+1)*sizeof(LDAPMod *))))
839 return ADS_ERROR(LDAP_NO_MEMORY);
840 memset(&modlist[curmod], 0,
841 ADS_MODLIST_ALLOC_SIZE*sizeof(LDAPMod *));
842 modlist[curmod+ADS_MODLIST_ALLOC_SIZE] = (LDAPMod *) -1;
846 if (!(modlist[curmod] = talloc_zero(ctx, sizeof(LDAPMod))))
847 return ADS_ERROR(LDAP_NO_MEMORY);
848 modlist[curmod]->mod_type = talloc_strdup(ctx, name);
849 if (mod_op & LDAP_MOD_BVALUES)
850 modlist[curmod]->mod_bvalues = (struct berval **) values;
852 modlist[curmod]->mod_values = (char **) values;
853 modlist[curmod]->mod_op = mod_op;
854 return ADS_ERROR(LDAP_SUCCESS);
858 * Add a single string value to a mod list
859 * @param ctx An initialized TALLOC_CTX
860 * @param mods An initialized ADS_MODLIST
861 * @param name The attribute name to add
862 * @param val The value to add - NULL means DELETE
863 * @return ADS STATUS indicating success of add
865 ADS_STATUS ads_mod_str(TALLOC_CTX *ctx, ADS_MODLIST *mods,
866 const char *name, const char *val)
868 const char *values[2];
874 return ads_modlist_add(ctx, mods, LDAP_MOD_DELETE, name, NULL);
875 return ads_modlist_add(ctx, mods, LDAP_MOD_REPLACE, name,
876 (const void **) values);
880 * Add an array of string values to a mod list
881 * @param ctx An initialized TALLOC_CTX
882 * @param mods An initialized ADS_MODLIST
883 * @param name The attribute name to add
884 * @param vals The array of string values to add - NULL means DELETE
885 * @return ADS STATUS indicating success of add
887 ADS_STATUS ads_mod_strlist(TALLOC_CTX *ctx, ADS_MODLIST *mods,
888 const char *name, const char **vals)
891 return ads_modlist_add(ctx, mods, LDAP_MOD_DELETE, name, NULL);
892 return ads_modlist_add(ctx, mods, LDAP_MOD_REPLACE,
893 name, (const void **) vals);
897 * Add a single ber-encoded value to a mod list
898 * @param ctx An initialized TALLOC_CTX
899 * @param mods An initialized ADS_MODLIST
900 * @param name The attribute name to add
901 * @param val The value to add - NULL means DELETE
902 * @return ADS STATUS indicating success of add
904 static ADS_STATUS ads_mod_ber(TALLOC_CTX *ctx, ADS_MODLIST *mods,
905 const char *name, const struct berval *val)
907 const struct berval *values[2];
912 return ads_modlist_add(ctx, mods, LDAP_MOD_DELETE, name, NULL);
913 return ads_modlist_add(ctx, mods, LDAP_MOD_REPLACE|LDAP_MOD_BVALUES,
914 name, (const void **) values);
918 * Perform an ldap modify
919 * @param ads connection to ads server
920 * @param mod_dn DistinguishedName to modify
921 * @param mods list of modifications to perform
922 * @return status of modify
924 ADS_STATUS ads_gen_mod(ADS_STRUCT *ads, const char *mod_dn, ADS_MODLIST mods)
927 char *utf8_dn = NULL;
929 this control is needed to modify that contains a currently
930 non-existent attribute (but allowable for the object) to run
932 LDAPControl PermitModify = {
933 ADS_PERMIT_MODIFY_OID,
936 LDAPControl *controls[2];
938 controls[0] = &PermitModify;
941 if (push_utf8_allocate(&utf8_dn, mod_dn) == -1) {
942 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
945 /* find the end of the list, marked by NULL or -1 */
946 for(i=0;(mods[i]!=0)&&(mods[i]!=(LDAPMod *) -1);i++);
947 /* make sure the end of the list is NULL */
949 ret = ldap_modify_ext_s(ads->ld, utf8_dn,
950 (LDAPMod **) mods, controls, NULL);
952 return ADS_ERROR(ret);
956 * Perform an ldap add
957 * @param ads connection to ads server
958 * @param new_dn DistinguishedName to add
959 * @param mods list of attributes and values for DN
960 * @return status of add
962 ADS_STATUS ads_gen_add(ADS_STRUCT *ads, const char *new_dn, ADS_MODLIST mods)
965 char *utf8_dn = NULL;
967 if (push_utf8_allocate(&utf8_dn, new_dn) == -1) {
968 DEBUG(1, ("ads_gen_add: push_utf8_allocate failed!"));
969 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
972 /* find the end of the list, marked by NULL or -1 */
973 for(i=0;(mods[i]!=0)&&(mods[i]!=(LDAPMod *) -1);i++);
974 /* make sure the end of the list is NULL */
977 ret = ldap_add_s(ads->ld, utf8_dn, mods);
979 return ADS_ERROR(ret);
983 * Delete a DistinguishedName
984 * @param ads connection to ads server
985 * @param new_dn DistinguishedName to delete
986 * @return status of delete
988 ADS_STATUS ads_del_dn(ADS_STRUCT *ads, char *del_dn)
991 char *utf8_dn = NULL;
992 if (push_utf8_allocate(&utf8_dn, del_dn) == -1) {
993 DEBUG(1, ("ads_del_dn: push_utf8_allocate failed!"));
994 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
997 ret = ldap_delete(ads->ld, utf8_dn);
998 return ADS_ERROR(ret);
1002 * Build an org unit string
1003 * if org unit is Computers or blank then assume a container, otherwise
1004 * assume a \ separated list of organisational units
1005 * @param org_unit Organizational unit
1006 * @return org unit string - caller must free
1008 char *ads_ou_string(const char *org_unit)
1010 if (!org_unit || !*org_unit || strcasecmp(org_unit, "Computers") == 0) {
1011 return strdup("cn=Computers");
1014 return ads_build_path(org_unit, "\\/", "ou=", 1);
1020 add a machine account to the ADS server
1022 static ADS_STATUS ads_add_machine_acct(ADS_STRUCT *ads, const char *hostname,
1023 const char *org_unit)
1026 char *host_spn, *host_upn, *new_dn, *samAccountName, *controlstr;
1030 const char *objectClass[] = {"top", "person", "organizationalPerson",
1031 "user", "computer", NULL};
1032 const char *servicePrincipalName[5] = {NULL, NULL, NULL, NULL, NULL};
1034 unsigned acct_control;
1036 if (!(ctx = talloc_init("machine_account")))
1037 return ADS_ERROR(LDAP_NO_MEMORY);
1039 ret = ADS_ERROR(LDAP_NO_MEMORY);
1041 if (!(host_spn = talloc_asprintf(ctx, "HOST/%s", hostname)))
1043 if (!(host_upn = talloc_asprintf(ctx, "%s@%s", host_spn, ads->config.realm)))
1045 ou_str = ads_ou_string(org_unit);
1047 DEBUG(1, ("ads_ou_string returned NULL (malloc failure?)\n"));
1050 new_dn = talloc_asprintf(ctx, "cn=%s,%s,%s", hostname, ou_str,
1051 ads->config.bind_path);
1052 servicePrincipalName[0] = talloc_asprintf(ctx, "HOST/%s", hostname);
1053 psp = talloc_asprintf(ctx, "HOST/%s.%s",
1057 servicePrincipalName[1] = psp;
1058 servicePrincipalName[2] = talloc_asprintf(ctx, "CIFS/%s", hostname);
1059 psp2 = talloc_asprintf(ctx, "CIFS/%s.%s",
1063 servicePrincipalName[3] = psp2;
1069 if (!(samAccountName = talloc_asprintf(ctx, "%s$", hostname)))
1072 acct_control = UF_WORKSTATION_TRUST_ACCOUNT | UF_DONT_EXPIRE_PASSWD;
1073 #ifndef ENCTYPE_ARCFOUR_HMAC
1074 acct_control |= UF_USE_DES_KEY_ONLY;
1076 if (!(controlstr = talloc_asprintf(ctx, "%u", acct_control)))
1079 if (!(mods = ads_init_mods(ctx)))
1082 ads_mod_str(ctx, &mods, "cn", hostname);
1083 ads_mod_str(ctx, &mods, "sAMAccountName", samAccountName);
1084 ads_mod_strlist(ctx, &mods, "objectClass", objectClass);
1085 ads_mod_str(ctx, &mods, "userPrincipalName", host_upn);
1086 ads_mod_strlist(ctx, &mods, "servicePrincipalName", servicePrincipalName);
1087 ads_mod_str(ctx, &mods, "dNSHostName", hostname);
1088 ads_mod_str(ctx, &mods, "userAccountControl", controlstr);
1089 ads_mod_str(ctx, &mods, "operatingSystem", "Samba");
1090 ads_mod_str(ctx, &mods, "operatingSystemVersion", VERSION);
1092 ads_gen_add(ads, new_dn, mods);
1093 ret = ads_set_machine_sd(ads, hostname, new_dn);
1096 talloc_destroy(ctx);
1101 dump a binary result from ldap
1103 static void dump_binary(const char *field, struct berval **values)
1106 for (i=0; values[i]; i++) {
1107 printf("%s: ", field);
1108 for (j=0; j<values[i]->bv_len; j++) {
1109 printf("%02X", (unsigned char)values[i]->bv_val[j]);
1122 static void dump_guid(const char *field, struct berval **values)
1126 for (i=0; values[i]; i++) {
1127 memcpy(guid.info, values[i]->bv_val, sizeof(guid.info));
1128 printf("%s: %s\n", field, uuid_string_static(guid));
1133 dump a sid result from ldap
1135 static void dump_sid(const char *field, struct berval **values)
1138 for (i=0; values[i]; i++) {
1140 sid_parse(values[i]->bv_val, values[i]->bv_len, &sid);
1141 printf("%s: %s\n", field, sid_string_static(&sid));
1146 dump ntSecurityDescriptor
1148 static void dump_sd(const char *filed, struct berval **values)
1153 TALLOC_CTX *ctx = 0;
1155 if (!(ctx = talloc_init("sec_io_desc")))
1159 prs_init(&ps, values[0]->bv_len, ctx, UNMARSHALL);
1160 prs_append_data(&ps, values[0]->bv_val, values[0]->bv_len);
1164 if (!sec_io_desc("sd", &psd, &ps, 1)) {
1166 talloc_destroy(ctx);
1169 if (psd) ads_disp_sd(psd);
1172 talloc_destroy(ctx);
1176 dump a string result from ldap
1178 static void dump_string(const char *field, char **values)
1181 for (i=0; values[i]; i++) {
1182 printf("%s: %s\n", field, values[i]);
1187 dump a field from LDAP on stdout
1191 static BOOL ads_dump_field(char *field, void **values, void *data_area)
1196 void (*handler)(const char *, struct berval **);
1198 {"objectGUID", False, dump_guid},
1199 {"nTSecurityDescriptor", False, dump_sd},
1200 {"dnsRecord", False, dump_binary},
1201 {"objectSid", False, dump_sid},
1206 if (!field) { /* must be end of an entry */
1211 for (i=0; handlers[i].name; i++) {
1212 if (StrCaseCmp(handlers[i].name, field) == 0) {
1213 if (!values) /* first time, indicate string or not */
1214 return handlers[i].string;
1215 handlers[i].handler(field, (struct berval **) values);
1219 if (!handlers[i].name) {
1220 if (!values) /* first time, indicate string conversion */
1222 dump_string(field, (char **)values);
1228 * Dump a result from LDAP on stdout
1229 * used for debugging
1230 * @param ads connection to ads server
1231 * @param res Results to dump
1234 void ads_dump(ADS_STRUCT *ads, void *res)
1236 ads_process_results(ads, res, ads_dump_field, NULL);
1240 * Walk through results, calling a function for each entry found.
1241 * The function receives a field name, a berval * array of values,
1242 * and a data area passed through from the start. The function is
1243 * called once with null for field and values at the end of each
1245 * @param ads connection to ads server
1246 * @param res Results to process
1247 * @param fn Function for processing each result
1248 * @param data_area user-defined area to pass to function
1250 void ads_process_results(ADS_STRUCT *ads, void *res,
1251 BOOL(*fn)(char *, void **, void *),
1257 if (!(ctx = talloc_init("ads_process_results")))
1260 for (msg = ads_first_entry(ads, res); msg;
1261 msg = ads_next_entry(ads, msg)) {
1265 for (utf8_field=ldap_first_attribute(ads->ld,
1266 (LDAPMessage *)msg,&b);
1268 utf8_field=ldap_next_attribute(ads->ld,
1269 (LDAPMessage *)msg,b)) {
1270 struct berval **ber_vals;
1271 char **str_vals, **utf8_vals;
1275 pull_utf8_talloc(ctx, &field, utf8_field);
1276 string = fn(field, NULL, data_area);
1279 utf8_vals = ldap_get_values(ads->ld,
1280 (LDAPMessage *)msg, field);
1281 str_vals = ads_pull_strvals(ctx,
1282 (const char **) utf8_vals);
1283 fn(field, (void **) str_vals, data_area);
1284 ldap_value_free(utf8_vals);
1286 ber_vals = ldap_get_values_len(ads->ld,
1287 (LDAPMessage *)msg, field);
1288 fn(field, (void **) ber_vals, data_area);
1290 ldap_value_free_len(ber_vals);
1292 ldap_memfree(utf8_field);
1295 talloc_destroy_pool(ctx);
1296 fn(NULL, NULL, data_area); /* completed an entry */
1299 talloc_destroy(ctx);
1303 * count how many replies are in a LDAPMessage
1304 * @param ads connection to ads server
1305 * @param res Results to count
1306 * @return number of replies
1308 int ads_count_replies(ADS_STRUCT *ads, void *res)
1310 return ldap_count_entries(ads->ld, (LDAPMessage *)res);
1314 * Join a machine to a realm
1315 * Creates the machine account and sets the machine password
1316 * @param ads connection to ads server
1317 * @param hostname name of host to add
1318 * @param org_unit Organizational unit to place machine in
1319 * @return status of join
1321 ADS_STATUS ads_join_realm(ADS_STRUCT *ads, const char *hostname, const char *org_unit)
1327 /* hostname must be lowercase */
1328 host = strdup(hostname);
1331 status = ads_find_machine_acct(ads, (void **)&res, host);
1332 if (ADS_ERR_OK(status) && ads_count_replies(ads, res) == 1) {
1333 DEBUG(0, ("Host account for %s already exists - deleting old account\n", host));
1334 status = ads_leave_realm(ads, host);
1335 if (!ADS_ERR_OK(status)) {
1336 DEBUG(0, ("Failed to delete host '%s' from the '%s' realm.\n",
1337 host, ads->config.realm));
1342 status = ads_add_machine_acct(ads, host, org_unit);
1343 if (!ADS_ERR_OK(status)) {
1344 DEBUG(0, ("ads_add_machine_acct: %s\n", ads_errstr(status)));
1348 status = ads_find_machine_acct(ads, (void **)&res, host);
1349 if (!ADS_ERR_OK(status)) {
1350 DEBUG(0, ("Host account test failed\n"));
1360 * Delete a machine from the realm
1361 * @param ads connection to ads server
1362 * @param hostname Machine to remove
1363 * @return status of delete
1365 ADS_STATUS ads_leave_realm(ADS_STRUCT *ads, const char *hostname)
1369 char *hostnameDN, *host;
1372 /* hostname must be lowercase */
1373 host = strdup(hostname);
1376 status = ads_find_machine_acct(ads, &res, host);
1377 if (!ADS_ERR_OK(status)) {
1378 DEBUG(0, ("Host account for %s does not exist.\n", host));
1382 hostnameDN = ads_get_dn(ads, (LDAPMessage *)res);
1383 rc = ldap_delete_s(ads->ld, hostnameDN);
1384 ads_memfree(ads, hostnameDN);
1385 if (rc != LDAP_SUCCESS) {
1386 return ADS_ERROR(rc);
1389 status = ads_find_machine_acct(ads, &res, host);
1390 if (ADS_ERR_OK(status) && ads_count_replies(ads, res) == 1) {
1391 DEBUG(0, ("Failed to remove host account.\n"));
1401 * add machine account to existing security descriptor
1402 * @param ads connection to ads server
1403 * @param hostname machine to add
1404 * @param dn DN of security descriptor
1407 ADS_STATUS ads_set_machine_sd(ADS_STRUCT *ads, const char *hostname, char *dn)
1409 const char *attrs[] = {"ntSecurityDescriptor", "objectSid", 0};
1412 struct berval bval = {0, NULL};
1414 char *escaped_hostname = escape_ldap_string_alloc(hostname);
1416 LDAPMessage *res = 0;
1417 LDAPMessage *msg = 0;
1418 ADS_MODLIST mods = 0;
1424 TALLOC_CTX *ctx = 0;
1426 if (!ads) return ADS_ERROR(LDAP_SERVER_DOWN);
1428 ret = ADS_ERROR(LDAP_SUCCESS);
1430 if (!escaped_hostname) {
1431 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
1434 if (asprintf(&exp, "(samAccountName=%s$)", escaped_hostname) == -1) {
1435 DEBUG(1, ("ads_set_machine_sd: asprintf failed!\n"));
1436 SAFE_FREE(escaped_hostname);
1437 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
1440 SAFE_FREE(escaped_hostname);
1442 ret = ads_search(ads, (void *) &res, exp, attrs);
1444 if (!ADS_ERR_OK(ret)) return ret;
1446 if ( !(msg = ads_first_entry(ads, res) )) {
1447 ret = ADS_ERROR(LDAP_NO_RESULTS_RETURNED);
1448 goto ads_set_sd_error;
1451 ads_pull_sid(ads, msg, attrs[1], &sid);
1452 if (!(ctx = talloc_init("sec_io_desc"))) {
1453 ret = ADS_ERROR(LDAP_NO_MEMORY);
1454 goto ads_set_sd_error;
1457 if (!ads_pull_sd(ads, ctx, msg, attrs[0], &psd)) {
1458 ret = ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
1459 goto ads_set_sd_error;
1462 status = sec_desc_add_sid(ctx, &psd, &sid, SEC_RIGHTS_FULL_CTRL, &sd_size);
1464 if (!NT_STATUS_IS_OK(status)) {
1465 ret = ADS_ERROR_NT(status);
1466 goto ads_set_sd_error;
1469 prs_init(&ps_wire, sd_size, ctx, MARSHALL);
1470 if (!sec_io_desc("sd_wire", &psd, &ps_wire, 1)) {
1471 ret = ADS_ERROR(LDAP_NO_MEMORY);
1472 goto ads_set_sd_error;
1476 file_save("/tmp/sec_desc.new", ps_wire.data_p, sd_size);
1478 if (!(mods = ads_init_mods(ctx))) return ADS_ERROR(LDAP_NO_MEMORY);
1480 bval.bv_len = sd_size;
1481 bval.bv_val = prs_data_p(&ps_wire);
1482 ads_mod_ber(ctx, &mods, attrs[0], &bval);
1483 ret = ads_gen_mod(ads, dn, mods);
1486 ads_msgfree(ads, res);
1487 prs_mem_free(&ps_wire);
1488 talloc_destroy(ctx);
1493 * pull the first entry from a ADS result
1494 * @param ads connection to ads server
1495 * @param res Results of search
1496 * @return first entry from result
1498 void *ads_first_entry(ADS_STRUCT *ads, void *res)
1500 return (void *)ldap_first_entry(ads->ld, (LDAPMessage *)res);
1504 * pull the next entry from a ADS result
1505 * @param ads connection to ads server
1506 * @param res Results of search
1507 * @return next entry from result
1509 void *ads_next_entry(ADS_STRUCT *ads, void *res)
1511 return (void *)ldap_next_entry(ads->ld, (LDAPMessage *)res);
1515 * pull a single string from a ADS result
1516 * @param ads connection to ads server
1517 * @param mem_ctx TALLOC_CTX to use for allocating result string
1518 * @param msg Results of search
1519 * @param field Attribute to retrieve
1520 * @return Result string in talloc context
1522 char *ads_pull_string(ADS_STRUCT *ads,
1523 TALLOC_CTX *mem_ctx, void *msg, const char *field)
1530 values = ldap_get_values(ads->ld, msg, field);
1531 if (!values) return NULL;
1534 rc = pull_utf8_talloc(mem_ctx, &ux_string,
1540 ldap_value_free(values);
1545 * pull an array of strings from a ADS result
1546 * @param ads connection to ads server
1547 * @param mem_ctx TALLOC_CTX to use for allocating result string
1548 * @param msg Results of search
1549 * @param field Attribute to retrieve
1550 * @return Result strings in talloc context
1552 char **ads_pull_strings(ADS_STRUCT *ads,
1553 TALLOC_CTX *mem_ctx, void *msg, const char *field)
1559 values = ldap_get_values(ads->ld, msg, field);
1560 if (!values) return NULL;
1562 for (i=0;values[i];i++) /* noop */ ;
1565 ret = talloc(mem_ctx, sizeof(char *) * (n+1));
1568 if (pull_utf8_talloc(mem_ctx, &ret[i], values[i]) == -1) {
1574 ldap_value_free(values);
1580 * pull a single uint32 from a ADS result
1581 * @param ads connection to ads server
1582 * @param msg Results of search
1583 * @param field Attribute to retrieve
1584 * @param v Pointer to int to store result
1585 * @return boolean inidicating success
1587 BOOL ads_pull_uint32(ADS_STRUCT *ads,
1588 void *msg, const char *field, uint32 *v)
1592 values = ldap_get_values(ads->ld, msg, field);
1593 if (!values) return False;
1595 ldap_value_free(values);
1599 *v = atoi(values[0]);
1600 ldap_value_free(values);
1605 * pull a single objectGUID from an ADS result
1606 * @param ads connection to ADS server
1607 * @param msg results of search
1608 * @param guid 37-byte area to receive text guid
1609 * @return boolean indicating success
1611 BOOL ads_pull_guid(ADS_STRUCT *ads,
1612 void *msg, GUID *guid)
1616 values = ldap_get_values(ads->ld, msg, "objectGUID");
1617 if (!values) return False;
1620 memcpy(guid, values[0], sizeof(GUID));
1621 ldap_value_free(values);
1624 ldap_value_free(values);
1631 * pull a single DOM_SID from a ADS result
1632 * @param ads connection to ads server
1633 * @param msg Results of search
1634 * @param field Attribute to retrieve
1635 * @param sid Pointer to sid to store result
1636 * @return boolean inidicating success
1638 BOOL ads_pull_sid(ADS_STRUCT *ads,
1639 void *msg, const char *field, DOM_SID *sid)
1641 struct berval **values;
1644 values = ldap_get_values_len(ads->ld, msg, field);
1646 if (!values) return False;
1649 ret = sid_parse(values[0]->bv_val, values[0]->bv_len, sid);
1652 ldap_value_free_len(values);
1657 * pull an array of DOM_SIDs from a ADS result
1658 * @param ads connection to ads server
1659 * @param mem_ctx TALLOC_CTX for allocating sid array
1660 * @param msg Results of search
1661 * @param field Attribute to retrieve
1662 * @param sids pointer to sid array to allocate
1663 * @return the count of SIDs pulled
1665 int ads_pull_sids(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
1666 void *msg, const char *field, DOM_SID **sids)
1668 struct berval **values;
1672 values = ldap_get_values_len(ads->ld, msg, field);
1674 if (!values) return 0;
1676 for (i=0; values[i]; i++) /* nop */ ;
1678 (*sids) = talloc(mem_ctx, sizeof(DOM_SID) * i);
1681 for (i=0; values[i]; i++) {
1682 ret = sid_parse(values[i]->bv_val, values[i]->bv_len, &(*sids)[count]);
1686 ldap_value_free_len(values);
1691 * pull a SEC_DESC from a ADS result
1692 * @param ads connection to ads server
1693 * @param mem_ctx TALLOC_CTX for allocating sid array
1694 * @param msg Results of search
1695 * @param field Attribute to retrieve
1696 * @param sd Pointer to *SEC_DESC to store result (talloc()ed)
1697 * @return boolean inidicating success
1699 BOOL ads_pull_sd(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
1700 void *msg, const char *field, SEC_DESC **sd)
1702 struct berval **values;
1706 values = ldap_get_values_len(ads->ld, msg, field);
1708 if (!values) return False;
1711 prs_init(&ps, values[0]->bv_len, mem_ctx, UNMARSHALL);
1712 prs_append_data(&ps, values[0]->bv_val, values[0]->bv_len);
1715 ret = sec_io_desc("sd", sd, &ps, 1);
1718 ldap_value_free_len(values);
1723 * in order to support usernames longer than 21 characters we need to
1724 * use both the sAMAccountName and the userPrincipalName attributes
1725 * It seems that not all users have the userPrincipalName attribute set
1727 * @param ads connection to ads server
1728 * @param mem_ctx TALLOC_CTX for allocating sid array
1729 * @param msg Results of search
1730 * @return the username
1732 char *ads_pull_username(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, void *msg)
1736 ret = ads_pull_string(ads, mem_ctx, msg, "userPrincipalName");
1737 if (ret && (p = strchr(ret, '@'))) {
1741 return ads_pull_string(ads, mem_ctx, msg, "sAMAccountName");
1746 * find the update serial number - this is the core of the ldap cache
1747 * @param ads connection to ads server
1748 * @param ads connection to ADS server
1749 * @param usn Pointer to retrieved update serial number
1750 * @return status of search
1752 ADS_STATUS ads_USN(ADS_STRUCT *ads, uint32 *usn)
1754 const char *attrs[] = {"highestCommittedUSN", NULL};
1758 status = ads_do_search(ads, "", LDAP_SCOPE_BASE, "(objectclass=*)", attrs, &res);
1759 if (!ADS_ERR_OK(status)) return status;
1761 if (ads_count_replies(ads, res) != 1) {
1762 return ADS_ERROR(LDAP_NO_RESULTS_RETURNED);
1765 ads_pull_uint32(ads, res, "highestCommittedUSN", usn);
1766 ads_msgfree(ads, res);
1770 /* parse a ADS timestring - typical string is
1771 '20020917091222.0Z0' which means 09:12.22 17th September
1773 static time_t ads_parse_time(const char *str)
1779 if (sscanf(str, "%4d%2d%2d%2d%2d%2d",
1780 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
1781 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
1792 * Find the servers name and realm - this can be done before authentication
1793 * The ldapServiceName field on w2k looks like this:
1794 * vnet3.home.samba.org:win2000-vnet3$@VNET3.HOME.SAMBA.ORG
1795 * @param ads connection to ads server
1796 * @return status of search
1798 ADS_STATUS ads_server_info(ADS_STRUCT *ads)
1800 const char *attrs[] = {"ldapServiceName", "currentTime", NULL};
1808 if (!(ctx = talloc_init("ads_server_info"))) {
1809 return ADS_ERROR(LDAP_NO_MEMORY);
1812 status = ads_do_search(ads, "", LDAP_SCOPE_BASE, "(objectclass=*)", attrs, &res);
1813 if (!ADS_ERR_OK(status)) return status;
1815 value = ads_pull_string(ads, ctx, res, "ldapServiceName");
1817 return ADS_ERROR(LDAP_NO_RESULTS_RETURNED);
1820 timestr = ads_pull_string(ads, ctx, res, "currentTime");
1822 return ADS_ERROR(LDAP_NO_RESULTS_RETURNED);
1827 p = strchr(value, ':');
1829 talloc_destroy(ctx);
1830 DEBUG(1, ("ads_server_info: returned ldap server name did not contain a ':' so was deemed invalid\n"));
1831 return ADS_ERROR(LDAP_DECODING_ERROR);
1834 SAFE_FREE(ads->config.ldap_server_name);
1836 ads->config.ldap_server_name = strdup(p+1);
1837 p = strchr(ads->config.ldap_server_name, '$');
1838 if (!p || p[1] != '@') {
1839 talloc_destroy(ctx);
1840 DEBUG(1, ("ads_server_info: returned ldap server name (%s) does not contain '$@' so was deemed invalid\n", ads->config.ldap_server_name));
1841 SAFE_FREE(ads->config.ldap_server_name);
1842 return ADS_ERROR(LDAP_DECODING_ERROR);
1847 SAFE_FREE(ads->config.realm);
1848 SAFE_FREE(ads->config.bind_path);
1850 ads->config.realm = strdup(p+2);
1851 ads->config.bind_path = ads_build_dn(ads->config.realm);
1853 DEBUG(3,("got ldap server name %s@%s, using bind path: %s\n",
1854 ads->config.ldap_server_name, ads->config.realm,
1855 ads->config.bind_path));
1857 ads->config.current_time = ads_parse_time(timestr);
1859 if (ads->config.current_time != 0) {
1860 ads->auth.time_offset = ads->config.current_time - time(NULL);
1861 DEBUG(4,("time offset is %d seconds\n", ads->auth.time_offset));
1864 talloc_destroy(ctx);
1871 * find the list of trusted domains
1872 * @param ads connection to ads server
1873 * @param mem_ctx TALLOC_CTX for allocating results
1874 * @param num_trusts pointer to number of trusts
1875 * @param names pointer to trusted domain name list
1876 * @param sids pointer to list of sids of trusted domains
1877 * @return the count of SIDs pulled
1879 ADS_STATUS ads_trusted_domains(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
1885 const char *attrs[] = {"name", "flatname", "securityIdentifier",
1886 "trustDirection", NULL};
1893 status = ads_search(ads, &res, "(objectcategory=trustedDomain)", attrs);
1894 if (!ADS_ERR_OK(status)) return status;
1896 count = ads_count_replies(ads, res);
1898 ads_msgfree(ads, res);
1899 return ADS_ERROR(LDAP_NO_RESULTS_RETURNED);
1902 (*names) = talloc(mem_ctx, sizeof(char *) * count);
1903 (*alt_names) = talloc(mem_ctx, sizeof(char *) * count);
1904 (*sids) = talloc(mem_ctx, sizeof(DOM_SID) * count);
1905 if (! *names || ! *sids) return ADS_ERROR(LDAP_NO_MEMORY);
1907 for (i=0, msg = ads_first_entry(ads, res); msg; msg = ads_next_entry(ads, msg)) {
1910 /* direction is a 2 bit bitfield, 1 means they trust us
1911 but we don't trust them, so we should not list them
1912 as users from that domain can't login */
1913 if (ads_pull_uint32(ads, msg, "trustDirection", &direction) &&
1918 (*names)[i] = ads_pull_string(ads, mem_ctx, msg, "name");
1919 (*alt_names)[i] = ads_pull_string(ads, mem_ctx, msg, "flatname");
1921 if ((*alt_names)[i] && (*alt_names)[i][0]) {
1922 /* we prefer the flatname as the primary name
1923 for consistency with RPC */
1924 char *name = (*alt_names)[i];
1925 (*alt_names)[i] = (*names)[i];
1928 if (ads_pull_sid(ads, msg, "securityIdentifier", &(*sids)[i])) {
1933 ads_msgfree(ads, res);
1941 * find the domain sid for our domain
1942 * @param ads connection to ads server
1943 * @param sid Pointer to domain sid
1944 * @return status of search
1946 ADS_STATUS ads_domain_sid(ADS_STRUCT *ads, DOM_SID *sid)
1948 const char *attrs[] = {"objectSid", NULL};
1952 rc = ads_do_search(ads, ads->config.bind_path, LDAP_SCOPE_BASE, "(objectclass=*)",
1954 if (!ADS_ERR_OK(rc)) return rc;
1955 if (!ads_pull_sid(ads, res, "objectSid", sid)) {
1956 return ADS_ERROR_SYSTEM(ENOENT);
1958 ads_msgfree(ads, res);
1963 /* this is rather complex - we need to find the allternate (netbios) name
1964 for the domain, but there isn't a simple query to do this. Instead
1965 we look for the principle names on the DCs account and find one that has
1966 the right form, then extract the netbios name of the domain from that
1968 ADS_STATUS ads_workgroup_name(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, char **workgroup)
1977 const char *attrs[] = {"servicePrincipalName", NULL};
1979 (*workgroup) = NULL;
1981 asprintf(&exp, "(&(objectclass=computer)(dnshostname=%s.%s))",
1982 ads->config.ldap_server_name, ads->config.realm);
1983 rc = ads_search(ads, &res, exp, attrs);
1986 if (!ADS_ERR_OK(rc)) {
1990 principles = ads_pull_strings(ads, mem_ctx, res, "servicePrincipalName");
1992 ads_msgfree(ads, res);
1995 return ADS_ERROR(LDAP_NO_RESULTS_RETURNED);
1998 asprintf(&prefix, "HOST/%s.%s/",
1999 ads->config.ldap_server_name,
2002 prefix_length = strlen(prefix);
2004 for (i=0;principles[i]; i++) {
2005 if (strncasecmp(principles[i], prefix, prefix_length) == 0 &&
2006 strcasecmp(ads->config.realm, principles[i]+prefix_length) != 0 &&
2007 !strchr(principles[i]+prefix_length, '.')) {
2008 /* found an alternate (short) name for the domain. */
2009 DEBUG(3,("Found alternate name '%s' for realm '%s'\n",
2010 principles[i]+prefix_length,
2011 ads->config.realm));
2012 (*workgroup) = talloc_strdup(mem_ctx, principles[i]+prefix_length);
2019 return ADS_ERROR(LDAP_NO_RESULTS_RETURNED);