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