This file is indexed.

/usr/lib/python2.7/dist-packages/ipaserver/plugins/baseldap.py is in python-ipaserver 4.7.0~pre1+git20180411-2ubuntu2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
# Authors:
#   Pavel Zuna <pzuna@redhat.com>
#
# Copyright (C) 2009  Red Hat
# see file 'COPYING' for use and warranty information
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
"""
Base classes for LDAP plugins.
"""

import re
import time
from copy import deepcopy
import base64

import six

from ipalib import api, crud, errors
from ipalib import Method, Object
from ipalib import Flag, Int, Str
from ipalib.cli import to_cli
from ipalib import output
from ipalib.text import _
from ipalib.util import json_serialize, validate_hostname
from ipalib.capabilities import client_has_capability
from ipalib.messages import add_message, SearchResultTruncated
from ipapython.dn import DN, RDN
from ipapython.version import API_VERSION

if six.PY3:
    unicode = str

DNA_MAGIC = -1

global_output_params = (
    Flag('has_password',
        label=_('Password'),
    ),
    Str('member',
        label=_('Failed members'),
    ),
    Str('member_user?',
        label=_('Member users'),
    ),
    Str('member_group?',
        label=_('Member groups'),
    ),
    Str('memberof_group?',
        label=_('Member of groups'),
    ),
    Str('member_host?',
        label=_('Member hosts'),
    ),
    Str('member_hostgroup?',
        label=_('Member host-groups'),
    ),
    Str('memberof_hostgroup?',
        label=_('Member of host-groups'),
    ),
    Str('memberof_permission?',
        label=_('Permissions'),
    ),
    Str('memberof_privilege?',
        label='Privileges',
    ),
    Str('memberof_role?',
        label=_('Roles'),
    ),
    Str('memberof_sudocmdgroup?',
        label=_('Sudo Command Groups'),
    ),
    Str('member_privilege?',
        label='Granted to Privilege',
    ),
    Str('member_role?',
        label=_('Granting privilege to roles'),
    ),
    Str('member_netgroup?',
        label=_('Member netgroups'),
    ),
    Str('memberof_netgroup?',
        label=_('Member of netgroups'),
    ),
    Str('member_service?',
        label=_('Member services'),
    ),
    Str('member_servicegroup?',
        label=_('Member service groups'),
    ),
    Str('memberof_servicegroup?',
        label='Member of service groups',
    ),
    Str('member_hbacsvc?',
        label=_('Member HBAC service'),
    ),
    Str('member_hbacsvcgroup?',
        label=_('Member HBAC service groups'),
    ),
    Str('memberof_hbacsvcgroup?',
        label='Member of HBAC service groups',
    ),
    Str('member_sudocmd?',
        label='Member Sudo commands',
    ),
    Str('memberof_sudorule?',
        label='Member of Sudo rule',
    ),
    Str('memberof_hbacrule?',
        label='Member of HBAC rule',
    ),
    Str('memberindirect_user?',
        label=_('Indirect Member users'),
    ),
    Str('memberindirect_group?',
        label=_('Indirect Member groups'),
    ),
    Str('memberindirect_host?',
        label=_('Indirect Member hosts'),
    ),
    Str('memberindirect_hostgroup?',
        label=_('Indirect Member host-groups'),
    ),
    Str('memberindirect_role?',
        label=_('Indirect Member of roles'),
    ),
    Str('memberindirect_permission?',
        label=_('Indirect Member permissions'),
    ),
    Str('memberindirect_hbacsvc?',
        label=_('Indirect Member HBAC service'),
    ),
    Str('memberindirect_hbacsvcgrp?',
        label=_('Indirect Member HBAC service group'),
    ),
    Str('memberindirect_netgroup?',
        label=_('Indirect Member netgroups'),
    ),
    Str('memberofindirect_group?',
        label='Indirect Member of group',
    ),
    Str('memberofindirect_netgroup?',
        label='Indirect Member of netgroup',
    ),
    Str('memberofindirect_hostgroup?',
        label='Indirect Member of host-group',
    ),
    Str('memberofindirect_role?',
        label='Indirect Member of role',
    ),
    Str('memberofindirect_sudorule?',
        label='Indirect Member of Sudo rule',
    ),
    Str('memberofindirect_hbacrule?',
        label='Indirect Member of HBAC rule',
    ),
    Str('sourcehost',
        label=_('Failed source hosts/hostgroups'),
    ),
    Str('memberhost',
        label=_('Failed hosts/hostgroups'),
    ),
    Str('memberuser',
        label=_('Failed users/groups'),
    ),
    Str('memberservice',
        label=_('Failed service/service groups'),
    ),
    Str('failed',
        label=_('Failed to remove'),
        flags=['suppress_empty'],
    ),
    Str('ipasudorunas',
        label=_('Failed RunAs'),
    ),
    Str('ipasudorunasgroup',
        label=_('Failed RunAsGroup'),
    ),
)


def validate_add_attribute(ugettext, attr):
    validate_attribute(ugettext, 'addattr', attr)

def validate_set_attribute(ugettext, attr):
    validate_attribute(ugettext, 'setattr', attr)

def validate_del_attribute(ugettext, attr):
    validate_attribute(ugettext, 'delattr', attr)

def validate_attribute(ugettext, name, attr):
    m = re.match("\s*(.*?)\s*=\s*(.*?)\s*$", attr)
    if not m or len(m.groups()) != 2:
        raise errors.ValidationError(
            name=name, error=_('Invalid format. Should be name=value'))

def get_effective_rights(ldap, dn, attrs=None):
    assert isinstance(dn, DN)
    if attrs is None:
        attrs = ['*', 'nsaccountlock', 'cospriority']
    rights = ldap.get_effective_rights(dn, attrs)
    rdict = {}
    if 'attributelevelrights' in rights:
        rights = rights['attributelevelrights']
        rights = rights[0].split(', ')
        for r in rights:
            (k,v) = r.split(':')
            if v == 'none':
                # the string "none" means "no rights found"
                # see https://fedorahosted.org/freeipa/ticket/4359
                v = u''
            rdict[k.strip().lower()] = v

    return rdict

def entry_from_entry(entry, newentry):
    """
    Python is more or less pass-by-value except for immutable objects. So if
    you pass in a dict to a function you are free to change members of that
    dict but you can't create a new dict in the function and expect to replace
    what was passed in.

    In some post-op plugins that is exactly what we want to do, so here is a
    clumsy way around the problem.
    """

    # Wipe out the current data
    for e in list(entry):
        del entry[e]

    # Re-populate it with new wentry
    for e in newentry.keys():
        entry[e] = newentry[e]

def entry_to_dict(entry, **options):
    if options.get('raw', False):
        result = {}
        for attr in entry:
            if attr.lower() == 'attributelevelrights':
                value = entry[attr]
            elif entry.conn.get_attribute_type(attr) is bytes:
                value = entry.raw[attr]
            else:
                value = list(entry.raw[attr])
                for (i, v) in enumerate(value):
                    try:
                        value[i] = v.decode('utf-8')
                    except UnicodeDecodeError:
                        pass
            result[attr] = value
    else:
        result = dict((k.lower(), v) for (k, v) in entry.items())
    if options.get('all', False):
        result['dn'] = entry.dn
    return result

def pkey_to_unicode(key):
    if key is None:
        key = []
    elif not isinstance(key, (tuple, list)):
        key = [key]
    key = u','.join(unicode(k) for k in key)
    return key

def pkey_to_value(key, options):
    version = options.get('version', API_VERSION)
    if client_has_capability(version, 'primary_key_types'):
        return key
    return pkey_to_unicode(key)

def wait_for_value(ldap, dn, attr, value):
    """
    389-ds postoperation plugins are executed after the data has been
    returned to a client. This means that plugins that add data in a
    postop are not included in data returned to the user.

    The downside of waiting is that this increases the time of the
    command.

    The updated entry is returned.
    """
    # Loop a few times to give the postop-plugin a chance to complete
    # Don't sleep for more than 6 seconds.
    x = 0
    while x < 20:
        # sleep first because the first search, even on a quiet system,
        # almost always fails.
        time.sleep(.3)
        x = x + 1

        # FIXME: put a try/except around here? I think it is probably better
        # to just let the exception filter up to the caller.
        entry_attrs = ldap.get_entry(dn, ['*'])
        if attr in entry_attrs:
            if isinstance(entry_attrs[attr], (list, tuple)):
                values = [y.lower() for y in entry_attrs[attr]]
                if value.lower() in values:
                    break
            else:
                if value.lower() == entry_attrs[attr].lower():
                    break

    return entry_attrs


def validate_externalhost(ugettext, hostname):
    try:
        validate_hostname(hostname, check_fqdn=False, allow_underscore=True)
    except ValueError as e:
        return unicode(e)


external_host_param = Str('externalhost*', validate_externalhost,
        label=_('External host'),
        flags=['no_option'],
)


def add_external_pre_callback(membertype, ldap, dn, keys, options):
    """
    Pre callback to validate external members.

    This should be called by a command pre callback directly.

    membertype is the type of member
    """
    assert isinstance(dn, DN)

    # validate hostname with allowed underscore characters, non-fqdn
    # hostnames are allowed
    def validate_host(hostname):
        validate_hostname(hostname, check_fqdn=False, allow_underscore=True)

    if options.get(membertype):
        if membertype == 'host':
            validator = validate_host
        else:
            param = api.Object[membertype].primary_key

            def validator(value):
                value = param(value)
                param.validate(value)

        for value in options[membertype]:
            try:
                validator(value)
            except errors.ValidationError as e:
                raise errors.ValidationError(name=membertype, error=e.error)
            except ValueError as e:
                raise errors.ValidationError(name=membertype, error=e)
    return dn


