summaryrefslogtreecommitdiff
path: root/src/pam_radius_auth.c
blob: 5bf93ea8d54c4cda53935b4e2bc36db174586f91 (plain)
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
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
/*
 * pam_radius_auth
 *      Authenticate a user via a RADIUS session
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * The original pam_radius.c code is copyright (c) Cristian Gafton, 1996,
 *                                             <gafton@redhat.com>
 *
 * Some challenge-response code is copyright (c) CRYPTOCard Inc, 1998.
 *                                              All rights reserved.
 *
 * Copyright (C) 2017, 2018 Cumulus Networks, Inc.
 * All rights reserved.
 */

#define PAM_SM_AUTH
#define PAM_SM_PASSWORD
#define PAM_SM_SESSION

#include "pam_radius_auth.h"

#define DPRINT if (debug || cfg_debug) _pam_log

static int cfg_debug;
static int cleaned_up;

/* logging */
static void _pam_log(pam_handle_t * pamh, int err, CONST char *format, ...)
{
	va_list args;

	va_start(args, format);
	pam_vsyslog(pamh, err, format, args);
	va_end(args);

}

/*  base config, plus config file, but no pam cmdline */
static radius_conf_t savconf = {.min_priv_lvl = 15,	/* default priv level */
	.conf_file = CONF_FILE,	/* default config file */
	.prompt = DEFAULT_PROMPT,	/*  default prompt */
};

/* pam cmdline argument parsing */
static int _pam_parse(pam_handle_t * pamh, int argc, CONST char **argv,
		      radius_conf_t * conf)
{
	int ctrl = 0;

	*conf = savconf;	/* initialze from the static config */

	/*
	 *      If either is not there, then we can't parse anything.
	 */
	if ((argc == 0) || (argv == NULL))
		return ctrl;

	/* step through arguments */
	for (ctrl = 0; argc-- > 0; ++argv) {

		/* generic options */
		if (!strncmp(*argv, "conf=", 5)) {
			conf->conf_file = *argv + 5;

		} else if (!strcmp(*argv, "use_first_pass")) {
			ctrl |= PAM_USE_FIRST_PASS;

		} else if (!strcmp(*argv, "try_first_pass")) {
			ctrl |= PAM_TRY_FIRST_PASS;

		} else if (!strcmp(*argv, "skip_passwd")) {
			ctrl |= PAM_SKIP_PASSWD;

		} else if (!strncmp(*argv, "retry=", 6)) {
			conf->retries = atoi(*argv + 6);

		} else if (!strcmp(*argv, "localifdown")) {
			conf->localifdown = 1;

		} else if (!strncmp(*argv, "client_id=", 10)) {
			if (conf->client_id) {
				_pam_log(pamh, LOG_WARNING, "ignoring duplicate"
					 " '%s'", *argv);
			} else {
				conf->client_id = (char *)*argv + 10;	/* point to the client-id */
			}
		} else if (!strcmp(*argv, "accounting_bug")) {
			conf->accounting_bug = TRUE;

		} else if (!strcmp(*argv, "ruser")) {
			ctrl |= PAM_RUSER_ARG;

		} else if (!strcmp(*argv, "debug")) {
			ctrl |= PAM_DEBUG_ARG;
			conf->debug = TRUE;

		} else if (!strncmp(*argv, "prompt=", 7)) {
			if (!strncmp
			    (conf->prompt, (char *)*argv + 7, MAXPROMPT)) {
				_pam_log(pamh, LOG_WARNING,
					 "ignoring duplicate" " '%s'", *argv);
			} else {
				/* truncate excessive prompts to (MAXPROMPT - 3) length */
				if (strlen((char *)*argv + 7) >=
				    (MAXPROMPT - 3)) {
					*((char *)*argv + 7 + (MAXPROMPT - 3)) =
					    0;
				}
				/* set the new prompt */
				memset(conf->prompt, 0, sizeof(conf->prompt));
				snprintf(conf->prompt, MAXPROMPT, "%s: ",
					 (char *)*argv + 7);
			}

		} else if (!strcmp(*argv, "force_prompt")) {
			conf->force_prompt = TRUE;

		} else if (!strncmp(*argv, "max_challenge=", 14)) {
			conf->max_challenge = atoi(*argv + 14);

		} else {
			_pam_log(pamh, LOG_WARNING, "unrecognized option '%s'",
				 *argv);
		}
	}

	return ctrl;
}

/* Callback function used to free the saved return value for pam_setcred. */
static void _int_free(pam_handle_t * pamh, void *x, int error_status)
{
	free(x);
}

/*************************************************************************
 * SMALL HELPER FUNCTIONS
 *************************************************************************/

/* useful for debugging, and maybe some user messages.  */
__attribute__ ((unused))
static char *ai_ntop(const struct sockaddr *sa)
{
	static char server_address[INET6_ADDRSTRLEN + 16];

	switch (sa->sa_family) {
	case AF_INET:
		inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr),
			  server_address, INET_ADDRSTRLEN);

		snprintf(server_address + strlen(server_address), 14, ":%hu",
			 htons(((struct sockaddr_in *)sa)->sin_port));
		break;

	case AF_INET6:
		inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr),
			  server_address, INET6_ADDRSTRLEN);

		snprintf(server_address + strlen(server_address), 14, ":%hu",
			 htons(((struct sockaddr_in6 *)sa)->sin6_port));
		break;

	default:
		strcpy(server_address, "Unknown AF");
	}
	return server_address;
}

/*
 * Return an IP address as a struct sockaddr *.
 */
static int get_ipaddr(char *host, struct sockaddr *addr, char *port)
{
	struct addrinfo hints;
	struct addrinfo *results;
	int r;

	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_DGRAM;
	hints.ai_flags = AI_ADDRCONFIG;

	r = getaddrinfo(host, port && port[0] ? port : NULL, &hints, &results);
	if (r == 0) {
		memcpy(addr, results->ai_addr, results->ai_addrlen);
		freeaddrinfo(results);
	}

	return r;
}

/*
 * take server->hostname, and convert it to server->ip
 * Done once when server list parsed.  The last part, the
 * if port isn't set in config, it needs to be set to either
 * radius or raddacct
 */
static int host2server(pam_handle_t * pamh, radius_server_t * server)
{
	char hostbuffer[256], tmpn[11];
	char *hostname, *portstart, *p, *port = NULL, *portacct = NULL;
	int retval, retvala;

	/* hostname might be [ipv6::address] */
	strncpy(hostbuffer, server->hostname, sizeof(hostbuffer) - 1);
	hostbuffer[sizeof(hostbuffer) - 1] = 0;
	hostname = hostbuffer;
	portstart = hostbuffer;
	if (hostname[0] == '[') {
		if ((p = strchr(hostname, ']')) != NULL) {
			hostname++;
			*p++ = 0;
			portstart = p;
		}
	}
	if ((port = strchr(portstart, ':')) != NULL)
		*port++ = '\0';

	/*
	 * Use the configured port if set, otherwise if accounting,
	 * "radacct", else "radius".  If configured port is numeric,
	 * and we are doing accounting, add 1.
	 * if they specified a name, and it's "radius", use "radacct"
	 * for the accounting port.  Otherwise, warn them that we'll
	 * have problems with accounting.
	 */
	if (port) {
		if (isdigit((unsigned char)*port)) {
			int n;
			/*
			 * normal accounting port 1 higher than auth port, so
			 * assume that's true if they gave a numeric
			 * port, and add 1 to it, when doing accounting
			 */
			if (sscanf(port, "%d", &n) == 1) {
				snprintf(tmpn, sizeof(tmpn), "%d", n + 1);
				portacct = tmpn;
			}
		} else {
			portacct = "radacct";
			if (strcmp(port, "radius"))
				_pam_log(pamh, LOG_WARNING, "Server %s uses"
					 " non-standard port '%s', using %s for"
					 " accounting", server->hostname, port,
					 portacct);
		}
	}
	if (!port)
		port = "radius";
	if (!portacct)
		portacct = "radacct";

	server->hostpart = strdup(hostname);
	if (!server->hostpart)
		_pam_log(pamh, LOG_ERR, "Memory allocation error saving"
			 " hostname %s in server %s info: %m", hostname,
			 server->hostname);

	/*  set up sockaddr for sendto calls  */
	server->ip = (struct sockaddr *)&server->ip_storage;
	server->ip_acct = (struct sockaddr *)&server->ipacct_storage;

	retval = get_ipaddr(server->hostpart, server->ip, port);
	if (retval) {
		_pam_log(pamh, LOG_WARNING,
			 "Failed looking up IP address for"
			 " server %s port %s (error=%s)",
			 server->hostpart, port, gai_strerror(retval));
		server->family = AF_INET;	/* assume, for sanity */
	} else
		server->family = server->ip->sa_family;

	retvala = get_ipaddr(server->hostpart, server->ip_acct, portacct);
	if (retvala)
		_pam_log(pamh, LOG_WARNING,
			 "Failed looking up IP address for"
			 " accounting server %s port %s (error=%s)",
			 server->hostpart, portacct, gai_strerror(retval));
	return retval + retvala;
}

