GCC Code Coverage Report


Directory: ./
File: panels/system/remote-desktop/cc-gnome-remote-desktop.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 46 0.0%
Functions: 0 4 0.0%
Branches: 0 12 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2018 Red Hat, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 */
19
20 #include "config.h"
21
22 #include "cc-gnome-remote-desktop.h"
23
24 const SecretSchema *
25 cc_grd_rdp_credentials_get_schema (void)
26 {
27 static const SecretSchema grd_rdp_credentials_schema = {
28 .name = "org.gnome.RemoteDesktop.RdpCredentials",
29 .flags = SECRET_SCHEMA_NONE,
30 .attributes = {
31 { "credentials", SECRET_SCHEMA_ATTRIBUTE_STRING },
32 { "NULL", 0 },
33 },
34 };
35
36 return &grd_rdp_credentials_schema;
37 }
38
39 void
40 cc_grd_store_rdp_credentials (const gchar *username,
41 const gchar *password,
42 GCancellable *cancellable)
43 {
44 GVariantBuilder builder;
45 g_autofree gchar *credentials = NULL;
46
47 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
48 g_variant_builder_add (&builder, "{sv}", "username", g_variant_new_string (username));
49 g_variant_builder_add (&builder, "{sv}", "password", g_variant_new_string (password));
50 credentials = g_variant_print (g_variant_builder_end (&builder), TRUE);
51
52 secret_password_store_sync (cc_grd_rdp_credentials_get_schema (),
53 SECRET_COLLECTION_DEFAULT,
54 "GNOME Remote Desktop RDP credentials",
55 credentials,
56 NULL, NULL,
57 NULL);
58 }
59
60 gchar *
61 cc_grd_lookup_rdp_username (GCancellable *cancellable)
62 {
63 g_autoptr(GError) error = NULL;
64 gchar *username = NULL;
65 g_autofree gchar *secret = NULL;
66 g_autoptr(GVariant) variant = NULL;
67
68 secret = secret_password_lookup_sync (cc_grd_rdp_credentials_get_schema (),
69 cancellable, &error,
70 NULL);
71 if (error)
72 {
73 g_warning ("Failed to get username: %s", error->message);
74 return NULL;
75 }
76
77 if (secret == NULL)
78 {
79 g_debug ("No RDP credentials available");
80 return NULL;
81 }
82
83 variant = g_variant_parse (NULL, secret, NULL, NULL, &error);
84 if (variant == NULL)
85 {
86 g_warning ("Invalid credentials format in the keyring: %s", error->message);
87 return NULL;
88 }
89
90 g_variant_lookup (variant, "username", "s", &username);
91
92 return username;
93 }
94
95 gchar *
96 cc_grd_lookup_rdp_password (GCancellable *cancellable)
97 {
98 g_autoptr(GError) error = NULL;
99 g_autofree gchar *secret = NULL;
100 gchar *password = NULL;
101 g_autoptr(GVariant) variant = NULL;
102
103 secret = secret_password_lookup_sync (cc_grd_rdp_credentials_get_schema (),
104 cancellable, &error,
105 NULL);
106 if (error)
107 {
108 g_warning ("Failed to get password: %s", error->message);
109 return NULL;
110 }
111
112 if (secret == NULL)
113 {
114 g_debug ("No RDP credentials available");
115 return NULL;
116 }
117
118 variant = g_variant_parse (NULL, secret, NULL, NULL, &error);
119 if (variant == NULL)
120 {
121 g_warning ("Invalid credentials format in the keyring: %s", error->message);
122 return NULL;
123 }
124
125 g_variant_lookup (variant, "password", "s", &password);
126
127 return password;
128 }
129