2 * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
34 #include "krb5_locl.h"
38 typedef struct krb5_mcache
{
42 krb5_principal primary_principal
;
47 struct krb5_mcache
*next
;
50 static struct krb5_mcache
*mcc_head
;
52 #define MCACHE(X) ((krb5_mcache *)(X)->data.data)
54 #define MISDEAD(X) ((X)->dead)
56 #define MCC_CURSOR(C) ((struct link*)(C))
59 mcc_get_name(krb5_context context
,
62 return MCACHE(id
)->name
;
66 mcc_alloc(const char *name
)
74 asprintf(&m
->name
, "%p", m
);
76 m
->name
= strdup(name
);
83 m
->primary_principal
= NULL
;
90 static krb5_error_code
91 mcc_resolve(krb5_context context
, krb5_ccache
*id
, const char *res
)
95 for (m
= mcc_head
; m
!= NULL
; m
= m
->next
)
96 if (strcmp(m
->name
, res
) == 0)
101 (*id
)->data
.data
= m
;
102 (*id
)->data
.length
= sizeof(*m
);
108 krb5_set_error_string (context
, "malloc: out of memory");
109 return KRB5_CC_NOMEM
;
112 (*id
)->data
.data
= m
;
113 (*id
)->data
.length
= sizeof(*m
);
119 static krb5_error_code
120 mcc_gen_new(krb5_context context
, krb5_ccache
*id
)
127 krb5_set_error_string (context
, "malloc: out of memory");
128 return KRB5_CC_NOMEM
;
131 (*id
)->data
.data
= m
;
132 (*id
)->data
.length
= sizeof(*m
);
137 static krb5_error_code
138 mcc_initialize(krb5_context context
,
140 krb5_principal primary_principal
)
142 krb5_mcache
*m
= MCACHE(id
);
144 return krb5_copy_principal (context
,
146 &m
->primary_principal
);
149 static krb5_error_code
150 mcc_close(krb5_context context
,
153 krb5_mcache
*m
= MCACHE(id
);
155 if (--m
->refcnt
!= 0)
160 krb5_data_free(&id
->data
);
166 static krb5_error_code
167 mcc_destroy(krb5_context context
,
170 krb5_mcache
**n
, *m
= MCACHE(id
);
174 krb5_abortx(context
, "mcc_destroy: refcnt already 0");
177 /* if this is an active mcache, remove it from the linked
178 list, and free all data */
179 for(n
= &mcc_head
; n
&& *n
; n
= &(*n
)->next
) {
185 if (m
->primary_principal
!= NULL
) {
186 krb5_free_principal (context
, m
->primary_principal
);
187 m
->primary_principal
= NULL
;
195 krb5_free_creds_contents (context
, &l
->cred
);
205 static krb5_error_code
206 mcc_store_cred(krb5_context context
,
210 krb5_mcache
*m
= MCACHE(id
);
217 l
= malloc (sizeof(*l
));
219 krb5_set_error_string (context
, "malloc: out of memory");
220 return KRB5_CC_NOMEM
;
224 memset (&l
->cred
, 0, sizeof(l
->cred
));
225 ret
= krb5_copy_creds_contents (context
, creds
, &l
->cred
);
234 static krb5_error_code
235 mcc_get_principal(krb5_context context
,
237 krb5_principal
*principal
)
239 krb5_mcache
*m
= MCACHE(id
);
241 if (MISDEAD(m
) || m
->primary_principal
== NULL
)
243 return krb5_copy_principal (context
,
244 m
->primary_principal
,
248 static krb5_error_code
249 mcc_get_first (krb5_context context
,
251 krb5_cc_cursor
*cursor
)
253 krb5_mcache
*m
= MCACHE(id
);
262 static krb5_error_code
263 mcc_get_next (krb5_context context
,
265 krb5_cc_cursor
*cursor
,
268 krb5_mcache
*m
= MCACHE(id
);
277 return krb5_copy_creds_contents (context
,
284 static krb5_error_code
285 mcc_end_get (krb5_context context
,
287 krb5_cc_cursor
*cursor
)
292 static krb5_error_code
293 mcc_remove_cred(krb5_context context
,
298 krb5_mcache
*m
= MCACHE(id
);
300 for(q
= &m
->creds
, p
= *q
; p
; p
= *q
) {
301 if(krb5_compare_creds(context
, which
, mcreds
, &p
->cred
)) {
303 krb5_free_creds_contents(context
, &p
->cred
);
311 static krb5_error_code
312 mcc_set_flags(krb5_context context
,
319 const krb5_cc_ops krb5_mcc_ops
= {
328 NULL
, /* mcc_retrieve */