dsdb/samdb: use RECYCLED it implies DELETED...
[metze/samba/wip.git] / source4 / dsdb / samdb / cracknames.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    crachnames implementation for the drsuapi pipe
5    DsCrackNames()
6
7    Copyright (C) Stefan Metzmacher 2004
8    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
9    Copyright (C) Matthieu Patou <mat@matws.net> 2012
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "librpc/gen_ndr/drsuapi.h"
27 #include "lib/events/events.h"
28 #include <ldb.h>
29 #include <ldb_errors.h>
30 #include "auth/kerberos/kerberos.h"
31 #include "libcli/ldap/ldap_ndr.h"
32 #include "libcli/security/security.h"
33 #include "auth/auth.h"
34 #include "../lib/util/util_ldb.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "dsdb/common/util.h"
37 #include "param/param.h"
38
39 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
40                                    struct smb_krb5_context *smb_krb5_context,
41                                    uint32_t format_flags, enum drsuapi_DsNameFormat format_offered,
42                                    enum drsuapi_DsNameFormat format_desired,
43                                    struct ldb_dn *name_dn, const char *name, 
44                                    const char *domain_filter, const char *result_filter, 
45                                    struct drsuapi_DsNameInfo1 *info1, int scope, struct ldb_dn *search_dn);
46 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
47                                         enum drsuapi_DsNameFormat format_offered,
48                                         enum drsuapi_DsNameFormat format_desired,
49                                         struct ldb_dn *name_dn, const char *name, 
50                                         struct drsuapi_DsNameInfo1 *info1);
51
52 static WERROR dns_domain_from_principal(TALLOC_CTX *mem_ctx, struct smb_krb5_context *smb_krb5_context, 
53                                         const char *name, 
54                                         struct drsuapi_DsNameInfo1 *info1) 
55 {
56         krb5_error_code ret;
57         krb5_principal principal;
58         /* perhaps it's a principal with a realm, so return the right 'domain only' response */
59         const char *realm;
60         ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name, 
61                                     KRB5_PRINCIPAL_PARSE_REQUIRE_REALM, &principal);
62         if (ret) {
63                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
64                 return WERR_OK;
65         }
66
67         /* This isn't an allocation assignemnt, so it is free'ed with the krb5_free_principal */
68         realm = smb_krb5_principal_get_realm(smb_krb5_context->krb5_context, principal);
69
70         info1->dns_domain_name  = talloc_strdup(mem_ctx, realm);
71         krb5_free_principal(smb_krb5_context->krb5_context, principal);
72
73         W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
74
75         info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
76         return WERR_OK;
77 }               
78
79 static enum drsuapi_DsNameStatus LDB_lookup_spn_alias(krb5_context context, struct ldb_context *ldb_ctx, 
80                                                       TALLOC_CTX *mem_ctx,
81                                                       const char *alias_from,
82                                                       char **alias_to)
83 {
84         unsigned int i;
85         int ret;
86         struct ldb_result *res;
87         struct ldb_message_element *spnmappings;
88         TALLOC_CTX *tmp_ctx;
89         struct ldb_dn *service_dn;
90         char *service_dn_str;
91
92         const char *directory_attrs[] = {
93                 "sPNMappings", 
94                 NULL
95         };
96
97         tmp_ctx = talloc_new(mem_ctx);
98         if (!tmp_ctx) {
99                 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
100         }
101
102         service_dn = ldb_dn_new(tmp_ctx, ldb_ctx, "CN=Directory Service,CN=Windows NT,CN=Services");
103         if ( ! ldb_dn_add_base(service_dn, ldb_get_config_basedn(ldb_ctx))) {
104                 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
105         }
106         service_dn_str = ldb_dn_alloc_linearized(tmp_ctx, service_dn);
107         if ( ! service_dn_str) {
108                 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
109         }
110
111         ret = ldb_search(ldb_ctx, tmp_ctx, &res, service_dn, LDB_SCOPE_BASE,
112                          directory_attrs, "(objectClass=nTDSService)");
113
114         if (ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_OBJECT) {
115                 DEBUG(1, ("ldb_search: dn: %s not found: %s\n", service_dn_str, ldb_errstring(ldb_ctx)));
116                 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
117         } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
118                 DEBUG(1, ("ldb_search: dn: %s not found\n", service_dn_str));
119                 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
120         } else if (res->count != 1) {
121                 talloc_free(res);
122                 DEBUG(1, ("ldb_search: dn: %s not found\n", service_dn_str));
123                 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
124         }
125
126         spnmappings = ldb_msg_find_element(res->msgs[0], "sPNMappings");
127         if (!spnmappings || spnmappings->num_values == 0) {
128                 DEBUG(1, ("ldb_search: dn: %s no sPNMappings attribute\n", service_dn_str));
129                 talloc_free(tmp_ctx);
130                 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
131         }
132
133         for (i = 0; i < spnmappings->num_values; i++) {
134                 char *mapping, *p, *str;
135                 mapping = talloc_strdup(tmp_ctx, 
136                                         (const char *)spnmappings->values[i].data);
137                 if (!mapping) {
138                         DEBUG(1, ("LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping\n", service_dn_str));
139                         talloc_free(tmp_ctx);
140                         return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
141                 }
142
143                 /* C string manipulation sucks */
144
145                 p = strchr(mapping, '=');
146                 if (!p) {
147                         DEBUG(1, ("ldb_search: dn: %s sPNMapping malformed: %s\n", 
148                                   service_dn_str, mapping));
149                         talloc_free(tmp_ctx);
150                         return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
151                 }
152                 p[0] = '\0';
153                 p++;
154                 do {
155                         str = p;
156                         p = strchr(p, ',');
157                         if (p) {
158                                 p[0] = '\0';
159                                 p++;
160                         }
161                         if (strcasecmp(str, alias_from) == 0) {
162                                 *alias_to = mapping;
163                                 talloc_steal(mem_ctx, mapping);
164                                 talloc_free(tmp_ctx);
165                                 return DRSUAPI_DS_NAME_STATUS_OK;
166                         }
167                 } while (p);
168         }
169         DEBUG(4, ("LDB_lookup_spn_alias: no alias for service %s applicable\n", alias_from));
170         talloc_free(tmp_ctx);
171         return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
172 }
173
174 /* When cracking a ServicePrincipalName, many services may be served
175  * by the host/ servicePrincipalName.  The incoming query is for cifs/
176  * but we translate it here, and search on host/.  This is done after
177  * the cifs/ entry has been searched for, making this a fallback */
178
179 static WERROR DsCrackNameSPNAlias(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
180                                   struct smb_krb5_context *smb_krb5_context,
181                                   uint32_t format_flags, enum drsuapi_DsNameFormat format_offered,
182                                   enum drsuapi_DsNameFormat format_desired,
183                                   const char *name, struct drsuapi_DsNameInfo1 *info1)
184 {
185         WERROR wret;
186         krb5_error_code ret;
187         krb5_principal principal;
188         const krb5_data *component;
189         const char *service, *dns_name;
190         char *new_service;
191         char *new_princ;
192         enum drsuapi_DsNameStatus namestatus;
193
194         /* parse principal */
195         ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, 
196                                     name, KRB5_PRINCIPAL_PARSE_NO_REALM, &principal);
197         if (ret) {
198                 DEBUG(2, ("Could not parse principal: %s: %s\n",
199                           name, smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
200                                                            ret, mem_ctx)));
201                 return WERR_NOMEM;
202         }
203
204         /* grab cifs/, http/ etc */
205
206         /* This is checked for in callers, but be safe */
207         if (krb5_princ_size(smb_krb5_context->krb5_context, principal) < 2) {
208                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
209                 krb5_free_principal(smb_krb5_context->krb5_context, principal);
210                 return WERR_OK;
211         }
212         component = krb5_princ_component(smb_krb5_context->krb5_context,
213                                          principal, 0);
214         service = (const char *)component->data;
215         component = krb5_princ_component(smb_krb5_context->krb5_context,
216                                          principal, 1);
217         dns_name = (const char *)component->data;
218
219         /* MAP it */
220         namestatus = LDB_lookup_spn_alias(smb_krb5_context->krb5_context, 
221                                           sam_ctx, mem_ctx, 
222                                           service, &new_service);
223
224         if (namestatus == DRSUAPI_DS_NAME_STATUS_NOT_FOUND) {
225                 wret = WERR_OK;
226                 info1->status           = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
227                 info1->dns_domain_name  = talloc_strdup(mem_ctx, dns_name);
228                 if (!info1->dns_domain_name) {
229                         wret = WERR_NOMEM;
230                 }
231                 krb5_free_principal(smb_krb5_context->krb5_context, principal);
232                 return wret;
233         } else if (namestatus != DRSUAPI_DS_NAME_STATUS_OK) {
234                 info1->status = namestatus;
235                 krb5_free_principal(smb_krb5_context->krb5_context, principal);
236                 return WERR_OK;
237         }
238
239         /* reform principal */
240         new_princ = talloc_asprintf(mem_ctx, "%s/%s", new_service, dns_name);
241         if (!new_princ) {
242                 krb5_free_principal(smb_krb5_context->krb5_context, principal);
243                 return WERR_NOMEM;
244         }
245
246         wret = DsCrackNameOneName(sam_ctx, mem_ctx, format_flags, format_offered, format_desired,
247                                   new_princ, info1);
248         talloc_free(new_princ);
249         if (W_ERROR_IS_OK(wret) && (info1->status == DRSUAPI_DS_NAME_STATUS_NOT_FOUND)) {
250                 info1->status           = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
251                 info1->dns_domain_name  = talloc_strdup(mem_ctx, dns_name);
252                 if (!info1->dns_domain_name) {
253                         wret = WERR_NOMEM;
254                 }
255         }
256         krb5_free_principal(smb_krb5_context->krb5_context, principal);
257         return wret;
258 }
259
260 /* Subcase of CrackNames, for the userPrincipalName */
261
262 static WERROR DsCrackNameUPN(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
263                              struct smb_krb5_context *smb_krb5_context,
264                              uint32_t format_flags, enum drsuapi_DsNameFormat format_offered,
265                              enum drsuapi_DsNameFormat format_desired,
266                              const char *name, struct drsuapi_DsNameInfo1 *info1)
267 {
268         int ldb_ret;
269         WERROR status;
270         const char *domain_filter = NULL;
271         const char *result_filter = NULL;
272         krb5_error_code ret;
273         krb5_principal principal;
274         const char *realm;
275         char *unparsed_name_short;
276         const char *domain_attrs[] = { NULL };
277         struct ldb_result *domain_res = NULL;
278
279         /* Prevent recursion */
280         if (!name) {
281                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
282                 return WERR_OK;
283         }
284
285         ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name, 
286                                     KRB5_PRINCIPAL_PARSE_REQUIRE_REALM, &principal);
287         if (ret) {
288                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
289                 return WERR_OK;
290         }
291
292         realm = smb_krb5_principal_get_realm(smb_krb5_context->krb5_context,
293                                              principal);
294
295         ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
296                              samdb_partitions_dn(sam_ctx, mem_ctx),
297                              LDB_SCOPE_ONELEVEL,
298                              domain_attrs,
299                              "(&(objectClass=crossRef)(|(dnsRoot=%s)(netbiosName=%s))(systemFlags:%s:=%u))",
300                              ldb_binary_encode_string(mem_ctx, realm),
301                              ldb_binary_encode_string(mem_ctx, realm),
302                              LDB_OID_COMPARATOR_AND,
303                              SYSTEM_FLAG_CR_NTDS_DOMAIN);
304
305         if (ldb_ret != LDB_SUCCESS) {
306                 DEBUG(2, ("DsCrackNameUPN domain ref search failed: %s\n", ldb_errstring(sam_ctx)));
307                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
308                 krb5_free_principal(smb_krb5_context->krb5_context, principal);
309                 return WERR_OK;
310         }
311
312         switch (domain_res->count) {
313         case 1:
314                 break;
315         case 0:
316                 krb5_free_principal(smb_krb5_context->krb5_context, principal);
317                 return dns_domain_from_principal(mem_ctx, smb_krb5_context, 
318                                                  name, info1);
319         default:
320                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
321                 krb5_free_principal(smb_krb5_context->krb5_context, principal);
322                 return WERR_OK;
323         }
324
325         ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal, 
326                                       KRB5_PRINCIPAL_UNPARSE_NO_REALM, &unparsed_name_short);
327         krb5_free_principal(smb_krb5_context->krb5_context, principal);
328
329         if (ret) {
330                 free(unparsed_name_short);
331                 return WERR_NOMEM;
332         }
333
334         /* This may need to be extended for more userPrincipalName variations */
335         result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))", 
336                                         ldb_binary_encode_string(mem_ctx, unparsed_name_short));
337
338         domain_filter = talloc_asprintf(mem_ctx, "(distinguishedName=%s)", ldb_dn_get_linearized(domain_res->msgs[0]->dn));
339
340         if (!result_filter || !domain_filter) {
341                 free(unparsed_name_short);
342                 return WERR_NOMEM;
343         }
344         status = DsCrackNameOneFilter(sam_ctx, mem_ctx, 
345                                       smb_krb5_context, 
346                                       format_flags, format_offered, format_desired, 
347                                       NULL, unparsed_name_short, domain_filter, result_filter, 
348                                       info1, LDB_SCOPE_SUBTREE, NULL);
349         free(unparsed_name_short);
350
351         return status;
352 }
353
354 /*
355  * This function will workout the filtering parameter in order to be able to do
356  * the adapted search when the incomming format is format_functional.
357  * This boils down to defining the search_dn (passed as pointer to ldb_dn *) and the
358  * ldap filter request.
359  * Main input parameters are:
360  * * name, which is the portion of the functional name after the
361  * first '/'.
362  * * domain_filter, which is a ldap search filter used to find the NC DN given the
363  * function name to crack.
364  */
365 static WERROR get_format_functional_filtering_param(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
366                         char *name, struct drsuapi_DsNameInfo1 *info1,
367                         struct ldb_dn **psearch_dn, const char *domain_filter, const char **presult_filter)
368 {
369         struct ldb_result *domain_res = NULL;
370         const char * const domain_attrs[] = {"ncName", NULL};
371         struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
372         int ldb_ret;
373         char *account,  *s, *result_filter = NULL;
374         struct ldb_dn *search_dn = NULL;
375
376         *psearch_dn = NULL;
377         *presult_filter = NULL;
378
379         ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
380                                 partitions_basedn,
381                                 LDB_SCOPE_ONELEVEL,
382                                 domain_attrs,
383                                 "%s", domain_filter);
384
385         if (ldb_ret != LDB_SUCCESS) {
386                 DEBUG(2, ("DsCrackNameOne domain ref search failed: %s\n", ldb_errstring(sam_ctx)));
387                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
388                 return WERR_FOOBAR;
389         }
390
391         if (domain_res->count == 1) {
392                 struct ldb_dn *tmp_dn = samdb_result_dn(sam_ctx, mem_ctx, domain_res->msgs[0], "ncName", NULL);
393                 const char * const name_attrs[] = {"name", NULL};
394
395                 account = name;
396                 s = strchr(account, '/');
397                 talloc_free(domain_res);
398                 while(s) {
399                         s[0] = '\0';
400                         s++;
401
402                         ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
403                                                 tmp_dn,
404                                                 LDB_SCOPE_ONELEVEL,
405                                                 name_attrs,
406                                                 "name=%s", account);
407
408                         if (ldb_ret != LDB_SUCCESS) {
409                                 DEBUG(2, ("DsCrackNameOne domain ref search failed: %s\n", ldb_errstring(sam_ctx)));
410                                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
411                                 return WERR_OK;
412                         }
413                         talloc_free(tmp_dn);
414                         switch (domain_res->count) {
415                         case 1:
416                                 break;
417                         case 0:
418                                 talloc_free(domain_res);
419                                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
420                                 return WERR_OK;
421                         default:
422                                 talloc_free(domain_res);
423                                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
424                                 return WERR_OK;
425                         }
426
427                         tmp_dn = talloc_steal(mem_ctx, domain_res->msgs[0]->dn);
428                         talloc_free(domain_res);
429                         search_dn = tmp_dn;
430                         account = s;
431                         s = strchr(account, '/');
432                 }
433                 account = ldb_binary_encode_string(mem_ctx, account);
434                 W_ERROR_HAVE_NO_MEMORY(account);
435                 result_filter = talloc_asprintf(mem_ctx, "(name=%s)",
436                                                 account);
437                 W_ERROR_HAVE_NO_MEMORY(result_filter);
438         }
439         *psearch_dn = search_dn;
440         *presult_filter = result_filter;
441         return WERR_OK;
442 }
443
444 /* Crack a single 'name', from format_offered into format_desired, returning the result in info1 */
445
446 WERROR DsCrackNameOneName(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
447                           uint32_t format_flags, enum drsuapi_DsNameFormat format_offered,
448                           enum drsuapi_DsNameFormat format_desired,
449                           const char *name, struct drsuapi_DsNameInfo1 *info1)
450 {
451         krb5_error_code ret;
452         const char *domain_filter = NULL;
453         const char *result_filter = NULL;
454         struct ldb_dn *name_dn = NULL;
455         struct ldb_dn *search_dn = NULL;
456
457         struct smb_krb5_context *smb_krb5_context = NULL;
458         int scope = LDB_SCOPE_SUBTREE;
459
460         info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
461         info1->dns_domain_name = NULL;
462         info1->result_name = NULL;
463
464         if (!name) {
465                 return WERR_INVALID_PARAM;
466         }
467
468         /* TODO: - fill the correct names in all cases!
469          *       - handle format_flags
470          */
471
472         /* here we need to set the domain_filter and/or the result_filter */
473         switch (format_offered) {
474         case DRSUAPI_DS_NAME_FORMAT_UNKNOWN:
475         {
476                 unsigned int i;
477                 enum drsuapi_DsNameFormat formats[] = {
478                         DRSUAPI_DS_NAME_FORMAT_FQDN_1779, DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
479                         DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT, DRSUAPI_DS_NAME_FORMAT_CANONICAL,
480                         DRSUAPI_DS_NAME_FORMAT_GUID, DRSUAPI_DS_NAME_FORMAT_DISPLAY,
481                         DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL,
482                         DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY,
483                         DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX
484                 };
485                 WERROR werr;
486                 for (i=0; i < ARRAY_SIZE(formats); i++) {
487                         werr = DsCrackNameOneName(sam_ctx, mem_ctx, format_flags, formats[i], format_desired, name, info1);
488                         if (!W_ERROR_IS_OK(werr)) {
489                                 return werr;
490                         }
491                         if (info1->status != DRSUAPI_DS_NAME_STATUS_NOT_FOUND) {
492                                 return werr;
493                         }
494                 }
495                 return werr;
496         }
497
498         case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
499         case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
500         {
501                 char *str, *s, *account;
502                 scope = LDB_SCOPE_ONELEVEL;
503
504                 if (strlen(name) == 0) {
505                         info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
506                         return WERR_OK;
507                 }
508
509                 str = talloc_strdup(mem_ctx, name);
510                 W_ERROR_HAVE_NO_MEMORY(str);
511
512                 if (format_offered == DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX) {
513                         /* Look backwards for the \n, and replace it with / */
514                         s = strrchr(str, '\n');
515                         if (!s) {
516                                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
517                                 return WERR_OK;
518                         }
519                         s[0] = '/';
520                 }
521
522                 s = strchr(str, '/');
523                 if (!s) {
524                         /* there must be at least one / */
525                         info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
526                         return WERR_OK;
527                 }
528
529                 s[0] = '\0';
530                 s++;
531
532                 domain_filter = talloc_asprintf(mem_ctx, "(&(objectClass=crossRef)(dnsRoot=%s)(systemFlags:%s:=%u))",
533                                                 ldb_binary_encode_string(mem_ctx, str),
534                                                 LDB_OID_COMPARATOR_AND,
535                                                 SYSTEM_FLAG_CR_NTDS_DOMAIN);
536                 W_ERROR_HAVE_NO_MEMORY(domain_filter);
537
538                 /* There may not be anything after the domain component (search for the domain itself) */
539                 account = s;
540                 if (account && *account) {
541                         WERROR werr = get_format_functional_filtering_param(sam_ctx,
542                                                                                 mem_ctx,
543                                                                                 account,
544                                                                                 info1,
545                                                                                 &search_dn,
546                                                                                 domain_filter,
547                                                                                 &result_filter);
548                         if (!W_ERROR_IS_OK(werr)) {
549                                 return werr;
550                         }
551                         if (info1->status != DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR)
552                                 return WERR_OK;
553                 }
554                 break;
555         }
556         case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
557                 char *p;
558                 char *domain;
559                 const char *account = NULL;
560
561                 domain = talloc_strdup(mem_ctx, name);
562                 W_ERROR_HAVE_NO_MEMORY(domain);
563
564                 p = strchr(domain, '\\');
565                 if (!p) {
566                         /* invalid input format */
567                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
568                         return WERR_OK;
569                 }
570                 p[0] = '\0';
571
572                 if (p[1]) {
573                         account = &p[1];
574                 }
575
576                 domain_filter = talloc_asprintf(mem_ctx, 
577                                                 "(&(objectClass=crossRef)(|(dnsRoot=%s)(netbiosName=%s))(systemFlags:%s:=%u))",
578                                                 ldb_binary_encode_string(mem_ctx, domain),
579                                                 ldb_binary_encode_string(mem_ctx, domain),
580                                                 LDB_OID_COMPARATOR_AND,
581                                                 SYSTEM_FLAG_CR_NTDS_DOMAIN);
582                 W_ERROR_HAVE_NO_MEMORY(domain_filter);
583                 if (account) {
584                         result_filter = talloc_asprintf(mem_ctx, "(sAMAccountName=%s)",
585                                                         ldb_binary_encode_string(mem_ctx, account));
586                         W_ERROR_HAVE_NO_MEMORY(result_filter);
587                 }
588
589                 talloc_free(domain);
590                 break;
591         }
592
593                 /* A LDAP DN as a string */
594         case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
595                 domain_filter = NULL;
596                 name_dn = ldb_dn_new(mem_ctx, sam_ctx, name);
597                 if (! ldb_dn_validate(name_dn)) {
598                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
599                         return WERR_OK;
600                 }
601                 break;
602         }
603
604                 /* A GUID as a string */
605         case DRSUAPI_DS_NAME_FORMAT_GUID: {
606                 struct GUID guid;
607                 char *ldap_guid;
608                 NTSTATUS nt_status;
609                 domain_filter = NULL;
610
611                 nt_status = GUID_from_string(name, &guid);
612                 if (!NT_STATUS_IS_OK(nt_status)) {
613                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
614                         return WERR_OK;
615                 }
616
617                 ldap_guid = ldap_encode_ndr_GUID(mem_ctx, &guid);
618                 if (!ldap_guid) {
619                         return WERR_NOMEM;
620                 }
621                 result_filter = talloc_asprintf(mem_ctx, "(objectGUID=%s)",
622                                                 ldap_guid);
623                 W_ERROR_HAVE_NO_MEMORY(result_filter);
624                 break;
625         }
626         case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
627                 domain_filter = NULL;
628
629                 result_filter = talloc_asprintf(mem_ctx, "(|(displayName=%s)(samAccountName=%s))",
630                                                 ldb_binary_encode_string(mem_ctx, name), 
631                                                 ldb_binary_encode_string(mem_ctx, name));
632                 W_ERROR_HAVE_NO_MEMORY(result_filter);
633                 break;
634         }
635
636                 /* A S-1234-5678 style string */
637         case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY: {
638                 struct dom_sid *sid = dom_sid_parse_talloc(mem_ctx, name);
639                 char *ldap_sid;
640
641                 domain_filter = NULL;
642                 if (!sid) {
643                         info1->dns_domain_name = NULL;
644                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
645                         return WERR_OK;
646                 }
647                 ldap_sid = ldap_encode_ndr_dom_sid(mem_ctx, 
648                                                    sid);
649                 if (!ldap_sid) {
650                         return WERR_NOMEM;
651                 }
652                 result_filter = talloc_asprintf(mem_ctx, "(objectSid=%s)",
653                                                 ldap_sid);
654                 W_ERROR_HAVE_NO_MEMORY(result_filter);
655                 break;
656         }
657         case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL: {
658                 krb5_principal principal;
659                 char *unparsed_name;
660
661                 ret = smb_krb5_init_context(mem_ctx, 
662                                             ldb_get_event_context(sam_ctx),
663                                             (struct loadparm_context *)ldb_get_opaque(sam_ctx, "loadparm"), 
664                                             &smb_krb5_context);
665
666                 if (ret) {
667                         return WERR_NOMEM;
668                 }
669
670                 /* Ensure we reject compleate junk first */
671                 ret = krb5_parse_name(smb_krb5_context->krb5_context, name, &principal);
672                 if (ret) {
673                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
674                         return WERR_OK;
675                 }
676
677                 domain_filter = NULL;
678
679                 /* By getting the unparsed name here, we ensure the escaping is correct (and trust the client less) */
680                 ret = krb5_unparse_name(smb_krb5_context->krb5_context, principal, &unparsed_name);
681                 if (ret) {
682                         krb5_free_principal(smb_krb5_context->krb5_context, principal);
683                         return WERR_NOMEM;
684                 }
685
686                 krb5_free_principal(smb_krb5_context->krb5_context, principal);
687
688                 /* The ldb_binary_encode_string() here avoid LDAP filter injection attacks */
689                 result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(userPrincipalName=%s))", 
690                                                 ldb_binary_encode_string(mem_ctx, unparsed_name));
691
692                 free(unparsed_name);
693                 W_ERROR_HAVE_NO_MEMORY(result_filter);
694                 break;
695         }
696         case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: {
697                 krb5_principal principal;
698                 char *unparsed_name_short;
699                 const krb5_data *component;
700                 char *service;
701
702                 ret = smb_krb5_init_context(mem_ctx, 
703                                             ldb_get_event_context(sam_ctx),
704                                             (struct loadparm_context *)ldb_get_opaque(sam_ctx, "loadparm"), 
705                                             &smb_krb5_context);
706
707                 if (ret) {
708                         return WERR_NOMEM;
709                 }
710
711                 ret = krb5_parse_name(smb_krb5_context->krb5_context, name, &principal);
712                 if (ret == 0 &&
713                     krb5_princ_size(smb_krb5_context->krb5_context,
714                                                         principal) < 2) {
715                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
716                         krb5_free_principal(smb_krb5_context->krb5_context, principal);
717                         return WERR_OK;
718                 } else if (ret == 0) {
719                         krb5_free_principal(smb_krb5_context->krb5_context, principal);
720                 }
721                 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name, 
722                                             KRB5_PRINCIPAL_PARSE_NO_REALM, &principal);
723                 if (ret) {
724                         return dns_domain_from_principal(mem_ctx, smb_krb5_context,
725                                                          name, info1);
726                 }
727
728                 domain_filter = NULL;
729
730                 ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal, 
731                                               KRB5_PRINCIPAL_UNPARSE_NO_REALM, &unparsed_name_short);
732                 if (ret) {
733                         krb5_free_principal(smb_krb5_context->krb5_context, principal);
734                         return WERR_NOMEM;
735                 }
736
737                 component = krb5_princ_component(smb_krb5_context->krb5_context,
738                                                  principal, 0);
739                 service = (char *)component->data;
740                 if ((krb5_princ_size(smb_krb5_context->krb5_context,
741                                                         principal) == 2) &&
742                         (strcasecmp(service, "host") == 0)) {
743                         /* the 'cn' attribute is just the leading part of the name */
744                         char *computer_name;
745                         component = krb5_princ_component(
746                                                 smb_krb5_context->krb5_context,
747                                                 principal, 1);
748                         computer_name = talloc_strndup(mem_ctx, (char *)component->data,
749                                                         strcspn((char *)component->data, "."));
750                         if (computer_name == NULL) {
751                                 krb5_free_principal(smb_krb5_context->krb5_context, principal);
752                                 free(unparsed_name_short);
753                                 return WERR_NOMEM;
754                         }
755
756                         result_filter = talloc_asprintf(mem_ctx, "(|(&(servicePrincipalName=%s)(objectClass=user))(&(cn=%s)(objectClass=computer)))", 
757                                                         ldb_binary_encode_string(mem_ctx, unparsed_name_short), 
758                                                         ldb_binary_encode_string(mem_ctx, computer_name));
759                 } else {
760                         result_filter = talloc_asprintf(mem_ctx, "(&(servicePrincipalName=%s)(objectClass=user))",
761                                                         ldb_binary_encode_string(mem_ctx, unparsed_name_short));
762                 }
763                 krb5_free_principal(smb_krb5_context->krb5_context, principal);
764                 free(unparsed_name_short);
765                 W_ERROR_HAVE_NO_MEMORY(result_filter);
766
767                 break;
768         }
769         default: {
770                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
771                 return WERR_OK;
772         }
773         }
774
775         if (format_flags & DRSUAPI_DS_NAME_FLAG_SYNTACTICAL_ONLY) {
776                 return DsCrackNameOneSyntactical(mem_ctx, format_offered, format_desired,
777                                                  name_dn, name, info1);
778         }
779
780         return DsCrackNameOneFilter(sam_ctx, mem_ctx, 
781                                     smb_krb5_context, 
782                                     format_flags, format_offered, format_desired, 
783                                     name_dn, name, 
784                                     domain_filter, result_filter, 
785                                     info1, scope, search_dn);
786 }
787
788 /* Subcase of CrackNames.  It is possible to translate a LDAP-style DN
789  * (FQDN_1779) into a canoical name without actually searching the
790  * database */
791
792 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
793                                         enum drsuapi_DsNameFormat format_offered,
794                                         enum drsuapi_DsNameFormat format_desired,
795                                         struct ldb_dn *name_dn, const char *name, 
796                                         struct drsuapi_DsNameInfo1 *info1)
797 {
798         char *cracked;
799         if (format_offered != DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
800                 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
801                 return WERR_OK;
802         }
803
804         switch (format_desired) {
805         case DRSUAPI_DS_NAME_FORMAT_CANONICAL: 
806                 cracked = ldb_dn_canonical_string(mem_ctx, name_dn);
807                 break;
808         case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
809                 cracked = ldb_dn_canonical_ex_string(mem_ctx, name_dn);
810                 break;
811         default:
812                 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
813                 return WERR_OK;
814         }
815         info1->status = DRSUAPI_DS_NAME_STATUS_OK;
816         info1->result_name      = cracked;
817         if (!cracked) {
818                 return WERR_NOMEM;
819         }
820
821         return WERR_OK; 
822 }
823
824 /* Given a filter for the domain, and one for the result, perform the
825  * ldb search. The format offered and desired flags change the
826  * behaviours, including what attributes to return.
827  *
828  * The smb_krb5_context is required because we use the krb5 libs for principal parsing
829  */
830
831 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
832                                    struct smb_krb5_context *smb_krb5_context,
833                                    uint32_t format_flags, enum drsuapi_DsNameFormat format_offered,
834                                    enum drsuapi_DsNameFormat format_desired,
835                                    struct ldb_dn *name_dn, const char *name, 
836                                    const char *domain_filter, const char *result_filter, 
837                                    struct drsuapi_DsNameInfo1 *info1,
838                                    int scope, struct ldb_dn *search_dn)
839 {
840         int ldb_ret;
841         struct ldb_result *domain_res = NULL;
842         const char * const *domain_attrs;
843         const char * const *result_attrs;
844         struct ldb_message **result_res = NULL;
845         struct ldb_message *result = NULL;
846         int i;
847         char *p;
848         struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
849
850         const char * const _domain_attrs_1779[] = { "ncName", "dnsRoot", NULL};
851         const char * const _result_attrs_null[] = { NULL };
852
853         const char * const _domain_attrs_canonical[] = { "ncName", "dnsRoot", NULL};
854         const char * const _result_attrs_canonical[] = { "canonicalName", NULL };
855
856         const char * const _domain_attrs_nt4[] = { "ncName", "dnsRoot", "nETBIOSName", NULL};
857         const char * const _result_attrs_nt4[] = { "sAMAccountName", "objectSid", "objectClass", NULL};
858
859         const char * const _domain_attrs_guid[] = { "ncName", "dnsRoot", NULL};
860         const char * const _result_attrs_guid[] = { "objectGUID", NULL};
861
862         const char * const _domain_attrs_display[] = { "ncName", "dnsRoot", NULL};
863         const char * const _result_attrs_display[] = { "displayName", "samAccountName", NULL};
864
865         const char * const _domain_attrs_none[] = { "ncName", "dnsRoot" , NULL};
866         const char * const _result_attrs_none[] = { NULL};
867
868         /* here we need to set the attrs lists for domain and result lookups */
869         switch (format_desired) {
870         case DRSUAPI_DS_NAME_FORMAT_FQDN_1779:
871         case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
872                 domain_attrs = _domain_attrs_1779;
873                 result_attrs = _result_attrs_null;
874                 break;
875         case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
876                 domain_attrs = _domain_attrs_canonical;
877                 result_attrs = _result_attrs_canonical;
878                 break;
879         case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT:
880                 domain_attrs = _domain_attrs_nt4;
881                 result_attrs = _result_attrs_nt4;
882                 break;
883         case DRSUAPI_DS_NAME_FORMAT_GUID:               
884                 domain_attrs = _domain_attrs_guid;
885                 result_attrs = _result_attrs_guid;
886                 break;
887         case DRSUAPI_DS_NAME_FORMAT_DISPLAY:            
888                 domain_attrs = _domain_attrs_display;
889                 result_attrs = _result_attrs_display;
890                 break;
891         default:
892                 domain_attrs = _domain_attrs_none;
893                 result_attrs = _result_attrs_none;
894                 break;
895         }
896
897         if (domain_filter) {
898                 /* if we have a domain_filter look it up and set the result_basedn and the dns_domain_name */
899                 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
900                                              partitions_basedn,
901                                              LDB_SCOPE_ONELEVEL,
902                                              domain_attrs,
903                                              "%s", domain_filter);
904
905                 if (ldb_ret != LDB_SUCCESS) {
906                         DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s\n", ldb_errstring(sam_ctx)));
907                         info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
908                         return WERR_OK;
909                 }
910
911                 switch (domain_res->count) {
912                 case 1:
913                         break;
914                 case 0:
915                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
916                         return WERR_OK;
917                 default:
918                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
919                         return WERR_OK;
920                 }
921
922                 info1->dns_domain_name  = ldb_msg_find_attr_as_string(domain_res->msgs[0], "dnsRoot", NULL);
923                 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
924                 info1->status           = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
925         } else {
926                 info1->dns_domain_name  = NULL;
927                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
928         }
929
930         if (result_filter) {
931                 int ret;
932                 struct ldb_result *res;
933                 uint32_t dsdb_flags = 0;
934                 struct ldb_dn *real_search_dn;
935
936                 if (domain_res) {
937                         if (!search_dn) {
938                                 struct ldb_dn *tmp_dn = samdb_result_dn(sam_ctx, mem_ctx, domain_res->msgs[0], "ncName", NULL);
939                                 real_search_dn = tmp_dn;
940                         } else {
941                                 real_search_dn = search_dn;
942                         }
943                 } else {
944                         dsdb_flags = DSDB_SEARCH_SEARCH_ALL_PARTITIONS;
945                         real_search_dn = NULL;
946                 }
947                 if (format_desired == DRSUAPI_DS_NAME_FORMAT_GUID){
948                          dsdb_flags |= DSDB_SEARCH_SHOW_RECYCLED;
949                 }
950
951                 /* search with the 'phantom root' flag */
952                 ret = dsdb_search(sam_ctx, mem_ctx, &res,
953                                   real_search_dn,
954                                   scope,
955                                   result_attrs,
956                                   dsdb_flags,
957                                   "%s", result_filter);
958                 if (ret != LDB_SUCCESS) {
959                         DEBUG(2, ("DsCrackNameOneFilter phantom root search failed: %s\n",
960                                   ldb_errstring(sam_ctx)));
961                         info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
962                         return WERR_OK;
963                 }
964
965                 ldb_ret = res->count;
966                 result_res = res->msgs;
967         } else if (format_offered == DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
968                 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
969                                           result_attrs);
970         } else if (domain_res) {
971                 name_dn = samdb_result_dn(sam_ctx, mem_ctx, domain_res->msgs[0], "ncName", NULL);
972                 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
973                                           result_attrs);
974         } else {
975                 /* Can't happen */
976                 DEBUG(0, ("LOGIC ERROR: DsCrackNameOneFilter domain ref search not available: This can't happen...\n"));
977                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
978                 return WERR_OK;
979         }
980
981         switch (ldb_ret) {
982         case 1:
983                 result = result_res[0];
984                 break;
985         case 0:
986                 switch (format_offered) {
987                 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: 
988                         return DsCrackNameSPNAlias(sam_ctx, mem_ctx, 
989                                                    smb_krb5_context, 
990                                                    format_flags, format_offered, format_desired,
991                                                    name, info1);
992
993                 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL:
994                         return DsCrackNameUPN(sam_ctx, mem_ctx, smb_krb5_context, 
995                                               format_flags, format_offered, format_desired,
996                                               name, info1);
997                 default:
998                         break;
999                 }
1000                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1001                 return WERR_OK;
1002         case -1:
1003                 DEBUG(2, ("DsCrackNameOneFilter result search failed: %s\n", ldb_errstring(sam_ctx)));
1004                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1005                 return WERR_OK;
1006         default:
1007                 switch (format_offered) {
1008                 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
1009                 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
1010                 {
1011                         const char *canonical_name = NULL; /* Not required, but we get warnings... */
1012                         /* We may need to manually filter further */
1013                         for (i = 0; i < ldb_ret; i++) {
1014                                 switch (format_offered) {
1015                                 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
1016                                         canonical_name = ldb_dn_canonical_string(mem_ctx, result_res[i]->dn);
1017                                         break;
1018                                 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
1019                                         canonical_name = ldb_dn_canonical_ex_string(mem_ctx, result_res[i]->dn);
1020                                         break;
1021                                 default:
1022                                         break;
1023                                 }
1024                                 if (strcasecmp_m(canonical_name, name) == 0) {
1025                                         result = result_res[i];
1026                                         break;
1027                                 }
1028                         }
1029                         if (!result) {
1030                                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1031                                 return WERR_OK;
1032                         }
1033                 }
1034                 /* FALL TROUGH */
1035                 default:
1036                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1037                         return WERR_OK;
1038                 }
1039         }
1040
1041         info1->dns_domain_name = ldb_dn_canonical_string(mem_ctx, result->dn);
1042         W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
1043         p = strchr(info1->dns_domain_name, '/');
1044         if (p) {
1045                 p[0] = '\0';
1046         }
1047
1048         /* here we can use result and domain_res[0] */
1049         switch (format_desired) {
1050         case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
1051                 info1->result_name      = ldb_dn_alloc_linearized(mem_ctx, result->dn);
1052                 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
1053
1054                 info1->status           = DRSUAPI_DS_NAME_STATUS_OK;
1055                 return WERR_OK;
1056         }
1057         case DRSUAPI_DS_NAME_FORMAT_CANONICAL: {
1058                 info1->result_name      = ldb_msg_find_attr_as_string(result, "canonicalName", NULL);
1059                 info1->status           = DRSUAPI_DS_NAME_STATUS_OK;
1060                 return WERR_OK;
1061         }
1062         case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX: {
1063                 /* Not in the virtual ldb attribute */
1064                 return DsCrackNameOneSyntactical(mem_ctx, 
1065                                                  DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
1066                                                  DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX,
1067                                                  result->dn, name, info1);
1068         }
1069         case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
1070
1071                 const struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, result, "objectSid");
1072                 const char *_acc = "", *_dom = "";
1073                 if (sid == NULL) {
1074                         info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
1075                         return WERR_OK;
1076                 }
1077
1078                 if (samdb_find_attribute(sam_ctx, result, "objectClass",
1079                                          "domain")) {
1080                         /* This can also find a DomainDNSZones entry,
1081                          * but it won't have the SID we just
1082                          * checked.  */
1083                         ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
1084                                                      partitions_basedn,
1085                                                      LDB_SCOPE_ONELEVEL,
1086                                                      domain_attrs,
1087                                                      "(ncName=%s)", ldb_dn_get_linearized(result->dn));
1088
1089                         if (ldb_ret != LDB_SUCCESS) {
1090                                 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s\n", ldb_errstring(sam_ctx)));
1091                                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1092                                 return WERR_OK;
1093                         }
1094
1095                         switch (domain_res->count) {
1096                         case 1:
1097                                 break;
1098                         case 0:
1099                                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1100                                 return WERR_OK;
1101                         default:
1102                                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1103                                 return WERR_OK;
1104                         }
1105                         _dom = ldb_msg_find_attr_as_string(domain_res->msgs[0], "nETBIOSName", NULL);
1106                         W_ERROR_HAVE_NO_MEMORY(_dom);
1107                 } else {
1108                         _acc = ldb_msg_find_attr_as_string(result, "sAMAccountName", NULL);
1109                         if (!_acc) {
1110                                 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
1111                                 return WERR_OK;
1112                         }
1113                         if (dom_sid_in_domain(dom_sid_parse_talloc(mem_ctx, SID_BUILTIN), sid)) {
1114                                 _dom = "BUILTIN";
1115                         } else {
1116                                 const char *attrs[] = { NULL };
1117                                 struct ldb_result *domain_res2;
1118                                 struct dom_sid *dom_sid = dom_sid_dup(mem_ctx, sid);
1119                                 if (!dom_sid) {
1120                                         return WERR_OK;
1121                                 }
1122                                 dom_sid->num_auths--;
1123                                 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res,
1124                                                              NULL,
1125                                                              LDB_SCOPE_BASE,
1126                                                              attrs,
1127                                                              "(&(objectSid=%s)(objectClass=domain))", 
1128                                                              ldap_encode_ndr_dom_sid(mem_ctx, dom_sid));
1129
1130                                 if (ldb_ret != LDB_SUCCESS) {
1131                                         DEBUG(2, ("DsCrackNameOneFilter domain search failed: %s\n", ldb_errstring(sam_ctx)));
1132                                         info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1133                                         return WERR_OK;
1134                                 }
1135
1136                                 switch (domain_res->count) {
1137                                 case 1:
1138                                         break;
1139                                 case 0:
1140                                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1141                                         return WERR_OK;
1142                                 default:
1143                                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1144                                         return WERR_OK;
1145                                 }
1146
1147                                 ldb_ret = ldb_search(sam_ctx, mem_ctx, &domain_res2,
1148                                                              partitions_basedn,
1149                                                              LDB_SCOPE_ONELEVEL,
1150                                                              domain_attrs,
1151                                                              "(ncName=%s)", ldb_dn_get_linearized(domain_res->msgs[0]->dn));
1152
1153                                 if (ldb_ret != LDB_SUCCESS) {
1154                                         DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s\n", ldb_errstring(sam_ctx)));
1155                                         info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1156                                         return WERR_OK;
1157                                 }
1158
1159                                 switch (domain_res2->count) {
1160                                 case 1:
1161                                         break;
1162                                 case 0:
1163                                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1164                                         return WERR_OK;
1165                                 default:
1166                                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1167                                         return WERR_OK;
1168                                 }
1169                                 _dom = ldb_msg_find_attr_as_string(domain_res2->msgs[0], "nETBIOSName", NULL);
1170                                 W_ERROR_HAVE_NO_MEMORY(_dom);
1171                         }
1172                 }
1173
1174                 info1->result_name      = talloc_asprintf(mem_ctx, "%s\\%s", _dom, _acc);
1175                 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
1176
1177                 info1->status           = DRSUAPI_DS_NAME_STATUS_OK;
1178                 return WERR_OK;
1179         }
1180         case DRSUAPI_DS_NAME_FORMAT_GUID: {
1181                 struct GUID guid;
1182
1183                 guid = samdb_result_guid(result, "objectGUID");
1184
1185                 info1->result_name      = GUID_string2(mem_ctx, &guid);
1186                 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
1187
1188                 info1->status           = DRSUAPI_DS_NAME_STATUS_OK;
1189                 return WERR_OK;
1190         }
1191         case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
1192                 info1->result_name      = ldb_msg_find_attr_as_string(result, "displayName", NULL);
1193                 if (!info1->result_name) {
1194                         info1->result_name      = ldb_msg_find_attr_as_string(result, "sAMAccountName", NULL);
1195                 } 
1196                 if (!info1->result_name) {
1197                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1198                 } else {
1199                         info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1200                 }
1201                 return WERR_OK;
1202         }
1203         case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: {
1204                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1205                 return WERR_OK;
1206         }
1207         case DRSUAPI_DS_NAME_FORMAT_DNS_DOMAIN: 
1208         case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY: {
1209                 info1->dns_domain_name = NULL;
1210                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1211                 return WERR_OK;
1212         }
1213         default:
1214                 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
1215                 return WERR_OK;
1216         }
1217 }
1218
1219 /* Given a user Principal Name (such as foo@bar.com),
1220  * return the user and domain DNs.  This is used in the KDC to then
1221  * return the Keys and evaluate policy */
1222
1223 NTSTATUS crack_user_principal_name(struct ldb_context *sam_ctx, 
1224                                    TALLOC_CTX *mem_ctx, 
1225                                    const char *user_principal_name, 
1226                                    struct ldb_dn **user_dn,
1227                                    struct ldb_dn **domain_dn) 
1228 {
1229         WERROR werr;
1230         struct drsuapi_DsNameInfo1 info1;
1231         werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1232                                   DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
1233                                   DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
1234                                   user_principal_name,
1235                                   &info1);
1236         if (!W_ERROR_IS_OK(werr)) {
1237                 return werror_to_ntstatus(werr);
1238         }
1239         switch (info1.status) {
1240         case DRSUAPI_DS_NAME_STATUS_OK:
1241                 break;
1242         case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1243         case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1244         case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1245                 return NT_STATUS_NO_SUCH_USER;
1246         case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1247         default:
1248                 return NT_STATUS_UNSUCCESSFUL;
1249         }
1250
1251         *user_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1252
1253         if (domain_dn) {
1254                 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1255                                           DRSUAPI_DS_NAME_FORMAT_CANONICAL,
1256                                           DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
1257                                           talloc_asprintf(mem_ctx, "%s/", 
1258                                                           info1.dns_domain_name),
1259                                           &info1);
1260                 if (!W_ERROR_IS_OK(werr)) {
1261                         return werror_to_ntstatus(werr);
1262                 }
1263                 switch (info1.status) {
1264                 case DRSUAPI_DS_NAME_STATUS_OK:
1265                         break;
1266                 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1267                 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1268                 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1269                         return NT_STATUS_NO_SUCH_USER;
1270                 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1271                 default:
1272                         return NT_STATUS_UNSUCCESSFUL;
1273                 }
1274
1275                 *domain_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1276         }
1277
1278         return NT_STATUS_OK;
1279 }
1280
1281 /* Given a Service Principal Name (such as host/foo.bar.com@BAR.COM),
1282  * return the user and domain DNs.  This is used in the KDC to then
1283  * return the Keys and evaluate policy */
1284
1285 NTSTATUS crack_service_principal_name(struct ldb_context *sam_ctx, 
1286                                       TALLOC_CTX *mem_ctx, 
1287                                       const char *service_principal_name, 
1288                                       struct ldb_dn **user_dn,
1289                                       struct ldb_dn **domain_dn) 
1290 {
1291         WERROR werr;
1292         struct drsuapi_DsNameInfo1 info1;
1293         werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1294                                   DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL,
1295                                   DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
1296                                   service_principal_name,
1297                                   &info1);
1298         if (!W_ERROR_IS_OK(werr)) {
1299                 return werror_to_ntstatus(werr);
1300         }
1301         switch (info1.status) {
1302         case DRSUAPI_DS_NAME_STATUS_OK:
1303                 break;
1304         case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1305         case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1306         case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1307                 return NT_STATUS_NO_SUCH_USER;
1308         case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1309         default:
1310                 return NT_STATUS_UNSUCCESSFUL;
1311         }
1312
1313         *user_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1314
1315         if (domain_dn) {
1316                 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1317                                           DRSUAPI_DS_NAME_FORMAT_CANONICAL,
1318                                           DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
1319                                           talloc_asprintf(mem_ctx, "%s/", 
1320                                                           info1.dns_domain_name),
1321                                           &info1);
1322                 if (!W_ERROR_IS_OK(werr)) {
1323                         return werror_to_ntstatus(werr);
1324                 }
1325                 switch (info1.status) {
1326                 case DRSUAPI_DS_NAME_STATUS_OK:
1327                         break;
1328                 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1329                 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1330                 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1331                         return NT_STATUS_NO_SUCH_USER;
1332                 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1333                 default:
1334                         return NT_STATUS_UNSUCCESSFUL;
1335                 }
1336
1337                 *domain_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1338         }
1339
1340         return NT_STATUS_OK;
1341 }
1342
1343 NTSTATUS crack_name_to_nt4_name(TALLOC_CTX *mem_ctx, 
1344                                 struct tevent_context *ev_ctx, 
1345                                 struct loadparm_context *lp_ctx,
1346                                 enum drsuapi_DsNameFormat format_offered,
1347                                 const char *name, 
1348                                 const char **nt4_domain, const char **nt4_account)
1349 {
1350         WERROR werr;
1351         struct drsuapi_DsNameInfo1 info1;
1352         struct ldb_context *ldb;
1353         char *p;
1354
1355         /* Handle anonymous bind */
1356         if (!name || !*name) {
1357                 *nt4_domain = "";
1358                 *nt4_account = "";
1359                 return NT_STATUS_OK;
1360         }
1361
1362         ldb = samdb_connect(mem_ctx, ev_ctx, lp_ctx, system_session(lp_ctx), 0);
1363         if (ldb == NULL) {
1364                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1365         }
1366
1367         werr = DsCrackNameOneName(ldb, mem_ctx, 0,
1368                                   format_offered, 
1369                                   DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT,
1370                                   name,
1371                                   &info1);
1372         if (!W_ERROR_IS_OK(werr)) {
1373                 return werror_to_ntstatus(werr);
1374         }
1375         switch (info1.status) {
1376         case DRSUAPI_DS_NAME_STATUS_OK:
1377                 break;
1378         case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1379         case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1380         case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1381                 return NT_STATUS_NO_SUCH_USER;
1382         case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1383         default:
1384                 return NT_STATUS_UNSUCCESSFUL;
1385         }
1386
1387         *nt4_domain = talloc_strdup(mem_ctx, info1.result_name);
1388         if (*nt4_domain == NULL) {
1389                 return NT_STATUS_NO_MEMORY;
1390         }
1391
1392         p = strchr(*nt4_domain, '\\');
1393         if (!p) {
1394                 return NT_STATUS_INVALID_PARAMETER;
1395         }
1396         p[0] = '\0';
1397
1398         *nt4_account = talloc_strdup(mem_ctx, &p[1]);
1399         if (*nt4_account == NULL) {
1400                 return NT_STATUS_NO_MEMORY;
1401         }
1402
1403         return NT_STATUS_OK;
1404 }
1405
1406 NTSTATUS crack_auto_name_to_nt4_name(TALLOC_CTX *mem_ctx,
1407                                      struct tevent_context *ev_ctx, 
1408                                      struct loadparm_context *lp_ctx,
1409                                      const char *name,
1410                                      const char **nt4_domain,
1411                                      const char **nt4_account)
1412 {
1413         enum drsuapi_DsNameFormat format_offered = DRSUAPI_DS_NAME_FORMAT_UNKNOWN;
1414
1415         /* Handle anonymous bind */
1416         if (!name || !*name) {
1417                 *nt4_domain = "";
1418                 *nt4_account = "";
1419                 return NT_STATUS_OK;
1420         }
1421
1422         if (strchr_m(name, '=')) {
1423                 format_offered = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
1424         } else if (strchr_m(name, '@')) {
1425                 format_offered = DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL;
1426         } else if (strchr_m(name, '\\')) {
1427                 format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
1428         } else if (strchr_m(name, '/')) {
1429                 format_offered = DRSUAPI_DS_NAME_FORMAT_CANONICAL;
1430         } else {
1431                 return NT_STATUS_NO_SUCH_USER;
1432         }
1433
1434         return crack_name_to_nt4_name(mem_ctx, ev_ctx, lp_ctx, format_offered, name, nt4_domain, nt4_account);
1435 }
1436
1437
1438 WERROR dcesrv_drsuapi_ListRoles(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
1439                                 const struct drsuapi_DsNameRequest1 *req1,
1440                                 struct drsuapi_DsNameCtr1 **ctr1)
1441 {
1442         struct drsuapi_DsNameInfo1 *names;
1443         uint32_t i;
1444         uint32_t count = 5;/*number of fsmo role owners we are going to return*/
1445
1446         *ctr1 = talloc(mem_ctx, struct drsuapi_DsNameCtr1);
1447         W_ERROR_HAVE_NO_MEMORY(*ctr1);
1448         names = talloc_array(mem_ctx, struct drsuapi_DsNameInfo1, count);
1449         W_ERROR_HAVE_NO_MEMORY(names);
1450
1451         for (i = 0; i < count; i++) {
1452                 WERROR werr;
1453                 struct ldb_dn *role_owner_dn, *fsmo_role_dn, *server_dn;
1454                 werr = dsdb_get_fsmo_role_info(mem_ctx, sam_ctx, i,
1455                                                &fsmo_role_dn, &role_owner_dn);
1456                 if(!W_ERROR_IS_OK(werr)) {
1457                         return werr;
1458                 }
1459                 server_dn = ldb_dn_copy(mem_ctx, role_owner_dn);
1460                 ldb_dn_remove_child_components(server_dn, 1);
1461                 names[i].status = DRSUAPI_DS_NAME_STATUS_OK;
1462                 names[i].dns_domain_name = samdb_dn_to_dnshostname(sam_ctx, mem_ctx,
1463                                                                    server_dn);
1464                 if(!names[i].dns_domain_name) {
1465                         DEBUG(4, ("list_roles: Failed to find dNSHostName for server %s\n",
1466                                   ldb_dn_get_linearized(server_dn)));
1467                 }
1468                 names[i].result_name = talloc_strdup(mem_ctx, ldb_dn_get_linearized(role_owner_dn));
1469         }
1470
1471         (*ctr1)->count = count;
1472         (*ctr1)->array = names;
1473
1474         return WERR_OK;
1475 }
1476
1477 WERROR dcesrv_drsuapi_CrackNamesByNameFormat(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
1478                                              const struct drsuapi_DsNameRequest1 *req1,
1479                                              struct drsuapi_DsNameCtr1 **ctr1)
1480 {
1481         struct drsuapi_DsNameInfo1 *names;
1482         uint32_t i, count;
1483         WERROR status;
1484
1485         *ctr1 = talloc_zero(mem_ctx, struct drsuapi_DsNameCtr1);
1486         W_ERROR_HAVE_NO_MEMORY(*ctr1);
1487
1488         count = req1->count;
1489         names = talloc_array(mem_ctx, struct drsuapi_DsNameInfo1, count);
1490         W_ERROR_HAVE_NO_MEMORY(names);
1491
1492         for (i=0; i < count; i++) {
1493                 status = DsCrackNameOneName(sam_ctx, mem_ctx,
1494                                             req1->format_flags,
1495                                             req1->format_offered,
1496                                             req1->format_desired,
1497                                             req1->names[i].str,
1498                                             &names[i]);
1499                 if (!W_ERROR_IS_OK(status)) {
1500                         return status;
1501                 }
1502         }
1503
1504         (*ctr1)->count = count;
1505         (*ctr1)->array = names;
1506
1507         return WERR_OK;
1508 }
1509
1510 WERROR dcesrv_drsuapi_ListInfoServer(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
1511                                      const struct drsuapi_DsNameRequest1 *req1,
1512                                      struct drsuapi_DsNameCtr1 **_ctr1)
1513 {
1514         struct drsuapi_DsNameInfo1 *names;
1515         struct ldb_result *res;
1516         struct ldb_dn *server_dn, *dn;
1517         struct drsuapi_DsNameCtr1 *ctr1;
1518         int ret, i;
1519         const char *str;
1520         const char *attrs[] = {
1521                 "dn",
1522                 "dNSHostName",
1523                 "serverReference",
1524                 NULL
1525         };
1526
1527         *_ctr1 = NULL;
1528
1529         ctr1 = talloc_zero(mem_ctx, struct drsuapi_DsNameCtr1);
1530         W_ERROR_HAVE_NO_MEMORY(ctr1);
1531
1532         /*
1533          * No magic value here, we have to return 3 entries according to the
1534          * MS-DRSR.pdf
1535          */
1536         ctr1->count = 3;
1537         names = talloc_zero_array(ctr1, struct drsuapi_DsNameInfo1,
1538                                   ctr1->count);
1539         W_ERROR_HAVE_NO_MEMORY(names);
1540         ctr1->array = names;
1541
1542         for (i=0; i < ctr1->count; i++) {
1543                 names[i].status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1544         }
1545         *_ctr1 = ctr1;
1546
1547         if (req1->count != 1) {
1548                 DEBUG(1, ("Expected a count of 1 for the ListInfoServer crackname \n"));
1549                 return WERR_OK;
1550         }
1551
1552         if (req1->names[0].str == NULL) {
1553                 return WERR_OK;
1554         }
1555
1556         server_dn = ldb_dn_new(mem_ctx, sam_ctx, req1->names[0].str);
1557         W_ERROR_HAVE_NO_MEMORY(server_dn);
1558
1559         ret = ldb_search(sam_ctx, mem_ctx, &res, server_dn, LDB_SCOPE_ONELEVEL,
1560                          NULL, "(objectClass=nTDSDSA)");
1561
1562         if (ret != LDB_SUCCESS) {
1563                 DEBUG(1, ("Search for objectClass=nTDSDSA "
1564                           "returned less than 1 objects\n"));
1565                 return WERR_OK;
1566         }
1567
1568         if (res->count != 1) {
1569                 DEBUG(1, ("Search for objectClass=nTDSDSA "
1570                           "returned less than 1 objects\n"));
1571                 return WERR_OK;
1572         }
1573
1574         if (res->msgs[0]->dn) {
1575                 names[0].result_name = ldb_dn_alloc_linearized(names, res->msgs[0]->dn);
1576                 W_ERROR_HAVE_NO_MEMORY(names[0].result_name);
1577                 names[0].status = DRSUAPI_DS_NAME_STATUS_OK;
1578         }
1579
1580         talloc_free(res);
1581
1582         ret = ldb_search(sam_ctx, mem_ctx, &res, server_dn, LDB_SCOPE_BASE,
1583                          attrs, "(objectClass=*)");
1584         if (ret != LDB_SUCCESS) {
1585                 DEBUG(1, ("Search for objectClass=* on dn %s"
1586                           "returned %s\n", req1->names[0].str,
1587                           ldb_strerror(ret)));
1588                 return WERR_OK;
1589         }
1590
1591         if (res->count != 1) {
1592                 DEBUG(1, ("Search for objectClass=* on dn %s"
1593                           "returned less than 1 objects\n", req1->names[0].str));
1594                 return WERR_OK;
1595         }
1596
1597         str = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
1598         if (str != NULL) {
1599                 names[1].result_name = talloc_strdup(names, str);
1600                 W_ERROR_HAVE_NO_MEMORY(names[1].result_name);
1601                 names[1].status = DRSUAPI_DS_NAME_STATUS_OK;
1602         }
1603
1604         dn = ldb_msg_find_attr_as_dn(sam_ctx, mem_ctx, res->msgs[0], "serverReference");
1605         if (dn != NULL) {
1606                 names[2].result_name = ldb_dn_alloc_linearized(names, dn);
1607                 W_ERROR_HAVE_NO_MEMORY(names[2].result_name);
1608                 names[2].status = DRSUAPI_DS_NAME_STATUS_OK;
1609         }
1610
1611         talloc_free(dn);
1612         talloc_free(res);
1613
1614         return WERR_OK;
1615 }