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
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
|
/* $OpenBSD: radiusd.c,v 1.58 2024/11/21 13:17:02 claudio Exp $ */
/*
* Copyright (c) 2013, 2023 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 <netinet/in.h>
#include <arpa/inet.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
#include <event.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <imsg.h>
#include <netdb.h>
#include <paths.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 <radius.h>
#include "radiusd.h"
#include "radiusd_local.h"
#include "radius_subr.h"
#include "log.h"
#include "util.h"
#include "imsg_subr.h"
#include "control.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_listen_handle_packet(struct radiusd_listen *,
RADIUS_PACKET *, struct sockaddr *, socklen_t);
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 void raidus_query_access_request(struct radius_query *);
static void radius_query_access_response(struct radius_query *);
static void raidus_query_accounting_request(
struct radiusd_accounting *, struct radius_query *);
static void radius_query_accounting_response(
struct radius_query *);
static const char *radius_query_client_secret(struct radius_query *);
static const char *radius_code_string(int);
static const char *radius_acct_status_type_string(uint32_t);
static int radiusd_access_response_fixup(struct radius_query *,
struct radius_query *, bool);
static void radiusd_module_reset_ev_handler(
struct radiusd_module *);
static int radiusd_module_imsg_read(struct radiusd_module *);
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 *);
static void radiusd_module_start(struct radiusd_module *);
static 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 void radiusd_module_next_response(struct radiusd_module *,
struct radius_query *, RADIUS_PACKET *);
static void radiusd_module_request_decoration(
struct radiusd_module *, struct radius_query *);
static void radiusd_module_response_decoration(
struct radiusd_module *, struct radius_query *);
static void radiusd_module_account_request(struct radiusd_module *,
struct radius_query *);
static int imsg_compose_radius_packet(struct imsgbuf *,
uint32_t, u_int, RADIUS_PACKET *);
static void close_stdio(void);
static u_int radius_query_id_seq = 0;
int debug = 0;
struct radiusd *radiusd_s = NULL;
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, error;
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");
radiusd_s = radiusd;
TAILQ_INIT(&radiusd->listen);
TAILQ_INIT(&radiusd->query);
if (!noaction && debug == 0)
daemon(0, 1); /* pend closing stdio files */
if (parse_config(conffile, radiusd) != 0)
errx(EXIT_FAILURE, "config error");
log_init(debug);
if (noaction) {
fprintf(stderr, "configuration OK\n");
exit(EXIT_SUCCESS);
}
if (debug == 0)
close_stdio(); /* close stdio files now */
if (control_init(RADIUSD_SOCK) == -1)
exit(EXIT_FAILURE);
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");
if (control_listen() == -1)
exit(EXIT_FAILURE);
if (pledge("stdio inet", NULL) == -1)
err(EXIT_FAILURE, "pledge");
event_loop(0);
if (radiusd->error != 0)
log_warnx("exiting on error");
radiusd_stop(radiusd);
control_cleanup();
event_loop(0);
error = radiusd->error;
radiusd_free(radiusd);
event_base_free(NULL);
if (error != 0)
exit(EXIT_FAILURE);
else
exit(EXIT_SUCCESS);
}
static int
radiusd_start(struct radiusd *radiusd)
{
struct radiusd_listen *l;
struct radiusd_module *module;
int s, on;
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)) == -1) {
log_warn("Listen %s port %d is failed: socket()",
hbuf, (int)htons(l->addr.ipv4.sin_port));
goto on_error;
}
on = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))
== -1)
log_warn("%s: setsockopt(,,SO_REUSEADDR) failed: %m",
__func__);
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) {
if (debug > 0)
radiusd_module_set(module, "_debug", 0, NULL);
radiusd_module_start(module);
}
return (0);
on_error:
radiusd->error++;
event_loopbreak();
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;
struct radiusd_accounting *acct, *acctt;
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(acct, &radiusd->account, next, acctt) {
TAILQ_REMOVE(&radiusd->account, acct, next);
free(acct->secret);
free(acct->acct);
TAILQ_FOREACH_SAFE(modref, &acct->deco, next, modreft) {
TAILQ_REMOVE(&acct->deco, modref, next);
free(modref);
}
for (i = 0; acct->username[i] != NULL; i++)
free(acct->username[i]);
free(acct->username);
free(acct);
}
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 sz;
RADIUS_PACKET *packet = NULL;
struct sockaddr_storage peer;
socklen_t peersz;
struct radiusd_listen *listn = ctx;
static u_char buf[65535];
if (evmask & EV_READ) {
peersz = sizeof(peer);
if ((sz = recvfrom(listn->sock, buf, sizeof(buf), 0,
(struct sockaddr *)&peer, &peersz)) == -1) {
if (errno == EAGAIN)
return;
log_warn("%s: recvfrom() failed", __func__);
return;
}
RADIUSD_ASSERT(peer.ss_family == AF_INET ||
peer.ss_family == AF_INET6);
if ((packet = radius_convert_packet(buf, sz)) == NULL)
log_warn("%s: radius_convert_packet() failed",
__func__);
else
radiusd_listen_handle_packet(listn, packet,
(struct sockaddr *)&peer, peersz);
}
}
static void
radiusd_listen_handle_packet(struct radiusd_listen *listn,
RADIUS_PACKET *packet, struct sockaddr *peer, socklen_t peerlen)
{
int i, req_id, req_code;
static char username[256];
char peerstr[NI_MAXHOST + NI_MAXSERV + 30];
struct radiusd_authentication *authen;
struct radiusd_accounting *accounting;
struct radiusd_client *client;
struct radius_query *q = NULL;
uint32_t acct_status;
#define in(_x) (((struct sockaddr_in *)_x)->sin_addr)
#define in6(_x) (((struct sockaddr_in6 *)_x)->sin6_addr)
req_id = radius_get_id(packet);
req_code = radius_get_code(packet);
/* prepare some information about this messages */
if (addrport_tostring(peer, peerlen, peerstr, sizeof(peerstr)) ==
NULL) {
log_warn("%s: getnameinfo() failed", __func__);
goto on_error;
}
/*
* Find a matching `client' entry
*/
TAILQ_FOREACH(client, &listn->radiusd->client, next) {
if (client->af != peer->sa_family)
continue;
if (peer->sa_family == AF_INET && IPv4_cmp(
&in(peer), &client->addr, &client->mask))
break;
else if (peer->sa_family == AF_INET6 && IPv6_cmp(
&in6(peer), &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 request authenticator if accounting */
if (req_code == RADIUS_CODE_ACCOUNTING_REQUEST &&
radius_check_accounting_request_authenticator(packet,
client->secret) != 0) {
log_warnx("Received %s(code=%d) from %s id=%d: bad request "
"authenticator", radius_code_string(req_code), req_code,
peerstr, req_id);
goto on_error;
}
/* Check the client's Message-Authenticator */
if (client->msgauth_required && !listn->accounting &&
!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->sa_family == q->clientaddr.ss_family &&
((peer->sa_family == AF_INET && in(&q->clientaddr).s_addr ==
in(peer).s_addr) || (peer->sa_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: duplicated "
"with q=%u", radius_code_string(req_code), req_code,
peerstr, req_id, q->id);
q = NULL;
goto on_error;
}
if ((q = calloc(1, sizeof(struct radius_query))) == NULL) {
log_warn("%s: Out of memory", __func__);
goto on_error;
}
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);
} else
strlcpy(q->username, username, sizeof(q->username));
q->id = ++radius_query_id_seq;
q->radiusd = listn->radiusd;
q->clientaddrlen = peerlen;
memcpy(&q->clientaddr, peer, peerlen);
q->listen = listn;
q->req = packet;
q->client = client;
q->req_id = req_id;
radius_get_authenticator(packet, q->req_auth);
packet = NULL;
TAILQ_INSERT_TAIL(&listn->radiusd->query, q, next);
switch (req_code) {
case RADIUS_CODE_ACCESS_REQUEST:
if (listn->accounting) {
log_info("Received %s(code=%d) from %s id=%d: "
"ignored because the port is for authentication",
radius_code_string(req_code), req_code, peerstr,
req_id);
break;
}
/*
* Find a matching `authenticate' entry
*/
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;
}
}
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;
}
q->authen = authen;
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;
}
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);
raidus_query_access_request(q);
return;
case RADIUS_CODE_ACCOUNTING_REQUEST:
if (!listn->accounting) {
log_info("Received %s(code=%d) from %s id=%d: "
"ignored because the port is for accounting",
radius_code_string(req_code), req_code, peerstr,
req_id);
break;
}
if (radius_get_uint32_attr(q->req, RADIUS_TYPE_ACCT_STATUS_TYPE,
&acct_status) != 0)
acct_status = 0;
/*
* Find a matching `accounting' entry
*/
TAILQ_FOREACH(accounting, &listn->radiusd->account, next) {
if (acct_status == RADIUS_ACCT_STATUS_TYPE_ACCT_ON ||
acct_status == RADIUS_ACCT_STATUS_TYPE_ACCT_OFF) {
raidus_query_accounting_request(accounting, q);
continue;
}
for (i = 0; accounting->username[i] != NULL; i++) {
if (fnmatch(accounting->username[i], username,
0) == 0)
break;
}
if (accounting->username[i] == NULL)
continue;
raidus_query_accounting_request(accounting, q);
if (accounting->quick)
break;
}
/* pass NULL to hadnle this self without module */
raidus_query_accounting_request(NULL, q);
if ((q->res = radius_new_response_packet(
RADIUS_CODE_ACCOUNTING_RESPONSE, q->req)) == NULL)
log_warn("%s: radius_new_response_packet() failed",
__func__);
else
radius_query_accounting_response(q);
break;
default:
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));
break;
}
on_error:
if (packet != NULL)
radius_delete_packet(packet);
if (q != NULL)
radiusd_access_request_aborted(q);
#undef in
#undef in6
}
static void
raidus_query_access_request(struct radius_query *q)
{
struct radiusd_authentication *authen = q->authen;
/* first or next request decoration */
for (;;) {
if (q->deco == NULL)
q->deco = TAILQ_FIRST(&q->authen->deco);
else
q->deco = TAILQ_NEXT(q->deco, next);
if (q->deco == NULL || MODULE_DO_REQDECO(q->deco->module))
break;
}
if (q->deco != NULL)
radiusd_module_request_decoration(q->deco->module, q);
else {
RADIUSD_ASSERT(authen->auth != NULL);
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);
}
}
static void
radius_query_access_response(struct radius_query *q)
{
int sz, res_id, res_code;
char buf[NI_MAXHOST + NI_MAXSERV + 30];
struct radius_query *q_last, *q0;
q_last = q;
next:
/* first or next response decoration */
for (;;) {
if (q->deco == NULL)
q->deco = TAILQ_FIRST(&q->authen->deco);
else
q->deco = TAILQ_NEXT(q->deco, next);
if (q->deco == NULL || MODULE_DO_RESDECO(q->deco->module))
break;
}
if (q->deco != NULL) {
radiusd_module_response_decoration(q->deco->module, q);
return;
}
if (q->prev != NULL) {
if (MODULE_DO_NEXTRES(q->prev->authen->auth->module)) {
if (radiusd_access_response_fixup(q->prev, q_last, 0)
!= 0)
goto on_error;
q0 = q;
q = q->prev;
/* dissolve the relation */
q0->prev = NULL;
q->hasnext = false;
radiusd_module_next_response(q->authen->auth->module,
q, q_last->res);
radiusd_access_request_aborted(q0);
return;
}
q = q->prev;
goto next;
}
if (radiusd_access_response_fixup(q, q_last, 1) != 0)
goto on_error;
res_id = radius_get_id(q->res);
res_code = radius_get_code(q->res);
/* Reset response/message authenticator */
if (radius_has_attr(q->res, RADIUS_TYPE_MESSAGE_AUTHENTICATOR))
radius_del_attr_all(q->res, RADIUS_TYPE_MESSAGE_AUTHENTICATOR);
radius_put_message_authenticator(q->res, q->client->secret);
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);
}
static void
raidus_query_accounting_request(struct radiusd_accounting *accounting,
struct radius_query *q)
{
int req_code;
uint32_t acct_status;
char buf0[NI_MAXHOST + NI_MAXSERV + 30];
if (accounting != NULL) {
/* handle by the module */
if (MODULE_DO_ACCTREQ(accounting->acct->module))
radiusd_module_account_request(accounting->acct->module,
q);
return;
}
req_code = radius_get_code(q->req);
if (radius_get_uint32_attr(q->req, RADIUS_TYPE_ACCT_STATUS_TYPE,
&acct_status) != 0)
acct_status = 0;
log_info("Received %s(code=%d) type=%s(%lu) from %s id=%d username=%s "
"q=%u", radius_code_string(req_code), req_code,
radius_acct_status_type_string(acct_status), (unsigned long)
acct_status, addrport_tostring((struct sockaddr *)&q->clientaddr,
q->clientaddrlen, buf0, sizeof(buf0)), q->req_id, q->username,
q->id);
}
static void
radius_query_accounting_response(struct radius_query *q)
{
int sz, res_id, res_code;
char buf[NI_MAXHOST + NI_MAXSERV + 30];
radius_set_response_authenticator(q->res, q->client->secret);
res_id = radius_get_id(q->res);
res_code = radius_get_code(q->res);
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");
}
static const char *
radius_query_client_secret(struct radius_query *q)
{
struct radius_query *q0;
const char *client_secret = NULL;
for (q0 = q; q0 != NULL && client_secret == NULL; q0 = q0->prev) {
if (q0->client != NULL)
client_secret = q0->client->secret;
}
RADIUSD_ASSERT(client_secret != NULL);
return (client_secret);
}
/***********************************************************************
* Callback functions from the modules
***********************************************************************/
void
radiusd_access_request_answer(struct radius_query *q)
{
radius_set_request_packet(q->res, q->req);
RADIUSD_ASSERT(q->deco == NULL);
radius_query_access_response(q);
}
void
radiusd_access_request_next(struct radius_query *q, RADIUS_PACKET *pkt)
{
struct radius_query *q_next = NULL;
static char username[256];
struct radiusd_authentication *authen;
int i;
RADIUSD_ASSERT(q->deco == NULL);
if (!q->authen->isfilter) {
log_warnx("q=%u `%s' requested next authentication, but it's "
"not authentication-filter", q->id,
q->authen->auth->module->name);
goto on_error;
}
if (radius_get_string_attr(pkt, RADIUS_TYPE_USER_NAME, username,
sizeof(username)) != 0)
username[0] = '\0';
for (authen = TAILQ_NEXT(q->authen, next); authen != NULL;
authen = TAILQ_NEXT(authen, next)) {
for (i = 0; authen->username[i] != NULL; i++) {
if (fnmatch(authen->username[i], username, 0)
== 0)
goto found;
}
}
found:
if (authen == NULL) { /* no more authentication */
log_warnx("q=%u module `%s' requested next authentication "
"no more `authenticate' matches", q->id,
q->authen->auth->module->name);
goto on_error;
}
if ((q_next = calloc(1, sizeof(struct radius_query))) == NULL) {
log_warn("%s: q=%u calloc: %m", __func__, q->id);
goto on_error;
}
q_next->id = ++radius_query_id_seq;
q_next->radiusd = q->radiusd;
q_next->req_id = q->req_id;
q_next->req = pkt;
radius_get_authenticator(pkt, q_next->req_auth);
q_next->authen = authen;
q_next->prev = q;
q->hasnext = true;
strlcpy(q_next->username, username, sizeof(q_next->username));
TAILQ_INSERT_TAIL(&q->radiusd->query, q_next, next);
raidus_query_access_request(q_next);
return;
on_error:
RADIUSD_ASSERT(q_next == NULL);
radius_delete_packet(pkt);
radiusd_access_request_aborted(q);
}
void
radiusd_access_request_aborted(struct radius_query *q)
{
if (q->hasnext) /* don't abort if filtering */
return;
if (q->prev != NULL) {
q->prev->hasnext = false;
radiusd_access_request_aborted(q->prev);
}
if (q->req != NULL)
radius_delete_packet(q->req);
if (q->res != NULL)
radius_delete_packet(q->res);
TAILQ_REMOVE(&q->radiusd->query, q, next);
free(q);
}
/***********************************************************************
* Signal handlers
***********************************************************************/
static void
radiusd_on_sigterm(int fd, short evmask, void *ctx)
{
log_info("Received SIGTERM");
event_loopbreak();
}
static void
radiusd_on_sigint(int fd, short evmask, void *ctx)
{
log_info("Received SIGINT");
event_loopbreak();
}
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, ndeath = 0;
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));
ndeath++;
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));
}
}
if (ndeath > 0) {
radiusd->error++;
event_loopbreak();
}
}
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-Client" },
{ -1, NULL }
};
for (i = 0; codestrings[i].code != -1; i++)
if (codestrings[i].code == code)
return (codestrings[i].string);
return ("Unknown");
}
static const char *
radius_acct_status_type_string(uint32_t type)
{
int i;
struct _typestrings {
uint32_t type;
const char *string;
} typestrings[] = {
{ RADIUS_ACCT_STATUS_TYPE_START, "Start" },
{ RADIUS_ACCT_STATUS_TYPE_STOP, "Stop" },
{ RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE, "Interim-Update" },
{ RADIUS_ACCT_STATUS_TYPE_ACCT_ON, "Accounting-On" },
{ RADIUS_ACCT_STATUS_TYPE_ACCT_OFF, "Accounting-Off" },
{ -1, NULL }
};
for (i = 0; typestrings[i].string != NULL; i++)
if (typestrings[i].type == type)
return (typestrings[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->account);
TAILQ_INIT(&conf->client);
return;
}
/*
* Fix some attributes which depend the secret value.
*/
static int
radiusd_access_response_fixup(struct radius_query *q, struct radius_query *q0,
bool islast)
{
int res_id;
size_t attrlen;
u_char authen_req_auth[16], attrbuf[256];
const char *client_req_auth;
const char *authen_secret, *client_secret;
authen_secret = q0->authen->auth->module->secret;
client_secret = (islast)? q->client->secret :
q->authen->auth->module->secret;
radius_get_authenticator(q0->req, authen_req_auth);
client_req_auth = q->req_auth;
if (client_secret == NULL && authen_secret == NULL)
return (0);
if (!(authen_secret != NULL && client_secret != NULL &&
strcmp(authen_secret, client_secret) == 0 &&
timingsafe_bcmp(authen_req_auth, client_req_auth, 16) == 0)) {
/* RFC 2865 Tunnel-Password */
attrlen = sizeof(attrbuf);
if (radius_get_raw_attr(q0->res, RADIUS_TYPE_TUNNEL_PASSWORD,
attrbuf, &attrlen) == 0) {
if (authen_secret != NULL)
radius_attr_unhide(authen_secret,
authen_req_auth, attrbuf, attrbuf + 3,
attrlen - 3);
if (client_secret != NULL)
radius_attr_hide(client_secret, client_req_auth,
attrbuf, attrbuf + 3, attrlen - 3);
radius_del_attr_all(q0->res,
RADIUS_TYPE_TUNNEL_PASSWORD);
radius_put_raw_attr(q0->res,
RADIUS_TYPE_TUNNEL_PASSWORD, attrbuf, attrlen);
}
/* RFC 2548 Microsoft MPPE-{Send,Recv}-Key */
attrlen = sizeof(attrbuf);
if (radius_get_vs_raw_attr(q0->res, RADIUS_VENDOR_MICROSOFT,
RADIUS_VTYPE_MPPE_SEND_KEY, attrbuf, &attrlen) == 0) {
if (authen_secret != NULL)
radius_attr_unhide(authen_secret,
authen_req_auth, attrbuf, attrbuf + 2,
attrlen - 2);
if (client_secret != NULL)
radius_attr_hide(client_secret, client_req_auth,
attrbuf, attrbuf + 2, attrlen - 2);
radius_del_vs_attr_all(q0->res, RADIUS_VENDOR_MICROSOFT,
RADIUS_VTYPE_MPPE_SEND_KEY);
radius_put_vs_raw_attr(q0->res, RADIUS_VENDOR_MICROSOFT,
RADIUS_VTYPE_MPPE_SEND_KEY, attrbuf, attrlen);
}
attrlen = sizeof(attrbuf);
if (radius_get_vs_raw_attr(q0->res, RADIUS_VENDOR_MICROSOFT,
RADIUS_VTYPE_MPPE_RECV_KEY, attrbuf, &attrlen) == 0) {
if (authen_secret != NULL)
radius_attr_unhide(authen_secret,
authen_req_auth, attrbuf, attrbuf + 2,
attrlen - 2);
if (client_secret != NULL)
radius_attr_hide(client_secret, client_req_auth,
attrbuf, attrbuf + 2, attrlen - 2);
radius_del_vs_attr_all(q0->res, RADIUS_VENDOR_MICROSOFT,
RADIUS_VTYPE_MPPE_RECV_KEY);
radius_put_vs_raw_attr(q0->res, RADIUS_VENDOR_MICROSOFT,
RADIUS_VTYPE_MPPE_RECV_KEY, attrbuf, attrlen);
}
}
res_id = radius_get_id(q0->res);
if (res_id != q->req_id) {
/* authentication server change the id */
radius_set_id(q0->res, q->req_id);
}
return (0);
}
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);
}
int
radiusd_imsg_compose_module(struct radiusd *radiusd, const char *module_name,
uint32_t type, uint32_t id, pid_t pid, int fd, void *data, size_t datalen)
{
struct radiusd_module *module;
TAILQ_FOREACH(module, &radiusd_s->module, next) {
if (strcmp(module->name, module_name) == 0)
break;
}
if (module == NULL ||
(module->capabilities & RADIUSD_MODULE_CAP_CONTROL) == 0 ||
module->fd < 0)
return (-1);
if (imsg_compose(&module->ibuf, type, id, pid, fd, data,
datalen) == -1)
return (-1);
radiusd_module_reset_ev_handler(module);
return (0);
}
/***********************************************************************
* 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)) == -1) {
log_warn("Could not load module `%s': fcntl(F_GETFL)",
name);
goto on_error;
}
if (fcntl(module->fd, F_SETFL, ival | O_NONBLOCK) == -1) {
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;
imsgbuf_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 unknown 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;
if (module->secret != NULL) {
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);
imsgbuf_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;
if (evmask & EV_WRITE) {
module->writeready = true;
if (imsgbuf_write(&module->ibuf) == -1) {
log_warn("Failed to write to module `%s': "
"imsgbuf_write()", module->name);
goto on_error;
}
module->writeready = false;
}
if (evmask & EV_READ) {
if (radiusd_module_imsg_read(module) == -1)
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 */
}
/* 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)
{
int n;
struct imsg imsg;
if ((n = imsgbuf_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: imsgbuf_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);
imsg_free(&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,
radius_query_client_secret(q));
radiusd_access_request_answer(q);
}
break;
}
case IMSG_RADIUSD_MODULE_ACCSREQ_ANSWER:
case IMSG_RADIUSD_MODULE_ACCSREQ_NEXT:
case IMSG_RADIUSD_MODULE_REQDECO_DONE:
case IMSG_RADIUSD_MODULE_RESDECO_DONE:
{
static struct radiusd_module_radpkt_arg *ans;
const char *typestr = "unknown";
switch (imsg->hdr.type) {
case IMSG_RADIUSD_MODULE_ACCSREQ_ANSWER:
typestr = "ACCSREQ_ANSWER";
break;
case IMSG_RADIUSD_MODULE_ACCSREQ_NEXT:
typestr = "ACCSREQ_NEXT";
break;
case IMSG_RADIUSD_MODULE_REQDECO_DONE:
typestr = "REQDECO_DONE";
break;
case IMSG_RADIUSD_MODULE_RESDECO_DONE:
typestr = "RESDECO_DONE";
break;
}
if (datalen <
(ssize_t)sizeof(struct radiusd_module_radpkt_arg)) {
log_warnx("Received %s message, but length is wrong",
typestr);
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 %s from %s, but query id=%u "
"unknown", typestr, module->name, q_id);
break;
}
if ((ans = radiusd_module_recv_radpkt(module, imsg,
imsg->hdr.type, typestr)) != NULL) {
RADIUS_PACKET *radpkt = NULL;
if (module->radpktoff > 0 &&
(radpkt = radius_convert_packet(
module->radpkt, module->radpktoff)) == NULL) {
log_warn("q=%u radius_convert_packet() failed",
q->id);
radiusd_access_request_aborted(q);
break;
}
module->radpktoff = 0;
switch (imsg->hdr.type) {
case IMSG_RADIUSD_MODULE_REQDECO_DONE:
if (q->deco == NULL || q->deco->type !=
IMSG_RADIUSD_MODULE_REQDECO) {
log_warnx("q=%u received %s but not "
"requested", q->id, typestr);
if (radpkt != NULL)
radius_delete_packet(radpkt);
break;
}
if (radpkt != NULL) {
radius_delete_packet(q->req);
q->req = radpkt;
}
raidus_query_access_request(q);
break;
case IMSG_RADIUSD_MODULE_ACCSREQ_ANSWER:
if (radpkt == NULL) {
log_warnx("q=%u wrong pkt from module",
q->id);
radiusd_access_request_aborted(q);
break;
}
q->res = radpkt;
radiusd_access_request_answer(q);
break;
case IMSG_RADIUSD_MODULE_ACCSREQ_NEXT:
if (radpkt == NULL) {
log_warnx("q=%u wrong pkt from module",
q->id);
radiusd_access_request_aborted(q);
break;
}
radiusd_access_request_next(q, radpkt);
break;
case IMSG_RADIUSD_MODULE_RESDECO_DONE:
if (q->deco == NULL || q->deco->type !=
IMSG_RADIUSD_MODULE_RESDECO) {
log_warnx("q=%u received %s but not "
"requested", q->id, typestr);
if (radpkt != NULL)
radius_delete_packet(radpkt);
break;
}
if (radpkt != NULL) {
radius_delete_packet(q->res);
radius_set_request_packet(radpkt,
q->req);
q->res = radpkt;
}
radius_query_access_response(q);
break;
}
}
break;
}
case IMSG_RADIUSD_MODULE_ACCSREQ_ABORTED:
{
if (datalen < (ssize_t)sizeof(u_int)) {
log_warnx("Received ACCSREQ_ABORTED message, but "
"length is wrong");
break;
}
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;
}
case IMSG_RADIUSD_MODULE_CTRL_BIND:
control_conn_bind(imsg->hdr.peerid, module->name);
break;
default:
if (imsg->hdr.peerid != 0)
control_imsg_relay(imsg);
else
RADIUSD_DBG(("Unhandled imsg type=%d from %s",
imsg->hdr.type, module->name));
}
}
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->pktlen) {
u_char *nradpkt;
if ((nradpkt = realloc(module->radpkt, ans->pktlen)) == NULL) {
log_warn("Could not handle received %s message from "
"`%s'", type_str, module->name);
goto on_fail;
}
module->radpkt = nradpkt;
module->radpktsiz = ans->pktlen;
}
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;
}
if (chunklen > 0) {
memcpy(module->radpkt + module->radpktoff,
(caddr_t)(ans + 1), chunklen);
module->radpktoff += chunklen;
}
if (!ans->final)
return (NULL); /* again */
if (module->radpktoff != ans->pktlen) {
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), radius_query_client_secret(q)) == 0)
userpass.has_pass = true;
else
userpass.has_pass = false;
if (radius_get_string_attr(q->req, RADIUS_TYPE_USER_NAME,
userpass.user, sizeof(userpass.user)) != 0) {
log_warnx("q=%u no User-Name attribute", q->id);
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)
{
RADIUS_PACKET *radpkt;
char pass[256];
if ((radpkt = radius_convert_packet(radius_get_data(q->req),
radius_get_length(q->req))) == NULL) {
log_warn("q=%u Could not send ACCSREQ to `%s'", q->id,
module->name);
radiusd_access_request_aborted(q);
return;
}
if (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));
}
if (imsg_compose_radius_packet(&module->ibuf,
IMSG_RADIUSD_MODULE_ACCSREQ, q->id, radpkt) == -1) {
log_warn("q=%u Could not send ACCSREQ to `%s'", q->id,
module->name);
radiusd_access_request_aborted(q);
}
radiusd_module_reset_ev_handler(module);
radius_delete_packet(radpkt);
}
static void
radiusd_module_next_response(struct radiusd_module *module,
struct radius_query *q, RADIUS_PACKET *pkt)
{
if (imsg_compose_radius_packet(&module->ibuf,
IMSG_RADIUSD_MODULE_NEXTRES, q->id, pkt) == -1) {
log_warn("q=%u Could not send NEXTRES to `%s'", q->id,
module->name);
radiusd_access_request_aborted(q);
}
radiusd_module_reset_ev_handler(module);
}
static void
radiusd_module_request_decoration(struct radiusd_module *module,
struct radius_query *q)
{
if (module->fd < 0) {
log_warnx("q=%u Could not send REQDECO to `%s': module is "
"not running?", q->id, module->name);
radiusd_access_request_aborted(q);
return;
}
if (imsg_compose_radius_packet(&module->ibuf,
IMSG_RADIUSD_MODULE_REQDECO, q->id, q->req) == -1) {
log_warn("q=%u Could not send REQDECO to `%s'", q->id,
module->name);
radiusd_access_request_aborted(q);
return;
}
RADIUSD_ASSERT(q->deco != NULL);
q->deco->type = IMSG_RADIUSD_MODULE_REQDECO;
radiusd_module_reset_ev_handler(module);
}
static void
radiusd_module_response_decoration(struct radiusd_module *module,
struct radius_query *q)
{
if (module->fd < 0) {
log_warnx("q=%u Could not send RESDECO to `%s': module is "
"not running?", q->id, module->name);
radiusd_access_request_aborted(q);
return;
}
if (imsg_compose_radius_packet(&module->ibuf,
IMSG_RADIUSD_MODULE_RESDECO0_REQ, q->id, q->req) == -1) {
log_warn("q=%u Could not send RESDECO0_REQ to `%s'", q->id,
module->name);
radiusd_access_request_aborted(q);
return;
}
if (imsg_compose_radius_packet(&module->ibuf,
IMSG_RADIUSD_MODULE_RESDECO, q->id, q->res) == -1) {
log_warn("q=%u Could not send RESDECO to `%s'", q->id,
module->name);
radiusd_access_request_aborted(q);
return;
}
RADIUSD_ASSERT(q->deco != NULL);
q->deco->type = IMSG_RADIUSD_MODULE_RESDECO;
radiusd_module_reset_ev_handler(module);
}
static void
radiusd_module_account_request(struct radiusd_module *module,
struct radius_query *q)
{
RADIUS_PACKET *radpkt;
if ((radpkt = radius_convert_packet(radius_get_data(q->req),
radius_get_length(q->req))) == NULL) {
log_warn("q=%u Could not send ACCSREQ to `%s'", q->id,
module->name);
radiusd_access_request_aborted(q);
return;
}
if (imsg_compose_radius_packet(&module->ibuf,
IMSG_RADIUSD_MODULE_ACCTREQ, q->id, radpkt) == -1) {
log_warn("q=%u Could not send ACCTREQ to `%s'", q->id,
module->name);
radiusd_access_request_aborted(q);
}
radiusd_module_reset_ev_handler(module);
radius_delete_packet(radpkt);
}
static int
imsg_compose_radius_packet(struct imsgbuf *ibuf, uint32_t type, u_int q_id,
RADIUS_PACKET *radpkt)
{
struct radiusd_module_radpkt_arg arg;
int off = 0, len, siz;
struct iovec iov[2];
const u_char *pkt;
pkt = radius_get_data(radpkt);
len = radius_get_length(radpkt);
memset(&arg, 0, sizeof(arg));
arg.q_id = q_id;
arg.pktlen = len;
while (off < len) {
siz = MAX_IMSGSIZE - sizeof(arg);
if (len - off > siz)
arg.final = false;
else {
arg.final = true;
siz = len - off;
}
iov[0].iov_base = &arg;
iov[0].iov_len = sizeof(arg);
iov[1].iov_base = (caddr_t)pkt + off;
iov[1].iov_len = siz;
if (imsg_composev(ibuf, type, 0, 0, -1, iov, 2) == -1)
return (-1);
off += siz;
}
return (0);
}
static void
close_stdio(void)
{
int fd;
if ((fd = open(_PATH_DEVNULL, O_RDWR)) != -1) {
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if (fd > STDERR_FILENO)
close(fd);
}
}
/***********************************************************************
* imsg_event
***********************************************************************/
struct iovec;
void
imsg_event_add(struct imsgev *iev)
{
iev->events = EV_READ;
if (iev->ibuf.w.queued)
iev->events |= EV_WRITE;
event_del(&iev->ev);
event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
event_add(&iev->ev, NULL);
}
int
imsg_compose_event(struct imsgev *iev, uint32_t type, uint32_t peerid,
pid_t pid, int fd, void *data, size_t datalen)
{
int ret;
if ((ret = imsg_compose(&iev->ibuf, type, peerid,
pid, fd, data, datalen)) != -1)
imsg_event_add(iev);
return (ret);
}
int
imsg_composev_event(struct imsgev *iev, uint32_t type, uint32_t peerid,
pid_t pid, int fd, struct iovec *iov, int niov)
{
int ret;
if ((ret = imsg_composev(&iev->ibuf, type, peerid,
pid, fd, iov, niov)) != -1)
imsg_event_add(iev);
return (ret);
}
|