summaryrefslogtreecommitdiff
path: root/source3/nsswitch/winbindd_pam.c
blob: 3ae7692c127cc9cf4526c7929ab7e60c1949bcef (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
/*
   Unix SMB/CIFS implementation.

   Winbind daemon - pam auth funcions

   Copyright (C) Andrew Tridgell 2000
   Copyright (C) Tim Potter 2001
   Copyright (C) Andrew Bartlett 2001-2002
   Copyright (C) Guenther Deschner 2005
   
   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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "winbindd.h"
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_WINBIND

static NTSTATUS append_info3_as_txt(TALLOC_CTX *mem_ctx, 
				    struct winbindd_cli_state *state, 
				    NET_USER_INFO_3 *info3) 
{
	fstring str_sid;

	state->response.data.auth.info3.logon_time = 
		nt_time_to_unix(&(info3->logon_time));
	state->response.data.auth.info3.logoff_time = 
		nt_time_to_unix(&(info3->logoff_time));
	state->response.data.auth.info3.kickoff_time = 
		nt_time_to_unix(&(info3->kickoff_time));
	state->response.data.auth.info3.pass_last_set_time = 
		nt_time_to_unix(&(info3->pass_last_set_time));
	state->response.data.auth.info3.pass_can_change_time = 
		nt_time_to_unix(&(info3->pass_can_change_time));
	state->response.data.auth.info3.pass_must_change_time = 
		nt_time_to_unix(&(info3->pass_must_change_time));

	state->response.data.auth.info3.logon_count = info3->logon_count;
	state->response.data.auth.info3.bad_pw_count = info3->bad_pw_count;

	state->response.data.auth.info3.user_rid = info3->user_rid;
	state->response.data.auth.info3.group_rid = info3->group_rid;
	sid_to_string(str_sid, &(info3->dom_sid.sid));
	fstrcpy(state->response.data.auth.info3.dom_sid, str_sid);

	state->response.data.auth.info3.num_groups = info3->num_groups;
	state->response.data.auth.info3.user_flgs = info3->user_flgs;

	state->response.data.auth.info3.acct_flags = info3->acct_flags;
	state->response.data.auth.info3.num_other_sids = info3->num_other_sids;

	unistr2_to_ascii(state->response.data.auth.info3.user_name, 
		&info3->uni_user_name, -1);
	unistr2_to_ascii(state->response.data.auth.info3.full_name, 
		&info3->uni_full_name, -1);
	unistr2_to_ascii(state->response.data.auth.info3.logon_script, 
		&info3->uni_logon_script, -1);
	unistr2_to_ascii(state->response.data.auth.info3.profile_path, 
		&info3->uni_profile_path, -1);
	unistr2_to_ascii(state->response.data.auth.info3.home_dir, 
		&info3->uni_home_dir, -1);
	unistr2_to_ascii(state->response.data.auth.info3.dir_drive, 
		&info3->uni_dir_drive, -1);

	unistr2_to_ascii(state->response.data.auth.info3.logon_srv, 
		&info3->uni_logon_srv, -1);
	unistr2_to_ascii(state->response.data.auth.info3.logon_dom, 
		&info3->uni_logon_dom, -1);

	return NT_STATUS_OK;
}

static NTSTATUS append_info3_as_ndr(TALLOC_CTX *mem_ctx, 
				    struct winbindd_cli_state *state, 
				    NET_USER_INFO_3 *info3) 
{
	prs_struct ps;
	uint32 size;
	if (!prs_init(&ps, 256 /* Random, non-zero number */, mem_ctx, MARSHALL)) {
		return NT_STATUS_NO_MEMORY;
	}
	if (!net_io_user_info3("", info3, &ps, 1, 3, False)) {
		prs_mem_free(&ps);
		return NT_STATUS_UNSUCCESSFUL;
	}

	size = prs_data_size(&ps);
	state->response.extra_data.data = SMB_MALLOC(size);
	if (!state->response.extra_data.data) {
		prs_mem_free(&ps);
		return NT_STATUS_NO_MEMORY;
	}
	memset( state->response.extra_data.data, '\0', size );
	prs_copy_all_data_out(state->response.extra_data.data, &ps);
	state->response.length += size;
	prs_mem_free(&ps);
	return NT_STATUS_OK;
}

static NTSTATUS check_info3_in_group(TALLOC_CTX *mem_ctx, 
				     NET_USER_INFO_3 *info3,
				     const char *group_sid) 
{
	DOM_SID require_membership_of_sid;
	DOM_SID *all_sids;
	size_t num_all_sids = (2 + info3->num_groups2 + info3->num_other_sids);
	size_t i, j = 0;

	/* Parse the 'required group' SID */
	
	if (!group_sid || !group_sid[0]) {
		/* NO sid supplied, all users may access */
		return NT_STATUS_OK;
	}
	
	if (!string_to_sid(&require_membership_of_sid, group_sid)) {
		DEBUG(0, ("check_info3_in_group: could not parse %s as a SID!", 
			  group_sid));

		return NT_STATUS_INVALID_PARAMETER;
	}

	all_sids = TALLOC_ARRAY(mem_ctx, DOM_SID, num_all_sids);
	if (!all_sids)
		return NT_STATUS_NO_MEMORY;

	/* and create (by appending rids) the 'domain' sids */
	
	sid_copy(&all_sids[0], &(info3->dom_sid.sid));
	
	if (!sid_append_rid(&all_sids[0], info3->user_rid)) {
		DEBUG(3,("could not append user's primary RID 0x%x\n",
			 info3->user_rid));			
		
		return NT_STATUS_INVALID_PARAMETER;
	}
	j++;

	sid_copy(&all_sids[1], &(info3->dom_sid.sid));
		
	if (!sid_append_rid(&all_sids[1], info3->group_rid)) {
		DEBUG(3,("could not append additional group rid 0x%x\n",
			 info3->group_rid));			
		
		return NT_STATUS_INVALID_PARAMETER;
	}
	j++;	

	for (i = 0; i < info3->num_groups2; i++) {
	
		sid_copy(&all_sids[j], &(info3->dom_sid.sid));
		
		if (!sid_append_rid(&all_sids[j], info3->gids[i].g_rid)) {
			DEBUG(3,("could not append additional group rid 0x%x\n",
				info3->gids[i].g_rid));			
				
			return NT_STATUS_INVALID_PARAMETER;
		}
		j++;
	}

	/* Copy 'other' sids.  We need to do sid filtering here to
 	   prevent possible elevation of privileges.  See:

           http://www.microsoft.com/windows2000/techinfo/administration/security/sidfilter.asp
         */

	for (i = 0; i < info3->num_other_sids; i++) {
		sid_copy(&all_sids[info3->num_groups2 + i + 2],
			 &info3->other_sids[i].sid);
		j++;
	}

	for (i = 0; i < j; i++) {
		fstring sid1, sid2;
		DEBUG(10, ("User has SID: %s\n", 
			   sid_to_string(sid1, &all_sids[i])));
		if (sid_equal(&require_membership_of_sid, &all_sids[i])) {
			DEBUG(10, ("SID %s matches %s - user permitted to authenticate!\n", 
				   sid_to_string(sid1, &require_membership_of_sid), sid_to_string(sid2, &all_sids[i])));
			return NT_STATUS_OK;
		}
	}
	
	/* Do not distinguish this error from a wrong username/pw */

	return NT_STATUS_LOGON_FAILURE;
}

static struct winbindd_domain *find_auth_domain(struct winbindd_cli_state *state, 
						const char *domain_name)
{
	struct winbindd_domain *domain;

	if (IS_DC) {
		domain = find_domain_from_name_noinit(domain_name);
		if (domain == NULL) {
			DEBUG(3, ("Authentication for domain [%s] refused"
				  "as it is not a trusted domain\n", 
				  domain_name));
		}
		return domain;
	}

	if (is_myname(domain_name)) {
		DEBUG(3, ("Authentication for domain %s (local domain "
			  "to this server) not supported at this "
			  "stage\n", domain_name));
		return NULL;
	}

	return find_our_domain();
}