def add_external_post_callback(ldap, dn, entry_attrs, failed, completed,
                               memberattr, membertype, externalattr,
                               normalize=True):
    """
    Takes the following arguments:
        failed - the list of failed entries, these are candidates for possible
                 external entries to add
        completed - the number of successfully added entries so far
        memberattr - the attribute name that IPA uses for membership natively
                     (e.g. memberhost)
        membertype - the object type of the member (e.g. host)
        externalattr - the attribute name that IPA uses to store the membership
                       of the entries that are not managed by IPA
                       (e.g. externalhost)

    Returns the number of completed entries so far (the number of entries
    handled by IPA incremented by the number of handled external entries) and
    dn.
    """
    assert isinstance(dn, DN)

    completed_external = 0

    # Sift through the failures. We assume that these are all
    # entries that aren't stored in IPA, aka external entries.
    if memberattr in failed and membertype in failed[memberattr]:
        entry_attrs_ = ldap.get_entry(dn, [externalattr])
        dn = entry_attrs_.dn
        members = entry_attrs.get(memberattr, [])
        external_entries = entry_attrs_.get(externalattr, [])
        lc_external_entries = set(e.lower() for e in external_entries)

        failed_entries = []
        for entry in failed[memberattr][membertype]:
            membername = entry[0].lower()
            member_dn = api.Object[membertype].get_dn(membername)
            assert isinstance(member_dn, DN)

            if (membername not in lc_external_entries and
                member_dn not in members):
                # Not an IPA entry, assume external
                if normalize:
                    external_entries.append(membername)
                else:
                    external_entries.append(entry[0])
                lc_external_entries.add(membername)
                completed_external += 1
            elif (membername in lc_external_entries and
               member_dn not in members):
                # Already an external member, reset the error message
                msg = unicode(errors.AlreadyGroupMember())
                newerror = (entry[0], msg)
                ind = failed[memberattr][membertype].index(entry)
                failed[memberattr][membertype][ind] = newerror
                failed_entries.append(membername)
            else:
                # Really a failure
                failed_entries.append(membername)

        if completed_external:
            entry_attrs_[externalattr] = external_entries
            try:
                ldap.update_entry(entry_attrs_)
            except errors.EmptyModlist:
                pass
            failed[memberattr][membertype] = failed_entries
            entry_attrs[externalattr] = external_entries

    return (completed + completed_external, dn)


def remove_external_post_callback(ldap, dn, entry_attrs, failed, completed,
                                  memberattr, membertype, externalattr):
    """
    Takes the following arguments:
        failed - the list of failed entries, these are candidates for possible
                 external entries to remove
        completed - the number of successfully removed entries so far
        memberattr - the attribute name that IPA uses for membership natively
                     (e.g. memberhost)
        membertype - the object type of the member (e.g. host)
        externalattr - the attribute name that IPA uses to store the membership
                       of the entries that are not managed by IPA
                       (e.g. externalhost)

    Returns the number of completed entries so far (the number of entries
    handled by IPA incremented by the number of handled external entries) and
    dn.
    """

    assert isinstance(dn, DN)

    # Run through the failures and gracefully remove any member defined
    # as an external member.
    completed_external = 0
    if memberattr in failed and membertype in failed[memberattr]:
        entry_attrs_ = ldap.get_entry(dn, [externalattr])
        dn = entry_attrs_.dn
        external_entries = entry_attrs_.get(externalattr, [])
        failed_entries = []

        for entry in failed[memberattr][membertype]:
            membername = entry[0].lower()
            if membername in external_entries or entry[0] in external_entries:
                try:
                    external_entries.remove(membername)
                except ValueError:
                    external_entries.remove(entry[0])
                completed_external += 1
            else:
                msg = unicode(errors.NotGroupMember())
                newerror = (entry[0], msg)
                ind = failed[memberattr][membertype].index(entry)
                failed[memberattr][membertype][ind] = newerror
                failed_entries.append(membername)

        if completed_external:
            entry_attrs_[externalattr] = external_entries
            try:
                ldap.update_entry(entry_attrs_)
            except errors.EmptyModlist:
                pass
            failed[memberattr][membertype] = failed_entries
            entry_attrs[externalattr] = external_entries

    return (completed + completed_external, dn)


def host_is_master(ldap, fqdn):
    """
    Check to see if this host is a master.

    Raises an exception if a master, otherwise returns nothing.
    """
    master_dn = DN(('cn', fqdn), ('cn', 'masters'), ('cn', 'ipa'), ('cn', 'etc'), api.env.basedn)
    try:
        ldap.get_entry(master_dn, ['objectclass'])
        raise errors.ValidationError(name='hostname', error=_('An IPA master host cannot be deleted or disabled'))
    except errors.NotFound:
        # Good, not a master
        return


def add_missing_object_class(ldap, objectclass, dn, entry_attrs=None, update=True):
    """
    Add object class if missing into entry. Fetches entry if not passed. Updates
    the entry by default.

    Returns the entry
    """

    if not entry_attrs:
        entry_attrs = ldap.get_entry(dn, ['objectclass'])
    if (objectclass.lower() not in (o.lower() for o in entry_attrs['objectclass'])):
        entry_attrs['objectclass'].append(objectclass)
        if update:
            ldap.update_entry(entry_attrs)
    return entry_attrs


