forked from agamez/libnss-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shadow.c
213 lines (180 loc) · 6.04 KB
/
shadow.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
209
210
211
212
213
/*
* 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
*/
/*
* shadow.c : Functions handling shadow entries retrieval.
*/
#include "nss-sqlite.h"
#include "utils.h"
#include <errno.h>
#include <grp.h>
#include <malloc.h>
#include <pwd.h>
#include <shadow.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
/*
* struct used to store data used by getspent.
*/
static struct {
sqlite3* pDb;
sqlite3_stmt* pSt;
int try_again; /* flag to know if NSS_TRYAGAIN
was returned by previous call
to getspent_r */
/* user information cache used if NSS_TRYAGAIN was returned */
struct spwd entry;
} spent_data = { NULL, NULL, 0, NULL};
/* mutex used to serialize xxspent operation */
pthread_mutex_t spent_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
/**
* Setup everything needed to retrieve shadow entries.
*/
enum nss_status _nss_sqlite_setspent(void) {
char* sql;
pthread_mutex_lock(&spent_mutex);
if(spent_data.pDb == NULL) {
NSS_DEBUG("setspent: opening DB connection\n");
if(sqlite3_open(NSS_SQLITE_SHADOW_DB, &spent_data.pDb) != SQLITE_OK) {
NSS_ERROR(sqlite3_errmsg(spent_data.pDb));
return NSS_STATUS_UNAVAIL;
}
if(!(sql = get_query(spent_data.pDb, "setspent")) ) {
NSS_ERROR(sqlite3_errmsg(spent_data.pDb));
sqlite3_close(spent_data.pDb);
return NSS_STATUS_UNAVAIL;
}
if(sqlite3_prepare(spent_data.pDb, sql, -1, &spent_data.pSt, NULL) != SQLITE_OK) {
NSS_ERROR(sqlite3_errmsg(spent_data.pDb));
sqlite3_finalize(spent_data.pSt);
sqlite3_close(spent_data.pDb);
free(sql);
return NSS_STATUS_UNAVAIL;
}
}
free(sql);
pthread_mutex_unlock(&spent_mutex);
return NSS_STATUS_SUCCESS;
}
/*
* Free getspent resources.
*/
enum nss_status _nss_sqlite_endspent(void) {
NSS_DEBUG("endspent: finalizing shadow serial access facilities\n");
pthread_mutex_lock(&spent_mutex);
if(spent_data.pDb != NULL) {
sqlite3_finalize(spent_data.pSt);
sqlite3_close(spent_data.pDb);
spent_data.pDb = NULL;
}
pthread_mutex_unlock(&spent_mutex);
return NSS_STATUS_SUCCESS;
}
/*
* Return next shadow entry. see man getspent_r
* @param pwbuf Buffer to store shadow 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_getspent_r(struct spwd *spbuf, char *buf,
size_t buflen, int *errnop) {
int res;
NSS_DEBUG("getspent_r\n");
pthread_mutex_lock(&spent_mutex);
if(spent_data.pDb == NULL) {
_nss_sqlite_setspent();
}
if(spent_data.try_again) {
res = fill_shadow(spbuf, buf, buflen, spent_data.entry, errnop);
/* buffer was long enough this time */
if(res != NSS_STATUS_TRYAGAIN || (*errnop) != ERANGE) {
spent_data.try_again = 0;
pthread_mutex_unlock(&spent_mutex);
return res;
}
}
res = res2nss_status(sqlite3_step(spent_data.pSt), spent_data.pDb, spent_data.pSt);
if(res != NSS_STATUS_SUCCESS) {
spent_data.pDb = NULL;
pthread_mutex_unlock(&spent_mutex);
return res;
}
fill_shadow_sql(&spent_data.entry, spent_data.pSt);
res = fill_shadow(spbuf, buf, buflen, spent_data.entry, errnop);
NSS_DEBUG("getspent_r: fetched user %s\n", spent_data.entry.sp_namp);
if(res == NSS_STATUS_TRYAGAIN && (*errnop) == ERANGE) {
/* cache result for next try */
spent_data.try_again = 1;
pthread_mutex_unlock(&spent_mutex);
return NSS_STATUS_TRYAGAIN;
}
pthread_mutex_unlock(&spent_mutex);
return NSS_STATUS_SUCCESS;
}
/*
* Get shadow information using username.
*/
enum nss_status _nss_sqlite_getspnam_r(const char* name, struct spwd *spbuf,
char *buf, size_t buflen, int *errnop) {
sqlite3 *pDb;
struct sqlite3_stmt* pSquery;
int res;
struct spwd entry;
char* query;
NSS_DEBUG("getspnam_r: looking for user %s (shadow)\n", name);
if(sqlite3_open(NSS_SQLITE_SHADOW_DB, &pDb) != SQLITE_OK) {
NSS_ERROR(sqlite3_errmsg(pDb));
sqlite3_close(pDb);
return NSS_STATUS_UNAVAIL;
}
if(!(query = get_query(pDb, "getspnam_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_shadow_sql(&entry, pSquery);
res = fill_shadow(spbuf, buf, buflen, entry, errnop);
free(query);
sqlite3_finalize(pSquery);
sqlite3_close(pDb);
return res;
}