static void set_auth_errors(struct winbindd_response *resp, NTSTATUS result)
{
	resp->data.auth.nt_status = NT_STATUS_V(result);
	fstrcpy(resp->data.auth.nt_status_string, nt_errstr(result));

	/* we might have given a more useful error above */
	if (*resp->data.auth.error_string == '\0') 
		fstrcpy(resp->data.auth.error_string,
			get_friendly_nt_error_msg(result));
	resp->data.auth.pam_error = nt_status_to_pam(result);
}

static NTSTATUS fillup_password_policy(struct winbindd_domain *domain,
				       struct winbindd_cli_state *state)
{
	struct winbindd_methods *methods;
	NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
	SAM_UNK_INFO_1 password_policy;

	methods = domain->methods;

	status = methods->password_policy(domain, state->mem_ctx, &password_policy);
	if (NT_STATUS_IS_ERR(status)) {
		return status;
	}

	state->response.data.auth.policy.min_length_password =
		password_policy.min_length_password;
	state->response.data.auth.policy.password_history =
		password_policy.password_history;
	state->response.data.auth.policy.password_properties =
		password_policy.password_properties;
	state->response.data.auth.policy.expire	=
		nt_time_to_unix_abs(&(password_policy.expire));
	state->response.data.auth.policy.min_passwordage = 
		nt_time_to_unix_abs(&(password_policy.min_passwordage));

	return NT_STATUS_OK;
}

static NTSTATUS get_max_bad_attempts_from_lockout_policy(struct winbindd_domain *domain, 
							 TALLOC_CTX *mem_ctx, 
							 uint16 *max_allowed_bad_attempts)
{
	struct winbindd_methods *methods;
	NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
	SAM_UNK_INFO_12 lockout_policy;

	*max_allowed_bad_attempts = 0;

	methods = domain->methods;

	status = methods->lockout_policy(domain, mem_ctx, &lockout_policy);
	if (NT_STATUS_IS_ERR(status)) {
		return status;
	}

	*max_allowed_bad_attempts = lockout_policy.bad_attempt_lockout;

	return NT_STATUS_OK;
}

static NTSTATUS get_pwd_properties(struct winbindd_domain *domain, 
				   TALLOC_CTX *mem_ctx, 
				   uint32 *password_properties)
{
	struct winbindd_methods *methods;
	NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
	SAM_UNK_INFO_1 password_policy;

	*password_properties = 0;

	methods = domain->methods;

	status = methods->password_policy(domain, mem_ctx, &password_policy);
	if (NT_STATUS_IS_ERR(status)) {
		return status;
	}

	*password_properties = password_policy.password_properties;

	return NT_STATUS_OK;
}

static const char *generate_krb5_ccache(TALLOC_CTX *mem_ctx, 
					const char *type,
					uid_t uid,
					BOOL *internal_ccache)
{
	/* accept FILE and WRFILE as krb5_cc_type from the client and then
	 * build the full ccname string based on the user's uid here -
	 * Guenther*/

	const char *gen_cc = NULL;

	*internal_ccache = True;

	if (uid == -1) {
		goto memory_ccache;
	}

	if (!type || type[0] == '\0') {
		goto memory_ccache;
	}

	if (strequal(type, "FILE")) {
		gen_cc = talloc_asprintf(mem_ctx, "FILE:/tmp/krb5cc_%d", uid);
	} else if (strequal(type, "WRFILE")) {
		gen_cc = talloc_asprintf(mem_ctx, "WRFILE:/tmp/krb5cc_%d", uid);
	} else {
		DEBUG(10,("we don't allow to set a %s type ccache\n", type));
		goto memory_ccache;
	}

	*internal_ccache = False;
	goto done;

  memory_ccache:
  	gen_cc = talloc_strdup(mem_ctx, "MEMORY:winbindd_pam_ccache");

  done:
  	if (gen_cc == NULL) {
		DEBUG(0,("out of memory\n"));
		return NULL;
	}

	DEBUG(10,("using ccache: %s %s\n", gen_cc, *internal_ccache ? "(internal)":""));

	return gen_cc;
}

static uid_t get_uid_from_state(struct winbindd_cli_state *state)
{
	uid_t uid = -1;

	uid = state->request.data.auth.uid;

	if (uid < 0) {
		DEBUG(1,("invalid uid: '%d'\n", uid));
		return -1;
	}
	return uid;
}

static void setup_return_cc_name(struct winbindd_cli_state *state, const char *cc)
{
	const char *type = state->request.data.auth.krb5_cc_type;

	state->response.data.auth.krb5ccname[0] = '\0';

	if (type[0] == '\0') {
		return;
	}

	if (!strequal(type, "FILE") &&
	    !strequal(type, "WRFILE")) {
	    	DEBUG(10,("won't return krbccname for a %s type ccache\n", 
			type));
		return;
	}
	
	fstrcpy(state->response.data.auth.krb5ccname, cc);
}

/**********************************************************************
 Authenticate a user with a clear text password using Kerberos and fill up
 ccache if required
 **********************************************************************/
