xref: /onnv-gate/usr/src/lib/nsswitch/ldap/common/getprinter.c (revision 2830:5228d1267a01)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2830Sdjl  * Common Development and Distribution License (the "License").
6*2830Sdjl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2830Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma weak _nss_ldap__printers_constr = _nss_ldap_printers_constr
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include "ldap_common.h"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate static void append_attr(char *buf, char *attr);
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /* printer attributes filters */
350Sstevel@tonic-gate #define	_F_GETPRINTERBYNAME	\
360Sstevel@tonic-gate 	"(&(objectClass=sunPrinter)(|(printer-name=%s)(printer-aliases=%s)))"
370Sstevel@tonic-gate 
38*2830Sdjl #define	PRINTER_PREFIX	"printer-"
39*2830Sdjl #define	SUNWPR_PREFIX	"sunwpr-"
40*2830Sdjl 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * Attributes from the following classes:
430Sstevel@tonic-gate  * 	printerService
440Sstevel@tonic-gate  * 	printerAbstact
450Sstevel@tonic-gate  * 	sunPrinter
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * Get all attributes.
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate static const char **printer_attrs = NULL;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
55*2830Sdjl  * _nss_ldap_printers2str is the data marshaling method for the printers
560Sstevel@tonic-gate  * getXbyY backend processes. This method is called after a successful
570Sstevel@tonic-gate  * ldap search has been performed. This method will parse the ldap search
580Sstevel@tonic-gate  * values into argp->buf.buffer. Three error conditions are expected and
590Sstevel@tonic-gate  * returned to nsswitch.
60*2830Sdjl  * In order to be compatible with old data output, the code is commented out
61*2830Sdjl  * with NSS_LDAP_PRINTERS. The NSS_LDAP_PRINTERS section is for future
62*2830Sdjl  * refrences if it's decided to fix the output format.
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate static int
_nss_ldap_printers2str(ldap_backend_ptr be,nss_XbyY_args_t * argp)66*2830Sdjl _nss_ldap_printers2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	int			i, j;
690Sstevel@tonic-gate 	int			nss_result;
70*2830Sdjl 	int			buflen = 0, len;
71*2830Sdjl 	char			*buffer = NULL;
72*2830Sdjl 	char			**name, *attrname;
730Sstevel@tonic-gate 	ns_ldap_attr_t		*attr;
740Sstevel@tonic-gate 	ns_ldap_result_t	*result = be->result;
75*2830Sdjl #ifdef	NSS_LDAP_PRINTERS
76*2830Sdjl 	int			slen, plen;
77*2830Sdjl #endif
780Sstevel@tonic-gate 
79*2830Sdjl 	if (result == NULL)
80*2830Sdjl 		return (NSS_STR_PARSE_PARSE);
81*2830Sdjl 
82*2830Sdjl 	buflen = argp->buf.buflen;
83*2830Sdjl 	if (argp->buf.result != NULL) {
84*2830Sdjl 		be->buffer = calloc(1, buflen);
85*2830Sdjl 		if (be->buffer == NULL)
86*2830Sdjl 			return (NSS_STR_PARSE_PARSE);
87*2830Sdjl 		be->buflen = buflen;
88*2830Sdjl 		buffer = be->buffer;
89*2830Sdjl 	} else {
90*2830Sdjl 		buffer = argp->buf.buffer;
91*2830Sdjl 		(void) memset(argp->buf.buffer, 0, buflen);
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 
94*2830Sdjl 	nss_result = NSS_STR_PARSE_SUCCESS;
950Sstevel@tonic-gate 
96*2830Sdjl #ifdef	NSS_LDAP_PRINTERS
97*2830Sdjl 	slen = strlen(SUNWPR_PREFIX);
98*2830Sdjl 	plen = strlen(PRINTER_PREFIX);
99*2830Sdjl #endif
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	/*
102*2830Sdjl 	 * Pick out the printer name and aliases
1030Sstevel@tonic-gate 	 */
104*2830Sdjl 	name = __ns_ldap_getAttr(result->entry, "printer-name");
105*2830Sdjl 	if (name == NULL || name[0] == NULL) {
106*2830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
107*2830Sdjl 		goto result_printers2str;
108*2830Sdjl 	}
109*2830Sdjl 	len = snprintf(buffer, buflen, "%s", name[0]);
110*2830Sdjl 	TEST_AND_ADJUST(len, buffer, buflen, result_printers2str);
111*2830Sdjl 
112*2830Sdjl #ifdef	NSS_LDAP_PRINTERS
113*2830Sdjl 	attr = __ns_ldap_getAttrStruct(result->entry, "printer-aliases");
114*2830Sdjl 	if (attr != NULL && attr->attrvalue != NULL) {
115*2830Sdjl 		for (i = 0; i < attr->value_count; i++) {
116*2830Sdjl 			len = snprintf(buffer, buflen, "|%s",
117*2830Sdjl 					attr->attrvalue[i]);
118*2830Sdjl 			TEST_AND_ADJUST(len, buffer, buflen,
119*2830Sdjl 					result_printers2str);
1200Sstevel@tonic-gate 		}
1210Sstevel@tonic-gate 	}
122*2830Sdjl #endif
1230Sstevel@tonic-gate 	/*
1240Sstevel@tonic-gate 	 * Add the rest of the attributes
1250Sstevel@tonic-gate 	 */
1260Sstevel@tonic-gate 	for (i = 0; i < result->entry->attr_count; i++) {
1270Sstevel@tonic-gate 		attr = getattr(result, i);
1280Sstevel@tonic-gate 		if (attr == NULL) {
129*2830Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
130*2830Sdjl 			goto result_printers2str;
1310Sstevel@tonic-gate 		}
1320Sstevel@tonic-gate 		/*
1330Sstevel@tonic-gate 		 * The attribute contains key=value
1340Sstevel@tonic-gate 		 */
1350Sstevel@tonic-gate 		if (strcasecmp(attr->attrname, "sun-printer-kvp") == 0) {
1360Sstevel@tonic-gate 			for (j = 0; j < attr->value_count; j++) {
1370Sstevel@tonic-gate 				len = strlen(attr->attrvalue[j]);
1380Sstevel@tonic-gate 				if (len < 1 ||
1390Sstevel@tonic-gate 				    (attr->attrvalue[j] == '\0')) {
1400Sstevel@tonic-gate 					*buffer = 0;
1410Sstevel@tonic-gate 					nss_result = (int)NSS_STR_PARSE_PARSE;
142*2830Sdjl 					goto result_printers2str;
1430Sstevel@tonic-gate 				}
144*2830Sdjl 				len =  snprintf(buffer, buflen, ":%s",
145*2830Sdjl 						attr->attrvalue[j]);
146*2830Sdjl 				TEST_AND_ADJUST(len, buffer, buflen,
147*2830Sdjl 						result_printers2str);
1480Sstevel@tonic-gate 			}
1490Sstevel@tonic-gate 		} else {
1500Sstevel@tonic-gate 			/*
151*2830Sdjl 			 * Skip some attr names
1520Sstevel@tonic-gate 			 */
153*2830Sdjl #ifdef	NSS_LDAP_PRINTERS
154*2830Sdjl 			if (strcasecmp(attr->attrname, "printer-name") == 0 ||
155*2830Sdjl 				strcasecmp(attr->attrname, "dn") == 0 ||
156*2830Sdjl 				strcasecmp(attr->attrname,
157*2830Sdjl 					"objectclass") == 0 ||
158*2830Sdjl 				strcasecmp(attr->attrname,
159*2830Sdjl 					"printer-uri") == 0 ||
160*2830Sdjl 				strcasecmp(attr->attrname,
161*2830Sdjl 					"printer-aliases") == 0)
162*2830Sdjl #else
163*2830Sdjl 			if (strcasecmp(attr->attrname, "printer-name") == 0)
164*2830Sdjl #endif
1650Sstevel@tonic-gate 				continue;
1660Sstevel@tonic-gate 			}
1670Sstevel@tonic-gate 			/*
168*2830Sdjl 			 * Translate attr name ->key name
1690Sstevel@tonic-gate 			 */
170*2830Sdjl 			if (strcmp(attr->attrname, "sun-printer-bsdaddr")
171*2830Sdjl 					== 0)
172*2830Sdjl 				attrname = "bsdaddr";
173*2830Sdjl #ifdef	NSS_LDAP_PRINTERS
174*2830Sdjl 			else if (strcmp(attr->attrname, "printer-info")
175*2830Sdjl 					== 0)
176*2830Sdjl 				attrname = "description";
177*2830Sdjl 			else if (strcmp(attr->attrname, "sunwpr-support")
178*2830Sdjl 					== 0)
179*2830Sdjl 				attrname = "itopssupported";
180*2830Sdjl 			else if (strncmp(attr->attrname, PRINTER_PREFIX, plen)
181*2830Sdjl 					== 0)
182*2830Sdjl 				attrname = attr->attrname + plen;
183*2830Sdjl 			else if (strncmp(attr->attrname, SUNWPR_PREFIX, slen)
184*2830Sdjl 					== 0)
185*2830Sdjl 				attrname = attr->attrname + slen;
186*2830Sdjl #endif
187*2830Sdjl 			else
188*2830Sdjl 				attrname = attr->attrname;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 			/*
191*2830Sdjl 			 * The attrname is the key. The attribute
1920Sstevel@tonic-gate 			 * data is the value.
1930Sstevel@tonic-gate 			 */
194*2830Sdjl 			len = snprintf(buffer, buflen, ":%s=", attrname);
195*2830Sdjl 			TEST_AND_ADJUST(len, buffer, buflen,
196*2830Sdjl 					result_printers2str);
197*2830Sdjl 
1980Sstevel@tonic-gate 			for (j = 0; j < attr->value_count; j++) {
1990Sstevel@tonic-gate 				int k;
2000Sstevel@tonic-gate 				char *kp;
2010Sstevel@tonic-gate 
202*2830Sdjl 				if (attr->attrvalue[j] == NULL) {
203*2830Sdjl 					*buffer = 0;
204*2830Sdjl 					nss_result = NSS_STR_PARSE_PARSE;
205*2830Sdjl 					goto result_printers2str;
206*2830Sdjl 				}
2070Sstevel@tonic-gate 				len = strlen(attr->attrvalue[j]);
208*2830Sdjl 				if (len < 1) {
2090Sstevel@tonic-gate 					*buffer = 0;
210*2830Sdjl 					nss_result = NSS_STR_PARSE_PARSE;
211*2830Sdjl 					goto result_printers2str;
2120Sstevel@tonic-gate 				}
2130Sstevel@tonic-gate 				/*
2140Sstevel@tonic-gate 				 * Add extra for any colons which need to
2150Sstevel@tonic-gate 				 * be backslashed plus ending ':' or ','.
2160Sstevel@tonic-gate 				 */
2170Sstevel@tonic-gate 				k = 0;
2180Sstevel@tonic-gate 				for (kp = attr->attrvalue[j]; *kp != NULL; kp++)
2190Sstevel@tonic-gate 					if (*kp == ':')
220*2830Sdjl 						/* count ':' in value */
2210Sstevel@tonic-gate 						k++;
222*2830Sdjl 				if (j == 0)
223*2830Sdjl 					/* first time */
224*2830Sdjl 					len += k;
225*2830Sdjl 				else
226*2830Sdjl 					/* add ',' */
227*2830Sdjl 					len += k + 1;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 				if (len > buflen) {
230*2830Sdjl 					nss_result = NSS_STR_PARSE_ERANGE;
231*2830Sdjl 					goto result_printers2str;
2320Sstevel@tonic-gate 				}
233*2830Sdjl 				if (j > 0)
234*2830Sdjl 					*buffer++ = ',';
235*2830Sdjl 
236*2830Sdjl 				(void) append_attr(buffer,
2370Sstevel@tonic-gate 					    attr->attrvalue[j]);
238*2830Sdjl 				buffer += strlen(attr->attrvalue[j]) + k;
239*2830Sdjl 				buflen -= len;
2400Sstevel@tonic-gate 			}
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 
243*2830Sdjl 	if (argp->buf.result != NULL)
244*2830Sdjl 		be->buflen = strlen(be->buffer);
2450Sstevel@tonic-gate 
246*2830Sdjl result_printers2str:
2470Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
2480Sstevel@tonic-gate 	return ((int)nss_result);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate /*
2520Sstevel@tonic-gate  * Attributes which contain colons must be backslashed.
2530Sstevel@tonic-gate  */
2540Sstevel@tonic-gate static void
append_attr(char * buf,char * attr)2550Sstevel@tonic-gate append_attr(char *buf, char *attr)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate 	char *cp, *bp;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	if (strchr(attr, ':') == NULL) {
2600Sstevel@tonic-gate 		(void) strcat(buf, attr);
2610Sstevel@tonic-gate 		return;
2620Sstevel@tonic-gate 	}
263*2830Sdjl 	bp = buf;
2640Sstevel@tonic-gate 	cp = attr;
2650Sstevel@tonic-gate 	while (*cp != NULL) {
2660Sstevel@tonic-gate 		if (*cp == ':') {
2670Sstevel@tonic-gate 			*bp++ = '\\';
2680Sstevel@tonic-gate 		}
2690Sstevel@tonic-gate 		*bp++ = *cp++;
2700Sstevel@tonic-gate 	}
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate  * getbyname gets printer attributes by printer name. This function
2750Sstevel@tonic-gate  * constructs an ldap search filter using the printer name invocation
2760Sstevel@tonic-gate  * parameter and the getprinterbyname search filter defined. Once the
2770Sstevel@tonic-gate  * filter is constructed, we search for matching entries and marshal
2780Sstevel@tonic-gate  * the data results into argp->buf.buffer for the frontend process.
279*2830Sdjl  * The function _nss_ldap_printers2str performs the data marshaling.
2800Sstevel@tonic-gate  */
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate static nss_status_t
getbyname(ldap_backend_ptr be,void * a)2830Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate 	char		printername[BUFSIZ];
2860Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
2870Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	(void) strncpy(printername, argp->key.name, BUFSIZ);
2900Sstevel@tonic-gate 	if (snprintf(searchfilter, SEARCHFILTERLEN,
2910Sstevel@tonic-gate 		_F_GETPRINTERBYNAME, printername, printername) < 0)
2920Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
2950Sstevel@tonic-gate 		_PRINTERS, searchfilter, NULL, NULL, NULL));
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate static ldap_backend_op_t printers_ops[] = {
2990Sstevel@tonic-gate 	_nss_ldap_destr,
3000Sstevel@tonic-gate 	_nss_ldap_endent,
3010Sstevel@tonic-gate 	_nss_ldap_setent,
3020Sstevel@tonic-gate 	_nss_ldap_getent,
3030Sstevel@tonic-gate 	getbyname,
3040Sstevel@tonic-gate };
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate /*
3080Sstevel@tonic-gate  * _nss_ldap_printers_constr is where life begins. This function calls
3090Sstevel@tonic-gate  * the generic ldap constructor function to define and build the abstract
3100Sstevel@tonic-gate  * data types required to support ldap operations.
3110Sstevel@tonic-gate  */
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate /*ARGSUSED0*/
3140Sstevel@tonic-gate nss_backend_t *
_nss_ldap_printers_constr(const char * dummy1,const char * dummy2,const char * dummy3)3150Sstevel@tonic-gate _nss_ldap_printers_constr(const char *dummy1, const char *dummy2,
3160Sstevel@tonic-gate 			const char *dummy3)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(printers_ops,
3200Sstevel@tonic-gate 		sizeof (printers_ops)/sizeof (printers_ops[0]), _PRINTERS,
321*2830Sdjl 		printer_attrs, _nss_ldap_printers2str));
3220Sstevel@tonic-gate }
323