Print this page
5910 libnisdb won't build with modern GCC
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libnisdb/ldap_attr.c
+++ new/usr/src/lib/libnisdb/ldap_attr.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License, Version 1.0 only
6 6 * (the "License"). You may not use this file except in compliance
7 7 * with the License.
8 8 *
9 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 * or http://www.opensolaris.org/os/licensing.
11 11 * See the License for the specific language governing permissions
12 12 * and limitations under the License.
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
13 13 *
14 14 * When distributing Covered Code, include this CDDL HEADER in each
15 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 16 * If applicable, add the following below this CDDL HEADER, with the
17 17 * fields enclosed by brackets "[]" replaced with your own identifying
18 18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 19 *
20 20 * CDDL HEADER END
21 21 */
22 22 /*
23 + * Copyright 2015 Gary Mills
23 24 * Copyright 2001-2003 Sun Microsystems, Inc. All rights reserved.
24 25 * Use is subject to license terms.
25 26 */
26 27
27 -#pragma ident "%Z%%M% %I% %E% SMI"
28 -
29 28 #include <sys/systeminfo.h>
30 29 #include <strings.h>
31 30 #include <rpcsvc/nis.h>
32 31
33 32 #include "nis_parse_ldap_conf.h"
34 33
35 34 #include "ldap_attr.h"
36 35 #include "ldap_util.h"
37 36 #include "ldap_structs.h"
38 37
39 38
40 39 /*
41 40 * If 'name' doesn't end in a trailing dot, return a copy with the
42 41 * value of "nisplusLDAPbaseDomain" appended. Otherwise, return a
43 42 * copy of 'name'. If deallocate!=0, free 'name'.
44 43 */
45 44 char *
46 45 fullObjName(int deallocate, char *name) {
47 46 int l;
48 47 char *full;
49 48 char *myself = "fullObjName";
50 49
51 50 if (name == 0)
52 51 return (sdup(myself, T, proxyInfo.default_nis_domain));
53 52
54 53 l = strlen(name);
55 54 if (name[l-1] == '.') {
56 55 full = sdup(myself, T, name);
57 56 } else {
58 57 full = scat(myself, T, scat(myself, F, name, "."),
59 58 sdup(myself, T, proxyInfo.default_nis_domain));
60 59 }
61 60 if (deallocate)
62 61 free(name);
63 62
64 63 return (full);
65 64 }
66 65
67 66 /*
68 67 * Convert a domain name ("x.y.z.", say) to a "dc=..." type LDAP equivalent
69 68 * ("dc=x,dc=y,dx=z"). The domain name supplied MUST be terminated by a
70 69 * trailing dot. If 'domain' is NULL, the value of "nisplusLDAPbaseDomain"
71 70 * is converted.
72 71 */
73 72 char *
74 73 domain2base(char *domain) {
75 74 char *base = 0;
76 75 int l, i;
77 76 char *myself = "domain2base";
78 77
79 78 if (domain == 0)
80 79 domain = sdup(myself, T, proxyInfo.default_nis_domain);
81 80 if (domain == 0)
82 81 return (0);
83 82
84 83 for (l = 0, i = 0; domain[i] != '\0'; i++) {
85 84 if (domain[i] == '.') {
86 85 domain[i] = '\0';
87 86 if (l != 0)
88 87 base = scat(myself, T, base,
89 88 scat(myself, F, ",dc=", &domain[l]));
90 89 else
91 90 base = scat(myself, T, base,
92 91 scat(myself, F, "dc=", &domain[l]));
93 92 l = i+1;
94 93 }
95 94 }
96 95
97 96 return (base);
98 97 }
99 98
100 99 /*
101 100 * If 'name' ends in a trailing comma, append the value of the
102 101 * "defaultSearchBase". If deallocate!=0, free 'name'.
103 102 */
104 103 char *
105 104 fullLDAPname(int deallocate, char *name) {
106 105 int err = 0;
107 106
108 107 return (appendBase(name, proxyInfo.default_search_base, &err,
109 108 deallocate));
110 109 }
111 110
112 111 /*
113 112 * If the 'item' string ends in a comma, append 'base', and return
114 113 * the result. On exit, '*err' will be zero if successful, non-zero
115 114 * otherwise. If 'dealloc' is non-zero, 'item' is freed; this happens
116 115 * even if an error status is returned.
117 116 *
118 117 * The return value is always allocated, and must be freed by the caller.
119 118 */
120 119 char *
121 120 appendBase(char *item, char *base, int *err, int dealloc) {
122 121 char *new;
123 122 int len, deferr;
124 123 char *myself = "appendBase";
125 124
126 125 /*
127 126 * Make sure that 'err' points to something valid, so that we can
128 127 * dispense with all those 'if (err != 0)'.
129 128 */
130 129 if (err == 0)
131 130 err = &deferr;
132 131
133 132 /* Establish default (successful) error status */
134 133 *err = 0;
135 134
136 135 /* Trivial case 1: If 'item' is NULL, return a copy of 'base' */
137 136 if (item == 0) {
138 137 new = sdup(myself, T, base);
139 138 if (new == 0)
140 139 *err = -1;
141 140 return (new);
142 141 }
143 142
144 143 /* Trivial case 2: If 'base' is NULL, return a copy of 'item' */
145 144 if (base == 0) {
146 145 new = sdup(myself, T, item);
147 146 if (new == 0)
148 147 *err = -1;
149 148 if (dealloc)
150 149 free(item);
151 150 return (new);
152 151 }
153 152
154 153 len = strlen(item);
155 154
156 155 /* If 'item' is the empty string, return a copy of 'base' */
157 156 if (len <= 0) {
158 157 new = sdup(myself, T, base);
159 158 if (new == 0)
160 159 *err = -1;
161 160 if (dealloc)
162 161 free(item);
163 162 return (new);
164 163 }
165 164
166 165 /*
167 166 * If 'item' ends in a comma, append 'base', and return a copy
168 167 * of the result. Otherwise, return a copy of 'item'.
169 168 */
170 169 if (item[len-1] == ',') {
171 170 int blen = slen(base);
172 171 new = am(myself, len + blen + 1);
173 172 if (new != 0) {
174 173 (void) memcpy(new, item, len);
175 174 (void) memcpy(&new[len], base, blen);
176 175 } else {
177 176 *err = -1;
178 177 }
179 178 } else {
180 179 new = sdup(myself, T, item);
181 180 if (new == 0)
182 181 *err = -1;
183 182 }
184 183
185 184 if (dealloc)
186 185 free(item);
187 186
188 187 return (new);
189 188 }
190 189
191 190 /*
192 191 * Despite its general-sounding name, this function only knows how to
193 192 * turn a list of attributes ("a,b,c") into an AND filter ("(&(a)(b)(c))").
194 193 */
195 194 char *
196 195 makeFilter(char *attr) {
197 196 int len, s, e, c;
198 197 char *str, *filter, *tmp;
199 198 char *myself = "makeFilter";
200 199
201 200 if (attr == 0 || (len = strlen(attr)) == 0)
202 201 return (0);
203 202
204 203 /* Assume already of appropriate form if first char is '(' */
205 204 if (len > 1 && attr[0] == '(' && attr[len-1] == ')')
206 205 return (sdup(myself, T, attr));
207 206
↓ open down ↓ |
169 lines elided |
↑ open up ↑ |
208 207 str = sdup(myself, T, attr);
209 208 if (str == 0)
210 209 return (0);
211 210 filter = sdup(myself, T, "(&");
212 211 if (filter == 0) {
213 212 free(str);
214 213 return (0);
215 214 }
216 215 for (s = c = 0; s < len; s = e+1) {
217 216 /* Skip blank space, if any */
218 - for (0; str[s] == ' ' || str[s] == '\t'; s++);
217 + for (; str[s] == ' ' || str[s] == '\t'; s++);
219 218 /* Find delimiter (comma) or end of string */
220 219 for (e = s; str[e] != '\0' && str[e] != ','; e++);
221 220 str[e] = '\0';
222 221 tmp = scat(myself, T, sdup(myself, T, "("),
223 222 scat(myself, F, &str[s], ")"));
224 223 if (tmp == 0) {
225 224 sfree(filter);
226 225 return (0);
227 226 }
228 227 c++;
229 228 filter = scat(myself, T, filter, tmp);
230 229 }
231 230
232 231 /*
233 232 * If there's just one component, we return it as is. This
234 233 * means we avoid turning "objectClass=posixAccount" into
235 234 * "(&(objectClass=posixAccount))".
236 235 */
237 236 if (c == 1) {
238 237 sfree(filter);
239 238 return (str);
240 239 }
241 240
242 241 /* Add the closing ')' */
243 242 tmp = filter;
244 243 filter = scat(myself, F, tmp, ")");
245 244 sfree(tmp);
246 245
247 246 free(str);
248 247
249 248 return (filter);
250 249 }
251 250
252 251 /*
253 252 * Split an AND-filter string into components.
254 253 */
255 254 char **
256 255 makeFilterComp(char *filter, int *numComps) {
257 256 int nc = 0, s, e, i;
258 257 char **comp = 0, **new, *str;
259 258 int len;
260 259 char *myself = "makeFilterComp";
261 260
262 261 if ((len = slen(filter)) <= 0)
263 262 return (0);
264 263
265 264 /* Is it just a plain "attr=val" string ? If so, return a copy */
266 265 if (len <= 2 || filter[0] != '(') {
267 266 comp = am(myself, 2 * sizeof (comp[0]));
268 267 if (comp == 0)
269 268 return (0);
270 269 comp[0] = sdup(myself, T, filter);
271 270 if (comp[0] == 0) {
272 271 sfree(comp);
273 272 return (0);
274 273 }
275 274 if (numComps != 0)
276 275 *numComps = 1;
277 276 return (comp);
↓ open down ↓ |
49 lines elided |
↑ open up ↑ |
278 277 }
279 278
280 279 if (filter != 0 && (len = strlen(filter)) != 0 && len > 2 &&
281 280 filter[0] == '(' && filter[1] == '&' &&
282 281 filter[len-1] == ')') {
283 282 str = sdup(myself, T, filter);
284 283 if (str == 0)
285 284 return (0);
286 285 for (s = 2; s < len; s = e+1) {
287 286 /* Skip past the '(' */
288 - for (0; s < len && str[s] != '('; s++);
287 + for (; s < len && str[s] != '('; s++);
289 288 s++;
290 289 if (s >= len)
291 290 break;
292 291 for (e = s; str[e] != '\0' && str[e] != ')'; e++);
293 292 str[e] = '\0';
294 293 new = realloc(comp, (nc+1) * sizeof (comp[nc]));
295 294 if (new == 0) {
296 295 if (comp != 0) {
297 296 for (i = 0; i < nc; i++)
298 297 sfree(comp[i]);
299 298 free(comp);
300 299 comp = 0;
301 300 }
302 301 nc = 0;
303 302 break;
304 303 }
305 304 comp = new;
306 305 comp[nc] = sdup(myself, T, &str[s]);
307 306 if (comp[nc] == 0) {
308 307 for (i = 0; i < nc; i++)
309 308 sfree(comp[i]);
310 309 sfree(comp);
311 310 comp = 0;
312 311 nc = 0;
313 312 break;
314 313 }
315 314 nc++;
316 315 }
317 316 sfree(str);
318 317 }
319 318
320 319 if (numComps != 0)
321 320 *numComps = nc;
322 321
323 322 return (comp);
324 323 }
325 324
326 325 void
327 326 freeFilterComp(char **comp, int numComps) {
328 327 int i;
329 328
330 329 if (comp == 0)
331 330 return;
332 331
333 332 for (i = 0; i < numComps; i++) {
334 333 sfree(comp[i]);
335 334 }
336 335 free(comp);
337 336 }
338 337
339 338 char **
340 339 addFilterComp(char *new, char **comp, int *numComps) {
341 340 char **tmp, *str;
342 341 char *myself = "addFilterComp";
343 342
344 343 if (new == 0 || numComps == 0 || *numComps < 0)
345 344 return (comp);
346 345
347 346 str = sdup(myself, T, new);
348 347 if (str == 0)
349 348 return (0);
350 349 tmp = realloc(comp, ((*numComps)+1) * sizeof (comp[0]));
351 350 if (tmp == 0) {
352 351 sfree(str);
353 352 return (0);
354 353 }
355 354
356 355 comp = tmp;
357 356 comp[*numComps] = str;
358 357 *numComps += 1;
359 358
360 359 return (comp);
361 360 }
362 361
363 362 char *
364 363 concatenateFilterComps(int numComps, char **comp) {
365 364 int i;
366 365 __nis_buffer_t b = {0, 0};
367 366 char *myself = "concatenateFilterComps";
368 367
369 368 if (numComps == 0 || comp == 0)
370 369 return (0);
371 370
372 371 bp2buf(myself, &b, "(&");
373 372 for (i = 0; i < numComps; i++) {
374 373 if (comp[i] == 0)
375 374 continue;
376 375 bp2buf(myself, &b, "(%s)", comp[i]);
377 376 }
378 377 bp2buf(myself, &b, ")");
379 378
380 379 return (b.buf);
381 380 }
382 381
383 382 void
384 383 freeDNs(char **dn, int numDN) {
385 384 int i;
386 385
387 386 if (dn == 0)
388 387 return;
389 388
390 389 for (i = 0; i < numDN; i++) {
391 390 sfree(dn[i]);
392 391 }
393 392 sfree(dn);
394 393 }
395 394
396 395 /*
397 396 * Search the supplied rule-value structure array for any attributes called
398 397 * "dn", and return their values. If the "dn" value(s) end in a comma, they
399 398 * get the 'defBase' value appended.
400 399 */
401 400 char **
402 401 findDNs(char *msg, __nis_rule_value_t *rv, int nrv, char *defBase,
403 402 int *numDN) {
404 403 char **dn;
405 404 int irv, iv, ndn;
406 405 char *myself = "findDNs";
407 406
408 407 if (rv == 0 || nrv <= 0 || numDN == 0)
409 408 return (0);
410 409
411 410 if (msg == 0)
412 411 msg = myself;
413 412
414 413 /* Avoid realloc() by pre-allocating 'dn' at maximum size */
415 414 dn = am(msg, nrv * sizeof (dn[0]));
416 415 if (dn == 0)
417 416 return (0);
418 417
419 418 for (ndn = 0, irv = 0; irv < nrv; irv++) {
420 419 for (iv = 0; iv < rv[irv].numAttrs; iv++) {
421 420 /* Looking for string-valued attribute called "dn" */
422 421 if (rv[irv].attrName[iv] != 0 &&
423 422 rv[irv].attrVal[iv].type == vt_string &&
424 423 rv[irv].attrVal[iv].numVals >= 1 &&
425 424 strcasecmp("dn", rv[irv].attrName[iv]) == 0) {
426 425 int err = 0;
427 426 dn[ndn] = appendBase(
428 427 rv[irv].attrVal[iv].val[0].value,
429 428 defBase, &err, 0);
430 429 if (err != 0) {
431 430 freeDNs(dn, ndn);
432 431 return (0);
433 432 }
434 433 ndn++;
435 434 break;
436 435 }
437 436 }
438 437 }
439 438
440 439 *numDN = ndn;
441 440 return (dn);
442 441 }
↓ open down ↓ |
144 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX