summaryrefslogtreecommitdiff
path: root/usr/src/lib/print/libprint/common/nss_ldap.c
blob: 193ba50f77a4608668f2c00d2d7c2c3fb9ad59ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
#include <syslog.h>
#include <errno.h>
#include <pwd.h>
#include <libintl.h>
#include <netdb.h>	/* for rcmd() */

#include <ns.h>
#include <list.h>

#define	LDAP_REFERRALS
#include <lber.h>
#include <ldap.h>
#include <sys/systeminfo.h>


/*
 * This modules contains the code required to manipulate printer objects in
 * a LDAP directory for the Naming Service (NS) switch.
 * It can "add", "modify" and "delete" the objects on the given ldap server
 * and in the given NS domain DN, eg. "dc=mkg,dc=sun,dc=com".
 * Note: printers known to the naming service are contained in the RDN
 * "ou=printers" under the NS domain DN
 */

#define	PCONTAINER	"ou=printers"

/* attribute keywords */
#define	ATTR_DN		"dn"
#define	ATTR_OCLASS	"objectClass"
#define	ATTR_URI	"printer-uri"
#define	ATTR_PNAME	"printer-name"
#define	ATTR_XRISUP	"printer-xri-supported"
#define	ATTR_BSDADDR	"sun-printer-bsdaddr"
#define	ATTR_KVP	"sun-printer-kvp"

/* objectClass values */
#define	OCV_TOP		"top"
#define	OCV_PSERVICE	"printerService"
#define	OCV_SUNPRT	"sunPrinter"
#define	OCV_PABSTRACT	"printerAbstract"

/* xri-supported attribute value */
#define	AV_UNKNOWN	"unknown"


/*
 * LDAP objectclass atributes that the user can explicity change
 */

static const char *nsl_attr_printerService[] = {
	"printer-uri",
	"printer-xri-supported",
	/* Not allowed "printer-name", */
	"printer-natural-language-configured",
	"printer-location",
	"printer-info",
	"printer-more-info",
	"printer-make-and-model",
	"printer-charset-configured",
	"printer-charset-supported",
	"printer-generated-natural-language-supported",
	"printer-document-format-supported",
	"printer-color-supported",
	"printer-compression-supported",
	"printer-pages-per-minute",
	"printer-pages-per-minute-color",
	"printer-finishings-supported",
	"printer-number-up-supported",
	"printer-sides-supported",
	"printer-media-supported",
	"printer-media-local-supported",
	"printer-resolution-supported",
	"printer-print-quality-supported",
	"printer-job-priority-supported",
	"printer-copies-supported",
	"printer-job-k-octets-supported",
	"printer-current-operator",
	"printer-service-person",
	"printer-delivery-orientation-supported",
	"printer-stacking-order-supported",
	"printer-output-features-supported",
	(char *)NULL
};


static const char *nsl_attr_printerIPP[] = {
	"printer-ipp-versions-supported",
	"printer-multiple-document-jobs-supported",
	(char *)NULL
};

static const char *nsl_attr_sunPrinter[] = {
	/* Not allowed "sun-printer-bsdaddr", */
	/* Not allowed "sun-printer-kvp", */
	(char *)NULL
};


/*
 * List of LDAP attributes that user is not allowed to explicitly change
 */
static const char *nsl_attr_notAllowed[] = {
	ATTR_DN,
	ATTR_OCLASS,		/* objectclass */
	ATTR_PNAME,		/* printer-name */
	ATTR_BSDADDR,
	ATTR_KVP,
	(char *)NULL
};


static NSL_RESULT _connectToLDAP(ns_cred_t *cred, LDAP **ld);
static uchar_t *_constructPrinterDN(uchar_t *printerName,
				uchar_t *domainDN, char **attrList);
static NSL_RESULT _checkPrinterExists(LDAP *ld, uchar_t *printerName,
			uchar_t *domainDN, uchar_t **printerDN);
static NSL_RESULT _checkPrinterDNExists(LDAP *ld, uchar_t *objectDN);
static NSL_RESULT _checkSunPrinter(LDAP *ld, uchar_t *printerDN);
static NSL_RESULT _addNewPrinterObject(LDAP *ld, uchar_t *printerName,
					uchar_t *domainDN, char **attrList);
static NSL_RESULT _modifyPrinterObject(LDAP *ld, uchar_t *printerDN,
		uchar_t *printerName, uchar_t *domainDN, char **attrList);
static NSL_RESULT _checkAttributes(char **list);
static NSL_RESULT _addLDAPmodValue(LDAPMod ***attrs, char *type, char *value);
static NSL_RESULT _modLDAPmodValue(LDAPMod ***attrs, char *type, char *value);
static NSL_RESULT _constructAddLDAPMod(uchar_t *printerName,
					char **attrList,  LDAPMod ***attrs);
static NSL_RESULT _constructModLDAPMod(uchar_t *printerName, int sunPrinter,
			char **attrList, char ***oldKVPList, LDAPMod ***attrs);
static NSL_RESULT _compareURIinDNs(uchar_t *dn1, uchar_t *dn2);
static uchar_t *_getThisNSDomainDN(void);
static int _popen(char *cmd, char *results, int size);
static int _attrInList(char *attr, const char **list);
static int _attrInLDAPList(char *attr);
static NSL_RESULT _getCurrentKVPValues(LDAP *ld,
					uchar_t *objectDN, char ***list);
static void _freeList(char ***list);
static NSL_RESULT _modAttrKVP(char *value, char ***kvpList);
static NSL_RESULT _attrAddKVP(LDAPMod ***attrs, char **kvpList, int kvpExists);
static int _manageReferralCredentials(LDAP *ld, char **dn, char **credp,
	int *methodp, int freeit, void *);

/*
 * *****************************************************************************
 *
 * Function:    ldap_put_printer()
 *
 * Description: Action the request to change a printer object in the LDAP
 *              directory DIT. The object is either added, modified or deleted
 *              depending on the request's attribute list. A null list indicates
 *              the request is a delete.
 *              The object's DN is constructed from the supplied domain DN and
 *              a check is done to see if the object exists already, if it
 *              doesn't exist then this is a request to add a new object
 *              If a URI is given in the attribute list and it is different to
 *              the existing printing object's DN then the request will be
 *              rejected.
 *
 *
 * Parameters:
 * Input:       const ns_printer_t *printer
 *                - this structure contains the following :
 *                  char *printerName - name of the printer
 *                  ns_cred_t *cred - structure containing the ldap host and
 *                                port, user, password and NS domain DN for the
 *                                directory server to be updated.
 *                  char **attrList - pointer to a list of attribute key values
 *                                for the printer object. If the object does
 *                                not already exist then this list contains the
 *                                values for the new object, otherwise this list
 *                                is a list of attributes to modify. For modify
 *                                a null attribute value is a attribute delete
 *                                request. A NULL ptr = delete the object.
 * Output:      None
 *
 * Returns:     int - 0 = request actioned okay
 *                   !0 = error - see NSL_RESULT codes
 *
 * *****************************************************************************
 */

int
ldap_put_printer(const ns_printer_t *printer)

