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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* gvfs - extensions for gio
*
* Copyright (C) 2006-2009 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.
*
* This library 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
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* Author: David Zeuthen <davidz@redhat.com>
*/
#include <config.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "gproxymountoperation.h"
/* for protecting the id_to_op and id_count */
G_LOCK_DEFINE_STATIC(proxy_op);
/* map from id to GMountOperation */
static GHashTable *id_to_op = NULL;
static guint id_count = 1;
typedef struct
{
gchar *id;
GMountOperation *op;
GProxyVolumeMonitor *monitor;
gulong reply_handler_id;
} ProxyMountOpData;
static void
proxy_mount_op_data_free (ProxyMountOpData *data)
{
g_free (data->id);
if (data->reply_handler_id > 0)
g_signal_handler_disconnect (data->op, data->reply_handler_id);
g_object_unref (data->op);
g_object_unref (data->monitor);
g_free (data);
}
/* must be called with lock held */
static ProxyMountOpData *
proxy_mount_op_data_new (GMountOperation *op,
GProxyVolumeMonitor *monitor)
{
ProxyMountOpData *data;
data = g_new0 (ProxyMountOpData, 1);
data->id = g_strdup_printf ("%d:%d", getpid (), id_count++);
data->op = g_object_ref (op);
data->monitor = g_object_ref (monitor);
return data;
}
/* must be called with lock held */
static void
ensure_hash (void)
{
if (id_to_op == NULL)
id_to_op = g_hash_table_new_full (g_str_hash,
g_str_equal,
NULL,
(GDestroyNotify) proxy_mount_op_data_free);
}
const gchar *
g_proxy_mount_operation_wrap (GMountOperation *op,
GProxyVolumeMonitor *monitor)
{
ProxyMountOpData *data;
if (op == NULL)
return "";
G_LOCK (proxy_op);
ensure_hash ();
data = proxy_mount_op_data_new (op, monitor);
g_hash_table_insert (id_to_op,
data->id,
data);
G_UNLOCK (proxy_op);
return data->id;
}
/* ---------------------------------------------------------------------------------------------------- */
static void
mount_op_reply_cb (GVfsRemoteVolumeMonitor *proxy,
GAsyncResult *res,
gpointer user_data)
{
GError *error = NULL;
if (!gvfs_remote_volume_monitor_call_mount_op_reply_finish (proxy,
res,
&error))
{
g_warning ("Error from MountOpReply(): %s", error->message);
g_error_free (error);
}
}
static void
mount_operation_reply (GMountOperation *mount_operation,
GMountOperationResult result,
gpointer user_data)
{
ProxyMountOpData *data = user_data;
GVfsRemoteVolumeMonitor *proxy;
const gchar *user_name;
const gchar *domain;
const gchar *password;
gchar *encoded_password;
gint password_save;
gint choice;
gboolean anonymous;
user_name = g_mount_operation_get_username (mount_operation);
domain = g_mount_operation_get_domain (mount_operation);
password = g_mount_operation_get_password (mount_operation);
password_save = g_mount_operation_get_password_save (mount_operation);
choice = g_mount_operation_get_choice (mount_operation);
anonymous = g_mount_operation_get_anonymous (mount_operation);
if (user_name == NULL)
user_name = "";
if (domain == NULL)
domain = "";
if (password == NULL)
password = "";
/* NOTE: this is not to add "security", it's merely to prevent accidental exposure
* of passwords when running dbus-monitor
*/
encoded_password = g_base64_encode ((const guchar *) password, (gsize) (strlen (password) + 1));
proxy = g_proxy_volume_monitor_get_dbus_proxy (data->monitor);
gvfs_remote_volume_monitor_call_mount_op_reply (proxy,
data->id,
result,
user_name,
domain,
encoded_password,
password_save,
choice,
anonymous,
NULL,
(GAsyncReadyCallback) mount_op_reply_cb,
data);
g_object_unref (proxy);
g_free (encoded_password);
}
/* ---------------------------------------------------------------------------------------------------- */
void
g_proxy_mount_operation_handle_ask_password (const gchar *wrapped_id,
const gchar *message,
const gchar *default_user,
const gchar *default_domain,
guint flags)
{
ProxyMountOpData *data;
g_return_if_fail (wrapped_id != NULL);
if (id_to_op == NULL)
goto out;
G_LOCK (proxy_op);
data = g_hash_table_lookup (id_to_op, wrapped_id);
G_UNLOCK (proxy_op);
if (data == NULL)
goto out;
if (data->reply_handler_id == 0)
{
data->reply_handler_id = g_signal_connect (data->op,
"reply",
G_CALLBACK (mount_operation_reply),
data);
}
g_signal_emit_by_name (data->op,
"ask-password",
message,
default_user,
default_domain,
flags);
out:
;
}
/* ---------------------------------------------------------------------------------------------------- */
void
g_proxy_mount_operation_handle_ask_question (const gchar *wrapped_id,
const gchar *message,
const gchar *const *choices)
{
ProxyMountOpData *data;
g_return_if_fail (wrapped_id != NULL);
if (id_to_op == NULL)
goto out;
G_LOCK (proxy_op);
data = g_hash_table_lookup (id_to_op, wrapped_id);
G_UNLOCK (proxy_op);
if (data == NULL)
goto out;
if (data->reply_handler_id == 0)
{
data->reply_handler_id = g_signal_connect (data->op,
"reply",
G_CALLBACK (mount_operation_reply),
data);
}
g_signal_emit_by_name (data->op,
"ask-question",
message,
choices);
out:
;
}
/* ---------------------------------------------------------------------------------------------------- */
void
g_proxy_mount_operation_handle_show_processes (const gchar *wrapped_id,
const gchar *message,
GVariant *pids,
const gchar *const *choices)
{
ProxyMountOpData *data;
GArray *processes;
GVariantIter iter;
GPid pid;
g_return_if_fail (wrapped_id != NULL);
processes = NULL;
if (id_to_op == NULL)
goto out;
G_LOCK (proxy_op);
data = g_hash_table_lookup (id_to_op, wrapped_id);
G_UNLOCK (proxy_op);
if (data == NULL)
goto out;
processes = g_array_new (FALSE, FALSE, sizeof (GPid));
g_variant_iter_init (&iter, pids);
while (g_variant_iter_loop (&iter, "i", &pid))
g_array_append_val (processes, pid);
if (data->reply_handler_id == 0)
{
data->reply_handler_id = g_signal_connect (data->op,
"reply",
G_CALLBACK (mount_operation_reply),
data);
}
g_signal_emit_by_name (data->op,
"show-processes",
message,
processes,
choices);
out:
if (processes)
g_array_unref (processes);
}
/* ---------------------------------------------------------------------------------------------------- */
void
g_proxy_mount_operation_handle_show_unmount_progress (const gchar *wrapped_id,
const gchar *message,
gint64 time_left,
gint64 bytes_left)
{
ProxyMountOpData *data;
g_return_if_fail (wrapped_id != NULL);
if (id_to_op == NULL)
return;
G_LOCK (proxy_op);
data = g_hash_table_lookup (id_to_op, wrapped_id);
G_UNLOCK (proxy_op);
if (data == NULL)
return;
g_signal_emit_by_name (data->op,
"show-unmount-progress",
message,
time_left,
bytes_left);
}
/* ---------------------------------------------------------------------------------------------------- */
void
g_proxy_mount_operation_handle_aborted (const gchar *wrapped_id)
{
ProxyMountOpData *data;
g_return_if_fail (wrapped_id != NULL);
if (id_to_op == NULL)
goto out;
G_LOCK (proxy_op);
data = g_hash_table_lookup (id_to_op, wrapped_id);
G_UNLOCK (proxy_op);
if (data == NULL)
goto out;
g_signal_emit_by_name (data->op, "aborted");
out:
;
}
/* ---------------------------------------------------------------------------------------------------- */
void
g_proxy_mount_operation_destroy (const gchar *wrapped_id)
{
g_return_if_fail (wrapped_id != NULL);
if (strlen (wrapped_id) == 0)
return;
if (id_to_op == NULL)
return;
G_LOCK (proxy_op);
g_hash_table_remove (id_to_op, wrapped_id);
G_UNLOCK (proxy_op);
}
/* ---------------------------------------------------------------------------------------------------- */
|