Пример #1
0
int main(void) {
        URL_T url;
        char buf[BSIZE];
        char *help = "Please enter a valid database connection URL and press ENTER\n"
                    "E.g. sqlite:///tmp/sqlite.db?synchronous=off&heap_limit=2000\n"
                    "E.g. mysql://localhost:3306/test?user=root&password=root\n"
                    "E.g. postgresql://localhost:5432/test?user=root&password=root\n"
                    "E.g. oracle://localhost:1526/test?user=scott&password=tiger\n"
                    "To exit, enter '.' on a single line\n\nConnection URL> ";
        ZBDEBUG = true;
        Exception_init();
        printf("============> Start Connection Pool Tests\n\n");
        printf("This test will create and drop a table called zild_t in the database\n");
	printf("%s", help);
	while (fgets(buf, BSIZE, stdin)) {
		if (*buf == '.')
                        break;
		if (*buf == '\r' || *buf == '\n' || *buf == 0) 
			goto next;
                url = URL_new(buf);
                if (! url) {
                        printf("Please enter a valid database URL or stop by entering '.'\n");
                        goto next;
                }
                testPool(URL_toString(url));
                URL_free(&url);
                printf("%s", help);
                continue;
next:
                printf("Connection URL> ");
	}
	return 0;
}
Пример #2
0
SeafDB *
seaf_db_new_sqlite (const char *db_path)
{
    SeafDB *db;
    GString *url;
    URL_T zdb_url;

    db = g_new0 (SeafDB, 1);
    if (!db) {
        g_warning ("Failed to alloc db structure.\n");
        return NULL;
    }

    url = g_string_new ("");
    g_string_append_printf (url, "sqlite://%s", db_path);

    zdb_url = URL_new (url->str);
    db->pool = ConnectionPool_new (zdb_url);
    if (!db->pool) {
        g_warning ("Failed to create db connection pool.\n");
        g_string_free (url, TRUE);
        g_free (db);
        return NULL;
    }

    ConnectionPool_start (db->pool);
    db->type = SEAF_DB_TYPE_SQLITE;

    return db;
}
Пример #3
0
PKI_X509_KEYPAIR *HSM_X509_KEYPAIR_new( PKI_KEYPARAMS *params, 
			char *label, PKI_CRED *cred, HSM *hsm ) {

	PKI_X509_KEYPAIR *ret = NULL;
	URL *url = NULL;

	if( hsm && !url && (hsm->type == HSM_TYPE_PKCS11) ) {
		PKI_log_debug("PKI_X509_KEYPAIR_new()::Label is required when "
			"using HSM!");
		return ( NULL );
	}

	if ( label ) {
		if(( url = URL_new(label)) == NULL ) {
			PKI_ERROR(PKI_ERR_URI_PARSE, label);
			return ( NULL );
		}
	};

	ret = HSM_X509_KEYPAIR_new_url ( params, url, cred, hsm );
	
	if( url ) URL_free( url );

	return ( ret );
}
Пример #4
0
int PKI_HTTP_POST_data (const char     * url_s,
		                const char     * data,
						size_t           size,
			            const char     * content_type,
						int              timeout,
						size_t           max_size,
				        PKI_MEM_STACK ** ret_sk,
						PKI_SSL  * ssl ) {

	URL *url = NULL;
	int ret = PKI_OK;

	if( !url_s || !data || !content_type ) {
		/* ERROR: All data are strictly required! */
		return PKI_ERR;
	}

	if((url = URL_new(url_s)) == NULL ) {
		/* Error in creating the URL structure */
		return PKI_ERR;
	}

	ret = PKI_HTTP_get_url ( url, data, size, content_type,
			PKI_HTTP_METHOD_POST, timeout, max_size, ret_sk, ssl);

	if ( url ) URL_free (url);

	return ret;
}
Пример #5
0
PKI_MEM_STACK *URL_get_data_mysql ( const char *url_s, ssize_t size )
{
	PKI_MEM_STACK *ret = NULL;
	URL *url = NULL;

	if( !url_s ) 
	{
		PKI_ERROR(PKI_ERR_PARAM_NULL, NULL);
		return (NULL);
	}

	if ((url = URL_new(url_s)) == NULL)
	{
		PKI_ERROR(PKI_ERR_MEMORY_ALLOC, NULL);
		return NULL;
	}

	if (url->proto != URI_PROTO_MYSQL)
	{
		PKI_log_debug("Wrong protocol for MySQL queries (%d)", URI_PROTO_MYSQL);
		URL_free(url);

		return NULL;
	}

	// Get the results
	ret = URL_get_data_mysql_url( url, size );
	
	// Free the URL
	URL_free(url);

	// Return the results
	return ret;
}
Пример #6
0
CcnetDB *
ccnet_db_new_mysql (const char *host, 
                    const char *port,
                    const char *user, 
                    const char *passwd,
                    const char *db_name,
                    const char *unix_socket,
                    gboolean use_ssl,
                    const char *charset,
                    int max_connections)
{
    CcnetDB *db;
    GString *url;
    URL_T zdb_url;
    gboolean has_param = FALSE;

    db = g_new0 (CcnetDB, 1);
    if (!db) {
        g_warning ("Failed to alloc db structure.\n");
        return NULL;
    }

    char *passwd_esc = g_uri_escape_string (passwd, NULL, FALSE);

    url = g_string_new ("");
    g_string_append_printf (url, "mysql://%s:%s@%s:%s/", user, passwd_esc, host, port);
    if (db_name)
        g_string_append (url, db_name);
    if (unix_socket) {
        g_string_append_printf (url, "?unix-socket=%s", unix_socket);
        has_param = TRUE;
    }
    if (use_ssl) {
        g_string_append_printf (url, "%suse-ssl=true", has_param?"&":"?");
        has_param = TRUE;
    }
    if (charset) {
        g_string_append_printf (url, "%scharset=%s", has_param?"&":"?", charset);
        has_param = TRUE;
    }

    g_free (passwd_esc);

    zdb_url = URL_new (url->str);
    db->pool = ConnectionPool_new (zdb_url);
    if (!db->pool) {
        g_warning ("Failed to create db connection pool.\n");
        g_string_free (url, TRUE);
        g_free (db);
        return NULL;
    }

    ConnectionPool_setMaxConnections (db->pool, max_connections);
    ConnectionPool_start (db->pool);
    db->type = CCNET_DB_TYPE_MYSQL;

    return db;
}
Пример #7
0
PKI_MEM_STACK *URL_get_data_pkcs11 ( char *url_s, ssize_t size ) {
	URL *url = NULL;

	if( !url_s ) return (NULL);

	if(((url = URL_new( url_s )) == NULL) ||
		url->proto != URI_PROTO_PKCS11 ) {
		PKI_log_debug ("Not a PKCS11 URL");
		return (NULL);
	}

	return ( URL_get_data_pkcs11_url( url, size ));
}
Пример #8
0
PKI_X509_PRQP_RESP * PKI_DISCOVER_get_resp ( PKI_X509_PRQP_REQ *p, char *url_s ) {

	URL *url = NULL;

	if( p == NULL ) return (NULL);

	if( url_s != NULL ) {
		if((url = URL_new( url_s )) == NULL) {
			return(NULL);
		}
	}

	return( PKI_DISCOVER_get_resp_url( p, url ));
}
Пример #9
0
PKI_CONFIG * PKI_CONFIG_load(char *urlPath)
{
	FILE *file = NULL;
	PKI_CONFIG *doc = NULL;
	URL *url = NULL;
	xmlParserCtxt *parserCtxt = NULL;

	LIBXML_TEST_VERSION

	if (urlPath) url = URL_new( urlPath );
  else return ( NULL );

	// Let's check the URL was parsed correctly
	if( !url || !url->addr ) return(PKI_ERR);

	if ((file = fopen(url->addr, "r")) == NULL)
	{
		URL_free(url);
		return PKI_ERR;
	}
	fclose(file);

	if ((parserCtxt = xmlNewParserCtxt()) == NULL )
	{
		URL_free( url );
		return(PKI_ERR);
	}

#if LIBXML_VERSION > LIBXML_MIN_VERSION
	xmlSetStructuredErrorFunc( parserCtxt, logXmlMessages );
#endif

	/* Do not Keep Blank Nodes */
	xmlKeepBlanksDefault(0);

	/*parse the file and get the DOM */
#if LIBXML_VERSION > LIBXML_MIN_VERSION
	doc = (PKI_CONFIG *) xmlCtxtReadFile(parserCtxt, url->addr, NULL, 
				XML_PARSE_RECOVER | XML_PARSE_NOERROR | XML_PARSE_NOWARNING | 
				XML_PARSE_NOENT );
#else
	doc = (PKI_CONFIG *) xmlCtxtReadFile(parserCtxt, url->addr, NULL, 0);
#endif

	// xmlClearParserCtxt ( parserCtxt );
	xmlFreeParserCtxt ( parserCtxt );
	URL_free(url);

	return( doc );
}
Пример #10
0
PKI_X509_STACK *PKI_X509_STACK_get ( char *url_s, PKI_DATATYPE type, 
						PKI_CRED *cred, HSM *hsm ) {

	URL *url = NULL;
	PKI_X509_STACK *ret = NULL;

	if( !url_s ) return (NULL);

	if((url = URL_new( url_s )) == NULL ) {
		return(NULL);
	}

	ret = PKI_X509_STACK_get_url ( url, type, cred, hsm );

	if( url ) URL_free ( url );
	return ( ret );
}
Пример #11
0
int PKI_X509_STACK_put (PKI_X509_STACK *sk, PKI_DATA_FORMAT format, 
		char *url_string, const char *mime, PKI_CRED *cred, HSM *hsm) {

	URL *url = NULL;
	int ret = PKI_OK;

	if( !sk || !url_string ) return (PKI_ERR);

	if((url = URL_new (url_string)) == NULL ) {
		return (PKI_ERR);
	}

	ret = PKI_X509_STACK_put_url( sk, format, url, mime, cred, hsm );

	if( url ) URL_free ( url );

	return ( ret );
	
}
Пример #12
0
Файл: test.c Проект: irr/c-labs
int main(void) {
    URL_T url = URL_new("sqlite:///tmp/test.db");

    ConnectionPool_T pool = ConnectionPool_new(url);
    ConnectionPool_start(pool);
    Connection_T con = ConnectionPool_getConnection(pool);

    TRY
    {
        Connection_execute(con, "create table bleach(name varchar(255))");
        PreparedStatement_T p = Connection_prepareStatement(con, "insert into bleach values (?)"); 
        const char *bleach[] = {
            "Ichigo Kurosaki", "Rukia Kuchiki", "Orihime Inoue",  "Yasutora \"Chad\" Sado", 
            "Kisuke Urahara", "Uryū Ishida", "Renji Abarai", 0
        };
        for (int i = 0; bleach[i]; i++) {
            PreparedStatement_setString(p, 1, bleach[i]);
            PreparedStatement_execute(p);
        }
        
        ResultSet_T r = Connection_executeQuery(con, "select name from bleach");
        while (ResultSet_next(r))
            printf("%s\n", ResultSet_getString(r, 1));
        
        Connection_execute(con, "drop table bleach;");
    }
    CATCH(SQLException)
    {
        printf("SQLException -- %s\n", Exception_frame.message);
    }
    FINALLY
    {
        Connection_close(con);
    }
    END_TRY;
    
    ConnectionPool_free(&pool);
    URL_free(&url);
        
    return 0;
}
Пример #13
0
int PKI_HTTP_GET_data (const char     * url_s,
	               int              timeout,
		       size_t           max_size,
		       PKI_MEM_STACK ** ret,
		       PKI_SSL  * ssl ) {

	URL *url = NULL;
	int rv = PKI_OK;

	if( !url_s ) return PKI_ERR;

	if((url = URL_new( url_s )) == NULL ) {
		return PKI_ERR;
	}

	rv = PKI_HTTP_get_url ( url, NULL, 0, NULL,
			PKI_HTTP_METHOD_GET, timeout, max_size, ret, ssl );

	if ( url ) URL_free ( url );
	return rv;
}
Пример #14
0
int main(int argc, char *argv[])
{

    URL_T url = URL_new("mysql://127.0.0.1:3306/oauth2?user=dev&password=dev");
    ConnectionPool_T pool = ConnectionPool_new(url);
    ConnectionPool_start(pool);

    Connection_T con = ConnectionPool_getConnection(pool);

    TRY
    {
        ResultSet_T result = Connection_executeQuery(con,
            "SELECT access_token, client_id, user_id, expires FROM oauth_access_tokens");
        while (ResultSet_next(result))
        {
            const char *access_token = ResultSet_getStringByName(result, "access_token");
            printf("%s\t", access_token);

            const char *client_id = ResultSet_getStringByName(result, "client_id");
            printf("%s\t", client_id);

            printf("\n");
        }
    }
    CATCH(SQLException)
    {
        printf("SQLException -- %s\n", Exception_frame.message);
    }
    FINALLY
    {
        Connection_close(con);
    }
    END_TRY;

    ConnectionPool_free(&pool);
    URL_free(&url);

    return 0;
}
Пример #15
0
SeafDB *
seaf_db_new_pgsql (const char *host,
                   const char *user,
                   const char *passwd,
                   const char *db_name,
                   const char *unix_socket)
{
    SeafDB *db;
    GString *url;
    URL_T zdb_url;

    db = g_new0(SeafDB, 1);
    if (!db) {
        g_warning ("Failed to alloc db structre.\n");
        return NULL;
    }

    url = g_string_new ("");
    g_string_append_printf (url, "postgresql://%s:%s@%s/", user, passwd, host);
    if (db_name)
        g_string_append (url, db_name);
    if (unix_socket)
        g_string_append_printf (url, "?unix-socket=%s", unix_socket);

    zdb_url = URL_new (url->str);
    db->pool = ConnectionPool_new (zdb_url);
    if (!db->pool) {
        g_warning ("Failed to create db connection pool.\n");
        g_string_free(url, TRUE);
        g_free (db);
        return NULL;
    }

    ConnectionPool_start (db->pool);
    db->type = SEAF_DB_TYPE_PGSQL;

    return db;
}
Пример #16
0
int URL_put_data_mysql ( const char *url_s, const PKI_MEM *data ) {

	int ret = 0;
	URL *url = NULL;

	// Parameter checking
	if( !url_s ) 
	{
		PKI_ERROR(PKI_ERR_PARAM_NULL, NULL);
		return (PKI_ERR);
	}

	// Allocates a new URL structure
	if ((url = URL_new(url_s)) == NULL)
	{
		PKI_ERROR(PKI_ERR_MEMORY_ALLOC, NULL);
		return PKI_ERR;
	}

	// Checks the protocol to by MySQL
	if (url->proto != URI_PROTO_MYSQL)
	{
		PKI_log_debug("Wrong protocol for MySQL queries (%d)", URI_PROTO_MYSQL);
		URL_free(url);

		return PKI_ERR;
	}

	// Gets the response
	ret = URL_put_data_mysql_url( url, data );

	// Frees the URL data structure
	URL_free (url);

	return ret;
}
Пример #17
0
PKI_CONFIG_STACK * PKI_CONFIG_load_dir ( char *dir, PKI_CONFIG_STACK *sk ) {

        struct dirent *dd = NULL;
	DIR *dirp = NULL;
	URL *url = NULL;

	int found = 0;
	PKI_CONFIG_STACK *ret = NULL;

	/* Check input */
	if( !dir ) {
		return (NULL);
	}

	if(( url = URL_new ( dir )) == NULL ) {
		PKI_log_debug( "Dir not valid for config (%s)", dir );
		return ( NULL );
	}

	if( url->proto != URI_PROTO_FILE ) {
		PKI_log_debug( "Dir not valid for config (%s)", dir );
		return (NULL);
	}

	if((dirp = opendir( url->addr )) == NULL ) {
		PKI_log_debug("ERROR, Can not open dir %s!\n", url->addr );
		return (NULL);
	} else {
		if( !sk ) {
			if((ret = PKI_STACK_CONFIG_new()) == NULL ) {
				PKI_log_debug("Memory Error (%s:%d)", 
							__FILE__, __LINE__ );
				return(NULL);
			}
		} else {
			ret = sk;
		}

		while(( dd = readdir( dirp )) != NULL ) {
			long len;
			char *filename = NULL;

			filename = dd->d_name;
			len = (long) strlen( filename );

			if( (len < 4) || (strcmp( ".xml", filename +len -4 ))) {
				PKI_log_debug( "Skipping file %s", filename);
				continue;
			} else {
			
				char fullpath[BUFF_MAX_SIZE];
				size_t fullsize = 0;

				PKI_CONFIG *tmp_cfg = NULL;

				PKI_log_debug( "Loading file %s" LIBPKI_PATH_SEPARATOR "%s", 
							url->addr, filename );

				snprintf(fullpath, BUFF_MAX_SIZE,
					"%s" LIBPKI_PATH_SEPARATOR "%s", url->addr, filename );

				if((fullsize = strlen(url->addr) + 
					strlen( filename ) + 1) > 
							BUFF_MAX_SIZE) {
					continue;
				}
				
				if((tmp_cfg = PKI_CONFIG_load( fullpath )) ==
									NULL ) {
					continue;
				}

				PKI_log_debug( "Loaded %s file", fullpath );
				PKI_STACK_CONFIG_push( ret, tmp_cfg );
				found = 1;
			}
		}
		closedir( dirp );
	}
	if( url ) URL_free (url);

	if( found == 1 ) {
		return (ret);
	} else {
		PKI_STACK_CONFIG_free( ret );
		PKI_log_debug("PKI_CONFIG_load_dir() Failed!\n" );
		return ( NULL );
	}
}
Пример #18
0
char * PKI_CONFIG_find ( char *dir, char *name )
{
	struct dirent *dd = NULL;
	DIR *dirp = NULL;
	URL *url = NULL;

	int found = 0;
	char *ret = NULL;

	/* Check input */
	if( !dir || !name )
	{
		PKI_ERROR(PKI_ERR_PARAM_NULL, NULL);
		return (PKI_ERR);
	}

	if ((url = URL_new(dir)) == NULL)
	{
		PKI_log_debug("Dir [%s] is not a valid URI", dir );
		return (PKI_ERR);
	}

	if (url->proto != URI_PROTO_FILE)
	{
		PKI_log_debug("URL is not a file, skipping!", dir );
		return (PKI_ERR);
	}

	if ((dirp = opendir(url->addr)) == NULL)
	{
		PKI_log_debug("Can not open directory [%s]", url->addr );
		return (PKI_ERR);
	}
	else
	{
		while(( dd = readdir( dirp )) != NULL )
		{
			long len;
			char *filename = NULL;

			filename = dd->d_name;
			len = (long) strlen( filename );

			PKI_log_debug("Processing file [%s]", filename );

			if (len < 4 || strcmp(".xml", filename +len-4) != 0)
			{
				PKI_log_debug("Skipping %s", filename );
				continue;
			}
			else
			{
				char fullpath[BUFF_MAX_SIZE];
				size_t fullsize = 0;

				PKI_CONFIG *tmp_cfg = NULL;
				char *tmp_name = NULL;

				snprintf(fullpath, BUFF_MAX_SIZE,
					"%s/%s", url->addr, filename );

				PKI_log_debug("Opening File %s", fullpath );

				// Check the allowed size
				fullsize = strlen(url->addr) + strlen( filename ) + 1;
				if (fullsize > BUFF_MAX_SIZE) continue;
				
				if ((tmp_cfg = PKI_CONFIG_load(fullpath)) == NULL)
				{
					PKI_log_debug("Can not load %s", fullpath );
					continue;
				}

				PKI_log_debug("Getting Name Param... ");
				tmp_name = PKI_CONFIG_get_value(tmp_cfg, "/*/name");
				PKI_CONFIG_free(tmp_cfg);

				if (tmp_name != NULL)
				{
					PKI_log_debug("Got Name::%s", tmp_name);
					if (strcmp_nocase(tmp_name, name) == 0)
					{
						PKI_Free(tmp_name);
						tmp_name = NULL; // Safety

						found = 1;
						ret = strdup(fullpath);
						PKI_log_debug("File successfully loaded %s", fullpath );
						break;
					}
					PKI_Free(tmp_name);
					tmp_name = NULL; // Safety
				}
				else PKI_log_debug("No Name found!");
			}
		}
		closedir( dirp );
	}

	// Let's free the URL memory
	if (url) URL_free(url);

	// If found, let's return it
	if (found == 1) return ret;

	// If not found, we return NULL
	return NULL;
}
Пример #19
0
int PKI_HTTP_get_socket (const PKI_SOCKET * sock,
	                 const char       * data,
			 size_t             data_size,
		         const char       * content_type,
			 int                method,
			 int                timeout,
	                 size_t             max_size,
			 PKI_MEM_STACK   ** sk ) {

	size_t len = 0;

	const char *my_cont_type = "application/unknown";

	PKI_HTTP *http_rv	 = NULL;

	int rv   = -1;
	int ret  = PKI_OK;

	size_t max_len = 0;
	size_t auth_len = 0;

	char *tmp  = NULL;
	char *auth_tmp = NULL;
    
	char *head_get =
			"GET %s HTTP/1.1\r\n"
			"Host: %s\r\n"
			"User-Agent: LibPKI\r\n"
			"Connection: close\r\n"
			"%s";

	char *head_post = 
			"POST %s HTTP/1.1\r\n"
			"Host: %s\r\n"
			"User-Agent: LibPKI\r\n"
			"Connection: close\r\n"
			"Content-type: %s\r\n"
			"Content-Length: %d\r\n"
			"%s";

	char *head = NULL;

	if ( timeout < 0 ) timeout = 0;

	if ( !sock || !sock->url ) return PKI_ERR;

	// Process the authentication information if provided by the caller
	if (sock->url && sock->url->usr && sock->url->pwd)
	{
		// Rough estimate for the auth string
		max_len = strlen(sock->url->usr) + strlen(sock->url->pwd) + 100;

		// Special case for when a usr/pwd was specified in the URL
		auth_tmp = PKI_Malloc(len);
		auth_len = (size_t)snprintf(auth_tmp, len, "Authentication: user %s:%s\r\n\r\n", sock->url->usr, sock->url->pwd);
	}
	else
	{
		// If we do not have the auth info, we just add the end of header
		auth_len = 2;
		auth_tmp = "\r\n";
	}

	if (method == PKI_HTTP_METHOD_GET)
	{
		// Gets the right header
		head = head_get;

		// Estimate the header's final size
		max_len =
				strlen(head) +
				strlen(sock->url->path) +
				strlen(sock->url->addr) +
				101;

		// Allocates enough space for the header
		tmp = PKI_Malloc ( max_len + auth_len );

		// Prints the header into the tmp container
		len = (size_t) snprintf(tmp, max_len, head, sock->url->path, sock->url->addr, auth_tmp);
	}
	else if (method == PKI_HTTP_METHOD_POST)
	{
		// Gets the right head
		head = head_post;

		// Determines the right content type
		if ( content_type ) my_cont_type = content_type;
		else my_cont_type = "text/html";

		// Checks the max len for the allocated header
		max_len =
				strlen(head) +
				strlen(sock->url->path) +
				strlen(sock->url->addr) +
				strlen(my_cont_type) +
				101;

		// Allocates the memory for the header
		tmp = PKI_Malloc ( max_len + auth_len );

		// Prints the header into the tmp container
		len = (size_t) snprintf(tmp, max_len, head, sock->url->path, sock->url->addr, 
					my_cont_type, data_size, auth_tmp );
	}
	else
	{
		PKI_log_err ( "Method (%d) not supported!", method );
		return PKI_ERR;
	}

	// PKI_MEM *r = PKI_MEM_new_data(len, tmp);
	// URL_put_data("file://http_req.txt", r, NULL, NULL, 0, 0, NULL);
	// PKI_MEM_free(r);

	if ((rv = (int) PKI_SOCKET_write(sock, tmp, len)) < 0)
	{
		PKI_log_err("Can not write HTTP header to socket");
		PKI_Free(tmp);
		goto err;
	}

	// Free the tmp pointer that held the request header
	if (tmp) PKI_Free (tmp);

	// If we were using a POST method, we need to actually send the data
	if(data != NULL)
	{
		PKI_log_err("{DEBUG} Writing Data -> data_size = %d, data = %p", data_size, data);

		if ((PKI_SOCKET_write(sock, data, data_size)) < 0)
		{
			PKI_log_err ("Can not write POST to socket.");
			goto err;
		}
	}
	
	// Let's now wait for the response from the server
	if ((http_rv = PKI_HTTP_get_message(sock, timeout, max_size)) == NULL)
	{
		PKI_log_err ("HTTP retrieval error\n");
		goto err;
	}

	// We shall now check for the return code
	if (http_rv->code >= 400 )
	{
		goto err;
	}
	else if (http_rv->code >= 300)
	{
		/* Redirection - let's try that */
		if (http_rv->location == NULL)
		{
			PKI_log_debug ( "HTTP Redirection but no location provided!");
			goto err;
		}

    PKI_log_debug("HTTP Redirection Detected [URL: %s]", http_rv->location );

		if (strstr(http_rv->location, "://") != NULL)
		{
			URL *url_tmp = NULL;

			if( strncmp_nocase( http_rv->location, sock->url->url_s, 
					(int) strlen(http_rv->location)) == 0)
			{
				PKI_log_debug( "HTTP cyclic redirection!");
				goto err;
			}

			if ((url_tmp = URL_new ( http_rv->location )) == NULL)
			{
				PKI_log_debug("HTTP location is not a valid URI (%s)", http_rv->location );
				goto err;
			}

			if ( sock->url->ssl == 0 )
			{
				ret = PKI_HTTP_get_url ( url_tmp, data, 
					data_size, content_type, method, timeout, 
							max_size, sk, NULL );
			}
			else
			{
				PKI_SSL *ssl2 = PKI_SSL_dup ( sock->ssl );

				ret = PKI_HTTP_get_url ( url_tmp, data, 
					data_size, content_type, method, timeout, 
							max_size, sk, ssl2 );
			}

			if ( url_tmp ) URL_free ( url_tmp );
	
			goto end;

		}
		else
		{
			const char *prot_s = NULL;
			char new_url[2048];
			URL *my_new_url = NULL;
			PKI_SSL *ssl2 = PKI_SSL_dup ( sock->ssl );

			prot_s = URL_proto_to_string ( sock->url->proto );
			if( !prot_s ) goto err;

			snprintf(new_url, sizeof(new_url),"%s://%s%s", prot_s, sock->url->addr, http_rv->location );

			if( strncmp_nocase( new_url, sock->url->url_s, (int) strlen ( new_url )) == 0 )
			{
				PKI_log_debug( "HTTP cyclic redirection!");
				goto err;
			}

			my_new_url = URL_new ( new_url );

			ret = PKI_HTTP_get_url ( my_new_url, data, data_size, content_type, method,
						timeout, max_size, sk, ssl2 );

			if (ssl2) PKI_SSL_free ( ssl2 );
		}
	}
	else if (http_rv->code != 200)
	{
		PKI_log_debug( "Unknown HTTP Return code [Code: %d]", http_rv->code );
		goto err;
	}

	/*
	PKI_log_err("{DEBUG} method = %d, header->size = %d, body = %p, body_size = %d",
			  http_rv->method, http_rv->head->size, http_rv->body, http_rv->body->size);
	URL_put_data("file://http-resp-header.txt", http_rv->head, NULL, NULL, 0, 0, NULL);
	URL_put_data("file://http-resp-data.txt", http_rv->body, NULL, NULL, 0, 0, NULL);
	*/

	// If a Pointer was provided, we want the data back
	if (sk) {

		// Checks if the caller provided an already allocated data
		// structure. If not, we allocate it.
		if (*sk) PKI_STACK_MEM_free_all(*sk);

    // Allocates a new structure
		if ((*sk = PKI_STACK_MEM_new()) == NULL) {

      // If a memory error occurs report it and exit
			PKI_ERROR(PKI_ERR_MEMORY_ALLOC, NULL);

      // Nothing more to do
			goto err;
		}

		// Add the returned value to the stack
		if (PKI_STACK_MEM_push(*sk, http_rv->body) != PKI_OK) {
			PKI_log_err("Can not push the HTTP result body in the result stack");
			goto err;
		}

		// Remove ownership of the body PKI_MEM from the original
		// HTTP msg container
		http_rv->body = NULL;
	}

end:
	// Finally free the HTTP message memory
	if (http_rv) PKI_HTTP_free(http_rv);

	// Returns the result
	return ret;

err:
	// Error condition
	if (http_rv) PKI_HTTP_free ( http_rv );

	// Free the locally allocated memory
	if (*sk) PKI_STACK_MEM_free_all(*sk);
	*sk = NULL;

	return PKI_ERR;
}
Пример #20
0
/* Functions */
OCSPD_CONFIG * OCSPD_load_config(char *configfile)
{
	OCSPD_CONFIG *h = NULL;
	PKI_CONFIG *cnf = NULL;
	PKI_CONFIG_STACK *ca_config_stack = NULL;

	char *tmp_s = NULL;
	char *tmp_s2 = NULL;

	int i;

	/* Check for the environment variable PRQP_CONF */
	if (configfile == NULL) configfile = getenv("OCSPD_CONF");

	/* If not, check for the default CONFIG_FILE */
	if (configfile == NULL) configfile = CONFIG_FILE;

	if( !configfile ) {
		/* No config file is available */
		PKI_log(PKI_LOG_ERR, "No config file provided!");
		return (NULL);
	}

	/* Load the config file */
	if(( cnf = PKI_CONFIG_load ( configfile )) == NULL ) {
		PKI_log( PKI_LOG_ERR, "Can not load config file [%s]!",
			configfile );
		return (NULL);
	}
	if(( h = (OCSPD_CONFIG *)PKI_Malloc(sizeof(OCSPD_CONFIG))) == NULL) {
		PKI_ERROR(PKI_ERR_MEMORY_ALLOC, NULL);
		goto err;
	}

	/* Set the group and user string to NULL */
	h->user = NULL;
	h->group = NULL;

	/* Set the PRQPD verbose status */
	h->verbose   = 0;
	h->debug     = 0;
	h->nthreads  = 5;
	h->http_proto = "1.0";
	h->max_timeout_secs = 5;

	h->crl_auto_reload = 3600;
	h->crl_reload_expired = 1;
	h->crl_check_validity = 600;

	/* Copy the config filename so that it could be re-loaded on SIGHUP */
	h->cnf_filename = strdup( configfile );

	/* Initialize the COND variables and MUTEXES */
	for( i = 0; i < sizeof ( h->mutexes ) / sizeof( PKI_MUTEX ); i++ )
	{
		PKI_MUTEX_init ( &h->mutexes[i] );
	}

	for( i = 0; i < sizeof ( h->condVars ) / sizeof( PKI_COND ); i++)
	{
		PKI_COND_init ( &h->condVars[i] );
	}

	PKI_RWLOCK_init ( &h->crl_lock );

	/* Token Initialization */
	if (( tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/general/pkiConfigDir")) == NULL)
	{
		PKI_log_err("Missing pkiConfigDir in configuration!");
		return NULL;
	}
	else 
	{
		if ((tmp_s2 = PKI_CONFIG_get_value( cnf, "/serverConfig/general/token" )) != NULL)
		{
			h->token_name = strdup( tmp_s2 );
			h->token_config_dir = strdup ( tmp_s );

			if ((h->token = PKI_TOKEN_new_null()) == NULL)
			{
				PKI_log( PKI_LOG_ERR, "Memory error for new token");
				exit(1);
			}

			PKI_Free(tmp_s2);
		}
		else
		{
			PKI_log_err("No General Token provided in configuration.");

			PKI_Free(tmp_s);
			return NULL;
		}

		PKI_Free(tmp_s);
	}

	/* Thread configuration */
	if((tmp_s = PKI_CONFIG_get_value(cnf, "/serverConfig/general/spawnThreads")) != NULL)
	{
		int t = 0;
		if((t = atoi( tmp_s )) > 0 ) h->nthreads = t;

		PKI_Free(tmp_s);
	}

	if((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/general/caConfigDir")) != NULL)
	{
		h->ca_config_dir = strdup(tmp_s);

		ca_config_stack = PKI_CONFIG_load_dir(h->ca_config_dir, NULL);
		if (ca_config_stack == NULL)
		{
			PKI_log( PKI_LOG_ERR, "Can't load caConfigDir (%s)", h->ca_config_dir);
			PKI_Free(tmp_s);

			goto err;
		}

		PKI_Free(tmp_s);
	}
	else
	{
		PKI_log( PKI_LOG_ERR, "/serverConfig/general/caConfigDir needed in conf!\n");
		goto err;
	}

	/* Pid File */
	if((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/general/pidFile")) != NULL )
	{
		h->pidfile = strdup(tmp_s);

		PKI_Free(tmp_s);
	}

	/* AutoReload timeout */
	if((tmp_s = PKI_CONFIG_get_value( cnf, 
		"/serverConfig/general/crlAutoReload")) != NULL)
	{
		h->crl_auto_reload = atoi(tmp_s);

		if( h->crl_auto_reload <= 0 )
		{
			h->crl_auto_reload = 0;
			PKI_log(PKI_LOG_INFO, "Auto Reload Disabled");
		}

		PKI_Free(tmp_s);
	}

	/* CRL validity check timeout */
	if((tmp_s = PKI_CONFIG_get_value( cnf, 
			"/serverConfig/general/crlCheckValidity")) != NULL )
	{
		h->crl_check_validity = atoi(tmp_s);
		if ( h->crl_check_validity <= 0 )
		{
			h->crl_check_validity = 0;
			PKI_log(PKI_LOG_INFO, "CRL check validity disabled");
		}

		PKI_Free(tmp_s);
	}

	/* AutoReload timeout */
	if ((tmp_s = PKI_CONFIG_get_value( cnf, 
				"/serverConfig/general/crlReloadExpired")) != NULL )
	{
		if (strncmp_nocase(tmp_s, "n", 1) == 0)
		{
			h->crl_reload_expired = 0;
			PKI_log(PKI_LOG_INFO, "Expired CRLs Reload Disabled");
		}

		PKI_Free(tmp_s);
	}

	/* Server Privileges */
	if ((tmp_s = PKI_CONFIG_get_value(cnf, "/serverConfig/security/user")) != NULL)
	{
		h->user = strdup(tmp_s);
		PKI_Free(tmp_s);
	}

	if ((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/security/group" )) != NULL)
	{
		h->group = strdup(tmp_s);
		PKI_Free(tmp_s);
	}

	if ((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/security/chrootDir" )) != NULL )
	{
		h->chroot_dir = strdup(tmp_s);
		PKI_Free(tmp_s);
	}

	/* Bind Address */
	if((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/network/bindAddress" )) == NULL)
	{
		// If not bindAddress, let's use the universal one
		tmp_s = strdup("http://0.0.0.0:2560");
	}

	if ((h->bindUrl = URL_new( tmp_s )) == NULL)
	{
		PKI_log( PKI_LOG_ERR, "Can't parse bindAddress (%s)", tmp_s );
		PKI_Free(tmp_s);

		goto err;
	}

	// We need to free the tmp_s
	PKI_Free(tmp_s);

	/* HTTP Version */
	if((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/network/httpProtocol")) != NULL)
	{
		h->http_proto = strdup(tmp_s);
		PKI_Free(tmp_s);
	}

	/* Timeout for incoming connections */
	if((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/network/timeOut")) != NULL )
	{
		long t = 0;

		if ((t = atol( tmp_s )) > 0) h->max_timeout_secs = (unsigned int) t;
		PKI_Free(tmp_s);
	}

	/* Maximum Request Size */
	if((tmp_s = PKI_CONFIG_get_value( cnf,
				"/serverConfig/response/maxReqSize" )) != NULL ) {
		int t = 0;

		if((t = atoi( tmp_s )) > 0 ) {
			h->max_req_size = t;
		}
		PKI_Free(tmp_s);
	}


	// Default
	h->digest = PKI_DIGEST_ALG_SHA1;

	/* Digest Algorithm to be used */
	if ((tmp_s = PKI_CONFIG_get_value(cnf, "/serverConfig/response/digestAlgorithm" )) != NULL)
	{
		h->digest = PKI_DIGEST_ALG_get_by_name( tmp_s );

		if (!h->digest) 
		{
			PKI_log_err("Can not parse response digest algorithm: %s", tmp_s);
			exit(1);
		}
		else PKI_log_debug("Selected response digest algorithm: %s", tmp_s);

		PKI_Free(tmp_s);
	}

	/* Signing Digest Algorithm to be used */
	if((tmp_s = PKI_CONFIG_get_value( cnf,
			"/serverConfig/response/signatureDigestAlgorithm" )) == NULL)
	{
		PKI_log_debug("No specific signature digest algorithm selected.");
		h->sigDigest = NULL;
	}
	else
	{
		h->sigDigest = PKI_DIGEST_ALG_get_by_name( tmp_s );

		if (!h->sigDigest) 
		{
			PKI_log_err("Can not parse signing digest algorithm: %s", tmp_s);
			exit(1);
		}
		else PKI_log_debug("Selected signature digest algorithm: %s", tmp_s);

		PKI_Free(tmp_s);
	}

	/* Now Parse the PRQP Response Section */
	if ((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/response/validity/days" )) != NULL)
	{
		h->ndays = atoi(tmp_s);
		PKI_Free(tmp_s);
	}

	if ((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/response/validity/mins" )) != NULL)
	{
		h->nmin = atoi(tmp_s);
		PKI_Free(tmp_s);
	}

	h->set_nextUpdate = h->ndays * 3600 + h->nmin * 60;

	/* Database Options */
	if ((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/general/dbUrl")) != NULL)
	{
		if ((h->db_url = URL_new ( tmp_s )) == NULL)
		{
			PKI_log_err ( "Database Url not parsable (%s)", tmp_s );
			PKI_Free(tmp_s);
			goto err;
		}

		PKI_Free(tmp_s);
	}

	/* Database Persistant */
	if ((tmp_s = PKI_CONFIG_get_value( cnf, "/serverConfig/general/dbPersistant")) != NULL)
	{
		if (strncmp_nocase ( "n", tmp_s, 1 ) == 0 )
			h->db_persistant = 0;
		else 
			h->db_persistant = 1;

		PKI_Free(tmp_s);
	}

	/* Now we should load the CA configuration files and generate the
	   CERT_ID for the different CAs */
	if ((OCSPD_build_ca_list( h, ca_config_stack )) == PKI_ERR )
	{

		PKI_log(PKI_LOG_ERR, "Can not build CA list!");
		if (ca_config_stack) PKI_STACK_CONFIG_free ( ca_config_stack );
		goto err;
	}

	if (ca_config_stack) PKI_STACK_CONFIG_free ( ca_config_stack );

	return ( h );

err:
	if( ca_config_stack ) PKI_STACK_CONFIG_free ( ca_config_stack );
	if( cnf ) PKI_CONFIG_free ( cnf );
	if( h ) PKI_Free ( h );

	return( NULL );
}
Пример #21
0
int OCSPD_build_ca_list ( OCSPD_CONFIG *handler,
			PKI_CONFIG_STACK *ca_conf_sk) {

	int i = 0;
	PKI_STACK *ca_list = NULL;

	PKI_log_debug("Building CA List");

	if ( !ca_conf_sk ) {
		PKI_log( PKI_LOG_ERR, "No stack of ca configs!");
		return ( PKI_ERR );
	}

	if((ca_list = PKI_STACK_new((void (*))CA_LIST_ENTRY_free)) == NULL ) {
		PKI_log_err ( "Memory Error");
	return ( PKI_ERR );
	}

	for (i = 0; i < PKI_STACK_CONFIG_elements( ca_conf_sk ); i++)
	{
		char *tmp_s = NULL;
		URL *tmp_url = NULL;
		PKI_X509_CERT *tmp_cert = NULL;

		CA_LIST_ENTRY *ca = NULL;
		PKI_CONFIG *cnf = NULL;

		/* Get the current Configureation file */
		cnf = PKI_STACK_CONFIG_get_num( ca_conf_sk, i );
		if (!cnf) continue;

		/* Get the CA cert from the cfg file itself */
		if((tmp_s = PKI_CONFIG_get_value( cnf, "/caConfig/caCertValue" )) == NULL )
		{
			/* Get the CA parsed url */
			if((tmp_url = URL_new( PKI_CONFIG_get_value( cnf, "/caConfig/caCertUrl" ))) == NULL )
			{
				/* Error, can not parse url data */
				PKI_log( PKI_LOG_ERR, "Can not parse CA cert url (%s)", 
					PKI_CONFIG_get_value(cnf, "/caConfig/caCertUrl"));

				continue;
			}

			if((tmp_cert = PKI_X509_CERT_get_url(tmp_url, NULL, NULL ))== NULL)
			{
				PKI_log_err("Can not get CA cert from (%s)", tmp_url);
				URL_free (tmp_url);

				continue;
			}
		}
		else
		{
			PKI_X509_CERT_STACK *cc_sk = NULL;
			PKI_MEM *mm = NULL;

			if((mm = PKI_MEM_new_null()) == NULL )
			{
				PKI_Free(tmp_s);
				continue;
			}

			PKI_MEM_add ( mm, tmp_s, strlen(tmp_s));

			if((cc_sk=PKI_X509_CERT_STACK_get_mem(mm, NULL)) == NULL )
			{
				PKI_log_err ( "Can not parse cert from /caConfig/caCertValue");
				PKI_Free(tmp_s);

				continue;
			}

			if ((tmp_cert = PKI_STACK_X509_CERT_pop( cc_sk )) == NULL )
			{
				PKI_log_err ( "No elements on stack from /caConfig/caCertValue");

				PKI_STACK_X509_CERT_free_all(cc_sk);
				PKI_Free(tmp_s);

				continue;
			}

			PKI_STACK_X509_CERT_free ( cc_sk );
			PKI_Free(tmp_s);
		}

		/* OCSPD create the CA entry */
		if ((ca = CA_LIST_ENTRY_new()) == NULL )
		{
			PKI_log_err ( "CA List structure init error");

			/* remember to do THIS!!!! */
			if( tmp_url ) URL_free ( tmp_url );
			if( tmp_cert ) PKI_X509_CERT_free ( tmp_cert );

			continue;
		}

		ca->ca_cert = tmp_cert;
		tmp_cert = NULL;

		ca->ca_url = tmp_url;
		tmp_url = NULL;

		ca->ca_id = PKI_CONFIG_get_value( cnf, "/caConfig/name" );
		ca->cid = CA_ENTRY_CERTID_new ( ca->ca_cert, handler->digest );

		/* Get the CRL URL and the CRL itself */
		if((tmp_s = PKI_CONFIG_get_value(cnf, "/caConfig/crlUrl")) == NULL)
		{
			PKI_STACK *cdp_sk = NULL;

			/* Now let's get it from PRQP */

			/* Now from the Certificate */
			
			if((cdp_sk = PKI_X509_CERT_get_cdp (ca->ca_cert)) ==NULL)
			{
				// No source for the CRL Distribution Point
				PKI_log_err ( "ERROR::Can not find the CDP for %s, skipping CA", ca->ca_id );

				CA_LIST_ENTRY_free ( ca );
				continue;
			}

			while ((tmp_s = PKI_STACK_pop ( cdp_sk )) != NULL)
			{
				if ((ca->crl_url = URL_new ( tmp_s )) == NULL )
				{
					PKI_log_err( "URL %s not in the right format!");
					CA_LIST_ENTRY_free ( ca );
					continue;
				}
				else if( tmp_s ) PKI_Free ( tmp_s );

				break;
			}
		}
		else
		{
			PKI_log_debug("Got CRL Url -> %s", tmp_s );

			if((ca->crl_url = URL_new ( tmp_s )) == NULL )
			{
				PKI_log_err ("Error Parsing CRL URL [%s] for CA [%s]", ca->ca_id, tmp_s);

				CA_LIST_ENTRY_free ( ca );
				PKI_Free(tmp_s);

				continue;
			}

			PKI_Free(tmp_s);
		}

		if(OCSPD_load_crl ( ca, handler ) == PKI_ERR )
		{
			PKI_log_err ( "Can not get CRL for %s", ca->ca_id);
			CA_LIST_ENTRY_free ( ca );

			continue;
		}

		/* If the Server has a Token to be used with this CA, let's
                   load it */
		if((tmp_s = PKI_CONFIG_get_value ( cnf, "/caConfig/serverToken" )) == NULL)
		{
			/* No token in config, let's see if a specific cert
			   is configured */
			ca->token = NULL;

			if((tmp_s = PKI_CONFIG_get_value ( cnf, "/caConfig/serverCertUrl" )) == NULL )
			{
				/* No cert is configured, we will use the defaults */
				ca->server_cert = NULL;
			}
			else
			{
				/* The Server's cert URL is found, let's load the certificate */
				if ((tmp_cert = PKI_X509_CERT_get ( tmp_s, NULL, NULL )) == NULL )
				{
					PKI_log_err("Can not get server's cert from %s!", tmp_s );

					CA_LIST_ENTRY_free ( ca );
					PKI_Free(tmp_s);

					continue;
				}
				else
				{
					ca->server_cert = tmp_cert;
				}

				PKI_Free(tmp_s);
			}
		}
		else
		{
			/* A Token for this CA is found - we do not load
 			   it to avoid problems with Thread Initialization */
			ca->server_cert = NULL;
			ca->token_name = tmp_s;
			ca->token = PKI_TOKEN_new_null();

			if ((tmp_s = PKI_CONFIG_get_value ( cnf, "/caConfig/pkiConfigDir" )) != NULL) {
				ca->token_config_dir = strdup( tmp_s );
				PKI_Free(tmp_s);
			}
			else
			{
				ca->token_config_dir = strdup(handler->token_config_dir);
			}
		}

		if((tmp_s = PKI_CONFIG_get_value ( cnf, "/caConfig/caCompromised" )) == NULL) {
			ca->compromised = 0;
		}
		else
		{
			ca->compromised = atoi(tmp_s);
			PKI_Free(tmp_s);
		}

		/* Responder Id Type */
		if ((tmp_s = PKI_CONFIG_get_value(cnf, "/caConfig/responderIdType")) != NULL)
		{
			if (strncmp_nocase(tmp_s, "keyid", 5) == 0) 
			{
				ca->response_id_type = PKI_X509_OCSP_RESPID_TYPE_BY_KEYID;
			}
			else if (strncmp_nocase(tmp_s, "name", 4) == 0)
			{
				ca->response_id_type = PKI_X509_OCSP_RESPID_TYPE_BY_NAME;
			}
			else
			{
				PKI_log_err("Can not parse responderIdType: %s (allowed 'keyid' or 'name')", tmp_s);
				exit(1);
			}

			PKI_Free(tmp_s);
		}
		else
		{
			// Default Value
			ca->response_id_type = PKI_X509_OCSP_RESPID_TYPE_BY_NAME;
		}

		// Now let's add the CA_LIST_ENTRY to the list of configured CAs
		PKI_STACK_push ( ca_list, ca );

	}

	handler->ca_list = ca_list;

	return ( PKI_OK );
}
Пример #22
0
static void testPool(const char *testURL) {
        URL_T url;
        char *schema;
        ConnectionPool_T pool;
        char *data[]= {"Fry", "Leela", "Bender", "Farnsworth",
                "Zoidberg", "Amy", "Hermes", "Nibbler", "Cubert",
                "Zapp", "Joey Mousepad", "ЯΣ༆", 0}; 
        
        if (Str_startsWith(testURL, "mysql")) {
                schema = SCHEMA_MYSQL;
        } else if (Str_startsWith(testURL, "postgresql")) {
                schema = SCHEMA_POSTGRESQL;
        } else if (Str_startsWith(testURL, "sqlite")) {
                schema = SCHEMA_SQLITE;
        } else if (Str_startsWith(testURL, "oracle")) {
                schema = SCHEMA_ORACLE;
        }
        else {
                printf("Unsupported database protocol\n");
                exit(1);
        }

        printf("=> Test1: create/destroy\n");
        {
                pool = ConnectionPool_new(URL_new(testURL));
                assert(pool);
                url = ConnectionPool_getURL(pool);
                ConnectionPool_free(&pool);
                assert(! pool);
                URL_free(&url);
        }
        printf("=> Test1: OK\n\n");
        
        printf("=> Test2: NULL value\n");
        {
                url = URL_new(NULL);
                assert(! url);
                pool = ConnectionPool_new(url);
                assert(! pool);
        }
        printf("=> Test2: OK\n\n");
        
        printf("=> Test3: start/stop\n");
        {
                url = URL_new(testURL);
                pool = ConnectionPool_new(url);
                assert(pool);
                ConnectionPool_start(pool);
                ConnectionPool_stop(pool);
                ConnectionPool_free(&pool);
                assert(pool==NULL);
                URL_free(&url);
                // Test that exception is thrown on start error
                TRY
                {
                        url = URL_new("not://a/database");
                        pool = ConnectionPool_new(url);
                        assert(pool);
                        ConnectionPool_start(pool);
                        printf("\tResult: Test failed -- exception not thrown\n");
                        exit(1);
                }
                CATCH(SQLException) {
                        // OK
                }
                FINALLY {
                        ConnectionPool_free(&pool);
                        assert(pool==NULL);
                        URL_free(&url);
                }
                END_TRY;
        }
        printf("=> Test3: OK\n\n");
        
        printf("=> Test4: Connection execute & transaction\n");
        {
                int i;
                Connection_T con;
                url = URL_new(testURL);
                pool = ConnectionPool_new(url);
                assert(pool);
                ConnectionPool_setAbortHandler(pool, TabortHandler);
                ConnectionPool_start(pool);
                con = ConnectionPool_getConnection(pool);
                assert(con);
                TRY Connection_execute(con, "drop table zild_t;"); ELSE END_TRY;
                Connection_execute(con, "%s", schema);
                Connection_beginTransaction(con);
                /* Insert values into database and assume that auto increment of id works */
                for (i = 0; data[i]; i++) 
                        Connection_execute(con, "insert into zild_t (name, percent) values('%s', %d.%d);", data[i], i+1, i);
                // Assert that the last insert statement added one row
                assert(Connection_rowsChanged(con) == 1);
                /* Assert that last row id works for MySQL and SQLite. Neither Oracle nor PostgreSQL
                 support last row id directly. The way to do this in PostgreSQL is to use 
                 currval() or return the id on insert. */
                if (IS(URL_getProtocol(url), "sqlite") || IS(URL_getProtocol(url), "mysql")) 
                        assert(Connection_lastRowId(con) == 12);
                Connection_commit(con);
                printf("\tResult: table zild_t successfully created\n");
                Connection_close(con);
        }
        printf("=> Test4: OK\n\n");     
        
        
        printf("=> Test5: Prepared Statement\n");
        {
                int i;
                char blob[8192];
                char *images[]= {"Ceci n'est pas une pipe", "Mona Lisa",
                        "Bryllup i Hardanger", "The Scream",
                        "Vampyre", "Balcony", "Cycle", "Day & Night", 
                        "Hand with Reflecting Sphere",
                        "Drawing Hands", "Ascending and Descending", 0}; 
                Connection_T con = ConnectionPool_getConnection(pool);
                assert(con);
                // 1. Prepared statement, perform a nonsense update to test rowsChanged
                PreparedStatement_T p1 = Connection_prepareStatement(con, "update zild_t set image=?;");
                PreparedStatement_setString(p1, 1, "");
                PreparedStatement_execute(p1);
                printf("\tRows changed: %lld\n", PreparedStatement_rowsChanged(p1));
                // Assert that all 12 rows in the data set was changed
                assert(PreparedStatement_rowsChanged(p1) == 12);
                // 2. Prepared statement, update the table proper with "images". 
                PreparedStatement_T pre = Connection_prepareStatement(con, "update zild_t set image=? where id=?;");
                assert(pre);
                for (i = 0; images[i]; i++) {
                        PreparedStatement_setBlob(pre, 1, images[i], (int)strlen(images[i])+1);
                        PreparedStatement_setInt(pre, 2, i + 1);
                        PreparedStatement_execute(pre);
                }
                // The last execute changed one row only
                assert(PreparedStatement_rowsChanged(pre) == 1);
                /* Add a database null blob value */
                PreparedStatement_setBlob(pre, 1, NULL, 0);
                PreparedStatement_setInt(pre, 2, 5);
                PreparedStatement_execute(pre);
                /* Add a database null string value */
                PreparedStatement_setString(pre, 1, NULL);
                PreparedStatement_setInt(pre, 2, 1);
                PreparedStatement_execute(pre);
                /* Add a large blob */
                memset(blob, 'x', 8192);
                blob[8191] = 0;
                /* Mark start and end */
                *blob='S'; blob[8190] = 'E';
                PreparedStatement_setBlob(pre, 1, blob, 8192);
                PreparedStatement_setInt(pre, 2, i + 1);
                PreparedStatement_execute(pre);
                printf("\tResult: prepared statement successfully executed\n");
                Connection_close(con);
        }
        printf("=> Test5: OK\n\n");     
        
        
        printf("=> Test6: Result Sets\n");
        {
                int i;
                int imagesize = 0;
                Connection_T con;
                ResultSet_T rset;
                ResultSet_T names;
                PreparedStatement_T pre;
                con = ConnectionPool_getConnection(pool);
                assert(con);
                rset = Connection_executeQuery(con, "select id, name, percent, image from zild_t where id < %d order by id;", 100);
                assert(rset);
                printf("\tResult:\n");
                printf("\tNumber of columns in resultset: %d\n\t", ResultSet_getColumnCount(rset));
                assert(4==ResultSet_getColumnCount(rset));
                i = 1;
                printf("%-5s", ResultSet_getColumnName(rset, i++));
                printf("%-16s", ResultSet_getColumnName(rset, i++));
                printf("%-10s", ResultSet_getColumnName(rset, i++));
                printf("%-16s", ResultSet_getColumnName(rset, i++));
                printf("\n\t------------------------------------------------------\n");
                while (ResultSet_next(rset)) {
                        int id = ResultSet_getIntByName(rset, "id");
                        const char *name = ResultSet_getString(rset, 2);
                        double percent = ResultSet_getDoubleByName(rset, "percent");
                        const char *blob = (char*)ResultSet_getBlob(rset, 4, &imagesize);
                        printf("\t%-5d%-16s%-10.2f%-16.38s\n", id, name ? name : "null", percent, imagesize ? blob : "");
                }
                rset = Connection_executeQuery(con, "select image from zild_t where id=12;");
                assert(1==ResultSet_getColumnCount(rset));
                // Assert that types are interchangeable (to some degree) and that all data is returned
                while (ResultSet_next(rset)) {
                        const char *image = ResultSet_getStringByName(rset, "image");
                        const void *blob = ResultSet_getBlobByName(rset, "image", &imagesize);
                        assert(image && blob);
                        assert(strlen(image) + 1 == 8192);
                        assert(imagesize == 8192);
                }
                printf("\tResult: check max rows..");
                Connection_setMaxRows(con, 3);
                rset = Connection_executeQuery(con, "select id from zild_t;");
                assert(rset);
                i = 0;
                while (ResultSet_next(rset)) i++;
                assert((i)==3);
                printf("success\n");
                printf("\tResult: check prepared statement resultset..");
                Connection_setMaxRows(con, 0);
                pre = Connection_prepareStatement(con, "select name from zild_t where id=?");
                assert(pre);
                PreparedStatement_setInt(pre, 1, 2);
                names = PreparedStatement_executeQuery(pre);
                assert(names);
                assert(ResultSet_next(names));
                assert(Str_isEqual("Leela", ResultSet_getString(names, 1)));
                printf("success\n");
                printf("\tResult: check prepared statement re-execute..");
                PreparedStatement_setInt(pre, 1, 1);
                names = PreparedStatement_executeQuery(pre);
                assert(names);
                assert(ResultSet_next(names));
                assert(Str_isEqual("Fry", ResultSet_getString(names, 1)));
                printf("success\n");
                printf("\tResult: check prepared statement without in-params..");
                pre = Connection_prepareStatement(con, "select name from zild_t;");
                assert(pre);
                names = PreparedStatement_executeQuery(pre);
                assert(names);
                for (i = 0; ResultSet_next(names); i++);
                assert(i==12);
                printf("success\n");
                /* Need to close and release statements before
                   we can drop the table, sqlite need this */
                Connection_clear(con);
                Connection_execute(con, "drop table zild_t;");
                Connection_close(con);
                ConnectionPool_stop(pool);
                ConnectionPool_free(&pool);
                assert(pool==NULL);
                URL_free(&url);
        }
        printf("=> Test6: OK\n\n");
        
        
        printf("=> Test7: reaper start/stop\n");
        {
                int i;
                Vector_T v = Vector_new(20);
                url = URL_new(testURL);
                pool = ConnectionPool_new(url);
                assert(pool);
                ConnectionPool_setInitialConnections(pool, 4);
                ConnectionPool_setMaxConnections(pool, 20);
                ConnectionPool_setConnectionTimeout(pool, 4);
                ConnectionPool_setReaper(pool, 4);
                ConnectionPool_setAbortHandler(pool, TabortHandler);
                ConnectionPool_start(pool);
                assert(4==ConnectionPool_size(pool));
                printf("Creating 20 Connections..");
                for (i = 0; i<20; i++)
                        Vector_push(v, ConnectionPool_getConnection(pool));
                assert(ConnectionPool_size(pool) == 20);
                assert(ConnectionPool_active(pool) == 20);
                printf("success\n");
                printf("Closing Connections down to initial..");
                while (! Vector_isEmpty(v))
                        Connection_close(Vector_pop(v));
                assert(ConnectionPool_active(pool) == 0);
                assert(ConnectionPool_size(pool) == 20);
                printf("success\n");
                printf("Please wait 10 sec for reaper to harvest closed connections..");
                Connection_T con = ConnectionPool_getConnection(pool); // Activate one connection to verify the reaper does not close any active
                fflush(stdout);
                sleep(10);
                assert(5 == ConnectionPool_size(pool)); // 4 initial connections + the one active we got above
                assert(1 == ConnectionPool_active(pool));
                printf("success\n");
                Connection_close(con);
                ConnectionPool_stop(pool);
                ConnectionPool_free(&pool);
                Vector_free(&v);
                assert(pool==NULL);
                URL_free(&url);
        }
        printf("=> Test7: OK\n\n");

        printf("=> Test8: Exceptions handling\n");
        {
                int i;
                Connection_T con;
                ResultSet_T result;
                url = URL_new(testURL);
                pool = ConnectionPool_new(url);
                assert(pool);
                ConnectionPool_setAbortHandler(pool, TabortHandler);
                ConnectionPool_start(pool);
                con = ConnectionPool_getConnection(pool);
                assert(con);
                /* 
                 * The following should work without throwing exceptions 
                 */
                TRY
                {
                        Connection_execute(con, "%s", schema);
                }
                ELSE
                {
                        printf("\tResult: Creating table zild_t failed -- %s\n", Exception_frame.message);
                        assert(false); // Should not fail
                }
                END_TRY;
                TRY
                {
                        Connection_beginTransaction(con);
                        for (i = 0; data[i]; i++) 
                                Connection_execute(con, "insert into zild_t (name, percent) values('%s', %d.%d);", data[i], i+1, i);
                        Connection_commit(con);
                        printf("\tResult: table zild_t successfully created\n");
                }
                ELSE
                {
                        printf("\tResult: Test failed -- %s\n", Exception_frame.message);
                        assert(false); // Should not fail
                }
                FINALLY
                {
                        Connection_close(con);
                }
                END_TRY;
                assert((con = ConnectionPool_getConnection(pool)));
                TRY
                {
                        int i, j;
                        const char *bg[]= {"Starbuck", "Sharon Valerii",
                                "Number Six", "Gaius Baltar", "William Adama",
                                "Lee \"Apollo\" Adama", "Laura Roslin", 0};
                        PreparedStatement_T p = Connection_prepareStatement
                        (con, "insert into zild_t (name) values(?);");
                        /* If we did not get a statement, an SQLException is thrown
                           and we will not get here. So we can safely use the 
                           statement now. Likewise, below, we do not have to 
                           check return values from the statement since any error
                           will throw an SQLException and transfer the control
                           to the exception handler
                        */
                        for (i = 0, j = 42; bg[i]; i++, j++) {
                                PreparedStatement_setString(p, 1, bg[i]);
                                PreparedStatement_execute(p);
                        }
                }
                CATCH(SQLException)
                {
                        printf("\tResult: prepare statement failed -- %s\n", Exception_frame.message);
                        assert(false);
                }
                END_TRY;
                TRY
                {
                        printf("\t\tBattlestar Galactica: \n");
                        result = Connection_executeQuery(con, "select name from zild_t where id > 12;");
                        while (ResultSet_next(result))
                                printf("\t\t%s\n", ResultSet_getString(result, 1));
                }
                CATCH(SQLException)
                {
                        printf("\tResult: resultset failed -- %s\n", Exception_frame.message);
                       assert(false);
                }
                FINALLY
                {
                        Connection_close(con);
                }
                END_TRY;
                /* 
                 * The following should fail and throw exceptions. The exception error 
                 * message can be obtained with Exception_frame.message, or from 
                 * Connection_getLastError(con). Exception_frame.message contains both
                 * SQL errors or api errors such as prepared statement parameter index
                 * out of range, while Connection_getLastError(con) only has SQL errors
                 */
                TRY
                {
                        assert((con = ConnectionPool_getConnection(pool)));
                        Connection_execute(con, "%s", schema);
                        /* Creating the table again should fail and we 
                        should not come here */
                        printf("\tResult: Test failed -- exception not thrown\n");
                        exit(1);
                }
                CATCH(SQLException)
                {
                        Connection_close(con);
                }
                END_TRY;
                TRY
                {
                        assert((con = ConnectionPool_getConnection(pool)));
                        printf("\tTesting: Query with errors.. ");
                        Connection_executeQuery(con, "blablabala;");
                        printf("\tResult: Test failed -- exception not thrown\n");
                        exit(1);
                }
                CATCH(SQLException)
                {
                        printf("ok\n");
                        Connection_close(con);
                }
                END_TRY;
                TRY
                {
                        printf("\tTesting: Prepared statement query with errors.. ");
                        assert((con = ConnectionPool_getConnection(pool)));
                        PreparedStatement_T p = Connection_prepareStatement(con, "blablabala;");
                        ResultSet_T r = PreparedStatement_executeQuery(p);
                        while(ResultSet_next(r));
                        printf("\tResult: Test failed -- exception not thrown\n");
                        exit(1);
                }
                CATCH(SQLException)
                {
                        printf("ok\n");
                        Connection_close(con);
                }
                END_TRY;
                TRY
                {
                        assert((con = ConnectionPool_getConnection(pool)));
                        printf("\tTesting: Column index out of range.. ");
                        result = Connection_executeQuery(con, "select id, name from zild_t;");
                        while (ResultSet_next(result)) {
                                int id = ResultSet_getInt(result, 1);  
                                const char *name = ResultSet_getString(result, 2);
                                /* So far so good, now, try access an invalid
                                   column, which should throw an SQLException */
                                int bogus = ResultSet_getInt(result, 3);
                                printf("\tResult: Test failed -- exception not thrown\n");
                                printf("%d, %s, %d", id, name, bogus);
                                exit(1);
                        }
                }
                CATCH(SQLException)
                {
                        printf("ok\n");
                        Connection_close(con);
                }
                END_TRY;
                TRY
                {
                        assert((con = ConnectionPool_getConnection(pool)));
                        printf("\tTesting: Invalid column name.. ");
                        result = Connection_executeQuery(con, "select name from zild_t;");
                        while (ResultSet_next(result)) {
                                const char *name = ResultSet_getStringByName(result, "nonexistingcolumnname");
                                printf("%s", name);
                                printf("\tResult: Test failed -- exception not thrown\n");
                                exit(1);
                        }
                }
                CATCH(SQLException)
                {
                        printf("ok\n");
                        Connection_close(con);
                }
                END_TRY;
                TRY
                {
                        assert((con = ConnectionPool_getConnection(pool)));
                        PreparedStatement_T p = Connection_prepareStatement(con, "update zild_t set name = ? where id = ?;");
                        printf("\tTesting: Parameter index out of range.. ");
                        PreparedStatement_setInt(p, 3, 123);
                        printf("\tResult: Test failed -- exception not thrown\n");
                        exit(1);
                }
                CATCH(SQLException)
                {
                        printf("ok\n");
                }
                FINALLY
                {
                        Connection_close(con);
                }
                END_TRY;
                assert((con = ConnectionPool_getConnection(pool)));
                Connection_execute(con, "drop table zild_t;");
                Connection_close(con);
                ConnectionPool_stop(pool);
                ConnectionPool_free(&pool);
                assert(pool==NULL);
                URL_free(&url);
        }
        printf("=> Test8: OK\n\n");
        
        printf("=> Test9: Ensure Capacity\n");
        {
                /* Check that MySQL ensureCapacity works for columns that exceed the preallocated buffer and that no truncation is done */
                if ( Str_startsWith(testURL, "mysql")) {
                        int myimagesize;
                        url = URL_new(testURL);
                        pool = ConnectionPool_new(url);
                        assert(pool);
                        ConnectionPool_start(pool);
                        Connection_T con = ConnectionPool_getConnection(pool);
                        assert(con);
                        Connection_execute(con, "CREATE TABLE zild_t(id INTEGER AUTO_INCREMENT PRIMARY KEY, image BLOB, string TEXT);");
                        PreparedStatement_T p = Connection_prepareStatement(con, "insert into zild_t (image, string) values(?, ?);");
                        char t[4096];
                        memset(t, 'x', 4096);
                        t[4095] = 0;
                        for (int i = 0; i < 4; i++) {
                                PreparedStatement_setBlob(p, 1, t, (i+1)*512); // store successive larger string-blobs to trigger realloc on ResultSet_getBlobByName
                                PreparedStatement_setString(p, 2, t);
                                PreparedStatement_execute(p);
                        }
                        ResultSet_T r = Connection_executeQuery(con, "select image, string from zild_t;");
                        for (int i = 0; ResultSet_next(r); i++) {
                                ResultSet_getBlobByName(r, "image", &myimagesize);
                                const char *image = ResultSet_getStringByName(r, "image"); // Blob as image should be terminated
                                const char *string = ResultSet_getStringByName(r, "string");
                                assert(myimagesize == (i+1)*512);
                                assert(strlen(image) == ((i+1)*512));
                                assert(strlen(string) == 4095);
                        }
                        p = Connection_prepareStatement(con, "select image, string from zild_t;");
                        r = PreparedStatement_executeQuery(p);
                        for (int i = 0; ResultSet_next(r); i++) {
                                ResultSet_getBlobByName(r, "image", &myimagesize);
                                const char *image = ResultSet_getStringByName(r, "image");
                                const char *string = (char*)ResultSet_getStringByName(r, "string");
                                assert(myimagesize == (i+1)*512);
                                assert(strlen(image) == ((i+1)*512));
                                assert(strlen(string) == 4095);
                        }
                        Connection_execute(con, "drop table zild_t;");
                        Connection_close(con);
                        ConnectionPool_stop(pool);
                        ConnectionPool_free(&pool);
                        URL_free(&url);
                }
        }
        printf("=> Test9: OK\n\n");

        printf("============> Connection Pool Tests: OK\n\n");
}
Пример #23
0
int main (int argc, char *argv[]) {

	PKI_MEM_STACK *sk = NULL;
	PKI_MEM *obj = NULL;
	PKI_SSL *ssl = NULL;
	// PKI_TOKEN *tk = NULL;
	PKI_SOCKET *sock = NULL;

	URL * url = NULL;

	char *url_s = NULL;
	char *outurl_s = "fd://1";
	char *trusted_certs = NULL;
	char *dump_cert = NULL;
	char *dump_chain = NULL;

	int debug = 0;
	int verify_chain = 1;
	int i = 0;
	int timeout = 0;
	int get_via_socket = 0;

	PKI_init_all();

	if( !argv[1] ) {
		usage();
		return(1);
	}

	for( i = 1; i <= argc; i++ ) {
		if( strcmp_nocase( argv[i], "-out" ) == 0 ) {
			outurl_s = argv[++i];
		} else if ( strcmp_nocase ( argv[i], "-trusted" ) == 0 ) {
			trusted_certs = argv[++i];
		} else if ( strcmp_nocase ( argv[i], "-dumpcert" ) == 0 ) {
			if((dump_cert = argv[++i]) == NULL ) {
				fprintf(stderr, "\nERROR: -dumpcert needs a file url!\n\n");
				exit(1);
			}
		} else if ( strcmp_nocase ( argv[i], "-dumpchain" ) == 0 ) {
			if((dump_chain = argv[++i]) == NULL ) {
				fprintf(stderr, "\nERROR: -dumpchain needs a file url!\n\n");
				exit(1);
			}
		} else if ( strcmp_nocase ( argv[i], "-timeout" ) == 0 ) {
			timeout = atoi( argv[++i] );
			if ( timeout < 0 ) timeout = 0;
		} else if ( strcmp_nocase ( argv[i], "-no_verify" ) == 0 ) {
			verify_chain = 0;
		} else if ( strcmp_nocase( argv[i], "-debug" ) == 0 ) {
			debug = 1;
		} else {
			url_s = argv[i];
			if ( i < argc - 1 ) {
				fprintf( stderr, "Args after URL ignored!(%s %d/%d)\n",
					url_s, i, argc );
			}
			break;
		}
	}

	if((url = URL_new( url_s )) == NULL ) {
		printf("\nERROR, %s is not a valid URL!\n\n", url_s );

		usage();
		return (1);
	}

	if( debug ) {
		if(( PKI_log_init (PKI_LOG_TYPE_STDERR, PKI_LOG_INFO, NULL,
        	              PKI_LOG_FLAGS_ENABLE_DEBUG, NULL )) == PKI_ERR) {
        	        exit(1);
        	}
	} else {
		if(( PKI_log_init (PKI_LOG_TYPE_STDERR, PKI_LOG_INFO, NULL,
        	              0, NULL )) == PKI_ERR) {
        	        exit(1);
        	}
	}

	// Check if we should use the socket approach or the simple URL
	// retrieval facility
	switch (url->proto) {
		case URI_PROTO_FD:
		case URI_PROTO_FILE:
		case URI_PROTO_HTTP:
		case URI_PROTO_HTTPS:
		case URI_PROTO_LDAP:
			get_via_socket = 1;
			break;
		default:
			get_via_socket = 0;
	}

	//
	// -------------------------- Setup the SSL Options ------------------------
	//
	if(( ssl = PKI_SSL_new( NULL )) == NULL ) {
		fprintf(stderr, "ERROR: Memory allocation error (PKI_SSL_new)\n");
		return ( 1 );
	}

	if ( trusted_certs ) {
		PKI_X509_CERT_STACK *sk = NULL;

		if(( sk = PKI_X509_CERT_STACK_get ( trusted_certs, NULL, NULL))
								== NULL ) {
			PKI_log_err ("Can't load Trusted Certs from %s",
						trusted_certs );
			return 1;
		}		

		PKI_SSL_set_trusted ( ssl, sk );

		if ( verify_chain ) {
			PKI_SSL_set_verify(ssl, PKI_SSL_VERIFY_PEER_REQUIRE);
		} else {
			PKI_SSL_set_verify(ssl, PKI_SSL_VERIFY_PEER);
		}
	}

	if ( verify_chain == 0 ) {
			PKI_SSL_set_verify ( ssl, PKI_SSL_VERIFY_NONE );
			fprintf(stderr, "WARNING: no verify set!\n");
	}

	if(( sock = PKI_SOCKET_new ()) == NULL ) {
		fprintf(stderr, "ERROR, can not create a new Socket!\n\n");
		exit(1);
	}

	PKI_SOCKET_set_ssl ( sock, ssl );

	//
	// ------------------------------ Retrieve Data -----------------------------
	//
	if (get_via_socket) {

		if( PKI_SOCKET_open( sock, url_s, timeout ) == PKI_ERR ) {
			fprintf(stderr, "ERROR, can not connect to %s!\n\n", url_s);
			exit(1);
		}

		ssl = PKI_SOCKET_get_ssl (sock);

		if (dump_cert) { 
			PKI_X509_CERT *x = NULL;

			if ( !ssl ) {
				fprintf( stderr, 
					"ERROR: Can not dump cert (no SSL)\n");
			}

			if((x = PKI_SSL_get_peer_cert ( ssl )) == NULL ) {
				fprintf( stderr,
					"ERROR: No Peer certificate is available\n");
			}

			if( PKI_X509_CERT_put ( x, PKI_DATA_FORMAT_PEM,
					dump_cert, NULL, NULL, NULL ) == PKI_ERR){
				fprintf(stderr, "ERROR: can not write Peer cert to "
					"%s\n", dump_cert );
			}
		}

		if (dump_chain) { 
			PKI_X509_CERT_STACK *x_sk = NULL;
	
			if ( !ssl ) {
				fprintf( stderr, 
					"ERROR: Can not dump cert (no SSL)\n");
			}

			if((x_sk = PKI_SSL_get_peer_chain ( ssl )) == NULL ) {
				fprintf( stderr,
					"ERROR: No certificate chain is available\n");
			}

			if( PKI_X509_CERT_STACK_put ( x_sk, PKI_DATA_FORMAT_PEM,
					dump_chain, NULL, NULL, NULL ) == PKI_ERR){
				fprintf(stderr, "ERROR: can not write Peer cert to "
					"%s\n", dump_cert );
			}
		}

		if((sk = URL_get_data_socket ( sock, timeout, 0 )) == NULL ) {
			fprintf(stderr, "ERROR, can not retrieve data!\n\n");
			return(-1);
		}

		PKI_SOCKET_close ( sock );
		PKI_SOCKET_free ( sock );
	}
	else // Get Data via the usual URL socket-less approach
	{
		sk = URL_get_data_url (url, timeout, 0, ssl);
	}

	PKI_log_debug("URL: Number of retrieved entries is %d",
		PKI_STACK_MEM_elements(sk));

	while( (obj = PKI_STACK_MEM_pop ( sk )) != NULL ) {
		URL_put_data ( outurl_s, obj, NULL, NULL, 0, 0, NULL );
	}

	return 0;
}
Пример #24
0
PKI_X509_PRQP_RESP * PKI_DISCOVER_get_resp_url ( PKI_X509_PRQP_REQ *p, URL *url ) {

	PKI_X509_PRQP_RESP * ret = NULL;

	char line[1024], name[1024], addr[1024];

        FILE *file;

	if( !p || !p->value ) {
		PKI_log_debug( "WARNING, no PRQP request when trying to get"
				" the response!");
		return ( NULL );
	}

	if( url ) {
		if (( ret = PKI_X509_PRQP_RESP_get_http ( url, p, 0)) != NULL ) {
			return ret;
		} else {
			return NULL;
		}
	}

        file = fopen( PKI_PRQP_LIB_CONF_FILE, "r");
        if( !file ) {
		PKI_log_debug( "WARNING, PRQP config file %s not found!",
			PKI_PRQP_LIB_CONF_FILE );
		return ( NULL );
        }

       	while(!feof(file)) {
               	if( fgets(line, sizeof(line), file) ) {
                       	if((memcmp(line, ";", 1) == 0) || 
					(memcmp(line, "#", 1) == 0))
                                		continue;

			if(sscanf(line, "%1024s %1024s", name, addr ) > 1 ) {
				char *full_url_s = NULL;
				size_t full_len = 0;

				if((strcmp_nocase( name, 
					PKI_PRQP_LIB_CONF_ENTRY_LONG)==0) ||
						(strcmp_nocase ( name, 
						   PKI_PRQP_LIB_CONF_ENTRY_SHORT ) 
									== 0)) {

					URL *l_url = NULL;

					full_len = sizeof( addr ) + 12;
					full_url_s = PKI_Malloc ( full_len );
					snprintf( full_url_s, full_len, "http://%s", addr );
					if ( strchr ( addr, ':') == NULL ) {
						strncat ( full_url_s, ":830", full_len );
					}

					PKI_log_debug( "Trying PRQP RQA -> %s",
								full_url_s );

					if((l_url = URL_new( full_url_s )) == NULL) {
						PKI_log_debug("Can not parse address %s",
							full_url_s );
						PKI_Free ( full_url_s );
						continue;
					}

					if( l_url->port <= 0 ) 
						l_url->port = PKI_PRQP_DEFAULT_PORT;

					l_url->proto = URI_PROTO_HTTP;
					ret = PKI_X509_PRQP_RESP_get_http ( l_url, p, 0);

					PKI_Free ( full_url_s );

					if( ret == NULL ) {
						PKI_log( PKI_LOG_ERR,
							"Can not get response "
							"from server (%s:%d)!",
								l_url->addr, 
								l_url->port);
						URL_free ( l_url );
					} else {
						/* Exit the cycle */
						PKI_log_debug("Got PRQP response from server");
						URL_free ( l_url );
        					fclose(file);
						return ret;
					}
				}
                	}
        	}
	}

        fclose(file);

	return ret;
}