/*
 * Do XOR of two buffers.
 */
static unsigned char *xor(unsigned char *p, unsigned char *q, int length)
{
	int i;
	unsigned char *retval = p;

	for (i = 0; i < length; i++) {
		*(p++) ^= *(q++);
	}
	return retval;
}

/**************************************************************************
 * MID-LEVEL RADIUS CODE
 **************************************************************************/

/*
 * get a pseudo-random vector.
 */
static void get_random_vector(unsigned char *vector)
{
#ifdef linux
	int fd = open("/dev/urandom", O_RDONLY);	/* Linux: get *real* random numbers */
	int total = 0;
	if (fd >= 0) {
		while (total < AUTH_VECTOR_LEN) {
			int bytes =
			    read(fd, vector + total, AUTH_VECTOR_LEN - total);
			if (bytes <= 0)
				break;	/* oops! Error */
			total += bytes;
		}
		close(fd);
	}

	if (total != AUTH_VECTOR_LEN)
#endif
	{			/* do this *always* on other platforms */
		MD5_CTX my_md5;
		struct timeval tv;
		struct timezone tz;
		static unsigned int session = 0;	/* make the number harder to guess */

		/* Use the time of day with the best resolution the system can
		   give us -- often close to microsecond accuracy. */
		gettimeofday(&tv, &tz);

		if (session == 0) {
			session = getppid();	/* (possibly) hard to guess information */
		}

		tv.tv_sec ^= getpid() * session++;

		/* Hash things to get maybe cryptographically strong pseudo-random numbers */
		MD5Init(&my_md5);
		MD5Update(&my_md5, (unsigned char *)&tv, sizeof(tv));
		MD5Update(&my_md5, (unsigned char *)&tz, sizeof(tz));
		MD5Final(vector, &my_md5);	/* set the final vector */
	}
}

/*
 * RFC 2139 says to do generate the accounting request vector this way.
 * However, the Livingston 1.16 server doesn't check it.	The Cistron
 * server (http://home.cistron.nl/~miquels/radius/) does, and this code
 * seems to work with it.	It also works with Funk's Steel-Belted RADIUS.
 */
static void get_accounting_vector(AUTH_HDR * request, radius_server_t * server)
{
	MD5_CTX my_md5;
	int secretlen = strlen(server->secret);
	int len = ntohs(request->length);

	memset(request->vector, 0, AUTH_VECTOR_LEN);
	MD5Init(&my_md5);
	memcpy(((char *)request) + len, server->secret, secretlen);

	MD5Update(&my_md5, (unsigned char *)request, len + secretlen);
	MD5Final(request->vector, &my_md5);	/* set the final vector */
}

/*
 * Verify the response from the server
 */
static int verify_packet(char *secret, AUTH_HDR * response, AUTH_HDR * request)
{
	MD5_CTX my_md5;
	unsigned char calculated[AUTH_VECTOR_LEN];
	unsigned char reply[AUTH_VECTOR_LEN];

	/*
	 * We could dispense with the memcpy, and do MD5's of the packet
	 * + vector piece by piece.     This is easier understand, and maybe faster.
	 */
	memcpy(reply, response->vector, AUTH_VECTOR_LEN);	/* save the reply */
	memcpy(response->vector, request->vector, AUTH_VECTOR_LEN);	/* sent vector */

	/* MD5(response packet header + vector + response packet data + secret) */
	MD5Init(&my_md5);
	MD5Update(&my_md5, (unsigned char *)response, ntohs(response->length));

	/*
	 * This next bit is necessary because of a bug in the original Livingston
	 * RADIUS server.       The authentication vector is *supposed* to be MD5'd
	 * with the old password (as the secret) for password changes.
	 * However, the old password isn't used.        The "authentication" vector
	 * for the server reply packet is simply the MD5 of the reply packet.
	 * Odd, the code is 99% there, but the old password is never copied
	 * to the secret!
	 */
	if (*secret) {
		MD5Update(&my_md5, (unsigned char *)secret, strlen(secret));
	}

	MD5Final(calculated, &my_md5);	/* set the final vector */

	/* Did he use the same random vector + shared secret? */
	if (memcmp(calculated, reply, AUTH_VECTOR_LEN) != 0) {
		return FALSE;
	}
	return TRUE;
}

/*
 * Find an attribute in a RADIUS packet.	Note that the packet length
 * is *always* kept in network byte order.
 */
static attribute_t *find_attribute(AUTH_HDR * response, unsigned char type)
{
	attribute_t *attr = (attribute_t *) & response->data;

	int len = ntohs(response->length) - AUTH_HDR_LEN;

	while (attr->attribute != type) {
		if ((len -= attr->length) <= 0) {
			return NULL;	/* not found */
		}
		attr = (attribute_t *) ((char *)attr + attr->length);
	}

	return attr;
}

struct radius_vsa {
	unsigned char vendorid[4];
	unsigned char vendor_type;
	unsigned char vendor_len;
	unsigned char string[1];
};

/* 
 * Find the VSA attribute with the shell:priv-lvl string if present.
 * If present, return the integer value, otherwise return -1.
 */
static int priv_from_vsa(AUTH_HDR * response)
{
	int ret = -1;
	int len = ntohs(response->length) - AUTH_HDR_LEN;
	attribute_t *attr = (attribute_t *) & response->data;
	const char shellpriv[] = "shell:priv-lvl";
	const int slen = strlen(shellpriv);

	while (len > 0) {
		if (attr->attribute == PW_VENDOR_SPECIFIC) {
			struct radius_vsa *v = (struct radius_vsa *)attr->data;
			int j, s;
			j = attr->length - 6;
			if (j < 0)
				j = 0;
			s = slen + 1;
			if (j > s) {
				const char *vsastr = (char *)v->string;
				/*  skip over the '*' or '=' that should follow
				 *  the attrname */
				if (!strncmp(shellpriv, vsastr, slen)) {
					char *e;
					int priv;
					priv = (int)strtol(&vsastr[s], &e, 0);
					if (e > &vsastr[s]) {
						ret = priv;
						break;
					}

				}
			}
		}
		len -= attr->length;
		attr = (attribute_t *) ((char *)attr + attr->length);
	}
	return ret;
}

/*
 * Add an attribute to a RADIUS packet.
 */
static void add_attribute(AUTH_HDR * request, unsigned char type,
			  CONST unsigned char *data, int length)
{
	attribute_t *p;

	p = (attribute_t *) ((unsigned char *)request + ntohs(request->length));
	p->attribute = type;
	p->length = length + 2;	/* the total size of the attribute */
	request->length = htons(ntohs(request->length) + p->length);
	memcpy(p->data, data, length);
}

/*
 * Add an integer attribute to a RADIUS packet.
 */
static void add_int_attribute(AUTH_HDR * request, unsigned char type, int data)
{
	int value = htonl(data);

	add_attribute(request, type, (unsigned char *)&value, sizeof(int));
}

