r8302: import mini HEIMDAL into the tree
[tprouty/samba.git] / source / heimdal / lib / krb5 / keytab_memory.c
1 /*
2  * Copyright (c) 1997 - 2001 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: keytab_memory.c,v 1.6 2005/05/18 04:44:40 lha Exp $");
37
38 /* memory operations -------------------------------------------- */
39
40 struct mkt_data {
41     krb5_keytab_entry *entries;
42     int num_entries;
43 };
44
45 static krb5_error_code
46 mkt_resolve(krb5_context context, const char *name, krb5_keytab id)
47 {
48     struct mkt_data *d;
49     d = malloc(sizeof(*d));
50     if(d == NULL) {
51         krb5_set_error_string (context, "malloc: out of memory");
52         return ENOMEM;
53     }
54     d->entries = NULL;
55     d->num_entries = 0;
56     id->data = d;
57     return 0;
58 }
59
60 static krb5_error_code
61 mkt_close(krb5_context context, krb5_keytab id)
62 {
63     struct mkt_data *d = id->data;
64     int i;
65     for(i = 0; i < d->num_entries; i++)
66         krb5_kt_free_entry(context, &d->entries[i]);
67     free(d->entries);
68     free(d);
69     return 0;
70 }
71
72 static krb5_error_code 
73 mkt_get_name(krb5_context context, 
74              krb5_keytab id, 
75              char *name, 
76              size_t namesize)
77 {
78     strlcpy(name, "", namesize);
79     return 0;
80 }
81
82 static krb5_error_code
83 mkt_start_seq_get(krb5_context context, 
84                   krb5_keytab id, 
85                   krb5_kt_cursor *c)
86 {
87     /* XXX */
88     c->fd = 0;
89     return 0;
90 }
91
92 static krb5_error_code
93 mkt_next_entry(krb5_context context, 
94                krb5_keytab id, 
95                krb5_keytab_entry *entry, 
96                krb5_kt_cursor *c)
97 {
98     struct mkt_data *d = id->data;
99     if(c->fd >= d->num_entries)
100         return KRB5_KT_END;
101     return krb5_kt_copy_entry_contents(context, &d->entries[c->fd++], entry);
102 }
103
104 static krb5_error_code
105 mkt_end_seq_get(krb5_context context, 
106                 krb5_keytab id,
107                 krb5_kt_cursor *cursor)
108 {
109     return 0;
110 }
111
112 static krb5_error_code
113 mkt_add_entry(krb5_context context,
114               krb5_keytab id,
115               krb5_keytab_entry *entry)
116 {
117     struct mkt_data *d = id->data;
118     krb5_keytab_entry *tmp;
119     tmp = realloc(d->entries, (d->num_entries + 1) * sizeof(*d->entries));
120     if(tmp == NULL) {
121         krb5_set_error_string (context, "malloc: out of memory");
122         return ENOMEM;
123     }
124     d->entries = tmp;
125     return krb5_kt_copy_entry_contents(context, entry, 
126                                        &d->entries[d->num_entries++]);
127 }
128
129 static krb5_error_code
130 mkt_remove_entry(krb5_context context,
131                  krb5_keytab id,
132                  krb5_keytab_entry *entry)
133 {
134     struct mkt_data *d = id->data;
135     krb5_keytab_entry *e, *end;
136     int found = 0;
137     
138     if (d->num_entries == 0) {
139         krb5_clear_error_string(context);
140         return KRB5_KT_NOTFOUND;
141     }
142
143     /* do this backwards to minimize copying */
144     for(end = d->entries + d->num_entries, e = end - 1; e >= d->entries; e--) {
145         if(krb5_kt_compare(context, e, entry->principal, 
146                            entry->vno, entry->keyblock.keytype)) {
147             krb5_kt_free_entry(context, e);
148             memmove(e, e + 1, (end - e - 1) * sizeof(*e));
149             memset(end - 1, 0, sizeof(*end));
150             d->num_entries--;
151             end--;
152             found = 1;
153         }
154     }
155     if (!found) {
156         krb5_clear_error_string (context);
157         return KRB5_KT_NOTFOUND;
158     }
159     e = realloc(d->entries, d->num_entries * sizeof(*d->entries));
160     if(e != NULL)
161         d->entries = e;
162     return 0;
163 }
164
165 const krb5_kt_ops krb5_mkt_ops = {
166     "MEMORY",
167     mkt_resolve,
168     mkt_get_name,
169     mkt_close,
170     NULL, /* get */
171     mkt_start_seq_get,
172     mkt_next_entry,
173     mkt_end_seq_get,
174     mkt_add_entry,
175     mkt_remove_entry
176 };
177
178 static krb5_error_code 
179 mktw_get_entry(krb5_context context,
180                krb5_keytab id,
181                krb5_const_principal principal,
182                krb5_kvno kvno,
183                krb5_enctype enctype,
184                krb5_keytab_entry *entry)
185 {
186     krb5_keytab_entry tmp;
187     krb5_error_code ret;
188     krb5_kt_cursor cursor;
189
190     ret = krb5_kt_start_seq_get (context, id, &cursor);
191     if (ret)
192         return KRB5_KT_NOTFOUND; /* XXX i.e. file not found */
193
194     entry->vno = 0;
195     while (krb5_kt_next_entry(context, id, &tmp, &cursor) == 0) {
196         if (krb5_kt_compare(context, &tmp, NULL, 0, enctype)) {
197             if (kvno == tmp.vno) {
198                 krb5_kt_copy_entry_contents (context, &tmp, entry);
199                 krb5_kt_free_entry (context, &tmp);
200                 krb5_kt_end_seq_get(context, id, &cursor);
201                 return 0;
202             } else if (kvno == 0 && tmp.vno > entry->vno) {
203                 if (entry->vno)
204                     krb5_kt_free_entry (context, entry);
205                 krb5_kt_copy_entry_contents (context, &tmp, entry);
206             }
207         }
208         krb5_kt_free_entry(context, &tmp);
209     }
210     krb5_kt_end_seq_get (context, id, &cursor);
211     if (entry->vno) {
212         return 0;
213     } else {
214             return KRB5_KT_NOTFOUND;
215     }
216 };
217
218 const krb5_kt_ops krb5_mktw_ops = {
219     "MEMORY_WILDCARD",
220     mkt_resolve,
221     mkt_get_name,
222     mkt_close,
223     mktw_get_entry, /* get */
224     mkt_start_seq_get,
225     mkt_next_entry,
226     mkt_end_seq_get,
227     mkt_add_entry,
228     mkt_remove_entry
229 };