http_impl.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  1. /* --- BEGIN COPYRIGHT BLOCK ---
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * --- END COPYRIGHT BLOCK --- */
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /**
  13. * Implementation of a Simple HTTP Client
  14. */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "nspr.h"
  18. #include "nss.h"
  19. #include "pk11func.h"
  20. #include "ssl.h"
  21. #include "prprf.h"
  22. #include "plstr.h"
  23. #include "slapi-plugin.h"
  24. #include "http_client.h"
  25. #include "secerr.h"
  26. #include "sslerr.h"
  27. #include "slap.h"
  28. #include "slapi-private.h"
  29. #include "slapi-plugin-compat4.h"
  30. #include <sys/stat.h>
  31. /*** from proto-slap.h ***/
  32. int slapd_log_error_proc( char *subsystem, char *fmt, ... );
  33. /*** from ldaplog.h ***/
  34. /* edited ldaplog.h for LDAPDebug()*/
  35. #ifndef _LDAPLOG_H
  36. #define _LDAPLOG_H
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. #ifdef BUILD_STANDALONE
  41. #define slapi_log_error(a,b,c,d) printf((c),(d))
  42. #define stricmp strcasecmp
  43. #endif
  44. #define LDAP_DEBUG_TRACE 0x00001 /* 1 */
  45. #define LDAP_DEBUG_ANY 0x04000 /* 16384 */
  46. #define LDAP_DEBUG_PLUGIN 0x10000 /* 65536 */
  47. /* debugging stuff */
  48. extern int slapd_ldap_debug;
  49. #define LDAPDebug( level, fmt, arg1, arg2, arg3 ) \
  50. { \
  51. if ( slapd_ldap_debug & level ) { \
  52. slapd_log_error_proc( NULL, fmt, arg1, arg2, arg3 ); \
  53. } \
  54. }
  55. #ifdef __cplusplus
  56. }
  57. #endif
  58. #endif /* _LDAP_H */
  59. #define HTTP_PLUGIN_SUBSYSTEM "http-client-plugin" /* used for logging */
  60. #define HTTP_IMPL_SUCCESS 0
  61. #define HTTP_IMPL_FAILURE -1
  62. #define HTTP_REQ_TYPE_GET 1
  63. #define HTTP_REQ_TYPE_REDIRECT 2
  64. #define HTTP_REQ_TYPE_POST 3
  65. #define HTTP_GET "GET"
  66. #define HTTP_POST "POST"
  67. #define HTTP_PROTOCOL "HTTP/1.0"
  68. #define HTTP_CONTENT_LENGTH "Content-length:"
  69. #define HTTP_CONTENT_TYPE_URL_ENCODED "Content-type: application/x-www-form-urlencoded"
  70. #define HTTP_GET_STD_LEN 18
  71. #define HTTP_POST_STD_LEN 85
  72. #define HTTP_DEFAULT_BUFFER_SIZE 4096
  73. #define HTTP_RESPONSE_REDIRECT (retcode == 302 || retcode == 301)
  74. /**
  75. * Error strings used for logging error messages
  76. */
  77. #define HTTP_ERROR_BAD_URL " Badly formatted URL"
  78. #define HTTP_ERROR_NET_ADDR " NetAddr initialization failed"
  79. #define HTTP_ERROR_SOCKET_CREATE " Creation of socket failed"
  80. #define HTTP_ERROR_SSLSOCKET_CREATE " Creation of SSL socket failed"
  81. #define HTTP_ERROR_CONNECT_FAILED " Couldn't connect to remote host"
  82. #define HTTP_ERROR_SEND_REQ " Send request failed"
  83. #define HTTP_ERROR_BAD_RESPONSE " Invalid response from remote host"
  84. #define HTTP_PLUGIN_DN "cn=HTTP Client,cn=plugins,cn=config"
  85. #define CONFIG_DN "cn=config"
  86. #define ATTR_CONNECTION_TIME_OUT "nsHTTPConnectionTimeOut"
  87. #define ATTR_READ_TIME_OUT "nsHTTPReadTimeOut"
  88. #define ATTR_RETRY_COUNT "nsHTTPRetryCount"
  89. #define ATTR_DS_SECURITY "nsslapd-security"
  90. #define ATTR_INSTANCE_PATH "nsslapd-errorlog"
  91. /*static Slapi_ComponentId *plugin_id = NULL;*/
  92. typedef struct {
  93. int retryCount;
  94. int connectionTimeOut;
  95. int readTimeOut;
  96. int nssInitialized;
  97. char *DS_sslOn;
  98. } httpPluginConfig;
  99. httpPluginConfig *httpConfig;
  100. /**
  101. * Public functions
  102. */
  103. int http_impl_init(Slapi_ComponentId *plugin_id);
  104. int http_impl_get_text(char *url, char **data, int *bytesRead);
  105. int http_impl_get_binary(char *url, char **data, int *bytesRead);
  106. int http_impl_get_redirected_uri(char *url, char **data, int *bytesRead);
  107. int http_impl_post(char *url, httpheader **httpheaderArray, char *body, char **data, int *bytesRead);
  108. void http_impl_shutdown(void);
  109. /**
  110. * Http handling functions
  111. */
  112. static int doRequest(const char *url, httpheader **httpheaderArray, char *body, char **buf, int *bytesRead, int reqType);
  113. static int doRequestRetry(const char *url, httpheader **httpheaderArray, char *body, char **buf, int *bytesRead, int reqType);
  114. static void setTCPNoDelay(PRFileDesc* fd);
  115. static PRStatus sendGetReq(PRFileDesc *fd, const char *path);
  116. static PRStatus sendPostReq(PRFileDesc *fd, const char *path, httpheader **httpheaderArray, char *body);
  117. static PRStatus processResponse(PRFileDesc *fd, char **resBUF, int *bytesRead, int reqType);
  118. static PRStatus getChar(PRFileDesc *fd, char *buf);
  119. static PRInt32 http_read(PRFileDesc *fd, char *buf, int size);
  120. static PRStatus getBody(PRFileDesc *fd, char **buf, int *actualBytesRead);
  121. static PRBool isWhiteSpace(char ch);
  122. static PRStatus sendFullData( PRFileDesc *fd, char *buf, int timeOut);
  123. /**
  124. * Helper functions to parse URL
  125. */
  126. static PRStatus parseURI(const char *url, char **host, PRInt32 *port, char **path, int *sslOn);
  127. static void toLowerCase(char* str);
  128. static PRStatus parseAtPort(const char* url, PRInt32 *port, char **path);
  129. static PRStatus parseAtPath(const char *url, char **path);
  130. static PRInt32 getPort(const char* src);
  131. static PRBool isAsciiSpace(char aChar);
  132. static PRBool isAsciiDigit(char aChar);
  133. static char * isHttpReq(const char *url, int *sslOn);
  134. /*To get config from entry*/
  135. static int readConfigLDAPurl(Slapi_ComponentId *plugin_id, char *plugindn);
  136. static int parseHTTPConfigEntry(Slapi_Entry *e);
  137. static int parseConfigEntry(Slapi_Entry *e);
  138. /*SSL functions */
  139. PRFileDesc* setupSSLSocket(PRFileDesc* fd);
  140. /*SSL callback functions */
  141. SECStatus badCertHandler(void *arg, PRFileDesc *socket);
  142. SECStatus authCertificate(void *arg, PRFileDesc *socket, PRBool checksig, PRBool isServer);
  143. SECStatus getClientAuthData(void *arg, PRFileDesc *socket,struct CERTDistNamesStr *caNames, struct CERTCertificateStr **pRetCert, struct SECKEYPrivateKeyStr **pRetKey);
  144. SECStatus handshakeCallback(PRFileDesc *socket, void *arg);
  145. static int doRequestRetry(const char *url, httpheader **httpheaderArray, char *body, char **buf, int *bytesRead, int reqType)
  146. {
  147. int status = HTTP_IMPL_SUCCESS;
  148. int retrycnt = 0;
  149. int i = 1;
  150. retrycnt = httpConfig->retryCount;
  151. if (retrycnt == 0) {
  152. LDAPDebug( LDAP_DEBUG_PLUGIN, "doRequestRetry: Retry Count cannot be read. Setting to default value of 3 \n", 0,0,0);
  153. retrycnt = 3;
  154. }
  155. status = doRequest(url, httpheaderArray, body, buf, bytesRead, reqType);
  156. if (status != HTTP_IMPL_SUCCESS) {
  157. LDAPDebug( LDAP_DEBUG_PLUGIN, "doRequestRetry: Failed to perform http request \n", 0,0,0);
  158. while (retrycnt > 0) {
  159. LDAPDebug( LDAP_DEBUG_PLUGIN, "doRequestRetry: Retrying http request %d.\n", i,0,0);
  160. status = doRequest(url, httpheaderArray, body, buf, bytesRead, reqType);
  161. if (status == HTTP_IMPL_SUCCESS) {
  162. break;
  163. }
  164. retrycnt--;
  165. i++;
  166. }
  167. if (status != HTTP_IMPL_SUCCESS) {
  168. LDAPDebug( LDAP_DEBUG_ANY, "doRequestRetry: Failed to perform http request after %d attempts.\n", i,0,0);
  169. LDAPDebug( LDAP_DEBUG_ANY, "doRequestRetry: Verify plugin URI configuration and contact Directory Administrator.\n",0,0,0);
  170. }
  171. }
  172. return status;
  173. }
  174. static int doRequest(const char *url, httpheader **httpheaderArray, char *body, char **buf, int *bytesRead, int reqType)
  175. {
  176. PRStatus status = PR_SUCCESS;
  177. char *host = NULL;
  178. char *path = NULL;
  179. PRFileDesc *fd = NULL;
  180. PRNetAddr addr;
  181. PRInt32 port;
  182. PRInt32 errcode = 0;
  183. PRInt32 http_connection_time_out = 0;
  184. PRInt32 sslOn = 0;
  185. LDAPDebug( LDAP_DEBUG_PLUGIN, "--> doRequest -- BEGIN\n",0,0,0);
  186. LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> url=[%s] \n",url,0,0);
  187. /* Parse the URL and initialize the host, port, path */
  188. if (parseURI(url, &host, &port, &path, &sslOn) == PR_FAILURE) {
  189. slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
  190. "doRequest: %s \n", HTTP_ERROR_BAD_URL);
  191. status = PR_FAILURE;
  192. goto bail;
  193. }
  194. LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> host=[%s] port[%d] path[%s] \n",host,port,path);
  195. /* Initialize the Net Addr */
  196. if (PR_StringToNetAddr(host, &addr) == PR_FAILURE) {
  197. char buf[PR_NETDB_BUF_SIZE];
  198. PRHostEnt ent;
  199. status = PR_GetIPNodeByName(host, PR_AF_INET, PR_AI_DEFAULT, buf, sizeof(buf), &ent);
  200. if (status == PR_SUCCESS) {
  201. PR_EnumerateHostEnt(0, &ent, (PRUint16)port, &addr);
  202. } else {
  203. slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
  204. "doRequest: %s\n", HTTP_ERROR_NET_ADDR);
  205. status = HTTP_CLIENT_ERROR_NET_ADDR;
  206. goto bail;
  207. }
  208. } else {
  209. addr.inet.port = (PRUint16)port;
  210. }
  211. LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> Successfully created NetAddr \n",0,0,0);
  212. /* open a TCP connection to the server */
  213. fd = PR_NewTCPSocket();
  214. if (!fd) {
  215. slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
  216. "doRequest: %s\n", HTTP_ERROR_SOCKET_CREATE);
  217. status = HTTP_CLIENT_ERROR_SOCKET_CREATE;
  218. goto bail;
  219. }
  220. LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> Successfully created New TCP Socket \n",0,0,0);
  221. /* immediately send the response */
  222. setTCPNoDelay(fd);
  223. if (sslOn) {
  224. fd = setupSSLSocket(fd);
  225. if (fd == NULL) {
  226. slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
  227. "doRequest: %s\n", HTTP_ERROR_SSLSOCKET_CREATE);
  228. status = HTTP_CLIENT_ERROR_SSLSOCKET_CREATE;
  229. goto bail;
  230. }
  231. if (SSL_SetURL(fd, host) != 0) {
  232. errcode = PR_GetError();
  233. slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
  234. "doRequest: SSL_SetURL -> NSPR Error code (%d) \n", errcode);
  235. status = HTTP_CLIENT_ERROR_SSLSOCKET_CREATE;
  236. goto bail;
  237. }
  238. }
  239. http_connection_time_out = httpConfig->connectionTimeOut;
  240. /* connect to the host */
  241. if (PR_Connect(fd, &addr, PR_MillisecondsToInterval(http_connection_time_out)) == PR_FAILURE) {
  242. errcode = PR_GetError();
  243. slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
  244. "doRequest: %s (%s:%d) -> NSPR Error code (%d)\n",
  245. HTTP_ERROR_CONNECT_FAILED, host, addr.inet.port, errcode);
  246. status = HTTP_CLIENT_ERROR_CONNECT_FAILED;
  247. goto bail;
  248. }
  249. LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> Successfully connected to host [%s] \n",host,0,0);
  250. /* send the request to the server */
  251. if (reqType == HTTP_REQ_TYPE_POST) {
  252. if (sendPostReq(fd, path, httpheaderArray, body) == PR_FAILURE) {
  253. slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
  254. "doRequest-sendPostReq: %s (%s)\n", HTTP_ERROR_SEND_REQ, path);
  255. status = HTTP_CLIENT_ERROR_SEND_REQ;
  256. goto bail;
  257. }
  258. }
  259. else {
  260. if (sendGetReq(fd, path) == PR_FAILURE) {
  261. slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
  262. "doRequest-sendGetReq: %s (%s)\n", HTTP_ERROR_SEND_REQ, path);
  263. status = HTTP_CLIENT_ERROR_SEND_REQ;
  264. goto bail;
  265. }
  266. }
  267. LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> Successfully sent the request [%s] \n",path,0,0);
  268. /* read the response */
  269. if (processResponse(fd, buf, bytesRead, reqType) == PR_FAILURE) {
  270. slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
  271. "doRequest: %s (%s)\n", HTTP_ERROR_BAD_RESPONSE, url);
  272. status = HTTP_CLIENT_ERROR_BAD_RESPONSE;
  273. goto bail;
  274. }
  275. LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> Successfully read the response\n",0,0,0);
  276. bail:
  277. if (host) {
  278. PR_Free(host);
  279. }
  280. if (path) {
  281. PR_Free(path);
  282. }
  283. if (fd) {
  284. PR_Close(fd);
  285. fd = NULL;
  286. }
  287. LDAPDebug( LDAP_DEBUG_PLUGIN, "<-- doRequest -- END\n",0,0,0);
  288. return status;
  289. }
  290. static PRStatus processResponse(PRFileDesc *fd, char **resBUF, int *bytesRead, int reqType)
  291. {
  292. PRStatus status = PR_SUCCESS;
  293. char *location = NULL;
  294. char *protocol = NULL;
  295. char *statusNum = NULL;
  296. char *statusString = NULL;
  297. char *headers = NULL;
  298. char tmp[HTTP_DEFAULT_BUFFER_SIZE];
  299. int pos=0;
  300. char ch;
  301. int index;
  302. int retcode;
  303. PRBool doneParsing = PR_FALSE;
  304. PRBool isRedirect = PR_FALSE;
  305. char name[HTTP_DEFAULT_BUFFER_SIZE];
  306. char value[HTTP_DEFAULT_BUFFER_SIZE];
  307. PRBool atEOL = PR_FALSE;
  308. PRBool inName = PR_TRUE;
  309. /* PKBxxx: If we are getting a redirect and the response is more the
  310. * the HTTP_DEFAULT_BUFFER_SIZE, it will cause the server to crash. A 4k
  311. * buffer should be good enough.
  312. */
  313. LDAPDebug( LDAP_DEBUG_PLUGIN, "--> processResponse -- BEGIN\n",0,0,0);
  314. headers = (char *)PR_Calloc(1, 4 * HTTP_DEFAULT_BUFFER_SIZE);
  315. /* Get protocol string */
  316. index = 0;
  317. while (1) {
  318. status = getChar(fd, headers+pos);
  319. if (status == PR_FAILURE) {
  320. /* Error : */
  321. goto bail;
  322. }
  323. ch = (char)headers[pos];
  324. pos++;
  325. if (!isWhiteSpace(ch)) {
  326. tmp[index++] = ch;
  327. } else {
  328. break;
  329. }
  330. }
  331. tmp[index] = '\0';
  332. protocol = PL_strdup(tmp);
  333. LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> protocol=[%s] \n",protocol,0,0);
  334. /* Get status num */
  335. index = 0;
  336. while (1) {
  337. status = getChar(fd, headers+pos);
  338. if (status == PR_FAILURE) {
  339. /* Error : */
  340. goto bail;
  341. }
  342. ch = (char)headers[pos];
  343. pos++;
  344. if (!isWhiteSpace(ch)) {
  345. tmp[index++] = ch;
  346. } else {
  347. break;
  348. }
  349. }
  350. tmp[index] = '\0';
  351. statusNum = PL_strdup(tmp);
  352. retcode=atoi(tmp);
  353. LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> statusNum=[%s] \n",statusNum,0,0);
  354. if (HTTP_RESPONSE_REDIRECT && (reqType == HTTP_REQ_TYPE_REDIRECT)) {
  355. isRedirect = PR_TRUE;
  356. }
  357. /* Get status string */
  358. if (ch != '\r')
  359. {
  360. index = 0;
  361. while (ch != '\r') {
  362. status = getChar(fd, headers+pos);
  363. if (status == PR_FAILURE) {
  364. /* Error : */
  365. goto bail;
  366. }
  367. ch = (char)headers[pos];
  368. pos++;
  369. tmp[index++] = ch;
  370. }
  371. tmp[index] = '\0';
  372. statusString = PL_strdup(tmp);
  373. LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> statusString [%s] \n",statusString,0,0);
  374. }
  375. /**
  376. * Skip CRLF
  377. */
  378. status = getChar(fd, headers+pos);
  379. if (status == PR_FAILURE) {
  380. /* Error : */
  381. goto bail;
  382. }
  383. ch = (char)headers[pos];
  384. pos++;
  385. /**
  386. * loop over response headers
  387. */
  388. index = 0;
  389. while (!doneParsing) {
  390. status = getChar(fd, headers+pos);
  391. if (status == PR_FAILURE) {
  392. /* Error : */
  393. goto bail;
  394. }
  395. ch = (char)headers[pos];
  396. pos++;
  397. switch(ch)
  398. {
  399. case ':':
  400. if (inName) {
  401. name[index] = '\0';
  402. index = 0;
  403. inName = PR_FALSE;
  404. /* skip whitespace */
  405. ch = ' ';
  406. /* status = getChar(fd, headers+pos);
  407. if (status == PR_FAILURE) {
  408. goto bail;
  409. }
  410. ch = (char)headers[pos];
  411. pos++; */
  412. while(isWhiteSpace(ch)) {
  413. status = getChar(fd, headers+pos);
  414. if (status == PR_FAILURE) {
  415. /* Error : */
  416. goto bail;
  417. }
  418. ch = (char)headers[pos];
  419. pos++;
  420. }
  421. value[index++] = ch;
  422. } else {
  423. value[index++] = ch;
  424. }
  425. break;
  426. case '\r':
  427. if (inName && !atEOL) {
  428. return PR_FALSE;
  429. }
  430. break;
  431. case '\n':
  432. if (atEOL) {
  433. doneParsing = PR_TRUE;
  434. break;
  435. }
  436. if (inName) {
  437. return PR_FALSE;
  438. }
  439. value[index] = '\0';
  440. index = 0;
  441. inName = PR_TRUE;
  442. LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> name=[%s] value=[%s]\n",name,value,0);
  443. if (isRedirect && !PL_strcasecmp(name,"location")) {
  444. location = PL_strdup(value);
  445. }
  446. atEOL = PR_TRUE;
  447. break;
  448. default:
  449. atEOL = PR_FALSE;
  450. if (inName) {
  451. name[index++] = ch;
  452. } else {
  453. value[index++] = ch;
  454. }
  455. break;
  456. }
  457. }
  458. if (!isRedirect) {
  459. getBody(fd, resBUF, bytesRead);
  460. } else {
  461. *resBUF = PL_strdup(location);
  462. *bytesRead = strlen(location);
  463. }
  464. LDAPDebug( LDAP_DEBUG_PLUGIN, "----------> Response Buffer=[%s] bytesRead=[%d] \n",*resBUF,*bytesRead,0);
  465. bail:
  466. if (headers) {
  467. PR_Free(headers);
  468. }
  469. if (protocol) {
  470. PL_strfree(protocol);
  471. }
  472. if (statusNum) {
  473. PL_strfree(statusNum);
  474. }
  475. if (statusString) {
  476. PL_strfree(statusString);
  477. }
  478. if (location) {
  479. PL_strfree(location);
  480. }
  481. LDAPDebug( LDAP_DEBUG_PLUGIN, "<-- processResponse -- END\n",0,0,0);
  482. return status;
  483. }
  484. static PRStatus sendGetReq(PRFileDesc *fd, const char *path)
  485. {
  486. PRStatus status = PR_SUCCESS;
  487. char *reqBUF = NULL;
  488. PRInt32 http_connection_time_out = 0;
  489. int buflen = (HTTP_GET_STD_LEN + strlen(path));
  490. reqBUF = (char *)PR_Calloc(1, buflen);
  491. if (!reqBUF) {
  492. status = PR_FAILURE;
  493. goto out;
  494. }
  495. strcpy(reqBUF, HTTP_GET);
  496. strcat(reqBUF, " ");
  497. strcat(reqBUF, path);
  498. strcat(reqBUF, " ");
  499. strcat(reqBUF, HTTP_PROTOCOL);
  500. strcat(reqBUF, "\r\n\r\n\0");
  501. http_connection_time_out = httpConfig->connectionTimeOut;
  502. status = sendFullData( fd, reqBUF, http_connection_time_out);
  503. out:
  504. if (reqBUF) {
  505. PR_Free(reqBUF);
  506. reqBUF = 0;
  507. }
  508. return status;
  509. }
  510. static PRStatus sendFullData( PRFileDesc *fd, char *buf, int timeOut)
  511. {
  512. int dataSent = 0;
  513. int bufLen = strlen(buf);
  514. int retVal = 0;
  515. PRInt32 errcode = 0;
  516. while (dataSent < bufLen)
  517. {
  518. retVal = PR_Send(fd, buf+dataSent, bufLen-dataSent, 0, PR_MillisecondsToInterval(timeOut));
  519. if (retVal == -1 )
  520. break;
  521. dataSent += retVal;
  522. }
  523. if (dataSent == bufLen )
  524. return PR_SUCCESS;
  525. else
  526. {
  527. errcode = PR_GetError();
  528. slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
  529. "sendFullData: dataSent=%d bufLen=%d -> NSPR Error code (%d)\n",
  530. dataSent, bufLen, errcode);
  531. LDAPDebug( LDAP_DEBUG_PLUGIN, "---------->NSPR Error code (%d) \n", errcode,0,0);
  532. return PR_FAILURE;
  533. }
  534. }
  535. static PRStatus sendPostReq(PRFileDesc *fd, const char *path, httpheader **httpheaderArray, char *body)
  536. {
  537. PRStatus status = PR_SUCCESS;
  538. char body_len_str[20];
  539. char *reqBUF = NULL;
  540. PRInt32 http_connection_time_out = 0;
  541. int i = 0;
  542. int body_len, buflen = 0;
  543. if (body) {
  544. body_len = strlen(body);
  545. } else {
  546. body_len = 0;
  547. }
  548. PR_snprintf(body_len_str, sizeof(body_len_str), "%d", body_len);
  549. buflen = (HTTP_POST_STD_LEN + strlen(path) + body_len + strlen(body_len_str));
  550. for (i = 0; httpheaderArray[i] != NULL; i++) {
  551. if (httpheaderArray[i]->name != NULL)
  552. {
  553. buflen += strlen(httpheaderArray[i]->name) + 2;
  554. if (httpheaderArray[i]->value != NULL)
  555. buflen += strlen(httpheaderArray[i]->value) + 2;
  556. }
  557. }
  558. reqBUF = (char *)PR_Calloc(1, buflen);
  559. if (!reqBUF) {
  560. status = PR_FAILURE;
  561. goto out;
  562. }
  563. strcpy(reqBUF, HTTP_POST);
  564. strcat(reqBUF, " ");
  565. strcat(reqBUF, path);
  566. strcat(reqBUF, " ");
  567. strcat(reqBUF, HTTP_PROTOCOL);
  568. strcat(reqBUF, "\r\n");
  569. strcat(reqBUF, HTTP_CONTENT_LENGTH);
  570. strcat(reqBUF, " ");
  571. strcat(reqBUF, body_len_str);
  572. strcat(reqBUF, "\r\n");
  573. strcat(reqBUF, HTTP_CONTENT_TYPE_URL_ENCODED);
  574. strcat(reqBUF, "\r\n");
  575. for (i = 0; httpheaderArray[i] != NULL; i++) {
  576. if (httpheaderArray[i]->name != NULL)
  577. strcat(reqBUF, httpheaderArray[i]->name);
  578. strcat(reqBUF, ": ");
  579. if (httpheaderArray[i]->value != NULL)
  580. strcat(reqBUF, httpheaderArray[i]->value);
  581. strcat(reqBUF, "\r\n");
  582. }
  583. strcat(reqBUF, "\r\n");
  584. if (body) {
  585. strcat(reqBUF, body);
  586. }
  587. strcat(reqBUF, "\0");
  588. LDAPDebug( LDAP_DEBUG_PLUGIN, "---------->reqBUF is %s \n",reqBUF,0,0);
  589. http_connection_time_out = httpConfig->connectionTimeOut;
  590. status = sendFullData( fd, reqBUF, http_connection_time_out);
  591. out:
  592. if (reqBUF) {
  593. PR_Free(reqBUF);
  594. reqBUF = 0;
  595. }
  596. return status;
  597. }
  598. static PRStatus getChar(PRFileDesc *fd, char *buf)
  599. {
  600. PRInt32 bytesRead = http_read(fd, buf, 1);
  601. if (bytesRead <=0) {
  602. PRInt32 errcode = PR_GetError();
  603. slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
  604. "getChar: NSPR Error code (%d)\n", errcode);
  605. return PR_FAILURE;
  606. }
  607. return PR_SUCCESS;
  608. }
  609. static PRStatus getBody(PRFileDesc *fd, char **buf, int *actualBytesRead)
  610. {
  611. int totalBytesRead = 0;
  612. int size = 4 * HTTP_DEFAULT_BUFFER_SIZE;
  613. int bytesRead = size;
  614. char *data = (char *) PR_Calloc(1, size);
  615. while (bytesRead == size) {
  616. bytesRead = http_read(fd, (data+totalBytesRead), size);
  617. if (bytesRead <= 0) {
  618. /* Read error */
  619. return PR_FAILURE;
  620. }
  621. if (bytesRead == size) {
  622. /* more data to be read so increase the buffer */
  623. size = size * 2 ;
  624. data = (char *) PR_Realloc(data, size);
  625. }
  626. totalBytesRead += bytesRead;
  627. }
  628. *buf = data;
  629. *actualBytesRead = totalBytesRead;
  630. return PR_SUCCESS;
  631. }
  632. static PRInt32 http_read(PRFileDesc *fd, char *buf, int size)
  633. {
  634. PRInt32 http_read_time_out = 0;
  635. http_read_time_out = httpConfig->readTimeOut;
  636. return PR_Recv(fd, buf, size, 0, PR_MillisecondsToInterval(http_read_time_out));
  637. }
  638. static PRBool isWhiteSpace(char ch)
  639. {
  640. PRBool b = PR_FALSE;
  641. if (ch == ' ') {
  642. b = PR_TRUE;
  643. }
  644. return b;
  645. }
  646. static PRStatus parseURI(const char *urlstr, char **host, PRInt32 *port, char **path, int *sslOn)
  647. {
  648. PRStatus status = PR_SUCCESS;
  649. char *brk;
  650. int len;
  651. static const char delimiters[] = ":/?#";
  652. char *url = isHttpReq(urlstr, sslOn);
  653. if (*sslOn) {
  654. *port = 443;
  655. }
  656. else {
  657. *port = 80;
  658. }
  659. if (url == NULL) {
  660. /* Error : */
  661. status = PR_FAILURE;
  662. goto bail;
  663. }
  664. len = PL_strlen(url);
  665. /* Currently we do not support Ipv6 addresses */
  666. brk = PL_strpbrk(url, delimiters);
  667. if (!brk) {
  668. *host = PL_strndup(url, len);
  669. toLowerCase(*host);
  670. goto bail;
  671. }
  672. switch (*brk)
  673. {
  674. case '/' :
  675. case '?' :
  676. case '#' :
  677. /* Get the Host, the rest is Path */
  678. *host = PL_strndup(url, (brk - url));
  679. toLowerCase(*host);
  680. status = parseAtPath(brk, path);
  681. break;
  682. case ':' :
  683. /* Get the Host and process port, path */
  684. *host = PL_strndup(url, (brk - url));
  685. toLowerCase(*host);
  686. status = parseAtPort(brk+1, port, path);
  687. break;
  688. default:
  689. /* Error : HTTP_BAD_URL */
  690. break;
  691. }
  692. bail:
  693. if (url) {
  694. PR_Free(url);
  695. }
  696. return status;
  697. }
  698. static PRStatus parseAtPort(const char* url, PRInt32 *port, char **path)
  699. {
  700. PRStatus status = PR_SUCCESS;
  701. static const char delimiters[] = "/?#";
  702. char* brk = PL_strpbrk(url, delimiters);
  703. if (!brk) /* everything is a Port */
  704. {
  705. *port = getPort(url);
  706. if (*port <= 0) {
  707. /* Error : HTTP_BAD_URL */
  708. return PR_FAILURE;
  709. } else {
  710. return status;
  711. }
  712. }
  713. switch (*brk)
  714. {
  715. case '/' :
  716. case '?' :
  717. case '#' :
  718. /* Get the Port, the rest is Path */
  719. *port = getPort(url);
  720. if (*port <= 0) {
  721. /* Error : HTTP_BAD_URL */
  722. return PR_FAILURE;
  723. }
  724. status = parseAtPath(brk, path);
  725. break;
  726. default:
  727. /* Error : HTTP_BAD_URL */
  728. break;
  729. }
  730. return status;
  731. }
  732. static PRStatus parseAtPath(const char *url, char **path)
  733. {
  734. PRStatus status = PR_SUCCESS;
  735. char *dir = "%s%s";
  736. *path = (char *)PR_Calloc(1, strlen(dir) + strlen(url) + 2);
  737. if (!*path) {
  738. /* Error : HTTP_BAD_URL */
  739. status = PR_FAILURE;
  740. goto out;
  741. }
  742. /* Just write the path and check for a starting / */
  743. if ('/' != *url) {
  744. sprintf(*path, dir, "/", url);
  745. } else {
  746. strcpy(*path, url);
  747. }
  748. out:
  749. return status;
  750. }
  751. static void toLowerCase(char* str)
  752. {
  753. if (str) {
  754. char* lstr = str;
  755. PRInt8 shift = 'a' - 'A';
  756. for(; (*lstr != '\0'); ++lstr) {
  757. if ((*(lstr) <= 'Z') && (*(lstr) >= 'A')) {
  758. *(lstr) = *(lstr) + shift;
  759. }
  760. }
  761. }
  762. }
  763. static PRInt32 getPort(const char* src)
  764. {
  765. /* search for digits up to a slash or the string ends */
  766. const char* port = src;
  767. PRInt32 returnValue = -1;
  768. char c;
  769. /* skip leading white space */
  770. while (isAsciiSpace(*port))
  771. port++;
  772. while ((c = *port++) != '\0') {
  773. /* stop if slash or ? or # reached */
  774. if (c == '/' || c == '?' || c == '#')
  775. break;
  776. else if (!isAsciiDigit(c))
  777. return returnValue;
  778. }
  779. return (0 < PR_sscanf(src, "%d", &returnValue)) ? returnValue : -1;
  780. }
  781. static PRBool isAsciiSpace(char aChar)
  782. {
  783. if ((aChar == ' ') || (aChar == '\r') || (aChar == '\n') || (aChar == '\t')) {
  784. return PR_TRUE;
  785. }
  786. return PR_FALSE;
  787. }
  788. static PRBool isAsciiDigit(char aChar)
  789. {
  790. if ((aChar >= '0') && (aChar <= '9')) {
  791. return PR_TRUE;
  792. }
  793. return PR_FALSE;
  794. }
  795. static void setTCPNoDelay(PRFileDesc* fd)
  796. {
  797. PRStatus status = PR_SUCCESS;
  798. PRSocketOptionData opt;
  799. opt.option = PR_SockOpt_NoDelay;
  800. opt.value.no_delay = PR_FALSE;
  801. status = PR_GetSocketOption(fd, &opt);
  802. if (status == PR_FAILURE) {
  803. return;
  804. }
  805. opt.option = PR_SockOpt_NoDelay;
  806. opt.value.no_delay = PR_TRUE;
  807. status = PR_SetSocketOption(fd, &opt);
  808. if (status == PR_FAILURE) {
  809. return;
  810. }
  811. return;
  812. }
  813. static char * isHttpReq(const char *url, int *sslOn)
  814. {
  815. static const char http_protopol_header[] = "http://";
  816. static const char https_protopol_header[] = "https://";
  817. char *newstr = NULL;
  818. /* skip leading white space */
  819. while (isAsciiSpace(*url)) {
  820. url++;
  821. }
  822. if (strncmp(url, http_protopol_header, strlen(http_protopol_header)) == 0) {
  823. newstr = (char *)PR_Calloc(1, (strlen(url)-strlen(http_protopol_header) + 1));
  824. strcpy(newstr, url+7);
  825. strcat(newstr,"\0");
  826. *sslOn = 0;
  827. }
  828. else if (strncmp(url, https_protopol_header, strlen(https_protopol_header)) == 0) {
  829. newstr = (char *)PR_Calloc(1, (strlen(url)-strlen(https_protopol_header) + 1));
  830. strcpy(newstr, url+8);
  831. strcat(newstr,"\0");
  832. *sslOn = 1;
  833. }
  834. return newstr;
  835. }
  836. PRFileDesc* setupSSLSocket(PRFileDesc* fd)
  837. {
  838. SECStatus secStatus;
  839. PRFileDesc* sslSocket;
  840. PRSocketOptionData socketOption;
  841. char *certNickname = NULL;
  842. socketOption.option = PR_SockOpt_Nonblocking;
  843. socketOption.value.non_blocking = PR_FALSE;
  844. if( PR_SetSocketOption(fd, &socketOption) != 0) {
  845. slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
  846. "Cannot set socket option NSS \n");
  847. return NULL;
  848. }
  849. sslSocket = SSL_ImportFD(NULL, fd);
  850. if (!sslSocket) {
  851. slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
  852. "setupSSLSocket: Cannot import to SSL Socket\n" );
  853. goto sslbail;
  854. }
  855. slapi_log_error( SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
  856. "setupSSLSocket: setupssl socket created\n" );
  857. secStatus = SSL_OptionSet(sslSocket, SSL_SECURITY, 1);
  858. if (SECSuccess != secStatus) {
  859. slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
  860. "setupSSLSocket: Cannot set SSL_SECURITY option\n");
  861. goto sslbail;
  862. }
  863. secStatus = SSL_OptionSet(sslSocket, SSL_HANDSHAKE_AS_CLIENT, 1);
  864. if (SECSuccess != secStatus) {
  865. slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
  866. "setupSSLSocket: CAnnot set SSL_HANDSHAKE_AS_CLIENT option\n");
  867. goto sslbail;
  868. }
  869. /* Set SSL callback routines. */
  870. secStatus = SSL_GetClientAuthDataHook(sslSocket,
  871. (SSLGetClientAuthData) getClientAuthData,
  872. (void *)certNickname);
  873. if (secStatus != SECSuccess) {
  874. slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
  875. "setupSSLSocket: SSL_GetClientAuthDataHook Failed\n");
  876. goto sslbail;
  877. }
  878. secStatus = SSL_AuthCertificateHook(sslSocket,
  879. (SSLAuthCertificate) authCertificate,
  880. (void *)CERT_GetDefaultCertDB());
  881. if (secStatus != SECSuccess) {
  882. slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
  883. "setupSSLSocket: SSL_AuthCertificateHook Failed\n");
  884. goto sslbail;
  885. }
  886. secStatus = SSL_BadCertHook(sslSocket,
  887. (SSLBadCertHandler) badCertHandler, NULL);
  888. if (secStatus != SECSuccess) {
  889. slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
  890. "setupSSLSocket: SSL_BadCertHook Failed\n");
  891. goto sslbail;
  892. }
  893. secStatus = SSL_HandshakeCallback(sslSocket,
  894. (SSLHandshakeCallback) handshakeCallback, NULL);
  895. if (secStatus != SECSuccess) {
  896. slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
  897. "setupSSLSocket: SSL_HandshakeCallback Failed\n");
  898. goto sslbail;
  899. }
  900. return sslSocket;
  901. sslbail:
  902. PR_Close(fd);
  903. return NULL;
  904. }
  905. SECStatus
  906. authCertificate(void *arg, PRFileDesc *socket,
  907. PRBool checksig, PRBool isServer)
  908. {
  909. SECCertUsage certUsage;
  910. CERTCertificate * cert;
  911. void * pinArg;
  912. char * hostName;
  913. SECStatus secStatus;
  914. if (!arg || !socket) {
  915. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
  916. " authCertificate: Faulty socket in callback function \n");
  917. return SECFailure;
  918. }
  919. /* Define how the cert is being used based upon the isServer flag. */
  920. certUsage = isServer ? certUsageSSLClient : certUsageSSLServer;
  921. cert = SSL_PeerCertificate(socket);
  922. pinArg = SSL_RevealPinArg(socket);
  923. secStatus = CERT_VerifyCertNow((CERTCertDBHandle *)arg,
  924. cert,
  925. checksig,
  926. certUsage,
  927. pinArg);
  928. /* If this is a server, we're finished. */
  929. if (isServer || secStatus != SECSuccess) {
  930. return secStatus;
  931. }
  932. hostName = SSL_RevealURL(socket);
  933. if (hostName && hostName[0]) {
  934. secStatus = CERT_VerifyCertName(cert, hostName);
  935. } else {
  936. PR_SetError(SSL_ERROR_BAD_CERT_DOMAIN, 0);
  937. secStatus = SECFailure;
  938. }
  939. if (hostName)
  940. PR_Free(hostName);
  941. return secStatus;
  942. }
  943. SECStatus
  944. badCertHandler(void *arg, PRFileDesc *socket)
  945. {
  946. SECStatus secStatus = SECFailure;
  947. PRErrorCode err;
  948. /* log invalid cert here */
  949. if (!arg) {
  950. return secStatus;
  951. }
  952. *(PRErrorCode *)arg = err = PORT_GetError();
  953. switch (err) {
  954. case SEC_ERROR_INVALID_AVA:
  955. case SEC_ERROR_INVALID_TIME:
  956. case SEC_ERROR_BAD_SIGNATURE:
  957. case SEC_ERROR_EXPIRED_CERTIFICATE:
  958. case SEC_ERROR_UNKNOWN_ISSUER:
  959. case SEC_ERROR_UNTRUSTED_CERT:
  960. case SEC_ERROR_CERT_VALID:
  961. case SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE:
  962. case SEC_ERROR_CRL_EXPIRED:
  963. case SEC_ERROR_CRL_BAD_SIGNATURE:
  964. case SEC_ERROR_EXTENSION_VALUE_INVALID:
  965. case SEC_ERROR_CA_CERT_INVALID:
  966. case SEC_ERROR_CERT_USAGES_INVALID:
  967. case SEC_ERROR_UNKNOWN_CRITICAL_EXTENSION:
  968. secStatus = SECSuccess;
  969. break;
  970. default:
  971. secStatus = SECFailure;
  972. break;
  973. }
  974. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
  975. "Bad certificate: %d\n", err);
  976. return secStatus;
  977. }
  978. SECStatus
  979. getClientAuthData(void *arg,
  980. PRFileDesc *socket,
  981. struct CERTDistNamesStr *caNames,
  982. struct CERTCertificateStr **pRetCert,
  983. struct SECKEYPrivateKeyStr **pRetKey)
  984. {
  985. CERTCertificate * cert;
  986. SECKEYPrivateKey * privKey = NULL;
  987. char * chosenNickName = (char *)arg;
  988. void * proto_win = NULL;
  989. SECStatus secStatus = SECFailure;
  990. proto_win = SSL_RevealPinArg(socket);
  991. if (chosenNickName) {
  992. cert = PK11_FindCertFromNickname(chosenNickName, proto_win);
  993. if (cert) {
  994. privKey = PK11_FindKeyByAnyCert(cert, proto_win);
  995. if (privKey) {
  996. secStatus = SECSuccess;
  997. } else {
  998. CERT_DestroyCertificate(cert);
  999. }
  1000. }
  1001. } else { /* no nickname given, automatically find the right cert */
  1002. CERTCertNicknames *names;
  1003. int i;
  1004. names = CERT_GetCertNicknames(CERT_GetDefaultCertDB(),
  1005. SEC_CERT_NICKNAMES_USER, proto_win);
  1006. if (names != NULL) {
  1007. for(i = 0; i < names->numnicknames; i++ ) {
  1008. cert = PK11_FindCertFromNickname(names->nicknames[i],
  1009. proto_win);
  1010. if (!cert) {
  1011. continue;
  1012. }
  1013. /* Only check unexpired certs */
  1014. if (CERT_CheckCertValidTimes(cert, PR_Now(), PR_FALSE)
  1015. != secCertTimeValid ) {
  1016. CERT_DestroyCertificate(cert);
  1017. continue;
  1018. }
  1019. secStatus = NSS_CmpCertChainWCANames(cert, caNames);
  1020. if (secStatus == SECSuccess) {
  1021. privKey = PK11_FindKeyByAnyCert(cert, proto_win);
  1022. if (privKey) {
  1023. break;
  1024. }
  1025. secStatus = SECFailure;
  1026. break;
  1027. }
  1028. CERT_FreeNicknames(names);
  1029. } /* for loop */
  1030. }
  1031. }
  1032. if (secStatus == SECSuccess) {
  1033. *pRetCert = cert;
  1034. *pRetKey = privKey;
  1035. }
  1036. return secStatus;
  1037. }
  1038. SECStatus
  1039. handshakeCallback(PRFileDesc *socket, void *arg)
  1040. {
  1041. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
  1042. "----------> Handshake has completed, ready to send data securely.\n");
  1043. return SECSuccess;
  1044. }
  1045. /**
  1046. * PUBLIC FUNCTIONS IMPLEMENTATION
  1047. */
  1048. int http_impl_init(Slapi_ComponentId *plugin_id)
  1049. {
  1050. int status = HTTP_IMPL_SUCCESS;
  1051. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
  1052. "-> http_impl_init \n");
  1053. httpConfig = NULL;
  1054. httpConfig = (httpPluginConfig *) slapi_ch_calloc(1, sizeof(httpPluginConfig));
  1055. status = readConfigLDAPurl(plugin_id, HTTP_PLUGIN_DN);
  1056. if (status != 0) {
  1057. slapi_log_error(SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
  1058. "http_impl_start: Unable to get HTTP config information \n");
  1059. return HTTP_IMPL_FAILURE;
  1060. }
  1061. status = readConfigLDAPurl(plugin_id, CONFIG_DN);
  1062. if (status != 0) {
  1063. slapi_log_error(SLAPI_LOG_FATAL, HTTP_PLUGIN_SUBSYSTEM,
  1064. "http_impl_start: Unable to get config information \n");
  1065. return HTTP_IMPL_FAILURE;
  1066. }
  1067. slapi_log_error(SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
  1068. "<- http_impl_init \n");
  1069. return status;
  1070. }
  1071. int http_impl_get_text(char *url, char **data, int *bytesRead)
  1072. {
  1073. int status = HTTP_IMPL_SUCCESS;
  1074. status = doRequestRetry(url, NULL, NULL, data, bytesRead, HTTP_REQ_TYPE_GET);
  1075. return status;
  1076. }
  1077. int http_impl_get_binary(char *url, char **data, int *bytesRead)
  1078. {
  1079. int status = HTTP_IMPL_SUCCESS;
  1080. status = doRequestRetry(url, NULL, NULL, data, bytesRead, HTTP_REQ_TYPE_GET);
  1081. return status;
  1082. }
  1083. int http_impl_get_redirected_uri(char *url, char **data, int *bytesRead)
  1084. {
  1085. int status = HTTP_IMPL_SUCCESS;
  1086. status = doRequestRetry(url, NULL, NULL, data, bytesRead, HTTP_REQ_TYPE_REDIRECT);
  1087. return status;
  1088. }
  1089. int http_impl_post(char *url, httpheader **httpheaderArray, char *body, char **data, int *bytesRead)
  1090. {
  1091. int status = HTTP_IMPL_SUCCESS;
  1092. status = doRequestRetry(url, httpheaderArray, body, data, bytesRead, HTTP_REQ_TYPE_POST);
  1093. return status;
  1094. }
  1095. void http_impl_shutdown(void)
  1096. {
  1097. /**
  1098. * Put cleanup code here
  1099. */
  1100. }
  1101. static int readConfigLDAPurl(Slapi_ComponentId *plugin_id, char *plugindn) {
  1102. int rc = LDAP_SUCCESS;
  1103. Slapi_DN *sdn = NULL;
  1104. int status = HTTP_IMPL_SUCCESS;
  1105. Slapi_Entry *entry = NULL;
  1106. sdn = slapi_sdn_new_dn_byref(plugindn);
  1107. rc = slapi_search_internal_get_entry(sdn, NULL, &entry, plugin_id);
  1108. slapi_sdn_free(&sdn);
  1109. if (rc != LDAP_SUCCESS) {
  1110. slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
  1111. "readConfigLDAPurl: Could not find entry %s (error %d)\n", plugindn, rc);
  1112. status = HTTP_IMPL_FAILURE;
  1113. return status;
  1114. }
  1115. if (NULL == entry)
  1116. {
  1117. slapi_log_error( SLAPI_LOG_PLUGIN, HTTP_PLUGIN_SUBSYSTEM,
  1118. "readConfigLDAPurl: No entries found for <%s>\n", plugindn);
  1119. status = HTTP_IMPL_FAILURE;
  1120. return status;
  1121. }
  1122. if ((PL_strcasecmp(plugindn, HTTP_PLUGIN_DN) == 0))
  1123. status = parseHTTPConfigEntry(entry);
  1124. else
  1125. status = parseConfigEntry(entry);
  1126. slapi_entry_free(entry);
  1127. return status;
  1128. }
  1129. /* Retrieves the plugin configuration info */
  1130. /* Retrieves security info as well as the path info required for the SSL
  1131. config dir */
  1132. static int parseConfigEntry(Slapi_Entry *e)
  1133. {
  1134. char *value = NULL;
  1135. value = slapi_entry_attr_get_charptr(e, ATTR_DS_SECURITY);
  1136. if (value) {
  1137. httpConfig->DS_sslOn = value;
  1138. }
  1139. return HTTP_IMPL_SUCCESS;
  1140. }
  1141. static int parseHTTPConfigEntry(Slapi_Entry *e)
  1142. {
  1143. int value = 0;
  1144. value = slapi_entry_attr_get_int(e, ATTR_RETRY_COUNT);
  1145. if (value) {
  1146. httpConfig->retryCount = value;
  1147. }
  1148. value = slapi_entry_attr_get_int(e, ATTR_CONNECTION_TIME_OUT);
  1149. if (value) {
  1150. httpConfig->connectionTimeOut = value;
  1151. }
  1152. else {
  1153. LDAPDebug( LDAP_DEBUG_PLUGIN, "parseHTTPConfigEntry: HTTP Connection Time Out cannot be read. Setting to default value of 5000 ms \n", 0,0,0);
  1154. httpConfig->connectionTimeOut = 5000;
  1155. }
  1156. value = slapi_entry_attr_get_int(e, ATTR_READ_TIME_OUT);
  1157. if (value) {
  1158. httpConfig->readTimeOut = value;
  1159. }
  1160. else {
  1161. LDAPDebug( LDAP_DEBUG_PLUGIN, "parseHTTPConfigEntry: HTTP Read Time Out cannot be read. Setting to default value of 5000 ms \n", 0,0,0);
  1162. httpConfig->readTimeOut = 5000;
  1163. }
  1164. httpConfig->nssInitialized = 0;
  1165. return HTTP_IMPL_SUCCESS;
  1166. }
  1167. /**
  1168. * Self Testing
  1169. */
  1170. #ifdef BUILD_STANDALONE
  1171. int main(int argc, char **argv)
  1172. {
  1173. PRStatus status = PR_SUCCESS;
  1174. char *buf;
  1175. int bytes;
  1176. char *host;
  1177. PRInt32 port;
  1178. char *path;
  1179. if (argc < 2) {
  1180. printf("URL missing\n");
  1181. return -1;
  1182. }
  1183. PR_Init(PR_USER_THREAD,PR_PRIORITY_NORMAL, 0);
  1184. doRequest(argv[1], &buf, &bytes, 2);
  1185. printf( "%s\n", buf );
  1186. return -1;
  1187. }
  1188. #endif