Changeset 1546962


Ignore:
Timestamp:
Jul 15, 2013 11:41:56 AM (6 years ago)
Author:
Ted Faber <faber@…>
Branches:
abac0-leak, abac0-mei, master
Children:
1afd681, 28d6d08
Parents:
6cd69a0
Message:

Comments (and correct return value of abac_keyid_map_add_nickname)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libabac/abac_keyid_map.c

    r6cd69a0 r1546962  
    66#include "uthash.h"
    77
     8/*
     9 * A mapping entry that maps key to valus (both char *s).  These can be hashed
     10 * because of the UT_hash_handle - see uthash.h.
     11 */
    812struct abac_keyid_mapping_t {
    913    char *key;
     
    1216};
    1317
     18/*
     19 * A map from keyids -> nicknames and nicknames to keys.  These are managed by
     20 * the libabac reference counting system, hence the refcount
     21 */
    1422struct abac_keyid_map_t {
    15     abac_keyid_mapping_t *keys;
    16     abac_keyid_mapping_t *nicknames;
     23    abac_keyid_mapping_t *keys;         /* Key to nickname map */
     24    abac_keyid_mapping_t *nicknames;    /* Nickname to key map */
    1725    int refcount;
    1826};
    1927
     28/*
     29 * Create a new mapping entry from key(k) to value (v).  It must be freed using
     30 * abac_keyid_mapping_free.
     31 */
    2032abac_keyid_mapping_t *abac_keyid_mapping_new(char *k, char *v) {
    2133    abac_keyid_mapping_t *m = NULL;
     
    2941}
    3042
     43/*
     44 * Free the given mapping.  These are not reference counted, so free the key
     45 * and value, and then the mapping memory
     46 */
    3147void abac_keyid_mapping_free(abac_keyid_mapping_t *m) {
    3248    if ( m->key ) free(m->key);
     
    3551}
    3652
     53/*
     54 * Create a new keyid map.  These are reference counted and must be freed using
     55 * abac_keyid_map_free.
     56 */
    3757abac_keyid_map_t *abac_keyid_map_new() {
    3858    abac_keyid_map_t *m = abac_xmalloc(sizeof(abac_keyid_map_t));
     
    4464}
    4565
    46 /* Make a new, independent copy of the old keymap. */
     66/*
     67 * Make a new, independent copy of the old keymap. This allocates new memory
     68 * with a new reference count.  abac_keyid_map_free must be called on it.
     69 */
    4770abac_keyid_map_t *abac_keyid_map_clone(abac_keyid_map_t *old) {
    4871    abac_keyid_map_t *m = abac_xmalloc(sizeof(abac_keyid_map_t));
     
    5982}
    6083
    61 /* Add a reference to old and return it. */
     84/*
     85 * Add a reference to the old map and return it.  The reference count has been
     86 * incremented, so the underlying memory will not be freed until
     87 * abac_keyid_map_free is called once for each reference.
     88 */
    6289abac_keyid_map_t *abac_keyid_map_dup(abac_keyid_map_t *old) {
    6390    old->refcount ++;
     
    6592}
    6693
     94/*
     95 * Free the reference-counted map m.  Decrement the reference count.  If and
     96 * only if the reference count is 0 or less, delete all the associated memory.
     97 */
    6798void abac_keyid_map_free(abac_keyid_map_t *m) {
    6899    abac_keyid_mapping_t *me = NULL;
     
    81112}
    82113
     114/*
     115 * Return the nickname associated with this keyid, if nay.  The caller is
     116 * responsible for freeing the returned string.
     117 */
    83118char *abac_keyid_map_key_to_nickname(abac_keyid_map_t *m, char *key) {
    84119    abac_keyid_mapping_t *me = NULL;
     
    91126}
    92127
     128/*
     129 * Return the keyid associated with this nickname, if nay.  The caller is
     130 * responsible for freeing the returned string.
     131 */
    93132char *abac_keyid_map_nickname_to_key(abac_keyid_map_t *m, char *nick) {
    94133    abac_keyid_mapping_t *me = NULL;
     
    101140}
    102141
     142/*
     143 * Remove this keyid from both mappings.
     144 */
    103145int abac_keyid_map_remove_keyid(abac_keyid_map_t *m, char *key) {
    104146    abac_keyid_mapping_t *me = NULL;
     
    119161}
    120162
    121 
     163/*
     164 * If this keyid is not mapped to a nickname, add a mapping from key to
     165 * nickname.  If the nickname is already assigned to a key, disambiguate it by
     166 * adding trailing numbers.  If more than 1000 tries are made to disambiguate,
     167 * give up.
     168 */
    122169int abac_keyid_map_add_nickname(abac_keyid_map_t *m, char *key, char *nick) {
    123170    abac_keyid_mapping_t *me = NULL;
    124171    char *name = NULL;
     172    char *p = NULL;
    125173    int i =0;
    126174
    127175    if ( !key || !nick) return 0;
    128     if (abac_keyid_map_key_to_nickname(m, key)) return 0;
     176    if ( (p = abac_keyid_map_key_to_nickname(m, key))) {
     177        free(p);
     178        return 0;
     179    }
    129180
    130181    if ( !(name = abac_xmalloc(strlen(nick)+10))) return 0;
     
    141192    }
    142193    free(name);
    143     return 1;
    144 }
    145 
     194    return (i < 1000);
     195}
     196
     197/*
     198 * Merge the mappings in src to the mappings in dest.  If overwrite is true,
     199 * src mappings always overwrite dest mappings, otherwise the dest mappings
     200 * remain.  Calls abac_keyid_map_add_nickname internally, so nicknames in src
     201 * that are also in dest are disambiguated.
     202 */
    146203void abac_keyid_map_merge(abac_keyid_map_t *dest, abac_keyid_map_t *src,
    147204        int overwrite) {
     
    163220}
    164221
     222/*
     223 * Utility function to identify separator characters in a role.
     224 */
    165225static int is_sep(char *c) {
    166226    switch (*c) {
     
    174234}
    175235
     236/*
     237 * Break s into sections by separator characters (see is_sep) and copy them
     238 * into a return value.  If lookup finds a replacement for a section, the
     239 * replacement is used, otherwise the original string is used.  Using different
     240 * lookup functions implements expand_key and expand_name.  In either case, the
     241 * caller is responsible for freeing the returned value.
     242 */
    176243static char *abac_keyid_map_replace(abac_keyid_map_t *m, char *s,
    177244        char *(*lookup)(abac_keyid_map_t *, char *)) {
     
    230297}
    231298
     299/*
     300 * Break s up into sections and replace any keyids that have nicknames with
     301 * those nicknames.  The caller is responsible for freeing the return value.
     302 */
    232303char *abac_keyid_map_expand_key(abac_keyid_map_t *m, char *s) {
    233304    return abac_keyid_map_replace(m , s, abac_keyid_map_key_to_nickname);
    234305}
    235306
     307/*
     308 * Break s up into sections and replace any nicknames that have keyids with
     309 * those keyids.  The caller is responsible for freeing the return value.
     310 */
    236311char *abac_keyid_map_expand_nickname(abac_keyid_map_t *m, char *s) {
    237312    return abac_keyid_map_replace(m , s, abac_keyid_map_nickname_to_key);
Note: See TracChangeset for help on using the changeset viewer.