r17824: add a wrapper for the common partitions_basedn calculation
[bbaumbach/samba-autobuild/.git] / source4 / dsdb / samdb / cracknames.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    endpoint server for the drsuapi pipe
5    DsCrackNames()
6
7    Copyright (C) Stefan Metzmacher 2004
8    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "librpc/gen_ndr/drsuapi.h"
27 #include "rpc_server/common/common.h"
28 #include "lib/ldb/include/ldb_errors.h"
29 #include "system/kerberos.h"
30 #include "auth/kerberos/kerberos.h"
31 #include "libcli/ldap/ldap.h"
32 #include "libcli/security/security.h"
33 #include "librpc/gen_ndr/ndr_misc.h"
34 #include "auth/auth.h"
35 #include "db_wrap.h"
36 #include "dsdb/samdb/samdb.h"
37
38 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
39                                    struct smb_krb5_context *smb_krb5_context,
40                                    uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
41                                    const struct ldb_dn *name_dn, const char *name, 
42                                    const char *domain_filter, const char *result_filter, 
43                                    struct drsuapi_DsNameInfo1 *info1);
44 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
45                                         uint32_t format_offered, uint32_t format_desired,
46                                         const struct ldb_dn *name_dn, const char *name, 
47                                         struct drsuapi_DsNameInfo1 *info1);
48
49 static enum drsuapi_DsNameStatus LDB_lookup_spn_alias(krb5_context context, struct ldb_context *ldb_ctx, 
50                                                       TALLOC_CTX *mem_ctx,
51                                                       const char *alias_from,
52                                                       char **alias_to)
53 {
54         int i;
55         int ret;
56         struct ldb_result *res;
57         struct ldb_message_element *spnmappings;
58         TALLOC_CTX *tmp_ctx;
59         struct ldb_dn *service_dn;
60         char *service_dn_str;
61
62         const char *directory_attrs[] = {
63                 "sPNMappings", 
64                 NULL
65         };
66
67         tmp_ctx = talloc_new(mem_ctx);
68         if (!tmp_ctx) {
69                 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
70         }
71
72         service_dn = ldb_dn_string_compose(tmp_ctx, NULL,
73                                            "CN=Directory Service,CN=Windows NT"
74                                            ",CN=Services,CN=Configuration");
75         service_dn_str = ldb_dn_linearize(tmp_ctx, service_dn);
76
77         ret = ldb_search(ldb_ctx, service_dn, LDB_SCOPE_BASE, "(objectClass=nTDSService)",
78                          directory_attrs, &res);
79
80         if (ret != LDB_SUCCESS) {
81                 DEBUG(1, ("ldb_search: dn: %s not found: %s", service_dn_str, ldb_errstring(ldb_ctx)));
82                 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
83         } else if (res->count != 1) {
84                 talloc_free(res);
85                 DEBUG(1, ("ldb_search: dn: %s found %d times!", service_dn_str, res->count));
86                 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
87         }
88         talloc_steal(tmp_ctx, res);
89         
90         spnmappings = ldb_msg_find_element(res->msgs[0], "sPNMappings");
91         if (!spnmappings || spnmappings->num_values == 0) {
92                 DEBUG(1, ("ldb_search: dn: %s no sPNMappings attribute", service_dn_str));
93                 talloc_free(tmp_ctx);
94                 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
95         }
96
97         for (i = 0; i < spnmappings->num_values; i++) {
98                 char *mapping, *p, *str;
99                 mapping = talloc_strdup(tmp_ctx, 
100                                         (const char *)spnmappings->values[i].data);
101                 if (!mapping) {
102                         DEBUG(1, ("LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping\n", service_dn_str));
103                         talloc_free(tmp_ctx);
104                         return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
105                 }
106                 
107                 /* C string manipulation sucks */
108                 
109                 p = strchr(mapping, '=');
110                 if (!p) {
111                         DEBUG(1, ("ldb_search: dn: %s sPNMapping malformed: %s\n", 
112                                   service_dn_str, mapping));
113                         talloc_free(tmp_ctx);
114                         return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
115                 }
116                 p[0] = '\0';
117                 p++;
118                 do {
119                         str = p;
120                         p = strchr(p, ',');
121                         if (p) {
122                                 p[0] = '\0';
123                                 p++;
124                         }
125                         if (strcasecmp(str, alias_from) == 0) {
126                                 *alias_to = mapping;
127                                 talloc_steal(mem_ctx, mapping);
128                                 talloc_free(tmp_ctx);
129                                 return DRSUAPI_DS_NAME_STATUS_OK;
130                         }
131                 } while (p);
132         }
133         DEBUG(4, ("LDB_lookup_spn_alias: no alias for service %s applicable\n", alias_from));
134         talloc_free(tmp_ctx);
135         return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
136 }
137
138 /* When cracking a ServicePrincipalName, many services may be served
139  * by the host/ servicePrincipalName.  The incoming query is for cifs/
140  * but we translate it here, and search on host/.  This is done after
141  * the cifs/ entry has been searched for, making this a fallback */
142
143 static WERROR DsCrackNameSPNAlias(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
144                                   struct smb_krb5_context *smb_krb5_context,
145                                   uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
146                                   const char *name, struct drsuapi_DsNameInfo1 *info1)
147 {
148         WERROR wret;
149         krb5_error_code ret;
150         krb5_principal principal;
151         const char *service;
152         char *new_service;
153         char *new_princ;
154         enum drsuapi_DsNameStatus namestatus;
155         
156         /* parse principal */
157         ret = krb5_parse_name_norealm(smb_krb5_context->krb5_context, 
158                                       name, &principal);
159         if (ret) {
160                 DEBUG(2, ("Could not parse principal: %s: %s",
161                           name, smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
162                                                            ret, mem_ctx)));
163                 return WERR_NOMEM;
164         }
165         
166         /* grab cifs/, http/ etc */
167         
168         /* This is checked for in callers, but be safe */
169         if (principal->name.name_string.len < 2) {
170                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
171                 return WERR_OK;
172         }
173         service = principal->name.name_string.val[0];
174         
175         /* MAP it */
176         namestatus = LDB_lookup_spn_alias(smb_krb5_context->krb5_context, 
177                                           sam_ctx, mem_ctx, 
178                                           service, &new_service);
179         
180         if (namestatus != DRSUAPI_DS_NAME_STATUS_OK) {
181                 info1->status = namestatus;
182                 return WERR_OK;
183         }
184         
185         if (ret != 0) {
186                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
187                 return WERR_OK;
188         }
189         
190         /* ooh, very nasty playing around in the Principal... */
191         free(principal->name.name_string.val[0]);
192         principal->name.name_string.val[0] = strdup(new_service);
193         if (!principal->name.name_string.val[0]) {
194                 krb5_free_principal(smb_krb5_context->krb5_context, principal);
195                 return WERR_NOMEM;
196         }
197         
198         /* reform principal */
199         ret = krb5_unparse_name_norealm(smb_krb5_context->krb5_context, principal, &new_princ);
200
201         krb5_free_principal(smb_krb5_context->krb5_context, principal);
202         
203         if (ret) {
204                 return WERR_NOMEM;
205         }
206         
207         wret = DsCrackNameOneName(sam_ctx, mem_ctx, format_flags, format_offered, format_desired,
208                                   new_princ, info1);
209         free(new_princ);
210         return wret;
211 }
212
213 /* Subcase of CrackNames, for the userPrincipalName */
214
215 static WERROR DsCrackNameUPN(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
216                              struct smb_krb5_context *smb_krb5_context,
217                              uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
218                              const char *name, struct drsuapi_DsNameInfo1 *info1)
219 {
220         WERROR status;
221         const char *domain_filter = NULL;
222         const char *result_filter = NULL;
223         krb5_error_code ret;
224         krb5_principal principal;
225         char **realm;
226         char *unparsed_name_short;
227
228         /* Prevent recursion */
229         if (!name) {
230                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
231                 return WERR_OK;
232         }
233
234         ret = krb5_parse_name_mustrealm(smb_krb5_context->krb5_context, name, &principal);
235         if (ret) {
236                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
237                 return WERR_OK;
238         }
239         
240         domain_filter = NULL;
241         realm = krb5_princ_realm(smb_krb5_context->krb5_context, principal);
242         domain_filter = talloc_asprintf(mem_ctx, 
243                                         "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
244                                         ldb_binary_encode_string(mem_ctx, *realm), 
245                                         ldb_binary_encode_string(mem_ctx, *realm));
246         ret = krb5_unparse_name_norealm(smb_krb5_context->krb5_context, principal, &unparsed_name_short);
247         krb5_free_principal(smb_krb5_context->krb5_context, principal);
248                 
249         if (ret) {
250                 free(unparsed_name_short);
251                 return WERR_NOMEM;
252         }
253         
254         /* This may need to be extended for more userPrincipalName variations */
255         result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))", 
256                                         ldb_binary_encode_string(mem_ctx, unparsed_name_short));
257         if (!result_filter || !domain_filter) {
258                 free(unparsed_name_short);
259                 return WERR_NOMEM;
260         }
261         status = DsCrackNameOneFilter(sam_ctx, mem_ctx, 
262                                       smb_krb5_context, 
263                                       format_flags, format_offered, format_desired, 
264                                       NULL, unparsed_name_short, domain_filter, result_filter, 
265                                       info1);
266         free(unparsed_name_short);
267         return status;
268 }
269
270 /* Crack a single 'name', from format_offered into format_desired, returning the result in info1 */
271
272 WERROR DsCrackNameOneName(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
273                           uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
274                           const char *name, struct drsuapi_DsNameInfo1 *info1)
275 {
276         krb5_error_code ret;
277         const char *domain_filter = NULL;
278         const char *result_filter = NULL;
279         struct ldb_dn *name_dn = NULL;
280
281         struct smb_krb5_context *smb_krb5_context;
282         ret = smb_krb5_init_context(mem_ctx, &smb_krb5_context);
283                                 
284         if (ret) {
285                 return WERR_NOMEM;
286         }
287
288         info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
289         info1->dns_domain_name = NULL;
290         info1->result_name = NULL;
291
292         if (!name) {
293                 return WERR_INVALID_PARAM;
294         }
295
296         /* TODO: - fill the correct names in all cases!
297          *       - handle format_flags
298          */
299
300         /* here we need to set the domain_filter and/or the result_filter */
301         switch (format_offered) {
302         case DRSUAPI_DS_NAME_FORMAT_CANONICAL: {
303                 char *str;
304                 
305                 str = talloc_strdup(mem_ctx, name);
306                 W_ERROR_HAVE_NO_MEMORY(str);
307                 
308                 if (strlen(str) == 0 || str[strlen(str)-1] != '/') {
309                         info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
310                         return WERR_OK;
311                 }
312                 
313                 str[strlen(str)-1] = '\0';
314                 
315                 domain_filter = talloc_asprintf(mem_ctx, 
316                                                 "(&(&(&(dnsRoot=%s)(objectclass=crossRef)))(nETBIOSName=*)(ncName=*))", 
317                                                 ldb_binary_encode_string(mem_ctx, str));
318                 W_ERROR_HAVE_NO_MEMORY(domain_filter);
319                 
320                 break;
321         }
322         case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
323                 char *p;
324                 char *domain;
325                 const char *account = NULL;
326                 
327                 domain = talloc_strdup(mem_ctx, name);
328                 W_ERROR_HAVE_NO_MEMORY(domain);
329                 
330                 p = strchr(domain, '\\');
331                 if (!p) {
332                         /* invalid input format */
333                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
334                         return WERR_OK;
335                 }
336                 p[0] = '\0';
337                 
338                 if (p[1]) {
339                         account = &p[1];
340                 }
341                 
342                 domain_filter = talloc_asprintf(mem_ctx, 
343                                                 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))", 
344                                                 ldb_binary_encode_string(mem_ctx, domain));
345                 W_ERROR_HAVE_NO_MEMORY(domain_filter);
346                 if (account) {
347                         result_filter = talloc_asprintf(mem_ctx, "(sAMAccountName=%s)",
348                                                         ldb_binary_encode_string(mem_ctx, account));
349                         W_ERROR_HAVE_NO_MEMORY(result_filter);
350                 }
351                 
352                 talloc_free(domain);
353                 break;
354         }
355
356                 /* A LDAP DN as a string */
357         case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
358                 name_dn = ldb_dn_explode(mem_ctx, name);
359                 domain_filter = NULL;
360                 if (!name_dn) {
361                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
362                         return WERR_OK;
363                 }
364                 break;
365         }
366
367                 /* A GUID as a string */
368         case DRSUAPI_DS_NAME_FORMAT_GUID: {
369                 struct GUID guid;
370                 char *ldap_guid;
371                 NTSTATUS nt_status;
372                 domain_filter = NULL;
373
374                 nt_status = GUID_from_string(name, &guid);
375                 if (!NT_STATUS_IS_OK(nt_status)) {
376                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
377                         return WERR_OK;
378                 }
379                         
380                 ldap_guid = ldap_encode_ndr_GUID(mem_ctx, &guid);
381                 if (!ldap_guid) {
382                         return WERR_NOMEM;
383                 }
384                 result_filter = talloc_asprintf(mem_ctx, "(objectGUID=%s)",
385                                                 ldap_guid);
386                 W_ERROR_HAVE_NO_MEMORY(result_filter);
387                 break;
388         }
389         case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
390                 domain_filter = NULL;
391
392                 result_filter = talloc_asprintf(mem_ctx, "(|(displayName=%s)(samAccountName=%s))",
393                                                 ldb_binary_encode_string(mem_ctx, name), 
394                                                 ldb_binary_encode_string(mem_ctx, name));
395                 W_ERROR_HAVE_NO_MEMORY(result_filter);
396                 break;
397         }
398         
399                 /* A S-1234-5678 style string */
400         case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY: {
401                 struct dom_sid *sid = dom_sid_parse_talloc(mem_ctx, name);
402                 char *ldap_sid;
403                                                                             
404                 domain_filter = NULL;
405                 if (!sid) {
406                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
407                         return WERR_OK;
408                 }
409                 ldap_sid = ldap_encode_ndr_dom_sid(mem_ctx, 
410                                                    sid);
411                 if (!ldap_sid) {
412                         return WERR_NOMEM;
413                 }
414                 result_filter = talloc_asprintf(mem_ctx, "(objectSid=%s)",
415                                                 ldap_sid);
416                 W_ERROR_HAVE_NO_MEMORY(result_filter);
417                 break;
418         }
419         case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL: {
420                 krb5_principal principal;
421                 char *unparsed_name;
422                 ret = krb5_parse_name(smb_krb5_context->krb5_context, name, &principal);
423                 if (ret) {
424                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
425                         return WERR_OK;
426                 }
427                 
428                 domain_filter = NULL;
429                 
430                 ret = krb5_unparse_name(smb_krb5_context->krb5_context, principal, &unparsed_name);
431                 if (ret) {
432                         krb5_free_principal(smb_krb5_context->krb5_context, principal);
433                         return WERR_NOMEM;
434                 }
435
436                 krb5_free_principal(smb_krb5_context->krb5_context, principal);
437                 result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(userPrincipalName=%s))", 
438                                                 ldb_binary_encode_string(mem_ctx, unparsed_name));
439                 
440                 free(unparsed_name);
441                 W_ERROR_HAVE_NO_MEMORY(result_filter);
442                 break;
443         }
444         case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: {
445                 krb5_principal principal;
446                 char *unparsed_name_short;
447                 char *service;
448                 ret = krb5_parse_name_norealm(smb_krb5_context->krb5_context, name, &principal);
449                 if (ret) {
450                         /* perhaps it's a principal with a realm, so return the right 'domain only' response */
451                         char **realm;
452                         ret = krb5_parse_name_mustrealm(smb_krb5_context->krb5_context, name, &principal);
453                         if (ret) {
454                                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
455                                 return WERR_OK;
456                         }
457                                 
458                         /* This isn't an allocation assignemnt, so it is free'ed with the krb5_free_principal */
459                         realm = krb5_princ_realm(smb_krb5_context->krb5_context, principal);
460
461                         info1->dns_domain_name  = talloc_strdup(info1, *realm);
462                         krb5_free_principal(smb_krb5_context->krb5_context, principal);
463         
464                         W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
465
466                         info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
467                         return WERR_OK;
468                         
469                 } else if (principal->name.name_string.len < 2) {
470                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
471                         return WERR_OK;
472                 }
473
474                 domain_filter = NULL;
475                 
476                 ret = krb5_unparse_name_norealm(smb_krb5_context->krb5_context, principal, &unparsed_name_short);
477                 if (ret) {
478                         krb5_free_principal(smb_krb5_context->krb5_context, principal);
479                         return WERR_NOMEM;
480                 }
481
482                 service = principal->name.name_string.val[0];
483                 if ((principal->name.name_string.len == 2) && (strcasecmp(service, "host") == 0)) {
484                         /* the 'cn' attribute is just the leading part of the name */
485                         char *computer_name;
486                         computer_name = talloc_strndup(mem_ctx, principal->name.name_string.val[1], 
487                                                       strcspn(principal->name.name_string.val[1], "."));
488                         if (computer_name == NULL) {
489                                 return WERR_NOMEM;
490                         }
491
492                         result_filter = talloc_asprintf(mem_ctx, "(|(&(servicePrincipalName=%s)(objectClass=user))(&(cn=%s)(objectClass=computer)))", 
493                                                         ldb_binary_encode_string(mem_ctx, unparsed_name_short), 
494                                                         ldb_binary_encode_string(mem_ctx, computer_name));
495                 } else {
496                         result_filter = talloc_asprintf(mem_ctx, "(&(servicePrincipalName=%s)(objectClass=user))",
497                                                         ldb_binary_encode_string(mem_ctx, unparsed_name_short));
498                 }
499                 krb5_free_principal(smb_krb5_context->krb5_context, principal);
500                 free(unparsed_name_short);
501                 W_ERROR_HAVE_NO_MEMORY(result_filter);
502                 
503                 break;
504         }
505         default: {
506                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
507                 return WERR_OK;
508         }
509
510         }
511
512         if (format_flags & DRSUAPI_DS_NAME_FLAG_SYNTACTICAL_ONLY) {
513                 return DsCrackNameOneSyntactical(mem_ctx, format_offered, format_desired,
514                                                  name_dn, name, info1);
515         }
516         
517         return DsCrackNameOneFilter(sam_ctx, mem_ctx, 
518                                     smb_krb5_context, 
519                                     format_flags, format_offered, format_desired, 
520                                     name_dn, name, 
521                                     domain_filter, result_filter, 
522                                     info1);
523 }
524
525 /* Subcase of CrackNames.  It is possible to translate a LDAP-style DN
526  * (FQDN_1779) into a canoical name without actually searching the
527  * database */
528
529 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
530                                         uint32_t format_offered, uint32_t format_desired,
531                                         const struct ldb_dn *name_dn, const char *name, 
532                                         struct drsuapi_DsNameInfo1 *info1)
533 {
534         char *cracked;
535         if (format_offered != DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
536                 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
537                 return WERR_OK;
538         }
539
540         switch (format_desired) {
541         case DRSUAPI_DS_NAME_FORMAT_CANONICAL: 
542                 cracked = ldb_dn_canonical_string(mem_ctx, name_dn);
543                 break;
544         case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
545                 cracked = ldb_dn_canonical_ex_string(mem_ctx, name_dn);
546                 break;
547         default:
548                 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
549                 return WERR_OK;
550         }
551         info1->status = DRSUAPI_DS_NAME_STATUS_OK;
552         info1->result_name      = cracked;
553         if (!cracked) {
554                 return WERR_NOMEM;
555         }
556         
557         return WERR_OK; 
558 }
559
560 /* Given a filter for the domain, and one for the result, perform the
561  * ldb search. The format offered and desired flags change the
562  * behaviours, including what attributes to return.
563  *
564  * The smb_krb5_context is required because we use the krb5 libs for principal parsing
565  */
566
567 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
568                                    struct smb_krb5_context *smb_krb5_context,
569                                    uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
570                                    const struct ldb_dn *name_dn, const char *name, 
571                                    const char *domain_filter, const char *result_filter, 
572                                    struct drsuapi_DsNameInfo1 *info1)
573 {
574         int ldb_ret;
575         struct ldb_message **domain_res = NULL;
576         const char * const *domain_attrs;
577         const char * const *result_attrs;
578         struct ldb_message **result_res = NULL;
579         const struct ldb_dn *result_basedn;
580         const struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
581
582         const char * const _domain_attrs_1779[] = { "ncName", "dnsRoot", NULL};
583         const char * const _result_attrs_null[] = { NULL };
584
585         const char * const _domain_attrs_canonical[] = { "ncName", "dnsRoot", NULL};
586         const char * const _result_attrs_canonical[] = { "canonicalName", NULL };
587
588         const char * const _domain_attrs_nt4[] = { "ncName", "dnsRoot", "nETBIOSName", NULL};
589         const char * const _result_attrs_nt4[] = { "sAMAccountName", "objectSid", NULL};
590                 
591         const char * const _domain_attrs_guid[] = { "ncName", "dnsRoot", NULL};
592         const char * const _result_attrs_guid[] = { "objectGUID", NULL};
593                 
594         const char * const _domain_attrs_display[] = { "ncName", "dnsRoot", NULL};
595         const char * const _result_attrs_display[] = { "displayName", "samAccountName", NULL};
596
597         /* here we need to set the attrs lists for domain and result lookups */
598         switch (format_desired) {
599         case DRSUAPI_DS_NAME_FORMAT_FQDN_1779:
600         case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
601                 domain_attrs = _domain_attrs_1779;
602                 result_attrs = _result_attrs_null;
603                 break;
604         case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
605                 domain_attrs = _domain_attrs_canonical;
606                 result_attrs = _result_attrs_canonical;
607                 break;
608         case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT:
609                 domain_attrs = _domain_attrs_nt4;
610                 result_attrs = _result_attrs_nt4;
611                 break;
612         case DRSUAPI_DS_NAME_FORMAT_GUID:               
613                 domain_attrs = _domain_attrs_guid;
614                 result_attrs = _result_attrs_guid;
615                 break;
616         case DRSUAPI_DS_NAME_FORMAT_DISPLAY:            
617                 domain_attrs = _domain_attrs_display;
618                 result_attrs = _result_attrs_display;
619                 break;
620         default:
621                 return WERR_OK;
622         }
623
624         if (domain_filter) {
625                 /* if we have a domain_filter look it up and set the result_basedn and the dns_domain_name */
626                 ldb_ret = gendb_search(sam_ctx, mem_ctx, partitions_basedn, &domain_res, domain_attrs,
627                                        "%s", domain_filter);
628         } else {
629                 ldb_ret = gendb_search(sam_ctx, mem_ctx, partitions_basedn, &domain_res, domain_attrs,
630                                        "(ncName=%s)", ldb_dn_linearize(mem_ctx, samdb_base_dn(sam_ctx)));
631         } 
632
633         switch (ldb_ret) {
634         case 1:
635                 break;
636         case 0:
637                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
638                 return WERR_OK;
639         case -1:
640                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
641                 return WERR_OK;
642         default:
643                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
644                 return WERR_OK;
645         }
646
647         info1->dns_domain_name  = samdb_result_string(domain_res[0], "dnsRoot", NULL);
648         W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
649         info1->status           = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
650
651         if (result_filter) {
652                 result_basedn = samdb_result_dn(mem_ctx, domain_res[0], "ncName", NULL);
653                 
654                 ldb_ret = gendb_search(sam_ctx, mem_ctx, result_basedn, &result_res,
655                                        result_attrs, "%s", result_filter);
656         } else if (format_offered == DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
657                 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
658                                           result_attrs);
659         } else {
660                 name_dn = samdb_result_dn(mem_ctx, domain_res[0], "ncName", NULL);
661                 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
662                                           result_attrs);
663         }
664
665         switch (ldb_ret) {
666         case 1:
667                 break;
668         case 0:
669                 switch (format_offered) {
670                 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: 
671                         return DsCrackNameSPNAlias(sam_ctx, mem_ctx, 
672                                                    smb_krb5_context, 
673                                                    format_flags, format_offered, format_desired,
674                                                    name, info1);
675                         
676                 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL:
677                         return DsCrackNameUPN(sam_ctx, mem_ctx, smb_krb5_context, 
678                                               format_flags, format_offered, format_desired,
679                                               name, info1);
680                 }
681                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
682                 return WERR_OK;
683         case -1:
684                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
685                 return WERR_OK;
686         default:
687                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
688                 return WERR_OK;
689         }
690
691         /* here we can use result_res[0] and domain_res[0] */
692         switch (format_desired) {
693         case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
694                 info1->result_name      = ldb_dn_linearize(mem_ctx, result_res[0]->dn);
695                 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
696
697                 info1->status           = DRSUAPI_DS_NAME_STATUS_OK;
698                 return WERR_OK;
699         }
700         case DRSUAPI_DS_NAME_FORMAT_CANONICAL: {
701                 info1->result_name      = samdb_result_string(result_res[0], "canonicalName", NULL);
702                 info1->status           = DRSUAPI_DS_NAME_STATUS_OK;
703                 return WERR_OK;
704         }
705         case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX: {
706                 /* Not in the virtual ldb attribute */
707                 return DsCrackNameOneSyntactical(mem_ctx, 
708                                                  DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
709                                                  DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX,
710                                                  result_res[0]->dn, name, info1);
711         }
712         case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
713                 const struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, result_res[0], "objectSid");
714                 const char *_acc = "", *_dom = "";
715                 
716                 if (!sid || (sid->num_auths < 4) || (sid->num_auths > 5)) {
717                         info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
718                         return WERR_OK;
719                 }
720
721                 if (sid->num_auths == 4) {
722                         ldb_ret = gendb_search(sam_ctx, mem_ctx, partitions_basedn, &domain_res, domain_attrs,
723                                                "(ncName=%s)", ldb_dn_linearize(mem_ctx, result_res[0]->dn));
724                         if (ldb_ret != 1) {
725                                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
726                                 return WERR_OK;
727                         }
728                         _dom = samdb_result_string(domain_res[0], "nETBIOSName", NULL);
729                         W_ERROR_HAVE_NO_MEMORY(_dom);
730                 
731                 } else if (sid->num_auths == 5) {
732                         const char *attrs[] = { NULL };
733                         struct ldb_message **domain_res2;
734                         struct dom_sid *dom_sid = dom_sid_dup(mem_ctx, sid);
735                         if (!dom_sid) {
736                                 return WERR_OK;
737                         }
738                         dom_sid->num_auths--;
739                         ldb_ret = gendb_search(sam_ctx, mem_ctx, NULL, &domain_res, attrs,
740                                                "(&(objectSid=%s)(objectClass=domain))", ldap_encode_ndr_dom_sid(mem_ctx, dom_sid));
741                         if (ldb_ret != 1) {
742                                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
743                                 return WERR_OK;
744                         }
745                         ldb_ret = gendb_search(sam_ctx, mem_ctx, partitions_basedn, &domain_res2, domain_attrs,
746                                                "(ncName=%s)", ldb_dn_linearize(mem_ctx, domain_res[0]->dn));
747                         if (ldb_ret != 1) {
748                                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
749                                 return WERR_OK;
750                         }
751                         
752                         _dom = samdb_result_string(domain_res2[0], "nETBIOSName", NULL);
753                         W_ERROR_HAVE_NO_MEMORY(_dom);
754
755                         _acc = samdb_result_string(result_res[0], "sAMAccountName", NULL);
756                         W_ERROR_HAVE_NO_MEMORY(_acc);
757                 }
758
759                 info1->result_name      = talloc_asprintf(mem_ctx, "%s\\%s", _dom, _acc);
760                 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
761                 
762                 info1->status           = DRSUAPI_DS_NAME_STATUS_OK;
763                 return WERR_OK;
764         }
765         case DRSUAPI_DS_NAME_FORMAT_GUID: {
766                 struct GUID guid;
767                 
768                 guid = samdb_result_guid(result_res[0], "objectGUID");
769                 
770                 info1->result_name      = GUID_string2(mem_ctx, &guid);
771                 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
772                 
773                 info1->status           = DRSUAPI_DS_NAME_STATUS_OK;
774                 return WERR_OK;
775         }
776         case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
777                 info1->result_name      = samdb_result_string(result_res[0], "displayName", NULL);
778                 if (!info1->result_name) {
779                         info1->result_name      = samdb_result_string(result_res[0], "sAMAccountName", NULL);
780                 } 
781                 if (!info1->result_name) {
782                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
783                 } else {
784                         info1->status = DRSUAPI_DS_NAME_STATUS_OK;
785                 }
786                 return WERR_OK;
787         }
788         default:
789                 return WERR_OK;
790         }
791         
792         return WERR_INVALID_PARAM;
793 }
794
795 /* Given a user Principal Name (such as foo@bar.com),
796  * return the user and domain DNs.  This is used in the KDC to then
797  * return the Keys and evaluate policy */
798
799 NTSTATUS crack_user_principal_name(struct ldb_context *sam_ctx, 
800                                    TALLOC_CTX *mem_ctx, 
801                                    const char *user_principal_name, 
802                                    struct ldb_dn **user_dn,
803                                    struct ldb_dn **domain_dn) 
804 {
805         WERROR werr;
806         struct drsuapi_DsNameInfo1 info1;
807         werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
808                                   DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
809                                   DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
810                                   user_principal_name,
811                                   &info1);
812         if (!W_ERROR_IS_OK(werr)) {
813                 return werror_to_ntstatus(werr);
814         }
815         switch (info1.status) {
816         case DRSUAPI_DS_NAME_STATUS_OK:
817                 break;
818         case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
819         case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
820         case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
821                 return NT_STATUS_NO_SUCH_USER;
822         case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
823         default:
824                 return NT_STATUS_UNSUCCESSFUL;
825         }
826         
827         *user_dn = ldb_dn_explode(mem_ctx, info1.result_name);
828         
829         if (domain_dn) {
830                 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
831                                           DRSUAPI_DS_NAME_FORMAT_CANONICAL,
832                                           DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
833                                           talloc_asprintf(mem_ctx, "%s/", 
834                                                           info1.dns_domain_name),
835                                           &info1);
836                 if (!W_ERROR_IS_OK(werr)) {
837                         return werror_to_ntstatus(werr);
838                 }
839                 switch (info1.status) {
840                 case DRSUAPI_DS_NAME_STATUS_OK:
841                         break;
842                 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
843                 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
844                 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
845                         return NT_STATUS_NO_SUCH_USER;
846                 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
847                 default:
848                         return NT_STATUS_UNSUCCESSFUL;
849                 }
850                 
851                 *domain_dn = ldb_dn_explode(mem_ctx, info1.result_name);
852         }
853
854         return NT_STATUS_OK;
855         
856 }
857
858 /* Given a Service Principal Name (such as host/foo.bar.com@BAR.COM),
859  * return the user and domain DNs.  This is used in the KDC to then
860  * return the Keys and evaluate policy */
861
862 NTSTATUS crack_service_principal_name(struct ldb_context *sam_ctx, 
863                                       TALLOC_CTX *mem_ctx, 
864                                       const char *service_principal_name, 
865                                       struct ldb_dn **user_dn,
866                                       struct ldb_dn **domain_dn) 
867 {
868         WERROR werr;
869         struct drsuapi_DsNameInfo1 info1;
870         werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
871                                   DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL,
872                                   DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
873                                   service_principal_name,
874                                   &info1);
875         if (!W_ERROR_IS_OK(werr)) {
876                 return werror_to_ntstatus(werr);
877         }
878         switch (info1.status) {
879         case DRSUAPI_DS_NAME_STATUS_OK:
880                 break;
881         case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
882         case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
883         case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
884                 return NT_STATUS_NO_SUCH_USER;
885         case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
886         default:
887                 return NT_STATUS_UNSUCCESSFUL;
888         }
889         
890         *user_dn = ldb_dn_explode(mem_ctx, info1.result_name);
891         
892         if (domain_dn) {
893                 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
894                                           DRSUAPI_DS_NAME_FORMAT_CANONICAL,
895                                           DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
896                                           talloc_asprintf(mem_ctx, "%s/", 
897                                                           info1.dns_domain_name),
898                                           &info1);
899                 if (!W_ERROR_IS_OK(werr)) {
900                         return werror_to_ntstatus(werr);
901                 }
902                 switch (info1.status) {
903                 case DRSUAPI_DS_NAME_STATUS_OK:
904                         break;
905                 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
906                 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
907                 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
908                         return NT_STATUS_NO_SUCH_USER;
909                 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
910                 default:
911                         return NT_STATUS_UNSUCCESSFUL;
912                 }
913                 
914                 *domain_dn = ldb_dn_explode(mem_ctx, info1.result_name);
915         }
916
917         return NT_STATUS_OK;
918         
919 }
920
921 NTSTATUS crack_dn_to_nt4_name(TALLOC_CTX *mem_ctx, 
922                               const char *dn, 
923                               const char **nt4_domain, const char **nt4_account)
924 {
925         WERROR werr;
926         struct drsuapi_DsNameInfo1 info1;
927         struct ldb_context *ldb;
928         char *p;
929
930         /* Handle anonymous bind */
931         if (!dn || !*dn) {
932                 *nt4_domain = "";
933                 *nt4_account = "";
934                 return NT_STATUS_OK;
935         }
936
937         ldb = samdb_connect(mem_ctx, system_session(mem_ctx));
938         if (ldb == NULL) {
939                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
940         }
941
942         werr = DsCrackNameOneName(ldb, mem_ctx, 0,
943                                   DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
944                                   DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT,
945                                   dn,
946                                   &info1);
947         if (!W_ERROR_IS_OK(werr)) {
948                 return werror_to_ntstatus(werr);
949         }
950         switch (info1.status) {
951         case DRSUAPI_DS_NAME_STATUS_OK:
952                 break;
953         case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
954         case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
955         case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
956                 return NT_STATUS_NO_SUCH_USER;
957         case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
958         default:
959                 return NT_STATUS_UNSUCCESSFUL;
960         }
961         
962         *nt4_domain = talloc_strdup(mem_ctx, info1.result_name);
963         
964         p = strchr(*nt4_domain, '\\');
965         if (!p) {
966                 return NT_STATUS_INVALID_PARAMETER;
967         }
968         p[0] = '\0';
969         
970         if (p[1]) {
971                 *nt4_account = talloc_strdup(mem_ctx, &p[1]);
972         }
973
974         if (!*nt4_account || !*nt4_domain) {
975                 return NT_STATUS_NO_MEMORY;
976         }
977
978         return NT_STATUS_OK;
979         
980 }