summaryrefslogtreecommitdiff
path: root/usr/src/lib/nsswitch/ldap/common/getprinter.c
blob: c24bb673206644f6ae98afdbcc5ad8c32a53b26f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma weak _nss_ldap__printers_constr = _nss_ldap_printers_constr

#include "ldap_common.h"

static void append_attr(char *buf, char *attr);

/* printer attributes filters */
#define	_F_GETPRINTERBYNAME	\
	"(&(objectClass=sunPrinter)(|(printer-name=%s)(printer-aliases=%s)))"

#define	PRINTER_PREFIX	"printer-"
#define	SUNWPR_PREFIX	"sunwpr-"

/*
 * Attributes from the following classes:
 * 	printerService
 * 	printerAbstact
 * 	sunPrinter
 */

/*
 * Get all attributes.
 */
static const char **printer_attrs = NULL;


/*
 * _nss_ldap_printers2str is the data marshaling method for the printers
 * getXbyY backend processes. This method is called after a successful
 * ldap search has been performed. This method will parse the ldap search
 * values into argp->buf.buffer. Three error conditions are expected and
 * returned to nsswitch.
 * In order to be compatible with old data output, the code is commented out
 * with NSS_LDAP_PRINTERS. The NSS_LDAP_PRINTERS section is for future
 * refrences if it's decided to fix the output format.
 */

static int
_nss_ldap_printers2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
{
	int			i, j;
	int			nss_result;
	int			buflen = 0, len;
	char			*buffer = NULL;
	char			**name, *attrname;
	ns_ldap_attr_t		*attr;
	ns_ldap_result_t	*result = be->result;
#ifdef	NSS_LDAP_PRINTERS
	int			slen, plen;
#endif

	if (result == NULL)
		return (NSS_STR_PARSE_PARSE);

	buflen = argp->buf.buflen;
	if (argp->buf.result != NULL) {
		be->buffer = calloc(1, buflen);
		if (be->buffer == NULL)
			return (NSS_STR_PARSE_PARSE);
		be->buflen = buflen;
		buffer = be->buffer;
	} else {
		buffer = argp->buf.buffer;
		(void) memset(argp->buf.buffer, 0, buflen);
	}

	nss_result = NSS_STR_PARSE_SUCCESS;

#ifdef	NSS_LDAP_PRINTERS
	slen = strlen(SUNWPR_PREFIX);
	plen = strlen(PRINTER_PREFIX);
#endif

	/*
	 * Pick out the printer name and aliases
	 */
	name = __ns_ldap_getAttr(result->entry, "printer-name");
	if (name == NULL || name[0] == NULL) {
		nss_result = NSS_STR_PARSE_PARSE;
		goto result_printers2str;
	}
	len = snprintf(buffer, buflen, "%s", name[0]);
	TEST_AND_ADJUST(len, buffer, buflen, result_printers2str);

#ifdef	NSS_LDAP_PRINTERS
	attr = __ns_ldap_getAttrStruct(result->entry, "printer-aliases");
	if (attr != NULL && attr->attrvalue != NULL) {
		for (i = 0; i < attr->value_count; i++) {
			len = snprintf(buffer, buflen, "|%s",
					attr->attrvalue[i]);
			TEST_AND_ADJUST(len, buffer, buflen,
					result_printers2str);
		}
	}
#endif
	/*
	 * Add the rest of the attributes
	 */
	for (i = 0; i < result->entry->attr_count; i++) {
		attr = getattr(result, i);
		if (attr == NULL) {
			nss_result = NSS_STR_PARSE_PARSE;
			goto result_printers2str;
		}
		/*
		 * The attribute contains key=value
		 */
		if (strcasecmp(attr->attrname, "sun-printer-kvp") == 0) {
			for (j = 0; j < attr->value_count; j++) {
				len = strlen(attr->attrvalue[j]);
				if (len < 1 ) {
					*buffer = '\0';
					nss_result = (int)NSS_STR_PARSE_PARSE;
					goto result_printers2str;
				}
				len =  snprintf(buffer, buflen, ":%s",
						attr->attrvalue[j]);
				TEST_AND_ADJUST(len, buffer, buflen,
						result_printers2str);
			}
		} else {
			/*
			 * Skip some attr names
			 */
#ifdef	NSS_LDAP_PRINTERS
			if (strcasecmp(attr->attrname, "printer-name") == 0 ||
				strcasecmp(attr->attrname, "dn") == 0 ||
				strcasecmp(attr->attrname,
					"objectclass") == 0 ||
				strcasecmp(attr->attrname,
					"printer-uri") == 0 ||
				strcasecmp(attr->attrname,
					"printer-aliases") == 0)
#else
			if (strcasecmp(attr->attrname, "printer-name") == 0)
#endif
				continue;
			}
			/*
			 * Translate attr name ->key name
			 */
			if (strcmp(attr->attrname, "sun-printer-bsdaddr")
					== 0)
				attrname = "bsdaddr";
#ifdef	NSS_LDAP_PRINTERS
			else if (strcmp(attr->attrname, "printer-info")
					== 0)
				attrname = "description";
			else if (strcmp(attr->attrname, "sunwpr-support")
					== 0)
				attrname = "itopssupported";
			else if (strncmp(attr->attrname, PRINTER_PREFIX, plen)
					== 0)
				attrname = attr->attrname + plen;
			else if (strncmp(attr->attrname, SUNWPR_PREFIX, slen)
					== 0)
				attrname = attr->attrname + slen;
#endif
			else
				attrname = attr->attrname;

			/*
			 * The attrname is the key. The attribute
			 * data is the value.
			 */
			len = snprintf(buffer, buflen, ":%s=", attrname);
			TEST_AND_ADJUST(len, buffer, buflen,
					result_printers2str);

			for (j = 0; j < attr->value_count; j++) {
				int k;
				char *kp;

				if (attr->attrvalue[j] == NULL) {
					*buffer = 0;
					nss_result = NSS_STR_PARSE_PARSE;
					goto result_printers2str;
				}
				len = strlen(attr->attrvalue[j]);
				if (len < 1) {
					*buffer = 0;
					nss_result = NSS_STR_PARSE_PARSE;
					goto result_printers2str;
				}
				/*
				 * Add extra for any colons which need to
				 * be backslashed plus ending ':' or ','.
				 */
				k = 0;
				for (kp = attr->attrvalue[j]; *kp != '\0'; kp++)
					if (*kp == ':')
						/* count ':' in value */
						k++;
				if (j == 0)
					/* first time */
					len += k;
				else
					/* add ',' */
					len += k + 1;

				if (len > buflen) {
					nss_result = NSS_STR_PARSE_ERANGE;
					goto result_printers2str;
				}
				if (j > 0)
					*buffer++ = ',';

				(void) append_attr(buffer,
					    attr->attrvalue[j]);
				buffer += strlen(attr->attrvalue[j]) + k;
				buflen -= len;
			}
	}

	if (argp->buf.result != NULL)
		be->buflen = strlen(be->buffer);

result_printers2str:
	(void) __ns_ldap_freeResult(&be->result);
	return ((int)nss_result);
}

