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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
|
#include "usuals.h"
#include "httpauthd.h"
#include "md5.h"
#include "sha1.h"
#include "bd.h"
#include <sys/time.h>
/* Mysql library */
#include <mysql.h>
#include <errmsg.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 mysql_context
{
/* Base Handler ------------------------------------------------------ */
bd_context_t bd;
/* Readonly Settings ------------------------------------------------- */
const char* host; /* The connection host or path */
unsigned int 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 mysql_max; /* Number of open connections allowed */
int mysql_timeout; /* Maximum amount of time to dedicate to a query */
int use_unix_socket;
/* Require Locking --------------------------------------------------- */
MYSQL** pool; /* Pool of available connections */
int pool_mark; /* Amount of connections allocated */
}
mysql_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_mysql(const ha_request_t* rq, ha_buffer_t* buf, const char* value);
/* The defaults for the context */
static const mysql_context_t mysql_defaults =
{
BD_CALLBACKS(validate_digest,
validate_basic, escape_mysql),
NULL, /* host */
0, /* port */
NULL, /* user */
NULL, /* password */
NULL, /* database */
NULL, /* query */
NULL, /* pw_attr */
DB_PW_CLEAR, /* pw_type */
NULL, /* ha1_attr */
10, /* mysql_max */
30, /* mysql_timeout */
0, /* use_unix_socket */
NULL, /* pool */
0 /* pool_mark */
};
/* mysql_real_connect is not always thread-safe :( */
static pthread_mutex_t g_mysql_mutex;
static pthread_mutexattr_t g_mysql_mutexattr;
/* -------------------------------------------------------------------------------
* Internal Functions
*/
static void escape_mysql(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)
{
mysql_escape_string(t, value, len);
ha_bufcpy(buf, t);
free(t);
}
}
static int dec_mysql_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;
}
}
/* TODO: Does mysql have raw binary encoding? */
/* 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, mysql_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_mysql_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, mysql_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_mysql_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_mysql_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 MYSQL* get_mysql_connection(const ha_request_t* rq, mysql_context_t* ctx)
{
MYSQL* my;
MYSQL* r;
int i, create = 1;
ASSERT(ctx);
/*
* Note that below there maybe a race condition between the two locks
* but this will only allow a few extra connections to open at best
* and as such really isn't a big issue.
*/
ha_lock(&g_mysql_mutex);
for(i = 0; i < ctx->mysql_max; i++)
{
/* An open connection in the pool */
if(ctx->pool[i])
{
ha_messagex(rq, LOG_DEBUG, "using cached mysql connection");
my = ctx->pool[i];
ctx->pool[i] = NULL;
}
}
if(my == NULL && ctx->pool_mark >= ctx->mysql_max)
{
ha_messagex(rq, LOG_ERR, "too many open mysql connections");
create = 0;
}
ha_unlock(&g_mysql_mutex);
if(my != NULL || create == 0)
return my;
my = mysql_init(NULL);
if(!my)
{
ha_messagex(rq, LOG_ERR, "out of memory");
return NULL;
}
mysql_options(my, MYSQL_OPT_CONNECT_TIMEOUT, (char*)&(ctx->mysql_timeout));
/* mysql_options(my, MYSQL_OPT_READ_TIMEOUT, (char*)&(ctx->mysql_timeout));
mysql_options(my, MYSQL_OPT_WRITE_TIMEOUT, (char*)&(ctx->mysql_timeout)); */
/* Apparently mysql_real_connect is not thread safe :( */
ha_lock(&g_mysql_mutex);
r = mysql_real_connect(my, ctx->use_unix_socket ? NULL : ctx->host,
ctx->user, ctx->password, ctx->database, ctx->port,
ctx->use_unix_socket ? ctx->host : NULL, 0);
if(!r)
{
ha_messagex(rq, LOG_ERR, "error opening mysql connection: %s", mysql_error(my));
mysql_close(my);
my = NULL;
}
else
{
ctx->pool_mark++;
ha_messagex(rq, LOG_DEBUG, "opened new mysql connection (total %d)", ctx->pool_mark);
}
ha_unlock(&g_mysql_mutex);
return my;
}
static void discard_mysql_connection(const ha_request_t* rq, mysql_context_t* ctx, MYSQL* my)
{
mysql_close(my);
ha_lock(&g_mysql_mutex);
ctx->pool_mark--;
ha_messagex(rq, LOG_DEBUG, "discarding mysql connection (total %d)", ctx->pool_mark);
ha_unlock(&g_mysql_mutex);
}
static void save_mysql_connection(const ha_request_t* rq, mysql_context_t* ctx, MYSQL* my)
{
int i, e;
ASSERT(ctx);
if(!my)
return;
switch(mysql_errno(my))
{
case CR_SOCKET_CREATE_ERROR:
case CR_CONNECTION_ERROR:
case CR_CONN_HOST_ERROR:
case CR_IPSOCK_ERROR:
case CR_UNKNOWN_HOST:
case CR_SERVER_GONE_ERROR:
case CR_VERSION_ERROR:
case CR_WRONG_HOST_INFO:
case CR_LOCALHOST_CONNECTION:
case CR_TCP_CONNECTION:
case CR_SERVER_HANDSHAKE_ERR:
case CR_SERVER_LOST:
case CR_COMMANDS_OUT_OF_SYNC:
break;
/* Make sure it's worth saving */
default:
ha_lock(&g_mysql_mutex);
for(i = 0; i < ctx->mysql_max; i++)
{
/* An open connection in the pool */
if(!ctx->pool[i])
{
ha_messagex(rq, LOG_DEBUG, "caching mysql connection for later use");
ctx->pool[i] = my;
my = NULL;
break;
}
}
ha_unlock(&g_mysql_mutex);
break;
};
if(my != NULL)
discard_mysql_connection(rq, ctx, my);
}
static int resolve_column(MYSQL_RES* res, const char* column)
{
int i, fields;
if(column)
{
fields = mysql_num_fields(res);
for(i = 0; i < fields; i++)
{
if(strcasecmp(column, mysql_fetch_field_direct(res, i)->name) == 0)
return i;
}
}
return -1;
}
static int retrieve_user_rows(ha_request_t* rq, mysql_context_t* ctx,
const char* user, MYSQL_RES** results)
{
MYSQL* my = NULL;
MYSQL_RES* res = NULL;
const char* query;
int ret = HA_OK;
ASSERT(rq && ctx && user && results);
*results = NULL;
my = get_mysql_connection(rq, ctx);
if(!my)
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);
if(mysql_query(my, query) != 0)
{
ha_messagex(rq, LOG_ERR, "error querying database: %s", mysql_error(my));
RETURN(HA_FAILED);
}
res = mysql_store_result(my);
if(!res)
{
if(mysql_field_count(my) == 0)
ha_messagex(rq, LOG_ERR, "mysql query didn't return results: %s", query);
else
ha_messagex(rq, LOG_ERR, "error querying database: %s", mysql_error(my));
RETURN(HA_FAILED);
}
if(mysql_num_rows(res) == 0)
{
ha_messagex(rq, LOG_WARNING, "login failed. couldn't find user: %s", user);
RETURN(HA_FALSE);
}
ha_messagex(rq, LOG_DEBUG, "received %d result rows", mysql_num_rows(res));
*results = res;
res = NULL;
finally:
if(res != NULL)
mysql_free_result(res);
/* TODO: Look into what happens if we free a mysql connection
* before processing results: */
if(my != NULL)
save_mysql_connection(rq, ctx, my);
return ret;
}
static int validate_digest(ha_request_t* rq, const char* user, digest_context_t* dg)
{
mysql_context_t* ctx = (mysql_context_t*)rq->context->ctx_data;
MYSQL_RES* res = NULL;
MYSQL_ROW row;
int ret = HA_FALSE;
int pw_column = -1;
int ha1_column = -1;
int r, i, foundany = 0;
const char* v;
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(mysql_num_fields(res) > 1)
ha_messagex(rq, LOG_WARNING, "query returned more than 1 column, using first as password");
pw_column = 0;
}
while((row = mysql_fetch_row(res)) != NULL)
{
if(pw_column != -1 && ctx->pw_type == DB_PW_CLEAR)
{
v = *(row + pw_column);
if(v != NULL)
{
foundany = 1;
digest_makeha1(dg->ha1, user, rq->context->realm, v);
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)
{
v = *(row + ha1_column);
if(v != NULL)
{
ret = dec_mysql_binary(rq, v, 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)
mysql_free_result(res);
return ret;
}
static int validate_basic(ha_request_t* rq, const char* user, const char* password)
{
mysql_context_t* ctx = (mysql_context_t*)rq->context->ctx_data;
MYSQL_RES* res = NULL;
MYSQL_ROW row;
int ret = HA_FALSE;
int pw_column = -1;
int ha1_column = -1;
int i, foundany = 0;
const char* v;
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(mysql_num_fields(res) > 1)
ha_messagex(rq, LOG_WARNING, "query returned more than 1 column, using first as password");
pw_column = 0;
}
while((row = mysql_fetch_row(res)) != NULL)
{
if(pw_column != -1)
{
v = *(row + pw_column);
if(v != NULL)
{
foundany = 1;
ret = validate_password(rq, ctx, user, password, v);
if(ret != HA_FALSE)
RETURN(ret);
}
}
if(ha1_column != -1)
{
v = *(row + ha1_column);
if(v != NULL)
{
foundany = 1;
ret = validate_ha1(rq, ctx, user, password, v);
if(ret != HA_FALSE)
RETURN(ret);
}
}
}
if(!foundany)
ha_messagex(rq, LOG_WARNING, "no password present for user: %s", user);
finally:
if(res)
mysql_free_result(res);
return ret;
}
/* -------------------------------------------------------------------------------
* Handler Functions
*/
int mysql_config(ha_context_t* context, const char* name, const char* value)
{
mysql_context_t* ctx = (mysql_context_t*)(context->ctx_data);
ASSERT(name && value && value[0]);
if(strcmp(name, "dbserver") == 0)
{
ctx->use_unix_socket = (value[0] == '/');
ctx->host = value;
return HA_OK;
}
if(strcmp(name, "dbport") == 0)
{
return ha_confint(name, value, 0, 65535, &(ctx->port));
}
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->mysql_max));
}
else if(strcmp(name, "dbtimeout") == 0)
{
return ha_confint(name, value, 0, 86400, &(ctx->mysql_timeout));
}
return HA_FALSE;
}
int mysql_initialize(ha_context_t* context)
{
int r;
if((r = bd_init(context)) != HA_OK)
return r;
/* Context specific initialization */
if(context)
{
mysql_context_t* ctx = (mysql_context_t*)(context->ctx_data);
ASSERT(ctx);
/* Check for mandatory configuration */
if(!ctx->database || !ctx->query)
{
ha_messagex(NULL, LOG_ERR, "mysql configuration incomplete. "
"Must have DBDatabase and DBQuery.");
return HA_FAILED;
}
ASSERT(!ctx->pool);
ASSERT(ctx->mysql_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 = (MYSQL**)malloc(sizeof(MYSQL*) * ctx->mysql_max);
if(!ctx->pool)
{
ha_messagex(NULL, LOG_CRIT, "out of memory");
return HA_CRITERROR;
}
memset(ctx->pool, 0, sizeof(MYSQL*) * ctx->mysql_max);
ha_messagex(NULL, LOG_INFO, "initialized mysql handler");
}
/* Global Initialization */
else
{
/* Create the smblib mutex */
if(pthread_mutexattr_init(&g_mysql_mutexattr) != 0 ||
pthread_mutexattr_settype(&g_mysql_mutexattr, HA_MUTEX_TYPE) ||
pthread_mutex_init(&g_mysql_mutex, &g_mysql_mutexattr) != 0)
{
ha_messagex(NULL, LOG_CRIT, "threading problem. can't create mutex");
return HA_CRITERROR;
}
}
return HA_OK;
}
void mysql_destroy(ha_context_t* context)
{
if(context)
{
/* Note: We don't need to be thread safe here anymore */
mysql_context_t* ctx = (mysql_context_t*)(context->ctx_data);
int i;
ASSERT(ctx);
if(ctx->pool)
{
/* Close any connections we have open */
for(i = 0; i < ctx->mysql_max; i++)
{
if(ctx->pool[i])
mysql_close(ctx->pool[i]);
}
/* And free the connection pool */
free(ctx->pool);
}
}
else
{
/* Close the mutex */
pthread_mutex_destroy(&g_mysql_mutex);
pthread_mutexattr_destroy(&g_mysql_mutexattr);
}
bd_destroy(context);
ha_messagex(NULL, LOG_INFO, "uninitialized mysql handler");
}
/* -------------------------------------------------------------------------------
* Handler Definition
*/
ha_handler_t mysql_handler =
{
"MYSQL", /* The type */
mysql_initialize, /* Initialization function */
mysql_destroy, /* Uninitialization routine */
mysql_config, /* Config routine */
bd_process, /* Processing routine */
&mysql_defaults, /* The context defaults */
sizeof(mysql_context_t)
};
|