static void add_nas_ip_address(AUTH_HDR * request, char *hostname)
{
	struct addrinfo hints;
	struct addrinfo *ai;
	int v4seen = 0, v6seen = 0;
	int r;

	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_DGRAM;
	hints.ai_flags = AI_ADDRCONFIG;

	r = getaddrinfo(hostname, NULL, &hints, &ai);
	if (r != 0)
		return;

	while (ai != NULL) {
		if (!v4seen && ai->ai_family == AF_INET) {
			v4seen = 1;
			r = ((struct sockaddr_in *)
			     ai->ai_addr)->sin_addr.s_addr;
			add_int_attribute(request, PW_NAS_IP_ADDRESS, ntohl(r));
		}
		if (!v6seen && ai->ai_family == AF_INET6) {
			v6seen = 1;
			add_attribute(request, PW_NAS_IPV6_ADDRESS,
				      (unsigned char *)&((struct sockaddr_in6 *)
						 ai->ai_addr)->sin6_addr, 16);
		}
		ai = ai->ai_next;
	}
	freeaddrinfo(ai);
}

/*
 * Add a RADIUS password attribute to the packet.	Some magic is done here.
 *
 * If it's an PW_OLD_PASSWORD attribute, it's encrypted using the encrypted
 * PW_PASSWORD attribute as the initialization vector.
 *
 * If the password attribute already exists, it's over-written.	This allows
 * us to simply call add_password to update the password for different
 * servers.
 */
static void add_password(AUTH_HDR * request, unsigned char type,
			 CONST char *password, char *secret)
{
	MD5_CTX md5_secret, my_md5;
	unsigned char misc[AUTH_VECTOR_LEN];
	int i;
	int length = strlen(password);
	unsigned char hashed[256 + AUTH_PASS_LEN];	/* can't be longer than this */
	unsigned char *vector;
	attribute_t *attr;

	if (length > MAXPASS) {	/* shorten the password for now */
		length = MAXPASS;
	}

	memcpy(hashed, password, length);
	memset(hashed + length, 0, sizeof(hashed) - length);

	if (length == 0) {
		length = AUTH_PASS_LEN;	/* 0 maps to 16 */
	}
	if ((length & (AUTH_PASS_LEN - 1)) != 0) {
		length += (AUTH_PASS_LEN - 1);	/* round it up */
		length &= ~(AUTH_PASS_LEN - 1);	/* chop it off */
	}
	/* 16*N maps to itself */
	attr = find_attribute(request, PW_PASSWORD);

	if (type == PW_PASSWORD) {
		vector = request->vector;
	} else {
		vector = attr->data;	/* attr CANNOT be NULL here. */
	}

	/* ************************************************************ */
	/* encrypt the password */
	/* password : e[0] = p[0] ^ MD5(secret + vector) */
	MD5Init(&md5_secret);
	MD5Update(&md5_secret, (unsigned char *)secret, strlen(secret));
	my_md5 = md5_secret;	/* so we won't re-do the hash later */
	MD5Update(&my_md5, vector, AUTH_VECTOR_LEN);
	MD5Final(misc, &my_md5);	/* set the final vector */
	xor(hashed, misc, AUTH_PASS_LEN);

	/* For each step through, e[i] = p[i] ^ MD5(secret + e[i-1]) */
	for (i = 1; i < (length >> 4); i++) {
		my_md5 = md5_secret;	/* grab old value of the hash */
		MD5Update(&my_md5, &hashed[(i - 1) * AUTH_PASS_LEN],
			  AUTH_PASS_LEN);
		MD5Final(misc, &my_md5);	/* set the final vector */
		xor(&hashed[i * AUTH_PASS_LEN], misc, AUTH_PASS_LEN);
	}

	if (type == PW_OLD_PASSWORD) {
		attr = find_attribute(request, PW_OLD_PASSWORD);
	}

	if (!attr) {
		add_attribute(request, type, hashed, length);
	} else {
		memcpy(attr->data, hashed, length);	/* overwrite the packet */
	}
}

/*  called from _pam_end() via pam_set_data() arg to cleanup memory and fd's */
static void cleanup_conf(pam_handle_t * pamh, void *arg, int unused)
{
	radius_server_t *next, *server;

	for (server = (radius_server_t *) arg; server; server = next) {
		if (server->sockfd != -1) {
			close(server->sockfd);
			server->sockfd = -1;
		}
		next = server->next;
		_pam_forget(server->secret);
		_pam_drop(server->port);
		_pam_drop(server->hostname);
		_pam_drop(server->hostpart);
		_pam_drop(server);
	}
}

/*
 * Parse the config file (not the PAM cmdline args)
 * Only do once, since all the entry points call this.  _pam_parse
 * has to be done per entry point, because it can have command line
 * args that are different, but the config file only needs to be read
 * once (again, except for the very rare case of different config files
 * being specified in the PAM lines, which the stat check will catch).
 *
 * Returns  0 if parsed and OK
 * Returns  1 if file already parsed and no change in mtime
 * Returns -1 if errors
*/
static int parse_conffile(pam_handle_t * pamh, radius_conf_t * cf)
{
	static struct stat last_st;
	int line = 0, timeout;
	const char *cfname = cf->conf_file;
	char *p;
	radius_server_t *server = NULL, *tmp;
	FILE *fserver;
	char hostname[BUFFER_SIZE], secret[BUFFER_SIZE], buffer[BUFFER_SIZE];
	char srcip[BUFFER_SIZE];

	if (!cfname || !*cfname)
		return -1;

	if (last_st.st_ino) {
		struct stat st;
		int rst;
		rst = stat(cfname, &st);
		if (!rst && st.st_ino == last_st.st_ino && st.st_mtime ==
		    last_st.st_mtime && st.st_ctime == last_st.st_ctime) {
			return 1;
		}
	}

	if (cf->server)		/* we already had sockets open and bound, cleanup */
		pam_set_data(pamh, "rad_conf_cleanup", NULL, NULL);

	/* the first time around, read the configuration file */
	if ((fserver = fopen(cfname, "r")) == (FILE *) NULL) {
		_pam_log(pamh, LOG_ERR, "Could not open configuration file %s:"
			 " %m", cfname);
		return -1;
	}

	while (!feof(fserver) &&
	       (fgets(buffer, sizeof(buffer), fserver) != (char *)NULL) &&
	       (!ferror(fserver))) {
		int scancnt;

		line++;
		p = buffer;

		/* Skip leading whitespace */
		while ((*p == ' ') || (*p == '\t'))
			p++;

		/* Skip blank lines and comments.  */
		if ((*p == '\r') || (*p == '\n') || (*p == '#'))
			continue;

		/* Error out if the text is too long.  */
		if (!*p) {
			_pam_log(pamh, LOG_ERR, "ERROR reading %s, line %d:"
				 " Line too long", cfname, line);
			break;
		}

		srcip[0] = '\0';
		scancnt =
		    sscanf(p, "%s %s %d %s", hostname, secret, &timeout, srcip);

		if (!strcmp(hostname, "vrf-name")) {
			/* is it the name of a vrf we should bind to? */
			if (scancnt < 2)
				_pam_log(pamh, LOG_ERR,
					 "ERROR reading %s, line %d:"
					 " only %d fields", cf->conf_file, line,
					 scancnt);
			else
				snprintf(cf->vrfname, sizeof cf->vrfname, "%s",
					 secret);
			snprintf(savconf.vrfname, sizeof savconf.vrfname,
				 "%s", secret);
			continue;
		} else if (!strcmp(hostname, "priv-lvl")) {
			/* privilege level for privileged logins */
			if (scancnt < 2)
				_pam_log(pamh, LOG_ERR, "ERROR reading %s, line"
					 " %d: only %d fields", cfname, line,
					 scancnt);
			else {
				unsigned long val;
				char *ok;
				val = strtoul(secret, &ok, 0);
				if (ok == secret || val > 15UL ||
				    (*ok && !isspace(*ok)))
					_pam_log(pamh, LOG_ERR, "Invalid number"
						 " (%s) \"%s\" in %s line %d:",
						 secret, hostname, cfname,
						 line);
				else {
					cf->min_priv_lvl = (unsigned)val;
					savconf.min_priv_lvl = cf->min_priv_lvl;
				}
			}
			continue;
		} else if (!strcmp(hostname, "debug")) {
			/* allow setting debug in config file as well */
			cf->debug = cfg_debug = 1;
			continue;
		}

		if (scancnt < 2) {
			_pam_log(pamh, LOG_ERR, "ERROR reading %s, line %d:"
				 " only %d fields", cfname, line, scancnt);
			continue;	/* invalid line */
		}
		if (scancnt < 3)
			timeout = 3;	/*  default timeout */

		/* read it in and save the data */
		tmp = calloc(sizeof(radius_server_t), 1);
		if (!tmp) {
			_pam_log(pamh, LOG_ERR,
				 "Unable to allocate server info for %s: %m",
				 hostname);
			return -1;
		}
		tmp->sockfd = -1;	/* mark as uninitialized */
		if (server) {
			server->next = tmp;
			server = server->next;
		} else {
			cf->server = tmp;
			server = tmp;	/* first time */
		}

		if (srcip[0])
			snprintf(server->src_ip, sizeof server->src_ip, "%s",
				 srcip);

		server->hostname = strdup(hostname);
		server->secret = strdup(secret);
		if (!server->hostname || !server->secret)
			_pam_log(pamh, LOG_ERR, "Memory allocation error saving"
				 " server %s info: %m", hostname);

		if ((timeout < 1) || (timeout > 60))
			server->timeout = 3;
		else
			server->timeout = timeout;
		server->next = NULL;

	}
	fclose(fserver);

	/*  save config file info for test on multiple calls */
	if (stat(cfname, &last_st) == -1)
		_pam_log(pamh, LOG_ERR, "Error stat'ing config file %s: %m",
			 cfname);

	if (!cf->server) {	/* no server found, die a horrible death */
		_pam_log(pamh, LOG_ERR, "No server found in"
			 " configuration file %s", cf->conf_file);
		return -1;
	}

	/*
	 * save the server in savconf for next call (if any) to _parse_args()
	 * for the same config file (will be overridden if a different config
	 * file
	 */
	savconf.server = cf->server;

	return 0;
}