{
	NSL_RESULT result = NSL_OK;
	NSL_RESULT printerExists = NSL_ERR_UNKNOWN_PRINTER;
	LDAP *ld = NULL;
	uchar_t *printerDN = NULL;
	uchar_t *domainDN = NULL;
	char *printerName = NULL;
	ns_cred_t *cred = NULL;
	char **attrList = NULL;

	/* -------- */

	/*
	 * Note: the "attributes" list should be null for ldap as the attribute
	 * values are passed in the nsdata field
	 */

	if ((printer != NULL) &&
	    (printer->attributes == NULL) && (printer->name != NULL))
	{
		/* extract required pointer values from structure */

		printerName = printer->name;
		cred = printer->cred;
		if (printer->nsdata != NULL)
		{
			attrList = ((NS_LDAPDATA *)(printer->nsdata))->attrList;
		}

		/* connect and bind to the ldap directory server */

		result = _connectToLDAP(cred, &ld);
		if ((result == NSL_OK) && (ld != NULL))
		{
			/*
			 * check if the NS domain DN was given, if not use the
			 * current NS domain
			 */

			if (cred->domainDN != NULL)
			{
				domainDN = (uchar_t *)
					strdup((char *)cred->domainDN);
			}
			else
			{
				/* get DN of current domain */
				domainDN = _getThisNSDomainDN();
			}

			printerExists =
				_checkPrinterExists(ld, (uchar_t *)printerName,
							domainDN, &printerDN);
			if (printerExists != LDAP_SUCCESS)
			{
				/*
				 * could not find the printer by printer-name,
				 * but there could be a non sunPrinter object
				 * so if the printer-uri was given check if
				 * an object for that exists
				 */
				printerDN =
				    _constructPrinterDN(NULL,
							domainDN, attrList);
				if (printerDN != NULL)
				{
					printerExists = _checkPrinterDNExists(
								ld, printerDN);
				}
			}
#ifdef DEBUG
if (printerExists == NSL_OK)
{
printf("DN found = '%s' for '%s'\n", printerDN, printerName);
}
#endif

			if (attrList == NULL)
			{
				/*
				 * a null list indicates that this is a DELETE
				 * object request, so if object exists delete
				 * it, otherwise report an error.
				 */
				if (printerExists == LDAP_SUCCESS)
				{
				    result = ldap_delete_s(ld,
						(char *)printerDN);
				    if (result != LDAP_SUCCESS)
				    {
					result = NSL_ERR_DEL_FAILED;
#ifdef DEBUG
ldap_perror(ld, "ldap_delete_s failed");
#endif
				    }
				}
				else
				{
				    result = NSL_ERR_UNKNOWN_PRINTER;
				}
			}
			else
			{
				/*
				 * if object exists then this is a
				 * modify request otherwise is is an add request
				 */

				if (printerExists == LDAP_SUCCESS)
				{
					/*
					 * Modify the printer object to
					 * give it the new attribute values
					 * specified by the user
					 */
					result =
					_modifyPrinterObject(ld, printerDN,
						(uchar_t *)printerName,
						domainDN, attrList);
				}
				else
				{
					/*
					 * add new printer object into the
					 * ldap directory with the user
					 * specified attribute values
					 */
					result =
					    _addNewPrinterObject(ld,
						(uchar_t *)printerName,
						domainDN, attrList);
				}
			}

			if (printerDN != NULL)
			{
				free(printerDN);
			}
			if (domainDN != NULL)
			{
				free(domainDN);
			}

			/* disconnect from LDAP server */

			(void) ldap_unbind(ld);
		}
	}

	else
	{
		/* no printerName given */
		result = NSL_ERR_INTERNAL;
	}

	return ((int)result);
} /* ldap_put_printer */




/*
 * *****************************************************************************
 *
 * Function:    _connectToLDAP()
 *
 * Description: Setup the connection and bind to the LDAP directory server.
 *              The function returns the ldap connection descriptor
 *
 * Note:        Currently the native ldap functions do not support secure
 *              passwords, when this is supported this function will require
 *              updating to allow the type passed in cred->passwdType to
 *              be used with the ldap_simple_bind()
 *
 * Parameters:
 * Input:       ns_cred_t *cred - structure containing the credentials (host,
 *                                port, user and password) required to bind
 *                                to the directory server to be updated.
 *              char *printerName - printer name used only for error messages
 * Output:      LDAP** - ldap connection descriptor pointer. NULL = failed
 *
 * Returns:     NSL_RESULT - NSL_OK = connected okay
 *
 * *****************************************************************************
 */

static NSL_RESULT
_connectToLDAP(ns_cred_t *cred, LDAP **ld)

{
	NSL_RESULT result = NSL_OK;
	int lresult = 0;
	int ldapPort = LDAP_PORT;	/* default LDAP port number */
	int protoVersion = LDAP_VERSION3;
	int derefOption = LDAP_DEREF_NEVER;
	int referrals = 1;
	char hostname[MAXHOSTNAMELEN];
	int tmpMethod = LDAP_AUTH_SIMPLE; /* temp - until its passed in */

	/* -------- */

	if ((ld == NULL) || (cred == NULL) ||
		((cred->passwd == NULL) || (cred->binddn == NULL)))
	{
		result = NSL_ERR_CREDENTIALS;
	}

	else
	{
		*ld = NULL;

		/* if host was not given then bind to local host */

		if (cred->host != NULL)
		{
			(void) strlcpy(hostname, cred->host, sizeof (hostname));
		}
		else
		{
			(void) sysinfo(SI_HOSTNAME,
					hostname, sizeof (hostname));
		}

		/* initialise the connection to the ldap server */

		if (cred->port != 0)
		{
			ldapPort = cred->port;
		}
		*ld = ldap_init(hostname, ldapPort);
		if (*ld == NULL)
		{
			/* connection setup failed */
			result = NSL_ERR_CONNECT;
#ifdef DEBUG
(void) perror("ldap_init");
#endif
		}
		else
		{
			/* set ldap options */

			(void) ldap_set_option(*ld, LDAP_OPT_DEREF,
						&derefOption);
			(void) ldap_set_option(*ld, LDAP_OPT_PROTOCOL_VERSION,
						&protoVersion);
			(void) ldap_set_option(*ld, LDAP_OPT_REFERRALS,
						&referrals);

			/* bind to the user DN in the directory */

			/* cred->passwdType is currently not supported */

			lresult = ldap_simple_bind_s(*ld,
						cred->binddn, cred->passwd);

			/*
			 * before doing anything else, set up the function to
			 * call to get authentication details if the
			 * ldap update function calls (eg. ldap_add_s()) get a
			 * "referral" (to another ldap server) from the
			 * original ldap server, eg. if we are trying to do
			 * a update on a LDAP replica server.
			 */
			(void) _manageReferralCredentials(*ld,
					&(cred->binddn), &(cred->passwd),
					&tmpMethod, -1, NULL);
			ldap_set_rebind_proc(*ld,
				_manageReferralCredentials, NULL);

			if (lresult != LDAP_SUCCESS)
			{
				result = NSL_ERR_BIND;
				*ld = NULL;
#ifdef DEBUG
(void) ldap_perror(*ld, "ldap_simple_bind_s");
#endif
			}
		}
	}

	return (result);
} /* _connectToLDAP */





/*
 * *****************************************************************************
 *
 * Function:    _constructPrinterDN()
 *
 * Description: Construct the DN for the printer object from its name and NS
 *              domain DN. If the printer-uri is given in the attrList then
 *              that is used instead of the printerName.
 *
 * Parameters:
 * Input:       uchar_t *printerName
 *              uchar_t *domainDN
 *              char **attrList - this list is searched for printer-uri
 * Output:      None
 *
 * Returns:     uchar_t* - pointer to the DN, this memory is malloced so
 *                         must be freed using free() when finished with.
 *
 * *****************************************************************************
 */

static uchar_t *
_constructPrinterDN(uchar_t *printerName, uchar_t *domainDN, char **attrList)