class LDAPObject(Object):
    """
    Object representing a LDAP entry.
    """
    backend_name = 'ldap2'

    parent_object = ''
    container_dn = ''
    object_name = _('entry')
    object_name_plural = _('entries')
    object_class = []
    object_class_config = None
    # If an objectclass is possible but not default in an entry. Needed for
    # collecting attributes for ACI UI.
    possible_objectclasses = []
    limit_object_classes = [] # Only attributes in these are allowed
    disallow_object_classes = [] # Disallow attributes in these
    permission_filter_objectclasses = None
    search_attributes = []
    search_attributes_config = None
    default_attributes = []
    search_display_attributes = [] # attributes displayed in LDAPSearch
    hidden_attributes = ['objectclass', 'aci']
    # set rdn_attribute only if RDN attribute differs from primary key!
    rdn_attribute = ''
    uuid_attribute = ''
    attribute_members = {}
    allow_rename = False
    password_attributes = []
    # Can bind as this entry (has userPassword or krbPrincipalKey)
    bindable = False
    relationships = {
        # attribute: (label, inclusive param prefix, exclusive param prefix)
        'member': ('Member', '', 'no_'),
        'memberof': ('Member Of', 'in_', 'not_in_'),
        'memberindirect': (
            'Indirect Member', None, 'no_indirect_'
        ),
        'memberofindirect': (
            'Indirect Member Of', None, 'not_in_indirect_'
        ),
    }
    label = _('Entry')
    label_singular = _('Entry')
    managed_permissions = {}

    container_not_found_msg = _('container entry (%(container)s) not found')
    parent_not_found_msg = _('%(parent)s: %(oname)s not found')
    object_not_found_msg = _('%(pkey)s: %(oname)s not found')
    already_exists_msg = _('%(oname)s with name "%(pkey)s" already exists')

    def get_dn(self, *keys, **kwargs):
        if self.parent_object:
            parent_dn = self.api.Object[self.parent_object].get_dn(*keys[:-1])
        else:
            parent_dn = DN(self.container_dn, api.env.basedn)
        if self.rdn_attribute:
            try:
                entry_attrs = self.backend.find_entry_by_attr(
                    self.primary_key.name, keys[-1], self.object_class, [''],
                    DN(self.container_dn, api.env.basedn)
                )
            except errors.NotFound:
                pass
            else:
                return entry_attrs.dn
        if self.primary_key and keys[-1] is not None:
            return self.backend.make_dn_from_attr(
                self.primary_key.name, keys[-1], parent_dn
            )
        assert isinstance(parent_dn, DN)
        return parent_dn

    def get_dn_if_exists(self, *keys, **kwargs):
        dn = self.get_dn(*keys, **kwargs)
        entry = self.backend.get_entry(dn, [''])
        return entry.dn

    def get_primary_key_from_dn(self, dn):
        assert isinstance(dn, DN)
        try:
            if self.rdn_attribute:
                entry_attrs = self.backend.get_entry(
                    dn, [self.primary_key.name]
                )
                try:
                    return entry_attrs[self.primary_key.name][0]
                except (KeyError, IndexError):
                    return ''
        except errors.NotFound:
            pass
        try:
            return dn[self.primary_key.name]
        except KeyError:
            # The primary key is not in the DN.
            # This shouldn't happen, but we don't want a "show" command to
            # crash.
            # Just return the entire DN, it's all we have if the entry
            # doesn't exist
            return unicode(dn)

    def get_ancestor_primary_keys(self):
        if self.parent_object:
            parent_obj = self.api.Object[self.parent_object]
            for key in parent_obj.get_ancestor_primary_keys():
                yield key
            if parent_obj.primary_key:
                pkey = parent_obj.primary_key
                yield pkey.clone_rename(
                    parent_obj.name + pkey.name, required=True, query=True,
                    cli_name=parent_obj.name, label=pkey.label
                )

    def has_objectclass(self, classes, objectclass):
        oc = [x.lower() for x in classes]
        return objectclass.lower() in oc

    def convert_attribute_members(self, entry_attrs, *keys, **options):
        if options.get('raw', False):
            return

        container_dns = {}
        new_attrs = {}

        for attr in self.attribute_members:
            try:
                value = entry_attrs.raw[attr]
            except KeyError:
                continue
            del entry_attrs[attr]

            for member in value:
                memberdn = DN(member.decode('utf-8'))
                for ldap_obj_name in self.attribute_members[attr]:
                    ldap_obj = self.api.Object[ldap_obj_name]
                    try:
                        container_dn = container_dns[ldap_obj_name]
                    except KeyError:
                        container_dn = DN(ldap_obj.container_dn, api.env.basedn)
                        container_dns[ldap_obj_name] = container_dn

                    if memberdn.endswith(container_dn):
                        new_value = ldap_obj.get_primary_key_from_dn(memberdn)
                        new_attr_name = '%s_%s' % (attr, ldap_obj.name)
                        try:
                            new_attr = new_attrs[new_attr_name]
                        except KeyError:
                            new_attr = entry_attrs.setdefault(new_attr_name, [])
                            new_attrs[new_attr_name] = new_attr
                        new_attr.append(new_value)
                        break

    def get_indirect_members(self, entry_attrs, attrs_list):
        if 'memberindirect' in attrs_list:
            self.get_memberindirect(entry_attrs)
        if 'memberofindirect' in attrs_list:
            self.get_memberofindirect(entry_attrs)

    def get_memberindirect(self, group_entry):
        """
        Get indirect members
        """

        mo_filter = self.backend.make_filter({'memberof': group_entry.dn})
        filter = self.backend.combine_filters(
            ('(member=*)', mo_filter), self.backend.MATCH_ALL)
        try:
            result = self.backend.get_entries(
                self.api.env.basedn,
                filter=filter,
                attrs_list=['member'],
                size_limit=-1, # paged search will get everything anyway
                paged_search=True)
        except errors.NotFound:
            result = []

        indirect = set()
        for entry in result:
            indirect.update(entry.raw.get('member', []))
        indirect.difference_update(group_entry.raw.get('member', []))

        if indirect:
            group_entry.raw['memberindirect'] = list(indirect)

    def get_memberofindirect(self, entry):

        dn = entry.dn
        filter = self.backend.make_filter(
            {'member': dn, 'memberuser': dn, 'memberhost': dn})
        try:
            result = self.backend.get_entries(
                self.api.env.basedn,
                filter=filter,
                attrs_list=[''],
                size_limit=-1,  # paged search will get everything anyway
                paged_search=True)
        except errors.NotFound:
            result = []

        direct = set()
        indirect = set(entry.raw.get('memberof', []))
        for group_entry in result:
            dn = str(group_entry.dn).encode('utf-8')
            if dn in indirect:
                indirect.remove(dn)
                direct.add(dn)

        entry.raw['memberof'] = list(direct)
        if indirect:
            entry.raw['memberofindirect'] = list(indirect)

    def get_password_attributes(self, ldap, dn, entry_attrs):
        """
        Search on the entry to determine if it has a password or
        keytab set.

        A tuple is used to determine which attribute is set
        in entry_attrs. The value is set to True/False whether a
        given password type is set.
        """
        for (pwattr, attr) in self.password_attributes:
            search_filter = '(%s=*)' % pwattr
            try:
                ldap.find_entries(
                    search_filter, [pwattr], dn, ldap.SCOPE_BASE
                )
                entry_attrs[attr] = True
            except errors.NotFound:
                entry_attrs[attr] = False

    def handle_not_found(self, *keys):
        """Handle NotFound exception

        Must raise errors.NotFound again.
        """
        pkey = ''
        if self.primary_key:
            pkey = keys[-1]
        raise errors.NotFound(
            reason=self.object_not_found_msg % {
                'pkey': pkey, 'oname': self.object_name,
            }
        )

    def handle_duplicate_entry(self, *keys):
        try:
            pkey = keys[-1]
        except IndexError:
            pkey = ''
        raise errors.DuplicateEntry(
            message=self.already_exists_msg % {
                'pkey': pkey, 'oname': self.object_name,
            }
        )

    # list of attributes we want exported to JSON
    json_friendly_attributes = (
        'parent_object', 'container_dn', 'object_name', 'object_name_plural',
        'object_class', 'object_class_config', 'default_attributes', 'label', 'label_singular',
        'hidden_attributes', 'uuid_attribute', 'attribute_members', 'name',
        'takes_params', 'rdn_attribute', 'bindable', 'relationships',
    )

    def __json__(self):
        ldap = self.backend
        json_dict = dict(
            (a, json_serialize(getattr(self, a))) for a in self.json_friendly_attributes
        )
        if self.primary_key:
            json_dict['primary_key'] = self.primary_key.name
        objectclasses = self.object_class
        if self.object_class_config:
            config = ldap.get_ipa_config()
            objectclasses = config.get(
                self.object_class_config, objectclasses
            )
        objectclasses = objectclasses + self.possible_objectclasses
        # Get list of available attributes for this object for use
        # in the ACI UI.
        attrs = self.api.Backend.ldap2.schema.attribute_types(objectclasses)
        attrlist = []
        # Go through the MUST first
        for attr in attrs[0].values():
            attrlist.append(attr.names[0].lower())
        # And now the MAY
        for attr in attrs[1].values():
            attrlist.append(attr.names[0].lower())
        json_dict['aciattrs'] = attrlist
        attrlist.sort()
        json_dict['methods'] = [m for m in self.methods]
        json_dict['can_have_permissions'] = bool(
            self.permission_filter_objectclasses)
        return json_dict


# addattr can cause parameters to have more than one value even if not defined
# as multivalue, make sure this isn't the case
def _check_single_value_attrs(params, entry_attrs):
    for (a, v) in entry_attrs.items():
        if isinstance(v, (list, tuple)) and len(v) > 1:
            if a in params and not params[a].multivalue:
                raise errors.OnlyOneValueAllowed(attr=a)

# setattr or --option='' can cause parameters to be empty that are otherwise
# required, make sure we enforce that.
def _check_empty_attrs(params, entry_attrs):
    for (a, v) in entry_attrs.items():
        if v is None or (isinstance(v, six.string_types) and len(v) == 0):
            if a in params and params[a].required:
                raise errors.RequirementError(name=a)


def _check_limit_object_class(attributes, attrs, allow_only):
    """
    If the set of objectclasses is limited enforce that only those
    are updated in entry_attrs (plus dn)

    allow_only tells us what mode to check in:

    If True then we enforce that the attributes must be in the list of
    allowed.

    If False then those attributes are not allowed.
    """
    if len(attributes[0]) == 0 and len(attributes[1]) == 0:
        return
    limitattrs = deepcopy(attrs)
    # Go through the MUST first
    for attr in attributes[0].values():
        if attr.names[0].lower() in limitattrs:
            if not allow_only:
                raise errors.ObjectclassViolation(
                    info=_('attribute "%(attribute)s" not allowed') % dict(
                        attribute=attr.names[0].lower()))
            limitattrs.remove(attr.names[0].lower())
    # And now the MAY
    for attr in attributes[1].values():
        if attr.names[0].lower() in limitattrs:
            if not allow_only:
                raise errors.ObjectclassViolation(
                    info=_('attribute "%(attribute)s" not allowed') % dict(
                        attribute=attr.names[0].lower()))
            limitattrs.remove(attr.names[0].lower())
    if len(limitattrs) > 0 and allow_only:
        raise errors.ObjectclassViolation(
            info=_('attribute "%(attribute)s" not allowed') % dict(
                attribute=limitattrs[0]))


