-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathpasswd.c
More file actions
275 lines (233 loc) · 7.66 KB
/
passwd.c
File metadata and controls
275 lines (233 loc) · 7.66 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Copyright (C) 2007, Sébastien Le Ray
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* passwd.c : Functions handling passwd entries retrieval.
*/
#include "nss-sqlite.h"
#include "utils.h"
#include <errno.h>
#include <grp.h>
#include <malloc.h>
#include <pwd.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
/*
* struct used to store data used by getpwent.
*/
static struct {
sqlite3* pDb;
sqlite3_stmt* pSt;
int try_again; /* flag to know if NSS_TRYAGAIN
was returned by previous call
to getpwent_r */
/* user information cache used if NSS_TRYAGAIN was returned */
struct passwd entry;
} pwent_data = { NULL, NULL, 0, NULL};
/* mutex used to serialize xxpwent operation */
pthread_mutex_t pwent_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
/**
* Setup everything needed to retrieve passwd entries.
*/
enum nss_status _nss_sqlite_setpwent(void) {
char* sql;
pthread_mutex_lock(&pwent_mutex);
if(pwent_data.pDb == NULL) {
NSS_DEBUG("setpwent: opening DB connection\n");
if(sqlite3_open(NSS_SQLITE_PASSWD_DB, &pwent_data.pDb) != SQLITE_OK) {
NSS_ERROR(sqlite3_errmsg(pwent_data.pDb));
return NSS_STATUS_UNAVAIL;
}
if(!(sql = get_query(pwent_data.pDb, "setpwent")) ) {
NSS_ERROR(sqlite3_errmsg(pwent_data.pDb));
sqlite3_close(pwent_data.pDb);
return NSS_STATUS_UNAVAIL;
}
if(sqlite3_prepare(pwent_data.pDb, sql, -1, &pwent_data.pSt, NULL) != SQLITE_OK) {
NSS_ERROR(sqlite3_errmsg(pwent_data.pDb));
sqlite3_finalize(pwent_data.pSt);
sqlite3_close(pwent_data.pDb);
free(sql);
return NSS_STATUS_UNAVAIL;
}
}
free(sql);
pthread_mutex_unlock(&pwent_mutex);
return NSS_STATUS_SUCCESS;
}
/*
* Free getpwent resources.
*/
enum nss_status _nss_sqlite_endpwent(void) {
NSS_DEBUG("endpwent: finalizing passwd serial access facilities\n");
pthread_mutex_lock(&pwent_mutex);
if(pwent_data.pDb != NULL) {
sqlite3_finalize(pwent_data.pSt);
sqlite3_close(pwent_data.pDb);
pwent_data.pDb = NULL;
}
pthread_mutex_unlock(&pwent_mutex);
return NSS_STATUS_SUCCESS;
}
/*
* Return next passwd entry. see man getpwent_r
* @param pwbuf Buffer to store passwd data.
* @param buf Buffer which will contain all string pointed
* to by pwbuf entries.
* @param buflen buf length.
* @param errnop Pointer to errno, will be filled if
* an error occurs.
*/
enum nss_status
_nss_sqlite_getpwent_r(struct passwd *pwbuf, char *buf,
size_t buflen, int *errnop) {
int res;
const unsigned char* name;
uid_t uid;
gid_t gid;
const unsigned char* shell;
const unsigned char* homedir;
NSS_DEBUG("getpwent_r\n");
pthread_mutex_lock(&pwent_mutex);
if(pwent_data.pDb == NULL) {
_nss_sqlite_setpwent();
}
if(pwent_data.try_again) {
res = fill_passwd(pwbuf, buf, buflen, pwent_data.entry, errnop);
/* buffer was long enough this time */
if(res != NSS_STATUS_TRYAGAIN || (*errnop) != ERANGE) {
pwent_data.try_again = 0;
pthread_mutex_unlock(&pwent_mutex);
return res;
}
}
res = res2nss_status(sqlite3_step(pwent_data.pSt), pwent_data.pDb, pwent_data.pSt);
if(res != NSS_STATUS_SUCCESS) {
pwent_data.pDb = NULL;
pthread_mutex_unlock(&pwent_mutex);
return res;
}
fill_passwd_sql(&pwent_data.entry, pwent_data.pSt);
res = fill_passwd(pwbuf, buf, buflen, pwent_data.entry, errnop);
NSS_DEBUG("getpwent_r: fetched user #%d: %s\n", uid, name);
if(res == NSS_STATUS_TRYAGAIN && (*errnop) == ERANGE) {
/* cache result for next try */
pwent_data.try_again = 1;
pthread_mutex_unlock(&pwent_mutex);
return NSS_STATUS_TRYAGAIN;
}
pthread_mutex_unlock(&pwent_mutex);
return NSS_STATUS_SUCCESS;
}
/**
* Get user info by username.
* Open database connection, fetch the user by name, close the connection.
*/
enum nss_status _nss_sqlite_getpwnam_r(const char* name, struct passwd *pwbuf,
char *buf, size_t buflen, int *errnop) {
sqlite3 *pDb;
struct sqlite3_stmt* pSquery;
char* query;
int res;
struct passwd entry;
NSS_DEBUG("getpwnam_r: Looking for user %s\n", name);
if(sqlite3_open(NSS_SQLITE_PASSWD_DB, &pDb) != SQLITE_OK) {
NSS_ERROR(sqlite3_errmsg(pDb));
sqlite3_close(pDb);
return NSS_STATUS_UNAVAIL;
}
if(!(query = get_query(pDb, "getpwnam_r")) ) {
NSS_ERROR(sqlite3_errmsg(pDb));
sqlite3_close(pDb);
return NSS_STATUS_UNAVAIL;
}
if(sqlite3_prepare(pDb, query, strlen(query), &pSquery, NULL) != SQLITE_OK) {
NSS_ERROR(sqlite3_errmsg(pDb));
free(query);
sqlite3_finalize(pSquery);
sqlite3_close(pDb);
return FALSE;
}
if(sqlite3_bind_text(pSquery, 1, name, -1, SQLITE_STATIC) != SQLITE_OK) {
NSS_DEBUG(sqlite3_errmsg(pDb));
free(query);
sqlite3_finalize(pSquery);
sqlite3_close(pDb);
return NSS_STATUS_UNAVAIL;
}
res = res2nss_status(sqlite3_step(pSquery), pDb, pSquery);
if(res != NSS_STATUS_SUCCESS) {
free(query);
return res;
}
fill_passwd_sql(&entry, pSquery);
res = fill_passwd(pwbuf, buf, buflen, entry, errnop);
free(query);
sqlite3_finalize(pSquery);
sqlite3_close(pDb);
NSS_DEBUG("Look successfull !\n");
return res;
}
/*
* Get user by UID.
*/
enum nss_status _nss_sqlite_getpwuid_r(uid_t uid, struct passwd *pwbuf,
char *buf, size_t buflen, int *errnop) {
sqlite3 *pDb;
struct sqlite3_stmt* pSquery;
char* query;
int res, nss_res;
struct passwd entry;
NSS_DEBUG("getpwuid_r: looking for user #%d\n", uid);
if(sqlite3_open(NSS_SQLITE_PASSWD_DB, &pDb) != SQLITE_OK) {
NSS_ERROR(sqlite3_errmsg(pDb));
sqlite3_close(pDb);
return NSS_STATUS_UNAVAIL;
}
if(!(query = get_query(pDb, "getpwuid_r")) ) {
NSS_ERROR(sqlite3_errmsg(pDb));
sqlite3_close(pDb);
return NSS_STATUS_UNAVAIL;
}
if(sqlite3_prepare(pDb, query, strlen(query), &pSquery, NULL) != SQLITE_OK) {
NSS_ERROR(sqlite3_errmsg(pDb));
free(query);
sqlite3_finalize(pSquery);
sqlite3_close(pDb);
return FALSE;
}
if(sqlite3_bind_int(pSquery, 1, uid) != SQLITE_OK) {
NSS_DEBUG(sqlite3_errmsg(pDb));
free(query);
sqlite3_finalize(pSquery);
sqlite3_close(pDb);
return NSS_STATUS_UNAVAIL;
}
res = sqlite3_step(pSquery);
nss_res = res2nss_status(res, pDb, pSquery);
if(nss_res != NSS_STATUS_SUCCESS) {
free(query);
return nss_res;
}
fill_passwd_sql(&entry, pSquery);
res = fill_passwd(pwbuf, buf, buflen, entry, errnop);
free(query);
sqlite3_finalize(pSquery);
sqlite3_close(pDb);
return res;
}