heimdal: update to lorikeet-heimdal rev 801
[sfrench/samba-autobuild/.git] / source4 / 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 23280 2008-06-23 03:26:18Z lha $");
37
38 #undef __attribute__
39 #define __attribute__(X)
40
41 #ifndef HEIMDAL_SMALLER
42
43 /* keep this for compatibility with older code */
44 krb5_error_code KRB5_LIB_FUNCTION __attribute__((deprecated))
45 krb5_free_creds_contents (krb5_context context, krb5_creds *c)
46 {
47     return krb5_free_cred_contents (context, c);
48 }    
49
50 #endif /* HEIMDAL_SMALLER */
51
52 /**
53  * Free content of krb5_creds.
54  *
55  * @param context Kerberos 5 context.
56  * @param c krb5_creds to free.
57  *
58  * @return Returns 0 to indicate success. Otherwise an kerberos et
59  * error code is returned, see krb5_get_error_message().
60  *
61  * @ingroup krb5
62  */
63
64 krb5_error_code KRB5_LIB_FUNCTION
65 krb5_free_cred_contents (krb5_context context, krb5_creds *c)
66 {
67     krb5_free_principal (context, c->client);
68     c->client = NULL;
69     krb5_free_principal (context, c->server);
70     c->server = NULL;
71     krb5_free_keyblock_contents (context, &c->session);
72     krb5_data_free (&c->ticket);
73     krb5_data_free (&c->second_ticket);
74     free_AuthorizationData (&c->authdata);
75     krb5_free_addresses (context, &c->addresses);
76     memset(c, 0, sizeof(*c));
77     return 0;
78 }
79
80 /**
81  * Copy content of krb5_creds.
82  *
83  * @param context Kerberos 5 context.
84  * @param incred source credential
85  * @param c destination credential, free with krb5_free_cred_contents().
86  *
87  * @return Returns 0 to indicate success. Otherwise an kerberos et
88  * error code is returned, see krb5_get_error_message().
89  *
90  * @ingroup krb5
91  */
92
93 krb5_error_code KRB5_LIB_FUNCTION
94 krb5_copy_creds_contents (krb5_context context,
95                           const krb5_creds *incred,
96                           krb5_creds *c)
97 {
98     krb5_error_code ret;
99
100     memset(c, 0, sizeof(*c));
101     ret = krb5_copy_principal (context, incred->client, &c->client);
102     if (ret)
103         goto fail;
104     ret = krb5_copy_principal (context, incred->server, &c->server);
105     if (ret)
106         goto fail;
107     ret = krb5_copy_keyblock_contents (context, &incred->session, &c->session);
108     if (ret)
109         goto fail;
110     c->times = incred->times;
111     ret = krb5_data_copy (&c->ticket,
112                           incred->ticket.data,
113                           incred->ticket.length);
114     if (ret)
115         goto fail;
116     ret = krb5_data_copy (&c->second_ticket,
117                           incred->second_ticket.data,
118                           incred->second_ticket.length);
119     if (ret)
120         goto fail;
121     ret = copy_AuthorizationData(&incred->authdata, &c->authdata);
122     if (ret)
123         goto fail;
124     ret = krb5_copy_addresses (context,
125                                &incred->addresses,
126                                &c->addresses);
127     if (ret)
128         goto fail;
129     c->flags = incred->flags;
130     return 0;
131
132 fail:
133     krb5_free_cred_contents (context, c);
134     return ret;
135 }
136
137 /**
138  * Copy krb5_creds.
139  *
140  * @param context Kerberos 5 context.
141  * @param incred source credential
142  * @param outcred destination credential, free with krb5_free_creds().
143  *
144  * @return Returns 0 to indicate success. Otherwise an kerberos et
145  * error code is returned, see krb5_get_error_message().
146  *
147  * @ingroup krb5
148  */
149
150 krb5_error_code KRB5_LIB_FUNCTION
151 krb5_copy_creds (krb5_context context,
152                  const krb5_creds *incred,
153                  krb5_creds **outcred)
154 {
155     krb5_creds *c;
156
157     c = malloc (sizeof (*c));
158     if (c == NULL) {
159         krb5_set_error_message (context, ENOMEM, "malloc: out of memory");
160         return ENOMEM;
161     }
162     memset (c, 0, sizeof(*c));
163     *outcred = c;
164     return krb5_copy_creds_contents (context, incred, c);
165 }
166
167 /**
168  * Free krb5_creds.
169  *
170  * @param context Kerberos 5 context.
171  * @param c krb5_creds to free.
172  *
173  * @return Returns 0 to indicate success. Otherwise an kerberos et
174  * error code is returned, see krb5_get_error_message().
175  *
176  * @ingroup krb5
177  */
178
179 krb5_error_code KRB5_LIB_FUNCTION
180 krb5_free_creds (krb5_context context, krb5_creds *c)
181 {
182     krb5_free_cred_contents (context, c);
183     free (c);
184     return 0;
185 }
186
187 /* XXX this do not belong here */
188 static krb5_boolean
189 krb5_times_equal(const krb5_times *a, const krb5_times *b)
190 {
191     return a->starttime == b->starttime &&
192         a->authtime == b->authtime &&
193         a->endtime == b->endtime &&
194         a->renew_till == b->renew_till;
195 }
196
197 /**
198  * Return TRUE if `mcreds' and `creds' are equal (`whichfields'
199  * determines what equal means).
200  *
201  * @param context Kerberos 5 context.
202  * @param whichfields which fields to compare.
203  * @param mcreds cred to compare with.
204  * @param creds cred to compare with.
205  *
206  * @return return TRUE if mcred and creds are equal, FALSE if not.
207  *
208  * @ingroup krb5
209  */
210
211 krb5_boolean KRB5_LIB_FUNCTION
212 krb5_compare_creds(krb5_context context, krb5_flags whichfields,
213                    const krb5_creds * mcreds, const krb5_creds * creds)
214 {
215     krb5_boolean match = TRUE;
216     
217     if (match && mcreds->server) {
218         if (whichfields & (KRB5_TC_DONT_MATCH_REALM | KRB5_TC_MATCH_SRV_NAMEONLY)) 
219             match = krb5_principal_compare_any_realm (context, mcreds->server, 
220                                                       creds->server);
221         else
222             match = krb5_principal_compare (context, mcreds->server, 
223                                             creds->server);
224     }
225
226     if (match && mcreds->client) {
227         if(whichfields & KRB5_TC_DONT_MATCH_REALM)
228             match = krb5_principal_compare_any_realm (context, mcreds->client, 
229                                                       creds->client);
230         else
231             match = krb5_principal_compare (context, mcreds->client, 
232                                             creds->client);
233     }
234             
235     if (match && (whichfields & KRB5_TC_MATCH_KEYTYPE))
236         match = krb5_enctypes_compatible_keys(context,
237                                               mcreds->session.keytype,
238                                               creds->session.keytype);
239
240     if (match && (whichfields & KRB5_TC_MATCH_FLAGS_EXACT))
241         match = mcreds->flags.i == creds->flags.i;
242
243     if (match && (whichfields & KRB5_TC_MATCH_FLAGS))
244         match = (creds->flags.i & mcreds->flags.i) == mcreds->flags.i;
245
246     if (match && (whichfields & KRB5_TC_MATCH_TIMES_EXACT))
247         match = krb5_times_equal(&mcreds->times, &creds->times);
248     
249     if (match && (whichfields & KRB5_TC_MATCH_TIMES))
250         /* compare only expiration times */
251         match = (mcreds->times.renew_till <= creds->times.renew_till) &&
252             (mcreds->times.endtime <= creds->times.endtime);
253
254     if (match && (whichfields & KRB5_TC_MATCH_AUTHDATA)) {
255         unsigned int i;
256         if(mcreds->authdata.len != creds->authdata.len)
257             match = FALSE;
258         else
259             for(i = 0; match && i < mcreds->authdata.len; i++)
260                 match = (mcreds->authdata.val[i].ad_type == 
261                          creds->authdata.val[i].ad_type) &&
262                     (krb5_data_cmp(&mcreds->authdata.val[i].ad_data,
263                                    &creds->authdata.val[i].ad_data) == 0);
264     }
265     if (match && (whichfields & KRB5_TC_MATCH_2ND_TKT))
266         match = (krb5_data_cmp(&mcreds->second_ticket, &creds->second_ticket) == 0);
267
268     if (match && (whichfields & KRB5_TC_MATCH_IS_SKEY))
269         match = ((mcreds->second_ticket.length == 0) == 
270                  (creds->second_ticket.length == 0));
271
272     return match;
273 }