class BaseLDAPCommand(Method):
    """
    Base class for Base LDAP Commands.
    """
    setattr_option = Str('setattr*', validate_set_attribute,
                         cli_name='setattr',
                         doc=_("""Set an attribute to a name/value pair. Format is attr=value.
For multi-valued attributes, the command replaces the values already present."""),
                         exclude='webui',
                        )
    addattr_option = Str('addattr*', validate_add_attribute,
                         cli_name='addattr',
                         doc=_("""Add an attribute/value pair. Format is attr=value. The attribute
must be part of the schema."""),
                         exclude='webui',
                        )
    delattr_option = Str('delattr*', validate_del_attribute,
                         cli_name='delattr',
                         doc=_("""Delete an attribute/value pair. The option will be evaluated
last, after all sets and adds."""),
                         exclude='webui',
                        )

    callback_types = Method.callback_types + ('pre',
                                              'post',
                                              'exc')

    def get_summary_default(self, output):
        if 'value' in output:
            output = dict(output)
            output['value'] = pkey_to_unicode(output['value'])
        return super(BaseLDAPCommand, self).get_summary_default(output)

    def _convert_2_dict(self, ldap, attrs):
        """
        Convert a string in the form of name/value pairs into a dictionary.

        :param attrs: A list of name/value pair strings, in the "name=value"
            format. May also be a single string, or None.
        """

        newdict = {}
        if attrs is None:
            attrs = []
        elif type(attrs) not in (list, tuple):
            attrs = [attrs]
        for a in attrs:
            m = re.match("\s*(.*?)\s*=\s*(.*?)\s*$", a)
            attr = str(m.group(1)).lower()
            value = m.group(2)
            if attr in self.obj.params and attr not in self.params:
                # The attribute is managed by IPA, but it didn't get cloned
                # to the command. This happens with no_update/no_create attrs.
                raise errors.ValidationError(
                    name=attr, error=_('attribute is not configurable'))
            if len(value) == 0:
                # None means "delete this attribute"
                value = None

            if attr in newdict:
                if type(value) in (tuple,):
                    newdict[attr] += list(value)
                else:
                    newdict[attr].append(value)
            else:
                if type(value) in (tuple,):
                    newdict[attr] = list(value)
                else:
                    newdict[attr] = [value]
        return newdict

    def process_attr_options(self, entry_attrs, dn, keys, options):
        """
        Process all --setattr, --addattr, and --delattr options and add the
        resulting value to the list of attributes. --setattr is processed first,
        then --addattr and finally --delattr.

        When --setattr is not used then the original LDAP object is looked up
        (of course, not when dn is None) and the changes are applied to old
        object values.

        Attribute values deleted by --delattr may be deleted from attribute
        values set or added by --setattr, --addattr. For example, the following
        attributes will result in a NOOP:

        --addattr=attribute=foo --delattr=attribute=foo

        AttrValueNotFound exception may be raised when an attribute value was
        not found either by --setattr and --addattr nor in existing LDAP object.

        :param entry_attrs: A list of attributes that will be updated
        :param dn: dn of updated LDAP object or None if a new object is created
        :param keys: List of command arguments
        :param options: List of options
        """

        if all(k not in options for k in ("setattr", "addattr", "delattr")):
            return

        ldap = self.obj.backend

        adddict = self._convert_2_dict(ldap, options.get('addattr', []))
        setdict = self._convert_2_dict(ldap, options.get('setattr', []))
        deldict = self._convert_2_dict(ldap, options.get('delattr', []))

        setattrs = set(setdict)
        addattrs = set(adddict)
        delattrs = set(deldict)

        if dn is None:
            direct_add = addattrs
            direct_del = delattrs
            needldapattrs = []
        else:
            assert isinstance(dn, DN)
            direct_add = setattrs & addattrs
            direct_del = setattrs & delattrs
            needldapattrs = list((addattrs | delattrs) - setattrs)

        for attr, val in setdict.items():
            entry_attrs[attr] = val

        for attr in direct_add:
            try:
                val = entry_attrs[attr]
            except KeyError:
                val = []
            else:
                if not isinstance(val, (list, tuple)):
                    val = [val]
                elif isinstance(val, tuple):
                    val = list(val)
            val.extend(adddict[attr])
            entry_attrs[attr] = val

        for attr in direct_del:
            for delval in deldict[attr]:
                try:
                    entry_attrs[attr].remove(delval)
                except ValueError:
                    raise errors.AttrValueNotFound(attr=attr, value=delval)

        if needldapattrs:
            try:
                old_entry = self._exc_wrapper(keys, options, ldap.get_entry)(
                    dn, needldapattrs
                )
            except errors.NotFound:
                raise self.obj.handle_not_found(*keys)

            # Provide a nice error message when user tries to delete an
            # attribute that does not exist on the entry (and user is not
            # adding it)
            names = set(n.lower() for n in old_entry)
            del_nonexisting = delattrs - (names | setattrs | addattrs)
            if del_nonexisting:
                raise errors.ValidationError(name=del_nonexisting.pop(),
                    error=_('No such attribute on this entry'))

            for attr in needldapattrs:
                entry_attrs[attr] = old_entry.get(attr, [])

                if attr in addattrs:
                    entry_attrs[attr].extend(adddict.get(attr, []))

                for delval in deldict.get(attr, []):
                    try:
                        entry_attrs[attr].remove(delval)
                    except ValueError:
                        if isinstance(delval, bytes):
                            # This is a Binary value, base64 encode it
                            delval = base64.b64encode(delval).decode('ascii')
                        raise errors.AttrValueNotFound(attr=attr, value=delval)

        # normalize all values
        changedattrs = setattrs | addattrs | delattrs
        for attr in changedattrs:
            if attr in self.params and self.params[attr].attribute:
                # convert single-value params to scalars
                param = self.params[attr]
                value = entry_attrs[attr]
                if not param.multivalue:
                    if len(value) == 1:
                        value = value[0]
                    elif not value:
                        value = None
                    else:
                        raise errors.OnlyOneValueAllowed(attr=attr)
                # validate, convert and encode params
                try:
                    value = param(value)
                    param.validate(value)
                except errors.ValidationError as err:
                    raise errors.ValidationError(name=attr, error=err.error)
                except errors.ConversionError as err:
                    raise errors.ConversionError(name=attr, error=err.error)
                if isinstance(value, tuple):
                    value = list(value)
                entry_attrs[attr] = value
            else:
                # unknown attribute: remove duplicite and invalid values
                entry_attrs[attr] = list(set([val for val in entry_attrs[attr] if val]))
                if not entry_attrs[attr]:
                    entry_attrs[attr] = None
                elif isinstance(entry_attrs[attr], (tuple, list)) and len(entry_attrs[attr]) == 1:
                    entry_attrs[attr] = entry_attrs[attr][0]

    @classmethod
    def register_pre_callback(cls, callback, first=False):
        """Shortcut for register_callback('pre', ...)"""
        cls.register_callback('pre', callback, first)

    @classmethod
    def register_post_callback(cls, callback, first=False):
        """Shortcut for register_callback('post', ...)"""
        cls.register_callback('post', callback, first)

    @classmethod
    def register_exc_callback(cls, callback, first=False):
        """Shortcut for register_callback('exc', ...)"""
        cls.register_callback('exc', callback, first)

    def _exc_wrapper(self, keys, options, call_func):
        """Function wrapper that automatically calls exception callbacks"""
        def wrapped(*call_args, **call_kwargs):
            # call call_func first
            func = call_func
            callbacks = list(self.get_callbacks('exc'))
            while True:
                try:
                    return func(*call_args, **call_kwargs)
                except errors.ExecutionError as exc:
                    e = exc
                    if not callbacks:
                        raise
                    # call exc_callback in the next loop
                    callback = callbacks.pop(0)
                    def exc_func(*args, **kwargs):
                        return callback(
                            self, keys, options, e, call_func, *args, **kwargs)
                    func = exc_func
        return wrapped

    def get_options(self):
        for param in super(BaseLDAPCommand, self).get_options():
            yield param
        if self.obj.attribute_members:
            for o in self.has_output:
                if isinstance(o, (output.Entry, output.ListOfEntries)):
                    yield Flag('no_members',
                        doc=_('Suppress processing of membership attributes.'),
                        exclude='webui',
                        flags={'no_output'},
                    )
                    break

class LDAPCreate(BaseLDAPCommand, crud.Create):
    """
    Create a new entry in LDAP.
    """
    takes_options = (BaseLDAPCommand.setattr_option, BaseLDAPCommand.addattr_option)

    def get_args(self):
        for key in self.obj.get_ancestor_primary_keys():
            yield key
        for arg in super(LDAPCreate, self).get_args():
            yield arg

    has_output_params = global_output_params

    def execute(self, *keys, **options):
        ldap = self.obj.backend

        dn = self.obj.get_dn(*keys, **options)
        entry_attrs = ldap.make_entry(
            dn, self.args_options_2_entry(*keys, **options))

        self.process_attr_options(entry_attrs, None, keys, options)

        entry_attrs['objectclass'] = deepcopy(self.obj.object_class)

        if self.obj.object_class_config:
            config = ldap.get_ipa_config()
            entry_attrs['objectclass'] = config.get(
                self.obj.object_class_config, entry_attrs['objectclass']
            )

        if self.obj.uuid_attribute:
            entry_attrs[self.obj.uuid_attribute] = 'autogenerate'

        if self.obj.rdn_attribute:
            try:
                dn_attr = dn[0].attr
            except (IndexError, KeyError):
                dn_attr = None
            if dn_attr != self.obj.primary_key.name:
                self.obj.handle_duplicate_entry(*keys)
            entry_attrs.dn = ldap.make_dn(
                entry_attrs, self.obj.rdn_attribute,
                DN(self.obj.container_dn, api.env.basedn))

        if options.get('all', False):
            attrs_list = ['*'] + self.obj.default_attributes
        else:
            attrs_list = set(self.obj.default_attributes)
            attrs_list.update(entry_attrs.keys())
            if options.get('no_members', False):
                attrs_list.difference_update(self.obj.attribute_members)
            attrs_list = list(attrs_list)

        for callback in self.get_callbacks('pre'):
            entry_attrs.dn = callback(
                self, ldap, entry_attrs.dn, entry_attrs, attrs_list,
                *keys, **options)

        _check_single_value_attrs(self.params, entry_attrs)
        _check_limit_object_class(self.api.Backend.ldap2.schema.attribute_types(self.obj.limit_object_classes), list(entry_attrs), allow_only=True)
        _check_limit_object_class(self.api.Backend.ldap2.schema.attribute_types(self.obj.disallow_object_classes), list(entry_attrs), allow_only=False)

        try:
            self._exc_wrapper(keys, options, ldap.add_entry)(entry_attrs)
        except errors.NotFound:
            parent = self.obj.parent_object
            if parent:
                raise errors.NotFound(
                    reason=self.obj.parent_not_found_msg % {
                        'parent': keys[-2],
                        'oname': self.api.Object[parent].object_name,
                    }
                )
            raise errors.NotFound(
                reason=self.obj.container_not_found_msg % {
                    'container': self.obj.container_dn,
                }
            )
        except errors.DuplicateEntry:
            self.obj.handle_duplicate_entry(*keys)

        try:
            if self.obj.rdn_attribute:
                # make sure objectclass is either set or None
                if self.obj.object_class:
                    object_class = self.obj.object_class
                else:
                    object_class = None
                entry_attrs = self._exc_wrapper(keys, options, ldap.find_entry_by_attr)(
                    self.obj.primary_key.name, keys[-1], object_class, attrs_list,
                    DN(self.obj.container_dn, api.env.basedn)
                )
            else:
                entry_attrs = self._exc_wrapper(keys, options, ldap.get_entry)(
                    entry_attrs.dn, attrs_list)
        except errors.NotFound:
            raise self.obj.handle_not_found(*keys)

        self.obj.get_indirect_members(entry_attrs, attrs_list)

        for callback in self.get_callbacks('post'):
            entry_attrs.dn = callback(
                self, ldap, entry_attrs.dn, entry_attrs, *keys, **options)

        self.obj.convert_attribute_members(entry_attrs, *keys, **options)

        dn = entry_attrs.dn
        entry_attrs = entry_to_dict(entry_attrs, **options)
        entry_attrs['dn'] = dn

        if self.obj.primary_key:
            pkey = keys[-1]
        else:
            pkey = None

        return dict(result=entry_attrs, value=pkey_to_value(pkey, options))

    def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys, **options):
        assert isinstance(dn, DN)
        return dn

    def post_callback(self, ldap, dn, entry_attrs, *keys, **options):
        assert isinstance(dn, DN)
        return dn

    def exc_callback(self, keys, options, exc, call_func, *call_args, **call_kwargs):
        raise exc