static int setup_sock(pam_handle_t * pamh, radius_server_t * server,
		      radius_conf_t * conf)
{
	struct sockaddr_storage nullip;
	struct sockaddr *addr;
	char *hname;
	size_t sockaddrsz;
	int debug = conf->debug;

	if (host2server(pamh, server))
		return 1;

	memset(&nullip, 0, sizeof nullip);
	addr = (struct sockaddr *)&nullip;
	if (server->src_ip[0]) {
		int r;
		/*  bind to specified source IP and family */
		hname = server->src_ip;
		r = get_ipaddr(hname, addr, NULL);
		if (r)
			_pam_log(pamh, LOG_WARNING,
				 "Failed looking up source IP address %s for"
				 " server %s (error=%s)",
				 server->src_ip, server->hostname,
				 gai_strerror(r));
	} else
		hname = server->hostpart;

	addr->sa_family = server->family;

	server->sockfd = socket(server->family, SOCK_DGRAM, 0);
	if (server->sockfd == -1) {
		_pam_log(pamh, LOG_WARNING, "Failed to open socket for"
			 " %s: %m", server->hostname);
		return 1;
	}

	/*  warn only, not fatal */
	if (fcntl(server->sockfd, F_SETFD, FD_CLOEXEC))
		_pam_log(pamh, LOG_WARNING, "Set sockets close on exec failed"
			 " for %s: %m", server->hostname);

#ifndef HAVE_POLL_H
	if (server->sockfd >= FD_SETSIZE) {
		_pam_log(pamh, LOG_ERR, "Unusable socket, FD is larger than"
			 " %d", FD_SETSIZE);
		close(server->sockfd);
		server->sockfd = -1;
		return 1;
	}
#endif
	sockaddrsz = server->family == AF_INET ? sizeof(struct sockaddr_in) :
	    sizeof(struct sockaddr_in6);

	if (bind(server->sockfd, addr, sockaddrsz) < 0) {
		_pam_log(pamh, LOG_ERR, "Bind for server %s failed: %m", hname);
		/*  mark sockfd as not usable, by closing and set to -1 */
		close(server->sockfd);
		server->sockfd = -1;
		return 1;
	}

	if (conf->vrfname[0]) {
		/*  do not fail if the bind fails, connection may succeed */
		if (setsockopt(server->sockfd, SOL_SOCKET, SO_BINDTODEVICE,
			       conf->vrfname,
			       strlen(conf->vrfname) + 1) == -1) {
			_pam_log(pamh, LOG_WARNING,
				 "Binding host %s socket to VRF %s"
				 " failed: %m", server->hostname,
				 conf->vrfname);
		}
		DPRINT(pamh, LOG_DEBUG, "Configured server %s vrf as: %s",
		       server->hostname, conf->vrfname);
	}
	return 0;
}

/*
 * allocate and open a local port for communication with the RADIUS
 * server.
 * The server connection only needs to be redone if there are arguments,
 * and they are different (rare).
 */
static int initialize(pam_handle_t * pamh, radius_conf_t * conf)
{
	int ret = PAM_SUCCESS, retsetup, nservers = 0;
	radius_server_t *server = NULL;

	ret = parse_conffile(pamh, conf);
	if (ret == -1)
		return ret;
	else if (ret == 1)
		return PAM_SUCCESS;	/*  no changes to previous parse */

	/*  setup the sockets, bind to them, etc. */
	for (server = conf->server; server; server = server->next) {
		retsetup = setup_sock(pamh, server, conf);
		if (retsetup == PAM_SUCCESS)
			nservers++;
	}

	if (!nservers) {
		_pam_log(pamh, LOG_ERR, "No valid server found in configuration"
			 " file %s", conf->conf_file);
		ret = PAM_AUTHINFO_UNAVAIL;
	}

	if (conf->server) {
		cleaned_up = 0;
		pam_set_data(pamh, "rad_conf_cleanup", (void *)conf->server,
			     cleanup_conf);
	}

	return ret;
}

/*
 * Helper function for building a radius packet.
 * It initializes *some* of the header, and adds common attributes.
 */
static void build_radius_packet(AUTH_HDR * request, CONST char *user,
				CONST char *password, radius_conf_t * conf)
{
	char hostname[256];

	hostname[0] = '\0';
	gethostname(hostname, sizeof(hostname) - 1);

	request->length = htons(AUTH_HDR_LEN);

	if (password) {		/* make a random authentication req vector */
		get_random_vector(request->vector);
	}

	add_attribute(request, PW_USER_NAME, (unsigned char *)user,
		      strlen(user));

	/*
	 *      Add a password, if given.
	 */
	if (password) {
		add_password(request, PW_PASSWORD, password,
			     conf->server->secret);

		/*
		 *      Add a NULL password to non-accounting requests.
		 */
	} else if (request->code != PW_ACCOUNTING_REQUEST) {
		add_password(request, PW_PASSWORD, "", conf->server->secret);
	}

	/* Perhaps add NAS IP Address (and v6 version) */
	add_nas_ip_address(request, hostname);

	/* There's always a NAS identifier */
	if (conf->client_id && *conf->client_id) {
		add_attribute(request, PW_NAS_IDENTIFIER,
			      (unsigned char *)conf->client_id,
			      strlen(conf->client_id));
	}

	/*
	 *      Add in the port (pid) and port type (virtual).
	 *
	 *      We might want to give the TTY name here, too.
	 */
	add_int_attribute(request, PW_NAS_PORT_ID, getpid());
	add_int_attribute(request, PW_NAS_PORT_TYPE, PW_NAS_PORT_TYPE_VIRTUAL);
}

/*
 * Talk RADIUS to a server.
 * Send a packet and get the response
 */
