r14860: create libcli/security/security.h
[kai/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 "auth/auth.h"
34 #include "db_wrap.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "librpc/gen_ndr/ndr_security.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, samdb_base_dn(tmp_ctx),
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                 WERR_TALLOC_CHECK(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                 WERR_TALLOC_CHECK(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                 WERR_TALLOC_CHECK(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                 WERR_TALLOC_CHECK(domain_filter);
346                 if (account) {
347                         result_filter = talloc_asprintf(mem_ctx, "(sAMAccountName=%s)",
348                                                         ldb_binary_encode_string(mem_ctx, account));
349                         WERR_TALLOC_CHECK(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                 WERR_TALLOC_CHECK(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                 WERR_TALLOC_CHECK(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                 WERR_TALLOC_CHECK(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                 WERR_TALLOC_CHECK(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                         WERR_TALLOC_CHECK(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                 WERR_TALLOC_CHECK(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
581         const char * const _domain_attrs_1779[] = { "ncName", "dnsRoot", NULL};
582         const char * const _result_attrs_null[] = { NULL };
583
584         const char * const _domain_attrs_canonical[] = { "ncName", "dnsRoot", NULL};
585         const char * const _result_attrs_canonical[] = { "canonicalName", NULL };
586
587         const char * const _domain_attrs_nt4[] = { "ncName", "dnsRoot", "nETBIOSName", NULL};
588         const char * const _result_attrs_nt4[] = { "sAMAccountName", "objectSid", NULL};
589                 
590         const char * const _domain_attrs_guid[] = { "ncName", "dnsRoot", NULL};
591         const char * const _result_attrs_guid[] = { "objectGUID", NULL};
592                 
593         const char * const _domain_attrs_display[] = { "ncName", "dnsRoot", NULL};
594         const char * const _result_attrs_display[] = { "displayName", "samAccountName", NULL};
595
596         /* here we need to set the attrs lists for domain and result lookups */
597         switch (format_desired) {
598         case DRSUAPI_DS_NAME_FORMAT_FQDN_1779:
599         case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
600                 domain_attrs = _domain_attrs_1779;
601                 result_attrs = _result_attrs_null;
602                 break;
603         case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
604                 domain_attrs = _domain_attrs_canonical;
605                 result_attrs = _result_attrs_canonical;
606                 break;
607         case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT:
608                 domain_attrs = _domain_attrs_nt4;
609                 result_attrs = _result_attrs_nt4;
610                 break;
611         case DRSUAPI_DS_NAME_FORMAT_GUID:               
612                 domain_attrs = _domain_attrs_guid;
613                 result_attrs = _result_attrs_guid;
614                 break;
615         case DRSUAPI_DS_NAME_FORMAT_DISPLAY:            
616                 domain_attrs = _domain_attrs_display;
617                 result_attrs = _result_attrs_display;
618                 break;
619         default:
620                 return WERR_OK;
621         }
622
623         if (domain_filter) {
624                 /* if we have a domain_filter look it up and set the result_basedn and the dns_domain_name */
625                 ldb_ret = gendb_search(sam_ctx, mem_ctx, NULL, &domain_res, domain_attrs,
626                                        "%s", domain_filter);
627         } else {
628                 ldb_ret = gendb_search(sam_ctx, mem_ctx, NULL, &domain_res, domain_attrs,
629                                        "(ncName=%s)", ldb_dn_linearize(mem_ctx, samdb_base_dn(mem_ctx)));
630         } 
631
632         switch (ldb_ret) {
633         case 1:
634                 break;
635         case 0:
636                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
637                 return WERR_OK;
638         case -1:
639                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
640                 return WERR_OK;
641         default:
642                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
643                 return WERR_OK;
644         }
645
646         info1->dns_domain_name  = samdb_result_string(domain_res[0], "dnsRoot", NULL);
647         WERR_TALLOC_CHECK(info1->dns_domain_name);
648         info1->status           = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
649
650         if (result_filter) {
651                 result_basedn = samdb_result_dn(mem_ctx, domain_res[0], "ncName", NULL);
652                 
653                 ldb_ret = gendb_search(sam_ctx, mem_ctx, result_basedn, &result_res,
654                                        result_attrs, "%s", result_filter);
655         } else if (format_offered == DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
656                 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
657                                           result_attrs);
658         } else {
659                 name_dn = samdb_result_dn(mem_ctx, domain_res[0], "ncName", NULL);
660                 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
661                                           result_attrs);
662         }
663
664         switch (ldb_ret) {
665         case 1:
666                 break;
667         case 0:
668                 switch (format_offered) {
669                 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: 
670                         return DsCrackNameSPNAlias(sam_ctx, mem_ctx, 
671                                                    smb_krb5_context, 
672                                                    format_flags, format_offered, format_desired,
673                                                    name, info1);
674                         
675                 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL:
676                         return DsCrackNameUPN(sam_ctx, mem_ctx, smb_krb5_context, 
677                                               format_flags, format_offered, format_desired,
678                                               name, info1);
679                 }
680                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
681                 return WERR_OK;
682         case -1:
683                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
684                 return WERR_OK;
685         default:
686                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
687                 return WERR_OK;
688         }
689
690         /* here we can use result_res[0] and domain_res[0] */
691         switch (format_desired) {
692         case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
693                 info1->result_name      = ldb_dn_linearize(mem_ctx, result_res[0]->dn);
694                 WERR_TALLOC_CHECK(info1->result_name);
695
696                 info1->status           = DRSUAPI_DS_NAME_STATUS_OK;
697                 return WERR_OK;
698         }
699         case DRSUAPI_DS_NAME_FORMAT_CANONICAL: {
700                 info1->result_name      = samdb_result_string(result_res[0], "canonicalName", NULL);
701                 info1->status           = DRSUAPI_DS_NAME_STATUS_OK;
702                 return WERR_OK;
703         }
704         case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX: {
705                 /* Not in the virtual ldb attribute */
706                 return DsCrackNameOneSyntactical(mem_ctx, 
707                                                  DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
708                                                  DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX,
709                                                  result_res[0]->dn, name, info1);
710         }
711         case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
712                 const struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, result_res[0], "objectSid");
713                 const char *_acc = "", *_dom = "";
714                 
715                 if (!sid || (sid->num_auths < 4) || (sid->num_auths > 5)) {
716                         info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
717                         return WERR_OK;
718                 }
719
720                 if (sid->num_auths == 4) {
721                         ldb_ret = gendb_search(sam_ctx, mem_ctx, NULL, &domain_res, domain_attrs,
722                                                "(ncName=%s)", ldb_dn_linearize(mem_ctx, result_res[0]->dn));
723                         if (ldb_ret != 1) {
724                                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
725                                 return WERR_OK;
726                         }
727                         _dom = samdb_result_string(domain_res[0], "nETBIOSName", NULL);
728                         WERR_TALLOC_CHECK(_dom);
729                 
730                 } else if (sid->num_auths == 5) {
731                         const char *attrs[] = { NULL };
732                         struct ldb_message **domain_res2;
733                         struct dom_sid *dom_sid = dom_sid_dup(mem_ctx, sid);
734                         if (!dom_sid) {
735                                 return WERR_OK;
736                         }
737                         dom_sid->num_auths--;
738                         ldb_ret = gendb_search(sam_ctx, mem_ctx, NULL, &domain_res, attrs,
739                                                "(&(objectSid=%s)(objectClass=domain))", ldap_encode_ndr_dom_sid(mem_ctx, dom_sid));
740                         if (ldb_ret != 1) {
741                                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
742                                 return WERR_OK;
743                         }
744                         ldb_ret = gendb_search(sam_ctx, mem_ctx, NULL, &domain_res2, domain_attrs,
745                                                "(ncName=%s)", ldb_dn_linearize(mem_ctx, domain_res[0]->dn));
746                         if (ldb_ret != 1) {
747                                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
748                                 return WERR_OK;
749                         }
750                         
751                         _dom = samdb_result_string(domain_res2[0], "nETBIOSName", NULL);
752                         WERR_TALLOC_CHECK(_dom);
753
754                         _acc = samdb_result_string(result_res[0], "sAMAccountName", NULL);
755                         WERR_TALLOC_CHECK(_acc);
756                 }
757
758                 info1->result_name      = talloc_asprintf(mem_ctx, "%s\\%s", _dom, _acc);
759                 WERR_TALLOC_CHECK(info1->result_name);
760                 
761                 info1->status           = DRSUAPI_DS_NAME_STATUS_OK;
762                 return WERR_OK;
763         }
764         case DRSUAPI_DS_NAME_FORMAT_GUID: {
765                 struct GUID guid;
766                 
767                 guid = samdb_result_guid(result_res[0], "objectGUID");
768                 
769                 info1->result_name      = GUID_string2(mem_ctx, &guid);
770                 WERR_TALLOC_CHECK(info1->result_name);
771                 
772                 info1->status           = DRSUAPI_DS_NAME_STATUS_OK;
773                 return WERR_OK;
774         }
775         case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
776                 info1->result_name      = samdb_result_string(result_res[0], "displayName", NULL);
777                 if (!info1->result_name) {
778                         info1->result_name      = samdb_result_string(result_res[0], "sAMAccountName", NULL);
779                 } 
780                 if (!info1->result_name) {
781                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
782                 } else {
783                         info1->status = DRSUAPI_DS_NAME_STATUS_OK;
784                 }
785                 return WERR_OK;
786         }
787         default:
788                 return WERR_OK;
789         }
790         
791         return WERR_INVALID_PARAM;
792 }
793
794 /* Given a user Principal Name (such as foo@bar.com),
795  * return the user and domain DNs.  This is used in the KDC to then
796  * return the Keys and evaluate policy */
797
798 NTSTATUS crack_user_principal_name(struct ldb_context *sam_ctx, 
799                                    TALLOC_CTX *mem_ctx, 
800                                    const char *user_principal_name, 
801                                    struct ldb_dn **user_dn,
802                                    struct ldb_dn **domain_dn) 
803 {
804         WERROR werr;
805         struct drsuapi_DsNameInfo1 info1;
806         werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
807                                   DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
808                                   DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
809                                   user_principal_name,
810                                   &info1);
811         if (!W_ERROR_IS_OK(werr)) {
812                 return werror_to_ntstatus(werr);
813         }
814         switch (info1.status) {
815         case DRSUAPI_DS_NAME_STATUS_OK:
816                 break;
817         case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
818         case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
819         case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
820                 return NT_STATUS_NO_SUCH_USER;
821         case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
822         default:
823                 return NT_STATUS_UNSUCCESSFUL;
824         }
825         
826         *user_dn = ldb_dn_explode(mem_ctx, info1.result_name);
827         
828         if (domain_dn) {
829                 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
830                                           DRSUAPI_DS_NAME_FORMAT_CANONICAL,
831                                           DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
832                                           talloc_asprintf(mem_ctx, "%s/", 
833                                                           info1.dns_domain_name),
834                                           &info1);
835                 if (!W_ERROR_IS_OK(werr)) {
836                         return werror_to_ntstatus(werr);
837                 }
838                 switch (info1.status) {
839                 case DRSUAPI_DS_NAME_STATUS_OK:
840                         break;
841                 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
842                 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
843                 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
844                         return NT_STATUS_NO_SUCH_USER;
845                 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
846                 default:
847                         return NT_STATUS_UNSUCCESSFUL;
848                 }
849                 
850                 *domain_dn = ldb_dn_explode(mem_ctx, info1.result_name);
851         }
852
853         return NT_STATUS_OK;
854         
855 }
856
857 /* Given a Service Principal Name (such as host/foo.bar.com@BAR.COM),
858  * return the user and domain DNs.  This is used in the KDC to then
859  * return the Keys and evaluate policy */
860
861 NTSTATUS crack_service_principal_name(struct ldb_context *sam_ctx, 
862                                       TALLOC_CTX *mem_ctx, 
863                                       const char *service_principal_name, 
864                                       struct ldb_dn **user_dn,
865                                       struct ldb_dn **domain_dn) 
866 {
867         WERROR werr;
868         struct drsuapi_DsNameInfo1 info1;
869         werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
870                                   DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL,
871                                   DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
872                                   service_principal_name,
873                                   &info1);
874         if (!W_ERROR_IS_OK(werr)) {
875                 return werror_to_ntstatus(werr);
876         }
877         switch (info1.status) {
878         case DRSUAPI_DS_NAME_STATUS_OK:
879                 break;
880         case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
881         case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
882         case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
883                 return NT_STATUS_NO_SUCH_USER;
884         case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
885         default:
886                 return NT_STATUS_UNSUCCESSFUL;
887         }
888         
889         *user_dn = ldb_dn_explode(mem_ctx, info1.result_name);
890         
891         if (domain_dn) {
892                 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
893                                           DRSUAPI_DS_NAME_FORMAT_CANONICAL,
894                                           DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
895                                           talloc_asprintf(mem_ctx, "%s/", 
896                                                           info1.dns_domain_name),
897                                           &info1);
898                 if (!W_ERROR_IS_OK(werr)) {
899                         return werror_to_ntstatus(werr);
900                 }
901                 switch (info1.status) {
902                 case DRSUAPI_DS_NAME_STATUS_OK:
903                         break;
904                 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
905                 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
906                 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
907                         return NT_STATUS_NO_SUCH_USER;
908                 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
909                 default:
910                         return NT_STATUS_UNSUCCESSFUL;
911                 }
912                 
913                 *domain_dn = ldb_dn_explode(mem_ctx, info1.result_name);
914         }
915
916         return NT_STATUS_OK;
917         
918 }
919
920 NTSTATUS crack_dn_to_nt4_name(TALLOC_CTX *mem_ctx, 
921                               const char *dn, 
922                               const char **nt4_domain, const char **nt4_account)
923 {
924         WERROR werr;
925         struct drsuapi_DsNameInfo1 info1;
926         struct ldb_context *ldb;
927         char *p;
928
929         /* Handle anonymous bind */
930         if (!dn || !*dn) {
931                 *nt4_domain = "";
932                 *nt4_account = "";
933                 return NT_STATUS_OK;
934         }
935
936         ldb = samdb_connect(mem_ctx, system_session(mem_ctx));
937         if (ldb == NULL) {
938                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
939         }
940
941         werr = DsCrackNameOneName(ldb, mem_ctx, 0,
942                                   DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
943                                   DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT,
944                                   dn,
945                                   &info1);
946         if (!W_ERROR_IS_OK(werr)) {
947                 return werror_to_ntstatus(werr);
948         }
949         switch (info1.status) {
950         case DRSUAPI_DS_NAME_STATUS_OK:
951                 break;
952         case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
953         case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
954         case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
955                 return NT_STATUS_NO_SUCH_USER;
956         case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
957         default:
958                 return NT_STATUS_UNSUCCESSFUL;
959         }
960         
961         *nt4_domain = talloc_strdup(mem_ctx, info1.result_name);
962         
963         p = strchr(*nt4_domain, '\\');
964         if (!p) {
965                 return NT_STATUS_INVALID_PARAMETER;
966         }
967         p[0] = '\0';
968         
969         if (p[1]) {
970                 *nt4_account = talloc_strdup(mem_ctx, &p[1]);
971         }
972
973         if (!*nt4_account || !*nt4_domain) {
974                 return NT_STATUS_NO_MEMORY;
975         }
976
977         return NT_STATUS_OK;
978         
979 }