class LDAPQuery(BaseLDAPCommand, crud.PKQuery):
    """
    Base class for commands that need to retrieve an existing entry.
    """
    def get_args(self):
        for key in self.obj.get_ancestor_primary_keys():
            yield key
        for arg in super(LDAPQuery, self).get_args():
            yield arg


class LDAPMultiQuery(LDAPQuery):
    """
    Base class for commands that need to retrieve one or more existing entries.
    """
    takes_options = (
        Flag('continue',
            cli_name='continue',
            doc=_('Continuous mode: Don\'t stop on errors.'),
        ),
    )

    def get_args(self):
        for arg in super(LDAPMultiQuery, self).get_args():
            if self.obj.primary_key and arg.name == self.obj.primary_key.name:
                yield arg.clone(multivalue=True)
            else:
                yield arg


class LDAPRetrieve(LDAPQuery):
    """
    Retrieve an LDAP entry.
    """
    has_output = output.standard_entry
    has_output_params = global_output_params

    takes_options = (
        Flag('rights',
            label=_('Rights'),
            doc=_('Display the access rights of this entry (requires --all). See ipa man page for details.'),
        ),
    )

    def execute(self, *keys, **options):
        ldap = self.obj.backend

        dn = self.obj.get_dn(*keys, **options)
        assert isinstance(dn, DN)

        if options.get('all', False):
            attrs_list = ['*'] + self.obj.default_attributes
        else:
            attrs_list = set(self.obj.default_attributes)
            if options.get('no_members', False):
                attrs_list.difference_update(self.obj.attribute_members)
            attrs_list = list(attrs_list)

        for callback in self.get_callbacks('pre'):
            dn = callback(self, ldap, dn, attrs_list, *keys, **options)
            assert isinstance(dn, DN)

        try:
            entry_attrs = self._exc_wrapper(keys, options, ldap.get_entry)(
                dn, attrs_list
            )
        except errors.NotFound:
            raise self.obj.handle_not_found(*keys)

        self.obj.get_indirect_members(entry_attrs, attrs_list)

        if options.get('rights', False) and options.get('all', False):
            entry_attrs['attributelevelrights'] = get_effective_rights(
                ldap, entry_attrs.dn)

        for callback in self.get_callbacks('post'):
            entry_attrs.dn = callback(
                self, ldap, entry_attrs.dn, entry_attrs, *keys, **options)

        self.obj.convert_attribute_members(entry_attrs, *keys, **options)

        dn = entry_attrs.dn
        entry_attrs = entry_to_dict(entry_attrs, **options)
        entry_attrs['dn'] = dn

        if self.obj.primary_key:
            pkey = keys[-1]
        else:
            pkey = None

        return dict(result=entry_attrs, value=pkey_to_value(pkey, options))

    def pre_callback(self, ldap, dn, attrs_list, *keys, **options):
        assert isinstance(dn, DN)
        return dn

    def post_callback(self, ldap, dn, entry_attrs, *keys, **options):
        assert isinstance(dn, DN)
        return dn

    def exc_callback(self, keys, options, exc, call_func, *call_args, **call_kwargs):
        raise exc


class LDAPUpdate(LDAPQuery, crud.Update):
    """
    Update an LDAP entry.
    """

    takes_options = (
        BaseLDAPCommand.setattr_option,
        BaseLDAPCommand.addattr_option,
        BaseLDAPCommand.delattr_option,
        Flag('rights',
            label=_('Rights'),
            doc=_('Display the access rights of this entry (requires --all). See ipa man page for details.'),
        ),
    )

    has_output_params = global_output_params

    def _get_rename_option(self):
        rdnparam = getattr(self.obj.params, self.obj.primary_key.name)
        return rdnparam.clone_rename('rename',
            cli_name='rename', required=False, label=_('Rename'),
            doc=_('Rename the %(ldap_obj_name)s object') % dict(
                ldap_obj_name=self.obj.object_name
            )
        )

    def get_options(self):
        for option in super(LDAPUpdate, self).get_options():
            yield option
        if self.obj.allow_rename:
            yield self._get_rename_option()

    def execute(self, *keys, **options):
        ldap = self.obj.backend

        if len(options) == 2: # 'all' and 'raw' are always sent
            raise errors.EmptyModlist()

        dn = self.obj.get_dn(*keys, **options)
        entry_attrs = ldap.make_entry(dn, self.args_options_2_entry(**options))

        self.process_attr_options(entry_attrs, dn, keys, options)

        if options.get('all', False):
            attrs_list = ['*'] + self.obj.default_attributes
        else:
            attrs_list = set(self.obj.default_attributes)
            attrs_list.update(entry_attrs.keys())
            if options.get('no_members', False):
                attrs_list.difference_update(self.obj.attribute_members)
            attrs_list = list(attrs_list)

        _check_single_value_attrs(self.params, entry_attrs)
        _check_empty_attrs(self.obj.params, entry_attrs)

        for callback in self.get_callbacks('pre'):
            entry_attrs.dn = callback(
                self, ldap, entry_attrs.dn, entry_attrs, attrs_list,
                *keys, **options)

        _check_limit_object_class(self.api.Backend.ldap2.schema.attribute_types(self.obj.limit_object_classes), list(entry_attrs), allow_only=True)
        _check_limit_object_class(self.api.Backend.ldap2.schema.attribute_types(self.obj.disallow_object_classes), list(entry_attrs), allow_only=False)

        rdnupdate = False
        if 'rename' in options:
            if not options['rename']:
                raise errors.ValidationError(
                    name='rename', error=u'can\'t be empty')
            entry_attrs[self.obj.primary_key.name] = options['rename']

        # if setattr was used to change the RDN, the primary_key.name is
        # already in entry_attrs
        if self.obj.allow_rename and self.obj.primary_key.name in entry_attrs:
            # perform RDN change if the primary key is also RDN
            if (RDN((self.obj.primary_key.name, keys[-1])) ==
                    entry_attrs.dn[0]):
                try:
                    new_dn = DN((self.obj.primary_key.name,
                                 entry_attrs[self.obj.primary_key.name]),
                                *entry_attrs.dn[1:])
                    self._exc_wrapper(keys, options, ldap.move_entry)(
                        entry_attrs.dn,
                        new_dn)

                    rdnkeys = (keys[:-1] +
                               (entry_attrs[self.obj.primary_key.name], ))
                    entry_attrs.dn = self.obj.get_dn(*rdnkeys)
                    options['rdnupdate'] = True
                    rdnupdate = True
                except errors.EmptyModlist:
                    # Attempt to rename to the current name, ignore
                    pass
                except errors.NotFound:
                    raise self.obj.handle_not_found(*keys)
                finally:
                    # Delete the primary_key from entry_attrs either way
                    del entry_attrs[self.obj.primary_key.name]

        try:
            # Exception callbacks will need to test for options['rdnupdate']
            # to decide what to do. An EmptyModlist in this context doesn't
            # mean an error occurred, just that there were no other updates to
            # perform.
            update = self._exc_wrapper(keys, options, ldap.get_entry)(
                entry_attrs.dn, list(entry_attrs))
            update.update(entry_attrs)

            self._exc_wrapper(keys, options, ldap.update_entry)(update)
        except errors.EmptyModlist as e:
            if not rdnupdate:
                raise e
        except errors.NotFound:
            raise self.obj.handle_not_found(*keys)

        try:
            entry_attrs = self._exc_wrapper(keys, options, ldap.get_entry)(
                entry_attrs.dn, attrs_list)
        except errors.NotFound:
            raise errors.MidairCollision(
                message=_('the entry was deleted while being modified')
            )

        self.obj.get_indirect_members(entry_attrs, attrs_list)

        if options.get('rights', False) and options.get('all', False):
            entry_attrs['attributelevelrights'] = get_effective_rights(
                ldap, entry_attrs.dn)

        for callback in self.get_callbacks('post'):
            entry_attrs.dn = callback(
                self, ldap, entry_attrs.dn, entry_attrs, *keys, **options)

        self.obj.convert_attribute_members(entry_attrs, *keys, **options)

        entry_attrs = entry_to_dict(entry_attrs, **options)

        if self.obj.primary_key:
            pkey = keys[-1]
        else:
            pkey = None

        return dict(result=entry_attrs, value=pkey_to_value(pkey, options))

    def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys, **options):
        assert isinstance(dn, DN)
        return dn

    def post_callback(self, ldap, dn, entry_attrs, *keys, **options):
        assert isinstance(dn, DN)
        return dn

    def exc_callback(self, keys, options, exc, call_func, *call_args, **call_kwargs):
        raise exc


