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