/
pam_radius_auth.c
1770 lines (1460 loc) · 49 KB
/
pam_radius_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
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
/*
* Authenticate a user via a RADIUS session
*
* 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
*/
/**
* $Id: ee1d54767afa6c3801bb201f8a907dedb376cb11 $
*
* @file pam_radius_auth.c
* @brief Authenticate a user via a RADIUS session
*
* @copyright 1996 Cristian Gafton <gafton@redhat.com>
* @copyright 1998 CRYPTOCard Inc
* @copyright 2021 Network RADIUS SAS (legal@networkradius.com)
*/
#define PAM_SM_AUTH
#define PAM_SM_PASSWORD
#define PAM_SM_SESSION
#include "pam_radius_auth.h"
#define DPRINT if (debug) _pam_log
/* internal data */
static CONST char pam_module_name[] = "pam_radius_auth";
/* module version */
static CONST char pam_module_version[] = PAM_RADIUS_VERSION_STRING
#ifndef NDEBUG
" DEVELOPER BUILD - "
#endif
#ifdef PAM_RADIUS_VERSION_COMMIT
" (git #" PAM_RADIUS_VERSION_COMMIT ")"
#endif
", built on " __DATE__ " at " __TIME__ ""
;
/**
* Convert the packet code to string.
*/
static const char *get_packet_name(int code) {
switch(code) {
case PW_ACCESS_REQUEST: return "Access-Request";
case PW_ACCESS_ACCEPT: return "Access-Accept";
case PW_ACCESS_REJECT: return "Access-Reject";
case PW_ACCOUNTING_REQUEST: return "Accounting-Request";
case PW_ACCOUNTING_RESPONSE: return "Accounting-Response";
case PW_ACCOUNTING_STATUS: return "Accounting-Status";
case PW_ACCOUNTING_MESSAGE: return "Accounting-Message";
case PW_ACCESS_CHALLENGE: return "Access-Challenge";
default: return "Unknown";
}
}
/** log helper
*
* @param[in] err Syslog priority id.
* @param[in] msg Mensagem to print.
* @param[in] ... Arguments for msg string.
*/
static void _pam_log(int err, char CONST *msg, ...)
{
char buf[BUFFER_SIZE];
va_list ap;
va_start(ap, msg);
#ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wformat-nonliteral"
#endif
(void)vsnprintf(buf, sizeof(buf), msg, ap);
#ifdef __clang__
# pragma clang diagnostic pop
#endif
va_end(ap);
/* don't do openlog or closelog, but put our name in to be friendly */
syslog(err, "%s: %s", pam_module_name, buf);
}
/** Argument parsing
*
* @param[in] argc Number of parameters
* @param[in] argv Array with all parameters
* @param[out] conf Where to write the radius_conf_t.
* @return
* - PAM used parameters
*/
static int _pam_parse(int argc, CONST char **argv, radius_conf_t *conf)
{
int ctrl = 0;
int i = 0;
memset(conf, 0, sizeof(radius_conf_t)); /* ensure it's initialized */
conf->conf_file = CONF_FILE;
/* set the default prompt */
snprintf(conf->prompt, MAXPROMPT, "%s: ", DEFAULT_PROMPT);
conf->use_ipv4 = 1;
conf->use_ipv6 = 1;
/*
* If either is not there, then we can't parse anything.
*/
if ((argc == 0) || (argv == NULL)) return ctrl;
/* step through arguments */
for (i = 0; i < argc; i++) {
char *arg;
memcpy(&arg, &argv[i], sizeof(arg));
#ifndef NDEBUG
_pam_log(LOG_DEBUG, "_pam_parse: argv[%d] = '%s'", i, arg);
#endif
/* generic options */
if (!strncmp(arg, "conf=", 5)) {
conf->conf_file = (arg + 5);
} else if (!strcmp(arg, "use_first_pass")) {
ctrl |= PAM_USE_FIRST_PASS;
} else if (!strcmp(arg, "try_first_pass")) {
ctrl |= PAM_TRY_FIRST_PASS;
} else if (!strcmp(arg, "skip_passwd")) {
ctrl |= PAM_SKIP_PASSWD;
} else if (!strncmp(arg, "retry=", 6)) {
conf->retries = strtoul((arg + 6), 0, 10);
} else if (!strcmp(arg, "localifdown")) {
conf->localifdown = 1;
} else if (!strncmp(arg, "client_id=", 10)) {
if (conf->client_id) {
_pam_log(LOG_WARNING, "ignoring duplicate '%s'", arg);
} else {
conf->client_id = (arg + 10); /* point to the client-id */
}
} else if (!strcmp(arg, "ruser")) {
ctrl |= PAM_RUSER_ARG;
} else if (!strcmp(arg, "debug")) {
ctrl |= PAM_DEBUG_ARG;
conf->debug = TRUE;
} else if (!strncmp(arg, "prompt=", 7)) {
if (!strncmp(conf->prompt, (arg+7), MAXPROMPT)) {
_pam_log(LOG_WARNING, "ignoring duplicate '%s'", arg);
} else {
/* truncate excessive prompts to (MAXPROMPT - 3) length */
if (strlen((arg+7)) >= (MAXPROMPT - 3)) {
*((arg + 7) + (MAXPROMPT - 3)) = '\0';
}
/* set the new prompt */
memset(conf->prompt, 0, sizeof(conf->prompt));
snprintf(conf->prompt, MAXPROMPT, "%s: ", (arg+7));
}
} else if (!strcmp(arg, "force_prompt")) {
conf->force_prompt = TRUE;
} else if (!strcmp(arg, "prompt_attribute")) {
conf->prompt_attribute = TRUE;
} else if (!strncmp(arg, "max_challenge=", 14)) {
conf->max_challenge = strtoul((arg+14), 0, 10);
} else if (!strncmp(arg, "ipv4=", 5)) {
if (!strcmp(arg + 5, "yes")) conf->use_ipv4 = 1;
if (!strcmp(arg + 5, "no")) conf->use_ipv4 = 0;
} else if (!strncmp(arg, "ipv6=", 5)) {
if (!strcmp(arg + 5, "yes")) conf->use_ipv6 = 1;
if (!strcmp(arg + 5, "no")) conf->use_ipv6 = 0;
} else if (!strcmp(arg, "privilege_level")) {
conf->privilege_level = TRUE;
} else {
_pam_log(LOG_WARNING, "unrecognized option '%s'", arg);
}
}
if (!conf->use_ipv4 && !conf->use_ipv6) {
_pam_log(LOG_WARNING, "Cannot disable both IPv4 and IPv6'");
conf->use_ipv4 = 1;
}
if (conf->debug) {
#define print_bool(cond) (cond) ? "yes" : "no"
#define print_string(cond) (cond) ? cond : ""
_pam_log(LOG_DEBUG, "DEBUG: conf='%s' use_first_pass=%s try_first_pass=%s skip_passwd=%s retry=%d " \
"localifdown=%s client_id='%s' ruser=%s prompt='%s' force_prompt=%s "\
"prompt_attribute=%s max_challenge=%d privilege_level=%s",
conf->conf_file,
print_bool(ctrl & PAM_USE_FIRST_PASS),
print_bool(ctrl & PAM_TRY_FIRST_PASS),
print_bool(ctrl & PAM_SKIP_PASSWD),
conf->retries,
print_bool(conf->localifdown),
print_string(conf->client_id),
print_bool(ctrl & PAM_RUSER_ARG),
conf->prompt,
print_bool(conf->force_prompt),
print_bool(conf->prompt_attribute),
conf->max_challenge,
print_bool(conf->privilege_level)
);
}
return ctrl;
}
/** Callback function used to free the saved return value for pam_setcred.
*
* @param[in] pamh PAM context specified by the pamh argument
* @param[in] data Associate some data with the handle pamh
* @param[in] error_status used to indicate to the module the sort of action it
*/
static void _int_free(UNUSED pam_handle_t *pamh, void *data, UNUSED int error_status)
{
free(data);
}
/** A strerror_r() wrapper function to deal with its nuisances.
*
* @param[in] errnum syslog priority id
* @param[in] buf Variadic arguments
* @param[in] buflen Variadic arguments
*/
static void get_error_string(int errnum, char *buf, size_t buflen)
{
#if !defined(__GLIBC__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE)
/* XSI version of strerror_r(). */
int retval = strerror_r(errnum, buf, buflen);
/* POSIX does not state what will happen to the buffer if the function fails.
* Put it into a known state rather than leave it possibly uninitialized. */
if (retval != 0 && buflen > (size_t)0) {
buf[0] = '\0';
}
#else
/* GNU version of strerror_r(). */
char tmp_buf[BUFFER_SIZE];
char *retval = strerror_r(errnum, tmp_buf, sizeof(tmp_buf));
snprintf(buf, buflen, "%s", retval);
#endif
}
/** Return an IP address as a struct sockaddr
*
* @param[in] host Hostname
* @param[out] addr sockaddr buffer
* @param[in] port used port
* @return
* - returns zero on success or one of the error codes listed in gai_strerror(3)
* if an error occurs
*/
static int get_ipaddr(CONST char *host, struct sockaddr *addr, CONST char *port)
{
struct addrinfo hints;
struct addrinfo *results;
int retval;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_ADDRCONFIG;
retval = getaddrinfo(host, port && port[0] ? port : NULL, &hints, &results);
if (retval == 0) {
memcpy(addr, results->ai_addr, results->ai_addrlen);
freeaddrinfo(results);
}
return retval;
}
/** take server->hostname, and convert it to server->ip
*
* @param[in] debug Debug status
* @param[out] server radius_server_t buffer
* @return
* - returns zero on success or one of the error codes listed in gai_strerror(3)
* if an error occurs
*/
static int host2server(int debug, radius_server_t *server)
{
char hostbuffer[256];
char tmp[256];
char *hostname;
char *portstart;
char *p, *port;
int retval, n;
/* hostname might be [ipv6::address] */
strncpy(hostbuffer, server->hostname, sizeof(hostbuffer) - 1);
hostbuffer[sizeof(hostbuffer) - 1] = 0;
hostname = hostbuffer;
portstart = hostbuffer;
if (hostname[0] == '[') {
if ((p = strchr(hostname, ']')) != NULL) {
hostname++;
*p++ = 0;
portstart = p;
}
}
if ((port = strchr(portstart, ':')) != NULL) {
*port++ = '\0';
if (isdigit((uint8_t)*port) && server->accounting) {
if (sscanf(port, "%d", &n) == 1) {
snprintf(tmp, sizeof(tmp), "%d", n + 1);
port = tmp;
}
}
} else {
strncpy(tmp, (server->accounting) ? "radacct" : "radius", sizeof(tmp));
port = tmp;
}
server->ip = (struct sockaddr *)&server->ip_storage;
retval = get_ipaddr(hostname, server->ip, port);
DPRINT(LOG_DEBUG, "DEBUG: get_ipaddr(%s) returned %d.\n", hostname, retval);
return retval;
}
/** Do XOR of two buffers.
*/
static uint8_t * xor(uint8_t *p, uint8_t *q, int length)
{
uint8_t *retval= p;
int i;
for (i = 0; i < length; i++) *(p++) ^= *(q++);
return retval;
}
/**************************************************************************
* MID-LEVEL RADIUS CODE
**************************************************************************/
/* get a pseudo-random vector.
*/
static void get_random_vector(uint8_t *vector)
{
#ifdef linux
int total = 0;
int fd;
fd = open("/dev/urandom", O_RDONLY); /* Linux: get *real* random numbers */
if (fd >= 0) {
while (total < AUTH_VECTOR_LEN) {
int bytes = read(fd, vector + total, AUTH_VECTOR_LEN - total);
if (bytes <= 0) break; /* oops! Error */
total += bytes;
}
close(fd);
}
if (total != AUTH_VECTOR_LEN)
#endif
{ /* do this *always* on other platforms */
MD5_CTX my_md5;
struct timeval tv;
struct timezone tz;
static unsigned int session = 0; /* make the number harder to guess */
/**
* Use the time of day with the best resolution the system can
* give us -- often close to microsecond accuracy.
*/
gettimeofday(&tv,&tz);
if (session == 0) session = getppid(); /* (possibly) hard to guess information */
tv.tv_sec ^= getpid() * session++;
/* Hash things to get maybe cryptographically strong pseudo-random numbers */
MD5Init(&my_md5);
MD5Update(&my_md5, (uint8_t *) &tv, sizeof(tv));
MD5Update(&my_md5, (uint8_t *) &tz, sizeof(tz));
MD5Final(vector, &my_md5); /* set the final vector */
}
}
/**
* RFC 2139 says to do generate the accounting request vector this way.
* However, the Livingston 1.16 server doesn't check it. The Cistron
* server (http://home.cistron.nl/~miquels/radius/) does, and this code
* seems to work with it. It also works with Funk's Steel-Belted RADIUS.
*/
static void get_accounting_vector(AUTH_HDR *request, radius_server_t *server)
{
MD5_CTX my_md5;
uint16_t len = ntohs(request->length);
int secretlen = strlen(server->secret);
memset(request->vector, 0, AUTH_VECTOR_LEN);
MD5Init(&my_md5);
memcpy(((char *)request) + len, server->secret, secretlen);
MD5Update(&my_md5, (uint8_t *)request, len + secretlen);
MD5Final(request->vector, &my_md5); /* set the final vector */
}
/**
* Verify the response from the server
*/
static int verify_packet(CONST char *secret, AUTH_HDR *response, AUTH_HDR *request)
{
MD5_CTX my_md5;
uint8_t calculated[AUTH_VECTOR_LEN];
uint8_t reply[AUTH_VECTOR_LEN];
/*
* We could dispense with the memcpy, and do MD5's of the packet
* + vector piece by piece. This is easier understand, and maybe faster.
*/
memcpy(reply, response->vector, AUTH_VECTOR_LEN); /* save the reply */
memcpy(response->vector, request->vector, AUTH_VECTOR_LEN); /* sent vector */
/* MD5(response packet header + vector + response packet data + secret) */
MD5Init(&my_md5);
MD5Update(&my_md5, (uint8_t *) response, ntohs(response->length));
/*
* This next bit is necessary because of a bug in the original Livingston
* RADIUS server. The authentication vector is *supposed* to be MD5'd
* with the old password (as the secret) for password changes.
* However, the old password isn't used. The "authentication" vector
* for the server reply packet is simply the MD5 of the reply packet.
* Odd, the code is 99% there, but the old password is never copied
* to the secret!
*/
if (*secret) MD5Update(&my_md5, (CONST uint8_t *) secret, strlen(secret));
MD5Final(calculated, &my_md5); /* set the final vector */
/* Did he use the same random vector + shared secret? */
if (memcmp(calculated, reply, AUTH_VECTOR_LEN) != 0) return FALSE;
return TRUE;
}
/**
* Find an attribute in a RADIUS packet. Note that the packet length
* is *always* kept in network byte order.
*/
static attribute_t *find_attribute(AUTH_HDR *response, uint8_t type)
{
attribute_t *attr = (attribute_t *) &response->data;
uint16_t len;
len = (ntohs(response->length) - AUTH_HDR_LEN);
while (attr->attribute != type) {
if ((len -= attr->length) <= 0) return NULL; /* not found */
attr = (attribute_t *) ((char *) attr + attr->length);
}
return attr;
}
/**
* Add an attribute to a RADIUS packet.
*/
static void add_attribute(AUTH_HDR *request, uint8_t type, CONST uint8_t *data, int length)
{
attribute_t *p;
p = (attribute_t *) ((uint8_t *)request + ntohs(request->length));
p->attribute = type;
p->length = length + 2; /* the total size of the attribute */
request->length = htons(ntohs(request->length) + p->length);
memcpy(p->data, data, length);
}
/**
* Add an integer attribute to a RADIUS packet.
*/
static void add_int_attribute(AUTH_HDR *request, uint8_t type, int data)
{
uint32_t value = htonl(data);
add_attribute(request, type, (uint8_t *) &value, sizeof(value));
}
static void add_nas_ip_address(AUTH_HDR *request, CONST char *hostname) {
struct addrinfo hints;
struct addrinfo *ai_start;
struct addrinfo *ai;
int v4seen = 0, v6seen = 0;
int r;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_ADDRCONFIG;
r = getaddrinfo(hostname, NULL, &hints, &ai_start);
if (r != 0) return;
ai = ai_start;
while (ai != NULL) {
if (!v4seen && ai->ai_family == AF_INET) {
v4seen = 1;
r = ((struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr;
add_int_attribute(request, PW_NAS_IP_ADDRESS, ntohl(r));
}
if (!v6seen && ai->ai_family == AF_INET6) {
v6seen = 1;
add_attribute(request, PW_NAS_IPV6_ADDRESS,
(uint8_t *) &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr, 16);
}
ai = ai->ai_next;
}
freeaddrinfo(ai_start);
}
/*
* Add a RADIUS password attribute to the packet.
*
* If the password attribute already exists, it's over-written. This allows
* us to simply call add_password to update the password for different
* servers.
*/
static void add_password(AUTH_HDR *request, uint8_t type, CONST char *password, CONST char *secret)
{
MD5_CTX md5_secret, my_md5;
uint8_t misc[AUTH_VECTOR_LEN];
int i;
int length = strlen(password);
uint8_t hashed[256 + AUTH_PASS_LEN]; /* can't be longer than this */
uint8_t *vector;
attribute_t *attr;
if (length > MAXPASS) { /* shorten the password for now */
length = MAXPASS;
}
memcpy(hashed, password, length);
memset(hashed + length, 0, sizeof(hashed) - length);
if (length == 0) {
length = AUTH_PASS_LEN; /* 0 maps to 16 */
} if ((length & (AUTH_PASS_LEN - 1)) != 0) {
length += (AUTH_PASS_LEN - 1); /* round it up */
length &= ~(AUTH_PASS_LEN - 1); /* chop it off */
} /* 16*N maps to itself */
attr = find_attribute(request, PW_USER_PASSWORD);
vector = request->vector;
/* ************************************************************ */
/* encrypt the password */
/* password : e[0] = p[0] ^ MD5(secret + vector) */
MD5Init(&md5_secret);
MD5Update(&md5_secret, (CONST uint8_t *) secret, strlen(secret));
my_md5 = md5_secret; /* so we won't re-do the hash later */
MD5Update(&my_md5, vector, AUTH_VECTOR_LEN);
MD5Final(misc, &my_md5); /* set the final vector */
xor(hashed, misc, AUTH_PASS_LEN);
/* For each step through, e[i] = p[i] ^ MD5(secret + e[i-1]) */
for (i = 1; i < (length >> 4); i++) {
my_md5 = md5_secret; /* grab old value of the hash */
MD5Update(&my_md5, &hashed[(i-1) * AUTH_PASS_LEN], AUTH_PASS_LEN);
MD5Final(misc, &my_md5); /* set the final vector */
xor(&hashed[i * AUTH_PASS_LEN], misc, AUTH_PASS_LEN);
}
if (!attr) {
add_attribute(request, type, hashed, length);
} else {
memcpy(attr->data, hashed, length); /* overwrite the old value of the attribute */
}
}
static void cleanup(radius_server_t *server)
{
radius_server_t *next;
while (server) {
next = server->next;
_pam_drop(server->hostname);
_pam_forget(server->secret);
if (server->sockfd != -1) close(server->sockfd);
if (server->sockfd6 != -1) close(server->sockfd6);
_pam_drop(server);
server = next;
}
}
static int initialize_sockets(radius_conf_t const *conf, int *sockfd, int *sockfd6, struct sockaddr_storage *salocal4, struct sockaddr_storage *salocal6, char *vrf)
{
if (!conf->use_ipv4) {
*sockfd = -1;
goto use_ipv6;
}
/* open a socket. Dies if it fails */
*sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (*sockfd < 0) {
char error_string[BUFFER_SIZE];
get_error_string(errno, error_string, sizeof(error_string));
_pam_log(LOG_ERR, "Failed to open RADIUS socket: %s\n", error_string);
return -1;
}
#ifndef HAVE_POLL_H
if (*sockfd >= FD_SETSIZE) {
_pam_log(LOG_ERR, "Unusable socket, FD is larger than %d\n", FD_SETSIZE);
return -1;
}
#endif
if (vrf && vrf[0]) {
#ifdef SO_BINDTODEVICE
int r = setsockopt(*sockfd, SOL_SOCKET, SO_BINDTODEVICE, vrf, strlen(vrf));
if (r != 0) {
_pam_log(LOG_ERR, "Failed bind to %s: %s", vrf, strerror(errno));
return -1;
}
#else
_pam_log(LOG_ERR, "No SO_BINDTODEVICE, unable to bind to: %s", vrf);
return -1;
#endif
}
/* set up the local end of the socket communications */
if (bind(*sockfd, (struct sockaddr *)salocal4, sizeof (struct sockaddr_in)) < 0) {
char error_string[BUFFER_SIZE];
get_error_string(errno, error_string, sizeof(error_string));
_pam_log(LOG_ERR, "Failed binding to port: %s", error_string);
return -1;
}
if (!conf->use_ipv6) {
*sockfd6 = -1;
return 0;
}
use_ipv6:
/* open a IPv6 socket. */
*sockfd6 = socket(AF_INET6, SOCK_DGRAM, 0);
if (*sockfd6 < 0) {
char error_string[BUFFER_SIZE];
/*
* IPv6 can be disabled on localhost.
*/
if (errno == EAFNOSUPPORT) return 0;
get_error_string(errno, error_string, sizeof(error_string));
_pam_log(LOG_ERR, "Failed to open RADIUS IPv6 socket: %s\n", error_string);
return -1;
}
#ifndef HAVE_POLL_H
if (*sockfd6 >= FD_SETSIZE) {
_pam_log(LOG_ERR, "Unusable socket, FD is larger than %d\n", FD_SETSIZE);
return -1;
}
#endif
if (vrf && vrf[0]) {
#ifdef SO_BINDTODEVICE
int r = setsockopt(*sockfd6, SOL_SOCKET, SO_BINDTODEVICE, vrf, strlen(vrf));
if (r != 0) {
_pam_log(LOG_ERR, "Failed bind to %s: %s", vrf, strerror(errno));
return -1;
}
#else
_pam_log(LOG_ERR, "No SO_BINDTODEVICE, unable to bind to: %s", vrf);
return -1;
#endif
}
/* set up the local end of the socket communications */
if (bind(*sockfd6, (struct sockaddr *)salocal6, sizeof (struct sockaddr_in6)) < 0) {
char error_string[BUFFER_SIZE];
get_error_string(errno, error_string, sizeof(error_string));
_pam_log(LOG_ERR, "Failed binding to IPv6 port: %s", error_string);
return -1;
}
return 0;
}
/*
* allocate and open a local port for communication with the RADIUS
* server
*/
static int initialize(radius_conf_t *conf, int accounting)
{
struct sockaddr_storage salocal;
struct sockaddr_storage salocal4;
struct sockaddr_storage salocal6;
char hostname[BUFFER_SIZE];
char secret[BUFFER_SIZE];
char buffer[BUFFER_SIZE];
char *p;
FILE *fp;
radius_server_t *server, **last;
int timeout;
int line = 0;
char src_ip[MAX_IP_LEN];
int valid_src_ip;
char vrf[IFNAMSIZ];
memset(&salocal4, 0, sizeof(salocal4));
memset(&salocal6, 0, sizeof(salocal6));
((struct sockaddr *)&salocal4)->sa_family = AF_INET;
((struct sockaddr *)&salocal6)->sa_family = AF_INET6;
conf->sockfd = -1;
conf->sockfd6 = -1;
/* the first time around, read the configuration file */
fp = fopen (conf->conf_file, "r");
if (!fp) {
char error_string[BUFFER_SIZE];
get_error_string(errno, error_string, sizeof(error_string));
_pam_log(LOG_ERR, "Could not open configuration file %s: %s\n",
conf->conf_file, error_string);
return PAM_ABORT;
}
conf->server = NULL;
last = &conf->server;
/*
* Read the file
*/
while (!feof(fp) && (fgets (buffer, sizeof(buffer), fp) != NULL) && (!ferror(fp))) {
line++;
p = buffer;
/*
* Skip whitespace
*/
while ((*p == ' ') || (*p == '\t')) p++;
/*
* Skip blank lines and comments.
*/
if ((*p == '\r') || (*p == '\n') || (*p == '#')) continue;
/*
* Error out if the text is too long.
*/
if (!*p) {
_pam_log(LOG_ERR, "ERROR reading %s, line %d: Line too long\n",
conf->conf_file, line);
break;
}
/*
* Initialize the optional variables.
*/
timeout = 3;
src_ip[0] = 0;
vrf[0] = 0;
/*
* Scan the line for data.
*/
if (sscanf(p, "%s %s %d %s %s", hostname, secret, &timeout, src_ip, vrf) < 2) {
_pam_log(LOG_ERR, "ERROR reading %s, line %d: Could not read hostname or secret\n",
conf->conf_file, line);
continue; /* invalid line */
}
/*
* Fill in the relevant fields.
*/
server = calloc(1, sizeof(radius_server_t));
*last = server;
server->next = NULL;
last = &server->next;
/* sometime later do memory checks here */
server->hostname = strdup(hostname);
server->secret = strdup(secret);
server->accounting = accounting;
/*
* Clamp the timeouts to reasonable values.
*/
if (timeout < 3) {
server->timeout = 3;
} else if (timeout > 60) {
server->timeout = 60;
} else {
server->timeout = timeout;
}
server->sockfd = -1;
server->sockfd6 = -1;
/*
* No source IP for this socket, it uses the
* global one.
*/
if (!src_ip[0]) continue;
memset(&salocal4, 0, sizeof(salocal4));
memset(&salocal6, 0, sizeof(salocal6));
((struct sockaddr *)&salocal4)->sa_family = AF_INET;
((struct sockaddr *)&salocal6)->sa_family = AF_INET6;
valid_src_ip = -1;
vrf[IFNAMSIZ - 1] = 0;
memset(&salocal, 0, sizeof(salocal));
valid_src_ip = get_ipaddr(src_ip, (struct sockaddr *)&salocal, NULL);
if (valid_src_ip == 0) {
switch (salocal.ss_family) {
case AF_INET:
memcpy(&salocal4, &salocal, sizeof(salocal));
break;
case AF_INET6:
memcpy(&salocal6, &salocal, sizeof(salocal));
break;
}
}
if (valid_src_ip == 0 || vrf[0]) {
if (initialize_sockets(conf, &server->sockfd, &server->sockfd6, &salocal4, &salocal6, vrf) != 0) {
goto error;
}
}
}
if (!conf->server) { /* no server found, die a horrible death */
_pam_log(LOG_ERR, "No RADIUS server found in configuration file %s\n",
conf->conf_file);
goto error;
}
/*
* Open the global sockets.
*/
memset(&salocal4, 0, sizeof(salocal4));
memset(&salocal6, 0, sizeof(salocal6));
((struct sockaddr *)&salocal4)->sa_family = AF_INET;
((struct sockaddr *)&salocal6)->sa_family = AF_INET6;
if (initialize_sockets(conf, &conf->sockfd, &conf->sockfd6, &salocal4, &salocal6, NULL) != 0) {
goto error;
}
fclose(fp);
return PAM_SUCCESS;
error:
fclose(fp);
if (conf->sockfd != -1) close(conf->sockfd);
if (conf->sockfd6 != -1) close(conf->sockfd6);
cleanup(conf->server);
return PAM_AUTHINFO_UNAVAIL;
}
/*
* Helper function for building a radius packet.
* It initializes *some* of the header, and adds common attributes.
*/
static void build_radius_packet(AUTH_HDR *request, CONST char *user, CONST char *password, radius_conf_t *conf)
{
char hostname[256];
hostname[0] = '\0';
gethostname(hostname, sizeof(hostname) - 1);
request->length = htons(AUTH_HDR_LEN);
if (password) { /* make a random authentication req vector */
get_random_vector(request->vector);
}
add_attribute(request, PW_USER_NAME, (CONST uint8_t *) user, strlen(user));
/*
* Add a password, if given.
*/
if (password) {
add_password(request, PW_USER_PASSWORD, password, conf->server->secret);
/*
* Add a NULL password to non-accounting requests.
*/
} else if (request->code != PW_ACCOUNTING_REQUEST) {
add_password(request, PW_USER_PASSWORD, "", conf->server->secret);
}
/* Perhaps add NAS IP Address (and v6 version) */
add_nas_ip_address(request, hostname);
/* There's always a NAS identifier */
if (conf->client_id && *conf->client_id) {
add_attribute(request, PW_NAS_IDENTIFIER, (CONST uint8_t *) conf->client_id, strlen(conf->client_id));
}
/*
* Add in the port (pid) and port type (virtual).
*
* We might want to give the TTY name here, too.
*/
add_int_attribute(request, PW_NAS_PORT_ID, getpid());
add_int_attribute(request, PW_NAS_PORT_TYPE, PW_NAS_PORT_TYPE_VIRTUAL);
}
static int ipaddr_cmp(struct sockaddr_storage const *a, struct sockaddr_storage const *b)
{
if (a->ss_family != b->ss_family) {
_pam_log(LOG_ERR, "RADIUS packet from invalid source - ignoring it");
return 0;
}
switch (a->ss_family) {
case AF_INET:
return (memcmp(&((struct sockaddr_in const *) a)->sin_addr.s_addr,
&((struct sockaddr_in const *) b)->sin_addr.s_addr,
sizeof(((struct sockaddr_in const *) a)->sin_addr.s_addr)) == 0);
case AF_INET6:
return (memcmp(&((struct sockaddr_in6 const *) a)->sin6_addr.s6_addr,
&((struct sockaddr_in6 const *) b)->sin6_addr.s6_addr,
sizeof(((struct sockaddr_in6 const *) a)->sin6_addr.s6_addr)) == 0);
default:
break;
}
return 0;
}
/*
* Talk RADIUS to a server.
* Send a packet and get the response
*/
static int talk_radius(radius_conf_t *conf, AUTH_HDR *request, AUTH_HDR *response,
char *password, int tries)
{
int total_length;
#ifdef HAVE_POLL_H
struct pollfd pollfds[1];
#else
fd_set set;
#endif
struct timeval tv;