class LDAPDelete(LDAPMultiQuery):
    """
    Delete an LDAP entry and all of its direct subentries.
    """
    has_output = output.standard_multi_delete

    has_output_params = global_output_params

    subtree_delete = True

    def execute(self, *keys, **options):
        ldap = self.obj.backend

        def delete_entry(pkey):
            nkeys = keys[:-1] + (pkey, )
            dn = self.obj.get_dn(*nkeys, **options)
            assert isinstance(dn, DN)

            for callback in self.get_callbacks('pre'):
                dn = callback(self, ldap, dn, *nkeys, **options)
                assert isinstance(dn, DN)

            def delete_subtree(base_dn):
                assert isinstance(base_dn, DN)
                truncated = True
                while truncated:
                    try:
                        (subentries, truncated) = ldap.find_entries(
                            None, [''], base_dn, ldap.SCOPE_ONELEVEL
                        )
                    except errors.NotFound:
                        break
                    else:
                        for entry_attrs in subentries:
                            delete_subtree(entry_attrs.dn)
                try:
                    self._exc_wrapper(nkeys, options, ldap.delete_entry)(
                        base_dn
                    )
                except errors.NotFound:
                    raise self.obj.handle_not_found(*nkeys)

            try:
                self._exc_wrapper(nkeys, options, ldap.delete_entry)(dn)
            except errors.NotFound:
                raise self.obj.handle_not_found(*nkeys)
            except errors.NotAllowedOnNonLeaf:
                if not self.subtree_delete:
                    raise
                # this entry is not a leaf entry, delete all child nodes
                delete_subtree(dn)

            for callback in self.get_callbacks('post'):
                result = callback(self, ldap, dn, *nkeys, **options)

            return result

        if self.obj.primary_key and isinstance(keys[-1], (list, tuple)):
            pkeyiter = keys[-1]
        elif keys[-1] is not None:
            pkeyiter = [keys[-1]]
        else:
            pkeyiter = []

        deleted = []
        failed = []
        for pkey in pkeyiter:
            try:
                delete_entry(pkey)
            except errors.ExecutionError:
                if not options.get('continue', False):
                    raise
                failed.append(pkey)
            else:
                deleted.append(pkey)
        deleted = pkey_to_value(deleted, options)
        failed = pkey_to_value(failed, options)

        return dict(result=dict(failed=failed), value=deleted)

    def pre_callback(self, ldap, dn, *keys, **options):
        assert isinstance(dn, DN)
        return dn

    def post_callback(self, ldap, dn, *keys, **options):
        assert isinstance(dn, DN)
        return True

    def exc_callback(self, keys, options, exc, call_func, *call_args, **call_kwargs):
        raise exc


class LDAPModMember(LDAPQuery):
    """
    Base class for member manipulation.
    """
    member_attributes = ['member']
    member_param_doc = _('%s')
    member_param_label = _('member %s')
    member_count_out = ('%i member processed.', '%i members processed.')

    def get_options(self):
        for option in super(LDAPModMember, self).get_options():
            yield option
        for attr in self.member_attributes:
            for ldap_obj_name in self.obj.attribute_members[attr]:
                ldap_obj = self.api.Object[ldap_obj_name]
                name = to_cli(ldap_obj_name)
                doc = self.member_param_doc % ldap_obj.object_name_plural
                label = self.member_param_label % ldap_obj.object_name
                yield Str('%s*' % name, cli_name='%ss' % name, doc=doc,
                          label=label, alwaysask=True)

    def get_member_dns(self, **options):
        dns = {}
        failed = {}
        for attr in self.member_attributes:
            dns[attr] = {}
            failed[attr] = {}
            for ldap_obj_name in self.obj.attribute_members[attr]:
                dns[attr][ldap_obj_name] = []
                failed[attr][ldap_obj_name] = []
                names = options.get(to_cli(ldap_obj_name), [])
                if not names:
                    continue
                for name in names:
                    if not name:
                        continue
                    ldap_obj = self.api.Object[ldap_obj_name]
                    try:
                        dns[attr][ldap_obj_name].append(ldap_obj.get_dn(name))
                    except errors.PublicError as e:
                        failed[attr][ldap_obj_name].append((name, unicode(e)))
        return (dns, failed)


class LDAPAddMember(LDAPModMember):
    """
    Add other LDAP entries to members.
    """
    member_param_doc = _('%s to add')
    member_count_out = ('%i member added.', '%i members added.')
    allow_same = False

    has_output = (
        output.Entry('result'),
        output.Output('failed',
            type=dict,
            doc=_('Members that could not be added'),
        ),
        output.Output('completed',
            type=int,
            doc=_('Number of members added'),
        ),
    )

    has_output_params = global_output_params

    def execute(self, *keys, **options):
        ldap = self.obj.backend

        (member_dns, failed) = self.get_member_dns(**options)

        dn = self.obj.get_dn(*keys, **options)
        assert isinstance(dn, DN)

        for callback in self.get_callbacks('pre'):
            dn = callback(self, ldap, dn, member_dns, failed, *keys, **options)
            assert isinstance(dn, DN)

        completed = 0
        for (attr, objs) in member_dns.items():
            for ldap_obj_name in objs:
                for m_dn in member_dns[attr][ldap_obj_name]:
                    assert isinstance(m_dn, DN)
                    if not m_dn:
                        continue
                    try:
                        ldap.add_entry_to_group(m_dn, dn, attr, allow_same=self.allow_same)
                    except errors.PublicError as e:
                        ldap_obj = self.api.Object[ldap_obj_name]
                        failed[attr][ldap_obj_name].append((
                            ldap_obj.get_primary_key_from_dn(m_dn),
                            unicode(e),)
                        )
                    else:
                        completed += 1

        if options.get('all', False):
            attrs_list = ['*'] + self.obj.default_attributes
        else:
            attrs_list = set(self.obj.default_attributes)
            attrs_list.update(member_dns.keys())
            if options.get('no_members', False):
                attrs_list.difference_update(self.obj.attribute_members)
            attrs_list = list(attrs_list)

        try:
            entry_attrs = self._exc_wrapper(keys, options, ldap.get_entry)(
                dn, attrs_list
            )
        except errors.NotFound:
            raise self.obj.handle_not_found(*keys)

        self.obj.get_indirect_members(entry_attrs, attrs_list)

        for callback in self.get_callbacks('post'):
            (completed, entry_attrs.dn) = callback(
                self, ldap, completed, failed, entry_attrs.dn, entry_attrs,
                *keys, **options)

        self.obj.convert_attribute_members(entry_attrs, *keys, **options)

        dn = entry_attrs.dn
        entry_attrs = entry_to_dict(entry_attrs, **options)
        entry_attrs['dn'] = dn

        return dict(
            completed=completed,
            failed=failed,
            result=entry_attrs,
        )

    def pre_callback(self, ldap, dn, found, not_found, *keys, **options):
        assert isinstance(dn, DN)
        return dn

    def post_callback(self, ldap, completed, failed, dn, entry_attrs, *keys, **options):
        assert isinstance(dn, DN)
        return (completed, dn)

    def exc_callback(self, keys, options, exc, call_func, *call_args, **call_kwargs):
        raise exc


class LDAPRemoveMember(LDAPModMember):
    """
    Remove LDAP entries from members.
    """
    member_param_doc = _('%s to remove')
    member_count_out = ('%i member removed.', '%i members removed.')

    has_output = (
        output.Entry('result'),
        output.Output('failed',
            type=dict,
            doc=_('Members that could not be removed'),
        ),
        output.Output('completed',
            type=int,
            doc=_('Number of members removed'),
        ),
    )

    has_output_params = global_output_params

    def execute(self, *keys, **options):
        ldap = self.obj.backend

        (member_dns, failed) = self.get_member_dns(**options)

        dn = self.obj.get_dn(*keys, **options)
        assert isinstance(dn, DN)

        for callback in self.get_callbacks('pre'):
            dn = callback(self, ldap, dn, member_dns, failed, *keys, **options)
            assert isinstance(dn, DN)

        completed = 0
        for (attr, objs) in member_dns.items():
            for ldap_obj_name, m_dns in objs.items():
                for m_dn in m_dns:
                    assert isinstance(m_dn, DN)
                    if not m_dn:
                        continue
                    try:
                        ldap.remove_entry_from_group(m_dn, dn, attr)
                    except errors.PublicError as e:
                        ldap_obj = self.api.Object[ldap_obj_name]
                        failed[attr][ldap_obj_name].append((
                            ldap_obj.get_primary_key_from_dn(m_dn),
                            unicode(e),)
                        )
                    else:
                        completed += 1

        if options.get('all', False):
            attrs_list = ['*'] + self.obj.default_attributes
        else:
            attrs_list = set(self.obj.default_attributes)
            attrs_list.update(member_dns.keys())
            if options.get('no_members', False):
                attrs_list.difference_update(self.obj.attribute_members)
            attrs_list = list(attrs_list)

        # Give memberOf a chance to update entries
        time.sleep(.3)

        try:
            entry_attrs = self._exc_wrapper(keys, options, ldap.get_entry)(
                dn, attrs_list
            )
        except errors.NotFound:
            raise self.obj.handle_not_found(*keys)

        self.obj.get_indirect_members(entry_attrs, attrs_list)

        for callback in self.get_callbacks('post'):
            (completed, entry_attrs.dn) = callback(
                self, ldap, completed, failed, entry_attrs.dn, entry_attrs,
                *keys, **options)

        self.obj.convert_attribute_members(entry_attrs, *keys, **options)

        dn = entry_attrs.dn
        entry_attrs = entry_to_dict(entry_attrs, **options)
        entry_attrs['dn'] = dn

        return dict(
            completed=completed,
            failed=failed,
            result=entry_attrs,
        )

    def pre_callback(self, ldap, dn, found, not_found, *keys, **options):
        assert isinstance(dn, DN)
        return dn

    def post_callback(self, ldap, completed, failed, dn, entry_attrs, *keys, **options):
        assert isinstance(dn, DN)
        return (completed, dn)

    def exc_callback(self, keys, options, exc, call_func, *call_args, **call_kwargs):
        raise exc


def gen_pkey_only_option(cli_name):
    return Flag('pkey_only?',
                label=_('Primary key only'),
                doc=_('Results should contain primary key attribute only ("%s")') \
                    % to_cli(cli_name),)