/*
 * Attributes which contain colons must be backslashed.
 */
static void
append_attr(char *buf, char *attr)
{
	char *cp, *bp;

	if (strchr(attr, ':') == NULL) {
		(void) strcat(buf, attr);
		return;
	}
	bp = buf;
	cp = attr;
	while (*cp != '\0') {
		if (*cp == ':') {
			*bp++ = '\\';
		}
		*bp++ = *cp++;
	}
}

/*
 * getbyname gets printer attributes by printer name. This function
 * constructs an ldap search filter using the printer name invocation
 * parameter and the getprinterbyname search filter defined. Once the
 * filter is constructed, we search for matching entries and marshal
 * the data results into argp->buf.buffer for the frontend process.
 * The function _nss_ldap_printers2str performs the data marshaling.
 */

static nss_status_t
getbyname(ldap_backend_ptr be, void *a)
{
	char		printername[BUFSIZ];
	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
	char		searchfilter[SEARCHFILTERLEN];

	(void) strncpy(printername, argp->key.name, BUFSIZ);
	if (snprintf(searchfilter, SEARCHFILTERLEN,
		_F_GETPRINTERBYNAME, printername, printername) < 0)
		return ((nss_status_t)NSS_NOTFOUND);

	return ((nss_status_t)_nss_ldap_lookup(be, argp,
		_PRINTERS, searchfilter, NULL, NULL, NULL));
}

static ldap_backend_op_t printers_ops[] = {
	_nss_ldap_destr,
	_nss_ldap_endent,
	_nss_ldap_setent,
	_nss_ldap_getent,
	getbyname,
};


/*
 * _nss_ldap_printers_constr is where life begins. This function calls
 * the generic ldap constructor function to define and build the abstract
 * data types required to support ldap operations.
 */

/*ARGSUSED0*/
nss_backend_t *
_nss_ldap_printers_constr(const char *dummy1, const char *dummy2,
			const char *dummy3)
{

	return ((nss_backend_t *)_nss_ldap_constr(printers_ops,
		sizeof (printers_ops)/sizeof (printers_ops[0]), _PRINTERS,
		printer_attrs, _nss_ldap_printers2str));
}