Oracle Internet Directory Application Developer's Guide Release 2 (9.0.2) Part Number A95193-01 |
|
This chapter introduces the Oracle Internet Directory C API and provides examples of how to use it. It contains these topics:
The Oracle Internet Directory SDK C API is based on:
You can use the Oracle Internet Directory API Release 9.0.2 in the following modes:
The API uses TCP/IP to connect to an LDAP server. When it does this, it uses, by default, an unencrypted channel. To use the SSL mode, you must use the Oracle SSL call interface. You determine which mode you are using by the presence or absence of the SSL calls in the API usage. You can easily switch between SSL and non-SSL modes.
See Also:
"Sample C API Usage" for more details on how to use the two modes |
This section contains these topics:
Oracle SSL extensions to the LDAP API are based on standard SSL protocol. The SSL extensions provide encryption and decryption of data over the wire, and authentication.
There are three modes of authentication:
The type of authentication is indicated by a parameter in the SSL interface call.
There is only one call required to enable SSL:
int ldap_init_SSL(Sockbuf *sb, text *sslwallet, text *sslwalletpasswd, int sslauthmode)
The ldap_init_SSL call performs the necessary handshake between client and server using the standard SSL protocol. If the call is successful, all subsequent communication happens over a secure connection.
To use the SSL feature, both the server and the client may require wallets, depending on which authentication mode is being used. Release 9.0.2 of the API supports only Oracle Wallet. You can create wallets using Oracle Wallet Manager.
This section contains these topics:
This section lists all the calls available in the LDAP C API found in RFC 1823.
See Also:
The following URL: http://www.ietf.org/rfc/rfc1823.txt for a more detailed explanation of these calls |
This section contains these topics:
ldap_init() initializes a session with an LDAP server. The server is not actually contacted until an operation is performed that requires it, allowing various options to be set after initialization.
LDAP *ldap_init (const char *hostname, int portno) ;
ldap_init() and ldap_open() both return a "session handle," a pointer to an opaque structure that MUST be passed to subsequent calls pertaining to the session. These routines return NULL if the session cannot be initialized in which case the operating system error reporting mechanism can be checked to see why the call failed.
Note that if you connect to an LDAPv2 server, one of the LDAP bind calls described below SHOULD be completed before other operations can be per formed on the session. LDAPv3 does not require that a bind operation be completed before other operations can be performed.
The calling program can set various attributes of the session by calling the routines described in the next section.
The LDAP session handle returned by ldap_init()
is a pointer to an opaque data type representing an LDAP session. In RFC 1823 this data type was a structure exposed to the caller, and various fields in the structure could be set to control aspects of the session, such as size and time limits on searches.
In the interest of insulating callers from inevitable changes to this structure, these aspects of the session are now accessed through a pair of accessor functions, described below.
ldap_get_option()
is used to access the current value of various session-wide parameters. ldap_set_option()
is used to set the value of these parameters. Note that some options are READ-ONLY and cannot be set; it is an error to call ldap_set_option()
and attempt to set a READ-ONLY option.
Note that if automatic referral following is enabled (the default), any connections created during the course of following referrals will inherit the options associated with the session that sent the original request that caused the referrals to be returned.
int ldap_get_option (LDAP *ld, int option, void *outvalue) ; int ldap_set_option (LDAP *ld, int option, const void *invalue) ;
#define LDAP_OPT_ON ((void *)1) #define LDAP_OPT_OFF ((void *)0)
Parameters | Description |
---|---|
ld |
The session handle. If this is NULL, a set of global defaults is accessed. New LDAP session handles created with ldap_init() or ldap_open() inherit their characteristics from these global defaults. |
option |
The name of the option being accessed or set. This parameter SHOULD be one of the constants listed and described in Table 3-4. After the constant the actual hexadecimal value of the constant is listed in parentheses. |
outvalue |
The address of a place to put the value of the option. The actual type of this parameter depends on the setting of the option parameter. For outvalues of type char ** and LDAPControl **, a copy of the data that is associated with the LDAP session ld is returned; callers should dispose of the memory by calling ldap_memfree() or ldap_controls_free(), depending on the type of data returned. |
invalue |
A pointer to the value the option is to be given. The actual type of this parameter depends on the setting of the option parameter. The data associated with invalue is copied by the API implementation to allow callers of the API to dispose of or otherwise change their copy of the data after a successful call to ldap_set_option(). If a value passed for invalue is invalid or cannot be accepted by the implementation, ldap_set_option() should return -1 to indicate an error. |
Both ldap_get_option() and ldap_set_option() return 0 if successful and -1 if an error occurs. If -1 is returned by either function, a specific error code MAY be retrieved by calling ldap_get_option() with an option value of LDAP_OPT_ERROR_NUMBER. Note that there is no way to retrieve a more specific error code if a call to ldap_get_option() with an option value of LDAP_OPT_ERROR_NUMBER fails.
When a call to ldap_get_option() succeeds, the API implementation MUST NOT change the state of the LDAP session handle or the state of the underlying implementation in a way that affects the behavior of future LDAP API calls. When a call to ldap_get_option() fails, the only session handle change permitted is setting the LDAP error code (as returned by the LDAP_OPT_ERROR_NUMBER option).
When a call to ldap_set_option() fails, it MUST NOT change the state of the LDAP session handle or the state of the underlying implementation in a way that affects the behavior of future LDAP API calls.
Standards track documents that extend this specification and specify new options SHOULD use values for option macros that are between 0x1000 and 0x3FFF inclusive. Private and experimental extensions SHOULD use values for the option macros that are between 0x4000 and 0x7FFF inclusive. All values below 0x1000 and above 0x7FFF that are not defined in this document are reserved and SHOULD NOT be used. The following macro MUST be defined by C LDAP API implementations to aid extension implementors:
#define LDAP_OPT_PRIVATE_EXTENSION_BASE 0x4000 /* to 0x7FFF inclusive */
LDAPv3 operations can be extended through the use of controls. Controls can be sent to a server or returned to the client with any LDAP message. These controls are referred to as server controls.
The LDAP API also supports a client-side extension mechanism through the use of client controls. These controls affect the behavior of the LDAP API only and are never sent to a server. A common data structure is used to represent both types of controls:
typedef struct ldapcontrol {char *ldctl_oid; struct berval ldctl_value; char ldctl_iscritical;} LDAPControl;
The fields in the ldapcontrol structure have the following meanings:
Some LDAP API calls allocate an ldapcontrol structure or a NULL-terminated array of ldapcontrol structures. The following routines can be used to dispose of a single control or an array of controls:
void ldap_control_free( LDAPControl *ctrl ); void ldap_controls_free( LDAPControl **ctrls );
If the ctrl
or ctrls
parameter is NULL, these calls do nothing.
A set of controls that affect the entire session can be set using the ldap_set_option()
function (see above). A list of controls can also be passed directly to some LDAP API calls such as ldap_search_ext(), in which case any controls set for the session through the use of ldap_set_option() are ignored. Control lists are represented as a NULL-terminated array of pointers to ldapcontrol structures.
Server controls are defined by LDAPv3 protocol extension documents; for example, a control has been proposed to support server-side sorting of search results.
One client control is defined in this document (described in the following section). Other client controls MAY be defined in future revisions of this document or in documents that extend this API.
As described previously in "LDAP Session Handle Options", applications can enable and disable automatic chasing of referrals on a session-wide basic by using the ldap_set_option() function with the LDAP_OPT_REFERRALS option. It is also useful to govern automatic referral chasing on per-request basis. A client control with an OID of 1.2.840.113556.1.4.616 exists to provide this functionality.
/* OID for referrals client control */ #define LDAP_CONTROL_REFERRALS "1.2.840.113556.1.4.616" /* Flags for referrals client control value */ #define LDAP_CHASE_SUBORDINATE_REFERRALS 0x00000020U #define LDAP_CHASE_EXTERNAL_REFERRALS 0x00000040U
To create a referrals client control, the ldctl_oid field of an LDAPControl structure MUST be set to LDAP_CONTROL_REFERRALS ("1.2.840.113556.1.4.616") and the ldctl_value field MUST be set to a 4-octet value that contains a set of flags. The ldctl_value.bv_len field MUST always be set to 4. The ldctl_value.bv_val field MUST point to a 4-octet integer flags value. This flags value can be set to zero to disable automatic chasing of referrals and LDAPv3 references altogether. Alternatively, the flags value can be set to the value LDAP_CHASE_SUBORDINATE_REFERRALS (0x00000020U) to indicate that only LDAPv3 search continuation references are to be automatically chased by the API implementation, to the value LDAP_CHASE_EXTERNAL_REFERRALS (0x00000040U) to indicate that only LDAPv3 referrals are to be automatically chased, or the logical OR of the two flag values (0x00000060U) to indicate that both referrals and references are to be automatically chased.
The following functions are used to authenticate an LDAP client to an LDAP directory server.
The ldap_sasl_bind() and ldap_sasl_bind_s() functions can be used to do general and extensible authentication over LDAP through the use of the Simple Authentication Security Layer. The routines both take the dn to bind as, the method to use, as a dotted-string representation of an object identifier identifying the method, and a struct berval holding the credentials. The special constant value LDAP_SASL_SIMPLE (NULL) can be passed to request simple authentication, or the simplified routines ldap_simple_bind() or ldap_simple_bind_s() can be used.
int ldap_sasl_bind (LDAP *ld, const char *dn, const char *mechanism, const struct berval *cred, LDAPControl **serverctrls, LDAPControl **clientctrls, int *msgidp
);
int ldap_sasl_bind_s(LDAP *ld, const char *dn, const char *mechanism, const struct berval *cred, LDAPControl **serverctrls, LDAPControl **clientctrls, struct berval **servercredp);
int ldap_simple_bind(LDAP *ld, const char *dn, const char *passwd);
int ldap_simple_bind_s(LDAP *ld, const char *dn, const char *passwd);
The use of the following routines is deprecated and more complete descriptions can be found in RFC 1823:
int ldap_bind( LDAP *ld, const char *dn, const char *cred, int method );
int ldap_bind_s( LDAP *ld, const char *dn, const char *cred, int method );
int ldap_kerberos_bind( LDAP *ld, const char *dn );
int ldap_kerberos_bind_s( LDAP *ld, const char *dn );
Additional parameters for the deprecated routines are not described. Interested readers are referred to RFC 1823.
The ldap_sasl_bind() function initiates an asynchronous bind operation and returns the constant LDAP_SUCCESS if the request was successfully sent, or another LDAP error code if not. See the section below on error handling for more information about possible errors and how to interpret them. If successful, ldap_sasl_bind() places the message id of the request in *msgidp. A subsequent call to ldap_result(), described below, can be used to obtain the result of the bind.
The ldap_simple_bind() function initiates a simple asynchronous bind operation and returns the message id of the operation initiated. A subsequent call to ldap_result(), described below, can be used to obtain the result of the bind. In case of error, ldap_simple_bind() will return -1, setting the session error parameters in the LDAP structure appropriately.
The synchronous ldap_sasl_bind_s() and ldap_simple_bind_s() functions both return the result of the operation, either the constant LDAP_SUCCESS if the operation was successful, or another LDAP error code if it was not. See the section below on error handling for more information about possible errors and how to interpret them.
Note that if an LDAPv2 server is contacted, no other operations over the connection can be attempted before a bind call has successfully completed.
Subsequent bind calls can be used to re-authenticate over the same connection, and multistep SASL sequences can be accomplished through a sequence of calls to ldap_sasl_bind() or ldap_sasl_bind_s().
The following functions are used to unbind from the directory, close open connections, and dispose of the session handle.
int ldap_unbind_ext( LDAP *ld, LDAPControl **serverctrls, LDAPControl **clientctrls ); int ldap_unbind( LDAP *ld ); int ldap_unbind_s( LDAP *ld );
Parameter | Description |
---|---|
ld |
The session handle |
serverctrls |
List of LDAP server controls |
clientctrls |
List of client controls |
The ldap_unbind_ext(), ldap_unbind() and ldap_unbind_s() all work synchronously in the sense that they send an unbind request to the server, close all open connections associated with the LDAP session handle, and dispose of all resources associated with the session handle before returning. Note, however, that there is no server response to an LDAP unbind operation. All three of the unbind functions return LDAP_SUCCESS (or another LDAP error code if the request cannot be sent to the LDAP server). After a call to one of the unbind functions, the session handle ld is invalid and it is illegal to make any further LDAP API calls using ld.
The ldap_unbind() and ldap_unbind_s() functions behave identically. The ldap_unbind_ext() function allows server and client controls to be included explicitly, but note that since there is no server response to an unbind request there is no way to receive a response to a server control sent with an unbind request.
These functions are used to search the LDAP directory, returning a requested set of attributes for each entry matched.
int ldap_search_ext (LDAP *ld, const char *base, int scope, const char *filter, char **attrs, int attrsonly, LDAPControl **serverctrls, LDAPControl **clientctrls, struct timeval *timeout, int sizelimit, int *msgidp); int ldap_search_ext_s (LDAP *ld, const char *base, int scope, const char *filter, char **attrs, int attrsonly, LDAPControl **serverctrls, LDAPControl **clientctrls, struct timeval *timeout, int sizelimit, LDAPMessage **res); int ldap_search (LDAP *ld, const char *base, int scope, const char *filter, char **attrs, int attrsonly); int ldap_search_s (LDAP *ld, const char *base, int scope, const char *filter, char **attrs, int attrsonly, LDAPMessage **res); int ldap_search_st
(
LDAP *ld, const char *base, int scope, const char *filter, char **attrs, int attrsonly, struct timeval *timeout, LDAPMessage **res
);
The ldap_search_ext() function initiates an asynchronous search operation and returns the constant LDAP_SUCCESS if the request was successfully sent, or another LDAP error code if not. See the section below on error handling for more information about possible errors and how to interpret them. If successful, ldap_search_ext() places the message id of the request in *msgidp. A subsequent call to ldap_result(), described below, can be used to obtain the results from the search. These results can be parsed using the result parsing routines described in detail later.
Similar to ldap_search_ext(), the ldap_search() function initiates an asynchronous search operation and returns the message id of the operation initiated. As for ldap_search_ext(), a subsequent call to ldap_result(), described below, can be used to obtain the result of the bind. In case of error, ldap_search() will return -1, setting the session error parameters in the LDAP structure appropriately.
The synchronous ldap_search_ext_s(), ldap_search_s(), and ldap_search_st() functions all return the result of the operation, either the constant LDAP_SUCCESS if the operation was successful, or another LDAP error code if it was not. See the section below on error handling for more information about possible errors and how to interpret them. Entries returned from the search (if any) are contained in the res parameter. This parameter is opaque to the caller. Entries, attributes, values, etc., can be extracted by calling the parsing routines described below. The results contained in res SHOULD be freed when no longer in use by calling ldap_msgfree(), described later.
The ldap_search_ext() and ldap_search_ext_s() functions support LDAPv3 server controls, client controls, and allow varying size and time limits to be easily specified for each search operation. The ldap_search_st() function is identical to ldap_search_s() except that it takes an additional parameter specifying a local timeout for the search. The local search timeout is used to limit the amount of time the API implementation will wait for a search to complete. After the local search timeout expires, the API implementation will send an abandon operation to abort the search operation.
LDAP does not support a read operation directly. Instead, this operation is emulated by a search with base set to the DN of the entry to read, scope set to LDAP_SCOPE_BASE, and filter set to "(objectclass=*)" or NULL. attrs contains the list of attributes to return.
LDAP does not support a list operation directly. Instead, this operation is emulated by a search with base set to the DN of the entry to list, scope set to LDAP_SCOPE_ONELEVEL, and filter set to "(objectclass=*)" or NULL. attrs contains the list of attributes to return for each child entry.
These routines are used to compare a given attribute value assertion against an LDAP entry.
int ldap_compare_ext (LDAP *ld, const char *dn, const char *attr, const struct berval *bvalue, LDAPControl **serverctrls, LDAPControl **clientctrls, int *msgidp); int ldap_compare_ext_s (LDAP *ld, const char *dn, const char *attr, const struct berval *bvalue, LDAPControl **serverctrls, LDAPControl **clientctrls); int ldap_compare (LDAP *ld, const char *dn, const char *attr, const char *value); int ldap_compare_s (LDAP *ld, const char *dn, const char *attr, const char *value);
The ldap_compare_ext() function initiates an asynchronous compare operation and returns the constant LDAP_SUCCESS if the request was successfully sent, or another LDAP error code if not. See the section below on error handling for more information about possible errors and how to interpret them. If successful, ldap_compare_ext() places the message id of the request in *msgidp. A subsequent call to ldap_result(), described below, can be used to obtain the result of the compare.
Similar to ldap_compare_ext(), the ldap_compare() function initiates an asynchronous compare operation and returns the message id of the operation initiated. As for ldap_compare_ext(), a subsequent call to ldap_result(), described below, can be used to obtain the result of the bind. In case of error, ldap_compare() will return -1, setting the session error parameters in the LDAP structure appropriately.
The synchronous ldap_compare_ext_s() and ldap_compare_s() functions both return the result of the operation, either the constant LDAP_SUCCESS if the operation was successful, or another LDAP error code if it was not. See the section below on error handling for more information about possible errors and how to interpret them.
The ldap_compare_ext() and ldap_compare_ext_s() functions support LDAPv3 server controls and client controls.
These routines are used to modify an existing LDAP entry.
typedef struct ldapmod {int mod_op; char *mod_type; union mod_vals_u {char **modv_strvals; struct berval **modv_bvals; } mod_vals;} LDAPMod;#define mod_values mod_vals.modv_strvals #define mod_bvalues mod_vals.modv_bvalsint ldap_modify_ext (LDAP *ld, const char *dn, LDAPMod **mods, LDAPControl **serverctrls, LDAPControl **clientctrls, int *msgidp); int ldap_modify_ext_s (LDAP *ld, const char *dn, LDAPMod **mods, LDAPControl **serverctrls, LDAPControl **clientctrls); int ldap_modify (LDAP *ld, const char *dn, LDAPMod **mods );int ldap_modify_s (LDAP *ld, const char *dn, LDAPMod **mods);
The fields in the LDAPMod structure have the following meanings:
For LDAP_MOD_ADD modifications, the given values are added to the entry, creating the attribute if necessary.
For LDAP_MOD_DELETE modifications, the given values are deleted from the entry, removing the attribute if no values remain. If the entire attribute is to be deleted, the mod_vals field can be set to NULL.
For LDAP_MOD_REPLACE modifications, the attribute will have the listed values after the modification, having been created if necessary, or removed if the mod_vals field is NULL. All modifications are performed in the order in which they are listed.
The ldap_modify_ext() function initiates an asynchronous modify operation and returns the constant LDAP_SUCCESS if the request was successfully sent, or another LDAP error code if not. See the section below on error handling for more information about possible errors and how to interpret them. If successful, ldap_modify_ext() places the message id of the request in *msgidp. A subsequent call to ldap_result(), described below, can be used to obtain the result of the modify.
Similar to ldap_modify_ext(), the ldap_modify() function initiates an asynchronous modify operation and returns the message id of the operation initiated. As for ldap_modify_ext(), a subsequent call to ldap_result(), described below, can be used to obtain the result of the modify. In case of error, ldap_modify() will return -1, setting the session error parameters in the LDAP structure appropriately.
The synchronous ldap_modify_ext_s() and ldap_modify_s() functions both return the result of the operation, either the constant LDAP_SUCCESS if the operation was successful, or another LDAP error code if it was not. See the section below on error handling for more information about possible errors and how to interpret them.
The ldap_modify_ext() and ldap_modify_ext_s() functions support LDAPv3 server controls and client controls.
These routines are used to change the name of an entry.
int ldap_rename (LDAP *ld, const char *dn, const char *newrdn, const char *newparent, int deleteoldrdn, LDAPControl **serverctrls, LDAPControl **clientctrls, int *msgidp); int ldap_rename_s (LDAP *ld, const char *dn, const char *newrdn, const char *newparent, int deleteoldrdn, LDAPControl **serverctrls, LDAPControl **clientctrls);
The use of the following routines is deprecated and more complete descriptions can be found in RFC 1823:
int ldap_modrdn (LDAP *ld, const char *dn, const char *newrdn); int ldap_modrdn_s (LDAP *ld, const char *dn, const char *newrdn); int ldap_modrdn2 (LDAP *ld, const char *dn, const char *newrdn, int deleteoldrdn); int ldap_modrdn2_s (LDAP *ld, const char *dn, const char *newrdn, int deleteoldrdn);
The ldap_rename() function initiates an asynchronous modify DN operation and returns the constant LDAP_SUCCESS if the request was successfully sent, or another LDAP error code if not. See the section below on error handling for more information about possible errors and how to interpret them. If successful, ldap_rename() places the DN message id of the request in *msgidp. A subsequent call to ldap_result(), described below, can be used to obtain the result of the rename.
The synchronous ldap_rename_s() returns the result of the operation, either the constant LDAP_SUCCESS if the operation was successful, or another LDAP error code if it was not. See the section below on error handling for more information about possible errors and how to interpret them.
The ldap_rename() and ldap_rename_s() functions both support LDAPv3 server controls and client controls.
These functions are used to add entries to the LDAP directory.
int ldap_add_ext (LDAP *ld, const char *dn, LDAPMod **attrs, LDAPControl **serverctrls, LDAPControl **clientctrls, int *msgidp); int ldap_add_ext_s (LDAP *ld, const char *dn, LDAPMod **attrs, LDAPControl **serverctrls, LDAPControl **clientctrls); int ldap_add (LDAP *ld, const char *dn, LDAPMod **attrs); int ldap_add_s (LDAP *ld, const char *dn, LDAPMod **attrs);
Note that the parent of the entry being added must already exist or the parent must be empty (i.e., equal to the root DN) for an add to succeed.
The ldap_add_ext() function initiates an asynchronous add operation and returns the constant LDAP_SUCCESS if the request was successfully sent, or another LDAP error code if not. See the section below on error handling for more information about possible errors and how to interpret them. If successful, ldap_add_ext() places the message id of the request in *msgidp. A subsequent call to ldap_result(), described below, can be used to obtain the result of the add.
Similar to ldap_add_ext(), the ldap_add() function initiates an asynchronous add operation and returns the message id of the operation initiated. As for ldap_add_ext(), a subsequent call to ldap_result(), described below, can be used to obtain the result of the add. In case of error, ldap_add() will return -1, setting the session error parameters in the LDAP structure appropriately.
The synchronous ldap_add_ext_s() and ldap_add_s() functions both return the result of the operation, either the constant LDAP_SUCCESS if the operation was successful, or another LDAP error code if it was not. See the section below on error handling for more information about possible errors and how to interpret them.
The ldap_add_ext() and ldap_add_ext_s() functions support LDAPv3 server controls and client controls.
These functions are used to delete a leaf entry from the LDAP directory.
int ldap_delete_ext (
LDAP *ld, const char *dn, LDAPControl **serverctrls, LDAPControl **clientctrls, int *msgidp ); int ldap_delete_ext_s ( LDAP *ld,
const char *dn, LDAPControl **serverctrls, LDAPControl **clientctrls ); int ldap_delete
(
LDAP *ld, const char *dn ); int ldap_delete_s (
LDAP *ld, const char *dn );
Note that the entry to delete must be a leaf entry (i.e., it must have no children). Deletion of entire subtrees in a single operation is not supported by LDAP.
The ldap_delete_ext() function initiates an asynchronous delete operation and returns the constant LDAP_SUCCESS if the request was successfully sent, or another LDAP error code if not. See the section below on error handling for more information about possible errors and how to interpret them. If successful, ldap_delete_ext() places the message id of the request in *msgidp. A subsequent call to ldap_result(), described below, can be used to obtain the result of the delete.
Similar to ldap_delete_ext(), the ldap_delete() function initiates an asynchronous delete operation and returns the message id of the operation initiated. As for ldap_delete_ext(), a subsequent call to ldap_result(), described below, can be used to obtain the result of the delete. In case of error, ldap_delete() will return -1, setting the session error parameters in the LDAP structure appropriately.
The synchronous ldap_delete_ext_s() and ldap_delete_s() functions both return the result of the operation, either the constant LDAP_SUCCESS if the operation was successful, or another LDAP error code if it was not. See the section below on error handling for more information about possible errors and how to interpret them.
The ldap_delete_ext() and ldap_delete_ext_s() functions support LDAPv3 server controls and client controls.
These routines allow extended LDAP operations to be passed to the server, providing a general protocol extensibility mechanism.
int ldap_extended_operation (LDAP *ld, const char *requestoid, const struct berval *requestdata, LDAPControl **serverctrls, LDAPControl **clientctrls, int *msgidp); int ldap_extended_operation_s (LDAP *ld, const char *requestoid, const struct berval *requestdata, LDAPControl **serverctrls, LDAPControl **clientctrls, char **retoidp, struct berval **retdatap);
The ldap_extended_operation() function initiates an asynchronous extended operation and returns the constant LDAP_SUCCESS if the request was successfully sent, or another LDAP error code if not. See the section below on error handling for more information about possible errors and how to interpret them. If successful, ldap_extended_operation() places the message id of the request in *msgidp. A subsequent call to ldap_result(), described below, can be used to obtain the result of the extended operation which can be passed to ldap_parse_extended_result() to obtain the OID and data contained in the response.
The synchronous ldap_extended_operation_s() function returns the result of the operation, either the constant LDAP_SUCCESS if the operation was successful, or another LDAP error code if it was not. See the section below on error handling for more information about possible errors and how to interpret them. The retoid and retdata parameters are filled in with the OID and data from the response. If no OID or data was returned, these parameters are set to NULL.
The ldap_extended_operation() and ldap_extended_operation_s() functions both support LDAPv3 server controls and client controls.
These calls are used to abandon an operation in progress:
int ldap_abandon_ext (LDAP *ld, int msgid, LDAPControl **serverctrls, LDAPControl **clientctrls); int ldap_abandon (LDAP *ld, int msgid);
Parameter | Description |
---|---|
ld |
The session handle. |
msgid |
The message id of the request to be abandoned. |
serverctrls |
List of LDAP server controls. |
clientctrls |
List of client controls. |
ldap_abandon_ext() abandons the operation with message id msgid and returns the constant LDAP_SUCCESS if the abandon was successful or another LDAP error code if not. See the section below on error handling for more information about possible errors and how to interpret them.
ldap_abandon() is identical to ldap_abandon_ext() except that it does not accept client or server controls and it returns zero if the abandon was successful, -1 otherwise.
After a successful call to ldap_abandon() or ldap_abandon_ext(), results with the given message id are never returned from a subsequent call to ldap_result(). There is no server response to LDAP abandon operations.
ldap_result() is used to obtain the result of a previous asynchronously initiated operation. Note that depending on how it is called, ldap_result() can actually return a list or "chain" of result messages. The ldap_result() function only returns messages for a single request, so for all LDAP operations other than search only one result message is expected; that is, the only time the "result chain" can contain more than one message is if results from a search operation are returned.
Once a chain of messages has been returned to the caller, it is no longer tied in any caller-visible way to the LDAP request that produced it. Therefore, a chain of messages returned by calling ldap_result() or by calling a synchronous search routine will never be affected by subsequent LDAP API calls (except for ldap_msgfree() which is used to dispose of a chain of messages).
ldap_msgfree() frees the result messages (possibly an entire chain of messages) obtained from a previous call to ldap_result() or from a call to a synchronous search routine.
ldap_msgtype() returns the type of an LDAP message. ldap_msgid() returns the message ID of an LDAP message.
int ldap_result (LDAP *ld, int msgid, int all, struct timeval *timeout, LDAPMessage **res); int ldap_msgfree( LDAPMessage *res ); int ldap_msgtype( LDAPMessage *res ); int ldap_msgid( LDAPMessage *res );
Upon successful completion, ldap_result() returns the type of the first result returned in the res
parameter. This will be one of the following constants.
LDAP_RES_BIND (0x61)
LDAP_RES_SEARCH_ENTRY (0x64)
LDAP_RES_SEARCH_REFERENCE (0x73) -- new in LDAPv3
LDAP_RES_SEARCH_RESULT (0x65)
LDAP_RES_MODIFY (0x67)
LDAP_RES_ADD (0x69)
LDAP_RES_DELETE (0x6B)
LDAP_RES_MODDN (0x6D)
LDAP_RES_COMPARE (0x6F)
LDAP_RES_EXTENDED (0x78) -- new in LDAPv3
ldap_result() returns 0 if the timeout expired and -1 if an error occurs, in which case the error parameters of the LDAP session handle will be set accordingly.
ldap_msgfree() frees each message in the result chain pointed to by res and returns the type of the last message in the chain. If res is NULL, nothing is done and the value zero is returned.
ldap_msgtype() returns the type of the LDAP message it is passed as a parameter. The type will be one of the types listed above, or -1 on error.
ldap_msgid() returns the message ID associated with the LDAP message passed as a parameter, or -1 on error.
These calls are used to extract information from results and handle errors returned by other LDAP API routines. Note that ldap_parse_sasl_bind_result() and ldap_parse_extended_result() must typically be used in addition to ldap_parse_result() to retrieve all the result information from SASL Bind and Extended Operations respectively.
int ldap_parse_result (
LDAP *ld, LDAPMessage *res, int *errcodep, char **matcheddnp, char **errmsgp, char ***referralsp, LDAPControl ***serverctrlsp, int freeit );
int ldap_parse_sasl_bind_result (
LDAP *ld, LDAPMessage *res, struct berval **servercredp, int freeit );
int ldap_parse_extended_result (
LDAP *ld, LDAPMessage *res, char **retoidp, struct berval **retdatap, int freeit ); #define LDAP_NOTICE_OF_DISCONNECTION "1.3.6.1.4.1.1466.20036" char *ldap_err2string( int err );
The use of the following routines is deprecated and more complete descriptions can be found in RFC 1823:
int ldap_result2error (
LDAP *ld, LDAPMessage *res, int freeit );
void ldap_perror( LDAP *ld, const char *msg );
Additional parameters for the deprecated routines are not described. Interested readers are referred to RFC 1823.
The ldap_parse_result(), ldap_parse_sasl_bind_result(), and ldap_parse_extended_result() functions all skip over messages of type LDAP_RES_SEARCH_ENTRY and LDAP_RES_SEARCH_REFERENCE when looking for a result message to parse. They return the constant LDAP_SUCCESS if the result was successfully parsed and another LDAP error code if not. Note that the LDAP error code that indicates the outcome of the operation performed by the server is placed in the errcodep ldap_parse_result() parameter. If a chain of messages that contains more than one result message is passed to these routines they always operate on the first result in the chain.
ldap_err2string() is used to convert a numeric LDAP error code, as returned by ldap_parse_result(), ldap_parse_sasl_bind_result(), ldap_parse_extended_result() or one of the synchronous API operation calls, into an informative zero-terminated character string message describing the error. It returns a pointer to static data.
These routines are used to step through the list of messages in a result chain returned by ldap_result()
. For search operations, the result chain can actually include referral messages, entry messages, and result messages.
ldap_count_messages()
is used to count the number of messages returned. The ldap_msgtype()
function, described above, can be used to distinguish between the different message types.
LDAPMessage *ldap_first_message( LDAP *ld, LDAPMessage *res ); LDAPMessage *ldap_next_message( LDAP *ld, LDAPMessage *msg ); int ldap_count_messages( LDAP *ld, LDAPMessage *res );
ldap_first_message() and ldap_next_message() will return NULL when no more messages exist in the result set to be returned. NULL is also returned if an error occurs while stepping through the entries, in which case the error parameters in the session handle ld will be set to indicate the error.
If successful, ldap_count_messages() returns the number of messages contained in a chain of results; if an error occurs such as the res parameter being invalid, -1 is returned. The ldap_count_messages() call can also be used to count the number of messages that remain in a chain if called with a message, entry, or reference returned by ldap_first_message(), ldap_next_message(), ldap_first_entry(), ldap_next_entry(), ldap_first_reference(), ldap_next_reference().
The following calls are used to parse the entries and references returned by ldap_search() and friends. These results are returned in an opaque structure that MAY be accessed by calling the routines described below. Routines are provided to step through the entries and references returned, step through the attributes of an entry, retrieve the name of an entry, and retrieve the values associated with a given attribute in an entry.
The ldap_first_entry() and ldap_next_entry() routines are used to step through and retrieve the list of entries from a search result chain. The ldap_first_reference() and ldap_next_reference() routines are used to step through and retrieve the list of continuation references from a search result chain. ldap_count_entries() is used to count the number of entries returned. ldap_count_references() is used to count the number of references returned.
LDAPMessage *ldap_first_entry( LDAP *ld, LDAPMessage *res ); LDAPMessage *ldap_next_entry( LDAP *ld, LDAPMessage *entry ); LDAPMessage *ldap_first_reference( LDAP *ld, LDAPMessage *res ); LDAPMessage *ldap_next_reference( LDAP *ld, LDAPMessage *ref ); int ldap_count_entries( LDAP *ld, LDAPMessage *res ); int ldap_count_references( LDAP *ld, LDAPMessage *res );
ldap_first_entry(), ldap_next_entry(), ldap_first_reference() and ldap_next_reference() all return NULL when no more entries or references exist in the result set to be returned. NULL is also returned if an error occurs while stepping through the entries or references, in which case the error parameters in the session handle ld will be set to indicate the error.
ldap_count_entries() returns the number of entries contained in a chain of entries; if an error occurs such as the res parameter being invalid, -1 is returned. The ldap_count_entries() call can also be used to count the number of entries that remain in a chain if called with a message, entry or reference returned by ldap_first_message(), ldap_next_message(), ldap_first_entry(), ldap_next_entry(), ldap_first_reference(), ldap_next_reference().
ldap_count_references() returns the number of references contained in a chain of search results; if an error occurs such as the res parameter being invalid, -1 is returned. The ldap_count_references() call can also be used to count the number of references that remain in a chain.
These calls are used to step through the list of attribute types returned with an entry.
char *ldap_first_attribute
(LDAP *ld, LDAPMessage *entry, BerElement **ptr); char *ldap_next_attribute (LDAP *ld, LDAPMessage *entry, BerElement *ptr); void ldap_memfree( char *mem );
ldap_first_attribute() and ldap_next_attribute() will return NULL when the end of the attributes is reached, or if there is an error, in which case the error parameters in the session handle ld will be set to indicate the error.
Both routines return a pointer to an allocated buffer containing the current attribute name. This SHOULD be freed when no longer in use by calling ldap_memfree().
ldap_first_attribute() will allocate and return in ptr a pointer to a BerElement used to keep track of the current position. This pointer MAY be passed in subsequent calls to ldap_next_attribute() to step through the entry's attributes. After a set of calls to ldap_first_attribute() and ldap_next_attribute(), if ptr is non-NULL, it SHOULD be freed by calling ber_free( ptr, 0 ). Note that it is very important to pass the second parameter as 0 (zero) in this call, since the buffer associated with the BerElement does not point to separately allocated memory.
The attribute type names returned are suitable for passing in a call to ldap_get_values() and friends to retrieve the associated values.
ldap_get_values() and ldap_get_values_len() are used to retrieve the values of a given attribute from an entry. ldap_count_values() and ldap_count_values_len() are used to count the returned values.
ldap_value_free() and ldap_value_free_len() are used to free the values.
char **ldap_get_values (LDAP *ld, LDAPMessage *entry, const char *attr); struct berval **ldap_get_values_len (LDAP *ld, LDAPMessage *entry, const char *attr); int ldap_count_values( char **vals ); int ldap_count_values_len( struct berval **vals ); void ldap_value_free( char **vals ); void ldap_value_free_len( struct berval **vals );
Two forms of the various calls are provided. The first form is only suitable for use with non-binary character string data. The second _len form is used with any kind of data.
ldap_get_values() and ldap_get_values_len() return NULL if no values are found for attr or if an error occurs.
ldap_count_values() and ldap_count_values_len() return -1 if an error occurs such as the vals parameter being invalid.
If a NULL vals parameter is passed to ldap_value_free() or ldap_value_free_len(), nothing is done.
Note that the values returned are dynamically allocated and SHOULD be freed by calling either ldap_value_free() or ldap_value_free_len() when no longer in use.
ldap_get_dn() is used to retrieve the name of an entry. ldap_explode_dn() and ldap_explode_rdn() are used to break up a name into its component parts. ldap_dn2ufn() is used to convert the name into a more "user friendly" format.
char *ldap_get_dn( LDAP *ld, LDAPMessage *entry ); char **ldap_explode_dn( const char *dn, int notypes ); char **ldap_explode_rdn( const char *rdn, int notypes ); char *ldap_dn2ufn( const char *dn );
ldap_get_dn() will return NULL if there is some error parsing the dn, setting error parameters in the session handle ld to indicate the error. It returns a pointer to newly allocated space that the caller SHOULD free by calling ldap_memfree() when it is no longer in use.
ldap_explode_dn() returns a NULL-terminated char * array containing the RDN components of the DN supplied, with or without types as indicated by the notypes parameter. The components are returned in the order they appear in the dn. The array returned SHOULD be freed when it is no longer in use by calling ldap_value_free().
ldap_explode_rdn() returns a NULL-terminated char * array containing the components of the RDN supplied, with or without types as indicated by the notypes parameter. The components are returned in the order they appear in the rdn. The array returned SHOULD be freed when it is no longer in use by calling ldap_value_free().
ldap_dn2ufn() converts the DN into a user friendly format. The UFN returned is newly allocated space that SHOULD be freed by a call to ldap_memfree() when no longer in use.
ldap_get_entry_controls() is used to extract LDAP controls from an entry.
int ldap_get_entry_controls (LDAP *ld, LDAPMessage *entry, LDAPControl ***serverctrlsp);
ldap_get_entry_controls() returns an LDAP error code that indicates whether the reference could be successfully parsed (LDAP_SUCCESS if all goes well).
ldap_parse_reference() is used to extract referrals and controls from a SearchResultReference message.
int ldap_parse_reference (LDAP *ld, LDAPMessage *ref, char ***referralsp, LDAPControl ***serverctrlsp, int freeit);
ldap_parse_reference() returns an LDAP error code that indicates whether the reference could be successfully parsed (LDAP_SUCCESS if all goes well).
The following examples show how to use the API both with and without SSL. More complete examples are given in RFC 1823. The sample code for the command line tool to perform LDAP search also demonstrates use of the API in two modes.
This section contains these topics:
#include <stdio.h> #include <string.h> #include <ctype.h> #include <netdb.h> #include <gsle.h> #include <gslc.h> #include <gsld.h> #include "gslcc.h" main() {LDAP *ld; int ret = 0; .... /* open a connection */ if ( (ld = ldap_open( "MyHost", 636 )) == NULL ) exit( 1 ); /* SSL initialization */ ret = ldap_init_SSL(&ld->ld_sb, "file:/sslwallet", "welcome", GSLC_SSL_ONEWAY_AUTH ); if(ret != 0) {printf(" %s \n", ldap_err2string(ret)); exit(1);} /* authenticate as nobody */ if ( ldap_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) { ldap_perror( ld, "ldap_bind_s" ); exit( 1 ); }..... .....}
Because the user is making the ldap_init_SSL
call, the client-to-sever communication in the above example is secured by using SSL.
#include <stdio.h> #include <string.h> #include <ctype.h> #include <netdb.h> #include <gsle.h> #include <gslc.h> #include <gsld.h> #include "gslcc.h" main() {LDAP *ld; int ret = 0; .... /* open a connection */ if ( (ld = ldap_open( "MyHost", LDAP_PORT )) == NULL ) exit( 1 ); /* authenticate as nobody */ if ( ldap_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) { ldap_perror( ld, "ldap_bind_s" ); exit( 1 ); } ..... .....}
In the previous example, the user is not making the ldap_init_SSL
call, and the client-to-server communication is therefore not secure.
This section contains these topics:
To build applications with the C API, you need:
The Oracle Internet Directory SDK Release 9.0.2 provides a sample command line tool, samplesearch, for demonstrating how to use the C API to build applications. You can use samplesearch to perform LDAP searches in either SSL or non-SSL mode.
You can find the source file (samplesearch.c
) and the make file (demo_ldap.mk
) in the following directory: ORACLE_HOME/ldap/demo
.
To build the sample search tool, enter the following command:
make -f demo_ldap.mk build EXE=samplesearch OBJS=samplesearch.o
The sample code for samplesearch is:
/* NAME s0gsldsearch.c - <one-line expansion of the name> DESCRIPTION <short description of component this file declares/defines> PUBLIC FUNCTION(S) <list of external functions declared/defined - with one-line descriptions> PRIVATE FUNCTION(S) <list of static functions defined in .c file - with one-line descriptions> RETURNS <function return values, for .c file with single function> NOTES <other useful comments, qualifications, etc.> */ #include <stdio.h> #include <string.h> #include <ctype.h> #include <netdb.h> #include "ldap.h" #define DEFSEP "=" #define LDAPSEARCH_BINDDN NULL #define LDAPSEARCH_BASE DEFAULT_BASE #define DEFAULT_BASE "o=oracle, c=US" #ifdef LDAP_DEBUG extern int ldap_debug, lber_debug; #endif /* LDAP_DEBUG */ usage( s ) char *s; { fprintf( stderr, "usage: %s [options] filter [attributes...]\nwhere:\n", s ); fprintf( stderr, " filter\tRFC-1558 compliant LDAP search filter\n" ); fprintf( stderr, " attributes\twhitespace-separated list of attributes to retrieve\n" ); fprintf( stderr, "\t\t(if no attribute list is given, all are retrieved)\n" ); fprintf( stderr, "options:\n" ); fprintf( stderr, " -n\t\tshow what would be done but don't actually search\n" ); fprintf( stderr, " -v\t\trun in verbose mode (diagnostics to standard output)\n" ); fprintf( stderr, " -t\t\twrite values to files in /tmp\n" ); fprintf( stderr, " -u\t\tinclude User Friendly entry names in the output\n" ); fprintf( stderr, " -A\t\tretrieve attribute names only (no values)\n" ); fprintf( stderr, " -B\t\tdo not suppress printing of non-ASCII values\n" ); fprintf( stderr, " -L\t\tprint entries in LDIF format (-B is implied)\n" ); #ifdef LDAP_REFERRALS fprintf( stderr, " -R\t\tdo not automatically follow referrals\n" ); #endif /* LDAP_REFERRALS */ fprintf( stderr, " -d level\tset LDAP debugging level to `level'\n" ); fprintf( stderr, " -F sep\tprint `sep' instead of `=' between attribute names and values\n" ); fprintf( stderr, " -S attr\tsort the results by attribute `attr'\n" ); fprintf( stderr, " -f file\tperform sequence of searches listed in `file'\n" ); fprintf( stderr, " -b basedn\tbase dn for search\n" ); fprintf( stderr, " -s scope\tone of base, one, or sub (search scope)\n" ); fprintf( stderr, " -a deref\tone of never, always, search, or find (alias dereferencing)\n" ); fprintf( stderr, " -l time lim\ttime limit (in seconds) for search\n" ); fprintf( stderr, " -z size lim\tsize limit (in entries) for search\n" ); fprintf( stderr, " -D binddn\tbind dn\n" ); fprintf( stderr, " -w passwd\tbind passwd (for simple authentication)\n" ); #ifdef KERBEROS fprintf( stderr, " -k\t\tuse Kerberos instead of Simple Password authentication\n" ); #endif fprintf( stderr, " -h host\tldap server\n" ); fprintf( stderr, " -p port\tport on ldap server\n" ); fprintf( stderr, " -W Wallet\tWallet location\n" ); fprintf( stderr, " -P Wpasswd\tWallet Password\n" ); fprintf( stderr, " -U SSLAuth\tSSL Authentication Mode\n" ); return; } static char *binddn = LDAPSEARCH_BINDDN; static char *passwd = NULL; static char *base = LDAPSEARCH_BASE; static char *ldaphost = NULL; static int ldapport = LDAP_PORT; static char *sep = DEFSEP; static char *sortattr = NULL; static int skipsortattr = 0; static int verbose, not, includeufn, allow_binary, vals2tmp, ldif; /* TEMP */ main( argc, argv ) int argc; char **argv; { char *infile, *filtpattern, **attrs, line[ BUFSIZ ]; FILE *fp; int rc, i, first, scope, kerberos, deref, attrsonly; int ldap_options, timelimit, sizelimit, authmethod; LDAP *ld; extern char *optarg; extern int optind; char localHostName[MAXHOSTNAMELEN + 1]; char *sslwrl = NULL; char *sslpasswd = NULL; int sslauth=0,err=0; infile = NULL; deref = verbose = allow_binary = not = kerberos = vals2tmp = attrsonly = ldif = 0; #ifdef LDAP_REFERRALS ldap_options = LDAP_OPT_REFERRALS; #else /* LDAP_REFERRALS */ ldap_options = 0; #endif /* LDAP_REFERRALS */ sizelimit = timelimit = 0; scope = LDAP_SCOPE_SUBTREE; while (( i = getopt( argc, argv, #ifdef KERBEROS "KknuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:" #else "nuvtRABLD:s:f:h:b:d:p:F:a:w:l:z:S:W:P:U:" #endif )) != EOF ) { switch( i ) { case 'n': /* do Not do any searches */ ++not; break; case 'v': /* verbose mode */ ++verbose; break; case 'd': #ifdef LDAP_DEBUG ldap_debug = lber_debug = atoi( optarg ); /* */ #else /* LDAP_DEBUG */ fprintf( stderr, "compile with -DLDAP_DEBUG for debugging\n" ); #endif /* LDAP_DEBUG */ break; #ifdef KERBEROS case 'k': /* use kerberos bind */ kerberos = 2; break; case 'K': /* use kerberos bind, 1st part only */ kerberos = 1; break; #endif case 'u': /* include UFN */ ++includeufn; break; case 't': /* write attribute values to /tmp files */ ++vals2tmp; break; case 'R': /* don't automatically chase referrals */ #ifdef LDAP_REFERRALS ldap_options &= ~LDAP_OPT_REFERRALS; #else /* LDAP_REFERRALS */ fprintf( stderr, "compile with -DLDAP_REFERRALS for referral support\n" ); #endif /* LDAP_REFERRALS */ break; case 'A': /* retrieve attribute names only -- no values */ ++attrsonly; break; case 'L': /* print entries in LDIF format */ ++ldif; /* fall through -- always allow binary when outputting LDIF */ case 'B': /* allow binary values to be printed */ ++allow_binary; break; case 's': /* search scope */ if ( strncasecmp( optarg, "base", 4 ) == 0 ) { scope = LDAP_SCOPE_BASE; } else if ( strncasecmp( optarg, "one", 3 ) == 0 ) { scope = LDAP_SCOPE_ONELEVEL; } else if ( strncasecmp( optarg, "sub", 3 ) == 0 ) { scope = LDAP_SCOPE_SUBTREE; } else { fprintf( stderr, "scope should be base, one, or sub\n" ); usage( argv[ 0 ] ); exit(1); } break; case 'a': /* set alias deref option */ if ( strncasecmp( optarg, "never", 5 ) == 0 ) { deref = LDAP_DEREF_NEVER; } else if ( strncasecmp( optarg, "search", 5 ) == 0 ) { deref = LDAP_DEREF_SEARCHING; } else if ( strncasecmp( optarg, "find", 4 ) == 0 ) { deref = LDAP_DEREF_FINDING; } else if ( strncasecmp( optarg, "always", 6 ) == 0 ) { deref = LDAP_DEREF_ALWAYS; } else { fprintf( stderr, "alias deref should be never, search, find, or always\n" ); usage( argv[ 0 ] ); exit(1); } break; case 'F': /* field separator */ sep = (char *)strdup( optarg ); break; case 'f': /* input file */ infile = (char *)strdup( optarg ); break; case 'h': /* ldap host */ ldaphost = (char *)strdup( optarg ); break; case 'b': /* searchbase */ base = (char *)strdup( optarg ); break; case 'D': /* bind DN */ binddn = (char *)strdup( optarg ); break; case 'p': /* ldap port */ ldapport = atoi( optarg ); break; case 'w': /* bind password */ passwd = (char *)strdup( optarg ); break; case 'l': /* time limit */ timelimit = atoi( optarg ); break; case 'z': /* size limit */ sizelimit = atoi( optarg ); break; case 'S': /* sort attribute */ sortattr = (char *)strdup( optarg ); break; case 'W': /* Wallet URL */ sslwrl = (char *)strdup( optarg ); break; case 'P': /* Wallet password */ sslpasswd = (char *)strdup( optarg ); break; case 'U': /* SSL Authentication Mode */ sslauth = atoi( optarg ); break; default: usage( argv[0] ); exit(1); break; } } if ( argc - optind < 1 ) { usage( argv[ 0 ] ); exit(1); } filtpattern = (char *)strdup( argv[ optind ] ); if ( argv[ optind + 1 ] == NULL ) { attrs = NULL; } else if ( sortattr == NULL || *sortattr == '\0' ) { attrs = &argv[ optind + 1 ]; } else { for ( i = optind + 1; i < argc; i++ ) { if ( strcasecmp( argv[ i ], sortattr ) == 0 ) { break; } } if ( i == argc ) { skipsortattr = 1; argv[ optind ] = sortattr; } else { optind++; } attrs = &argv[ optind ]; } if ( infile != NULL ) { if ( infile[0] == '-' && infile[1] == '\0' ) { fp = stdin; } else if (( fp = fopen( infile, "r" )) == NULL ) { perror( infile ); exit( 1 ); } } if (ldaphost == NULL) { if (gethostname(localHostName, MAXHOSTNAMELEN) != 0) { perror("gethostname"); exit(1); } ldaphost = localHostName; } if ( verbose ) { printf( "ldap_open( %s, %d )\n", ldaphost, ldapport ); } if (( ld = ldap_open( ldaphost, ldapport )) == NULL ) { perror( ldaphost ); exit( 1 ); } if (sslauth > 1) { if (!sslwrl || !sslpasswd) { printf ("Null Wallet or password given\n"); exit (0); } } if (sslauth > 0) { if (sslauth == 1) sslauth = GSLC_SSL_NO_AUTH; else if (sslauth == 2) sslauth = GSLC_SSL_ONEWAY_AUTH; else if (sslauth == 3) sslauth = GSLC_SSL_TWOWAY_AUTH; else { printf(" Wrong SSL Authenication Mode Value\n"); exit(0); } err = ldap_init_SSL(&ld->ld_sb,sslwrl,sslpasswd,sslauth); if(err != 0) { printf(" %s\n", ldap_err2string(err)); exit(0); } } ld->ld_deref = deref; ld->ld_timelimit = timelimit; ld->ld_sizelimit = sizelimit; ld->ld_options = ldap_options; if ( !kerberos ) { authmethod = LDAP_AUTH_SIMPLE; } else if ( kerberos == 1 ) { authmethod = LDAP_AUTH_KRBV41; } else { authmethod = LDAP_AUTH_KRBV4; } if ( ldap_bind_s( ld, binddn, passwd, authmethod ) != LDAP_SUCCESS ) { ldap_perror( ld, "ldap_bind" ); exit( 1 ); } if ( verbose ) { printf( "filter pattern: %s\nreturning: ", filtpattern ); if ( attrs == NULL ) { printf( "ALL" ); } else { for ( i = 0; attrs[ i ] != NULL; ++i ) { printf( "%s ", attrs[ i ] ); } } putchar( '\n' ); } if ( infile == NULL ) { rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, "" ); } else { rc = 0; first = 1; while ( rc == 0 && fgets( line, sizeof( line ), fp ) != NULL ) { line[ strlen( line ) - 1 ] = '\0'; if ( !first ) { putchar( '\n' ); } else { first = 0; } rc = dosearch( ld, base, scope, attrs, attrsonly, filtpattern, line ); } if ( fp != stdin ) { fclose( fp ); } } ldap_unbind( ld ); exit( rc ); } dosearch( ld, base, scope, attrs, attrsonly, filtpatt, value ) LDAP *ld; char *base; int scope; char **attrs; int attrsonly; char *filtpatt; char *value; { char filter[ BUFSIZ ], **val; int rc, first, matches; LDAPMessage *res, *e; sprintf( filter, filtpatt, value ); if ( verbose ) { printf( "filter is: (%s)\n", filter ); } if ( not ) { return( LDAP_SUCCESS ); } if ( ldap_search( ld, base, scope, filter, attrs, attrsonly ) == -1 ) { ldap_perror( ld, "ldap_search" ); return( ld->ld_errno ); } matches = 0; first = 1; while ( (rc = ldap_result( ld, LDAP_RES_ANY, sortattr ? 1 : 0, NULL, &res )) == LDAP_RES_SEARCH_ENTRY ) { matches++; e = ldap_first_entry( ld, res ); if ( !first ) { putchar( '\n' ); } else { first = 0; } print_entry( ld, e, attrsonly ); ldap_msgfree( res ); } if ( rc == -1 ) { ldap_perror( ld, "ldap_result" ); return( rc ); } if (( rc = ldap_result2error( ld, res, 0 )) != LDAP_SUCCESS ) { ldap_perror( ld, "ldap_search" ); } if ( sortattr != NULL ) { extern int strcasecmp(); (void) ldap_sort_entries( ld, &res, ( *sortattr == '\0' ) ? NULL : sortattr, strcasecmp ); matches = 0; first = 1; for ( e = ldap_first_entry( ld, res ); e != NULLMSG; e = ldap_next_entry( ld, e ) ) { matches++; if ( !first ) { putchar( '\n' ); } else { first = 0; } print_entry( ld, e, attrsonly ); } } if ( verbose ) { printf( "%d matches\n", matches ); } ldap_msgfree( res ); return( rc ); } print_entry( ld, entry, attrsonly ) LDAP *ld; LDAPMessage *entry; int attrsonly; { char *a, *dn, *ufn, tmpfname[ 64 ]; int i, j, notascii; BerElement *ber; struct berval **bvals; FILE *tmpfp; extern char *mktemp(); dn = ldap_get_dn( ld, entry ); if ( ldif ) { write_ldif_value( "dn", dn, strlen( dn )); } else { printf( "%s\n", dn ); } if ( includeufn ) { ufn = ldap_dn2ufn( dn ); if ( ldif ) { write_ldif_value( "ufn", ufn, strlen( ufn )); } else { printf( "%s\n", ufn ); } free( ufn ); } free( dn ); for ( a = ldap_first_attribute( ld, entry, &ber ); a != NULL; a = ldap_next_attribute( ld, entry, ber ) ) { if ( skipsortattr && strcasecmp( a, sortattr ) == 0 ) { continue; } if ( attrsonly ) { if ( ldif ) { write_ldif_value( a, "", 0 ); } else { printf( "%s\n", a ); } } else if (( bvals = ldap_get_values_len( ld, entry, a )) != NULL ) { for ( i = 0; bvals[i] != NULL; i++ ) { if ( vals2tmp ) { sprintf( tmpfname, "/tmp/ldapsearch-%s-XXXXXX", a ); tmpfp = NULL; if ( mktemp( tmpfname ) == NULL ) { perror( tmpfname ); } else if (( tmpfp = fopen( tmpfname, "w")) == NULL ) { perror( tmpfname ); } else if ( fwrite( bvals[ i ]->bv_val, bvals[ i ]->bv_len, 1, tmpfp ) == 0 ) { perror( tmpfname ); } else if ( ldif ) { write_ldif_value( a, tmpfname, strlen( tmpfname )); } else { printf( "%s%s%s\n", a, sep, tmpfname ); } if ( tmpfp != NULL ) { fclose( tmpfp ); } } else { notascii = 0; if ( !allow_binary ) { for ( j = 0; j < bvals[ i ]->bv_len; ++j ) { if ( !isascii( bvals[ i ]->bv_val[ j ] )) { notascii = 1; break; } } } if ( ldif ) { write_ldif_value( a, bvals[ i ]->bv_val, bvals[ i ]->bv_len ); } else { printf( "%s%s%s\n", a, sep, notascii ? "NOT ASCII" : (char *)bvals[ i ]->bv_val ); } } } gsledePBerBvecfree( bvals ); } } } int write_ldif_value( char *type, char *value, unsigned long vallen ) { char *ldif; if (( ldif = gsldlDLdifTypeAndValue( type, value, (int)vallen )) == NULL ) { return( -1 ); } fputs( ldif, stdout ); free( ldif ); return( 0 ); }
This API can work against any release of Oracle Internet Directory. It requires either an Oracle environment or, at minimum, NLS and other core libraries.
To use the different authentication modes in SSL, the directory server requires corresponding configuration settings.
Oracle Wallet Manager is required for creating wallets if you are using the C API in SSL mode.
TCP/IP Socket Library is required.
The following Oracle libraries are required:
Sample libraries are included in the release for the sample command line tool. You should replace these libraries with your own versions of the libraries.
The product supports only those authentication mechanisms described in LDAP SDK specifications (RFC 1823).
|
Copyright © 2002 Oracle Corporation. All Rights Reserved. |
|