static int talk_radius(radius_conf_t * conf, AUTH_HDR * request,
		       AUTH_HDR * response, char *password, char *old_password,
		       int tries, pam_handle_t * pamh, int accounting)
{
	int total_length;
#ifdef HAVE_POLL_H
	struct pollfd pollfds[1];
#else
	fd_set set;
#endif
	struct timeval tv;

	time_t now, end;
	int rcode;
	radius_server_t *server = conf->server;
	int ok;
	int server_tries;
	int retval;
	int sockfd;

	/* ************************************************************ */
	/* Now that we're done building the request, we can send it */

	/*
	   Hmm... on password change requests, all of the found server information
	   could be saved with a pam_set_data(), which means even the radius_conf_t
	   information will have to be malloc'd at some point

	   On the other hand, we could just try all of the servers again in
	   sequence, on the off chance that one may have ended up fixing itself.
	 */

	/* loop over all available servers */
	while (server != NULL) {
		sockfd = server->sockfd;
		struct sockaddr *addr =
		    accounting ? server->ip_acct : server->ip;

		if (sockfd == -1) {
			ok = FALSE;
			goto next;	/*  try next server, if any */
		}

		/* clear the response */
		memset(response, 0, sizeof(AUTH_HDR));

		if (!password) {	/* make an RFC 2139 p6 request authenticator */
			get_accounting_vector(request, server);
		}

		total_length = ntohs(request->length);
		server_tries = tries;
 send:
		/* send the packet */
		if (sendto(sockfd, (char *)request, total_length, 0,
			   addr, sizeof(struct sockaddr_storage)) < 0) {
			_pam_log(pamh, LOG_ERR, "Error sending packet to"
				 " server %s: %m", server->hostname);
			ok = FALSE;
			goto next;	/* skip to the next server */
		}

		/* ************************************************************ */
		/* Wait for the response, and verify it. */
		time(&now);

		tv.tv_sec = server->timeout;	/* wait for the specified time */
		tv.tv_usec = 0;
		end = now + tv.tv_sec;

#ifdef HAVE_POLL_H
		pollfds[0].fd = sockfd;	/* wait only for the RADIUS UDP socket */
		pollfds[0].events = POLLIN;	/* wait for data to read */
#else
		FD_ZERO(&set);	/* clear out the set */
		FD_SET(sockfd, &set);	/* wait only for the RADIUS UDP socket */
#endif

		/* loop, waiting for the network to return data */
		ok = TRUE;
		while (ok) {
#ifdef HAVE_POLL_H
			rcode =
			    poll((struct pollfd *)&pollfds, 1,
				 tv.tv_sec * 1000);
#else
			rcode = select(sockfd + 1, &set, NULL, NULL, &tv);
#endif

			/* timed out */
			if (rcode == 0) {
				if (--server_tries) {
					_pam_log(pamh, LOG_WARNING, "server %s"
						 " no reponse, retrying",
						 server->hostname);
					goto send;
				}
				_pam_log(pamh, LOG_ERR, "server %s"
					 " failed to respond",
					 server->hostname);
				ok = FALSE;
				break;	/* exit from the loop */
			} else if (rcode < 0) {

				/* poll returned an error */
				if (errno == EINTR) {	/* we were interrupted */
					time(&now);

					if (now > end) {
						_pam_log(pamh, LOG_ERR,
							 "server %s "
							 "failed to respond",
							 server->hostname);
						if (--server_tries)
							goto send;
						ok = FALSE;
						break;	/* exit from the loop */
					}

					tv.tv_sec = end - now;
					if (tv.tv_sec == 0) {	/* keep waiting */
						tv.tv_sec = 1;
					}
				} else {	/* a real error */
					_pam_log(pamh, LOG_ERR, "Error waiting"
						 " for response from"
						 " server %s: %m",
						 server->hostname);
					ok = FALSE;
					break;
				}
			} else	/* the poll/select  returned OK */
#ifdef HAVE_POLL_H
			if (pollfds[0].revents & POLLIN)
#else
			if (FD_ISSET(sockfd, &set))
#endif
			{

				/* try to receive some data */
				if ((total_length =
				     recvfrom(sockfd, (void *)response,
					      BUFFER_SIZE, 0, NULL,
					      NULL)) < 0) {
					_pam_log(pamh, LOG_ERR,
						 "error reading"
						 "response from server"
						 " %s: %m", server->hostname);
					ok = FALSE;
					break;

					/* there's data, see if it's valid */
				} else {
					char *p = server->secret;

					if ((ntohs(response->length) !=
					     total_length)
					    || (ntohs(response->length) >
						BUFFER_SIZE)) {
						_pam_log(pamh, LOG_ERR,
							 "response from "
							 "server %s is "
							 "corrupted",
							 server->hostname);
						ok = FALSE;
						break;
					}

					/* Check if we have the data OK.
					 * We should also check request->id */
					if (password) {
						if (old_password) {
#ifdef LIVINGSTON_PASSWORD_VERIFY_BUG_FIXED
							/* what it should be */
							p = old_password;
#else
							/* what it really is */
							p = "";
#endif
						}
						/*
						 * RFC 2139 p.6 says not do do this, but
						 * the Livingston 1.16 server disagrees.
						 * If the user says he wants the bug,
						 * give in.
						 */
					} else {	/* authentication request */
						if (conf->accounting_bug) {
							p = "";
						}
					}

					if (!verify_packet
					    (p, response, request)) {
						_pam_log(pamh, LOG_ERR,
							 "response from server"
							 " %s failed"
							 " verification:"
							 " The shared secret is"
							 " probably incorrect.",
							 server->hostname);
						ok = FALSE;
						break;
					}

					/*
					 * Check that the response ID matches
					 * the request ID.
					 */
					if (response->id != request->id) {
						_pam_log(pamh, LOG_WARNING,
							 "Response packet ID %d"
							 " does not match the"
							 " request packet ID"
							 " %d: verification of"
							 " packet fails",
							 response->id,
							 request->id);
						ok = FALSE;
						break;
					}
				}

				/*
				 * Whew! The poll is done. It hasn't timed out,
				 * or errored out.  It's our descriptor.
				 * We've got some data. It's the right size.
				 * The packet is valid.  NOW, we can skip out of
				 * the loop, and process the packet
				 */
				break;
			}
			/* otherwise, we've got data on another descriptor, keep
			 * checking the network */
		}
 next:				/* go to the next server if this one didn't respond */
		if (ok)
			break;
		server = server->next;
		if (server) {	/* if there's more servers to check */
			/* get a new authentication vector, and update
			 * the passwords */
			get_random_vector(request->vector);
			request->id = request->vector[0];

			/* update passwords, as appropriate */
			if (password) {
				get_random_vector(request->vector);
				if (old_password) {
					/* password change request */
					add_password(request,
						     PW_PASSWORD,
						     password, old_password);
					add_password(request,
						     PW_OLD_PASSWORD,
						     old_password,
						     old_password);
				} else {	/* authentication request */
					add_password(request,
						     PW_PASSWORD,
						     password, server->secret);
				}
			}
		}
	}

	if (!server) {
		_pam_log(pamh, LOG_ERR, "All RADIUS servers failed to respond");
		if (conf->localifdown)
			retval = PAM_IGNORE;
		else
			retval = PAM_AUTHINFO_UNAVAIL;
	} else
		retval = PAM_SUCCESS;

	return retval;
}

/**************************************************************************
 * MIDLEVEL PAM CODE
 **************************************************************************/

/* this is our front-end for module-application conversations */

#undef PAM_FAIL_CHECK
#define PAM_FAIL_CHECK if (retval != PAM_SUCCESS) { return retval; }

static int rad_converse(pam_handle_t * pamh, int msg_style, char *message,
			char **password)
{
	CONST struct pam_conv *conv;
	struct pam_message resp_msg;
	CONST struct pam_message *msg;
	struct pam_response *resp = NULL;
	int retval;

	resp_msg.msg_style = msg_style;
	resp_msg.msg = message;
	msg = &resp_msg;

	/* grab the password */
	retval = pam_get_item(pamh, PAM_CONV, (CONST void **)&conv);
	PAM_FAIL_CHECK;

	retval = conv->conv(1, &msg, &resp, conv->appdata_ptr);
	PAM_FAIL_CHECK;

	if (password) {		/* assume msg.type needs a response */
		/* I'm not sure if this next bit is necessary on Linux */
#ifdef sun
		/* NULL response, fail authentication */
		if ((resp == NULL) || (resp->resp == NULL)) {
			return PAM_SYSTEM_ERR;
		}
#endif

		*password = resp->resp;
		free(resp);
	}

	return PAM_SUCCESS;
}

