forked from NigelCunningham/pam-MySQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpam_mysql.c
4651 lines (3812 loc) · 114 KB
/
pam_mysql.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
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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* PAM module for MySQL
*
* Copyright (C) 1998-2005 Gunay Arslan and the contributors.
* Copyright (C) 2015-2016 Nigel Cunningham and contributors.
* All rights reserved.
*
* 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.
*
* Original Version written by: Gunay ARSLAN <[email protected]>
* This version by: James O'Kane <[email protected]>
* Modifications by Steve Brown, <[email protected]>
* B.J. Black, <[email protected]>
* Kyle Smith, <[email protected]>
* Patch integration by Moriyoshi Koizumi, <[email protected]>,
* based on the patches by the following contributors:
* Paul Bryan (check_acct_mgmt service support)
* Kev Green (Documentation, UNIX socket option)
* Kees Cook (Misc. clean-ups and more)
* Fredrik Rambris (RPM spec file)
* Peter E. Stokke (chauthtok service support)
* Sergey Matveychuk (OpenPAM support)
* Package unmaintained for some years; now taken care of by Nigel Cunningham
* https://github.com/NigelCunningham/pam-MySQL
*/
#define _GNU_SOURCE
#define PAM_SM_AUTH
#define PAM_SM_ACCOUNT
#define PAM_SM_SESSION
#define PAM_SM_PASSWORD
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
/* {{{ includes */
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYSLOG_H
#include <syslog.h>
#endif
#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <time.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_CRYPT_H
#include <crypt.h>
#endif
#ifndef HAVE_OPENSSL
#ifdef HAVE_MD5_H
#include <md5.h>
#endif
#if defined(HAVE_SASL_MD5_H) && (defined(HAVE_CYRUS_SASL_V1) || defined(HAVE_CYRUS_SASL_V2))
#define USE_SASL_MD5
#include <md5global.h>
#include <md5.h>
#endif
#endif
#ifdef HAVE_OPENSSL
#include <openssl/md5.h>
#include <openssl/sha.h>
#endif
#ifdef HAVE_MYSQL_H
#include <mysql.h>
#endif
/*
* here, we make definitions for the externally accessible functions
* in this file (these definitions are required for static modules
* but strongly encouraged generally) they are used to instruct the
* modules include file to define their prototypes.
*/
#define PAM_SM_AUTH
#define PAM_SM_ACCOUNT
#define PAM_SM_SESSION
#define PAM_SM_PASSWORD
#include <security/pam_appl.h>
#include <security/pam_modules.h>
/* }}} */
#ifndef PAM_EXTERN
#define PAM_EXTERN
#endif
#ifndef LOG_AUTHPRIV
#define LOG_AUTHPRIV LOG_AUTH
#endif
#ifdef LINUX_PAM_CONST_BUG
#define PAM_AUTHTOK_RECOVERY_ERR PAM_AUTHTOK_RECOVER_ERR
#endif
#ifndef HAVE_MAKE_SCRAMBLED_PASSWORD
#include "crypto.h"
#include "crypto-sha1.h"
// Implementation from commit 2db6b50c7b7c638104bd9639994f0574e8f4813c in Pure-ftp source.
void make_scrambled_password(char scrambled_password[42], const char password[255])
{
SHA1_CTX ctx;
unsigned char h0[20], h1[20];
SHA1Init(&ctx);
SHA1Update(&ctx, password, strlen(password));
SHA1Final(h0, &ctx);
SHA1Init(&ctx);
SHA1Update(&ctx, h0, sizeof h0);
# ifdef HAVE_EXPLICIT_BZERO
explicit_bzero(h0, strlen(password));
# else
volatile unsigned char *pnt_ = (volatile unsigned char *) h0;
size_t i = (size_t) 0U;
while (i < strlen(password)) {
pnt_[i++] = 0U;
}
# endif
SHA1Final(h1, &ctx);
*scrambled_password = '*';
hexify(scrambled_password + 1U, h1,
42, sizeof h1);
}
#endif
#define PAM_MODULE_NAME "pam_mysql"
#define PAM_MYSQL_LOG_PREFIX PAM_MODULE_NAME " - "
#define PLEASE_ENTER_PASSWORD "Password:"
#define PLEASE_ENTER_OLD_PASSWORD "(Current) Password:"
#define PLEASE_ENTER_NEW_PASSWORD "(New) Password:"
#define PLEASE_REENTER_NEW_PASSWORD "Retype (New) Password:"
/* {{{ consts */
enum _pam_mysql_err_t {
PAM_MYSQL_ERR_SUCCESS = 0,
PAM_MYSQL_ERR_UNKNOWN = -1,
PAM_MYSQL_ERR_NO_ENTRY = 1,
PAM_MYSQL_ERR_ALLOC = 2,
PAM_MYSQL_ERR_INVAL = 3,
PAM_MYSQL_ERR_BUSY = 4,
PAM_MYSQL_ERR_DB = 5,
PAM_MYSQL_ERR_MISMATCH = 6,
PAM_MYSQL_ERR_IO = 7,
PAM_MYSQL_ERR_SYNTAX = 8,
PAM_MYSQL_ERR_EOF = 9,
PAM_MYSQL_ERR_NOTIMPL = 10
};
enum _pam_mysql_config_token_t {
PAM_MYSQL_CONFIG_TOKEN_EQUAL = 0,
PAM_MYSQL_CONFIG_TOKEN_NEWLINE,
PAM_MYSQL_CONFIG_TOKEN_STRING,
PAM_MYSQL_CONFIG_TOKEN_SEMICOLON,
PAM_MYSQL_CONFIG_TOKEN_COMMENT,
PAM_MYSQL_CONFIG_TOKEN__LAST
};
#define PAM_MYSQL_USER_STAT_EXPIRED 0x0001
#define PAM_MYSQL_USER_STAT_AUTHTOK_EXPIRED 0x0002
#define PAM_MYSQL_USER_STAT_NULL_PASSWD 0x0004
#define PAM_MYSQL_CAP_CHAUTHTOK_SELF 0x0001
#define PAM_MYSQL_CAP_CHAUTHTOK_OTHERS 0x0002
/* }}} */
/* {{{ typedefs */
/* {{{ typedef struct pam_mysql_ctx_t */
typedef struct _pam_mysql_ctx_t {
MYSQL *mysql_hdl;
char *host;
char *where;
char *db;
char *user;
char *passwd;
char *table;
char *update_table;
char *usercolumn;
char *passwdcolumn;
char *statcolumn;
int crypt_type;
int use_323_passwd;
int md5;
int sqllog;
int verbose;
int use_first_pass;
int try_first_pass;
int disconnect_every_op;
char *logtable;
char *logmsgcolumn;
char *logpidcolumn;
char *logusercolumn;
char *loghostcolumn;
char *logrhostcolumn;
char *logtimecolumn;
char *config_file;
char *my_host_info;
} pam_mysql_ctx_t; /*Max length for most MySQL fields is 16 */
/* }}} */
/* {{{ typedef enum pam_mysql_err_t */
typedef enum _pam_mysql_err_t pam_mysql_err_t;
/* }}} */
/* {{{ typedef enum pam_mysql_config_token_t */
typedef enum _pam_mysql_config_token_t pam_mysql_config_token_t;
/* }}} */
/* {{{ typedef (func) pam_mysql_option_getter_t */
typedef int(*pam_mysql_option_getter_t)(void *val, const char **pretval, int *to_release);
/* }}} */
/* {{{ typedef (func) pam_mysql_option_setter_t */
typedef int(*pam_mysql_option_setter_t)(void *val, const char *newval_str);
/* }}} */
/* {{{ typedef struct pam_mysql_option_accessor_t */
typedef struct _pam_mysql_option_accessor_t {
pam_mysql_option_getter_t get_op;
pam_mysql_option_setter_t set_op;
} pam_mysql_option_accessor_t;
/* }}} */
/* {{{ typedef struct pam_mysql_option_t */
typedef struct _pam_mysql_option_t {
const char *name;
size_t name_len;
size_t offset;
pam_mysql_option_accessor_t *accessor;
} pam_mysql_option_t;
/* }}} */
/* {{{ typedef struct pam_mysql_str_t */
typedef struct _pam_mysql_str_t {
char *p;
size_t len;
size_t alloc_size;
int mangle;
} pam_mysql_str_t;
/* }}} */
/* {{{ typedef (func) pam_mysql_handle_entry_fn_t */
struct _pam_mysql_entry_handler_t;
typedef pam_mysql_err_t (*pam_mysql_handle_entry_fn_t)(
struct _pam_mysql_entry_handler_t *, int, const char *, size_t,
const char *, size_t);
/* }}} */
/* {{{ typedef struct pam_mysql_entry_handler_t */
typedef struct _pam_mysql_entry_handler_t {
pam_mysql_ctx_t *ctx;
pam_mysql_handle_entry_fn_t handle_entry_fn;
pam_mysql_option_t *options;
} pam_mysql_entry_handler_t;
/* }}} */
/* {{{ typedef struct pam_mysql_config_parser_t */
typedef struct _pam_mysql_config_parser_t {
pam_mysql_ctx_t *ctx;
pam_mysql_entry_handler_t *hdlr;
} pam_mysql_config_parser_t;
/* }}} */
/* {{{ typedef struct pam_mysql_stream_t */
typedef struct _pam_mysql_stream_t {
int fd;
unsigned char buf[2][2048];
unsigned char *buf_start;
unsigned char *buf_ptr;
unsigned char *buf_end;
unsigned char *pushback;
size_t buf_in_use;
int eof;
} pam_mysql_stream_t;
/* }}} */
/* {{{ typedef struct _pam_mysql_config_scanner_t */
typedef struct _pam_mysql_config_scanner_t {
pam_mysql_ctx_t *ctx;
pam_mysql_str_t image;
pam_mysql_config_token_t token;
pam_mysql_stream_t *stream;
int state;
} pam_mysql_config_scanner_t;
/* }}} */
/* }}} */
/* {{{ prototypes */
/* {{{ General PAM Prototypes */
PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh,
int flags, int argc, const char **argv);
PAM_EXTERN int pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc,
const char **argv);
PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc,
const char **argv);
PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc,
const char **argv);
PAM_EXTERN int pam_sm_open_session(pam_handle_t *pamh, int flags, int argc,
const char **argv);
PAM_EXTERN int pam_sm_close_session(pam_handle_t *pamh, int flags, int argc,
const char **argv);
/* }}} */
/* {{{ static prototypes */
static void pam_mysql_cleanup_hdlr(pam_handle_t *pamh, void * voiddata, int status);
/* {{{ pam_mysql methods */
static pam_mysql_err_t pam_mysql_retrieve_ctx(pam_mysql_ctx_t **pretval, pam_handle_t *pamh);
static pam_mysql_err_t pam_mysql_init_ctx(pam_mysql_ctx_t *);
static void pam_mysql_destroy_ctx(pam_mysql_ctx_t *);
static void pam_mysql_saltify(pam_mysql_ctx_t *, char *salt, const char *salter );
static pam_mysql_err_t pam_mysql_parse_args(pam_mysql_ctx_t *, int argc, const char **argv);
static pam_mysql_err_t pam_mysql_open_db(pam_mysql_ctx_t *);
static void pam_mysql_close_db(pam_mysql_ctx_t *);
static pam_mysql_err_t pam_mysql_check_passwd(pam_mysql_ctx_t *ctx,
const char *user, const char *passwd, int null_inhibited);
static pam_mysql_err_t pam_mysql_update_passwd(pam_mysql_ctx_t *,
const char *user, const char *new_passwd);
static pam_mysql_err_t pam_mysql_query_user_stat(pam_mysql_ctx_t *,
int *pretval, const char *user);
static pam_mysql_err_t pam_mysql_query_user_caps(pam_mysql_ctx_t *,
int *pretval, const char *user);
static pam_mysql_err_t pam_mysql_sql_log(pam_mysql_ctx_t *, const char *msg,
const char *user, const char *host);
static pam_mysql_err_t pam_mysql_get_host_info(pam_mysql_ctx_t *,
const char **pretval);
/* }}} */
static size_t strnncpy(char *dest, size_t dest_size, const char *src, size_t src_len);
static void *xcalloc(size_t nmemb, size_t size);
static char *xstrdup(const char *ptr);
static void xfree(void *ptr);
static void xfree_overwrite(char *ptr);
/* }}} */
/* }}} */
/* {{{ strnncpy */
static size_t strnncpy(char *dest, size_t dest_size, const char *src, size_t src_len)
{
size_t cpy_len;
dest_size--;
cpy_len = (dest_size < src_len ? dest_size: src_len);
memcpy(dest, src, cpy_len);
dest[cpy_len] = '\0';
return cpy_len;
}
/* }}} */
/* {{{ xcalloc */
static void *xcalloc(size_t nmemb, size_t size)
{
void *retval;
double v = ((double)size) * (int)(nmemb & (((size_t)-1) >> 1));
if (v != nmemb * size) {
return NULL;
}
retval = calloc(nmemb, size);
return retval;
}
/* }}} */
/* {{{ xrealloc */
static void *xrealloc(void *ptr, size_t nmemb, size_t size)
{
void *retval;
size_t total = nmemb * size;
if (((double)size) * (int)(nmemb & (((size_t)-1) >> 1)) != total) {
return NULL;
}
retval = realloc(ptr, total);
return retval;
}
/* }}} */
/* {{{ xstrdup */
static char *xstrdup(const char *ptr)
{
size_t len = strlen(ptr) + sizeof(char);
char *retval = xcalloc(sizeof(char), len);
if (retval == NULL) {
return NULL;
}
memcpy(retval, ptr, len);
return retval;
}
/* }}} */
/* {{{ xfree */
static void xfree(void *ptr)
{
if (ptr != NULL) {
free(ptr);
}
}
/* }}} */
/* {{{ xfree_overwrite */
static void xfree_overwrite(char *ptr)
{
if (ptr != NULL) {
char *p;
for (p = ptr; *p != '\0'; p++) {
*p = '\0';
}
free(ptr);
}
}
/* }}} */
/* {{{ memspn */
static void *memspn(void *buf, size_t buf_len, const unsigned char *delims,
size_t ndelims)
{
unsigned char *buf_end = ((unsigned char *)buf) + buf_len;
unsigned char *p;
switch (ndelims) {
case 0:
return buf_end;
case 1: {
unsigned char c = delims[0];
for (p = (unsigned char *)buf; p < buf_end; p++) {
if (*p != c) {
return (void *)p;
}
}
} break;
case 2: {
unsigned char c1 = delims[0], c2 = delims[1];
for (p = (unsigned char *)buf; p < buf_end; p++) {
if (*p != c1 && *p != c2) {
return (void *)p;
}
}
} break;
default: {
const unsigned char *delims_end = delims + ndelims;
unsigned char and_mask = ~0, or_mask = 0;
const unsigned char *q;
for (q = delims; q < delims_end; q++) {
and_mask &= *q;
or_mask |= *q;
}
for (p = (unsigned char *)buf; p < buf_end; p++) {
if ((*p & and_mask) == and_mask && (*p & or_mask) != 0) {
for (q = delims; *p != *q; q++) {
if (q >= delims_end) {
return (void *)p;
}
}
}
}
} break;
}
return NULL;
}
/* }}} */
/* {{{ memcspn */
static void *memcspn(void *buf, size_t buf_len, const unsigned char *delims,
size_t ndelims)
{
if (ndelims == 1) {
return memchr(buf, delims[0], buf_len);
} else {
unsigned char *buf_end = ((unsigned char *)buf) + buf_len;
const unsigned char *delims_end = delims + ndelims;
unsigned char *p;
for (p = (unsigned char *)buf; p < buf_end; p++) {
const unsigned char *q;
for (q = delims; q < delims_end; q++) {
if (*p == *q) {
return (void *)p;
}
}
}
return NULL;
}
}
/* }}} */
/* {{{ pam_mysql_md5_data
*
* AFAIK, only FreeBSD has MD5Data() defined in md5.h
* better MD5 support will appear in 0.5
*/
#ifdef HAVE_MD5DATA
#define HAVE_PAM_MYSQL_MD5_DATA
#define pam_mysql_md5_data MD5Data
#elif defined(HAVE_OPENSSL) || (defined(HAVE_SASL_MD5_H) && defined(USE_SASL_MD5)) || (!defined(HAVE_OPENSSL) && defined(HAVE_SOLARIS_MD5))
#if defined(USE_SASL_MD5)
static unsigned char *MD5(const unsigned char *d, unsigned int n,
unsigned char *md)
{
MD5_CTX ctx;
_sasl_MD5Init(&ctx);
_sasl_MD5Update(&ctx, (unsigned char *)d, n);
_sasl_MD5Final(md, &ctx);
return md;
}
#elif defined(USE_SOLARIS_MD5)
#define MD5(d, n, md) md5_calc(d, md, n)
#endif
#define HAVE_PAM_MYSQL_MD5_DATA
static char *pam_mysql_md5_data(const unsigned char *d, unsigned int sz, char *md)
{
size_t i, j;
unsigned char buf[16];
if (md == NULL) {
if ((md = xcalloc(32 + 1, sizeof(char))) == NULL) {
return NULL;
}
}
MD5(d, (unsigned long)sz, buf);
for (i = 0, j = 0; i < 16; i++, j += 2) {
md[j + 0] = "0123456789abcdef"[(int)(buf[i] >> 4)];
md[j + 1] = "0123456789abcdef"[(int)(buf[i] & 0x0f)];
}
md[j] = '\0';
return md;
}
#endif
/* }}} */
/* {{{ pam_mysql_sha1_data */
#if defined(HAVE_OPENSSL)
#define HAVE_PAM_MYSQL_SHA1_DATA
static char *pam_mysql_sha1_data(const unsigned char *d, unsigned int sz, char *md)
{
size_t i, j;
unsigned char buf[20];
if (md == NULL) {
if ((md = xcalloc(40 + 1, sizeof(char))) == NULL) {
return NULL;
}
}
SHA1(d, (unsigned long)sz, buf);
for (i = 0, j = 0; i < 20; i++, j += 2) {
md[j + 0] = "0123456789abcdef"[(int)(buf[i] >> 4)];
md[j + 1] = "0123456789abcdef"[(int)(buf[i] & 0x0f)];
}
md[j] = '\0';
return md;
}
static char *pam_mysql_sha512_data(const unsigned char *d, unsigned int sz, char *md)
{
size_t i, j;
unsigned char buf[64];
if (md == NULL) {
if ((md = xcalloc(128 + 1, sizeof(char))) == NULL) {
return NULL;
}
}
SHA512(d, (unsigned long)sz, buf);
for (i = 0, j = 0; i < 64; i++, j += 2) {
md[j + 0] = "0123456789abcdef"[(int)(buf[i] >> 4)];
md[j + 1] = "0123456789abcdef"[(int)(buf[i] & 0x0f)];
}
md[j] = '\0';
return md;
}
#endif
/* }}} */
#if defined(HAVE_PAM_MYSQL_SHA1_DATA) && defined(HAVE_PAM_MYSQL_MD5_DATA)
#define HAVE_PAM_MYSQL_DRUPAL7
/**
* The standard log2 number of iterations for password stretching. This should
* increase by 1 every Drupal version in order to counteract increases in the
* speed and power of computers available to crack the hashes.
*/
#define DRUPAL_HASH_COUNT 14
/**
* The minimum allowed log2 number of iterations for password stretching.
*/
#define DRUPAL_MIN_HASH_COUNT 7
/**
* The maximum allowed log2 number of iterations for password stretching.
*/
#define DRUPAL_MAX_HASH_COUNT 30
/**
* The expected (and maximum) number of characters in a hashed password.
*/
#define DRUPAL_HASH_LENGTH 55
/**
* Returns a string for mapping an int to the corresponding base 64 character.
*/
static char * _password_itoa64(void)
{
return "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
}
/**
* Parse the log2 iteration count from a stored hash or setting string.
*/
static int d7_password_get_count_log2(char *setting)
{
char *itoa64 = _password_itoa64();
int i;
for (i = 0; i < strlen(itoa64); i++) {
if (itoa64[i] == setting[3])
return i;
}
return -1;
}
static char * _password_base64_encode(unsigned char *input, int count, char *output) {
int i = 0, off = 0;
char *itoa64 = _password_itoa64();
unsigned long value;
do {
value = (unsigned long) input[i++];
output[off++] = itoa64[value & 0x3f];
if (i < count) {
value |= (((unsigned long) input[i]) << 8);
}
output[off++] = itoa64[(value >> 6) & 0x3f];
if (i++ >= count) {
break;
}
if (i < count) {
value |= (((unsigned long) input[i]) << 16);
}
output[off++] = itoa64[(value >> 12) & 0x3f];
if (i++ >= count) {
break;
}
output[off++] = itoa64[(value >> 18) & 0x3f];
} while (i < count);
output[off] = 0;
return output;
}
/* Strings may be binary and contain \0, so we can't use strlen */
static char *d7_hash(int use_md5, char *string1, int len1, char *string2, int len2)
{
int len = len1 + len2;
char *combined = xcalloc(len, sizeof(char)), *output = xcalloc(129, sizeof(char));
if (!combined) {
syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "hash: Failed to allocate memory for combined value.");
return NULL;
}
memcpy(combined, string1, len1);
memcpy(combined + len1, string2, len2);
if (use_md5)
MD5(combined, (unsigned long)len, output);
else
SHA512(combined, (unsigned long)len, output);
xfree(combined);
return output;
}
// The first 12 characters of an existing hash are its setting string.
static char * d7_password_crypt(int use_md5, char *password, char *setting) {
char salt[9], *old, *new, *final;
int expected, count, count_log2 = d7_password_get_count_log2(setting);
int len;
// Hashes may be imported from elsewhere, so we allow != DRUPAL_HASH_COUNT
if (count_log2 < DRUPAL_MIN_HASH_COUNT || count_log2 > DRUPAL_MAX_HASH_COUNT) {
syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "_password_crypt: count_log2 outside of range.");
return NULL;
}
strncpy(salt, &setting[4], 8);
salt[8] = '\0';
if (strlen(salt) != 8) {
syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "_password_crypt: Salt length is not 8.");
return NULL;
}
// Convert the base 2 logarithm into an integer.
count = 1 << count_log2;
old = d7_hash(use_md5, salt , 8, password, strlen(password));
do {
new = d7_hash(use_md5, old , use_md5 ? 16 : 64, password, strlen(password));
xfree(old);
if (!new)
return NULL;
old = new;
} while (--count);
len = use_md5 ? 16 : 64;
new = xcalloc(129, sizeof(char));
memcpy(new, setting, 12);
_password_base64_encode(old, len, &new[12]);
xfree(old);
// _password_base64_encode() of a 16 byte MD5 will always be 22 characters.
// _password_base64_encode() of a 64 byte sha512 will always be 86 characters.
expected = 12 + ((8 * len + 5) / 6);
if (strlen(new) != expected) {
syslog(LOG_AUTHPRIV | LOG_ERR, PAM_MYSQL_LOG_PREFIX "_password_crypt: Hash length not as expected.");
xfree(new);
return NULL;
}
final = xcalloc(DRUPAL_HASH_LENGTH + 1, sizeof(char));
strncpy(final, new, DRUPAL_HASH_LENGTH);
xfree(new);
return final;
}
static char *pam_mysql_drupal7_data(const unsigned char *pwd, unsigned int sz, char *md, char *db_pwd)
{
char *stored_hash = db_pwd, *pwd_ptr = (char *) pwd, *hashed;
int match = 0, offset = 0;
// Algorithm taken from user_check_password in includes/password.c in D7.0.
if (db_pwd[0] == 'U' && db_pwd[1] == '$') {
// This may be an updated password from user_update_7000(). Such hashes
// have 'U' added as the first character and need an extra md5().
stored_hash = &db_pwd[1];
offset++;
pwd_ptr = pam_mysql_md5_data(pwd, (unsigned long)sz, md);
} else
stored_hash = &db_pwd[0];
if (stored_hash[0] == '$' && stored_hash[2] == '$') {
switch (stored_hash[1]) {
case 'S':
match = 1;
hashed = d7_password_crypt(0, pwd_ptr, stored_hash);
break;
case 'H':
// phpBB3 uses "$H$" for the same thing as "$P$".
case 'P':
match = 1;
hashed = d7_password_crypt(1, pwd_ptr, stored_hash);
break;
}
}
if (!match || !hashed) {
md[0] = db_pwd[0] + 1;
return NULL;
}
memcpy(md, hashed, strlen(hashed));
xfree(hashed);
return md;
}
#endif
/* }}} */
/* {{{ option handlers */
/* {{{ pam_mysql_string_opt_getter */
static pam_mysql_err_t pam_mysql_string_opt_getter(void *val, const char **pretval, int *to_release)
{
*pretval = *(char **)val;
*to_release = 0;
return PAM_MYSQL_ERR_SUCCESS;
}
/* }}} */
/* {{{ pam_mysql_string_opt_setter */
static pam_mysql_err_t pam_mysql_string_opt_setter(void *val, const char *newval_str)
{
if (*(char **)val != NULL) {
xfree(*(char **)val);
}
if (NULL == (*(char **)val = xstrdup(newval_str))) {
syslog(LOG_AUTHPRIV | LOG_CRIT, PAM_MYSQL_LOG_PREFIX "allocation failure at " __FILE__ ":%d", __LINE__);
return PAM_MYSQL_ERR_ALLOC;
}
return PAM_MYSQL_ERR_SUCCESS;
}
/* }}} */
/* {{{ pam_mysql_boolean_opt_getter
*/
static pam_mysql_err_t pam_mysql_boolean_opt_getter(void *val, const char **pretval, int *to_release)
{
*pretval = (*(int *)val ? "true": "false");
*to_release = 0;
return PAM_MYSQL_ERR_SUCCESS;
}
/* }}} */
/* {{{ pam_mysql_boolean_opt_setter */
static pam_mysql_err_t pam_mysql_boolean_opt_setter(void *val, const char *newval_str)
{
*(int *)val = (strcmp(newval_str, "0") != 0 &&
strcasecmp(newval_str, "N") != 0 &&
strcasecmp(newval_str, "false") != 0 &&
strcasecmp(newval_str, "no") != 0);
return PAM_MYSQL_ERR_SUCCESS;
}
/* }}} */
/* {{{ pam_mysql_crypt_opt_getter */
static pam_mysql_err_t pam_mysql_crypt_opt_getter(void *val, const char **pretval, int *to_release)
{
switch (*(int *)val) {
case 0:
*pretval = "plain";
break;
case 1:
*pretval = "Y";
break;
case 2:
*pretval = "mysql";
break;
case 3:
*pretval = "md5";
break;
case 4:
*pretval = "sha1";
break;
case 5:
*pretval = "drupal7";
break;
case 6:
*pretval = "joomla15";
break;
default:
*pretval = NULL;
}
*to_release = 0;
return PAM_MYSQL_ERR_SUCCESS;
}
/* }}} */
/* {{{ pam_mysql_crypt_opt_setter */
static pam_mysql_err_t pam_mysql_crypt_opt_setter(void *val, const char *newval_str)
{
if (strcmp(newval_str, "0") == 0 || strcasecmp(newval_str, "plain") == 0) {
*(int *)val = 0;
return PAM_MYSQL_ERR_SUCCESS;
}
if (strcmp(newval_str, "1") == 0 || strcasecmp(newval_str, "Y") == 0) {
*(int *)val = 1;
return PAM_MYSQL_ERR_SUCCESS;
}
if (strcmp(newval_str, "2") == 0 || strcasecmp(newval_str, "mysql") == 0) {
*(int *)val = 2;
return PAM_MYSQL_ERR_SUCCESS;
}
if (strcmp(newval_str, "3") == 0 || strcasecmp(newval_str, "md5") == 0) {
*(int *)val = 3;
return PAM_MYSQL_ERR_SUCCESS;
}
if (strcmp(newval_str, "4") == 0 || strcasecmp(newval_str, "sha1") == 0) {
*(int *)val = 4;
return PAM_MYSQL_ERR_SUCCESS;
}
if (strcmp(newval_str, "5") == 0 || strcasecmp(newval_str, "drupal7") == 0) {
*(int *)val = 5;
return PAM_MYSQL_ERR_SUCCESS;
}