os400sys.c 31 KB

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