~ubuntu-branches/ubuntu/vivid/libpam-tacplus/vivid

« back to all changes in this revision

Viewing changes to libtac/lib/attrib.c

  • Committer: Bazaar Package Importer
  • Author(s): Jeroen Nijhof
  • Date: 2011-09-05 16:01:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110905160100-7o4aqefbiflj4232
Tags: upstream-1.3.5
ImportĀ upstreamĀ versionĀ 1.3.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* attrib.c - Procedures for handling internal list of attributes
 
2
 *               for accounting and authorization functions. 
 
3
 * 
 
4
 * Copyright (C) 2010, Pawel Krawczyk <pawel.krawczyk@hush.com> and
 
5
 * Jeroen Nijhof <jeroen@nijhofnet.nl>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program - see the file COPYING.
 
19
 *
 
20
 * See `CHANGES' file for revision history.
 
21
 */
 
22
 
 
23
#include "tacplus.h"
 
24
#include "libtac.h"
 
25
#include "xalloc.h"
 
26
 
 
27
void tac_add_attrib(struct tac_attrib **attr, char *name, char *value) {
 
28
    tac_add_attrib_pair(attr, name, '=', value);
 
29
}
 
30
 
 
31
void tac_add_attrib_pair(struct tac_attrib **attr, char *name, char sep, char *value) {
 
32
    struct tac_attrib *a;
 
33
    u_char l1 = (u_char) strlen(name);
 
34
    u_char l2 = (u_char) strlen(value);
 
35
    int total_len = l1 + l2 + 1; /* "name" + "=" + "value" */
 
36
 
 
37
    if (total_len > 255) {
 
38
        TACSYSLOG((LOG_WARNING,\
 
39
            "%s: attribute `%s' total length exceeds 255 characters, skipping",\
 
40
            __FUNCTION__, name))
 
41
        return;
 
42
    }
 
43
    
 
44
    /* initialize the list if application passed us a null pointer */
 
45
    if(*attr == NULL) {
 
46
        *attr = (struct tac_attrib *) xcalloc(1, sizeof(struct tac_attrib));
 
47
        a = *attr;
 
48
    } else {
 
49
        /* find the last allocated block */
 
50
        a = *attr;
 
51
        while(a->next != NULL)
 
52
            a = a->next; /* a holds last allocated block */
 
53
 
 
54
        a->next = (struct tac_attrib *) xcalloc(1, sizeof(struct tac_attrib)); 
 
55
        a = a->next; /* set current block pointer to the new one */
 
56
    }
 
57
 
 
58
    if ( sep != '=' && sep != '*' ) {
 
59
        sep = '=';
 
60
    }
 
61
 
 
62
    /* fill the block */
 
63
    a->attr_len=total_len;
 
64
    a->attr = (char *) xcalloc(1, total_len+1);
 
65
    bcopy(name, a->attr, l1);    /* paste name */
 
66
    *(a->attr+l1)=sep;           /* insert seperator "[=*]" */
 
67
    bcopy(value, (a->attr+l1+1), l2); /* paste value */
 
68
    *(a->attr+total_len) = '\0';      /* add 0 for safety */
 
69
    a->next = NULL; /* make sure it's null */
 
70
}
 
71
 
 
72
void tac_free_attrib(struct tac_attrib **attr) {
 
73
    struct tac_attrib *a;
 
74
    struct tac_attrib *b;
 
75
 
 
76
    if(*attr == NULL)
 
77
            return;
 
78
 
 
79
    a  = b = *attr;
 
80
    
 
81
    /* find last allocated block */
 
82
    do {
 
83
        a = b;
 
84
        b = a->next;
 
85
        free(a->attr);
 
86
        free(a);
 
87
    } while (b != NULL);
 
88
}