1

I am trying to write a simple Ada (with AWS) program to post data to a server. The curl command is working as follows and return a valid response in JSON after successful login:

curl -XPOST -d '{"type":"m.login.password", "user":"xxx", "password": "xxxxxxxxxx"}' "https://matrix.org/_matrix/client/r0/login"

My Ada program:

with Ada.Exceptions;    use Ada.Exceptions;
with Ada.Text_Io;       use Ada.Text_IO;

with AWS.Client;
with AWS.Communication.Client;
with AWS.MIME;
with AWS.Net;
with AWS.Response;

use AWS;


procedure Communicate is

   Result : Response.Data;
   Data   : String := "{""type"":""m.login.password"", ""user"":""xxx"", ""password"": ""xxxxxxxxxx""}";

begin

   Result := Client.Post
      ( URL => "https://matrix.org/_matrix/client/r0/login",
        Data => Data,
        Content_Type => AWS.MIME.Application_JSON ) ;

   Put_Line ( Response.Message_Body ( Result ) ) ;   

end Communicate;

An exception was raised. I can't figure out what is wrong with this code.

$ ./Communicate

raised PROGRAM_ERROR : aws-client.adb:543 finalize/adjust raised exception

To test the code, you can create an account at http://matrix.org and replace the login credential.

Thanks.

Adrian

Adrian Hoe
  • 2,081
  • 2
  • 12
  • 12
  • Does your AWS include SSL support? FWIW, I see the same message and debugging points at an `/users/bauhaus/src/aws/src/core/aws-client.adb:210` calling `initialize` in AWS.NET.SSL (aws-net-ssl__dummy.adb:148), which package claims that "Every use of this interface will raise the Program_Error exception." – B98 Jan 08 '17 at 17:57
  • How do I check if AWS includes SSL support? – Adrian Hoe Jan 08 '17 at 18:00
  • After the fact, that is, after installation, I'd first look at the other libraries required by libaws. For example, by running `xcrun dyldinfo -dylibs` on a Mac, or `ldd` on GNU/Linux. (The second lists libgnutls.) Otherwise, there should be some log of the installation process including that information. – B98 Jan 08 '17 at 18:22
  • It doesn't looks like SSL is included: $ xcrun dyldinfo -dylibs /usr/local/gnat/lib/libaws.dylib attributes dependent dylibs @rpath/libgnarl-2016.dylib @rpath/libgnat-2016.dylib @rpath/libxmlada_schema.dylib @rpath/libxmlada_dom.dylib @rpath/libxmlada_sax.dylib @rpath/libxmlada_input_sources.dylib @rpath/libxmlada_unicode.dylib /usr/lib/libz.1.dylib /usr/lib/libSystem.B.dylib – Adrian Hoe Jan 09 '17 at 00:40
  • How can I make sure SSL is included? I remembered I could make configure. – Adrian Hoe Jan 09 '17 at 00:46
  • Strangely, I can't locate libels_ssl.dylib in instal directory. It is in the AWS .build directory. – Adrian Hoe Jan 09 '17 at 01:28
  • @B98 Looking at aws-net-ssl__dummy.adb:148, it will raise a PROGRAM_ERROR with an error message "SSL not supported." I didn't get this "SSL not supported" error. Strange things I can't figure out. – Adrian Hoe Jan 09 '17 at 02:12
  • Mac notes: Building AWS with OpenSSL support on this target does not seem to work so well. Apple's openssl library lists symbols different from those imported in AWS. A new OpenSSL (with -lcrypto), while found if added via `-I` and `-L` to `CFLAGS` and `LDFLAGS`, is not compatible in different ways. Also, as per "crypto.h", several macros have been dropped without replacement, but AWS is still referring to them, so it will find more symbols to be undefined. – B98 Jan 10 '17 at 08:22

2 Answers2

1

After a few minor changes (mostly because I don't like compiler warnings), and an adaption to the Debian/Jessie version of AWS, I got it to work.

Here's the adapted version:

with Ada.Text_IO;       use Ada.Text_IO;

with AWS.Client;
--  with AWS.MIME;
with AWS.Response;

use AWS;

procedure Communicate is

   Result : Response.Data;
   Data   : constant String :=
                       "{""type"":""m.login.password"", ""user"":""xxx"", " &
                       """password"": ""xxxxxxxxxx""}";

begin
   Result := Client.Post
      (URL          => "https://matrix.org/_matrix/client/r0/login",
       Data         => Data,
       Content_Type => "application/json");
   --  Content_Type => AWS.MIME.Application_JSON);

   Put_Line (Response.Message_Body (Result));
end Communicate;

Here is my project file:

with "aws";

project Communicate is
   for Main use ("communicate");

   package Builder is
      for Default_Switches ("Ada")
        use ("-m");
   end Builder;

   package Compiler is
      for Default_Switches ("Ada")
        use ("-fstack-check", --  Generate stack checking code (part of Ada)
             "-gnata",        --  Enable assertions            (part of Ada)
             "-gnato13",      --  Overflow checking            (part of Ada)
             "-gnatf",                      --  Full, verbose error messages
             "-gnatwa",                     --  All optional warnings
             "-gnatVa",                     --  All validity checks
             "-gnaty3abcdefhiklmnoOprstux", --  Style checks
             "-gnatwe",                     --  Treat warnings as errors
             "-gnat2012",                   --  Use Ada 2012
             "-Wall",                       --  All GCC warnings
             "-O2");                        --  Optimise (level 2/3)
   end Compiler;
end Communicate;

I built the program with:

% gprbuild -P communicate
gnatgcc -c -fstack-check -gnata -gnato13 -gnatf -gnatwa -gnatVa -gnaty3abcdefhiklmnoOprstux -gnatwe -gnat2012 -Wall -O2 communicate.adb
gprbind communicate.bexch
gnatbind communicate.ali
gnatgcc -c b__communicate.adb
gnatgcc communicate.o -L/usr/lib/x86_64-linux-gnu -lgnutls -lz -llber -lldap -lpthread -o communicate
%

And then tested with:

% ./communicate
{"errcode":"M_FORBIDDEN","error":"Invalid password"}
%

It looks like the problem is located in your AWS version/installation.

Jacob Sparre Andersen
  • 6,733
  • 17
  • 22
1

Problem resolved by building AWS with gnutls from MacPorts. Apple deprecated OpenSSL since OS X Lion and used CommonCrypto so modern macOS does not come with OpenSSL. The solution is to download and install OpenSSL or gnutls from Mac Ports or Home Brew.

Another problem is that Apple introduced SIP (System Integrity Protection) since El Capitan. With SIP enabled, user with administrator's rights is unable to change the contents in /usr/include and /usr/lib etc.

Mac Ports installs to /opt/local so I made references to /opt/local/include and /opt/local/lib so that AWS can build with either OpenSSL or gnutls.

Adrian Hoe
  • 2,081
  • 2
  • 12
  • 12