4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma weak _nss_ldap__printers_constr = _nss_ldap_printers_constr
28 #include "ldap_common.h"
30 static void append_attr(char *buf
, char *attr
);
32 /* printer attributes filters */
33 #define _F_GETPRINTERBYNAME \
34 "(&(objectClass=sunPrinter)(|(printer-name=%s)(printer-aliases=%s)))"
36 #define PRINTER_PREFIX "printer-"
37 #define SUNWPR_PREFIX "sunwpr-"
40 * Attributes from the following classes:
49 static const char **printer_attrs
= NULL
;
53 * _nss_ldap_printers2str is the data marshaling method for the printers
54 * getXbyY backend processes. This method is called after a successful
55 * ldap search has been performed. This method will parse the ldap search
56 * values into argp->buf.buffer. Three error conditions are expected and
57 * returned to nsswitch.
58 * In order to be compatible with old data output, the code is commented out
59 * with NSS_LDAP_PRINTERS. The NSS_LDAP_PRINTERS section is for future
60 * refrences if it's decided to fix the output format.
64 _nss_ldap_printers2str(ldap_backend_ptr be
, nss_XbyY_args_t
*argp
)
70 char **name
, *attrname
;
72 ns_ldap_result_t
*result
= be
->result
;
73 #ifdef NSS_LDAP_PRINTERS
78 return (NSS_STR_PARSE_PARSE
);
80 buflen
= argp
->buf
.buflen
;
81 if (argp
->buf
.result
!= NULL
) {
82 be
->buffer
= calloc(1, buflen
);
83 if (be
->buffer
== NULL
)
84 return (NSS_STR_PARSE_PARSE
);
88 buffer
= argp
->buf
.buffer
;
89 (void) memset(argp
->buf
.buffer
, 0, buflen
);
92 nss_result
= NSS_STR_PARSE_SUCCESS
;
94 #ifdef NSS_LDAP_PRINTERS
95 slen
= strlen(SUNWPR_PREFIX
);
96 plen
= strlen(PRINTER_PREFIX
);
100 * Pick out the printer name and aliases
102 name
= __ns_ldap_getAttr(result
->entry
, "printer-name");
103 if (name
== NULL
|| name
[0] == NULL
) {
104 nss_result
= NSS_STR_PARSE_PARSE
;
105 goto result_printers2str
;
107 len
= snprintf(buffer
, buflen
, "%s", name
[0]);
108 TEST_AND_ADJUST(len
, buffer
, buflen
, result_printers2str
);
110 #ifdef NSS_LDAP_PRINTERS
111 attr
= __ns_ldap_getAttrStruct(result
->entry
, "printer-aliases");
112 if (attr
!= NULL
&& attr
->attrvalue
!= NULL
) {
113 for (i
= 0; i
< attr
->value_count
; i
++) {
114 len
= snprintf(buffer
, buflen
, "|%s",
116 TEST_AND_ADJUST(len
, buffer
, buflen
,
117 result_printers2str
);
122 * Add the rest of the attributes
124 for (i
= 0; i
< result
->entry
->attr_count
; i
++) {
125 attr
= getattr(result
, i
);
127 nss_result
= NSS_STR_PARSE_PARSE
;
128 goto result_printers2str
;
131 * The attribute contains key=value
133 if (strcasecmp(attr
->attrname
, "sun-printer-kvp") == 0) {
134 for (j
= 0; j
< attr
->value_count
; j
++) {
135 len
= strlen(attr
->attrvalue
[j
]);
138 nss_result
= (int)NSS_STR_PARSE_PARSE
;
139 goto result_printers2str
;
141 len
= snprintf(buffer
, buflen
, ":%s",
143 TEST_AND_ADJUST(len
, buffer
, buflen
,
144 result_printers2str
);
148 * Skip some attr names
150 #ifdef NSS_LDAP_PRINTERS
151 if (strcasecmp(attr
->attrname
, "printer-name") == 0 ||
152 strcasecmp(attr
->attrname
, "dn") == 0 ||
153 strcasecmp(attr
->attrname
,
154 "objectclass") == 0 ||
155 strcasecmp(attr
->attrname
,
156 "printer-uri") == 0 ||
157 strcasecmp(attr
->attrname
,
158 "printer-aliases") == 0)
160 if (strcasecmp(attr
->attrname
, "printer-name") == 0)
165 * Translate attr name ->key name
167 if (strcmp(attr
->attrname
, "sun-printer-bsdaddr")
169 attrname
= "bsdaddr";
170 #ifdef NSS_LDAP_PRINTERS
171 else if (strcmp(attr
->attrname
, "printer-info")
173 attrname
= "description";
174 else if (strcmp(attr
->attrname
, "sunwpr-support")
176 attrname
= "itopssupported";
177 else if (strncmp(attr
->attrname
, PRINTER_PREFIX
, plen
)
179 attrname
= attr
->attrname
+ plen
;
180 else if (strncmp(attr
->attrname
, SUNWPR_PREFIX
, slen
)
182 attrname
= attr
->attrname
+ slen
;
185 attrname
= attr
->attrname
;
188 * The attrname is the key. The attribute
191 len
= snprintf(buffer
, buflen
, ":%s=", attrname
);
192 TEST_AND_ADJUST(len
, buffer
, buflen
,
193 result_printers2str
);
195 for (j
= 0; j
< attr
->value_count
; j
++) {
199 if (attr
->attrvalue
[j
] == NULL
) {
201 nss_result
= NSS_STR_PARSE_PARSE
;
202 goto result_printers2str
;
204 len
= strlen(attr
->attrvalue
[j
]);
207 nss_result
= NSS_STR_PARSE_PARSE
;
208 goto result_printers2str
;
211 * Add extra for any colons which need to
212 * be backslashed plus ending ':' or ','.
215 for (kp
= attr
->attrvalue
[j
]; *kp
!= '\0'; kp
++)
217 /* count ':' in value */
227 nss_result
= NSS_STR_PARSE_ERANGE
;
228 goto result_printers2str
;
233 (void) append_attr(buffer
,
235 buffer
+= strlen(attr
->attrvalue
[j
]) + k
;
240 if (argp
->buf
.result
!= NULL
)
241 be
->buflen
= strlen(be
->buffer
);
244 (void) __ns_ldap_freeResult(&be
->result
);
245 return ((int)nss_result
);
249 * Attributes which contain colons must be backslashed.
252 append_attr(char *buf
, char *attr
)
256 if (strchr(attr
, ':') == NULL
) {
257 (void) strcat(buf
, attr
);
262 while (*cp
!= '\0') {
271 * getbyname gets printer attributes by printer name. This function
272 * constructs an ldap search filter using the printer name invocation
273 * parameter and the getprinterbyname search filter defined. Once the
274 * filter is constructed, we search for matching entries and marshal
275 * the data results into argp->buf.buffer for the frontend process.
276 * The function _nss_ldap_printers2str performs the data marshaling.
280 getbyname(ldap_backend_ptr be
, void *a
)
282 char printername
[BUFSIZ
];
283 nss_XbyY_args_t
*argp
= (nss_XbyY_args_t
*)a
;
284 char searchfilter
[SEARCHFILTERLEN
];
286 (void) strncpy(printername
, argp
->key
.name
, BUFSIZ
);
287 if (snprintf(searchfilter
, SEARCHFILTERLEN
,
288 _F_GETPRINTERBYNAME
, printername
, printername
) < 0)
289 return ((nss_status_t
)NSS_NOTFOUND
);
291 return ((nss_status_t
)_nss_ldap_lookup(be
, argp
,
292 _PRINTERS
, searchfilter
, NULL
, NULL
, NULL
));
295 static ldap_backend_op_t printers_ops
[] = {
305 * _nss_ldap_printers_constr is where life begins. This function calls
306 * the generic ldap constructor function to define and build the abstract
307 * data types required to support ldap operations.
312 _nss_ldap_printers_constr(const char *dummy1
, const char *dummy2
,
316 return ((nss_backend_t
*)_nss_ldap_constr(printers_ops
,
317 sizeof (printers_ops
)/sizeof (printers_ops
[0]), _PRINTERS
,
318 printer_attrs
, _nss_ldap_printers2str
));