155682Smarkm/*
2233294Sstas * Copyright (c) 1997 - 2001 Kungliga Tekniska H��gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4233294Sstas * All rights reserved.
555682Smarkm *
6233294Sstas * Redistribution and use in source and binary forms, with or without
7233294Sstas * modification, are permitted provided that the following conditions
8233294Sstas * are met:
955682Smarkm *
10233294Sstas * 1. Redistributions of source code must retain the above copyright
11233294Sstas *    notice, this list of conditions and the following disclaimer.
1255682Smarkm *
13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
14233294Sstas *    notice, this list of conditions and the following disclaimer in the
15233294Sstas *    documentation and/or other materials provided with the distribution.
1655682Smarkm *
17233294Sstas * 3. Neither the name of the Institute nor the names of its contributors
18233294Sstas *    may be used to endorse or promote products derived from this software
19233294Sstas *    without specific prior written permission.
2055682Smarkm *
21233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31233294Sstas * SUCH DAMAGE.
3255682Smarkm */
3355682Smarkm
3455682Smarkm#include "krb5_locl.h"
3555682Smarkm
3655682Smarkm/* memory operations -------------------------------------------- */
3755682Smarkm
3855682Smarkmstruct mkt_data {
3955682Smarkm    krb5_keytab_entry *entries;
4055682Smarkm    int num_entries;
41178825Sdfr    char *name;
42178825Sdfr    int refcount;
43178825Sdfr    struct mkt_data *next;
4455682Smarkm};
4555682Smarkm
46233294Sstas/* this mutex protects mkt_head, ->refcount, and ->next
47178825Sdfr * content is not protected (name is static and need no protection)
48178825Sdfr */
49178825Sdfrstatic HEIMDAL_MUTEX mkt_mutex = HEIMDAL_MUTEX_INITIALIZER;
50178825Sdfrstatic struct mkt_data *mkt_head;
51178825Sdfr
52178825Sdfr
53233294Sstasstatic krb5_error_code KRB5_CALLCONV
5455682Smarkmmkt_resolve(krb5_context context, const char *name, krb5_keytab id)
5555682Smarkm{
5655682Smarkm    struct mkt_data *d;
57178825Sdfr
58178825Sdfr    HEIMDAL_MUTEX_lock(&mkt_mutex);
59178825Sdfr
60178825Sdfr    for (d = mkt_head; d != NULL; d = d->next)
61178825Sdfr	if (strcmp(d->name, name) == 0)
62178825Sdfr	    break;
63178825Sdfr    if (d) {
64178825Sdfr	if (d->refcount < 1)
65178825Sdfr	    krb5_abortx(context, "Double close on memory keytab, "
66178825Sdfr			"refcount < 1 %d", d->refcount);
67178825Sdfr	d->refcount++;
68178825Sdfr	id->data = d;
69178825Sdfr	HEIMDAL_MUTEX_unlock(&mkt_mutex);
70178825Sdfr	return 0;
71178825Sdfr    }
72178825Sdfr
73178825Sdfr    d = calloc(1, sizeof(*d));
7478527Sassar    if(d == NULL) {
75178825Sdfr	HEIMDAL_MUTEX_unlock(&mkt_mutex);
76233294Sstas	krb5_set_error_message(context, ENOMEM,
77233294Sstas			       N_("malloc: out of memory", ""));
7855682Smarkm	return ENOMEM;
7978527Sassar    }
80178825Sdfr    d->name = strdup(name);
81178825Sdfr    if (d->name == NULL) {
82178825Sdfr	HEIMDAL_MUTEX_unlock(&mkt_mutex);
83178825Sdfr	free(d);
84233294Sstas	krb5_set_error_message(context, ENOMEM,
85233294Sstas			       N_("malloc: out of memory", ""));
86178825Sdfr	return ENOMEM;
87178825Sdfr    }
8855682Smarkm    d->entries = NULL;
8955682Smarkm    d->num_entries = 0;
90178825Sdfr    d->refcount = 1;
91178825Sdfr    d->next = mkt_head;
92178825Sdfr    mkt_head = d;
93178825Sdfr    HEIMDAL_MUTEX_unlock(&mkt_mutex);
9455682Smarkm    id->data = d;
9555682Smarkm    return 0;
9655682Smarkm}
9755682Smarkm
98233294Sstasstatic krb5_error_code KRB5_CALLCONV
9955682Smarkmmkt_close(krb5_context context, krb5_keytab id)
10055682Smarkm{
101178825Sdfr    struct mkt_data *d = id->data, **dp;
10255682Smarkm    int i;
103178825Sdfr
104178825Sdfr    HEIMDAL_MUTEX_lock(&mkt_mutex);
105178825Sdfr    if (d->refcount < 1)
106233294Sstas	krb5_abortx(context,
107178825Sdfr		    "krb5 internal error, memory keytab refcount < 1 on close");
108178825Sdfr
109178825Sdfr    if (--d->refcount > 0) {
110178825Sdfr	HEIMDAL_MUTEX_unlock(&mkt_mutex);
111178825Sdfr	return 0;
112178825Sdfr    }
113178825Sdfr    for (dp = &mkt_head; *dp != NULL; dp = &(*dp)->next) {
114178825Sdfr	if (*dp == d) {
115178825Sdfr	    *dp = d->next;
116178825Sdfr	    break;
117178825Sdfr	}
118178825Sdfr    }
119178825Sdfr    HEIMDAL_MUTEX_unlock(&mkt_mutex);
120178825Sdfr
121178825Sdfr    free(d->name);
12255682Smarkm    for(i = 0; i < d->num_entries; i++)
12355682Smarkm	krb5_kt_free_entry(context, &d->entries[i]);
12455682Smarkm    free(d->entries);
12555682Smarkm    free(d);
12655682Smarkm    return 0;
12755682Smarkm}
12855682Smarkm
129233294Sstasstatic krb5_error_code KRB5_CALLCONV
130233294Sstasmkt_get_name(krb5_context context,
131233294Sstas	     krb5_keytab id,
132233294Sstas	     char *name,
13355682Smarkm	     size_t namesize)
13455682Smarkm{
135178825Sdfr    struct mkt_data *d = id->data;
136178825Sdfr    strlcpy(name, d->name, namesize);
13755682Smarkm    return 0;
13855682Smarkm}
13955682Smarkm
140233294Sstasstatic krb5_error_code KRB5_CALLCONV
141233294Sstasmkt_start_seq_get(krb5_context context,
142233294Sstas		  krb5_keytab id,
14355682Smarkm		  krb5_kt_cursor *c)
14455682Smarkm{
14555682Smarkm    /* XXX */
14655682Smarkm    c->fd = 0;
14755682Smarkm    return 0;
14855682Smarkm}
14955682Smarkm
150233294Sstasstatic krb5_error_code KRB5_CALLCONV
151233294Sstasmkt_next_entry(krb5_context context,
152233294Sstas	       krb5_keytab id,
153233294Sstas	       krb5_keytab_entry *entry,
15455682Smarkm	       krb5_kt_cursor *c)
15555682Smarkm{
15655682Smarkm    struct mkt_data *d = id->data;
15755682Smarkm    if(c->fd >= d->num_entries)
15855682Smarkm	return KRB5_KT_END;
15955682Smarkm    return krb5_kt_copy_entry_contents(context, &d->entries[c->fd++], entry);
16055682Smarkm}
16155682Smarkm
162233294Sstasstatic krb5_error_code KRB5_CALLCONV
163233294Sstasmkt_end_seq_get(krb5_context context,
16455682Smarkm		krb5_keytab id,
16555682Smarkm		krb5_kt_cursor *cursor)
16655682Smarkm{
16755682Smarkm    return 0;
16855682Smarkm}
16955682Smarkm
170233294Sstasstatic krb5_error_code KRB5_CALLCONV
17155682Smarkmmkt_add_entry(krb5_context context,
17255682Smarkm	      krb5_keytab id,
17355682Smarkm	      krb5_keytab_entry *entry)
17455682Smarkm{
17555682Smarkm    struct mkt_data *d = id->data;
17655682Smarkm    krb5_keytab_entry *tmp;
17755682Smarkm    tmp = realloc(d->entries, (d->num_entries + 1) * sizeof(*d->entries));
17878527Sassar    if(tmp == NULL) {
179233294Sstas	krb5_set_error_message(context, ENOMEM,
180233294Sstas			       N_("malloc: out of memory", ""));
18155682Smarkm	return ENOMEM;
18278527Sassar    }
18355682Smarkm    d->entries = tmp;
184233294Sstas    return krb5_kt_copy_entry_contents(context, entry,
18555682Smarkm				       &d->entries[d->num_entries++]);
18655682Smarkm}
18755682Smarkm
188233294Sstasstatic krb5_error_code KRB5_CALLCONV
18955682Smarkmmkt_remove_entry(krb5_context context,
19055682Smarkm		 krb5_keytab id,
19155682Smarkm		 krb5_keytab_entry *entry)
19255682Smarkm{
19355682Smarkm    struct mkt_data *d = id->data;
19455682Smarkm    krb5_keytab_entry *e, *end;
195178825Sdfr    int found = 0;
196233294Sstas
197178825Sdfr    if (d->num_entries == 0) {
198233294Sstas	krb5_clear_error_message(context);
199178825Sdfr        return KRB5_KT_NOTFOUND;
200178825Sdfr    }
201178825Sdfr
20255682Smarkm    /* do this backwards to minimize copying */
20355682Smarkm    for(end = d->entries + d->num_entries, e = end - 1; e >= d->entries; e--) {
204233294Sstas	if(krb5_kt_compare(context, e, entry->principal,
20555682Smarkm			   entry->vno, entry->keyblock.keytype)) {
20655682Smarkm	    krb5_kt_free_entry(context, e);
20755682Smarkm	    memmove(e, e + 1, (end - e - 1) * sizeof(*e));
20855682Smarkm	    memset(end - 1, 0, sizeof(*end));
20955682Smarkm	    d->num_entries--;
21055682Smarkm	    end--;
211178825Sdfr	    found = 1;
21255682Smarkm	}
21355682Smarkm    }
214178825Sdfr    if (!found) {
215233294Sstas	krb5_clear_error_message (context);
216178825Sdfr	return KRB5_KT_NOTFOUND;
217178825Sdfr    }
21855682Smarkm    e = realloc(d->entries, d->num_entries * sizeof(*d->entries));
219178825Sdfr    if(e != NULL || d->num_entries == 0)
22055682Smarkm	d->entries = e;
22155682Smarkm    return 0;
22255682Smarkm}
22355682Smarkm
22455682Smarkmconst krb5_kt_ops krb5_mkt_ops = {
22555682Smarkm    "MEMORY",
22655682Smarkm    mkt_resolve,
22755682Smarkm    mkt_get_name,
22855682Smarkm    mkt_close,
229233294Sstas    NULL, /* destroy */
23055682Smarkm    NULL, /* get */
23155682Smarkm    mkt_start_seq_get,
23255682Smarkm    mkt_next_entry,
23355682Smarkm    mkt_end_seq_get,
23455682Smarkm    mkt_add_entry,
23555682Smarkm    mkt_remove_entry
23655682Smarkm};
237