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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
|
#include "usuals.h"
#include "httpauthd.h"
#include "md5.h"
#include "sha1.h"
#include "bd.h"
#include <sys/time.h>
/* Postgresql library */
#include <libpq-fe.h>
/* -------------------------------------------------------------------------------
* Structures
*/
#define DB_PW_CLEAR 0
#define DB_PW_SHA1 1
#define DB_PW_MD5 2
#define DB_PW_CRYPT 3
/* Our hanler context */
typedef struct pgsql_context
{
/* Base Handler ------------------------------------------------------ */
bd_context_t bd;
/* Settings ---------------------------------------------------------- */
const char* host; /* The connection host or path */
const char* port; /* The connection port */
const char* user; /* The pgsql user name */
const char* password; /* The pgsql password */
const char* database; /* The database name */
const char* query; /* The query */
const char* pw_column; /* The database query to retrieve a password */
int pw_type; /* The type of password encoded in database */
const char* ha1_column; /* The database query to retrieve a ha1 */
int pgsql_max; /* Number of open connections allowed */
int pgsql_timeout; /* Maximum amount of time to dedicate to a query */
/* Context ----------------------------------------------------------- */
PGconn** pool; /* Pool of available connections */
int pool_mark; /* Amount of connections allocated */
}
pgsql_context_t;
/* Forward declarations for callbacks */
static int validate_digest(ha_request_t* rq, const char* user, digest_context_t* dg);
static int validate_basic(ha_request_t* rq, const char* user, const char* password);
static void escape_pgsql(const ha_request_t* rq, ha_buffer_t* buf, const char* value);
/* The defaults for the context */
static const pgsql_context_t pgsql_defaults =
{
BD_CALLBACKS(validate_digest,
validate_basic, escape_pgsql),
NULL, /* host */
0, /* port */
NULL, /* user */
NULL, /* password */
NULL, /* database */
NULL, /* query */
NULL, /* pw_attr */
DB_PW_CLEAR, /* pw_type */
NULL, /* ha1_attr */
10, /* pgsql_max */
30, /* pgsql_timeout */
NULL, /* pool */
0 /* pool_mark */
};
/* -------------------------------------------------------------------------------
* Internal Functions
*/
static void escape_pgsql(const ha_request_t* rq, ha_buffer_t* buf, const char* value)
{
size_t len;
char* t;
ASSERT(value);
len = strlen(value);
t = (char*)malloc((len * 2) + 1);
if(t != NULL)
{
PQescapeString(t, value, len);
ha_bufcpy(buf, t);
free(t);
}
}
static int dec_pgsql_binary(const ha_request_t* rq, const char* enc,
unsigned char* bytes, size_t len)
{
size_t enclen;
void* d;
ASSERT(rq && enc && bytes && len);
enclen = strlen(enc);
/* Hex encoded */
if(enclen == (len * 2))
{
d = ha_bufdechex(rq->buf, enc, &enclen);
if(d && enclen == len)
{
ha_messagex(rq, LOG_DEBUG, "found value in hex encoded format");
memcpy(bytes, d, len);
return HA_OK;
}
}
/* Raw binary postgres encoded */
d = PQunescapeBytea(enc, &enclen);
if(d != NULL)
{
if(enclen == len)
memcpy(bytes, d, len);
PQfreemem(d);
if(enclen == len)
{
ha_messagex(rq, LOG_DEBUG, "found value in raw binary format");
return HA_OK;
}
}
/* B64 Encoded */
enclen = strlen(enc);
d = ha_bufdec64(rq->buf, enc, &enclen);
if(d && len == enclen)
{
ha_messagex(rq, LOG_DEBUG, "found value in b64 encoded format");
memcpy(bytes, d, len);
return HA_OK;
}
return CHECK_RBUF(rq) ? HA_CRITERROR : HA_FALSE;
}
static int validate_ha1(ha_request_t* rq, pgsql_context_t* ctx, const char* user,
const char* clearpw, const char* dbpw)
{
unsigned char dbha1[MD5_LEN];
unsigned char ha1[MD5_LEN];
const char* p;
int r = dec_pgsql_binary(rq, dbpw, dbha1, MD5_LEN);
if(r < 0)
return r;
if(r == HA_OK)
{
digest_makeha1(ha1, user, rq->context->realm, clearpw);
if(memcmp(ha1, dbha1, MD5_LEN) == 0)
return HA_OK;
}
return HA_FALSE;
}
static int validate_password(ha_request_t* rq, pgsql_context_t* ctx, const char* user,
const char* clearpw, const char* dbpw)
{
unsigned char buf[SHA1_LEN];
const char* p;
int r;
ASSERT(SHA1_LEN > MD5_LEN);
switch(ctx->pw_type)
{
/* Clear text */
case DB_PW_CLEAR:
if(strcmp(clearpw, dbpw) == 0)
{
ha_messagex(rq, LOG_DEBUG, "found matching clear text password");
return HA_OK;
}
break;
/* Crypt pw */
case DB_PW_CRYPT:
ha_lock(NULL);
p = (const char*)crypt(clearpw, dbpw);
ha_unlock(NULL);
if(p && strcmp(dbpw, p) == 0)
{
ha_messagex(rq, LOG_DEBUG, "found matching crypt password");
return HA_OK;
}
break;
/* MD5 */
case DB_PW_MD5:
r = dec_pgsql_binary(rq, dbpw, buf, MD5_LEN);
if(r < 0) return r;
if(r == HA_OK && md5_strcmp(buf, clearpw) == 0)
{
ha_messagex(rq, LOG_DEBUG, "found matching md5 password");
return HA_OK;
}
break;
/* SHA1 */
case DB_PW_SHA1:
r = dec_pgsql_binary(rq, dbpw, buf, SHA1_LEN);
if(r < 0) return r;
if(r == HA_OK && sha1_strcmp(buf, clearpw) == 0)
{
ha_messagex(rq, LOG_DEBUG, "found matching sha1 password");
return HA_OK;
}
break;
default:
ASSERT(0 && "invalid password type");
break;
};
return HA_FALSE;
}
static PGconn* get_pgsql_connection(const ha_request_t* rq, pgsql_context_t* ctx)
{
PGconn* pg;
int i;
ASSERT(ctx);
for(i = 0; i < ctx->pgsql_max; i++)
{
/* An open connection in the pool */
if(ctx->pool[i])
{
ha_messagex(rq, LOG_DEBUG, "using cached pgsql connection");
pg = ctx->pool[i];
ctx->pool[i] = NULL;
return pg;
}
}
if(ctx->pool_mark >= ctx->pgsql_max)
{
ha_messagex(rq, LOG_ERR, "too many open pgsql connections");
return NULL;
}
pg = PQsetdbLogin(ctx->host, ctx->port, NULL, NULL, ctx->database,
ctx->user, ctx->password);
if(!pg)
{
ha_messagex(rq, LOG_CRIT, "internal error in postgres library");
return NULL;
}
if(PQstatus(pg) == CONNECTION_BAD)
{
ha_messagex(rq, LOG_ERR, "error opening pgsql connection: %s", PQerrorMessage(pg));
PQfinish(pg);
return NULL;
}
ctx->pool_mark++;
ha_messagex(rq, LOG_DEBUG, "opened new pgsql connection (total %d)", ctx->pool_mark);
return pg;
}
static void discard_pgsql_connection(const ha_request_t* rq, pgsql_context_t* ctx, PGconn* pg)
{
PQfinish(pg);
ctx->pool_mark--;
ha_messagex(rq, LOG_DEBUG, "discarding pgsql connection (total %d)", ctx->pool_mark);
}
static void save_pgsql_connection(const ha_request_t* rq, pgsql_context_t* ctx, PGconn* pg)
{
int i, e;
ASSERT(ctx);
if(!pg)
return;
/* Make sure it's worth saving */
if(PQstatus(pg) != CONNECTION_BAD)
{
for(i = 0; i < ctx->pgsql_max; i++)
{
/* An open connection in the pool */
if(!ctx->pool[i])
{
ha_messagex(rq, LOG_DEBUG, "caching pgsql connection for later use");
ctx->pool[i] = pg;
pg = NULL;
break;
}
}
}
if(pg != NULL)
discard_pgsql_connection(rq, ctx, pg);
}
static int check_pgsql_result(ha_request_t* rq, PGresult* res)
{
switch(PQresultStatus(res))
{
case PGRES_COMMAND_OK:
case PGRES_EMPTY_QUERY:
ha_messagex(rq, LOG_WARNING, "query did not return data");
return HA_FALSE;
case PGRES_TUPLES_OK:
return HA_OK;
case PGRES_BAD_RESPONSE:
ha_messagex(rq, LOG_ERR, "error communicating with pgsql server");
return HA_FAILED;
case PGRES_NONFATAL_ERROR:
ha_messagex(rq, LOG_ERR, "warning querying database: %s", PQresultErrorMessage(res));
return HA_OK;
case PGRES_FATAL_ERROR:
ha_messagex(rq, LOG_ERR, "error querying database: %s", PQresultErrorMessage(res));
return HA_FAILED;
case PGRES_COPY_OUT:
case PGRES_COPY_IN:
default:
ASSERT(0 && "unexpected response");
return HA_FAILED;
};
return HA_OK;
}
static int resolve_column(PGresult* res, const char* column)
{
int i;
if(column)
{
for(i = 0; i < PQnfields(res); i++)
{
if(strcasecmp(column, PQfname(res, i)) == 0)
return i;
}
}
return -1;
}
static int retrieve_user_rows(ha_request_t* rq, pgsql_context_t* ctx,
const char* user, PGresult** results)
{
PGconn* pg = NULL;
PGresult* res = NULL;
const char* query;
int ret = HA_OK;
ASSERT(rq && ctx && user && results);
*results = NULL;
pg = get_pgsql_connection(rq, ctx);
if(!pg)
RETURN(HA_FAILED);
ASSERT(ctx->query);
/* The map can have %u and %r to denote user and realm */
query = bd_substitute(rq, user, ctx->query);
if(!query)
RETURN(HA_CRITERROR);
ha_messagex(rq, LOG_DEBUG, "executing query: %s", query);
res = PQexec(pg, query);
ret = check_pgsql_result(rq, res);
if(ret != HA_OK)
RETURN(ret);
if(PQntuples(res) == 0)
{
ha_messagex(rq, LOG_WARNING, "login failed. couldn't find user: %s", user);
RETURN(HA_FALSE);
}
if(PQnfields(res) <= 0)
{
ha_messagex(rq, LOG_ERR, "query returned 0 columns: %s", query);
RETURN(HA_FAILED);
}
*results = res;
res = NULL;
ha_messagex(rq, LOG_DEBUG, "received %d result rows", PQntuples(res));
finally:
if(res != NULL)
PQclear(res);
/* According to libpg we can close/save the connection
* before the returned results are freed, no worries there */
if(pg != NULL)
save_pgsql_connection(rq, ctx, pg);
return ret;
}
static int validate_digest(ha_request_t* rq, const char* user, digest_context_t* dg)
{
pgsql_context_t* ctx = (pgsql_context_t*)rq->context->ctx_data;
PGresult* res = NULL;
int ret = HA_FALSE;
int pw_column = -1;
int ha1_column = -1;
int r, i, foundany = 0;
ASSERT(rq && user && dg);
ret = retrieve_user_rows(rq, ctx, user, &res);
if(ret != HA_OK)
RETURN(ret);
ASSERT(res);
pw_column = resolve_column(res, ctx->pw_column);
ha1_column = resolve_column(res, ctx->ha1_column);
if(pw_column == -1 && ha1_column == -1)
{
if(PQnfields(res) > 1)
ha_messagex(rq, LOG_WARNING, "query returned more than 1 column, using first as password");
pw_column = 0;
}
for(i = 0; i < PQntuples(res); i++)
{
if(pw_column != -1)
{
if(ctx->pw_type == DB_PW_CLEAR && !PQgetisnull(res, i, pw_column))
{
foundany = 1;
digest_makeha1(dg->ha1, user, rq->context->realm, PQgetvalue(res, i, pw_column));
ha_messagex(rq, LOG_DEBUG, "testing clear text password for digest auth");
/* Run the actual check */
ret = digest_complete_check(dg, rq->buf);
if(ret != HA_FALSE)
RETURN(ret);
}
}
if(ha1_column != -1)
{
if(!PQgetisnull(res, i, ha1_column))
{
ret = dec_pgsql_binary(rq, PQgetvalue(res, i, ha1_column), dg->ha1, MD5_LEN);
if(ret < 0)
RETURN(ret)
else if(ret == HA_FALSE)
continue;
foundany = 1;
/* Run the actual check */
ret = digest_complete_check(dg, rq->buf);
if(ret != HA_FALSE)
RETURN(ret);
}
}
}
if(!foundany)
ha_messagex(rq, LOG_WARNING, "no clear password or ha1 present for user: %s", user);
finally:
if(res)
PQclear(res);
return ret;
}
static int validate_basic(ha_request_t* rq, const char* user, const char* password)
{
pgsql_context_t* ctx = (pgsql_context_t*)rq->context->ctx_data;
PGresult* res = NULL;
int ret = HA_FALSE;
int pw_column = -1;
int ha1_column = -1;
int i, foundany = 0;
ASSERT(rq && user && password);
ret = retrieve_user_rows(rq, ctx, user, &res);
if(ret != HA_OK)
RETURN(ret);
ASSERT(res);
pw_column = resolve_column(res, ctx->pw_column);
ha1_column = resolve_column(res, ctx->ha1_column);
if(pw_column == -1 && ha1_column == -1)
{
if(PQnfields(res) > 1)
ha_messagex(rq, LOG_WARNING, "query returned more than 1 column, using first as password");
pw_column = 0;
}
for(i = 0; i < PQntuples(res); i++)
{
if(pw_column != -1)
{
if(!PQgetisnull(res, i, pw_column))
{
foundany = 1;
ret = validate_password(rq, ctx, user, password, PQgetvalue(res, i, pw_column));
if(ret != HA_FALSE)
RETURN(ret);
}
}
if(ha1_column != -1)
{
if(!PQgetisnull(res, i, ha1_column))
{
foundany = 1;
ret = validate_ha1(rq, ctx, user, password, PQgetvalue(res, i, ha1_column));
if(ret != HA_FALSE)
RETURN(ret);
}
}
}
if(!foundany)
ha_messagex(rq, LOG_WARNING, "no password present for user: %s", user);
finally:
if(res)
PQclear(res);
return ret;
}
/* -------------------------------------------------------------------------------
* Handler Functions
*/
int pgsql_config(ha_context_t* context, const char* name, const char* value)
{
pgsql_context_t* ctx = (pgsql_context_t*)(context->ctx_data);
ASSERT(name && value && value[0]);
if(strcmp(name, "dbserver") == 0)
{
ctx->host = value;
return HA_OK;
}
if(strcmp(name, "dbport") == 0)
{
ctx->port = value;
return HA_OK;
}
if(strcmp(name, "dbuser") == 0)
{
ctx->user = value;
return HA_OK;
}
if(strcmp(name, "dbpassword") == 0)
{
ctx->password = value;
return HA_OK;
}
if(strcmp(name, "dbdatabase") == 0)
{
ctx->database = value;
return HA_OK;
}
if(strcmp(name, "dbquery") == 0)
{
ctx->query = value;
return HA_OK;
}
if(strcmp(name, "dbpwcolumn") == 0)
{
ctx->pw_column = value;
return HA_OK;
}
if(strcmp(name, "dbpwtype") == 0)
{
if(strcasecmp(value, "clear") == 0)
ctx->pw_type = DB_PW_CLEAR;
else if(strcasecmp(value, "crypt") == 0)
ctx->pw_type = DB_PW_CRYPT;
else if(strcasecmp(value, "md5") == 0)
ctx->pw_type = DB_PW_MD5;
else if(strcasecmp(value, "sha1") == 0)
ctx->pw_type = DB_PW_SHA1;
else
{
ha_messagex(NULL, LOG_ERR, "invalid value for '%s' (must be 'clear', 'crypt', 'md5' or 'sha1')", name);
return HA_FAILED;
}
return HA_OK;
}
if(strcmp(name, "dbha1column") == 0)
{
ctx->ha1_column = value;
return HA_OK;
}
else if(strcmp(name, "dbmax") == 0)
{
return ha_confint(name, value, 1, 256, &(ctx->pgsql_max));
}
else if(strcmp(name, "dbtimeout") == 0)
{
/* TODO: Implement database timeouts */
return ha_confint(name, value, 0, 86400, &(ctx->pgsql_timeout));
}
return HA_FALSE;
}
int pgsql_init(ha_context_t* context)
{
int r;
if((r = bd_init(context)) != HA_OK)
return r;
/* Context specific initialization */
if(context)
{
pgsql_context_t* ctx = (pgsql_context_t*)(context->ctx_data);
ASSERT(ctx);
/* Check for mandatory configuration */
if(!ctx->database || !ctx->query)
{
ha_messagex(NULL, LOG_ERR, "pgsql configuration incomplete. "
"Must have DBDatabase and DBQuery.");
return HA_FAILED;
}
ASSERT(!ctx->pool);
ASSERT(ctx->pgsql_max > 0);
/*
* Our connection pool. It's the size of our maximum
* amount of pending connections as that's the max
* we'd be able to use at a time anyway.
*/
ctx->pool = (PGconn**)malloc(sizeof(PGconn*) * ctx->pgsql_max);
if(!ctx->pool)
{
ha_messagex(NULL, LOG_CRIT, "out of memory");
return HA_CRITERROR;
}
memset(ctx->pool, 0, sizeof(PGconn*) * ctx->pgsql_max);
ha_messagex(NULL, LOG_INFO, "initialized pgsql handler");
}
return HA_OK;
}
void pgsql_destroy(ha_context_t* context)
{
if(context)
{
/* Note: We don't need to be thread safe here anymore */
pgsql_context_t* ctx = (pgsql_context_t*)(context->ctx_data);
int i;
ASSERT(ctx);
if(ctx->pool)
{
/* Close any connections we have open */
for(i = 0; i < ctx->pgsql_max; i++)
{
if(ctx->pool[i])
PQfinish(ctx->pool[i]);
}
/* And free the connection pool */
free(ctx->pool);
}
}
bd_destroy(context);
ha_messagex(NULL, LOG_INFO, "uninitialized pgsql handler");
}
/* -------------------------------------------------------------------------------
* Handler Definition
*/
ha_handler_t pgsql_handler =
{
"PGSQL", /* The type */
pgsql_init, /* Initialization function */
pgsql_destroy, /* Uninitialization routine */
pgsql_config, /* Config routine */
bd_process, /* Processing routine */
&pgsql_defaults, /* The context defaults */
sizeof(pgsql_context_t)
};
|