Don't use 'dn', this attribute does not exist with the LDAP backend,
[samba.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 3 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, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "librpc/gen_ndr/drsuapi.h"
26 #include "rpc_server/common/common.h"
27 #include "lib/ldb/include/ldb.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_ndr.h"
32 #include "libcli/security/security.h"
33 #include "librpc/gen_ndr/ndr_misc.h"
34 #include "auth/auth.h"
35 #include "util/util_ldb.h"
36 #include "dsdb/samdb/samdb.h"
37 #include "param/param.h"
38
39 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
40                                    struct smb_krb5_context *smb_krb5_context,
41                                    uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
42                                    struct ldb_dn *name_dn, const char *name, 
43                                    const char *domain_filter, const char *result_filter, 
44                                    struct drsuapi_DsNameInfo1 *info1);
45 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
46                                         uint32_t format_offered, uint32_t format_desired,
47                                         struct ldb_dn *name_dn, const char *name, 
48                                         struct drsuapi_DsNameInfo1 *info1);
49
50 static WERROR dns_domain_from_principal(TALLOC_CTX *mem_ctx, struct smb_krb5_context *smb_krb5_context, 
51                                         const char *name, 
52                                         struct drsuapi_DsNameInfo1 *info1) 
53 {
54         krb5_error_code ret;
55         krb5_principal principal;
56         /* perhaps it's a principal with a realm, so return the right 'domain only' response */
57         char **realm;
58         ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name, 
59                                     KRB5_PRINCIPAL_PARSE_MUST_REALM, &principal);
60         if (ret) {
61                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
62                 return WERR_OK;
63         }
64
65         /* This isn't an allocation assignemnt, so it is free'ed with the krb5_free_principal */
66         realm = krb5_princ_realm(smb_krb5_context->krb5_context, principal);
67         
68         info1->dns_domain_name  = talloc_strdup(mem_ctx, *realm);
69         krb5_free_principal(smb_krb5_context->krb5_context, principal);
70         
71         W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
72         
73         info1->status = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
74         return WERR_OK;
75 }               
76
77 static enum drsuapi_DsNameStatus LDB_lookup_spn_alias(krb5_context context, struct ldb_context *ldb_ctx, 
78                                                       TALLOC_CTX *mem_ctx,
79                                                       const char *alias_from,
80                                                       char **alias_to)
81 {
82         int i;
83         int ret;
84         struct ldb_result *res;
85         struct ldb_message_element *spnmappings;
86         TALLOC_CTX *tmp_ctx;
87         struct ldb_dn *service_dn;
88         char *service_dn_str;
89
90         const char *directory_attrs[] = {
91                 "sPNMappings", 
92                 NULL
93         };
94
95         tmp_ctx = talloc_new(mem_ctx);
96         if (!tmp_ctx) {
97                 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
98         }
99
100         service_dn = ldb_dn_new(tmp_ctx, ldb_ctx, "CN=Directory Service,CN=Windows NT,CN=Services");
101         if ( ! ldb_dn_add_base(service_dn, samdb_config_dn(ldb_ctx))) {
102                 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
103         }
104         service_dn_str = ldb_dn_alloc_linearized(tmp_ctx, service_dn);
105         if ( ! service_dn_str) {
106                 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
107         }
108
109         ret = ldb_search(ldb_ctx, service_dn, LDB_SCOPE_BASE, "(objectClass=nTDSService)",
110                          directory_attrs, &res);
111
112         if (ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_OBJECT) {
113                 DEBUG(1, ("ldb_search: dn: %s not found: %s", service_dn_str, ldb_errstring(ldb_ctx)));
114                 return DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
115         } else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
116                 DEBUG(1, ("ldb_search: dn: %s not found", service_dn_str));
117                 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
118         } else if (res->count != 1) {
119                 talloc_free(res);
120                 DEBUG(1, ("ldb_search: dn: %s not found", service_dn_str));
121                 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
122         }
123         talloc_steal(tmp_ctx, res);
124         
125         spnmappings = ldb_msg_find_element(res->msgs[0], "sPNMappings");
126         if (!spnmappings || spnmappings->num_values == 0) {
127                 DEBUG(1, ("ldb_search: dn: %s no sPNMappings attribute", service_dn_str));
128                 talloc_free(tmp_ctx);
129                 return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
130         }
131
132         for (i = 0; i < spnmappings->num_values; i++) {
133                 char *mapping, *p, *str;
134                 mapping = talloc_strdup(tmp_ctx, 
135                                         (const char *)spnmappings->values[i].data);
136                 if (!mapping) {
137                         DEBUG(1, ("LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping\n", service_dn_str));
138                         talloc_free(tmp_ctx);
139                         return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
140                 }
141                 
142                 /* C string manipulation sucks */
143                 
144                 p = strchr(mapping, '=');
145                 if (!p) {
146                         DEBUG(1, ("ldb_search: dn: %s sPNMapping malformed: %s\n", 
147                                   service_dn_str, mapping));
148                         talloc_free(tmp_ctx);
149                         return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
150                 }
151                 p[0] = '\0';
152                 p++;
153                 do {
154                         str = p;
155                         p = strchr(p, ',');
156                         if (p) {
157                                 p[0] = '\0';
158                                 p++;
159                         }
160                         if (strcasecmp(str, alias_from) == 0) {
161                                 *alias_to = mapping;
162                                 talloc_steal(mem_ctx, mapping);
163                                 talloc_free(tmp_ctx);
164                                 return DRSUAPI_DS_NAME_STATUS_OK;
165                         }
166                 } while (p);
167         }
168         DEBUG(4, ("LDB_lookup_spn_alias: no alias for service %s applicable\n", alias_from));
169         talloc_free(tmp_ctx);
170         return DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
171 }
172
173 /* When cracking a ServicePrincipalName, many services may be served
174  * by the host/ servicePrincipalName.  The incoming query is for cifs/
175  * but we translate it here, and search on host/.  This is done after
176  * the cifs/ entry has been searched for, making this a fallback */
177
178 static WERROR DsCrackNameSPNAlias(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
179                                   struct smb_krb5_context *smb_krb5_context,
180                                   uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
181                                   const char *name, struct drsuapi_DsNameInfo1 *info1)
182 {
183         WERROR wret;
184         krb5_error_code ret;
185         krb5_principal principal;
186         const char *service, *dns_name;
187         char *new_service;
188         char *new_princ;
189         enum drsuapi_DsNameStatus namestatus;
190         
191         /* parse principal */
192         ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, 
193                                     name, KRB5_PRINCIPAL_PARSE_NO_REALM, &principal);
194         if (ret) {
195                 DEBUG(2, ("Could not parse principal: %s: %s",
196                           name, smb_get_krb5_error_message(smb_krb5_context->krb5_context, 
197                                                            ret, mem_ctx)));
198                 return WERR_NOMEM;
199         }
200         
201         /* grab cifs/, http/ etc */
202         
203         /* This is checked for in callers, but be safe */
204         if (principal->name.name_string.len < 2) {
205                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
206                 return WERR_OK;
207         }
208         service = principal->name.name_string.val[0];
209         dns_name = principal->name.name_string.val[1];
210         
211         /* MAP it */
212         namestatus = LDB_lookup_spn_alias(smb_krb5_context->krb5_context, 
213                                           sam_ctx, mem_ctx, 
214                                           service, &new_service);
215         
216         if (namestatus == DRSUAPI_DS_NAME_STATUS_NOT_FOUND) {
217                 info1->status           = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
218                 info1->dns_domain_name  = talloc_strdup(mem_ctx, dns_name);
219                 if (!info1->dns_domain_name) {
220                         krb5_free_principal(smb_krb5_context->krb5_context, principal);
221                         return WERR_NOMEM;
222                 }
223                 return WERR_OK;
224         } else if (namestatus != DRSUAPI_DS_NAME_STATUS_OK) {
225                 info1->status = namestatus;
226                 krb5_free_principal(smb_krb5_context->krb5_context, principal);
227                 return WERR_OK;
228         }
229         
230         /* ooh, very nasty playing around in the Principal... */
231         free(principal->name.name_string.val[0]);
232         principal->name.name_string.val[0] = strdup(new_service);
233         if (!principal->name.name_string.val[0]) {
234                 krb5_free_principal(smb_krb5_context->krb5_context, principal);
235                 return WERR_NOMEM;
236         }
237         
238         /* reform principal */
239         ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal, 
240                                       KRB5_PRINCIPAL_UNPARSE_NO_REALM, &new_princ);
241
242         if (ret) {
243                 krb5_free_principal(smb_krb5_context->krb5_context, principal);
244                 return WERR_NOMEM;
245         }
246         
247         wret = DsCrackNameOneName(sam_ctx, mem_ctx, format_flags, format_offered, format_desired,
248                                   new_princ, info1);
249         free(new_princ);
250         if (W_ERROR_IS_OK(wret) && (info1->status == DRSUAPI_DS_NAME_STATUS_NOT_FOUND)) {
251                 info1->status           = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
252                 info1->dns_domain_name  = talloc_strdup(mem_ctx, dns_name);
253                 if (!info1->dns_domain_name) {
254                         wret = WERR_NOMEM;
255                 }
256         }
257         krb5_free_principal(smb_krb5_context->krb5_context, principal);
258         return wret;
259 }
260
261 /* Subcase of CrackNames, for the userPrincipalName */
262
263 static WERROR DsCrackNameUPN(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
264                              struct smb_krb5_context *smb_krb5_context,
265                              uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
266                              const char *name, struct drsuapi_DsNameInfo1 *info1)
267 {
268         int ldb_ret;
269         WERROR status;
270         const char *domain_filter = NULL;
271         const char *result_filter = NULL;
272         krb5_error_code ret;
273         krb5_principal principal;
274         char **realm;
275         char *unparsed_name_short;
276         const char *domain_attrs[] = { NULL };
277         struct ldb_result *domain_res = NULL;
278         
279         /* Prevent recursion */
280         if (!name) {
281                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
282                 return WERR_OK;
283         }
284
285         ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name, 
286                                     KRB5_PRINCIPAL_PARSE_MUST_REALM, &principal);
287         if (ret) {
288                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
289                 return WERR_OK;
290         }
291         
292         realm = krb5_princ_realm(smb_krb5_context->krb5_context, principal);
293
294         ldb_ret = ldb_search_exp_fmt(sam_ctx, mem_ctx, &domain_res, 
295                                      samdb_partitions_dn(sam_ctx, mem_ctx), 
296                                      LDB_SCOPE_ONELEVEL,
297                                      domain_attrs,
298                                      "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
299                                      ldb_binary_encode_string(mem_ctx, *realm), 
300                                      ldb_binary_encode_string(mem_ctx, *realm));
301
302         if (ldb_ret != LDB_SUCCESS) {
303                 DEBUG(2, ("DsCrackNameUPN domain ref search failed: %s", ldb_errstring(sam_ctx)));
304                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
305                 return WERR_OK;
306         }
307         
308         switch (domain_res->count) {
309         case 1:
310                 break;
311         case 0:
312                 return dns_domain_from_principal(mem_ctx, smb_krb5_context, 
313                                                  name, info1);
314         default:
315                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
316                 return WERR_OK;
317         }
318         
319         ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal, 
320                                       KRB5_PRINCIPAL_UNPARSE_NO_REALM, &unparsed_name_short);
321         krb5_free_principal(smb_krb5_context->krb5_context, principal);
322                 
323         if (ret) {
324                 free(unparsed_name_short);
325                 return WERR_NOMEM;
326         }
327         
328         /* This may need to be extended for more userPrincipalName variations */
329         result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))", 
330                                         ldb_binary_encode_string(mem_ctx, unparsed_name_short));
331
332         domain_filter = talloc_asprintf(mem_ctx, "(distinguishedName=%s)", ldb_dn_get_linearized(domain_res->msgs[0]->dn));
333
334         if (!result_filter || !domain_filter) {
335                 free(unparsed_name_short);
336                 return WERR_NOMEM;
337         }
338         status = DsCrackNameOneFilter(sam_ctx, mem_ctx, 
339                                       smb_krb5_context, 
340                                       format_flags, format_offered, format_desired, 
341                                       NULL, unparsed_name_short, domain_filter, result_filter, 
342                                       info1);
343         free(unparsed_name_short);
344
345         return status;
346 }
347
348 /* Crack a single 'name', from format_offered into format_desired, returning the result in info1 */
349
350 WERROR DsCrackNameOneName(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
351                           uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
352                           const char *name, struct drsuapi_DsNameInfo1 *info1)
353 {
354         krb5_error_code ret;
355         const char *domain_filter = NULL;
356         const char *result_filter = NULL;
357         struct ldb_dn *name_dn = NULL;
358
359         struct smb_krb5_context *smb_krb5_context;
360         ret = smb_krb5_init_context(mem_ctx, 
361                                     (struct event_context *)ldb_get_opaque(sam_ctx, "EventContext"), 
362                                     (struct loadparm_context *)ldb_get_opaque(sam_ctx, "loadparm"), 
363                                     &smb_krb5_context);
364                                 
365         if (ret) {
366                 return WERR_NOMEM;
367         }
368
369         info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
370         info1->dns_domain_name = NULL;
371         info1->result_name = NULL;
372
373         if (!name) {
374                 return WERR_INVALID_PARAM;
375         }
376
377         /* TODO: - fill the correct names in all cases!
378          *       - handle format_flags
379          */
380
381         /* here we need to set the domain_filter and/or the result_filter */
382         switch (format_offered) {
383         case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
384         case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
385         {
386                 char *str, *s, *account;
387                 
388                 if (strlen(name) == 0) {
389                         info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
390                         return WERR_OK;
391                 }
392                 
393                 str = talloc_strdup(mem_ctx, name);
394                 W_ERROR_HAVE_NO_MEMORY(str);
395                 
396                 if (format_offered == DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX) {
397                         /* Look backwards for the \n, and replace it with / */
398                         s = strrchr(str, '\n');
399                         if (!s) {
400                                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
401                                 return WERR_OK;
402                         }
403                         s[0] = '/';
404                 }
405
406                 s = strchr(str, '/');
407                 if (!s) {
408                         /* there must be at least one / */
409                         info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
410                         return WERR_OK;
411                 }
412                 
413                 s[0] = '\0';
414                 s++;
415
416                 domain_filter = talloc_asprintf(mem_ctx, "(&(objectClass=crossRef)(ncName=%s))", 
417                                                 ldb_dn_get_linearized(samdb_dns_domain_to_dn(sam_ctx, mem_ctx, str)));
418                 W_ERROR_HAVE_NO_MEMORY(domain_filter);
419
420                 /* There may not be anything after the domain component (search for the domain itself) */
421                 if (s[0]) {
422                         
423                         account = strrchr(s, '/');
424                         if (!account) {
425                                 account = s;
426                         } else {
427                                 account++;
428                         }
429                         account = ldb_binary_encode_string(mem_ctx, account);
430                         W_ERROR_HAVE_NO_MEMORY(account);
431                         result_filter = talloc_asprintf(mem_ctx, "(name=%s)",
432                                                         account);              
433                         W_ERROR_HAVE_NO_MEMORY(result_filter);
434                 }
435                 break;
436         }
437         case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
438                 char *p;
439                 char *domain;
440                 const char *account = NULL;
441                 
442                 domain = talloc_strdup(mem_ctx, name);
443                 W_ERROR_HAVE_NO_MEMORY(domain);
444                 
445                 p = strchr(domain, '\\');
446                 if (!p) {
447                         /* invalid input format */
448                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
449                         return WERR_OK;
450                 }
451                 p[0] = '\0';
452                 
453                 if (p[1]) {
454                         account = &p[1];
455                 }
456                 
457                 domain_filter = talloc_asprintf(mem_ctx, 
458                                                 "(&(&(nETBIOSName=%s)(objectclass=crossRef))(ncName=*))", 
459                                                 ldb_binary_encode_string(mem_ctx, domain));
460                 W_ERROR_HAVE_NO_MEMORY(domain_filter);
461                 if (account) {
462                         result_filter = talloc_asprintf(mem_ctx, "(sAMAccountName=%s)",
463                                                         ldb_binary_encode_string(mem_ctx, account));
464                         W_ERROR_HAVE_NO_MEMORY(result_filter);
465                 }
466                 
467                 talloc_free(domain);
468                 break;
469         }
470
471                 /* A LDAP DN as a string */
472         case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
473                 domain_filter = NULL;
474                 name_dn = ldb_dn_new(mem_ctx, sam_ctx, name);
475                 if (! ldb_dn_validate(name_dn)) {
476                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
477                         return WERR_OK;
478                 }
479                 break;
480         }
481
482                 /* A GUID as a string */
483         case DRSUAPI_DS_NAME_FORMAT_GUID: {
484                 struct GUID guid;
485                 char *ldap_guid;
486                 NTSTATUS nt_status;
487                 domain_filter = NULL;
488
489                 nt_status = GUID_from_string(name, &guid);
490                 if (!NT_STATUS_IS_OK(nt_status)) {
491                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
492                         return WERR_OK;
493                 }
494                         
495                 ldap_guid = ldap_encode_ndr_GUID(mem_ctx, &guid);
496                 if (!ldap_guid) {
497                         return WERR_NOMEM;
498                 }
499                 result_filter = talloc_asprintf(mem_ctx, "(objectGUID=%s)",
500                                                 ldap_guid);
501                 W_ERROR_HAVE_NO_MEMORY(result_filter);
502                 break;
503         }
504         case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
505                 domain_filter = NULL;
506
507                 result_filter = talloc_asprintf(mem_ctx, "(|(displayName=%s)(samAccountName=%s))",
508                                                 ldb_binary_encode_string(mem_ctx, name), 
509                                                 ldb_binary_encode_string(mem_ctx, name));
510                 W_ERROR_HAVE_NO_MEMORY(result_filter);
511                 break;
512         }
513         
514                 /* A S-1234-5678 style string */
515         case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY: {
516                 struct dom_sid *sid = dom_sid_parse_talloc(mem_ctx, name);
517                 char *ldap_sid;
518                                                                             
519                 domain_filter = NULL;
520                 if (!sid) {
521                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
522                         return WERR_OK;
523                 }
524                 ldap_sid = ldap_encode_ndr_dom_sid(mem_ctx, 
525                                                    sid);
526                 if (!ldap_sid) {
527                         return WERR_NOMEM;
528                 }
529                 result_filter = talloc_asprintf(mem_ctx, "(objectSid=%s)",
530                                                 ldap_sid);
531                 W_ERROR_HAVE_NO_MEMORY(result_filter);
532                 break;
533         }
534         case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL: {
535                 krb5_principal principal;
536                 char *unparsed_name;
537                 ret = krb5_parse_name(smb_krb5_context->krb5_context, name, &principal);
538                 if (ret) {
539                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
540                         return WERR_OK;
541                 }
542                 
543                 domain_filter = NULL;
544                 
545                 ret = krb5_unparse_name(smb_krb5_context->krb5_context, principal, &unparsed_name);
546                 if (ret) {
547                         krb5_free_principal(smb_krb5_context->krb5_context, principal);
548                         return WERR_NOMEM;
549                 }
550
551                 krb5_free_principal(smb_krb5_context->krb5_context, principal);
552                 result_filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(userPrincipalName=%s))", 
553                                                 ldb_binary_encode_string(mem_ctx, unparsed_name));
554                 
555                 free(unparsed_name);
556                 W_ERROR_HAVE_NO_MEMORY(result_filter);
557                 break;
558         }
559         case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: {
560                 krb5_principal principal;
561                 char *unparsed_name_short;
562                 char *service;
563                 ret = krb5_parse_name(smb_krb5_context->krb5_context, name, &principal);
564                 if (ret == 0 && principal->name.name_string.len < 2) {
565                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
566                         krb5_free_principal(smb_krb5_context->krb5_context, principal);
567                         return WERR_OK;
568                 }
569                 ret = krb5_parse_name_flags(smb_krb5_context->krb5_context, name, 
570                                             KRB5_PRINCIPAL_PARSE_NO_REALM, &principal);
571                 if (ret) {
572                         krb5_free_principal(smb_krb5_context->krb5_context, principal);
573
574                         return dns_domain_from_principal(mem_ctx, smb_krb5_context,
575                                                          name, info1);
576                 }
577
578                 domain_filter = NULL;
579                 
580                 ret = krb5_unparse_name_flags(smb_krb5_context->krb5_context, principal, 
581                                               KRB5_PRINCIPAL_UNPARSE_NO_REALM, &unparsed_name_short);
582                 if (ret) {
583                         krb5_free_principal(smb_krb5_context->krb5_context, principal);
584                         return WERR_NOMEM;
585                 }
586
587                 service = principal->name.name_string.val[0];
588                 if ((principal->name.name_string.len == 2) && (strcasecmp(service, "host") == 0)) {
589                         /* the 'cn' attribute is just the leading part of the name */
590                         char *computer_name;
591                         computer_name = talloc_strndup(mem_ctx, principal->name.name_string.val[1], 
592                                                       strcspn(principal->name.name_string.val[1], "."));
593                         if (computer_name == NULL) {
594                                 return WERR_NOMEM;
595                         }
596
597                         result_filter = talloc_asprintf(mem_ctx, "(|(&(servicePrincipalName=%s)(objectClass=user))(&(cn=%s)(objectClass=computer)))", 
598                                                         ldb_binary_encode_string(mem_ctx, unparsed_name_short), 
599                                                         ldb_binary_encode_string(mem_ctx, computer_name));
600                 } else {
601                         result_filter = talloc_asprintf(mem_ctx, "(&(servicePrincipalName=%s)(objectClass=user))",
602                                                         ldb_binary_encode_string(mem_ctx, unparsed_name_short));
603                 }
604                 krb5_free_principal(smb_krb5_context->krb5_context, principal);
605                 free(unparsed_name_short);
606                 W_ERROR_HAVE_NO_MEMORY(result_filter);
607                 
608                 break;
609         }
610         default: {
611                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
612                 return WERR_OK;
613         }
614
615         }
616
617         if (format_flags & DRSUAPI_DS_NAME_FLAG_SYNTACTICAL_ONLY) {
618                 return DsCrackNameOneSyntactical(mem_ctx, format_offered, format_desired,
619                                                  name_dn, name, info1);
620         }
621         
622         return DsCrackNameOneFilter(sam_ctx, mem_ctx, 
623                                     smb_krb5_context, 
624                                     format_flags, format_offered, format_desired, 
625                                     name_dn, name, 
626                                     domain_filter, result_filter, 
627                                     info1);
628 }
629
630 /* Subcase of CrackNames.  It is possible to translate a LDAP-style DN
631  * (FQDN_1779) into a canoical name without actually searching the
632  * database */
633
634 static WERROR DsCrackNameOneSyntactical(TALLOC_CTX *mem_ctx,
635                                         uint32_t format_offered, uint32_t format_desired,
636                                         struct ldb_dn *name_dn, const char *name, 
637                                         struct drsuapi_DsNameInfo1 *info1)
638 {
639         char *cracked;
640         if (format_offered != DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
641                 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
642                 return WERR_OK;
643         }
644
645         switch (format_desired) {
646         case DRSUAPI_DS_NAME_FORMAT_CANONICAL: 
647                 cracked = ldb_dn_canonical_string(mem_ctx, name_dn);
648                 break;
649         case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
650                 cracked = ldb_dn_canonical_ex_string(mem_ctx, name_dn);
651                 break;
652         default:
653                 info1->status = DRSUAPI_DS_NAME_STATUS_NO_SYNTACTICAL_MAPPING;
654                 return WERR_OK;
655         }
656         info1->status = DRSUAPI_DS_NAME_STATUS_OK;
657         info1->result_name      = cracked;
658         if (!cracked) {
659                 return WERR_NOMEM;
660         }
661         
662         return WERR_OK; 
663 }
664
665 /* Given a filter for the domain, and one for the result, perform the
666  * ldb search. The format offered and desired flags change the
667  * behaviours, including what attributes to return.
668  *
669  * The smb_krb5_context is required because we use the krb5 libs for principal parsing
670  */
671
672 static WERROR DsCrackNameOneFilter(struct ldb_context *sam_ctx, TALLOC_CTX *mem_ctx,
673                                    struct smb_krb5_context *smb_krb5_context,
674                                    uint32_t format_flags, uint32_t format_offered, uint32_t format_desired,
675                                    struct ldb_dn *name_dn, const char *name, 
676                                    const char *domain_filter, const char *result_filter, 
677                                    struct drsuapi_DsNameInfo1 *info1)
678 {
679         int ldb_ret;
680         struct ldb_result *domain_res = NULL;
681         const char * const *domain_attrs;
682         const char * const *result_attrs;
683         struct ldb_message **result_res = NULL;
684         struct ldb_message *result = NULL;
685         struct ldb_dn *result_basedn = NULL;
686         int i;
687         char *p;
688         struct ldb_dn *partitions_basedn = samdb_partitions_dn(sam_ctx, mem_ctx);
689
690         const char * const _domain_attrs_1779[] = { "ncName", "dnsRoot", NULL};
691         const char * const _result_attrs_null[] = { NULL };
692
693         const char * const _domain_attrs_canonical[] = { "ncName", "dnsRoot", NULL};
694         const char * const _result_attrs_canonical[] = { "canonicalName", NULL };
695
696         const char * const _domain_attrs_nt4[] = { "ncName", "dnsRoot", "nETBIOSName", NULL};
697         const char * const _result_attrs_nt4[] = { "sAMAccountName", "objectSid", "objectClass", NULL};
698                 
699         const char * const _domain_attrs_guid[] = { "ncName", "dnsRoot", NULL};
700         const char * const _result_attrs_guid[] = { "objectGUID", NULL};
701                 
702         const char * const _domain_attrs_display[] = { "ncName", "dnsRoot", NULL};
703         const char * const _result_attrs_display[] = { "displayName", "samAccountName", NULL};
704
705         const char * const _domain_attrs_none[] = { "ncName", "dnsRoot" , NULL};
706         const char * const _result_attrs_none[] = { NULL};
707
708         /* here we need to set the attrs lists for domain and result lookups */
709         switch (format_desired) {
710         case DRSUAPI_DS_NAME_FORMAT_FQDN_1779:
711         case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
712                 domain_attrs = _domain_attrs_1779;
713                 result_attrs = _result_attrs_null;
714                 break;
715         case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
716                 domain_attrs = _domain_attrs_canonical;
717                 result_attrs = _result_attrs_canonical;
718                 break;
719         case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT:
720                 domain_attrs = _domain_attrs_nt4;
721                 result_attrs = _result_attrs_nt4;
722                 break;
723         case DRSUAPI_DS_NAME_FORMAT_GUID:               
724                 domain_attrs = _domain_attrs_guid;
725                 result_attrs = _result_attrs_guid;
726                 break;
727         case DRSUAPI_DS_NAME_FORMAT_DISPLAY:            
728                 domain_attrs = _domain_attrs_display;
729                 result_attrs = _result_attrs_display;
730                 break;
731         default:
732                 domain_attrs = _domain_attrs_none;
733                 result_attrs = _result_attrs_none;
734                 break;
735         }
736
737         if (domain_filter) {
738                 /* if we have a domain_filter look it up and set the result_basedn and the dns_domain_name */
739                 ldb_ret = ldb_search_exp_fmt(sam_ctx, mem_ctx, &domain_res, 
740                                              partitions_basedn,
741                                              LDB_SCOPE_ONELEVEL,
742                                              domain_attrs,
743                                              "%s", domain_filter);
744                 
745                 if (ldb_ret != LDB_SUCCESS) {
746                         DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s", ldb_errstring(sam_ctx)));
747                         info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
748                         return WERR_OK;
749                 }
750                 
751                 switch (domain_res->count) {
752                 case 1:
753                         break;
754                 case 0:
755                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
756                         return WERR_OK;
757                 default:
758                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
759                         return WERR_OK;
760                 }
761
762                 info1->dns_domain_name  = samdb_result_string(domain_res->msgs[0], "dnsRoot", NULL);
763                 W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
764                 info1->status           = DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY;
765         } else {
766                 info1->dns_domain_name  = NULL;
767                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
768         }
769
770         if (result_filter) {
771                 int ret;
772                 struct ldb_result *res;
773                 if (domain_res) {
774                         result_basedn = samdb_result_dn(sam_ctx, mem_ctx, domain_res->msgs[0], "ncName", NULL);
775                         
776                         ret = ldb_search_exp_fmt(sam_ctx, mem_ctx, &res, 
777                                                  result_basedn, LDB_SCOPE_SUBTREE, 
778                                                  result_attrs, "%s", result_filter);
779                         if (ret != LDB_SUCCESS) {
780                                 talloc_free(result_res);
781                                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
782                                 return WERR_OK;
783                         }
784                         ldb_ret = res->count;
785                         result_res = res->msgs;
786                 } else {
787                         /* search with the 'phantom root' flag */
788                         struct ldb_request *req;
789
790                         res = talloc_zero(mem_ctx, struct ldb_result);
791                         W_ERROR_HAVE_NO_MEMORY(res);
792                         
793                         ret = ldb_build_search_req(&req, sam_ctx, mem_ctx,
794                                                    ldb_get_root_basedn(sam_ctx),
795                                                    LDB_SCOPE_SUBTREE,
796                                                    result_filter,
797                                                    result_attrs,
798                                                    NULL,
799                                                    res,
800                                                    ldb_search_default_callback);
801                         if (ret == LDB_SUCCESS) {
802                                 struct ldb_search_options_control *search_options;
803                                 search_options = talloc(req, struct ldb_search_options_control);
804                                 W_ERROR_HAVE_NO_MEMORY(search_options);
805                                 search_options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
806
807                                 ret = ldb_request_add_control(req, LDB_CONTROL_SEARCH_OPTIONS_OID, false, search_options);
808                         }
809                         if (ret != LDB_SUCCESS) {
810                                 talloc_free(res);
811                                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
812                                 return WERR_OK;
813                         }
814                         
815                         ldb_set_timeout(sam_ctx, req, 0); /* use default timeout */
816                         
817                         ret = ldb_request(sam_ctx, req);
818                         
819                         if (ret == LDB_SUCCESS) {
820                                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
821                         }
822                         
823                         talloc_free(req);
824
825                         if (ret != LDB_SUCCESS) {
826                                 DEBUG(2, ("DsCrackNameOneFilter phantom root search failed: %s", 
827                                           ldb_errstring(sam_ctx)));
828                                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
829                                 return WERR_OK;
830                         }
831                         ldb_ret = res->count;
832                         result_res = res->msgs;
833                 }
834         } else if (format_offered == DRSUAPI_DS_NAME_FORMAT_FQDN_1779) {
835                 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
836                                           result_attrs);
837         } else if (domain_res) {
838                 name_dn = samdb_result_dn(sam_ctx, mem_ctx, domain_res->msgs[0], "ncName", NULL);
839                 ldb_ret = gendb_search_dn(sam_ctx, mem_ctx, name_dn, &result_res,
840                                           result_attrs);
841         } else {
842                 /* Can't happen */
843                 DEBUG(0, ("LOGIC ERROR: DsCrackNameOneFilter domain ref search not availible: This can't happen..."));
844                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
845                 return WERR_OK;
846         }
847
848         switch (ldb_ret) {
849         case 1:
850                 result = result_res[0];
851                 break;
852         case 0:
853                 switch (format_offered) {
854                 case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: 
855                         return DsCrackNameSPNAlias(sam_ctx, mem_ctx, 
856                                                    smb_krb5_context, 
857                                                    format_flags, format_offered, format_desired,
858                                                    name, info1);
859                         
860                 case DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL:
861                         return DsCrackNameUPN(sam_ctx, mem_ctx, smb_krb5_context, 
862                                               format_flags, format_offered, format_desired,
863                                               name, info1);
864                 }
865                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
866                 return WERR_OK;
867         case -1:
868                 DEBUG(2, ("DsCrackNameOneFilter result search failed: %s", ldb_errstring(sam_ctx)));
869                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
870                 return WERR_OK;
871         default:
872                 switch (format_offered) {
873                 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
874                 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
875                 {
876                         const char *canonical_name = NULL; /* Not required, but we get warnings... */
877                         /* We may need to manually filter further */
878                         for (i = 0; i < ldb_ret; i++) {
879                                 switch (format_offered) {
880                                 case DRSUAPI_DS_NAME_FORMAT_CANONICAL:
881                                         canonical_name = ldb_dn_canonical_string(mem_ctx, result_res[i]->dn);
882                                         break;
883                                 case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX:
884                                         canonical_name = ldb_dn_canonical_ex_string(mem_ctx, result_res[i]->dn);
885                                         break;
886                                 }
887                                 if (strcasecmp_m(canonical_name, name) == 0) {
888                                         result = result_res[i];
889                                         break;
890                                 }
891                         }
892                         if (!result) {
893                                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
894                                 return WERR_OK;
895                         }
896                 }
897                 default:
898                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
899                         return WERR_OK;
900                 }
901         }
902
903         info1->dns_domain_name = ldb_dn_canonical_string(mem_ctx, result->dn);
904         W_ERROR_HAVE_NO_MEMORY(info1->dns_domain_name);
905         p = strchr(info1->dns_domain_name, '/');
906         if (p) {
907                 p[0] = '\0';
908         }
909         
910         /* here we can use result and domain_res[0] */
911         switch (format_desired) {
912         case DRSUAPI_DS_NAME_FORMAT_FQDN_1779: {
913                 info1->result_name      = ldb_dn_alloc_linearized(mem_ctx, result->dn);
914                 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
915
916                 info1->status           = DRSUAPI_DS_NAME_STATUS_OK;
917                 return WERR_OK;
918         }
919         case DRSUAPI_DS_NAME_FORMAT_CANONICAL: {
920                 info1->result_name      = samdb_result_string(result, "canonicalName", NULL);
921                 info1->status           = DRSUAPI_DS_NAME_STATUS_OK;
922                 return WERR_OK;
923         }
924         case DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX: {
925                 /* Not in the virtual ldb attribute */
926                 return DsCrackNameOneSyntactical(mem_ctx, 
927                                                  DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
928                                                  DRSUAPI_DS_NAME_FORMAT_CANONICAL_EX,
929                                                  result->dn, name, info1);
930         }
931         case DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT: {
932
933                 const struct dom_sid *sid = samdb_result_dom_sid(mem_ctx, result, "objectSid");
934                 const char *_acc = "", *_dom = "";
935                 
936                 if (samdb_find_attribute(sam_ctx, result, "objectClass", "domain")) {
937
938                         ldb_ret = ldb_search_exp_fmt(sam_ctx, mem_ctx, &domain_res, 
939                                                      partitions_basedn,
940                                                      LDB_SCOPE_ONELEVEL,
941                                                      domain_attrs,
942                                                      "(ncName=%s)", ldb_dn_get_linearized(result->dn));
943                         
944                         if (ldb_ret != LDB_SUCCESS) {
945                                 DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s", ldb_errstring(sam_ctx)));
946                                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
947                                 return WERR_OK;
948                         }
949                         
950                         switch (domain_res->count) {
951                         case 1:
952                                 break;
953                         case 0:
954                                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
955                                 return WERR_OK;
956                         default:
957                                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
958                                 return WERR_OK;
959                         }
960                         _dom = samdb_result_string(domain_res->msgs[0], "nETBIOSName", NULL);
961                         W_ERROR_HAVE_NO_MEMORY(_dom);
962                 } else {
963                         _acc = samdb_result_string(result, "sAMAccountName", NULL);
964                         if (!_acc) {
965                                 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
966                                 return WERR_OK;
967                         }
968                         if (dom_sid_in_domain(dom_sid_parse_talloc(mem_ctx, SID_BUILTIN), sid)) {
969                                 _dom = "BUILTIN";
970                         } else {
971                                 const char *attrs[] = { NULL };
972                                 struct ldb_result *domain_res2;
973                                 struct dom_sid *dom_sid = dom_sid_dup(mem_ctx, sid);
974                                 if (!dom_sid) {
975                                         return WERR_OK;
976                                 }
977                                 dom_sid->num_auths--;
978                                 ldb_ret = ldb_search_exp_fmt(sam_ctx, mem_ctx, &domain_res, 
979                                                              NULL,
980                                                              LDB_SCOPE_BASE,
981                                                              attrs,
982                                                              "(&(objectSid=%s)(objectClass=domain))", 
983                                                              ldap_encode_ndr_dom_sid(mem_ctx, dom_sid));
984                                 
985                                 if (ldb_ret != LDB_SUCCESS) {
986                                         DEBUG(2, ("DsCrackNameOneFilter domain search failed: %s", ldb_errstring(sam_ctx)));
987                                         info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
988                                         return WERR_OK;
989                                 }
990                                 
991                                 switch (domain_res->count) {
992                                 case 1:
993                                         break;
994                                 case 0:
995                                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
996                                         return WERR_OK;
997                                 default:
998                                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
999                                         return WERR_OK;
1000                                 }
1001
1002                                 ldb_ret = ldb_search_exp_fmt(sam_ctx, mem_ctx, &domain_res2, 
1003                                                              partitions_basedn,
1004                                                              LDB_SCOPE_ONELEVEL,
1005                                                              domain_attrs,
1006                                                              "(ncName=%s)", ldb_dn_get_linearized(domain_res->msgs[0]->dn));
1007                                 
1008                                 if (ldb_ret != LDB_SUCCESS) {
1009                                         DEBUG(2, ("DsCrackNameOneFilter domain ref search failed: %s", ldb_errstring(sam_ctx)));
1010                                         info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1011                                         return WERR_OK;
1012                                 }
1013                                 
1014                                 switch (domain_res2->count) {
1015                                 case 1:
1016                                         break;
1017                                 case 0:
1018                                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1019                                         return WERR_OK;
1020                                 default:
1021                                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1022                                         return WERR_OK;
1023                                 }
1024                                 _dom = samdb_result_string(domain_res2->msgs[0], "nETBIOSName", NULL);
1025                                 W_ERROR_HAVE_NO_MEMORY(_dom);
1026                         }
1027                 }
1028
1029                 info1->result_name      = talloc_asprintf(mem_ctx, "%s\\%s", _dom, _acc);
1030                 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
1031                 
1032                 info1->status           = DRSUAPI_DS_NAME_STATUS_OK;
1033                 return WERR_OK;
1034         }
1035         case DRSUAPI_DS_NAME_FORMAT_GUID: {
1036                 struct GUID guid;
1037                 
1038                 guid = samdb_result_guid(result, "objectGUID");
1039                 
1040                 info1->result_name      = GUID_string2(mem_ctx, &guid);
1041                 W_ERROR_HAVE_NO_MEMORY(info1->result_name);
1042                 
1043                 info1->status           = DRSUAPI_DS_NAME_STATUS_OK;
1044                 return WERR_OK;
1045         }
1046         case DRSUAPI_DS_NAME_FORMAT_DISPLAY: {
1047                 info1->result_name      = samdb_result_string(result, "displayName", NULL);
1048                 if (!info1->result_name) {
1049                         info1->result_name      = samdb_result_string(result, "sAMAccountName", NULL);
1050                 } 
1051                 if (!info1->result_name) {
1052                         info1->status = DRSUAPI_DS_NAME_STATUS_NOT_FOUND;
1053                 } else {
1054                         info1->status = DRSUAPI_DS_NAME_STATUS_OK;
1055                 }
1056                 return WERR_OK;
1057         }
1058         case DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL: {
1059                 info1->status = DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE;
1060                 return WERR_OK;
1061         }
1062         case DRSUAPI_DS_NAME_FORMAT_DNS_DOMAIN: 
1063         case DRSUAPI_DS_NAME_FORMAT_SID_OR_SID_HISTORY: {
1064                 info1->status = DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR;
1065                 return WERR_OK;
1066         }
1067         default:
1068                 info1->status = DRSUAPI_DS_NAME_STATUS_NO_MAPPING;
1069                 return WERR_OK;
1070         }
1071 }
1072
1073 /* Given a user Principal Name (such as foo@bar.com),
1074  * return the user and domain DNs.  This is used in the KDC to then
1075  * return the Keys and evaluate policy */
1076
1077 NTSTATUS crack_user_principal_name(struct ldb_context *sam_ctx, 
1078                                    TALLOC_CTX *mem_ctx, 
1079                                    const char *user_principal_name, 
1080                                    struct ldb_dn **user_dn,
1081                                    struct ldb_dn **domain_dn) 
1082 {
1083         WERROR werr;
1084         struct drsuapi_DsNameInfo1 info1;
1085         werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1086                                   DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL,
1087                                   DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
1088                                   user_principal_name,
1089                                   &info1);
1090         if (!W_ERROR_IS_OK(werr)) {
1091                 return werror_to_ntstatus(werr);
1092         }
1093         switch (info1.status) {
1094         case DRSUAPI_DS_NAME_STATUS_OK:
1095                 break;
1096         case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1097         case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1098         case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1099                 return NT_STATUS_NO_SUCH_USER;
1100         case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1101         default:
1102                 return NT_STATUS_UNSUCCESSFUL;
1103         }
1104         
1105         *user_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1106         
1107         if (domain_dn) {
1108                 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1109                                           DRSUAPI_DS_NAME_FORMAT_CANONICAL,
1110                                           DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
1111                                           talloc_asprintf(mem_ctx, "%s/", 
1112                                                           info1.dns_domain_name),
1113                                           &info1);
1114                 if (!W_ERROR_IS_OK(werr)) {
1115                         return werror_to_ntstatus(werr);
1116                 }
1117                 switch (info1.status) {
1118                 case DRSUAPI_DS_NAME_STATUS_OK:
1119                         break;
1120                 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1121                 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1122                 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1123                         return NT_STATUS_NO_SUCH_USER;
1124                 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1125                 default:
1126                         return NT_STATUS_UNSUCCESSFUL;
1127                 }
1128                 
1129                 *domain_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1130         }
1131
1132         return NT_STATUS_OK;
1133         
1134 }
1135
1136 /* Given a Service Principal Name (such as host/foo.bar.com@BAR.COM),
1137  * return the user and domain DNs.  This is used in the KDC to then
1138  * return the Keys and evaluate policy */
1139
1140 NTSTATUS crack_service_principal_name(struct ldb_context *sam_ctx, 
1141                                       TALLOC_CTX *mem_ctx, 
1142                                       const char *service_principal_name, 
1143                                       struct ldb_dn **user_dn,
1144                                       struct ldb_dn **domain_dn) 
1145 {
1146         WERROR werr;
1147         struct drsuapi_DsNameInfo1 info1;
1148         werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1149                                   DRSUAPI_DS_NAME_FORMAT_SERVICE_PRINCIPAL,
1150                                   DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
1151                                   service_principal_name,
1152                                   &info1);
1153         if (!W_ERROR_IS_OK(werr)) {
1154                 return werror_to_ntstatus(werr);
1155         }
1156         switch (info1.status) {
1157         case DRSUAPI_DS_NAME_STATUS_OK:
1158                 break;
1159         case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1160         case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1161         case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1162                 return NT_STATUS_NO_SUCH_USER;
1163         case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1164         default:
1165                 return NT_STATUS_UNSUCCESSFUL;
1166         }
1167         
1168         *user_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1169         
1170         if (domain_dn) {
1171                 werr = DsCrackNameOneName(sam_ctx, mem_ctx, 0,
1172                                           DRSUAPI_DS_NAME_FORMAT_CANONICAL,
1173                                           DRSUAPI_DS_NAME_FORMAT_FQDN_1779, 
1174                                           talloc_asprintf(mem_ctx, "%s/", 
1175                                                           info1.dns_domain_name),
1176                                           &info1);
1177                 if (!W_ERROR_IS_OK(werr)) {
1178                         return werror_to_ntstatus(werr);
1179                 }
1180                 switch (info1.status) {
1181                 case DRSUAPI_DS_NAME_STATUS_OK:
1182                         break;
1183                 case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1184                 case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1185                 case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1186                         return NT_STATUS_NO_SUCH_USER;
1187                 case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1188                 default:
1189                         return NT_STATUS_UNSUCCESSFUL;
1190                 }
1191                 
1192                 *domain_dn = ldb_dn_new(mem_ctx, sam_ctx, info1.result_name);
1193         }
1194
1195         return NT_STATUS_OK;
1196         
1197 }
1198
1199 NTSTATUS crack_name_to_nt4_name(TALLOC_CTX *mem_ctx, 
1200                                 struct loadparm_context *lp_ctx,
1201                                 uint32_t format_offered,
1202                                 const char *name, 
1203                                 const char **nt4_domain, const char **nt4_account)
1204 {
1205         WERROR werr;
1206         struct drsuapi_DsNameInfo1 info1;
1207         struct ldb_context *ldb;
1208         char *p;
1209
1210         /* Handle anonymous bind */
1211         if (!name || !*name) {
1212                 *nt4_domain = "";
1213                 *nt4_account = "";
1214                 return NT_STATUS_OK;
1215         }
1216
1217         ldb = samdb_connect(mem_ctx, lp_ctx, system_session(mem_ctx, lp_ctx));
1218         if (ldb == NULL) {
1219                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
1220         }
1221
1222         werr = DsCrackNameOneName(ldb, mem_ctx, 0,
1223                                   format_offered, 
1224                                   DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT,
1225                                   name,
1226                                   &info1);
1227         if (!W_ERROR_IS_OK(werr)) {
1228                 return werror_to_ntstatus(werr);
1229         }
1230         switch (info1.status) {
1231         case DRSUAPI_DS_NAME_STATUS_OK:
1232                 break;
1233         case DRSUAPI_DS_NAME_STATUS_NOT_FOUND:
1234         case DRSUAPI_DS_NAME_STATUS_DOMAIN_ONLY:
1235         case DRSUAPI_DS_NAME_STATUS_NOT_UNIQUE:
1236                 return NT_STATUS_NO_SUCH_USER;
1237         case DRSUAPI_DS_NAME_STATUS_RESOLVE_ERROR:
1238         default:
1239                 return NT_STATUS_UNSUCCESSFUL;
1240         }
1241         
1242         *nt4_domain = talloc_strdup(mem_ctx, info1.result_name);
1243         
1244         p = strchr(*nt4_domain, '\\');
1245         if (!p) {
1246                 return NT_STATUS_INVALID_PARAMETER;
1247         }
1248         p[0] = '\0';
1249         
1250         if (p[1]) {
1251                 *nt4_account = talloc_strdup(mem_ctx, &p[1]);
1252         }
1253
1254         if (!*nt4_account || !*nt4_domain) {
1255                 return NT_STATUS_NO_MEMORY;
1256         }
1257
1258         return NT_STATUS_OK;
1259 }
1260
1261 NTSTATUS crack_auto_name_to_nt4_name(TALLOC_CTX *mem_ctx,
1262                                      struct loadparm_context *lp_ctx,
1263                                      const char *name,
1264                                      const char **nt4_domain,
1265                                      const char **nt4_account)
1266 {
1267         uint32_t format_offered = DRSUAPI_DS_NAME_FORMAT_UKNOWN;
1268
1269         /* Handle anonymous bind */
1270         if (!name || !*name) {
1271                 *nt4_domain = "";
1272                 *nt4_account = "";
1273                 return NT_STATUS_OK;
1274         }
1275
1276         if (strchr_m(name, '=')) {
1277                 format_offered = DRSUAPI_DS_NAME_FORMAT_FQDN_1779;
1278         } else if (strchr_m(name, '@')) {
1279                 format_offered = DRSUAPI_DS_NAME_FORMAT_USER_PRINCIPAL;
1280         } else if (strchr_m(name, '\\')) {
1281                 format_offered = DRSUAPI_DS_NAME_FORMAT_NT4_ACCOUNT;
1282         } else if (strchr_m(name, '/')) {
1283                 format_offered = DRSUAPI_DS_NAME_FORMAT_CANONICAL;
1284         }
1285
1286         return crack_name_to_nt4_name(mem_ctx, lp_ctx, format_offered, name, nt4_domain, nt4_account);
1287 }