Index: libabac/abac_keyid_map.c
===================================================================
--- libabac/abac_keyid_map.c	(revision 94605f2f760296ab619ea8692910ba98131a3abb)
+++ libabac/abac_keyid_map.c	(revision 94605f2f760296ab619ea8692910ba98131a3abb)
@@ -0,0 +1,206 @@
+/* abac_keyid_map.c */
+
+
+#include "abac.h"
+#include "abac_util.h"
+#include "uthash.h"
+
+struct abac_keyid_mapping_t {
+    char *key;
+    char *value;
+    UT_hash_handle hh;
+};
+
+struct abac_keyid_map_t {
+    abac_keyid_mapping_t *keys;
+    abac_keyid_mapping_t *nicknames;
+};
+
+abac_keyid_mapping_t *abac_keyid_mapping_new(char *k, char *v) {
+    abac_keyid_mapping_t *m = NULL;
+
+    if ( !k || ! v) return NULL;
+    m = abac_xmalloc(sizeof(abac_keyid_mapping_t));
+    m->key = abac_xstrdup(k);
+    m->value = abac_xstrdup(v);
+
+    return m;
+}
+
+void abac_keyid_mapping_free(abac_keyid_mapping_t *m) {
+    if ( m->key ) free(m->key);
+    if ( m->value ) free(m->value);
+    free(m);
+}
+
+abac_keyid_map_t *abac_keyid_map_new() {
+    abac_keyid_map_t *m = abac_xmalloc(sizeof(abac_keyid_map_t));
+
+    m->keys = NULL;
+    m->nicknames = NULL;
+    return m;
+}
+
+abac_keyid_map_t *abac_keyid_map_dup(abac_keyid_map_t *old) {
+    abac_keyid_map_t *m = abac_xmalloc(sizeof(abac_keyid_map_t));
+    abac_keyid_mapping_t *me = NULL;
+
+    m->keys = NULL;
+    m->nicknames = NULL;
+
+    for ( me = old->keys; me ; me = me->hh.next) 
+	abac_keyid_map_add_nickname(m, me->key, me->value);
+
+    return m;
+}
+
+void abac_keyid_map_free(abac_keyid_map_t *m) {
+    abac_keyid_mapping_t *me = NULL;
+    while ( (me = m->keys) ) {
+	HASH_DEL(m->keys, me);
+	abac_keyid_mapping_free(me);
+    }
+    while ( (me = m->nicknames) ) {
+	HASH_DEL(m->nicknames, me);
+	abac_keyid_mapping_free(me);
+    }
+    free(m);
+}
+
+char *abac_keyid_map_key_to_nickname(abac_keyid_map_t *m, char *key) {
+    abac_keyid_mapping_t *me = NULL;
+
+    if ( !key) return 0;
+    HASH_FIND_STR(m->keys, key, me);
+
+    if ( me ) return me->value;
+    else return NULL;
+}
+
+char *abac_keyid_map_nickname_to_key(abac_keyid_map_t *m, char *nick) {
+    abac_keyid_mapping_t *me = NULL;
+
+    if ( !nick) return 0;
+    HASH_FIND_STR(m->nicknames, nick, me);
+
+    if ( me ) return me->value;
+    else return NULL;
+}
+
+int abac_keyid_map_remove_keyid(abac_keyid_map_t *m, char *key) {
+    abac_keyid_mapping_t *me = NULL;
+    abac_keyid_mapping_t *nne = NULL;
+
+    HASH_FIND_STR(m->keys, key, me);
+    if ( !me ) return 0;
+    HASH_FIND_STR(m->nicknames, me->value, nne);
+    /* delete from keys */
+    HASH_DEL(m->keys, me);
+    abac_keyid_mapping_free(me);
+    /* If we found a nickname, delete that too */
+    if ( nne ) { 
+	HASH_DEL(m->nicknames, nne);
+	abac_keyid_mapping_free(nne);
+    }
+    return 1;
+}
+
+
+int abac_keyid_map_add_nickname(abac_keyid_map_t *m, char *key, char *nick) {
+    abac_keyid_mapping_t *me = NULL;
+    char *name = NULL;
+    int i =0;
+
+    if ( !key || !nick) return 0;
+    if (abac_keyid_map_key_to_nickname(m, key)) return 0;
+
+    if ( !(name = abac_xmalloc(strlen(nick)+10))) return 0;
+    sprintf(name, "%s", nick);
+
+    while (abac_keyid_map_nickname_to_key(m, name) && i < 1000) 
+	sprintf(name, "%s%05d", nick, i++);
+
+    if ( i < 1000 ) {
+	me = abac_keyid_mapping_new(key, name);
+	HASH_ADD_KEYPTR(hh, m->keys, me->key, strlen(me->key), me);
+	me = abac_keyid_mapping_new(name, key);
+	HASH_ADD_KEYPTR(hh, m->nicknames, me->key, strlen(me->key), me);
+    }
+    free(name);
+    return 1;
+}
+
+static int is_sep(char *c) {
+    switch (*c) {
+	case ' ':
+	case '_':
+	case '.':
+	    return 1;
+	default:
+	    return 0;
+    }
+}
+
+static char *abac_keyid_map_replace(abac_keyid_map_t *m, char *s, 
+	char *(*lookup)(abac_keyid_map_t *, char *)) {
+    int lim = strlen(s);
+    int i; 
+    int sz = 0;
+    char *rv = NULL;
+    char *start = s;
+    char old = '\0';
+    char *repl = NULL;
+    char *newrv = NULL;
+    int newlen = 0;
+
+    for ( i = 0; i < lim; i++ ) {
+	if ( is_sep(s+i) ) {
+	    old = s[i];
+	    s[i] = '\0';
+	   
+	    if ( !(repl = lookup(m, start)) ) repl = start;
+
+	    newlen = strlen(repl)+1;
+
+	    if ( !(newrv = realloc(rv, sz + newlen +1)) ) {
+		if ( rv ) free(rv);
+		return NULL;
+	    } else {
+		rv = newrv;
+		if (sz == 0 ) 
+		    rv[0] = '\0';
+		sz += newlen;
+	    }
+	    strncat(rv, repl, newlen);
+	    rv[sz-1] = old;
+	    rv[sz] = '\0';
+	    s[i] = old;
+	    start = s+i+1;
+	}
+    }
+    if ( start != s+i ) {
+	if ( !(repl = lookup(m, start)) ) repl = start;
+
+	newlen = strlen(repl);
+
+	if ( !(newrv = realloc(rv, sz + newlen)) ) {
+	    if ( rv ) free(rv);
+	    return NULL;
+	} else {
+	    rv = newrv;
+	    if ( sz == 0) 
+		rv[0] = '\0';
+	    sz += newlen;
+	}
+	strncat(rv, repl, newlen);
+    }
+    return rv;
+}
+
+char *abac_keyid_map_expand_key(abac_keyid_map_t *m, char *s) {
+    return abac_keyid_map_replace(m , s, abac_keyid_map_key_to_nickname);
+}
+
+char *abac_keyid_map_expand_nickname(abac_keyid_map_t *m, char *s) {
+    return abac_keyid_map_replace(m , s, abac_keyid_map_nickname_to_key);
+}
