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 (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
34 #include "standalone.h"
36 extern char *set_filter(char **, char *, char **);
37 extern char *set_filter_publickey(char **, char *, int, char **);
38 extern void _printResult(ns_ldap_result_t
*);
39 extern void printMapping();
45 static struct database_t
{
49 { NS_LDAP_TYPE_HOSTS
, "cn" },
50 { NS_LDAP_TYPE_IPNODES
, "cn" },
51 { NS_LDAP_TYPE_RPC
, "cn" },
52 { NS_LDAP_TYPE_PROTOCOLS
, "cn" },
53 { NS_LDAP_TYPE_NETWORKS
, "ipnetworknumber" },
54 { NS_LDAP_TYPE_SERVICES
, "cn" },
55 { NS_LDAP_TYPE_GROUP
, "gidnumber" },
56 { NS_LDAP_TYPE_NETMASKS
, "ipnetworknumber"},
57 { NS_LDAP_TYPE_ETHERS
, "cn" },
58 { NS_LDAP_TYPE_NETGROUP
, "cn" },
59 { NS_LDAP_TYPE_BOOTPARAMS
, "cn" },
60 { NS_LDAP_TYPE_PUBLICKEY
, "cn" },
61 { NS_LDAP_TYPE_PASSWD
, "uid" },
62 { NS_LDAP_TYPE_SHADOW
, "uid" },
63 { NS_LDAP_TYPE_ALIASES
, "cn" },
64 { NS_LDAP_TYPE_AUTOMOUNT
, "automountKey" },
65 { NS_LDAP_TYPE_USERATTR
, "uid" },
66 { NS_LDAP_TYPE_PROFILE
, "cn" },
67 { NS_LDAP_TYPE_EXECATTR
, "cn" },
68 { NS_LDAP_TYPE_AUTHATTR
, "cn" },
69 { NS_LDAP_TYPE_PROJECT
, "SolarisProjectName" },
77 (void) fprintf(stderr
, "%s\n", msg
);
79 (void) fprintf(stderr
,
82 "usage: ldaplist [-dlv] [-h LDAP_server[:serverPort] [-M domainName]\n"
83 "[-N profileName] [-a authenticationMethod] [-P certifPath]\n"
84 "[-D bindDN] [-w bindPassword] [-j passwdFile]]\n"
85 "[<database> [<key>] ...]\n\n"
86 "usage: ldaplist -h\n"
88 "usage: ldaplist -g\n\n"
90 "\t -l list all the attributes found in entry.\n"
91 "\t By default, it lists only the DNs.\n"
92 "\t -d list attributes for the database instead of its entries\n"
93 "\t -v print out the LDAP search filter.\n"
94 "\t -g list the database mappings.\n"
95 "\t -h An address (or a name) and a port of the LDAP server in\n"
96 "\t which the entries will be stored. The default value for\n"
97 "\t the port is 389 (or 636 for TLS connections).\n"
98 "\t -M The name of a domain served by the specified server.\n"
99 "\t If not specified, the default domain name will be used.\n"
100 "\t -N Specifies a DUAProfile name.\n"
101 "\t The default value is \"default\".\n"
102 "\t -a Specifies an authentication method.\n"
103 "\t -P The certificate path for the location of the certificate\n"
105 "\t -D Specifies an entry which has read permission to\n"
106 "\t the requested database.\n"
107 "\t -w Password to be used for authenticating the bindDN.\n"
108 "\t -j File containing the password for bindDN or SSL key db.\n"
109 "\t<database> is the database to be searched in. Standard system\n"
111 "\t\tpassword, printers, group, hosts, ethers, networks, netmasks,\n"
112 "\t\trpc, bootparams, protocols, services, netgroup, auto_*.\n"
113 "\tNon-standard system databases can be specified as follows:\n"
114 "\t\tby specific container: ou=<dbname> or\n"
115 "\t\tby default container: <dbname>. In this case, 'nismapname'\n"
116 "\t\twill be used, thus mapping this to nismapname=<dbname>.\n"
117 "\t<key> is the key to search in the database. For the standard\n"
118 "\tdatabases, the search type for the key is predefined. You can\n"
119 "\toverride this by specifying <type>=<key>.\n"
120 "\nNOTE: The old -h option printing the mapping information is "
121 "deprecated.\nFor backward compatibility the following mode is "
122 "available:\nldaplist -h\n"));
127 * This is a generic filter call back function for
128 * merging the filter from service search descriptor with
129 * an existing search filter. This routine expects userdata
130 * contain a format string with a single %s in it, and will
131 * use the format string with sprintf() to insert the SSD filter.
133 * This routine is passed to the __ns_ldap_list() or
134 * __ns_ldap_firstEntry() APIs as the filter call back
135 * together with the userdata. For example,
136 * the "ldaplist hosts sys1" processing may call __ns_ldap_list()
137 * with "(&(objectClass=ipHost)(cn=sys1))" as filter, this function
138 * as the filter call back, and "(&(%s)(cn=sys1))" as the
139 * userdata, this routine will in turn gets call to produce
140 * "(&(department=sds)(cn=sys1))" as the real search
141 * filter, if the input SSD contains a filter "department=sds".
144 merge_SSD_filter(const ns_ldap_search_desc_t
*desc
,
146 const void *userdata
)
152 if (realfilter
== NULL
)
153 return (NS_LDAP_INVALID_PARAM
);
156 if (desc
== NULL
|| desc
->filter
== NULL
||
158 return (NS_LDAP_INVALID_PARAM
);
160 /* Parameter check. We only want one %s here, otherwise bail. */
161 len
= 0; /* Reuse 'len' as "Number of %s hits"... */
162 checker
= (char *)userdata
;
164 checker
= strchr(checker
, '%');
165 if (checker
!= NULL
) {
166 if (len
> 0 || *(checker
+ 1) != 's')
167 return (NS_LDAP_INVALID_PARAM
);
168 len
++; /* Got our %s. */
171 return (NS_LDAP_INVALID_PARAM
);
172 } while (checker
!= NULL
);
174 len
= strlen(userdata
) + strlen(desc
->filter
) + 1;
176 *realfilter
= (char *)malloc(len
);
177 if (*realfilter
== NULL
)
178 return (NS_LDAP_MEMORY
);
180 (void) sprintf(*realfilter
, (char *)userdata
,
183 return (NS_LDAP_SUCCESS
);
186 /* returns 0=success, 1=error */
188 list(char *database
, char *ldapfilter
, char **ldapattribute
,
189 char **err
, char *userdata
)
191 ns_ldap_result_t
*result
;
192 ns_ldap_error_t
*errorp
;
195 const char *sort
= NULL
;
199 for (i
= 0; databaselist
[i
].database
; i
++) {
200 if (strcmp(databaselist
[i
].database
, database
) == 0) {
201 sort
= databaselist
[i
].sortattr
;
204 if (strcmp(databaselist
[i
].database
,
205 NS_LDAP_TYPE_AUTOMOUNT
) == 0 &&
206 strncmp(database
, NS_LDAP_TYPE_AUTOMOUNT
,
207 sizeof (NS_LDAP_TYPE_AUTOMOUNT
) - 1) == 0) {
208 sort
= databaselist
[i
].sortattr
;
216 rc
= __ns_ldap_list_sort(database
, (const char *)ldapfilter
,
217 sort
, merge_SSD_filter
, (const char **)ldapattribute
, NULL
,
218 listflag
, &result
, &errorp
, NULL
, userdata
);
219 if (rc
!= NS_LDAP_SUCCESS
) {
221 (void) __ns_ldap_err2str(rc
, &p
);
222 if (errorp
&& errorp
->message
) {
223 (void) snprintf(buf
, sizeof (buf
), "%s (%s)",
225 (void) __ns_ldap_freeError(&errorp
);
227 (void) snprintf(buf
, sizeof (buf
), "%s\n", p
);
232 _printResult(result
);
233 (void) __ns_ldap_freeResult(&result
);
242 case NS_LDAP_SUCCESS
:
244 case NS_LDAP_NOTFOUND
:
251 main(int argc
, char **argv
)
255 char *database
= NULL
;
256 char *ldapfilter
= NULL
;
257 char *attribute
= "dn";
259 char **ldapattribute
= NULL
;
269 ns_standalone_conf_t standalone_cfg
= standaloneDefaults
;
270 ns_ldap_error_t
*errorp
= NULL
;
271 char *authmech
= NULL
;
272 ns_auth_t auth
= {NS_LDAP_AUTH_NONE
,
275 NS_LDAP_SASLOPT_NONE
};
277 (void) setlocale(LC_ALL
, "");
278 (void) textdomain(TEXT_DOMAIN
);
280 openlog("ldaplist", LOG_PID
, LOG_USER
);
283 strlen(argv
[1]) == 2 && strncmp(argv
[1], "-h", 2) == 0) {
284 /* preserve backwards compatability, support old -h option */
285 (void) printMapping();
289 while ((c
= getopt(argc
, argv
, "h:M:N:P:r:a:D:w:j:dgvl")) != EOF
) {
292 listflag
|= NS_LDAP_SCOPE_BASE
;
295 (void) printMapping();
297 break; /* Never reached */
305 standalone_cfg
.type
= NS_LDAP_SERVER
;
306 standalone_cfg
.SA_DOMAIN
= optarg
;
309 standalone_cfg
.type
= NS_LDAP_SERVER
;
310 if (separatePort(optarg
,
311 &standalone_cfg
.SA_SERVER
,
312 &standalone_cfg
.SA_PORT
) > 0) {
317 standalone_cfg
.type
= NS_LDAP_SERVER
;
318 standalone_cfg
.SA_CERT_PATH
= optarg
;
321 standalone_cfg
.type
= NS_LDAP_SERVER
;
322 standalone_cfg
.SA_PROFILE_NAME
= optarg
;
325 standalone_cfg
.type
= NS_LDAP_SERVER
;
326 standalone_cfg
.SA_BIND_DN
= strdup(optarg
);
329 if (standalone_cfg
.SA_BIND_PWD
!= NULL
) {
330 (void) fprintf(stderr
,
331 gettext("The -w option is mutually "
332 "exclusive of -j. -w is ignored.\n"));
336 if (optarg
!= NULL
&&
337 optarg
[0] == '-' && optarg
[1] == '\0') {
338 /* Ask for a password later */
342 standalone_cfg
.type
= NS_LDAP_SERVER
;
343 standalone_cfg
.SA_BIND_PWD
= strdup(optarg
);
346 if (standalone_cfg
.SA_BIND_PWD
!= NULL
) {
347 (void) fprintf(stderr
,
348 gettext("The -w option is mutually "
349 "exclusive of -j. -w is ignored.\n"));
350 free(standalone_cfg
.SA_BIND_PWD
);
352 standalone_cfg
.type
= NS_LDAP_SERVER
;
353 standalone_cfg
.SA_BIND_PWD
= readPwd(optarg
);
354 if (standalone_cfg
.SA_BIND_PWD
== NULL
) {
362 usage(gettext("Invalid option"));
366 if (standalone_cfg
.type
== NS_LDAP_SERVER
&&
367 standalone_cfg
.SA_SERVER
== NULL
) {
368 (void) fprintf(stderr
,
369 gettext("Please specify an LDAP server you want "
370 "to connect to. \n"));
374 if ((c
= argc
- optind
) > 0)
375 database
= argv
[optind
++];
379 if (authmech
!= NULL
) {
380 if (__ns_ldap_initAuth(authmech
,
382 &errorp
) != NS_LDAP_SUCCESS
) {
384 (void) fprintf(stderr
, "%s", errorp
->message
);
385 (void) __ns_ldap_freeError(&errorp
);
391 if (auth
.saslmech
!= NS_LDAP_SASL_GSSAPI
&&
392 standalone_cfg
.SA_BIND_DN
!= NULL
&&
393 standalone_cfg
.SA_BIND_PWD
== NULL
) {
394 /* If password is not specified, then prompt user for it. */
395 standalone_cfg
.SA_BIND_PWD
=
396 strdup(getpassphrase("Enter password:"));
399 standalone_cfg
.SA_AUTH
= (authmech
== NULL
) ? NULL
: &auth
;
401 if (__ns_ldap_initStandalone(&standalone_cfg
,
402 &errorp
) != NS_LDAP_SUCCESS
) {
404 (void) fprintf(stderr
, "%s\n", errorp
->message
);
405 (void) __ns_ldap_freeError(&errorp
);
410 if (authmech
!= NULL
) {
411 if (__ns_ldap_setParam(NS_LDAP_AUTH_P
,
412 authmech
, &errorp
) != NS_LDAP_SUCCESS
) {
413 __ns_ldap_cancelStandalone();
414 if (errorp
!= NULL
) {
415 (void) fprintf(stderr
, "%s", errorp
->message
);
416 (void) __ns_ldap_freeError(&errorp
);
421 if (standalone_cfg
.SA_CRED
!= NULL
) {
422 if (__ns_ldap_setParam(NS_LDAP_CREDENTIAL_LEVEL_P
,
423 standalone_cfg
.SA_CRED
, &errorp
) != NS_LDAP_SUCCESS
) {
424 __ns_ldap_cancelStandalone();
425 if (errorp
!= NULL
) {
426 (void) fprintf(stderr
, "%s", errorp
->message
);
427 (void) __ns_ldap_freeError(&errorp
);
433 if (standalone_cfg
.type
!= NS_CACHEMGR
&&
434 standalone_cfg
.SA_BIND_DN
!= NULL
) {
435 ns_auth_t
**authpp
= NULL
, **authp
= NULL
;
437 if (__ns_ldap_getParam(NS_LDAP_AUTH_P
,
439 &errorp
) != NS_LDAP_SUCCESS
|| authpp
== NULL
) {
440 __ns_ldap_cancelStandalone();
441 (void) __ns_ldap_freeParam((void ***)&authpp
);
443 (void) fprintf(stderr
,
444 gettext(errorp
->message
));
445 (void) __ns_ldap_freeError(&errorp
);
449 for (authp
= authpp
; *authp
; authp
++) {
450 if ((*authp
)->saslmech
== NS_LDAP_SASL_GSSAPI
) {
452 * For now we have no use for bindDN and
453 * bindPassword when using SASL/GSSAPI.
455 (void) fprintf(stderr
,
456 gettext("Warning: SASL/GSSAPI will be "
457 "used as an authentication method"
458 "The bind DN and password will "
466 * If dumpping a database,
467 * or all the containers,
468 * use page control just
469 * in case there are too many entries
471 if (!key
&& !(listflag
& NS_LDAP_SCOPE_BASE
))
472 listflag
|= NS_LDAP_PAGE_CTRL
;
474 /* build the attribute array */
475 if (strncasecmp(attribute
, "NULL", 4) == 0)
476 ldapattribute
= NULL
;
478 buffer
[0] = strdup(attribute
);
479 while ((p
= strchr(attribute
, ',')) != NULL
) {
480 buffer
[index
++] = attribute
= p
+ 1;
483 buffer
[index
] = NULL
;
484 ldapattribute
= buffer
;
487 /* build the filter */
488 if (database
&& (strcasecmp(database
, "publickey") == 0)) {
489 /* user publickey lookup */
494 ldapfilter
= set_filter_publickey(key
, database
, 0, &udata
);
497 (void) fprintf(stdout
,
498 gettext("+++ database=%s\n"),
499 (database
? database
: "NULL"));
500 (void) fprintf(stdout
,
501 gettext("+++ filter=%s\n"),
502 (ldapfilter
? ldapfilter
: "NULL"));
503 (void) fprintf(stdout
,
504 gettext("+++ template for merging"
506 (udata
? udata
: "NULL"));
508 rc
= list("passwd", ldapfilter
, ldapattribute
,
513 /* hosts publickey lookup */
514 ldapfilter
= set_filter_publickey(key
, database
, 1, &udata
);
517 (void) fprintf(stdout
,
518 gettext("+++ database=%s\n"),
519 (database
? database
: "NULL"));
520 (void) fprintf(stdout
,
521 gettext("+++ filter=%s\n"),
522 (ldapfilter
? ldapfilter
: "NULL"));
523 (void) fprintf(stdout
,
524 gettext("+++ template for merging"
526 (udata
? udata
: "NULL"));
528 rc1
= list("hosts", ldapfilter
, ldapattribute
,
533 if (rc
== -1 && rc1
== -1) {
534 /* this should never happen */
535 (void) fprintf(stderr
,
536 gettext("ldaplist: invalid publickey lookup\n"));
538 } else if (rc
!= 0 && rc1
!= 0) {
539 (void) fprintf(stderr
,
540 gettext("ldaplist: %s\n"), (err
? err
: err1
));
545 exit(switch_err(rc
));
549 * we set the search filter to (objectclass=*) when we want
550 * to list the directory attribute instead of the entries
553 if (((ldapfilter
= set_filter(key
, database
, &udata
)) == NULL
) ||
554 (listflag
== NS_LDAP_SCOPE_BASE
)) {
555 ldapfilter
= strdup("objectclass=*");
556 udata
= strdup("%s");
560 (void) fprintf(stdout
, gettext("+++ database=%s\n"),
561 (database
? database
: "NULL"));
562 (void) fprintf(stdout
, gettext("+++ filter=%s\n"),
563 (ldapfilter
? ldapfilter
: "NULL"));
564 (void) fprintf(stdout
,
565 gettext("+++ template for merging SSD filter=%s\n"),
566 (udata
? udata
: "NULL"));
568 if (rc
= list(database
, ldapfilter
, ldapattribute
, &err
, udata
))
569 (void) fprintf(stderr
, gettext("ldaplist: %s\n"), err
);
571 __ns_ldap_cancelStandalone();
575 exit(switch_err(rc
));
576 return (0); /* Never reached */