| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427 |
- /* --- BEGIN COPYRIGHT BLOCK ---
- * This Program is free software; you can redistribute it and/or modify it under
- * the terms of the GNU General Public License as published by the Free Software
- * Foundation; version 2 of the License.
- *
- * This Program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place, Suite 330, Boston, MA 02111-1307 USA.
- *
- * In addition, as a special exception, Red Hat, Inc. gives You the additional
- * right to link the code of this Program with code not covered under the GNU
- * General Public License ("Non-GPL Code") and to distribute linked combinations
- * including the two, subject to the limitations in this paragraph. Non-GPL Code
- * permitted under this exception must only link to the code of this Program
- * through those well defined interfaces identified in the file named EXCEPTION
- * found in the source code files (the "Approved Interfaces"). The files of
- * Non-GPL Code may instantiate templates or use macros or inline functions from
- * the Approved Interfaces without causing the resulting work to be covered by
- * the GNU General Public License. Only Red Hat, Inc. may make changes or
- * additions to the list of Approved Interfaces. You must obey the GNU General
- * Public License in all respects for all of the Program code and other code used
- * in conjunction with the Program except the Non-GPL Code covered by this
- * exception. If you modify this file, you may extend this exception to your
- * version of the file, but you are not obligated to do so. If you do not wish to
- * provide this exception without modification, you must delete this exception
- * statement from your version and license this file solely under the GPL without
- * exception.
- *
- *
- * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
- * Copyright (C) 2005 Red Hat, Inc.
- * All rights reserved.
- * --- END COPYRIGHT BLOCK --- */
- #ifdef HAVE_CONFIG_H
- # include <config.h>
- #endif
- /**
- * Implementation of a Simple HTTP Client
- */
- #include <stdio.h>
- #include <string.h>
- #include "nspr.h"
- #include "nss.h"
- #include "pk11func.h"
- #include "ssl.h"
- #include "prprf.h"
- #include "plstr.h"
- #include "slapi-plugin.h"
- #include "http_client.h"
- #include "secerr.h"
- #include "sslerr.h"
- #include "slap.h"
- #include "slapi-private.h"
- #include "slapi-plugin-compat4.h"
- #include <sys/stat.h>
- /*** from proto-slap.h ***/
- int slapd_log_error_proc( char *subsystem, char *fmt, ... );
- char *config_get_instancedir();
- /*** from ldaplog.h ***/
- /* edited ldaplog.h for LDAPDebug()*/
- #ifndef _LDAPLOG_H
- #define _LDAPLOG_H
- #ifdef __cplusplus
- extern "C" {
- #endif
- #ifdef BUILD_STANDALONE
- #define slapi_log_error(a,b,c,d) printf((c),(d))
- #define stricmp strcasecmp
- #endif
- #define LDAP_DEBUG_TRACE 0x00001 /* 1 */
- #define LDAP_DEBUG_ANY 0x04000 /* 16384 */
- #define LDAP_DEBUG_PLUGIN 0x10000 /* 65536 */
- /* debugging stuff */
- # ifdef _WIN32
- extern int *module_ldap_debug;
- # define LDAPDebug( level, fmt, arg1, arg2, arg3 ) \
- { \
- if ( *module_ldap_debug & level ) { \
- slapd_log_error_proc( NULL, fmt, arg1, arg2, arg3 ); \
- } \
- }
- # else /* _WIN32 */
- extern int slapd_ldap_debug;
- # define LDAPDebug( level, fmt, arg1, arg2, arg3 ) \
- { \
- if ( slapd_ldap_debug & level ) { \
- slapd_log_error_proc( NULL, fmt, arg1, arg2, arg3 ); \
- } \
- }
- # endif /* Win32 */
- #ifdef __cplusplus
- }
- #endif
- #endif /* _LDAP_H */
- #define HTTP_PLUGIN_SUBSYSTEM "http-client-plugin" /* used for logging */
- #define HTTP_IMPL_SUCCESS 0
- #define HTTP_IMPL_FAILURE -1
- #define HTTP_REQ_TYPE_GET 1
- #define HTTP_REQ_TYPE_REDIRECT 2
- #define HTTP_REQ_TYPE_POST 3
- #define HTTP_GET "GET"
- #define HTTP_POST "POST"
- #define HTTP_PROTOCOL "HTTP/1.0"
- #define HTTP_CONTENT_LENGTH "Content-length:"
- #define HTTP_CONTENT_TYPE_URL_ENCODED "Content-type: application/x-www-form-urlencoded"
- #define HTTP_GET_STD_LEN 18
- #define HTTP_POST_STD_LEN 85
- #define HTTP_DEFAULT_BUFFER_SIZE 4096
- #define HTTP_RESPONSE_REDIRECT (retcode == 302 || retcode == 301)
- /**
- * Error strings used for logging error messages
- */
- #define HTTP_ERROR_BAD_URL " Badly formatted URL"
- #define HTTP_ERROR_NET_ADDR " NetAddr initialization failed"
- #define HTTP_ERROR_SOCKET_CREATE " Creation of socket failed"
- #define HTTP_ERROR_SSLSOCKET_CREATE " Creation of SSL socket failed"
- #define HTTP_ERROR_CONNECT_FAILED " Couldn't connect to remote host"
- #define HTTP_ERROR_SEND_REQ " Send request failed"
- #define HTTP_ERROR_BAD_RESPONSE " Invalid response from remote host"
- #define HTTP_PLUGIN_DN "cn=HTTP Client,cn=plugins,cn=config"
- #define CONFIG_DN "cn=config"
- #define ATTR_CONNECTION_TIME_OUT "nsHTTPConnectionTimeOut"
- #define ATTR_READ_TIME_OUT "nsHTTPReadTimeOut"
- #define ATTR_RETRY_COUNT "nsHTTPRetryCount"
- #define ATTR_DS_SECURITY "nsslapd-security"
- #define ATTR_INSTANCE_PATH "nsslapd-errorlog"
- /*static Slapi_ComponentId *plugin_id = NULL;*/
- typedef struct {
- int retryCount;
- int connectionTimeOut;
- int readTimeOut;
- int nssInitialized;
- char *DS_sslOn;
- } httpPluginConfig;
- httpPluginConfig *httpConfig;
- /**
- * Public functions
- */
- int http_impl_init(Slapi_ComponentId *plugin_id);
- int http_impl_get_text(char *url, char **data, int *bytesRead);
- int http_impl_get_binary(char *url, char **data, int *bytesRead);
- int http_impl_get_redirected_uri(char *url, char **data, int *bytesRead);
- int http_impl_post(char *url, httpheader **httpheaderArray, char *body, char **data, int *bytesRead);
- void http_impl_shutdown();
- /**
- * Http handling functions
- */
- static int doRequest(const char *url, httpheader **httpheaderArray, char *body, char **buf, int *bytesRead, int reqType);
- static int doRequestRetry(const char *url, httpheader **httpheaderArray, char *body, char **buf, int *bytesRead, int reqType);
- static void setTCPNoDelay(PRFileDesc* fd);
- static PRStatus sendGetReq(PRFileDesc *fd, const char *path);
- static PRStatus sendPostReq(PRFileDesc *fd, const char *path, httpheader **httpheaderArray, char *body);
- static PRStatus processResponse(PRFileDesc *fd, char **resBUF, int *bytesRead, int reqType);
- static PRStatus getChar(PRFileDesc *fd, char *buf);
- static PRInt32 http_read(PRFileDesc *fd, char *buf, int size);
- static PRStatus getBody(PRFileDesc *fd, char **buf, int *actualBytesRead);
- static PRBool isWhiteSpace(char ch);
- static PRStatus sendFullData( PRFileDesc *fd, char *buf, int timeOut);
- /**
- * Helper functions to parse URL
- */
- static PRStatus parseURI(const char *url, char **host, PRInt32 *port, char **path, int *sslOn);
- static void toLowerCase(char* str);
- static PRStatus parseAtPort(const char* url, PRInt32 *port, char **path);
- static PRStatus parseAtPath(const char *url, char **path);
- static PRInt32 getPort(const char* src);
- static PRBool isAsciiSpace(char aChar);
- static PRBool isAsciiDigit(char aChar);
- static char * isHttpReq(const char *url, int *sslOn);
- /*To get config from entry*/
- static int readConfigLDAPurl(Slapi_ComponentId *plugin_id, char *plugindn);
- static int parseHTTPConfigEntry(Slapi_Entry *e);
- static int parseConfigEntry(Slapi_Entry *e);
- /*SSL functions */
- PRFileDesc* setupSSLSocket(PRFileDesc* fd);
- /*SSL callback functions */
- SECStatus badCertHandler(void *arg, PRFileDesc *socket);
- SECStatus authCertificate(void *arg, PRFileDesc *socket, PRBool checksig, PRBool isServer);
- SECStatus getClientAuthData(void *arg, PRFileDesc *socket,struct CERTDistNamesStr *caNames, struct CERTCertificateStr **pRetCert, struct SECKEYPrivateKeyStr **pRetKey);
- SECStatus handshakeCallback(PRFileDesc *socket, void *arg);
- static int doRequestRetry(const char *url, httpheader **httpheaderArray, char *body, char **buf, int *bytesRead, int reqType)
- {
- int status = HTTP_IMPL_SUCCESS;
- int retrycnt = 0;
- int i = 1;
- retrycnt = httpConfig->retryCount;
-
- if (retrycnt == 0) {
- LDAPDebug( LDAP_DEBUG_PLUGIN, "doRequestRetry: Retry Count cannot be read. Setting to default value of 3 \n", 0,0,0);
- retrycnt = 3;
- }
- status = doRequest(url, httpheaderArray, body, buf, bytesRead, reqType);
- if (status != HTTP_IMPL_SUCCESS) {
- LDAPDebug( LDAP_DEBUG_PLUGIN, "doRequestRetry: Failed to perform http request \n", 0,0,0);
- while (retrycnt > 0) {
- LDAPDebug( LDAP_DEBUG_PLUGIN, "doRequestRetry: Retrying http request %d.\n", i,0,0);
- status = doRequest(url, httpheaderArray, body, buf, bytesRead, reqType);
- if (status == HTTP_IMPL_SUCCESS) {
- break;
- }
- retrycnt--;
- i++;
- }
- if (status != HTTP_IMPL_SUCCESS) {
- LDAPDebug( LDAP_DEBUG_ANY, "doRequestRetry: Failed to perform http request after %d attempts.\n", i,0,0);
- LDAPDebug( LDAP_DEBUG_ANY, "doRequestRetry: Verify plugin URI configuration and contact Directory Administrator.\n",0,0,0);
- }
- }
- return status;
- }
- static int doRequest(const char *url, httpheader **httpheaderArray, char *body, char **buf, int *bytesRead, int reqType)
- {
- PRStatus status = PR_SUCCESS;
- char *host = NULL;
- char *path = NULL;
- PRFileDesc *fd = NULL;
- PRNetAddr addr;
- PRInt32 port;
- PRInt32 errcode = 0;
- PRInt32 http_connection_time_out = 0;
- PRInt32 sslOn = 0;
-
- LDAPDebug( LDAP_DEBUG_PLUGIN, "--> doRequest -- BEGIN\n",0,0,0);
- LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> url=[%s] \n",url,0,0);
- /* Parse the URL and initialize the host, port, path */
- if (parseURI(url, &host, &port, &path, &sslOn) == PR_FAILURE) {
- slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
- "doRequest: %s \n", HTTP_ERROR_BAD_URL);
- status = PR_FAILURE;
- goto bail;
- }
- LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> host=[%s] port[%d] path[%s] \n",host,port,path);
- /* Initialize the Net Addr */
- if (PR_StringToNetAddr(host, &addr) == PR_FAILURE) {
- char buf[PR_NETDB_BUF_SIZE];
- PRHostEnt ent;
- status = PR_GetIPNodeByName(host, PR_AF_INET, PR_AI_DEFAULT, buf, sizeof(buf), &ent);
- if (status == PR_SUCCESS) {
- PR_EnumerateHostEnt(0, &ent, (PRUint16)port, &addr);
- } else {
- slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
- "doRequest: %s\n", HTTP_ERROR_NET_ADDR);
- status = HTTP_CLIENT_ERROR_NET_ADDR;
- goto bail;
- }
- } else {
- addr.inet.port = (PRUint16)port;
- }
- LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> Successfully created NetAddr \n",0,0,0);
- /* open a TCP connection to the server */
- fd = PR_NewTCPSocket();
- if (!fd) {
- slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
- "doRequest: %s\n", HTTP_ERROR_SOCKET_CREATE);
- status = HTTP_CLIENT_ERROR_SOCKET_CREATE;
- goto bail;
- }
- LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> Successfully created New TCP Socket \n",0,0,0);
- /* immediately send the response */
- setTCPNoDelay(fd);
- if (sslOn) {
- fd = setupSSLSocket(fd);
- if (fd == NULL) {
- slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
- "doRequest: %s\n", HTTP_ERROR_SSLSOCKET_CREATE);
- status = HTTP_CLIENT_ERROR_SSLSOCKET_CREATE;
- goto bail;
- }
-
- if (SSL_SetURL(fd, host) != 0) {
- errcode = PR_GetError();
- slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
- "doRequest: SSL_SetURL -> NSPR Error code (%d) \n", errcode);
- status = HTTP_CLIENT_ERROR_SSLSOCKET_CREATE;
- goto bail;
- }
- }
-
- http_connection_time_out = httpConfig->connectionTimeOut;
- /* connect to the host */
- if (PR_Connect(fd, &addr, PR_MillisecondsToInterval(http_connection_time_out)) == PR_FAILURE) {
- errcode = PR_GetError();
- slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
- "doRequest: %s (%s:%d) -> NSPR Error code (%d)\n",
- HTTP_ERROR_CONNECT_FAILED, host, addr.inet.port, errcode);
- status = HTTP_CLIENT_ERROR_CONNECT_FAILED;
- goto bail;
- }
- LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> Successfully connected to host [%s] \n",host,0,0);
- /* send the request to the server */
- if (reqType == HTTP_REQ_TYPE_POST) {
- if (sendPostReq(fd, path, httpheaderArray, body) == PR_FAILURE) {
- slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
- "doRequest-sendPostReq: %s (%s)\n", HTTP_ERROR_SEND_REQ, path);
- status = HTTP_CLIENT_ERROR_SEND_REQ;
- goto bail;
- }
- }
- else {
- if (sendGetReq(fd, path) == PR_FAILURE) {
- slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
- "doRequest-sendGetReq: %s (%s)\n", HTTP_ERROR_SEND_REQ, path);
- status = HTTP_CLIENT_ERROR_SEND_REQ;
- goto bail;
- }
- }
- LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> Successfully sent the request [%s] \n",path,0,0);
- /* read the response */
- if (processResponse(fd, buf, bytesRead, reqType) == PR_FAILURE) {
- slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
- "doRequest: %s (%s)\n", HTTP_ERROR_BAD_RESPONSE, url);
- status = HTTP_CLIENT_ERROR_BAD_RESPONSE;
- goto bail;
- }
- LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> Successfully read the response\n",0,0,0);
- bail:
- if (host) {
- PR_Free(host);
- }
- if (path) {
- PR_Free(path);
- }
- if (fd) {
- PR_Close(fd);
- fd = NULL;
- }
- LDAPDebug( LDAP_DEBUG_PLUGIN, "<-- doRequest -- END\n",0,0,0);
- return status;
- }
- static PRStatus processResponse(PRFileDesc *fd, char **resBUF, int *bytesRead, int reqType)
- {
- PRStatus status = PR_SUCCESS;
- char *location = NULL;
- char *protocol = NULL;
- char *statusNum = NULL;
- char *statusString = NULL;
- char *headers = NULL;
- char tmp[HTTP_DEFAULT_BUFFER_SIZE];
- int pos=0;
- char ch;
- int index;
- int retcode;
- PRBool doneParsing = PR_FALSE;
- PRBool isRedirect = PR_FALSE;
- char name[HTTP_DEFAULT_BUFFER_SIZE];
- char value[HTTP_DEFAULT_BUFFER_SIZE];
- PRBool atEOL = PR_FALSE;
- PRBool inName = PR_TRUE;
- /* PKBxxx: If we are getting a redirect and the response is more the
- * the HTTP_DEFAULT_BUFFER_SIZE, it will cause the server to crash. A 4k
- * buffer should be good enough.
- */
- LDAPDebug( LDAP_DEBUG_PLUGIN, "--> processResponse -- BEGIN\n",0,0,0);
- headers = (char *)PR_Calloc(1, 4 * HTTP_DEFAULT_BUFFER_SIZE);
- /* Get protocol string */
- index = 0;
- while (1) {
- status = getChar(fd, headers+pos);
- if (status == PR_FAILURE) {
- /* Error : */
- goto bail;
- }
- ch = (char)headers[pos];
- pos++;
- if (!isWhiteSpace(ch)) {
- tmp[index++] = ch;
- } else {
- break;
- }
- }
- tmp[index] = '\0';
- protocol = PL_strdup(tmp);
- LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> protocol=[%s] \n",protocol,0,0);
- /* Get status num */
- index = 0;
- while (1) {
- status = getChar(fd, headers+pos);
- if (status == PR_FAILURE) {
- /* Error : */
- goto bail;
- }
- ch = (char)headers[pos];
- pos++;
- if (!isWhiteSpace(ch)) {
- tmp[index++] = ch;
- } else {
- break;
- }
- }
- tmp[index] = '\0';
- statusNum = PL_strdup(tmp);
- retcode=atoi(tmp);
- LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> statusNum=[%s] \n",statusNum,0,0);
- if (HTTP_RESPONSE_REDIRECT && (reqType == HTTP_REQ_TYPE_REDIRECT)) {
- isRedirect = PR_TRUE;
- }
- /* Get status string */
- if (ch != '\r')
- {
- index = 0;
- while (ch != '\r') {
- status = getChar(fd, headers+pos);
- if (status == PR_FAILURE) {
- /* Error : */
- goto bail;
- }
- ch = (char)headers[pos];
- pos++;
- tmp[index++] = ch;
- }
- tmp[index] = '\0';
- statusString = PL_strdup(tmp);
- LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> statusString [%s] \n",statusString,0,0);
- }
- /**
- * Skip CRLF
- */
- status = getChar(fd, headers+pos);
- if (status == PR_FAILURE) {
- /* Error : */
- goto bail;
- }
- ch = (char)headers[pos];
- pos++;
- /**
- * loop over response headers
- */
- index = 0;
- while (!doneParsing) {
- status = getChar(fd, headers+pos);
- if (status == PR_FAILURE) {
- /* Error : */
- goto bail;
- }
- ch = (char)headers[pos];
- pos++;
- switch(ch)
- {
- case ':':
- if (inName) {
- name[index] = '\0';
- index = 0;
- inName = PR_FALSE;
- /* skip whitespace */
- ch = ' ';
-
- /* status = getChar(fd, headers+pos);
- if (status == PR_FAILURE) {
- goto bail;
- }
- ch = (char)headers[pos];
- pos++; */
- while(isWhiteSpace(ch)) {
- status = getChar(fd, headers+pos);
- if (status == PR_FAILURE) {
- /* Error : */
- goto bail;
- }
- ch = (char)headers[pos];
- pos++;
- }
- value[index++] = ch;
- } else {
- value[index++] = ch;
- }
- break;
- case '\r':
- if (inName && !atEOL) {
- return PR_FALSE;
- }
- break;
- case '\n':
- if (atEOL) {
- doneParsing = PR_TRUE;
- break;
- }
- if (inName) {
- return PR_FALSE;
- }
- value[index] = '\0';
- index = 0;
- inName = PR_TRUE;
- LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> name=[%s] value=[%s]\n",name,value,0);
- if (isRedirect && !PL_strcasecmp(name,"location")) {
- location = PL_strdup(value);
- }
- atEOL = PR_TRUE;
- break;
- default:
- atEOL = PR_FALSE;
- if (inName) {
- name[index++] = ch;
- } else {
- value[index++] = ch;
- }
- break;
- }
- }
- if (!isRedirect) {
- getBody(fd, resBUF, bytesRead);
- } else {
- *resBUF = PL_strdup(location);
- *bytesRead = strlen(location);
- }
- LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> Response Buffer=[%s] bytesRead=[%d] \n",*resBUF,*bytesRead,0);
- bail:
- if (headers) {
- PR_Free(headers);
- }
- if (protocol) {
- PL_strfree(protocol);
- }
- if (statusNum) {
- PL_strfree(statusNum);
- }
- if (statusString) {
- PL_strfree(statusString);
- }
- if (location) {
- PL_strfree(location);
- }
-
- LDAPDebug( LDAP_DEBUG_PLUGIN, "<-- processResponse -- END\n",0,0,0);
- return status;
- }
- static PRStatus sendGetReq(PRFileDesc *fd, const char *path)
- {
- PRStatus status = PR_SUCCESS;
- char *reqBUF = NULL;
- PRInt32 http_connection_time_out = 0;
- int buflen = (HTTP_GET_STD_LEN + strlen(path));
- reqBUF = (char *)PR_Calloc(1, buflen);
- if (!reqBUF) {
- status = PR_FAILURE;
- goto out;
- }
- strcpy(reqBUF, HTTP_GET);
- strcat(reqBUF, " ");
- strcat(reqBUF, path);
- strcat(reqBUF, " ");
- strcat(reqBUF, HTTP_PROTOCOL);
- strcat(reqBUF, "\r\n\r\n\0");
- http_connection_time_out = httpConfig->connectionTimeOut;
- status = sendFullData( fd, reqBUF, http_connection_time_out);
- out:
- if (reqBUF) {
- PR_Free(reqBUF);
- reqBUF = 0;
- }
- return status;
- }
- static PRStatus sendFullData( PRFileDesc *fd, char *buf, int timeOut)
- {
- int dataSent = 0;
- int bufLen = strlen(buf);
- int retVal = 0;
- PRInt32 errcode = 0;
- while (dataSent < bufLen)
- {
- retVal = PR_Send(fd, buf+dataSent, bufLen-dataSent, 0, PR_MillisecondsToInterval(timeOut));
- if (retVal == -1 )
- break;
- dataSent += retVal;
- }
- if (dataSent == bufLen )
- return PR_SUCCESS;
- else
- {
- errcode = PR_GetError();
- slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
- "sendFullData: dataSent=%d bufLen=%d -> NSPR Error code (%d)\n",
- dataSent, bufLen, errcode);
- LDAPDebug( LDAP_DEBUG_PLUGIN, "---------->NSPR Error code (%d) \n", errcode,0,0);
- return PR_FAILURE;
- }
- }
- static PRStatus sendPostReq(PRFileDesc *fd, const char *path, httpheader **httpheaderArray, char *body)
- {
- PRStatus status = PR_SUCCESS;
- char body_len_str[20];
- char *reqBUF = NULL;
- PRInt32 http_connection_time_out = 0;
- int i = 0;
- int body_len, buflen = 0;
- if (body) {
- body_len = strlen(body);
- } else {
- body_len = 0;
- }
- PR_snprintf(body_len_str, sizeof(body_len_str), "%d", body_len);
- buflen = (HTTP_POST_STD_LEN + strlen(path) + body_len + strlen(body_len_str));
- for (i = 0; httpheaderArray[i] != NULL; i++) {
- if (httpheaderArray[i]->name != NULL)
- {
- buflen += strlen(httpheaderArray[i]->name) + 2;
- if (httpheaderArray[i]->value != NULL)
- buflen += strlen(httpheaderArray[i]->value) + 2;
- }
- }
- reqBUF = (char *)PR_Calloc(1, buflen);
- if (!reqBUF) {
- status = PR_FAILURE;
- goto out;
- }
- strcpy(reqBUF, HTTP_POST);
- strcat(reqBUF, " ");
- strcat(reqBUF, path);
- strcat(reqBUF, " ");
- strcat(reqBUF, HTTP_PROTOCOL);
- strcat(reqBUF, "\r\n");
- strcat(reqBUF, HTTP_CONTENT_LENGTH);
- strcat(reqBUF, " ");
- strcat(reqBUF, body_len_str);
- strcat(reqBUF, "\r\n");
- strcat(reqBUF, HTTP_CONTENT_TYPE_URL_ENCODED);
- strcat(reqBUF, "\r\n");
-
- for (i = 0; httpheaderArray[i] != NULL; i++) {
- if (httpheaderArray[i]->name != NULL)
- strcat(reqBUF, httpheaderArray[i]->name);
- strcat(reqBUF, ": ");
- if (httpheaderArray[i]->value != NULL)
- strcat(reqBUF, httpheaderArray[i]->value);
- strcat(reqBUF, "\r\n");
- }
- strcat(reqBUF, "\r\n");
- if (body) {
- strcat(reqBUF, body);
- }
- strcat(reqBUF, "\0");
- LDAPDebug( LDAP_DEBUG_PLUGIN, "---------->reqBUF is %s \n",reqBUF,0,0);
- http_connection_time_out = httpConfig->connectionTimeOut;
- status = sendFullData( fd, reqBUF, http_connection_time_out);
- out:
- if (reqBUF) {
- PR_Free(reqBUF);
- reqBUF = 0;
- }
- return status;
- }
- static PRStatus getChar(PRFileDesc *fd, char *buf)
- {
- PRInt32 bytesRead = http_read(fd, buf, 1);
- if (bytesRead <=0) {
- PRInt32 errcode = PR_GetError();
- slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
- "getChar: NSPR Error code (%d)\n", errcode);
- return PR_FAILURE;
- }
- return PR_SUCCESS;
- }
- static PRStatus getBody(PRFileDesc *fd, char **buf, int *actualBytesRead)
- {
- int totalBytesRead = 0;
- int size = 4 * HTTP_DEFAULT_BUFFER_SIZE;
- int bytesRead = size;
- char *data = (char *) PR_Calloc(1, size);
- while (bytesRead == size) {
- bytesRead = http_read(fd, (data+totalBytesRead), size);
- if (bytesRead <= 0) {
- /* Read error */
- return PR_FAILURE;
- }
- if (bytesRead == size) {
- /* more data to be read so increase the buffer */
- size = size * 2 ;
- data = (char *) PR_Realloc(data, size);
- }
- totalBytesRead += bytesRead;
- }
- *buf = data;
- *actualBytesRead = totalBytesRead;
- return PR_SUCCESS;
- }
- static PRInt32 http_read(PRFileDesc *fd, char *buf, int size)
- {
- PRInt32 http_read_time_out = 0;
- http_read_time_out = httpConfig->readTimeOut;
- return PR_Recv(fd, buf, size, 0, PR_MillisecondsToInterval(http_read_time_out));
- }
- static PRBool isWhiteSpace(char ch)
- {
- PRBool b = PR_FALSE;
- if (ch == ' ') {
- b = PR_TRUE;
- }
- return b;
- }
- static PRStatus parseURI(const char *urlstr, char **host, PRInt32 *port, char **path, int *sslOn)
- {
- PRStatus status = PR_SUCCESS;
- char *brk;
- int len;
- static const char delimiters[] = ":/?#";
- char *url = isHttpReq(urlstr, sslOn);
-
- if (*sslOn) {
- *port = 443;
- }
- else {
- *port = 80;
- }
- if (url == NULL) {
- /* Error : */
- status = PR_FAILURE;
- goto bail;
- }
- len = PL_strlen(url);
- /* Currently we do not support Ipv6 addresses */
- brk = PL_strpbrk(url, delimiters);
- if (!brk) {
- *host = PL_strndup(url, len);
- toLowerCase(*host);
- goto bail;
- }
- switch (*brk)
- {
- case '/' :
- case '?' :
- case '#' :
- /* Get the Host, the rest is Path */
- *host = PL_strndup(url, (brk - url));
- toLowerCase(*host);
- status = parseAtPath(brk, path);
- break;
- case ':' :
- /* Get the Host and process port, path */
- *host = PL_strndup(url, (brk - url));
- toLowerCase(*host);
- status = parseAtPort(brk+1, port, path);
- break;
- default:
- /* Error : HTTP_BAD_URL */
- break;
- }
- bail:
- if (url) {
- PR_Free(url);
- }
- return status;
- }
- static PRStatus parseAtPort(const char* url, PRInt32 *port, char **path)
- {
- PRStatus status = PR_SUCCESS;
- static const char delimiters[] = "/?#";
- char* brk = PL_strpbrk(url, delimiters);
- if (!brk) /* everything is a Port */
- {
- *port = getPort(url);
- if (*port <= 0) {
- /* Error : HTTP_BAD_URL */
- return PR_FAILURE;
- } else {
- return status;
- }
- }
- switch (*brk)
- {
- case '/' :
- case '?' :
- case '#' :
- /* Get the Port, the rest is Path */
- *port = getPort(url);
- if (*port <= 0) {
- /* Error : HTTP_BAD_URL */
- return PR_FAILURE;
- }
- status = parseAtPath(brk, path);
- break;
- default:
- /* Error : HTTP_BAD_URL */
- break;
- }
- return status;
- }
- static PRStatus parseAtPath(const char *url, char **path)
- {
- PRStatus status = PR_SUCCESS;
- char *dir = "%s%s";
- *path = (char *)PR_Calloc(1, strlen(dir) + strlen(url) + 2);
- if (!*path) {
- /* Error : HTTP_BAD_URL */
- status = PR_FAILURE;
- goto out;
- }
- /* Just write the path and check for a starting / */
- if ('/' != *url) {
- sprintf(*path, dir, "/", url);
- } else {
- strcpy(*path, url);
- }
- out:
- return status;
- }
- static void toLowerCase(char* str)
- {
- if (str) {
- char* lstr = str;
- PRInt8 shift = 'a' - 'A';
- for(; (*lstr != '\0'); ++lstr) {
- if ((*(lstr) <= 'Z') && (*(lstr) >= 'A')) {
- *(lstr) = *(lstr) + shift;
- }
- }
- }
- }
- static PRInt32 getPort(const char* src)
- {
- /* search for digits up to a slash or the string ends */
- const char* port = src;
- PRInt32 returnValue = -1;
- char c;
- /* skip leading white space */
- while (isAsciiSpace(*port))
- port++;
- while ((c = *port++) != '\0') {
- /* stop if slash or ? or # reached */
- if (c == '/' || c == '?' || c == '#')
- break;
- else if (!isAsciiDigit(c))
- return returnValue;
- }
- return (0 < PR_sscanf(src, "%d", &returnValue)) ? returnValue : -1;
- }
- static PRBool isAsciiSpace(char aChar)
- {
- if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) {
- return PR_TRUE;
- }
- return PR_FALSE;
- }
- static PRBool isAsciiDigit(char aChar)
- {
- if ((aChar >= '0') && (aChar <= '9')) {
- return PR_TRUE;
- }
- return PR_FALSE;
- }
- static void setTCPNoDelay(PRFileDesc* fd)
- {
- PRStatus status = PR_SUCCESS;
- PRSocketOptionData opt;
- opt.option = PR_SockOpt_NoDelay;
- opt.value.no_delay = PR_FALSE;
- status = PR_GetSocketOption(fd, &opt);
- if (status == PR_FAILURE) {
- return;
- }
- opt.option = PR_SockOpt_NoDelay;
- opt.value.no_delay = PR_TRUE;
- status = PR_SetSocketOption(fd, &opt);
- if (status == PR_FAILURE) {
- return;
- }
- return;
- }
- static char * isHttpReq(const char *url, int *sslOn)
- {
- static const char http_protopol_header[] = "http://";
- static const char https_protopol_header[] = "https://";
- char *newstr = NULL;
- /* skip leading white space */
- while (isAsciiSpace(*url))
- url++;
- if (strncmp(url, http_protopol_header, strlen(http_protopol_header)) == 0) {
- newstr = (char *)PR_Calloc(1, (strlen(url)-strlen(http_protopol_header) + 1));
- strcpy(newstr, url+7);
- strcat(newstr,"\0");
- *sslOn = 0;
- }
- else if (strncmp(url, https_protopol_header, strlen(https_protopol_header)) == 0) {
- newstr = (char *)PR_Calloc(1, (strlen(url)-strlen(https_protopol_header) + 1));
- strcpy(newstr, url+8);
- strcat(newstr,"\0");
- *sslOn = 1;
- }
- return newstr;
- }
- PRFileDesc* setupSSLSocket(PRFileDesc* fd)
- {
- SECStatus secStatus;
- PRFileDesc* sslSocket;
- PRSocketOptionData socketOption;
- char *certNickname = NULL;
- socketOption.option = PR_SockOpt_Nonblocking;
- socketOption.value.non_blocking = PR_FALSE;
- if( PR_SetSocketOption(fd, &socketOption) != 0) {
- slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
- "Cannot set socket option NSS \n");
- return NULL;
- }
- sslSocket = SSL_ImportFD(NULL, fd);
- if (!sslSocket) {
- slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
- "setupSSLSocket: Cannot import to SSL Socket\n" );
- goto sslbail;
- }
-
- slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
- "setupSSLSocket: setupssl socket created\n" );
- secStatus = SSL_OptionSet(sslSocket, SSL_SECURITY, 1);
- if (SECSuccess != secStatus) {
- slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
- "setupSSLSocket: Cannot set SSL_SECURITY option\n");
- goto sslbail;
- }
- secStatus = SSL_OptionSet(sslSocket, SSL_HANDSHAKE_AS_CLIENT, 1);
- if (SECSuccess != secStatus) {
- slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
- "setupSSLSocket: CAnnot set SSL_HANDSHAKE_AS_CLIENT option\n");
- goto sslbail;
- }
-
- /* Set SSL callback routines. */
- secStatus = SSL_GetClientAuthDataHook(sslSocket,
- (SSLGetClientAuthData) getClientAuthData,
- (void *)certNickname);
- if (secStatus != SECSuccess) {
- slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
- "setupSSLSocket: SSL_GetClientAuthDataHook Failed\n");
- goto sslbail;
- }
- secStatus = SSL_AuthCertificateHook(sslSocket,
- (SSLAuthCertificate) authCertificate,
- (void *)CERT_GetDefaultCertDB());
- if (secStatus != SECSuccess) {
- slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
- "setupSSLSocket: SSL_AuthCertificateHook Failed\n");
- goto sslbail;
- }
- secStatus = SSL_BadCertHook(sslSocket,
- (SSLBadCertHandler) badCertHandler, NULL);
- if (secStatus != SECSuccess) {
- slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
- "setupSSLSocket: SSL_BadCertHook Failed\n");
- goto sslbail;
- }
- secStatus = SSL_HandshakeCallback(sslSocket,
- (SSLHandshakeCallback) handshakeCallback, NULL);
- if (secStatus != SECSuccess) {
- slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
- "setupSSLSocket: SSL_HandshakeCallback Failed\n");
- goto sslbail;
- }
- return sslSocket;
- sslbail:
- PR_Close(fd);
- return NULL;
- }
- SECStatus
- authCertificate(void *arg, PRFileDesc *socket,
- PRBool checksig, PRBool isServer)
- {
- SECCertUsage certUsage;
- CERTCertificate * cert;
- void * pinArg;
- char * hostName;
- SECStatus secStatus;
- if (!arg || !socket) {
- slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
- " authCertificate: Faulty socket in callback function \n");
- return SECFailure;
- }
- /* Define how the cert is being used based upon the isServer flag. */
- certUsage = isServer ? certUsageSSLClient : certUsageSSLServer;
- cert = SSL_PeerCertificate(socket);
- pinArg = SSL_RevealPinArg(socket);
- secStatus = CERT_VerifyCertNow((CERTCertDBHandle *)arg,
- cert,
- checksig,
- certUsage,
- pinArg);
- /* If this is a server, we're finished. */
- if (isServer || secStatus != SECSuccess) {
- return secStatus;
- }
- hostName = SSL_RevealURL(socket);
- if (hostName && hostName[0]) {
- secStatus = CERT_VerifyCertName(cert, hostName);
- } else {
- PR_SetError(SSL_ERROR_BAD_CERT_DOMAIN, 0);
- secStatus = SECFailure;
- }
- if (hostName)
- PR_Free(hostName);
- return secStatus;
- }
- SECStatus
- badCertHandler(void *arg, PRFileDesc *socket)
- {
- SECStatus secStatus = SECFailure;
- PRErrorCode err;
- /* log invalid cert here */
- if (!arg) {
- return secStatus;
- }
- *(PRErrorCode *)arg = err = PORT_GetError();
- switch (err) {
- case SEC_ERROR_INVALID_AVA:
- case SEC_ERROR_INVALID_TIME:
- case SEC_ERROR_BAD_SIGNATURE:
- case SEC_ERROR_EXPIRED_CERTIFICATE:
- case SEC_ERROR_UNKNOWN_ISSUER:
- case SEC_ERROR_UNTRUSTED_CERT:
- case SEC_ERROR_CERT_VALID:
- case SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE:
- case SEC_ERROR_CRL_EXPIRED:
- case SEC_ERROR_CRL_BAD_SIGNATURE:
- case SEC_ERROR_EXTENSION_VALUE_INVALID:
- case SEC_ERROR_CA_CERT_INVALID:
- case SEC_ERROR_CERT_USAGES_INVALID:
- case SEC_ERROR_UNKNOWN_CRITICAL_EXTENSION:
- secStatus = SECSuccess;
- break;
- default:
- secStatus = SECFailure;
- break;
- }
- slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
- "Bad certificate: %d\n", err);
- return secStatus;
- }
- SECStatus
- getClientAuthData(void *arg,
- PRFileDesc *socket,
- struct CERTDistNamesStr *caNames,
- struct CERTCertificateStr **pRetCert,
- struct SECKEYPrivateKeyStr **pRetKey)
- {
- CERTCertificate * cert;
- SECKEYPrivateKey * privKey = NULL;
- char * chosenNickName = (char *)arg;
- void * proto_win = NULL;
- SECStatus secStatus = SECFailure;
- proto_win = SSL_RevealPinArg(socket);
- if (chosenNickName) {
- cert = PK11_FindCertFromNickname(chosenNickName, proto_win);
- if (cert) {
- privKey = PK11_FindKeyByAnyCert(cert, proto_win);
- if (privKey) {
- secStatus = SECSuccess;
- } else {
- CERT_DestroyCertificate(cert);
- }
- }
- } else { /* no nickname given, automatically find the right cert */
- CERTCertNicknames *names;
- int i;
- names = CERT_GetCertNicknames(CERT_GetDefaultCertDB(),
- SEC_CERT_NICKNAMES_USER, proto_win);
- if (names != NULL) {
- for(i = 0; i < names->numnicknames; i++ ) {
- cert = PK11_FindCertFromNickname(names->nicknames[i],
- proto_win);
- if (!cert) {
- continue;
- }
- /* Only check unexpired certs */
- if (CERT_CheckCertValidTimes(cert, PR_Now(), PR_FALSE)
- != secCertTimeValid ) {
- CERT_DestroyCertificate(cert);
- continue;
- }
- secStatus = NSS_CmpCertChainWCANames(cert, caNames);
- if (secStatus == SECSuccess) {
- privKey = PK11_FindKeyByAnyCert(cert, proto_win);
- if (privKey) {
- break;
- }
- secStatus = SECFailure;
- break;
- }
- CERT_FreeNicknames(names);
- } /* for loop */
- }
- }
- if (secStatus == SECSuccess) {
- *pRetCert = cert;
- *pRetKey = privKey;
- }
- return secStatus;
- }
- SECStatus
- handshakeCallback(PRFileDesc *socket, void *arg)
- {
- slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
- "----------> Handshake has completed, ready to send data securely.\n");
- return SECSuccess;
- }
- /**
- * PUBLIC FUNCTIONS IMPLEMENTATION
- */
- int http_impl_init(Slapi_ComponentId *plugin_id)
- {
- int status = HTTP_IMPL_SUCCESS;
- slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
- "-> http_impl_init \n");
- httpConfig = NULL;
- httpConfig = (httpPluginConfig *) slapi_ch_calloc(1, sizeof(httpPluginConfig));
- status = readConfigLDAPurl(plugin_id, HTTP_PLUGIN_DN);
- if (status != 0) {
- slapi_log_error(SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
- "http_impl_start: Unable to get HTTP config information \n");
- return HTTP_IMPL_FAILURE;
- }
-
- status = readConfigLDAPurl(plugin_id, CONFIG_DN);
- if (status != 0) {
- slapi_log_error(SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
- "http_impl_start: Unable to get config information \n");
- return HTTP_IMPL_FAILURE;
- }
-
- slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
- "<- http_impl_init \n");
- return status;
- }
- int http_impl_get_text(char *url, char **data, int *bytesRead)
- {
- int status = HTTP_IMPL_SUCCESS;
- status = doRequestRetry(url, NULL, NULL, data, bytesRead, HTTP_REQ_TYPE_GET);
- return status;
- }
- int http_impl_get_binary(char *url, char **data, int *bytesRead)
- {
- int status = HTTP_IMPL_SUCCESS;
- status = doRequestRetry(url, NULL, NULL, data, bytesRead, HTTP_REQ_TYPE_GET);
- return status;
- }
- int http_impl_get_redirected_uri(char *url, char **data, int *bytesRead)
- {
- int status = HTTP_IMPL_SUCCESS;
- status = doRequestRetry(url, NULL, NULL, data, bytesRead, HTTP_REQ_TYPE_REDIRECT);
- return status;
- }
- int http_impl_post(char *url, httpheader **httpheaderArray, char *body, char **data, int *bytesRead)
- {
- int status = HTTP_IMPL_SUCCESS;
- status = doRequestRetry(url, httpheaderArray, body, data, bytesRead, HTTP_REQ_TYPE_POST);
- return status;
- }
- void http_impl_shutdown()
- {
- /**
- * Put cleanup code here
- */
- }
- static int readConfigLDAPurl(Slapi_ComponentId *plugin_id, char *plugindn) {
- int rc = LDAP_SUCCESS;
- Slapi_DN *sdn = NULL;
- int status = HTTP_IMPL_SUCCESS;
- Slapi_Entry *entry = NULL;
-
- sdn = slapi_sdn_new_dn_byref(plugindn);
- rc = slapi_search_internal_get_entry(sdn, NULL, &entry, plugin_id);
- slapi_sdn_free(&sdn);
- if (rc != LDAP_SUCCESS) {
- slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
- "readConfigLDAPurl: Could not find entry %s (error %d)\n", plugindn, rc);
- status = HTTP_IMPL_FAILURE;
- return status;
- }
- if (NULL == entry)
- {
- slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
- "readConfigLDAPurl: No entries found for <%s>\n", plugindn);
- status = HTTP_IMPL_FAILURE;
- return status;
- }
- if ((PL_strcasecmp(plugindn, HTTP_PLUGIN_DN) == 0))
- status = parseHTTPConfigEntry(entry);
- else
- status = parseConfigEntry(entry);
- slapi_entry_free(entry);
- return status;
- }
- /* Retrieves the plugin configuration info */
- /* Retrieves security info as well as the path info required for the SSL
- config dir */
- static int parseConfigEntry(Slapi_Entry *e)
- {
- char *value = NULL;
- value = slapi_entry_attr_get_charptr(e, ATTR_DS_SECURITY);
- if (value) {
- httpConfig->DS_sslOn = value;
- }
- return HTTP_IMPL_SUCCESS;
- }
- static int parseHTTPConfigEntry(Slapi_Entry *e)
- {
- int value = 0;
- value = slapi_entry_attr_get_int(e, ATTR_RETRY_COUNT);
- if (value) {
- httpConfig->retryCount = value;
- }
- value = slapi_entry_attr_get_int(e, ATTR_CONNECTION_TIME_OUT);
- if (value) {
- httpConfig->connectionTimeOut = value;
- }
- else {
- LDAPDebug( LDAP_DEBUG_PLUGIN, "parseHTTPConfigEntry: HTTP Connection Time Out cannot be read. Setting to default value of 5000 ms \n", 0,0,0);
- httpConfig->connectionTimeOut = 5000;
- }
- value = slapi_entry_attr_get_int(e, ATTR_READ_TIME_OUT);
- if (value) {
- httpConfig->readTimeOut = value;
- }
- else {
- LDAPDebug( LDAP_DEBUG_PLUGIN, "parseHTTPConfigEntry: HTTP Read Time Out cannot be read. Setting to default value of 5000 ms \n", 0,0,0);
- httpConfig->readTimeOut = 5000;
- }
-
- httpConfig->nssInitialized = 0;
- return HTTP_IMPL_SUCCESS;
- }
- /**
- * Self Testing
- */
- #ifdef BUILD_STANDALONE
- int main(int argc, char **argv)
- {
- PRStatus status = PR_SUCCESS;
- char *buf;
- int bytes;
- char *host;
- PRInt32 port;
- char *path;
- if (argc < 2) {
- printf("URL missing\n");
- return -1;
- }
- PR_Init(PR_USER_THREAD,PR_PRIORITY_NORMAL, 0);
- doRequest(argv[1], &buf, &bytes, 2);
- printf( "%s\n", buf );
- return -1;
- }
- #endif
|