{
	uchar_t *dn = NULL;
	uchar_t *uri = NULL;
	char **p = NULL;
	int len = 0;

	/* ------- */

	/* first search for printer-uri in the attribute list */

	for (p = attrList; (p != NULL) && (*p != NULL) && (uri == NULL); p++)
	{
		/* get length of this key word */

		for (len = 0; ((*p)[len] != '=') && ((*p)[len] != '\0'); len++);

		if ((strncasecmp(*p, ATTR_URI, len) == 0) &&
		    (strlen(*p) > len+1))
		{
			uri = (uchar_t *)&((*p)[len+1]);
		}
	}


	if (domainDN != NULL) {
		size_t size;

		/* malloc memory for the DN and then construct it */

		if ((uri == NULL) && (printerName != NULL))
		{
			/* use the printerName for the RDN */

			size = strlen(ATTR_URI) +
			    strlen((char *)printerName) +
			    strlen((char *)domainDN) +
			    strlen(PCONTAINER) +
			    10; /* plus a few extra */

			if ((dn = malloc(size)) != NULL)
				(void) snprintf((char *)dn, size, "%s=%s,%s,%s",
				ATTR_URI, printerName, PCONTAINER, domainDN);
		}
		else
		if (uri != NULL)
		{
			/* use the URI for the RDN */

			size = strlen(ATTR_URI) +
			    strlen((char *)uri) +
			    strlen((char *)domainDN) +
			    strlen(PCONTAINER) +
			    10; /* plus a few extra */

			if ((dn = malloc(size)) != NULL)
				(void) snprintf((char *)dn, size, "%s=%s,%s,%s",
				ATTR_URI, uri, PCONTAINER, domainDN);
		}

		/*
		 * else
		 * {
		 *    printName not given so return null
		 * }
		 */

	}

	return (dn);	/* caller must free this memory */
} /* _constructPrinterDN */



/*
 * *****************************************************************************
 *
 * Function:    _checkPrinterExists()
 *
 * Description: Check that the printer object for the printerName exists in the
 *              directory DIT and then extract the object's DN
 *              The function uses an exiting ldap connection and does a
 *              search for the printerName in the supplied domain DN.
 *
 * Parameters:
 * Input:       LDAP *ld             - existing ldap connection descriptor
 *              uchar_t *printerName - printer name
 *              uchar_t *domainDN    - DN of domain to search in
 * Output:      uchar_t **printerDN  - DN of the printer - the caller should
 *                                     free this memory using free()
 *
 * Result:      NSL_RESULT - NSL_OK = object exists
 *
 * *****************************************************************************
 */

static NSL_RESULT
_checkPrinterExists(LDAP *ld, uchar_t *printerName, uchar_t *domainDN,
			uchar_t **printerDN)

{
	NSL_RESULT result = NSL_ERR_UNKNOWN_PRINTER;
	int sresult = LDAP_NO_SUCH_OBJECT;
	LDAPMessage *ldapMsg = NULL;
	char *requiredAttrs[2] = { ATTR_PNAME, NULL };
	LDAPMessage *ldapEntry = NULL;
	uchar_t *filter = NULL;
	uchar_t *baseDN = NULL;

	/* ---------- */

	if ((printerName != NULL) && (domainDN != NULL) && (printerDN != NULL))
	{
		size_t size;

		if (printerDN != NULL)
		{
			*printerDN = NULL;
		}

		/* search for this Printer in the directory */

		size = (3 + strlen((char *)printerName) + strlen(ATTR_PNAME) +
			2);

		if ((filter = malloc(size)) != NULL)
			(void) snprintf((char *)filter, size, "(%s=%s)",
			    ATTR_PNAME, (char *)printerName);

		size = (strlen((char *)domainDN) + strlen(PCONTAINER) + 5);

		if ((baseDN = malloc(size)) != NULL)
			(void) snprintf((char *)baseDN, size, "%s,%s",
			    PCONTAINER, (char *)domainDN);

		sresult = ldap_search_s(ld, (char *)baseDN, LDAP_SCOPE_SUBTREE,
				(char *)filter, requiredAttrs, 0, &ldapMsg);
		if (sresult == LDAP_SUCCESS)
		{
			/* check that the object exists and extract its DN */

			ldapEntry = ldap_first_entry(ld, ldapMsg);
			if (ldapEntry != NULL)
			{
				/* object found - there should only be one */
				result = NSL_OK;

				if (printerDN != NULL)
				{
					*printerDN = (uchar_t *)
						ldap_get_dn(ld, ldapEntry);
				}
			}

			(void) ldap_msgfree(ldapMsg);
		}
	}

	else
	{
		result = NSL_ERR_INTERNAL;
	}

	return (result);
} /* _checkPrinterExists */




/*
 * *****************************************************************************
 *
 * Function:    _checkPrinterDNExists()
 *
 * Description: Check that the printer object for the DN exists in the
 *              directory DIT.
 *              The function uses an exiting ldap connection and does a
 *              search for the DN supplied.
 *
 * Parameters:  LDAP *ld       - existing ldap connection descriptor
 *              char *objectDN - DN to search for
 *
 * Result:      NSL_RESULT - NSL_OK = object exists
 *
 * *****************************************************************************
 */

static NSL_RESULT
_checkPrinterDNExists(LDAP *ld, uchar_t *objectDN)

{
	NSL_RESULT result = NSL_ERR_UNKNOWN_PRINTER;
	int sresult = LDAP_NO_SUCH_OBJECT;
	LDAPMessage *ldapMsg;
	char *requiredAttrs[2] = { ATTR_PNAME, NULL };
	LDAPMessage *ldapEntry;

	/* ---------- */

	if ((ld != NULL) && (objectDN != NULL))
	{
		/* search for this Printer in the directory */

		sresult = ldap_search_s(ld, (char *)objectDN, LDAP_SCOPE_BASE,
				"(objectclass=*)", requiredAttrs, 0, &ldapMsg);
		if (sresult == LDAP_SUCCESS)
		{
			/* check that the object exists */
			ldapEntry = ldap_first_entry(ld, ldapMsg);
			if (ldapEntry != NULL)
			{
				/* object found */
				result = NSL_OK;
			}

			(void) ldap_msgfree(ldapMsg);
		}
	}

	else
	{
		result = NSL_ERR_INTERNAL;
	}

	return (result);
} /* _checkPrinterDNExists */





/*
 * *****************************************************************************
 *
 * Function:    _checkSunPrinter()
 *
 * Description: Check that the printer object for the printerDN is a sunPrinter
 *              ie. it has the required objectclass attribute value.
 *
 * Parameters:
 * Input:       LDAP *ld            - existing ldap connection descriptor
 * Output:      uchar_t *printerDN  - DN of the printer
 *
 * Result:      NSL_RESULT - NSL_OK = object exists and is a sunPrinter
 *
 * *****************************************************************************
 */

static NSL_RESULT
_checkSunPrinter(LDAP *ld, uchar_t *printerDN)

{
	NSL_RESULT result = NSL_ERR_UNKNOWN_PRINTER;
	int sresult = LDAP_NO_SUCH_OBJECT;
	char *requiredAttrs[2] = { ATTR_PNAME, NULL };
	LDAPMessage *ldapMsg = NULL;
	LDAPMessage *ldapEntry = NULL;
	char *filter = NULL;

	/* ---------- */

	if ((ld != NULL) && (printerDN != NULL))
	{
		size_t size;

		/* search for this Printer in the directory */

		size = (3 + strlen(OCV_SUNPRT) + strlen(ATTR_OCLASS) + 2);
		if ((filter = malloc(size)) != NULL)
			(void) snprintf(filter, size, "(%s=%s)",
					ATTR_OCLASS, OCV_SUNPRT);

		sresult = ldap_search_s(ld, (char *)printerDN,
						LDAP_SCOPE_SUBTREE, filter,
						requiredAttrs, 0, &ldapMsg);
		if (sresult == LDAP_SUCCESS)
		{
			/* check that the printer object exists */

			ldapEntry = ldap_first_entry(ld, ldapMsg);
			if (ldapEntry != NULL)
			{
				/* object is a sunPrinter */
				result = NSL_OK;
			}

			(void) ldap_msgfree(ldapMsg);
		}
	}

	else
	{
		result = NSL_ERR_INTERNAL;
	}

	return (result);
} /* _checkSunPrinter */





/*
 * *****************************************************************************
 *
 * Function:    _addNewPrinterObject()
 *
 * Description: For the given printerName add a printer object into the
 *              LDAP directory NS domain. The object is created with the
 *              supplied attribute values. Note: if the printer's uri is
 *              given that is used as the RDN otherwise the printer's
 *              name is used as the RDN
 *
 * Parameters:
 * Input:       LDAP    *ld        - existing ldap connection descriptor
 *              uchar_t *printerName - Name of printer to be added
 *              uchar_t *domainDN    - DN of the domain to add the printer
 *              char    **attrList - user specified attribute values list
 * Output:      None
 *
 * Returns:     NSL_RESULT - NSL_OK  = request actioned okay
 *                           !NSL_OK = error
 *
 * *****************************************************************************
 */

