os400sys.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2008, Daniel Stenberg, <[email protected]>, 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. * $Id$
  22. *
  23. ***************************************************************************/
  24. /* OS/400 additional support. */
  25. #include "config-os400.h" /* Not 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 HAVE_GSSAPI
  40. #include <gssapi.h>
  41. #endif
  42. #ifndef CURL_DISABLE_LDAP
  43. #include <ldap.h>
  44. #endif
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include "os400sys.h"
  48. /**
  49. *** QADRT OS/400 ASCII runtime defines only the most used procedures, but
  50. *** but a lot of them are not supported. This module implements
  51. *** ASCII wrappers for those that are used by libcurl, but not
  52. *** defined by QADRT.
  53. **/
  54. #pragma convert(0) /* Restore EBCDIC. */
  55. #define MIN_BYTE_GAIN 1024 /* Minimum gain when shortening a buffer. */
  56. typedef struct {
  57. unsigned long size; /* Buffer size. */
  58. char * buf; /* Buffer address. */
  59. } buffer_t;
  60. static char * buffer_undef(localkey_t key, long size);
  61. static char * buffer_threaded(localkey_t key, long size);
  62. static char * buffer_unthreaded(localkey_t key, long size);
  63. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  64. static pthread_key_t thdkey;
  65. static buffer_t * locbufs;
  66. char * (* Curl_thread_buffer)(localkey_t key, long size) = buffer_undef;
  67. static void
  68. thdbufdestroy(void * private)
  69. {
  70. localkey_t i;
  71. buffer_t * p;
  72. if (private) {
  73. p = (buffer_t *) private;
  74. for (i = (localkey_t) 0; i < LK_LAST; i++) {
  75. if (p->buf)
  76. free(p->buf);
  77. p++;
  78. }
  79. free(private);
  80. }
  81. }
  82. static void
  83. terminate(void)
  84. {
  85. if (Curl_thread_buffer == buffer_threaded) {
  86. locbufs = pthread_getspecific(thdkey);
  87. pthread_setspecific(thdkey, (void *) NULL);
  88. pthread_key_delete(thdkey);
  89. }
  90. if (Curl_thread_buffer != buffer_undef) {
  91. thdbufdestroy((void *) locbufs);
  92. locbufs = (buffer_t *) NULL;
  93. }
  94. Curl_thread_buffer = buffer_undef;
  95. }
  96. static char *
  97. get_buffer(buffer_t * buf, long size)
  98. {
  99. char * cp;
  100. /* If `size' >= 0, make sure buffer at `buf' is at least `size'-byte long.
  101. Return the buffer address. */
  102. if (size < 0)
  103. return buf->buf;
  104. if (!buf->buf) {
  105. if ((buf->buf = malloc(size)))
  106. buf->size = size;
  107. return buf->buf;
  108. }
  109. if ((unsigned long) size <= buf->size) {
  110. /* Shorten the buffer only if it frees a significant byte count. This
  111. avoids some realloc() overhead. */
  112. if (buf->size - size < MIN_BYTE_GAIN)
  113. return buf->buf;
  114. }
  115. /* Resize the buffer. */
  116. if ((cp = realloc(buf->buf, size))) {
  117. buf->buf = cp;
  118. buf->size = size;
  119. }
  120. else if (size <= buf->size)
  121. cp = buf->buf;
  122. return cp;
  123. }
  124. static char *
  125. buffer_unthreaded(localkey_t key, long size)
  126. {
  127. return get_buffer(locbufs + key, size);
  128. }
  129. static char *
  130. buffer_threaded(localkey_t key, long size)
  131. {
  132. buffer_t * bufs;
  133. /* Get the buffer for the given local key in the current thread, and
  134. make sure it is at least `size'-byte long. Set `size' to < 0 to get
  135. its address only. */
  136. bufs = (buffer_t *) pthread_getspecific(thdkey);
  137. if (!bufs) {
  138. if (size < 0)
  139. return (char *) NULL; /* No buffer yet. */
  140. /* Allocate buffer descriptors for the current thread. */
  141. if (!(bufs = (buffer_t *) calloc((size_t) LK_LAST, sizeof *bufs)))
  142. return (char *) NULL;
  143. if (pthread_setspecific(thdkey, (void *) bufs)) {
  144. free(bufs);
  145. return (char *) NULL;
  146. }
  147. }
  148. return get_buffer(bufs + key, size);
  149. }
  150. static char *
  151. buffer_undef(localkey_t key, long size)
  152. {
  153. /* Define the buffer system, get the buffer for the given local key in
  154. the current thread, and make sure it is at least `size'-byte long.
  155. Set `size' to < 0 to get its address only. */
  156. pthread_mutex_lock(&mutex);
  157. /* Determine if we can use pthread-specific data. */
  158. if (Curl_thread_buffer == buffer_undef) { /* If unchanged during lock. */
  159. if (!pthread_key_create(&thdkey, thdbufdestroy))
  160. Curl_thread_buffer = buffer_threaded;
  161. else if (!(locbufs = (buffer_t *) calloc((size_t) LK_LAST,
  162. sizeof *locbufs))) {
  163. pthread_mutex_unlock(&mutex);
  164. return (char *) NULL;
  165. }
  166. else
  167. Curl_thread_buffer = buffer_unthreaded;
  168. atexit(terminate);
  169. }
  170. pthread_mutex_unlock(&mutex);
  171. return Curl_thread_buffer(key, size);
  172. }
  173. int
  174. Curl_getnameinfo_a(const struct sockaddr * sa, socklen_t salen,
  175. char * nodename, socklen_t nodenamelen,
  176. char * servname, socklen_t servnamelen,
  177. int flags)
  178. {
  179. char * enodename;
  180. char * eservname;
  181. int status;
  182. int i;
  183. enodename = (char *) NULL;
  184. eservname = (char *) NULL;
  185. if (nodename && nodenamelen)
  186. if (!(enodename = malloc(nodenamelen)))
  187. return EAI_MEMORY;
  188. if (servname && servnamelen)
  189. if (!(eservname = malloc(servnamelen))) {
  190. if (enodename)
  191. free(enodename);
  192. return EAI_MEMORY;
  193. }
  194. status = getnameinfo(sa, salen, enodename, nodenamelen,
  195. eservname, servnamelen, flags);
  196. if (!status) {
  197. if (enodename) {
  198. i = QadrtConvertE2A(nodename, enodename,
  199. nodenamelen - 1, strlen(enodename));
  200. nodename[i] = '\0';
  201. }
  202. if (eservname) {
  203. i = QadrtConvertE2A(servname, eservname,
  204. servnamelen - 1, strlen(eservname));
  205. servname[i] = '\0';
  206. }
  207. }
  208. if (enodename)
  209. free(enodename);
  210. if (eservname)
  211. free(eservname);
  212. return status;
  213. }
  214. int
  215. Curl_getaddrinfo_a(const char * nodename, const char * servname,
  216. const struct addrinfo * hints,
  217. struct addrinfo * * res)
  218. {
  219. char * enodename;
  220. char * eservname;
  221. int status;
  222. int i;
  223. enodename = (char *) NULL;
  224. eservname = (char *) NULL;
  225. if (nodename) {
  226. i = strlen(nodename);
  227. if (!(enodename = malloc(i + 1)))
  228. return EAI_MEMORY;
  229. i = QadrtConvertA2E(enodename, nodename, i, i);
  230. enodename[i] = '\0';
  231. }
  232. if (servname) {
  233. i = strlen(servname);
  234. if (!(eservname = malloc(i + 1))) {
  235. if (enodename)
  236. free(enodename);
  237. return EAI_MEMORY;
  238. }
  239. QadrtConvertA2E(eservname, servname, i, i);
  240. eservname[i] = '\0';
  241. }
  242. status = getaddrinfo(enodename, eservname, hints, res);
  243. if (enodename)
  244. free(enodename);
  245. if (eservname)
  246. free(eservname);
  247. return status;
  248. }
  249. int
  250. Curl_inet_ntoa_r_a(struct in_addr internet_address,
  251. char * output_buffer, int output_buffer_length)
  252. {
  253. int rc;
  254. int i;
  255. char * cp;
  256. if (!output_buffer || output_buffer_length < 16)
  257. return inet_ntoa_r(internet_address, output_buffer, output_buffer_length);
  258. if (!(cp = malloc(output_buffer_length + 1)))
  259. return -1;
  260. rc = inet_ntoa_r(internet_address, cp, output_buffer_length);
  261. if (rc) {
  262. free(cp);
  263. return rc;
  264. }
  265. cp[output_buffer_length - 1] = '\0';
  266. i = strlen(cp);
  267. QadrtConvertE2A(output_buffer, cp, i, i);
  268. output_buffer[i] = '\0';
  269. free(cp);
  270. return rc;
  271. }
  272. #ifdef USE_QSOSSL
  273. /* ASCII wrappers for the SSL procedures. */
  274. int
  275. Curl_SSL_Init_Application_a(SSLInitApp * init_app)
  276. {
  277. int rc;
  278. unsigned int i;
  279. SSLInitApp ia;
  280. if (!init_app || !init_app->applicationID || !init_app->applicationIDLen)
  281. return SSL_Init_Application(init_app);
  282. memcpy((char *) &ia, (char *) init_app, sizeof ia);
  283. i = ia.applicationIDLen;
  284. if (!(ia.applicationID = malloc(i + 1))) {
  285. errno = ENOMEM;
  286. return SSL_ERROR_IO;
  287. }
  288. QadrtConvertA2E(ia.applicationID, init_app->applicationID, i, i);
  289. ia.applicationID[i] = '\0';
  290. rc = SSL_Init_Application(&ia);
  291. free(ia.applicationID);
  292. init_app->localCertificateLen = ia.localCertificateLen;
  293. init_app->sessionType = ia.sessionType;
  294. return rc;
  295. }
  296. int
  297. Curl_SSL_Init_a(SSLInit * init)
  298. {
  299. int rc;
  300. unsigned int i;
  301. SSLInit ia;
  302. if (!init || (!init->keyringFileName && !init->keyringPassword))
  303. return SSL_Init(init);
  304. memcpy((char *) &ia, (char *) init, sizeof ia);
  305. if (ia.keyringFileName) {
  306. i = strlen(ia.keyringFileName);
  307. if (!(ia.keyringFileName = malloc(i + 1))) {
  308. errno = ENOMEM;
  309. return SSL_ERROR_IO;
  310. }
  311. QadrtConvertA2E(ia.keyringFileName, init->keyringFileName, i, i);
  312. ia.keyringFileName[i] = '\0';
  313. }
  314. if (ia.keyringPassword) {
  315. i = strlen(ia.keyringPassword);
  316. if (!(ia.keyringPassword = malloc(i + 1))) {
  317. if (ia.keyringFileName)
  318. free(ia.keyringFileName);
  319. errno = ENOMEM;
  320. return SSL_ERROR_IO;
  321. }
  322. QadrtConvertA2E(ia.keyringPassword, init->keyringPassword, i, i);
  323. ia.keyringPassword[i] = '\0';
  324. }
  325. rc = SSL_Init(&ia);
  326. if (ia.keyringFileName)
  327. free(ia.keyringFileName);
  328. if (ia.keyringPassword)
  329. free(ia.keyringPassword);
  330. return rc;
  331. }
  332. char *
  333. Curl_SSL_Strerror_a(int sslreturnvalue, SSLErrorMsg * serrmsgp)
  334. {
  335. int i;
  336. char * cp;
  337. char * cp2;
  338. cp = SSL_Strerror(sslreturnvalue, serrmsgp);
  339. if (!cp)
  340. return cp;
  341. i = strlen(cp);
  342. if (!(cp2 = Curl_thread_buffer(LK_SSL_ERROR, MAX_CONV_EXPANSION * i + 1)))
  343. return cp2;
  344. i = QadrtConvertE2A(cp2, cp, MAX_CONV_EXPANSION * i, i);
  345. cp2[i] = '\0';
  346. return cp2;
  347. }
  348. #endif /* USE_QSOSSL */
  349. #ifdef HAVE_GSSAPI
  350. /* ASCII wrappers for the GSSAPI procedures. */
  351. static int
  352. Curl_gss_convert_in_place(OM_uint32 * minor_status, gss_buffer_t buf)
  353. {
  354. unsigned int i;
  355. char * t;
  356. /* Convert `buf' in place, from EBCDIC to ASCII.
  357. If error, release the buffer and return -1. Else return 0. */
  358. i = buf->length;
  359. if (i) {
  360. if (!(t = malloc(i))) {
  361. gss_release_buffer(minor_status, buf);
  362. if (minor_status)
  363. *minor_status = ENOMEM;
  364. return -1;
  365. }
  366. QadrtConvertE2A(t, buf->value, i, i);
  367. memcpy(buf->value, t, i);
  368. free(t);
  369. }
  370. return 0;
  371. }
  372. OM_uint32
  373. Curl_gss_import_name_a(OM_uint32 * minor_status, gss_buffer_t in_name,
  374. gss_OID in_name_type, gss_name_t * out_name)
  375. {
  376. int rc;
  377. unsigned int i;
  378. gss_buffer_desc in;
  379. if (!in_name || !in_name->value || !in_name->length)
  380. return gss_import_name(minor_status, in_name, in_name_type, out_name);
  381. memcpy((char *) &in, (char *) in_name, sizeof in);
  382. i = in.length;
  383. if (!(in.value = malloc(i + 1))) {
  384. if (minor_status)
  385. *minor_status = ENOMEM;
  386. return GSS_S_FAILURE;
  387. }
  388. QadrtConvertA2E(in.value, in_name->value, i, i);
  389. ((char *) in.value)[i] = '\0';
  390. rc = gss_import_name(minor_status, &in, in_name_type, out_name);
  391. free(in.value);
  392. return rc;
  393. }
  394. OM_uint32
  395. Curl_gss_display_status_a(OM_uint32 * minor_status, OM_uint32 status_value,
  396. int status_type, gss_OID mech_type,
  397. gss_msg_ctx_t * message_context, gss_buffer_t status_string)
  398. {
  399. int rc;
  400. rc = gss_display_status(minor_status, status_value, status_type,
  401. mech_type, message_context, status_string);
  402. if (rc != GSS_S_COMPLETE || !status_string ||
  403. !status_string->length || !status_string->value)
  404. return rc;
  405. /* No way to allocate a buffer here, because it will be released by
  406. gss_release_buffer(). The solution is to overwrite the EBCDIC buffer
  407. with ASCII to return it. */
  408. if (Curl_gss_convert_in_place(minor_status, status_string))
  409. return GSS_S_FAILURE;
  410. return rc;
  411. }
  412. OM_uint32
  413. Curl_gss_init_sec_context_a(OM_uint32 * minor_status, gss_cred_id_t cred_handle,
  414. gss_ctx_id_t * context_handle,
  415. gss_name_t target_name, gss_OID mech_type,
  416. gss_flags_t req_flags, OM_uint32 time_req,
  417. gss_channel_bindings_t input_chan_bindings,
  418. gss_buffer_t input_token,
  419. gss_OID * actual_mech_type,
  420. gss_buffer_t output_token, gss_flags_t * ret_flags,
  421. OM_uint32 * time_rec)
  422. {
  423. int rc;
  424. unsigned int i;
  425. gss_buffer_desc in;
  426. gss_buffer_t inp;
  427. in.value = NULL;
  428. if ((inp = input_token))
  429. if (inp->length && inp->value) {
  430. i = inp->length;
  431. if (!(in.value = malloc(i + 1))) {
  432. if (minor_status)
  433. *minor_status = ENOMEM;
  434. return GSS_S_FAILURE;
  435. }
  436. QadrtConvertA2E(in.value, input_token->value, i, i);
  437. ((char *) in.value)[i] = '\0';
  438. in.length = i;
  439. inp = &in;
  440. }
  441. rc = gss_init_sec_context(minor_status, cred_handle, context_handle,
  442. target_name, mech_type, req_flags, time_req,
  443. input_chan_bindings, inp, actual_mech_type,
  444. output_token, ret_flags, time_rec);
  445. if (in.value)
  446. free(in.value);
  447. if (rc != GSS_S_COMPLETE || !output_token ||
  448. !output_token->length || !output_token->value)
  449. return rc;
  450. /* No way to allocate a buffer here, because it will be released by
  451. gss_release_buffer(). The solution is to overwrite the EBCDIC buffer
  452. with ASCII to return it. */
  453. if (Curl_gss_convert_in_place(minor_status, output_token))
  454. return GSS_S_FAILURE;
  455. return rc;
  456. }
  457. OM_uint32
  458. Curl_gss_delete_sec_context_a(OM_uint32 * minor_status,
  459. gss_ctx_id_t * context_handle,
  460. gss_buffer_t output_token)
  461. {
  462. int rc;
  463. rc = gss_delete_sec_context(minor_status, context_handle, output_token);
  464. if (rc != GSS_S_COMPLETE || !output_token ||
  465. !output_token->length || !output_token->value)
  466. return rc;
  467. /* No way to allocate a buffer here, because it will be released by
  468. gss_release_buffer(). The solution is to overwrite the EBCDIC buffer
  469. with ASCII to return it. */
  470. if (Curl_gss_convert_in_place(minor_status, output_token))
  471. return GSS_S_FAILURE;
  472. return rc;
  473. }
  474. #endif /* HAVE_GSSAPI */
  475. #ifndef CURL_DISABLE_LDAP
  476. /* ASCII wrappers for the LDAP procedures. */
  477. void *
  478. Curl_ldap_init_a(char * host, int port)
  479. {
  480. unsigned int i;
  481. char * ehost;
  482. void * result;
  483. if (!host)
  484. return (void *) ldap_init(host, port);
  485. i = strlen(host);
  486. if (!(ehost = malloc(i + 1)))
  487. return (void *) NULL;
  488. QadrtConvertA2E(ehost, host, i, i);
  489. ehost[i] = '\0';
  490. result = (void *) ldap_init(ehost, port);
  491. free(ehost);
  492. return result;
  493. }
  494. int
  495. Curl_ldap_simple_bind_s_a(void * ld, char * dn, char * passwd)
  496. {
  497. int i;
  498. char * edn;
  499. char * epasswd;
  500. edn = (char *) NULL;
  501. epasswd = (char *) NULL;
  502. if (dn) {
  503. i = strlen(dn);
  504. if (!(edn = malloc(i + 1)))
  505. return LDAP_NO_MEMORY;
  506. QadrtConvertA2E(edn, dn, i, i);
  507. edn[i] = '\0';
  508. }
  509. if (passwd) {
  510. i = strlen(passwd);
  511. if (!(epasswd = malloc(i + 1))) {
  512. if (edn)
  513. free(edn);
  514. return LDAP_NO_MEMORY;
  515. }
  516. QadrtConvertA2E(epasswd, passwd, i, i);
  517. epasswd[i] = '\0';
  518. }
  519. i = ldap_simple_bind_s(ld, edn, epasswd);
  520. if (epasswd)
  521. free(epasswd);
  522. if (edn)
  523. free(edn);
  524. return i;
  525. }
  526. int
  527. Curl_ldap_search_s_a(void * ld, char * base, int scope, char * filter,
  528. char * * attrs, int attrsonly, LDAPMessage * * res)
  529. {
  530. int i;
  531. int j;
  532. char * ebase;
  533. char * efilter;
  534. char * * eattrs;
  535. int status;
  536. ebase = (char *) NULL;
  537. efilter = (char *) NULL;
  538. eattrs = (char * *) NULL;
  539. status = LDAP_SUCCESS;
  540. if (base) {
  541. i = strlen(base);
  542. if (!(ebase = malloc(i + 1)))
  543. status = LDAP_NO_MEMORY;
  544. else {
  545. QadrtConvertA2E(ebase, base, i, i);
  546. ebase[i] = '\0';
  547. }
  548. }
  549. if (filter && status == LDAP_SUCCESS) {
  550. i = strlen(filter);
  551. if (!(efilter = malloc(i + 1)))
  552. status = LDAP_NO_MEMORY;
  553. else {
  554. QadrtConvertA2E(efilter, filter, i, i);
  555. efilter[i] = '\0';
  556. }
  557. }
  558. if (attrs && status == LDAP_SUCCESS) {
  559. for (i = 0; attrs[i++];)
  560. ;
  561. if (!(eattrs = (char * *) calloc(i, sizeof *eattrs)))
  562. status = LDAP_NO_MEMORY;
  563. else {
  564. for (j = 0; attrs[j]; j++) {
  565. i = strlen(attrs[j]);
  566. if (!(eattrs[j] = malloc(i + 1))) {
  567. status = LDAP_NO_MEMORY;
  568. break;
  569. }
  570. QadrtConvertA2E(eattrs[j], attrs[j], i, i);
  571. eattrs[j][i] = '\0';
  572. }
  573. }
  574. }
  575. if (status == LDAP_SUCCESS)
  576. status = ldap_search_s(ld, ebase? ebase: "", scope,
  577. efilter? efilter: "(objectclass=*)",
  578. eattrs, attrsonly, res);
  579. if (eattrs) {
  580. for (j = 0; eattrs[j]; j++)
  581. free(eattrs[j]);
  582. free(eattrs);
  583. }
  584. if (efilter)
  585. free(efilter);
  586. if (ebase)
  587. free(ebase);
  588. return status;
  589. }
  590. struct berval * *
  591. Curl_ldap_get_values_len_a(void * ld, LDAPMessage * entry, const char * attr)
  592. {
  593. int i;
  594. char * cp;
  595. struct berval * * result;
  596. cp = (char *) NULL;
  597. if (attr) {
  598. i = strlen(attr);
  599. if (!(cp = malloc(i + 1))) {
  600. ldap_set_lderrno(ld, LDAP_NO_MEMORY, NULL,
  601. ldap_err2string(LDAP_NO_MEMORY));
  602. return (struct berval * *) NULL;
  603. }
  604. QadrtConvertA2E(cp, attr, i, i);
  605. cp[i] = '\0';
  606. }
  607. result = ldap_get_values_len(ld, entry, cp);
  608. if (cp)
  609. free(cp);
  610. /* Result data are binary in nature, so they haven't been converted to EBCDIC.
  611. Therefore do not convert. */
  612. return result;
  613. }
  614. char *
  615. Curl_ldap_err2string_a(int error)
  616. {
  617. int i;
  618. char * cp;
  619. char * cp2;
  620. cp = ldap_err2string(error);
  621. if (!cp)
  622. return cp;
  623. i = strlen(cp);
  624. if (!(cp2 = Curl_thread_buffer(LK_LDAP_ERROR, MAX_CONV_EXPANSION * i + 1)))
  625. return cp2;
  626. i = QadrtConvertE2A(cp2, cp, MAX_CONV_EXPANSION * i, i);
  627. cp2[i] = '\0';
  628. return cp2;
  629. }
  630. char *
  631. Curl_ldap_get_dn_a(void * ld, LDAPMessage * entry)
  632. {
  633. int i;
  634. char * cp;
  635. char * cp2;
  636. cp = ldap_get_dn(ld, entry);
  637. if (!cp)
  638. return cp;
  639. i = strlen(cp);
  640. if (!(cp2 = malloc(i + 1)))
  641. return cp2;
  642. QadrtConvertE2A(cp2, cp, i, i);
  643. /* No way to allocate a buffer here, because it will be released by
  644. ldap_memfree() and ldap_memalloc() does not exist. The solution is to
  645. overwrite the EBCDIC buffer with ASCII to return it. */
  646. strcpy(cp, cp2);
  647. free(cp2);
  648. return cp;
  649. }
  650. char *
  651. Curl_ldap_first_attribute_a(void * ld,
  652. LDAPMessage * entry, BerElement * * berptr)
  653. {
  654. int i;
  655. char * cp;
  656. char * cp2;
  657. cp = ldap_first_attribute(ld, entry, berptr);
  658. if (!cp)
  659. return cp;
  660. i = strlen(cp);
  661. if (!(cp2 = malloc(i + 1)))
  662. return cp2;
  663. QadrtConvertE2A(cp2, cp, i, i);
  664. /* No way to allocate a buffer here, because it will be released by
  665. ldap_memfree() and ldap_memalloc() does not exist. The solution is to
  666. overwrite the EBCDIC buffer with ASCII to return it. */
  667. strcpy(cp, cp2);
  668. free(cp2);
  669. return cp;
  670. }
  671. char *
  672. Curl_ldap_next_attribute_a(void * ld,
  673. LDAPMessage * entry, BerElement * berptr)
  674. {
  675. int i;
  676. char * cp;
  677. char * cp2;
  678. cp = ldap_next_attribute(ld, entry, berptr);
  679. if (!cp)
  680. return cp;
  681. i = strlen(cp);
  682. if (!(cp2 = malloc(i + 1)))
  683. return cp2;
  684. QadrtConvertE2A(cp2, cp, i, i);
  685. /* No way to allocate a buffer here, because it will be released by
  686. ldap_memfree() and ldap_memalloc() does not exist. The solution is to
  687. overwrite the EBCDIC buffer with ASCII to return it. */
  688. strcpy(cp, cp2);
  689. free(cp2);
  690. return cp;
  691. }
  692. #endif /* CURL_DISABLE_LDAP */
  693. static int
  694. convert_sockaddr(struct sockaddr_storage * dstaddr,
  695. const struct sockaddr * srcaddr, int srclen)
  696. {
  697. const struct sockaddr_un * srcu;
  698. struct sockaddr_un * dstu;
  699. unsigned int i;
  700. unsigned int dstsize;
  701. /* Convert a socket address into job CCSID, if needed. */
  702. if (!srcaddr || srclen < offsetof(struct sockaddr, sa_family) +
  703. sizeof srcaddr->sa_family || srclen > sizeof *dstaddr) {
  704. errno = EINVAL;
  705. return -1;
  706. }
  707. memcpy((char *) dstaddr, (char *) srcaddr, srclen);
  708. switch (srcaddr->sa_family) {
  709. case AF_UNIX:
  710. srcu = (const struct sockaddr_un *) srcaddr;
  711. dstu = (struct sockaddr_un *) dstaddr;
  712. dstsize = sizeof *dstaddr - offsetof(struct sockaddr_un, sun_path);
  713. srclen -= offsetof(struct sockaddr_un, sun_path);
  714. i = QadrtConvertA2E(dstu->sun_path, srcu->sun_path, dstsize - 1, srclen);
  715. dstu->sun_path[i] = '\0';
  716. i += offsetof(struct sockaddr_un, sun_path);
  717. srclen = i;
  718. }
  719. return srclen;
  720. }
  721. int
  722. Curl_os400_connect(int sd, struct sockaddr * destaddr, int addrlen)
  723. {
  724. int i;
  725. struct sockaddr_storage laddr;
  726. i = convert_sockaddr(&laddr, destaddr, addrlen);
  727. if (i < 0)
  728. return -1;
  729. return connect(sd, (struct sockaddr *) &laddr, i);
  730. }
  731. int
  732. Curl_os400_bind(int sd, struct sockaddr * localaddr, int addrlen)
  733. {
  734. int i;
  735. struct sockaddr_storage laddr;
  736. i = convert_sockaddr(&laddr, localaddr, addrlen);
  737. if (i < 0)
  738. return -1;
  739. return bind(sd, (struct sockaddr *) &laddr, i);
  740. }
  741. int
  742. Curl_os400_sendto(int sd, char * buffer, int buflen, int flags,
  743. struct sockaddr * dstaddr, int addrlen)
  744. {
  745. int i;
  746. struct sockaddr_storage laddr;
  747. i = convert_sockaddr(&laddr, dstaddr, addrlen);
  748. if (i < 0)
  749. return -1;
  750. return sendto(sd, buffer, buflen, flags, (struct sockaddr *) &laddr, i);
  751. }
  752. int
  753. Curl_os400_recvfrom(int sd, char * buffer, int buflen, int flags,
  754. struct sockaddr * fromaddr, int * addrlen)
  755. {
  756. int i;
  757. int rcvlen;
  758. int laddrlen;
  759. const struct sockaddr_un * srcu;
  760. struct sockaddr_un * dstu;
  761. struct sockaddr_storage laddr;
  762. if (!fromaddr || !addrlen || *addrlen <= 0)
  763. return recvfrom(sd, buffer, buflen, flags, fromaddr, addrlen);
  764. laddrlen = sizeof laddr;
  765. laddr.ss_family = AF_UNSPEC; /* To detect if unused. */
  766. rcvlen = recvfrom(sd, buffer, buflen, flags,
  767. (struct sockaddr *) &laddr, &laddrlen);
  768. if (rcvlen < 0)
  769. return rcvlen;
  770. switch (laddr.ss_family) {
  771. case AF_UNIX:
  772. srcu = (const struct sockaddr_un *) &laddr;
  773. dstu = (struct sockaddr_un *) fromaddr;
  774. i = *addrlen - offsetof(struct sockaddr_un, sun_path);
  775. laddrlen -= offsetof(struct sockaddr_un, sun_path);
  776. i = QadrtConvertE2A(dstu->sun_path, srcu->sun_path, i, laddrlen);
  777. laddrlen = i + offsetof(struct sockaddr_un, sun_path);
  778. if (laddrlen < *addrlen)
  779. dstu->sun_path[i] = '\0';
  780. break;
  781. case AF_UNSPEC:
  782. break;
  783. default:
  784. if (laddrlen > *addrlen)
  785. laddrlen = *addrlen;
  786. if (laddrlen)
  787. memcpy((char *) fromaddr, (char *) &laddr, laddrlen);
  788. break;
  789. }
  790. *addrlen = laddrlen;
  791. return rcvlen;
  792. }