Imported Upstream version 2.55.1
[platform/upstream/glib.git] / gio / gdbusauthmechanismsha1.c
index bc9ed25..0cbaf94 100644 (file)
@@ -1,11 +1,11 @@
 /* GDBus - GLib D-Bus Library
  *
- * Copyright (C) 2008-2009 Red Hat, Inc.
+ * Copyright (C) 2008-2010 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -13,9 +13,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Author: David Zeuthen <davidz@redhat.com>
  */
 #include "config.h"
 
 #include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/types.h>
 
-#include "gdbusauthmechanismsha1.h"
-#include "gcredentials.h"
-#include "gdbuserror.h"
-#include "gioenumtypes.h"
-#include "gioerror.h"
+#include <glib/gstdio.h>
 
 #ifdef G_OS_UNIX
 #include <unistd.h>
-#include <sys/types.h>
-#include <fcntl.h>
+#endif
+#ifdef G_OS_WIN32
+#include <io.h>
 #endif
 
-#include <errno.h>
-
-#include <glib/gstdio.h>
+#include "gdbusauthmechanismsha1.h"
+#include "gcredentials.h"
+#include "gdbuserror.h"
+#include "gioenumtypes.h"
+#include "gioerror.h"
+#include "gdbusprivate.h"
 
 #include "glibintl.h"
 
@@ -91,7 +92,7 @@ static void                     mechanism_client_shutdown           (GDBusAuthMe
 
 /* ---------------------------------------------------------------------------------------------------- */
 
-G_DEFINE_TYPE (GDBusAuthMechanismSha1, _g_dbus_auth_mechanism_sha1, G_TYPE_DBUS_AUTH_MECHANISM);
+G_DEFINE_TYPE_WITH_PRIVATE (GDBusAuthMechanismSha1, _g_dbus_auth_mechanism_sha1, G_TYPE_DBUS_AUTH_MECHANISM)
 
 /* ---------------------------------------------------------------------------------------------------- */
 
@@ -115,8 +116,6 @@ _g_dbus_auth_mechanism_sha1_class_init (GDBusAuthMechanismSha1Class *klass)
   GObjectClass *gobject_class;
   GDBusAuthMechanismClass *mechanism_class;
 
-  g_type_class_add_private (klass, sizeof (GDBusAuthMechanismSha1Private));
-
   gobject_class = G_OBJECT_CLASS (klass);
   gobject_class->finalize = _g_dbus_auth_mechanism_sha1_finalize;
 
@@ -142,9 +141,7 @@ _g_dbus_auth_mechanism_sha1_class_init (GDBusAuthMechanismSha1Class *klass)
 static void
 _g_dbus_auth_mechanism_sha1_init (GDBusAuthMechanismSha1 *mechanism)
 {
-  mechanism->priv = G_TYPE_INSTANCE_GET_PRIVATE (mechanism,
-                                                 G_TYPE_DBUS_AUTH_MECHANISM_SHA1,
-                                                 GDBusAuthMechanismSha1Private);
+  mechanism->priv = _g_dbus_auth_mechanism_sha1_get_instance_private (mechanism);
 }
 
 /* ---------------------------------------------------------------------------------------------------- */
@@ -258,12 +255,13 @@ ensure_keyring_directory (GError **error)
           struct stat statbuf;
           if (stat (path, &statbuf) != 0)
             {
+              int errsv = errno;
               g_set_error (error,
                            G_IO_ERROR,
-                           g_io_error_from_errno (errno),
-                           _("Error statting directory `%s': %s"),
+                           g_io_error_from_errno (errsv),
+                           _("Error when getting information for directory “%s”: %s"),
                            path,
-                           strerror (errno));
+                           g_strerror (errsv));
               g_free (path);
               path = NULL;
               goto out;
@@ -273,28 +271,31 @@ ensure_keyring_directory (GError **error)
               g_set_error (error,
                            G_IO_ERROR,
                            G_IO_ERROR_FAILED,
-                           _("Permissions on directory `%s' are malformed. Expected mode 0700, got 0%o"),
+                           _("Permissions on directory “%s” are malformed. Expected mode 0700, got 0%o"),
                            path,
-                           statbuf.st_mode & 0777);
+                           (guint) (statbuf.st_mode & 0777));
               g_free (path);
               path = NULL;
               goto out;
             }
 #else
