No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / krb5 / keytab_any.c
blobdb04d683ec38bd67df2f045bce878d9eef98c319
1 /*
2 * Copyright (c) 2001-2002 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_any.c 17035 2006-04-10 09:20:13Z lha $"
37 "$NetBSD$");
39 struct any_data {
40 krb5_keytab kt;
41 char *name;
42 struct any_data *next;
45 static void
46 free_list (krb5_context context, struct any_data *a)
48 struct any_data *next;
50 for (; a != NULL; a = next) {
51 next = a->next;
52 free (a->name);
53 if(a->kt)
54 krb5_kt_close(context, a->kt);
55 free (a);
59 static krb5_error_code
60 any_resolve(krb5_context context, const char *name, krb5_keytab id)
62 struct any_data *a, *a0 = NULL, *prev = NULL;
63 krb5_error_code ret;
64 char buf[256];
66 while (strsep_copy(&name, ",", buf, sizeof(buf)) != -1) {
67 a = malloc(sizeof(*a));
68 if (a == NULL) {
69 ret = ENOMEM;
70 goto fail;
72 if (a0 == NULL) {
73 a0 = a;
74 a->name = strdup(buf);
75 if (a->name == NULL) {
76 krb5_set_error_string(context, "malloc: out of memory");
77 ret = ENOMEM;
78 goto fail;
80 } else
81 a->name = NULL;
82 if (prev != NULL)
83 prev->next = a;
84 a->next = NULL;
85 ret = krb5_kt_resolve (context, buf, &a->kt);
86 if (ret)
87 goto fail;
88 prev = a;
90 if (a0 == NULL) {
91 krb5_set_error_string(context, "empty ANY: keytab");
92 return ENOENT;
94 id->data = a0;
95 return 0;
96 fail:
97 free_list (context, a0);
98 return ret;
101 static krb5_error_code
102 any_get_name (krb5_context context,
103 krb5_keytab id,
104 char *name,
105 size_t namesize)
107 struct any_data *a = id->data;
108 strlcpy(name, a->name, namesize);
109 return 0;
112 static krb5_error_code
113 any_close (krb5_context context,
114 krb5_keytab id)
116 struct any_data *a = id->data;
118 free_list (context, a);
119 return 0;
122 struct any_cursor_extra_data {
123 struct any_data *a;
124 krb5_kt_cursor cursor;
127 static krb5_error_code
128 any_start_seq_get(krb5_context context,
129 krb5_keytab id,
130 krb5_kt_cursor *c)
132 struct any_data *a = id->data;
133 struct any_cursor_extra_data *ed;
134 krb5_error_code ret;
136 c->data = malloc (sizeof(struct any_cursor_extra_data));
137 if(c->data == NULL){
138 krb5_set_error_string (context, "malloc: out of memory");
139 return ENOMEM;
141 ed = (struct any_cursor_extra_data *)c->data;
142 ed->a = a;
143 ret = krb5_kt_start_seq_get(context, ed->a->kt, &ed->cursor);
144 if (ret) {
145 free (c->data);
146 c->data = NULL;
147 return ret;
149 return 0;
152 static krb5_error_code
153 any_next_entry (krb5_context context,
154 krb5_keytab id,
155 krb5_keytab_entry *entry,
156 krb5_kt_cursor *cursor)
158 krb5_error_code ret, ret2;
159 struct any_cursor_extra_data *ed;
161 ed = (struct any_cursor_extra_data *)cursor->data;
162 do {
163 ret = krb5_kt_next_entry(context, ed->a->kt, entry, &ed->cursor);
164 if (ret == 0)
165 return 0;
166 else if (ret != KRB5_KT_END)
167 return ret;
169 ret2 = krb5_kt_end_seq_get (context, ed->a->kt, &ed->cursor);
170 if (ret2)
171 return ret2;
172 while ((ed->a = ed->a->next) != NULL) {
173 ret2 = krb5_kt_start_seq_get(context, ed->a->kt, &ed->cursor);
174 if (ret2 == 0)
175 break;
177 if (ed->a == NULL) {
178 krb5_clear_error_string (context);
179 return KRB5_KT_END;
181 } while (1);
184 static krb5_error_code
185 any_end_seq_get(krb5_context context,
186 krb5_keytab id,
187 krb5_kt_cursor *cursor)
189 krb5_error_code ret = 0;
190 struct any_cursor_extra_data *ed;
192 ed = (struct any_cursor_extra_data *)cursor->data;
193 if (ed->a != NULL)
194 ret = krb5_kt_end_seq_get(context, ed->a->kt, &ed->cursor);
195 free (ed);
196 cursor->data = NULL;
197 return ret;
200 static krb5_error_code
201 any_add_entry(krb5_context context,
202 krb5_keytab id,
203 krb5_keytab_entry *entry)
205 struct any_data *a = id->data;
206 krb5_error_code ret;
207 while(a != NULL) {
208 ret = krb5_kt_add_entry(context, a->kt, entry);
209 if(ret != 0 && ret != KRB5_KT_NOWRITE) {
210 krb5_set_error_string(context, "failed to add entry to %s",
211 a->name);
212 return ret;
214 a = a->next;
216 return 0;
219 static krb5_error_code
220 any_remove_entry(krb5_context context,
221 krb5_keytab id,
222 krb5_keytab_entry *entry)
224 struct any_data *a = id->data;
225 krb5_error_code ret;
226 int found = 0;
227 while(a != NULL) {
228 ret = krb5_kt_remove_entry(context, a->kt, entry);
229 if(ret == 0)
230 found++;
231 else {
232 if(ret != KRB5_KT_NOWRITE && ret != KRB5_KT_NOTFOUND) {
233 krb5_set_error_string(context, "failed to remove entry from %s",
234 a->name);
235 return ret;
238 a = a->next;
240 if(!found)
241 return KRB5_KT_NOTFOUND;
242 return 0;
245 const krb5_kt_ops krb5_any_ops = {
246 "ANY",
247 any_resolve,
248 any_get_name,
249 any_close,
250 NULL, /* get */
251 any_start_seq_get,
252 any_next_entry,
253 any_end_seq_get,
254 any_add_entry,
255 any_remove_entry