-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnss_cdb.c
209 lines (181 loc) · 5.81 KB
/
nss_cdb.c
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* $Id: nss_cdb.c,v 1.5 2006-06-28 17:49:21 mjt Exp $
* nss_cdb common routines.
*
* This file is a part of tinycdb package by Michael Tokarev, [email protected].
* Public domain.
*/
#include "nss_cdb.h"
#include "cdb_int.h" /* for internal_function */
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#if __GLIBC__ /* XXX this is in fact not a right condition */
/* XXX on glibc, this stuff works due to linker/libpthreads stubs/tricks.
* On other libcs, it may require linking whole -lpthread, which is
* not a good thing to do for nss module...
*/
#include <pthread.h>
#define lock_define(class,name) \
class pthread_mutex_t name = PTHREAD_MUTEX_INITIALIZER;
#define lock_lock(name) pthread_mutex_lock(&(name))
#define lock_unlock(name) pthread_mutex_unlock(&(name))
#else /* !__GNU_LIBRARY__ */
# define lock_define_initialized(class,name)
# define lock_lock(name)
# define lock_unlock(name)
#endif /* __GNU_LIBRARY__ */
lock_define(static, lock)
/* General principle: we skip invalid/unparseable entries completely,
* as if there was no such entry at all (returning NOTFOUND).
* In case of data read error (e.g. invalid .cdb structure), we
* return UNAVAIL.
*/
#define isopen(dbp) ((dbp)->lastpos)
static int
__nss_cdb_dosetent(struct nss_cdb *dbp) {
int fd;
fd = open(dbp->dbname, O_RDONLY);
if (fd < 0)
return 0;
if (cdb_init(&dbp->cdb, fd) != 0) {
close(fd);
return 0;
}
close(fd);
dbp->lastpos = 2048; /* cdb_seqinit() */
return 1;
}
static void
__nss_cdb_doendent(struct nss_cdb *dbp) {
cdb_free(&dbp->cdb);
dbp->lastpos = 0;
dbp->keepopen = 0;
}
enum nss_status internal_function
__nss_cdb_setent(struct nss_cdb *dbp, int stayopen) {
enum nss_status r;
lock_lock(lock);
if (isopen(dbp) || __nss_cdb_dosetent(dbp))
r = NSS_STATUS_SUCCESS, dbp->keepopen |= stayopen;
else
r = NSS_STATUS_UNAVAIL;
lock_unlock(lock);
return r;
}
enum nss_status internal_function
__nss_cdb_endent(struct nss_cdb *dbp) {
lock_lock(lock);
if (isopen(dbp))
__nss_cdb_doendent(dbp);
lock_unlock(lock);
return NSS_STATUS_SUCCESS;
}
static enum nss_status
__nss_cdb_dobyname(struct nss_cdb *dbp, const char *key, unsigned len,
void *result, char *buf, size_t bufl, int *errnop) {
int r;
if ((r = cdb_find(&dbp->cdb, key, len)) < 0)
return *errnop = errno, NSS_STATUS_UNAVAIL;
len = cdb_datalen(&dbp->cdb);
if (!r || len < 2)
return *errnop = ENOENT, NSS_STATUS_NOTFOUND;
if (len >= bufl)
return *errnop = ERANGE, NSS_STATUS_TRYAGAIN;
if (cdb_read(&dbp->cdb, buf, len, cdb_datapos(&dbp->cdb)) != 0)
return *errnop = errno, NSS_STATUS_UNAVAIL;
buf[len] = '\0';
if ((r = dbp->parsefn(result, buf, bufl)) < 0)
return *errnop = ENOENT, NSS_STATUS_NOTFOUND;
if (!r)
return *errnop = ERANGE, NSS_STATUS_TRYAGAIN;
return NSS_STATUS_SUCCESS;
}
enum nss_status internal_function
__nss_cdb_byname(struct nss_cdb *dbp, const char *name,
void *result, char *buf, size_t bufl, int *errnop) {
enum nss_status r;
if (*name == ':')
return *errnop = ENOENT, NSS_STATUS_NOTFOUND;
lock_lock(lock);
if (!isopen(dbp) && !__nss_cdb_dosetent(dbp))
*errnop = errno, r = NSS_STATUS_UNAVAIL;
else {
r = __nss_cdb_dobyname(dbp, name, strlen(name), result, buf, bufl, errnop);
if (!dbp->keepopen)
__nss_cdb_doendent(dbp);
}
lock_unlock(lock);
return r;
}
static enum nss_status
__nss_cdb_dobyid(struct nss_cdb *dbp, unsigned long id,
void *result, char *buf, size_t bufl, int *errnop) {
int r;
unsigned len;
const char *data;
if ((r = cdb_find(&dbp->cdb, buf, sprintf(buf, ":%lu", id))) < 0)
return *errnop = errno, NSS_STATUS_UNAVAIL;
len = cdb_datalen(&dbp->cdb);
if (!r || len < 2)
return *errnop = ENOENT, NSS_STATUS_NOTFOUND;
if (!(data = (const char*)cdb_get(&dbp->cdb, len, cdb_datapos(&dbp->cdb))))
return *errnop = errno, NSS_STATUS_UNAVAIL;
return __nss_cdb_dobyname(dbp, data, len, result, buf, bufl, errnop);
}
enum nss_status internal_function
__nss_cdb_byid(struct nss_cdb *dbp, unsigned long id,
void *result, char *buf, size_t bufl, int *errnop) {
enum nss_status r;
if (bufl < 30)
return *errnop = ERANGE, NSS_STATUS_TRYAGAIN;
lock_lock(lock);
if (!isopen(dbp) && !__nss_cdb_dosetent(dbp))
*errnop = errno, r = NSS_STATUS_UNAVAIL;
else {
r = __nss_cdb_dobyid(dbp, id, result, buf, bufl, errnop);
if (!dbp->keepopen)
__nss_cdb_doendent(dbp);
}
lock_unlock(lock);
return r;
}
static enum nss_status
__nss_cdb_dogetent(struct nss_cdb *dbp,
void *result, char *buf, size_t bufl, int *errnop) {
int r;
unsigned lastpos;
if (!isopen(dbp) && !__nss_cdb_dosetent(dbp))
return *errnop = errno, NSS_STATUS_UNAVAIL;
while((lastpos = dbp->lastpos, r = cdb_seqnext(&dbp->lastpos, &dbp->cdb)) > 0)
{
if (cdb_keylen(&dbp->cdb) < 2) continue;
if (((const char *)cdb_getkey(&dbp->cdb))[0] == ':') /* can't fail */
continue;
if (cdb_datalen(&dbp->cdb) >= bufl)
return dbp->lastpos = lastpos, *errnop = ERANGE, NSS_STATUS_TRYAGAIN;
cdb_readdata(&dbp->cdb, buf);
buf[cdb_datalen(&dbp->cdb)] = '\0';
if ((r = dbp->parsefn(result, buf, bufl)) == 0)
return dbp->lastpos = lastpos, *errnop = ERANGE, NSS_STATUS_TRYAGAIN;
if (r > 0)
return NSS_STATUS_SUCCESS;
}
if (r < 0)
return *errnop = errno, NSS_STATUS_UNAVAIL;
else
return *errnop = ENOENT, NSS_STATUS_NOTFOUND;
}
enum nss_status internal_function
__nss_cdb_getent(struct nss_cdb *dbp,
void *result, char *buf, size_t bufl, int *errnop) {
enum nss_status r;
if (bufl < 30)
return *errnop = ERANGE, NSS_STATUS_TRYAGAIN;
lock_lock(lock);
dbp->keepopen |= 1;
r = __nss_cdb_dogetent(dbp, result, buf, bufl, errnop);
lock_unlock(lock);
return r;
}