r7993: Further work on the Krb5 PAC.
[kai/samba-autobuild/.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
27  krb5_error_code samba_get_pac(krb5_context context, 
28                               struct krb5_kdc_configuration *config,
29                               krb5_principal client, 
30                               krb5_keyblock *keyblock, 
31                               krb5_data *pac) 
32 {
33         krb5_error_code ret;
34         NTSTATUS nt_status;
35         struct auth_serversupplied_info *server_info;
36         char *username, *p;
37         const char *realm;
38         TALLOC_CTX *mem_ctx = talloc_named(config, 0, "samba_get_pac context");
39         if (!mem_ctx) {
40                 return ENOMEM;
41         }
42
43         ret = krb5_unparse_name(context, client, &username);
44
45         if (ret != 0) {
46                 krb5_set_error_string(context, "get pac: could not parse principal");
47                 krb5_warnx(context, "get pac: could not parse principal");
48                 talloc_free(mem_ctx);
49                 return ret;
50         }
51
52         /* parse the principal name */
53         realm = krb5_principal_get_realm(context, client);
54         username = talloc_strdup(mem_ctx, username);
55         p = strchr(username, '@');
56         if (p) {
57                 p[0] = '\0';
58         }
59
60
61         nt_status = sam_get_server_info(mem_ctx, username, realm, 
62                                         data_blob(NULL, 0), data_blob(NULL, 0),
63                                         &server_info);
64         if (!NT_STATUS_IS_OK(nt_status)) {
65                 DEBUG(0, ("Getting user info for PAC failed: %s\n",
66                           nt_errstr(nt_status)));
67                 talloc_free(mem_ctx);
68                 return EINVAL;
69         }
70
71         ret = kerberos_encode_pac(mem_ctx, server_info, 
72                                   context, 
73                                   keyblock,
74                                   pac);
75
76         talloc_free(mem_ctx);
77         
78         return ret;
79 }