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