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 <tsol/label.h>
31 #include "../../../lib/libsldap/common/ns_sldap.h"
45 static struct mapping maplist
[] = {
46 {"publickey", "uidnumber", "niskeyobject", "passwd"},
47 {"publickey", "cn", "niskeyobject", "host"},
48 {"bootparams", "cn", "bootableDevice", NULL
},
49 {"ethers", "cn", "ieee802Device", NULL
},
50 {"group", "cn", "posixgroup", NULL
},
51 {"hosts", "cn", "iphost", NULL
},
52 {"ipnodes", "cn", "iphost", NULL
},
53 {"netgroup", "cn", "nisnetgroup", NULL
},
54 {"netmasks", "ipnetworknumber", "ipnetwork", NULL
},
55 {"networks", "ipnetworknumber", "ipnetwork", NULL
},
56 {"passwd", "uid", "posixaccount", NULL
},
57 {"protocols", "cn", "ipprotocol", NULL
},
58 {"rpc", "cn", "oncrpc", NULL
},
59 {"services", "cn", "ipservice", NULL
},
60 {"aliases", "cn", "mailGroup", NULL
},
61 {"project", "SolarisProjectID", "SolarisProject", NULL
},
62 {"printers", "printer-uri", "sunPrinter", NULL
},
63 {"shadow", "uid", "shadowaccount", NULL
},
64 {"auth_attr", "cn", "SolarisAuthAttr", NULL
},
65 {"prof_attr", "cn", "SolarisProfAttr", NULL
},
66 {"exec_attr", "cn", "SolarisExecAttr", NULL
},
67 {"user_attr", "uid", "SolarisUserAttr", NULL
},
68 {"tnrhtp", "ipTnetTemplateName", "ipTnetTemplate", NULL
},
69 {"tnrhdb", "ipTnetNumber", "ipTnetHost", NULL
},
70 {NULL
, NULL
, NULL
, NULL
}
73 #define PROF_ATTR_FILTER \
74 "(&(objectclass=SolarisProfAttr)(!(SolarisKernelSecurityPolicy=*))%s)"
75 #define TNRHTP_FILTER \
76 "(&(objectclass=ipTnetTemplate)(!(objectclass=ipTnetHost))%s)"
77 #define OC_FILTER "objectclass=%s"
79 #define OC_FILTER2 "(&(objectclass=%s)%s)"
82 /* Malloc and print error message in case of failure */
83 #define MALLOC(ptr, len) \
84 if ((ptr = (char *)malloc(len)) == NULL) { \
85 (void) fprintf(stderr, gettext("out of memory\n")); \
89 * Allocate memory for filter and user data. Set
90 * error to 1 if either of the mallocs fail.
91 * In addition, free the memory allocated for filter,
92 * if memory allocation for user data fails.
94 #define MALLOC_FILTER_UDATA(ptr1, len1, ptr2, len2, error) \
101 MALLOC(ptr2, len2); \
113 (void) fprintf(stdout
,
114 gettext("database default type objectclass\n"));
115 (void) fprintf(stdout
,
116 gettext("============= ================= =============\n"));
117 /* first dump auto_* and automount which are not in maplist[] */
118 (void) fprintf(stdout
, "%-15s%-20s%s\n", "auto_*", "automountKey",
120 (void) fprintf(stdout
, "%-15s%-20s%s\n", "automount",
121 "automountMapName", "automountMap");
122 for (i
= 0; maplist
[i
].database
!= NULL
; i
++) {
123 /* skip printing shadow */
124 if (strcasecmp(maplist
[i
].database
, "shadow") == 0)
126 if (!is_system_labeled()) {
128 * do not print tnrhdb and tnrhtp if system is
129 * not configured with Trusted Extensions
131 if ((strcasecmp(maplist
[i
].database
, "tnrhdb") == 0) ||
132 (strcasecmp(maplist
[i
].database
, "tnrhtp") == 0))
135 (void) fprintf(stdout
, "%-15s%-20s%s\n", maplist
[i
].database
,
136 maplist
[i
].def_type
, maplist
[i
].objectclass
);
141 * set_key routine to handle user specified keys.
142 * A key can be of the form: attribute=value or value.
143 * A filter is constructed from a set of keys specified in
144 * the form (|(key1)(key2)...(keyn))
145 * It returns: NULL if no keys are defined or
146 * the keyfilter as constructed above.
150 set_keys(char **key
, char *attrtype
)
153 char *keyfilter
= NULL
;
154 int len
, totlen
= 1; /* Terminating NULL byte */
158 if (!key
|| !key
[0]) /* should never contain NULL string */
163 /* Allocate memory for '(|)' */
164 MALLOC(keyfilter
, totlen
);
167 (void) snprintf(keyfilter
, totlen
, "(|");
171 while ((k
= *karray
) != 0) {
172 keyeq
= strchr(k
, '=');
174 /* make enough room for (%s) */
175 totlen
+= strlen(k
) + 2;
177 /* make enough room for (%s=%s) */
178 totlen
+= strlen(attrtype
) + strlen(k
) + 3;
181 len
= keyfilter
? strlen(keyfilter
) : 0;
183 if (!(tmpptr
= (char *)realloc(keyfilter
, totlen
))) {
186 (void) fprintf(stderr
, gettext("out of memory\n"));
192 (void) snprintf(keyfilter
+ len
, totlen
- len
,
195 (void) snprintf(keyfilter
+ len
, totlen
- len
,
196 "(%s=%s)", attrtype
, k
);
202 /* We allocated memory for this earlier */
203 (void) strlcat(keyfilter
, ")", totlen
);
211 * A special set_key routine for to handle public keys.
212 * If the key starts with a digiti, view it as a user id.
213 * Otherwise, view it as a hostname.
214 * It returns: -1 no keys defined, 0 key defined but none for type
215 * specified, n>0 number of matches found.
218 set_keys_publickey(char **key
, char *attrtype
, int type
, char **ret
)
221 char *keyfilter
= NULL
;
222 char *pre_filter
= NULL
;
225 int len
, totlen
= 1; /* Terminating NULL byte */
228 if (!key
|| !key
[0]) { /* should never contain NULL string */
234 while ((k
= *karray
) != 0) {
235 keyeq
= strchr(k
, '=');
237 /* make enough room for (%s) */
238 totlen
+= strlen(k
) + 2;
240 if ((type
== 0 && isdigit(*k
)) ||
242 (type
== 1 && (!isdigit(*k
)))) {
243 /* hosts type keys */
244 /* make enough room for (%s=%s) */
245 totlen
+= strlen(k
) + strlen(attrtype
) + 3;
252 len
= pre_filter
? strlen(pre_filter
) : 0;
254 if (!(tmpptr
= (char *)realloc(pre_filter
, totlen
))) {
257 (void) fprintf(stderr
, gettext("out of memory\n"));
263 (void) snprintf(pre_filter
+ len
, totlen
- len
,
266 (void) snprintf(pre_filter
+ len
, totlen
- len
,
267 "(%s=%s)", attrtype
, k
);
273 len
= strlen(pre_filter
) + 4;
274 if (!(keyfilter
= (char *)malloc(len
))) {
275 (void) fprintf(stderr
, gettext("out of memory\n"));
279 (void) snprintf(keyfilter
, len
, "(|%s)", pre_filter
);
288 * publickey specific set_filter
289 * type 0 -> check for user publickeys
290 * type 1 -> check for hosts publickeys
293 set_filter_publickey(char **key
, char *database
, int type
, char **udata
)
297 char *keyfilter
= NULL
;
299 int filterlen
, udatalen
;
302 if (!database
|| !udata
) {
306 if (strcasecmp(database
, maplist
[PUBLICKEY
].database
) == SAME
) {
307 rc
= set_keys_publickey(key
,
308 maplist
[PUBLICKEY
+ type
].def_type
, type
, &keyfilter
);
311 filterlen
= strlen(maplist
[PUBLICKEY
].objectclass
) + 13;
313 MALLOC_FILTER_UDATA(filter
, filterlen
, userdata
,
316 (void) snprintf(filter
, filterlen
,
318 maplist
[PUBLICKEY
].objectclass
);
319 (void) snprintf(userdata
, udatalen
, "%%s");
325 filterlen
= strlen(maplist
[PUBLICKEY
].objectclass
) +
326 strlen(keyfilter
) + 18;
327 udatalen
= strlen(keyfilter
) + 8;
328 MALLOC_FILTER_UDATA(filter
, filterlen
, userdata
,
331 (void) snprintf(filter
, filterlen
,
332 "(&(objectclass=%s)%s)",
333 maplist
[PUBLICKEY
].objectclass
, keyfilter
);
334 (void) snprintf(userdata
, udatalen
,
335 "(&(%%s)%s)", keyfilter
);
339 if ((keyfilter
= set_keys(key
, "cn")) == NULL
) {
342 MALLOC_FILTER_UDATA(filter
, filterlen
, userdata
,
345 (void) snprintf(filter
, filterlen
,
347 (void) snprintf(userdata
, udatalen
, "%%s");
350 filterlen
= strlen(keyfilter
) + 1;
351 udatalen
= strlen(keyfilter
) + 8;
352 MALLOC_FILTER_UDATA(filter
, filterlen
, userdata
,
355 (void) snprintf(filter
, filterlen
, "%s",
357 (void) snprintf(userdata
, udatalen
,
358 "(&(%%s)%s)", keyfilter
);
363 (void) fprintf(stdout
, "set_filter: filter=\"%s\"\n", filter
);
364 (void) fprintf(stdout
, "set_filter: userdata=\"%s\"\n", userdata
);
375 /* generic set_filter, this function is not thread safe */
377 set_filter(char **key
, char *database
, char **udata
)
380 char *userdata
= NULL
;
382 int i
, filterlen
, udatalen
;
385 void **paramVal
= NULL
;
386 ns_ldap_error_t
*errorp
= NULL
;
389 if (!database
|| !udata
) {
395 * Check for version of the profile the client is using
397 * For version 1 profiles we do use nisMap and nisObject schema
398 * for backward compatibility with Solaris 8 clients.
400 * For version 2 profiles we use automountMap and automount as
401 * default attributes (which can then be overridden in libsldap
402 * if schema mapping is configured in the profile).
404 * If profile version is not available, use version 2 as default.
406 rc
= __ns_ldap_getParam(NS_LDAP_FILE_VERSION_P
, ¶mVal
, &errorp
);
407 if (rc
!= NS_LDAP_SUCCESS
|| !paramVal
|| !*paramVal
) {
408 /* should print a message here: using v2 defaults */
409 (void) __ns_ldap_freeError(&errorp
);
411 if (strcasecmp(*paramVal
, NS_LDAP_VERSION_1
) == 0)
413 (void) __ns_ldap_freeParam(¶mVal
);
417 * starts at 2 to skip over publickey databases.
418 * These databases are handled separately.
420 for (i
= 2; maplist
[i
].database
!= NULL
; i
++) {
421 if (strcasecmp(database
, maplist
[i
].database
) == SAME
) {
423 if (strcasecmp(database
, "prof_attr") == 0)
425 else if (strcasecmp(database
, "tnrhtp") == 0)
427 if ((keyfilter
= set_keys(key
, maplist
[i
].def_type
))
429 filterlen
= strlen(maplist
[i
].objectclass
);
432 filterlen
+= strlen(PROF_ATTR_FILTER
)
435 filterlen
+= strlen(TNRHTP_FILTER
) + 1;
437 filterlen
+= OC_FLEN
;
439 MALLOC_FILTER_UDATA(filter
, filterlen
, userdata
,
444 (void) snprintf(filter
, filterlen
,
445 PROF_ATTR_FILTER
, "");
447 (void) snprintf(filter
, filterlen
,
450 (void) snprintf(filter
, filterlen
,
452 maplist
[i
].objectclass
);
454 (void) snprintf(userdata
, udatalen
, "%%s");
456 filterlen
= strlen(maplist
[i
].objectclass
) +
459 filterlen
+= strlen(PROF_ATTR_FILTER
)
462 filterlen
+= strlen(TNRHTP_FILTER
) + 1;
464 filterlen
+= OC_FLEN2
;
466 udatalen
= strlen(keyfilter
) + 8;
467 MALLOC_FILTER_UDATA(filter
, filterlen
, userdata
,
472 (void) snprintf(filter
, filterlen
,
473 PROF_ATTR_FILTER
, keyfilter
);
475 (void) snprintf(filter
, filterlen
,
476 TNRHTP_FILTER
, keyfilter
);
478 (void) snprintf(filter
, filterlen
,
480 maplist
[i
].objectclass
, keyfilter
);
482 (void) snprintf(userdata
, udatalen
,
483 "(&(%%s)%s)", keyfilter
);
489 /* special cases for automounter and other services */
491 /* auto_* services */
492 if (strncasecmp(database
, "auto_", 5) == SAME
) {
494 if ((keyfilter
= set_keys(key
, "automountKey"))
496 filterlen
= strlen(keyfilter
) + 27;
497 udatalen
= strlen(keyfilter
) + 8;
498 MALLOC_FILTER_UDATA(filter
, filterlen
,
499 userdata
, udatalen
, nomem
);
501 (void) snprintf(filter
, filterlen
,
502 "(&(objectclass=automount)%s)",
504 (void) snprintf(userdata
, udatalen
,
505 "(&(%%s)%s)", keyfilter
);
510 MALLOC_FILTER_UDATA(filter
, filterlen
,
511 userdata
, udatalen
, nomem
);
513 (void) strlcpy(filter
,
514 "objectclass=automount", filterlen
);
515 (void) strlcpy(userdata
, "%s",
520 if ((keyfilter
= set_keys(key
, "cn")) != NULL
) {
521 filterlen
= strlen(keyfilter
) + 27;
522 udatalen
= strlen(keyfilter
) + 8;
523 MALLOC_FILTER_UDATA(filter
, filterlen
,
524 userdata
, udatalen
, nomem
);
526 (void) snprintf(filter
, filterlen
,
527 "(&(objectclass=nisObject)%s)",
529 (void) snprintf(userdata
, udatalen
,
530 "(&(%%s)%s)", keyfilter
);
535 MALLOC_FILTER_UDATA(filter
, filterlen
,
536 userdata
, udatalen
, nomem
);
538 (void) strlcpy(filter
,
539 "objectclass=nisObject", filterlen
);
540 (void) strlcpy(userdata
, "%s",
548 /* automount service */
549 if (strcasecmp(database
, "automount") == SAME
) {
551 if ((keyfilter
= set_keys(key
, "automountMapName"))
553 filterlen
= strlen(keyfilter
) + 30;
554 udatalen
= strlen(keyfilter
) + 8;
555 MALLOC_FILTER_UDATA(filter
, filterlen
,
556 userdata
, udatalen
, nomem
);
558 (void) snprintf(filter
, filterlen
,
559 "(&(objectclass=automountMap)%s)",
561 (void) snprintf(userdata
, udatalen
,
562 "(&(%%s)%s)", keyfilter
);
567 MALLOC_FILTER_UDATA(filter
, filterlen
,
568 userdata
, udatalen
, nomem
);
570 (void) strlcpy(filter
,
571 "objectclass=automountMap",
573 (void) strlcpy(userdata
, "%s",
578 if ((keyfilter
= set_keys(key
, "nisMapName"))
580 filterlen
= strlen(keyfilter
) + 24;
581 udatalen
= strlen(keyfilter
) + 8;
582 MALLOC_FILTER_UDATA(filter
, filterlen
,
583 userdata
, udatalen
, nomem
);
585 (void) snprintf(filter
, filterlen
,
586 "(&(objectclass=nisMap)%s)",
588 (void) snprintf(userdata
, udatalen
,
589 "(&(%%s)%s)", keyfilter
);
594 MALLOC_FILTER_UDATA(filter
, filterlen
,
595 userdata
, udatalen
, nomem
);
597 (void) strlcpy(filter
,
598 "objectclass=nisMap", filterlen
);
599 (void) strlcpy(userdata
, "%s",
607 /* other services (catch all) */
608 if ((keyfilter
= set_keys(key
, "cn")) == NULL
) {
611 MALLOC_FILTER_UDATA(filter
, filterlen
, userdata
, udatalen
,
614 (void) snprintf(filter
, filterlen
, "objectclass=*");
615 (void) strlcpy(userdata
, "%s", udatalen
);
618 filterlen
= strlen(keyfilter
) + 1;
619 udatalen
= strlen(keyfilter
) + 8;
620 MALLOC_FILTER_UDATA(filter
, filterlen
, userdata
, udatalen
,
623 (void) snprintf(filter
, filterlen
, "%s", keyfilter
);
624 (void) snprintf(userdata
, udatalen
, "(&(%%s)%s)",
631 (void) fprintf(stdout
, "set_filter: filter=\"%s\"\n", filter
);
632 (void) fprintf(stdout
, "set_filter: userdata=\"%s\"\n", userdata
);