summaryrefslogtreecommitdiffstats
path: root/panels/sharing/cc-gnome-remote-desktop.c
blob: ef7bc5303d89ec3520022dee4efc60184073a17f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Copyright (C) 2018 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include "config.h"

#include "cc-gnome-remote-desktop.h"

const SecretSchema *
cc_grd_rdp_credentials_get_schema (void)
{
  static const SecretSchema grd_rdp_credentials_schema = {
    .name = "org.gnome.RemoteDesktop.RdpCredentials",
    .flags = SECRET_SCHEMA_NONE,
    .attributes = {
      { "credentials", SECRET_SCHEMA_ATTRIBUTE_STRING },
      { "NULL", 0 },
    },
  };

  return &grd_rdp_credentials_schema;
}

void
cc_grd_store_rdp_credentials (const gchar  *username,
                              const gchar  *password,
                              GCancellable *cancellable)
{
  GVariantBuilder builder;
  char *credentials;

  g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
  g_variant_builder_add (&builder, "{sv}", "username", g_variant_new_string (username));
  g_variant_builder_add (&builder, "{sv}", "password", g_variant_new_string (password));
  credentials = g_variant_print (g_variant_builder_end (&builder), TRUE);

  secret_password_store_sync (CC_GRD_RDP_CREDENTIALS_SCHEMA,
                              SECRET_COLLECTION_DEFAULT,
                              "GNOME Remote Desktop RDP credentials",
                              credentials,
                              NULL, NULL,
                              NULL);

  g_free (credentials);
}

gchar *
cc_grd_lookup_rdp_username (GCancellable *cancellable)
{
  g_autoptr(GError) error = NULL;
  gchar *username = NULL;
  g_autofree gchar *secret;
  GVariant *variant = NULL;

  secret = secret_password_lookup_sync (CC_GRD_RDP_CREDENTIALS_SCHEMA,
                                          cancellable, &error,
                                          NULL);
  if (error) {
    g_warning ("Failed to get username: %s", error->message);
    return NULL;
  }

  if (secret == NULL) {
      g_debug ("No RDP credentials available");
      return NULL;
  }

  variant = g_variant_parse (NULL, secret, NULL, NULL, &error);
  if (variant == NULL) {
    g_warning ("Invalid credentials format in the keyring: %s", error->message);
    return NULL;
  }

  g_variant_lookup (variant, "username", "&s", &username);

  return username;
}

gchar *
cc_grd_lookup_rdp_password (GCancellable *cancellable)
{
  g_autoptr(GError) error = NULL;
  g_autofree gchar *secret;
  gchar *password = NULL;
  GVariant *variant = NULL;

  secret = secret_password_lookup_sync (CC_GRD_RDP_CREDENTIALS_SCHEMA,
                                          cancellable, &error,
                                          NULL);
  if (error) {
    g_warning ("Failed to get password: %s", error->message);
    return NULL;
  }

  if (secret == NULL) {
      g_debug ("No RDP credentials available");
      return NULL;
  }

  variant = g_variant_parse (NULL, secret, NULL, NULL, &error);
  if (variant == NULL) {
    g_warning ("Invalid credentials format in the keyring: %s", error->message);
    return NULL;
  }

  g_variant_lookup (variant, "password", "&s", &password);

  return password;
}