/*
 *	We'll create the home directory if needed, and we'll write the flat file
 *	mapping entry.  It's done at this point, because this is the end of the
 *	authentication phase (and authorization, too, since authorization is part of
 *	authentication phase for RADIUS) for ssh, login, etc.
 */
static void
setup_userinfo(pam_handle_t * pamh, const char *user, int debug, int privileged)
{
	struct passwd *pw;

	/*
	 * set SUDO_PROMPT in env so that it prompts as the login user, not the mapped
	 * user, unless (unlikely) the prompt has already been set.
	 * It won't hurt to do this if the user wasn't mapped.
	 */
	if (!pam_getenv(pamh, "SUDO_PROMPT")) {
		char nprompt[strlen("SUDO_PROMPT=[sudo] password for ") + strlen(user) + 3];	/* + 3 for ": " and the \0 */
		snprintf(nprompt, sizeof nprompt,
			 "SUDO_PROMPT=[sudo] password for %s: ", user);
		if (pam_putenv(pamh, nprompt) != PAM_SUCCESS)
			_pam_log(pamh, LOG_NOTICE,
				 "failed to set PAM sudo prompt " "(%s)",
				 nprompt);
	}
	pw = getpwnam(user);	/* this should never fail, at this point... */
	if (!pw) {
		if (debug)
			pam_syslog(pamh, LOG_DEBUG,
				   "Failed to get homedir for user (%s)", user);
		return;
	}

	/*
	 * We don't "fail" on errors here, since they are not fatal for
	 * the session, although they can result in name or uid lookups not
	 * working correctly.
	 */
	__write_mapfile(pamh, user, pw->pw_uid, privileged, debug);
	__chk_homedir(pamh, user, pw->pw_dir, debug);
}

/*  this is used so that sm_auth returns an appropriate value */
static void inline setcred_return(pam_handle_t * pamh, int val)
{
	int *pret = malloc(sizeof(int));
	*pret = val;
	pam_set_data(pamh, "rad_setcred_return", (void *)pret, _int_free);
}

/**************************************************************************
 * GENERAL CODE
 **************************************************************************/

#undef PAM_FAIL_CHECK
#define PAM_FAIL_CHECK if (retval != PAM_SUCCESS) { \
    goto do_next; }

PAM_EXTERN int pam_sm_authenticate(pam_handle_t * pamh, int flags, int argc,
				   CONST char **argv)
{
	CONST char *user;
	CONST char *userinfo;
	char *password = NULL;
	CONST char *rhost;
	char *resp2challenge = NULL;
	int ctrl, debug = 0;
	int retval = PAM_AUTH_ERR;
	int num_challenge = 0;

	char recv_buffer[4096];
	char send_buffer[4096];
	AUTH_HDR *request = (AUTH_HDR *) send_buffer;
	AUTH_HDR *response = (AUTH_HDR *) recv_buffer;
	radius_conf_t config;

	ctrl = _pam_parse(pamh, argc, argv, &config);

	/*
	 * Get the IP address of the authentication server
	 * Then, open a socket, and bind it to a port
	 * Called early, so we can set debug flag
	 */
	retval = initialize(pamh, &config);
	PAM_FAIL_CHECK;

	debug = config.debug + cfg_debug;

	/* grab the user name */
	retval = pam_get_user(pamh, &user, NULL);
	PAM_FAIL_CHECK;

	/* check that they've entered something, and not too long, either */
	if (user == NULL || (strlen(user) > MAXPWNAM)) {
		retval = PAM_USER_UNKNOWN;
		DPRINT(pamh, LOG_DEBUG, "User name was NULL, or too long");
	}
	PAM_FAIL_CHECK;

	if (ctrl & PAM_RUSER_ARG) {
		retval =
		    pam_get_item(pamh, PAM_RUSER, (CONST void **)&userinfo);
		PAM_FAIL_CHECK;
		DPRINT(pamh, LOG_DEBUG, "Got PAM_RUSER name %s", userinfo);

		if (!strcmp("root", user)) {
			user = userinfo;
			DPRINT(pamh, LOG_DEBUG, "Username now %s from ruser",
			       user);
		} else {
			DPRINT(pamh, LOG_DEBUG,
			       "Skipping ruser for non-root auth");
		}
	}

	/*
	 * If there's no client id specified, use the service type, to help
	 * keep track of which service is doing the authentication.
	 */
	if (!config.client_id) {
		retval =
		    pam_get_item(pamh, PAM_SERVICE,
				 (CONST void **)&config.client_id);
		PAM_FAIL_CHECK;
	}

	/* build and initialize the RADIUS packet */
	request->code = PW_AUTHENTICATION_REQUEST;
	get_random_vector(request->vector);
	request->id = request->vector[0];	/* this should be evenly distributed */

	/* grab the password (if any) from the previous authentication layer */
	if (!config.force_prompt) {
		DPRINT(pamh, LOG_DEBUG, "ignore last_pass, force_prompt set");
		retval =
		    pam_get_item(pamh, PAM_AUTHTOK, (CONST void **)&password);
		PAM_FAIL_CHECK;
	}

	if (password) {
		password = strdup(password);
		DPRINT(pamh, LOG_DEBUG, "Got password %s", password);
	}

	/* no previous password: maybe get one from the user */
	if (!password) {
		if (ctrl & PAM_USE_FIRST_PASS) {
			retval = PAM_AUTH_ERR;	/* use one pass only, stopping if it fails */
			goto do_next;
		}

		/* check to see if we send a NULL password the first time around */
		if (!(ctrl & PAM_SKIP_PASSWD)) {
			retval = rad_converse(pamh, PAM_PROMPT_ECHO_OFF,
					      config.prompt, &password);
			PAM_FAIL_CHECK;

		} else {
			password = strdup("");
		}
	}
	/* end of password == NULL */
	build_radius_packet(request, user, password, &config);
	/* not all servers understand this service type, but some do */
	add_int_attribute(request, PW_USER_SERVICE_TYPE, PW_AUTHENTICATE_ONLY);

	/*
	 *      Tell the server which host the user is coming from.
	 *
	 *      Note that this is NOT the IP address of the machine running PAM!
	 *      It's the IP address of the client.
	 */
	retval = pam_get_item(pamh, PAM_RHOST, (CONST void **)&rhost);
	PAM_FAIL_CHECK;
	if (rhost) {
		add_attribute(request, PW_CALLING_STATION_ID,
			      (unsigned char *)rhost, strlen(rhost));
	}

	DPRINT(pamh, LOG_DEBUG, "Sending request code %d", request->code);

	retval = talk_radius(&config, request, response, password, NULL,
			     config.retries + 1, pamh, 0);
	PAM_FAIL_CHECK;

	DPRINT(pamh, LOG_DEBUG, "Got response code %d", response->code);

	/*
	 *      If we get an authentication failure, and we sent a NULL password,
	 *      ask the user for one and continue.
	 *
	 *      If we get an access challenge, then do a response, for as many
	 *      challenges as we receive.
	 */
	while (response->code == PW_ACCESS_CHALLENGE) {
		attribute_t *a_state, *a_reply;
		char challenge[BUFFER_SIZE];

		/* Now we do a bit more work: challenge the user, and get a response */
		if (((a_state = find_attribute(response, PW_STATE)) == NULL) ||
		    ((a_reply =
		      find_attribute(response, PW_REPLY_MESSAGE)) == NULL)) {
			/* Actually, State isn't required. */
			_pam_log(pamh, LOG_ERR, "Access-Challenge"
				 " received with State or Reply-Message"
				 " missing");
			retval = PAM_AUTHINFO_UNAVAIL;
			goto do_next;
		}

		/*
		 *      Security fixes.
		 */
		if ((a_state->length <= 2) || (a_reply->length <= 2)) {
			_pam_log(pamh, LOG_ERR, "Access-Challenge"
				 " received with invalid State or"
				 " Reply-Message");
			retval = PAM_AUTHINFO_UNAVAIL;
			goto do_next;
		}

		memcpy(challenge, a_reply->data, a_reply->length - 2);
		challenge[a_reply->length - 2] = 0;

		/* It's full challenge-response, we should have echo on */
		retval = rad_converse(pamh, PAM_PROMPT_ECHO_ON, challenge,
				      &resp2challenge);
		PAM_FAIL_CHECK;

		/* now that we've got a response, build a new radius packet */
		build_radius_packet(request, user, resp2challenge, &config);
		/* request->code is already PW_AUTHENTICATION_REQUEST */
		request->id++;	/* one up from the request */

		if (rhost) {
			add_attribute(request, PW_CALLING_STATION_ID,
				      (unsigned char *)rhost, strlen(rhost));
		}

		/* copy the state over from the servers response */
		add_attribute(request, PW_STATE, a_state->data,
			      a_state->length - 2);

		retval = talk_radius(&config, request, response,
				     resp2challenge, NULL, 1, pamh, 0);
		PAM_FAIL_CHECK;

		DPRINT(pamh, LOG_DEBUG, "Got response to challenge code %d",
		       response->code);

		/*
		 * max_challenge limits the # of challenges a server can issue
		 * It's a workaround for buggy servers
		 */
		if (config.max_challenge > 0
		    && response->code == PW_ACCESS_CHALLENGE) {
			num_challenge++;
			if (num_challenge >= config.max_challenge) {
				DPRINT(pamh, LOG_DEBUG,
				       "maximum number of challenges (%d)"
				       " reached, failing", num_challenge);
				break;
			}
		}
	}

	/* Whew! Done the pasword checks, look for an authentication acknowledge */
	if (response->code == PW_AUTHENTICATION_ACK) {
		int privlvl;

		/*
		 * get the privilege level via VSA, if present, and save it for the
		 *  accounting entry point
		 */
		privlvl = priv_from_vsa(response);
		if (debug) {
			if (privlvl < 0)
				_pam_log(pamh, LOG_NOTICE,
					 "server did not return VSA"
					 "with shell:priv-lvl");
			else
				_pam_log(pamh, LOG_NOTICE,
					 "server VSA shell:priv-lvl"
					 "=%d, min for priv=%d", privlvl,
					 config.min_priv_lvl);
		}
		setup_userinfo(pamh, user, debug,
			       privlvl >= config.min_priv_lvl);
		retval = PAM_SUCCESS;
	} else {
		retval = PAM_AUTH_ERR;	/* authentication failure */
	}

 do_next:
	/* If there was a password pass it to the next layer */
	if (password && *password) {
		pam_set_item(pamh, PAM_AUTHTOK, password);
	}

	DPRINT(pamh, LOG_DEBUG, "authentication %s",
	       retval == PAM_SUCCESS ? "succeeded" : "failed");

	_pam_forget(password);
	_pam_forget(resp2challenge);
	setcred_return(pamh, retval);
	return retval;
}

