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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/*
* Remmina - The GTK+ Remote Desktop Client
* Copyright (C) 2016-2023 Antenore Gatta, Giovanni Panozzo
* Copyright (C) 2023-2024 Hiroyuki Tanaka, Sunil Bhat
*
* 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.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. * If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. * If you
* do not wish to do so, delete this exception statement from your
* version. * If you delete this exception statement from all source
* files in the program, then also delete it here.
*
*/
#include <curl/curl.h>
#include <gtk/gtk.h>
#include <json-glib/json-glib.h>
#include "remmina.h"
#include "remmina_info.h"
#include "remmina_log.h"
#include "remmina_plugin_manager.h"
#include "remmina_pref.h"
#include "remmina_curl_connector.h"
#include "remmina/remmina_trace_calls.h"
#include "remmina_utils.h"
#ifdef GDK_WINDOWING_WAYLAND
#include <gdk/gdkwayland.h>
#endif
#ifdef GDK_WINDOWING_X11
#include <gdk/gdkx.h>
#endif
extern gboolean info_disable_stats;
extern gboolean info_disable_news;
extern gboolean info_disable_tip;
struct curl_msg {
gchar *body;
gchar *url;
gpointer *user_data;
char* response;
size_t size;
};
enum ShowValue {ShowNews, ShowTip, ShowNone};
// If we receive a response from the server parse out the message
// and call the appropiate functions to handle the message.
void handle_resp(struct curl_msg * message){
gchar *news_checksum = NULL;
JsonParser *parser = NULL;
enum ShowValue to_show = ShowNone;
if (!imode) {
parser = json_parser_new();
if (!parser) {
return;
}
if (!json_parser_load_from_data(parser, message->response , message->size, NULL)){
g_object_unref(parser);
return;
}
JsonReader *reader = json_reader_new(json_parser_get_root(parser));
if (!reader) {
g_object_unref(parser);
return;
}
const gchar *json_tip_string;
const gchar *json_news_string;
gint64 json_stats_int = 0;
if (!json_reader_read_element(reader, 0)) {
g_object_unref(parser);
g_object_unref(reader);
return;
}
if (!json_reader_read_member(reader, "TIP")) {
json_tip_string = NULL;
}
else {
json_tip_string = json_reader_get_string_value(reader);
}
json_reader_end_member(reader);
if (!json_reader_read_member(reader, "NEWS")) {
json_news_string = NULL;
}
else {
json_news_string = json_reader_get_string_value(reader);
}
json_reader_end_member(reader);
if (!json_reader_read_member(reader, "STATS")) {
json_stats_int = 0;
}
else {
json_stats_int = json_reader_get_int_value(reader);
}
json_reader_end_member(reader);
if (json_reader_read_member(reader, "LIST")){
g_idle_add(remmina_plugin_manager_parse_plugin_list, json_reader_new(json_parser_get_root(parser)));
}
json_reader_end_member(reader);
if (json_reader_read_member(reader, "PLUGINS")){
g_idle_add(remmina_plugin_manager_download_plugins, json_reader_new(json_parser_get_root(parser)));
}
json_reader_end_member(reader);
json_reader_end_element(reader);
g_object_unref(reader);
if (json_news_string == NULL) {
if (json_tip_string == NULL) {
to_show = ShowNone;
}
else if (info_disable_tip == 0) {
to_show = ShowTip;
}
}
else {
news_checksum = remmina_sha256_buffer((const guchar *)json_news_string, strlen(json_news_string));
if (news_checksum == NULL) {
REMMINA_DEBUG("News checksum is NULL");
}
else if ((remmina_pref.periodic_news_last_checksum == NULL) || strcmp(news_checksum, remmina_pref.periodic_news_last_checksum) != 0) {
REMMINA_DEBUG("Latest news checksum: %s", news_checksum);
remmina_pref.periodic_news_last_checksum = g_strdup(news_checksum);
remmina_pref_save();
if (info_disable_news == 0) {
to_show = ShowNews;
}
}
if (to_show == ShowNone && json_tip_string != NULL && info_disable_tip == 0) {
to_show = ShowTip;
}
g_free(news_checksum);
}
if (to_show == ShowTip) {
RemminaInfoMessage *message = malloc(sizeof(RemminaInfoMessage));
if (message == NULL) {
return;
}
message->info_string= g_strdup(json_tip_string);
message->title_string= g_strdup("Tip of the Day");
g_idle_add(remmina_info_show_response, message);
}
else if (to_show == ShowNews) {
RemminaInfoMessage *message = malloc(sizeof(RemminaInfoMessage));
if (message == NULL) {
return;
}
message->info_string= g_strdup(json_news_string);
message->title_string= g_strdup("NEWS");
g_idle_add(remmina_info_show_response, message);
}
if (json_stats_int){
remmina_info_stats_collector();
}
g_object_unref(parser);
}
}
// Allocate memory to hold response from the server in msg->response
size_t remmina_curl_process_response(void *buffer, size_t size, size_t nmemb, void *userp){
size_t realsize = size * nmemb;
struct curl_msg *msg = (struct curl_msg *)userp;
char *ptr = realloc(msg->response, msg->size + realsize + 1);
if (!ptr) {
return 0; /* out of memory! */
}
msg->response = ptr;
memcpy(&(msg->response[msg->size]), buffer, realsize);
msg->size += realsize;
msg->response[msg->size] = 0;
return realsize;
}
void remmina_curl_send_message(gpointer data)
{
gchar *result_message = NULL;
gchar *marked_up_message = NULL;
struct curl_msg* message = (struct curl_msg*)data;
message->size = 0;
message->response = NULL;
CURL* curl = curl_easy_init();
if (curl){
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, message->url);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, message->body);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, remmina_curl_process_response);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, message);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60);
res = curl_easy_perform(curl);
if(res != CURLE_OK){
curl_easy_strerror(res);
result_message = "Failure: Transport error occurred - see debug or logs for more information";
marked_up_message = RED_TEXT(result_message);
}
else{
handle_resp(message);
result_message = "Success: Message sent successfully, please wait for report to automatically open a new issue. This may take a little while.";
marked_up_message = GREEN_TEXT(result_message);
}
if (message->user_data != NULL){
gtk_label_set_markup(GTK_LABEL(message->user_data), marked_up_message);
}
if (message->body){
g_free(message->body);
}
if (message->response){
g_free(message->response);
}
g_free(message);
g_free(marked_up_message);
curl_easy_cleanup(curl);
}
}
void remmina_curl_compose_message(gchar* body, char* type, char* url, gpointer data)
{
if (!imode){
struct curl_msg* message = (struct curl_msg*)malloc(sizeof(struct curl_msg));
if (message == NULL) {
return;
}
message->body = body;
message->url = url;
message->user_data = data;
g_thread_new("send_curl_message", (GThreadFunc)remmina_curl_send_message, message);
}
}
|