static NTSTATUS winbindd_raw_kerberos_login(struct winbindd_domain *domain,
					    struct winbindd_cli_state *state,
					    NET_USER_INFO_3 **info3)
{
#ifdef HAVE_KRB5
	NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
	krb5_error_code krb5_ret;
	DATA_BLOB tkt, session_key_krb5;
	DATA_BLOB ap_rep, session_key;
	PAC_DATA *pac_data = NULL;
	PAC_LOGON_INFO *logon_info = NULL;
	char *client_princ = NULL;
	char *client_princ_out = NULL;
	char *local_service = NULL;
	const char *cc = NULL;
	const char *principal_s = NULL;
	const char *service = NULL;
	char *realm = NULL;
	fstring name_domain, name_user;
	time_t ticket_lifetime = 0;
	time_t renewal_until = 0;
	uid_t uid = -1;
	ADS_STRUCT *ads;
	time_t time_offset = 0;
	BOOL internal_ccache = True;

	ZERO_STRUCT(session_key);
	ZERO_STRUCT(session_key_krb5);
	ZERO_STRUCT(tkt);
	ZERO_STRUCT(ap_rep);

	ZERO_STRUCTP(info3);

	*info3 = NULL;
	
	/* 1st step: 
	 * prepare a krb5_cc_cache string for the user */

	uid = get_uid_from_state(state);
	if (uid == -1) {
		DEBUG(0,("no valid uid\n"));
	}

	cc = generate_krb5_ccache(state->mem_ctx,
				  state->request.data.auth.krb5_cc_type,
				  state->request.data.auth.uid, 
				  &internal_ccache);
	if (cc == NULL) {
		return NT_STATUS_NO_MEMORY;
	}


	/* 2nd step: 
	 * get kerberos properties */
	
	if (domain->private_data) {
		ads = (ADS_STRUCT *)domain->private_data;
		time_offset = ads->auth.time_offset; 
	}


	/* 3rd step: 
	 * do kerberos auth and setup ccache as the user */

	parse_domain_user(state->request.data.auth.user, name_domain, name_user);

	realm = domain->alt_name;
	strupper_m(realm);
	
	principal_s = talloc_asprintf(state->mem_ctx, "%s@%s", name_user, realm); 
	if (principal_s == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	service = talloc_asprintf(state->mem_ctx, "%s/%s@%s", KRB5_TGS_NAME, realm, realm);
	if (service == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	/* if this is a user ccache, we need to act as the user to let the krb5
	 * library handle the chown, etc. */

	/************************ NON-ROOT **********************/

	if (!internal_ccache) {

		set_effective_uid(uid);
		DEBUG(10,("winbindd_raw_kerberos_login: uid is %d\n", uid));
	}

	krb5_ret = kerberos_kinit_password_ext(principal_s, 
					       state->request.data.auth.pass, 
					       time_offset, 
					       &ticket_lifetime,
					       &renewal_until,
					       cc, 
					       True,
					       True,
					       WINBINDD_PAM_AUTH_KRB5_RENEW_TIME);

	if (krb5_ret) {
		DEBUG(1,("winbindd_raw_kerberos_login: kinit failed for '%s' with: %s (%d)\n", 
			principal_s, error_message(krb5_ret), krb5_ret));
		result = krb5_to_nt_status(krb5_ret);
		goto failed;
	}

	/* does http_timestring use heimdals libroken strftime?? - Guenther */
	DEBUG(10,("got TGT for %s in %s (valid until: %s (%d), renewable till: %s (%d))\n", 
		principal_s, cc, 
		http_timestring(ticket_lifetime), (int)ticket_lifetime, 
		http_timestring(renewal_until), (int)renewal_until));

	client_princ = talloc_strdup(state->mem_ctx, global_myname());
	if (client_princ == NULL) {
		result = NT_STATUS_NO_MEMORY;
		goto failed;
	}
	strlower_m(client_princ);

	local_service = talloc_asprintf(state->mem_ctx, "%s$@%s", client_princ, lp_realm());
	if (local_service == NULL) {
		DEBUG(0,("winbindd_raw_kerberos_login: out of memory\n"));
		result = NT_STATUS_NO_MEMORY;
		goto failed;
	}

	krb5_ret = cli_krb5_get_ticket(local_service, 
				       time_offset, 
				       &tkt, 
				       &session_key_krb5, 
				       0, 
				       cc);
	if (krb5_ret) {
		DEBUG(1,("winbindd_raw_kerberos_login: failed to get ticket for %s: %s\n", 
			local_service, error_message(krb5_ret)));
		result = krb5_to_nt_status(krb5_ret);
		goto failed;
	}

	if (!internal_ccache) {
		gain_root_privilege();
	}

	/************************ NON-ROOT **********************/

	result = ads_verify_ticket(state->mem_ctx, 
				   lp_realm(), 
				   time_offset,
				   &tkt, 
				   &client_princ_out, 
				   &pac_data, 
				   &ap_rep, 
				   &session_key);	
	if (!NT_STATUS_IS_OK(result)) {
		DEBUG(0,("winbindd_raw_kerberos_login: ads_verify_ticket failed: %s\n", 
			nt_errstr(result)));
		goto failed;
	}

	if (!pac_data) {
		DEBUG(3,("winbindd_raw_kerberos_login: no pac data\n"));
		result = NT_STATUS_INVALID_PARAMETER;
		goto failed;
	}
			
	logon_info = get_logon_info_from_pac(pac_data);
	if (logon_info == NULL) {
		DEBUG(1,("winbindd_raw_kerberos_login: no logon info\n"));
		result = NT_STATUS_INVALID_PARAMETER;
		goto failed;
	}

	DEBUG(10,("winbindd_raw_kerberos_login: winbindd validated ticket of %s\n", 
		local_service));


	/* last step: 
	 * put results together */

	*info3 = &logon_info->info3;

	/* if we had a user's ccache then return that string for the pam
	 * environment */

	if (!internal_ccache) {
		
		setup_return_cc_name(state, cc);

		result = add_ccache_to_list(principal_s,
					    cc,
					    service,
					    state->request.data.auth.user,
					    NULL,
					    state->request.data.auth.pass,
					    uid,
					    time(NULL),
					    ticket_lifetime,
					    renewal_until, 
					    lp_winbind_refresh_tickets());

		if (!NT_STATUS_IS_OK(result)) {
			DEBUG(10,("winbindd_raw_kerberos_login: failed to add ccache to list: %s\n", 
				nt_errstr(result)));
		}
	}

	result = NT_STATUS_OK;

	goto done;

failed:

	/* we could have created a new credential cache with a valid tgt in it
	 * but we werent able to get or verify the service ticket for this
	 * local host and therefor didn't get the PAC, we need to remove that
	 * cache entirely now */

	krb5_ret = ads_kdestroy(cc);
	if (krb5_ret) {
		DEBUG(3,("winbindd_raw_kerberos_login: "
			 "could not destroy krb5 credential cache: "
			 "%s\n", error_message(krb5_ret)));
	}

	if (!NT_STATUS_IS_OK(remove_ccache_by_ccname(cc))) {
		DEBUG(3,("winbindd_raw_kerberos_login: "
			  "could not remove ccache\n"));
	}

done:
	data_blob_free(&session_key);
	data_blob_free(&session_key_krb5);
	data_blob_free(&ap_rep);
	data_blob_free(&tkt);

	SAFE_FREE(client_princ_out);

	if (!internal_ccache) {
		gain_root_privilege();
	}

	return result;
#else 
	return NT_STATUS_NOT_SUPPORTED;
#endif /* HAVE_KRB5 */
}

void winbindd_pam_auth(struct winbindd_cli_state *state)
{
	struct winbindd_domain *domain;
	fstring name_domain, name_user;

	/* Ensure null termination */
	state->request.data.auth.user
		[sizeof(state->request.data.auth.user)-1]='\0';

	/* Ensure null termination */
	state->request.data.auth.pass
		[sizeof(state->request.data.auth.pass)-1]='\0';

	DEBUG(3, ("[%5lu]: pam auth %s\n", (unsigned long)state->pid,
		  state->request.data.auth.user));

	/* Parse domain and username */
	
	if (!parse_domain_user(state->request.data.auth.user,
			       name_domain, name_user)) {
		set_auth_errors(&state->response, NT_STATUS_NO_SUCH_USER);
		DEBUG(5, ("Plain text authentication for %s returned %s "
			  "(PAM: %d)\n",
			  state->request.data.auth.user, 
			  state->response.data.auth.nt_status_string,
			  state->response.data.auth.pam_error));
		request_error(state);
		return;
	}

	domain = find_auth_domain(state, name_domain);

	if (domain == NULL) {
		set_auth_errors(&state->response, NT_STATUS_NO_SUCH_USER);
		DEBUG(5, ("Plain text authentication for %s returned %s "
			  "(PAM: %d)\n",
			  state->request.data.auth.user, 
			  state->response.data.auth.nt_status_string,
			  state->response.data.auth.pam_error));
		request_error(state);
		return;
	}

	sendto_domain(state, domain);
}

NTSTATUS winbindd_dual_pam_auth_cached(struct winbindd_domain *domain,
				       struct winbindd_cli_state *state,
				       NET_USER_INFO_3 **info3)
{
	NTSTATUS result = NT_STATUS_LOGON_FAILURE;
	uint16 max_allowed_bad_attempts; 
	fstring name_domain, name_user;
	DOM_SID sid;
	enum SID_NAME_USE type;
	uchar new_nt_pass[NT_HASH_LEN];
	const uint8 *cached_nt_pass;
	NET_USER_INFO_3 *my_info3;
	time_t kickoff_time, must_change_time;

	*info3 = NULL;

	ZERO_STRUCTP(info3);

	DEBUG(10,("winbindd_dual_pam_auth_cached\n"));

	/* Parse domain and username */
	
	parse_domain_user(state->request.data.auth.user, name_domain, name_user);


	if (!lookup_cached_name(state->mem_ctx,
	                        name_domain,
				name_user,
				&sid,
				&type)) {
		DEBUG(10,("winbindd_dual_pam_auth_cached: no such user in the cache\n"));
		return NT_STATUS_NO_SUCH_USER;
	}

	if (type != SID_NAME_USER) {
		DEBUG(10,("winbindd_dual_pam_auth_cached: not a user (%s)\n", sid_type_lookup(type)));
		return NT_STATUS_LOGON_FAILURE;
	}

	result = winbindd_get_creds(domain, 
				    state->mem_ctx, 
				    &sid, 
				    &my_info3, 
				    &cached_nt_pass);
	if (!NT_STATUS_IS_OK(result)) {
		DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get creds: %s\n", nt_errstr(result)));
		return result;
	}

	*info3 = my_info3;

	E_md4hash(state->request.data.auth.pass, new_nt_pass);

#if DEBUG_PASSWORD
	dump_data(100, (const char *)new_nt_pass, NT_HASH_LEN);
	dump_data(100, (const char *)cached_nt_pass, NT_HASH_LEN);
#endif

	if (!memcmp(cached_nt_pass, new_nt_pass, NT_HASH_LEN)) {

		/* User *DOES* know the password, update logon_time and reset
		 * bad_pw_count */
	
		my_info3->user_flgs |= LOGON_CACHED_ACCOUNT;
	
		if (my_info3->acct_flags & ACB_AUTOLOCK) {
			return NT_STATUS_ACCOUNT_LOCKED_OUT;
		}
	
		if (my_info3->acct_flags & ACB_DISABLED) {
			return NT_STATUS_ACCOUNT_DISABLED;
		}
	
		if (my_info3->acct_flags & ACB_WSTRUST) {
			return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
		}
	
		if (my_info3->acct_flags & ACB_SVRTRUST) {
			return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
		}
	
		if (my_info3->acct_flags & ACB_DOMTRUST) {
			return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
		}

		/* The info3 acct_flags in NT4's samlogon reply don't have
		 * ACB_NORMAL set. */
#if 0
		if (!(my_info3->acct_flags & ACB_NORMAL)) {
			DEBUG(10,("winbindd_dual_pam_auth_cached: whats wrong with that one?: 0x%08x\n", 
				my_info3->acct_flags));
			return NT_STATUS_LOGON_FAILURE;
		}
#endif
		kickoff_time = nt_time_to_unix(&my_info3->kickoff_time);
		if (kickoff_time != 0 && time(NULL) > kickoff_time) {
			return NT_STATUS_ACCOUNT_EXPIRED;
		}

		must_change_time = nt_time_to_unix(&my_info3->pass_must_change_time);
		if (must_change_time != 0 && must_change_time < time(NULL)) {
			return NT_STATUS_PASSWORD_EXPIRED;
		}
	
		/* FIXME: we possibly should handle logon hours as well (does xp when
		 * offline?) see auth/auth_sam.c:sam_account_ok for details */

		unix_to_nt_time(&my_info3->logon_time, time(NULL));
		my_info3->bad_pw_count = 0;

		result = winbindd_update_creds_by_info3(domain,
							state->mem_ctx,
							state->request.data.auth.user,
							state->request.data.auth.pass,
							my_info3);
		if (!NT_STATUS_IS_OK(result)) {
			DEBUG(1,("failed to update creds: %s\n", nt_errstr(result)));
			return result;
		}

		return NT_STATUS_OK;

	}

	/* User does *NOT* know the correct password, modify info3 accordingly */

	/* failure of this is not critical */
	result = get_max_bad_attempts_from_lockout_policy(domain, state->mem_ctx, &max_allowed_bad_attempts);
	if (!NT_STATUS_IS_OK(result)) {
		DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get max_allowed_bad_attempts. "
			  "Won't be able to honour account lockout policies\n"));
	}

	/* increase counter */
	my_info3->bad_pw_count++;

	if (max_allowed_bad_attempts == 0) {
		goto failed;
	}

	/* lockout user */
	if (my_info3->bad_pw_count >= max_allowed_bad_attempts) {

		uint32 password_properties;

		result = get_pwd_properties(domain, state->mem_ctx, &password_properties);
		if (!NT_STATUS_IS_OK(result)) {
			DEBUG(10,("winbindd_dual_pam_auth_cached: failed to get password properties.\n"));
		}

		if ((my_info3->user_rid != DOMAIN_USER_RID_ADMIN) || 
		    (password_properties & DOMAIN_LOCKOUT_ADMINS)) {
			my_info3->acct_flags |= ACB_AUTOLOCK;
		}
	}

failed:
	result = winbindd_update_creds_by_info3(domain,
						state->mem_ctx,
						state->request.data.auth.user,
						NULL,
						my_info3);

	if (!NT_STATUS_IS_OK(result)) {
		DEBUG(0,("winbindd_dual_pam_auth_cached: failed to update creds %s\n", 
			nt_errstr(result)));
	}

	return NT_STATUS_LOGON_FAILURE;
}

