r9084: 'resign' the sample PAC for the validation of the signature algorithms.
[ira/wip.git] / source4 / kdc / pac-glue.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    PAC Glue between Samba and the KDC
5    
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "kdc/kdc.h"
26 #include "kdc/pac-glue.h" /* Ensure we don't get this prototype wrong, as that could be painful */
27
28  krb5_error_code samba_get_pac(krb5_context context, 
29                               struct krb5_kdc_configuration *config,
30                               krb5_principal client, 
31                               krb5_keyblock *krbtgt_keyblock, 
32                               krb5_keyblock *server_keyblock, 
33                               krb5_data *pac) 
34 {
35         krb5_error_code ret;
36         NTSTATUS nt_status;
37         struct auth_serversupplied_info *server_info;
38         char *username, *p;
39         const char *realm;
40         DATA_BLOB tmp_blob;
41         TALLOC_CTX *mem_ctx = talloc_named(config, 0, "samba_get_pac context");
42         if (!mem_ctx) {
43                 return ENOMEM;
44         }
45
46         ret = krb5_unparse_name(context, client, &username);
47
48         if (ret != 0) {
49                 krb5_set_error_string(context, "get pac: could not parse principal");
50                 krb5_warnx(context, "get pac: could not parse principal");
51                 talloc_free(mem_ctx);
52                 return ret;
53         }
54
55         /* parse the principal name */
56         realm = krb5_principal_get_realm(context, client);
57         username = talloc_strdup(mem_ctx, username);
58         p = strchr(username, '@');
59         if (p) {
60                 p[0] = '\0';
61         }
62
63
64         nt_status = sam_get_server_info(mem_ctx, username, realm, 
65                                         data_blob(NULL, 0), data_blob(NULL, 0),
66                                         &server_info);
67         if (!NT_STATUS_IS_OK(nt_status)) {
68                 DEBUG(0, ("Getting user info for PAC failed: %s\n",
69                           nt_errstr(nt_status)));
70                 return EINVAL;
71         }
72
73         ret = kerberos_create_pac(mem_ctx, server_info, 
74                                   context, 
75                                   krbtgt_keyblock,
76                                   server_keyblock,
77                                   &tmp_blob);
78
79         if (ret) {
80                 DEBUG(1, ("PAC encoding failed: %s\n", 
81                           smb_get_krb5_error_message(context, ret, mem_ctx)));
82                 talloc_free(mem_ctx);
83                 return ret;
84         }
85
86         ret = krb5_data_copy(pac, tmp_blob.data, tmp_blob.length);
87         talloc_free(mem_ctx);
88         return ret;
89 }