summaryrefslogtreecommitdiffstats
path: root/collage/idmap.c
blob: bede4457ba0918deedb024a49dc86fd2e0ed2801 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <grp.h>
#include <pwd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include "idmap.h"

struct idMap_s {
    struct idElement * byId;
    int numEntries;
};

typedef struct idMap_s * idMap;

struct idElement {
    long int id;
    char * name;
};

typedef void * (*iterFn)(void);
typedef int (*infoFn)(void * item, struct idElement * el);

static idMap uidMap = NULL;
static idMap gidMap = NULL;

static int idCmp(const void * a, const void * b) {
    const struct idElement * one = a;
    const struct idElement * two = b;

    if (one->id < two->id)
	return -1;
    else if (one->id > two->id)
	return 1;

    return 0;
}

static idMap readmap(iterFn fn, infoFn info) {
    idMap map;
    int alloced;
    void * res;
    struct idElement * newEntries;

    map = malloc(sizeof(*map));
    if (!map) {
	return NULL;
    }

    alloced = 5;
    map->byId = malloc(sizeof(*map->byId) * alloced);
    if (!map->byId) {
	free(map);
	return NULL;
    }
    map->numEntries = 0;

    while ((res = fn())) {
	if (map->numEntries == alloced) {
	    alloced += 5;
	    newEntries = realloc(map->byId, 
					sizeof(*map->byId) * alloced);
	    if (!newEntries) {
		/* FIXME: this doesn't free the id names */
		free(map->byId);
		free(map);
		return NULL;
	    }

	    map->byId = newEntries;
	}

	if (info(res, map->byId + map->numEntries++)) {
	    /* FIXME: this doesn't free the id names */
	    free(map->byId);
	    free(map);
	    return NULL;
	}
    }

    map->byId = realloc(map->byId, 
				sizeof(*map->byId) * map->numEntries);

    qsort(map->byId, map->numEntries, sizeof(*map->byId), idCmp);

    return map;
}

static int pwInfo(struct passwd * pw, struct idElement * el) {
    el->id = pw->pw_uid;
    el->name = strdup(pw->pw_name);

    return el->name == NULL;
}

static int grInfo(struct group * gr, struct idElement * el) {
    el->id = gr->gr_gid;
    el->name = strdup(gr->gr_name);

    return el->name == NULL;
}

idMap readUIDmap(void) {
    idMap result;

    result = readmap((void *) getpwent, (void *) pwInfo);
    endpwent();

    return result;
}

idMap readGIDmap(void) {
    idMap result;

    result = readmap((void *) getgrent, (void *) grInfo);
    endgrent();

    return result;
}

char * idSearchByUid(long int id) {
    struct idElement el = { id, NULL };
    struct idElement * match;

    match = bsearch(&el, uidMap->byId, uidMap->numEntries, 
		   sizeof(*uidMap->byId), idCmp);

    if (match) return match->name; else return NULL;
}

char * idSearchByGid(long int id) {
    struct idElement el = { id, NULL };
    struct idElement * match;

    match = bsearch(&el, gidMap->byId, gidMap->numEntries, 
		   sizeof(*gidMap->byId), idCmp);

    if (match) return match->name; else return NULL;
}

int idInit(void) {
    if (!(uidMap = readUIDmap())) return 1;
    if (!(gidMap = readGIDmap())) return 1;

    return 0;
}