class LDAPSearch(BaseLDAPCommand, crud.Search):
    """
    Retrieve all LDAP entries matching the given criteria.
    """
    member_attributes = []
    member_param_incl_doc = _('Search for %(searched_object)s with these %(relationship)s %(ldap_object)s.')
    member_param_excl_doc = _('Search for %(searched_object)s without these %(relationship)s %(ldap_object)s.')

    # LDAPSearch sorts all matched records in the end using their primary key
    # as a key attribute
    # Set the following attribute to False to turn sorting off
    sort_result_entries = True

    takes_options = (
        Int('timelimit?',
            label=_('Time Limit'),
            doc=_('Time limit of search in seconds (0 is unlimited)'),
            flags=['no_display'],
            minvalue=0,
            autofill=False,
        ),
        Int('sizelimit?',
            label=_('Size Limit'),
            doc=_('Maximum number of entries returned (0 is unlimited)'),
            flags=['no_display'],
            minvalue=0,
            autofill=False,
        ),
    )

    def get_args(self):
        for key in self.obj.get_ancestor_primary_keys():
            yield key
        for arg in super(LDAPSearch, self).get_args():
            yield arg

    def get_member_options(self, attr):
        for ldap_obj_name in self.obj.attribute_members[attr]:
            ldap_obj = self.api.Object[ldap_obj_name]
            relationship = self.obj.relationships.get(
                attr, ['member', '', 'no_']
            )
            doc = self.member_param_incl_doc % dict(
                searched_object=self.obj.object_name_plural,
                relationship=relationship[0].lower(),
                ldap_object=ldap_obj.object_name_plural
            )
            name = '%s%s' % (relationship[1], to_cli(ldap_obj_name))
            yield ldap_obj.primary_key.clone_rename(
                '%s' % name, cli_name='%ss' % name, doc=doc,
                label=ldap_obj.object_name, multivalue=True, query=True,
                required=False, primary_key=False
            )
            doc = self.member_param_excl_doc % dict(
                searched_object=self.obj.object_name_plural,
                relationship=relationship[0].lower(),
                ldap_object=ldap_obj.object_name_plural
            )
            name = '%s%s' % (relationship[2], to_cli(ldap_obj_name))
            yield ldap_obj.primary_key.clone_rename(
                '%s' % name, cli_name='%ss' % name, doc=doc,
                label=ldap_obj.object_name, multivalue=True, query=True,
                required=False, primary_key=False
            )

    def get_options(self):
        for option in super(LDAPSearch, self).get_options():
            if option.name == 'no_members':
                # no_members are always true for find commands, do not
                # show option in CLI but keep API compatibility
                option = option.clone(
                    default=True, flags=option.flags | {"no_option"})
            yield option
        if self.obj.primary_key and \
                'no_output' not in self.obj.primary_key.flags:
            yield gen_pkey_only_option(self.obj.primary_key.cli_name)
        for attr in self.member_attributes:
            for option in self.get_member_options(attr):
                yield option

    def get_attr_filter(self, ldap, **options):
        """
        Returns a MATCH_ALL filter containing all required attributes from the
        options
        """
        search_kw = self.args_options_2_entry(**options)
        search_kw['objectclass'] = self.obj.object_class

        filters = []
        for name, value in search_kw.items():
            default = self.get_default_of(name, **options)
            fltr = ldap.make_filter_from_attr(name, value, ldap.MATCH_ALL)
            if default is not None and value == default:
                fltr = ldap.combine_filters([fltr, '(!({}=*))'.format(name)])
            filters.append(fltr)

        return ldap.combine_filters(filters, rules=ldap.MATCH_ALL)

    def get_term_filter(self, ldap, term):
        """
        Returns a filter to search for a value (term) in any of the
        search attributes of an entry.
        """
        if self.obj.search_attributes:
            search_attrs = self.obj.search_attributes
        else:
            search_attrs = self.obj.default_attributes
        if self.obj.search_attributes_config:
            config = ldap.get_ipa_config()
            config_attrs = config.get(
                self.obj.search_attributes_config, [])
            if len(config_attrs) == 1 and (
              isinstance(config_attrs[0], six.string_types)):
                search_attrs = config_attrs[0].split(',')

        search_kw = {}
        for a in search_attrs:
            search_kw[a] = term

        return ldap.make_filter(search_kw, exact=False)

    def get_member_filter(self, ldap, **options):
        filter = ''
        for attr in self.member_attributes:
            for ldap_obj_name in self.obj.attribute_members[attr]:
                ldap_obj = self.api.Object[ldap_obj_name]
                relationship = self.obj.relationships.get(
                    attr, ['member', '', 'no_']
                )
                # Handle positive (MATCH_ALL) and negative (MATCH_NONE)
                # searches similarly
                param_prefixes = relationship[1:]  # e.g. ('in_', 'not_in_')
                rules = ldap.MATCH_ALL, ldap.MATCH_NONE
                for param_prefix, rule in zip(param_prefixes, rules):
                    param_name = '%s%s' % (param_prefix, to_cli(ldap_obj_name))
                    if options.get(param_name):
                        dns = []
                        for pkey in options[param_name]:
                            dns.append(ldap_obj.get_dn(pkey))
                        flt = ldap.make_filter_from_attr(attr, dns, rule)
                        filter = ldap.combine_filters(
                            (filter, flt), ldap.MATCH_ALL
                        )
        return filter

    has_output_params = global_output_params

    def execute(self, *args, **options):
        ldap = self.obj.backend

        index = tuple(self.args).index('criteria')
        keys = args[:index]
        try:
            term = args[index]
        except IndexError:
            term = None
        if self.obj.parent_object:
            base_dn = self.api.Object[self.obj.parent_object].get_dn(*keys)
        else:
            base_dn = DN(self.obj.container_dn, api.env.basedn)
        assert isinstance(base_dn, DN)

        search_kw = self.args_options_2_entry(**options)

        if self.obj.search_display_attributes:
            defattrs = self.obj.search_display_attributes
        else:
            defattrs = self.obj.default_attributes

        if options.get('pkey_only', False):
            attrs_list = [self.obj.primary_key.name]
        elif options.get('all', False):
            attrs_list = ['*'] + defattrs
        else:
            attrs_list = set(defattrs)
            attrs_list.update(search_kw.keys())
            if options.get('no_members', False):
                attrs_list.difference_update(self.obj.attribute_members)
            attrs_list = list(attrs_list)

        attr_filter = self.get_attr_filter(ldap, **options)
        term_filter = self.get_term_filter(ldap, term)
        member_filter = self.get_member_filter(ldap, **options)

        filter = ldap.combine_filters(
            (term_filter, attr_filter, member_filter), rules=ldap.MATCH_ALL
        )

        scope = ldap.SCOPE_ONELEVEL
        for callback in self.get_callbacks('pre'):
            (filter, base_dn, scope) = callback(
                self, ldap, filter, attrs_list, base_dn, scope, *args, **options)
            assert isinstance(base_dn, DN)

        try:
            (entries, truncated) = self._exc_wrapper(args, options, ldap.find_entries)(
                filter, attrs_list, base_dn, scope,
                time_limit=options.get('timelimit', None),
                size_limit=options.get('sizelimit', None)
            )
        except errors.EmptyResult:
            (entries, truncated) = ([], False)
        except errors.NotFound:
            return self.api.Object[self.obj.parent_object].handle_not_found(
                *keys)

        for callback in self.get_callbacks('post'):
            truncated = callback(
                self, ldap, entries, truncated, *args, **options
            )

        if self.sort_result_entries:
            if self.obj.primary_key:
                def sort_key(x):
                    return self.obj.primary_key.sort_key(
                        x[self.obj.primary_key.name][0])
                entries.sort(key=sort_key)

        if not options.get('raw', False):
            for entry in entries:
                self.obj.get_indirect_members(entry, attrs_list)
                self.obj.convert_attribute_members(entry, *args, **options)

        for (i, e) in enumerate(entries):
            entries[i] = entry_to_dict(e, **options)
            entries[i]['dn'] = e.dn

        result = dict(
            result=entries,
            count=len(entries),
            truncated=bool(truncated),
        )

        try:
            ldap.handle_truncated_result(truncated)
        except errors.LimitsExceeded as exc:
            add_message(options['version'], result, SearchResultTruncated(
                reason=exc))

        return result

    def pre_callback(self, ldap, filters, attrs_list, base_dn, scope, *args, **options):
        assert isinstance(base_dn, DN)
        return (filters, base_dn, scope)

    def post_callback(self, ldap, entries, truncated, *args, **options):
        return truncated

    def exc_callback(self, args, options, exc, call_func, *call_args, **call_kwargs):
        raise exc


class LDAPModReverseMember(LDAPQuery):
    """
    Base class for reverse member manipulation.
    """
    reverse_attributes = ['member']
    reverse_param_doc = _('%s')
    reverse_count_out = ('%i member processed.', '%i members processed.')

    has_output_params = global_output_params

    def get_options(self):
        for option in super(LDAPModReverseMember, self).get_options():
            yield option
        for attr in self.reverse_attributes:
            for ldap_obj_name in self.obj.reverse_members[attr]:
                ldap_obj = self.api.Object[ldap_obj_name]
                name = to_cli(ldap_obj_name)
                doc = self.reverse_param_doc % ldap_obj.object_name_plural
                yield Str('%s*' % name, cli_name='%ss' % name, doc=doc,
                          label=ldap_obj.object_name, alwaysask=True)