static NSL_RESULT
_addNewPrinterObject(LDAP *ld, uchar_t *printerName,
			uchar_t *domainDN, char **attrList)

{
	NSL_RESULT result = NSL_ERR_ADD_FAILED;
	int lresult = 0;
	uchar_t *printerDN = NULL;
	LDAPMod **attrs = NULL;

	/* ---------- */

	if ((ld != NULL) && (printerName != NULL) && (domainDN != NULL) &&
		(attrList != NULL) && (attrList[0] != NULL))
	{
		result = _checkAttributes(attrList);

		if (result == NSL_OK)
		{
			/*
			 * construct a DN for the printer from the
			 * printerName and printer-uri if given.
			 */
			printerDN = _constructPrinterDN(printerName,
						domainDN, attrList);
			if (printerDN != NULL)
			{
				/*
				 * setup attribute values in an LDAPMod
				 * structure and then add the object
				 */
				result = _constructAddLDAPMod(printerName,
							attrList, &attrs);
				if (result == NSL_OK)
				{
					lresult = ldap_add_s(ld,
						    (char *)printerDN, attrs);
					if (lresult == LDAP_SUCCESS)
					{
						result = NSL_OK;
					}
					else
					{
						result = NSL_ERR_ADD_FAILED;
#ifdef DEBUG
(void) ldap_perror(ld, "ldap_add_s");
#endif
					}

					(void) ldap_mods_free(attrs, 1);
				}
				free(printerDN);
			}

			else
			{
				result = NSL_ERR_INTERNAL;
			}
		}
	}

	else
	{
		result = NSL_ERR_INTERNAL;
	}

	return (result);
} /* _addNewPrinterObject */






/*
 * *****************************************************************************
 *
 * Function:    _modifyPrinterObject()
 *
 * Description: Modify the given LDAP printer object to set the new attributes
 *              in the attribute list. If the printer's URI (specified in the
 *              attrList) changes the URI of the object the request is rejected.
 *
 * Parameters:
 * Input:       LDAP    *ld        - existing ldap connection descriptor
 *              uchar_t *printerDN - DN of printer object to modify
 *              uchar_t *printerName - Name of printer to be modified
 *              uchar_t *domainDN    - DN of the domain the printer is in
 *              char    **attrList - user specified attribute values list
 * Output:      None
 *
 * Returns:     NSL_RESULT - NSL_OK = object modified okay
 *
 * *****************************************************************************
 */

static NSL_RESULT
_modifyPrinterObject(LDAP *ld, uchar_t *printerDN,
		uchar_t *printerName, uchar_t *domainDN, char **attrList)

{
	NSL_RESULT result = NSL_ERR_INTERNAL;
	int lresult = 0;
	int sunPrinter = 0;
	uchar_t *uriDN = NULL;
	LDAPMod **attrs = NULL;
	char **kvpList = NULL;

	/* ---------- */

	if ((ld != NULL) && (printerDN != NULL) && (printerName != NULL) &&
	    (domainDN != NULL) && (attrList != NULL) && (attrList[0] != NULL))
	{
		result = _checkAttributes(attrList);

		if (result == NSL_OK)
		{
			/*
			 * The user may have requested that the printer object
			 * be given a new URI RDN, so construct a DN for the
			 * printer from the printerName or the printer-uri (if
			 * given).
			 */
			uriDN = _constructPrinterDN(NULL, domainDN, attrList);

			/*
			 * compare the 2 DNs to see if the URI has changed,
			 * if uriDN is null then the DN hasn't changed
			 */
			if ((uriDN == NULL) || ((uriDN != NULL) &&
			    (_compareURIinDNs(printerDN, uriDN) == NSL_OK)))
			{
				/*
				 * setup the modify object LDAPMod
				 * structure and then do the modify
				 */

				if (_checkSunPrinter(ld, printerDN) == NSL_OK)
				{
					sunPrinter = 1;
				}

				(void) _getCurrentKVPValues(ld,
							printerDN, &kvpList);

				result = _constructModLDAPMod(printerName,
							sunPrinter, attrList,
							&kvpList, &attrs);
				_freeList(&kvpList);

				if ((result == NSL_OK) && (attrs != NULL))
				{
					lresult = ldap_modify_s(
						ld, (char *)printerDN, attrs);
					if (lresult == LDAP_SUCCESS)
					{
						result = NSL_OK;
					}
					else
					{
						result = NSL_ERR_MOD_FAILED;
#ifdef DEBUG
(void) ldap_perror(ld, "ldap_modify_s");
#endif
					}

					(void) ldap_mods_free(attrs, 1);
				}
			}
			else
			{
				/*
				 * printer-uri name change has been requested
				 * this is NOT allowed as it requires that
				 * a new printer object is created
				 */
				result = NSL_ERR_RENAME;  /* NOT ALLOWED */
			}

			if (uriDN != NULL)
			{
				free(uriDN);
			}
		}
	}

	return (result);
} /* _modifyPrinterObject */




/*
 * *****************************************************************************
 *
 * Function:    _checkAttributes()
 *
 * Description: Check that the given attribute lists does not contain any
 *              key words that are not allowed.
 *
 * Parameters:
 * Input:       char **list - attribute list to check
 * Output:      None
 *
 * Returns:     NSL_RESULT - NSL_OK = checked okay
 *
 * *****************************************************************************
 */

static NSL_RESULT
_checkAttributes(char **list)

{
	NSL_RESULT result = NSL_OK;
	int len = 0;
	char *attr = NULL;
	char **p = NULL;

	/* ------ */

	for (p = list; (p != NULL) && (*p != NULL) && (result == NSL_OK); p++)
	{
		/* get length of this key word */

		for (len = 0; ((*p)[len] != '=') && ((*p)[len] != '\0'); len++);

		/* check if the key word is allowed */

		if (strncasecmp(*p, ATTR_KVP, len) == 0)
		{
			/* not supported through this interface */
			result = NSL_ERR_KVP;
		}
		else
		if (strncasecmp(*p, ATTR_BSDADDR, len) == 0)
		{
			/* not supported through this interface */
			result = NSL_ERR_BSDADDR;
		}
		else
		if (strncasecmp(*p, ATTR_PNAME, len) == 0)
		{
			/* not supported through this interface */
			result = NSL_ERR_PNAME;
		}
		else
		{
			/* check for any others */

			attr = strdup(*p);
			attr[len] = '\0'; /* terminate the key */

			if (_attrInList(attr, nsl_attr_notAllowed))
			{
				result = NSL_ERR_NOTALLOWED;
			}
		}

	}

	return (result);
} /* _checkAttributes */




/*
 * *****************************************************************************
 *
 * Function:    _addLDAPmodValue()
 *
 * Description: Add the given attribute and its value to the LDAPMod array.
 *              If this is the first entry in the array then create it.
 *
 * Parameters:
 * Input:       LDAPMod ***attrs  - array to update
 *              char *type        - attribute to add into array
 *              char *value       - attribute value
 * Output:      None
 *
 * Returns:     NSL_RESULT - NSL_OK = added okay
 *
 * *****************************************************************************
 */

static NSL_RESULT
_addLDAPmodValue(LDAPMod ***attrs, char *type, char *value)