NTSTATUS winbindd_dual_pam_auth_kerberos(struct winbindd_domain *domain,
					 struct winbindd_cli_state *state, 
					 NET_USER_INFO_3 **info3)
{
	struct winbindd_domain *contact_domain;
	fstring name_domain, name_user;
	NTSTATUS result;

	DEBUG(10,("winbindd_dual_pam_auth_kerberos\n"));
	
	/* Parse domain and username */
	
	parse_domain_user(state->request.data.auth.user, name_domain, name_user);

	/* what domain should we contact? */
	
	if ( IS_DC ) {
		if (!(contact_domain = find_domain_from_name(name_domain))) {
			DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n", 
				  state->request.data.auth.user, name_domain, name_user, name_domain)); 
			result = NT_STATUS_NO_SUCH_USER;
			goto done;
		}
		
	} else {
		if (is_myname(name_domain)) {
			DEBUG(3, ("Authentication for domain %s (local domain to this server) not supported at this stage\n", name_domain));
			result =  NT_STATUS_NO_SUCH_USER;
			goto done;
		}
		
		contact_domain = find_domain_from_name(name_domain);
		if (contact_domain == NULL) {
			DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n", 
				  state->request.data.auth.user, name_domain, name_user, name_domain)); 

			contact_domain = find_our_domain();
		}
	}

	set_dc_type_and_flags(contact_domain);

	if (!contact_domain->active_directory) {
		DEBUG(3,("krb5 auth requested but domain is not Active Directory\n"));
		return NT_STATUS_INVALID_LOGON_TYPE;
	}

	result = winbindd_raw_kerberos_login(contact_domain, state, info3);
done:
	return result;
}

