r19604: This is a massive commit, and I appologise in advance for it's size.
[ira/wip.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 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 "system/kerberos.h"
27 #include "roken.h"
28
29 #ifdef HAVE_KRB5
30
31 /*
32   simulate a kinit, putting the tgt in the given credentials cache. 
33   Orignally by remus@snapserver.com
34  
35   This version is built to use a keyblock, rather than needing the
36   original password.
37 */
38  int kerberos_kinit_keyblock_cc(krb5_context ctx, krb5_ccache cc, 
39                                 krb5_principal principal, krb5_keyblock *keyblock,
40                                 time_t *expire_time, time_t *kdc_time)
41 {
42         krb5_error_code code = 0;
43         krb5_creds my_creds;
44         krb5_get_init_creds_opt options;
45
46         krb5_get_init_creds_opt_init(&options);
47
48         krb5_get_init_creds_opt_set_default_flags(ctx, NULL, NULL, &options);
49
50         if ((code = krb5_get_init_creds_keyblock(ctx, &my_creds, principal, keyblock,
51                                                  0, NULL, &options))) {
52                 return code;
53         }
54         
55         if ((code = krb5_cc_initialize(ctx, cc, principal))) {
56                 krb5_free_cred_contents(ctx, &my_creds);
57                 return code;
58         }
59         
60         if ((code = krb5_cc_store_cred(ctx, cc, &my_creds))) {
61                 krb5_free_cred_contents(ctx, &my_creds);
62                 return code;
63         }
64         
65         if (expire_time) {
66                 *expire_time = (time_t) my_creds.times.endtime;
67         }
68
69         if (kdc_time) {
70                 *kdc_time = (time_t) my_creds.times.starttime;
71         }
72
73         krb5_free_cred_contents(ctx, &my_creds);
74         
75         return 0;
76 }
77
78 /*
79   simulate a kinit, putting the tgt in the given credentials cache. 
80   Orignally by remus@snapserver.com
81 */
82  int kerberos_kinit_password_cc(krb5_context ctx, krb5_ccache cc, 
83                                 krb5_principal principal, const char *password, 
84                                 time_t *expire_time, time_t *kdc_time)
85 {
86         krb5_error_code code = 0;
87         krb5_creds my_creds;
88         krb5_get_init_creds_opt options;
89
90         krb5_get_init_creds_opt_init(&options);
91
92         krb5_get_init_creds_opt_set_default_flags(ctx, NULL, NULL, &options);
93
94         if ((code = krb5_get_init_creds_password(ctx, &my_creds, principal, password, 
95                                                  NULL, 
96                                                  NULL, 0, NULL, &options))) {
97                 return code;
98         }
99         
100         if ((code = krb5_cc_initialize(ctx, cc, principal))) {
101                 krb5_free_cred_contents(ctx, &my_creds);
102                 return code;
103         }
104         
105         if ((code = krb5_cc_store_cred(ctx, cc, &my_creds))) {
106                 krb5_free_cred_contents(ctx, &my_creds);
107                 return code;
108         }
109         
110         if (expire_time) {
111                 *expire_time = (time_t) my_creds.times.endtime;
112         }
113
114         if (kdc_time) {
115                 *kdc_time = (time_t) my_creds.times.starttime;
116         }
117
118         krb5_free_cred_contents(ctx, &my_creds);
119         
120         return 0;
121 }
122
123
124 #endif