2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#pragma weak _nss_ldap__printers_constr = _nss_ldap_printers_constr
2N/A
2N/A#include "ldap_common.h"
2N/A
2N/Astatic void append_attr(char *buf, char *attr);
2N/A
2N/A/* printer attributes filters */
2N/A#define _F_GETPRINTERBYNAME \
2N/A "(&(objectClass=sunPrinter)(|(printer-name=%s)(printer-aliases=%s)))"
2N/A
2N/A#define PRINTER_PREFIX "printer-"
2N/A#define SUNWPR_PREFIX "sunwpr-"
2N/A
2N/A/*
2N/A * Attributes from the following classes:
2N/A * printerService
2N/A * printerAbstact
2N/A * sunPrinter
2N/A */
2N/A
2N/A/*
2N/A * Get all attributes.
2N/A */
2N/Astatic const char **printer_attrs = NULL;
2N/A
2N/A
2N/A/*
2N/A * _nss_ldap_printers2str is the data marshaling method for the printers
2N/A * getXbyY backend processes. This method is called after a successful
2N/A * ldap search has been performed. This method will parse the ldap search
2N/A * values into argp->buf.buffer. Three error conditions are expected and
2N/A * returned to nsswitch.
2N/A * In order to be compatible with old data output, the code is commented out
2N/A * with NSS_LDAP_PRINTERS. The NSS_LDAP_PRINTERS section is for future
2N/A * refrences if it's decided to fix the output format.
2N/A */
2N/A
2N/Astatic int
2N/A_nss_ldap_printers2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
2N/A{
2N/A int i, j;
2N/A int nss_result;
2N/A int buflen = 0, len;
2N/A char *buffer = NULL;
2N/A char **name, *attrname;
2N/A ns_ldap_attr_t *attr;
2N/A ns_ldap_result_t *result = be->result;
2N/A#ifdef NSS_LDAP_PRINTERS
2N/A int slen, plen;
2N/A#endif
2N/A
2N/A if (result == NULL)
2N/A return (NSS_STR_PARSE_PARSE);
2N/A
2N/A buflen = argp->buf.buflen;
2N/A if (argp->buf.result != NULL) {
2N/A be->buffer = calloc(1, buflen);
2N/A if (be->buffer == NULL)
2N/A return (NSS_STR_PARSE_PARSE);
2N/A be->buflen = buflen;
2N/A buffer = be->buffer;
2N/A } else {
2N/A buffer = argp->buf.buffer;
2N/A (void) memset(argp->buf.buffer, 0, buflen);
2N/A }
2N/A
2N/A nss_result = NSS_STR_PARSE_SUCCESS;
2N/A
2N/A#ifdef NSS_LDAP_PRINTERS
2N/A slen = strlen(SUNWPR_PREFIX);
2N/A plen = strlen(PRINTER_PREFIX);
2N/A#endif
2N/A
2N/A /*
2N/A * Pick out the printer name and aliases
2N/A */
2N/A name = __ns_ldap_getAttr(result->entry, "printer-name");
2N/A if (name == NULL || name[0] == NULL) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_printers2str;
2N/A }
2N/A len = snprintf(buffer, buflen, "%s", name[0]);
2N/A TEST_AND_ADJUST(len, buffer, buflen, result_printers2str);
2N/A
2N/A#ifdef NSS_LDAP_PRINTERS
2N/A attr = __ns_ldap_getAttrStruct(result->entry, "printer-aliases");
2N/A if (attr != NULL && attr->attrvalue != NULL) {
2N/A for (i = 0; i < attr->value_count; i++) {
2N/A len = snprintf(buffer, buflen, "|%s",
2N/A attr->attrvalue[i]);
2N/A TEST_AND_ADJUST(len, buffer, buflen,
2N/A result_printers2str);
2N/A }
2N/A }
2N/A#endif
2N/A /*
2N/A * Add the rest of the attributes
2N/A */
2N/A for (i = 0; i < result->entry->attr_count; i++) {
2N/A attr = getattr(result, i);
2N/A if (attr == NULL) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_printers2str;
2N/A }
2N/A /*
2N/A * The attribute contains key=value
2N/A */
2N/A if (strcasecmp(attr->attrname, "sun-printer-kvp") == 0) {
2N/A for (j = 0; j < attr->value_count; j++) {
2N/A len = strlen(attr->attrvalue[j]);
2N/A if (len < 1 ||
2N/A (attr->attrvalue[j] == '\0')) {
2N/A *buffer = 0;
2N/A nss_result = (int)NSS_STR_PARSE_PARSE;
2N/A goto result_printers2str;
2N/A }
2N/A len = snprintf(buffer, buflen, ":%s",
2N/A attr->attrvalue[j]);
2N/A TEST_AND_ADJUST(len, buffer, buflen,
2N/A result_printers2str);
2N/A }
2N/A } else {
2N/A /*
2N/A * Skip some attr names
2N/A */
2N/A#ifdef NSS_LDAP_PRINTERS
2N/A if (strcasecmp(attr->attrname, "printer-name") == 0 ||
2N/A strcasecmp(attr->attrname, "dn") == 0 ||
2N/A strcasecmp(attr->attrname,
2N/A "objectclass") == 0 ||
2N/A strcasecmp(attr->attrname,
2N/A "printer-uri") == 0 ||
2N/A strcasecmp(attr->attrname,
2N/A "printer-aliases") == 0)
2N/A#else
2N/A if (strcasecmp(attr->attrname, "printer-name") == 0)
2N/A#endif
2N/A continue;
2N/A }
2N/A /*
2N/A * Translate attr name ->key name
2N/A */
2N/A if (strcmp(attr->attrname, "sun-printer-bsdaddr")
2N/A == 0)
2N/A attrname = "bsdaddr";
2N/A#ifdef NSS_LDAP_PRINTERS
2N/A else if (strcmp(attr->attrname, "printer-info")
2N/A == 0)
2N/A attrname = "description";
2N/A else if (strcmp(attr->attrname, "sunwpr-support")
2N/A == 0)
2N/A attrname = "itopssupported";
2N/A else if (strncmp(attr->attrname, PRINTER_PREFIX, plen)
2N/A == 0)
2N/A attrname = attr->attrname + plen;
2N/A else if (strncmp(attr->attrname, SUNWPR_PREFIX, slen)
2N/A == 0)
2N/A attrname = attr->attrname + slen;
2N/A#endif
2N/A else
2N/A attrname = attr->attrname;
2N/A
2N/A /*
2N/A * The attrname is the key. The attribute
2N/A * data is the value.
2N/A */
2N/A len = snprintf(buffer, buflen, ":%s=", attrname);
2N/A TEST_AND_ADJUST(len, buffer, buflen,
2N/A result_printers2str);
2N/A
2N/A for (j = 0; j < attr->value_count; j++) {
2N/A int k;
2N/A char *kp;
2N/A
2N/A if (attr->attrvalue[j] == NULL) {
2N/A *buffer = 0;
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_printers2str;
2N/A }
2N/A len = strlen(attr->attrvalue[j]);
2N/A if (len < 1) {
2N/A *buffer = 0;
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_printers2str;
2N/A }
2N/A /*
2N/A * Add extra for any colons which need to
2N/A * be backslashed plus ending ':' or ','.
2N/A */
2N/A k = 0;
2N/A for (kp = attr->attrvalue[j]; *kp != NULL; kp++)
2N/A if (*kp == ':')
2N/A /* count ':' in value */
2N/A k++;
2N/A if (j == 0)
2N/A /* first time */
2N/A len += k;
2N/A else
2N/A /* add ',' */
2N/A len += k + 1;
2N/A
2N/A if (len > buflen) {
2N/A nss_result = NSS_STR_PARSE_ERANGE;
2N/A goto result_printers2str;
2N/A }
2N/A if (j > 0)
2N/A *buffer++ = ',';
2N/A
2N/A (void) append_attr(buffer,
2N/A attr->attrvalue[j]);
2N/A buffer += strlen(attr->attrvalue[j]) + k;
2N/A buflen -= len;
2N/A }
2N/A }
2N/A
2N/A if (argp->buf.result != NULL)
2N/A be->buflen = strlen(be->buffer);
2N/A
2N/Aresult_printers2str:
2N/A (void) __ns_ldap_freeResult(&be->result);
2N/A return ((int)nss_result);
2N/A}
2N/A
2N/A/*
2N/A * Attributes which contain colons must be backslashed.
2N/A */
2N/Astatic void
2N/Aappend_attr(char *buf, char *attr)
2N/A{
2N/A char *cp, *bp;
2N/A
2N/A if (strchr(attr, ':') == NULL) {
2N/A (void) strcat(buf, attr);
2N/A return;
2N/A }
2N/A bp = buf;
2N/A cp = attr;
2N/A while (*cp != NULL) {
2N/A if (*cp == ':') {
2N/A *bp++ = '\\';
2N/A }
2N/A *bp++ = *cp++;
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * getbyname gets printer attributes by printer name. This function
2N/A * constructs an ldap search filter using the printer name invocation
2N/A * parameter and the getprinterbyname search filter defined. Once the
2N/A * filter is constructed, we search for matching entries and marshal
2N/A * the data results into argp->buf.buffer for the frontend process.
2N/A * The function _nss_ldap_printers2str performs the data marshaling.
2N/A */
2N/A
2N/Astatic nss_status_t
2N/Agetbyname(ldap_backend_ptr be, void *a)
2N/A{
2N/A char printername[BUFSIZ];
2N/A nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
2N/A char searchfilter[SEARCHFILTERLEN];
2N/A
2N/A (void) strncpy(printername, argp->key.name, BUFSIZ);
2N/A if (snprintf(searchfilter, SEARCHFILTERLEN,
2N/A _F_GETPRINTERBYNAME, printername, printername) < 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A return ((nss_status_t)_nss_ldap_lookup(be, argp,
2N/A _PRINTERS, searchfilter, NULL, NULL, NULL));
2N/A}
2N/A
2N/Astatic ldap_backend_op_t printers_ops[] = {
2N/A _nss_ldap_destr,
2N/A _nss_ldap_endent,
2N/A _nss_ldap_setent,
2N/A _nss_ldap_getent,
2N/A getbyname,
2N/A};
2N/A
2N/A
2N/A/*
2N/A * _nss_ldap_printers_constr is where life begins. This function calls
2N/A * the generic ldap constructor function to define and build the abstract
2N/A * data types required to support ldap operations.
2N/A */
2N/A
2N/A/*ARGSUSED0*/
2N/Anss_backend_t *
2N/A_nss_ldap_printers_constr(const char *dummy1, const char *dummy2,
2N/A const char *dummy3)
2N/A{
2N/A
2N/A return ((nss_backend_t *)_nss_ldap_constr(printers_ops,
2N/A sizeof (printers_ops)/sizeof (printers_ops[0]), _PRINTERS,
2N/A printer_attrs, _nss_ldap_printers2str));
2N/A}