{
	int i = 0;
	int j = 0;
	NSL_RESULT result = NSL_OK;

	/* ---------- */

	if ((attrs != NULL) && (type != NULL) && (value != NULL))
	{
#ifdef DEBUG
printf("_addLDAPmodValue() type='%s', value='%s'\n", type, value);
#endif
		/* search the existing LDAPMod array for the attribute */

		for (i = 0; *attrs != NULL && (*attrs)[i] != NULL; i++)
		{
			if (strcasecmp((*attrs)[i]->mod_type, type) == 0)
			{
				break;
			}
		}

		if (*attrs == NULL)
		{
			/* array empty so create it */

			*attrs = (LDAPMod **)calloc(1, 2 * sizeof (LDAPMod *));
			if (*attrs != NULL)
			{
				i = 0;
			}
			else
			{
				result = NSL_ERR_MEMORY;
			}

		}
		else
		if ((*attrs)[i] == NULL)
		{
			*attrs = (LDAPMod **)
				realloc(*attrs, (i+2) * sizeof (LDAPMod *));
			if (*attrs == NULL)
			{
				result = NSL_ERR_MEMORY;
			}
		}
	}
	else
	{
		result = NSL_ERR_INTERNAL;
	}

	if (result == NSL_OK)
	{
		if ((*attrs)[i] == NULL)
		{
			/* We've got a new slot. Create the new mod. */

			(*attrs)[i] = (LDAPMod *) malloc(sizeof (LDAPMod));
			if ((*attrs)[i] != NULL)
			{
				(*attrs)[i]->mod_op = LDAP_MOD_ADD;
				(*attrs)[i]->mod_type = strdup(type);
				(*attrs)[i]->mod_values = (char **)
						malloc(2 * sizeof (char *));
				if ((*attrs)[i]->mod_values  != NULL)
				{
					(*attrs)[i]->mod_values[0] =
								strdup(value);
					(*attrs)[i]->mod_values[1] = NULL;
					(*attrs)[i+1] = NULL;
				}
				else
				{
					result = NSL_ERR_MEMORY;
				}
			}
			else
			{
				result = NSL_ERR_MEMORY;
			}
		}

		else
		{
			/* Found an existing entry so add value to it */

			for (j = 0; (*attrs)[i]->mod_values[j] != NULL; j++);

			(*attrs)[i]->mod_values =
				(char **)realloc((*attrs)[i]->mod_values,
						(j + 2) * sizeof (char *));
			if ((*attrs)[i]->mod_values != NULL)
			{
				(*attrs)[i]->mod_values[j] = strdup(value);
				(*attrs)[i]->mod_values[j+1] = NULL;
			}
			else
			{
				result = NSL_ERR_MEMORY;
			}
		}
	}

	return (result);
} /* _addLDAPmodValue */




/*
 * *****************************************************************************
 *
 * Function:    _modLDAPmodValue()
 *
 * Description: Add the given attribute modify operation and its value into
 *              the LDAPMod array. This will either be a "replace" or a
 *              "delete"; value = null implies a "delete".
 *              If this is the first entry in the array then create it.
 *
 * Parameters:
 * Input:       LDAPMod ***attrs  - array to update
 *              char *type        - attribute to modify
 *              char *value       - attribute value, null implies "delete"
 * Output:      None
 *
 * Returns:     NSL_RESULT - NSL_OK = added okay
 *
 * *****************************************************************************
 */

static NSL_RESULT
_modLDAPmodValue(LDAPMod ***attrs, char *type, char *value)

{
	int i = 0;
	int j = 0;
	NSL_RESULT result = NSL_OK;

	/* ---------- */

	if ((attrs != NULL) && (type != NULL))
	{
#ifdef DEBUG
if (value != NULL)
printf("_modLDAPmodValue() REPLACE type='%s', value='%s'\n", type, value);
else
printf("_modLDAPmodValue() DELETE type='%s'\n", type);
#endif
		/* search the existing LDAPMod array for the attribute */

		for (i = 0; *attrs != NULL && (*attrs)[i] != NULL; i++)
		{
			if (strcasecmp((*attrs)[i]->mod_type, type) == 0)
			{
				break;
			}
		}

		if (*attrs == NULL)
		{
			/* array empty so create it */

			*attrs = (LDAPMod **)calloc(1, 2 * sizeof (LDAPMod *));
			if (*attrs != NULL)
			{
				i = 0;
			}
			else
			{
				result = NSL_ERR_MEMORY;
			}

		}
		else
		if ((*attrs)[i] == NULL)
		{
			/* attribute not found in array so add slot for it */

			*attrs = (LDAPMod **)
				realloc(*attrs, (i+2) * sizeof (LDAPMod *));
			if (*attrs == NULL)
			{
				result = NSL_ERR_MEMORY;
			}
		}
	}
	else
	{
		result = NSL_ERR_INTERNAL;
	}

	if (result == NSL_OK)
	{
		if ((*attrs)[i] == NULL)
		{
			/* We've got a new slot. Create the new mod entry */

			(*attrs)[i] = (LDAPMod *) malloc(sizeof (LDAPMod));
			if (((*attrs)[i] != NULL) && (value != NULL))
			{
				/* Do an attribute replace */

				(*attrs)[i]->mod_op = LDAP_MOD_REPLACE;
				(*attrs)[i]->mod_type = strdup(type);
				(*attrs)[i]->mod_values = (char **)
						malloc(2 * sizeof (char *));
				if ((*attrs)[i]->mod_values  != NULL)
				{
					(*attrs)[i]->mod_values[0] =
								strdup(value);
					(*attrs)[i]->mod_values[1] = NULL;
					(*attrs)[i+1] = NULL;
				}
				else
				{
					result = NSL_ERR_MEMORY;
				}
			}
			else
			if ((*attrs)[i] != NULL)
			{
				/* value is null so do an attribute delete */

				(*attrs)[i]->mod_op = LDAP_MOD_DELETE;
				(*attrs)[i]->mod_type = strdup(type);
				(*attrs)[i]->mod_values = NULL;
				(*attrs)[i+1] = NULL;
			}
			else
			{
				result = NSL_ERR_MEMORY; /* malloc failed */
			}
		}

		else
		{
			/* Found an existing entry so add value to it */

			if (value != NULL)
			{
			    /* add value to attribute's replace list */

			    if ((*attrs)[i]->mod_op == LDAP_MOD_REPLACE)
			    {
				for (j = 0;
				    (*attrs)[i]->mod_values[j] != NULL; j++);

				(*attrs)[i]->mod_values =
				(char **)realloc((*attrs)[i]->mod_values,
						(j + 2) * sizeof (char *));
				if ((*attrs)[i]->mod_values != NULL)
				{
					(*attrs)[i]->mod_values[j] =
								strdup(value);
					(*attrs)[i]->mod_values[j+1] = NULL;
				}
				else
				{
					result = NSL_ERR_MEMORY;
				}
			    }
			    else
			    {
				/* Delete and replace not allowed */
				result = NSL_ERR_MULTIOP;
			    }
			}

			else
			{
				/*
				 * attribute delete - so free any existing
				 * entries in the value array
				 */

				(*attrs)[i]->mod_op = LDAP_MOD_DELETE;

				if ((*attrs)[i]->mod_values != NULL)
				{
					for (j = 0;
					    (*attrs)[i]->mod_values[j] != NULL;
					    j++)
					{
					    free((*attrs)[i]->mod_values[j]);
					}

					free((*attrs)[i]->mod_values);
					(*attrs)[i]->mod_values = NULL;
				}
			}
		}
	}

	return (result);
} /* _modLDAPmodValue */





/*
 * *****************************************************************************
 *
 * Function:    _constructAddLDAPMod()
 *
 * Description: For the given attribute list construct an
 *              LDAPMod array for the printer object to be added. Default
 *              attribute values are included.
 *
 * Parameters:
 * Input:
 *              uchar_t *printerName - Name of printer to be added
 *              char    **attrList - user specified attribute values list
 * Output:      LDAPMod ***attrs  - pointer to the constructed array
 *
 * Returns:     NSL_RESULT - NSL_OK = constructed okay
 *
 * *****************************************************************************
 */

static NSL_RESULT
_constructAddLDAPMod(uchar_t *printerName, char **attrList,  LDAPMod ***attrs)