NTSTATUS winbindd_dual_pam_auth_samlogon(struct winbindd_domain *domain,
					 struct winbindd_cli_state *state,
					 NET_USER_INFO_3 **info3)
{

	struct rpc_pipe_client *netlogon_pipe;
	uchar chal[8];
	DATA_BLOB lm_resp;
	DATA_BLOB nt_resp;
	int attempts = 0;
	unsigned char local_lm_response[24];
	unsigned char local_nt_response[24];
	struct winbindd_domain *contact_domain;
	fstring name_domain, name_user;
	BOOL retry;
	NTSTATUS result;
	NET_USER_INFO_3 *my_info3;

	ZERO_STRUCTP(info3);

	*info3 = NULL;

	my_info3 = TALLOC_ZERO_P(state->mem_ctx, NET_USER_INFO_3);
	if (my_info3 == NULL) {
		return NT_STATUS_NO_MEMORY;
	}


	DEBUG(10,("winbindd_dual_pam_auth_samlogon\n"));
	
	/* Parse domain and username */
	
	parse_domain_user(state->request.data.auth.user, name_domain, name_user);

	/* do password magic */
	

	generate_random_buffer(chal, 8);
	if (lp_client_ntlmv2_auth()) {
		DATA_BLOB server_chal;
		DATA_BLOB names_blob;
		DATA_BLOB nt_response;
		DATA_BLOB lm_response;
		server_chal = data_blob_talloc(state->mem_ctx, chal, 8); 
		
		/* note that the 'workgroup' here is a best guess - we don't know
		   the server's domain at this point.  The 'server name' is also
		   dodgy... 
		*/
		names_blob = NTLMv2_generate_names_blob(global_myname(), lp_workgroup());
		
		if (!SMBNTLMv2encrypt(name_user, name_domain, 
				      state->request.data.auth.pass, 
				      &server_chal, 
				      &names_blob,
				      &lm_response, &nt_response, NULL)) {
			data_blob_free(&names_blob);
			data_blob_free(&server_chal);
			DEBUG(0, ("winbindd_pam_auth: SMBNTLMv2encrypt() failed!\n"));
			result = NT_STATUS_NO_MEMORY;
			goto done;
		}
		data_blob_free(&names_blob);
		data_blob_free(&server_chal);
		lm_resp = data_blob_talloc(state->mem_ctx, lm_response.data,
					   lm_response.length);
		nt_resp = data_blob_talloc(state->mem_ctx, nt_response.data,
					   nt_response.length);
		data_blob_free(&lm_response);
		data_blob_free(&nt_response);

	} else {
		if (lp_client_lanman_auth() 
		    && SMBencrypt(state->request.data.auth.pass, 
				  chal, 
				  local_lm_response)) {
			lm_resp = data_blob_talloc(state->mem_ctx, 
						   local_lm_response, 
						   sizeof(local_lm_response));
		} else {
			lm_resp = data_blob(NULL, 0);
		}
		SMBNTencrypt(state->request.data.auth.pass, 
			     chal,
			     local_nt_response);

		nt_resp = data_blob_talloc(state->mem_ctx, 
					   local_nt_response, 
					   sizeof(local_nt_response));
	}
	
	/* what domain should we contact? */
	
	if ( IS_DC ) {
		if (!(contact_domain = find_domain_from_name(name_domain))) {
			DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n", 
				  state->request.data.auth.user, name_domain, name_user, name_domain)); 
			result = NT_STATUS_NO_SUCH_USER;
			goto done;
		}
		
	} else {
		if (is_myname(name_domain)) {
			DEBUG(3, ("Authentication for domain %s (local domain to this server) not supported at this stage\n", name_domain));
			result =  NT_STATUS_NO_SUCH_USER;
			goto done;
		}

		contact_domain = find_our_domain();
	}

	/* check authentication loop */

	do {

		ZERO_STRUCTP(my_info3);
		retry = False;

		result = cm_connect_netlogon(contact_domain, &netlogon_pipe);

		if (!NT_STATUS_IS_OK(result)) {
			DEBUG(3, ("could not open handle to NETLOGON pipe\n"));
			goto done;
		}

		result = rpccli_netlogon_sam_network_logon(netlogon_pipe,
							   state->mem_ctx,
							   0,
							   contact_domain->dcname, /* server name */
							   name_user,              /* user name */
							   name_domain,            /* target domain */
							   global_myname(),        /* workstation */
							   chal,
							   lm_resp,
							   nt_resp,
							   my_info3);
		attempts += 1;

		/* We have to try a second time as cm_connect_netlogon
		   might not yet have noticed that the DC has killed
		   our connection. */

		if (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)) {
			retry = True;
			continue;
		}
		
		/* if we get access denied, a possible cause was that we had
		   and open connection to the DC, but someone changed our
		   machine account password out from underneath us using 'net
		   rpc changetrustpw' */
		   
		if ( NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
			DEBUG(3,("winbindd_pam_auth: sam_logon returned "
				 "ACCESS_DENIED.  Maybe the trust account "
				"password was changed and we didn't know it. "
				 "Killing connections to domain %s\n",
				name_domain));
			invalidate_cm_connection(&contact_domain->conn);
			retry = True;
		} 
		
	} while ( (attempts < 2) && retry );

	*info3 = my_info3;
done:
	return result;
}

enum winbindd_result winbindd_dual_pam_auth(struct winbindd_domain *domain,
					    struct winbindd_cli_state *state) 
{
	NTSTATUS result = NT_STATUS_LOGON_FAILURE;
	fstring name_domain, name_user;
	NET_USER_INFO_3 *info3 = NULL;
	
	/* Ensure null termination */
	state->request.data.auth.user[sizeof(state->request.data.auth.user)-1]='\0';

	/* Ensure null termination */
	state->request.data.auth.pass[sizeof(state->request.data.auth.pass)-1]='\0';

	DEBUG(3, ("[%5lu]: dual pam auth %s\n", (unsigned long)state->pid,
		  state->request.data.auth.user));

	/* Parse domain and username */
	
	parse_domain_user(state->request.data.auth.user, name_domain, name_user);

	DEBUG(10,("winbindd_dual_pam_auth: domain: %s last was %s\n", domain->name, domain->online ? "online":"offline"));

	/* Check for Kerberos authentication */
	if (domain->online && (state->request.flags & WBFLAG_PAM_KRB5)) {
	
		result = winbindd_dual_pam_auth_kerberos(domain, state, &info3);

		if (NT_STATUS_IS_OK(result)) {
			DEBUG(10,("winbindd_dual_pam_auth_kerberos succeeded\n"));
			goto process_result;
		} else {
			DEBUG(10,("winbindd_dual_pam_auth_kerberos failed: %s\n", nt_errstr(result)));
		}

		if (NT_STATUS_EQUAL(result, NT_STATUS_NO_LOGON_SERVERS) ||
		    NT_STATUS_EQUAL(result, NT_STATUS_IO_TIMEOUT) ||
		    NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
			DEBUG(10,("winbindd_dual_pam_auth_kerberos setting domain to offline\n"));
			domain->online = False;
		}

		/* there are quite some NT_STATUS errors where there is no
		 * point in retrying with a samlogon, we explictly have to take
		 * care not to increase the bad logon counter on the DC */

		if (NT_STATUS_EQUAL(result, NT_STATUS_ACCOUNT_DISABLED) ||
		    NT_STATUS_EQUAL(result, NT_STATUS_ACCOUNT_EXPIRED) ||
		    NT_STATUS_EQUAL(result, NT_STATUS_ACCOUNT_LOCKED_OUT) ||
		    NT_STATUS_EQUAL(result, NT_STATUS_INVALID_LOGON_HOURS) ||
		    NT_STATUS_EQUAL(result, NT_STATUS_INVALID_WORKSTATION) ||
		    NT_STATUS_EQUAL(result, NT_STATUS_LOGON_FAILURE) ||
		    NT_STATUS_EQUAL(result, NT_STATUS_NO_SUCH_USER) ||
		    NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_EXPIRED) ||
		    NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_MUST_CHANGE) ||
		    NT_STATUS_EQUAL(result, NT_STATUS_WRONG_PASSWORD)) {
			goto process_result;
		}
		
		if (state->request.flags & WBFLAG_PAM_FALLBACK_AFTER_KRB5) {
			DEBUG(3,("falling back to samlogon\n"));
			goto sam_logon;
		} else {
			goto cached_logon;
		}
	}

sam_logon:
	/* Check for Samlogon authentication */
	if (domain->online) {
		result = winbindd_dual_pam_auth_samlogon(domain, state, &info3);
	
		if (NT_STATUS_IS_OK(result)) {
			DEBUG(10,("winbindd_dual_pam_auth_samlogon succeeded\n"));
			goto process_result;
		} else {
			DEBUG(10,("winbindd_dual_pam_auth_samlogon failed: %s\n", nt_errstr(result)));
			if (domain->online) {
				/* We're still online - fail. */
				goto done;
			}
			/* Else drop through and see if we can check offline.... */
		}
	}

cached_logon:
	/* Check for Cached logons */
	if (!domain->online && (state->request.flags & WBFLAG_PAM_CACHED_LOGIN) && 
	    lp_winbind_offline_logon()) {
	
		result = winbindd_dual_pam_auth_cached(domain, state, &info3);

		if (NT_STATUS_IS_OK(result)) {
			DEBUG(10,("winbindd_dual_pam_auth_cached succeeded\n"));
			goto process_result;
		} else {
			DEBUG(10,("winbindd_dual_pam_auth_cached failed: %s\n", nt_errstr(result)));
			goto done;
		}
	}

