4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2015 Gary Mills
24 * Copyright 2001-2003 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 #include <sys/systeminfo.h>
30 #include <rpcsvc/nis.h>
32 #include "nis_parse_ldap_conf.h"
34 #include "ldap_attr.h"
35 #include "ldap_util.h"
36 #include "ldap_structs.h"
40 * If 'name' doesn't end in a trailing dot, return a copy with the
41 * value of "nisplusLDAPbaseDomain" appended. Otherwise, return a
42 * copy of 'name'. If deallocate!=0, free 'name'.
45 fullObjName(int deallocate
, char *name
) {
48 char *myself
= "fullObjName";
51 return (sdup(myself
, T
, proxyInfo
.default_nis_domain
));
54 if (name
[l
-1] == '.') {
55 full
= sdup(myself
, T
, name
);
57 full
= scat(myself
, T
, scat(myself
, F
, name
, "."),
58 sdup(myself
, T
, proxyInfo
.default_nis_domain
));
67 * Convert a domain name ("x.y.z.", say) to a "dc=..." type LDAP equivalent
68 * ("dc=x,dc=y,dx=z"). The domain name supplied MUST be terminated by a
69 * trailing dot. If 'domain' is NULL, the value of "nisplusLDAPbaseDomain"
73 domain2base(char *domain
) {
76 char *myself
= "domain2base";
79 domain
= sdup(myself
, T
, proxyInfo
.default_nis_domain
);
83 for (l
= 0, i
= 0; domain
[i
] != '\0'; i
++) {
84 if (domain
[i
] == '.') {
87 base
= scat(myself
, T
, base
,
88 scat(myself
, F
, ",dc=", &domain
[l
]));
90 base
= scat(myself
, T
, base
,
91 scat(myself
, F
, "dc=", &domain
[l
]));
100 * If 'name' ends in a trailing comma, append the value of the
101 * "defaultSearchBase". If deallocate!=0, free 'name'.
104 fullLDAPname(int deallocate
, char *name
) {
107 return (appendBase(name
, proxyInfo
.default_search_base
, &err
,
112 * If the 'item' string ends in a comma, append 'base', and return
113 * the result. On exit, '*err' will be zero if successful, non-zero
114 * otherwise. If 'dealloc' is non-zero, 'item' is freed; this happens
115 * even if an error status is returned.
117 * The return value is always allocated, and must be freed by the caller.
120 appendBase(char *item
, char *base
, int *err
, int dealloc
) {
123 char *myself
= "appendBase";
126 * Make sure that 'err' points to something valid, so that we can
127 * dispense with all those 'if (err != 0)'.
132 /* Establish default (successful) error status */
135 /* Trivial case 1: If 'item' is NULL, return a copy of 'base' */
137 new = sdup(myself
, T
, base
);
143 /* Trivial case 2: If 'base' is NULL, return a copy of 'item' */
145 new = sdup(myself
, T
, item
);
155 /* If 'item' is the empty string, return a copy of 'base' */
157 new = sdup(myself
, T
, base
);
166 * If 'item' ends in a comma, append 'base', and return a copy
167 * of the result. Otherwise, return a copy of 'item'.
169 if (item
[len
-1] == ',') {
170 int blen
= slen(base
);
171 new = am(myself
, len
+ blen
+ 1);
173 (void) memcpy(new, item
, len
);
174 (void) memcpy(&new[len
], base
, blen
);
179 new = sdup(myself
, T
, item
);
191 * Despite its general-sounding name, this function only knows how to
192 * turn a list of attributes ("a,b,c") into an AND filter ("(&(a)(b)(c))").
195 makeFilter(char *attr
) {
197 char *str
, *filter
, *tmp
;
198 char *myself
= "makeFilter";
200 if (attr
== 0 || (len
= strlen(attr
)) == 0)
203 /* Assume already of appropriate form if first char is '(' */
204 if (len
> 1 && attr
[0] == '(' && attr
[len
-1] == ')')
205 return (sdup(myself
, T
, attr
));
207 str
= sdup(myself
, T
, attr
);
210 filter
= sdup(myself
, T
, "(&");
215 for (s
= c
= 0; s
< len
; s
= e
+1) {
216 /* Skip blank space, if any */
217 for (; str
[s
] == ' ' || str
[s
] == '\t'; s
++);
218 /* Find delimiter (comma) or end of string */
219 for (e
= s
; str
[e
] != '\0' && str
[e
] != ','; e
++);
221 tmp
= scat(myself
, T
, sdup(myself
, T
, "("),
222 scat(myself
, F
, &str
[s
], ")"));
228 filter
= scat(myself
, T
, filter
, tmp
);
232 * If there's just one component, we return it as is. This
233 * means we avoid turning "objectClass=posixAccount" into
234 * "(&(objectClass=posixAccount))".
241 /* Add the closing ')' */
243 filter
= scat(myself
, F
, tmp
, ")");
252 * Split an AND-filter string into components.
255 makeFilterComp(char *filter
, int *numComps
) {
257 char **comp
= 0, **new, *str
;
259 char *myself
= "makeFilterComp";
261 if ((len
= slen(filter
)) <= 0)
264 /* Is it just a plain "attr=val" string ? If so, return a copy */
265 if (len
<= 2 || filter
[0] != '(') {
266 comp
= am(myself
, 2 * sizeof (comp
[0]));
269 comp
[0] = sdup(myself
, T
, filter
);
279 if (filter
!= 0 && (len
= strlen(filter
)) != 0 && len
> 2 &&
280 filter
[0] == '(' && filter
[1] == '&' &&
281 filter
[len
-1] == ')') {
282 str
= sdup(myself
, T
, filter
);
285 for (s
= 2; s
< len
; s
= e
+1) {
286 /* Skip past the '(' */
287 for (; s
< len
&& str
[s
] != '('; s
++);
291 for (e
= s
; str
[e
] != '\0' && str
[e
] != ')'; e
++);
293 new = realloc(comp
, (nc
+1) * sizeof (comp
[nc
]));
296 for (i
= 0; i
< nc
; i
++)
305 comp
[nc
] = sdup(myself
, T
, &str
[s
]);
307 for (i
= 0; i
< nc
; i
++)
326 freeFilterComp(char **comp
, int numComps
) {
332 for (i
= 0; i
< numComps
; i
++) {
339 addFilterComp(char *new, char **comp
, int *numComps
) {
341 char *myself
= "addFilterComp";
343 if (new == 0 || numComps
== 0 || *numComps
< 0)
346 str
= sdup(myself
, T
, new);
349 tmp
= realloc(comp
, ((*numComps
)+1) * sizeof (comp
[0]));
356 comp
[*numComps
] = str
;
363 concatenateFilterComps(int numComps
, char **comp
) {
365 __nis_buffer_t b
= {0, 0};
366 char *myself
= "concatenateFilterComps";
368 if (numComps
== 0 || comp
== 0)
371 bp2buf(myself
, &b
, "(&");
372 for (i
= 0; i
< numComps
; i
++) {
375 bp2buf(myself
, &b
, "(%s)", comp
[i
]);
377 bp2buf(myself
, &b
, ")");
383 freeDNs(char **dn
, int numDN
) {
389 for (i
= 0; i
< numDN
; i
++) {
396 * Search the supplied rule-value structure array for any attributes called
397 * "dn", and return their values. If the "dn" value(s) end in a comma, they
398 * get the 'defBase' value appended.
401 findDNs(char *msg
, __nis_rule_value_t
*rv
, int nrv
, char *defBase
,
405 char *myself
= "findDNs";
407 if (rv
== 0 || nrv
<= 0 || numDN
== 0)
413 /* Avoid realloc() by pre-allocating 'dn' at maximum size */
414 dn
= am(msg
, nrv
* sizeof (dn
[0]));
418 for (ndn
= 0, irv
= 0; irv
< nrv
; irv
++) {
419 for (iv
= 0; iv
< rv
[irv
].numAttrs
; iv
++) {
420 /* Looking for string-valued attribute called "dn" */
421 if (rv
[irv
].attrName
[iv
] != 0 &&
422 rv
[irv
].attrVal
[iv
].type
== vt_string
&&
423 rv
[irv
].attrVal
[iv
].numVals
>= 1 &&
424 strcasecmp("dn", rv
[irv
].attrName
[iv
]) == 0) {
426 dn
[ndn
] = appendBase(
427 rv
[irv
].attrVal
[iv
].val
[0].value
,