17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
586b1a8baSrotondo * Common Development and Distribution License (the "License").
686b1a8baSrotondo * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22c1ecd8b9Sjacobs * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <unistd.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/stat.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <stdarg.h>
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate #include <syslog.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <pwd.h>
377c478bd9Sstevel@tonic-gate #include <libintl.h>
387c478bd9Sstevel@tonic-gate #include <netdb.h> /* for rcmd() */
397c478bd9Sstevel@tonic-gate
40355b4669Sjacobs #include <ns.h>
41355b4669Sjacobs #include <list.h>
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate #define LDAP_REFERRALS
447c478bd9Sstevel@tonic-gate #include <lber.h>
457c478bd9Sstevel@tonic-gate #include <ldap.h>
467c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate * This modules contains the code required to manipulate printer objects in
517c478bd9Sstevel@tonic-gate * a LDAP directory for the Naming Service (NS) switch.
527c478bd9Sstevel@tonic-gate * It can "add", "modify" and "delete" the objects on the given ldap server
537c478bd9Sstevel@tonic-gate * and in the given NS domain DN, eg. "dc=mkg,dc=sun,dc=com".
547c478bd9Sstevel@tonic-gate * Note: printers known to the naming service are contained in the RDN
557c478bd9Sstevel@tonic-gate * "ou=printers" under the NS domain DN
567c478bd9Sstevel@tonic-gate */
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate #define PCONTAINER "ou=printers"
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate /* attribute keywords */
617c478bd9Sstevel@tonic-gate #define ATTR_DN "dn"
627c478bd9Sstevel@tonic-gate #define ATTR_OCLASS "objectClass"
637c478bd9Sstevel@tonic-gate #define ATTR_URI "printer-uri"
647c478bd9Sstevel@tonic-gate #define ATTR_PNAME "printer-name"
657c478bd9Sstevel@tonic-gate #define ATTR_XRISUP "printer-xri-supported"
667c478bd9Sstevel@tonic-gate #define ATTR_BSDADDR "sun-printer-bsdaddr"
677c478bd9Sstevel@tonic-gate #define ATTR_KVP "sun-printer-kvp"
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate /* objectClass values */
707c478bd9Sstevel@tonic-gate #define OCV_TOP "top"
717c478bd9Sstevel@tonic-gate #define OCV_PSERVICE "printerService"
727c478bd9Sstevel@tonic-gate #define OCV_SUNPRT "sunPrinter"
737c478bd9Sstevel@tonic-gate #define OCV_PABSTRACT "printerAbstract"
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate /* xri-supported attribute value */
767c478bd9Sstevel@tonic-gate #define AV_UNKNOWN "unknown"
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate * LDAP objectclass atributes that the user can explicity change
817c478bd9Sstevel@tonic-gate */
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate static const char *nsl_attr_printerService[] = {
847c478bd9Sstevel@tonic-gate "printer-uri",
857c478bd9Sstevel@tonic-gate "printer-xri-supported",
867c478bd9Sstevel@tonic-gate /* Not allowed "printer-name", */
877c478bd9Sstevel@tonic-gate "printer-natural-language-configured",
887c478bd9Sstevel@tonic-gate "printer-location",
897c478bd9Sstevel@tonic-gate "printer-info",
907c478bd9Sstevel@tonic-gate "printer-more-info",
917c478bd9Sstevel@tonic-gate "printer-make-and-model",
927c478bd9Sstevel@tonic-gate "printer-charset-configured",
937c478bd9Sstevel@tonic-gate "printer-charset-supported",
947c478bd9Sstevel@tonic-gate "printer-generated-natural-language-supported",
957c478bd9Sstevel@tonic-gate "printer-document-format-supported",
967c478bd9Sstevel@tonic-gate "printer-color-supported",
977c478bd9Sstevel@tonic-gate "printer-compression-supported",
987c478bd9Sstevel@tonic-gate "printer-pages-per-minute",
997c478bd9Sstevel@tonic-gate "printer-pages-per-minute-color",
1007c478bd9Sstevel@tonic-gate "printer-finishings-supported",
1017c478bd9Sstevel@tonic-gate "printer-number-up-supported",
1027c478bd9Sstevel@tonic-gate "printer-sides-supported",
1037c478bd9Sstevel@tonic-gate "printer-media-supported",
1047c478bd9Sstevel@tonic-gate "printer-media-local-supported",
1057c478bd9Sstevel@tonic-gate "printer-resolution-supported",
1067c478bd9Sstevel@tonic-gate "printer-print-quality-supported",
1077c478bd9Sstevel@tonic-gate "printer-job-priority-supported",
1087c478bd9Sstevel@tonic-gate "printer-copies-supported",
1097c478bd9Sstevel@tonic-gate "printer-job-k-octets-supported",
1107c478bd9Sstevel@tonic-gate "printer-current-operator",
1117c478bd9Sstevel@tonic-gate "printer-service-person",
1127c478bd9Sstevel@tonic-gate "printer-delivery-orientation-supported",
1137c478bd9Sstevel@tonic-gate "printer-stacking-order-supported",
1147c478bd9Sstevel@tonic-gate "printer-output-features-supported",
1157c478bd9Sstevel@tonic-gate (char *)NULL
1167c478bd9Sstevel@tonic-gate };
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate static const char *nsl_attr_printerIPP[] = {
1207c478bd9Sstevel@tonic-gate "printer-ipp-versions-supported",
1217c478bd9Sstevel@tonic-gate "printer-multiple-document-jobs-supported",
1227c478bd9Sstevel@tonic-gate (char *)NULL
1237c478bd9Sstevel@tonic-gate };
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate static const char *nsl_attr_sunPrinter[] = {
1267c478bd9Sstevel@tonic-gate /* Not allowed "sun-printer-bsdaddr", */
1277c478bd9Sstevel@tonic-gate /* Not allowed "sun-printer-kvp", */
1287c478bd9Sstevel@tonic-gate (char *)NULL
1297c478bd9Sstevel@tonic-gate };
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate * List of LDAP attributes that user is not allowed to explicitly change
1347c478bd9Sstevel@tonic-gate */
1357c478bd9Sstevel@tonic-gate static const char *nsl_attr_notAllowed[] = {
1367c478bd9Sstevel@tonic-gate ATTR_DN,
1377c478bd9Sstevel@tonic-gate ATTR_OCLASS, /* objectclass */
1387c478bd9Sstevel@tonic-gate ATTR_PNAME, /* printer-name */
1397c478bd9Sstevel@tonic-gate ATTR_BSDADDR,
1407c478bd9Sstevel@tonic-gate ATTR_KVP,
1417c478bd9Sstevel@tonic-gate (char *)NULL
1427c478bd9Sstevel@tonic-gate };
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gate static NSL_RESULT _connectToLDAP(ns_cred_t *cred, LDAP **ld);
1467c478bd9Sstevel@tonic-gate static uchar_t *_constructPrinterDN(uchar_t *printerName,
1477c478bd9Sstevel@tonic-gate uchar_t *domainDN, char **attrList);
1487c478bd9Sstevel@tonic-gate static NSL_RESULT _checkPrinterExists(LDAP *ld, uchar_t *printerName,
1497c478bd9Sstevel@tonic-gate uchar_t *domainDN, uchar_t **printerDN);
1507c478bd9Sstevel@tonic-gate static NSL_RESULT _checkPrinterDNExists(LDAP *ld, uchar_t *objectDN);
1517c478bd9Sstevel@tonic-gate static NSL_RESULT _checkSunPrinter(LDAP *ld, uchar_t *printerDN);
1527c478bd9Sstevel@tonic-gate static NSL_RESULT _addNewPrinterObject(LDAP *ld, uchar_t *printerName,
1537c478bd9Sstevel@tonic-gate uchar_t *domainDN, char **attrList);
1547c478bd9Sstevel@tonic-gate static NSL_RESULT _modifyPrinterObject(LDAP *ld, uchar_t *printerDN,
1557c478bd9Sstevel@tonic-gate uchar_t *printerName, uchar_t *domainDN, char **attrList);
1567c478bd9Sstevel@tonic-gate static NSL_RESULT _checkAttributes(char **list);
1577c478bd9Sstevel@tonic-gate static NSL_RESULT _addLDAPmodValue(LDAPMod ***attrs, char *type, char *value);
1587c478bd9Sstevel@tonic-gate static NSL_RESULT _modLDAPmodValue(LDAPMod ***attrs, char *type, char *value);
1597c478bd9Sstevel@tonic-gate static NSL_RESULT _constructAddLDAPMod(uchar_t *printerName,
1607c478bd9Sstevel@tonic-gate char **attrList, LDAPMod ***attrs);
1617c478bd9Sstevel@tonic-gate static NSL_RESULT _constructModLDAPMod(uchar_t *printerName, int sunPrinter,
1627c478bd9Sstevel@tonic-gate char **attrList, char ***oldKVPList, LDAPMod ***attrs);
1637c478bd9Sstevel@tonic-gate static NSL_RESULT _compareURIinDNs(uchar_t *dn1, uchar_t *dn2);
1647c478bd9Sstevel@tonic-gate static uchar_t *_getThisNSDomainDN(void);
1657c478bd9Sstevel@tonic-gate static int _popen(char *cmd, char *results, int size);
1667c478bd9Sstevel@tonic-gate static int _attrInList(char *attr, const char **list);
1677c478bd9Sstevel@tonic-gate static int _attrInLDAPList(char *attr);
1687c478bd9Sstevel@tonic-gate static NSL_RESULT _getCurrentKVPValues(LDAP *ld,
1697c478bd9Sstevel@tonic-gate uchar_t *objectDN, char ***list);
1707c478bd9Sstevel@tonic-gate static void _freeList(char ***list);
1717c478bd9Sstevel@tonic-gate static NSL_RESULT _modAttrKVP(char *value, char ***kvpList);
1727c478bd9Sstevel@tonic-gate static NSL_RESULT _attrAddKVP(LDAPMod ***attrs, char **kvpList, int kvpExists);
1737c478bd9Sstevel@tonic-gate static int _manageReferralCredentials(LDAP *ld, char **dn, char **credp,
174*ef2333d1SToomas Soome int *methodp, int freeit, void *);
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate * *****************************************************************************
1787c478bd9Sstevel@tonic-gate *
1797c478bd9Sstevel@tonic-gate * Function: ldap_put_printer()
1807c478bd9Sstevel@tonic-gate *
1817c478bd9Sstevel@tonic-gate * Description: Action the request to change a printer object in the LDAP
1827c478bd9Sstevel@tonic-gate * directory DIT. The object is either added, modified or deleted
1837c478bd9Sstevel@tonic-gate * depending on the request's attribute list. A null list indicates
1847c478bd9Sstevel@tonic-gate * the request is a delete.
1857c478bd9Sstevel@tonic-gate * The object's DN is constructed from the supplied domain DN and
1867c478bd9Sstevel@tonic-gate * a check is done to see if the object exists already, if it
1877c478bd9Sstevel@tonic-gate * doesn't exist then this is a request to add a new object
1887c478bd9Sstevel@tonic-gate * If a URI is given in the attribute list and it is different to
1897c478bd9Sstevel@tonic-gate * the existing printing object's DN then the request will be
1907c478bd9Sstevel@tonic-gate * rejected.
1917c478bd9Sstevel@tonic-gate *
1927c478bd9Sstevel@tonic-gate *
1937c478bd9Sstevel@tonic-gate * Parameters:
1947c478bd9Sstevel@tonic-gate * Input: const ns_printer_t *printer
1957c478bd9Sstevel@tonic-gate * - this structure contains the following :
1967c478bd9Sstevel@tonic-gate * char *printerName - name of the printer
1977c478bd9Sstevel@tonic-gate * ns_cred_t *cred - structure containing the ldap host and
1987c478bd9Sstevel@tonic-gate * port, user, password and NS domain DN for the
1997c478bd9Sstevel@tonic-gate * directory server to be updated.
2007c478bd9Sstevel@tonic-gate * char **attrList - pointer to a list of attribute key values
2017c478bd9Sstevel@tonic-gate * for the printer object. If the object does
2027c478bd9Sstevel@tonic-gate * not already exist then this list contains the
2037c478bd9Sstevel@tonic-gate * values for the new object, otherwise this list
2047c478bd9Sstevel@tonic-gate * is a list of attributes to modify. For modify
2057c478bd9Sstevel@tonic-gate * a null attribute value is a attribute delete
2067c478bd9Sstevel@tonic-gate * request. A NULL ptr = delete the object.
2077c478bd9Sstevel@tonic-gate * Output: None
2087c478bd9Sstevel@tonic-gate *
2097c478bd9Sstevel@tonic-gate * Returns: int - 0 = request actioned okay
2107c478bd9Sstevel@tonic-gate * !0 = error - see NSL_RESULT codes
2117c478bd9Sstevel@tonic-gate *
2127c478bd9Sstevel@tonic-gate * *****************************************************************************
2137c478bd9Sstevel@tonic-gate */
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate int
ldap_put_printer(const ns_printer_t * printer)2167c478bd9Sstevel@tonic-gate ldap_put_printer(const ns_printer_t *printer)
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate NSL_RESULT result = NSL_OK;
2207c478bd9Sstevel@tonic-gate NSL_RESULT printerExists = NSL_ERR_UNKNOWN_PRINTER;
2217c478bd9Sstevel@tonic-gate LDAP *ld = NULL;
2227c478bd9Sstevel@tonic-gate uchar_t *printerDN = NULL;
2237c478bd9Sstevel@tonic-gate uchar_t *domainDN = NULL;
2247c478bd9Sstevel@tonic-gate char *printerName = NULL;
2257c478bd9Sstevel@tonic-gate ns_cred_t *cred = NULL;
2267c478bd9Sstevel@tonic-gate char **attrList = NULL;
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate /* -------- */
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate /*
2317c478bd9Sstevel@tonic-gate * Note: the "attributes" list should be null for ldap as the attribute
2327c478bd9Sstevel@tonic-gate * values are passed in the nsdata field
2337c478bd9Sstevel@tonic-gate */
2347c478bd9Sstevel@tonic-gate
2357c478bd9Sstevel@tonic-gate if ((printer != NULL) &&
2367c478bd9Sstevel@tonic-gate (printer->attributes == NULL) && (printer->name != NULL))
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate /* extract required pointer values from structure */
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate printerName = printer->name;
2417c478bd9Sstevel@tonic-gate cred = printer->cred;
2427c478bd9Sstevel@tonic-gate if (printer->nsdata != NULL)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate attrList = ((NS_LDAPDATA *)(printer->nsdata))->attrList;
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate /* connect and bind to the ldap directory server */
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate result = _connectToLDAP(cred, &ld);
2507c478bd9Sstevel@tonic-gate if ((result == NSL_OK) && (ld != NULL))
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate * check if the NS domain DN was given, if not use the
2547c478bd9Sstevel@tonic-gate * current NS domain
2557c478bd9Sstevel@tonic-gate */
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate if (cred->domainDN != NULL)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate domainDN = (uchar_t *)
2607c478bd9Sstevel@tonic-gate strdup((char *)cred->domainDN);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate else
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate /* get DN of current domain */
2657c478bd9Sstevel@tonic-gate domainDN = _getThisNSDomainDN();
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate printerExists =
2697c478bd9Sstevel@tonic-gate _checkPrinterExists(ld, (uchar_t *)printerName,
2707c478bd9Sstevel@tonic-gate domainDN, &printerDN);
2717c478bd9Sstevel@tonic-gate if (printerExists != LDAP_SUCCESS)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate * could not find the printer by printer-name,
2757c478bd9Sstevel@tonic-gate * but there could be a non sunPrinter object
2767c478bd9Sstevel@tonic-gate * so if the printer-uri was given check if
2777c478bd9Sstevel@tonic-gate * an object for that exists
2787c478bd9Sstevel@tonic-gate */
2797c478bd9Sstevel@tonic-gate printerDN =
2807c478bd9Sstevel@tonic-gate _constructPrinterDN(NULL,
2817c478bd9Sstevel@tonic-gate domainDN, attrList);
2827c478bd9Sstevel@tonic-gate if (printerDN != NULL)
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate printerExists = _checkPrinterDNExists(
2857c478bd9Sstevel@tonic-gate ld, printerDN);
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate #ifdef DEBUG
2897c478bd9Sstevel@tonic-gate if (printerExists == NSL_OK)
2907c478bd9Sstevel@tonic-gate {
2917c478bd9Sstevel@tonic-gate printf("DN found = '%s' for '%s'\n", printerDN, printerName);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate #endif
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate if (attrList == NULL)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate /*
2987c478bd9Sstevel@tonic-gate * a null list indicates that this is a DELETE
2997c478bd9Sstevel@tonic-gate * object request, so if object exists delete
3007c478bd9Sstevel@tonic-gate * it, otherwise report an error.
3017c478bd9Sstevel@tonic-gate */
3027c478bd9Sstevel@tonic-gate if (printerExists == LDAP_SUCCESS)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate result = ldap_delete_s(ld,
3057c478bd9Sstevel@tonic-gate (char *)printerDN);
3067c478bd9Sstevel@tonic-gate if (result != LDAP_SUCCESS)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate result = NSL_ERR_DEL_FAILED;
3097c478bd9Sstevel@tonic-gate #ifdef DEBUG
3107c478bd9Sstevel@tonic-gate ldap_perror(ld, "ldap_delete_s failed");
3117c478bd9Sstevel@tonic-gate #endif
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate else
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate result = NSL_ERR_UNKNOWN_PRINTER;
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate else
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate /*
3227c478bd9Sstevel@tonic-gate * if object exists then this is a
3237c478bd9Sstevel@tonic-gate * modify request otherwise is is an add request
3247c478bd9Sstevel@tonic-gate */
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate if (printerExists == LDAP_SUCCESS)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate * Modify the printer object to
3307c478bd9Sstevel@tonic-gate * give it the new attribute values
3317c478bd9Sstevel@tonic-gate * specified by the user
3327c478bd9Sstevel@tonic-gate */
3337c478bd9Sstevel@tonic-gate result =
3347c478bd9Sstevel@tonic-gate _modifyPrinterObject(ld, printerDN,
3357c478bd9Sstevel@tonic-gate (uchar_t *)printerName,
3367c478bd9Sstevel@tonic-gate domainDN, attrList);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate else
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate /*
3417c478bd9Sstevel@tonic-gate * add new printer object into the
3427c478bd9Sstevel@tonic-gate * ldap directory with the user
3437c478bd9Sstevel@tonic-gate * specified attribute values
3447c478bd9Sstevel@tonic-gate */
3457c478bd9Sstevel@tonic-gate result =
3467c478bd9Sstevel@tonic-gate _addNewPrinterObject(ld,
3477c478bd9Sstevel@tonic-gate (uchar_t *)printerName,
3487c478bd9Sstevel@tonic-gate domainDN, attrList);
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate
3527c478bd9Sstevel@tonic-gate if (printerDN != NULL)
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate free(printerDN);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate if (domainDN != NULL)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate free(domainDN);
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate
3617c478bd9Sstevel@tonic-gate /* disconnect from LDAP server */
3627c478bd9Sstevel@tonic-gate
3637c478bd9Sstevel@tonic-gate (void) ldap_unbind(ld);
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate else
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate /* no printerName given */
3707c478bd9Sstevel@tonic-gate result = NSL_ERR_INTERNAL;
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate return ((int)result);
3747c478bd9Sstevel@tonic-gate } /* ldap_put_printer */
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate
3777c478bd9Sstevel@tonic-gate
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate /*
3807c478bd9Sstevel@tonic-gate * *****************************************************************************
3817c478bd9Sstevel@tonic-gate *
3827c478bd9Sstevel@tonic-gate * Function: _connectToLDAP()
3837c478bd9Sstevel@tonic-gate *
3847c478bd9Sstevel@tonic-gate * Description: Setup the connection and bind to the LDAP directory server.
3857c478bd9Sstevel@tonic-gate * The function returns the ldap connection descriptor
3867c478bd9Sstevel@tonic-gate *
3877c478bd9Sstevel@tonic-gate * Note: Currently the native ldap functions do not support secure
3887c478bd9Sstevel@tonic-gate * passwords, when this is supported this function will require
3897c478bd9Sstevel@tonic-gate * updating to allow the type passed in cred->passwdType to
3907c478bd9Sstevel@tonic-gate * be used with the ldap_simple_bind()
3917c478bd9Sstevel@tonic-gate *
3927c478bd9Sstevel@tonic-gate * Parameters:
3937c478bd9Sstevel@tonic-gate * Input: ns_cred_t *cred - structure containing the credentials (host,
3947c478bd9Sstevel@tonic-gate * port, user and password) required to bind
3957c478bd9Sstevel@tonic-gate * to the directory server to be updated.
3967c478bd9Sstevel@tonic-gate * char *printerName - printer name used only for error messages
3977c478bd9Sstevel@tonic-gate * Output: LDAP** - ldap connection descriptor pointer. NULL = failed
3987c478bd9Sstevel@tonic-gate *
3997c478bd9Sstevel@tonic-gate * Returns: NSL_RESULT - NSL_OK = connected okay
4007c478bd9Sstevel@tonic-gate *
4017c478bd9Sstevel@tonic-gate * *****************************************************************************
4027c478bd9Sstevel@tonic-gate */
4037c478bd9Sstevel@tonic-gate
4047c478bd9Sstevel@tonic-gate static NSL_RESULT
_connectToLDAP(ns_cred_t * cred,LDAP ** ld)4057c478bd9Sstevel@tonic-gate _connectToLDAP(ns_cred_t *cred, LDAP **ld)
4067c478bd9Sstevel@tonic-gate
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate NSL_RESULT result = NSL_OK;
4097c478bd9Sstevel@tonic-gate int lresult = 0;
4107c478bd9Sstevel@tonic-gate int ldapPort = LDAP_PORT; /* default LDAP port number */
4117c478bd9Sstevel@tonic-gate int protoVersion = LDAP_VERSION3;
4127c478bd9Sstevel@tonic-gate int derefOption = LDAP_DEREF_NEVER;
4137c478bd9Sstevel@tonic-gate int referrals = 1;
4147c478bd9Sstevel@tonic-gate char hostname[MAXHOSTNAMELEN];
4157c478bd9Sstevel@tonic-gate int tmpMethod = LDAP_AUTH_SIMPLE; /* temp - until its passed in */
4167c478bd9Sstevel@tonic-gate
4177c478bd9Sstevel@tonic-gate /* -------- */
4187c478bd9Sstevel@tonic-gate
4197c478bd9Sstevel@tonic-gate if ((ld == NULL) || (cred == NULL) ||
4207c478bd9Sstevel@tonic-gate ((cred->passwd == NULL) || (cred->binddn == NULL)))
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate result = NSL_ERR_CREDENTIALS;
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate
4257c478bd9Sstevel@tonic-gate else
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate *ld = NULL;
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate /* if host was not given then bind to local host */
4307c478bd9Sstevel@tonic-gate
4317c478bd9Sstevel@tonic-gate if (cred->host != NULL)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate (void) strlcpy(hostname, cred->host, sizeof (hostname));
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate else
4367c478bd9Sstevel@tonic-gate {
4377c478bd9Sstevel@tonic-gate (void) sysinfo(SI_HOSTNAME,
4387c478bd9Sstevel@tonic-gate hostname, sizeof (hostname));
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate
4417c478bd9Sstevel@tonic-gate /* initialise the connection to the ldap server */
4427c478bd9Sstevel@tonic-gate
4437c478bd9Sstevel@tonic-gate if (cred->port != 0)
4447c478bd9Sstevel@tonic-gate {
4457c478bd9Sstevel@tonic-gate ldapPort = cred->port;
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate *ld = ldap_init(hostname, ldapPort);
4487c478bd9Sstevel@tonic-gate if (*ld == NULL)
4497c478bd9Sstevel@tonic-gate {
4507c478bd9Sstevel@tonic-gate /* connection setup failed */
4517c478bd9Sstevel@tonic-gate result = NSL_ERR_CONNECT;
4527c478bd9Sstevel@tonic-gate #ifdef DEBUG
4537c478bd9Sstevel@tonic-gate (void) perror("ldap_init");
4547c478bd9Sstevel@tonic-gate #endif
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate else
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate /* set ldap options */
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate (void) ldap_set_option(*ld, LDAP_OPT_DEREF,
4617c478bd9Sstevel@tonic-gate &derefOption);
4627c478bd9Sstevel@tonic-gate (void) ldap_set_option(*ld, LDAP_OPT_PROTOCOL_VERSION,
4637c478bd9Sstevel@tonic-gate &protoVersion);
4647c478bd9Sstevel@tonic-gate (void) ldap_set_option(*ld, LDAP_OPT_REFERRALS,
4657c478bd9Sstevel@tonic-gate &referrals);
4667c478bd9Sstevel@tonic-gate
4677c478bd9Sstevel@tonic-gate /* bind to the user DN in the directory */
4687c478bd9Sstevel@tonic-gate
4697c478bd9Sstevel@tonic-gate /* cred->passwdType is currently not supported */
4707c478bd9Sstevel@tonic-gate
4717c478bd9Sstevel@tonic-gate lresult = ldap_simple_bind_s(*ld,
4727c478bd9Sstevel@tonic-gate cred->binddn, cred->passwd);
4737c478bd9Sstevel@tonic-gate
4747c478bd9Sstevel@tonic-gate /*
4757c478bd9Sstevel@tonic-gate * before doing anything else, set up the function to
4767c478bd9Sstevel@tonic-gate * call to get authentication details if the
4777c478bd9Sstevel@tonic-gate * ldap update function calls (eg. ldap_add_s()) get a
4787c478bd9Sstevel@tonic-gate * "referral" (to another ldap server) from the
4797c478bd9Sstevel@tonic-gate * original ldap server, eg. if we are trying to do
4807c478bd9Sstevel@tonic-gate * a update on a LDAP replica server.
4817c478bd9Sstevel@tonic-gate */
4827c478bd9Sstevel@tonic-gate (void) _manageReferralCredentials(*ld,
4837c478bd9Sstevel@tonic-gate &(cred->binddn), &(cred->passwd),
484*ef2333d1SToomas Soome &tmpMethod, -1, NULL);
4857c478bd9Sstevel@tonic-gate ldap_set_rebind_proc(*ld,
4867c478bd9Sstevel@tonic-gate _manageReferralCredentials, NULL);
4877c478bd9Sstevel@tonic-gate
4887c478bd9Sstevel@tonic-gate if (lresult != LDAP_SUCCESS)
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate result = NSL_ERR_BIND;
4917c478bd9Sstevel@tonic-gate *ld = NULL;
4927c478bd9Sstevel@tonic-gate #ifdef DEBUG
4937c478bd9Sstevel@tonic-gate (void) ldap_perror(*ld, "ldap_simple_bind_s");
4947c478bd9Sstevel@tonic-gate #endif
4957c478bd9Sstevel@tonic-gate }
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate
4997c478bd9Sstevel@tonic-gate return (result);
5007c478bd9Sstevel@tonic-gate } /* _connectToLDAP */
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate
5037c478bd9Sstevel@tonic-gate
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate
5067c478bd9Sstevel@tonic-gate /*
5077c478bd9Sstevel@tonic-gate * *****************************************************************************
5087c478bd9Sstevel@tonic-gate *
5097c478bd9Sstevel@tonic-gate * Function: _constructPrinterDN()
5107c478bd9Sstevel@tonic-gate *
5117c478bd9Sstevel@tonic-gate * Description: Construct the DN for the printer object from its name and NS
5127c478bd9Sstevel@tonic-gate * domain DN. If the printer-uri is given in the attrList then
5137c478bd9Sstevel@tonic-gate * that is used instead of the printerName.
5147c478bd9Sstevel@tonic-gate *
5157c478bd9Sstevel@tonic-gate * Parameters:
5167c478bd9Sstevel@tonic-gate * Input: uchar_t *printerName
5177c478bd9Sstevel@tonic-gate * uchar_t *domainDN
5187c478bd9Sstevel@tonic-gate * char **attrList - this list is searched for printer-uri
5197c478bd9Sstevel@tonic-gate * Output: None
5207c478bd9Sstevel@tonic-gate *
5217c478bd9Sstevel@tonic-gate * Returns: uchar_t* - pointer to the DN, this memory is malloced so
5227c478bd9Sstevel@tonic-gate * must be freed using free() when finished with.
5237c478bd9Sstevel@tonic-gate *
5247c478bd9Sstevel@tonic-gate * *****************************************************************************
5257c478bd9Sstevel@tonic-gate */
5267c478bd9Sstevel@tonic-gate
5277c478bd9Sstevel@tonic-gate static uchar_t *
_constructPrinterDN(uchar_t * printerName,uchar_t * domainDN,char ** attrList)5287c478bd9Sstevel@tonic-gate _constructPrinterDN(uchar_t *printerName, uchar_t *domainDN, char **attrList)
5297c478bd9Sstevel@tonic-gate
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate uchar_t *dn = NULL;
5327c478bd9Sstevel@tonic-gate uchar_t *uri = NULL;
5337c478bd9Sstevel@tonic-gate char **p = NULL;
5347c478bd9Sstevel@tonic-gate int len = 0;
5357c478bd9Sstevel@tonic-gate
5367c478bd9Sstevel@tonic-gate /* ------- */
5377c478bd9Sstevel@tonic-gate
5387c478bd9Sstevel@tonic-gate /* first search for printer-uri in the attribute list */
5397c478bd9Sstevel@tonic-gate
5407c478bd9Sstevel@tonic-gate for (p = attrList; (p != NULL) && (*p != NULL) && (uri == NULL); p++)
5417c478bd9Sstevel@tonic-gate {
5427c478bd9Sstevel@tonic-gate /* get length of this key word */
5437c478bd9Sstevel@tonic-gate
5447c478bd9Sstevel@tonic-gate for (len = 0; ((*p)[len] != '=') && ((*p)[len] != '\0'); len++);
5457c478bd9Sstevel@tonic-gate
5467c478bd9Sstevel@tonic-gate if ((strncasecmp(*p, ATTR_URI, len) == 0) &&
5477c478bd9Sstevel@tonic-gate (strlen(*p) > len+1))
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate uri = (uchar_t *)&((*p)[len+1]);
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate
5537c478bd9Sstevel@tonic-gate
5547c478bd9Sstevel@tonic-gate if (domainDN != NULL) {
5557c478bd9Sstevel@tonic-gate size_t size;
5567c478bd9Sstevel@tonic-gate
5577c478bd9Sstevel@tonic-gate /* malloc memory for the DN and then construct it */
5587c478bd9Sstevel@tonic-gate
5597c478bd9Sstevel@tonic-gate if ((uri == NULL) && (printerName != NULL))
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate /* use the printerName for the RDN */
5627c478bd9Sstevel@tonic-gate
5637c478bd9Sstevel@tonic-gate size = strlen(ATTR_URI) +
5647c478bd9Sstevel@tonic-gate strlen((char *)printerName) +
5657c478bd9Sstevel@tonic-gate strlen((char *)domainDN) +
5667c478bd9Sstevel@tonic-gate strlen(PCONTAINER) +
5677c478bd9Sstevel@tonic-gate 10; /* plus a few extra */
5687c478bd9Sstevel@tonic-gate
5697c478bd9Sstevel@tonic-gate if ((dn = malloc(size)) != NULL)
5707c478bd9Sstevel@tonic-gate (void) snprintf((char *)dn, size, "%s=%s,%s,%s",
5717c478bd9Sstevel@tonic-gate ATTR_URI, printerName, PCONTAINER, domainDN);
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate else
5747c478bd9Sstevel@tonic-gate if (uri != NULL)
5757c478bd9Sstevel@tonic-gate {
5767c478bd9Sstevel@tonic-gate /* use the URI for the RDN */
5777c478bd9Sstevel@tonic-gate
5787c478bd9Sstevel@tonic-gate size = strlen(ATTR_URI) +
5797c478bd9Sstevel@tonic-gate strlen((char *)uri) +
5807c478bd9Sstevel@tonic-gate strlen((char *)domainDN) +
5817c478bd9Sstevel@tonic-gate strlen(PCONTAINER) +
5827c478bd9Sstevel@tonic-gate 10; /* plus a few extra */
5837c478bd9Sstevel@tonic-gate
5847c478bd9Sstevel@tonic-gate if ((dn = malloc(size)) != NULL)
5857c478bd9Sstevel@tonic-gate (void) snprintf((char *)dn, size, "%s=%s,%s,%s",
5867c478bd9Sstevel@tonic-gate ATTR_URI, uri, PCONTAINER, domainDN);
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate
5897c478bd9Sstevel@tonic-gate /*
5907c478bd9Sstevel@tonic-gate * else
5917c478bd9Sstevel@tonic-gate * {
5927c478bd9Sstevel@tonic-gate * printName not given so return null
5937c478bd9Sstevel@tonic-gate * }
5947c478bd9Sstevel@tonic-gate */
5957c478bd9Sstevel@tonic-gate
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate
5987c478bd9Sstevel@tonic-gate return (dn); /* caller must free this memory */
5997c478bd9Sstevel@tonic-gate } /* _constructPrinterDN */
6007c478bd9Sstevel@tonic-gate
6017c478bd9Sstevel@tonic-gate
6027c478bd9Sstevel@tonic-gate
6037c478bd9Sstevel@tonic-gate /*
6047c478bd9Sstevel@tonic-gate * *****************************************************************************
6057c478bd9Sstevel@tonic-gate *
6067c478bd9Sstevel@tonic-gate * Function: _checkPrinterExists()
6077c478bd9Sstevel@tonic-gate *
6087c478bd9Sstevel@tonic-gate * Description: Check that the printer object for the printerName exists in the
6097c478bd9Sstevel@tonic-gate * directory DIT and then extract the object's DN
6107c478bd9Sstevel@tonic-gate * The function uses an exiting ldap connection and does a
6117c478bd9Sstevel@tonic-gate * search for the printerName in the supplied domain DN.
6127c478bd9Sstevel@tonic-gate *
6137c478bd9Sstevel@tonic-gate * Parameters:
6147c478bd9Sstevel@tonic-gate * Input: LDAP *ld - existing ldap connection descriptor
6157c478bd9Sstevel@tonic-gate * uchar_t *printerName - printer name
6167c478bd9Sstevel@tonic-gate * uchar_t *domainDN - DN of domain to search in
6177c478bd9Sstevel@tonic-gate * Output: uchar_t **printerDN - DN of the printer - the caller should
6187c478bd9Sstevel@tonic-gate * free this memory using free()
6197c478bd9Sstevel@tonic-gate *
6207c478bd9Sstevel@tonic-gate * Result: NSL_RESULT - NSL_OK = object exists
6217c478bd9Sstevel@tonic-gate *
6227c478bd9Sstevel@tonic-gate * *****************************************************************************
6237c478bd9Sstevel@tonic-gate */
6247c478bd9Sstevel@tonic-gate
6257c478bd9Sstevel@tonic-gate static NSL_RESULT
_checkPrinterExists(LDAP * ld,uchar_t * printerName,uchar_t * domainDN,uchar_t ** printerDN)6267c478bd9Sstevel@tonic-gate _checkPrinterExists(LDAP *ld, uchar_t *printerName, uchar_t *domainDN,
6277c478bd9Sstevel@tonic-gate uchar_t **printerDN)
6287c478bd9Sstevel@tonic-gate
6297c478bd9Sstevel@tonic-gate {
6307c478bd9Sstevel@tonic-gate NSL_RESULT result = NSL_ERR_UNKNOWN_PRINTER;
6317c478bd9Sstevel@tonic-gate int sresult = LDAP_NO_SUCH_OBJECT;
6327c478bd9Sstevel@tonic-gate LDAPMessage *ldapMsg = NULL;
6337c478bd9Sstevel@tonic-gate char *requiredAttrs[2] = { ATTR_PNAME, NULL };
6347c478bd9Sstevel@tonic-gate LDAPMessage *ldapEntry = NULL;
6357c478bd9Sstevel@tonic-gate uchar_t *filter = NULL;
6367c478bd9Sstevel@tonic-gate uchar_t *baseDN = NULL;
6377c478bd9Sstevel@tonic-gate
6387c478bd9Sstevel@tonic-gate /* ---------- */
6397c478bd9Sstevel@tonic-gate
6407c478bd9Sstevel@tonic-gate if ((printerName != NULL) && (domainDN != NULL) && (printerDN != NULL))
6417c478bd9Sstevel@tonic-gate {
6427c478bd9Sstevel@tonic-gate size_t size;
6437c478bd9Sstevel@tonic-gate
6447c478bd9Sstevel@tonic-gate if (printerDN != NULL)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate *printerDN = NULL;
6477c478bd9Sstevel@tonic-gate }
6487c478bd9Sstevel@tonic-gate
6497c478bd9Sstevel@tonic-gate /* search for this Printer in the directory */
6507c478bd9Sstevel@tonic-gate
6517c478bd9Sstevel@tonic-gate size = (3 + strlen((char *)printerName) + strlen(ATTR_PNAME) +
6527c478bd9Sstevel@tonic-gate 2);
6537c478bd9Sstevel@tonic-gate
6547c478bd9Sstevel@tonic-gate if ((filter = malloc(size)) != NULL)
6557c478bd9Sstevel@tonic-gate (void) snprintf((char *)filter, size, "(%s=%s)",
6567c478bd9Sstevel@tonic-gate ATTR_PNAME, (char *)printerName);
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate size = (strlen((char *)domainDN) + strlen(PCONTAINER) + 5);
6597c478bd9Sstevel@tonic-gate
6607c478bd9Sstevel@tonic-gate if ((baseDN = malloc(size)) != NULL)
6617c478bd9Sstevel@tonic-gate (void) snprintf((char *)baseDN, size, "%s,%s",
6627c478bd9Sstevel@tonic-gate PCONTAINER, (char *)domainDN);
6637c478bd9Sstevel@tonic-gate
6647c478bd9Sstevel@tonic-gate sresult = ldap_search_s(ld, (char *)baseDN, LDAP_SCOPE_SUBTREE,
6657c478bd9Sstevel@tonic-gate (char *)filter, requiredAttrs, 0, &ldapMsg);
6667c478bd9Sstevel@tonic-gate if (sresult == LDAP_SUCCESS)
6677c478bd9Sstevel@tonic-gate {
6687c478bd9Sstevel@tonic-gate /* check that the object exists and extract its DN */
6697c478bd9Sstevel@tonic-gate
6707c478bd9Sstevel@tonic-gate ldapEntry = ldap_first_entry(ld, ldapMsg);
6717c478bd9Sstevel@tonic-gate if (ldapEntry != NULL)
6727c478bd9Sstevel@tonic-gate {
6737c478bd9Sstevel@tonic-gate /* object found - there should only be one */
6747c478bd9Sstevel@tonic-gate result = NSL_OK;
6757c478bd9Sstevel@tonic-gate
6767c478bd9Sstevel@tonic-gate if (printerDN != NULL)
6777c478bd9Sstevel@tonic-gate {
6787c478bd9Sstevel@tonic-gate *printerDN = (uchar_t *)
6797c478bd9Sstevel@tonic-gate ldap_get_dn(ld, ldapEntry);
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate
68386b1a8baSrotondo (void) ldap_msgfree(ldapMsg);
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate
6877c478bd9Sstevel@tonic-gate else
6887c478bd9Sstevel@tonic-gate {
6897c478bd9Sstevel@tonic-gate result = NSL_ERR_INTERNAL;
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate
6927c478bd9Sstevel@tonic-gate return (result);
6937c478bd9Sstevel@tonic-gate } /* _checkPrinterExists */
6947c478bd9Sstevel@tonic-gate
6957c478bd9Sstevel@tonic-gate
6967c478bd9Sstevel@tonic-gate
6977c478bd9Sstevel@tonic-gate
6987c478bd9Sstevel@tonic-gate /*
6997c478bd9Sstevel@tonic-gate * *****************************************************************************
7007c478bd9Sstevel@tonic-gate *
7017c478bd9Sstevel@tonic-gate * Function: _checkPrinterDNExists()
7027c478bd9Sstevel@tonic-gate *
7037c478bd9Sstevel@tonic-gate * Description: Check that the printer object for the DN exists in the
7047c478bd9Sstevel@tonic-gate * directory DIT.
7057c478bd9Sstevel@tonic-gate * The function uses an exiting ldap connection and does a
7067c478bd9Sstevel@tonic-gate * search for the DN supplied.
7077c478bd9Sstevel@tonic-gate *
7087c478bd9Sstevel@tonic-gate * Parameters: LDAP *ld - existing ldap connection descriptor
7097c478bd9Sstevel@tonic-gate * char *objectDN - DN to search for
7107c478bd9Sstevel@tonic-gate *
7117c478bd9Sstevel@tonic-gate * Result: NSL_RESULT - NSL_OK = object exists
7127c478bd9Sstevel@tonic-gate *
7137c478bd9Sstevel@tonic-gate * *****************************************************************************
7147c478bd9Sstevel@tonic-gate */
7157c478bd9Sstevel@tonic-gate
7167c478bd9Sstevel@tonic-gate static NSL_RESULT
_checkPrinterDNExists(LDAP * ld,uchar_t * objectDN)7177c478bd9Sstevel@tonic-gate _checkPrinterDNExists(LDAP *ld, uchar_t *objectDN)
7187c478bd9Sstevel@tonic-gate
7197c478bd9Sstevel@tonic-gate {
7207c478bd9Sstevel@tonic-gate NSL_RESULT result = NSL_ERR_UNKNOWN_PRINTER;
7217c478bd9Sstevel@tonic-gate int sresult = LDAP_NO_SUCH_OBJECT;
7227c478bd9Sstevel@tonic-gate LDAPMessage *ldapMsg;
7237c478bd9Sstevel@tonic-gate char *requiredAttrs[2] = { ATTR_PNAME, NULL };
7247c478bd9Sstevel@tonic-gate LDAPMessage *ldapEntry;
7257c478bd9Sstevel@tonic-gate
7267c478bd9Sstevel@tonic-gate /* ---------- */
7277c478bd9Sstevel@tonic-gate
7287c478bd9Sstevel@tonic-gate if ((ld != NULL) && (objectDN != NULL))
7297c478bd9Sstevel@tonic-gate {
7307c478bd9Sstevel@tonic-gate /* search for this Printer in the directory */
7317c478bd9Sstevel@tonic-gate
7327c478bd9Sstevel@tonic-gate sresult = ldap_search_s(ld, (char *)objectDN, LDAP_SCOPE_BASE,
7337c478bd9Sstevel@tonic-gate "(objectclass=*)", requiredAttrs, 0, &ldapMsg);
7347c478bd9Sstevel@tonic-gate if (sresult == LDAP_SUCCESS)
7357c478bd9Sstevel@tonic-gate {
7367c478bd9Sstevel@tonic-gate /* check that the object exists */
7377c478bd9Sstevel@tonic-gate ldapEntry = ldap_first_entry(ld, ldapMsg);
7387c478bd9Sstevel@tonic-gate if (ldapEntry != NULL)
7397c478bd9Sstevel@tonic-gate {
7407c478bd9Sstevel@tonic-gate /* object found */
7417c478bd9Sstevel@tonic-gate result = NSL_OK;
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate
74486b1a8baSrotondo (void) ldap_msgfree(ldapMsg);
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate }
7477c478bd9Sstevel@tonic-gate
7487c478bd9Sstevel@tonic-gate else
7497c478bd9Sstevel@tonic-gate {
7507c478bd9Sstevel@tonic-gate result = NSL_ERR_INTERNAL;
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate
7537c478bd9Sstevel@tonic-gate return (result);
7547c478bd9Sstevel@tonic-gate } /* _checkPrinterDNExists */
7557c478bd9Sstevel@tonic-gate
7567c478bd9Sstevel@tonic-gate
7577c478bd9Sstevel@tonic-gate
7587c478bd9Sstevel@tonic-gate
7597c478bd9Sstevel@tonic-gate
7607c478bd9Sstevel@tonic-gate /*
7617c478bd9Sstevel@tonic-gate * *****************************************************************************
7627c478bd9Sstevel@tonic-gate *
7637c478bd9Sstevel@tonic-gate * Function: _checkSunPrinter()
7647c478bd9Sstevel@tonic-gate *
7657c478bd9Sstevel@tonic-gate * Description: Check that the printer object for the printerDN is a sunPrinter
7667c478bd9Sstevel@tonic-gate * ie. it has the required objectclass attribute value.
7677c478bd9Sstevel@tonic-gate *
7687c478bd9Sstevel@tonic-gate * Parameters:
7697c478bd9Sstevel@tonic-gate * Input: LDAP *ld - existing ldap connection descriptor
7707c478bd9Sstevel@tonic-gate * Output: uchar_t *printerDN - DN of the printer
7717c478bd9Sstevel@tonic-gate *
7727c478bd9Sstevel@tonic-gate * Result: NSL_RESULT - NSL_OK = object exists and is a sunPrinter
7737c478bd9Sstevel@tonic-gate *
7747c478bd9Sstevel@tonic-gate * *****************************************************************************
7757c478bd9Sstevel@tonic-gate */
7767c478bd9Sstevel@tonic-gate
7777c478bd9Sstevel@tonic-gate static NSL_RESULT
_checkSunPrinter(LDAP * ld,uchar_t * printerDN)7787c478bd9Sstevel@tonic-gate _checkSunPrinter(LDAP *ld, uchar_t *printerDN)
7797c478bd9Sstevel@tonic-gate
7807c478bd9Sstevel@tonic-gate {
7817c478bd9Sstevel@tonic-gate NSL_RESULT result = NSL_ERR_UNKNOWN_PRINTER;
7827c478bd9Sstevel@tonic-gate int sresult = LDAP_NO_SUCH_OBJECT;
7837c478bd9Sstevel@tonic-gate char *requiredAttrs[2] = { ATTR_PNAME, NULL };
7847c478bd9Sstevel@tonic-gate LDAPMessage *ldapMsg = NULL;
7857c478bd9Sstevel@tonic-gate LDAPMessage *ldapEntry = NULL;
7867c478bd9Sstevel@tonic-gate char *filter = NULL;
7877c478bd9Sstevel@tonic-gate
7887c478bd9Sstevel@tonic-gate /* ---------- */
7897c478bd9Sstevel@tonic-gate
7907c478bd9Sstevel@tonic-gate if ((ld != NULL) && (printerDN != NULL))
7917c478bd9Sstevel@tonic-gate {
7927c478bd9Sstevel@tonic-gate size_t size;
7937c478bd9Sstevel@tonic-gate
7947c478bd9Sstevel@tonic-gate /* search for this Printer in the directory */
7957c478bd9Sstevel@tonic-gate
7967c478bd9Sstevel@tonic-gate size = (3 + strlen(OCV_SUNPRT) + strlen(ATTR_OCLASS) + 2);
7977c478bd9Sstevel@tonic-gate if ((filter = malloc(size)) != NULL)
7987c478bd9Sstevel@tonic-gate (void) snprintf(filter, size, "(%s=%s)",
7997c478bd9Sstevel@tonic-gate ATTR_OCLASS, OCV_SUNPRT);
8007c478bd9Sstevel@tonic-gate
8017c478bd9Sstevel@tonic-gate sresult = ldap_search_s(ld, (char *)printerDN,
8027c478bd9Sstevel@tonic-gate LDAP_SCOPE_SUBTREE, filter,
8037c478bd9Sstevel@tonic-gate requiredAttrs, 0, &ldapMsg);
8047c478bd9Sstevel@tonic-gate if (sresult == LDAP_SUCCESS)
8057c478bd9Sstevel@tonic-gate {
8067c478bd9Sstevel@tonic-gate /* check that the printer object exists */
8077c478bd9Sstevel@tonic-gate
8087c478bd9Sstevel@tonic-gate ldapEntry = ldap_first_entry(ld, ldapMsg);
8097c478bd9Sstevel@tonic-gate if (ldapEntry != NULL)
8107c478bd9Sstevel@tonic-gate {
8117c478bd9Sstevel@tonic-gate /* object is a sunPrinter */
8127c478bd9Sstevel@tonic-gate result = NSL_OK;
8137c478bd9Sstevel@tonic-gate }
8147c478bd9Sstevel@tonic-gate
81586b1a8baSrotondo (void) ldap_msgfree(ldapMsg);
8167c478bd9Sstevel@tonic-gate }
8177c478bd9Sstevel@tonic-gate }
8187c478bd9Sstevel@tonic-gate
8197c478bd9Sstevel@tonic-gate else
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate result = NSL_ERR_INTERNAL;
8227c478bd9Sstevel@tonic-gate }
8237c478bd9Sstevel@tonic-gate
8247c478bd9Sstevel@tonic-gate return (result);
8257c478bd9Sstevel@tonic-gate } /* _checkSunPrinter */
8267c478bd9Sstevel@tonic-gate
8277c478bd9Sstevel@tonic-gate
8287c478bd9Sstevel@tonic-gate
8297c478bd9Sstevel@tonic-gate
8307c478bd9Sstevel@tonic-gate
8317c478bd9Sstevel@tonic-gate /*
8327c478bd9Sstevel@tonic-gate * *****************************************************************************
8337c478bd9Sstevel@tonic-gate *
8347c478bd9Sstevel@tonic-gate * Function: _addNewPrinterObject()
8357c478bd9Sstevel@tonic-gate *
8367c478bd9Sstevel@tonic-gate * Description: For the given printerName add a printer object into the
8377c478bd9Sstevel@tonic-gate * LDAP directory NS domain. The object is created with the
8387c478bd9Sstevel@tonic-gate * supplied attribute values. Note: if the printer's uri is
8397c478bd9Sstevel@tonic-gate * given that is used as the RDN otherwise the printer's
8407c478bd9Sstevel@tonic-gate * name is used as the RDN
8417c478bd9Sstevel@tonic-gate *
8427c478bd9Sstevel@tonic-gate * Parameters:
8437c478bd9Sstevel@tonic-gate * Input: LDAP *ld - existing ldap connection descriptor
8447c478bd9Sstevel@tonic-gate * uchar_t *printerName - Name of printer to be added
8457c478bd9Sstevel@tonic-gate * uchar_t *domainDN - DN of the domain to add the printer
8467c478bd9Sstevel@tonic-gate * char **attrList - user specified attribute values list
8477c478bd9Sstevel@tonic-gate * Output: None
8487c478bd9Sstevel@tonic-gate *
8497c478bd9Sstevel@tonic-gate * Returns: NSL_RESULT - NSL_OK = request actioned okay
8507c478bd9Sstevel@tonic-gate * !NSL_OK = error
8517c478bd9Sstevel@tonic-gate *
8527c478bd9Sstevel@tonic-gate * *****************************************************************************
8537c478bd9Sstevel@tonic-gate */
8547c478bd9Sstevel@tonic-gate
8557c478bd9Sstevel@tonic-gate static NSL_RESULT
_addNewPrinterObject(LDAP * ld,uchar_t * printerName,uchar_t * domainDN,char ** attrList)8567c478bd9Sstevel@tonic-gate _addNewPrinterObject(LDAP *ld, uchar_t *printerName,
8577c478bd9Sstevel@tonic-gate uchar_t *domainDN, char **attrList)
8587c478bd9Sstevel@tonic-gate
8597c478bd9Sstevel@tonic-gate {
8607c478bd9Sstevel@tonic-gate NSL_RESULT result = NSL_ERR_ADD_FAILED;
8617c478bd9Sstevel@tonic-gate int lresult = 0;
8627c478bd9Sstevel@tonic-gate uchar_t *printerDN = NULL;
8637c478bd9Sstevel@tonic-gate LDAPMod **attrs = NULL;
8647c478bd9Sstevel@tonic-gate
8657c478bd9Sstevel@tonic-gate /* ---------- */
8667c478bd9Sstevel@tonic-gate
8677c478bd9Sstevel@tonic-gate if ((ld != NULL) && (printerName != NULL) && (domainDN != NULL) &&
8687c478bd9Sstevel@tonic-gate (attrList != NULL) && (attrList[0] != NULL))
8697c478bd9Sstevel@tonic-gate {
8707c478bd9Sstevel@tonic-gate result = _checkAttributes(attrList);
8717c478bd9Sstevel@tonic-gate
8727c478bd9Sstevel@tonic-gate if (result == NSL_OK)
8737c478bd9Sstevel@tonic-gate {
8747c478bd9Sstevel@tonic-gate /*
8757c478bd9Sstevel@tonic-gate * construct a DN for the printer from the
8767c478bd9Sstevel@tonic-gate * printerName and printer-uri if given.
8777c478bd9Sstevel@tonic-gate */
8787c478bd9Sstevel@tonic-gate printerDN = _constructPrinterDN(printerName,
8797c478bd9Sstevel@tonic-gate domainDN, attrList);
8807c478bd9Sstevel@tonic-gate if (printerDN != NULL)
8817c478bd9Sstevel@tonic-gate {
8827c478bd9Sstevel@tonic-gate /*
8837c478bd9Sstevel@tonic-gate * setup attribute values in an LDAPMod
8847c478bd9Sstevel@tonic-gate * structure and then add the object
8857c478bd9Sstevel@tonic-gate */
8867c478bd9Sstevel@tonic-gate result = _constructAddLDAPMod(printerName,
8877c478bd9Sstevel@tonic-gate attrList, &attrs);
8887c478bd9Sstevel@tonic-gate if (result == NSL_OK)
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate lresult = ldap_add_s(ld,
8917c478bd9Sstevel@tonic-gate (char *)printerDN, attrs);
8927c478bd9Sstevel@tonic-gate if (lresult == LDAP_SUCCESS)
8937c478bd9Sstevel@tonic-gate {
8947c478bd9Sstevel@tonic-gate result = NSL_OK;
8957c478bd9Sstevel@tonic-gate }
8967c478bd9Sstevel@tonic-gate else
8977c478bd9Sstevel@tonic-gate {
8987c478bd9Sstevel@tonic-gate result = NSL_ERR_ADD_FAILED;
8997c478bd9Sstevel@tonic-gate #ifdef DEBUG
9007c478bd9Sstevel@tonic-gate (void) ldap_perror(ld, "ldap_add_s");
9017c478bd9Sstevel@tonic-gate #endif
9027c478bd9Sstevel@tonic-gate }
9037c478bd9Sstevel@tonic-gate
9047c478bd9Sstevel@tonic-gate (void) ldap_mods_free(attrs, 1);
9057c478bd9Sstevel@tonic-gate }
9067c478bd9Sstevel@tonic-gate free(printerDN);
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate
9097c478bd9Sstevel@tonic-gate else
9107c478bd9Sstevel@tonic-gate {
9117c478bd9Sstevel@tonic-gate result = NSL_ERR_INTERNAL;
9127c478bd9Sstevel@tonic-gate }
9137c478bd9Sstevel@tonic-gate }
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate
9167c478bd9Sstevel@tonic-gate else
9177c478bd9Sstevel@tonic-gate {
9187c478bd9Sstevel@tonic-gate result = NSL_ERR_INTERNAL;
9197c478bd9Sstevel@tonic-gate }
9207c478bd9Sstevel@tonic-gate
9217c478bd9Sstevel@tonic-gate return (result);
9227c478bd9Sstevel@tonic-gate } /* _addNewPrinterObject */
9237c478bd9Sstevel@tonic-gate
9247c478bd9Sstevel@tonic-gate
9257c478bd9Sstevel@tonic-gate
9267c478bd9Sstevel@tonic-gate
9277c478bd9Sstevel@tonic-gate
9287c478bd9Sstevel@tonic-gate
9297c478bd9Sstevel@tonic-gate /*
9307c478bd9Sstevel@tonic-gate * *****************************************************************************
9317c478bd9Sstevel@tonic-gate *
9327c478bd9Sstevel@tonic-gate * Function: _modifyPrinterObject()
9337c478bd9Sstevel@tonic-gate *
9347c478bd9Sstevel@tonic-gate * Description: Modify the given LDAP printer object to set the new attributes
9357c478bd9Sstevel@tonic-gate * in the attribute list. If the printer's URI (specified in the
9367c478bd9Sstevel@tonic-gate * attrList) changes the URI of the object the request is rejected.
9377c478bd9Sstevel@tonic-gate *
9387c478bd9Sstevel@tonic-gate * Parameters:
9397c478bd9Sstevel@tonic-gate * Input: LDAP *ld - existing ldap connection descriptor
9407c478bd9Sstevel@tonic-gate * uchar_t *printerDN - DN of printer object to modify
9417c478bd9Sstevel@tonic-gate * uchar_t *printerName - Name of printer to be modified
9427c478bd9Sstevel@tonic-gate * uchar_t *domainDN - DN of the domain the printer is in
9437c478bd9Sstevel@tonic-gate * char **attrList - user specified attribute values list
9447c478bd9Sstevel@tonic-gate * Output: None
9457c478bd9Sstevel@tonic-gate *
9467c478bd9Sstevel@tonic-gate * Returns: NSL_RESULT - NSL_OK = object modified okay
9477c478bd9Sstevel@tonic-gate *
9487c478bd9Sstevel@tonic-gate * *****************************************************************************
9497c478bd9Sstevel@tonic-gate */
9507c478bd9Sstevel@tonic-gate
9517c478bd9Sstevel@tonic-gate static NSL_RESULT
_modifyPrinterObject(LDAP * ld,uchar_t * printerDN,uchar_t * printerName,uchar_t * domainDN,char ** attrList)9527c478bd9Sstevel@tonic-gate _modifyPrinterObject(LDAP *ld, uchar_t *printerDN,
9537c478bd9Sstevel@tonic-gate uchar_t *printerName, uchar_t *domainDN, char **attrList)
9547c478bd9Sstevel@tonic-gate
9557c478bd9Sstevel@tonic-gate {
9567c478bd9Sstevel@tonic-gate NSL_RESULT result = NSL_ERR_INTERNAL;
9577c478bd9Sstevel@tonic-gate int lresult = 0;
9587c478bd9Sstevel@tonic-gate int sunPrinter = 0;
9597c478bd9Sstevel@tonic-gate uchar_t *uriDN = NULL;
9607c478bd9Sstevel@tonic-gate LDAPMod **attrs = NULL;
9617c478bd9Sstevel@tonic-gate char **kvpList = NULL;
9627c478bd9Sstevel@tonic-gate
9637c478bd9Sstevel@tonic-gate /* ---------- */
9647c478bd9Sstevel@tonic-gate
9657c478bd9Sstevel@tonic-gate if ((ld != NULL) && (printerDN != NULL) && (printerName != NULL) &&
9667c478bd9Sstevel@tonic-gate (domainDN != NULL) && (attrList != NULL) && (attrList[0] != NULL))
9677c478bd9Sstevel@tonic-gate {
9687c478bd9Sstevel@tonic-gate result = _checkAttributes(attrList);
9697c478bd9Sstevel@tonic-gate
9707c478bd9Sstevel@tonic-gate if (result == NSL_OK)
9717c478bd9Sstevel@tonic-gate {
9727c478bd9Sstevel@tonic-gate /*
9737c478bd9Sstevel@tonic-gate * The user may have requested that the printer object
9747c478bd9Sstevel@tonic-gate * be given a new URI RDN, so construct a DN for the
9757c478bd9Sstevel@tonic-gate * printer from the printerName or the printer-uri (if
9767c478bd9Sstevel@tonic-gate * given).
9777c478bd9Sstevel@tonic-gate */
9787c478bd9Sstevel@tonic-gate uriDN = _constructPrinterDN(NULL, domainDN, attrList);
9797c478bd9Sstevel@tonic-gate
9807c478bd9Sstevel@tonic-gate /*
9817c478bd9Sstevel@tonic-gate * compare the 2 DNs to see if the URI has changed,
9827c478bd9Sstevel@tonic-gate * if uriDN is null then the DN hasn't changed
9837c478bd9Sstevel@tonic-gate */
9847c478bd9Sstevel@tonic-gate if ((uriDN == NULL) || ((uriDN != NULL) &&
9857c478bd9Sstevel@tonic-gate (_compareURIinDNs(printerDN, uriDN) == NSL_OK)))
9867c478bd9Sstevel@tonic-gate {
9877c478bd9Sstevel@tonic-gate /*
9887c478bd9Sstevel@tonic-gate * setup the modify object LDAPMod
9897c478bd9Sstevel@tonic-gate * structure and then do the modify
9907c478bd9Sstevel@tonic-gate */
9917c478bd9Sstevel@tonic-gate
9927c478bd9Sstevel@tonic-gate if (_checkSunPrinter(ld, printerDN) == NSL_OK)
9937c478bd9Sstevel@tonic-gate {
9947c478bd9Sstevel@tonic-gate sunPrinter = 1;
9957c478bd9Sstevel@tonic-gate }
9967c478bd9Sstevel@tonic-gate
9977c478bd9Sstevel@tonic-gate (void) _getCurrentKVPValues(ld,
9987c478bd9Sstevel@tonic-gate printerDN, &kvpList);
9997c478bd9Sstevel@tonic-gate
10007c478bd9Sstevel@tonic-gate result = _constructModLDAPMod(printerName,
10017c478bd9Sstevel@tonic-gate sunPrinter, attrList,
10027c478bd9Sstevel@tonic-gate &kvpList, &attrs);
10037c478bd9Sstevel@tonic-gate _freeList(&kvpList);
10047c478bd9Sstevel@tonic-gate
10057c478bd9Sstevel@tonic-gate if ((result == NSL_OK) && (attrs != NULL))
10067c478bd9Sstevel@tonic-gate {
10077c478bd9Sstevel@tonic-gate lresult = ldap_modify_s(
10087c478bd9Sstevel@tonic-gate ld, (char *)printerDN, attrs);
10097c478bd9Sstevel@tonic-gate if (lresult == LDAP_SUCCESS)
10107c478bd9Sstevel@tonic-gate {
10117c478bd9Sstevel@tonic-gate result = NSL_OK;
10127c478bd9Sstevel@tonic-gate }
10137c478bd9Sstevel@tonic-gate else
10147c478bd9Sstevel@tonic-gate {
10157c478bd9Sstevel@tonic-gate result = NSL_ERR_MOD_FAILED;
10167c478bd9Sstevel@tonic-gate #ifdef DEBUG
10177c478bd9Sstevel@tonic-gate (void) ldap_perror(ld, "ldap_modify_s");
10187c478bd9Sstevel@tonic-gate #endif
10197c478bd9Sstevel@tonic-gate }
10207c478bd9Sstevel@tonic-gate
10217c478bd9Sstevel@tonic-gate (void) ldap_mods_free(attrs, 1);
10227c478bd9Sstevel@tonic-gate }
10237c478bd9Sstevel@tonic-gate }
10247c478bd9Sstevel@tonic-gate else
10257c478bd9Sstevel@tonic-gate {
10267c478bd9Sstevel@tonic-gate /*
10277c478bd9Sstevel@tonic-gate * printer-uri name change has been requested
10287c478bd9Sstevel@tonic-gate * this is NOT allowed as it requires that
10297c478bd9Sstevel@tonic-gate * a new printer object is created
10307c478bd9Sstevel@tonic-gate */
10317c478bd9Sstevel@tonic-gate result = NSL_ERR_RENAME; /* NOT ALLOWED */
10327c478bd9Sstevel@tonic-gate }
10337c478bd9Sstevel@tonic-gate
10347c478bd9Sstevel@tonic-gate if (uriDN != NULL)
10357c478bd9Sstevel@tonic-gate {
10367c478bd9Sstevel@tonic-gate free(uriDN);
10377c478bd9Sstevel@tonic-gate }
10387c478bd9Sstevel@tonic-gate }
10397c478bd9Sstevel@tonic-gate }
10407c478bd9Sstevel@tonic-gate
10417c478bd9Sstevel@tonic-gate return (result);
10427c478bd9Sstevel@tonic-gate } /* _modifyPrinterObject */
10437c478bd9Sstevel@tonic-gate
10447c478bd9Sstevel@tonic-gate
10457c478bd9Sstevel@tonic-gate
10467c478bd9Sstevel@tonic-gate
10477c478bd9Sstevel@tonic-gate /*
10487c478bd9Sstevel@tonic-gate * *****************************************************************************
10497c478bd9Sstevel@tonic-gate *
10507c478bd9Sstevel@tonic-gate * Function: _checkAttributes()
10517c478bd9Sstevel@tonic-gate *
10527c478bd9Sstevel@tonic-gate * Description: Check that the given attribute lists does not contain any
10537c478bd9Sstevel@tonic-gate * key words that are not allowed.
10547c478bd9Sstevel@tonic-gate *
10557c478bd9Sstevel@tonic-gate * Parameters:
10567c478bd9Sstevel@tonic-gate * Input: char **list - attribute list to check
10577c478bd9Sstevel@tonic-gate * Output: None
10587c478bd9Sstevel@tonic-gate *
10597c478bd9Sstevel@tonic-gate * Returns: NSL_RESULT - NSL_OK = checked okay
10607c478bd9Sstevel@tonic-gate *
10617c478bd9Sstevel@tonic-gate * *****************************************************************************
10627c478bd9Sstevel@tonic-gate */
10637c478bd9Sstevel@tonic-gate
10647c478bd9Sstevel@tonic-gate static NSL_RESULT
_checkAttributes(char ** list)10657c478bd9Sstevel@tonic-gate _checkAttributes(char **list)
10667c478bd9Sstevel@tonic-gate
10677c478bd9Sstevel@tonic-gate {
10687c478bd9Sstevel@tonic-gate NSL_RESULT result = NSL_OK;
10697c478bd9Sstevel@tonic-gate int len = 0;
10707c478bd9Sstevel@tonic-gate char *attr = NULL;
10717c478bd9Sstevel@tonic-gate char **p = NULL;
10727c478bd9Sstevel@tonic-gate
10737c478bd9Sstevel@tonic-gate /* ------ */
10747c478bd9Sstevel@tonic-gate
10757c478bd9Sstevel@tonic-gate for (p = list; (p != NULL) && (*p != NULL) && (result == NSL_OK); p++)
10767c478bd9Sstevel@tonic-gate {
10777c478bd9Sstevel@tonic-gate /* get length of this key word */
10787c478bd9Sstevel@tonic-gate
10797c478bd9Sstevel@tonic-gate for (len = 0; ((*p)[len] != '=') && ((*p)[len] != '\0'); len++);
10807c478bd9Sstevel@tonic-gate
10817c478bd9Sstevel@tonic-gate /* check if the key word is allowed */
10827c478bd9Sstevel@tonic-gate
10837c478bd9Sstevel@tonic-gate if (strncasecmp(*p, ATTR_KVP, len) == 0)
10847c478bd9Sstevel@tonic-gate {
10857c478bd9Sstevel@tonic-gate /* not supported through this interface */
10867c478bd9Sstevel@tonic-gate result = NSL_ERR_KVP;
10877c478bd9Sstevel@tonic-gate }
10887c478bd9Sstevel@tonic-gate else
10897c478bd9Sstevel@tonic-gate if (strncasecmp(*p, ATTR_BSDADDR, len) == 0)
10907c478bd9Sstevel@tonic-gate {
10917c478bd9Sstevel@tonic-gate /* not supported through this interface */
10927c478bd9Sstevel@tonic-gate result = NSL_ERR_BSDADDR;
10937c478bd9Sstevel@tonic-gate }
10947c478bd9Sstevel@tonic-gate else
10957c478bd9Sstevel@tonic-gate if (strncasecmp(*p, ATTR_PNAME, len) == 0)
10967c478bd9Sstevel@tonic-gate {
10977c478bd9Sstevel@tonic-gate /* not supported through this interface */
10987c478bd9Sstevel@tonic-gate result = NSL_ERR_PNAME;
10997c478bd9Sstevel@tonic-gate }
11007c478bd9Sstevel@tonic-gate else
11017c478bd9Sstevel@tonic-gate {
11027c478bd9Sstevel@tonic-gate /* check for any others */
11037c478bd9Sstevel@tonic-gate
11047c478bd9Sstevel@tonic-gate attr = strdup(*p);
11057c478bd9Sstevel@tonic-gate attr[len] = '\0'; /* terminate the key */
11067c478bd9Sstevel@tonic-gate
11077c478bd9Sstevel@tonic-gate if (_attrInList(attr, nsl_attr_notAllowed))
11087c478bd9Sstevel@tonic-gate {
11097c478bd9Sstevel@tonic-gate result = NSL_ERR_NOTALLOWED;
11107c478bd9Sstevel@tonic-gate }
11117c478bd9Sstevel@tonic-gate }
11127c478bd9Sstevel@tonic-gate
11137c478bd9Sstevel@tonic-gate }
11147c478bd9Sstevel@tonic-gate
11157c478bd9Sstevel@tonic-gate return (result);
11167c478bd9Sstevel@tonic-gate } /* _checkAttributes */
11177c478bd9Sstevel@tonic-gate
11187c478bd9Sstevel@tonic-gate
11197c478bd9Sstevel@tonic-gate
11207c478bd9Sstevel@tonic-gate
11217c478bd9Sstevel@tonic-gate /*
11227c478bd9Sstevel@tonic-gate * *****************************************************************************
11237c478bd9Sstevel@tonic-gate *
11247c478bd9Sstevel@tonic-gate * Function: _addLDAPmodValue()
11257c478bd9Sstevel@tonic-gate *
11267c478bd9Sstevel@tonic-gate * Description: Add the given attribute and its value to the LDAPMod array.
11277c478bd9Sstevel@tonic-gate * If this is the first entry in the array then create it.
11287c478bd9Sstevel@tonic-gate *
11297c478bd9Sstevel@tonic-gate * Parameters:
11307c478bd9Sstevel@tonic-gate * Input: LDAPMod ***attrs - array to update
11317c478bd9Sstevel@tonic-gate * char *type - attribute to add into array
11327c478bd9Sstevel@tonic-gate * char *value - attribute value
11337c478bd9Sstevel@tonic-gate * Output: None
11347c478bd9Sstevel@tonic-gate *
11357c478bd9Sstevel@tonic-gate * Returns: NSL_RESULT - NSL_OK = added okay
11367c478bd9Sstevel@tonic-gate *
11377c478bd9Sstevel@tonic-gate * *****************************************************************************
11387c478bd9Sstevel@tonic-gate */
11397c478bd9Sstevel@tonic-gate
11407c478bd9Sstevel@tonic-gate static NSL_RESULT
_addLDAPmodValue(LDAPMod *** attrs,char * type,char * value)11417c478bd9Sstevel@tonic-gate _addLDAPmodValue(LDAPMod ***attrs, char *type, char *value)
11427c478bd9Sstevel@tonic-gate
11437c478bd9Sstevel@tonic-gate {
11447c478bd9Sstevel@tonic-gate int i = 0;
11457c478bd9Sstevel@tonic-gate int j = 0;
11467c478bd9Sstevel@tonic-gate NSL_RESULT result = NSL_OK;
11477c478bd9Sstevel@tonic-gate
11487c478bd9Sstevel@tonic-gate /* ---------- */
11497c478bd9Sstevel@tonic-gate
11507c478bd9Sstevel@tonic-gate if ((attrs != NULL) && (type != NULL) && (value != NULL))
11517c478bd9Sstevel@tonic-gate {
11527c478bd9Sstevel@tonic-gate #ifdef DEBUG
11537c478bd9Sstevel@tonic-gate printf("_addLDAPmodValue() type='%s', value='%s'\n", type, value);
11547c478bd9Sstevel@tonic-gate #endif
11557c478bd9Sstevel@tonic-gate /* search the existing LDAPMod array for the attribute */
11567c478bd9Sstevel@tonic-gate
11577c478bd9Sstevel@tonic-gate for (i = 0; *attrs != NULL && (*attrs)[i] != NULL; i++)
11587c478bd9Sstevel@tonic-gate {
11597c478bd9Sstevel@tonic-gate if (strcasecmp((*attrs)[i]->mod_type, type) == 0)
11607c478bd9Sstevel@tonic-gate {
11617c478bd9Sstevel@tonic-gate break;
11627c478bd9Sstevel@tonic-gate }
11637c478bd9Sstevel@tonic-gate }
11647c478bd9Sstevel@tonic-gate
11657c478bd9Sstevel@tonic-gate if (*attrs == NULL)
11667c478bd9Sstevel@tonic-gate {
11677c478bd9Sstevel@tonic-gate /* array empty so create it */
11687c478bd9Sstevel@tonic-gate
11697c478bd9Sstevel@tonic-gate *attrs = (LDAPMod **)calloc(1, 2 * sizeof (LDAPMod *));
11707c478bd9Sstevel@tonic-gate if (*attrs != NULL)
11717c478bd9Sstevel@tonic-gate {
11727c478bd9Sstevel@tonic-gate i = 0;
11737c478bd9Sstevel@tonic-gate }
11747c478bd9Sstevel@tonic-gate else
11757c478bd9Sstevel@tonic-gate {
11767c478bd9Sstevel@tonic-gate result = NSL_ERR_MEMORY;
11777c478bd9Sstevel@tonic-gate }
11787c478bd9Sstevel@tonic-gate
11797c478bd9Sstevel@tonic-gate }
11807c478bd9Sstevel@tonic-gate else
11817c478bd9Sstevel@tonic-gate if ((*attrs)[i] == NULL)
11827c478bd9Sstevel@tonic-gate {
11837c478bd9Sstevel@tonic-gate *attrs = (LDAPMod **)
11847c478bd9Sstevel@tonic-gate realloc(*attrs, (i+2) * sizeof (LDAPMod *));
11857c478bd9Sstevel@tonic-gate if (*attrs == NULL)
11867c478bd9Sstevel@tonic-gate {
11877c478bd9Sstevel@tonic-gate result = NSL_ERR_MEMORY;
11887c478bd9Sstevel@tonic-gate }
11897c478bd9Sstevel@tonic-gate }
11907c478bd9Sstevel@tonic-gate }
11917c478bd9Sstevel@tonic-gate else
11927c478bd9Sstevel@tonic-gate {
11937c478bd9Sstevel@tonic-gate result = NSL_ERR_INTERNAL;
11947c478bd9Sstevel@tonic-gate }
11957c478bd9Sstevel@tonic-gate
11967c478bd9Sstevel@tonic-gate if (result == NSL_OK)
11977c478bd9Sstevel@tonic-gate {
11987c478bd9Sstevel@tonic-gate if ((*attrs)[i] == NULL)
11997c478bd9Sstevel@tonic-gate {
12007c478bd9Sstevel@tonic-gate /* We've got a new slot. Create the new mod. */
12017c478bd9Sstevel@tonic-gate
12027c478bd9Sstevel@tonic-gate (*attrs)[i] = (LDAPMod *) malloc(sizeof (LDAPMod));
12037c478bd9Sstevel@tonic-gate if ((*attrs)[i] != NULL)
12047c478bd9Sstevel@tonic-gate {
12057c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_op = LDAP_MOD_ADD;
12067c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_type = strdup(type);
12077c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_values = (char **)
12087c478bd9Sstevel@tonic-gate malloc(2 * sizeof (char *));
12097c478bd9Sstevel@tonic-gate if ((*attrs)[i]->mod_values != NULL)
12107c478bd9Sstevel@tonic-gate {
12117c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_values[0] =
12127c478bd9Sstevel@tonic-gate strdup(value);
12137c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_values[1] = NULL;
12147c478bd9Sstevel@tonic-gate (*attrs)[i+1] = NULL;
12157c478bd9Sstevel@tonic-gate }
12167c478bd9Sstevel@tonic-gate else
12177c478bd9Sstevel@tonic-gate {
12187c478bd9Sstevel@tonic-gate result = NSL_ERR_MEMORY;
12197c478bd9Sstevel@tonic-gate }
12207c478bd9Sstevel@tonic-gate }
12217c478bd9Sstevel@tonic-gate else
12227c478bd9Sstevel@tonic-gate {
12237c478bd9Sstevel@tonic-gate result = NSL_ERR_MEMORY;
12247c478bd9Sstevel@tonic-gate }
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate
12277c478bd9Sstevel@tonic-gate else
12287c478bd9Sstevel@tonic-gate {
12297c478bd9Sstevel@tonic-gate /* Found an existing entry so add value to it */
12307c478bd9Sstevel@tonic-gate
12317c478bd9Sstevel@tonic-gate for (j = 0; (*attrs)[i]->mod_values[j] != NULL; j++);
12327c478bd9Sstevel@tonic-gate
12337c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_values =
12347c478bd9Sstevel@tonic-gate (char **)realloc((*attrs)[i]->mod_values,
12357c478bd9Sstevel@tonic-gate (j + 2) * sizeof (char *));
12367c478bd9Sstevel@tonic-gate if ((*attrs)[i]->mod_values != NULL)
12377c478bd9Sstevel@tonic-gate {
12387c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_values[j] = strdup(value);
12397c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_values[j+1] = NULL;
12407c478bd9Sstevel@tonic-gate }
12417c478bd9Sstevel@tonic-gate else
12427c478bd9Sstevel@tonic-gate {
12437c478bd9Sstevel@tonic-gate result = NSL_ERR_MEMORY;
12447c478bd9Sstevel@tonic-gate }
12457c478bd9Sstevel@tonic-gate }
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate
12487c478bd9Sstevel@tonic-gate return (result);
12497c478bd9Sstevel@tonic-gate } /* _addLDAPmodValue */
12507c478bd9Sstevel@tonic-gate
12517c478bd9Sstevel@tonic-gate
12527c478bd9Sstevel@tonic-gate
12537c478bd9Sstevel@tonic-gate
12547c478bd9Sstevel@tonic-gate /*
12557c478bd9Sstevel@tonic-gate * *****************************************************************************
12567c478bd9Sstevel@tonic-gate *
12577c478bd9Sstevel@tonic-gate * Function: _modLDAPmodValue()
12587c478bd9Sstevel@tonic-gate *
12597c478bd9Sstevel@tonic-gate * Description: Add the given attribute modify operation and its value into
12607c478bd9Sstevel@tonic-gate * the LDAPMod array. This will either be a "replace" or a
12617c478bd9Sstevel@tonic-gate * "delete"; value = null implies a "delete".
12627c478bd9Sstevel@tonic-gate * If this is the first entry in the array then create it.
12637c478bd9Sstevel@tonic-gate *
12647c478bd9Sstevel@tonic-gate * Parameters:
12657c478bd9Sstevel@tonic-gate * Input: LDAPMod ***attrs - array to update
12667c478bd9Sstevel@tonic-gate * char *type - attribute to modify
12677c478bd9Sstevel@tonic-gate * char *value - attribute value, null implies "delete"
12687c478bd9Sstevel@tonic-gate * Output: None
12697c478bd9Sstevel@tonic-gate *
12707c478bd9Sstevel@tonic-gate * Returns: NSL_RESULT - NSL_OK = added okay
12717c478bd9Sstevel@tonic-gate *
12727c478bd9Sstevel@tonic-gate * *****************************************************************************
12737c478bd9Sstevel@tonic-gate */
12747c478bd9Sstevel@tonic-gate
12757c478bd9Sstevel@tonic-gate static NSL_RESULT
_modLDAPmodValue(LDAPMod *** attrs,char * type,char * value)12767c478bd9Sstevel@tonic-gate _modLDAPmodValue(LDAPMod ***attrs, char *type, char *value)
12777c478bd9Sstevel@tonic-gate
12787c478bd9Sstevel@tonic-gate {
12797c478bd9Sstevel@tonic-gate int i = 0;
12807c478bd9Sstevel@tonic-gate int j = 0;
12817c478bd9Sstevel@tonic-gate NSL_RESULT result = NSL_OK;
12827c478bd9Sstevel@tonic-gate
12837c478bd9Sstevel@tonic-gate /* ---------- */
12847c478bd9Sstevel@tonic-gate
12857c478bd9Sstevel@tonic-gate if ((attrs != NULL) && (type != NULL))
12867c478bd9Sstevel@tonic-gate {
12877c478bd9Sstevel@tonic-gate #ifdef DEBUG
12887c478bd9Sstevel@tonic-gate if (value != NULL)
12897c478bd9Sstevel@tonic-gate printf("_modLDAPmodValue() REPLACE type='%s', value='%s'\n", type, value);
12907c478bd9Sstevel@tonic-gate else
12917c478bd9Sstevel@tonic-gate printf("_modLDAPmodValue() DELETE type='%s'\n", type);
12927c478bd9Sstevel@tonic-gate #endif
12937c478bd9Sstevel@tonic-gate /* search the existing LDAPMod array for the attribute */
12947c478bd9Sstevel@tonic-gate
12957c478bd9Sstevel@tonic-gate for (i = 0; *attrs != NULL && (*attrs)[i] != NULL; i++)
12967c478bd9Sstevel@tonic-gate {
12977c478bd9Sstevel@tonic-gate if (strcasecmp((*attrs)[i]->mod_type, type) == 0)
12987c478bd9Sstevel@tonic-gate {
12997c478bd9Sstevel@tonic-gate break;
13007c478bd9Sstevel@tonic-gate }
13017c478bd9Sstevel@tonic-gate }
13027c478bd9Sstevel@tonic-gate
13037c478bd9Sstevel@tonic-gate if (*attrs == NULL)
13047c478bd9Sstevel@tonic-gate {
13057c478bd9Sstevel@tonic-gate /* array empty so create it */
13067c478bd9Sstevel@tonic-gate
13077c478bd9Sstevel@tonic-gate *attrs = (LDAPMod **)calloc(1, 2 * sizeof (LDAPMod *));
13087c478bd9Sstevel@tonic-gate if (*attrs != NULL)
13097c478bd9Sstevel@tonic-gate {
13107c478bd9Sstevel@tonic-gate i = 0;
13117c478bd9Sstevel@tonic-gate }
13127c478bd9Sstevel@tonic-gate else
13137c478bd9Sstevel@tonic-gate {
13147c478bd9Sstevel@tonic-gate result = NSL_ERR_MEMORY;
13157c478bd9Sstevel@tonic-gate }
13167c478bd9Sstevel@tonic-gate
13177c478bd9Sstevel@tonic-gate }
13187c478bd9Sstevel@tonic-gate else
13197c478bd9Sstevel@tonic-gate if ((*attrs)[i] == NULL)
13207c478bd9Sstevel@tonic-gate {
13217c478bd9Sstevel@tonic-gate /* attribute not found in array so add slot for it */
13227c478bd9Sstevel@tonic-gate
13237c478bd9Sstevel@tonic-gate *attrs = (LDAPMod **)
13247c478bd9Sstevel@tonic-gate realloc(*attrs, (i+2) * sizeof (LDAPMod *));
13257c478bd9Sstevel@tonic-gate if (*attrs == NULL)
13267c478bd9Sstevel@tonic-gate {
13277c478bd9Sstevel@tonic-gate result = NSL_ERR_MEMORY;
13287c478bd9Sstevel@tonic-gate }
13297c478bd9Sstevel@tonic-gate }
13307c478bd9Sstevel@tonic-gate }
13317c478bd9Sstevel@tonic-gate else
13327c478bd9Sstevel@tonic-gate {
13337c478bd9Sstevel@tonic-gate result = NSL_ERR_INTERNAL;
13347c478bd9Sstevel@tonic-gate }
13357c478bd9Sstevel@tonic-gate
13367c478bd9Sstevel@tonic-gate if (result == NSL_OK)
13377c478bd9Sstevel@tonic-gate {
13387c478bd9Sstevel@tonic-gate if ((*attrs)[i] == NULL)
13397c478bd9Sstevel@tonic-gate {
13407c478bd9Sstevel@tonic-gate /* We've got a new slot. Create the new mod entry */
13417c478bd9Sstevel@tonic-gate
13427c478bd9Sstevel@tonic-gate (*attrs)[i] = (LDAPMod *) malloc(sizeof (LDAPMod));
13437c478bd9Sstevel@tonic-gate if (((*attrs)[i] != NULL) && (value != NULL))
13447c478bd9Sstevel@tonic-gate {
13457c478bd9Sstevel@tonic-gate /* Do an attribute replace */
13467c478bd9Sstevel@tonic-gate
13477c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_op = LDAP_MOD_REPLACE;
13487c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_type = strdup(type);
13497c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_values = (char **)
13507c478bd9Sstevel@tonic-gate malloc(2 * sizeof (char *));
13517c478bd9Sstevel@tonic-gate if ((*attrs)[i]->mod_values != NULL)
13527c478bd9Sstevel@tonic-gate {
13537c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_values[0] =
13547c478bd9Sstevel@tonic-gate strdup(value);
13557c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_values[1] = NULL;
13567c478bd9Sstevel@tonic-gate (*attrs)[i+1] = NULL;
13577c478bd9Sstevel@tonic-gate }
13587c478bd9Sstevel@tonic-gate else
13597c478bd9Sstevel@tonic-gate {
13607c478bd9Sstevel@tonic-gate result = NSL_ERR_MEMORY;
13617c478bd9Sstevel@tonic-gate }
13627c478bd9Sstevel@tonic-gate }
13637c478bd9Sstevel@tonic-gate else
13647c478bd9Sstevel@tonic-gate if ((*attrs)[i] != NULL)
13657c478bd9Sstevel@tonic-gate {
13667c478bd9Sstevel@tonic-gate /* value is null so do an attribute delete */
13677c478bd9Sstevel@tonic-gate
13687c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_op = LDAP_MOD_DELETE;
13697c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_type = strdup(type);
13707c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_values = NULL;
13717c478bd9Sstevel@tonic-gate (*attrs)[i+1] = NULL;
13727c478bd9Sstevel@tonic-gate }
13737c478bd9Sstevel@tonic-gate else
13747c478bd9Sstevel@tonic-gate {
13757c478bd9Sstevel@tonic-gate result = NSL_ERR_MEMORY; /* malloc failed */
13767c478bd9Sstevel@tonic-gate }
13777c478bd9Sstevel@tonic-gate }
13787c478bd9Sstevel@tonic-gate
13797c478bd9Sstevel@tonic-gate else
13807c478bd9Sstevel@tonic-gate {
13817c478bd9Sstevel@tonic-gate /* Found an existing entry so add value to it */
13827c478bd9Sstevel@tonic-gate
13837c478bd9Sstevel@tonic-gate if (value != NULL)
13847c478bd9Sstevel@tonic-gate {
13857c478bd9Sstevel@tonic-gate /* add value to attribute's replace list */
13867c478bd9Sstevel@tonic-gate
13877c478bd9Sstevel@tonic-gate if ((*attrs)[i]->mod_op == LDAP_MOD_REPLACE)
13887c478bd9Sstevel@tonic-gate {
13897c478bd9Sstevel@tonic-gate for (j = 0;
13907c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_values[j] != NULL; j++);
13917c478bd9Sstevel@tonic-gate
13927c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_values =
13937c478bd9Sstevel@tonic-gate (char **)realloc((*attrs)[i]->mod_values,
13947c478bd9Sstevel@tonic-gate (j + 2) * sizeof (char *));
13957c478bd9Sstevel@tonic-gate if ((*attrs)[i]->mod_values != NULL)
13967c478bd9Sstevel@tonic-gate {
13977c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_values[j] =
13987c478bd9Sstevel@tonic-gate strdup(value);
13997c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_values[j+1] = NULL;
14007c478bd9Sstevel@tonic-gate }
14017c478bd9Sstevel@tonic-gate else
14027c478bd9Sstevel@tonic-gate {
14037c478bd9Sstevel@tonic-gate result = NSL_ERR_MEMORY;
14047c478bd9Sstevel@tonic-gate }
14057c478bd9Sstevel@tonic-gate }
14067c478bd9Sstevel@tonic-gate else
14077c478bd9Sstevel@tonic-gate {
14087c478bd9Sstevel@tonic-gate /* Delete and replace not allowed */
14097c478bd9Sstevel@tonic-gate result = NSL_ERR_MULTIOP;
14107c478bd9Sstevel@tonic-gate }
14117c478bd9Sstevel@tonic-gate }
14127c478bd9Sstevel@tonic-gate
14137c478bd9Sstevel@tonic-gate else
14147c478bd9Sstevel@tonic-gate {
14157c478bd9Sstevel@tonic-gate /*
14167c478bd9Sstevel@tonic-gate * attribute delete - so free any existing
14177c478bd9Sstevel@tonic-gate * entries in the value array
14187c478bd9Sstevel@tonic-gate */
14197c478bd9Sstevel@tonic-gate
14207c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_op = LDAP_MOD_DELETE;
14217c478bd9Sstevel@tonic-gate
14227c478bd9Sstevel@tonic-gate if ((*attrs)[i]->mod_values != NULL)
14237c478bd9Sstevel@tonic-gate {
14247c478bd9Sstevel@tonic-gate for (j = 0;
14257c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_values[j] != NULL;
14267c478bd9Sstevel@tonic-gate j++)
14277c478bd9Sstevel@tonic-gate {
14287c478bd9Sstevel@tonic-gate free((*attrs)[i]->mod_values[j]);
14297c478bd9Sstevel@tonic-gate }
14307c478bd9Sstevel@tonic-gate
14317c478bd9Sstevel@tonic-gate free((*attrs)[i]->mod_values);
14327c478bd9Sstevel@tonic-gate (*attrs)[i]->mod_values = NULL;
14337c478bd9Sstevel@tonic-gate }
14347c478bd9Sstevel@tonic-gate }
14357c478bd9Sstevel@tonic-gate }
14367c478bd9Sstevel@tonic-gate }
14377c478bd9Sstevel@tonic-gate
14387c478bd9Sstevel@tonic-gate return (result);
14397c478bd9Sstevel@tonic-gate } /* _modLDAPmodValue */
14407c478bd9Sstevel@tonic-gate
14417c478bd9Sstevel@tonic-gate
14427c478bd9Sstevel@tonic-gate
14437c478bd9Sstevel@tonic-gate
14447c478bd9Sstevel@tonic-gate
14457c478bd9Sstevel@tonic-gate /*
14467c478bd9Sstevel@tonic-gate * *****************************************************************************
14477c478bd9Sstevel@tonic-gate *
14487c478bd9Sstevel@tonic-gate * Function: _constructAddLDAPMod()
14497c478bd9Sstevel@tonic-gate *
14507c478bd9Sstevel@tonic-gate * Description: For the given attribute list construct an
14517c478bd9Sstevel@tonic-gate * LDAPMod array for the printer object to be added. Default
14527c478bd9Sstevel@tonic-gate * attribute values are included.
14537c478bd9Sstevel@tonic-gate *
14547c478bd9Sstevel@tonic-gate * Parameters:
14557c478bd9Sstevel@tonic-gate * Input:
14567c478bd9Sstevel@tonic-gate * uchar_t *printerName - Name of printer to be added
14577c478bd9Sstevel@tonic-gate * char **attrList - user specified attribute values list
14587c478bd9Sstevel@tonic-gate * Output: LDAPMod ***attrs - pointer to the constructed array
14597c478bd9Sstevel@tonic-gate *
14607c478bd9Sstevel@tonic-gate * Returns: NSL_RESULT - NSL_OK = constructed okay
14617c478bd9Sstevel@tonic-gate *
14627c478bd9Sstevel@tonic-gate * *****************************************************************************
14637c478bd9Sstevel@tonic-gate */
14647c478bd9Sstevel@tonic-gate
14657c478bd9Sstevel@tonic-gate static NSL_RESULT
_constructAddLDAPMod(uchar_t * printerName,char ** attrList,LDAPMod *** attrs)14667c478bd9Sstevel@tonic-gate _constructAddLDAPMod(uchar_t *printerName, char **attrList, LDAPMod ***attrs)
14677c478bd9Sstevel@tonic-gate
14687c478bd9Sstevel@tonic-gate {
14697c478bd9Sstevel@tonic-gate NSL_RESULT result = NSL_ERROR;
14707c478bd9Sstevel@tonic-gate int len = 0;
14717c478bd9Sstevel@tonic-gate char **p = NULL;
14727c478bd9Sstevel@tonic-gate char *value = NULL;
14737c478bd9Sstevel@tonic-gate char *attr = NULL;
14747c478bd9Sstevel@tonic-gate
14757c478bd9Sstevel@tonic-gate /* ---------- */
14767c478bd9Sstevel@tonic-gate
14777c478bd9Sstevel@tonic-gate if ((printerName != NULL) &&
14787c478bd9Sstevel@tonic-gate ((attrList != NULL) && (attrList[0] != NULL)) && (attrs != NULL))
14797c478bd9Sstevel@tonic-gate {
14807c478bd9Sstevel@tonic-gate *attrs = NULL;
14817c478bd9Sstevel@tonic-gate
14827c478bd9Sstevel@tonic-gate /*
14837c478bd9Sstevel@tonic-gate * setup printer object attribute values in an LDAPMod structure
14847c478bd9Sstevel@tonic-gate */
14857c478bd9Sstevel@tonic-gate result = _addLDAPmodValue(attrs, ATTR_OCLASS, OCV_TOP);
14867c478bd9Sstevel@tonic-gate if (result == NSL_OK)
14877c478bd9Sstevel@tonic-gate {
14887c478bd9Sstevel@tonic-gate /* Structural Objectclass */
14897c478bd9Sstevel@tonic-gate result =
14907c478bd9Sstevel@tonic-gate _addLDAPmodValue(attrs, ATTR_OCLASS, OCV_PSERVICE);
14917c478bd9Sstevel@tonic-gate }
14927c478bd9Sstevel@tonic-gate if (result == NSL_OK)
14937c478bd9Sstevel@tonic-gate {
14947c478bd9Sstevel@tonic-gate result = _addLDAPmodValue(attrs,
14957c478bd9Sstevel@tonic-gate ATTR_OCLASS, OCV_PABSTRACT);
14967c478bd9Sstevel@tonic-gate }
14977c478bd9Sstevel@tonic-gate if (result == NSL_OK)
14987c478bd9Sstevel@tonic-gate {
14997c478bd9Sstevel@tonic-gate result = _addLDAPmodValue(attrs,
15007c478bd9Sstevel@tonic-gate ATTR_OCLASS, OCV_SUNPRT);
15017c478bd9Sstevel@tonic-gate }
15027c478bd9Sstevel@tonic-gate if (result == NSL_OK)
15037c478bd9Sstevel@tonic-gate {
15047c478bd9Sstevel@tonic-gate result = _addLDAPmodValue(attrs,
15057c478bd9Sstevel@tonic-gate ATTR_PNAME, (char *)printerName);
15067c478bd9Sstevel@tonic-gate }
15077c478bd9Sstevel@tonic-gate
15087c478bd9Sstevel@tonic-gate /*
15097c478bd9Sstevel@tonic-gate * Now work through the user supplied attribute
15107c478bd9Sstevel@tonic-gate * values list and add them into the LDAPMod array
15117c478bd9Sstevel@tonic-gate */
15127c478bd9Sstevel@tonic-gate
15137c478bd9Sstevel@tonic-gate for (p = attrList;
15147c478bd9Sstevel@tonic-gate (p != NULL) && (*p != NULL) && (result == NSL_OK); p++)
15157c478bd9Sstevel@tonic-gate {
15167c478bd9Sstevel@tonic-gate /* get length of this key word */
15177c478bd9Sstevel@tonic-gate
15187c478bd9Sstevel@tonic-gate for (len = 0;
15197c478bd9Sstevel@tonic-gate ((*p)[len] != '=') && ((*p)[len] != '\0'); len++);
15207c478bd9Sstevel@tonic-gate
15217c478bd9Sstevel@tonic-gate if ((strlen(*p) > len+1))
15227c478bd9Sstevel@tonic-gate {
15237c478bd9Sstevel@tonic-gate attr = strdup(*p);
15247c478bd9Sstevel@tonic-gate attr[len] = '\0';
15257c478bd9Sstevel@tonic-gate value = strdup(&attr[len+1]);
15267c478bd9Sstevel@tonic-gate
15277c478bd9Sstevel@tonic-gate /* handle specific Key Value Pairs (KVP) */
15287c478bd9Sstevel@tonic-gate
15297c478bd9Sstevel@tonic-gate if (strcasecmp(attr, NS_KEY_BSDADDR) == 0)
15307c478bd9Sstevel@tonic-gate {
15317c478bd9Sstevel@tonic-gate /* use LDAP attribute name */
15327c478bd9Sstevel@tonic-gate free(attr);
15337c478bd9Sstevel@tonic-gate attr = strdup(ATTR_BSDADDR);
15347c478bd9Sstevel@tonic-gate }
15357c478bd9Sstevel@tonic-gate else
15367c478bd9Sstevel@tonic-gate if (_attrInLDAPList(attr) == 0)
15377c478bd9Sstevel@tonic-gate {
15387c478bd9Sstevel@tonic-gate /*
15397c478bd9Sstevel@tonic-gate * Non-LDAP attribute so use LDAP
15407c478bd9Sstevel@tonic-gate * KVP attribute and the given KVP
15417c478bd9Sstevel@tonic-gate * as the value, ie.
15427c478bd9Sstevel@tonic-gate * sun-printer-kvp=description=printer
15437c478bd9Sstevel@tonic-gate */
15447c478bd9Sstevel@tonic-gate free(attr);
15457c478bd9Sstevel@tonic-gate attr = strdup(ATTR_KVP);
15467c478bd9Sstevel@tonic-gate value = strdup(*p);
15477c478bd9Sstevel@tonic-gate }
15487c478bd9Sstevel@tonic-gate
15497c478bd9Sstevel@tonic-gate /* add it into the LDAPMod array */
15507c478bd9Sstevel@tonic-gate
15517c478bd9Sstevel@tonic-gate result = _addLDAPmodValue(attrs, attr, value);
15527c478bd9Sstevel@tonic-gate
15537c478bd9Sstevel@tonic-gate free(attr);
15547c478bd9Sstevel@tonic-gate free(value);
15557c478bd9Sstevel@tonic-gate }
15567c478bd9Sstevel@tonic-gate } /* for */
15577c478bd9Sstevel@tonic-gate
15587c478bd9Sstevel@tonic-gate if ((result != NSL_OK) && (*attrs != NULL))
15597c478bd9Sstevel@tonic-gate {
15607c478bd9Sstevel@tonic-gate (void) ldap_mods_free(*attrs, 1);
15617c478bd9Sstevel@tonic-gate attrs = NULL;
15627c478bd9Sstevel@tonic-gate }
15637c478bd9Sstevel@tonic-gate }
15647c478bd9Sstevel@tonic-gate else
15657c478bd9Sstevel@tonic-gate {
15667c478bd9Sstevel@tonic-gate result = NSL_ERR_INTERNAL;
15677c478bd9Sstevel@tonic-gate }
15687c478bd9Sstevel@tonic-gate
15697c478bd9Sstevel@tonic-gate return (result);
15707c478bd9Sstevel@tonic-gate } /* _constructAddLDAPMod */
15717c478bd9Sstevel@tonic-gate
15727c478bd9Sstevel@tonic-gate
15737c478bd9Sstevel@tonic-gate
15747c478bd9Sstevel@tonic-gate
15757c478bd9Sstevel@tonic-gate
15767c478bd9Sstevel@tonic-gate
15777c478bd9Sstevel@tonic-gate
15787c478bd9Sstevel@tonic-gate /*
15797c478bd9Sstevel@tonic-gate * *****************************************************************************
15807c478bd9Sstevel@tonic-gate *
15817c478bd9Sstevel@tonic-gate * Function: _constructModLDAPMod()
15827c478bd9Sstevel@tonic-gate *
15837c478bd9Sstevel@tonic-gate * Description: For the given modify attribute list, construct an
15847c478bd9Sstevel@tonic-gate * LDAPMod array for the printer object to be modified
15857c478bd9Sstevel@tonic-gate *
15867c478bd9Sstevel@tonic-gate * Parameters:
15877c478bd9Sstevel@tonic-gate * Input: uchar_t *printerName - name of printer to be modified
15887c478bd9Sstevel@tonic-gate * int sunPrinter - Boolean; object is a sunPrinter
15897c478bd9Sstevel@tonic-gate * char **attrList - user specified attribute values list
15907c478bd9Sstevel@tonic-gate * char ***oldKVPList - current list of KVP values on object
15917c478bd9Sstevel@tonic-gate * Output: LDAPMod ***attrs - pointer to the constructed array
15927c478bd9Sstevel@tonic-gate *
15937c478bd9Sstevel@tonic-gate * Returns: NSL_RESULT - NSL_OK = constructed okay
15947c478bd9Sstevel@tonic-gate *
15957c478bd9Sstevel@tonic-gate * *****************************************************************************
15967c478bd9Sstevel@tonic-gate */
15977c478bd9Sstevel@tonic-gate
15987c478bd9Sstevel@tonic-gate static NSL_RESULT
_constructModLDAPMod(uchar_t * printerName,int sunPrinter,char ** attrList,char *** oldKVPList,LDAPMod *** attrs)15997c478bd9Sstevel@tonic-gate _constructModLDAPMod(uchar_t *printerName, int sunPrinter, char **attrList,
16007c478bd9Sstevel@tonic-gate char ***oldKVPList, LDAPMod ***attrs)
16017c478bd9Sstevel@tonic-gate
16027c478bd9Sstevel@tonic-gate {
16037c478bd9Sstevel@tonic-gate NSL_RESULT result = NSL_OK;
16047c478bd9Sstevel@tonic-gate int len = 0;
16057c478bd9Sstevel@tonic-gate int kvpUpdated = 0;
16067c478bd9Sstevel@tonic-gate int kvpExists = 0;
16077c478bd9Sstevel@tonic-gate char **p = NULL;
16087c478bd9Sstevel@tonic-gate char *value = NULL;
16097c478bd9Sstevel@tonic-gate char *attr = NULL;
16107c478bd9Sstevel@tonic-gate
16117c478bd9Sstevel@tonic-gate /* ---------- */
16127c478bd9Sstevel@tonic-gate
16137c478bd9Sstevel@tonic-gate if ((printerName != NULL) &&
16147c478bd9Sstevel@tonic-gate ((attrList != NULL) && (attrList[0] != NULL)) && (attrs != NULL))
16157c478bd9Sstevel@tonic-gate {
16167c478bd9Sstevel@tonic-gate *attrs = NULL;
16177c478bd9Sstevel@tonic-gate
16187c478bd9Sstevel@tonic-gate if ((oldKVPList != NULL) && (*oldKVPList != NULL))
16197c478bd9Sstevel@tonic-gate {
16207c478bd9Sstevel@tonic-gate kvpExists = 1;
16217c478bd9Sstevel@tonic-gate }
16227c478bd9Sstevel@tonic-gate
16237c478bd9Sstevel@tonic-gate if (!sunPrinter)
16247c478bd9Sstevel@tonic-gate {
16257c478bd9Sstevel@tonic-gate /*
16267c478bd9Sstevel@tonic-gate * The object was previously not a sunPrinter, so
16277c478bd9Sstevel@tonic-gate * add the required objectclass attribute value, and
16287c478bd9Sstevel@tonic-gate * ensure it has the printername attribute.
16297c478bd9Sstevel@tonic-gate */
16307c478bd9Sstevel@tonic-gate result = _addLDAPmodValue(attrs,
16317c478bd9Sstevel@tonic-gate ATTR_OCLASS, OCV_SUNPRT);
16327c478bd9Sstevel@tonic-gate if (result == NSL_OK)
16337c478bd9Sstevel@tonic-gate {
16347c478bd9Sstevel@tonic-gate result = _modLDAPmodValue(attrs,
16357c478bd9Sstevel@tonic-gate ATTR_PNAME, (char *)printerName);
16367c478bd9Sstevel@tonic-gate }
16377c478bd9Sstevel@tonic-gate }
16387c478bd9Sstevel@tonic-gate
16397c478bd9Sstevel@tonic-gate /*
16407c478bd9Sstevel@tonic-gate * work through the user supplied attribute
16417c478bd9Sstevel@tonic-gate * values list and add them into the LDAPMod array depending
16427c478bd9Sstevel@tonic-gate * on if they are a replace or delete attribute operation,
16437c478bd9Sstevel@tonic-gate * a "null value" means delete.
16447c478bd9Sstevel@tonic-gate */
16457c478bd9Sstevel@tonic-gate
16467c478bd9Sstevel@tonic-gate for (p = attrList;
16477c478bd9Sstevel@tonic-gate (p != NULL) && (*p != NULL) && (result == NSL_OK); p++)
16487c478bd9Sstevel@tonic-gate {
16497c478bd9Sstevel@tonic-gate /* get length of this key word */
16507c478bd9Sstevel@tonic-gate
16517c478bd9Sstevel@tonic-gate for (len = 0;
16527c478bd9Sstevel@tonic-gate ((*p)[len] != '=') && ((*p)[len] != '\0'); len++);
16537c478bd9Sstevel@tonic-gate
16547c478bd9Sstevel@tonic-gate if ((strlen(*p) > len+1))
16557c478bd9Sstevel@tonic-gate {
16567c478bd9Sstevel@tonic-gate attr = strdup(*p);
16577c478bd9Sstevel@tonic-gate attr[len] = '\0';
16587c478bd9Sstevel@tonic-gate value = strdup(&attr[len+1]);
16597c478bd9Sstevel@tonic-gate
16607c478bd9Sstevel@tonic-gate /* handle specific Key Value Pairs (KVP) */
16617c478bd9Sstevel@tonic-gate
16627c478bd9Sstevel@tonic-gate if ((_attrInLDAPList(attr) == 0) &&
16637c478bd9Sstevel@tonic-gate (strcasecmp(attr, NS_KEY_BSDADDR) != 0))
16647c478bd9Sstevel@tonic-gate {
16657c478bd9Sstevel@tonic-gate /*
16667c478bd9Sstevel@tonic-gate * Non-LDAP attribute so use LDAP
16677c478bd9Sstevel@tonic-gate * KVP attribute and the given KVP as
16687c478bd9Sstevel@tonic-gate * the value, ie.
16697c478bd9Sstevel@tonic-gate * sun-printer-kvp=description=printer
16707c478bd9Sstevel@tonic-gate */
16717c478bd9Sstevel@tonic-gate result = _modAttrKVP(*p, oldKVPList);
16727c478bd9Sstevel@tonic-gate kvpUpdated = 1;
16737c478bd9Sstevel@tonic-gate }
16747c478bd9Sstevel@tonic-gate
16757c478bd9Sstevel@tonic-gate else
16767c478bd9Sstevel@tonic-gate {
16777c478bd9Sstevel@tonic-gate if (strcasecmp(attr, NS_KEY_BSDADDR) ==
16787c478bd9Sstevel@tonic-gate 0)
16797c478bd9Sstevel@tonic-gate {
16807c478bd9Sstevel@tonic-gate /*
16817c478bd9Sstevel@tonic-gate * use LDAP bsdaddr attribute
16827c478bd9Sstevel@tonic-gate * name
16837c478bd9Sstevel@tonic-gate */
16847c478bd9Sstevel@tonic-gate free(attr);
16857c478bd9Sstevel@tonic-gate attr = strdup(ATTR_BSDADDR);
16867c478bd9Sstevel@tonic-gate }
16877c478bd9Sstevel@tonic-gate
16887c478bd9Sstevel@tonic-gate /*
16897c478bd9Sstevel@tonic-gate * else
16907c478bd9Sstevel@tonic-gate * use the supplied attribute name
16917c478bd9Sstevel@tonic-gate */
16927c478bd9Sstevel@tonic-gate
16937c478bd9Sstevel@tonic-gate /* add it into the LDAPMod array */
16947c478bd9Sstevel@tonic-gate
16957c478bd9Sstevel@tonic-gate result = _modLDAPmodValue(attrs,
16967c478bd9Sstevel@tonic-gate attr, value);
16977c478bd9Sstevel@tonic-gate }
16987c478bd9Sstevel@tonic-gate
16997c478bd9Sstevel@tonic-gate free(attr);
17007c478bd9Sstevel@tonic-gate free(value);
17017c478bd9Sstevel@tonic-gate }
17027c478bd9Sstevel@tonic-gate
17037c478bd9Sstevel@tonic-gate else
17047c478bd9Sstevel@tonic-gate if (strlen(*p) >= 1)
17057c478bd9Sstevel@tonic-gate {
17067c478bd9Sstevel@tonic-gate /* handle attribute DELETE request */
17077c478bd9Sstevel@tonic-gate
17087c478bd9Sstevel@tonic-gate attr = strdup(*p);
17097c478bd9Sstevel@tonic-gate if (attr[len] == '=')
17107c478bd9Sstevel@tonic-gate {
17117c478bd9Sstevel@tonic-gate /* terminate "attribute=" */
17127c478bd9Sstevel@tonic-gate attr[len] = '\0';
17137c478bd9Sstevel@tonic-gate }
17147c478bd9Sstevel@tonic-gate
17157c478bd9Sstevel@tonic-gate /* handle specific Key Value Pairs (KVP) */
17167c478bd9Sstevel@tonic-gate
17177c478bd9Sstevel@tonic-gate if (strcasecmp(attr, NS_KEY_BSDADDR) == 0)
17187c478bd9Sstevel@tonic-gate {
17197c478bd9Sstevel@tonic-gate /* use LDAP bsdaddr attribute name */
17207c478bd9Sstevel@tonic-gate result = _modLDAPmodValue(attrs,
17217c478bd9Sstevel@tonic-gate ATTR_BSDADDR, NULL);
17227c478bd9Sstevel@tonic-gate }
17237c478bd9Sstevel@tonic-gate else
17247c478bd9Sstevel@tonic-gate if (_attrInLDAPList(attr) == 0)
17257c478bd9Sstevel@tonic-gate {
17267c478bd9Sstevel@tonic-gate /*
17277c478bd9Sstevel@tonic-gate * Non-LDAP kvp, so sort items
17287c478bd9Sstevel@tonic-gate * in the kvp list
17297c478bd9Sstevel@tonic-gate */
17307c478bd9Sstevel@tonic-gate result = _modAttrKVP(*p, oldKVPList);
17317c478bd9Sstevel@tonic-gate kvpUpdated = 1;
17327c478bd9Sstevel@tonic-gate }
17337c478bd9Sstevel@tonic-gate else
17347c478bd9Sstevel@tonic-gate {
17357c478bd9Sstevel@tonic-gate result = _modLDAPmodValue(attrs,
17367c478bd9Sstevel@tonic-gate attr, NULL);
17377c478bd9Sstevel@tonic-gate }
17387c478bd9Sstevel@tonic-gate
17397c478bd9Sstevel@tonic-gate free(attr);
17407c478bd9Sstevel@tonic-gate }
17417c478bd9Sstevel@tonic-gate } /* for */
17427c478bd9Sstevel@tonic-gate
17437c478bd9Sstevel@tonic-gate if ((result == NSL_OK) && (kvpUpdated))
17447c478bd9Sstevel@tonic-gate {
17457c478bd9Sstevel@tonic-gate result = _attrAddKVP(attrs, *oldKVPList, kvpExists);
17467c478bd9Sstevel@tonic-gate }
17477c478bd9Sstevel@tonic-gate
17487c478bd9Sstevel@tonic-gate if ((result != NSL_OK) && (*attrs != NULL))
17497c478bd9Sstevel@tonic-gate {
17507c478bd9Sstevel@tonic-gate (void) ldap_mods_free(*attrs, 1);
17517c478bd9Sstevel@tonic-gate *attrs = NULL;
17527c478bd9Sstevel@tonic-gate }
17537c478bd9Sstevel@tonic-gate }
17547c478bd9Sstevel@tonic-gate else
17557c478bd9Sstevel@tonic-gate {
17567c478bd9Sstevel@tonic-gate result = NSL_ERR_INTERNAL;
17577c478bd9Sstevel@tonic-gate }
17587c478bd9Sstevel@tonic-gate
17597c478bd9Sstevel@tonic-gate return (result);
17607c478bd9Sstevel@tonic-gate } /* _constructModLDAPMod */
17617c478bd9Sstevel@tonic-gate
17627c478bd9Sstevel@tonic-gate
17637c478bd9Sstevel@tonic-gate
17647c478bd9Sstevel@tonic-gate
17657c478bd9Sstevel@tonic-gate
17667c478bd9Sstevel@tonic-gate
17677c478bd9Sstevel@tonic-gate /*
17687c478bd9Sstevel@tonic-gate * *****************************************************************************
17697c478bd9Sstevel@tonic-gate *
17707c478bd9Sstevel@tonic-gate * Function: _compareURIinDNs()
17717c478bd9Sstevel@tonic-gate *
17727c478bd9Sstevel@tonic-gate * Description: For the 2 given printer object DNs compare the naming part
17737c478bd9Sstevel@tonic-gate * part of the DN (printer-uri) to see if they are the same.
17747c478bd9Sstevel@tonic-gate *
17757c478bd9Sstevel@tonic-gate * Note: This function only returns "compare failed" if their URI don't
17767c478bd9Sstevel@tonic-gate * compare. Problems with the dn etc., return a good compare
17777c478bd9Sstevel@tonic-gate * because I don't want us to create a new object for these
17787c478bd9Sstevel@tonic-gate *
17797c478bd9Sstevel@tonic-gate * Parameters:
17807c478bd9Sstevel@tonic-gate * Input: uchar_t *dn1
17817c478bd9Sstevel@tonic-gate * uchar_t *dn2
17827c478bd9Sstevel@tonic-gate * Output: None
17837c478bd9Sstevel@tonic-gate *
17847c478bd9Sstevel@tonic-gate * Returns: NSL_RESULT - NSL_OK = URIs are the same
17857c478bd9Sstevel@tonic-gate *
17867c478bd9Sstevel@tonic-gate * *****************************************************************************
17877c478bd9Sstevel@tonic-gate */
17887c478bd9Sstevel@tonic-gate
17897c478bd9Sstevel@tonic-gate static NSL_RESULT
_compareURIinDNs(uchar_t * dn1,uchar_t * dn2)17907c478bd9Sstevel@tonic-gate _compareURIinDNs(uchar_t *dn1, uchar_t *dn2)
17917c478bd9Sstevel@tonic-gate
17927c478bd9Sstevel@tonic-gate {
17937c478bd9Sstevel@tonic-gate NSL_RESULT result = NSL_OK;
17947c478bd9Sstevel@tonic-gate uchar_t *DN1 = NULL;
17957c478bd9Sstevel@tonic-gate uchar_t *DN2 = NULL;
17967c478bd9Sstevel@tonic-gate char *p1 = NULL;
17977c478bd9Sstevel@tonic-gate char *p2 = NULL;
17987c478bd9Sstevel@tonic-gate
17997c478bd9Sstevel@tonic-gate /* --------- */
18007c478bd9Sstevel@tonic-gate
18017c478bd9Sstevel@tonic-gate if ((dn1 != NULL) && (dn2 != NULL))
18027c478bd9Sstevel@tonic-gate {
18037c478bd9Sstevel@tonic-gate DN1 = (uchar_t *)strdup((char *)dn1);
18047c478bd9Sstevel@tonic-gate DN2 = (uchar_t *)strdup((char *)dn2);
18057c478bd9Sstevel@tonic-gate
18067c478bd9Sstevel@tonic-gate /* terminate each string after the printer-uri */
18077c478bd9Sstevel@tonic-gate
18087c478bd9Sstevel@tonic-gate p1 = strstr((char *)DN1, PCONTAINER);
18097c478bd9Sstevel@tonic-gate /* move back to the comma */
18107c478bd9Sstevel@tonic-gate while ((p1 != NULL) && (*p1 != ',') && (p1 >= (char *)DN1))
18117c478bd9Sstevel@tonic-gate {
18127c478bd9Sstevel@tonic-gate p1--;
18137c478bd9Sstevel@tonic-gate }
18147c478bd9Sstevel@tonic-gate
18157c478bd9Sstevel@tonic-gate p2 = strstr((char *)DN2, PCONTAINER);
18167c478bd9Sstevel@tonic-gate /* move back to the comma */
18177c478bd9Sstevel@tonic-gate while ((p2 != NULL) && (*p2 != ',') && (p2 >= (char *)DN2))
18187c478bd9Sstevel@tonic-gate {
18197c478bd9Sstevel@tonic-gate p2--;
18207c478bd9Sstevel@tonic-gate }
18217c478bd9Sstevel@tonic-gate
18227c478bd9Sstevel@tonic-gate if ((*p1 == ',') && (*p2 == ','))
18237c478bd9Sstevel@tonic-gate {
18247c478bd9Sstevel@tonic-gate *p1 = '\0'; /* re-terminate it */
18257c478bd9Sstevel@tonic-gate *p2 = '\0'; /* re-terminate it */
18267c478bd9Sstevel@tonic-gate
18277c478bd9Sstevel@tonic-gate /* do the compare */
18287c478bd9Sstevel@tonic-gate
18297c478bd9Sstevel@tonic-gate /*
18307c478bd9Sstevel@tonic-gate * Note: SHOULD really normalise the 2 DNs before
18317c478bd9Sstevel@tonic-gate * doing the compare
18327c478bd9Sstevel@tonic-gate */
18337c478bd9Sstevel@tonic-gate #ifdef DEBUG
18347c478bd9Sstevel@tonic-gate printf("_compareURIinDNs() @1 (%s) (%s)\n", DN1, DN2);
18357c478bd9Sstevel@tonic-gate #endif
18367c478bd9Sstevel@tonic-gate if (strcasecmp((char *)DN1, (char *)DN2) != 0)
18377c478bd9Sstevel@tonic-gate {
18387c478bd9Sstevel@tonic-gate result = NSL_ERROR;
18397c478bd9Sstevel@tonic-gate }
18407c478bd9Sstevel@tonic-gate
18417c478bd9Sstevel@tonic-gate }
18427c478bd9Sstevel@tonic-gate
18437c478bd9Sstevel@tonic-gate free(DN1);
18447c478bd9Sstevel@tonic-gate free(DN2);
18457c478bd9Sstevel@tonic-gate }
18467c478bd9Sstevel@tonic-gate
18477c478bd9Sstevel@tonic-gate return (result);
18487c478bd9Sstevel@tonic-gate } /* _compareURIinDNs */
18497c478bd9Sstevel@tonic-gate
18507c478bd9Sstevel@tonic-gate
18517c478bd9Sstevel@tonic-gate
18527c478bd9Sstevel@tonic-gate
18537c478bd9Sstevel@tonic-gate
18547c478bd9Sstevel@tonic-gate
18557c478bd9Sstevel@tonic-gate
18567c478bd9Sstevel@tonic-gate /*
18577c478bd9Sstevel@tonic-gate * *****************************************************************************
18587c478bd9Sstevel@tonic-gate *
18597c478bd9Sstevel@tonic-gate * Function: _getThisNSDomainDN()
18607c478bd9Sstevel@tonic-gate *
18617c478bd9Sstevel@tonic-gate * Description: Get the current Name Service Domain DN
18627c478bd9Sstevel@tonic-gate * This is extracted from the result of executing ldaplist.
18637c478bd9Sstevel@tonic-gate *
18647c478bd9Sstevel@tonic-gate * Note: Do it this way until the NS LDAP library interface is
18657c478bd9Sstevel@tonic-gate * made public.
18667c478bd9Sstevel@tonic-gate *
18677c478bd9Sstevel@tonic-gate * Parameters:
18687c478bd9Sstevel@tonic-gate * Input: None
18697c478bd9Sstevel@tonic-gate * Output: None
18707c478bd9Sstevel@tonic-gate *
18717c478bd9Sstevel@tonic-gate * Returns: uchar_t* - pointer to NS Domain DN (The caller should free this
18727c478bd9Sstevel@tonic-gate * returned memory).
18737c478bd9Sstevel@tonic-gate *
18747c478bd9Sstevel@tonic-gate * *****************************************************************************
18757c478bd9Sstevel@tonic-gate */
18767c478bd9Sstevel@tonic-gate
18777c478bd9Sstevel@tonic-gate #define LDAPLIST_D "/usr/bin/ldaplist -d 2>&1"
18787c478bd9Sstevel@tonic-gate #define DNID "dn: "
18797c478bd9Sstevel@tonic-gate
18807c478bd9Sstevel@tonic-gate static uchar_t *
_getThisNSDomainDN(void)18817c478bd9Sstevel@tonic-gate _getThisNSDomainDN(void)
18827c478bd9Sstevel@tonic-gate
18837c478bd9Sstevel@tonic-gate {
18847c478bd9Sstevel@tonic-gate uchar_t *domainDN = NULL;
18857c478bd9Sstevel@tonic-gate char *cp = NULL;
18867c478bd9Sstevel@tonic-gate char buf[BUFSIZ] = "";
18877c478bd9Sstevel@tonic-gate
18887c478bd9Sstevel@tonic-gate /* --------- */
18897c478bd9Sstevel@tonic-gate
18907c478bd9Sstevel@tonic-gate if (_popen(LDAPLIST_D, buf, sizeof (buf)) == 0)
18917c478bd9Sstevel@tonic-gate {
18927c478bd9Sstevel@tonic-gate if ((cp = strstr(buf, DNID)) != NULL)
18937c478bd9Sstevel@tonic-gate {
18947c478bd9Sstevel@tonic-gate cp += strlen(DNID); /* increment past "dn: " label */
18957c478bd9Sstevel@tonic-gate domainDN = (uchar_t *)strdup(cp);
18967c478bd9Sstevel@tonic-gate
18977c478bd9Sstevel@tonic-gate if ((cp = strchr((char *)domainDN, '\n')) != NULL)
18987c478bd9Sstevel@tonic-gate {
18997c478bd9Sstevel@tonic-gate *cp = '\0'; /* terminate it */
19007c478bd9Sstevel@tonic-gate }
19017c478bd9Sstevel@tonic-gate }
19027c478bd9Sstevel@tonic-gate }
19037c478bd9Sstevel@tonic-gate
19047c478bd9Sstevel@tonic-gate return (domainDN);
19057c478bd9Sstevel@tonic-gate } /* _getThisNSDomainDN */
19067c478bd9Sstevel@tonic-gate
19077c478bd9Sstevel@tonic-gate
19087c478bd9Sstevel@tonic-gate
19097c478bd9Sstevel@tonic-gate
19107c478bd9Sstevel@tonic-gate
19117c478bd9Sstevel@tonic-gate /*
19127c478bd9Sstevel@tonic-gate * *****************************************************************************
19137c478bd9Sstevel@tonic-gate *
19147c478bd9Sstevel@tonic-gate * Function: _popen()
19157c478bd9Sstevel@tonic-gate *
19167c478bd9Sstevel@tonic-gate * Description: General popen function. The caller should always use a full
19177c478bd9Sstevel@tonic-gate * path cmd.
19187c478bd9Sstevel@tonic-gate *
19197c478bd9Sstevel@tonic-gate * Parameters:
19207c478bd9Sstevel@tonic-gate * Input: char *cmd - command line to execute
19217c478bd9Sstevel@tonic-gate * char *buffer - ptr to buffer to put result in
19227c478bd9Sstevel@tonic-gate * int size - size of result buffer
19237c478bd9Sstevel@tonic-gate * Output: None
19247c478bd9Sstevel@tonic-gate *
19257c478bd9Sstevel@tonic-gate * Returns: int - 0 = opened okay
19267c478bd9Sstevel@tonic-gate *
19277c478bd9Sstevel@tonic-gate * *****************************************************************************
19287c478bd9Sstevel@tonic-gate */
19297c478bd9Sstevel@tonic-gate
19307c478bd9Sstevel@tonic-gate static int
_popen(char * cmd,char * buffer,int size)19317c478bd9Sstevel@tonic-gate _popen(char *cmd, char *buffer, int size)
19327c478bd9Sstevel@tonic-gate
19337c478bd9Sstevel@tonic-gate {
19347c478bd9Sstevel@tonic-gate int result = -1;
19357c478bd9Sstevel@tonic-gate int rsize = 0;
19367c478bd9Sstevel@tonic-gate FILE *fptr;
19377c478bd9Sstevel@tonic-gate char safe_cmd[BUFSIZ];
19387c478bd9Sstevel@tonic-gate char linebuf[BUFSIZ];
19397c478bd9Sstevel@tonic-gate
19407c478bd9Sstevel@tonic-gate /* -------- */
19417c478bd9Sstevel@tonic-gate
19427c478bd9Sstevel@tonic-gate if ((cmd != NULL) && (buffer != NULL) && (size != 0))
19437c478bd9Sstevel@tonic-gate {
19447c478bd9Sstevel@tonic-gate (void) strcpy(buffer, "");
19457c478bd9Sstevel@tonic-gate (void) strcpy(linebuf, "");
19467c478bd9Sstevel@tonic-gate (void) snprintf(safe_cmd, BUFSIZ, "IFS=' \t'; %s", cmd);
19477c478bd9Sstevel@tonic-gate
19487c478bd9Sstevel@tonic-gate if ((fptr = popen(safe_cmd, "r")) != NULL)
19497c478bd9Sstevel@tonic-gate {
19507c478bd9Sstevel@tonic-gate while ((fgets(linebuf, BUFSIZ, fptr) != NULL) &&
19517c478bd9Sstevel@tonic-gate (rsize < size))
19527c478bd9Sstevel@tonic-gate {
19537c478bd9Sstevel@tonic-gate rsize = strlcat(buffer, linebuf, size);
19547c478bd9Sstevel@tonic-gate if (rsize >= size)
19557c478bd9Sstevel@tonic-gate {
19567c478bd9Sstevel@tonic-gate /* result is too long */
19577c478bd9Sstevel@tonic-gate (void) memset(buffer, '\0', size);
19587c478bd9Sstevel@tonic-gate }
19597c478bd9Sstevel@tonic-gate }
19607c478bd9Sstevel@tonic-gate
19617c478bd9Sstevel@tonic-gate if (strlen(buffer) > 0)
19627c478bd9Sstevel@tonic-gate {
19637c478bd9Sstevel@tonic-gate result = 0;
19647c478bd9Sstevel@tonic-gate }
19657c478bd9Sstevel@tonic-gate
19667c478bd9Sstevel@tonic-gate (void) pclose(fptr);
19677c478bd9Sstevel@tonic-gate }
19687c478bd9Sstevel@tonic-gate }
19697c478bd9Sstevel@tonic-gate
19707c478bd9Sstevel@tonic-gate return (result);
19717c478bd9Sstevel@tonic-gate } /* popen */
19727c478bd9Sstevel@tonic-gate
19737c478bd9Sstevel@tonic-gate
19747c478bd9Sstevel@tonic-gate /*
19757c478bd9Sstevel@tonic-gate * *****************************************************************************
19767c478bd9Sstevel@tonic-gate *
19777c478bd9Sstevel@tonic-gate * Function: _attrInList()
19787c478bd9Sstevel@tonic-gate *
19797c478bd9Sstevel@tonic-gate * Description: For the given list check if the attribute is it
19807c478bd9Sstevel@tonic-gate *
19817c478bd9Sstevel@tonic-gate * Parameters:
19827c478bd9Sstevel@tonic-gate * Input: char *attr - attribute to check
19837c478bd9Sstevel@tonic-gate * char **list - list of attributes to check against
19847c478bd9Sstevel@tonic-gate * Output: None
19857c478bd9Sstevel@tonic-gate *
19867c478bd9Sstevel@tonic-gate * Returns: int - TRUE = attr found in list
19877c478bd9Sstevel@tonic-gate *
19887c478bd9Sstevel@tonic-gate * *****************************************************************************
19897c478bd9Sstevel@tonic-gate */
19907c478bd9Sstevel@tonic-gate
19917c478bd9Sstevel@tonic-gate static int
_attrInList(char * attr,const char ** list)19927c478bd9Sstevel@tonic-gate _attrInList(char *attr, const char **list)
19937c478bd9Sstevel@tonic-gate
19947c478bd9Sstevel@tonic-gate {
19957c478bd9Sstevel@tonic-gate int result = 0;
19967c478bd9Sstevel@tonic-gate int j;
19977c478bd9Sstevel@tonic-gate
19987c478bd9Sstevel@tonic-gate /* ------- */
19997c478bd9Sstevel@tonic-gate
20007c478bd9Sstevel@tonic-gate if ((attr != NULL) && (list != NULL))
20017c478bd9Sstevel@tonic-gate {
20027c478bd9Sstevel@tonic-gate for (j = 0; (list[j] != NULL) && (result != 1); j++)
20037c478bd9Sstevel@tonic-gate {
20047c478bd9Sstevel@tonic-gate if (strcasecmp(list[j], attr) == 0)
20057c478bd9Sstevel@tonic-gate {
20067c478bd9Sstevel@tonic-gate result = 1; /* found */
20077c478bd9Sstevel@tonic-gate }
20087c478bd9Sstevel@tonic-gate }
20097c478bd9Sstevel@tonic-gate }
20107c478bd9Sstevel@tonic-gate
20117c478bd9Sstevel@tonic-gate return (result);
20127c478bd9Sstevel@tonic-gate } /* _attrInList */
20137c478bd9Sstevel@tonic-gate
20147c478bd9Sstevel@tonic-gate
20157c478bd9Sstevel@tonic-gate
20167c478bd9Sstevel@tonic-gate
20177c478bd9Sstevel@tonic-gate /*
20187c478bd9Sstevel@tonic-gate * *****************************************************************************
20197c478bd9Sstevel@tonic-gate *
20207c478bd9Sstevel@tonic-gate * Function: _attrInLDAPList()
20217c478bd9Sstevel@tonic-gate *
20227c478bd9Sstevel@tonic-gate * Description: Checks to see if the given attribute is an LDAP printing
20237c478bd9Sstevel@tonic-gate * attribute, ie. is either in an IPP objectclass or the
20247c478bd9Sstevel@tonic-gate * sun printer objectclass. Note: some attributes are handled
20257c478bd9Sstevel@tonic-gate * specifically outside this function, so are excluded from
20267c478bd9Sstevel@tonic-gate * the lists that are checked.
20277c478bd9Sstevel@tonic-gate *
20287c478bd9Sstevel@tonic-gate * Parameters:
20297c478bd9Sstevel@tonic-gate * Input: char *attr - attribute to check
20307c478bd9Sstevel@tonic-gate * Output: None
20317c478bd9Sstevel@tonic-gate *
20327c478bd9Sstevel@tonic-gate * Returns: int - TRUE = attr found in list
20337c478bd9Sstevel@tonic-gate *
20347c478bd9Sstevel@tonic-gate * *****************************************************************************
20357c478bd9Sstevel@tonic-gate */
20367c478bd9Sstevel@tonic-gate
20377c478bd9Sstevel@tonic-gate static int
_attrInLDAPList(char * attr)20387c478bd9Sstevel@tonic-gate _attrInLDAPList(char *attr)
20397c478bd9Sstevel@tonic-gate
20407c478bd9Sstevel@tonic-gate {
20417c478bd9Sstevel@tonic-gate int result = 0;
20427c478bd9Sstevel@tonic-gate
20437c478bd9Sstevel@tonic-gate /* ------- */
20447c478bd9Sstevel@tonic-gate
20457c478bd9Sstevel@tonic-gate if (_attrInList(attr, nsl_attr_printerService))
20467c478bd9Sstevel@tonic-gate {
20477c478bd9Sstevel@tonic-gate result = 1; /* in list */
20487c478bd9Sstevel@tonic-gate }
20497c478bd9Sstevel@tonic-gate else
20507c478bd9Sstevel@tonic-gate if (_attrInList(attr, nsl_attr_printerIPP))
20517c478bd9Sstevel@tonic-gate {
20527c478bd9Sstevel@tonic-gate result = 1; /* in list */
20537c478bd9Sstevel@tonic-gate }
20547c478bd9Sstevel@tonic-gate else
20557c478bd9Sstevel@tonic-gate if (_attrInList(attr, nsl_attr_sunPrinter))
20567c478bd9Sstevel@tonic-gate {
20577c478bd9Sstevel@tonic-gate result = 1; /* in list */
20587c478bd9Sstevel@tonic-gate }
20597c478bd9Sstevel@tonic-gate
20607c478bd9Sstevel@tonic-gate return (result);
20617c478bd9Sstevel@tonic-gate } /* _attrInLDAPList */
20627c478bd9Sstevel@tonic-gate
20637c478bd9Sstevel@tonic-gate
20647c478bd9Sstevel@tonic-gate
20657c478bd9Sstevel@tonic-gate
20667c478bd9Sstevel@tonic-gate /*
20677c478bd9Sstevel@tonic-gate * *****************************************************************************
20687c478bd9Sstevel@tonic-gate *
20697c478bd9Sstevel@tonic-gate * Function: _getCurrentKVPValues()
20707c478bd9Sstevel@tonic-gate *
20717c478bd9Sstevel@tonic-gate * Description: For the given printer object read the current set of values
20727c478bd9Sstevel@tonic-gate * the object has for the sun-printer-kvp (Key Value pair)
20737c478bd9Sstevel@tonic-gate *
20747c478bd9Sstevel@tonic-gate * Parameters:
20757c478bd9Sstevel@tonic-gate * Input: LDAP *ld - existing ldap connection descriptor
20767c478bd9Sstevel@tonic-gate * char *objectDN - DN to search for
20777c478bd9Sstevel@tonic-gate * Output: char ***list - returned set of kvp values
20787c478bd9Sstevel@tonic-gate *
20797c478bd9Sstevel@tonic-gate * Result: NSL_RESULT - NSL_OK = object exists
20807c478bd9Sstevel@tonic-gate *
20817c478bd9Sstevel@tonic-gate * *****************************************************************************
20827c478bd9Sstevel@tonic-gate */
20837c478bd9Sstevel@tonic-gate
20847c478bd9Sstevel@tonic-gate static NSL_RESULT
_getCurrentKVPValues(LDAP * ld,uchar_t * objectDN,char *** list)20857c478bd9Sstevel@tonic-gate _getCurrentKVPValues(LDAP *ld, uchar_t *objectDN, char ***list)
20867c478bd9Sstevel@tonic-gate
20877c478bd9Sstevel@tonic-gate {
20887c478bd9Sstevel@tonic-gate NSL_RESULT result = NSL_ERR_UNKNOWN_PRINTER;
20897c478bd9Sstevel@tonic-gate int sresult = LDAP_NO_SUCH_OBJECT;
20907c478bd9Sstevel@tonic-gate int i = 0;
20917c478bd9Sstevel@tonic-gate LDAPMessage *ldapMsg;
20927c478bd9Sstevel@tonic-gate char *requiredAttrs[2] = { ATTR_KVP, NULL };
20937c478bd9Sstevel@tonic-gate LDAPMessage *ldapEntry = NULL;
20947c478bd9Sstevel@tonic-gate char *entryAttrib = NULL;
20957c478bd9Sstevel@tonic-gate char **attribValues = NULL;
20967c478bd9Sstevel@tonic-gate BerElement *berElement = NULL;
20977c478bd9Sstevel@tonic-gate
20987c478bd9Sstevel@tonic-gate /* ---------- */
20997c478bd9Sstevel@tonic-gate
21007c478bd9Sstevel@tonic-gate if ((list != NULL) && (ld != NULL) && (objectDN != NULL))
21017c478bd9Sstevel@tonic-gate {
21027c478bd9Sstevel@tonic-gate /* search for this Printer in the directory */
21037c478bd9Sstevel@tonic-gate
21047c478bd9Sstevel@tonic-gate sresult = ldap_search_s(ld, (char *)objectDN, LDAP_SCOPE_BASE,
21057c478bd9Sstevel@tonic-gate "(objectclass=*)", requiredAttrs, 0, &ldapMsg);
21067c478bd9Sstevel@tonic-gate if (sresult == LDAP_SUCCESS)
21077c478bd9Sstevel@tonic-gate {
21087c478bd9Sstevel@tonic-gate /*
21097c478bd9Sstevel@tonic-gate * check that the object exists and extract its
21107c478bd9Sstevel@tonic-gate * KVP attribute values
21117c478bd9Sstevel@tonic-gate */
21127c478bd9Sstevel@tonic-gate ldapEntry = ldap_first_entry(ld, ldapMsg);
21137c478bd9Sstevel@tonic-gate if (ldapEntry != NULL)
21147c478bd9Sstevel@tonic-gate {
21157c478bd9Sstevel@tonic-gate entryAttrib = ldap_first_attribute(ld,
21167c478bd9Sstevel@tonic-gate ldapEntry, &berElement);
21177c478bd9Sstevel@tonic-gate if ((entryAttrib != NULL) &&
21187c478bd9Sstevel@tonic-gate (strcasecmp(entryAttrib, ATTR_KVP) == 0))
21197c478bd9Sstevel@tonic-gate
21207c478bd9Sstevel@tonic-gate {
21217c478bd9Sstevel@tonic-gate #ifdef DEBUG
21227c478bd9Sstevel@tonic-gate printf("Attribute: %s, its values are:\n", entryAttrib);
21237c478bd9Sstevel@tonic-gate #endif
21247c478bd9Sstevel@tonic-gate /*
21257c478bd9Sstevel@tonic-gate * add each KVP value to the list
21267c478bd9Sstevel@tonic-gate * that we will return
21277c478bd9Sstevel@tonic-gate */
21287c478bd9Sstevel@tonic-gate attribValues = ldap_get_values(
21297c478bd9Sstevel@tonic-gate ld, ldapEntry, entryAttrib);
21307c478bd9Sstevel@tonic-gate for (i = 0;
21317c478bd9Sstevel@tonic-gate attribValues[i] != NULL; i++)
21327c478bd9Sstevel@tonic-gate {
21337c478bd9Sstevel@tonic-gate *list = (char **)
21347c478bd9Sstevel@tonic-gate list_append((void **)*list,
21357c478bd9Sstevel@tonic-gate strdup(attribValues[i]));
21367c478bd9Sstevel@tonic-gate #ifdef DEBUG
21377c478bd9Sstevel@tonic-gate printf("\t%s\n", attribValues[i]);
21387c478bd9Sstevel@tonic-gate #endif
21397c478bd9Sstevel@tonic-gate }
21407c478bd9Sstevel@tonic-gate (void) ldap_value_free(attribValues);
21417c478bd9Sstevel@tonic-gate }
21427c478bd9Sstevel@tonic-gate
21437c478bd9Sstevel@tonic-gate if ((entryAttrib != NULL) &&
21447c478bd9Sstevel@tonic-gate (berElement != NULL))
21457c478bd9Sstevel@tonic-gate {
21467c478bd9Sstevel@tonic-gate ber_free(berElement, 0);
21477c478bd9Sstevel@tonic-gate }
21487c478bd9Sstevel@tonic-gate
21497c478bd9Sstevel@tonic-gate
21507c478bd9Sstevel@tonic-gate /* object found */
21517c478bd9Sstevel@tonic-gate result = NSL_OK;
21527c478bd9Sstevel@tonic-gate }
21537c478bd9Sstevel@tonic-gate
215486b1a8baSrotondo (void) ldap_msgfree(ldapMsg);
21557c478bd9Sstevel@tonic-gate }
21567c478bd9Sstevel@tonic-gate }
21577c478bd9Sstevel@tonic-gate
21587c478bd9Sstevel@tonic-gate else
21597c478bd9Sstevel@tonic-gate {
21607c478bd9Sstevel@tonic-gate result = NSL_ERR_INTERNAL;
21617c478bd9Sstevel@tonic-gate }
21627c478bd9Sstevel@tonic-gate
21637c478bd9Sstevel@tonic-gate return (result);
21647c478bd9Sstevel@tonic-gate } /* _getCurrentKVPValues */
21657c478bd9Sstevel@tonic-gate
21667c478bd9Sstevel@tonic-gate
21677c478bd9Sstevel@tonic-gate
21687c478bd9Sstevel@tonic-gate /*
21697c478bd9Sstevel@tonic-gate * *****************************************************************************
21707c478bd9Sstevel@tonic-gate *
21717c478bd9Sstevel@tonic-gate * Function: _freeList()
21727c478bd9Sstevel@tonic-gate *
21737c478bd9Sstevel@tonic-gate * Description: Free the list created by list_append() where the items in
21747c478bd9Sstevel@tonic-gate * the list have been strdup'ed.
21757c478bd9Sstevel@tonic-gate *
21767c478bd9Sstevel@tonic-gate * Parameters:
21777c478bd9Sstevel@tonic-gate * Input: char ***list - returned set of kvp values
21787c478bd9Sstevel@tonic-gate *
21797c478bd9Sstevel@tonic-gate * Result: void
21807c478bd9Sstevel@tonic-gate *
21817c478bd9Sstevel@tonic-gate * *****************************************************************************
21827c478bd9Sstevel@tonic-gate */
21837c478bd9Sstevel@tonic-gate
21847c478bd9Sstevel@tonic-gate static void
_freeList(char *** list)21857c478bd9Sstevel@tonic-gate _freeList(char ***list)
21867c478bd9Sstevel@tonic-gate
21877c478bd9Sstevel@tonic-gate {
21887c478bd9Sstevel@tonic-gate int i = 0;
21897c478bd9Sstevel@tonic-gate
21907c478bd9Sstevel@tonic-gate /* ------ */
21917c478bd9Sstevel@tonic-gate
21927c478bd9Sstevel@tonic-gate if (list != NULL)
21937c478bd9Sstevel@tonic-gate {
21947c478bd9Sstevel@tonic-gate if (*list != NULL)
21957c478bd9Sstevel@tonic-gate {
21967c478bd9Sstevel@tonic-gate for (i = 0; (*list)[i] != NULL; i++)
21977c478bd9Sstevel@tonic-gate {
21987c478bd9Sstevel@tonic-gate free((*list)[i]);
21997c478bd9Sstevel@tonic-gate }
22007c478bd9Sstevel@tonic-gate free(*list);
22017c478bd9Sstevel@tonic-gate }
22027c478bd9Sstevel@tonic-gate
22037c478bd9Sstevel@tonic-gate *list = NULL;
22047c478bd9Sstevel@tonic-gate }
22057c478bd9Sstevel@tonic-gate } /* _freeList */
22067c478bd9Sstevel@tonic-gate
22077c478bd9Sstevel@tonic-gate
22087c478bd9Sstevel@tonic-gate
22097c478bd9Sstevel@tonic-gate /*
22107c478bd9Sstevel@tonic-gate * *****************************************************************************
22117c478bd9Sstevel@tonic-gate *
22127c478bd9Sstevel@tonic-gate * Function: _modAttrKVP()
22137c478bd9Sstevel@tonic-gate *
22147c478bd9Sstevel@tonic-gate * Description: Sort out the KVP attribute value list, such that this new
22157c478bd9Sstevel@tonic-gate * value takes precidence over any existing value in the list.
22167c478bd9Sstevel@tonic-gate * The current list is updated to remove this key, and the new
22177c478bd9Sstevel@tonic-gate * key "value" is added to the list, eg. for
22187c478bd9Sstevel@tonic-gate * value: bbb=ddddd
22197c478bd9Sstevel@tonic-gate * and kvpList:
22207c478bd9Sstevel@tonic-gate * aaa=yyyy
22217c478bd9Sstevel@tonic-gate * bbb=zzzz
22227c478bd9Sstevel@tonic-gate * ccc=xxxx
22237c478bd9Sstevel@tonic-gate * the resulting kvpList is:
22247c478bd9Sstevel@tonic-gate * aaa=yyyy
22257c478bd9Sstevel@tonic-gate * ccc=xxxx
22267c478bd9Sstevel@tonic-gate * bbb=ddddd
22277c478bd9Sstevel@tonic-gate *
22287c478bd9Sstevel@tonic-gate * Note: When all new values have been handled the function _attrAddKVP()
22297c478bd9Sstevel@tonic-gate * must be called to add the "new list" values into the
22307c478bd9Sstevel@tonic-gate * LDAPMod array.
22317c478bd9Sstevel@tonic-gate *
22327c478bd9Sstevel@tonic-gate * Parameters:
22337c478bd9Sstevel@tonic-gate * Input: char *value - Key Value Pair to process,
22347c478bd9Sstevel@tonic-gate * eg. aaaaa=hhhhh, where aaaaa is the key
22357c478bd9Sstevel@tonic-gate * char ***kvpList - list of current KVP values
22367c478bd9Sstevel@tonic-gate * Output: char ***kvpList - updated list of KVP values
22377c478bd9Sstevel@tonic-gate *
22387c478bd9Sstevel@tonic-gate * Returns: NSL_RESULT - NSL_OK = done okay
22397c478bd9Sstevel@tonic-gate *
22407c478bd9Sstevel@tonic-gate * *****************************************************************************
22417c478bd9Sstevel@tonic-gate */
22427c478bd9Sstevel@tonic-gate
22437c478bd9Sstevel@tonic-gate static NSL_RESULT
_modAttrKVP(char * value,char *** kvpList)22447c478bd9Sstevel@tonic-gate _modAttrKVP(char *value, char ***kvpList)
22457c478bd9Sstevel@tonic-gate
22467c478bd9Sstevel@tonic-gate {
22477c478bd9Sstevel@tonic-gate NSL_RESULT result = NSL_ERR_INTERNAL;
22487c478bd9Sstevel@tonic-gate int i = 0;
22497c478bd9Sstevel@tonic-gate int inList = 0;
22507c478bd9Sstevel@tonic-gate int keyDelete = 0;
22517c478bd9Sstevel@tonic-gate char *key = NULL;
22527c478bd9Sstevel@tonic-gate char **p = NULL;
22537c478bd9Sstevel@tonic-gate char **newList = NULL;
22547c478bd9Sstevel@tonic-gate
22557c478bd9Sstevel@tonic-gate /* ------- */
22567c478bd9Sstevel@tonic-gate
22577c478bd9Sstevel@tonic-gate if ((value != NULL) && (kvpList != NULL))
22587c478bd9Sstevel@tonic-gate {
22597c478bd9Sstevel@tonic-gate result = NSL_OK;
22607c478bd9Sstevel@tonic-gate
22617c478bd9Sstevel@tonic-gate /* extract "key" from value */
22627c478bd9Sstevel@tonic-gate
22637c478bd9Sstevel@tonic-gate key = strdup(value);
22647c478bd9Sstevel@tonic-gate
22657c478bd9Sstevel@tonic-gate for (i = 0; ((key)[i] != '=') && ((key)[i] != '\0'); i++);
22667c478bd9Sstevel@tonic-gate key[i] = '\0'; /* terminate the key */
22677c478bd9Sstevel@tonic-gate
22687c478bd9Sstevel@tonic-gate /* Is this a request to delete a "key" value */
22697c478bd9Sstevel@tonic-gate
22707c478bd9Sstevel@tonic-gate if ((value[i] == '\0') || (value[i+1] == '\0'))
22717c478bd9Sstevel@tonic-gate {
22727c478bd9Sstevel@tonic-gate /* this is a request to delete the key */
22737c478bd9Sstevel@tonic-gate keyDelete = 1;
22747c478bd9Sstevel@tonic-gate }
22757c478bd9Sstevel@tonic-gate
22767c478bd9Sstevel@tonic-gate if ((*kvpList != NULL) && (**kvpList != NULL))
22777c478bd9Sstevel@tonic-gate {
22787c478bd9Sstevel@tonic-gate /*
22797c478bd9Sstevel@tonic-gate * for each item in the list remove it if the keys match
22807c478bd9Sstevel@tonic-gate */
22817c478bd9Sstevel@tonic-gate for (p = *kvpList; *p != NULL; p++)
22827c478bd9Sstevel@tonic-gate {
22837c478bd9Sstevel@tonic-gate for (i = 0;
22847c478bd9Sstevel@tonic-gate ((*p)[i] != '=') && ((*p)[i] != '\0'); i++);
22857c478bd9Sstevel@tonic-gate
22867c478bd9Sstevel@tonic-gate if ((strlen(key) == i) &&
22877c478bd9Sstevel@tonic-gate (strncasecmp(*p, key, i) == 0))
22887c478bd9Sstevel@tonic-gate {
22897c478bd9Sstevel@tonic-gate inList = 1;
22907c478bd9Sstevel@tonic-gate }
22917c478bd9Sstevel@tonic-gate else
22927c478bd9Sstevel@tonic-gate {
22937c478bd9Sstevel@tonic-gate /* no match so add value to new list */
22947c478bd9Sstevel@tonic-gate newList = (char **)list_append(
22957c478bd9Sstevel@tonic-gate (void **)newList,
22967c478bd9Sstevel@tonic-gate strdup(*p));
22977c478bd9Sstevel@tonic-gate }
22987c478bd9Sstevel@tonic-gate }
22997c478bd9Sstevel@tonic-gate }
23007c478bd9Sstevel@tonic-gate
23017c478bd9Sstevel@tonic-gate /*
23027c478bd9Sstevel@tonic-gate * if it was not a DELETE request add the new key value into
23037c478bd9Sstevel@tonic-gate * the newList, otherwise we have already removed the key
23047c478bd9Sstevel@tonic-gate */
23057c478bd9Sstevel@tonic-gate
23067c478bd9Sstevel@tonic-gate if (!keyDelete)
23077c478bd9Sstevel@tonic-gate {
23087c478bd9Sstevel@tonic-gate newList = (char **)list_append((void **)newList,
23097c478bd9Sstevel@tonic-gate strdup(value));
23107c478bd9Sstevel@tonic-gate }
23117c478bd9Sstevel@tonic-gate
23127c478bd9Sstevel@tonic-gate if ((newList != NULL) || (inList))
23137c478bd9Sstevel@tonic-gate {
23147c478bd9Sstevel@tonic-gate /* replace old list with the newList */
23157c478bd9Sstevel@tonic-gate _freeList(kvpList);
23167c478bd9Sstevel@tonic-gate *kvpList = newList;
23177c478bd9Sstevel@tonic-gate }
23187c478bd9Sstevel@tonic-gate
23197c478bd9Sstevel@tonic-gate free(key);
23207c478bd9Sstevel@tonic-gate }
23217c478bd9Sstevel@tonic-gate
23227c478bd9Sstevel@tonic-gate return (result);
23237c478bd9Sstevel@tonic-gate } /* modAttrKVP */
23247c478bd9Sstevel@tonic-gate
23257c478bd9Sstevel@tonic-gate
23267c478bd9Sstevel@tonic-gate
23277c478bd9Sstevel@tonic-gate
23287c478bd9Sstevel@tonic-gate /*
23297c478bd9Sstevel@tonic-gate * *****************************************************************************
23307c478bd9Sstevel@tonic-gate *
23317c478bd9Sstevel@tonic-gate * Function: _attrAddKVP()
23327c478bd9Sstevel@tonic-gate *
23337c478bd9Sstevel@tonic-gate * Description: Process KVP items in the kvpList adding them to the
23347c478bd9Sstevel@tonic-gate * LDAPMod modify array. If the list is empty but there were
23357c478bd9Sstevel@tonic-gate * previously LDAP KVP values delete them.
23367c478bd9Sstevel@tonic-gate *
23377c478bd9Sstevel@tonic-gate * Note: This function should only be called when all the new KVP
23387c478bd9Sstevel@tonic-gate * items have been processed by _modAttrKVP()
23397c478bd9Sstevel@tonic-gate *
23407c478bd9Sstevel@tonic-gate * Parameters:
23417c478bd9Sstevel@tonic-gate * Input: LDAPMod ***attrs - array to update
23427c478bd9Sstevel@tonic-gate * char **kvpList - list KVP values
23437c478bd9Sstevel@tonic-gate * int kvpExists - object currently has LDAP KVP values
23447c478bd9Sstevel@tonic-gate * Output: None
23457c478bd9Sstevel@tonic-gate *
23467c478bd9Sstevel@tonic-gate * Returns: NSL_RESULT - NSL_OK = done okay
23477c478bd9Sstevel@tonic-gate *
23487c478bd9Sstevel@tonic-gate * *****************************************************************************
23497c478bd9Sstevel@tonic-gate */
23507c478bd9Sstevel@tonic-gate
23517c478bd9Sstevel@tonic-gate static NSL_RESULT
_attrAddKVP(LDAPMod *** attrs,char ** kvpList,int kvpExists)23527c478bd9Sstevel@tonic-gate _attrAddKVP(LDAPMod ***attrs, char **kvpList, int kvpExists)
23537c478bd9Sstevel@tonic-gate
23547c478bd9Sstevel@tonic-gate {
23557c478bd9Sstevel@tonic-gate NSL_RESULT result = NSL_OK;
23567c478bd9Sstevel@tonic-gate
23577c478bd9Sstevel@tonic-gate /* ------- */
23587c478bd9Sstevel@tonic-gate
23597c478bd9Sstevel@tonic-gate if (attrs != NULL)
23607c478bd9Sstevel@tonic-gate {
23617c478bd9Sstevel@tonic-gate if (kvpList != NULL)
23627c478bd9Sstevel@tonic-gate {
23637c478bd9Sstevel@tonic-gate while ((kvpList != NULL) && (*kvpList != NULL))
23647c478bd9Sstevel@tonic-gate {
23657c478bd9Sstevel@tonic-gate /* add item to LDAPMod array */
23667c478bd9Sstevel@tonic-gate
23677c478bd9Sstevel@tonic-gate result =
23687c478bd9Sstevel@tonic-gate _modLDAPmodValue(attrs, ATTR_KVP, *kvpList);
23697c478bd9Sstevel@tonic-gate
23707c478bd9Sstevel@tonic-gate kvpList++;
23717c478bd9Sstevel@tonic-gate }
23727c478bd9Sstevel@tonic-gate }
23737c478bd9Sstevel@tonic-gate else
23747c478bd9Sstevel@tonic-gate if (kvpExists)
23757c478bd9Sstevel@tonic-gate {
23767c478bd9Sstevel@tonic-gate /*
23777c478bd9Sstevel@tonic-gate * We now have no LDAP KVP values but there were
23787c478bd9Sstevel@tonic-gate * some previously, so delete them
23797c478bd9Sstevel@tonic-gate */
23807c478bd9Sstevel@tonic-gate result = _modLDAPmodValue(attrs, ATTR_KVP, NULL);
23817c478bd9Sstevel@tonic-gate }
23827c478bd9Sstevel@tonic-gate }
23837c478bd9Sstevel@tonic-gate
23847c478bd9Sstevel@tonic-gate else
23857c478bd9Sstevel@tonic-gate {
23867c478bd9Sstevel@tonic-gate result = NSL_ERR_INTERNAL;
23877c478bd9Sstevel@tonic-gate }
23887c478bd9Sstevel@tonic-gate
23897c478bd9Sstevel@tonic-gate return (result);
23907c478bd9Sstevel@tonic-gate } /* _attrAddKVP */
23917c478bd9Sstevel@tonic-gate
23927c478bd9Sstevel@tonic-gate
23937c478bd9Sstevel@tonic-gate
23947c478bd9Sstevel@tonic-gate
23957c478bd9Sstevel@tonic-gate /*
23967c478bd9Sstevel@tonic-gate * *****************************************************************************
23977c478bd9Sstevel@tonic-gate *
23987c478bd9Sstevel@tonic-gate * Function: _manageReferralCredentials()
23997c478bd9Sstevel@tonic-gate *
24007c478bd9Sstevel@tonic-gate * Description: This function is called if a referral request is returned by
24017c478bd9Sstevel@tonic-gate * the origonal LDAP server during the ldap update request call,
24027c478bd9Sstevel@tonic-gate * eg. ldap_add_s(), ldap_modify_s() or ldap_delete_s().
24037c478bd9Sstevel@tonic-gate * Parameters:
24047c478bd9Sstevel@tonic-gate * Input: LDAP *ld - LDAP descriptor
24057c478bd9Sstevel@tonic-gate * int freeit - 0 = first call to get details
24067c478bd9Sstevel@tonic-gate * - 1 = second call to free details
24077c478bd9Sstevel@tonic-gate * - -1 = initial store of authentication details
24087c478bd9Sstevel@tonic-gate * Input/Output: char **dn - returns DN to bind to on master
24097c478bd9Sstevel@tonic-gate * char **credp - returns password for DN
24107c478bd9Sstevel@tonic-gate * int *methodp - returns authentication type, eg. simple
24117c478bd9Sstevel@tonic-gate *
24127c478bd9Sstevel@tonic-gate * Returns: int - 0 = okay
24137c478bd9Sstevel@tonic-gate *
24147c478bd9Sstevel@tonic-gate * *****************************************************************************
24157c478bd9Sstevel@tonic-gate */
_manageReferralCredentials(LDAP * ld,char ** dn,char ** credp,int * methodp,int freeit,void * arg __unused)24167c478bd9Sstevel@tonic-gate static int _manageReferralCredentials(LDAP *ld, char **dn, char **credp,
2417*ef2333d1SToomas Soome int *methodp, int freeit, void *arg __unused)
24187c478bd9Sstevel@tonic-gate {
24197c478bd9Sstevel@tonic-gate int result = 0;
24207c478bd9Sstevel@tonic-gate static char *sDN = NULL;
24217c478bd9Sstevel@tonic-gate static char *sPasswd = NULL;
24227c478bd9Sstevel@tonic-gate static int sMethod = LDAP_AUTH_SIMPLE;
24237c478bd9Sstevel@tonic-gate
24247c478bd9Sstevel@tonic-gate /* -------- */
24257c478bd9Sstevel@tonic-gate
24267c478bd9Sstevel@tonic-gate if (freeit == 1)
24277c478bd9Sstevel@tonic-gate {
24287c478bd9Sstevel@tonic-gate /* second call - free memory */
24297c478bd9Sstevel@tonic-gate
24307c478bd9Sstevel@tonic-gate if ((dn != NULL) && (*dn != NULL))
24317c478bd9Sstevel@tonic-gate {
24327c478bd9Sstevel@tonic-gate free(*dn);
24337c478bd9Sstevel@tonic-gate }
24347c478bd9Sstevel@tonic-gate
24357c478bd9Sstevel@tonic-gate if ((credp != NULL) && (*credp != NULL))
24367c478bd9Sstevel@tonic-gate {
24377c478bd9Sstevel@tonic-gate free(*credp);
24387c478bd9Sstevel@tonic-gate }
24397c478bd9Sstevel@tonic-gate }
24407c478bd9Sstevel@tonic-gate
24417c478bd9Sstevel@tonic-gate else
24427c478bd9Sstevel@tonic-gate if ((ld != NULL) &&
24437c478bd9Sstevel@tonic-gate (dn != NULL) && (credp != NULL) && (methodp != NULL))
24447c478bd9Sstevel@tonic-gate {
24457c478bd9Sstevel@tonic-gate if ((freeit == 0) && (sDN != NULL) && (sPasswd != NULL))
24467c478bd9Sstevel@tonic-gate {
24477c478bd9Sstevel@tonic-gate /* first call - get the saved bind credentials */
24487c478bd9Sstevel@tonic-gate
24497c478bd9Sstevel@tonic-gate *dn = strdup(sDN);
24507c478bd9Sstevel@tonic-gate *credp = strdup(sPasswd);
24517c478bd9Sstevel@tonic-gate *methodp = sMethod;
24527c478bd9Sstevel@tonic-gate }
24537c478bd9Sstevel@tonic-gate else
24547c478bd9Sstevel@tonic-gate if (freeit == -1)
24557c478bd9Sstevel@tonic-gate {
24567c478bd9Sstevel@tonic-gate /* initial call - save the saved bind credentials */
24577c478bd9Sstevel@tonic-gate
24587c478bd9Sstevel@tonic-gate sDN = *dn;
24597c478bd9Sstevel@tonic-gate sPasswd = *credp;
24607c478bd9Sstevel@tonic-gate sMethod = *methodp;
24617c478bd9Sstevel@tonic-gate }
24627c478bd9Sstevel@tonic-gate else
24637c478bd9Sstevel@tonic-gate {
24647c478bd9Sstevel@tonic-gate result = 1; /* error */
24657c478bd9Sstevel@tonic-gate }
24667c478bd9Sstevel@tonic-gate }
24677c478bd9Sstevel@tonic-gate else
24687c478bd9Sstevel@tonic-gate {
24697c478bd9Sstevel@tonic-gate result = 1; /* error */
24707c478bd9Sstevel@tonic-gate }
24717c478bd9Sstevel@tonic-gate
24727c478bd9Sstevel@tonic-gate return (result);
24737c478bd9Sstevel@tonic-gate } /* _manageReferralCredentials */
2474