dbus_bool_t retval;
int line_no;
DBusAuth *auth;
- DBusAuthorization *authorization;
DBusString from_auth;
DBusAuthState state;
DBusString context;
retval = FALSE;
auth = NULL;
- authorization = NULL;
_dbus_string_init_const (&guid, "5fa01f4202cd837709a3274ca0df9d00");
_dbus_string_init_const (&context, "org_freedesktop_test");
goto out;
}
+ /* test ref/unref */
+ _dbus_auth_ref (auth);
+ _dbus_auth_unref (auth);
+
creds = _dbus_credentials_new_from_current_process ();
if (creds == NULL)
{
_dbus_warn ("no memory for credentials\n");
+ _dbus_auth_unref (auth);
+ auth = NULL;
goto out;
}
if (!_dbus_auth_set_credentials (auth, creds))
{
_dbus_warn ("no memory for setting credentials\n");
+ _dbus_auth_unref (auth);
+ auth = NULL;
_dbus_credentials_unref (creds);
goto out;
}
_dbus_string_starts_with_c_str (&line, "SERVER_ANONYMOUS"))
{
DBusCredentials *creds;
+ DBusAuthorization *authorization;
if (auth != NULL)
{
_dbus_authorization_set_allow_anonymous (authorization, TRUE);
auth = _dbus_auth_server_new (&guid, authorization);
+ /* DBusAuth owns it, or finalized on OOM */
+ _dbus_authorization_unref (authorization);
if (auth == NULL)
{
_dbus_warn ("no memory to create DBusAuth\n");
goto out;
}
+ /* test ref/unref */
+ _dbus_auth_ref (auth);
+ _dbus_auth_unref (auth);
+
creds = _dbus_credentials_new_from_current_process ();
if (creds == NULL)
{
_dbus_warn ("no memory for credentials\n");
+ _dbus_auth_unref (auth);
+ auth = NULL;
goto out;
}
if (!_dbus_auth_set_credentials (auth, creds))
{
_dbus_warn ("no memory for setting credentials\n");
+ _dbus_auth_unref (auth);
+ auth = NULL;
_dbus_credentials_unref (creds);
goto out;
}
out:
if (auth)
- _dbus_auth_free (auth);
- if (authorization)
- _dbus_authorization_free (authorization);
+ _dbus_auth_unref (auth);
_dbus_string_free (&file);
_dbus_string_free (&line);
*/
struct DBusAuth
{
+ int refcount; /**< reference count */
const char *side; /**< Client or server */
DBusString incoming; /**< Incoming data buffer */
if (auth == NULL)
return NULL;
+ auth->refcount = 1;
+
auth->keyring = NULL;
auth->cookie_id = -1;
*/
DBusAuth*
_dbus_auth_server_new (const DBusString *guid,
- DBusAuthorization *authorization)
+ DBusAuthorization *authorization)
{
DBusAuth *auth;
DBusAuthServer *server_auth;
server_auth = DBUS_AUTH_SERVER (auth);
server_auth->guid = guid_copy;
- server_auth->authorization = authorization;
+ server_auth->authorization = _dbus_authorization_ref (authorization);
/* perhaps this should be per-mechanism with a lower
* max
* mechanism */
if (!send_auth (auth, &all_mechanisms[0]))
{
- _dbus_auth_free (auth);
+ _dbus_auth_unref (auth);
return NULL;
}
}
/**
- * Free memory allocated for an auth object.
+ * Increments the refcount of an auth object.
*
* @param auth the auth conversation
+ * @returns the auth conversation
*/
-void
-_dbus_auth_free (DBusAuth *auth)
+DBusAuth *
+_dbus_auth_ref (DBusAuth *auth)
{
_dbus_assert (auth != NULL);
+
+ auth->refcount += 1;
+
+ return auth;
+}
- shutdown_mech (auth);
+/**
+ * Decrements the refcount of an auth object.
+ *
+ * @param auth the auth conversation
+ */
+void
+_dbus_auth_unref (DBusAuth *auth)
+{
+ _dbus_assert (auth != NULL);
+ _dbus_assert (auth->refcount > 0);
- if (DBUS_AUTH_IS_CLIENT (auth))
+ auth->refcount -= 1;
+ if (auth->refcount == 0)
{
- _dbus_string_free (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
- _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
- }
- else
- {
- _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
+ shutdown_mech (auth);
- _dbus_string_free (& DBUS_AUTH_SERVER (auth)->guid);
- }
+ if (DBUS_AUTH_IS_CLIENT (auth))
+ {
+ _dbus_string_free (& DBUS_AUTH_CLIENT (auth)->guid_from_server);
+ _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
+ }
+ else
+ {
+ _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
- if (auth->keyring)
- _dbus_keyring_unref (auth->keyring);
+ _dbus_string_free (& DBUS_AUTH_SERVER (auth)->guid);
+ _dbus_authorization_unref (DBUS_AUTH_SERVER (auth)->authorization);
+ }
- _dbus_string_free (&auth->context);
- _dbus_string_free (&auth->challenge);
- _dbus_string_free (&auth->identity);
- _dbus_string_free (&auth->incoming);
- _dbus_string_free (&auth->outgoing);
+ if (auth->keyring)
+ _dbus_keyring_unref (auth->keyring);
- dbus_free_string_array (auth->allowed_mechs);
+ _dbus_string_free (&auth->context);
+ _dbus_string_free (&auth->challenge);
+ _dbus_string_free (&auth->identity);
+ _dbus_string_free (&auth->incoming);
+ _dbus_string_free (&auth->outgoing);
- _dbus_credentials_unref (auth->credentials);
- _dbus_credentials_unref (auth->authenticated_identity);
- _dbus_credentials_unref (auth->desired_identity);
+ dbus_free_string_array (auth->allowed_mechs);
- dbus_free (auth);
+ _dbus_credentials_unref (auth->credentials);
+ _dbus_credentials_unref (auth->authenticated_identity);
+ _dbus_credentials_unref (auth->desired_identity);
+
+ dbus_free (auth);
+ }
}
/**
DBusAuth* _dbus_auth_server_new (const DBusString *guid,
DBusAuthorization *authorization);
DBusAuth* _dbus_auth_client_new (void);
-void _dbus_auth_free (DBusAuth *auth);
+DBusAuth* _dbus_auth_ref (DBusAuth *auth);
+void _dbus_auth_unref (DBusAuth *auth);
dbus_bool_t _dbus_auth_set_mechanisms (DBusAuth *auth,
const char **mechanisms);
DBusAuthState _dbus_auth_do_work (DBusAuth *auth);
#include "dbus-connection-internal.h"
struct DBusAuthorization {
+ int refcount;
+
DBusConnection *connection;
/* Authorization functions, used as callback by SASL (implemented by
DBusAuthorization *
_dbus_authorization_new (void)
{
- /* it returns the allocated memory or NULL in case of OOM */
- return dbus_malloc0 (sizeof (DBusAuthorization));
+ DBusAuthorization *ret;
+
+ ret = dbus_malloc0 (sizeof (DBusAuthorization));
+ if (ret == NULL)
+ {
+ _dbus_verbose ("OOM\n");
+ return NULL; /* OOM */
+ }
+
+ ret->refcount = 1;
+
+ return ret;
+}
+
+DBusAuthorization *
+_dbus_authorization_ref (DBusAuthorization *self)
+{
+ _dbus_assert (self != NULL);
+
+ self->refcount += 1;
+
+ return self;
}
void
-_dbus_authorization_free (DBusAuthorization *self)
+_dbus_authorization_unref (DBusAuthorization *self)
{
_dbus_assert (self != NULL);
+ _dbus_assert (self->refcount > 0);
- if (self->unix_data && self->unix_data_free)
- {
- _dbus_verbose ("freeing unix authorization callback data\n");
- (*self->unix_data_free) (self->unix_data);
- self->unix_data = NULL;
- }
+ self->refcount -= 1;
- if (self->windows_data && self->windows_data_free)
+ if (self->refcount == 0)
{
- _dbus_verbose ("freeing windows authorization callback data\n");
- (*self->windows_data_free) (self->windows_data);
- self->windows_data = NULL;
+ _dbus_verbose ("last reference, finalizing\n");
+
+ if (self->unix_data && self->unix_data_free)
+ {
+ _dbus_verbose ("freeing unix authorization callback data\n");
+ (*self->unix_data_free) (self->unix_data);
+ self->unix_data = NULL;
+ }
+
+ if (self->windows_data && self->windows_data_free)
+ {
+ _dbus_verbose ("freeing windows authorization callback data\n");
+ (*self->windows_data_free) (self->windows_data);
+ self->windows_data = NULL;
+ }
+
+ dbus_free (self);
}
-
- _dbus_verbose ("freeing memory for %p\n", self);
- dbus_free (self);
}
/* Called by transport's set_connection with the connection locked */
_dbus_authorization_set_connection (DBusAuthorization *self,
DBusConnection *connection)
{
- _dbus_assert (self != NULL);
_dbus_assert (connection != NULL);
_dbus_assert (self->connection == NULL);
void **old_data,
DBusFreeFunction *old_free_data_function)
{
- _dbus_assert (self != NULL);
-
*old_data = self->unix_data;
*old_free_data_function = self->unix_data_free;
void **old_data,
DBusFreeFunction *old_free_data_function)
{
- _dbus_assert (self != NULL);
-
*old_data = self->windows_data;
*old_free_data_function = self->windows_data_free;
/* Dropping the lock here probably isn't that safe. */
- _dbus_assert (self != NULL);
_dbus_assert (auth_identity != NULL);
uid = _dbus_credentials_get_unix_uid (auth_identity);
/* Dropping the lock here probably isn't that safe. */
- _dbus_assert (self != NULL);
_dbus_assert (auth_identity != NULL);
windows_sid = _dbus_strdup (_dbus_credentials_get_windows_sid (auth_identity));
DBusCredentials *our_identity;
dbus_bool_t allow;
- _dbus_assert (self != NULL);
_dbus_assert (auth_identity != NULL);
/* By default, connection is allowed if the client is 1) root or 2)
{
dbus_bool_t allow;
- _dbus_assert (self != NULL);
-
/* maybe-FIXME: at this point we *should* have a connection set unless we
* are in some test case, but we assert its presence only in some if's
* branches since default_rules does not need one and is used in a test case
_dbus_authorization_set_allow_anonymous (DBusAuthorization *self,
dbus_bool_t value)
{
- _dbus_assert (self != NULL);
-
self->allow_anonymous = value != FALSE;
}
DBusAuthorization *_dbus_authorization_new (void);
void _dbus_authorization_set_connection (DBusAuthorization *self,
DBusConnection *connection);
-void _dbus_authorization_free (DBusAuthorization *self);
+DBusAuthorization * _dbus_authorization_ref (DBusAuthorization *self);
+void _dbus_authorization_unref (DBusAuthorization *self);
void _dbus_authorization_set_unix_authorization_callback (DBusAuthorization *self,
DBusAllowUnixUserFunction function, void *data,
DBusFreeFunction free_data_function, void **old_data,
if (auth == NULL)
{
if (authorization != NULL)
- _dbus_authorization_free (authorization);
+ _dbus_authorization_unref (authorization);
_dbus_message_loader_unref (loader);
return FALSE;
}
counter = _dbus_counter_new ();
if (counter == NULL)
{
- _dbus_auth_free (auth);
+ _dbus_auth_unref (auth);
if (authorization != NULL)
- _dbus_authorization_free (authorization);
+ _dbus_authorization_unref (authorization);
_dbus_message_loader_unref (loader);
return FALSE;
}
if (creds == NULL)
{
_dbus_counter_unref (counter);
- _dbus_auth_free (auth);
+ _dbus_auth_unref (auth);
if (authorization != NULL)
- _dbus_authorization_free (authorization);
+ _dbus_authorization_unref (authorization);
_dbus_message_loader_unref (loader);
return FALSE;
}
{
_dbus_credentials_unref (creds);
_dbus_counter_unref (counter);
- _dbus_auth_free (auth);
+ _dbus_auth_unref (auth);
if (authorization != NULL)
- _dbus_authorization_free (authorization);
+ _dbus_authorization_unref (authorization);
_dbus_message_loader_unref (loader);
return FALSE;
}
_dbus_transport_disconnect (transport);
_dbus_message_loader_unref (transport->loader);
- _dbus_auth_free (transport->auth);
+ _dbus_auth_unref (transport->auth);
if (transport->authorization)
- _dbus_authorization_free (transport->authorization);
+ _dbus_authorization_unref (transport->authorization);
_dbus_counter_set_notify (transport->live_messages,
0, 0, NULL, NULL);
_dbus_counter_unref (transport->live_messages);