{
	NSL_RESULT result = NSL_ERROR;
	int len = 0;
	char **p = NULL;
	char *value = NULL;
	char *attr = NULL;

	/* ---------- */

	if ((printerName != NULL) &&
	    ((attrList != NULL) && (attrList[0] != NULL)) && (attrs != NULL))
	{
		*attrs = NULL;

		/*
		 * setup printer object attribute values in an LDAPMod structure
		 */
		result = _addLDAPmodValue(attrs, ATTR_OCLASS, OCV_TOP);
		if (result == NSL_OK)
		{
			/* Structural Objectclass */
			result =
			    _addLDAPmodValue(attrs, ATTR_OCLASS, OCV_PSERVICE);
		}
		if (result == NSL_OK)
		{
			result = _addLDAPmodValue(attrs,
						ATTR_OCLASS, OCV_PABSTRACT);
		}
		if (result == NSL_OK)
		{
			result = _addLDAPmodValue(attrs,
						ATTR_OCLASS, OCV_SUNPRT);
		}
		if (result == NSL_OK)
		{
			result = _addLDAPmodValue(attrs,
					ATTR_PNAME, (char *)printerName);
		}

		/*
		 * Now work through the user supplied attribute
		 * values list and add them into the LDAPMod array
		 */

		for (p = attrList;
			(p != NULL) && (*p != NULL) && (result == NSL_OK); p++)
		{
			/* get length of this key word */

			for (len = 0;
			    ((*p)[len] != '=') && ((*p)[len] != '\0'); len++);

			if ((strlen(*p) > len+1))
			{
				attr = strdup(*p);
				attr[len] = '\0';
				value = strdup(&attr[len+1]);

				/* handle specific Key Value Pairs (KVP) */

				if (strcasecmp(attr, NS_KEY_BSDADDR) == 0)
				{
					/* use LDAP attribute name */
					free(attr);
					attr = strdup(ATTR_BSDADDR);
				}
				else
				if (_attrInLDAPList(attr) == 0)
				{
					/*
					 * Non-LDAP attribute so use LDAP
					 * KVP attribute and the given KVP
					 * as the value, ie.
					 * sun-printer-kvp=description=printer
					 */
					free(attr);
					attr = strdup(ATTR_KVP);
					value = strdup(*p);
				}

				/* add it into the LDAPMod array */

				result = _addLDAPmodValue(attrs, attr, value);

				free(attr);
				free(value);
			}
		} /* for */

		if ((result != NSL_OK) && (*attrs != NULL))
		{
			(void) ldap_mods_free(*attrs, 1);
			attrs = NULL;
		}
	}
	else
	{
		result = NSL_ERR_INTERNAL;
	}

	return (result);
} /* _constructAddLDAPMod */







/*
 * *****************************************************************************
 *
 * Function:    _constructModLDAPMod()
 *
 * Description: For the given modify attribute list, construct an
 *              LDAPMod array for the printer object to be modified
 *
 * Parameters:
 * Input:       uchar_t *printerName - name of printer to be modified
 *              int     sunPrinter - Boolean; object is a sunPrinter
 *              char    **attrList - user specified attribute values list
 *              char    ***oldKVPList - current list of KVP values on object
 * Output:      LDAPMod ***attrs  - pointer to the constructed array
 *
 * Returns:     NSL_RESULT - NSL_OK = constructed okay
 *
 * *****************************************************************************
 */

static NSL_RESULT
_constructModLDAPMod(uchar_t *printerName, int sunPrinter, char **attrList,
			char ***oldKVPList, LDAPMod ***attrs)

{
	NSL_RESULT result = NSL_OK;
	int len = 0;
	int kvpUpdated = 0;
	int kvpExists = 0;
	char **p = NULL;
	char *value = NULL;
	char *attr = NULL;

	/* ---------- */

	if ((printerName != NULL) &&
	    ((attrList != NULL) && (attrList[0] != NULL)) && (attrs != NULL))
	{
		*attrs = NULL;

		if ((oldKVPList != NULL) && (*oldKVPList != NULL))
		{
			kvpExists = 1;
		}

		if (!sunPrinter)
		{
			/*
			 * The object was previously not a sunPrinter, so
			 * add the required objectclass attribute value, and
			 * ensure it has the printername attribute.
			 */
			result = _addLDAPmodValue(attrs,
						ATTR_OCLASS, OCV_SUNPRT);
			if (result == NSL_OK)
			{
				result = _modLDAPmodValue(attrs,
					    ATTR_PNAME, (char *)printerName);
			}
		}

		/*
		 * work through the user supplied attribute
		 * values list and add them into the LDAPMod array depending
		 * on if they are a replace or delete attribute operation,
		 * a "null value" means delete.
		 */

		for (p = attrList;
			(p != NULL) && (*p != NULL) && (result == NSL_OK); p++)
		{
			/* get length of this key word */

			for (len = 0;
			    ((*p)[len] != '=') && ((*p)[len] != '\0'); len++);

			if ((strlen(*p) > len+1))
			{
				attr = strdup(*p);
				attr[len] = '\0';
				value = strdup(&attr[len+1]);

				/* handle specific Key Value Pairs (KVP) */

				if ((_attrInLDAPList(attr) == 0) &&
					(strcasecmp(attr, NS_KEY_BSDADDR) != 0))
				{
					/*
					 * Non-LDAP attribute so use LDAP
					 * KVP attribute and the given KVP as
					 * the value, ie.
					 * sun-printer-kvp=description=printer
					 */
					result = _modAttrKVP(*p, oldKVPList);
					kvpUpdated = 1;
				}

				else
				{
					if (strcasecmp(attr, NS_KEY_BSDADDR) ==
									0)
					{
						/*
						 * use LDAP bsdaddr attribute
						 * name
						 */
						free(attr);
						attr = strdup(ATTR_BSDADDR);
					}

					/*
					 * else
					 *   use the supplied attribute name
					 */

					/* add it into the LDAPMod array */

					result = _modLDAPmodValue(attrs,
								attr, value);
				}

				free(attr);
				free(value);
			}

			else
			if (strlen(*p) >= 1)
			{
				/* handle attribute DELETE request */

				attr = strdup(*p);
				if (attr[len] == '=')
				{
					/* terminate "attribute=" */
					attr[len] = '\0';
				}

				/* handle specific Key Value Pairs (KVP) */

				if (strcasecmp(attr, NS_KEY_BSDADDR) == 0)
				{
					/* use LDAP bsdaddr attribute name */
					result = _modLDAPmodValue(attrs,
							ATTR_BSDADDR, NULL);
				}
				else
				if (_attrInLDAPList(attr) == 0)
				{
					/*
					 * Non-LDAP kvp, so sort items
					 * in the kvp list
					 */
					result = _modAttrKVP(*p, oldKVPList);
					kvpUpdated = 1;
				}
				else
				{
					result = _modLDAPmodValue(attrs,
							attr, NULL);
				}

				free(attr);
			}
		} /* for */

		if ((result == NSL_OK) && (kvpUpdated))
		{
			result = _attrAddKVP(attrs, *oldKVPList, kvpExists);
		}

		if ((result != NSL_OK) && (*attrs != NULL))
		{
			(void) ldap_mods_free(*attrs, 1);
			*attrs = NULL;
		}
	}
	else
	{
		result = NSL_ERR_INTERNAL;
	}

	return (result);
} /* _constructModLDAPMod */






/*
 * *****************************************************************************
 *
 * Function:    _compareURIinDNs()
 *
 * Description: For the 2 given printer object DNs compare the naming part
 *              part of the DN (printer-uri) to see if they are the same.
 *
 * Note:        This function only returns "compare failed" if their URI don't
 *              compare. Problems with the dn etc., return a good compare
 *              because I don't want us to create a new object for these
 *
 * Parameters:
 * Input:       uchar_t *dn1
 *              uchar_t *dn2
 * Output:      None
 *
 * Returns:     NSL_RESULT - NSL_OK = URIs are the same
 *
 * *****************************************************************************
 */

static NSL_RESULT
_compareURIinDNs(uchar_t *dn1, uchar_t *dn2)

