d54664fe66dfbd5bfd6e7a160a97357e23a5d1aa
[samba.git] / source4 / auth / kerberos / kerberos.c
1 /* 
2    Unix SMB/CIFS implementation.
3    kerberos utility library
4    Copyright (C) Andrew Tridgell 2001
5    Copyright (C) Remus Koos 2001
6    Copyright (C) Nalin Dahyabhai 2004.
7    Copyright (C) Jeremy Allison 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 "system/kerberos.h"
26
27 #ifdef HAVE_KRB5
28
29 /*
30   simulate a kinit, putting the tgt in the given credentials cache. 
31   Orignally by remus@snapserver.com
32  
33   This version is built to use a keyblock, rather than needing the
34   original password.
35 */
36  krb5_error_code kerberos_kinit_keyblock_cc(krb5_context ctx, krb5_ccache cc, 
37                                 krb5_principal principal, krb5_keyblock *keyblock,
38                                 time_t *expire_time, time_t *kdc_time)
39 {
40         krb5_error_code code = 0;
41         krb5_creds my_creds;
42         krb5_get_init_creds_opt options;
43
44         krb5_get_init_creds_opt_init(&options);
45
46         krb5_get_init_creds_opt_set_default_flags(ctx, NULL, NULL, &options);
47
48         if ((code = krb5_get_init_creds_keyblock(ctx, &my_creds, principal, keyblock,
49                                                  0, NULL, &options))) {
50                 return code;
51         }
52         
53         if ((code = krb5_cc_initialize(ctx, cc, principal))) {
54                 krb5_free_cred_contents(ctx, &my_creds);
55                 return code;
56         }
57         
58         if ((code = krb5_cc_store_cred(ctx, cc, &my_creds))) {
59                 krb5_free_cred_contents(ctx, &my_creds);
60                 return code;
61         }
62         
63         if (expire_time) {
64                 *expire_time = (time_t) my_creds.times.endtime;
65         }
66
67         if (kdc_time) {
68                 *kdc_time = (time_t) my_creds.times.starttime;
69         }
70
71         krb5_free_cred_contents(ctx, &my_creds);
72         
73         return 0;
74 }
75
76 /*
77   simulate a kinit, putting the tgt in the given credentials cache. 
78   Orignally by remus@snapserver.com
79 */
80  krb5_error_code kerberos_kinit_password_cc(krb5_context ctx, krb5_ccache cc, 
81                                 krb5_principal principal, const char *password, 
82                                 time_t *expire_time, time_t *kdc_time)
83 {
84         krb5_error_code code = 0;
85         krb5_creds my_creds;
86         krb5_get_init_creds_opt options;
87
88         krb5_get_init_creds_opt_init(&options);
89
90         krb5_get_init_creds_opt_set_default_flags(ctx, NULL, NULL, &options);
91
92         if ((code = krb5_get_init_creds_password(ctx, &my_creds, principal, password, 
93                                                  NULL, 
94                                                  NULL, 0, NULL, &options))) {
95                 return code;
96         }
97         
98         if ((code = krb5_cc_initialize(ctx, cc, principal))) {
99                 krb5_free_cred_contents(ctx, &my_creds);
100                 return code;
101         }
102         
103         if ((code = krb5_cc_store_cred(ctx, cc, &my_creds))) {
104                 krb5_free_cred_contents(ctx, &my_creds);
105                 return code;
106         }
107         
108         if (expire_time) {
109                 *expire_time = (time_t) my_creds.times.endtime;
110         }
111
112         if (kdc_time) {
113                 *kdc_time = (time_t) my_creds.times.starttime;
114         }
115
116         krb5_free_cred_contents(ctx, &my_creds);
117         
118         return 0;
119 }
120
121
122 #endif