process_result:

	if (NT_STATUS_IS_OK(result)) {
	
		DOM_SID user_sid;

		/* In all codepaths were result == NT_STATUS_OK info3 must have
		   been initialized. */
		if (!info3) {
			result = NT_STATUS_INTERNAL_ERROR;
			goto done;
		}

		netsamlogon_cache_store(name_user, info3);
		wcache_invalidate_samlogon(find_domain_from_name(name_domain), info3);

		/* save name_to_sid info as early as possible */
		sid_compose(&user_sid, &info3->dom_sid.sid, info3->user_rid);
		cache_name2sid(domain, name_domain, name_user, SID_NAME_USER, &user_sid);
		
		/* Check if the user is in the right group */

		if (!NT_STATUS_IS_OK(result = check_info3_in_group(state->mem_ctx, info3,
					state->request.data.auth.require_membership_of_sid))) {
			DEBUG(3, ("User %s is not in the required group (%s), so plaintext authentication is rejected\n",
				  state->request.data.auth.user, 
				  state->request.data.auth.require_membership_of_sid));
			goto done;
		}

		if (state->request.flags & WBFLAG_PAM_INFO3_NDR) {
			result = append_info3_as_ndr(state->mem_ctx, state, info3);
			if (!NT_STATUS_IS_OK(result)) {
				DEBUG(10,("Failed to append INFO3 (NDR): %s\n", nt_errstr(result)));
				goto done;
			}
		}

		if (state->request.flags & WBFLAG_PAM_INFO3_TEXT) {
			result = append_info3_as_txt(state->mem_ctx, state, info3);
			if (!NT_STATUS_IS_OK(result)) {
				DEBUG(10,("Failed to append INFO3 (TXT): %s\n", nt_errstr(result)));
				goto done;
			}

		}

		if ((state->request.flags & WBFLAG_PAM_CACHED_LOGIN) &&
		    lp_winbind_offline_logon()) {

			result = winbindd_store_creds(domain,
						      state->mem_ctx,
						      state->request.data.auth.user,
						      state->request.data.auth.pass,
						      info3, NULL);
			if (!NT_STATUS_IS_OK(result)) {
				DEBUG(10,("Failed to store creds: %s\n", nt_errstr(result)));
				goto done;
			}

		}

		/* this is required to provide password expiry warning */ 
		if (state->request.flags & WBFLAG_PAM_GET_PWD_POLICY) {
			result = fillup_password_policy(domain, state);

			if (!NT_STATUS_IS_OK(result)) {
				DEBUG(10,("Failed to get password policies: %s\n", nt_errstr(result)));
				goto done;
			}
		}
	
	} 

done:
	/* give us a more useful (more correct?) error code */
	if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) ||
	    (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
		result = NT_STATUS_NO_LOGON_SERVERS;
	}
	
	state->response.data.auth.nt_status = NT_STATUS_V(result);
	fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));

	/* we might have given a more useful error above */
	if (!*state->response.data.auth.error_string) 
		fstrcpy(state->response.data.auth.error_string, get_friendly_nt_error_msg(result));
	state->response.data.auth.pam_error = nt_status_to_pam(result);

	DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, ("Plain-text authentication for user %s returned %s (PAM: %d)\n", 
	      state->request.data.auth.user, 
	      state->response.data.auth.nt_status_string,
	      state->response.data.auth.pam_error));	      

	if ( NT_STATUS_IS_OK(result) &&
	     (state->request.flags & WBFLAG_PAM_AFS_TOKEN) ) {

		char *afsname = talloc_strdup(state->mem_ctx,
					      lp_afs_username_map());
		char *cell;

		if (afsname == NULL) {
			goto no_token;
		}

		afsname = talloc_string_sub(state->mem_ctx,
					    lp_afs_username_map(),
					    "%D", name_domain);
		afsname = talloc_string_sub(state->mem_ctx, afsname,
					    "%u", name_user);
		afsname = talloc_string_sub(state->mem_ctx, afsname,
					    "%U", name_user);

		{
			DOM_SID user_sid;
			fstring sidstr;

			sid_copy(&user_sid, &info3->dom_sid.sid);
			sid_append_rid(&user_sid, info3->user_rid);
			sid_to_string(sidstr, &user_sid);
			afsname = talloc_string_sub(state->mem_ctx, afsname,
						    "%s", sidstr);
		}

		if (afsname == NULL) {
			goto no_token;
		}

		strlower_m(afsname);

		DEBUG(10, ("Generating token for user %s\n", afsname));

		cell = strchr(afsname, '@');

		if (cell == NULL) {
			goto no_token;
		}

		*cell = '\0';
		cell += 1;

		/* Append an AFS token string */
		state->response.extra_data.data =
			afs_createtoken_str(afsname, cell);

		if (state->response.extra_data.data != NULL)
			state->response.length +=
				strlen(state->response.extra_data.data)+1;

	no_token:
		TALLOC_FREE(afsname);
	}

	return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
}


/**********************************************************************
 Challenge Response Authentication Protocol 
**********************************************************************/

void winbindd_pam_auth_crap(struct winbindd_cli_state *state)
{
	struct winbindd_domain *domain = NULL;
	const char *domain_name = NULL;
	NTSTATUS result;

	if (!state->privileged) {
		char *error_string = NULL;
		DEBUG(2, ("winbindd_pam_auth_crap: non-privileged access "
			  "denied.  !\n"));
		DEBUGADD(2, ("winbindd_pam_auth_crap: Ensure permissions "
			     "on %s are set correctly.\n",
			     get_winbind_priv_pipe_dir()));
		/* send a better message than ACCESS_DENIED */
		error_string = talloc_asprintf(state->mem_ctx,
					       "winbind client not authorized "
					       "to use winbindd_pam_auth_crap."
					       " Ensure permissions on %s "
					       "are set correctly.",
					       get_winbind_priv_pipe_dir());
		fstrcpy(state->response.data.auth.error_string, error_string);
		result = NT_STATUS_ACCESS_DENIED;
		goto done;
	}

	/* Ensure null termination */
	state->request.data.auth_crap.user
		[sizeof(state->request.data.auth_crap.user)-1]=0;
	state->request.data.auth_crap.domain
		[sizeof(state->request.data.auth_crap.domain)-1]=0;

	DEBUG(3, ("[%5lu]: pam auth crap domain: [%s] user: %s\n",
		  (unsigned long)state->pid,
		  state->request.data.auth_crap.domain,
		  state->request.data.auth_crap.user));

	if (*state->request.data.auth_crap.domain != '\0') {
		domain_name = state->request.data.auth_crap.domain;
	} else if (lp_winbind_use_default_domain()) {
		domain_name = lp_workgroup();
	}

	if (domain_name != NULL)
		domain = find_auth_domain(state, domain_name);

	if (domain != NULL) {
		sendto_domain(state, domain);
		return;
	}

	result = NT_STATUS_NO_SUCH_USER;

 done:
	set_auth_errors(&state->response, result);
	DEBUG(5, ("CRAP authentication for %s\\%s returned %s (PAM: %d)\n",
		  state->request.data.auth_crap.domain,
		  state->request.data.auth_crap.user, 
		  state->response.data.auth.nt_status_string,
		  state->response.data.auth.pam_error));
	request_error(state);
	return;
}


enum winbindd_result winbindd_dual_pam_auth_crap(struct winbindd_domain *domain,
						 struct winbindd_cli_state *state) 
{
	NTSTATUS result;
        NET_USER_INFO_3 info3;
	struct rpc_pipe_client *netlogon_pipe;
	const char *name_user = NULL;
	const char *name_domain = NULL;
	const char *workstation;
	struct winbindd_domain *contact_domain;
	int attempts = 0;
	BOOL retry;

	DATA_BLOB lm_resp, nt_resp;

	/* This is child-only, so no check for privileged access is needed
	   anymore */

	/* Ensure null termination */
	state->request.data.auth_crap.user[sizeof(state->request.data.auth_crap.user)-1]=0;
	state->request.data.auth_crap.domain[sizeof(state->request.data.auth_crap.domain)-1]=0;

