1
0
mirror of https://github.com/moparisthebest/curl synced 2024-10-31 23:55:01 -04:00
curl/packages/OS400/os400sys.c

1417 lines
30 KiB
C
Raw Normal View History

2007-08-23 10:30:24 -04:00
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
2014-10-13 10:33:47 -04:00
* Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
2007-08-23 10:30:24 -04:00
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at http://curl.haxx.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
*
***************************************************************************/
/* OS/400 additional support. */
#include "curlbuild.h"
build: fix circular header inclusion with other packages This commit renames lib/setup.h to lib/curl_setup.h and renames lib/setup_once.h to lib/curl_setup_once.h. Removes the need and usage of a header inclusion guard foreign to libcurl. [1] Removes the need and presence of an alarming notice we carried in old setup_once.h [2] ---------------------------------------- 1 - lib/setup_once.h used __SETUP_ONCE_H macro as header inclusion guard up to commit ec691ca3 which changed this to HEADER_CURL_SETUP_ONCE_H, this single inclusion guard is enough to ensure that inclusion of lib/setup_once.h done from lib/setup.h is only done once. Additionally lib/setup.h has always used __SETUP_ONCE_H macro to protect inclusion of setup_once.h even after commit ec691ca3, this was to avoid a circular header inclusion triggered when building a c-ares enabled version with c-ares sources available which also has a setup_once.h header. Commit ec691ca3 exposes the real nature of __SETUP_ONCE_H usage in lib/setup.h, it is a header inclusion guard foreign to libcurl belonging to c-ares's setup_once.h The renaming this commit does, fixes the circular header inclusion, and as such removes the need and usage of a header inclusion guard foreign to libcurl. Macro __SETUP_ONCE_H no longer used in libcurl. 2 - Due to the circular interdependency of old lib/setup_once.h and the c-ares setup_once.h header, old file lib/setup_once.h has carried back from 2006 up to now days an alarming and prominent notice about the need of keeping libcurl's and c-ares's setup_once.h in sync. Given that this commit fixes the circular interdependency, the need and presence of mentioned notice is removed. All mentioned interdependencies come back from now old days when the c-ares project lived inside a curl subdirectory. This commit removes last traces of such fact.
2013-01-06 13:06:49 -05:00
#include "config-os400.h" /* Not curl_setup.h: we only need some defines. */
2007-08-23 10:30:24 -04:00
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
2007-08-23 10:30:24 -04:00
#include <stdlib.h>
#include <stddef.h>
2007-08-23 10:30:24 -04:00
#include <string.h>
#include <pthread.h>
#include <netdb.h>
#include <qadrt.h>
#include <errno.h>
#ifdef HAVE_ZLIB_H
#include <zlib.h>
#endif
2013-07-15 13:00:36 -04:00
#ifdef USE_GSKIT
#include <gskssl.h>
#include <qsoasync.h>
#endif
#ifdef HAVE_GSSAPI
2007-08-23 10:30:24 -04:00
#include <gssapi.h>
#endif
#ifndef CURL_DISABLE_LDAP
2007-08-23 10:30:24 -04:00
#include <ldap.h>
#endif
2007-08-23 10:30:24 -04:00
#include <netinet/in.h>
#include <arpa/inet.h>
#include "os400sys.h"
/**
*** QADRT OS/400 ASCII runtime defines only the most used procedures, but
*** but a lot of them are not supported. This module implements
*** ASCII wrappers for those that are used by libcurl, but not
*** defined by QADRT.
**/
#pragma convert(0) /* Restore EBCDIC. */
#define MIN_BYTE_GAIN 1024 /* Minimum gain when shortening a buffer. */
typedef struct {
unsigned long size; /* Buffer size. */
char * buf; /* Buffer address. */
} buffer_t;
static char * buffer_undef(localkey_t key, long size);
static char * buffer_threaded(localkey_t key, long size);
static char * buffer_unthreaded(localkey_t key, long size);
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_key_t thdkey;
static buffer_t * locbufs;
char * (* Curl_thread_buffer)(localkey_t key, long size) = buffer_undef;
static void
thdbufdestroy(void * private)
{
2013-10-28 07:00:22 -04:00
if(private) {
buffer_t * p = (buffer_t *) private;
localkey_t i;
2007-08-23 10:30:24 -04:00
2013-10-28 07:00:22 -04:00
for(i = (localkey_t) 0; i < LK_LAST; i++) {
free(p->buf);
2007-08-23 10:30:24 -04:00
p++;
}
free(private);
}
}
static void
terminate(void)
{
2013-10-28 07:00:22 -04:00
if(Curl_thread_buffer == buffer_threaded) {
2007-08-23 10:30:24 -04:00
locbufs = pthread_getspecific(thdkey);
pthread_setspecific(thdkey, (void *) NULL);
pthread_key_delete(thdkey);
}
2013-10-28 07:00:22 -04:00
if(Curl_thread_buffer != buffer_undef) {
2007-08-23 10:30:24 -04:00
thdbufdestroy((void *) locbufs);
locbufs = (buffer_t *) NULL;
}
Curl_thread_buffer = buffer_undef;
}
static char *
get_buffer(buffer_t * buf, long size)
{
char * cp;
/* If `size' >= 0, make sure buffer at `buf' is at least `size'-byte long.
Return the buffer address. */
2013-10-28 07:00:22 -04:00
if(size < 0)
2007-08-23 10:30:24 -04:00
return buf->buf;
2013-10-28 07:00:22 -04:00
if(!buf->buf) {
if((buf->buf = malloc(size)))
2007-08-23 10:30:24 -04:00
buf->size = size;
return buf->buf;
}
2013-10-28 07:00:22 -04:00
if((unsigned long) size <= buf->size) {
2007-08-23 10:30:24 -04:00
/* Shorten the buffer only if it frees a significant byte count. This
avoids some realloc() overhead. */
2013-10-28 07:00:22 -04:00
if(buf->size - size < MIN_BYTE_GAIN)
2007-08-23 10:30:24 -04:00
return buf->buf;
}
/* Resize the buffer. */
2013-10-28 07:00:22 -04:00
if((cp = realloc(buf->buf, size))) {
2007-08-23 10:30:24 -04:00
buf->buf = cp;
buf->size = size;
}
2013-10-28 07:00:22 -04:00
else if(size <= buf->size)
2007-08-23 10:30:24 -04:00
cp = buf->buf;
return cp;
}
static char *
buffer_unthreaded(localkey_t key, long size)
{
return get_buffer(locbufs + key, size);
}
static char *
buffer_threaded(localkey_t key, long size)
{
buffer_t * bufs;
/* Get the buffer for the given local key in the current thread, and
make sure it is at least `size'-byte long. Set `size' to < 0 to get
its address only. */
bufs = (buffer_t *) pthread_getspecific(thdkey);
2013-10-28 07:00:22 -04:00
if(!bufs) {
if(size < 0)
2007-08-23 10:30:24 -04:00
return (char *) NULL; /* No buffer yet. */
/* Allocate buffer descriptors for the current thread. */
2013-10-28 07:00:22 -04:00
if(!(bufs = calloc((size_t) LK_LAST, sizeof *bufs)))
2007-08-23 10:30:24 -04:00
return (char *) NULL;
2013-10-28 07:00:22 -04:00
if(pthread_setspecific(thdkey, (void *) bufs)) {
2007-08-23 10:30:24 -04:00
free(bufs);
return (char *) NULL;
}
}
return get_buffer(bufs + key, size);
}
static char *
buffer_undef(localkey_t key, long size)
{
/* Define the buffer system, get the buffer for the given local key in
the current thread, and make sure it is at least `size'-byte long.
Set `size' to < 0 to get its address only. */
pthread_mutex_lock(&mutex);
/* Determine if we can use pthread-specific data. */
2013-10-28 07:00:22 -04:00
if(Curl_thread_buffer == buffer_undef) { /* If unchanged during lock. */
if(!pthread_key_create(&thdkey, thdbufdestroy))
2007-08-23 10:30:24 -04:00
Curl_thread_buffer = buffer_threaded;
2013-10-28 07:00:22 -04:00
else if(!(locbufs = calloc((size_t) LK_LAST, sizeof *locbufs))) {
2007-08-23 10:30:24 -04:00
pthread_mutex_unlock(&mutex);
return (char *) NULL;
}
else
Curl_thread_buffer = buffer_unthreaded;
atexit(terminate);
}
pthread_mutex_unlock(&mutex);
return Curl_thread_buffer(key, size);
}
static char *
set_thread_string(localkey_t key, const char * s)
{
int i;
char * cp;
if(!s)
return (char *) NULL;
i = strlen(s) + 1;
cp = Curl_thread_buffer(key, MAX_CONV_EXPANSION * i + 1);
if(cp) {
i = QadrtConvertE2A(cp, s, MAX_CONV_EXPANSION * i, i);
cp[i] = '\0';
}
return cp;
}
2007-08-23 10:30:24 -04:00
int
Curl_getnameinfo_a(const struct sockaddr * sa, curl_socklen_t salen,
char * nodename, curl_socklen_t nodenamelen,
char * servname, curl_socklen_t servnamelen,
2007-08-23 10:30:24 -04:00
int flags)
{
char * enodename;
char * eservname;
int status;
int i;
enodename = (char *) NULL;
eservname = (char *) NULL;
2013-10-28 07:00:22 -04:00
if(nodename && nodenamelen)
if(!(enodename = malloc(nodenamelen)))
2007-08-23 10:30:24 -04:00
return EAI_MEMORY;
2013-10-28 07:00:22 -04:00
if(servname && servnamelen)
if(!(eservname = malloc(servnamelen))) {
free(enodename);
2007-08-23 10:30:24 -04:00
return EAI_MEMORY;
}
status = getnameinfo(sa, salen, enodename, nodenamelen,
eservname, servnamelen, flags);
2013-10-28 07:00:22 -04:00
if(!status) {
if(enodename) {
2007-08-23 10:30:24 -04:00
i = QadrtConvertE2A(nodename, enodename,
nodenamelen - 1, strlen(enodename));
nodename[i] = '\0';
}
2013-10-28 07:00:22 -04:00
if(eservname) {
2007-08-23 10:30:24 -04:00
i = QadrtConvertE2A(servname, eservname,
servnamelen - 1, strlen(eservname));
servname[i] = '\0';
}
}
free(enodename);
free(eservname);
2007-08-23 10:30:24 -04:00
return status;
}
int
Curl_getaddrinfo_a(const char * nodename, const char * servname,
const struct addrinfo * hints,
struct addrinfo * * res)
{
char * enodename;
char * eservname;
int status;
int i;
enodename = (char *) NULL;
eservname = (char *) NULL;
2013-10-28 07:00:22 -04:00
if(nodename) {
2007-08-23 10:30:24 -04:00
i = strlen(nodename);
2013-10-28 07:00:22 -04:00
if(!(enodename = malloc(i + 1)))
2007-08-23 10:30:24 -04:00
return EAI_MEMORY;
i = QadrtConvertA2E(enodename, nodename, i, i);
enodename[i] = '\0';
}
2013-10-28 07:00:22 -04:00
if(servname) {
2007-08-23 10:30:24 -04:00
i = strlen(servname);
2013-10-28 07:00:22 -04:00
if(!(eservname = malloc(i + 1))) {
free(enodename);
2007-08-23 10:30:24 -04:00
return EAI_MEMORY;
}
QadrtConvertA2E(eservname, servname, i, i);
eservname[i] = '\0';
}
status = getaddrinfo(enodename, eservname, hints, res);
free(enodename);
free(eservname);
2007-08-23 10:30:24 -04:00
return status;
}
2013-07-15 13:00:36 -04:00
#ifdef USE_GSKIT
/* ASCII wrappers for the GSKit procedures. */
/*
* EBCDIC --> ASCII string mapping table.
* Some strings returned by GSKit are dynamically allocated and automatically
* released when closing the handle.
* To provide the same functionality, we use a "private" handle that
* holds the GSKit handle and a list of string mappings. This will allow
* avoid conversion of already converted strings and releasing them upon
* close time.
*/
struct gskstrlist {
struct gskstrlist * next;
const char * ebcdicstr;
const char * asciistr;
};
struct Curl_gsk_descriptor {
gsk_handle h;
struct gskstrlist * strlist;
};
int
Curl_gsk_environment_open(gsk_handle * my_env_handle)
{
struct Curl_gsk_descriptor * p;
gsk_handle h;
int rc;
if(!my_env_handle)
return GSK_OS400_ERROR_INVALID_POINTER;
if(!(p = (struct Curl_gsk_descriptor *) malloc(sizeof *p)))
return GSK_INSUFFICIENT_STORAGE;
p->strlist = (struct gskstrlist *) NULL;
if((rc = gsk_environment_open(&p->h)) != GSK_OK)
free(p);
else
*my_env_handle = (gsk_handle) p;
return rc;
}
int
Curl_gsk_secure_soc_open(gsk_handle my_env_handle,
gsk_handle * my_session_handle)
{
struct Curl_gsk_descriptor * p;
gsk_handle h;
int rc;
if(!my_env_handle)
return GSK_INVALID_HANDLE;
if(!my_session_handle)
return GSK_OS400_ERROR_INVALID_POINTER;
h = ((struct Curl_gsk_descriptor *) my_env_handle)->h;
if(!(p = (struct Curl_gsk_descriptor *) malloc(sizeof *p)))
return GSK_INSUFFICIENT_STORAGE;
p->strlist = (struct gskstrlist *) NULL;
if((rc = gsk_secure_soc_open(h, &p->h)) != GSK_OK)
free(p);
else
*my_session_handle = (gsk_handle) p;
return rc;
}
static void
gsk_free_handle(struct Curl_gsk_descriptor * p)
{
struct gskstrlist * q;
2013-10-28 07:00:22 -04:00
while((q = p->strlist)) {
2013-07-15 13:00:36 -04:00
p->strlist = q;
free((void *) q->asciistr);
free(q);
}
free(p);
}
int
Curl_gsk_environment_close(gsk_handle * my_env_handle)
{
struct Curl_gsk_descriptor * p;
int rc;
if(!my_env_handle)
return GSK_OS400_ERROR_INVALID_POINTER;
if(!*my_env_handle)
return GSK_INVALID_HANDLE;
p = (struct Curl_gsk_descriptor *) *my_env_handle;
2013-10-28 07:00:22 -04:00
if((rc = gsk_environment_close(&p->h)) == GSK_OK) {
2013-07-15 13:00:36 -04:00
gsk_free_handle(p);
*my_env_handle = (gsk_handle) NULL;
}
return rc;
}
int
Curl_gsk_secure_soc_close(gsk_handle * my_session_handle)
{
struct Curl_gsk_descriptor * p;
int rc;
if(!my_session_handle)
return GSK_OS400_ERROR_INVALID_POINTER;
if(!*my_session_handle)
return GSK_INVALID_HANDLE;
p = (struct Curl_gsk_descriptor *) *my_session_handle;
2013-10-28 07:00:22 -04:00
if((rc = gsk_secure_soc_close(&p->h)) == GSK_OK) {
2013-07-15 13:00:36 -04:00
gsk_free_handle(p);
*my_session_handle = (gsk_handle) NULL;
}
return rc;
}
int
Curl_gsk_environment_init(gsk_handle my_env_handle)
{
struct Curl_gsk_descriptor * p;
if(!my_env_handle)
return GSK_INVALID_HANDLE;
p = (struct Curl_gsk_descriptor *) my_env_handle;
return gsk_environment_init(p->h);
}
int
Curl_gsk_secure_soc_init(gsk_handle my_session_handle)
{
struct Curl_gsk_descriptor * p;
if(!my_session_handle)
return GSK_INVALID_HANDLE;
p = (struct Curl_gsk_descriptor *) my_session_handle;
return gsk_secure_soc_init(p->h);
}
int
Curl_gsk_attribute_set_buffer_a(gsk_handle my_gsk_handle, GSK_BUF_ID bufID,
const char * buffer, int bufSize)
{
struct Curl_gsk_descriptor * p;
char * ebcdicbuf;
int rc;
if(!my_gsk_handle)
return GSK_INVALID_HANDLE;
if(!buffer)
return GSK_OS400_ERROR_INVALID_POINTER;
if(bufSize < 0)
return GSK_ATTRIBUTE_INVALID_LENGTH;
p = (struct Curl_gsk_descriptor *) my_gsk_handle;
if(!bufSize)
bufSize = strlen(buffer);
2013-10-28 07:00:22 -04:00
if(!(ebcdicbuf = malloc(bufSize + 1)))
2013-07-15 13:00:36 -04:00
return GSK_INSUFFICIENT_STORAGE;
QadrtConvertA2E(ebcdicbuf, buffer, bufSize, bufSize);
ebcdicbuf[bufSize] = '\0';
rc = gsk_attribute_set_buffer(p->h, bufID, ebcdicbuf, bufSize);
free(ebcdicbuf);
return rc;
}
int
Curl_gsk_attribute_set_enum(gsk_handle my_gsk_handle, GSK_ENUM_ID enumID,
GSK_ENUM_VALUE enumValue)
{
struct Curl_gsk_descriptor * p;
if(!my_gsk_handle)
return GSK_INVALID_HANDLE;
p = (struct Curl_gsk_descriptor *) my_gsk_handle;
return gsk_attribute_set_enum(p->h, enumID, enumValue);
}
int
Curl_gsk_attribute_set_numeric_value(gsk_handle my_gsk_handle,
GSK_NUM_ID numID, int numValue)
{
struct Curl_gsk_descriptor * p;
if(!my_gsk_handle)
return GSK_INVALID_HANDLE;
p = (struct Curl_gsk_descriptor *) my_gsk_handle;
return gsk_attribute_set_numeric_value(p->h, numID, numValue);
}
int
Curl_gsk_attribute_set_callback(gsk_handle my_gsk_handle,
GSK_CALLBACK_ID callBackID,
void * callBackAreaPtr)
{
struct Curl_gsk_descriptor * p;
if(!my_gsk_handle)
return GSK_INVALID_HANDLE;
p = (struct Curl_gsk_descriptor *) my_gsk_handle;
return gsk_attribute_set_callback(p->h, callBackID, callBackAreaPtr);
}
static int
cachestring(struct Curl_gsk_descriptor * p,
const char * ebcdicbuf, int bufsize, const char * * buffer)
{
int rc;
char * asciibuf;
struct gskstrlist * sp;
2013-10-28 07:00:22 -04:00
for(sp = p->strlist; sp; sp = sp->next)
2013-07-15 13:00:36 -04:00
if(sp->ebcdicstr == ebcdicbuf)
break;
if(!sp) {
if(!(sp = (struct gskstrlist *) malloc(sizeof *sp)))
return GSK_INSUFFICIENT_STORAGE;
if(!(asciibuf = malloc(bufsize + 1))) {
free(sp);
return GSK_INSUFFICIENT_STORAGE;
}
QadrtConvertE2A(asciibuf, ebcdicbuf, bufsize, bufsize);
asciibuf[bufsize] = '\0';
sp->ebcdicstr = ebcdicbuf;
sp->asciistr = asciibuf;
sp->next = p->strlist;
p->strlist = sp;
}
*buffer = sp->asciistr;
return GSK_OK;
}
int
Curl_gsk_attribute_get_buffer_a(gsk_handle my_gsk_handle, GSK_BUF_ID bufID,
const char * * buffer, int * bufSize)
{
struct Curl_gsk_descriptor * p;
int rc;
const char * mybuf;
int mylen;
if(!my_gsk_handle)
return GSK_INVALID_HANDLE;
if(!buffer || !bufSize)
return GSK_OS400_ERROR_INVALID_POINTER;
p = (struct Curl_gsk_descriptor *) my_gsk_handle;
2013-10-28 07:00:22 -04:00
if((rc = gsk_attribute_get_buffer(p->h, bufID, &mybuf, &mylen)) != GSK_OK)
2013-07-15 13:00:36 -04:00
return rc;
if((rc = cachestring(p, mybuf, mylen, buffer)) == GSK_OK)
*bufSize = mylen;
return rc;
}
int
Curl_gsk_attribute_get_enum(gsk_handle my_gsk_handle, GSK_ENUM_ID enumID,
GSK_ENUM_VALUE * enumValue)
{
struct Curl_gsk_descriptor * p;
if(!my_gsk_handle)
return GSK_INVALID_HANDLE;
p = (struct Curl_gsk_descriptor *) my_gsk_handle;
return gsk_attribute_get_enum(p->h, enumID, enumValue);
}
int
Curl_gsk_attribute_get_numeric_value(gsk_handle my_gsk_handle,
GSK_NUM_ID numID, int * numValue)
{
struct Curl_gsk_descriptor * p;
if(!my_gsk_handle)
return GSK_INVALID_HANDLE;
p = (struct Curl_gsk_descriptor *) my_gsk_handle;
return gsk_attribute_get_numeric_value(p->h, numID, numValue);
}
int
Curl_gsk_attribute_get_cert_info(gsk_handle my_gsk_handle,
GSK_CERT_ID certID,
const gsk_cert_data_elem * * certDataElem,
int * certDataElementCount)
{
struct Curl_gsk_descriptor * p;
if(!my_gsk_handle)
return GSK_INVALID_HANDLE;
p = (struct Curl_gsk_descriptor *) my_gsk_handle;
/* No need to convert code: text results are already in ASCII. */
return gsk_attribute_get_cert_info(p->h, certID,
certDataElem, certDataElementCount);
}
int
Curl_gsk_secure_soc_misc(gsk_handle my_session_handle, GSK_MISC_ID miscID)
{
struct Curl_gsk_descriptor * p;
if(!my_session_handle)
return GSK_INVALID_HANDLE;
p = (struct Curl_gsk_descriptor *) my_session_handle;
return gsk_secure_soc_misc(p->h, miscID);
}
int
Curl_gsk_secure_soc_read(gsk_handle my_session_handle, char * readBuffer,
int readBufSize, int * amtRead)
{
struct Curl_gsk_descriptor * p;
if(!my_session_handle)
return GSK_INVALID_HANDLE;
p = (struct Curl_gsk_descriptor *) my_session_handle;
return gsk_secure_soc_read(p->h, readBuffer, readBufSize, amtRead);
}
int
Curl_gsk_secure_soc_write(gsk_handle my_session_handle, char * writeBuffer,
int writeBufSize, int * amtWritten)
{
struct Curl_gsk_descriptor * p;
if(!my_session_handle)
return GSK_INVALID_HANDLE;
p = (struct Curl_gsk_descriptor *) my_session_handle;
return gsk_secure_soc_write(p->h, writeBuffer, writeBufSize, amtWritten);
}
const char *
Curl_gsk_strerror_a(int gsk_return_value)
{
return set_thread_string(LK_GSK_ERROR, gsk_strerror(gsk_return_value));
2013-07-15 13:00:36 -04:00
}
int
Curl_gsk_secure_soc_startInit(gsk_handle my_session_handle,
int IOCompletionPort,
Qso_OverlappedIO_t * communicationsArea)
{
struct Curl_gsk_descriptor * p;
if(!my_session_handle)
return GSK_INVALID_HANDLE;
p = (struct Curl_gsk_descriptor *) my_session_handle;
return gsk_secure_soc_startInit(p->h, IOCompletionPort, communicationsArea);
}
#endif /* USE_GSKIT */
2007-08-23 10:30:24 -04:00
#ifdef HAVE_GSSAPI
/* ASCII wrappers for the GSSAPI procedures. */
static int
Curl_gss_convert_in_place(OM_uint32 * minor_status, gss_buffer_t buf)
{
unsigned int i;
char * t;
/* Convert `buf' in place, from EBCDIC to ASCII.
If error, release the buffer and return -1. Else return 0. */
i = buf->length;
2013-10-28 07:00:22 -04:00
if(i) {
if(!(t = malloc(i))) {
2007-08-23 10:30:24 -04:00
gss_release_buffer(minor_status, buf);
2013-10-28 07:00:22 -04:00
if(minor_status)
2007-08-23 10:30:24 -04:00
*minor_status = ENOMEM;
return -1;
}
QadrtConvertE2A(t, buf->value, i, i);
memcpy(buf->value, t, i);
free(t);
}
return 0;
}
OM_uint32
Curl_gss_import_name_a(OM_uint32 * minor_status, gss_buffer_t in_name,
gss_OID in_name_type, gss_name_t * out_name)
{
int rc;
unsigned int i;
gss_buffer_desc in;
2013-10-28 07:00:22 -04:00
if(!in_name || !in_name->value || !in_name->length)
2007-08-23 10:30:24 -04:00
return gss_import_name(minor_status, in_name, in_name_type, out_name);
memcpy((char *) &in, (char *) in_name, sizeof in);
i = in.length;
2013-10-28 07:00:22 -04:00
if(!(in.value = malloc(i + 1))) {
if(minor_status)
2007-08-23 10:30:24 -04:00
*minor_status = ENOMEM;
return GSS_S_FAILURE;
}
QadrtConvertA2E(in.value, in_name->value, i, i);
((char *) in.value)[i] = '\0';
rc = gss_import_name(minor_status, &in, in_name_type, out_name);
free(in.value);
return rc;
}
OM_uint32
Curl_gss_display_status_a(OM_uint32 * minor_status, OM_uint32 status_value,
int status_type, gss_OID mech_type,
gss_msg_ctx_t * message_context, gss_buffer_t status_string)
{
int rc;
rc = gss_display_status(minor_status, status_value, status_type,
mech_type, message_context, status_string);
2013-10-28 07:00:22 -04:00
if(rc != GSS_S_COMPLETE || !status_string ||
!status_string->length || !status_string->value)
2007-08-23 10:30:24 -04:00
return rc;
/* No way to allocate a buffer here, because it will be released by
gss_release_buffer(). The solution is to overwrite the EBCDIC buffer
with ASCII to return it. */
2013-10-28 07:00:22 -04:00
if(Curl_gss_convert_in_place(minor_status, status_string))
2007-08-23 10:30:24 -04:00
return GSS_S_FAILURE;
return rc;
}
OM_uint32
2013-10-28 07:00:22 -04:00
Curl_gss_init_sec_context_a(OM_uint32 * minor_status,
gss_cred_id_t cred_handle,
2007-08-23 10:30:24 -04:00
gss_ctx_id_t * context_handle,
gss_name_t target_name, gss_OID mech_type,
gss_flags_t req_flags, OM_uint32 time_req,
gss_channel_bindings_t input_chan_bindings,
gss_buffer_t input_token,
gss_OID * actual_mech_type,
gss_buffer_t output_token, gss_flags_t * ret_flags,
OM_uint32 * time_rec)
{
int rc;
unsigned int i;
gss_buffer_desc in;
gss_buffer_t inp;
in.value = NULL;
2013-10-28 07:00:22 -04:00
if((inp = input_token))
if(inp->length && inp->value) {
2007-08-23 10:30:24 -04:00
i = inp->length;
2013-10-28 07:00:22 -04:00
if(!(in.value = malloc(i + 1))) {
if(minor_status)
2007-08-23 10:30:24 -04:00
*minor_status = ENOMEM;
return GSS_S_FAILURE;
}
QadrtConvertA2E(in.value, input_token->value, i, i);
((char *) in.value)[i] = '\0';
in.length = i;
inp = &in;
}
rc = gss_init_sec_context(minor_status, cred_handle, context_handle,
target_name, mech_type, req_flags, time_req,
input_chan_bindings, inp, actual_mech_type,
output_token, ret_flags, time_rec);
free(in.value);
2007-08-23 10:30:24 -04:00
2013-10-28 07:00:22 -04:00
if(rc != GSS_S_COMPLETE || !output_token ||
2007-08-23 10:30:24 -04:00
!output_token->length || !output_token->value)
return rc;
/* No way to allocate a buffer here, because it will be released by
gss_release_buffer(). The solution is to overwrite the EBCDIC buffer
with ASCII to return it. */
2013-10-28 07:00:22 -04:00
if(Curl_gss_convert_in_place(minor_status, output_token))
2007-08-23 10:30:24 -04:00
return GSS_S_FAILURE;
return rc;
}
OM_uint32
Curl_gss_delete_sec_context_a(OM_uint32 * minor_status,
gss_ctx_id_t * context_handle,
gss_buffer_t output_token)
{
int rc;
rc = gss_delete_sec_context(minor_status, context_handle, output_token);
2013-10-28 07:00:22 -04:00
if(rc != GSS_S_COMPLETE || !output_token ||
2007-08-23 10:30:24 -04:00
!output_token->length || !output_token->value)
return rc;
/* No way to allocate a buffer here, because it will be released by
gss_release_buffer(). The solution is to overwrite the EBCDIC buffer
with ASCII to return it. */
2013-10-28 07:00:22 -04:00
if(Curl_gss_convert_in_place(minor_status, output_token))
2007-08-23 10:30:24 -04:00
return GSS_S_FAILURE;
return rc;
}
#endif /* HAVE_GSSAPI */
#ifndef CURL_DISABLE_LDAP
/* ASCII wrappers for the LDAP procedures. */
void *
Curl_ldap_init_a(char * host, int port)
{
unsigned int i;
char * ehost;
void * result;
2013-10-28 07:00:22 -04:00
if(!host)
2007-08-23 10:30:24 -04:00
return (void *) ldap_init(host, port);
i = strlen(host);
2013-10-28 07:00:22 -04:00
if(!(ehost = malloc(i + 1)))
2007-08-23 10:30:24 -04:00
return (void *) NULL;
QadrtConvertA2E(ehost, host, i, i);
ehost[i] = '\0';
result = (void *) ldap_init(ehost, port);
free(ehost);
return result;
}
int
Curl_ldap_simple_bind_s_a(void * ld, char * dn, char * passwd)
{
int i;
char * edn;
char * epasswd;
edn = (char *) NULL;
epasswd = (char *) NULL;
2013-10-28 07:00:22 -04:00
if(dn) {
2007-08-23 10:30:24 -04:00
i = strlen(dn);
2013-10-28 07:00:22 -04:00
if(!(edn = malloc(i + 1)))
2007-08-23 10:30:24 -04:00
return LDAP_NO_MEMORY;
QadrtConvertA2E(edn, dn, i, i);
edn[i] = '\0';
}
2013-10-28 07:00:22 -04:00
if(passwd) {
2007-08-23 10:30:24 -04:00
i = strlen(passwd);
2013-10-28 07:00:22 -04:00
if(!(epasswd = malloc(i + 1))) {
free(edn);
2007-08-23 10:30:24 -04:00
return LDAP_NO_MEMORY;
}
QadrtConvertA2E(epasswd, passwd, i, i);
epasswd[i] = '\0';
}
i = ldap_simple_bind_s(ld, edn, epasswd);
free(epasswd);
free(edn);
2007-08-23 10:30:24 -04:00
return i;
}
int
Curl_ldap_search_s_a(void * ld, char * base, int scope, char * filter,
char * * attrs, int attrsonly, LDAPMessage * * res)
{
int i;
int j;
char * ebase;
char * efilter;
char * * eattrs;
int status;
ebase = (char *) NULL;
efilter = (char *) NULL;
eattrs = (char * *) NULL;
status = LDAP_SUCCESS;
2013-10-28 07:00:22 -04:00
if(base) {
2007-08-23 10:30:24 -04:00
i = strlen(base);
2013-10-28 07:00:22 -04:00
if(!(ebase = malloc(i + 1)))
2007-08-23 10:30:24 -04:00
status = LDAP_NO_MEMORY;
else {
QadrtConvertA2E(ebase, base, i, i);
ebase[i] = '\0';
}
}
2013-10-28 07:00:22 -04:00
if(filter && status == LDAP_SUCCESS) {
2007-08-23 10:30:24 -04:00
i = strlen(filter);
2013-10-28 07:00:22 -04:00
if(!(efilter = malloc(i + 1)))
2007-08-23 10:30:24 -04:00
status = LDAP_NO_MEMORY;
else {
QadrtConvertA2E(efilter, filter, i, i);
efilter[i] = '\0';
}
}
2013-10-28 07:00:22 -04:00
if(attrs && status == LDAP_SUCCESS) {
for(i = 0; attrs[i++];)
2007-08-23 10:30:24 -04:00
;
2013-10-28 07:00:22 -04:00
if(!(eattrs = calloc(i, sizeof *eattrs)))
2007-08-23 10:30:24 -04:00
status = LDAP_NO_MEMORY;
else {
2013-10-28 07:00:22 -04:00
for(j = 0; attrs[j]; j++) {
2007-08-23 10:30:24 -04:00
i = strlen(attrs[j]);
2013-10-28 07:00:22 -04:00
if(!(eattrs[j] = malloc(i + 1))) {
2007-08-23 10:30:24 -04:00
status = LDAP_NO_MEMORY;
break;
}
QadrtConvertA2E(eattrs[j], attrs[j], i, i);
eattrs[j][i] = '\0';
}
}
}
2013-10-28 07:00:22 -04:00
if(status == LDAP_SUCCESS)
2007-08-23 10:30:24 -04:00
status = ldap_search_s(ld, ebase? ebase: "", scope,
efilter? efilter: "(objectclass=*)",
eattrs, attrsonly, res);
2013-10-28 07:00:22 -04:00
if(eattrs) {
for(j = 0; eattrs[j]; j++)
2007-08-23 10:30:24 -04:00
free(eattrs[j]);
free(eattrs);
}
free(efilter);
free(ebase);
2007-08-23 10:30:24 -04:00
return status;
}
struct berval * *
Curl_ldap_get_values_len_a(void * ld, LDAPMessage * entry, const char * attr)
{
char * cp;
struct berval * * result;
cp = (char *) NULL;
2013-10-28 07:00:22 -04:00
if(attr) {
int i = strlen(attr);
2007-08-23 10:30:24 -04:00
2013-10-28 07:00:22 -04:00
if(!(cp = malloc(i + 1))) {
2007-08-23 10:30:24 -04:00
ldap_set_lderrno(ld, LDAP_NO_MEMORY, NULL,
ldap_err2string(LDAP_NO_MEMORY));
return (struct berval * *) NULL;
}
QadrtConvertA2E(cp, attr, i, i);
cp[i] = '\0';
}
result = ldap_get_values_len(ld, entry, cp);
free(cp);
2007-08-23 10:30:24 -04:00
2013-10-28 07:00:22 -04:00
/* Result data are binary in nature, so they haven't been
converted to EBCDIC. Therefore do not convert. */
2007-08-23 10:30:24 -04:00
return result;
}
char *
Curl_ldap_err2string_a(int error)
{
return set_thread_string(LK_LDAP_ERROR, ldap_err2string(error));
2007-08-23 10:30:24 -04:00
}
char *
Curl_ldap_get_dn_a(void * ld, LDAPMessage * entry)
{
int i;
char * cp;
char * cp2;
cp = ldap_get_dn(ld, entry);
2013-10-28 07:00:22 -04:00
if(!cp)
2007-08-23 10:30:24 -04:00
return cp;
i = strlen(cp);
2013-10-28 07:00:22 -04:00
if(!(cp2 = malloc(i + 1)))
2007-08-23 10:30:24 -04:00
return cp2;
QadrtConvertE2A(cp2, cp, i, i);
cp2[i] = '\0';
2007-08-23 10:30:24 -04:00
/* No way to allocate a buffer here, because it will be released by
ldap_memfree() and ldap_memalloc() does not exist. The solution is to
overwrite the EBCDIC buffer with ASCII to return it. */
strcpy(cp, cp2);
free(cp2);
return cp;
}
char *
Curl_ldap_first_attribute_a(void * ld,
LDAPMessage * entry, BerElement * * berptr)
{
int i;
char * cp;
char * cp2;
cp = ldap_first_attribute(ld, entry, berptr);
2013-10-28 07:00:22 -04:00
if(!cp)
2007-08-23 10:30:24 -04:00
return cp;
i = strlen(cp);
2013-10-28 07:00:22 -04:00
if(!(cp2 = malloc(i + 1)))
2007-08-23 10:30:24 -04:00
return cp2;
QadrtConvertE2A(cp2, cp, i, i);
cp2[i] = '\0';
2007-08-23 10:30:24 -04:00
/* No way to allocate a buffer here, because it will be released by
ldap_memfree() and ldap_memalloc() does not exist. The solution is to
overwrite the EBCDIC buffer with ASCII to return it. */
strcpy(cp, cp2);
free(cp2);
return cp;
}
char *
Curl_ldap_next_attribute_a(void * ld,
LDAPMessage * entry, BerElement * berptr)
{
int i;
char * cp;
char * cp2;
cp = ldap_next_attribute(ld, entry, berptr);
2013-10-28 07:00:22 -04:00
if(!cp)
2007-08-23 10:30:24 -04:00
return cp;
i = strlen(cp);
2013-10-28 07:00:22 -04:00
if(!(cp2 = malloc(i + 1)))
2007-08-23 10:30:24 -04:00
return cp2;
QadrtConvertE2A(cp2, cp, i, i);
cp2[i] = '\0';
2007-08-23 10:30:24 -04:00
/* No way to allocate a buffer here, because it will be released by
ldap_memfree() and ldap_memalloc() does not exist. The solution is to
overwrite the EBCDIC buffer with ASCII to return it. */
strcpy(cp, cp2);
free(cp2);
return cp;
}
#endif /* CURL_DISABLE_LDAP */
static int
convert_sockaddr(struct sockaddr_storage * dstaddr,
const struct sockaddr * srcaddr, int srclen)
{
const struct sockaddr_un * srcu;
struct sockaddr_un * dstu;
unsigned int i;
unsigned int dstsize;
/* Convert a socket address into job CCSID, if needed. */
2013-10-28 07:00:22 -04:00
if(!srcaddr || srclen < offsetof(struct sockaddr, sa_family) +
sizeof srcaddr->sa_family || srclen > sizeof *dstaddr) {
errno = EINVAL;
return -1;
}
memcpy((char *) dstaddr, (char *) srcaddr, srclen);
switch (srcaddr->sa_family) {
case AF_UNIX:
srcu = (const struct sockaddr_un *) srcaddr;
dstu = (struct sockaddr_un *) dstaddr;
dstsize = sizeof *dstaddr - offsetof(struct sockaddr_un, sun_path);
srclen -= offsetof(struct sockaddr_un, sun_path);
i = QadrtConvertA2E(dstu->sun_path, srcu->sun_path, dstsize - 1, srclen);
dstu->sun_path[i] = '\0';
i += offsetof(struct sockaddr_un, sun_path);
srclen = i;
}
return srclen;
}
int
Curl_os400_connect(int sd, struct sockaddr * destaddr, int addrlen)
{
int i;
struct sockaddr_storage laddr;
i = convert_sockaddr(&laddr, destaddr, addrlen);
2013-10-28 07:00:22 -04:00
if(i < 0)
return -1;
return connect(sd, (struct sockaddr *) &laddr, i);
}
int
Curl_os400_bind(int sd, struct sockaddr * localaddr, int addrlen)
{
int i;
struct sockaddr_storage laddr;
i = convert_sockaddr(&laddr, localaddr, addrlen);
2013-10-28 07:00:22 -04:00
if(i < 0)
return -1;
return bind(sd, (struct sockaddr *) &laddr, i);
}
int
Curl_os400_sendto(int sd, char * buffer, int buflen, int flags,
struct sockaddr * dstaddr, int addrlen)
{
int i;
struct sockaddr_storage laddr;
i = convert_sockaddr(&laddr, dstaddr, addrlen);
2013-10-28 07:00:22 -04:00
if(i < 0)
return -1;
return sendto(sd, buffer, buflen, flags, (struct sockaddr *) &laddr, i);
}
int
Curl_os400_recvfrom(int sd, char * buffer, int buflen, int flags,
struct sockaddr * fromaddr, int * addrlen)
{
int i;
int rcvlen;
int laddrlen;
const struct sockaddr_un * srcu;
struct sockaddr_un * dstu;
struct sockaddr_storage laddr;
2013-10-28 07:00:22 -04:00
if(!fromaddr || !addrlen || *addrlen <= 0)
return recvfrom(sd, buffer, buflen, flags, fromaddr, addrlen);
laddrlen = sizeof laddr;
laddr.ss_family = AF_UNSPEC; /* To detect if unused. */
rcvlen = recvfrom(sd, buffer, buflen, flags,
(struct sockaddr *) &laddr, &laddrlen);
2013-10-28 07:00:22 -04:00
if(rcvlen < 0)
return rcvlen;
switch (laddr.ss_family) {
case AF_UNIX:
srcu = (const struct sockaddr_un *) &laddr;
dstu = (struct sockaddr_un *) fromaddr;
i = *addrlen - offsetof(struct sockaddr_un, sun_path);
laddrlen -= offsetof(struct sockaddr_un, sun_path);
i = QadrtConvertE2A(dstu->sun_path, srcu->sun_path, i, laddrlen);
laddrlen = i + offsetof(struct sockaddr_un, sun_path);
2013-10-28 07:00:22 -04:00
if(laddrlen < *addrlen)
dstu->sun_path[i] = '\0';
break;
case AF_UNSPEC:
break;
default:
2013-10-28 07:00:22 -04:00
if(laddrlen > *addrlen)
laddrlen = *addrlen;
2013-10-28 07:00:22 -04:00
if(laddrlen)
memcpy((char *) fromaddr, (char *) &laddr, laddrlen);
break;
}
*addrlen = laddrlen;
return rcvlen;
}
#ifdef HAVE_LIBZ
const char *
Curl_os400_zlibVersion(void)
{
return set_thread_string(LK_ZLIB_VERSION, zlibVersion());
}
int
Curl_os400_inflateInit_(z_streamp strm, const char * version, int stream_size)
{
z_const char * msgb4 = strm->msg;
int ret;
ret = inflateInit(strm);
if(strm->msg != msgb4)
strm->msg = set_thread_string(LK_ZLIB_MSG, strm->msg);
return ret;
}
int
Curl_os400_inflateInit2_(z_streamp strm, int windowBits,
const char * version, int stream_size)
{
z_const char * msgb4 = strm->msg;
int ret;
ret = inflateInit2(strm, windowBits);
if(strm->msg != msgb4)
strm->msg = set_thread_string(LK_ZLIB_MSG, strm->msg);
return ret;
}
int
Curl_os400_inflate(z_streamp strm, int flush)
{
z_const char * msgb4 = strm->msg;
int ret;
ret = inflate(strm, flush);
if(strm->msg != msgb4)
strm->msg = set_thread_string(LK_ZLIB_MSG, strm->msg);
return ret;
}
int
Curl_os400_inflateEnd(z_streamp strm)
{
z_const char * msgb4 = strm->msg;
int ret;
ret = inflateEnd(strm);
if(strm->msg != msgb4)
strm->msg = set_thread_string(LK_ZLIB_MSG, strm->msg);
return ret;
}
#endif