#include "k5-int.h"
#include <errno.h>
static krb5_error_code KRB5_CALLCONV krb5_mcc_close
(krb5_context, krb5_ccache id );
static krb5_error_code KRB5_CALLCONV krb5_mcc_destroy
(krb5_context, krb5_ccache id );
static krb5_error_code KRB5_CALLCONV krb5_mcc_end_seq_get
(krb5_context, krb5_ccache id , krb5_cc_cursor *cursor );
static krb5_error_code KRB5_CALLCONV krb5_mcc_generate_new
(krb5_context, krb5_ccache *id );
static const char * KRB5_CALLCONV krb5_mcc_get_name
(krb5_context, krb5_ccache id );
static krb5_error_code KRB5_CALLCONV krb5_mcc_get_principal
(krb5_context, krb5_ccache id , krb5_principal *princ );
static krb5_error_code KRB5_CALLCONV krb5_mcc_initialize
(krb5_context, krb5_ccache id , krb5_principal princ );
static krb5_error_code KRB5_CALLCONV krb5_mcc_next_cred
(krb5_context,
krb5_ccache id ,
krb5_cc_cursor *cursor ,
krb5_creds *creds );
static krb5_error_code KRB5_CALLCONV krb5_mcc_resolve
(krb5_context, krb5_ccache *id , const char *residual );
static krb5_error_code KRB5_CALLCONV krb5_mcc_retrieve
(krb5_context,
krb5_ccache id ,
krb5_flags whichfields ,
krb5_creds *mcreds ,
krb5_creds *creds );
static krb5_error_code KRB5_CALLCONV krb5_mcc_start_seq_get
(krb5_context, krb5_ccache id , krb5_cc_cursor *cursor );
static krb5_error_code KRB5_CALLCONV krb5_mcc_store
(krb5_context, krb5_ccache id , krb5_creds *creds );
static krb5_error_code KRB5_CALLCONV krb5_mcc_set_flags
(krb5_context, krb5_ccache id , krb5_flags flags );
static krb5_error_code KRB5_CALLCONV krb5_mcc_ptcursor_new(
krb5_context,
krb5_cc_ptcursor *);
static krb5_error_code KRB5_CALLCONV krb5_mcc_ptcursor_next(
krb5_context,
krb5_cc_ptcursor,
krb5_ccache *);
static krb5_error_code KRB5_CALLCONV krb5_mcc_ptcursor_free(
krb5_context,
krb5_cc_ptcursor *);
extern const krb5_cc_ops krb5_mcc_ops;
extern krb5_error_code krb5_change_cache (void);
#define KRB5_OK 0
typedef struct _krb5_mcc_link {
struct _krb5_mcc_link *next;
krb5_creds *creds;
} krb5_mcc_link, *krb5_mcc_cursor;
typedef struct _krb5_mcc_data {
char *name;
k5_mutex_t lock;
krb5_principal prin;
krb5_mcc_cursor link;
} krb5_mcc_data;
typedef struct krb5_mcc_list_node {
struct krb5_mcc_list_node *next;
krb5_mcc_data *cache;
} krb5_mcc_list_node;
struct krb5_mcc_ptcursor_data {
struct krb5_mcc_list_node *cur;
};
k5_mutex_t krb5int_mcc_mutex = K5_MUTEX_PARTIAL_INITIALIZER;
static krb5_mcc_list_node *mcc_head = 0;
static void krb5_mcc_free (krb5_context context, krb5_ccache id);
krb5_error_code KRB5_CALLCONV
krb5_mcc_initialize(krb5_context context, krb5_ccache id, krb5_principal princ)
{
krb5_error_code ret;
krb5_mcc_free(context, id);
ret = krb5_copy_principal(context, princ,
&((krb5_mcc_data *)id->data)->prin);
if (ret == KRB5_OK)
krb5_change_cache();
return ret;
}
krb5_error_code KRB5_CALLCONV
krb5_mcc_close(krb5_context context, krb5_ccache id)
{
krb5_xfree(id);
return KRB5_OK;
}
void
krb5_mcc_free(krb5_context context, krb5_ccache id)
{
krb5_mcc_cursor curr,next;
krb5_mcc_data *d;
d = (krb5_mcc_data *) id->data;
for (curr = d->link; curr;) {
krb5_free_creds(context, curr->creds);
next = curr->next;
krb5_xfree(curr);
curr = next;
}
d->link = NULL;
krb5_free_principal(context, d->prin);
}
krb5_error_code KRB5_CALLCONV
krb5_mcc_destroy(krb5_context context, krb5_ccache id)
{
krb5_mcc_list_node **curr, *node;
krb5_mcc_data *d;
krb5_error_code err;
err = k5_mutex_lock(&krb5int_mcc_mutex);
if (err)
return err;
d = (krb5_mcc_data *)id->data;
for (curr = &mcc_head; *curr; curr = &(*curr)->next) {
if ((*curr)->cache == d) {
node = *curr;
*curr = node->next;
free(node);
break;
}
}
k5_mutex_unlock(&krb5int_mcc_mutex);
krb5_mcc_free(context, id);
krb5_xfree(d->name);
k5_mutex_destroy(&d->lock);
krb5_xfree(d);
krb5_xfree(id);
krb5_change_cache ();
return KRB5_OK;
}
static krb5_error_code new_mcc_data (const char *, krb5_mcc_data **);
krb5_error_code KRB5_CALLCONV
krb5_mcc_resolve (krb5_context context, krb5_ccache *id, const char *residual)
{
krb5_ccache lid;
krb5_mcc_list_node *ptr;
krb5_error_code err;
krb5_mcc_data *d;
lid = (krb5_ccache) malloc(sizeof(struct _krb5_ccache));
if (lid == NULL)
return KRB5_CC_NOMEM;
lid->ops = &krb5_mcc_ops;
err = k5_mutex_lock(&krb5int_mcc_mutex);
if (err)
return err;
for (ptr = mcc_head; ptr; ptr=ptr->next)
if (!strcmp(ptr->cache->name, residual))
break;
if (ptr)
d = ptr->cache;
else {
err = new_mcc_data(residual, &d);
if (err) {
k5_mutex_unlock(&krb5int_mcc_mutex);
krb5_xfree(lid);
return err;
}
}
k5_mutex_unlock(&krb5int_mcc_mutex);
lid->data = d;
*id = lid;
return KRB5_OK;
}
krb5_error_code KRB5_CALLCONV
krb5_mcc_start_seq_get(krb5_context context, krb5_ccache id,
krb5_cc_cursor *cursor)
{
krb5_mcc_cursor mcursor;
krb5_error_code err;
krb5_mcc_data *d;
d = id->data;
err = k5_mutex_lock(&d->lock);
if (err)
return err;
mcursor = d->link;
k5_mutex_unlock(&d->lock);
*cursor = (krb5_cc_cursor) mcursor;
return KRB5_OK;
}
krb5_error_code KRB5_CALLCONV
krb5_mcc_next_cred(krb5_context context, krb5_ccache id,
krb5_cc_cursor *cursor, krb5_creds *creds)
{
krb5_mcc_cursor mcursor;
krb5_error_code retval;
krb5_data *scratch;
mcursor = (krb5_mcc_cursor) *cursor;
if (mcursor == NULL)
return KRB5_CC_END;
memset(creds, 0, sizeof(krb5_creds));
if (mcursor->creds) {
*creds = *mcursor->creds;
retval = krb5_copy_principal(context, mcursor->creds->client, &creds->client);
if (retval)
return retval;
retval = krb5_copy_principal(context, mcursor->creds->server,
&creds->server);
if (retval)
goto cleanclient;
retval = krb5_copy_keyblock_contents(context, &mcursor->creds->keyblock,
&creds->keyblock);
if (retval)
goto cleanserver;
retval = krb5_copy_addresses(context, mcursor->creds->addresses,
&creds->addresses);
if (retval)
goto cleanblock;
retval = krb5_copy_data(context, &mcursor->creds->ticket, &scratch);
if (retval)
goto cleanaddrs;
creds->ticket = *scratch;
krb5_xfree(scratch);
retval = krb5_copy_data(context, &mcursor->creds->second_ticket, &scratch);
if (retval)
goto cleanticket;
creds->second_ticket = *scratch;
krb5_xfree(scratch);
retval = krb5_copy_authdata(context, mcursor->creds->authdata,
&creds->authdata);
if (retval)
goto clearticket;
}
*cursor = (krb5_cc_cursor)mcursor->next;
return KRB5_OK;
clearticket:
memset(creds->ticket.data,0, (unsigned) creds->ticket.length);
cleanticket:
krb5_xfree(creds->ticket.data);
cleanaddrs:
krb5_free_addresses(context, creds->addresses);
cleanblock:
krb5_xfree(creds->keyblock.contents);
cleanserver:
krb5_free_principal(context, creds->server);
cleanclient:
krb5_free_principal(context, creds->client);
return retval;
}
krb5_error_code KRB5_CALLCONV
krb5_mcc_end_seq_get(krb5_context context, krb5_ccache id, krb5_cc_cursor *cursor)
{
*cursor = 0L;
return KRB5_OK;
}
static krb5_error_code
new_mcc_data (const char *name, krb5_mcc_data **dataptr)
{
krb5_error_code err;
krb5_mcc_data *d;
krb5_mcc_list_node *n;
d = malloc(sizeof(krb5_mcc_data));
if (d == NULL)
return KRB5_CC_NOMEM;
err = k5_mutex_init(&d->lock);
if (err) {
krb5_xfree(d);
return err;
}
d->name = malloc(strlen(name) + 1);
if (d->name == NULL) {
k5_mutex_destroy(&d->lock);
krb5_xfree(d);
return KRB5_CC_NOMEM;
}
d->link = NULL;
d->prin = NULL;
strcpy(d->name, name);
n = malloc(sizeof(krb5_mcc_list_node));
if (n == NULL) {
free(d->name);
k5_mutex_destroy(&d->lock);
free(d);
return KRB5_CC_NOMEM;
}
n->cache = d;
n->next = mcc_head;
mcc_head = n;
*dataptr = d;
return 0;
}
static krb5_error_code random_string (krb5_context, char *, unsigned int);
krb5_error_code KRB5_CALLCONV
krb5_mcc_generate_new (krb5_context context, krb5_ccache *id)
{
krb5_ccache lid;
char uniquename[8];
krb5_error_code err;
krb5_mcc_data *d;
lid = (krb5_ccache) malloc(sizeof(struct _krb5_ccache));
if (lid == NULL)
return KRB5_CC_NOMEM;
lid->ops = &krb5_mcc_ops;
err = k5_mutex_lock(&krb5int_mcc_mutex);
if (err) {
free(lid);
return err;
}
while (1) {
krb5_mcc_list_node *ptr;
random_string (context, uniquename, sizeof (uniquename));
for (ptr = mcc_head; ptr; ptr=ptr->next) {
if (!strcmp(ptr->cache->name, uniquename)) {
break;
}
}
if (!ptr) break;
}
err = new_mcc_data(uniquename, &d);
k5_mutex_unlock(&krb5int_mcc_mutex);
if (err) {
krb5_xfree(lid);
return err;
}
lid->data = d;
*id = lid;
krb5_change_cache ();
return KRB5_OK;
}
static krb5_error_code
random_string (krb5_context context, char *string, unsigned int length)
{
static const unsigned char charlist[] =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
krb5_error_code err = 0;
unsigned char *bytes = NULL;
unsigned int bytecount = length - 1;
if (!err) {
bytes = malloc (bytecount);
if (bytes == NULL) { err = ENOMEM; }
}
if (!err) {
krb5_data data;
data.length = bytecount;
data.data = (char *) bytes;
err = krb5_c_random_make_octets (context, &data);
}
if (!err) {
unsigned int i;
for (i = 0; i < bytecount; i++) {
string [i] = charlist[bytes[i] % (sizeof (charlist) - 1)];
}
string[length - 1] = '\0';
}
if (bytes != NULL) { free (bytes); }
return err;
}
const char * KRB5_CALLCONV
krb5_mcc_get_name (krb5_context context, krb5_ccache id)
{
return (char *) ((krb5_mcc_data *) id->data)->name;
}
krb5_error_code KRB5_CALLCONV
krb5_mcc_get_principal(krb5_context context, krb5_ccache id, krb5_principal *princ)
{
krb5_mcc_data *ptr = (krb5_mcc_data *)id->data;
if (!ptr->prin) {
*princ = 0L;
return KRB5_FCC_NOFILE;
}
return krb5_copy_principal(context, ptr->prin, princ);
}
krb5_error_code KRB5_CALLCONV
krb5_mcc_retrieve(krb5_context context, krb5_ccache id, krb5_flags whichfields,
krb5_creds *mcreds, krb5_creds *creds)
{
return krb5_cc_retrieve_cred_default (context, id, whichfields,
mcreds, creds);
}
static krb5_error_code KRB5_CALLCONV
krb5_mcc_remove_cred(krb5_context context, krb5_ccache cache, krb5_flags flags,
krb5_creds *creds)
{
return KRB5_CC_NOSUPP;
}
krb5_error_code KRB5_CALLCONV
krb5_mcc_set_flags(krb5_context context, krb5_ccache id, krb5_flags flags)
{
return KRB5_OK;
}
static krb5_error_code KRB5_CALLCONV
krb5_mcc_get_flags(krb5_context context, krb5_ccache id, krb5_flags *flags)
{
*flags = 0;
return KRB5_OK;
}
krb5_error_code KRB5_CALLCONV
krb5_mcc_store(krb5_context ctx, krb5_ccache id, krb5_creds *creds)
{
krb5_error_code err;
krb5_mcc_link *new_node;
krb5_mcc_data *mptr = (krb5_mcc_data *)id->data;
new_node = malloc(sizeof(krb5_mcc_link));
if (new_node == NULL)
return errno;
err = krb5_copy_creds(ctx, creds, &new_node->creds);
if (err) {
free(new_node);
return err;
}
err = k5_mutex_lock(&mptr->lock);
if (err)
return err;
new_node->next = mptr->link;
mptr->link = new_node;
k5_mutex_unlock(&mptr->lock);
return 0;
}
static krb5_error_code KRB5_CALLCONV
krb5_mcc_ptcursor_new(
krb5_context context,
krb5_cc_ptcursor *cursor)
{
krb5_error_code ret = 0;
krb5_cc_ptcursor n = NULL;
struct krb5_mcc_ptcursor_data *cdata = NULL;
*cursor = NULL;
n = malloc(sizeof(*n));
if (n == NULL)
return ENOMEM;
n->ops = &krb5_mcc_ops;
cdata = malloc(sizeof(struct krb5_mcc_ptcursor_data));
if (cdata == NULL) {
ret = ENOMEM;
goto errout;
}
n->data = cdata;
ret = k5_mutex_lock(&krb5int_mcc_mutex);
if (ret)
goto errout;
cdata->cur = mcc_head;
ret = k5_mutex_unlock(&krb5int_mcc_mutex);
if (ret)
goto errout;
errout:
if (ret) {
krb5_mcc_ptcursor_free(context, &n);
}
*cursor = n;
return ret;
}
static krb5_error_code KRB5_CALLCONV
krb5_mcc_ptcursor_next(
krb5_context context,
krb5_cc_ptcursor cursor,
krb5_ccache *ccache)
{
krb5_error_code ret = 0;
struct krb5_mcc_ptcursor_data *cdata = NULL;
*ccache = NULL;
cdata = cursor->data;
if (cdata->cur == NULL)
return 0;
*ccache = malloc(sizeof(**ccache));
if (*ccache == NULL)
return ENOMEM;
(*ccache)->ops = &krb5_mcc_ops;
(*ccache)->data = cdata->cur->cache;
ret = k5_mutex_lock(&krb5int_mcc_mutex);
if (ret)
goto errout;
cdata->cur = cdata->cur->next;
ret = k5_mutex_unlock(&krb5int_mcc_mutex);
if (ret)
goto errout;
errout:
if (ret && *ccache != NULL) {
free(*ccache);
*ccache = NULL;
}
return ret;
}
static krb5_error_code KRB5_CALLCONV
krb5_mcc_ptcursor_free(
krb5_context context,
krb5_cc_ptcursor *cursor)
{
if (*cursor == NULL)
return 0;
if ((*cursor)->data != NULL)
free((*cursor)->data);
free(*cursor);
*cursor = NULL;
return 0;
}
const krb5_cc_ops krb5_mcc_ops = {
0,
"MEMORY",
krb5_mcc_get_name,
krb5_mcc_resolve,
krb5_mcc_generate_new,
krb5_mcc_initialize,
krb5_mcc_destroy,
krb5_mcc_close,
krb5_mcc_store,
krb5_mcc_retrieve,
krb5_mcc_get_principal,
krb5_mcc_start_seq_get,
krb5_mcc_next_cred,
krb5_mcc_end_seq_get,
krb5_mcc_remove_cred,
krb5_mcc_set_flags,
krb5_mcc_get_flags,
krb5_mcc_ptcursor_new,
krb5_mcc_ptcursor_next,
krb5_mcc_ptcursor_free,
NULL,
NULL,
NULL,
};