No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / krb5 / keytab_memory.c
blob008a7fc38cc716ec3c31f454d161400cd241dc06
1 /*
2 * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "krb5_locl.h"
36 __RCSID("$Heimdal: keytab_memory.c 16352 2005-12-05 18:39:46Z lha $"
37 "$NetBSD$");
39 /* memory operations -------------------------------------------- */
41 struct mkt_data {
42 krb5_keytab_entry *entries;
43 int num_entries;
44 char *name;
45 int refcount;
46 struct mkt_data *next;
49 /* this mutex protects mkt_head, ->refcount, and ->next
50 * content is not protected (name is static and need no protection)
52 static HEIMDAL_MUTEX mkt_mutex = HEIMDAL_MUTEX_INITIALIZER;
53 static struct mkt_data *mkt_head;
56 static krb5_error_code
57 mkt_resolve(krb5_context context, const char *name, krb5_keytab id)
59 struct mkt_data *d;
61 HEIMDAL_MUTEX_lock(&mkt_mutex);
63 for (d = mkt_head; d != NULL; d = d->next)
64 if (strcmp(d->name, name) == 0)
65 break;
66 if (d) {
67 if (d->refcount < 1)
68 krb5_abortx(context, "Double close on memory keytab, "
69 "refcount < 1 %d", d->refcount);
70 d->refcount++;
71 id->data = d;
72 HEIMDAL_MUTEX_unlock(&mkt_mutex);
73 return 0;
76 d = calloc(1, sizeof(*d));
77 if(d == NULL) {
78 HEIMDAL_MUTEX_unlock(&mkt_mutex);
79 krb5_set_error_string (context, "malloc: out of memory");
80 return ENOMEM;
82 d->name = strdup(name);
83 if (d->name == NULL) {
84 HEIMDAL_MUTEX_unlock(&mkt_mutex);
85 free(d);
86 krb5_set_error_string (context, "malloc: out of memory");
87 return ENOMEM;
89 d->entries = NULL;
90 d->num_entries = 0;
91 d->refcount = 1;
92 d->next = mkt_head;
93 mkt_head = d;
94 HEIMDAL_MUTEX_unlock(&mkt_mutex);
95 id->data = d;
96 return 0;
99 static krb5_error_code
100 mkt_close(krb5_context context, krb5_keytab id)
102 struct mkt_data *d = id->data, **dp;
103 int i;
105 HEIMDAL_MUTEX_lock(&mkt_mutex);
106 if (d->refcount < 1)
107 krb5_abortx(context,
108 "krb5 internal error, memory keytab refcount < 1 on close");
110 if (--d->refcount > 0) {
111 HEIMDAL_MUTEX_unlock(&mkt_mutex);
112 return 0;
114 for (dp = &mkt_head; *dp != NULL; dp = &(*dp)->next) {
115 if (*dp == d) {
116 *dp = d->next;
117 break;
120 HEIMDAL_MUTEX_unlock(&mkt_mutex);
122 free(d->name);
123 for(i = 0; i < d->num_entries; i++)
124 krb5_kt_free_entry(context, &d->entries[i]);
125 free(d->entries);
126 free(d);
127 return 0;
130 static krb5_error_code
131 mkt_get_name(krb5_context context,
132 krb5_keytab id,
133 char *name,
134 size_t namesize)
136 struct mkt_data *d = id->data;
137 strlcpy(name, d->name, namesize);
138 return 0;
141 static krb5_error_code
142 mkt_start_seq_get(krb5_context context,
143 krb5_keytab id,
144 krb5_kt_cursor *c)
146 /* XXX */
147 c->fd = 0;
148 return 0;
151 static krb5_error_code
152 mkt_next_entry(krb5_context context,
153 krb5_keytab id,
154 krb5_keytab_entry *entry,
155 krb5_kt_cursor *c)
157 struct mkt_data *d = id->data;
158 if(c->fd >= d->num_entries)
159 return KRB5_KT_END;
160 return krb5_kt_copy_entry_contents(context, &d->entries[c->fd++], entry);
163 static krb5_error_code
164 mkt_end_seq_get(krb5_context context,
165 krb5_keytab id,
166 krb5_kt_cursor *cursor)
168 return 0;
171 static krb5_error_code
172 mkt_add_entry(krb5_context context,
173 krb5_keytab id,
174 krb5_keytab_entry *entry)
176 struct mkt_data *d = id->data;
177 krb5_keytab_entry *tmp;
178 tmp = realloc(d->entries, (d->num_entries + 1) * sizeof(*d->entries));
179 if(tmp == NULL) {
180 krb5_set_error_string (context, "malloc: out of memory");
181 return ENOMEM;
183 d->entries = tmp;
184 return krb5_kt_copy_entry_contents(context, entry,
185 &d->entries[d->num_entries++]);
188 static krb5_error_code
189 mkt_remove_entry(krb5_context context,
190 krb5_keytab id,
191 krb5_keytab_entry *entry)
193 struct mkt_data *d = id->data;
194 krb5_keytab_entry *e, *end;
195 int found = 0;
197 if (d->num_entries == 0) {
198 krb5_clear_error_string(context);
199 return KRB5_KT_NOTFOUND;
202 /* do this backwards to minimize copying */
203 for(end = d->entries + d->num_entries, e = end - 1; e >= d->entries; e--) {
204 if(krb5_kt_compare(context, e, entry->principal,
205 entry->vno, entry->keyblock.keytype)) {
206 krb5_kt_free_entry(context, e);
207 memmove(e, e + 1, (end - e - 1) * sizeof(*e));
208 memset(end - 1, 0, sizeof(*end));
209 d->num_entries--;
210 end--;
211 found = 1;
214 if (!found) {
215 krb5_clear_error_string (context);
216 return KRB5_KT_NOTFOUND;
218 e = realloc(d->entries, d->num_entries * sizeof(*d->entries));
219 if(e != NULL || d->num_entries == 0)
220 d->entries = e;
221 return 0;
224 const krb5_kt_ops krb5_mkt_ops = {
225 "MEMORY",
226 mkt_resolve,
227 mkt_get_name,
228 mkt_close,
229 NULL, /* get */
230 mkt_start_seq_get,
231 mkt_next_entry,
232 mkt_end_seq_get,
233 mkt_add_entry,
234 mkt_remove_entry