do_list_or_revoke_temp_authz (gboolean revoke)
{
  gint ret;
  PolkitAuthority *authority;
  PolkitSubject *session;
  GError *error;

  ret = 1;
  authority = NULL;
  session = NULL;

  error = NULL;
  authority = polkit_authority_get_sync (NULL /* GCancellable* */, &error);
  if (authority == NULL)
    {
      g_printerr ("Error getting authority: %s\n", error->message);
      g_error_free (error);
      goto out;
    }

  error = NULL;
  session = polkit_unix_session_new_for_process_sync (getpid (),
                                                      NULL, /* GCancellable */
                                                      &error);
  if (session == NULL)
    {
      g_printerr ("Error getting session: %s\n", error->message);
      g_error_free (error);
      goto out;
    }

  if (revoke)
    {
      if (!polkit_authority_revoke_temporary_authorizations_sync (authority,
                                                                  session,
                                                                  NULL, /* GCancellable */
                                                                  &error))
        {
          g_printerr ("Error revoking temporary authorizations: %s\n", error->message);
          g_error_free (error);
          goto out;
        }

      ret = 0;
    }
  else
    {
      GList *authorizations;
      GList *l;

      error = NULL;
      authorizations = polkit_authority_enumerate_temporary_authorizations_sync (authority,
                                                                                 session,
                                                                                 NULL, /* GCancellable */
                                                                                 &error);
      if (error != NULL)
        {
          g_printerr ("Error getting temporary authorizations: %s\n", error->message);
          g_error_free (error);
          goto out;
        }

      for (l = authorizations; l != NULL; l = l->next)
        {
          PolkitTemporaryAuthorization *a = POLKIT_TEMPORARY_AUTHORIZATION (l->data);
          const gchar *id;
          const gchar *action_id;
          PolkitSubject *subject;
          gchar *subject_cmdline;
          time_t obtained;
          time_t expires;
          GTimeVal now;
          gchar *subject_str;
          gchar obtained_str[64];
          gchar expires_str[64];
          gchar *obtained_rel_str;
          gchar *expires_rel_str;
          struct tm *broken_down;

          id = polkit_temporary_authorization_get_id (a);
          action_id = polkit_temporary_authorization_get_action_id (a);
          subject = polkit_temporary_authorization_get_subject (a);
          subject_str = polkit_subject_to_string (subject);
          subject_cmdline = _polkit_subject_get_cmdline (subject);
          obtained = polkit_temporary_authorization_get_time_obtained (a);
          expires = polkit_temporary_authorization_get_time_expires (a);

          g_get_current_time (&now);

          broken_down = localtime (&obtained);
          strftime (obtained_str, sizeof (obtained_str), "%c", broken_down);
          broken_down = localtime (&expires);
          strftime (expires_str, sizeof (expires_str), "%c", broken_down);

          obtained_rel_str = format_reltime (obtained - now.tv_sec);
          expires_rel_str = format_reltime (expires - now.tv_sec);

          g_print ("authorization id: %s\n"
                   "action:           %s\n"
                   "subject:          %s (%s)\n"
                   "obtained:         %s (%s)\n"
                   "expires:          %s (%s)\n"
                   "\n",
                   id,
                   action_id,
                   subject_str, subject_cmdline != NULL ? subject_cmdline : "cannot read cmdline",
                   obtained_rel_str, obtained_str,
                   expires_rel_str, expires_str);

          g_object_unref (subject);
          g_free (subject_str);
          g_free (subject_cmdline);
          g_free (obtained_rel_str);
          g_free (expires_rel_str);
        }
      g_list_foreach (authorizations, (GFunc) g_object_unref, NULL);
      g_list_free (authorizations);

      ret = 0;
    }

 out:
  if (authority != NULL)
    g_object_unref (authority);
  if (session != NULL)
    g_object_unref (session);

  return ret;
}
do_list_or_revoke_temp_authz (gboolean revoke)
{
  gint ret;
  PolkitAuthority *authority;
  PolkitSubject *session;
  GError *error;

  ret = 1;
  authority = NULL;
  session = NULL;

  error = NULL;
  authority = polkit_authority_get_sync (NULL  , &error);
  if (authority == NULL)
    {
      g_printerr ("Error getting authority: %s\n", error->message);
      g_error_free (error);
      goto out;
    }

  error = NULL;
  session = polkit_unix_session_new_for_process_sync (getpid (),
                                                      NULL,  
                                                      &error);
  if (session == NULL)
    {
      g_printerr ("Error getting session: %s\n", error->message);
      g_error_free (error);
      goto out;
    }

  if (revoke)
    {
      if (!polkit_authority_revoke_temporary_authorizations_sync (authority,
                                                                  session,
                                                                  NULL,  
                                                                  &error))
        {
          g_printerr ("Error revoking temporary authorizations: %s\n", error->message);
          g_error_free (error);
          goto out;
        }

      ret = 0;
    }
  else
    {
      GList *authorizations;
      GList *l;

      error = NULL;
      authorizations = polkit_authority_enumerate_temporary_authorizations_sync (authority,
                                                                                 session,
                                                                                 NULL,  
                                                                                 &error);
      if (error != NULL)
        {
          g_printerr ("Error getting temporary authorizations: %s\n", error->message);
          g_error_free (error);
          goto out;
        }

      for (l = authorizations; l != NULL; l = l->next)
        {
          PolkitTemporaryAuthorization *a = POLKIT_TEMPORARY_AUTHORIZATION (l->data);
          const gchar *id;
          const gchar *action_id;
          PolkitSubject *subject;
          gchar *subject_cmdline;
          time_t obtained;
          time_t expires;
          GTimeVal now;
          gchar *subject_str;
          gchar obtained_str[64];
          gchar expires_str[64];
          gchar *obtained_rel_str;
          gchar *expires_rel_str;
          struct tm *broken_down;

          id = polkit_temporary_authorization_get_id (a);
          action_id = polkit_temporary_authorization_get_action_id (a);
          subject = polkit_temporary_authorization_get_subject (a);
          subject_str = polkit_subject_to_string (subject);
          subject_cmdline = _polkit_subject_get_cmdline (subject);
          obtained = polkit_temporary_authorization_get_time_obtained (a);
          expires = polkit_temporary_authorization_get_time_expires (a);

          g_get_current_time (&now);

          broken_down = localtime (&obtained);
          strftime (obtained_str, sizeof (obtained_str), "%c", broken_down);
          broken_down = localtime (&expires);
          strftime (expires_str, sizeof (expires_str), "%c", broken_down);

          obtained_rel_str = format_reltime (obtained - now.tv_sec);
          expires_rel_str = format_reltime (expires - now.tv_sec);

          g_print ("authorization id: %s\n"
                   "action:           %s\n"
                   "subject:          %s (%s)\n"
                   "obtained:         %s (%s)\n"
                   "expires:          %s (%s)\n"
                   "\n",
                   id,
                   action_id,
                   subject_str, subject_cmdline != NULL ? subject_cmdline : "cannot read cmdline",
                   obtained_rel_str, obtained_str,
                   expires_rel_str, expires_str);

          g_object_unref (subject);
          g_free (subject_str);
          g_free (subject_cmdline);
          g_free (obtained_rel_str);
          g_free (expires_rel_str);
        }
      g_list_foreach (authorizations, (GFunc) g_object_unref, NULL);
      g_list_free (authorizations);

      ret = 0;
    }

 out:
  if (authority != NULL)
    g_object_unref (authority);
  if (session != NULL)
    g_object_unref (session);

  return ret;
}