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