/
rlm_mschap.c
2101 lines (1804 loc) · 56 KB
/
rlm_mschap.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
/*
* This program is 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* $Id$
* @file rlm_mschap.c
* @brief Implemented mschap authentication.
*
* @copyright 2000,2001,2006 The FreeRADIUS server project
*/
/* MPPE support from Takahiro Wagatsuma <waga@sic.shibaura-it.ac.jp> */
RCSID("$Id$")
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/modules.h>
#include <freeradius-devel/rad_assert.h>
#include <freeradius-devel/md5.h>
#include <freeradius-devel/sha1.h>
#include <ctype.h>
#include "rlm_mschap.h"
#include "mschap.h"
#include "smbdes.h"
#include "auth_wbclient.h"
#ifdef HAVE_OPENSSL_CRYPTO_H
USES_APPLE_DEPRECATED_API /* OpenSSL API has been deprecated by Apple */
# include <openssl/rc4.h>
#endif
#ifdef __APPLE__
int od_mschap_auth(REQUEST *request, VALUE_PAIR *challenge, VALUE_PAIR * usernamepair);
#endif
/* Allowable account control bits */
#define ACB_DISABLED 0x00010000 //!< User account disabled.
#define ACB_HOMDIRREQ 0x00020000 //!< Home directory required.
#define ACB_PWNOTREQ 0x00040000 //!< User password not required.
#define ACB_TEMPDUP 0x00080000 //!< Temporary duplicate account.
#define ACB_NORMAL 0x00100000 //!< Normal user account.
#define ACB_MNS 0x00200000 //!< MNS logon user account.
#define ACB_DOMTRUST 0x00400000 //!< Interdomain trust account.
#define ACB_WSTRUST 0x00800000 //!< Workstation trust account.
#define ACB_SVRTRUST 0x01000000 //!< Server trust account.
#define ACB_PWNOEXP 0x02000000 //!< User password does not expire.
#define ACB_AUTOLOCK 0x04000000 //!< Account auto locked.
#define ACB_PW_EXPIRED 0x00020000 //!< Password Expired.
static int pdb_decode_acct_ctrl(char const *p)
{
int acct_ctrl = 0;
int done = 0;
/*
* Check if the account type bits have been encoded after the
* NT password (in the form [NDHTUWSLXI]).
*/
if (*p != '[') return 0;
for (p++; *p && !done; p++) {
switch (*p) {
case 'N': /* 'N'o password. */
acct_ctrl |= ACB_PWNOTREQ;
break;
case 'D': /* 'D'isabled. */
acct_ctrl |= ACB_DISABLED ;
break;
case 'H': /* 'H'omedir required. */
acct_ctrl |= ACB_HOMDIRREQ;
break;
case 'T': /* 'T'emp account. */
acct_ctrl |= ACB_TEMPDUP;
break;
case 'U': /* 'U'ser account (normal). */
acct_ctrl |= ACB_NORMAL;
break;
case 'M': /* 'M'NS logon user account. What is this? */
acct_ctrl |= ACB_MNS;
break;
case 'W': /* 'W'orkstation account. */
acct_ctrl |= ACB_WSTRUST;
break;
case 'S': /* 'S'erver account. */
acct_ctrl |= ACB_SVRTRUST;
break;
case 'L': /* 'L'ocked account. */
acct_ctrl |= ACB_AUTOLOCK;
break;
case 'X': /* No 'X'piry on password */
acct_ctrl |= ACB_PWNOEXP;
break;
case 'I': /* 'I'nterdomain trust account. */
acct_ctrl |= ACB_DOMTRUST;
break;
case 'e': /* 'e'xpired, the password has */
acct_ctrl |= ACB_PW_EXPIRED;
break;
case ' ': /* ignore spaces */
break;
case ':':
case '\n':
case '\0':
case ']':
default:
done = 1;
break;
}
}
return acct_ctrl;
}
/*
* Does dynamic translation of strings.
*
* Pulls NT-Response, LM-Response, or Challenge from MSCHAP
* attributes.
*/
static ssize_t mschap_xlat(char **out, size_t outlen,
void const *mod_inst, UNUSED void const *xlat_inst,
REQUEST *request, char const *fmt)
{
size_t i, data_len;
uint8_t const *data = NULL;
uint8_t buffer[32];
VALUE_PAIR *user_name;
VALUE_PAIR *chap_challenge, *response;
rlm_mschap_t const *inst = mod_inst;
response = NULL;
/*
* Challenge means MS-CHAPv1 challenge, or
* hash of MS-CHAPv2 challenge, and peer challenge.
*/
if (strncasecmp(fmt, "Challenge", 9) == 0) {
chap_challenge = fr_pair_find_by_num(request->packet->vps, VENDORPEC_MICROSOFT, PW_MSCHAP_CHALLENGE,
TAG_ANY);
if (!chap_challenge) {
REDEBUG("No MS-CHAP-Challenge in the request");
return -1;
}
/*
* MS-CHAP-Challenges are 8 octets,
* for MS-CHAPv1
*/
if (chap_challenge->vp_length == 8) {
RDEBUG2("mschap1: %02x", chap_challenge->vp_octets[0]);
data = chap_challenge->vp_octets;
data_len = 8;
/*
* MS-CHAP-Challenges are 16 octets,
* for MS-CHAPv2.
*/
} else if (chap_challenge->vp_length == 16) {
VALUE_PAIR *name_attr, *response_name;
char const *username_string;
response = fr_pair_find_by_num(request->packet->vps, VENDORPEC_MICROSOFT, PW_MSCHAP2_RESPONSE,
TAG_ANY);
if (!response) {
REDEBUG("MS-CHAP2-Response is required to calculate MS-CHAPv1 challenge");
return -1;
}
/*
* FIXME: Much of this is copied from
* below. We should put it into a
* separate function.
*/
/*
* Responses are 50 octets.
*/
if (response->vp_length < 50) {
REDEBUG("MS-CHAP-Response has the wrong format");
return -1;
}
user_name = fr_pair_find_by_num(request->packet->vps, 0, PW_USER_NAME, TAG_ANY);
if (!user_name) {
REDEBUG("User-Name is required to calculate MS-CHAPv1 Challenge");
return -1;
}
/*
* Check for MS-CHAP-User-Name and if found, use it
* to construct the MSCHAPv1 challenge. This is
* set by rlm_eap_mschap to the MS-CHAP Response
* packet Name field.
*
* We prefer this to the User-Name in the
* packet.
*/
response_name = fr_pair_find_by_num(request->packet->vps, 0, PW_MS_CHAP_USER_NAME, TAG_ANY);
if (response_name) {
name_attr = response_name;
} else {
name_attr = user_name;
}
/*
* with_ntdomain_hack moved here, too.
*/
if ((username_string = strchr(name_attr->vp_strvalue, '\\')) != NULL) {
if (inst->with_ntdomain_hack) {
username_string++;
} else {
RWDEBUG2("NT Domain delimiter found, should we have enabled with_ntdomain_hack?");
username_string = name_attr->vp_strvalue;
}
} else {
username_string = name_attr->vp_strvalue;
}
if (response_name &&
((user_name->vp_length != response_name->vp_length) ||
(strncasecmp(user_name->vp_strvalue, response_name->vp_strvalue,
user_name->vp_length) != 0))) {
RWDEBUG2("User-Name (%s) is not the same as MS-CHAP Name (%s) from EAP-MSCHAPv2",
user_name->vp_strvalue, response_name->vp_strvalue);
}
/*
* Get the MS-CHAPv1 challenge
* from the MS-CHAPv2 peer challenge,
* our challenge, and the user name.
*/
RDEBUG2("Creating challenge hash with username: %s", username_string);
mschap_challenge_hash(response->vp_octets + 2,
chap_challenge->vp_octets,
username_string, buffer);
data = buffer;
data_len = 8;
} else {
REDEBUG("Invalid MS-CHAP challenge length");
return -1;
}
/*
* Get the MS-CHAPv1 response, or the MS-CHAPv2
* response.
*/
} else if (strncasecmp(fmt, "NT-Response", 11) == 0) {
response = fr_pair_find_by_num(request->packet->vps, VENDORPEC_MICROSOFT, PW_MSCHAP_RESPONSE, TAG_ANY);
if (!response) response = fr_pair_find_by_num(request->packet->vps, VENDORPEC_MICROSOFT,
PW_MSCHAP2_RESPONSE, TAG_ANY);
if (!response) {
REDEBUG("No MS-CHAP-Response or MS-CHAP2-Response was found in the request");
return -1;
}
/*
* For MS-CHAPv1, the NT-Response exists only
* if the second octet says so.
*/
if ((response->da->vendor == VENDORPEC_MICROSOFT) &&
(response->da->attr == PW_MSCHAP_RESPONSE) &&
((response->vp_octets[1] & 0x01) == 0)) {
REDEBUG("No NT-Response in MS-CHAP-Response");
return -1;
}
/*
* MS-CHAP-Response and MS-CHAP2-Response have
* the NT-Response at the same offset, and are
* the same length.
*/
data = response->vp_octets + 26;
data_len = 24;
/*
* LM-Response is deprecated, and exists only
* in MS-CHAPv1, and not often there.
*/
} else if (strncasecmp(fmt, "LM-Response", 11) == 0) {
response = fr_pair_find_by_num(request->packet->vps, VENDORPEC_MICROSOFT, PW_MSCHAP_RESPONSE, TAG_ANY);
if (!response) {
REDEBUG("No MS-CHAP-Response was found in the request");
return -1;
}
/*
* For MS-CHAPv1, the LM-Response exists only
* if the second octet says so.
*/
if ((response->vp_octets[1] & 0x01) != 0) {
REDEBUG("No LM-Response in MS-CHAP-Response");
return -1;
}
data = response->vp_octets + 2;
data_len = 24;
/*
* Pull the NT-Domain out of the User-Name, if it exists.
*/
} else if (strncasecmp(fmt, "NT-Domain", 9) == 0) {
char *p, *q;
user_name = fr_pair_find_by_num(request->packet->vps, 0, PW_USER_NAME, TAG_ANY);
if (!user_name) {
REDEBUG("No User-Name was found in the request");
return -1;
}
/*
* First check to see if this is a host/ style User-Name
* (a la Kerberos host principal)
*/
if (strncmp(user_name->vp_strvalue, "host/", 5) == 0) {
/*
* If we're getting a User-Name formatted in this way,
* it's likely due to PEAP. The Windows Domain will be
* the first domain component following the hostname,
* or the machine name itself if only a hostname is supplied
*/
p = strchr(user_name->vp_strvalue, '.');
if (!p) {
RDEBUG2("setting NT-Domain to same as machine name");
strlcpy(*out, user_name->vp_strvalue + 5, outlen);
} else {
p++; /* skip the period */
q = strchr(p, '.');
/*
* use the same hack as below
* only if another period was found
*/
if (q) *q = '\0';
strlcpy(*out, p, outlen);
if (q) *q = '.';
}
} else {
p = strchr(user_name->vp_strvalue, '\\');
if (!p) {
REDEBUG("No NT-Domain was found in the User-Name");
return -1;
}
/*
* Hack. This is simpler than the alternatives.
*/
*p = '\0';
strlcpy(*out, user_name->vp_strvalue, outlen);
*p = '\\';
}
return strlen(*out);
/*
* Pull the User-Name out of the User-Name...
*/
} else if (strncasecmp(fmt, "User-Name", 9) == 0) {
char const *p, *q;
user_name = fr_pair_find_by_num(request->packet->vps, 0, PW_USER_NAME, TAG_ANY);
if (!user_name) {
REDEBUG("No User-Name was found in the request");
return -1;
}
/*
* First check to see if this is a host/ style User-Name
* (a la Kerberos host principal)
*/
if (strncmp(user_name->vp_strvalue, "host/", 5) == 0) {
p = user_name->vp_strvalue + 5;
/*
* If we're getting a User-Name formatted in this way,
* it's likely due to PEAP. When authenticating this against
* a Domain, Windows will expect the User-Name to be in the
* format of hostname$, the SAM version of the name, so we
* have to convert it to that here. We do so by stripping
* off the first 5 characters (host/), and copying everything
* from that point to the first period into a string and appending
* a $ to the end.
*/
q = strchr(p, '.');
/*
* use the same hack as above
* only if a period was found
*/
if (q) {
snprintf(*out, outlen, "%.*s$", (int) (q - p), p);
} else {
snprintf(*out, outlen, "%s$", p);
}
} else {
p = strchr(user_name->vp_strvalue, '\\');
if (p) {
p++; /* skip the backslash */
} else {
p = user_name->vp_strvalue; /* use the whole User-Name */
}
strlcpy(*out, p, outlen);
}
return strlen(*out);
/*
* Return the NT-Hash of the passed string
*/
} else if (strncasecmp(fmt, "NT-Hash ", 8) == 0) {
char const *p;
p = fmt + 8; /* 7 is the length of 'NT-Hash' */
if ((p == '\0') || (outlen <= 32))
return 0;
while (isspace(*p)) p++;
if (mschap_ntpwdhash(buffer, p) < 0) {
REDEBUG("Failed generating NT-Password");
*buffer = '\0';
return -1;
}
fr_bin2hex(*out, buffer, NT_DIGEST_LENGTH);
(*out)[32] = '\0';
RDEBUG("NT-Hash of \"known-good\" password: %s", *out);
return 32;
/*
* Return the LM-Hash of the passed string
*/
} else if (strncasecmp(fmt, "LM-Hash ", 8) == 0) {
char const *p;
p = fmt + 8; /* 7 is the length of 'LM-Hash' */
if ((p == '\0') || (outlen <= 32))
return 0;
while (isspace(*p)) p++;
smbdes_lmpwdhash(p, buffer);
fr_bin2hex(*out, buffer, LM_DIGEST_LENGTH);
(*out)[32] = '\0';
RDEBUG("LM-Hash of %s = %s", p, *out);
return 32;
} else {
REDEBUG("Unknown expansion string '%s'", fmt);
return -1;
}
if (outlen == 0) return 0; /* nowhere to go, don't do anything */
/*
* Didn't set anything: this is bad.
*/
if (!data) {
RWDEBUG2("Failed to do anything intelligent");
return 0;
}
/*
* Check the output length.
*/
if (outlen < ((data_len * 2) + 1)) {
data_len = (outlen - 1) / 2;
}
/*
*
*/
for (i = 0; i < data_len; i++) {
sprintf((*out) + (2 * i), "%02x", data[i]);
}
(*out)[data_len * 2] = '\0';
return data_len * 2;
}
#ifdef WITH_AUTH_WINBIND
/*
* Free connection pool winbind context
*/
static int _mod_conn_free(struct wbcContext **wb_ctx)
{
wbcCtxFree(*wb_ctx);
return 0;
}
/*
* Create connection pool winbind context
*/
static void *mod_conn_create(TALLOC_CTX *ctx, UNUSED void *instance, UNUSED struct timeval const *timeout)
{
struct wbcContext **wb_ctx;
wb_ctx = talloc_zero(ctx, struct wbcContext *);
*wb_ctx = wbcCtxCreate();
if (*wb_ctx == NULL) {
ERROR("failed to create winbind context");
talloc_free(wb_ctx);
return NULL;
}
talloc_set_destructor(wb_ctx, _mod_conn_free);
return *wb_ctx;
}
#endif
static const CONF_PARSER passchange_config[] = {
{ FR_CONF_OFFSET("ntlm_auth", PW_TYPE_STRING | PW_TYPE_XLAT, rlm_mschap_t, ntlm_cpw) },
{ FR_CONF_OFFSET("ntlm_auth_username", PW_TYPE_STRING | PW_TYPE_XLAT, rlm_mschap_t, ntlm_cpw_username) },
{ FR_CONF_OFFSET("ntlm_auth_domain", PW_TYPE_STRING | PW_TYPE_XLAT, rlm_mschap_t, ntlm_cpw_domain) },
{ FR_CONF_OFFSET("local_cpw", PW_TYPE_STRING | PW_TYPE_XLAT, rlm_mschap_t, local_cpw) },
CONF_PARSER_TERMINATOR
};
static const CONF_PARSER module_config[] = {
/*
* Cache the password by default.
*/
{ FR_CONF_OFFSET("use_mppe", PW_TYPE_BOOLEAN, rlm_mschap_t, use_mppe), .dflt = "yes" },
{ FR_CONF_OFFSET("require_encryption", PW_TYPE_BOOLEAN, rlm_mschap_t, require_encryption), .dflt = "no" },
{ FR_CONF_OFFSET("require_strong", PW_TYPE_BOOLEAN, rlm_mschap_t, require_strong), .dflt = "no" },
{ FR_CONF_OFFSET("with_ntdomain_hack", PW_TYPE_BOOLEAN, rlm_mschap_t, with_ntdomain_hack), .dflt = "yes" },
{ FR_CONF_OFFSET("ntlm_auth", PW_TYPE_STRING | PW_TYPE_XLAT, rlm_mschap_t, ntlm_auth) },
{ FR_CONF_OFFSET("ntlm_auth_timeout", PW_TYPE_INTEGER, rlm_mschap_t, ntlm_auth_timeout) },
{ FR_CONF_POINTER("passchange", PW_TYPE_SUBSECTION, NULL), .subcs = (void const *) passchange_config },
{ FR_CONF_OFFSET("allow_retry", PW_TYPE_BOOLEAN, rlm_mschap_t, allow_retry), .dflt = "yes" },
{ FR_CONF_OFFSET("retry_msg", PW_TYPE_STRING, rlm_mschap_t, retry_msg) },
{ FR_CONF_OFFSET("winbind_username", PW_TYPE_TMPL, rlm_mschap_t, wb_username) },
{ FR_CONF_OFFSET("winbind_domain", PW_TYPE_TMPL, rlm_mschap_t, wb_domain) },
#ifdef __APPLE__
{ FR_CONF_OFFSET("use_open_directory", PW_TYPE_BOOLEAN, rlm_mschap_t, open_directory), .dflt = "yes" },
#endif
CONF_PARSER_TERMINATOR
};
static int mod_bootstrap(CONF_SECTION *conf, void *instance)
{
char const *name;
rlm_mschap_t *inst = instance;
/*
* Create the dynamic translation.
*/
name = cf_section_name2(conf);
if (!name) name = cf_section_name1(conf);
inst->xlat_name = name;
xlat_register(inst, inst->xlat_name, mschap_xlat, NULL, NULL, 0, XLAT_DEFAULT_BUF_LEN);
return 0;
}
/*
* Create instance for our module. Allocate space for
* instance structure and read configuration parameters
*/
static int mod_instantiate(CONF_SECTION *conf, void *instance)
{
rlm_mschap_t *inst = instance;
/*
* For backwards compatibility
*/
if (!fr_dict_enum_by_name(NULL, fr_dict_attr_by_num(NULL, 0, PW_AUTH_TYPE), inst->xlat_name)) {
inst->auth_type = "MS-CHAP";
} else {
inst->auth_type = inst->xlat_name;
}
/*
* Set auth method
*/
inst->method = AUTH_INTERNAL;
if (inst->wb_username) {
#ifdef WITH_AUTH_WINBIND
inst->method = AUTH_WBCLIENT;
inst->wb_pool = module_connection_pool_init(conf, inst, mod_conn_create, NULL, NULL, NULL, NULL);
if (!inst->wb_pool) {
cf_log_err_cs(conf, "Unable to initialise winbind connection pool");
return -1;
}
#else
cf_log_err_cs(conf, "'winbind' auth not enabled at compiled time");
return -1;
#endif
}
/* preserve existing behaviour: this option overrides all */
if (inst->ntlm_auth) {
inst->method = AUTH_NTLMAUTH_EXEC;
}
switch (inst->method) {
case AUTH_INTERNAL:
DEBUG("%s: using internal authentication", inst->xlat_name);
break;
case AUTH_NTLMAUTH_EXEC:
DEBUG("%s : authenticating by calling 'ntlm_auth'", inst->xlat_name);
break;
#ifdef WITH_AUTH_WINBIND
case AUTH_WBCLIENT:
DEBUG("%s : authenticating directly to winbind", inst->xlat_name);
break;
#endif
}
/*
* Check ntlm_auth_timeout is sane
*/
if (!inst->ntlm_auth_timeout) {
inst->ntlm_auth_timeout = EXEC_TIMEOUT;
}
if (inst->ntlm_auth_timeout < 1) {
cf_log_err_cs(conf, "ntml_auth_timeout '%d' is too small (minimum: 1)",
inst->ntlm_auth_timeout);
return -1;
}
if (inst->ntlm_auth_timeout > 10) {
cf_log_err_cs(conf, "ntlm_auth_timeout '%d' is too large (maximum: 10)",
inst->ntlm_auth_timeout);
return -1;
}
return 0;
}
/*
* Tidy up instance
*/
static int mod_detach(UNUSED void *instance)
{
#ifdef WITH_AUTH_WINBIND
rlm_mschap_t *inst = instance;
fr_connection_pool_free(inst->wb_pool);
#endif
return 0;
}
/*
* add_reply() adds either MS-CHAP2-Success or MS-CHAP-Error
* attribute to reply packet
*/
void mschap_add_reply(REQUEST *request, uint8_t ident,
char const *name, char const *value, size_t len)
{
VALUE_PAIR *vp;
vp = pair_make_reply(name, NULL, T_OP_EQ);
if (!vp) {
REDEBUG("Failed to create attribute %s: %s", name, fr_strerror());
return;
}
if (vp->da->type == PW_TYPE_STRING) {
char *p;
p = talloc_array(vp, char, len + 1 + 1); /* Account for the ident byte */
p[len + 1] = '\0'; /* Always \0 terminate */
p[0] = ident;
memcpy(p + 1, value, len);
fr_pair_value_strsteal(vp, p);
} else {
uint8_t *p;
p = talloc_array(vp, uint8_t, len + 1); /* Account for the ident byte */
p[0] = ident;
memcpy(p + 1, value, len);
fr_pair_value_memsteal(vp, p);
}
}
/*
* Add MPPE attributes to the reply.
*/
static void mppe_add_reply(REQUEST *request, char const* name, uint8_t const * value, size_t len)
{
VALUE_PAIR *vp;
vp = pair_make_reply(name, NULL, T_OP_EQ);
if (!vp) {
REDEBUG("mppe_add_reply failed to create attribute %s: %s", name, fr_strerror());
return;
}
fr_pair_value_memcpy(vp, value, len);
}
static int write_all(int fd, char const *buf, int len) {
int rv,done=0;
while (done < len) {
rv = write(fd, buf+done, len-done);
if (rv <= 0)
break;
done += rv;
}
return done;
}
/*
* Perform an MS-CHAP2 password change
*/
static int CC_HINT(nonnull (1, 2, 4, 5)) do_mschap_cpw(rlm_mschap_t *inst,
REQUEST *request,
#ifdef HAVE_OPENSSL_CRYPTO_H
VALUE_PAIR *nt_password,
#else
UNUSED VALUE_PAIR *nt_password,
#endif
uint8_t *new_nt_password,
uint8_t *old_nt_hash,
MSCHAP_AUTH_METHOD method)
{
if (inst->ntlm_cpw && method != AUTH_INTERNAL) {
/*
* we're going to run ntlm_auth in helper-mode
* we're expecting to use the ntlm-change-password-1 protocol
* which needs the following on stdin:
*
* username: %{mschap:User-Name}
* nt-domain: %{mschap:NT-Domain}
* new-nt-password-blob: bin2hex(new_nt_password) - 1032 bytes encoded
* old-nt-hash-blob: bin2hex(old_nt_hash) - 32 bytes encoded
* new-lm-password-blob: 00000...0000 - 1032 bytes null
* old-lm-hash-blob: 000....000 - 32 bytes null
* .\n
*
* ...and it should then print out
*
* Password-Change: Yes
*
* or
*
* Password-Change: No
* Password-Change-Error: blah
*/
int to_child=-1;
int from_child=-1;
pid_t pid, child_pid;
int status, len;
char buf[2048];
char *pmsg;
char const *emsg;
RDEBUG("Doing MS-CHAPv2 password change via ntlm_auth helper");
/*
* Start up ntlm_auth with a pipe on stdin and stdout
*/
pid = radius_start_program(inst->ntlm_cpw, request, true, &to_child, &from_child, NULL, false);
if (pid < 0) {
REDEBUG("could not exec ntlm_auth cpw command");
return -1;
}
/*
* write the stuff to the client
*/
if (inst->ntlm_cpw_username) {
len = radius_xlat(buf, sizeof(buf) - 2, request, inst->ntlm_cpw_username, NULL, NULL);
if (len < 0) {
goto ntlm_auth_err;
}
buf[len++] = '\n';
buf[len] = '\0';
if (write_all(to_child, buf, len) != len) {
REDEBUG("Failed to write username to child");
goto ntlm_auth_err;
}
} else {
RWDEBUG2("No ntlm_auth username set, passchange will definitely fail!");
}
if (inst->ntlm_cpw_domain) {
len = radius_xlat(buf, sizeof(buf) - 2, request, inst->ntlm_cpw_domain, NULL, NULL);
if (len < 0) {
goto ntlm_auth_err;
}
buf[len++] = '\n';
buf[len] = '\0';
if (write_all(to_child, buf, len) != len) {
REDEBUG("Failed to write domain to child");
goto ntlm_auth_err;
}
} else {
RWDEBUG2("No ntlm_auth domain set, username must be full-username to work");
}
/* now the password blobs */
len = sprintf(buf, "new-nt-password-blob: ");
fr_bin2hex(buf+len, new_nt_password, 516);
buf[len+1032] = '\n';
buf[len+1033] = '\0';
len = strlen(buf);
if (write_all(to_child, buf, len) != len) {
RDEBUG2("failed to write new password blob to child");
goto ntlm_auth_err;
}
len = sprintf(buf, "old-nt-hash-blob: ");
fr_bin2hex(buf+len, old_nt_hash, NT_DIGEST_LENGTH);
buf[len+32] = '\n';
buf[len+33] = '\0';
len = strlen(buf);
if (write_all(to_child, buf, len) != len) {
REDEBUG("Failed to write old hash blob to child");
goto ntlm_auth_err;
}
/*
* In current samba versions, failure to supply empty LM password/hash
* blobs causes the change to fail.
*/
len = sprintf(buf, "new-lm-password-blob: %01032i\n", 0);
if (write_all(to_child, buf, len) != len) {
REDEBUG("Failed to write dummy LM password to child");
goto ntlm_auth_err;
}
len = sprintf(buf, "old-lm-hash-blob: %032i\n", 0);
if (write_all(to_child, buf, len) != len) {
REDEBUG("Failed to write dummy LM hash to child");
goto ntlm_auth_err;
}
if (write_all(to_child, ".\n", 2) != 2) {
REDEBUG("Failed to send finish to child");
goto ntlm_auth_err;
}
close(to_child);
to_child = -1;
/*
* Read from the child
*/
len = radius_readfrom_program(from_child, pid, 10, buf, sizeof(buf));
if (len < 0) {
/* radius_readfrom_program will have closed from_child for us */
REDEBUG("Failure reading from child");
return -1;
}
close(from_child);
from_child = -1;
buf[len] = 0;
RDEBUG2("ntlm_auth said: %s", buf);
child_pid = rad_waitpid(pid, &status);
if (child_pid == 0) {
REDEBUG("Timeout waiting for child");
return -1;
}
if (child_pid != pid) {
REDEBUG("Abnormal exit status: %s", fr_syserror(errno));
return -1;
}
if (strstr(buf, "Password-Change: Yes")) {
RDEBUG2("ntlm_auth password change succeeded");
return 0;
}
pmsg = strstr(buf, "Password-Change-Error: ");
if (pmsg) {
emsg = strsep(&pmsg, "\n");
} else {
emsg = "could not find error";
}
REDEBUG("ntlm auth password change failed: %s", emsg);
ntlm_auth_err:
/* safe because these either need closing or are == -1 */
close(to_child);
close(from_child);
return -1;
} else if (inst->local_cpw) {
#ifdef HAVE_OPENSSL_CRYPTO_H
/*
* Decrypt the new password blob, add it as a temporary request
* variable, xlat the local_cpw string, then remove it
*
* this allows is to write e..g
*
* %{sql:insert into ...}
*
* ...or...
*
* %{exec:/path/to %{mschap:User-Name} %{MS-CHAP-New-Password}}"
*
*/
VALUE_PAIR *new_pass, *new_hash;
uint8_t *p, *q;
char *x;
size_t i;
size_t passlen;
ssize_t result_len;
char result[253];
uint8_t nt_pass_decrypted[516], old_nt_hash_expected[NT_DIGEST_LENGTH];
RC4_KEY key;
if (!nt_password) {
RDEBUG("Local MS-CHAPv2 password change requires NT-Password attribute");
return -1;
} else {
RDEBUG("Doing MS-CHAPv2 password change locally");
}
/*
* Decrypt the blob
*/
RC4_set_key(&key, nt_password->vp_length, nt_password->vp_octets);
RC4(&key, 516, new_nt_password, nt_pass_decrypted);
/*
* pwblock is
* 512-N bytes random pad
* N bytes password as utf-16-le
* 4 bytes - N as big-endian int
*/
passlen = nt_pass_decrypted[512];
passlen += nt_pass_decrypted[513] << 8;
if ((nt_pass_decrypted[514] != 0) ||
(nt_pass_decrypted[515] != 0)) {
REDEBUG("Decrypted new password blob claims length > 65536, "
"probably an invalid NT-Password");
return -1;
}
/*
* Sanity check - passlen positive and <= 512 if not, crypto has probably gone wrong
*/
if (passlen > 512) {
REDEBUG("Decrypted new password blob claims length %zu > 512, "
"probably an invalid NT-Password", passlen);
return -1;
}
p = nt_pass_decrypted + 512 - passlen;
/*
* The new NT hash - this should be preferred over the
* cleartext password as it avoids unicode hassles.
*/
new_hash = pair_make_request("MS-CHAP-New-NT-Password", NULL, T_OP_EQ);
q = talloc_array(new_hash, uint8_t, NT_DIGEST_LENGTH);
fr_pair_value_memsteal(new_hash, q);
fr_md4_calc(q, p, passlen);
/*
* Check that nt_password encrypted with new_hash
* matches the old_hash value from the client.
*/
smbhash(old_nt_hash_expected, nt_password->vp_octets, q);
smbhash(old_nt_hash_expected+8, nt_password->vp_octets+8, q + 7);
if (memcmp(old_nt_hash_expected, old_nt_hash, NT_DIGEST_LENGTH)!=0) {