class LDAPAddReverseMember(LDAPModReverseMember):
    """
    Add other LDAP entries to members in reverse.

    The call looks like "add A to B" but in fact executes
    add B to A to handle reverse membership.
    """
    member_param_doc = _('%s to add')
    member_count_out = ('%i member added.', '%i members added.')

    show_command = None
    member_command = None
    reverse_attr = None
    member_attr = None

    has_output = (
        output.Entry('result'),
        output.Output('failed',
            type=dict,
            doc=_('Members that could not be added'),
        ),
        output.Output('completed',
            type=int,
            doc=_('Number of members added'),
        ),
    )

    has_output_params = global_output_params

    def execute(self, *keys, **options):
        ldap = self.obj.backend

        # Ensure our target exists
        result = self.api.Command[self.show_command](keys[-1])['result']
        dn = result['dn']
        assert isinstance(dn, DN)

        for callback in self.get_callbacks('pre'):
            dn = callback(self, ldap, dn, *keys, **options)
            assert isinstance(dn, DN)

        if options.get('all', False):
            attrs_list = ['*'] + self.obj.default_attributes
        else:
            attrs_list = set(self.obj.default_attributes)
            if options.get('no_members', False):
                attrs_list.difference_update(self.obj.attribute_members)
            attrs_list = list(attrs_list)

        completed = 0
        failed = {'member': {self.reverse_attr: []}}
        for attr in options.get(self.reverse_attr) or []:
            try:
                options = {'%s' % self.member_attr: keys[-1]}
                try:
                    result = self._exc_wrapper(keys, options, self.api.Command[self.member_command])(attr, **options)
                    if result['completed'] == 1:
                        completed = completed + 1
                    else:
                        failed['member'][self.reverse_attr].append((attr, result['failed']['member'][self.member_attr][0][1]))
                except errors.NotFound as e:
                    msg = str(e)
                    (attr, msg) = msg.split(':', 1)
                    failed['member'][self.reverse_attr].append((attr, unicode(msg.strip())))

            except errors.PublicError as e:
                failed['member'][self.reverse_attr].append((attr, unicode(e)))

        # Update the member data.
        entry_attrs = ldap.get_entry(dn, attrs_list)
        self.obj.convert_attribute_members(entry_attrs, *keys, **options)

        for callback in self.get_callbacks('post'):
            (completed, entry_attrs.dn) = callback(
                self, ldap, completed, failed, entry_attrs.dn, entry_attrs,
                *keys, **options)

        dn = entry_attrs.dn
        entry_attrs = entry_to_dict(entry_attrs, **options)
        entry_attrs['dn'] = dn

        return dict(
            completed=completed,
            failed=failed,
            result=entry_attrs,
        )

    def pre_callback(self, ldap, dn, *keys, **options):
        assert isinstance(dn, DN)
        return dn

    def post_callback(self, ldap, completed, failed, dn, entry_attrs, *keys, **options):
        assert isinstance(dn, DN)
        return (completed, dn)

    def exc_callback(self, keys, options, exc, call_func, *call_args, **call_kwargs):
        raise exc


class LDAPRemoveReverseMember(LDAPModReverseMember):
    """
    Remove other LDAP entries from members in reverse.

    The call looks like "remove A from B" but in fact executes
    remove B from A to handle reverse membership.
    """
    member_param_doc = _('%s to remove')
    member_count_out = ('%i member removed.', '%i members removed.')

    show_command = None
    member_command = None
    reverse_attr = None
    member_attr = None

    has_output = (
        output.Entry('result'),
        output.Output('failed',
            type=dict,
            doc=_('Members that could not be removed'),
        ),
        output.Output('completed',
            type=int,
            doc=_('Number of members removed'),
        ),
    )

    has_output_params = global_output_params

    def execute(self, *keys, **options):
        ldap = self.obj.backend

        # Ensure our target exists
        result = self.api.Command[self.show_command](keys[-1])['result']
        dn = result['dn']
        assert isinstance(dn, DN)

        for callback in self.get_callbacks('pre'):
            dn = callback(self, ldap, dn, *keys, **options)
            assert isinstance(dn, DN)

        if options.get('all', False):
            attrs_list = ['*'] + self.obj.default_attributes
        else:
            attrs_list = set(self.obj.default_attributes)
            if options.get('no_members', False):
                attrs_list.difference_update(self.obj.attribute_members)
            attrs_list = list(attrs_list)

        completed = 0
        failed = {'member': {self.reverse_attr: []}}
        for attr in options.get(self.reverse_attr) or []:
            try:
                options = {'%s' % self.member_attr: keys[-1]}
                try:
                    result = self._exc_wrapper(keys, options, self.api.Command[self.member_command])(attr, **options)
                    if result['completed'] == 1:
                        completed = completed + 1
                    else:
                        failed['member'][self.reverse_attr].append((attr, result['failed']['member'][self.member_attr][0][1]))
                except errors.NotFound as e:
                    msg = str(e)
                    (attr, msg) = msg.split(':', 1)
                    failed['member'][self.reverse_attr].append((attr, unicode(msg.strip())))

            except errors.PublicError as e:
                failed['member'][self.reverse_attr].append((attr, unicode(e)))

        # Update the member data.
        entry_attrs = ldap.get_entry(dn, attrs_list)
        self.obj.convert_attribute_members(entry_attrs, *keys, **options)

        for callback in self.get_callbacks('post'):
            (completed, entry_attrs.dn) = callback(
                self, ldap, completed, failed, entry_attrs.dn, entry_attrs,
                *keys, **options)

        dn = entry_attrs.dn
        entry_attrs = entry_to_dict(entry_attrs, **options)
        entry_attrs['dn'] = dn

        return dict(
            completed=completed,
            failed=failed,
            result=entry_attrs,
        )

    def pre_callback(self, ldap, dn, *keys, **options):
        assert isinstance(dn, DN)
        return dn

    def post_callback(self, ldap, completed, failed, dn, entry_attrs, *keys, **options):
        assert isinstance(dn, DN)
        return (completed, dn)

    def exc_callback(self, keys, options, exc, call_func, *call_args, **call_kwargs):
        raise exc


class BaseLDAPModAttribute(LDAPQuery):

    attribute = None

    has_output = output.standard_entry

    def _get_attribute_param(self):
        arg = self.obj.params[self.attribute]
        attribute = 'virtual_attribute' not in arg.flags
        return arg.clone(required=True, attribute=attribute, alwaysask=True)

    def _update_attrs(self, update, entry_attrs):
        raise NotImplementedError("%s.update_attrs()", self.__class__.__name__)

    def execute(self, *keys, **options):
        ldap = self.obj.backend
        try:
            index = tuple(self.args).index(self.attribute)
        except ValueError:
            obj_keys = keys
        else:
            obj_keys = keys[:index]

        dn = self.obj.get_dn(*obj_keys, **options)
        entry_attrs = ldap.make_entry(dn, self.args_options_2_entry(
            *keys, **options))

        entry_attrs.pop(self.obj.primary_key.name, None)

        if options.get('all', False):
            attrs_list = ['*', self.obj.primary_key.name]
        else:
            attrs_list = {self.obj.primary_key.name}
            attrs_list.update(entry_attrs.keys())
            attrs_list = list(attrs_list)

        for callback in self.get_callbacks('pre'):
            entry_attrs.dn = callback(
                self, ldap, entry_attrs.dn, entry_attrs, attrs_list,
                *keys, **options)

        try:
            update = self._exc_wrapper(keys, options, ldap.get_entry)(
                entry_attrs.dn, list(entry_attrs))

            self._update_attrs(update, entry_attrs)

            self._exc_wrapper(keys, options, ldap.update_entry)(update)
        except errors.NotFound:
            raise self.obj.handle_not_found(*keys)

        try:
            entry_attrs = self._exc_wrapper(keys, options, ldap.get_entry)(
                entry_attrs.dn, attrs_list)
        except errors.NotFound:
            raise errors.MidairCollision(
                message=_('the entry was deleted while being modified')
            )

        for callback in self.get_callbacks('post'):
            entry_attrs.dn = callback(
                self, ldap, entry_attrs.dn, entry_attrs, *keys, **options)

        entry_attrs = entry_to_dict(entry_attrs, **options)

        if self.obj.primary_key:
            pkey = obj_keys[-1]
        else:
            pkey = None

        return dict(result=entry_attrs, value=pkey_to_value(pkey, options))

    def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys,
                     **options):
        assert isinstance(dn, DN)
        return dn

    def post_callback(self, ldap, dn, entry_attrs, *keys, **options):
        assert isinstance(dn, DN)
        return dn

    def exc_callback(self, keys, options, exc, call_func, *call_args,
                     **call_kwargs):
        raise exc


class BaseLDAPAddAttribute(BaseLDAPModAttribute):
    msg_summary = _('added attribute value to entry %(value)s')

    def _update_attrs(self, update, entry_attrs):
        for name, value in entry_attrs.items():
            old_value = set(update.get(name, []))
            value_to_add = set(value)

            if not old_value.isdisjoint(value_to_add):
                raise errors.ExecutionError(
                    message=_('\'%(attr)s\' already contains one or more '
                              'values') % dict(attr=name))

            update[name] = list(old_value | value_to_add)


class BaseLDAPRemoveAttribute(BaseLDAPModAttribute):
    msg_summary = _('removed attribute values from entry %(value)s')

    def _update_attrs(self, update, entry_attrs):
        for name, value in entry_attrs.items():
            old_value = set(update.get(name, []))
            value_to_remove = set(value)

            if not value_to_remove.issubset(old_value):
                raise errors.AttrValueNotFound(
                    attr=name, value=_("one or more values to remove"))

            update[name] = list(old_value - value_to_remove)


class LDAPModAttribute(BaseLDAPModAttribute):

    def get_args(self):
        for arg in super(LDAPModAttribute, self).get_args():
            yield arg

        yield self._get_attribute_param()


class LDAPAddAttribute(LDAPModAttribute, BaseLDAPAddAttribute):
    pass


class LDAPRemoveAttribute(LDAPModAttribute, BaseLDAPRemoveAttribute):
    pass


class LDAPModAttributeViaOption(BaseLDAPModAttribute):

    def get_options(self):
        for option in super(LDAPModAttributeViaOption, self).get_options():
            yield option

        yield self._get_attribute_param()


class LDAPAddAttributeViaOption(LDAPModAttributeViaOption,
                                BaseLDAPAddAttribute):
    pass


class LDAPRemoveAttributeViaOption(LDAPModAttributeViaOption,
                                   BaseLDAPRemoveAttribute):
    pass