r23456: Update Samba4 to current lorikeet-heimdal.
[sfrench/samba-autobuild/.git] / source / heimdal / lib / krb5 / creds.c
1 /*
2  * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright 
14  *    notice, this list of conditions and the following disclaimer in the 
15  *    documentation and/or other materials provided with the distribution. 
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors 
18  *    may be used to endorse or promote products derived from this software 
19  *    without specific prior written permission. 
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
31  * SUCH DAMAGE. 
32  */
33
34 #include "krb5_locl.h"
35
36 RCSID("$Id: creds.c 15167 2005-05-18 04:21:57Z lha $");
37
38 /* keep this for compatibility with older code */
39 krb5_error_code KRB5_LIB_FUNCTION
40 krb5_free_creds_contents (krb5_context context, krb5_creds *c)
41 {
42     return krb5_free_cred_contents (context, c);
43 }    
44
45 krb5_error_code KRB5_LIB_FUNCTION
46 krb5_free_cred_contents (krb5_context context, krb5_creds *c)
47 {
48     krb5_free_principal (context, c->client);
49     c->client = NULL;
50     krb5_free_principal (context, c->server);
51     c->server = NULL;
52     krb5_free_keyblock_contents (context, &c->session);
53     krb5_data_free (&c->ticket);
54     krb5_data_free (&c->second_ticket);
55     free_AuthorizationData (&c->authdata);
56     krb5_free_addresses (context, &c->addresses);
57     memset(c, 0, sizeof(*c));
58     return 0;
59 }
60
61 krb5_error_code KRB5_LIB_FUNCTION
62 krb5_copy_creds_contents (krb5_context context,
63                           const krb5_creds *incred,
64                           krb5_creds *c)
65 {
66     krb5_error_code ret;
67
68     memset(c, 0, sizeof(*c));
69     ret = krb5_copy_principal (context, incred->client, &c->client);
70     if (ret)
71         goto fail;
72     ret = krb5_copy_principal (context, incred->server, &c->server);
73     if (ret)
74         goto fail;
75     ret = krb5_copy_keyblock_contents (context, &incred->session, &c->session);
76     if (ret)
77         goto fail;
78     c->times = incred->times;
79     ret = krb5_data_copy (&c->ticket,
80                           incred->ticket.data,
81                           incred->ticket.length);
82     if (ret)
83         goto fail;
84     ret = krb5_data_copy (&c->second_ticket,
85                           incred->second_ticket.data,
86                           incred->second_ticket.length);
87     if (ret)
88         goto fail;
89     ret = copy_AuthorizationData(&incred->authdata, &c->authdata);
90     if (ret)
91         goto fail;
92     ret = krb5_copy_addresses (context,
93                                &incred->addresses,
94                                &c->addresses);
95     if (ret)
96         goto fail;
97     c->flags = incred->flags;
98     return 0;
99
100 fail:
101     krb5_free_cred_contents (context, c);
102     return ret;
103 }
104
105 krb5_error_code KRB5_LIB_FUNCTION
106 krb5_copy_creds (krb5_context context,
107                  const krb5_creds *incred,
108                  krb5_creds **outcred)
109 {
110     krb5_creds *c;
111
112     c = malloc (sizeof (*c));
113     if (c == NULL) {
114         krb5_set_error_string (context, "malloc: out of memory");
115         return ENOMEM;
116     }
117     memset (c, 0, sizeof(*c));
118     *outcred = c;
119     return krb5_copy_creds_contents (context, incred, c);
120 }
121
122 krb5_error_code KRB5_LIB_FUNCTION
123 krb5_free_creds (krb5_context context, krb5_creds *c)
124 {
125     krb5_free_cred_contents (context, c);
126     free (c);
127     return 0;
128 }
129
130 /* XXX these do not belong here */
131 static krb5_boolean
132 krb5_data_equal(const krb5_data *a, const krb5_data *b)
133 {
134     if(a->length != b->length)
135         return FALSE;
136     return memcmp(a->data, b->data, a->length) == 0;
137 }
138
139 static krb5_boolean
140 krb5_times_equal(const krb5_times *a, const krb5_times *b)
141 {
142     return a->starttime == b->starttime &&
143         a->authtime == b->authtime &&
144         a->endtime == b->endtime &&
145         a->renew_till == b->renew_till;
146 }
147
148 /*
149  * Return TRUE if `mcreds' and `creds' are equal (`whichfields'
150  * determines what equal means).
151  */
152
153 krb5_boolean KRB5_LIB_FUNCTION
154 krb5_compare_creds(krb5_context context, krb5_flags whichfields,
155                    const krb5_creds * mcreds, const krb5_creds * creds)
156 {
157     krb5_boolean match = TRUE;
158     
159     if (match && mcreds->server) {
160         if (whichfields & (KRB5_TC_DONT_MATCH_REALM | KRB5_TC_MATCH_SRV_NAMEONLY)) 
161             match = krb5_principal_compare_any_realm (context, mcreds->server, 
162                                                       creds->server);
163         else
164             match = krb5_principal_compare (context, mcreds->server, 
165                                             creds->server);
166     }
167
168     if (match && mcreds->client) {
169         if(whichfields & KRB5_TC_DONT_MATCH_REALM)
170             match = krb5_principal_compare_any_realm (context, mcreds->client, 
171                                                       creds->client);
172         else
173             match = krb5_principal_compare (context, mcreds->client, 
174                                             creds->client);
175     }
176             
177     if (match && (whichfields & KRB5_TC_MATCH_KEYTYPE))
178         match = krb5_enctypes_compatible_keys(context,
179                                               mcreds->session.keytype,
180                                               creds->session.keytype);
181
182     if (match && (whichfields & KRB5_TC_MATCH_FLAGS_EXACT))
183         match = mcreds->flags.i == creds->flags.i;
184
185     if (match && (whichfields & KRB5_TC_MATCH_FLAGS))
186         match = (creds->flags.i & mcreds->flags.i) == mcreds->flags.i;
187
188     if (match && (whichfields & KRB5_TC_MATCH_TIMES_EXACT))
189         match = krb5_times_equal(&mcreds->times, &creds->times);
190     
191     if (match && (whichfields & KRB5_TC_MATCH_TIMES))
192         /* compare only expiration times */
193         match = (mcreds->times.renew_till <= creds->times.renew_till) &&
194             (mcreds->times.endtime <= creds->times.endtime);
195
196     if (match && (whichfields & KRB5_TC_MATCH_AUTHDATA)) {
197         unsigned int i;
198         if(mcreds->authdata.len != creds->authdata.len)
199             match = FALSE;
200         else
201             for(i = 0; match && i < mcreds->authdata.len; i++)
202                 match = (mcreds->authdata.val[i].ad_type == 
203                          creds->authdata.val[i].ad_type) &&
204                     krb5_data_equal(&mcreds->authdata.val[i].ad_data,
205                                     &creds->authdata.val[i].ad_data);
206     }
207     if (match && (whichfields & KRB5_TC_MATCH_2ND_TKT))
208         match = krb5_data_equal(&mcreds->second_ticket, &creds->second_ticket);
209
210     if (match && (whichfields & KRB5_TC_MATCH_IS_SKEY))
211         match = ((mcreds->second_ticket.length == 0) == 
212                  (creds->second_ticket.length == 0));
213
214     return match;
215 }