1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
22 ***************************************************************************/
24 /* OS/400 additional support. */
26 #include "curlbuild.h"
27 #include "config-os400.h" /* Not setup.h: we only need some defines. */
29 #include <sys/types.h>
30 #include <sys/socket.h>
49 #ifndef CURL_DISABLE_LDAP
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
60 *** QADRT OS/400 ASCII runtime defines only the most used procedures, but
61 *** but a lot of them are not supported. This module implements
62 *** ASCII wrappers for those that are used by libcurl, but not
66 #pragma convert(0) /* Restore EBCDIC. */
69 #define MIN_BYTE_GAIN 1024 /* Minimum gain when shortening a buffer. */
72 unsigned long size; /* Buffer size. */
73 char * buf; /* Buffer address. */
77 static char * buffer_undef(localkey_t key, long size);
78 static char * buffer_threaded(localkey_t key, long size);
79 static char * buffer_unthreaded(localkey_t key, long size);
81 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
82 static pthread_key_t thdkey;
83 static buffer_t * locbufs;
85 char * (* Curl_thread_buffer)(localkey_t key, long size) = buffer_undef;
89 thdbufdestroy(void * private)
96 p = (buffer_t *) private;
98 for (i = (localkey_t) 0; i < LK_LAST; i++) {
114 if (Curl_thread_buffer == buffer_threaded) {
115 locbufs = pthread_getspecific(thdkey);
116 pthread_setspecific(thdkey, (void *) NULL);
117 pthread_key_delete(thdkey);
120 if (Curl_thread_buffer != buffer_undef) {
121 thdbufdestroy((void *) locbufs);
122 locbufs = (buffer_t *) NULL;
125 Curl_thread_buffer = buffer_undef;
130 get_buffer(buffer_t * buf, long size)
135 /* If `size' >= 0, make sure buffer at `buf' is at least `size'-byte long.
136 Return the buffer address. */
142 if ((buf->buf = malloc(size)))
148 if ((unsigned long) size <= buf->size) {
149 /* Shorten the buffer only if it frees a significant byte count. This
150 avoids some realloc() overhead. */
152 if (buf->size - size < MIN_BYTE_GAIN)
156 /* Resize the buffer. */
158 if ((cp = realloc(buf->buf, size))) {
162 else if (size <= buf->size)
170 buffer_unthreaded(localkey_t key, long size)
173 return get_buffer(locbufs + key, size);
178 buffer_threaded(localkey_t key, long size)
183 /* Get the buffer for the given local key in the current thread, and
184 make sure it is at least `size'-byte long. Set `size' to < 0 to get
187 bufs = (buffer_t *) pthread_getspecific(thdkey);
191 return (char *) NULL; /* No buffer yet. */
193 /* Allocate buffer descriptors for the current thread. */
195 if (!(bufs = calloc((size_t) LK_LAST, sizeof *bufs)))
196 return (char *) NULL;
198 if (pthread_setspecific(thdkey, (void *) bufs)) {
200 return (char *) NULL;
204 return get_buffer(bufs + key, size);
209 buffer_undef(localkey_t key, long size)
212 /* Define the buffer system, get the buffer for the given local key in
213 the current thread, and make sure it is at least `size'-byte long.
214 Set `size' to < 0 to get its address only. */
216 pthread_mutex_lock(&mutex);
218 /* Determine if we can use pthread-specific data. */
220 if (Curl_thread_buffer == buffer_undef) { /* If unchanged during lock. */
221 if (!pthread_key_create(&thdkey, thdbufdestroy))
222 Curl_thread_buffer = buffer_threaded;
223 else if (!(locbufs = calloc((size_t) LK_LAST,
225 pthread_mutex_unlock(&mutex);
226 return (char *) NULL;
229 Curl_thread_buffer = buffer_unthreaded;
234 pthread_mutex_unlock(&mutex);
235 return Curl_thread_buffer(key, size);
240 Curl_getnameinfo_a(const struct sockaddr * sa, curl_socklen_t salen,
241 char * nodename, curl_socklen_t nodenamelen,
242 char * servname, curl_socklen_t servnamelen,
251 enodename = (char *) NULL;
252 eservname = (char *) NULL;
254 if (nodename && nodenamelen)
255 if (!(enodename = malloc(nodenamelen)))
258 if (servname && servnamelen)
259 if (!(eservname = malloc(servnamelen))) {
266 status = getnameinfo(sa, salen, enodename, nodenamelen,
267 eservname, servnamelen, flags);
271 i = QadrtConvertE2A(nodename, enodename,
272 nodenamelen - 1, strlen(enodename));
277 i = QadrtConvertE2A(servname, eservname,
278 servnamelen - 1, strlen(eservname));
294 Curl_getaddrinfo_a(const char * nodename, const char * servname,
295 const struct addrinfo * hints,
296 struct addrinfo * * res)
304 enodename = (char *) NULL;
305 eservname = (char *) NULL;
308 i = strlen(nodename);
310 if (!(enodename = malloc(i + 1)))
313 i = QadrtConvertA2E(enodename, nodename, i, i);
318 i = strlen(servname);
320 if (!(eservname = malloc(i + 1))) {
327 QadrtConvertA2E(eservname, servname, i, i);
331 status = getaddrinfo(enodename, eservname, hints, res);
345 /* ASCII wrappers for the SSL procedures. */
348 Curl_SSL_Init_Application_a(SSLInitApp * init_app)
355 if (!init_app || !init_app->applicationID || !init_app->applicationIDLen)
356 return SSL_Init_Application(init_app);
358 memcpy((char *) &ia, (char *) init_app, sizeof ia);
359 i = ia.applicationIDLen;
361 if (!(ia.applicationID = malloc(i + 1))) {
366 QadrtConvertA2E(ia.applicationID, init_app->applicationID, i, i);
367 ia.applicationID[i] = '\0';
368 rc = SSL_Init_Application(&ia);
369 free(ia.applicationID);
370 init_app->localCertificateLen = ia.localCertificateLen;
371 init_app->sessionType = ia.sessionType;
377 Curl_SSL_Init_a(SSLInit * init)
384 if (!init || (!init->keyringFileName && !init->keyringPassword))
385 return SSL_Init(init);
387 memcpy((char *) &ia, (char *) init, sizeof ia);
389 if (ia.keyringFileName) {
390 i = strlen(ia.keyringFileName);
392 if (!(ia.keyringFileName = malloc(i + 1))) {
397 QadrtConvertA2E(ia.keyringFileName, init->keyringFileName, i, i);
398 ia.keyringFileName[i] = '\0';
401 if (ia.keyringPassword) {
402 i = strlen(ia.keyringPassword);
404 if (!(ia.keyringPassword = malloc(i + 1))) {
405 if (ia.keyringFileName)
406 free(ia.keyringFileName);
412 QadrtConvertA2E(ia.keyringPassword, init->keyringPassword, i, i);
413 ia.keyringPassword[i] = '\0';
418 if (ia.keyringFileName)
419 free(ia.keyringFileName);
421 if (ia.keyringPassword)
422 free(ia.keyringPassword);
429 Curl_SSL_Strerror_a(int sslreturnvalue, SSLErrorMsg * serrmsgp)
436 cp = SSL_Strerror(sslreturnvalue, serrmsgp);
443 if (!(cp2 = Curl_thread_buffer(LK_SSL_ERROR, MAX_CONV_EXPANSION * i + 1)))
446 i = QadrtConvertE2A(cp2, cp, MAX_CONV_EXPANSION * i, i);
451 #endif /* USE_QSOSSL */
456 /* ASCII wrappers for the GSSAPI procedures. */
459 Curl_gss_convert_in_place(OM_uint32 * minor_status, gss_buffer_t buf)
465 /* Convert `buf' in place, from EBCDIC to ASCII.
466 If error, release the buffer and return -1. Else return 0. */
471 if (!(t = malloc(i))) {
472 gss_release_buffer(minor_status, buf);
475 *minor_status = ENOMEM;
480 QadrtConvertE2A(t, buf->value, i, i);
481 memcpy(buf->value, t, i);
490 Curl_gss_import_name_a(OM_uint32 * minor_status, gss_buffer_t in_name,
491 gss_OID in_name_type, gss_name_t * out_name)
498 if (!in_name || !in_name->value || !in_name->length)
499 return gss_import_name(minor_status, in_name, in_name_type, out_name);
501 memcpy((char *) &in, (char *) in_name, sizeof in);
504 if (!(in.value = malloc(i + 1))) {
506 *minor_status = ENOMEM;
508 return GSS_S_FAILURE;
511 QadrtConvertA2E(in.value, in_name->value, i, i);
512 ((char *) in.value)[i] = '\0';
513 rc = gss_import_name(minor_status, &in, in_name_type, out_name);
520 Curl_gss_display_status_a(OM_uint32 * minor_status, OM_uint32 status_value,
521 int status_type, gss_OID mech_type,
522 gss_msg_ctx_t * message_context, gss_buffer_t status_string)
527 rc = gss_display_status(minor_status, status_value, status_type,
528 mech_type, message_context, status_string);
530 if (rc != GSS_S_COMPLETE || !status_string ||
531 !status_string->length || !status_string->value)
534 /* No way to allocate a buffer here, because it will be released by
535 gss_release_buffer(). The solution is to overwrite the EBCDIC buffer
536 with ASCII to return it. */
538 if (Curl_gss_convert_in_place(minor_status, status_string))
539 return GSS_S_FAILURE;
546 Curl_gss_init_sec_context_a(OM_uint32 * minor_status, gss_cred_id_t cred_handle,
547 gss_ctx_id_t * context_handle,
548 gss_name_t target_name, gss_OID mech_type,
549 gss_flags_t req_flags, OM_uint32 time_req,
550 gss_channel_bindings_t input_chan_bindings,
551 gss_buffer_t input_token,
552 gss_OID * actual_mech_type,
553 gss_buffer_t output_token, gss_flags_t * ret_flags,
554 OM_uint32 * time_rec)
564 if ((inp = input_token))
565 if (inp->length && inp->value) {
568 if (!(in.value = malloc(i + 1))) {
570 *minor_status = ENOMEM;
572 return GSS_S_FAILURE;
575 QadrtConvertA2E(in.value, input_token->value, i, i);
576 ((char *) in.value)[i] = '\0';
581 rc = gss_init_sec_context(minor_status, cred_handle, context_handle,
582 target_name, mech_type, req_flags, time_req,
583 input_chan_bindings, inp, actual_mech_type,
584 output_token, ret_flags, time_rec);
589 if (rc != GSS_S_COMPLETE || !output_token ||
590 !output_token->length || !output_token->value)
593 /* No way to allocate a buffer here, because it will be released by
594 gss_release_buffer(). The solution is to overwrite the EBCDIC buffer
595 with ASCII to return it. */
597 if (Curl_gss_convert_in_place(minor_status, output_token))
598 return GSS_S_FAILURE;
605 Curl_gss_delete_sec_context_a(OM_uint32 * minor_status,
606 gss_ctx_id_t * context_handle,
607 gss_buffer_t output_token)
612 rc = gss_delete_sec_context(minor_status, context_handle, output_token);
614 if (rc != GSS_S_COMPLETE || !output_token ||
615 !output_token->length || !output_token->value)
618 /* No way to allocate a buffer here, because it will be released by
619 gss_release_buffer(). The solution is to overwrite the EBCDIC buffer
620 with ASCII to return it. */
622 if (Curl_gss_convert_in_place(minor_status, output_token))
623 return GSS_S_FAILURE;
628 #endif /* HAVE_GSSAPI */
631 #ifndef CURL_DISABLE_LDAP
633 /* ASCII wrappers for the LDAP procedures. */
636 Curl_ldap_init_a(char * host, int port)
644 return (void *) ldap_init(host, port);
648 if (!(ehost = malloc(i + 1)))
649 return (void *) NULL;
651 QadrtConvertA2E(ehost, host, i, i);
653 result = (void *) ldap_init(ehost, port);
660 Curl_ldap_simple_bind_s_a(void * ld, char * dn, char * passwd)
668 epasswd = (char *) NULL;
673 if (!(edn = malloc(i + 1)))
674 return LDAP_NO_MEMORY;
676 QadrtConvertA2E(edn, dn, i, i);
683 if (!(epasswd = malloc(i + 1))) {
687 return LDAP_NO_MEMORY;
690 QadrtConvertA2E(epasswd, passwd, i, i);
694 i = ldap_simple_bind_s(ld, edn, epasswd);
707 Curl_ldap_search_s_a(void * ld, char * base, int scope, char * filter,
708 char * * attrs, int attrsonly, LDAPMessage * * res)
718 ebase = (char *) NULL;
719 efilter = (char *) NULL;
720 eattrs = (char * *) NULL;
721 status = LDAP_SUCCESS;
726 if (!(ebase = malloc(i + 1)))
727 status = LDAP_NO_MEMORY;
729 QadrtConvertA2E(ebase, base, i, i);
734 if (filter && status == LDAP_SUCCESS) {
737 if (!(efilter = malloc(i + 1)))
738 status = LDAP_NO_MEMORY;
740 QadrtConvertA2E(efilter, filter, i, i);
745 if (attrs && status == LDAP_SUCCESS) {
746 for (i = 0; attrs[i++];)
749 if (!(eattrs = calloc(i, sizeof *eattrs)))
750 status = LDAP_NO_MEMORY;
752 for (j = 0; attrs[j]; j++) {
753 i = strlen(attrs[j]);
755 if (!(eattrs[j] = malloc(i + 1))) {
756 status = LDAP_NO_MEMORY;
760 QadrtConvertA2E(eattrs[j], attrs[j], i, i);
766 if (status == LDAP_SUCCESS)
767 status = ldap_search_s(ld, ebase? ebase: "", scope,
768 efilter? efilter: "(objectclass=*)",
769 eattrs, attrsonly, res);
772 for (j = 0; eattrs[j]; j++)
789 Curl_ldap_get_values_len_a(void * ld, LDAPMessage * entry, const char * attr)
794 struct berval * * result;
801 if (!(cp = malloc(i + 1))) {
802 ldap_set_lderrno(ld, LDAP_NO_MEMORY, NULL,
803 ldap_err2string(LDAP_NO_MEMORY));
804 return (struct berval * *) NULL;
807 QadrtConvertA2E(cp, attr, i, i);
811 result = ldap_get_values_len(ld, entry, cp);
816 /* Result data are binary in nature, so they haven't been converted to EBCDIC.
817 Therefore do not convert. */
824 Curl_ldap_err2string_a(int error)
831 cp = ldap_err2string(error);
838 if (!(cp2 = Curl_thread_buffer(LK_LDAP_ERROR, MAX_CONV_EXPANSION * i + 1)))
841 i = QadrtConvertE2A(cp2, cp, MAX_CONV_EXPANSION * i, i);
848 Curl_ldap_get_dn_a(void * ld, LDAPMessage * entry)
855 cp = ldap_get_dn(ld, entry);
862 if (!(cp2 = malloc(i + 1)))
865 QadrtConvertE2A(cp2, cp, i, i);
867 /* No way to allocate a buffer here, because it will be released by
868 ldap_memfree() and ldap_memalloc() does not exist. The solution is to
869 overwrite the EBCDIC buffer with ASCII to return it. */
878 Curl_ldap_first_attribute_a(void * ld,
879 LDAPMessage * entry, BerElement * * berptr)
886 cp = ldap_first_attribute(ld, entry, berptr);
893 if (!(cp2 = malloc(i + 1)))
896 QadrtConvertE2A(cp2, cp, i, i);
898 /* No way to allocate a buffer here, because it will be released by
899 ldap_memfree() and ldap_memalloc() does not exist. The solution is to
900 overwrite the EBCDIC buffer with ASCII to return it. */
909 Curl_ldap_next_attribute_a(void * ld,
910 LDAPMessage * entry, BerElement * berptr)
917 cp = ldap_next_attribute(ld, entry, berptr);
924 if (!(cp2 = malloc(i + 1)))
927 QadrtConvertE2A(cp2, cp, i, i);
929 /* No way to allocate a buffer here, because it will be released by
930 ldap_memfree() and ldap_memalloc() does not exist. The solution is to
931 overwrite the EBCDIC buffer with ASCII to return it. */
938 #endif /* CURL_DISABLE_LDAP */
942 convert_sockaddr(struct sockaddr_storage * dstaddr,
943 const struct sockaddr * srcaddr, int srclen)
946 const struct sockaddr_un * srcu;
947 struct sockaddr_un * dstu;
949 unsigned int dstsize;
951 /* Convert a socket address into job CCSID, if needed. */
953 if (!srcaddr || srclen < offsetof(struct sockaddr, sa_family) +
954 sizeof srcaddr->sa_family || srclen > sizeof *dstaddr) {
959 memcpy((char *) dstaddr, (char *) srcaddr, srclen);
961 switch (srcaddr->sa_family) {
964 srcu = (const struct sockaddr_un *) srcaddr;
965 dstu = (struct sockaddr_un *) dstaddr;
966 dstsize = sizeof *dstaddr - offsetof(struct sockaddr_un, sun_path);
967 srclen -= offsetof(struct sockaddr_un, sun_path);
968 i = QadrtConvertA2E(dstu->sun_path, srcu->sun_path, dstsize - 1, srclen);
969 dstu->sun_path[i] = '\0';
970 i += offsetof(struct sockaddr_un, sun_path);
979 Curl_os400_connect(int sd, struct sockaddr * destaddr, int addrlen)
983 struct sockaddr_storage laddr;
985 i = convert_sockaddr(&laddr, destaddr, addrlen);
990 return connect(sd, (struct sockaddr *) &laddr, i);
995 Curl_os400_bind(int sd, struct sockaddr * localaddr, int addrlen)
999 struct sockaddr_storage laddr;
1001 i = convert_sockaddr(&laddr, localaddr, addrlen);
1006 return bind(sd, (struct sockaddr *) &laddr, i);
1011 Curl_os400_sendto(int sd, char * buffer, int buflen, int flags,
1012 struct sockaddr * dstaddr, int addrlen)
1016 struct sockaddr_storage laddr;
1018 i = convert_sockaddr(&laddr, dstaddr, addrlen);
1023 return sendto(sd, buffer, buflen, flags, (struct sockaddr *) &laddr, i);
1028 Curl_os400_recvfrom(int sd, char * buffer, int buflen, int flags,
1029 struct sockaddr * fromaddr, int * addrlen)
1035 const struct sockaddr_un * srcu;
1036 struct sockaddr_un * dstu;
1037 struct sockaddr_storage laddr;
1039 if (!fromaddr || !addrlen || *addrlen <= 0)
1040 return recvfrom(sd, buffer, buflen, flags, fromaddr, addrlen);
1042 laddrlen = sizeof laddr;
1043 laddr.ss_family = AF_UNSPEC; /* To detect if unused. */
1044 rcvlen = recvfrom(sd, buffer, buflen, flags,
1045 (struct sockaddr *) &laddr, &laddrlen);
1050 switch (laddr.ss_family) {
1053 srcu = (const struct sockaddr_un *) &laddr;
1054 dstu = (struct sockaddr_un *) fromaddr;
1055 i = *addrlen - offsetof(struct sockaddr_un, sun_path);
1056 laddrlen -= offsetof(struct sockaddr_un, sun_path);
1057 i = QadrtConvertE2A(dstu->sun_path, srcu->sun_path, i, laddrlen);
1058 laddrlen = i + offsetof(struct sockaddr_un, sun_path);
1060 if (laddrlen < *addrlen)
1061 dstu->sun_path[i] = '\0';
1069 if (laddrlen > *addrlen)
1070 laddrlen = *addrlen;
1073 memcpy((char *) fromaddr, (char *) &laddr, laddrlen);
1078 *addrlen = laddrlen;