-
Notifications
You must be signed in to change notification settings - Fork 2
/
ngx_form_auth.c
452 lines (395 loc) · 14.7 KB
/
ngx_form_auth.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#include <nginx.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <security/pam_appl.h>
#define NGX_PAM_SERVICE_NAME "nginx"
#define NGX_LOGIN_FIELD "login"
#define NGX_PASSWORD_FIELD "password"
#define PAM_STEP_AUTH 1
#define PAM_STEP_ACCOUNT 2
#define PAM_STEP_ALL 3
typedef struct {
unsigned done:1;
unsigned wait_for_more_body:1;
} ngx_http_form_auth_ctx_t;
typedef struct {
ngx_flag_t enabled;
ngx_str_t pam_service;
ngx_str_t login_field;
ngx_str_t password_field;
ngx_flag_t set_remote_user;
} ngx_http_form_auth_loc_conf_t;
static void * ngx_http_form_auth_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_form_auth_loc_conf_t *conf;
conf = ngx_palloc(cf->pool, sizeof(ngx_http_form_auth_loc_conf_t));
if(conf == NULL) {
return NGX_CONF_ERROR;
}
conf->enabled = NGX_CONF_UNSET;
conf->set_remote_user = NGX_CONF_UNSET;
return conf;
}
static char * ngx_http_form_auth_merge_loc_conf(ngx_conf_t *cf,
void *parent, void *child)
{
ngx_http_form_auth_loc_conf_t *prev = parent;
ngx_http_form_auth_loc_conf_t *conf = child;
ngx_conf_merge_off_value(conf->enabled, prev->enabled, 0);
ngx_conf_merge_off_value(conf->set_remote_user, prev->set_remote_user, 0);
ngx_conf_merge_str_value(conf->pam_service,
prev->pam_service, NGX_PAM_SERVICE_NAME);
ngx_conf_merge_str_value(conf->login_field, prev->login_field,
NGX_LOGIN_FIELD);
ngx_conf_merge_str_value(conf->password_field, prev->password_field,
NGX_PASSWORD_FIELD);
return NGX_CONF_OK;
}
static ngx_command_t ngx_http_form_auth_commands[] = {
{ ngx_string("form_auth"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_form_auth_loc_conf_t, enabled),
NULL },
{ ngx_string("form_auth_pam_service"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_form_auth_loc_conf_t, pam_service),
NULL },
{ ngx_string("form_auth_login"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_form_auth_loc_conf_t, login_field),
NULL },
{ ngx_string("form_auth_password"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_form_auth_loc_conf_t, password_field),
NULL },
{ ngx_string("form_auth_remote_user"),
NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_form_auth_loc_conf_t, set_remote_user),
NULL },
ngx_null_command
};
static ngx_int_t ngx_http_form_auth_init(ngx_conf_t *cf);
static ngx_http_module_t ngx_http_form_auth_module_ctx = {
NULL, /* preconfiguration */
ngx_http_form_auth_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_form_auth_create_loc_conf, /* create location configuration */
ngx_http_form_auth_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_form_auth_module = {
NGX_MODULE_V1,
&ngx_http_form_auth_module_ctx, /* module context */
ngx_http_form_auth_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 precess */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static void post_handler(ngx_http_request_t *r)
{
ngx_http_form_auth_ctx_t *ctx =
ngx_http_get_module_ctx(r, ngx_http_form_auth_module);
ctx->done = 1;
r->main->count--;
if(ctx->wait_for_more_body) {
ctx->wait_for_more_body = 0;
ngx_http_core_run_phases(r);
}
}
void set_str_from_buffer(ngx_http_request_t *r, ngx_str_t *str_to_set,
ngx_str_t *field, u_char *buffer, u_char *last)
{
u_char *pos;
for(pos = buffer; pos < last; pos++) {
pos = ngx_strlcasestrn(pos, last - 1, field->data, field->len - 1);
if(pos == NULL) {
return;
}
if((pos == buffer || *(pos - 1) == '&') && *(pos + field->len) == '=') {
u_char *value = pos + field->len + 1;
pos = ngx_strlchr(value, last, '&');
if(pos == NULL) {
pos = last;
}
str_to_set->len = pos - value;
str_to_set->data = value;
return;
}
}
}
ngx_int_t get_credentials(ngx_http_request_t *r, ngx_str_t *user_login_name,
ngx_str_t *user_password, ngx_str_t *login_field, ngx_str_t *password_field)
{
u_char *last, *buffer;
if(!r->request_body || !r->request_body->bufs) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_form_auth: Empty buffers, no credentials provided");
return NGX_DECLINED;
}
if(r->request_body->bufs->next) {
size_t len = 0;
ngx_chain_t *chain;
for(chain = r->request_body->bufs; chain; chain = chain->next) {
if(!chain->buf->in_file) {
len += chain->buf->last - chain->buf->pos;
}
}
if(!len) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_form_auth: Empty buffers, no credentials provided");
return NGX_DECLINED;
}
buffer = ngx_palloc(r->pool, len);
if(buffer == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_form_auth: ngx_palloc failed.");
return NGX_ERROR;
}
last = buffer + len;
for(chain = r->request_body->bufs; chain; chain = chain->next) {
buffer = ngx_copy(buffer, chain->buf->pos,
chain->buf->last - chain->buf->pos);
}
} else {
if(!ngx_buf_size(r->request_body->bufs->buf)) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_form_auth: Empty buffer, no credentials provided");
return NGX_DECLINED;
}
buffer = r->request_body->bufs->buf->pos;
last = r->request_body->bufs->buf->last;
}
set_str_from_buffer(r, user_login_name, login_field, buffer, last);
set_str_from_buffer(r, user_password, password_field, buffer, last);
user_login_name->data[user_login_name->len] = '\0';
user_password->data[user_password->len] = '\0';
if(!user_login_name->len || !user_password->len) {
return NGX_DECLINED;
}
return NGX_OK;
}
static int pam_conversation(int num, const struct pam_message **message,
struct pam_response **response, void *data)
{
if(!message || !response || !data) {
return PAM_CONV_ERR;
}
struct pam_response *tmp_response =
malloc(num * sizeof(struct pam_response));
if(!tmp_response) {
return PAM_CONV_ERR;
}
int i;
for(i = 0; i < num; i++) {
tmp_response[i].resp = 0;
tmp_response[i].resp_retcode = 0;
if(message[i]->msg_style == PAM_PROMPT_ECHO_OFF) {
if(i == 0) {
tmp_response[i].resp = strdup(data);
} else {
tmp_response[i].resp = NULL;
}
} else {
free(tmp_response);
return PAM_CONV_ERR;
}
}
*response = tmp_response;
return PAM_SUCCESS;
}
static ngx_int_t authenticate_and_authorize(ngx_http_request_t *r,
ngx_int_t steps, ngx_http_form_auth_loc_conf_t *loc_conf,
const char *user, const char *password)
{
pam_handle_t *handle = NULL;
struct pam_conv conversation =
{ &pam_conversation, (void *)password };
int rc = pam_start((const char *)loc_conf->pam_service.data,
user, &conversation, &handle);
if(rc == PAM_SUCCESS) {
if(steps & PAM_STEP_AUTH) {
rc = pam_authenticate(handle, PAM_DISALLOW_NULL_AUTHTOK);
if(rc != PAM_SUCCESS) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_form_auth: Authentication failed for user %s", user);
return NGX_HTTP_UNAUTHORIZED;
}
r->access_code = NGX_OK;
}
if((rc == PAM_SUCCESS) && (steps & PAM_STEP_ACCOUNT)) {
rc = pam_acct_mgmt(handle, PAM_DISALLOW_NULL_AUTHTOK);
if(rc != PAM_SUCCESS) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_form_auth: Authorization failed for user %s", user);
return NGX_HTTP_FORBIDDEN;
}
}
pam_end(handle, rc);
if(rc == PAM_SUCCESS) {
return NGX_OK;
}
return NGX_HTTP_FORBIDDEN;
} else {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_form_auth: Unable to start PAM service: %s",
pam_strerror(handle, rc));
pam_end(handle, rc);
return NGX_ERROR;
}
}
// remote_user should only be set with Basic auth
void set_remote_user(ngx_http_request_t *r)
{
const char *basic_pass = "ngx_form_auth_password";
ngx_str_t user_pass, base64_user_pass, header;
user_pass.len = r->headers_in.user.len + ngx_strlen(basic_pass) + 2;
user_pass.data = ngx_pnalloc(r->pool, user_pass.len);
if(user_pass.data == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_form_auth: Unable to set remote_user variable; not encoded.");
return;
}
ngx_snprintf(user_pass.data, user_pass.len, "%s:%s",
r->headers_in.user.data, basic_pass);
base64_user_pass.len = ngx_base64_encoded_length(user_pass.len);
base64_user_pass.data = ngx_pnalloc(r->pool, base64_user_pass.len);
if(base64_user_pass.data == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_form_auth: Unable to set remote_user variable; encoded.");
return;
}
ngx_encode_base64(&base64_user_pass, &user_pass);
header.len = sizeof("Basic ") + base64_user_pass.len - 1;
header.data = ngx_pnalloc(r->pool, header.len);
if(header.data == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_form_auth: Unable to set remote_user variable; header unset.");
return;
}
ngx_snprintf(header.data, header.len, "Basic %V", &base64_user_pass);
if(r->headers_in.authorization == NULL) {
r->headers_in.authorization =
ngx_pnalloc(r->pool, sizeof(ngx_table_elt_t));
if(r->headers_in.authorization == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_form_auth: Not enough memory.");
return;
}
r->headers_in.authorization->hash = 1;
r->headers_in.authorization->key.len = sizeof("Basic") - 1;
r->headers_in.authorization->key.data = (u_char *)"Basic";
}
r->headers_in.authorization->value.len = header.len;
r->headers_in.authorization->value.data = header.data;
}
static ngx_int_t ngx_http_form_auth_handler(ngx_http_request_t *r)
{
ngx_http_form_auth_ctx_t *ctx =
ngx_http_get_module_ctx(r, ngx_http_form_auth_module);
if(ctx) {
if(ctx->done) {
return NGX_DECLINED;
}
return NGX_DONE;
}
ngx_http_form_auth_loc_conf_t *loc_conf =
ngx_http_get_module_loc_conf(r, ngx_http_form_auth_module);
if(!loc_conf->enabled) {
return NGX_DECLINED;
}
if(r->method != NGX_HTTP_POST) {
return NGX_DECLINED;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_form_auth: PAM service name set to %s",
loc_conf->pam_service.data);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_form_auth: Login field set to %s",
loc_conf->login_field.data);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_form_auth: Passowrd field set to %s",
loc_conf->password_field.data);
if(!loc_conf->set_remote_user) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_form_auth: Remote user variable setting is disabled");
} else {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_form_auth: Remote user variable setting is enabled");
}
int steps = 0;
ngx_int_t rc = 0;
if(r->headers_in.user.len) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_form_auth: User %s already logged in",
r->headers_in.user.data);
steps = PAM_STEP_ACCOUNT;
}
if(!steps) {
ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_form_auth_ctx_t));
if(ctx == NULL) {
return NGX_ERROR;
}
ngx_http_set_ctx(r, ctx, ngx_http_form_auth_module);
rc = ngx_http_read_client_request_body(r, post_handler);
if(rc == NGX_AGAIN) {
ctx->wait_for_more_body = 1;
return NGX_DONE;
}
ngx_str_t user_login_name;
ngx_str_t user_password;
ngx_str_set(&user_login_name, "");
ngx_str_set(&user_password, "");
rc = get_credentials(r, &user_login_name, &user_password,
&loc_conf->login_field, &loc_conf->password_field);
if(rc != NGX_OK) {
return NGX_HTTP_UNAUTHORIZED;
}
ngx_str_set(&r->headers_in.user, user_login_name.data);
ngx_str_set(&r->headers_in.passwd, user_password.data);
steps = PAM_STEP_ALL;
}
rc = authenticate_and_authorize(r, steps, loc_conf,
(const char *)r->headers_in.user.data,
(const char *)r->headers_in.passwd.data);
if(rc != NGX_OK) {
ngx_str_set(&r->headers_in.user, "");
ngx_str_set(&r->headers_in.passwd, "");
} else {
if(loc_conf->set_remote_user) {
set_remote_user(r);
}
}
return rc;
}
static ngx_int_t ngx_http_form_auth_init(ngx_conf_t *conf)
{
ngx_http_handler_pt *handler;
ngx_http_core_main_conf_t *main_conf;
main_conf = ngx_http_conf_get_module_main_conf(conf, ngx_http_core_module);
handler =
ngx_array_push(&main_conf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
if(handler == NULL) {
return NGX_ERROR;
}
*handler = ngx_http_form_auth_handler;
return NGX_OK;
}