10Sstevel@tonic-gate /* 2*2333Sjanga * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 30Sstevel@tonic-gate * Use is subject to license terms. 40Sstevel@tonic-gate */ 50Sstevel@tonic-gate 60Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 70Sstevel@tonic-gate 80Sstevel@tonic-gate 90Sstevel@tonic-gate /* 100Sstevel@tonic-gate * The contents of this file are subject to the Netscape Public 110Sstevel@tonic-gate * License Version 1.1 (the "License"); you may not use this file 120Sstevel@tonic-gate * except in compliance with the License. You may obtain a copy of 130Sstevel@tonic-gate * the License at http://www.mozilla.org/NPL/ 140Sstevel@tonic-gate * 150Sstevel@tonic-gate * Software distributed under the License is distributed on an "AS 160Sstevel@tonic-gate * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 170Sstevel@tonic-gate * implied. See the License for the specific language governing 180Sstevel@tonic-gate * rights and limitations under the License. 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * The Original Code is Mozilla Communicator client code, released 210Sstevel@tonic-gate * March 31, 1998. 220Sstevel@tonic-gate * 230Sstevel@tonic-gate * The Initial Developer of the Original Code is Netscape 240Sstevel@tonic-gate * Communications Corporation. Portions created by Netscape are 250Sstevel@tonic-gate * Copyright (C) 1998-1999 Netscape Communications Corporation. All 260Sstevel@tonic-gate * Rights Reserved. 270Sstevel@tonic-gate * 280Sstevel@tonic-gate * Contributor(s): 290Sstevel@tonic-gate */ 300Sstevel@tonic-gate 310Sstevel@tonic-gate /* 320Sstevel@tonic-gate * Public interface for libprldap -- use NSPR (Netscape Portable Runtime) 330Sstevel@tonic-gate * I/O, threads, etc. with libldap. 340Sstevel@tonic-gate * 350Sstevel@tonic-gate */ 360Sstevel@tonic-gate 370Sstevel@tonic-gate #include "ldappr-int.h" 380Sstevel@tonic-gate 390Sstevel@tonic-gate 400Sstevel@tonic-gate /* 410Sstevel@tonic-gate * Function: prldap_init(). 420Sstevel@tonic-gate * 430Sstevel@tonic-gate * Create a new LDAP session handle, but with NSPR I/O, threading, and DNS 440Sstevel@tonic-gate * functions installed. 450Sstevel@tonic-gate * 460Sstevel@tonic-gate * Pass a non-zero value for the 'shared' parameter if you plan to use 470Sstevel@tonic-gate * this LDAP * handle from more than one thread. 480Sstevel@tonic-gate * 490Sstevel@tonic-gate * prldap_init() returns an LDAP session handle (or NULL if an error occurs). 500Sstevel@tonic-gate */ 510Sstevel@tonic-gate LDAP * LDAP_CALL 520Sstevel@tonic-gate prldap_init( const char *defhost, int defport, int shared ) 530Sstevel@tonic-gate { 540Sstevel@tonic-gate LDAP *ld; 550Sstevel@tonic-gate 560Sstevel@tonic-gate if (( ld = ldap_init( defhost, defport )) != NULL ) { 570Sstevel@tonic-gate if ( prldap_install_routines( ld, shared ) != LDAP_SUCCESS ) { 580Sstevel@tonic-gate prldap_set_system_errno( EINVAL ); /* XXXmcs: just a guess! */ 590Sstevel@tonic-gate ldap_unbind( ld ); 600Sstevel@tonic-gate ld = NULL; 610Sstevel@tonic-gate } 620Sstevel@tonic-gate } 630Sstevel@tonic-gate 640Sstevel@tonic-gate return( ld ); 650Sstevel@tonic-gate } 660Sstevel@tonic-gate 670Sstevel@tonic-gate 680Sstevel@tonic-gate /* 690Sstevel@tonic-gate * Function: prldap_install_routines(). 700Sstevel@tonic-gate * 710Sstevel@tonic-gate * Install NSPR I/O, threading, and DNS functions so they will be used by 720Sstevel@tonic-gate * 'ld'. 730Sstevel@tonic-gate * 740Sstevel@tonic-gate * If 'ld' is NULL, the functions are installed as the default functions 750Sstevel@tonic-gate * for all new LDAP * handles). 760Sstevel@tonic-gate * 770Sstevel@tonic-gate * Pass a non-zero value for the 'shared' parameter if you plan to use 780Sstevel@tonic-gate * this LDAP * handle from more than one thread. 790Sstevel@tonic-gate * 800Sstevel@tonic-gate * prldap_install_routines() returns an LDAP API error code (LDAP_SUCCESS 810Sstevel@tonic-gate * if all goes well). 820Sstevel@tonic-gate */ 830Sstevel@tonic-gate int LDAP_CALL 840Sstevel@tonic-gate prldap_install_routines( LDAP *ld, int shared ) 850Sstevel@tonic-gate { 860Sstevel@tonic-gate 870Sstevel@tonic-gate if ( prldap_install_io_functions( ld, shared ) != 0 880Sstevel@tonic-gate || prldap_install_thread_functions( ld, shared ) != 0 890Sstevel@tonic-gate || prldap_install_dns_functions( ld ) != 0 ) { 900Sstevel@tonic-gate return( ldap_get_lderrno( ld, NULL, NULL )); 910Sstevel@tonic-gate } 920Sstevel@tonic-gate 930Sstevel@tonic-gate return( LDAP_SUCCESS ); 940Sstevel@tonic-gate } 950Sstevel@tonic-gate 960Sstevel@tonic-gate 970Sstevel@tonic-gate /* 980Sstevel@tonic-gate * Function: prldap_set_session_option(). 990Sstevel@tonic-gate * 1000Sstevel@tonic-gate * Given an LDAP session handle or a session argument such is passed to 1010Sstevel@tonic-gate * SOCKET, POLL, NEWHANDLE, or DISPOSEHANDLE extended I/O callbacks, set 1020Sstevel@tonic-gate * an option that affects the prldap layer. 1030Sstevel@tonic-gate * 1040Sstevel@tonic-gate * If 'ld' and 'session" are both NULL, the option is set as the default 1050Sstevel@tonic-gate * for all new prldap sessions. 1060Sstevel@tonic-gate * 1070Sstevel@tonic-gate * Returns an LDAP API error code (LDAP_SUCCESS if all goes well). 1080Sstevel@tonic-gate */ 1090Sstevel@tonic-gate int LDAP_CALL 1100Sstevel@tonic-gate prldap_set_session_option( LDAP *ld, void *sessionarg, int option, ... ) 1110Sstevel@tonic-gate { 1120Sstevel@tonic-gate int rc = LDAP_SUCCESS; /* optimistic */ 1130Sstevel@tonic-gate PRLDAPIOSessionArg *prsessp = NULL; 1140Sstevel@tonic-gate va_list ap; 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate if ( NULL != ld ) { 1170Sstevel@tonic-gate if ( LDAP_SUCCESS != 1180Sstevel@tonic-gate ( rc = prldap_session_arg_from_ld( ld, &prsessp ))) { 1190Sstevel@tonic-gate return( rc ); 1200Sstevel@tonic-gate } 1210Sstevel@tonic-gate } else if ( NULL != sessionarg ) { 1220Sstevel@tonic-gate prsessp = (PRLDAPIOSessionArg *)sessionarg; 1230Sstevel@tonic-gate } 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate va_start( ap, option ); 1260Sstevel@tonic-gate switch ( option ) { 1270Sstevel@tonic-gate case PRLDAP_OPT_IO_MAX_TIMEOUT: 1280Sstevel@tonic-gate rc = prldap_set_io_max_timeout( prsessp, va_arg( ap, int )); 1290Sstevel@tonic-gate break; 1300Sstevel@tonic-gate default: 1310Sstevel@tonic-gate rc = LDAP_PARAM_ERROR; 1320Sstevel@tonic-gate } 1330Sstevel@tonic-gate va_end( ap ); 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate return( rc ); 1360Sstevel@tonic-gate } 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate /* 1400Sstevel@tonic-gate * Function: prldap_get_session_option(). 1410Sstevel@tonic-gate * 1420Sstevel@tonic-gate * Given an LDAP session handle or a session argument such is passed to 1430Sstevel@tonic-gate * SOCKET, POLL, NEWHANDLE, or DISPOSEHANDLE extended I/O callbacks, retrieve 1440Sstevel@tonic-gate * the setting for an option that affects the prldap layer. 1450Sstevel@tonic-gate * 1460Sstevel@tonic-gate * If 'ld' and 'session" are both NULL, the default option value for all new 1470Sstevel@tonic-gate * new prldap sessions is retrieved. 1480Sstevel@tonic-gate * 1490Sstevel@tonic-gate * Returns an LDAP API error code (LDAP_SUCCESS if all goes well). 1500Sstevel@tonic-gate */ 1510Sstevel@tonic-gate int LDAP_CALL prldap_get_session_option( LDAP *ld, void *sessionarg, 1520Sstevel@tonic-gate int option, ... ) 1530Sstevel@tonic-gate { 1540Sstevel@tonic-gate int rc = LDAP_SUCCESS; /* optimistic */ 1550Sstevel@tonic-gate PRLDAPIOSessionArg *prsessp = NULL; 1560Sstevel@tonic-gate va_list ap; 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate if ( NULL != ld ) { 1590Sstevel@tonic-gate if ( LDAP_SUCCESS != 1600Sstevel@tonic-gate ( rc = prldap_session_arg_from_ld( ld, &prsessp ))) { 1610Sstevel@tonic-gate return( rc ); 1620Sstevel@tonic-gate } 1630Sstevel@tonic-gate } else if ( NULL != sessionarg ) { 1640Sstevel@tonic-gate prsessp = (PRLDAPIOSessionArg *)sessionarg; 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate va_start( ap, option ); 1680Sstevel@tonic-gate switch ( option ) { 1690Sstevel@tonic-gate case PRLDAP_OPT_IO_MAX_TIMEOUT: 1700Sstevel@tonic-gate rc = prldap_get_io_max_timeout( prsessp, va_arg( ap, int * )); 1710Sstevel@tonic-gate break; 1720Sstevel@tonic-gate default: 1730Sstevel@tonic-gate rc = LDAP_PARAM_ERROR; 1740Sstevel@tonic-gate } 1750Sstevel@tonic-gate va_end( ap ); 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate return( rc ); 1780Sstevel@tonic-gate } 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate /* 1820Sstevel@tonic-gate * Function: prldap_set_session_info(). 1830Sstevel@tonic-gate * 1840Sstevel@tonic-gate * Given an LDAP session handle, set some application-specific data. 1850Sstevel@tonic-gate * 1860Sstevel@tonic-gate * Returns an LDAP API error code (LDAP_SUCCESS if all goes well). 1870Sstevel@tonic-gate */ 1880Sstevel@tonic-gate int LDAP_CALL 1890Sstevel@tonic-gate prldap_set_session_info( LDAP *ld, void *sessionarg, PRLDAPSessionInfo *seip ) 1900Sstevel@tonic-gate { 1910Sstevel@tonic-gate int rc; 1920Sstevel@tonic-gate PRLDAPIOSessionArg *prsessp; 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate if ( seip == NULL || PRLDAP_SESSIONINFO_SIZE != seip->seinfo_size ) { 1950Sstevel@tonic-gate ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL ); 1960Sstevel@tonic-gate return( LDAP_PARAM_ERROR ); 1970Sstevel@tonic-gate } 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate if ( NULL != ld ) { 2000Sstevel@tonic-gate if ( LDAP_SUCCESS != 2010Sstevel@tonic-gate ( rc = prldap_session_arg_from_ld( ld, &prsessp ))) { 2020Sstevel@tonic-gate return( rc ); 2030Sstevel@tonic-gate } 2040Sstevel@tonic-gate } else if ( NULL != sessionarg ) { 2050Sstevel@tonic-gate prsessp = (PRLDAPIOSessionArg *)sessionarg; 2060Sstevel@tonic-gate } else { 2070Sstevel@tonic-gate ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL ); 2080Sstevel@tonic-gate return( LDAP_PARAM_ERROR ); 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate prsessp->prsess_appdata = seip->seinfo_appdata; 2120Sstevel@tonic-gate return( LDAP_SUCCESS ); 2130Sstevel@tonic-gate } 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate /* 2170Sstevel@tonic-gate * Function: prldap_get_session_info(). 2180Sstevel@tonic-gate * 2190Sstevel@tonic-gate * Given an LDAP session handle, retrieve some application-specific data. 2200Sstevel@tonic-gate * 2210Sstevel@tonic-gate * Returns an LDAP API error code (LDAP_SUCCESS if all goes well, in 2220Sstevel@tonic-gate * which case the fields in the structure that seip points to are filled in). 2230Sstevel@tonic-gate */ 2240Sstevel@tonic-gate int LDAP_CALL 2250Sstevel@tonic-gate prldap_get_session_info( LDAP *ld, void *sessionarg, PRLDAPSessionInfo *seip ) 2260Sstevel@tonic-gate { 2270Sstevel@tonic-gate int rc; 2280Sstevel@tonic-gate PRLDAPIOSessionArg *prsessp; 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate if ( seip == NULL || PRLDAP_SESSIONINFO_SIZE != seip->seinfo_size ) { 2310Sstevel@tonic-gate ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL ); 2320Sstevel@tonic-gate return( LDAP_PARAM_ERROR ); 2330Sstevel@tonic-gate } 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate if ( NULL != ld ) { 2360Sstevel@tonic-gate if ( LDAP_SUCCESS != 2370Sstevel@tonic-gate ( rc = prldap_session_arg_from_ld( ld, &prsessp ))) { 2380Sstevel@tonic-gate return( rc ); 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate } else if ( NULL != sessionarg ) { 2410Sstevel@tonic-gate prsessp = (PRLDAPIOSessionArg *)sessionarg; 2420Sstevel@tonic-gate } else { 2430Sstevel@tonic-gate ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL ); 2440Sstevel@tonic-gate return( LDAP_PARAM_ERROR ); 2450Sstevel@tonic-gate } 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate seip->seinfo_appdata = prsessp->prsess_appdata; 2480Sstevel@tonic-gate return( LDAP_SUCCESS ); 2490Sstevel@tonic-gate } 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate /* 2530Sstevel@tonic-gate * Function: prldap_set_socket_info(). 2540Sstevel@tonic-gate * 2550Sstevel@tonic-gate * Given an integer fd and a void * argument such as those passed to the 2560Sstevel@tonic-gate * extended I/O callback functions, set socket specific information. 2570Sstevel@tonic-gate * 2580Sstevel@tonic-gate * Returns an LDAP API error code (LDAP_SUCCESS if all goes well). 2590Sstevel@tonic-gate * 2600Sstevel@tonic-gate * Note: it is only safe to change soinfo_prfd from within the SOCKET 2610Sstevel@tonic-gate * extended I/O callback function. 2620Sstevel@tonic-gate */ 2630Sstevel@tonic-gate int LDAP_CALL 2640Sstevel@tonic-gate prldap_set_socket_info( int fd, void *socketarg, PRLDAPSocketInfo *soip ) 2650Sstevel@tonic-gate { 2660Sstevel@tonic-gate PRLDAPIOSocketArg *prsockp; 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate if ( NULL == socketarg || NULL == soip || 2690Sstevel@tonic-gate PRLDAP_SOCKETINFO_SIZE != soip->soinfo_size ) { 2700Sstevel@tonic-gate return( LDAP_PARAM_ERROR ); 2710Sstevel@tonic-gate } 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate prsockp = (PRLDAPIOSocketArg *)socketarg; 2740Sstevel@tonic-gate prsockp->prsock_prfd = soip->soinfo_prfd; 2750Sstevel@tonic-gate prsockp->prsock_appdata = soip->soinfo_appdata; 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate return( LDAP_SUCCESS ); 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate /* 2820Sstevel@tonic-gate * Function: prldap_get_socket_info(). 2830Sstevel@tonic-gate * 2840Sstevel@tonic-gate * Given an integer fd and a void * argument such as those passed to the 2850Sstevel@tonic-gate * extended I/O callback functions, retrieve socket specific information. 2860Sstevel@tonic-gate * 2870Sstevel@tonic-gate * Returns an LDAP API error code (LDAP_SUCCESS if all goes well, in 2880Sstevel@tonic-gate * which case the fields in the structure that soip points to are filled in). 2890Sstevel@tonic-gate */ 2900Sstevel@tonic-gate int LDAP_CALL 2910Sstevel@tonic-gate prldap_get_socket_info( int fd, void *socketarg, PRLDAPSocketInfo *soip ) 2920Sstevel@tonic-gate { 2930Sstevel@tonic-gate PRLDAPIOSocketArg *prsockp; 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate if ( NULL == socketarg || NULL == soip || 2960Sstevel@tonic-gate PRLDAP_SOCKETINFO_SIZE != soip->soinfo_size ) { 2970Sstevel@tonic-gate return( LDAP_PARAM_ERROR ); 2980Sstevel@tonic-gate } 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate prsockp = (PRLDAPIOSocketArg *)socketarg; 3010Sstevel@tonic-gate soip->soinfo_prfd = prsockp->prsock_prfd; 3020Sstevel@tonic-gate soip->soinfo_appdata = prsockp->prsock_appdata; 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate return( LDAP_SUCCESS ); 3050Sstevel@tonic-gate } 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate /* 3080Sstevel@tonic-gate * Function: prldap_get_default_socket_info(). 3090Sstevel@tonic-gate * 3100Sstevel@tonic-gate * Given an LDAP session handle, retrieve socket specific information. 3110Sstevel@tonic-gate * If ld is NULL, LDAP_PARAM_ERROR is returned. 3120Sstevel@tonic-gate * 3130Sstevel@tonic-gate * Returns an LDAP API error code (LDAP_SUCCESS if all goes well, in 3140Sstevel@tonic-gate * which case the fields in the structure that soip points to are filled in). 3150Sstevel@tonic-gate */ 3160Sstevel@tonic-gate int LDAP_CALL 3170Sstevel@tonic-gate prldap_get_default_socket_info( LDAP *ld, PRLDAPSocketInfo *soip ) 3180Sstevel@tonic-gate { 3190Sstevel@tonic-gate int rc; 3200Sstevel@tonic-gate PRLDAPIOSocketArg *prsockp; 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate if ( NULL == soip || PRLDAP_SOCKETINFO_SIZE != soip->soinfo_size ) { 3240Sstevel@tonic-gate ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL ); 3250Sstevel@tonic-gate return( LDAP_PARAM_ERROR ); 3260Sstevel@tonic-gate } 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate if ( NULL != ld ) { 3290Sstevel@tonic-gate if ( LDAP_SUCCESS != 3300Sstevel@tonic-gate ( rc = prldap_socket_arg_from_ld( ld, &prsockp ))) { 3310Sstevel@tonic-gate return( rc ); 3320Sstevel@tonic-gate } 3330Sstevel@tonic-gate } else { 3340Sstevel@tonic-gate ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL ); 3350Sstevel@tonic-gate return( LDAP_PARAM_ERROR ); 3360Sstevel@tonic-gate } 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate soip->soinfo_prfd = prsockp->prsock_prfd; 3390Sstevel@tonic-gate soip->soinfo_appdata = prsockp->prsock_appdata; 3400Sstevel@tonic-gate 3410Sstevel@tonic-gate return( LDAP_SUCCESS ); 3420Sstevel@tonic-gate } 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate /* 3460Sstevel@tonic-gate * Function: prldap_set_default_socket_info(). 3470Sstevel@tonic-gate * 3480Sstevel@tonic-gate * Given an LDAP session handle, set socket specific information. 3490Sstevel@tonic-gate * If ld is NULL, LDAP_PARAM_ERROR is returned. 3500Sstevel@tonic-gate * 3510Sstevel@tonic-gate * Returns an LDAP API error code (LDAP_SUCCESS if all goes well, in 3520Sstevel@tonic-gate * which case the fields in the structure that soip points to are filled in). 3530Sstevel@tonic-gate */ 3540Sstevel@tonic-gate int LDAP_CALL 3550Sstevel@tonic-gate prldap_set_default_socket_info( LDAP *ld, PRLDAPSocketInfo *soip ) 3560Sstevel@tonic-gate { 3570Sstevel@tonic-gate int rc; 3580Sstevel@tonic-gate PRLDAPIOSocketArg *prsockp; 3590Sstevel@tonic-gate 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate if ( NULL == soip || PRLDAP_SOCKETINFO_SIZE != soip->soinfo_size ) { 3620Sstevel@tonic-gate ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL ); 3630Sstevel@tonic-gate return( LDAP_PARAM_ERROR ); 3640Sstevel@tonic-gate } 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate if ( NULL != ld ) { 3670Sstevel@tonic-gate if ( LDAP_SUCCESS != 3680Sstevel@tonic-gate ( rc = prldap_socket_arg_from_ld( ld, &prsockp ))) { 3690Sstevel@tonic-gate return( rc ); 3700Sstevel@tonic-gate } 3710Sstevel@tonic-gate } else { 3720Sstevel@tonic-gate ldap_set_lderrno( ld, LDAP_PARAM_ERROR, NULL, NULL ); 3730Sstevel@tonic-gate return( LDAP_PARAM_ERROR ); 3740Sstevel@tonic-gate } 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate prsockp->prsock_prfd = soip->soinfo_prfd; 3770Sstevel@tonic-gate prsockp->prsock_appdata = soip->soinfo_appdata; 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate return( LDAP_SUCCESS ); 3800Sstevel@tonic-gate } 381