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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
|
/* $OpenBSD: radiusd.c,v 1.19 2017/05/21 02:37:52 deraadt Exp $ */
/*
* Copyright (c) 2013 Internet Initiative Japan Inc.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/queue.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <dlfcn.h>
#include <err.h>
#include <errno.h>
#include <event.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <imsg.h>
#include <md5.h>
#include <netdb.h>
#include <pwd.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <util.h>
#include <radius.h>
#include "radiusd.h"
#include "radiusd_local.h"
#include "log.h"
#include "util.h"
#include "imsg_subr.h"
static int radiusd_start(struct radiusd *);
static void radiusd_stop(struct radiusd *);
static void radiusd_free(struct radiusd *);
static void radiusd_listen_on_event(int, short, void *);
static void radiusd_on_sigterm(int, short, void *);
static void radiusd_on_sigint(int, short, void *);
static void radiusd_on_sighup(int, short, void *);
static void radiusd_on_sigchld(int, short, void *);
static int radius_query_request_decoration(struct radius_query *);
static int radius_query_response_decoration(
struct radius_query *);
static const char *radius_code_string(int);
static int radiusd_access_response_fixup (struct radius_query *);
static void radiusd_module_reset_ev_handler(
struct radiusd_module *);
static int radiusd_module_imsg_read(struct radiusd_module *,
bool);
static void radiusd_module_imsg(struct radiusd_module *,
struct imsg *);
static struct radiusd_module_radpkt_arg *
radiusd_module_recv_radpkt(struct radiusd_module *,
struct imsg *, uint32_t, const char *);
static void radiusd_module_on_imsg_io(int, short, void *);
void radiusd_module_start(struct radiusd_module *);
void radiusd_module_stop(struct radiusd_module *);
static void radiusd_module_close(struct radiusd_module *);
static void radiusd_module_userpass(struct radiusd_module *,
struct radius_query *);
static void radiusd_module_access_request(struct radiusd_module *,
struct radius_query *);
static u_int radius_query_id_seq = 0;
int debug = 0;
static __dead void
usage(void)
{
extern char *__progname;
fprintf(stderr, "usage: %s [-dn] [-f file]\n", __progname);
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
extern char *__progname;
const char *conffile = CONFFILE;
int ch;
struct radiusd *radiusd;
bool noaction = false;
struct passwd *pw;
while ((ch = getopt(argc, argv, "df:n")) != -1)
switch (ch) {
case 'd':
debug++;
break;
case 'f':
conffile = optarg;
break;
case 'n':
noaction = true;
break;
default:
usage();
/* NOTREACHED */
}
argc -= optind;
argv += optind;
if (argc != 0)
usage();
if ((radiusd = calloc(1, sizeof(*radiusd))) == NULL)
err(1, "calloc");
TAILQ_INIT(&radiusd->listen);
TAILQ_INIT(&radiusd->query);
log_init(debug);
if (parse_config(conffile, radiusd) != 0)
errx(EXIT_FAILURE, "config error");
if (noaction) {
fprintf(stderr, "configuration OK\n");
exit(EXIT_SUCCESS);
}
if (debug == 0)
daemon(0, 0);
event_init();
if ((pw = getpwnam(RADIUSD_USER)) == NULL)
errx(EXIT_FAILURE, "user `%s' is not found in password "
"database", RADIUSD_USER);
if (chroot(pw->pw_dir) == -1)
err(EXIT_FAILURE, "chroot");
if (chdir("/") == -1)
err(EXIT_FAILURE, "chdir(\"/\")");
if (setgroups(1, &pw->pw_gid) ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
err(EXIT_FAILURE, "cannot drop privileges");
signal(SIGPIPE, SIG_IGN);
openlog(NULL, LOG_PID, LOG_DAEMON);
signal_set(&radiusd->ev_sigterm, SIGTERM, radiusd_on_sigterm, radiusd);
signal_set(&radiusd->ev_sigint, SIGINT, radiusd_on_sigint, radiusd);
signal_set(&radiusd->ev_sighup, SIGHUP, radiusd_on_sighup, radiusd);
signal_set(&radiusd->ev_sigchld, SIGCHLD, radiusd_on_sigchld, radiusd);
if (radiusd_start(radiusd) != 0)
errx(EXIT_FAILURE, "start failed");
#ifdef RADIUSD_DEBUG
if (pledge("stdio inet proc", NULL) == -1)
err(EXIT_FAILURE, "pledge");
#else
if (pledge("stdio inet", NULL) == -1)
err(EXIT_FAILURE, "pledge");
#endif
if (event_loop(0) < 0)
radiusd_stop(radiusd);
radiusd_free(radiusd);
event_base_free(NULL);
exit(EXIT_SUCCESS);
}
static int
radiusd_start(struct radiusd *radiusd)
{
struct radiusd_listen *l;
struct radiusd_module *module;
int s;
char hbuf[NI_MAXHOST];
TAILQ_FOREACH(l, &radiusd->listen, next) {
if (getnameinfo(
(struct sockaddr *)&l->addr, l->addr.ipv4.sin_len,
hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) {
log_warn("%s: getnameinfo()", __func__);
goto on_error;
}
if ((s = socket(l->addr.ipv4.sin_family,
l->stype | SOCK_NONBLOCK, l->sproto)) < 0) {
log_warn("Listen %s port %d is failed: socket()",
hbuf, (int)htons(l->addr.ipv4.sin_port));
goto on_error;
}
if (bind(s, (struct sockaddr *)&l->addr, l->addr.ipv4.sin_len)
!= 0) {
log_warn("Listen %s port %d is failed: bind()",
hbuf, (int)htons(l->addr.ipv4.sin_port));
close(s);
goto on_error;
}
if (l->addr.ipv4.sin_family == AF_INET)
log_info("Start listening on %s:%d/udp", hbuf,
(int)ntohs(l->addr.ipv4.sin_port));
else
log_info("Start listening on [%s]:%d/udp", hbuf,
(int)ntohs(l->addr.ipv4.sin_port));
event_set(&l->ev, s, EV_READ | EV_PERSIST,
radiusd_listen_on_event, l);
if (event_add(&l->ev, NULL) != 0) {
log_warn("event_add() failed at %s()", __func__);
close(s);
goto on_error;
}
l->sock = s;
l->radiusd = radiusd;
}
signal_add(&radiusd->ev_sigterm, NULL);
signal_add(&radiusd->ev_sigint, NULL);
signal_add(&radiusd->ev_sighup, NULL);
signal_add(&radiusd->ev_sigchld, NULL);
TAILQ_FOREACH(module, &radiusd->module, next) {
radiusd_module_start(module);
}
return (0);
on_error:
radiusd_stop(radiusd);
return (-1);
}
static void
radiusd_stop(struct radiusd *radiusd)
{
char hbuf[NI_MAXHOST];
struct radiusd_listen *l;
struct radiusd_module *module;
TAILQ_FOREACH_REVERSE(l, &radiusd->listen, radiusd_listen_head, next) {
if (l->sock >= 0) {
if (getnameinfo(
(struct sockaddr *)&l->addr, l->addr.ipv4.sin_len,
hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
strlcpy(hbuf, "error", sizeof(hbuf));
if (l->addr.ipv4.sin_family == AF_INET)
log_info("Stop listening on %s:%d/udp", hbuf,
(int)ntohs(l->addr.ipv4.sin_port));
else
log_info("Stop listening on [%s]:%d/udp", hbuf,
(int)ntohs(l->addr.ipv4.sin_port));
event_del(&l->ev);
close(l->sock);
}
l->sock = -1;
}
TAILQ_FOREACH(module, &radiusd->module, next) {
radiusd_module_stop(module);
radiusd_module_close(module);
}
if (signal_pending(&radiusd->ev_sigterm, NULL))
signal_del(&radiusd->ev_sigterm);
if (signal_pending(&radiusd->ev_sigint, NULL))
signal_del(&radiusd->ev_sigint);
if (signal_pending(&radiusd->ev_sighup, NULL))
signal_del(&radiusd->ev_sighup);
if (signal_pending(&radiusd->ev_sigchld, NULL))
signal_del(&radiusd->ev_sigchld);
}
static void
radiusd_free(struct radiusd *radiusd)
{
int i;
struct radiusd_listen *listn, *listnt;
struct radiusd_client *client, *clientt;
struct radiusd_module *module, *modulet;
struct radiusd_module_ref *modref, *modreft;
struct radiusd_authentication *authen, *authent;
TAILQ_FOREACH_SAFE(authen, &radiusd->authen, next, authent) {
TAILQ_REMOVE(&radiusd->authen, authen, next);
free(authen->auth);
TAILQ_FOREACH_SAFE(modref, &authen->deco, next, modreft) {
TAILQ_REMOVE(&authen->deco, modref, next);
free(modref);
}
for (i = 0; authen->username[i] != NULL; i++)
free(authen->username[i]);
free(authen->username);
free(authen);
}
TAILQ_FOREACH_SAFE(module, &radiusd->module, next, modulet) {
TAILQ_REMOVE(&radiusd->module, module, next);
radiusd_module_unload(module);
}
TAILQ_FOREACH_SAFE(client, &radiusd->client, next, clientt) {
TAILQ_REMOVE(&radiusd->client, client, next);
explicit_bzero(client->secret, sizeof(client->secret));
free(client);
}
TAILQ_FOREACH_SAFE(listn, &radiusd->listen, next, listnt) {
TAILQ_REMOVE(&radiusd->listen, listn, next);
free(listn);
}
free(radiusd);
}
/***********************************************************************
* Network event handlers
***********************************************************************/
#define IPv4_cmp(_in, _addr, _mask) ( \
((_in)->s_addr & (_mask)->addr.ipv4.s_addr) == \
(_addr)->addr.ipv4.s_addr)
#define s6_addr32(_in6) ((uint32_t *)(_in6)->s6_addr)
#define IPv6_cmp(_in6, _addr, _mask) ( \
((s6_addr32(_in6)[3] & (_mask)->addr.addr32[3]) \
== (_addr)->addr.addr32[3]) && \
((s6_addr32(_in6)[2] & (_mask)->addr.addr32[2]) \
== (_addr)->addr.addr32[2]) && \
((s6_addr32(_in6)[1] & (_mask)->addr.addr32[1]) \
== (_addr)->addr.addr32[1]) && \
((s6_addr32(_in6)[0] & (_mask)->addr.addr32[0]) \
== (_addr)->addr.addr32[0]))
static void
radiusd_listen_on_event(int fd, short evmask, void *ctx)
{
int i, sz, req_id, req_code;
struct radiusd_listen *listn = ctx;
static u_char buf[65535];
static char username[256];
struct sockaddr_storage peer;
socklen_t peersz;
RADIUS_PACKET *packet = NULL;
char peerstr[NI_MAXHOST + NI_MAXSERV + 30];
struct radiusd_authentication *authen;
struct radiusd_client *client;
struct radius_query *q;
#define in(_x) (((struct sockaddr_in *)_x)->sin_addr)
#define in6(_x) (((struct sockaddr_in6 *)_x)->sin6_addr)
if (evmask & EV_READ) {
peersz = sizeof(peer);
if ((sz = recvfrom(listn->sock, buf, sizeof(buf), 0,
(struct sockaddr *)&peer, &peersz)) < 0) {
if (errno == EAGAIN)
return;
log_warn("%s: recvfrom() failed", __func__);
goto on_error;
}
RADIUSD_ASSERT(peer.ss_family == AF_INET ||
peer.ss_family == AF_INET6);
/* prepare some information about this messages */
if (addrport_tostring((struct sockaddr *)&peer, peersz,
peerstr, sizeof(peerstr)) == NULL) {
log_warn("%s: getnameinfo() failed", __func__);
goto on_error;
}
if ((packet = radius_convert_packet(buf, sz)) == NULL) {
log_warn("%s: radius_convert_packet() failed",
__func__);
goto on_error;
}
req_id = radius_get_id(packet);
req_code = radius_get_code(packet);
/*
* Find a matching `client' entry
*/
TAILQ_FOREACH(client, &listn->radiusd->client, next) {
if (client->af != peer.ss_family)
continue;
if (peer.ss_family == AF_INET &&
IPv4_cmp(&((struct sockaddr_in *)&peer)->sin_addr,
&client->addr, &client->mask))
break;
else if (peer.ss_family == AF_INET6 &&
IPv6_cmp(&((struct sockaddr_in6 *)&peer)->sin6_addr,
&client->addr, &client->mask))
break;
}
if (client == NULL) {
log_warnx("Received %s(code=%d) from %s id=%d: "
"no `client' matches", radius_code_string(req_code),
req_code, peerstr, req_id);
goto on_error;
}
/* Check the client's Message-Authenticator */
if (client->msgauth_required &&
!radius_has_attr(packet,
RADIUS_TYPE_MESSAGE_AUTHENTICATOR)) {
log_warnx("Received %s(code=%d) from %s id=%d: "
"no message authenticator",
radius_code_string(req_code), req_code, peerstr,
req_id);
goto on_error;
}
if (radius_has_attr(packet,
RADIUS_TYPE_MESSAGE_AUTHENTICATOR) &&
radius_check_message_authenticator(packet, client->secret)
!= 0) {
log_warnx("Received %s(code=%d) from %s id=%d: "
"bad message authenticator",
radius_code_string(req_code), req_code, peerstr,
req_id);
goto on_error;
}
/*
* Find a duplicate request. In RFC 2865, it has the same
* source IP address and source UDP port and Identifier.
*/
TAILQ_FOREACH(q, &listn->radiusd->query, next) {
if (peer.ss_family == q->clientaddr.ss_family &&
((peer.ss_family == AF_INET &&
in(&q->clientaddr).s_addr ==
in(&peer).s_addr) ||
(peer.ss_family == AF_INET6 &&
IN6_ARE_ADDR_EQUAL(
&in6(&q->clientaddr), &in6(&peer)))) &&
((struct sockaddr_in *)&q->clientaddr)->sin_port ==
((struct sockaddr_in *)&peer)->sin_port &&
req_id == q->req_id)
break; /* found it */
}
if (q != NULL) {
log_info("Received %s(code=%d) from %s id=%d: "
"duplicate request by q=%u",
radius_code_string(req_code), req_code, peerstr,
req_id, q->id);
/* XXX RFC 5080 suggests to answer the cached result */
goto on_error;
}
/* FIXME: we can support other request codes */
if (req_code != RADIUS_CODE_ACCESS_REQUEST) {
log_info("Received %s(code=%d) from %s id=%d: %s "
"is not supported in this implementation",
radius_code_string(req_code), req_code, peerstr,
req_id, radius_code_string(req_code));
goto on_error;
}
/*
* Find a matching `authenticate' entry
*/
if (radius_get_string_attr(packet, RADIUS_TYPE_USER_NAME,
username, sizeof(username)) != 0) {
log_info("Received %s(code=%d) from %s id=%d: "
"no User-Name attribute",
radius_code_string(req_code), req_code, peerstr,
req_id);
goto on_error;
}
TAILQ_FOREACH(authen, &listn->radiusd->authen, next) {
for (i = 0; authen->username[i] != NULL; i++) {
if (fnmatch(authen->username[i], username, 0)
== 0)
goto found;
}
}
if (authen == NULL) {
log_warnx("Received %s(code=%d) from %s id=%d "
"username=%s: no `authenticate' matches.",
radius_code_string(req_code), req_code, peerstr,
req_id, username);
goto on_error;
}
found:
if (!MODULE_DO_USERPASS(authen->auth->module) &&
!MODULE_DO_ACCSREQ(authen->auth->module)) {
log_warnx("Received %s(code=%d) from %s id=%d "
"username=%s: module `%s' is not running.",
radius_code_string(req_code), req_code, peerstr,
req_id, username, authen->auth->module->name);
goto on_error;
}
if ((q = calloc(1, sizeof(struct radius_query))) == NULL) {
log_warn("%s: Out of memory", __func__);
goto on_error;
}
memcpy(&q->clientaddr, &peer, peersz);
strlcpy(q->username, username, sizeof(q->username));
q->id = ++radius_query_id_seq;
q->clientaddrlen = peersz;
q->authen = authen;
q->listen = listn;
q->req = packet;
q->client = client;
q->req_id = req_id;
radius_get_authenticator(packet, q->req_auth);
if (radius_query_request_decoration(q) != 0) {
log_warnx(
"Received %s(code=%d) from %s id=%d username=%s "
"q=%u: failed to decorate the request",
radius_code_string(req_code), req_code, peerstr,
q->req_id, q->username, q->id);
radiusd_access_request_aborted(q);
return;
}
log_info("Received %s(code=%d) from %s id=%d username=%s "
"q=%u: `%s' authentication is starting",
radius_code_string(req_code), req_code, peerstr, q->req_id,
q->username, q->id, q->authen->auth->module->name);
TAILQ_INSERT_TAIL(&listn->radiusd->query, q, next);
if (MODULE_DO_ACCSREQ(authen->auth->module)) {
radiusd_module_access_request(authen->auth->module, q);
} else if (MODULE_DO_USERPASS(authen->auth->module))
radiusd_module_userpass(authen->auth->module, q);
return;
}
on_error:
if (packet != NULL)
radius_delete_packet(packet);
#undef in
#undef in6
return;
}
static int
radius_query_request_decoration(struct radius_query *q)
{
struct radiusd_module_ref *deco;
TAILQ_FOREACH(deco, &q->authen->deco, next) {
/* XXX decoration doesn't work for this moment. */
if (deco->module->request_decoration != NULL &&
deco->module->request_decoration(NULL, q) != 0) {
log_warnx("q=%u request decoration `%s' failed", q->id,
deco->module->name);
return (-1);
}
}
return (0);
}
static int
radius_query_response_decoration(struct radius_query *q)
{
struct radiusd_module_ref *deco;
TAILQ_FOREACH(deco, &q->authen->deco, next) {
/* XXX decoration doesn't work for this moment. */
if (deco->module->response_decoration != NULL &&
deco->module->response_decoration(NULL, q) != 0) {
log_warnx("q=%u response decoration `%s' failed", q->id,
deco->module->name);
return (-1);
}
}
return (0);
}
/***********************************************************************
* Callback functions from the modules
***********************************************************************/
void
radiusd_access_request_answer(struct radius_query *q)
{
int sz, res_id, res_code;
char buf[NI_MAXHOST + NI_MAXSERV + 30];
const char *authen_secret = q->authen->auth->module->secret;
radius_set_request_packet(q->res, q->req);
if (authen_secret == NULL) {
/*
* The module couldn't check the autheticators
*/
if (radius_check_response_authenticator(q->res,
q->client->secret) != 0) {
log_info("Response from module has bad response "
"authenticator: id=%d", q->id);
goto on_error;
}
if (radius_has_attr(q->res,
RADIUS_TYPE_MESSAGE_AUTHENTICATOR) &&
radius_check_message_authenticator(q->res,
q->client->secret) != 0) {
log_info("Response from module has bad message "
"authenticator: id=%d", q->id);
goto on_error;
}
}
/* Decorate the response */
if (radius_query_response_decoration(q) != 0)
goto on_error;
if (radiusd_access_response_fixup(q) != 0)
goto on_error;
res_id = radius_get_id(q->res);
res_code = radius_get_code(q->res);
/*
* Reset response authenticator in the following cases:
* - response is modified by decorator
* - server's secret is differ from client's secret.
*/
if (q->res_modified > 0 ||
(authen_secret != NULL &&
strcmp(q->client->secret, authen_secret) != 0))
radius_set_response_authenticator(q->res, q->client->secret);
log_info("Sending %s(code=%d) to %s id=%u q=%u",
radius_code_string(res_code), res_code,
addrport_tostring((struct sockaddr *)&q->clientaddr,
q->clientaddrlen, buf, sizeof(buf)), res_id, q->id);
if ((sz = sendto(q->listen->sock, radius_get_data(q->res),
radius_get_length(q->res), 0,
(struct sockaddr *)&q->clientaddr, q->clientaddrlen)) <= 0)
log_warn("Sending a RADIUS response failed");
on_error:
radiusd_access_request_aborted(q);
}
void
radiusd_access_request_aborted(struct radius_query *q)
{
if (q->req != NULL)
radius_delete_packet(q->req);
if (q->res != NULL)
radius_delete_packet(q->res);
TAILQ_REMOVE(&q->listen->radiusd->query, q, next);
free(q);
}
/***********************************************************************
* Signal handlers
***********************************************************************/
static void
radiusd_on_sigterm(int fd, short evmask, void *ctx)
{
struct radiusd *radiusd = ctx;
log_info("Received SIGTERM");
radiusd_stop(radiusd);
}
static void
radiusd_on_sigint(int fd, short evmask, void *ctx)
{
struct radiusd *radiusd = ctx;
log_info("Received SIGINT");
radiusd_stop(radiusd);
}
static void
radiusd_on_sighup(int fd, short evmask, void *ctx)
{
log_info("Received SIGHUP");
}
static void
radiusd_on_sigchld(int fd, short evmask, void *ctx)
{
struct radiusd *radiusd = ctx;
struct radiusd_module *module;
pid_t pid;
int status;
log_debug("Received SIGCHLD");
while ((pid = wait3(&status, WNOHANG, NULL)) != 0) {
if (pid == -1)
break;
TAILQ_FOREACH(module, &radiusd->module, next) {
if (module->pid == pid) {
if (WIFEXITED(status))
log_warnx("module `%s'(pid=%d) exited "
"with status %d", module->name,
(int)pid, WEXITSTATUS(status));
else
log_warnx("module `%s'(pid=%d) exited "
"by signal %d", module->name,
(int)pid, WTERMSIG(status));
break;
}
}
if (!module) {
if (WIFEXITED(status))
log_warnx("unkown child process pid=%d exited "
"with status %d", (int)pid,
WEXITSTATUS(status));
else
log_warnx("unkown child process pid=%d exited "
"by signal %d", (int)pid,
WTERMSIG(status));
}
}
}
static const char *
radius_code_string(int code)
{
int i;
struct _codestrings {
int code;
const char *string;
} codestrings[] = {
{ RADIUS_CODE_ACCESS_REQUEST, "Access-Request" },
{ RADIUS_CODE_ACCESS_ACCEPT, "Access-Accept" },
{ RADIUS_CODE_ACCESS_REJECT, "Access-Reject" },
{ RADIUS_CODE_ACCOUNTING_REQUEST, "Accounting-Request" },
{ RADIUS_CODE_ACCOUNTING_RESPONSE, "Accounting-Response" },
{ RADIUS_CODE_ACCESS_CHALLENGE, "Access-Challenge" },
{ RADIUS_CODE_STATUS_SERVER, "Status-Server" },
{ RADIUS_CODE_STATUS_CLIENT, "Status-Clinet" },
{ -1, NULL }
};
for (i = 0; codestrings[i].code != -1; i++)
if (codestrings[i].code == code)
return (codestrings[i].string);
return ("Unknown");
}
void
radiusd_conf_init(struct radiusd *conf)
{
TAILQ_INIT(&conf->listen);
TAILQ_INIT(&conf->module);
TAILQ_INIT(&conf->authen);
TAILQ_INIT(&conf->client);
/*
* TODO: load the standard modules
*/
#if 0
static struct radiusd_module *radiusd_standard_modules[] = {
NULL
};
u_int i;
struct radiusd_module *module;
for (i = 0; radiusd_standard_modules[i] != NULL; i++) {
module = radiusd_create_module_class(
radiusd_standard_modules[i]);
TAILQ_INSERT_TAIL(&conf->module, module, next);
}
#endif
return;
}
/*
* Fix some attributes which depend the secret value.
*/
static int
radiusd_access_response_fixup(struct radius_query *q)
{
int res_id;
size_t attrlen;
u_char req_auth[16], attrbuf[256];
const char *olds, *news;
const char *authen_secret = q->authen->auth->module->secret;
olds = q->client->secret;
news = authen_secret;
if (news == NULL)
olds = news;
radius_get_authenticator(q->req, req_auth);
if ((authen_secret != NULL &&
strcmp(authen_secret, q->client->secret) != 0) ||
timingsafe_bcmp(q->req_auth, req_auth, 16) != 0) {
/* RFC 2865 Tunnel-Password */
if (radius_get_raw_attr(q->res, RADIUS_TYPE_TUNNEL_PASSWORD,
attrbuf, &attrlen) == 0) {
radius_attr_unhide(news, req_auth,
attrbuf, attrbuf + 3, attrlen - 3);
radius_attr_hide(olds, q->req_auth,
attrbuf, attrbuf + 3, attrlen - 3);
radius_del_attr_all(q->res,
RADIUS_TYPE_TUNNEL_PASSWORD);
radius_put_raw_attr(q->res,
RADIUS_TYPE_TUNNEL_PASSWORD, attrbuf, attrlen);
q->res_modified++;
}
/* RFC 2548 Microsoft MPPE-{Send,Recv}-Key */
if (radius_get_vs_raw_attr(q->res, RADIUS_VENDOR_MICROSOFT,
RADIUS_VTYPE_MPPE_SEND_KEY, attrbuf, &attrlen) == 0) {
/* Re-crypt the KEY */
radius_attr_unhide(news, req_auth,
attrbuf, attrbuf + 2, attrlen - 2);
radius_attr_hide(olds, q->req_auth,
attrbuf, attrbuf + 2, attrlen - 2);
radius_del_vs_attr_all(q->res, RADIUS_VENDOR_MICROSOFT,
RADIUS_VTYPE_MPPE_SEND_KEY);
radius_put_vs_raw_attr(q->res, RADIUS_VENDOR_MICROSOFT,
RADIUS_VTYPE_MPPE_SEND_KEY, attrbuf, attrlen);
q->res_modified++;
}
if (radius_get_vs_raw_attr(q->res, RADIUS_VENDOR_MICROSOFT,
RADIUS_VTYPE_MPPE_RECV_KEY, attrbuf, &attrlen) == 0) {
/* Re-crypt the KEY */
radius_attr_unhide(news, req_auth,
attrbuf, attrbuf + 2, attrlen - 2);
radius_attr_hide(olds, q->req_auth,
attrbuf, attrbuf + 2, attrlen - 2);
radius_del_vs_attr_all(q->res, RADIUS_VENDOR_MICROSOFT,
RADIUS_VTYPE_MPPE_RECV_KEY);
radius_put_vs_raw_attr(q->res, RADIUS_VENDOR_MICROSOFT,
RADIUS_VTYPE_MPPE_RECV_KEY, attrbuf, attrlen);
q->res_modified++;
}
}
res_id = radius_get_id(q->res);
if (res_id != q->req_id) {
/* authentication server change the id */
radius_set_id(q->res, q->req_id);
q->res_modified++;
}
return (0);
}
void
radius_attr_hide(const char *secret, const char *authenticator,
const u_char *salt, u_char *plain, int plainlen)
{
int i, j;
u_char b[16];
MD5_CTX md5ctx;
i = 0;
do {
MD5Init(&md5ctx);
MD5Update(&md5ctx, secret, strlen(secret));
if (i == 0) {
MD5Update(&md5ctx, authenticator, 16);
if (salt != NULL)
MD5Update(&md5ctx, salt, 2);
} else
MD5Update(&md5ctx, plain + i - 16, 16);
MD5Final(b, &md5ctx);
for (j = 0; j < 16 && i < plainlen; i++, j++)
plain[i] ^= b[j];
} while (i < plainlen);
}
void
radius_attr_unhide(const char *secret, const char *authenticator,
const u_char *salt, u_char *crypt0, int crypt0len)
{
int i, j;
u_char b[16];
MD5_CTX md5ctx;
i = 16 * ((crypt0len - 1) / 16);
while (i >= 0) {
MD5Init(&md5ctx);
MD5Update(&md5ctx, secret, strlen(secret));
if (i == 0) {
MD5Update(&md5ctx, authenticator, 16);
if (salt != NULL)
MD5Update(&md5ctx, salt, 2);
} else
MD5Update(&md5ctx, crypt0 + i - 16, 16);
MD5Final(b, &md5ctx);
for (j = 0; j < 16 && i + j < crypt0len; j++)
crypt0[i + j] ^= b[j];
i -= 16;
}
}
static struct radius_query *
radiusd_find_query(struct radiusd *radiusd, u_int q_id)
{
struct radius_query *q;
TAILQ_FOREACH(q, &radiusd->query, next) {
if (q->id == q_id)
return (q);
}
return (NULL);
}
/***********************************************************************
* radiusd module handling
***********************************************************************/
struct radiusd_module *
radiusd_module_load(struct radiusd *radiusd, const char *path, const char *name)
{
struct radiusd_module *module = NULL;
pid_t pid;
int ival, pairsock[] = { -1, -1 };
const char *av[3];
ssize_t n;
struct imsg imsg;
module = calloc(1, sizeof(struct radiusd_module));
if (module == NULL)
fatal("Out of memory");
module->radiusd = radiusd;
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pairsock) == -1) {
log_warn("Could not load module `%s'(%s): pipe()", name, path);
goto on_error;
}
pid = fork();
if (pid == -1) {
log_warn("Could not load module `%s'(%s): fork()", name, path);
goto on_error;
}
if (pid == 0) {
setsid();
close(pairsock[0]);
av[0] = path;
av[1] = name;
av[2] = NULL;
dup2(pairsock[1], STDIN_FILENO);
dup2(pairsock[1], STDOUT_FILENO);
close(pairsock[1]);
closefrom(STDERR_FILENO + 1);
execv(path, (char * const *)av);
log_warn("Failed to execute %s", path);
_exit(EXIT_FAILURE);
}
close(pairsock[1]);
module->fd = pairsock[0];
if ((ival = fcntl(module->fd, F_GETFL)) < 0) {
log_warn("Could not load module `%s': fcntl(F_GETFL)",
name);
goto on_error;
}
if (fcntl(module->fd, F_SETFL, ival | O_NONBLOCK) < 0) {
log_warn("Could not load module `%s': fcntl(F_SETFL,O_NONBLOCK)",
name);
goto on_error;
}
strlcpy(module->name, name, sizeof(module->name));
module->pid = pid;
imsg_init(&module->ibuf, module->fd);
if (imsg_sync_read(&module->ibuf, MODULE_IO_TIMEOUT) <= 0 ||
(n = imsg_get(&module->ibuf, &imsg)) <= 0) {
log_warnx("Could not load module `%s': module didn't "
"respond", name);
goto on_error;
}
if (imsg.hdr.type != IMSG_RADIUSD_MODULE_LOAD) {
imsg_free(&imsg);
log_warnx("Could not load module `%s': unknown imsg type=%d",
name, imsg.hdr.type);
goto on_error;
}
module->capabilities =
((struct radiusd_module_load_arg *)imsg.data)->cap;
log_debug("Loaded module `%s' successfully. pid=%d", module->name,
module->pid);
imsg_free(&imsg);
return (module);
on_error:
free(module);
if (pairsock[0] >= 0)
close(pairsock[0]);
if (pairsock[1] >= 0)
close(pairsock[1]);
return (NULL);
}
void
radiusd_module_start(struct radiusd_module *module)
{
int datalen;
struct imsg imsg;
struct timeval tv = { 0, 0 };
RADIUSD_ASSERT(module->fd >= 0);
imsg_compose(&module->ibuf, IMSG_RADIUSD_MODULE_START, 0, 0, -1,
NULL, 0);
imsg_sync_flush(&module->ibuf, MODULE_IO_TIMEOUT);
if (imsg_sync_read(&module->ibuf, MODULE_IO_TIMEOUT) <= 0 ||
imsg_get(&module->ibuf, &imsg) <= 0) {
log_warnx("Module `%s' could not start: no response",
module->name);
goto on_fail;
}
datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
if (imsg.hdr.type != IMSG_OK) {
if (imsg.hdr.type == IMSG_NG) {
if (datalen > 0)
log_warnx("Module `%s' could not start: %s",
module->name, (char *)imsg.data);
else
log_warnx("Module `%s' could not start",
module->name);
} else
log_warnx("Module `%s' could not started: module "
"returned unknow message type %d", module->name,
imsg.hdr.type);
goto on_fail;
}
event_set(&module->ev, module->fd, EV_READ, radiusd_module_on_imsg_io,
module);
event_add(&module->ev, &tv);
log_debug("Module `%s' started successfully", module->name);
return;
on_fail:
radiusd_module_close(module);
return;
}
void
radiusd_module_stop(struct radiusd_module *module)
{
module->stopped = true;
freezero(module->secret, strlen(module->secret));
module->secret = NULL;
if (module->fd >= 0) {
imsg_compose(&module->ibuf, IMSG_RADIUSD_MODULE_STOP, 0, 0, -1,
NULL, 0);
radiusd_module_reset_ev_handler(module);
}
}
static void
radiusd_module_close(struct radiusd_module *module)
{
if (module->fd >= 0) {
event_del(&module->ev);
imsg_clear(&module->ibuf);
close(module->fd);
module->fd = -1;
}
}
void
radiusd_module_unload(struct radiusd_module *module)
{
free(module->radpkt);
radiusd_module_close(module);
free(module);
}
static void
radiusd_module_on_imsg_io(int fd, short evmask, void *ctx)
{
struct radiusd_module *module = ctx;
int ret;
if (evmask & EV_WRITE)
module->writeready = true;
if (evmask & EV_READ || module->ibuf.r.wpos > IMSG_HEADER_SIZE) {
if (radiusd_module_imsg_read(module,
(evmask & EV_READ)? true : false) == -1)
goto on_error;
}
while (module->writeready && module->ibuf.w.queued) {
ret = msgbuf_write(&module->ibuf.w);
if (ret > 0)
continue;
module->writeready = false;
if (ret == 0 && errno == EAGAIN)
break;
log_warn("Failed to write to module `%s': msgbuf_write()",
module->name);
goto on_error;
}
radiusd_module_reset_ev_handler(module);
return;
on_error:
radiusd_module_close(module);
}
static void
radiusd_module_reset_ev_handler(struct radiusd_module *module)
{
short evmask;
struct timeval *tvp = NULL, tv = { 0, 0 };
RADIUSD_ASSERT(module->fd >= 0);
event_del(&module->ev);
evmask = EV_READ;
if (module->ibuf.w.queued) {
if (!module->writeready)
evmask |= EV_WRITE;
else
tvp = &tv; /* fire immediately */
} else if (module->ibuf.r.wpos > IMSG_HEADER_SIZE)
tvp = &tv; /* fire immediately */
/* module stopped and no event handler is set */
if (evmask & EV_WRITE && tvp == NULL && module->stopped) {
/* stop requested and no more to write */
radiusd_module_close(module);
return;
}
event_set(&module->ev, module->fd, evmask, radiusd_module_on_imsg_io,
module);
if (event_add(&module->ev, tvp) == -1) {
log_warn("Could not set event handlers for module `%s': "
"event_add()", module->name);
radiusd_module_close(module);
}
}
static int
radiusd_module_imsg_read(struct radiusd_module *module, bool doread)
{
int n;
struct imsg imsg;
if (doread) {
if ((n = imsg_read(&module->ibuf)) == -1 || n == 0) {
if (n == -1 && errno == EAGAIN)
return (0);
if (n == -1)
log_warn("Receiving a message from module `%s' "
"failed: imsg_read", module->name);
/* else closed */
radiusd_module_close(module);
return (-1);
}
}
for (;;) {
if ((n = imsg_get(&module->ibuf, &imsg)) == -1) {
log_warn("Receiving a message from module `%s' failed: "
"imsg_get", module->name);
return (-1);
}
if (n == 0)
return (0);
radiusd_module_imsg(module, &imsg);
}
return (0);
}
static void
radiusd_module_imsg(struct radiusd_module *module, struct imsg *imsg)
{
int datalen;
struct radius_query *q;
u_int q_id;
datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
switch (imsg->hdr.type) {
case IMSG_RADIUSD_MODULE_NOTIFY_SECRET:
if (datalen > 0) {
module->secret = strdup(imsg->data);
if (module->secret == NULL)
log_warn("Could not handle NOTIFY_SECRET "
"from `%s'", module->name);
}
break;
case IMSG_RADIUSD_MODULE_USERPASS_OK:
case IMSG_RADIUSD_MODULE_USERPASS_FAIL:
{
char *msg = NULL;
const char *msgtypestr;
msgtypestr = (imsg->hdr.type == IMSG_RADIUSD_MODULE_USERPASS_OK)
? "USERPASS_OK" : "USERPASS_NG";
q_id = *(u_int *)imsg->data;
if (datalen > (ssize_t)sizeof(u_int))
msg = (char *)(((u_int *)imsg->data) + 1);
q = radiusd_find_query(module->radiusd, q_id);
if (q == NULL) {
log_warnx("Received %s from `%s', but query id=%u "
"unknown", msgtypestr, module->name, q_id);
break;
}
if ((q->res = radius_new_response_packet(
(imsg->hdr.type == IMSG_RADIUSD_MODULE_USERPASS_OK)
? RADIUS_CODE_ACCESS_ACCEPT : RADIUS_CODE_ACCESS_REJECT,
q->req)) == NULL) {
log_warn("radius_new_response_packet() failed");
radiusd_access_request_aborted(q);
} else {
if (msg)
radius_put_string_attr(q->res,
RADIUS_TYPE_REPLY_MESSAGE, msg);
radius_set_response_authenticator(q->res,
q->client->secret);
radiusd_access_request_answer(q);
}
break;
}
case IMSG_RADIUSD_MODULE_ACCSREQ_ANSWER:
{
static struct radiusd_module_radpkt_arg *ans;
if (datalen <
(ssize_t)sizeof(struct radiusd_module_radpkt_arg)) {
log_warnx("Received ACCSREQ_ANSWER message, but "
"length is wrong");
break;
}
q_id = ((struct radiusd_module_radpkt_arg *)imsg->data)->q_id;
q = radiusd_find_query(module->radiusd, q_id);
if (q == NULL) {
log_warnx("Received ACCSREQ_ANSWER from %s, but query "
"id=%u unknown", module->name, q_id);
break;
}
if ((ans = radiusd_module_recv_radpkt(module, imsg,
IMSG_RADIUSD_MODULE_ACCSREQ_ANSWER,
"ACCSREQ_ANSWER")) != NULL) {
q->res = radius_convert_packet(
module->radpkt, module->radpktoff);
q->res_modified = ans->modified;
radiusd_access_request_answer(q);
module->radpktoff = 0;
}
break;
}
case IMSG_RADIUSD_MODULE_ACCSREQ_ABORTED:
{
q_id = *((u_int *)imsg->data);
q = radiusd_find_query(module->radiusd, q_id);
if (q == NULL) {
log_warnx("Received ACCSREQ_ABORT from %s, but query "
"id=%u unknown", module->name, q_id);
break;
}
radiusd_access_request_aborted(q);
break;
}
default:
RADIUSD_DBG(("Unhandled imsg type=%d",
imsg->hdr.type));
}
}
static struct radiusd_module_radpkt_arg *
radiusd_module_recv_radpkt(struct radiusd_module *module, struct imsg *imsg,
uint32_t imsg_type, const char *type_str)
{
struct radiusd_module_radpkt_arg *ans;
int datalen, chunklen;
datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
ans = (struct radiusd_module_radpkt_arg *)imsg->data;
if (module->radpktsiz < ans->datalen) {
u_char *nradpkt;
if ((nradpkt = realloc(module->radpkt, ans->datalen)) == NULL) {
log_warn("Could not handle received %s message from "
"`%s'", type_str, module->name);
goto on_fail;
}
module->radpkt = nradpkt;
module->radpktsiz = ans->datalen;
}
chunklen = datalen - sizeof(struct radiusd_module_radpkt_arg);
if (chunklen > module->radpktsiz - module->radpktoff) {
log_warnx("Could not handle received %s message from `%s': "
"received length is too big", type_str, module->name);
goto on_fail;
}
memcpy(module->radpkt + module->radpktoff,
(caddr_t)(ans + 1), chunklen);
module->radpktoff += chunklen;
if (!ans->final)
return (NULL); /* again */
if (module->radpktoff != module->radpktsiz) {
log_warnx("Could not handle received %s message from `%s': "
"length is mismatch", type_str, module->name);
goto on_fail;
}
return (ans);
on_fail:
module->radpktoff = 0;
return (NULL);
}
int
radiusd_module_set(struct radiusd_module *module, const char *name,
int argc, char * const * argv)
{
struct radiusd_module_set_arg arg;
struct radiusd_module_object *val;
int i, niov = 0;
u_char *buf = NULL, *buf0;
ssize_t n;
size_t bufsiz = 0, bufoff = 0, bufsiz0;
size_t vallen, valsiz;
struct iovec iov[2];
struct imsg imsg;
memset(&arg, 0, sizeof(arg));
arg.nparamval = argc;
strlcpy(arg.paramname, name, sizeof(arg.paramname));
iov[niov].iov_base = &arg;
iov[niov].iov_len = sizeof(struct radiusd_module_set_arg);
niov++;
for (i = 0; i < argc; i++) {
vallen = strlen(argv[i]) + 1;
valsiz = sizeof(struct radiusd_module_object) + vallen;
if (bufsiz < bufoff + valsiz) {
bufsiz0 = bufoff + valsiz + 128;
if ((buf0 = realloc(buf, bufsiz0)) == NULL) {
log_warn("Failed to set config parameter to "
"module `%s': realloc", module->name);
goto on_error;
}
buf = buf0;
bufsiz = bufsiz0;
memset(buf + bufoff, 0, bufsiz - bufoff);
}
val = (struct radiusd_module_object *)(buf + bufoff);
val->size = valsiz;
memcpy(val + 1, argv[i], vallen);
bufoff += valsiz;
}
iov[niov].iov_base = buf;
iov[niov].iov_len = bufoff;
niov++;
if (imsg_composev(&module->ibuf, IMSG_RADIUSD_MODULE_SET_CONFIG, 0, 0,
-1, iov, niov) == -1) {
log_warn("Failed to set config parameter to module `%s': "
"imsg_composev", module->name);
goto on_error;
}
if (imsg_sync_flush(&module->ibuf, MODULE_IO_TIMEOUT) == -1) {
log_warn("Failed to set config parameter to module `%s': "
"imsg_flush_timeout", module->name);
goto on_error;
}
for (;;) {
if (imsg_sync_read(&module->ibuf, MODULE_IO_TIMEOUT) <= 0) {
log_warn("Failed to get reply from module `%s': "
"imsg_sync_read", module->name);
goto on_error;
}
if ((n = imsg_get(&module->ibuf, &imsg)) > 0)
break;
if (n < 0) {
log_warn("Failed to get reply from module `%s': "
"imsg_get", module->name);
goto on_error;
}
}
if (imsg.hdr.type == IMSG_NG) {
log_warnx("Could not set `%s' for module `%s': %s", name,
module->name, (char *)imsg.data);
goto on_error;
} else if (imsg.hdr.type != IMSG_OK) {
imsg_free(&imsg);
log_warnx("Failed to get reply from module `%s': "
"unknown imsg type=%d", module->name, imsg.hdr.type);
goto on_error;
}
imsg_free(&imsg);
free(buf);
return (0);
on_error:
free(buf);
return (-1);
}
static void
radiusd_module_userpass(struct radiusd_module *module, struct radius_query *q)
{
struct radiusd_module_userpass_arg userpass;
memset(&userpass, 0, sizeof(userpass));
userpass.q_id = q->id;
if (radius_get_user_password_attr(q->req, userpass.pass,
sizeof(userpass.pass), q->client->secret) == 0)
userpass.has_pass = true;
else
userpass.has_pass = false;
if (strlcpy(userpass.user, q->username, sizeof(userpass.user))
>= sizeof(userpass.user)) {
log_warnx("Could request USERPASS to module `%s': "
"User-Name too long", module->name);
goto on_error;
}
imsg_compose(&module->ibuf, IMSG_RADIUSD_MODULE_USERPASS, 0, 0, -1,
&userpass, sizeof(userpass));
radiusd_module_reset_ev_handler(module);
return;
on_error:
radiusd_access_request_aborted(q);
}
static void
radiusd_module_access_request(struct radiusd_module *module,
struct radius_query *q)
{
struct radiusd_module_radpkt_arg accsreq;
struct iovec iov[2];
int off = 0, len, siz;
const u_char *pkt;
RADIUS_PACKET *radpkt;
char pass[256];
if ((radpkt = radius_convert_packet(radius_get_data(q->req),
radius_get_length(q->req))) == NULL) {
log_warn("Could not send ACCSREQ for `%s'", module->name);
return;
}
if (q->client->secret[0] != '\0' && module->secret != NULL &&
radius_get_user_password_attr(radpkt, pass, sizeof(pass),
q->client->secret) == 0) {
radius_del_attr_all(radpkt, RADIUS_TYPE_USER_PASSWORD);
(void)radius_put_raw_attr(radpkt, RADIUS_TYPE_USER_PASSWORD,
pass, strlen(pass));
}
pkt = radius_get_data(radpkt);
len = radius_get_length(radpkt);
memset(&accsreq, 0, sizeof(accsreq));
accsreq.q_id = q->id;
while (off < len) {
siz = MAX_IMSGSIZE - sizeof(accsreq);
if (len - off > siz) {
accsreq.final = false;
accsreq.datalen = siz;
} else {
accsreq.final = true;
accsreq.datalen = len - off;
}
iov[0].iov_base = &accsreq;
iov[0].iov_len = sizeof(accsreq);
iov[1].iov_base = (caddr_t)pkt + off;
iov[1].iov_len = accsreq.datalen;
imsg_composev(&module->ibuf, IMSG_RADIUSD_MODULE_ACCSREQ, 0, 0,
-1, iov, 2);
off += accsreq.datalen;
}
radiusd_module_reset_ev_handler(module);
radius_delete_packet(radpkt);
return;
}
|