/*
 * Return a value matching the return value of pam_sm_authenticate, for
 * greatest compatibility.
 * (Always returning PAM_SUCCESS breaks other authentication modules;
 * always returning PAM_IGNORE breaks PAM when we're the only module.)
 */
PAM_EXTERN int pam_sm_setcred(pam_handle_t * pamh, int flags, int argc,
			      CONST char **argv)
{
	int ret, retval, *pret = NULL;

	retval = PAM_SUCCESS;
	ret = pam_get_data(pamh, "rad_setcred_return", (CONST void **)&pret);
	return ret == PAM_SUCCESS && pret ? *pret : retval;
}

#undef PAM_FAIL_CHECK
#define PAM_FAIL_CHECK if (retval != PAM_SUCCESS) {goto error; }

/* handle the accounting */
static int pam_private_session(pam_handle_t * pamh, int flags, int argc,
			       CONST char **argv, int status)
{
	CONST char *user;
	CONST char *rhost;
	int retval = PAM_AUTH_ERR, debug;

	char recv_buffer[4096];
	char send_buffer[4096];
	AUTH_HDR *request = (AUTH_HDR *) send_buffer;
	AUTH_HDR *response = (AUTH_HDR *) recv_buffer;
	radius_conf_t config;

	(void)_pam_parse(pamh, argc, argv, &config);

	/*
	 * Get the IP address of the authentication server
	 * Then, open a socket, and bind it to a port
	 * Called early, so we can set debug flag
	 */
	retval = initialize(pamh, &config);
	PAM_FAIL_CHECK;

	debug = config.debug + cfg_debug;

	/* grab the user name */
	retval = pam_get_user(pamh, &user, NULL);
	PAM_FAIL_CHECK;

	/* check that they've entered something, and not too long, either */
	if ((user == NULL) || (strlen(user) > MAXPWNAM)) {
		retval = PAM_USER_UNKNOWN;
		PAM_FAIL_CHECK;
	}

	if (status == PW_STATUS_STOP && !__remove_mapfile(pamh, user, debug))
		retval = PAM_USER_UNKNOWN;
	PAM_FAIL_CHECK;

	/*
	 * If there's no client id specified, use the service type, to help
	 * keep track of which service is doing the authentication.
	 */
	if (!config.client_id) {
		retval = pam_get_item(pamh, PAM_SERVICE,
				      (CONST void **)&config.client_id);
		PAM_FAIL_CHECK;
	}

	/* build and initialize the RADIUS packet */
	request->code = PW_ACCOUNTING_REQUEST;
	get_random_vector(request->vector);
	request->id = request->vector[0];	/* this should be evenly distributed */

	build_radius_packet(request, user, NULL, &config);

	add_int_attribute(request, PW_ACCT_STATUS_TYPE, status);

	sprintf(recv_buffer, "%08d", (int)getpid());
	add_attribute(request, PW_ACCT_SESSION_ID, (unsigned char *)recv_buffer,
		      strlen(recv_buffer));

	add_int_attribute(request, PW_ACCT_AUTHENTIC, PW_AUTH_RADIUS);

	if (status == PW_STATUS_START) {
		time_t *session_time = malloc(sizeof(time_t));
		time(session_time);
		pam_set_data(pamh, "rad_session_time", (void *)session_time,
			     _int_free);
	} else {
		time_t *session_time;
		retval =
		    pam_get_data(pamh, "rad_session_time",
				 (CONST void **)&session_time);
		PAM_FAIL_CHECK;

		add_int_attribute(request, PW_ACCT_SESSION_TIME,
				  time(NULL) - *session_time);
	}

	/*
	 *      Tell the server which host the user is coming from.
	 *
	 *      Note that this is NOT the IP address of the machine running PAM!
	 *      It's the IP address of the client.
	 */
	retval = pam_get_item(pamh, PAM_RHOST, (CONST void **)&rhost);
	PAM_FAIL_CHECK;
	if (rhost) {
		add_attribute(request, PW_CALLING_STATION_ID,
			      (unsigned char *)rhost, strlen(rhost));
	}

	retval =
	    talk_radius(&config, request, response, NULL, NULL, 1, pamh, 1);
	PAM_FAIL_CHECK;

	/* oops! They don't have the right password.    Complain and die. */
	if (response->code != PW_ACCOUNTING_RESPONSE) {
		retval = PAM_PERM_DENIED;
		goto error;
	}

	retval = PAM_SUCCESS;

 error:
	return retval;
}

PAM_EXTERN int pam_sm_open_session(pam_handle_t * pamh, int flags, int argc,
				   CONST char **argv)
{
	return pam_private_session(pamh, flags, argc, argv, PW_STATUS_START);
}

PAM_EXTERN int pam_sm_close_session(pam_handle_t * pamh, int flags, int argc,
				    CONST char **argv)
{
	return pam_private_session(pamh, flags, argc, argv, PW_STATUS_STOP);
}

#undef PAM_FAIL_CHECK
#define PAM_FAIL_CHECK if (retval != PAM_SUCCESS) { goto error; }
#define MAX_PASSWD_TRIES 3

