Issues while using dbus_connection_set_allow_anonymous for anonymous access

remya soman remyasomancs at gmail.com
Thu Aug 7 20:19:05 PDT 2008


Hi,



I am using DBUS over TCP for communication between two remote machines.

I am not using dbus-daemon and  have a custom dbus server and client
instead.



The issue I am facing is when I use
dbus_connection_set_allow_anonymous(conn,TRUE) for enabling anonymous only
access mode, the client is not able to send data and the connection is lost
during dbus_connection_flush.



If I comment out this line, DBUS_COOKIE_SHA1 takes place and from the same
machine and as the same user I am able to communicate between the client and
server.



When I enable verbose, the following message is shown at the server.



13972: server: Sent 46 bytes of: REJECTED EXTERNAL DBUS_COOKIE_SHA1
ANONYMOUS



When the DBUS_COOKIE_SHA1 authentication takes place the corresponding
message is :



13958: server: Sent 141 bytes of: DATA
6f72675f667265656465736b746f705f67656e6572616c2031323631363231303438203164363166373463633338626134636330623963623730353966346235353463



Why am I not able to get an anonymous access to the server even if that is
enabled?



My code looks like:



#define DBUS_API_SUBJECT_TO_CHANGE

#include <stdio.h>

#include <stdlib.h>

#include <dbus/dbus.h>

#include <glib.h>

#include <dbus/dbus-glib-lowlevel.h>



#define USAGE "usage %s client|server addr (f)\n"

#define BUFSIZE 10



int client(char * addr) {

        char * buffer;

        int i=0;

        DBusConnection* con;

        DBusError error;



        buffer = malloc(BUFSIZE);



        dbus_error_init (&error);



        con = dbus_connection_open(addr, &error);



        if (con == NULL)

        {

                fprintf (stderr, "could not open connection: %s\n",
error.message);

                dbus_error_free (&error);

                return 1;

        }



        for(;;) {



                DBusMessage *message;



                message = dbus_message_new_signal("/p","my.i","n");

                size_t blen = fread (buffer, 1, BUFSIZE, stdin);



                if (blen==0) break;

               dbus_message_append_args(message,

                        DBUS_TYPE_INT32, &i, DBUS_TYPE_ARRAY,

                        DBUS_TYPE_BYTE,

                        &buffer, blen,

                        DBUS_TYPE_INVALID

                );



                if (!dbus_connection_send (con,

                                        message,

                                        NULL))

                {

                        fprintf(stderr, "mes %d: send message failed\n", i);

                }



                dbus_message_unref (message);

                dbus_connection_flush(con) ;



                if (!dbus_connection_get_is_connected(con))

                {

                        fprintf(stderr, "connection not connected!\n");

                        exit(1);

                }



                i++;

        }



        dbus_connection_close(con);

        fprintf(stderr, "finished=%d\n", i);

}

static void

new_connection_callback (DBusServer     *server,

                        DBusConnection *new_connection,

                        void           *user_data)

{



        char * buffer;

        int len;



        fprintf (stderr, "new_connection_callback\n");

*       dbus_connection_set_allow_anonymous (new_connection, TRUE);*



        dbus_connection_ref (new_connection);



        while (dbus_connection_read_write_dispatch (new_connection, -1))

        {

                /* use dbus_connection_read_write(new_connection, -1))
instead? */

                DBusMessage* m;

                while (m=dbus_connection_pop_message(new_connection)) {

                        int ival;



                        if (!dbus_message_get_args(m, NULL, DBUS_TYPE_INT32,
&ival, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &buffer, &len, DBUS_TYPE_INVALID))

                        {

                                fprintf(stderr, "message error name=%s\n",
dbus_message_get_error_name(m));

                        }

                        else

                        {



                                fwrite (buffer, 1, len, stdout);

                        }

                        dbus_message_unref (m);



                }

        }



        fflush(stdout);



        fprintf (stderr, "end connection\n");

        exit(0);

}



static int

get_port(DBusServer *server)

{

    char *address;

    DBusAddressEntry **entries;

    int n_entries;

    const char *port_str;

    int port;



    address = dbus_server_get_address(server);



    if (!dbus_parse_address (address,

                             &entries,

                             &n_entries,

                             NULL) ||

        n_entries < 1)

        g_error("libdbus could not parse its own address '%s'", address);



    /* libdbus doesn't really guarantee this but it should be ok to assume
*/

    g_assert(strcmp(dbus_address_entry_get_method(entries[0]), "tcp") == 0);



    port_str = dbus_address_entry_get_value (entries[0], "port");

    if (port_str == NULL)

        g_error("libdbus returned no port in tcp address entry");



    port = atoi (port_str);

    g_assert (port > 0);



    dbus_address_entries_free (entries);

    dbus_free(address);



    return port;

}



int server(char * addr) {

        GMainLoop *loop;

        DBusServer *server;

        GMainContext *context;



        DBusError error;

            int listening_on_port;



            const char *auth_mechanisms[] = { "ANONYMOUS" };



        printf("In server\n");

        dbus_error_init (&error);



            server = dbus_server_listen(addr, &error);

            if (server == NULL) {

            g_printerr("Error listening on TCP: %s\n", error.message);

            return FALSE;

            }



    g_assert(dbus_server_get_is_connected(server));



    *dbus_server_set_auth_mechanisms(server, auth_mechanisms);*

    dbus_server_setup_with_g_main(server, NULL);



    /* Allow only anonymous auth, don't even attempt user auth

     */

    dbus_server_set_new_connection_function(server, new_connection_callback,
NULL, NULL);



    listening_on_port = get_port(server);

    printf("server is listening on port %d\n",listening_on_port);



    loop = g_main_loop_new (NULL, FALSE);

    g_main_run (loop);



}



int main(int argc, char** argv)

{

        if (argc < 3)

        {



                printf("args error\n");

                //fprintf (stderr, USAGE, argv[0]);

                return 1;

        }

        if (strcmp(argv[1], "server")==0) return server(argv[2]);

        if (strcmp(argv[1], "client")==0) return client(argv[2]);



        //printf (stderr, USAGE, argv[0]);

        return 1;



}


 Regards,
Remya
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freedesktop.org/archives/dbus/attachments/20080808/ac5d4400/attachment-0001.html 


More information about the dbus mailing list