	name_user = state->request.data.auth_crap.user;

	if (*state->request.data.auth_crap.domain) {
		name_domain = state->request.data.auth_crap.domain;
	} else if (lp_winbind_use_default_domain()) {
		name_domain = lp_workgroup();
	} else {
		DEBUG(5,("no domain specified with username (%s) - failing auth\n", 
			 name_user));
		result = NT_STATUS_NO_SUCH_USER;
		goto done;
	}

	DEBUG(3, ("[%5lu]: pam auth crap domain: %s user: %s\n", (unsigned long)state->pid,
		  name_domain, name_user));
	   
	if (*state->request.data.auth_crap.workstation) {
		workstation = state->request.data.auth_crap.workstation;
	} else {
		workstation = global_myname();
	}

	if (state->request.data.auth_crap.lm_resp_len > sizeof(state->request.data.auth_crap.lm_resp)
		|| state->request.data.auth_crap.nt_resp_len > sizeof(state->request.data.auth_crap.nt_resp)) {
		DEBUG(0, ("winbindd_pam_auth_crap: invalid password length %u/%u\n", 
			  state->request.data.auth_crap.lm_resp_len, 
			  state->request.data.auth_crap.nt_resp_len));
		result = NT_STATUS_INVALID_PARAMETER;
		goto done;
	}

	lm_resp = data_blob_talloc(state->mem_ctx, state->request.data.auth_crap.lm_resp,
					state->request.data.auth_crap.lm_resp_len);
	nt_resp = data_blob_talloc(state->mem_ctx, state->request.data.auth_crap.nt_resp,
					state->request.data.auth_crap.nt_resp_len);

	/* what domain should we contact? */
	
	if ( IS_DC ) {
		if (!(contact_domain = find_domain_from_name(name_domain))) {
			DEBUG(3, ("Authentication for domain for [%s] -> [%s]\\[%s] failed as %s is not a trusted domain\n", 
				  state->request.data.auth_crap.user, name_domain, name_user, name_domain)); 
			result = NT_STATUS_NO_SUCH_USER;
			goto done;
		}
	} else {
		if (is_myname(name_domain)) {
			DEBUG(3, ("Authentication for domain %s (local domain to this server) not supported at this stage\n", name_domain));
			result =  NT_STATUS_NO_SUCH_USER;
			goto done;
		}
		contact_domain = find_our_domain();
	}

	do {
		ZERO_STRUCT(info3);
		retry = False;

		netlogon_pipe = NULL;
		result = cm_connect_netlogon(contact_domain, &netlogon_pipe);

		if (!NT_STATUS_IS_OK(result)) {
			DEBUG(3, ("could not open handle to NETLOGON pipe (error: %s)\n",
				  nt_errstr(result)));
			goto done;
		}

		result = rpccli_netlogon_sam_network_logon(netlogon_pipe,
							   state->mem_ctx,
							   state->request.data.auth_crap.logon_parameters,
							   contact_domain->dcname,
							   name_user,
							   name_domain, 
									/* Bug #3248 - found by Stefan Burkei. */
							   workstation, /* We carefully set this above so use it... */
							   state->request.data.auth_crap.chal,
							   lm_resp,
							   nt_resp,
							   &info3);

		attempts += 1;

		/* We have to try a second time as cm_connect_netlogon
		   might not yet have noticed that the DC has killed
		   our connection. */

		if (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)) {
			retry = True;
			continue;
		}

		/* if we get access denied, a possible cause was that we had and open
		   connection to the DC, but someone changed our machine account password
		   out from underneath us using 'net rpc changetrustpw' */
		   
		if ( NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
			DEBUG(3,("winbindd_pam_auth: sam_logon returned "
				 "ACCESS_DENIED.  Maybe the trust account "
				"password was changed and we didn't know it. "
				 "Killing connections to domain %s\n",
				name_domain));
			invalidate_cm_connection(&contact_domain->conn);
			retry = True;
		} 

	} while ( (attempts < 2) && retry );

	if (NT_STATUS_IS_OK(result)) {

		netsamlogon_cache_store(name_user, &info3);
		wcache_invalidate_samlogon(find_domain_from_name(name_domain), &info3);

		/* Check if the user is in the right group */

		if (!NT_STATUS_IS_OK(result = check_info3_in_group(state->mem_ctx, &info3,
							state->request.data.auth_crap.require_membership_of_sid))) {
			DEBUG(3, ("User %s is not in the required group (%s), so plaintext authentication is rejected\n",
				  state->request.data.auth_crap.user, 
				  state->request.data.auth_crap.require_membership_of_sid));
			goto done;
		}

		if (state->request.flags & WBFLAG_PAM_INFO3_NDR) {
			result = append_info3_as_ndr(state->mem_ctx, state, &info3);
		} else if (state->request.flags & WBFLAG_PAM_UNIX_NAME) {
			/* ntlm_auth should return the unix username, per 
			   'winbind use default domain' settings and the like */

			fstring username_out;
			const char *nt_username, *nt_domain;
			if (!(nt_username = unistr2_tdup(state->mem_ctx, &(info3.uni_user_name)))) {
				/* If the server didn't give us one, just use the one we sent them */
				nt_username = name_user;
			}

			if (!(nt_domain = unistr2_tdup(state->mem_ctx, &(info3.uni_logon_dom)))) {
				/* If the server didn't give us one, just use the one we sent them */
				nt_domain = name_domain;
			}

			fill_domain_username(username_out, nt_domain, nt_username, True);

			DEBUG(5, ("Setting unix username to [%s]\n", username_out));

			state->response.extra_data.data = SMB_STRDUP(username_out);
			if (!state->response.extra_data.data) {
				result = NT_STATUS_NO_MEMORY;
				goto done;
			}
			state->response.length +=  strlen(state->response.extra_data.data)+1;
		}
		
		if (state->request.flags & WBFLAG_PAM_USER_SESSION_KEY) {
			memcpy(state->response.data.auth.user_session_key, info3.user_sess_key,
					sizeof(state->response.data.auth.user_session_key) /* 16 */);
		}
		if (state->request.flags & WBFLAG_PAM_LMKEY) {
			memcpy(state->response.data.auth.first_8_lm_hash, info3.lm_sess_key,
					sizeof(state->response.data.auth.first_8_lm_hash) /* 8 */);
		}
	}

done:

	/* give us a more useful (more correct?) error code */
	if ((NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) ||
	    (NT_STATUS_EQUAL(result, NT_STATUS_UNSUCCESSFUL)))) {
		result = NT_STATUS_NO_LOGON_SERVERS;
	}

	if (state->request.flags & WBFLAG_PAM_NT_STATUS_SQUASH) {
		result = nt_status_squash(result);
	}

	state->response.data.auth.nt_status = NT_STATUS_V(result);
	fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));

	/* we might have given a more useful error above */
	if (!*state->response.data.auth.error_string) {
		fstrcpy(state->response.data.auth.error_string, get_friendly_nt_error_msg(result));
	}
	state->response.data.auth.pam_error = nt_status_to_pam(result);

	DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, 
	      ("NTLM CRAP authentication for user [%s]\\[%s] returned %s (PAM: %d)\n", 
	       name_domain,
	       name_user,
	       state->response.data.auth.nt_status_string,
	       state->response.data.auth.pam_error));	      

	return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
}

/* Change a user password */

