诸暨麻将添加redis
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

1495 wiersze
32 KiB

  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. *
  22. ***************************************************************************/
  23. /* OS/400 additional support. */
  24. #include "curlbuild.h"
  25. #include "config-os400.h" /* Not curl_setup.h: we only need some defines. */
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <sys/un.h>
  29. #include <stdlib.h>
  30. #include <stddef.h>
  31. #include <string.h>
  32. #include <pthread.h>
  33. #include <netdb.h>
  34. #include <qadrt.h>
  35. #include <errno.h>
  36. #ifdef USE_QSOSSL
  37. #include <qsossl.h>
  38. #endif
  39. #ifdef USE_GSKIT
  40. #include <gskssl.h>
  41. #include <qsoasync.h>
  42. #endif
  43. #ifdef HAVE_GSSAPI
  44. #include <gssapi.h>
  45. #endif
  46. #ifndef CURL_DISABLE_LDAP
  47. #include <ldap.h>
  48. #endif
  49. #include <netinet/in.h>
  50. #include <arpa/inet.h>
  51. #include "os400sys.h"
  52. /**
  53. *** QADRT OS/400 ASCII runtime defines only the most used procedures, but
  54. *** but a lot of them are not supported. This module implements
  55. *** ASCII wrappers for those that are used by libcurl, but not
  56. *** defined by QADRT.
  57. **/
  58. #pragma convert(0) /* Restore EBCDIC. */
  59. #define MIN_BYTE_GAIN 1024 /* Minimum gain when shortening a buffer. */
  60. typedef struct {
  61. unsigned long size; /* Buffer size. */
  62. char * buf; /* Buffer address. */
  63. } buffer_t;
  64. static char * buffer_undef(localkey_t key, long size);
  65. static char * buffer_threaded(localkey_t key, long size);
  66. static char * buffer_unthreaded(localkey_t key, long size);
  67. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  68. static pthread_key_t thdkey;
  69. static buffer_t * locbufs;
  70. char * (* Curl_thread_buffer)(localkey_t key, long size) = buffer_undef;
  71. static void
  72. thdbufdestroy(void * private)
  73. {
  74. localkey_t i;
  75. buffer_t * p;
  76. if (private) {
  77. p = (buffer_t *) private;
  78. for (i = (localkey_t) 0; i < LK_LAST; i++) {
  79. if (p->buf)
  80. free(p->buf);
  81. p++;
  82. }
  83. free(private);
  84. }
  85. }
  86. static void
  87. terminate(void)
  88. {
  89. if (Curl_thread_buffer == buffer_threaded) {
  90. locbufs = pthread_getspecific(thdkey);
  91. pthread_setspecific(thdkey, (void *) NULL);
  92. pthread_key_delete(thdkey);
  93. }
  94. if (Curl_thread_buffer != buffer_undef) {
  95. thdbufdestroy((void *) locbufs);
  96. locbufs = (buffer_t *) NULL;
  97. }
  98. Curl_thread_buffer = buffer_undef;
  99. }
  100. static char *
  101. get_buffer(buffer_t * buf, long size)
  102. {
  103. char * cp;
  104. /* If `size' >= 0, make sure buffer at `buf' is at least `size'-byte long.
  105. Return the buffer address. */
  106. if (size < 0)
  107. return buf->buf;
  108. if (!buf->buf) {
  109. if ((buf->buf = malloc(size)))
  110. buf->size = size;
  111. return buf->buf;
  112. }
  113. if ((unsigned long) size <= buf->size) {
  114. /* Shorten the buffer only if it frees a significant byte count. This
  115. avoids some realloc() overhead. */
  116. if (buf->size - size < MIN_BYTE_GAIN)
  117. return buf->buf;
  118. }
  119. /* Resize the buffer. */
  120. if ((cp = realloc(buf->buf, size))) {
  121. buf->buf = cp;
  122. buf->size = size;
  123. }
  124. else if (size <= buf->size)
  125. cp = buf->buf;
  126. return cp;
  127. }
  128. static char *
  129. buffer_unthreaded(localkey_t key, long size)
  130. {
  131. return get_buffer(locbufs + key, size);
  132. }
  133. static char *
  134. buffer_threaded(localkey_t key, long size)
  135. {
  136. buffer_t * bufs;
  137. /* Get the buffer for the given local key in the current thread, and
  138. make sure it is at least `size'-byte long. Set `size' to < 0 to get
  139. its address only. */
  140. bufs = (buffer_t *) pthread_getspecific(thdkey);
  141. if (!bufs) {
  142. if (size < 0)
  143. return (char *) NULL; /* No buffer yet. */
  144. /* Allocate buffer descriptors for the current thread. */
  145. if (!(bufs = calloc((size_t) LK_LAST, sizeof *bufs)))
  146. return (char *) NULL;
  147. if (pthread_setspecific(thdkey, (void *) bufs)) {
  148. free(bufs);
  149. return (char *) NULL;
  150. }
  151. }
  152. return get_buffer(bufs + key, size);
  153. }
  154. static char *
  155. buffer_undef(localkey_t key, long size)
  156. {
  157. /* Define the buffer system, get the buffer for the given local key in
  158. the current thread, and make sure it is at least `size'-byte long.
  159. Set `size' to < 0 to get its address only. */
  160. pthread_mutex_lock(&mutex);
  161. /* Determine if we can use pthread-specific data. */
  162. if (Curl_thread_buffer == buffer_undef) { /* If unchanged during lock. */
  163. if (!pthread_key_create(&thdkey, thdbufdestroy))
  164. Curl_thread_buffer = buffer_threaded;
  165. else if (!(locbufs = calloc((size_t) LK_LAST,
  166. sizeof *locbufs))) {
  167. pthread_mutex_unlock(&mutex);
  168. return (char *) NULL;
  169. }
  170. else
  171. Curl_thread_buffer = buffer_unthreaded;
  172. atexit(terminate);
  173. }
  174. pthread_mutex_unlock(&mutex);
  175. return Curl_thread_buffer(key, size);
  176. }
  177. int
  178. Curl_getnameinfo_a(const struct sockaddr * sa, curl_socklen_t salen,
  179. char * nodename, curl_socklen_t nodenamelen,
  180. char * servname, curl_socklen_t servnamelen,
  181. int flags)
  182. {
  183. char * enodename;
  184. char * eservname;
  185. int status;
  186. int i;
  187. enodename = (char *) NULL;
  188. eservname = (char *) NULL;
  189. if (nodename && nodenamelen)
  190. if (!(enodename = malloc(nodenamelen)))
  191. return EAI_MEMORY;
  192. if (servname && servnamelen)
  193. if (!(eservname = malloc(servnamelen))) {
  194. if (enodename)
  195. free(enodename);
  196. return EAI_MEMORY;
  197. }
  198. status = getnameinfo(sa, salen, enodename, nodenamelen,
  199. eservname, servnamelen, flags);
  200. if (!status) {
  201. if (enodename) {
  202. i = QadrtConvertE2A(nodename, enodename,
  203. nodenamelen - 1, strlen(enodename));
  204. nodename[i] = '\0';
  205. }
  206. if (eservname) {
  207. i = QadrtConvertE2A(servname, eservname,
  208. servnamelen - 1, strlen(eservname));
  209. servname[i] = '\0';
  210. }
  211. }
  212. if (enodename)
  213. free(enodename);
  214. if (eservname)
  215. free(eservname);
  216. return status;
  217. }
  218. int
  219. Curl_getaddrinfo_a(const char * nodename, const char * servname,
  220. const struct addrinfo * hints,
  221. struct addrinfo * * res)
  222. {
  223. char * enodename;
  224. char * eservname;
  225. int status;
  226. int i;
  227. enodename = (char *) NULL;
  228. eservname = (char *) NULL;
  229. if (nodename) {
  230. i = strlen(nodename);
  231. if (!(enodename = malloc(i + 1)))
  232. return EAI_MEMORY;
  233. i = QadrtConvertA2E(enodename, nodename, i, i);
  234. enodename[i] = '\0';
  235. }
  236. if (servname) {
  237. i = strlen(servname);
  238. if (!(eservname = malloc(i + 1))) {
  239. if (enodename)
  240. free(enodename);
  241. return EAI_MEMORY;
  242. }
  243. QadrtConvertA2E(eservname, servname, i, i);
  244. eservname[i] = '\0';
  245. }
  246. status = getaddrinfo(enodename, eservname, hints, res);
  247. if (enodename)
  248. free(enodename);
  249. if (eservname)
  250. free(eservname);
  251. return status;
  252. }
  253. #ifdef USE_QSOSSL
  254. /* ASCII wrappers for the SSL procedures. */
  255. int
  256. Curl_SSL_Init_Application_a(SSLInitApp * init_app)
  257. {
  258. int rc;
  259. unsigned int i;
  260. SSLInitApp ia;
  261. if (!init_app || !init_app->applicationID || !init_app->applicationIDLen)
  262. return SSL_Init_Application(init_app);
  263. memcpy((char *) &ia, (char *) init_app, sizeof ia);
  264. i = ia.applicationIDLen;
  265. if (!(ia.applicationID = malloc(i + 1))) {
  266. errno = ENOMEM;
  267. return SSL_ERROR_IO;
  268. }
  269. QadrtConvertA2E(ia.applicationID, init_app->applicationID, i, i);
  270. ia.applicationID[i] = '\0';
  271. rc = SSL_Init_Application(&ia);
  272. free(ia.applicationID);
  273. init_app->localCertificateLen = ia.localCertificateLen;
  274. init_app->sessionType = ia.sessionType;
  275. return rc;
  276. }
  277. int
  278. Curl_SSL_Init_a(SSLInit * init)
  279. {
  280. int rc;
  281. unsigned int i;
  282. SSLInit ia;
  283. if (!init || (!init->keyringFileName && !init->keyringPassword))
  284. return SSL_Init(init);
  285. memcpy((char *) &ia, (char *) init, sizeof ia);
  286. if (ia.keyringFileName) {
  287. i = strlen(ia.keyringFileName);
  288. if (!(ia.keyringFileName = malloc(i + 1))) {
  289. errno = ENOMEM;
  290. return SSL_ERROR_IO;
  291. }
  292. QadrtConvertA2E(ia.keyringFileName, init->keyringFileName, i, i);
  293. ia.keyringFileName[i] = '\0';
  294. }
  295. if (ia.keyringPassword) {
  296. i = strlen(ia.keyringPassword);
  297. if (!(ia.keyringPassword = malloc(i + 1))) {
  298. if (ia.keyringFileName)
  299. free(ia.keyringFileName);
  300. errno = ENOMEM;
  301. return SSL_ERROR_IO;
  302. }
  303. QadrtConvertA2E(ia.keyringPassword, init->keyringPassword, i, i);
  304. ia.keyringPassword[i] = '\0';
  305. }
  306. rc = SSL_Init(&ia);
  307. if (ia.keyringFileName)
  308. free(ia.keyringFileName);
  309. if (ia.keyringPassword)
  310. free(ia.keyringPassword);
  311. return rc;
  312. }
  313. char *
  314. Curl_SSL_Strerror_a(int sslreturnvalue, SSLErrorMsg * serrmsgp)
  315. {
  316. int i;
  317. char * cp;
  318. char * cp2;
  319. cp = SSL_Strerror(sslreturnvalue, serrmsgp);
  320. if (!cp)
  321. return cp;
  322. i = strlen(cp);
  323. if (!(cp2 = Curl_thread_buffer(LK_SSL_ERROR, MAX_CONV_EXPANSION * i + 1)))
  324. return cp2;
  325. i = QadrtConvertE2A(cp2, cp, MAX_CONV_EXPANSION * i, i);
  326. cp2[i] = '\0';
  327. return cp2;
  328. }
  329. #endif /* USE_QSOSSL */
  330. #ifdef USE_GSKIT
  331. /* ASCII wrappers for the GSKit procedures. */
  332. /*
  333. * EBCDIC --> ASCII string mapping table.
  334. * Some strings returned by GSKit are dynamically allocated and automatically
  335. * released when closing the handle.
  336. * To provide the same functionality, we use a "private" handle that
  337. * holds the GSKit handle and a list of string mappings. This will allow
  338. * avoid conversion of already converted strings and releasing them upon
  339. * close time.
  340. */
  341. struct gskstrlist {
  342. struct gskstrlist * next;
  343. const char * ebcdicstr;
  344. const char * asciistr;
  345. };
  346. struct Curl_gsk_descriptor {
  347. gsk_handle h;
  348. struct gskstrlist * strlist;
  349. };
  350. int
  351. Curl_gsk_environment_open(gsk_handle * my_env_handle)
  352. {
  353. struct Curl_gsk_descriptor * p;
  354. gsk_handle h;
  355. int rc;
  356. if(!my_env_handle)
  357. return GSK_OS400_ERROR_INVALID_POINTER;
  358. if(!(p = (struct Curl_gsk_descriptor *) malloc(sizeof *p)))
  359. return GSK_INSUFFICIENT_STORAGE;
  360. p->strlist = (struct gskstrlist *) NULL;
  361. if((rc = gsk_environment_open(&p->h)) != GSK_OK)
  362. free(p);
  363. else
  364. *my_env_handle = (gsk_handle) p;
  365. return rc;
  366. }
  367. int
  368. Curl_gsk_secure_soc_open(gsk_handle my_env_handle,
  369. gsk_handle * my_session_handle)
  370. {
  371. struct Curl_gsk_descriptor * p;
  372. gsk_handle h;
  373. int rc;
  374. if(!my_env_handle)
  375. return GSK_INVALID_HANDLE;
  376. if(!my_session_handle)
  377. return GSK_OS400_ERROR_INVALID_POINTER;
  378. h = ((struct Curl_gsk_descriptor *) my_env_handle)->h;
  379. if(!(p = (struct Curl_gsk_descriptor *) malloc(sizeof *p)))
  380. return GSK_INSUFFICIENT_STORAGE;
  381. p->strlist = (struct gskstrlist *) NULL;
  382. if((rc = gsk_secure_soc_open(h, &p->h)) != GSK_OK)
  383. free(p);
  384. else
  385. *my_session_handle = (gsk_handle) p;
  386. return rc;
  387. }
  388. static void
  389. gsk_free_handle(struct Curl_gsk_descriptor * p)
  390. {
  391. struct gskstrlist * q;
  392. while ((q = p->strlist)) {
  393. p->strlist = q;
  394. free((void *) q->asciistr);
  395. free(q);
  396. }
  397. free(p);
  398. }
  399. int
  400. Curl_gsk_environment_close(gsk_handle * my_env_handle)
  401. {
  402. struct Curl_gsk_descriptor * p;
  403. int rc;
  404. if(!my_env_handle)
  405. return GSK_OS400_ERROR_INVALID_POINTER;
  406. if(!*my_env_handle)
  407. return GSK_INVALID_HANDLE;
  408. p = (struct Curl_gsk_descriptor *) *my_env_handle;
  409. if ((rc = gsk_environment_close(&p->h)) == GSK_OK) {
  410. gsk_free_handle(p);
  411. *my_env_handle = (gsk_handle) NULL;
  412. }
  413. return rc;
  414. }
  415. int
  416. Curl_gsk_secure_soc_close(gsk_handle * my_session_handle)
  417. {
  418. struct Curl_gsk_descriptor * p;
  419. int rc;
  420. if(!my_session_handle)
  421. return GSK_OS400_ERROR_INVALID_POINTER;
  422. if(!*my_session_handle)
  423. return GSK_INVALID_HANDLE;
  424. p = (struct Curl_gsk_descriptor *) *my_session_handle;
  425. if ((rc = gsk_secure_soc_close(&p->h)) == GSK_OK) {
  426. gsk_free_handle(p);
  427. *my_session_handle = (gsk_handle) NULL;
  428. }
  429. return rc;
  430. }
  431. int
  432. Curl_gsk_environment_init(gsk_handle my_env_handle)
  433. {
  434. struct Curl_gsk_descriptor * p;
  435. if(!my_env_handle)
  436. return GSK_INVALID_HANDLE;
  437. p = (struct Curl_gsk_descriptor *) my_env_handle;
  438. return gsk_environment_init(p->h);
  439. }
  440. int
  441. Curl_gsk_secure_soc_init(gsk_handle my_session_handle)
  442. {
  443. struct Curl_gsk_descriptor * p;
  444. if(!my_session_handle)
  445. return GSK_INVALID_HANDLE;
  446. p = (struct Curl_gsk_descriptor *) my_session_handle;
  447. return gsk_secure_soc_init(p->h);
  448. }
  449. int
  450. Curl_gsk_attribute_set_buffer_a(gsk_handle my_gsk_handle, GSK_BUF_ID bufID,
  451. const char * buffer, int bufSize)
  452. {
  453. struct Curl_gsk_descriptor * p;
  454. char * ebcdicbuf;
  455. int rc;
  456. if(!my_gsk_handle)
  457. return GSK_INVALID_HANDLE;
  458. if(!buffer)
  459. return GSK_OS400_ERROR_INVALID_POINTER;
  460. if(bufSize < 0)
  461. return GSK_ATTRIBUTE_INVALID_LENGTH;
  462. p = (struct Curl_gsk_descriptor *) my_gsk_handle;
  463. if(!bufSize)
  464. bufSize = strlen(buffer);
  465. if (!(ebcdicbuf = malloc(bufSize + 1)))
  466. return GSK_INSUFFICIENT_STORAGE;
  467. QadrtConvertA2E(ebcdicbuf, buffer, bufSize, bufSize);
  468. ebcdicbuf[bufSize] = '\0';
  469. rc = gsk_attribute_set_buffer(p->h, bufID, ebcdicbuf, bufSize);
  470. free(ebcdicbuf);
  471. return rc;
  472. }
  473. int
  474. Curl_gsk_attribute_set_enum(gsk_handle my_gsk_handle, GSK_ENUM_ID enumID,
  475. GSK_ENUM_VALUE enumValue)
  476. {
  477. struct Curl_gsk_descriptor * p;
  478. if(!my_gsk_handle)
  479. return GSK_INVALID_HANDLE;
  480. p = (struct Curl_gsk_descriptor *) my_gsk_handle;
  481. return gsk_attribute_set_enum(p->h, enumID, enumValue);
  482. }
  483. int
  484. Curl_gsk_attribute_set_numeric_value(gsk_handle my_gsk_handle,
  485. GSK_NUM_ID numID, int numValue)
  486. {
  487. struct Curl_gsk_descriptor * p;
  488. if(!my_gsk_handle)
  489. return GSK_INVALID_HANDLE;
  490. p = (struct Curl_gsk_descriptor *) my_gsk_handle;
  491. return gsk_attribute_set_numeric_value(p->h, numID, numValue);
  492. }
  493. int
  494. Curl_gsk_attribute_set_callback(gsk_handle my_gsk_handle,
  495. GSK_CALLBACK_ID callBackID,
  496. void * callBackAreaPtr)
  497. {
  498. struct Curl_gsk_descriptor * p;
  499. if(!my_gsk_handle)
  500. return GSK_INVALID_HANDLE;
  501. p = (struct Curl_gsk_descriptor *) my_gsk_handle;
  502. return gsk_attribute_set_callback(p->h, callBackID, callBackAreaPtr);
  503. }
  504. static int
  505. cachestring(struct Curl_gsk_descriptor * p,
  506. const char * ebcdicbuf, int bufsize, const char * * buffer)
  507. {
  508. int rc;
  509. char * asciibuf;
  510. struct gskstrlist * sp;
  511. for (sp = p->strlist; sp; sp = sp->next)
  512. if(sp->ebcdicstr == ebcdicbuf)
  513. break;
  514. if(!sp) {
  515. if(!(sp = (struct gskstrlist *) malloc(sizeof *sp)))
  516. return GSK_INSUFFICIENT_STORAGE;
  517. if(!(asciibuf = malloc(bufsize + 1))) {
  518. free(sp);
  519. return GSK_INSUFFICIENT_STORAGE;
  520. }
  521. QadrtConvertE2A(asciibuf, ebcdicbuf, bufsize, bufsize);
  522. asciibuf[bufsize] = '\0';
  523. sp->ebcdicstr = ebcdicbuf;
  524. sp->asciistr = asciibuf;
  525. sp->next = p->strlist;
  526. p->strlist = sp;
  527. }
  528. *buffer = sp->asciistr;
  529. return GSK_OK;
  530. }
  531. int
  532. Curl_gsk_attribute_get_buffer_a(gsk_handle my_gsk_handle, GSK_BUF_ID bufID,
  533. const char * * buffer, int * bufSize)
  534. {
  535. struct Curl_gsk_descriptor * p;
  536. int rc;
  537. const char * mybuf;
  538. int mylen;
  539. if(!my_gsk_handle)
  540. return GSK_INVALID_HANDLE;
  541. if(!buffer || !bufSize)
  542. return GSK_OS400_ERROR_INVALID_POINTER;
  543. p = (struct Curl_gsk_descriptor *) my_gsk_handle;
  544. if ((rc = gsk_attribute_get_buffer(p->h, bufID, &mybuf, &mylen)) != GSK_OK)
  545. return rc;
  546. if((rc = cachestring(p, mybuf, mylen, buffer)) == GSK_OK)
  547. *bufSize = mylen;
  548. return rc;
  549. }
  550. int
  551. Curl_gsk_attribute_get_enum(gsk_handle my_gsk_handle, GSK_ENUM_ID enumID,
  552. GSK_ENUM_VALUE * enumValue)
  553. {
  554. struct Curl_gsk_descriptor * p;
  555. if(!my_gsk_handle)
  556. return GSK_INVALID_HANDLE;
  557. p = (struct Curl_gsk_descriptor *) my_gsk_handle;
  558. return gsk_attribute_get_enum(p->h, enumID, enumValue);
  559. }
  560. int
  561. Curl_gsk_attribute_get_numeric_value(gsk_handle my_gsk_handle,
  562. GSK_NUM_ID numID, int * numValue)
  563. {
  564. struct Curl_gsk_descriptor * p;
  565. if(!my_gsk_handle)
  566. return GSK_INVALID_HANDLE;
  567. p = (struct Curl_gsk_descriptor *) my_gsk_handle;
  568. return gsk_attribute_get_numeric_value(p->h, numID, numValue);
  569. }
  570. int
  571. Curl_gsk_attribute_get_cert_info(gsk_handle my_gsk_handle,
  572. GSK_CERT_ID certID,
  573. const gsk_cert_data_elem * * certDataElem,
  574. int * certDataElementCount)
  575. {
  576. struct Curl_gsk_descriptor * p;
  577. if(!my_gsk_handle)
  578. return GSK_INVALID_HANDLE;
  579. p = (struct Curl_gsk_descriptor *) my_gsk_handle;
  580. /* No need to convert code: text results are already in ASCII. */
  581. return gsk_attribute_get_cert_info(p->h, certID,
  582. certDataElem, certDataElementCount);
  583. }
  584. int
  585. Curl_gsk_secure_soc_misc(gsk_handle my_session_handle, GSK_MISC_ID miscID)
  586. {
  587. struct Curl_gsk_descriptor * p;
  588. if(!my_session_handle)
  589. return GSK_INVALID_HANDLE;
  590. p = (struct Curl_gsk_descriptor *) my_session_handle;
  591. return gsk_secure_soc_misc(p->h, miscID);
  592. }
  593. int
  594. Curl_gsk_secure_soc_read(gsk_handle my_session_handle, char * readBuffer,
  595. int readBufSize, int * amtRead)
  596. {
  597. struct Curl_gsk_descriptor * p;
  598. if(!my_session_handle)
  599. return GSK_INVALID_HANDLE;
  600. p = (struct Curl_gsk_descriptor *) my_session_handle;
  601. return gsk_secure_soc_read(p->h, readBuffer, readBufSize, amtRead);
  602. }
  603. int
  604. Curl_gsk_secure_soc_write(gsk_handle my_session_handle, char * writeBuffer,
  605. int writeBufSize, int * amtWritten)
  606. {
  607. struct Curl_gsk_descriptor * p;
  608. if(!my_session_handle)
  609. return GSK_INVALID_HANDLE;
  610. p = (struct Curl_gsk_descriptor *) my_session_handle;
  611. return gsk_secure_soc_write(p->h, writeBuffer, writeBufSize, amtWritten);
  612. }
  613. const char *
  614. Curl_gsk_strerror_a(int gsk_return_value)
  615. {
  616. int i;
  617. const char * cp;
  618. char * cp2;
  619. cp = gsk_strerror(gsk_return_value);
  620. if (!cp)
  621. return cp;
  622. i = strlen(cp);
  623. if (!(cp2 = Curl_thread_buffer(LK_GSK_ERROR, MAX_CONV_EXPANSION * i + 1)))
  624. return cp2;
  625. i = QadrtConvertE2A(cp2, cp, MAX_CONV_EXPANSION * i, i);
  626. cp2[i] = '\0';
  627. return cp2;
  628. }
  629. int
  630. Curl_gsk_secure_soc_startInit(gsk_handle my_session_handle,
  631. int IOCompletionPort,
  632. Qso_OverlappedIO_t * communicationsArea)
  633. {
  634. struct Curl_gsk_descriptor * p;
  635. if(!my_session_handle)
  636. return GSK_INVALID_HANDLE;
  637. p = (struct Curl_gsk_descriptor *) my_session_handle;
  638. return gsk_secure_soc_startInit(p->h, IOCompletionPort, communicationsArea);
  639. }
  640. #endif /* USE_GSKIT */
  641. #ifdef HAVE_GSSAPI
  642. /* ASCII wrappers for the GSSAPI procedures. */
  643. static int
  644. Curl_gss_convert_in_place(OM_uint32 * minor_status, gss_buffer_t buf)
  645. {
  646. unsigned int i;
  647. char * t;
  648. /* Convert `buf' in place, from EBCDIC to ASCII.
  649. If error, release the buffer and return -1. Else return 0. */
  650. i = buf->length;
  651. if (i) {
  652. if (!(t = malloc(i))) {
  653. gss_release_buffer(minor_status, buf);
  654. if (minor_status)
  655. *minor_status = ENOMEM;
  656. return -1;
  657. }
  658. QadrtConvertE2A(t, buf->value, i, i);
  659. memcpy(buf->value, t, i);
  660. free(t);
  661. }
  662. return 0;
  663. }
  664. OM_uint32
  665. Curl_gss_import_name_a(OM_uint32 * minor_status, gss_buffer_t in_name,
  666. gss_OID in_name_type, gss_name_t * out_name)
  667. {
  668. int rc;
  669. unsigned int i;
  670. gss_buffer_desc in;
  671. if (!in_name || !in_name->value || !in_name->length)
  672. return gss_import_name(minor_status, in_name, in_name_type, out_name);
  673. memcpy((char *) &in, (char *) in_name, sizeof in);
  674. i = in.length;
  675. if (!(in.value = malloc(i + 1))) {
  676. if (minor_status)
  677. *minor_status = ENOMEM;
  678. return GSS_S_FAILURE;
  679. }
  680. QadrtConvertA2E(in.value, in_name->value, i, i);
  681. ((char *) in.value)[i] = '\0';
  682. rc = gss_import_name(minor_status, &in, in_name_type, out_name);
  683. free(in.value);
  684. return rc;
  685. }
  686. OM_uint32
  687. Curl_gss_display_status_a(OM_uint32 * minor_status, OM_uint32 status_value,
  688. int status_type, gss_OID mech_type,
  689. gss_msg_ctx_t * message_context, gss_buffer_t status_string)
  690. {
  691. int rc;
  692. rc = gss_display_status(minor_status, status_value, status_type,
  693. mech_type, message_context, status_string);
  694. if (rc != GSS_S_COMPLETE || !status_string ||
  695. !status_string->length || !status_string->value)
  696. return rc;
  697. /* No way to allocate a buffer here, because it will be released by
  698. gss_release_buffer(). The solution is to overwrite the EBCDIC buffer
  699. with ASCII to return it. */
  700. if (Curl_gss_convert_in_place(minor_status, status_string))
  701. return GSS_S_FAILURE;
  702. return rc;
  703. }
  704. OM_uint32
  705. Curl_gss_init_sec_context_a(OM_uint32 * minor_status, gss_cred_id_t cred_handle,
  706. gss_ctx_id_t * context_handle,
  707. gss_name_t target_name, gss_OID mech_type,
  708. gss_flags_t req_flags, OM_uint32 time_req,
  709. gss_channel_bindings_t input_chan_bindings,
  710. gss_buffer_t input_token,
  711. gss_OID * actual_mech_type,
  712. gss_buffer_t output_token, gss_flags_t * ret_flags,
  713. OM_uint32 * time_rec)
  714. {
  715. int rc;
  716. unsigned int i;
  717. gss_buffer_desc in;
  718. gss_buffer_t inp;
  719. in.value = NULL;
  720. if ((inp = input_token))
  721. if (inp->length && inp->value) {
  722. i = inp->length;
  723. if (!(in.value = malloc(i + 1))) {
  724. if (minor_status)
  725. *minor_status = ENOMEM;
  726. return GSS_S_FAILURE;
  727. }
  728. QadrtConvertA2E(in.value, input_token->value, i, i);
  729. ((char *) in.value)[i] = '\0';
  730. in.length = i;
  731. inp = &in;
  732. }
  733. rc = gss_init_sec_context(minor_status, cred_handle, context_handle,
  734. target_name, mech_type, req_flags, time_req,
  735. input_chan_bindings, inp, actual_mech_type,
  736. output_token, ret_flags, time_rec);
  737. if (in.value)
  738. free(in.value);
  739. if (rc != GSS_S_COMPLETE || !output_token ||
  740. !output_token->length || !output_token->value)
  741. return rc;
  742. /* No way to allocate a buffer here, because it will be released by
  743. gss_release_buffer(). The solution is to overwrite the EBCDIC buffer
  744. with ASCII to return it. */
  745. if (Curl_gss_convert_in_place(minor_status, output_token))
  746. return GSS_S_FAILURE;
  747. return rc;
  748. }
  749. OM_uint32
  750. Curl_gss_delete_sec_context_a(OM_uint32 * minor_status,
  751. gss_ctx_id_t * context_handle,
  752. gss_buffer_t output_token)
  753. {
  754. int rc;
  755. rc = gss_delete_sec_context(minor_status, context_handle, output_token);
  756. if (rc != GSS_S_COMPLETE || !output_token ||
  757. !output_token->length || !output_token->value)
  758. return rc;
  759. /* No way to allocate a buffer here, because it will be released by
  760. gss_release_buffer(). The solution is to overwrite the EBCDIC buffer
  761. with ASCII to return it. */
  762. if (Curl_gss_convert_in_place(minor_status, output_token))
  763. return GSS_S_FAILURE;
  764. return rc;
  765. }
  766. #endif /* HAVE_GSSAPI */
  767. #ifndef CURL_DISABLE_LDAP
  768. /* ASCII wrappers for the LDAP procedures. */
  769. void *
  770. Curl_ldap_init_a(char * host, int port)
  771. {
  772. unsigned int i;
  773. char * ehost;
  774. void * result;
  775. if (!host)
  776. return (void *) ldap_init(host, port);
  777. i = strlen(host);
  778. if (!(ehost = malloc(i + 1)))
  779. return (void *) NULL;
  780. QadrtConvertA2E(ehost, host, i, i);
  781. ehost[i] = '\0';
  782. result = (void *) ldap_init(ehost, port);
  783. free(ehost);
  784. return result;
  785. }
  786. int
  787. Curl_ldap_simple_bind_s_a(void * ld, char * dn, char * passwd)
  788. {
  789. int i;
  790. char * edn;
  791. char * epasswd;
  792. edn = (char *) NULL;
  793. epasswd = (char *) NULL;
  794. if (dn) {
  795. i = strlen(dn);
  796. if (!(edn = malloc(i + 1)))
  797. return LDAP_NO_MEMORY;
  798. QadrtConvertA2E(edn, dn, i, i);
  799. edn[i] = '\0';
  800. }
  801. if (passwd) {
  802. i = strlen(passwd);
  803. if (!(epasswd = malloc(i + 1))) {
  804. if (edn)
  805. free(edn);
  806. return LDAP_NO_MEMORY;
  807. }
  808. QadrtConvertA2E(epasswd, passwd, i, i);
  809. epasswd[i] = '\0';
  810. }
  811. i = ldap_simple_bind_s(ld, edn, epasswd);
  812. if (epasswd)
  813. free(epasswd);
  814. if (edn)
  815. free(edn);
  816. return i;
  817. }
  818. int
  819. Curl_ldap_search_s_a(void * ld, char * base, int scope, char * filter,
  820. char * * attrs, int attrsonly, LDAPMessage * * res)
  821. {
  822. int i;
  823. int j;
  824. char * ebase;
  825. char * efilter;
  826. char * * eattrs;
  827. int status;
  828. ebase = (char *) NULL;
  829. efilter = (char *) NULL;
  830. eattrs = (char * *) NULL;
  831. status = LDAP_SUCCESS;
  832. if (base) {
  833. i = strlen(base);
  834. if (!(ebase = malloc(i + 1)))
  835. status = LDAP_NO_MEMORY;
  836. else {
  837. QadrtConvertA2E(ebase, base, i, i);
  838. ebase[i] = '\0';
  839. }
  840. }
  841. if (filter && status == LDAP_SUCCESS) {
  842. i = strlen(filter);
  843. if (!(efilter = malloc(i + 1)))
  844. status = LDAP_NO_MEMORY;
  845. else {
  846. QadrtConvertA2E(efilter, filter, i, i);
  847. efilter[i] = '\0';
  848. }
  849. }
  850. if (attrs && status == LDAP_SUCCESS) {
  851. for (i = 0; attrs[i++];)
  852. ;
  853. if (!(eattrs = calloc(i, sizeof *eattrs)))
  854. status = LDAP_NO_MEMORY;
  855. else {
  856. for (j = 0; attrs[j]; j++) {
  857. i = strlen(attrs[j]);
  858. if (!(eattrs[j] = malloc(i + 1))) {
  859. status = LDAP_NO_MEMORY;
  860. break;
  861. }
  862. QadrtConvertA2E(eattrs[j], attrs[j], i, i);
  863. eattrs[j][i] = '\0';
  864. }
  865. }
  866. }
  867. if (status == LDAP_SUCCESS)
  868. status = ldap_search_s(ld, ebase? ebase: "", scope,
  869. efilter? efilter: "(objectclass=*)",
  870. eattrs, attrsonly, res);
  871. if (eattrs) {
  872. for (j = 0; eattrs[j]; j++)
  873. free(eattrs[j]);
  874. free(eattrs);
  875. }
  876. if (efilter)
  877. free(efilter);
  878. if (ebase)
  879. free(ebase);
  880. return status;
  881. }
  882. struct berval * *
  883. Curl_ldap_get_values_len_a(void * ld, LDAPMessage * entry, const char * attr)
  884. {
  885. int i;
  886. char * cp;
  887. struct berval * * result;
  888. cp = (char *) NULL;
  889. if (attr) {
  890. i = strlen(attr);
  891. if (!(cp = malloc(i + 1))) {
  892. ldap_set_lderrno(ld, LDAP_NO_MEMORY, NULL,
  893. ldap_err2string(LDAP_NO_MEMORY));
  894. return (struct berval * *) NULL;
  895. }
  896. QadrtConvertA2E(cp, attr, i, i);
  897. cp[i] = '\0';
  898. }
  899. result = ldap_get_values_len(ld, entry, cp);
  900. if (cp)
  901. free(cp);
  902. /* Result data are binary in nature, so they haven't been converted to EBCDIC.
  903. Therefore do not convert. */
  904. return result;
  905. }
  906. char *
  907. Curl_ldap_err2string_a(int error)
  908. {
  909. int i;
  910. char * cp;
  911. char * cp2;
  912. cp = ldap_err2string(error);
  913. if (!cp)
  914. return cp;
  915. i = strlen(cp);
  916. if (!(cp2 = Curl_thread_buffer(LK_LDAP_ERROR, MAX_CONV_EXPANSION * i + 1)))
  917. return cp2;
  918. i = QadrtConvertE2A(cp2, cp, MAX_CONV_EXPANSION * i, i);
  919. cp2[i] = '\0';
  920. return cp2;
  921. }
  922. char *
  923. Curl_ldap_get_dn_a(void * ld, LDAPMessage * entry)
  924. {
  925. int i;
  926. char * cp;
  927. char * cp2;
  928. cp = ldap_get_dn(ld, entry);
  929. if (!cp)
  930. return cp;
  931. i = strlen(cp);
  932. if (!(cp2 = malloc(i + 1)))
  933. return cp2;
  934. QadrtConvertE2A(cp2, cp, i, i);
  935. cp2[i] = '\0';
  936. /* No way to allocate a buffer here, because it will be released by
  937. ldap_memfree() and ldap_memalloc() does not exist. The solution is to
  938. overwrite the EBCDIC buffer with ASCII to return it. */
  939. strcpy(cp, cp2);
  940. free(cp2);
  941. return cp;
  942. }
  943. char *
  944. Curl_ldap_first_attribute_a(void * ld,
  945. LDAPMessage * entry, BerElement * * berptr)
  946. {
  947. int i;
  948. char * cp;
  949. char * cp2;
  950. cp = ldap_first_attribute(ld, entry, berptr);
  951. if (!cp)
  952. return cp;
  953. i = strlen(cp);
  954. if (!(cp2 = malloc(i + 1)))
  955. return cp2;
  956. QadrtConvertE2A(cp2, cp, i, i);
  957. cp2[i] = '\0';
  958. /* No way to allocate a buffer here, because it will be released by
  959. ldap_memfree() and ldap_memalloc() does not exist. The solution is to
  960. overwrite the EBCDIC buffer with ASCII to return it. */
  961. strcpy(cp, cp2);
  962. free(cp2);
  963. return cp;
  964. }
  965. char *
  966. Curl_ldap_next_attribute_a(void * ld,
  967. LDAPMessage * entry, BerElement * berptr)
  968. {
  969. int i;
  970. char * cp;
  971. char * cp2;
  972. cp = ldap_next_attribute(ld, entry, berptr);
  973. if (!cp)
  974. return cp;
  975. i = strlen(cp);
  976. if (!(cp2 = malloc(i + 1)))
  977. return cp2;
  978. QadrtConvertE2A(cp2, cp, i, i);
  979. cp2[i] = '\0';
  980. /* No way to allocate a buffer here, because it will be released by
  981. ldap_memfree() and ldap_memalloc() does not exist. The solution is to
  982. overwrite the EBCDIC buffer with ASCII to return it. */
  983. strcpy(cp, cp2);
  984. free(cp2);
  985. return cp;
  986. }
  987. #endif /* CURL_DISABLE_LDAP */
  988. static int
  989. convert_sockaddr(struct sockaddr_storage * dstaddr,
  990. const struct sockaddr * srcaddr, int srclen)
  991. {
  992. const struct sockaddr_un * srcu;
  993. struct sockaddr_un * dstu;
  994. unsigned int i;
  995. unsigned int dstsize;
  996. /* Convert a socket address into job CCSID, if needed. */
  997. if (!srcaddr || srclen < offsetof(struct sockaddr, sa_family) +
  998. sizeof srcaddr->sa_family || srclen > sizeof *dstaddr) {
  999. errno = EINVAL;
  1000. return -1;
  1001. }
  1002. memcpy((char *) dstaddr, (char *) srcaddr, srclen);
  1003. switch (srcaddr->sa_family) {
  1004. case AF_UNIX:
  1005. srcu = (const struct sockaddr_un *) srcaddr;
  1006. dstu = (struct sockaddr_un *) dstaddr;
  1007. dstsize = sizeof *dstaddr - offsetof(struct sockaddr_un, sun_path);
  1008. srclen -= offsetof(struct sockaddr_un, sun_path);
  1009. i = QadrtConvertA2E(dstu->sun_path, srcu->sun_path, dstsize - 1, srclen);
  1010. dstu->sun_path[i] = '\0';
  1011. i += offsetof(struct sockaddr_un, sun_path);
  1012. srclen = i;
  1013. }
  1014. return srclen;
  1015. }
  1016. int
  1017. Curl_os400_connect(int sd, struct sockaddr * destaddr, int addrlen)
  1018. {
  1019. int i;
  1020. struct sockaddr_storage laddr;
  1021. i = convert_sockaddr(&laddr, destaddr, addrlen);
  1022. if (i < 0)
  1023. return -1;
  1024. return connect(sd, (struct sockaddr *) &laddr, i);
  1025. }
  1026. int
  1027. Curl_os400_bind(int sd, struct sockaddr * localaddr, int addrlen)
  1028. {
  1029. int i;
  1030. struct sockaddr_storage laddr;
  1031. i = convert_sockaddr(&laddr, localaddr, addrlen);
  1032. if (i < 0)
  1033. return -1;
  1034. return bind(sd, (struct sockaddr *) &laddr, i);
  1035. }
  1036. int
  1037. Curl_os400_sendto(int sd, char * buffer, int buflen, int flags,
  1038. struct sockaddr * dstaddr, int addrlen)
  1039. {
  1040. int i;
  1041. struct sockaddr_storage laddr;
  1042. i = convert_sockaddr(&laddr, dstaddr, addrlen);
  1043. if (i < 0)
  1044. return -1;
  1045. return sendto(sd, buffer, buflen, flags, (struct sockaddr *) &laddr, i);
  1046. }
  1047. int
  1048. Curl_os400_recvfrom(int sd, char * buffer, int buflen, int flags,
  1049. struct sockaddr * fromaddr, int * addrlen)
  1050. {
  1051. int i;
  1052. int rcvlen;
  1053. int laddrlen;
  1054. const struct sockaddr_un * srcu;
  1055. struct sockaddr_un * dstu;
  1056. struct sockaddr_storage laddr;
  1057. if (!fromaddr || !addrlen || *addrlen <= 0)
  1058. return recvfrom(sd, buffer, buflen, flags, fromaddr, addrlen);
  1059. laddrlen = sizeof laddr;
  1060. laddr.ss_family = AF_UNSPEC; /* To detect if unused. */
  1061. rcvlen = recvfrom(sd, buffer, buflen, flags,
  1062. (struct sockaddr *) &laddr, &laddrlen);
  1063. if (rcvlen < 0)
  1064. return rcvlen;
  1065. switch (laddr.ss_family) {
  1066. case AF_UNIX:
  1067. srcu = (const struct sockaddr_un *) &laddr;
  1068. dstu = (struct sockaddr_un *) fromaddr;
  1069. i = *addrlen - offsetof(struct sockaddr_un, sun_path);
  1070. laddrlen -= offsetof(struct sockaddr_un, sun_path);
  1071. i = QadrtConvertE2A(dstu->sun_path, srcu->sun_path, i, laddrlen);
  1072. laddrlen = i + offsetof(struct sockaddr_un, sun_path);
  1073. if (laddrlen < *addrlen)
  1074. dstu->sun_path[i] = '\0';
  1075. break;
  1076. case AF_UNSPEC:
  1077. break;
  1078. default:
  1079. if (laddrlen > *addrlen)
  1080. laddrlen = *addrlen;
  1081. if (laddrlen)
  1082. memcpy((char *) fromaddr, (char *) &laddr, laddrlen);
  1083. break;
  1084. }
  1085. *addrlen = laddrlen;
  1086. return rcvlen;
  1087. }