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]
23 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
30 #include "../../../lib/libsldap/common/ns_sldap.h"
44 static struct mapping maplist
[] = {
45 {"publickey", "uidnumber", "niskeyobject", "passwd"},
46 {"publickey", "cn", "niskeyobject", "host"},
47 {"bootparams", "cn", "bootableDevice", NULL
},
48 {"ethers", "cn", "ieee802Device", NULL
},
49 {"group", "cn", "posixgroup", NULL
},
50 {"hosts", "cn", "iphost", NULL
},
51 {"ipnodes", "cn", "iphost", NULL
},
52 {"netgroup", "cn", "nisnetgroup", NULL
},
53 {"netmasks", "ipnetworknumber", "ipnetwork", NULL
},
54 {"networks", "ipnetworknumber", "ipnetwork", NULL
},
55 {"passwd", "uid", "posixaccount", NULL
},
56 {"protocols", "cn", "ipprotocol", NULL
},
57 {"rpc", "cn", "oncrpc", NULL
},
58 {"services", "cn", "ipservice", NULL
},
59 {"aliases", "cn", "mailGroup", NULL
},
60 {"project", "SolarisProjectID", "SolarisProject", NULL
},
61 {"printers", "printer-uri", "sunPrinter", NULL
},
62 {"shadow", "uid", "shadowaccount", NULL
},
63 {"auth_attr", "cn", "SolarisAuthAttr", NULL
},
64 {"prof_attr", "cn", "SolarisProfAttr", NULL
},
65 {"exec_attr", "cn", "SolarisExecAttr", NULL
},
66 {"user_attr", "uid", "SolarisUserAttr", NULL
},
67 {NULL
, NULL
, NULL
, NULL
}
70 #define PROF_ATTR_FILTER \
71 "(&(objectclass=SolarisProfAttr)(!(SolarisKernelSecurityPolicy=*))%s)"
72 #define OC_FILTER "objectclass=%s"
74 #define OC_FILTER2 "(&(objectclass=%s)%s)"
77 /* Malloc and print error message in case of failure */
78 #define MALLOC(ptr, len) \
79 if ((ptr = (char *)malloc(len)) == NULL) { \
80 (void) fprintf(stderr, gettext("out of memory\n")); \
84 * Allocate memory for filter and user data. Set
85 * error to 1 if either of the mallocs fail.
86 * In addition, free the memory allocated for filter,
87 * if memory allocation for user data fails.
89 #define MALLOC_FILTER_UDATA(ptr1, len1, ptr2, len2, error) \
108 (void) fprintf(stdout
,
109 gettext("database default type objectclass\n"));
110 (void) fprintf(stdout
,
111 gettext("============= ================= =============\n"));
112 /* first dump auto_* and automount which are not in maplist[] */
113 (void) fprintf(stdout
, "%-15s%-20s%s\n", "auto_*", "automountKey",
115 (void) fprintf(stdout
, "%-15s%-20s%s\n", "automount",
116 "automountMapName", "automountMap");
117 for (i
= 0; maplist
[i
].database
!= NULL
; i
++) {
118 /* skip printing shadow */
119 if (strcasecmp(maplist
[i
].database
, "shadow") == 0)
121 (void) fprintf(stdout
, "%-15s%-20s%s\n", maplist
[i
].database
,
122 maplist
[i
].def_type
, maplist
[i
].objectclass
);
127 * set_key routine to handle user specified keys.
128 * A key can be of the form: attribute=value or value.
129 * A filter is constructed from a set of keys specified in
130 * the form (|(key1)(key2)...(keyn))
131 * It returns: NULL if no keys are defined or
132 * the keyfilter as constructed above.
136 set_keys(char **key
, char *attrtype
)
139 char *keyfilter
= NULL
;
140 int len
, totlen
= 1; /* Terminating NULL byte */
144 if (!key
|| !key
[0]) /* should never contain NULL string */
149 /* Allocate memory for '(|)' */
150 MALLOC(keyfilter
, totlen
);
153 (void) snprintf(keyfilter
, totlen
, "(|");
157 while ((k
= *karray
) != 0) {
158 keyeq
= strchr(k
, '=');
160 /* make enough room for (%s) */
161 totlen
+= strlen(k
) + 2;
163 /* make enough room for (%s=%s) */
164 totlen
+= strlen(attrtype
) + strlen(k
) + 3;
167 len
= keyfilter
? strlen(keyfilter
) : 0;
169 if (!(tmpptr
= (char *)realloc(keyfilter
, totlen
))) {
171 (void) fprintf(stderr
, gettext("out of memory\n"));
177 (void) snprintf(keyfilter
+ len
, totlen
- len
,
180 (void) snprintf(keyfilter
+ len
, totlen
- len
,
181 "(%s=%s)", attrtype
, k
);
187 /* We allocated memory for this earlier */
188 (void) strlcat(keyfilter
, ")", totlen
);
196 * A special set_key routine for to handle public keys.
197 * If the key starts with a digiti, view it as a user id.
198 * Otherwise, view it as a hostname.
199 * It returns: -1 no keys defined, 0 key defined but none for type
200 * specified, n>0 number of matches found.
203 set_keys_publickey(char **key
, char *attrtype
, int type
, char **ret
)
206 char *keyfilter
= NULL
;
207 char *pre_filter
= NULL
;
210 int len
, totlen
= 1; /* Terminating NULL byte */
213 if (!key
|| !key
[0]) { /* should never contain NULL string */
219 while ((k
= *karray
) != 0) {
220 keyeq
= strchr(k
, '=');
222 /* make enough room for (%s) */
223 totlen
+= strlen(k
) + 2;
225 if ((type
== 0 && isdigit(*k
)) ||
227 (type
== 1 && (!isdigit(*k
)))) {
228 /* hosts type keys */
229 /* make enough room for (%s=%s) */
230 totlen
+= strlen(k
) + strlen(attrtype
) + 3;
237 len
= pre_filter
? strlen(pre_filter
) : 0;
239 if (!(tmpptr
= (char *)realloc(pre_filter
, totlen
))) {
241 (void) fprintf(stderr
, gettext("out of memory\n"));
247 (void) snprintf(pre_filter
+ len
, totlen
- len
,
250 (void) snprintf(pre_filter
+ len
, totlen
- len
,
251 "(%s=%s)", attrtype
, k
);
257 len
= strlen(pre_filter
) + 4;
258 if (!(keyfilter
= (char *)malloc(len
))) {
259 (void) fprintf(stderr
, gettext("out of memory\n"));
263 (void) snprintf(keyfilter
, len
, "(|%s)", pre_filter
);
272 * publickey specific set_filter
273 * type 0 -> check for user publickeys
274 * type 1 -> check for hosts publickeys
277 set_filter_publickey(char **key
, char *database
, int type
, char **udata
)
281 char *keyfilter
= NULL
;
283 int filterlen
, udatalen
;
286 if (!database
|| !udata
) {
290 if (strcasecmp(database
, maplist
[PUBLICKEY
].database
) == SAME
) {
291 rc
= set_keys_publickey(key
,
292 maplist
[PUBLICKEY
+ type
].def_type
, type
, &keyfilter
);
295 filterlen
= strlen(maplist
[PUBLICKEY
].objectclass
) + 13;
297 MALLOC_FILTER_UDATA(filter
, filterlen
, userdata
,
300 (void) snprintf(filter
, filterlen
,
302 maplist
[PUBLICKEY
].objectclass
);
303 (void) snprintf(userdata
, udatalen
, "%%s");
309 filterlen
= strlen(maplist
[PUBLICKEY
].objectclass
) +
310 strlen(keyfilter
) + 18;
311 udatalen
= strlen(keyfilter
) + 8;
312 MALLOC_FILTER_UDATA(filter
, filterlen
, userdata
,
315 (void) snprintf(filter
, filterlen
,
316 "(&(objectclass=%s)%s)",
317 maplist
[PUBLICKEY
].objectclass
, keyfilter
);
318 (void) snprintf(userdata
, udatalen
,
319 "(&(%%s)%s)", keyfilter
);
323 if ((keyfilter
= set_keys(key
, "cn")) == NULL
) {
326 MALLOC_FILTER_UDATA(filter
, filterlen
, userdata
,
329 (void) snprintf(filter
, filterlen
,
331 (void) snprintf(userdata
, udatalen
, "%%s");
334 filterlen
= strlen(keyfilter
) + 1;
335 udatalen
= strlen(keyfilter
) + 8;
336 MALLOC_FILTER_UDATA(filter
, filterlen
, userdata
,
339 (void) snprintf(filter
, filterlen
, "%s",
341 (void) snprintf(userdata
, udatalen
,
342 "(&(%%s)%s)", keyfilter
);
347 (void) fprintf(stdout
, "set_filter: filter=\"%s\"\n", filter
);
348 (void) fprintf(stdout
, "set_filter: userdata=\"%s\"\n", userdata
);
358 /* generic set_filter, this function is not thread safe */
360 set_filter(char **key
, char *database
, char **udata
)
363 char *userdata
= NULL
;
365 int i
, filterlen
, udatalen
;
368 void **paramVal
= NULL
;
369 ns_ldap_error_t
*errorp
= NULL
;
372 if (!database
|| !udata
) {
378 * Check for version of the profile the client is using
380 * For version 1 profiles we do use nisMap and nisObject schema
381 * for backward compatibility with Solaris 8 clients.
383 * For version 2 profiles we use automountMap and automount as
384 * default attributes (which can then be overridden in libsldap
385 * if schema mapping is configured in the profile).
387 * If profile version is not available, use version 2 as default.
389 rc
= __ns_ldap_getParam(NS_LDAP_FILE_VERSION_P
, ¶mVal
, &errorp
);
390 if (rc
!= NS_LDAP_SUCCESS
|| !paramVal
|| !*paramVal
) {
391 /* should print a message here: using v2 defaults */
392 (void) __ns_ldap_freeError(&errorp
);
394 if (strcasecmp(*paramVal
, NS_LDAP_VERSION_1
) == 0)
396 (void) __ns_ldap_freeParam(¶mVal
);
400 * starts at 2 to skip over publickey databases.
401 * These databases are handled separately.
403 for (i
= 2; maplist
[i
].database
!= NULL
; i
++) {
404 if (strcasecmp(database
, maplist
[i
].database
) == SAME
) {
406 if (strcasecmp(database
, "prof_attr") == 0)
408 if ((keyfilter
= set_keys(key
, maplist
[i
].def_type
))
410 filterlen
= strlen(maplist
[i
].objectclass
);
413 filterlen
+= strlen(PROF_ATTR_FILTER
)
416 filterlen
+= OC_FLEN
;
418 MALLOC_FILTER_UDATA(filter
, filterlen
, userdata
,
423 (void) snprintf(filter
, filterlen
,
424 PROF_ATTR_FILTER
, "");
426 (void) snprintf(filter
, filterlen
,
428 maplist
[i
].objectclass
);
430 (void) snprintf(userdata
, udatalen
, "%%s");
432 filterlen
= strlen(maplist
[i
].objectclass
) +
435 filterlen
+= strlen(PROF_ATTR_FILTER
)
438 filterlen
+= OC_FLEN2
;
440 udatalen
= strlen(keyfilter
) + 8;
441 MALLOC_FILTER_UDATA(filter
, filterlen
, userdata
,
446 (void) snprintf(filter
, filterlen
,
447 PROF_ATTR_FILTER
, keyfilter
);
449 (void) snprintf(filter
, filterlen
,
451 maplist
[i
].objectclass
, keyfilter
);
453 (void) snprintf(userdata
, udatalen
,
454 "(&(%%s)%s)", keyfilter
);
460 /* special cases for automounter and other services */
462 /* auto_* services */
463 if (strncasecmp(database
, "auto_", 5) == SAME
) {
465 if ((keyfilter
= set_keys(key
, "automountKey"))
467 filterlen
= strlen(keyfilter
) + 27;
468 udatalen
= strlen(keyfilter
) + 8;
469 MALLOC_FILTER_UDATA(filter
, filterlen
,
470 userdata
, udatalen
, nomem
);
472 (void) snprintf(filter
, filterlen
,
473 "(&(objectclass=automount)%s)",
475 (void) snprintf(userdata
, udatalen
,
476 "(&(%%s)%s)", keyfilter
);
481 MALLOC_FILTER_UDATA(filter
, filterlen
,
482 userdata
, udatalen
, nomem
);
484 (void) strlcpy(filter
,
485 "objectclass=automount", filterlen
);
486 (void) strlcpy(userdata
, "%s",
491 if ((keyfilter
= set_keys(key
, "cn")) != NULL
) {
492 filterlen
= strlen(keyfilter
) + 27;
493 udatalen
= strlen(keyfilter
) + 8;
494 MALLOC_FILTER_UDATA(filter
, filterlen
,
495 userdata
, udatalen
, nomem
);
497 (void) snprintf(filter
, filterlen
,
498 "(&(objectclass=nisObject)%s)",
500 (void) snprintf(userdata
, udatalen
,
501 "(&(%%s)%s)", keyfilter
);
506 MALLOC_FILTER_UDATA(filter
, filterlen
,
507 userdata
, udatalen
, nomem
);
509 (void) strlcpy(filter
,
510 "objectclass=nisObject", filterlen
);
511 (void) strlcpy(userdata
, "%s",
519 /* automount service */
520 if (strcasecmp(database
, "automount") == SAME
) {
522 if ((keyfilter
= set_keys(key
, "automountMapName"))
524 filterlen
= strlen(keyfilter
) + 30;
525 udatalen
= strlen(keyfilter
) + 8;
526 MALLOC_FILTER_UDATA(filter
, filterlen
,
527 userdata
, udatalen
, nomem
);
529 (void) snprintf(filter
, filterlen
,
530 "(&(objectclass=automountMap)%s)",
532 (void) snprintf(userdata
, udatalen
,
533 "(&(%%s)%s)", keyfilter
);
538 MALLOC_FILTER_UDATA(filter
, filterlen
,
539 userdata
, udatalen
, nomem
);
541 (void) strlcpy(filter
,
542 "objectclass=automountMap",
544 (void) strlcpy(userdata
, "%s",
549 if ((keyfilter
= set_keys(key
, "nisMapName"))
551 filterlen
= strlen(keyfilter
) + 24;
552 udatalen
= strlen(keyfilter
) + 8;
553 MALLOC_FILTER_UDATA(filter
, filterlen
,
554 userdata
, udatalen
, nomem
);
556 (void) snprintf(filter
, filterlen
,
557 "(&(objectclass=nisMap)%s)",
559 (void) snprintf(userdata
, udatalen
,
560 "(&(%%s)%s)", keyfilter
);
565 MALLOC_FILTER_UDATA(filter
, filterlen
,
566 userdata
, udatalen
, nomem
);
568 (void) strlcpy(filter
,
569 "objectclass=nisMap", filterlen
);
570 (void) strlcpy(userdata
, "%s",
578 /* other services (catch all) */
579 if ((keyfilter
= set_keys(key
, "cn")) == NULL
) {
582 MALLOC_FILTER_UDATA(filter
, filterlen
, userdata
, udatalen
,
585 (void) snprintf(filter
, filterlen
, "objectclass=*");
586 (void) strlcpy(userdata
, "%s", udatalen
);
589 filterlen
= strlen(keyfilter
) + 1;
590 udatalen
= strlen(keyfilter
) + 8;
591 MALLOC_FILTER_UDATA(filter
, filterlen
, userdata
, udatalen
,
594 (void) snprintf(filter
, filterlen
, "%s", keyfilter
);
595 (void) snprintf(userdata
, udatalen
, "(&(%%s)%s)",
602 (void) fprintf(stdout
, "set_filter: filter=\"%s\"\n", filter
);
603 (void) fprintf(stdout
, "set_filter: userdata=\"%s\"\n", userdata
);