-#error Please implement permission checking on non-UNIX platforms
+#ifdef __GNUC__
+#warning Please implement permission checking on this non-UNIX platform
+#endif
 #endif
         }
-        goto out;
+      goto out;
     }
 
   if (g_mkdir (path, 0700) != 0)
     {
+      int errsv = errno;
       g_set_error (error,
                    G_IO_ERROR,
-                   g_io_error_from_errno (errno),
-                   _("Error creating directory `%s': %s"),
+                   g_io_error_from_errno (errsv),
+                   _("Error creating directory “%s”: %s"),
                    path,
-                   strerror (errno));
+                   g_strerror (errsv));
       g_free (path);
       path = NULL;
       goto out;
@@ -355,8 +356,8 @@ keyring_lookup_entry (const gchar  *cookie_context,
   guint n;
   gchar **lines;
 
-  g_return_val_if_fail (cookie_context != NULL, FALSE);
-  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+  g_return_val_if_fail (cookie_context != NULL, NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
   ret = NULL;
   path = NULL;
@@ -375,7 +376,7 @@ keyring_lookup_entry (const gchar  *cookie_context,
                             error))
     {
       g_prefix_error (error,
-                      _("Error opening keyring `%s' for reading: "),
+                      _("Error opening keyring “%s” for reading: "),
                       path);
       goto out;
     }
@@ -388,7 +389,6 @@ keyring_lookup_entry (const gchar  *cookie_context,
       gchar **tokens;
       gchar *endp;
       gint line_id;
-      guint64 line_when;
 
       if (line[0] == '\0')
         continue;
@@ -399,7 +399,7 @@ keyring_lookup_entry (const gchar  *cookie_context,
           g_set_error (error,
                        G_IO_ERROR,
                        G_IO_ERROR_FAILED,
-                       _("Line %d of the keyring at `%s' with content `%s' is malformed"),
+                       _("Line %d of the keyring at “%s” with content “%s” is malformed"),
                        n + 1,
                        path,
                        line);
@@ -413,7 +413,7 @@ keyring_lookup_entry (const gchar  *cookie_context,
           g_set_error (error,
                        G_IO_ERROR,
                        G_IO_ERROR_FAILED,
-                       _("First token of line %d of the keyring at `%s' with content `%s' is malformed"),
+                       _("First token of line %d of the keyring at “%s” with content “%s” is malformed"),
                        n + 1,
                        path,
                        line);
@@ -421,13 +421,13 @@ keyring_lookup_entry (const gchar  *cookie_context,
           goto out;
         }
 
-      line_when = g_ascii_strtoll (tokens[1], &endp, 10);
+      (void)g_ascii_strtoll (tokens[1], &endp, 10); /* do not care what the timestamp is */
       if (*endp != '\0')
         {
           g_set_error (error,
                        G_IO_ERROR,
                        G_IO_ERROR_FAILED,
-                       _("Second token of line %d of the keyring at `%s' with content `%s' is malformed"),
+                       _("Second token of line %d of the keyring at “%s” with content “%s” is malformed"),
                        n + 1,
                        path,
                        line);
@@ -451,7 +451,7 @@ keyring_lookup_entry (const gchar  *cookie_context,
   g_set_error (error,
                G_IO_ERROR,
                G_IO_ERROR_FAILED,
-               _("Didn't find cookie with id %d in the keyring at `%s'"),
+               _("Didn’t find cookie with id %d in the keyring at “%s”"),
                cookie_id,
                path);
 
@@ -464,6 +464,7 @@ keyring_lookup_entry (const gchar  *cookie_context,
 }
 
 /* function for logging important events that the system administrator should take notice of */
+G_GNUC_PRINTF(1, 2)
 static void
 _log (const gchar *message,
       ...)
@@ -487,7 +488,10 @@ keyring_acquire_lock (const gchar  *path,
   gchar *lock;
   gint ret;
   guint num_tries;
+#ifdef EEXISTS
   guint num_create_tries;
+#endif
+  int errsv;
 
   g_return_val_if_fail (path != NULL, FALSE);
   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
@@ -508,8 +512,8 @@ keyring_acquire_lock (const gchar  *path,
    *         real locking implementations are still flaky on network filesystems
    */
 
-  num_create_tries = 0;
 #ifdef EEXISTS
+  num_create_tries = 0;
  again:
 #endif
   num_tries = 0;
@@ -520,20 +524,21 @@ keyring_acquire_lock (const gchar  *path,
       num_tries++;
       if (num_tries == 50)
         {
-          /* ok, we slept 50*10ms = 0.5 seconds.. Conclude that the lock-file must be
+          /* ok, we slept 50*10ms = 0.5 seconds. Conclude that the lock file must be
            * stale (nuke the it from orbit)
            */
           if (g_unlink (lock) != 0)
             {
+              errsv = errno;
               g_set_error (error,
                            G_IO_ERROR,
-                           g_io_error_from_errno (errno),
-                           _("Error deleting stale lock-file `%s': %s"),
+                           g_io_error_from_errno (errsv),
+                           _("Error deleting stale lock file “%s”: %s"),
                            lock,
-                           strerror (errno));
+                           g_strerror (errsv));
               goto out;
             }
-          _log ("Deleted stale lock-file `%s'", lock);
+          _log ("Deleted stale lock file '%s'", lock);
           break;
         }
     }
@@ -545,11 +550,12 @@ keyring_acquire_lock (const gchar  *path,
                 0,
 #endif
                 0700);
+  errsv = errno;
   if (ret == -1)
     {
 #ifdef EEXISTS
       /* EEXIST: pathname already exists and O_CREAT and O_EXCL were used. */
-      if (errno == EEXISTS)
+      if (errsv == EEXISTS)
         {
           num_create_tries++;
           if (num_create_tries < 5)
@@ -558,10 +564,10 @@ keyring_acquire_lock (const gchar  *path,
 #endif
       g_set_error (error,
                    G_IO_ERROR,
-                   g_io_error_from_errno (errno),
-                   _("Error creating lock-file `%s': %s"),
+                   g_io_error_from_errno (errsv),
+                   _("Error creating lock file “%s”: %s"),
                    lock,
-                   strerror (errno));
+                   g_strerror (errsv));
       goto out;
     }
 
@@ -584,24 +590,26 @@ keyring_release_lock (const gchar  *path,
 
   ret = FALSE;
   lock = g_strdup_printf ("%s.lock", path);
-  if (g_unlink (lock) != 0)
+  if (close (lock_fd) != 0)
     {
+      int errsv = errno;
       g_set_error (error,
                    G_IO_ERROR,
-                   g_io_error_from_errno (errno),
-                   _("Error unlinking lock-file `%s': %s"),
+                   g_io_error_from_errno (errsv),
+                   _("Error closing (unlinked) lock file “%s”: %s"),
                    lock,
-                   strerror (errno));
+                   g_strerror (errsv));
       goto out;
     }
-  if (close (lock_fd) != 0)
+  if (g_unlink (lock) != 0)
     {
+      int errsv = errno;
       g_set_error (error,
                    G_IO_ERROR,
-                   g_io_error_from_errno (errno),
-                   _("Error closing (unlinked) lock-file `%s': %s"),
+                   g_io_error_from_errno (errsv),
+                   _("Error unlinking lock file “%s”: %s"),
                    lock,
-                   strerror (errno));
+                   g_strerror (errsv));
       goto out;
     }
 
@@ -646,6 +654,7 @@ keyring_generate_entry (const gchar  *cookie_context,
   lines = NULL;
   new_contents = NULL;
   have_id = FALSE;
+  use_id = 0;
   use_cookie = NULL;
   lock_fd = -1;
 
@@ -675,7 +684,7 @@ keyring_generate_entry (const gchar  *cookie_context,
         {
           g_propagate_prefixed_error (error,
                                       local_error,
-                                      _("Error opening keyring `%s' for writing: "),
+                                      _("Error opening keyring “%s” for writing: "),
                                       path);
           goto out;
         }
@@ -708,7 +717,7 @@ keyring_generate_entry (const gchar  *cookie_context,
               g_set_error (error,
                            G_IO_ERROR,
                            G_IO_ERROR_FAILED,
-                           _("Line %d of the keyring at `%s' with content `%s' is malformed"),
+                           _("Line %d of the keyring at “%s” with content “%s” is malformed"),
                            n + 1,
                            path,
                            line);
@@ -722,7 +731,7 @@ keyring_generate_entry (const gchar  *cookie_context,
               g_set_error (error,
                            G_IO_ERROR,
                            G_IO_ERROR_FAILED,
-                           _("First token of line %d of the keyring at `%s' with content `%s' is malformed"),
+                           _("First token of line %d of the keyring at “%s” with content “%s” is malformed"),
                            n + 1,
                            path,
                            line);
@@ -736,7 +745,7 @@ keyring_generate_entry (const gchar  *cookie_context,
               g_set_error (error,
                            G_IO_ERROR,
                            G_IO_ERROR_FAILED,
-                           _("Second token of line %d of the keyring at `%s' with content `%s' is malformed"),
+                           _("Second token of line %d of the keyring at “%s” with content “%s” is malformed"),
                            n + 1,
                            path,
                            line);
@@ -744,6 +753,7 @@ keyring_generate_entry (const gchar  *cookie_context,
               goto out;
             }
 
+
           /* D-Bus spec says:
            *
            *  Once the lockfile has been created, the server loads the
@@ -870,7 +880,7 @@ keyring_generate_entry (const gchar  *cookie_context,
               else
                 {
                   g_prefix_error (error,
-                                  _("(Additionally, releasing the lock for `%s' also failed: %s) "),
+                                  _("(Additionally, releasing the lock for “%s” also failed: %s) "),
                                   path,
                                   local_error->message);
                 }
@@ -954,11 +964,11 @@ mechanism_server_initiate (GDBusAuthMechanism   *mechanism,
             }
         }
 #elif defined(G_OS_WIN32)
-      GCredentials *credentials;
-      credentials = g_credentials_new_for_process ();
-      if (g_strcmp0 (g_credentials_get_windows_user (credentials), initial_response) == 0)
+      gchar *sid;
+      sid = _g_dbus_win32_get_user_sid ();
+      if (g_strcmp0 (initial_response, sid) == 0)
         m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_HAVE_DATA_TO_SEND;
-      g_object_unref (credentials);
+      g_free (sid);
 #else
 #error Please implement for your OS
 #endif
@@ -986,7 +996,7 @@ mechanism_server_data_receive (GDBusAuthMechanism   *mechanism,
   tokens = g_strsplit (data, " ", 0);
   if (g_strv_length (tokens) != 2)
     {
-      g_warning ("Malformed data `%s'", data);
+      g_warning ("Malformed data '%s'", data);
       m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
       goto out;
     }
@@ -1029,6 +1039,7 @@ mechanism_server_data_send (GDBusAuthMechanism   *mechanism,
   /* TODO: use GDBusAuthObserver here to get the cookie context to use? */
   cookie_context = "org_gtk_gdbus_general";
 
+  cookie_id = -1;
   error = NULL;
   if (!keyring_generate_entry (cookie_context,
                                &cookie_id,
@@ -1110,13 +1121,9 @@ mechanism_client_initiate (GDBusAuthMechanism   *mechanism,
 #ifdef G_OS_UNIX
   initial_response = g_strdup_printf ("%" G_GINT64_FORMAT, (gint64) getuid ());
 #elif defined (G_OS_WIN32)
-  {
-    GCredentials *credentials;
-    credentials = g_credentials_new_for_process ();
-    initial_response = g_strdup (g_credentials_get_windows_user (credentials));
-    g_object_unref (credentials);
-  }
+initial_response = _g_dbus_win32_get_user_sid ();
 #else
+#error Please implement for your OS
 #endif
   g_assert (initial_response != NULL);
 
@@ -1150,7 +1157,7 @@ mechanism_client_data_receive (GDBusAuthMechanism   *mechanism,
   tokens = g_strsplit (data, " ", 0);
   if (g_strv_length (tokens) != 3)
     {
-      g_warning ("Malformed data `%s'", data);
+      g_warning ("Malformed data '%s'", data);
       m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
       goto out;
     }
@@ -1159,7 +1166,7 @@ mechanism_client_data_receive (GDBusAuthMechanism   *mechanism,
   cookie_id = g_ascii_strtoll (tokens[1], &endp, 10);
   if (*endp != '\0')
     {
-      g_warning ("Malformed cookie_id `%s'", tokens[1]);
+      g_warning ("Malformed cookie_id '%s'", tokens[1]);
       m->priv->state = G_DBUS_AUTH_MECHANISM_STATE_REJECTED;
       goto out;
     }