{
	NSL_RESULT result = NSL_OK;
	uchar_t *DN1 = NULL;
	uchar_t *DN2 = NULL;
	char *p1 = NULL;
	char *p2 = NULL;

	/* --------- */

	if ((dn1 != NULL) && (dn2 != NULL))
	{
		DN1 = (uchar_t *)strdup((char *)dn1);
		DN2 = (uchar_t *)strdup((char *)dn2);

		/* terminate each string after the printer-uri */

		p1 = strstr((char *)DN1, PCONTAINER);
		/* move back to the comma */
		while ((p1 != NULL) && (*p1 != ',') && (p1 >= (char *)DN1))
		{
			p1--;
		}

		p2 = strstr((char *)DN2, PCONTAINER);
		/* move back to the comma */
		while ((p2 != NULL) && (*p2 != ',') && (p2 >= (char *)DN2))
		{
			p2--;
		}

		if ((*p1 == ',') && (*p2 == ','))
		{
			*p1 = '\0';	/* re-terminate it */
			*p2 = '\0';	/* re-terminate it */

			/* do the compare */

			/*
			 * Note: SHOULD really normalise the 2 DNs before
			 * doing the compare
			 */
#ifdef DEBUG
printf("_compareURIinDNs() @1 (%s) (%s)\n", DN1, DN2);
#endif
			if (strcasecmp((char *)DN1, (char *)DN2) != 0)
			{
				result = NSL_ERROR;
			}

		}

		free(DN1);
		free(DN2);
	}

	return (result);
} /* _compareURIinDNs */







/*
 * *****************************************************************************
 *
 * Function:    _getThisNSDomainDN()
 *
 * Description: Get the current Name Service Domain DN
 *              This is extracted from the result of executing ldaplist.
 *
 * Note:        Do it this way until the NS LDAP library interface is
 *              made public.
 *
 * Parameters:
 * Input:       None
 * Output:      None
 *
 * Returns:     uchar_t*  - pointer to NS Domain DN (The caller should free this
 *                          returned memory).
 *
 * *****************************************************************************
 */

#define	LDAPLIST_D	"/usr/bin/ldaplist -d 2>&1"
#define	DNID		"dn: "

static uchar_t *
_getThisNSDomainDN(void)

{
	uchar_t *domainDN = NULL;
	char *cp = NULL;
	char buf[BUFSIZ] = "";

	/* --------- */

	if (_popen(LDAPLIST_D, buf, sizeof (buf)) == 0)
	{
		if ((cp = strstr(buf, DNID)) != NULL)
		{
			cp += strlen(DNID);  /* increment past "dn: " label */
			domainDN = (uchar_t *)strdup(cp);

			if ((cp = strchr((char *)domainDN, '\n')) != NULL)
			{
				*cp = '\0'; /* terminate it */
			}
		}
	}

	return (domainDN);
} /* _getThisNSDomainDN */





/*
 * *****************************************************************************
 *
 * Function:    _popen()
 *
 * Description: General popen function. The caller should always use a full
 *              path cmd.
 *
 * Parameters:
 * Input:       char *cmd - command line to execute
 *              char *buffer - ptr to buffer to put result in
 *              int  size - size of result buffer
 * Output:      None
 *
 * Returns:     int - 0 = opened okay
 *
 * *****************************************************************************
 */

static int
_popen(char *cmd, char *buffer, int size)

{
	int result = -1;
	int rsize = 0;
	FILE *fptr;
	char safe_cmd[BUFSIZ];
	char linebuf[BUFSIZ];

	/* -------- */

	if ((cmd != NULL) && (buffer != NULL) && (size != 0))
	{
		(void) strcpy(buffer, "");
		(void) strcpy(linebuf, "");
		(void) snprintf(safe_cmd, BUFSIZ, "IFS=' \t'; %s", cmd);

		if ((fptr = popen(safe_cmd, "r")) != NULL)
		{
			while ((fgets(linebuf, BUFSIZ, fptr) != NULL) &&
							(rsize  < size))
			{
				rsize = strlcat(buffer, linebuf, size);
				if (rsize >= size)
				{
					/* result is too long */
					(void) memset(buffer, '\0', size);
				}
			}

			if (strlen(buffer) > 0)
			{
				result = 0;
			}

			(void) pclose(fptr);
		}
	}

	return (result);
} /* popen */


/*
 * *****************************************************************************
 *
 * Function:    _attrInList()
 *
 * Description: For the given list check if the attribute is it
 *
 * Parameters:
 * Input:       char *attr   - attribute to check
 *              char **list  - list of attributes to check against
 * Output:      None
 *
 * Returns:     int - TRUE = attr found in list
 *
 * *****************************************************************************
 */

static int
_attrInList(char *attr, const char **list)

{
	int result = 0;
	int j;

	/* ------- */

	if ((attr != NULL) && (list != NULL))
	{
		for (j = 0; (list[j] != NULL) && (result != 1); j++)
		{
			if (strcasecmp(list[j], attr) == 0)
			{
				result = 1; /* found */
			}
		}
	}

	return (result);
} /* _attrInList */




/*
 * *****************************************************************************
 *
 * Function:    _attrInLDAPList()
 *
 * Description: Checks to see if the given attribute is an LDAP printing
 *              attribute, ie. is either in an IPP objectclass or the
 *              sun printer objectclass. Note: some attributes are handled
 *              specifically outside this function, so are excluded from
 *              the lists that are checked.
 *
 * Parameters:
 * Input:       char *attr    - attribute to check
 * Output:      None
 *
 * Returns:     int - TRUE = attr found in list
 *
 * *****************************************************************************
 */

static int
_attrInLDAPList(char *attr)

{
	int result = 0;

	/* ------- */

	if (_attrInList(attr, nsl_attr_printerService))
	{
		result = 1;	/* in list */
	}
	else
	if (_attrInList(attr, nsl_attr_printerIPP))
	{
		result = 1;	/* in list */
	}
	else
	if (_attrInList(attr, nsl_attr_sunPrinter))
	{
		result = 1;	/* in list */
	}

	return (result);
} /* _attrInLDAPList */




/*
 * *****************************************************************************
 *
 * Function:    _getCurrentKVPValues()
 *
 * Description: For the given printer object read the current set of values
 *              the object has for the sun-printer-kvp (Key Value pair)
 *
 * Parameters:
 * Input:       LDAP *ld       - existing ldap connection descriptor
 *              char *objectDN - DN to search for
 * Output:      char ***list   - returned set of kvp values
 *
 * Result:      NSL_RESULT - NSL_OK = object exists
 *
 * *****************************************************************************
 */

static NSL_RESULT
_getCurrentKVPValues(LDAP *ld, uchar_t *objectDN, char ***list)

{
	NSL_RESULT result = NSL_ERR_UNKNOWN_PRINTER;
	int sresult = LDAP_NO_SUCH_OBJECT;
	int i = 0;
	LDAPMessage *ldapMsg;
	char *requiredAttrs[2] = { ATTR_KVP, NULL };
	LDAPMessage *ldapEntry = NULL;
	char *entryAttrib = NULL;
	char **attribValues = NULL;
	BerElement *berElement = NULL;

	/* ---------- */

	if ((list != NULL) && (ld != NULL) && (objectDN != NULL))
	{
		/* search for this Printer in the directory */

		sresult = ldap_search_s(ld, (char *)objectDN, LDAP_SCOPE_BASE,
				"(objectclass=*)", requiredAttrs, 0, &ldapMsg);
		if (sresult == LDAP_SUCCESS)
		{
			/*
			 * check that the object exists and extract its
			 * KVP attribute values
			 */
			ldapEntry = ldap_first_entry(ld, ldapMsg);
			if (ldapEntry != NULL)
			{
				entryAttrib = ldap_first_attribute(ld,
							ldapEntry, &berElement);
				if ((entryAttrib != NULL) &&
				    (strcasecmp(entryAttrib, ATTR_KVP) == 0))

				{
#ifdef DEBUG
printf("Attribute: %s, its values are:\n", entryAttrib);
#endif
					/*
					 * add each KVP value to the list
					 * that we will return
					 */
					attribValues = ldap_get_values(
						ld, ldapEntry, entryAttrib);
					for (i = 0;
						attribValues[i] != NULL; i++)
					{
					    *list = (char **)
						list_append((void **)*list,
						    strdup(attribValues[i]));
#ifdef DEBUG
printf("\t%s\n", attribValues[i]);
#endif
					}
					(void) ldap_value_free(attribValues);
				}

				if ((entryAttrib != NULL) &&
				    (berElement != NULL))
				{
					ber_free(berElement, 0);
				}


				/* object found */
				result = NSL_OK;
			}

			(void) ldap_msgfree(ldapMsg);
		}
	}

	else
	{
		result = NSL_ERR_INTERNAL;
	}

	return (result);
} /* _getCurrentKVPValues */