PAM_EXTERN int pam_sm_chauthtok(pam_handle_t * pamh, int flags, int argc,
				CONST char **argv)
{
	CONST char *user;
	char *password = NULL;
	char *new_password = NULL;
	char *check_password = NULL;
	int ctrl;
	int retval = PAM_AUTHTOK_ERR;
	int attempts;
	char recv_buffer[4096];
	char send_buffer[4096];
	AUTH_HDR *request = (AUTH_HDR *) send_buffer;
	AUTH_HDR *response = (AUTH_HDR *) recv_buffer;
	radius_conf_t config;

	ctrl = _pam_parse(pamh, argc, argv, &config);

	/*
	 * Get the IP address of the authentication server
	 * Then, open a socket, and bind it to a port
	 */
	retval = initialize(pamh, &config);
	PAM_FAIL_CHECK;

	/* grab the user name */
	retval = pam_get_user(pamh, &user, NULL);
	PAM_FAIL_CHECK;

	/* check that they've entered something, and not too long, either */
	if ((user == NULL) || (strlen(user) > MAXPWNAM)) {
		return PAM_USER_UNKNOWN;
	}

	/*
	 * If there's no client id specified, use the service type, to help
	 * keep track of which service is doing the authentication.
	 */
	if (!config.client_id) {
		retval =
		    pam_get_item(pamh, PAM_SERVICE,
				 (CONST void **)&config.client_id);
		PAM_FAIL_CHECK;
	}

	/* grab the old password (if any) from the previous password layer */
	retval = pam_get_item(pamh, PAM_OLDAUTHTOK, (CONST void **)&password);
	PAM_FAIL_CHECK;
	if (password)
		password = strdup(password);

	/* grab the new password (if any) from the previous password layer */
	retval = pam_get_item(pamh, PAM_AUTHTOK, (CONST void **)&new_password);
	PAM_FAIL_CHECK;
	if (new_password)
		new_password = strdup(new_password);

	/* preliminary password change checks. */
	if (flags & PAM_PRELIM_CHECK) {
		if (!password) {	/* no previous password: ask for one */
			retval = rad_converse(pamh, PAM_PROMPT_ECHO_OFF,
					      config.prompt, &password);
			PAM_FAIL_CHECK;
		}

		/*
		 * We now check the password to see if it's the right one.
		 * If it isn't, we let the user try again.
		 * Note that RADIUS doesn't have any concept of 'root'. The only way
		 * that root can change someone's password is to log into the RADIUS
		 * server, and and change it there.
		 */

		/* build and initialize the access request RADIUS packet */
		request->code = PW_AUTHENTICATION_REQUEST;
		get_random_vector(request->vector);
		request->id = request->vector[0];	/* this should be evenly distributed */

		build_radius_packet(request, user, password, &config);
		add_int_attribute(request, PW_USER_SERVICE_TYPE,
				  PW_AUTHENTICATE_ONLY);

		retval =
		    talk_radius(&config, request, response, password, NULL, 1,
				pamh, 0);
		PAM_FAIL_CHECK;

		/* oops! They don't have the right password.    Complain and die. */
		if (response->code != PW_AUTHENTICATION_ACK) {
			_pam_forget(password);
			retval = PAM_PERM_DENIED;
			goto error;
		}

		/*
		 * We're now sure it's the right user.
		 * Ask for their new password, if appropriate
		 */

		if (!new_password) {	/* not found yet: ask for it */
			int new_attempts;
			attempts = 0;

			/* loop, trying to get matching new passwords */
			while (attempts++ < 3) {

				/* loop, trying to get a new password */
				new_attempts = 0;
				while (new_attempts++ < 3) {
					retval =
					    rad_converse(pamh,
							 PAM_PROMPT_ECHO_OFF,
							 "New password: ",
							 &new_password);
					PAM_FAIL_CHECK;

					/* the old password may be short.       Check it, first. */
					if (strcmp(password, new_password) == 0) {	/* are they the same? */
						rad_converse(pamh,
							     PAM_ERROR_MSG,
							     "You must choose a new password.",
							     NULL);
						_pam_forget(new_password);
						continue;
					} else if (strlen(new_password) < 6) {
						rad_converse(pamh,
							     PAM_ERROR_MSG,
							     "it's WAY too short",
							     NULL);
						_pam_forget(new_password);
						continue;
					}

					/* insert crypt password checking here */

					break;	/* the new password is OK */
				}

				if (new_attempts >= 3) {	/* too many new password attempts: die */
					retval = PAM_AUTHTOK_ERR;
					goto error;
				}

				/* make sure of the password by asking for verification */
				retval = rad_converse(pamh, PAM_PROMPT_ECHO_OFF,
						      "New password (again): ",
						      &check_password);
				PAM_FAIL_CHECK;

				retval = strcmp(new_password, check_password);
				_pam_forget(check_password);

				/* if they don't match, don't pass them to the next module */
				if (retval != 0) {
					_pam_forget(new_password);
					rad_converse(pamh, PAM_ERROR_MSG,
						     "You must enter the same password twice.",
						     NULL);
					retval = PAM_AUTHTOK_ERR;
					goto error;	/* ??? maybe this should be a 'continue' ??? */
				}

				break;	/* everything's fine */
			}	/* loop, trying to get matching new passwords */

			if (attempts >= 3) {	/* too many new password attempts: die */
				retval = PAM_AUTHTOK_ERR;
				goto error;
			}
		}

		/* now we have a new password which passes all of our tests */
		/*
		 * Solaris 2.6 calls pam_sm_chauthtok only ONCE, with PAM_PRELIM_CHECK
		 * set.
		 */
#ifndef sun
		/* If told to update the authentication token, do so. */
	} else if (flags & PAM_UPDATE_AUTHTOK) {
#endif

		if (!password || !new_password) {	/* ensure we've got passwords */
			retval = PAM_AUTHTOK_ERR;
			goto error;
		}

		/* build and initialize the password change request RADIUS packet */
		request->code = PW_PASSWORD_REQUEST;
		get_random_vector(request->vector);
		/* this should be evenly distributed */
		request->id = request->vector[0];

		/* the secret here can not be known to the user,
		 * so it's the new password */
		_pam_forget(config.server->secret);
		/* freed in cleanup_conf() */
		config.server->secret = strdup(password);

		build_radius_packet(request, user, new_password, &config);
		add_password(request, PW_OLD_PASSWORD, password, password);

		retval = talk_radius(&config, request, response, new_password,
				     password, 1, pamh, 0);
		PAM_FAIL_CHECK;

		/* Whew! Done password changing, check for password acknowledge */
		if (response->code != PW_PASSWORD_ACK) {
			retval = PAM_AUTHTOK_ERR;
			goto error;
		}
	}

	/*
	 * Send the passwords to the next stage if preliminary checks fail,
	 * or if the password change request fails.
	 */
	if ((flags & PAM_PRELIM_CHECK) || (retval != PAM_SUCCESS)) {
 error:

		/* If there was a password pass it to the next layer */
		if (password && *password) {
			pam_set_item(pamh, PAM_OLDAUTHTOK, password);
		}

		if (new_password && *new_password) {
			pam_set_item(pamh, PAM_AUTHTOK, new_password);
		}
	}

	if (ctrl & PAM_DEBUG_ARG) {
		_pam_log(pamh, LOG_DEBUG, "password change %s",
			 retval == PAM_SUCCESS ? "succeeded" : "failed");
	}

	_pam_forget(password);
	_pam_forget(new_password);
	return retval;
}

/*
 *	Do nothing for account management. This is apparently needed by
 *	some programs.
 */
PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t * pamh, int flags, int argc,
				CONST char **argv)
{
	int retval;
	retval = PAM_SUCCESS;
	return retval;
}

#ifdef PAM_STATIC

/* static module data */

struct pam_module _pam_radius_modstruct = {
	"pam_radius_auth",
	pam_sm_authenticate,
	pam_sm_setcred,
	pam_sm_acct_mgmt,
	pam_sm_open_session,
	pam_sm_close_session,
	pam_sm_chauthtok,
};
#endif