void winbindd_pam_chauthtok(struct winbindd_cli_state *state)
{
	NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
	char *oldpass;
	char *newpass = NULL;
	fstring domain, user;
	POLICY_HND dom_pol;
	struct winbindd_domain *contact_domain;
	struct rpc_pipe_client *cli;
	BOOL got_info = False;
	SAM_UNK_INFO_1 info;
	SAMR_CHANGE_REJECT reject;

	DEBUG(3, ("[%5lu]: pam chauthtok %s\n", (unsigned long)state->pid,
		state->request.data.chauthtok.user));

	/* Setup crap */

	parse_domain_user(state->request.data.chauthtok.user, domain, user);

	contact_domain = find_domain_from_name(domain);
	if (!contact_domain) {
		DEBUG(3, ("Cannot change password for [%s] -> [%s]\\[%s] as %s is not a trusted domain\n", 
			  state->request.data.chauthtok.user, domain, user, domain)); 
		result = NT_STATUS_NO_SUCH_USER;
		goto done;
	}

	/* Change password */

	oldpass = state->request.data.chauthtok.oldpass;
	newpass = state->request.data.chauthtok.newpass;

	/* Initialize reject reason */
	state->response.data.auth.reject_reason = Undefined;

	/* Get sam handle */

	result = cm_connect_sam(contact_domain, state->mem_ctx, &cli,
				&dom_pol);
	if (!NT_STATUS_IS_OK(result)) {
		DEBUG(1, ("could not get SAM handle on DC for %s\n", domain));
		goto done;
	}

	result = rpccli_samr_chgpasswd3(cli, state->mem_ctx, user, newpass, oldpass, &info, &reject);

	/* FIXME: need to check for other error codes ? */
	if (NT_STATUS_EQUAL(result, NT_STATUS_PASSWORD_RESTRICTION)) {

		state->response.data.auth.policy.min_length_password = 
			info.min_length_password;
		state->response.data.auth.policy.password_history = 
			info.password_history;
		state->response.data.auth.policy.password_properties = 
			info.password_properties;
		state->response.data.auth.policy.expire = 
			nt_time_to_unix_abs(&info.expire);
		state->response.data.auth.policy.min_passwordage = 
			nt_time_to_unix_abs(&info.min_passwordage);

		state->response.data.auth.reject_reason = 
			reject.reject_reason;

		got_info = True;

	/* only fallback when the chgpasswd3 call is not supported */
	} else if ((NT_STATUS_EQUAL(result, NT_STATUS(DCERPC_FAULT_OP_RNG_ERROR))) ||
		   (NT_STATUS_EQUAL(result, NT_STATUS_NOT_SUPPORTED)) ||
		   (NT_STATUS_EQUAL(result, NT_STATUS_NOT_IMPLEMENTED))) {

		DEBUG(10,("Password change with chgpasswd3 failed with: %s, retrying chgpasswd_user\n", 
			nt_errstr(result)));
		
		result = rpccli_samr_chgpasswd_user(cli, state->mem_ctx, user, newpass, oldpass);
	}

done: 
	if (NT_STATUS_IS_OK(result) && (state->request.flags & WBFLAG_PAM_CACHED_LOGIN) &&
	    lp_winbind_offline_logon()) {

		NTSTATUS cred_ret;
		
		cred_ret = winbindd_update_creds_by_name(contact_domain,
							 state->mem_ctx, user,
							 newpass);
		if (!NT_STATUS_IS_OK(cred_ret)) {
			DEBUG(10,("Failed to store creds: %s\n", nt_errstr(cred_ret)));
			goto process_result; /* FIXME: hm, risking inconsistant cache ? */
		}
	}		

	if (!NT_STATUS_IS_OK(result) && !got_info && contact_domain) {

		NTSTATUS policy_ret;
		
		policy_ret = fillup_password_policy(contact_domain, state);

		/* failure of this is non critical, it will just provide no
		 * additional information to the client why the change has
		 * failed - Guenther */

		if (!NT_STATUS_IS_OK(policy_ret)) {
			DEBUG(10,("Failed to get password policies: %s\n", nt_errstr(policy_ret)));
			goto process_result;
		}
	}

process_result:

	state->response.data.auth.nt_status = NT_STATUS_V(result);
	fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
	fstrcpy(state->response.data.auth.error_string, get_friendly_nt_error_msg(result));
	state->response.data.auth.pam_error = nt_status_to_pam(result);

	DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, 
	      ("Password change for user [%s]\\[%s] returned %s (PAM: %d)\n", 
	       domain,
	       user,
	       state->response.data.auth.nt_status_string,
	       state->response.data.auth.pam_error));	      

	if (NT_STATUS_IS_OK(result)) {
		request_ok(state);
	} else {
		request_error(state);
	}
}

void winbindd_pam_logoff(struct winbindd_cli_state *state)
{
	struct winbindd_domain *domain;
	fstring name_domain, user;
	
	DEBUG(3, ("[%5lu]: pam logoff %s\n", (unsigned long)state->pid,
		state->request.data.logoff.user));

	/* Ensure null termination */
	state->request.data.logoff.user
		[sizeof(state->request.data.logoff.user)-1]='\0';

	state->request.data.logoff.krb5ccname
		[sizeof(state->request.data.logoff.krb5ccname)-1]='\0';

	parse_domain_user(state->request.data.logoff.user, name_domain, user);

	domain = find_auth_domain(state, name_domain);

	if (domain == NULL) {
		set_auth_errors(&state->response, NT_STATUS_NO_SUCH_USER);
		DEBUG(5, ("Pam Logoff for %s returned %s "
			  "(PAM: %d)\n",
			  state->request.data.auth.user, 
			  state->response.data.auth.nt_status_string,
			  state->response.data.auth.pam_error));
		request_error(state);
		return;
	}

	sendto_domain(state, domain);
}

enum winbindd_result winbindd_dual_pam_logoff(struct winbindd_domain *domain,
					      struct winbindd_cli_state *state) 
{
	NTSTATUS result = NT_STATUS_NOT_SUPPORTED;
	struct WINBINDD_CCACHE_ENTRY *entry;
	int ret;

	DEBUG(3, ("[%5lu]: pam dual logoff %s\n", (unsigned long)state->pid,
		state->request.data.logoff.user));

	if (!(state->request.flags & WBFLAG_PAM_KRB5)) {
		result = NT_STATUS_OK;
		goto process_result;
	}

#ifdef HAVE_KRB5
	
	/* what we need here is to find the corresponding krb5 ccache name *we*
	 * created for a given username and destroy it (as the user who created it) */
	
	entry = get_ccache_by_username(state->request.data.logoff.user);
	if (entry == NULL) {
		DEBUG(10,("winbindd_pam_logoff: could not get ccname for user %s\n", 
			state->request.data.logoff.user));
		goto process_result;
	}

	DEBUG(10,("winbindd_pam_logoff: found ccache [%s]\n", entry->ccname));

	if (entry->uid < 0 || state->request.data.logoff.uid < 0) {
		DEBUG(0,("winbindd_pam_logoff: invalid uid\n"));
		goto process_result;
	}

	if (entry->uid != state->request.data.logoff.uid) {
		DEBUG(0,("winbindd_pam_logoff: uid's differ: %d != %d\n", 
			entry->uid, state->request.data.logoff.uid));
		goto process_result;
	}

	if (!strcsequal(entry->ccname, state->request.data.logoff.krb5ccname)) {
		DEBUG(0,("winbindd_pam_logoff: krb5ccnames differ: (daemon) %s != (client) %s\n", 
			entry->ccname, state->request.data.logoff.krb5ccname));
		goto process_result;
	}

	ret = ads_kdestroy(entry->ccname);

	if (ret) {
		DEBUG(0,("winbindd_pam_logoff: failed to destroy user ccache %s with: %s\n", 
			entry->ccname, error_message(ret)));
	} else {
		DEBUG(10,("winbindd_pam_logoff: successfully destroyed ccache %s for user %s\n", 
			entry->ccname, state->request.data.logoff.user));
		remove_ccache_by_ccname(entry->ccname);
	}

	result = krb5_to_nt_status(ret);
#else
	result = NT_STATUS_NOT_SUPPORTED;
#endif

process_result:
	state->response.data.auth.nt_status = NT_STATUS_V(result);
	fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
	fstrcpy(state->response.data.auth.error_string, get_friendly_nt_error_msg(result));
	state->response.data.auth.pam_error = nt_status_to_pam(result);

	return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
}