-
Notifications
You must be signed in to change notification settings - Fork 10
/
ngx_http_auth_sqlite_basic_module.c
306 lines (231 loc) · 9.58 KB
/
ngx_http_auth_sqlite_basic_module.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Copyright (C) Kunal Bharati
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <sqlite3.h>
#define NGX_HTTP_AUTH_BUF_SIZE 2048
typedef struct {
ngx_str_t realm;
ngx_str_t sqlite_table;
ngx_str_t sqlite_user;
ngx_str_t sqlite_passwd;
ngx_str_t sqlite_db_file;
} ngx_http_auth_sqlite_basic_loc_conf_t;
static ngx_int_t ngx_http_auth_sqlite_basic_handler(ngx_http_request_t *r);
static ngx_int_t ngx_http_auth_sqlite_basic_set_realm(ngx_http_request_t *r,
ngx_str_t *realm);
static void *ngx_http_auth_sqlite_basic_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_auth_sqlite_basic_merge_loc_conf(ngx_conf_t *cf,
void *parent, void *child);
static ngx_int_t ngx_http_auth_sqlite_basic_init(ngx_conf_t *cf);
static char *ngx_http_auth_sqlite_basic(ngx_conf_t *cf, void *post, void *data);
static ngx_conf_post_handler_pt ngx_http_auth_sqlite_basic_p = ngx_http_auth_sqlite_basic;
static ngx_command_t ngx_http_auth_sqlite_basic_commands[] = {
{ ngx_string("auth_sqlite_basic"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF
|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_auth_sqlite_basic_loc_conf_t, realm),
&ngx_http_auth_sqlite_basic_p },
{ ngx_string("auth_sqlite_basic_database_file"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF
|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_auth_sqlite_basic_loc_conf_t, sqlite_db_file),
NULL },
{ ngx_string("auth_sqlite_basic_database_table"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF
|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_auth_sqlite_basic_loc_conf_t, sqlite_table),
NULL },
{ ngx_string("auth_sqlite_basic_table_user_column"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF
|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_auth_sqlite_basic_loc_conf_t, sqlite_user),
NULL },
{ ngx_string("auth_sqlite_basic_table_passwd_column"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF
|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_auth_sqlite_basic_loc_conf_t, sqlite_passwd),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_auth_sqlite_basic_module_ctx = {
NULL, /* preconfiguration */
ngx_http_auth_sqlite_basic_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_auth_sqlite_basic_create_loc_conf, /* create location configuration */
ngx_http_auth_sqlite_basic_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_auth_sqlite_basic_module = {
NGX_MODULE_V1,
&ngx_http_auth_sqlite_basic_module_ctx, /* module context */
ngx_http_auth_sqlite_basic_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_int_t
ngx_http_auth_sqlite_basic_handler(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_http_auth_sqlite_basic_loc_conf_t *alcf;
/* Sqlite info */
ngx_int_t sqlite_return_value;
sqlite3_stmt *sqlite_stmt;
sqlite3 *sqlite_handle;
alcf = ngx_http_get_module_loc_conf(r, ngx_http_auth_sqlite_basic_module);
if (alcf->realm.len == 0 || alcf->sqlite_db_file.len == 0) {
return NGX_DECLINED;
}
rc = ngx_http_auth_basic_user(r);
if (rc == NGX_DECLINED) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no user/password was provided for basic authentication");
return ngx_http_auth_sqlite_basic_set_realm(r, &alcf->realm);
}
if (rc == NGX_ERROR) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
sqlite_return_value = sqlite3_open((char *) alcf->sqlite_db_file.data, &sqlite_handle);
if(sqlite_return_value) {
ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0, "Opening \"%s\" database failed", alcf->sqlite_db_file.data);
return rc;
}
//FIXME: Dirty work done here as r->headers_in.user.data values is username:password. Following code reads the r->headers_in.user.data upto ":"
ngx_uint_t i = 0;
ngx_str_t login, encoded; // = (char *) malloc(r->headers_in.user.data.len);
encoded = r->headers_in.authorization->value;
encoded.len -= sizeof("Basic ") - 1;
encoded.data += sizeof("Basic ") - 1;
login.len = ngx_base64_decoded_length(encoded.len);
login.data = ngx_pnalloc(r->pool, login.len + 1);
while (r->headers_in.user.data[i] != ':') {
login.data[i] = r->headers_in.user.data[i];
i++;
}
login.data[i] = '\0';
// End //
//calculating the length of the required char array
int select_query_len = strlen("select * from ") +
strlen((char *) alcf->sqlite_table.data) +
strlen(" where ") +
strlen((char *) alcf->sqlite_user.data) +
strlen(" = \"") +
strlen((char *) login.data) +
strlen("\" and ") +
strlen((char *) alcf->sqlite_passwd.data) +
strlen(" = \"") +
strlen((char *) r->headers_in.passwd.data) +
strlen("\"");
//creating array based on calculated length
char select_query[select_query_len + 1];
sprintf(select_query, "select * from %s where %s = \"%s\" and %s = \"%s\"", (char *) alcf->sqlite_table.data
, (char *) alcf->sqlite_user.data
, (char *) login.data
, (char *) alcf->sqlite_passwd.data
, (char *) r->headers_in.passwd.data);
const char* tail;
sqlite_return_value = sqlite3_prepare_v2(sqlite_handle, select_query, strlen(select_query), &sqlite_stmt, &tail);
if (sqlite_return_value != SQLITE_OK) {
ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0, "Unable to fetch data from \"%s\" database", alcf->sqlite_db_file.data);
sqlite3_close(sqlite_handle);
return ngx_http_auth_sqlite_basic_set_realm(r, &alcf->realm);
}
sqlite_return_value = sqlite3_step(sqlite_stmt);
if(sqlite_return_value == SQLITE_ROW) {
sqlite3_close(sqlite_handle);
return rc;
}
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s was not found in %s table", r->headers_in.user.data, alcf->sqlite_table.data);
sqlite3_close(sqlite_handle);
return ngx_http_auth_sqlite_basic_set_realm(r, &alcf->realm);
}
static ngx_int_t
ngx_http_auth_sqlite_basic_set_realm(ngx_http_request_t *r, ngx_str_t *realm)
{
r->headers_out.www_authenticate = ngx_list_push(&r->headers_out.headers);
if (r->headers_out.www_authenticate == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
r->headers_out.www_authenticate->hash = 1;
ngx_str_set(&r->headers_out.www_authenticate->key, "WWW-Authenticate");
r->headers_out.www_authenticate->value = *realm;
return NGX_HTTP_UNAUTHORIZED;
}
static void *
ngx_http_auth_sqlite_basic_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_auth_sqlite_basic_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_auth_sqlite_basic_loc_conf_t));
if (conf == NULL) {
return NULL;
}
return conf;
}
static char *
ngx_http_auth_sqlite_basic_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_auth_sqlite_basic_loc_conf_t *prev = parent;
ngx_http_auth_sqlite_basic_loc_conf_t *conf = child;
if (conf->realm.data == NULL) {
conf->realm = prev->realm;
}
if (conf->sqlite_db_file.len == 0) {
conf->sqlite_db_file = prev->sqlite_db_file;
}
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_auth_sqlite_basic_init(ngx_conf_t *cf)
{
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_auth_sqlite_basic_handler;
return NGX_OK;
}
static char *
ngx_http_auth_sqlite_basic(ngx_conf_t *cf, void *post, void *data)
{
ngx_str_t *realm = data;
size_t len;
u_char *basic, *p;
if (ngx_strcmp(realm->data, "off") == 0) {
ngx_str_set(realm, "");
return NGX_CONF_OK;
}
len = sizeof("Basic realm=\"") - 1 + realm->len + 1;
basic = ngx_pnalloc(cf->pool, len);
if (basic == NULL) {
return NGX_CONF_ERROR;
}
p = ngx_cpymem(basic, "Basic realm=\"", sizeof("Basic realm=\"") - 1);
p = ngx_cpymem(p, realm->data, realm->len);
*p = '"';
realm->len = len;
realm->data = basic;
return NGX_CONF_OK;
}