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