Debian Codegraph @

modules/lsm/lsm_linux_drive_local.c

raw
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 *
3 * Copyright (C) 2016 Gris Ge <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21 #include "config.h"
22
23 #include <sys/types.h>
24
25 #include <pwd.h>
26 #include <grp.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <glib.h>
31 #include <glib/gi18n-lib.h>
32 #include <libstoragemgmt/libstoragemgmt.h>
33
34 #include <src/udiskslogging.h>
35 #include <src/udiskslinuxdriveobject.h>
36 #include <src/udisksdaemon.h>
37 #include <src/udisksdaemonutil.h>
38 #include <src/udisksbasejob.h>
39 #include <src/udiskssimplejob.h>
40 #include <src/udisksthreadedjob.h>
41 #include <src/udiskslinuxdevice.h>
42 #include <src/udisksmodule.h>
43 #include <src/udisksmoduleobject.h>
44
45 #include "lsm_types.h"
46 #include "lsm_linux_drive_local.h"
47
48
49 typedef struct _UDisksLinuxDriveLSMLocalClass UDisksLinuxDriveLSMLocalClass;
50
51 struct _UDisksLinuxDriveLSMLocal
52 {
53 UDisksDriveLsmLocalSkeleton parent_instance;
54
55 UDisksLinuxModuleLSM *module;
56 UDisksLinuxDriveObject *drive_object;
57 };
58
59 struct _UDisksLinuxDriveLSMLocalClass
60 {
61 UDisksDriveLsmLocalSkeletonClass parent_class;
62 };
63
64 static void udisks_linux_drive_lsm_local_iface_init (UDisksDriveLsmLocalIface *iface);
65 static void udisks_linux_drive_lsm_local_module_object_iface_init (UDisksModuleObjectIface *iface);
66
67 G_DEFINE_TYPE_WITH_CODE (UDisksLinuxDriveLSMLocal, udisks_linux_drive_lsm_local, UDISKS_TYPE_DRIVE_LSM_LOCAL_SKELETON,
68 G_IMPLEMENT_INTERFACE (UDISKS_TYPE_DRIVE_LSM_LOCAL, udisks_linux_drive_lsm_local_iface_init)
69 G_IMPLEMENT_INTERFACE (UDISKS_TYPE_MODULE_OBJECT, udisks_linux_drive_lsm_local_module_object_iface_init));
70
71 enum
72 {
73 PROP_0,
74 PROP_MODULE,
75 PROP_DRIVE_OBJECT,
76 N_PROPERTIES
77 };
78
79
80 static void
81 udisks_linux_drive_lsm_local_get_property (GObject *object,
82 guint property_id,
83 GValue *value,
84 GParamSpec *pspec)
85 {
86 UDisksLinuxDriveLSMLocal *drive_lsm_local = UDISKS_LINUX_DRIVE_LSM_LOCAL (object);
87
88 switch (property_id)
89 {
90 case PROP_MODULE:
91 g_value_set_object (value, UDISKS_MODULE (drive_lsm_local->module));
92 break;
93
94 case PROP_DRIVE_OBJECT:
95 g_value_set_object (value, drive_lsm_local->drive_object);
96 break;
97
98 default:
99 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
100 break;
101 }
102 }
103
104 static void
105 udisks_linux_drive_lsm_local_set_property (GObject *object,
106 guint property_id,
107 const GValue *value,
108 GParamSpec *pspec)
109 {
110 UDisksLinuxDriveLSMLocal *drive_lsm_local = UDISKS_LINUX_DRIVE_LSM_LOCAL (object);
111
112 switch (property_id)
113 {
114 case PROP_MODULE:
115 g_assert (drive_lsm_local->module == NULL);
116 drive_lsm_local->module = UDISKS_LINUX_MODULE_LSM (g_value_dup_object (value));
117 break;
118
119 case PROP_DRIVE_OBJECT:
120 g_assert (drive_lsm_local->drive_object == NULL);
121 /* we don't take reference to drive_object */
122 drive_lsm_local->drive_object = g_value_get_object (value);
123 break;
124
125 default:
126 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
127 break;
128 }
129 }
130
131 static void
132 udisks_linux_drive_lsm_local_finalize (GObject *object)
133 {
134 UDisksLinuxDriveLSMLocal *drive_lsm_local = UDISKS_LINUX_DRIVE_LSM_LOCAL (object);
135
136 udisks_debug ("LSM: udisks_linux_drive_lsm_local_finalize ()");
137
138 /* we don't take reference to drive_object */
139 g_object_unref (drive_lsm_local->module);
140
141 if (G_OBJECT_CLASS (udisks_linux_drive_lsm_local_parent_class)->finalize != NULL)
142 G_OBJECT_CLASS (udisks_linux_drive_lsm_local_parent_class)->finalize (object);
143 }
144
145 static void
146 udisks_linux_drive_lsm_local_init (UDisksLinuxDriveLSMLocal *drive_lsm_local)
147 {
148 g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (drive_lsm_local),
149 G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
150 }
151
152 static void
153 udisks_linux_drive_lsm_local_class_init (UDisksLinuxDriveLSMLocalClass *class)
154 {
155 GObjectClass *gobject_class;
156
157 udisks_debug ("LSM: udisks_linux_drive_lsm_local_class_init");
158
159 gobject_class = G_OBJECT_CLASS (class);
160 gobject_class->get_property = udisks_linux_drive_lsm_local_get_property;
161 gobject_class->set_property = udisks_linux_drive_lsm_local_set_property;
162 gobject_class->finalize = udisks_linux_drive_lsm_local_finalize;
163
164 /**
165 * UDisksLinuxDriveLSMLocal:module:
166 *
167 * The #UDisksModule for the object.
168 */
169 g_object_class_install_property (gobject_class,
170 PROP_MODULE,
171 g_param_spec_object ("module",
172 "Module",
173 "The module for the object",
174 UDISKS_TYPE_MODULE,
175 G_PARAM_READABLE |
176 G_PARAM_WRITABLE |
177 G_PARAM_CONSTRUCT_ONLY |
178 G_PARAM_STATIC_STRINGS));
179 /**
180 * UDisksLinuxDriveLSMLocal:driveobject:
181 *
182 * The #UDisksLinuxDriveObject for the object.
183 */
184 g_object_class_install_property (gobject_class,
185 PROP_DRIVE_OBJECT,
186 g_param_spec_object ("driveobject",
187 "Drive object",
188 "The drive object for the interface",
189 UDISKS_TYPE_LINUX_DRIVE_OBJECT,
190 G_PARAM_READABLE |
191 G_PARAM_WRITABLE |
192 G_PARAM_CONSTRUCT_ONLY |
193 G_PARAM_STATIC_STRINGS));
194 }
195
196 /**
197 * udisks_linux_drive_lsm_local_new:
198 * @module: A #UDisksLinuxModuleLSM.
199 * @drive_object: A #UDisksLinuxDriveObject.
200 *
201 * Creates a new #UDisksLinuxDriveLSMLocal instance.
202 *
203 * Returns: A new #UDisksLinuxDriveLSMLocal. Free with g_object_unref().
204 */
205 UDisksLinuxDriveLSMLocal *
206 udisks_linux_drive_lsm_local_new (UDisksLinuxModuleLSM *module,
207 UDisksLinuxDriveObject *drive_object)
208 {
209 g_return_val_if_fail (UDISKS_IS_LINUX_MODULE_LSM (module), NULL);
210 g_return_val_if_fail (UDISKS_IS_LINUX_DRIVE_OBJECT (drive_object), NULL);
211
212 udisks_debug ("LSM: udisks_linux_drive_lsm_local_new");
213
214 return g_object_new (UDISKS_TYPE_LINUX_DRIVE_LSM_LOCAL,
215 "module", UDISKS_MODULE (module),
216 "driveobject", drive_object,
217 NULL);
218 }
219
220 static gchar *
221 get_blk_path (UDisksLinuxDriveLSMLocal *drive_lsm_local,
222 GDBusMethodInvocation *invocation)
223 {
224 UDisksLinuxDriveObject *drive_object;
225 UDisksLinuxBlockObject *block_object;
226 UDisksBlock *block = NULL;
227 gchar *blk_path = NULL;
228
229 drive_object = drive_lsm_local->drive_object;
230 block_object = udisks_linux_drive_object_get_block (drive_object, FALSE);
231 if (block_object == NULL)
232 {
233 g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
234 "Unable to find block device for drive");
235 goto out;
236 }
237
238 block = udisks_object_get_block (UDISKS_OBJECT (block_object));
239 blk_path = udisks_block_dup_device (block);
240 if (blk_path == NULL)
241 {
242 g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
243 "Failed to retrieve block path of specified disk drive");
244 goto out;
245 }
246
247 out:
248 g_clear_object (&block);
249 g_clear_object (&block_object);
250 return blk_path;
251 }
252
253 static gboolean
254 is_authed (GDBusMethodInvocation *invocation,
255 UDisksLinuxDriveLSMLocal *drive_lsm_local,
256 GVariant *options)
257 {
258 UDisksLinuxDriveObject *drive_object;
259 UDisksLinuxBlockObject *block_object;
260 UDisksDaemon *daemon = NULL;
261 gboolean rc = FALSE;
262
263 drive_object = drive_lsm_local->drive_object;
264 daemon = udisks_module_get_daemon (UDISKS_MODULE (drive_lsm_local->module));
265 block_object = udisks_linux_drive_object_get_block (drive_object, FALSE);
266 if (block_object == NULL)
267 {
268 g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
269 "Unable to find block device for drive");
270 goto out;
271 }
272
273 /* Check that the user is actually authorized */
274 if (! udisks_daemon_util_check_authorization_sync (daemon,
275 UDISKS_OBJECT (block_object),
276 LSM_POLICY_ACTION_ID,
277 options,
278 N_("Authentication is required to change $(drive) LED"),
279 invocation))
280 goto out;
281
282 rc = TRUE;
283
284 out:
285 g_clear_object (&block_object);
286 return rc;
287 }
288
289 static gboolean
290 led_control (UDisksLinuxDriveLSMLocal *drive_lsm_local,
291 GDBusMethodInvocation *invocation,
292 GVariant *options,
293 int (*lsm_func) (const char *blk_path, lsm_error **lsm_err),
294 const gchar *lsm_fun_name)
295 {
296 lsm_error *lsm_err = NULL;
297 int lsm_rc = 0;
298 gchar *blk_path = NULL;
299
300 if (! is_authed (invocation, drive_lsm_local, options))
301 goto out;
302
303 blk_path = get_blk_path (drive_lsm_local, invocation);
304 if (blk_path == NULL)
305 goto out;
306
307 lsm_rc = lsm_func (blk_path, &lsm_err);
308 if (lsm_rc != LSM_ERR_OK)
309 {
310 if (lsm_rc == LSM_ERR_NO_SUPPORT)
311 g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_NOT_SUPPORTED,
312 "Specified disk does not support this action");
313 else
314 g_dbus_method_invocation_return_error (invocation, UDISKS_ERROR, UDISKS_ERROR_FAILED,
315 "%s failed %d: %s",
316 lsm_fun_name, lsm_error_number_get (lsm_err),
317 lsm_error_message_get (lsm_err));
318 goto out;
319 }
320
321 /* success, complete the method call in a generic way */
322 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
323
324 out:
325 g_free (blk_path);
326 if (lsm_err != NULL)
327 lsm_error_free (lsm_err);
328
329 return TRUE;
330 }
331
332 static gboolean
333 handle_turn_ident_ledon (UDisksDriveLsmLocal *drive_lsm_local,
334 GDBusMethodInvocation *invocation,
335 GVariant *options)
336 {
337 return led_control (UDISKS_LINUX_DRIVE_LSM_LOCAL (drive_lsm_local),
338 invocation, options,
339 lsm_local_disk_ident_led_on, "lsm_local_ident_led_on");
340 }
341
342 static gboolean
343 handle_turn_ident_ledoff (UDisksDriveLsmLocal *drive_lsm_local,
344 GDBusMethodInvocation *invocation,
345 GVariant *options)
346 {
347 return led_control (UDISKS_LINUX_DRIVE_LSM_LOCAL (drive_lsm_local),
348 invocation, options,
349 lsm_local_disk_ident_led_off, "lsm_local_ident_led_off");
350 }
351
352 static gboolean
353 handle_turn_fault_ledon (UDisksDriveLsmLocal *drive_lsm_local,
354 GDBusMethodInvocation *invocation,
355 GVariant *options)
356 {
357 return led_control (UDISKS_LINUX_DRIVE_LSM_LOCAL (drive_lsm_local),
358 invocation, options,
359 lsm_local_disk_fault_led_on, "lsm_local_fault_led_on");
360 }
361
362 static gboolean
363 handle_turn_fault_ledoff (UDisksDriveLsmLocal *drive_lsm_local,
364 GDBusMethodInvocation *invocation,
365 GVariant *options)
366 {
367 return led_control (UDISKS_LINUX_DRIVE_LSM_LOCAL (drive_lsm_local),
368 invocation, options,
369 lsm_local_disk_fault_led_off, "lsm_local_fault_led_off");
370 }
371
372 gboolean
373 udisks_linux_drive_lsm_local_update (UDisksLinuxDriveLSMLocal *drive_lsm_local,
374 UDisksLinuxDriveObject *drive_object)
375 {
376 return FALSE;
377 }
378
379 static void
380 udisks_linux_drive_lsm_local_iface_init (UDisksDriveLsmLocalIface *iface)
381 {
382 iface->handle_turn_ident_ledon = handle_turn_ident_ledon;
383 iface->handle_turn_ident_ledoff = handle_turn_ident_ledoff;
384 iface->handle_turn_fault_ledon = handle_turn_fault_ledon;
385 iface->handle_turn_fault_ledoff = handle_turn_fault_ledoff;
386 }
387
388 /* -------------------------------------------------------------------------- */
389
390 static gboolean
391 udisks_linux_drive_lsm_local_module_object_process_uevent (UDisksModuleObject *module_object,
392 const gchar *action,
393 UDisksLinuxDevice *device,
394 gboolean *keep)
395 {
396 UDisksLinuxDriveLSMLocal *drive_lsm_local = UDISKS_LINUX_DRIVE_LSM_LOCAL (module_object);
397
398 g_return_val_if_fail (UDISKS_IS_LINUX_DRIVE_LSM_LOCAL (module_object), FALSE);
399
400 *keep = udisks_linux_module_lsm_drive_local_check (drive_lsm_local->module, drive_lsm_local->drive_object);
401 if (*keep)
402 {
403 udisks_linux_drive_lsm_local_update (drive_lsm_local, drive_lsm_local->drive_object);
404 }
405
406 return TRUE;
407 }
408
409 static void
410 udisks_linux_drive_lsm_local_module_object_iface_init (UDisksModuleObjectIface *iface)
411 {
412 iface->process_uevent = udisks_linux_drive_lsm_local_module_object_process_uevent;
413 }