1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #pragma weak _nss_ldap__printers_constr = _nss_ldap_printers_constr
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include "ldap_common.h"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate static void append_attr(char *buf, char *attr);
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate /* printer attributes filters */
36*0Sstevel@tonic-gate #define	_F_GETPRINTERBYNAME	\
37*0Sstevel@tonic-gate 	"(&(objectClass=sunPrinter)(|(printer-name=%s)(printer-aliases=%s)))"
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate /*
40*0Sstevel@tonic-gate  * Attributes from the following classes:
41*0Sstevel@tonic-gate  * 	printerService
42*0Sstevel@tonic-gate  * 	printerAbstact
43*0Sstevel@tonic-gate  * 	sunPrinter
44*0Sstevel@tonic-gate  */
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate /*
47*0Sstevel@tonic-gate  * Get all attributes.
48*0Sstevel@tonic-gate  */
49*0Sstevel@tonic-gate static const char **printer_attrs = NULL;
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate  * _nss_ldap_printers2ent is the data marshaling method for the printers
54*0Sstevel@tonic-gate  * getXbyY backend processes. This method is called after a successful
55*0Sstevel@tonic-gate  * ldap search has been performed. This method will parse the ldap search
56*0Sstevel@tonic-gate  * values into argp->buf.buffer. Three error conditions are expected and
57*0Sstevel@tonic-gate  * returned to nsswitch.
58*0Sstevel@tonic-gate  */
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate static int
61*0Sstevel@tonic-gate _nss_ldap_printers2ent(ldap_backend_ptr be, nss_XbyY_args_t *argp)
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate 	int			i, j;
64*0Sstevel@tonic-gate 	int			nss_result;
65*0Sstevel@tonic-gate 	int			buflen = (int)0;
66*0Sstevel@tonic-gate 	unsigned long		len = 0L;
67*0Sstevel@tonic-gate 	char			*cp = (char *)NULL;
68*0Sstevel@tonic-gate 	char			*buffer = (char *)NULL;
69*0Sstevel@tonic-gate 	ns_ldap_attr_t		*attr;
70*0Sstevel@tonic-gate 	ns_ldap_result_t	*result = be->result;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	buffer = argp->buf.buffer;
73*0Sstevel@tonic-gate 	buflen = (size_t)argp->buf.buflen;
74*0Sstevel@tonic-gate 	if (!argp->buf.result) {
75*0Sstevel@tonic-gate 		nss_result = (int)NSS_STR_PARSE_ERANGE;
76*0Sstevel@tonic-gate 		goto result_printers2ent;
77*0Sstevel@tonic-gate 	}
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate 	nss_result = (int)NSS_STR_PARSE_SUCCESS;
80*0Sstevel@tonic-gate 	(void) memset(argp->buf.buffer, 0, buflen);
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 	/* Make sure our buffer stays NULL terminated */
83*0Sstevel@tonic-gate 	buflen--;
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	attr = getattr(result, 0);
86*0Sstevel@tonic-gate 	if (attr == NULL) {
87*0Sstevel@tonic-gate 		nss_result = (int)NSS_STR_PARSE_PARSE;
88*0Sstevel@tonic-gate 		goto result_printers2ent;
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	/*
92*0Sstevel@tonic-gate 	 * Pick out the printer name.
93*0Sstevel@tonic-gate 	 */
94*0Sstevel@tonic-gate 	for (i = 0; i < result->entry->attr_count; i++) {
95*0Sstevel@tonic-gate 		attr = getattr(result, i);
96*0Sstevel@tonic-gate 		if (attr == NULL) {
97*0Sstevel@tonic-gate 			nss_result = (int)NSS_STR_PARSE_PARSE;
98*0Sstevel@tonic-gate 			goto result_printers2ent;
99*0Sstevel@tonic-gate 		}
100*0Sstevel@tonic-gate 		if (strcasecmp(attr->attrname, "printer-name") == 0) {
101*0Sstevel@tonic-gate 			len = strlen(attr->attrvalue[0]);
102*0Sstevel@tonic-gate 			if (len < 1 || (attr->attrvalue[0] == '\0')) {
103*0Sstevel@tonic-gate 				*buffer = 0;
104*0Sstevel@tonic-gate 				nss_result = (int)NSS_STR_PARSE_PARSE;
105*0Sstevel@tonic-gate 				goto result_printers2ent;
106*0Sstevel@tonic-gate 			}
107*0Sstevel@tonic-gate 			if (len > buflen) {
108*0Sstevel@tonic-gate 				nss_result = (int)NSS_STR_PARSE_ERANGE;
109*0Sstevel@tonic-gate 				goto result_printers2ent;
110*0Sstevel@tonic-gate 			}
111*0Sstevel@tonic-gate 			(void) strcpy(buffer, attr->attrvalue[0]);
112*0Sstevel@tonic-gate 		}
113*0Sstevel@tonic-gate 	}
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	/*
116*0Sstevel@tonic-gate 	 * Should never happen since it is mandatory but bail if
117*0Sstevel@tonic-gate 	 * we don't have a printer name.
118*0Sstevel@tonic-gate 	 */
119*0Sstevel@tonic-gate 	if (buffer[0] == NULL) {
120*0Sstevel@tonic-gate 		nss_result = (int)NSS_STR_PARSE_PARSE;
121*0Sstevel@tonic-gate 		goto result_printers2ent;
122*0Sstevel@tonic-gate 	}
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	/*
125*0Sstevel@tonic-gate 	 * Add the rest of the attributes
126*0Sstevel@tonic-gate 	 */
127*0Sstevel@tonic-gate 	for (i = 0; i < result->entry->attr_count; i++) {
128*0Sstevel@tonic-gate 		attr = getattr(result, i);
129*0Sstevel@tonic-gate 		if (attr == NULL) {
130*0Sstevel@tonic-gate 			nss_result = (int)NSS_STR_PARSE_PARSE;
131*0Sstevel@tonic-gate 			goto result_printers2ent;
132*0Sstevel@tonic-gate 		}
133*0Sstevel@tonic-gate 		/*
134*0Sstevel@tonic-gate 		 * The attribute contains key=value
135*0Sstevel@tonic-gate 		 */
136*0Sstevel@tonic-gate 		if (strcasecmp(attr->attrname, "sun-printer-kvp") == 0) {
137*0Sstevel@tonic-gate 			for (j = 0; j < attr->value_count; j++) {
138*0Sstevel@tonic-gate 				len = strlen(attr->attrvalue[j]);
139*0Sstevel@tonic-gate 				if (len < 1 ||
140*0Sstevel@tonic-gate 				    (attr->attrvalue[j] == '\0')) {
141*0Sstevel@tonic-gate 					*buffer = 0;
142*0Sstevel@tonic-gate 					nss_result = (int)NSS_STR_PARSE_PARSE;
143*0Sstevel@tonic-gate 					goto result_printers2ent;
144*0Sstevel@tonic-gate 				}
145*0Sstevel@tonic-gate 				len += strlen(buffer) + 1;	/* 1 for ':' */
146*0Sstevel@tonic-gate 				if (len > buflen) {
147*0Sstevel@tonic-gate 					nss_result = (int)NSS_STR_PARSE_ERANGE;
148*0Sstevel@tonic-gate 					goto result_printers2ent;
149*0Sstevel@tonic-gate 				}
150*0Sstevel@tonic-gate 				if ((cp = strrchr(buffer, '\0')) != NULL) {
151*0Sstevel@tonic-gate 						*cp = ':';
152*0Sstevel@tonic-gate 					(void) strcat(buffer,
153*0Sstevel@tonic-gate 					    attr->attrvalue[j]);
154*0Sstevel@tonic-gate 				}
155*0Sstevel@tonic-gate 			}
156*0Sstevel@tonic-gate 		} else {
157*0Sstevel@tonic-gate 			/*
158*0Sstevel@tonic-gate 			 * Skip the printer name
159*0Sstevel@tonic-gate 			 */
160*0Sstevel@tonic-gate 			if (strcmp(attr->attrname, "printer-name") == 0) {
161*0Sstevel@tonic-gate 				continue;
162*0Sstevel@tonic-gate 			}
163*0Sstevel@tonic-gate 			/*
164*0Sstevel@tonic-gate 			 * Translate sun-printer-bsdaddr -> bsdaddr
165*0Sstevel@tonic-gate 			 */
166*0Sstevel@tonic-gate 			if (strcmp(attr->attrname, "sun-printer-bsdaddr") ==
167*0Sstevel@tonic-gate 									0) {
168*0Sstevel@tonic-gate 				if (attr->attrname != NULL) {
169*0Sstevel@tonic-gate 					free(attr->attrname);
170*0Sstevel@tonic-gate 				}
171*0Sstevel@tonic-gate 				attr->attrname = strdup("bsdaddr");
172*0Sstevel@tonic-gate 			}
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 			/*
175*0Sstevel@tonic-gate 			 * The attribute name is the key. The attribute
176*0Sstevel@tonic-gate 			 * data is the value.
177*0Sstevel@tonic-gate 			 */
178*0Sstevel@tonic-gate 			for (j = 0; j < attr->value_count; j++) {
179*0Sstevel@tonic-gate 				int k;
180*0Sstevel@tonic-gate 				char *kp;
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 				len = strlen(attr->attrvalue[j]);
183*0Sstevel@tonic-gate 				if (len < 1 ||
184*0Sstevel@tonic-gate 				    (attr->attrvalue[j] == '\0')) {
185*0Sstevel@tonic-gate 					*buffer = 0;
186*0Sstevel@tonic-gate 					nss_result = (int)NSS_STR_PARSE_PARSE;
187*0Sstevel@tonic-gate 					goto result_printers2ent;
188*0Sstevel@tonic-gate 				}
189*0Sstevel@tonic-gate 				/*
190*0Sstevel@tonic-gate 				 * Add extra for any colons which need to
191*0Sstevel@tonic-gate 				 * be backslashed plus ending ':' or ','.
192*0Sstevel@tonic-gate 				 */
193*0Sstevel@tonic-gate 				k = 0;
194*0Sstevel@tonic-gate 				for (kp = attr->attrvalue[j]; *kp != NULL; kp++)
195*0Sstevel@tonic-gate 					if (*kp == ':')
196*0Sstevel@tonic-gate 						k++;
197*0Sstevel@tonic-gate 				len += strlen(buffer) + k;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 				if (j == 0) {
200*0Sstevel@tonic-gate 					len += strlen(attr->attrname) + 1;
201*0Sstevel@tonic-gate 				}
202*0Sstevel@tonic-gate 				if (len > buflen) {
203*0Sstevel@tonic-gate 					nss_result = (int)NSS_STR_PARSE_ERANGE;
204*0Sstevel@tonic-gate 					goto result_printers2ent;
205*0Sstevel@tonic-gate 				}
206*0Sstevel@tonic-gate 				if ((cp = strrchr(buffer, '\0')) != NULL) {
207*0Sstevel@tonic-gate 					if (j == 0) {
208*0Sstevel@tonic-gate 						*cp = ':';
209*0Sstevel@tonic-gate 						(void) strcat(buffer,
210*0Sstevel@tonic-gate 						    attr->attrname);
211*0Sstevel@tonic-gate 						(void) strcat(buffer, "=");
212*0Sstevel@tonic-gate 					} else {
213*0Sstevel@tonic-gate 						*cp = ',';
214*0Sstevel@tonic-gate 					}
215*0Sstevel@tonic-gate 					(void) append_attr(buffer,
216*0Sstevel@tonic-gate 					    attr->attrvalue[j]);
217*0Sstevel@tonic-gate 				}
218*0Sstevel@tonic-gate 			}
219*0Sstevel@tonic-gate 		}
220*0Sstevel@tonic-gate 	}
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate #ifdef DEBUG
223*0Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[getprinter.c: _nss_ldap_printers2ent]\n");
224*0Sstevel@tonic-gate 	(void) fprintf(stdout, " printers: [%s]\n", buffer);
225*0Sstevel@tonic-gate #endif
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate result_printers2ent:
228*0Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
229*0Sstevel@tonic-gate 	return ((int)nss_result);
230*0Sstevel@tonic-gate }
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate /*
233*0Sstevel@tonic-gate  * Attributes which contain colons must be backslashed.
234*0Sstevel@tonic-gate  */
235*0Sstevel@tonic-gate static void
236*0Sstevel@tonic-gate append_attr(char *buf, char *attr)
237*0Sstevel@tonic-gate {
238*0Sstevel@tonic-gate 	char *cp, *bp;
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 	if (strchr(attr, ':') == NULL) {
241*0Sstevel@tonic-gate 		(void) strcat(buf, attr);
242*0Sstevel@tonic-gate 		return;
243*0Sstevel@tonic-gate 	}
244*0Sstevel@tonic-gate 	bp = buf + strlen(buf);
245*0Sstevel@tonic-gate 	cp = attr;
246*0Sstevel@tonic-gate 	while (*cp != NULL) {
247*0Sstevel@tonic-gate 		if (*cp == ':') {
248*0Sstevel@tonic-gate 			*bp++ = '\\';
249*0Sstevel@tonic-gate 		}
250*0Sstevel@tonic-gate 		*bp++ = *cp++;
251*0Sstevel@tonic-gate 	}
252*0Sstevel@tonic-gate }
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate /*
255*0Sstevel@tonic-gate  * getbyname gets printer attributes by printer name. This function
256*0Sstevel@tonic-gate  * constructs an ldap search filter using the printer name invocation
257*0Sstevel@tonic-gate  * parameter and the getprinterbyname search filter defined. Once the
258*0Sstevel@tonic-gate  * filter is constructed, we search for matching entries and marshal
259*0Sstevel@tonic-gate  * the data results into argp->buf.buffer for the frontend process.
260*0Sstevel@tonic-gate  * The function * _nss_ldap_printers2ent performs the data marshaling.
261*0Sstevel@tonic-gate  */
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate static nss_status_t
264*0Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
265*0Sstevel@tonic-gate {
266*0Sstevel@tonic-gate 	char		printername[BUFSIZ];
267*0Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
268*0Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	(void) strncpy(printername, argp->key.name, BUFSIZ);
271*0Sstevel@tonic-gate 	if (snprintf(searchfilter, SEARCHFILTERLEN,
272*0Sstevel@tonic-gate 		_F_GETPRINTERBYNAME, printername, printername) < 0)
273*0Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
276*0Sstevel@tonic-gate 		_PRINTERS, searchfilter, NULL, NULL, NULL));
277*0Sstevel@tonic-gate }
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate static ldap_backend_op_t printers_ops[] = {
280*0Sstevel@tonic-gate 	_nss_ldap_destr,
281*0Sstevel@tonic-gate 	_nss_ldap_endent,
282*0Sstevel@tonic-gate 	_nss_ldap_setent,
283*0Sstevel@tonic-gate 	_nss_ldap_getent,
284*0Sstevel@tonic-gate 	getbyname,
285*0Sstevel@tonic-gate };
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate /*
289*0Sstevel@tonic-gate  * _nss_ldap_printers_constr is where life begins. This function calls
290*0Sstevel@tonic-gate  * the generic ldap constructor function to define and build the abstract
291*0Sstevel@tonic-gate  * data types required to support ldap operations.
292*0Sstevel@tonic-gate  */
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate /*ARGSUSED0*/
295*0Sstevel@tonic-gate nss_backend_t *
296*0Sstevel@tonic-gate _nss_ldap_printers_constr(const char *dummy1, const char *dummy2,
297*0Sstevel@tonic-gate 			const char *dummy3)
298*0Sstevel@tonic-gate {
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate #ifdef DEBUG
301*0Sstevel@tonic-gate 	(void) fprintf(stdout,
302*0Sstevel@tonic-gate 		"\n[getprinterent.c: _nss_ldap_printers_constr]\n");
303*0Sstevel@tonic-gate #endif
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(printers_ops,
306*0Sstevel@tonic-gate 		sizeof (printers_ops)/sizeof (printers_ops[0]), _PRINTERS,
307*0Sstevel@tonic-gate 		printer_attrs, _nss_ldap_printers2ent));
308*0Sstevel@tonic-gate }
309