/*
 * *****************************************************************************
 *
 * Function:    _freeList()
 *
 * Description: Free the list created by list_append() where the items in
 *              the list have been strdup'ed.
 *
 * Parameters:
 * Input:       char ***list   - returned set of kvp values
 *
 * Result:      void
 *
 * *****************************************************************************
 */

static void
_freeList(char ***list)

{
	int i = 0;

	/* ------ */

	if (list != NULL)
	{
		if (*list != NULL)
		{
			for (i = 0; (*list)[i] != NULL; i++)
			{
				free((*list)[i]);
			}
			free(*list);
		}

		*list = NULL;
	}
} /* _freeList */



/*
 * *****************************************************************************
 *
 * Function:    _modAttrKVP()
 *
 * Description: Sort out the KVP attribute value list, such that this new
 *              value takes precidence over any existing value in the list.
 *              The current list is updated to remove this key, and the new
 *              key "value" is added to the list, eg. for
 *                  value: bbb=ddddd
 *                  and kvpList:
 *                         aaa=yyyy
 *                         bbb=zzzz
 *                         ccc=xxxx
 *                  the resulting kvpList is:
 *                         aaa=yyyy
 *                         ccc=xxxx
 *                         bbb=ddddd
 *
 * Note:        When all new values have been handled the function _attrAddKVP()
 *              must be called to add the "new list" values into the
 *              LDAPMod array.
 *
 * Parameters:
 * Input:       char *value       - Key Value Pair to process,
 *                                  eg. aaaaa=hhhhh, where aaaaa is the key
 *              char ***kvpList   - list of current KVP values
 * Output:      char ***kvpList   - updated list of KVP values
 *
 * Returns:     NSL_RESULT - NSL_OK = done okay
 *
 * *****************************************************************************
 */

static NSL_RESULT
_modAttrKVP(char *value, char ***kvpList)

{
	NSL_RESULT result = NSL_ERR_INTERNAL;
	int i = 0;
	int inList = 0;
	int keyDelete = 0;
	char *key = NULL;
	char **p = NULL;
	char **newList = NULL;

	/* ------- */

	if ((value != NULL) && (kvpList != NULL))
	{
		result = NSL_OK;

		/* extract "key" from value */

		key = strdup(value);

		for (i = 0; ((key)[i] != '=') && ((key)[i] != '\0'); i++);
		key[i] = '\0'; /* terminate the key */

		/* Is this a request to delete a "key" value */

		if ((value[i] == '\0') || (value[i+1] == '\0'))
		{
			/* this is a request to delete the key */
			keyDelete = 1;
		}

		if ((*kvpList != NULL) && (**kvpList != NULL))
		{
			/*
			 * for each item in the list remove it if the keys match
			 */
			for (p = *kvpList; *p != NULL; p++)
			{
				for (i = 0;
				    ((*p)[i] != '=') && ((*p)[i] != '\0'); i++);

				if ((strlen(key) == i) &&
					(strncasecmp(*p, key, i) == 0))
				{
					inList = 1;
				}
				else
				{
					/* no match so add value to new list */
					newList = (char **)list_append(
							(void **)newList,
							strdup(*p));
				}
			}
		}

		/*
		 * if it was not a DELETE request add the new key value into
		 * the newList, otherwise we have already removed the key
		 */

		if (!keyDelete)
		{
			newList = (char **)list_append((void **)newList,
							strdup(value));
		}

		if ((newList != NULL) || (inList))
		{
			/* replace old list with the newList */
			_freeList(kvpList);
			*kvpList = newList;
		}

		free(key);
	}

	return (result);
} /* modAttrKVP */




/*
 * *****************************************************************************
 *
 * Function:    _attrAddKVP()
 *
 * Description: Process KVP items in the kvpList adding them to the
 *              LDAPMod modify array. If the list is empty but there were
 *              previously LDAP KVP values delete them.
 *
 * Note:        This function should only be called when all the new KVP
 *              items have been processed by _modAttrKVP()
 *
 * Parameters:
 * Input:       LDAPMod ***attrs - array to update
 *              char **kvpList   - list KVP values
 *              int  kvpExists   - object currently has LDAP KVP values
 * Output:      None
 *
 * Returns:     NSL_RESULT - NSL_OK = done okay
 *
 * *****************************************************************************
 */

static NSL_RESULT
_attrAddKVP(LDAPMod ***attrs, char **kvpList, int kvpExists)

{
	NSL_RESULT result = NSL_OK;

	/* ------- */

	if (attrs != NULL)
	{
		if (kvpList != NULL)
		{
			while ((kvpList != NULL) && (*kvpList != NULL))
			{
				/* add item to LDAPMod array */

				result =
				    _modLDAPmodValue(attrs, ATTR_KVP, *kvpList);

				kvpList++;
			}
		}
		else
		if (kvpExists)
		{
			/*
			 * We now have no LDAP KVP values but there were
			 * some previously, so delete them
			 */
			result = _modLDAPmodValue(attrs, ATTR_KVP, NULL);
		}
	}

	else
	{
		result = NSL_ERR_INTERNAL;
	}

	return (result);
} /* _attrAddKVP */




/*
 * *****************************************************************************
 *
 * Function:    _manageReferralCredentials()
 *
 * Description: This function is called if a referral request is returned by
 *              the origonal LDAP server during the ldap update request call,
 *              eg. ldap_add_s(), ldap_modify_s() or ldap_delete_s().
 * Parameters:
 * Input:       LDAP *ld      - LDAP descriptor
 *              int freeit    - 0 = first call to get details
 *                            - 1 = second call to free details
 *                            - -1 = initial store of authentication details
 * Input/Output: char **dn    - returns DN to bind to on master
 *               char **credp - returns password for DN
 *               int *methodp - returns authentication type, eg. simple
 *
 * Returns:     int - 0 = okay
 *
 * *****************************************************************************
 */
static int _manageReferralCredentials(LDAP *ld, char **dn, char **credp,
    int *methodp, int freeit, void *arg __unused)
{
	int result = 0;
	static char *sDN = NULL;
	static char *sPasswd = NULL;
	static int  sMethod = LDAP_AUTH_SIMPLE;

	/* -------- */

	if (freeit == 1)
	{
		/* second call - free memory */

		if ((dn != NULL) && (*dn != NULL))
		{
			free(*dn);
		}

		if ((credp != NULL) && (*credp != NULL))
		{
			free(*credp);
		}
	}

	else
	if ((ld != NULL) &&
	    (dn != NULL) && (credp != NULL) && (methodp != NULL))
	{
		if ((freeit == 0) && (sDN != NULL) && (sPasswd != NULL))
		{
			/* first call - get the saved bind credentials */

			*dn = strdup(sDN);
			*credp = strdup(sPasswd);
			*methodp = sMethod;
		}
		else
		if (freeit == -1)
		{
			/* initial call - save the saved bind credentials */

			sDN = *dn;
			sPasswd = *credp;
			sMethod = *methodp;
		}
		else
		{
			result = 1;	/* error */
		}
	}
	else
	{
		result = 1;	/* error */
	}

	return (result);
} /* _manageReferralCredentials */