Debian Codegraph @

modules/iscsi/udisksiscsiutil.c

raw
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 *
3 * Copyright (C) 2015 Peter Hatina <[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 #include "config.h"
21
22 #include <string.h>
23 #include <libiscsi.h>
24
25 #include <src/udisksdaemon.h>
26 #include <src/udisksmodulemanager.h>
27 #include <src/udiskslogging.h>
28
29 #include "udisksiscsitypes.h"
30 #include "udisksiscsiutil.h"
31
32 #ifndef HAVE_LIBISCSI_ERR
33 /* XXX: We need to expose these in libiscsi.h. If we can't make it appear in
34 * the libiscsi.h, then we need to keep this in sync with iscsi_err.h.
35 */
36 enum {
37 ISCSI_SUCCESS = 0,
38 /* Generic error */
39 ISCSI_ERR = 1,
40 /* session could not be found */
41 ISCSI_ERR_SESS_NOT_FOUND = 2,
42 /* Could not allocate resource for operation */
43 ISCSI_ERR_NOMEM = 3,
44 /* Transport error caused operation to fail */
45 ISCSI_ERR_TRANS = 4,
46 /* Generic login failure */
47 ISCSI_ERR_LOGIN = 5,
48 /* Error accessing/managing iSCSI DB */
49 ISCSI_ERR_IDBM = 6,
50 /* Invalid argument */
51 ISCSI_ERR_INVAL = 7,
52 /* Connection timer exired while trying to connect */
53 ISCSI_ERR_TRANS_TIMEOUT = 8,
54 /* Generic internal iscsid failure */
55 ISCSI_ERR_INTERNAL = 9,
56 /* Logout failed */
57 ISCSI_ERR_LOGOUT = 10,
58 /* iSCSI PDU timedout */
59 ISCSI_ERR_PDU_TIMEOUT = 11,
60 /* iSCSI transport module not loaded in kernel or iscsid */
61 ISCSI_ERR_TRANS_NOT_FOUND = 12,
62 /* Permission denied */
63 ISCSI_ERR_ACCESS = 13,
64 /* Transport module did not support operation */
65 ISCSI_ERR_TRANS_CAPS = 14,
66 /* Session is logged in */
67 ISCSI_ERR_SESS_EXISTS = 15,
68 /* Invalid IPC MGMT request */
69 ISCSI_ERR_INVALID_MGMT_REQ = 16,
70 /* iSNS service is not supported */
71 ISCSI_ERR_ISNS_UNAVAILABLE = 17,
72 /* A read/write to iscsid failed */
73 ISCSI_ERR_ISCSID_COMM_ERR = 18,
74 /* Fatal login error */
75 ISCSI_ERR_FATAL_LOGIN = 19,
76 /* Could ont connect to iscsid */
77 ISCSI_ERR_ISCSID_NOTCONN = 20,
78 /* No records/targets/sessions/portals found to execute operation on */
79 ISCSI_ERR_NO_OBJS_FOUND = 21,
80 /* Could not lookup object in sysfs */
81 ISCSI_ERR_SYSFS_LOOKUP = 22,
82 /* Could not lookup host */
83 ISCSI_ERR_HOST_NOT_FOUND = 23,
84 /* Login failed due to authorization failure */
85 ISCSI_ERR_LOGIN_AUTH_FAILED = 24,
86 /* iSNS query failure */
87 ISCSI_ERR_ISNS_QUERY = 25,
88 /* iSNS registration/deregistration failed */
89 ISCSI_ERR_ISNS_REG_FAILED = 26,
90 /* operation not supported */
91 ISCSI_ERR_OP_NOT_SUPP = 27,
92 /* device or resource in use */
93 ISCSI_ERR_BUSY = 28,
94 /* Operation failed, but retrying layer may succeed */
95 ISCSI_ERR_AGAIN = 29,
96 /* unknown discovery type */
97 ISCSI_ERR_UNKNOWN_DISCOVERY_TYPE = 30,
98
99 /* Always last. Indicates end of error code space */
100 ISCSI_MAX_ERR_VAL,
101 };
102 #endif /* HAVE_LIBISCSI_ERR */
103
104
105 const gchar *iscsi_nodes_fmt = "a(sisis)";
106 const gchar *iscsi_node_fmt = "(sisis)";
107
108 static void
109 iscsi_make_auth_info (struct libiscsi_auth_info *auth_info,
110 const gchar *username,
111 const gchar *password,
112 const gchar *reverse_username,
113 const gchar *reverse_password)
114 {
115 g_return_if_fail (auth_info);
116
117 memset (auth_info, 0, sizeof (struct libiscsi_auth_info));
118 auth_info->method = libiscsi_auth_none;
119
120 /* CHAP username + password */
121 if (username && *username)
122 {
123 auth_info->method = libiscsi_auth_chap;
124 strncpy (auth_info->chap.username, username, LIBISCSI_VALUE_MAXLEN - 1);
125 if (password && *password)
126 strncpy (auth_info->chap.password, password, LIBISCSI_VALUE_MAXLEN - 1);
127 }
128
129 /* CHAP reverse username + reverse password */
130 if (reverse_username && *reverse_username)
131 {
132 auth_info->method = libiscsi_auth_chap;
133 strncpy (auth_info->chap.reverse_username, reverse_username, LIBISCSI_VALUE_MAXLEN - 1);
134 if (reverse_password && *reverse_password)
135 strncpy (auth_info->chap.reverse_password, reverse_password, LIBISCSI_VALUE_MAXLEN - 1);
136 }
137 }
138
139 static void
140 iscsi_make_node (struct libiscsi_node *node,
141 const gchar *name,
142 const gint tpgt,
143 const gchar *address,
144 const gint port,
145 const gchar *iface)
146 {
147 g_return_if_fail (node);
148
149 memset (node, 0, sizeof (struct libiscsi_node));
150
151 /* Fill libiscsi parameters. */
152 strncpy (node->name, name, LIBISCSI_VALUE_MAXLEN - 1);
153 strncpy (node->address, address, NI_MAXHOST - 1);
154 strncpy (node->iface, iface, LIBISCSI_VALUE_MAXLEN - 1);
155 node->tpgt = tpgt;
156 node->port = port;
157 }
158
159 static gint
160 iscsi_perform_login_action (UDisksLinuxModuleISCSI *module,
161 libiscsi_login_action action,
162 struct libiscsi_node *node,
163 struct libiscsi_auth_info *auth_info,
164 gchar **errorstr)
165 {
166 struct libiscsi_context *ctx;
167 gint err;
168
169 g_return_val_if_fail (UDISKS_IS_LINUX_MODULE_ISCSI (module), 1);
170
171 /* Get a libiscsi context. */
172 ctx = udisks_linux_module_iscsi_get_libiscsi_context (module);
173
174 if (action == ACTION_LOGIN && auth_info)
175 libiscsi_node_set_auth (ctx, node, auth_info);
176
177 /* Login or Logout */
178 err = action == ACTION_LOGIN ?
179 libiscsi_node_login (ctx, node) :
180 libiscsi_node_logout (ctx, node);
181
182 if (errorstr && err != 0)
183 *errorstr = g_strdup (libiscsi_get_error_string (ctx));
184
185 return err;
186 }
187
188 static gint
189 iscsi_node_set_parameters (struct libiscsi_context *ctx,
190 struct libiscsi_node *node,
191 GVariant *params,
192 gchar **errorstr)
193 {
194 GVariantIter iter;
195 GVariant *value;
196 gchar *key;
197 gchar *param_value;
198 gint err = 0;
199
200 g_return_val_if_fail (ctx, ISCSI_ERR_INVAL);
201 g_return_val_if_fail (node, ISCSI_ERR_INVAL);
202 g_return_val_if_fail (params, ISCSI_ERR_INVAL);
203
204 g_variant_iter_init (&iter, params);
205 while (err == 0 && g_variant_iter_next (&iter, "{sv}", &key, &value))
206 {
207 g_variant_get (value, "&s", &param_value);
208
209 /* Update the node parameter value. */
210 err = libiscsi_node_set_parameter (ctx, node, key, param_value);
211 if (errorstr && err != 0)
212 *errorstr = g_strdup (libiscsi_get_error_string (ctx));
213
214 g_variant_unref (value);
215 g_free (key);
216 }
217
218 return 0;
219 }
220
221 static void
222 iscsi_params_get_chap_data (GVariant *params,
223 const gchar **username,
224 const gchar **password,
225 const gchar **reverse_username,
226 const gchar **reverse_password)
227 {
228 g_return_if_fail (params);
229
230 g_variant_lookup (params, "username", "&s", username);
231 g_variant_lookup (params, "password", "&s", password);
232 g_variant_lookup (params, "reverse-username", "&s", reverse_username);
233 g_variant_lookup (params, "reverse-password", "&s", reverse_password);
234 }
235
236 static GVariant *
237 iscsi_params_pop_chap_data (GVariant *params,
238 const gchar **username,
239 const gchar **password,
240 const gchar **reverse_username,
241 const gchar **reverse_password)
242 {
243 GVariantDict dict;
244
245 g_return_val_if_fail (params, NULL);
246
247 /* Pop CHAP parameters */
248 g_variant_dict_init (&dict, params);
249 g_variant_dict_lookup (&dict, "username", "&s", username);
250 g_variant_dict_lookup (&dict, "password", "&s", password);
251 g_variant_dict_lookup (&dict, "reverse-username", "&s", reverse_username);
252 g_variant_dict_lookup (&dict, "reverse-password", "&s", reverse_password);
253
254 if (username)
255 g_variant_dict_remove (&dict, "username");
256 if (password)
257 g_variant_dict_remove (&dict, "password");
258 if (reverse_username)
259 g_variant_dict_remove (&dict, "reverse-username");
260 if (reverse_password)
261 g_variant_dict_remove (&dict, "reverse-password");
262
263 /* Update the params, so that it doesn't contain CHAP parameters. */
264 return g_variant_dict_end (&dict);
265 }
266
267 static gboolean
268 is_auth_required (struct libiscsi_context *ctx,
269 struct libiscsi_node *node,
270 struct libiscsi_auth_info *auth_info)
271 {
272 char val[LIBISCSI_VALUE_MAXLEN + 1] = {'\0',};
273 int ret;
274
275 /* TODO: No way to distinguish between the "no auth requested" and
276 * "retain discovered auth info" scenarios from the D-Bus API.
277 */
278
279 /* In case CHAP auth is requested, let's use it unconditionally */
280 if (auth_info->method != libiscsi_auth_none)
281 return TRUE;
282
283 /* Avoid auth override on firmware-discovered nodes */
284 ret = libiscsi_node_get_parameter (ctx, node, "node.discovery_type", val);
285 if (ret == 0 && g_strcmp0 (val, "fw") == 0)
286 return FALSE;
287
288 /* Not a firmware-discovered node, maintain legacy rules */
289 return TRUE;
290 }
291
292 gint
293 iscsi_login (UDisksLinuxModuleISCSI *module,
294 const gchar *name,
295 const gint tpgt,
296 const gchar *address,
297 const gint port,
298 const gchar *iface,
299 GVariant *params,
300 gchar **errorstr)
301 {
302 struct libiscsi_context *ctx;
303 struct libiscsi_auth_info auth_info = {0,};
304 struct libiscsi_node node = {0,};
305 GVariant *params_without_chap;
306 const gchar *username = NULL;
307 const gchar *password = NULL;
308 const gchar *reverse_username = NULL;
309 const gchar *reverse_password = NULL;
310 gint err = 0;
311
312 g_return_val_if_fail (UDISKS_IS_LINUX_MODULE_ISCSI (module), 1);
313
314 /* Optional data for CHAP authentication. We pop these parameters from the
315 * dictionary; it then contains only iSCSI node parameters. */
316 params_without_chap = iscsi_params_pop_chap_data (params,
317 &username,
318 &password,
319 &reverse_username,
320 &reverse_password);
321
322 /* Prepare authentication data */
323 iscsi_make_auth_info (&auth_info,
324 username,
325 password,
326 reverse_username,
327 reverse_password);
328
329 /* Create iscsi node. */
330 iscsi_make_node (&node, name, tpgt, address, port, iface);
331
332 /* Get iscsi context. */
333 ctx = udisks_linux_module_iscsi_get_libiscsi_context (module);
334
335 /* Update node parameters. */
336 if (params)
337 err = iscsi_node_set_parameters (ctx, &node, params_without_chap, errorstr);
338
339 /* Login */
340 if (err == 0)
341 {
342 err = iscsi_perform_login_action (module,
343 ACTION_LOGIN,
344 &node,
345 is_auth_required (ctx, &node, &auth_info) ? &auth_info : NULL,
346 errorstr);
347 }
348
349 g_variant_unref (params_without_chap);
350
351 return err;
352 }
353
354 gint
355 iscsi_logout (UDisksLinuxModuleISCSI *module,
356 const gchar *name,
357 const gint tpgt,
358 const gchar *address,
359 const gint port,
360 const gchar *iface,
361 GVariant *params,
362 gchar **errorstr)
363 {
364 struct libiscsi_context *ctx;
365 struct libiscsi_node node = {0,};
366 gint err = 0;
367
368 g_return_val_if_fail (UDISKS_IS_LINUX_MODULE_ISCSI (module), 1);
369
370 /* Create iscsi node. */
371 iscsi_make_node (&node, name, tpgt, address, port, iface);
372
373 /* Get iscsi context. */
374 ctx = udisks_linux_module_iscsi_get_libiscsi_context (module);
375
376 /* Update node parameters. */
377 if (params)
378 err = iscsi_node_set_parameters (ctx, &node, params, errorstr);
379
380 /* Logout */
381 if (err == 0)
382 {
383 err = iscsi_perform_login_action (module,
384 ACTION_LOGOUT,
385 &node,
386 NULL,
387 errorstr);
388 }
389
390 return err;
391 }
392
393 gint
394 iscsi_discover_send_targets (UDisksLinuxModuleISCSI *module,
395 const gchar *address,
396 const guint16 port,
397 GVariant *params,
398 GVariant **nodes,
399 gint *nodes_cnt,
400 gchar **errorstr)
401 {
402 struct libiscsi_context *ctx;
403 struct libiscsi_auth_info auth_info = {0,};
404 struct libiscsi_node *found_nodes;
405 const gchar *username = NULL;
406 const gchar *password = NULL;
407 const gchar *reverse_username = NULL;
408 const gchar *reverse_password = NULL;
409 gint err;
410
411 g_return_val_if_fail (UDISKS_IS_LINUX_MODULE_ISCSI (module), 1);
412
413 ctx = udisks_linux_module_iscsi_get_libiscsi_context (module);
414
415 /* Optional data for CHAP authentication. */
416 iscsi_params_get_chap_data (params,
417 &username,
418 &password,
419 &reverse_username,
420 &reverse_password);
421
422 /* Prepare authentication data */
423 iscsi_make_auth_info (&auth_info,
424 username,
425 password,
426 reverse_username,
427 reverse_password);
428
429 /* Discovery */
430 err = libiscsi_discover_sendtargets (ctx,
431 address,
432 port,
433 &auth_info,
434 nodes_cnt,
435 &found_nodes);
436
437 if (err == 0)
438 *nodes = iscsi_libiscsi_nodes_to_gvariant (found_nodes, *nodes_cnt);
439 else if (errorstr)
440 *errorstr = g_strdup (libiscsi_get_error_string (ctx));
441
442 /* Release the resources */
443 iscsi_libiscsi_nodes_free (found_nodes);
444
445 return err;
446 }
447
448 GVariant *
449 iscsi_libiscsi_nodes_to_gvariant (const struct libiscsi_node *nodes,
450 const gint nodes_cnt)
451 {
452 gint i;
453 GVariantBuilder builder;
454
455 g_variant_builder_init (&builder, G_VARIANT_TYPE (iscsi_nodes_fmt));
456 for (i = 0; i < nodes_cnt; ++i)
457 {
458 g_variant_builder_add (&builder,
459 iscsi_node_fmt,
460 nodes[i].name,
461 nodes[i].tpgt,
462 nodes[i].address,
463 nodes[i].port,
464 nodes[i].iface);
465 }
466 return g_variant_builder_end (&builder);
467 }
468
469 void
470 iscsi_libiscsi_nodes_free (const struct libiscsi_node *nodes)
471 {
472 g_free ((gpointer) nodes);
473 }
474
475 UDisksError
476 iscsi_error_to_udisks_error (const gint err)
477 {
478 switch (err)
479 {
480 case ISCSI_ERR_TRANS: /* 4 */
481 return UDISKS_ERROR_ISCSI_TRANSPORT_FAILED;
482 case ISCSI_ERR_LOGIN: /* 5 */
483 return UDISKS_ERROR_ISCSI_LOGIN_FAILED;
484 case ISCSI_ERR_IDBM: /* 6 */
485 return UDISKS_ERROR_ISCSI_IDMB;
486 case ISCSI_ERR_LOGOUT: /* 10 */
487 return UDISKS_ERROR_ISCSI_LOGOUT_FAILED;
488 case ISCSI_ERR_ISCSID_COMM_ERR: /* 18 */
489 return UDISKS_ERROR_ISCSI_DAEMON_TRANSPORT_FAILED;
490 case ISCSI_ERR_FATAL_LOGIN: /* 19 */
491 return UDISKS_ERROR_ISCSI_LOGIN_FATAL;
492 case ISCSI_ERR_ISCSID_NOTCONN: /* 20 */
493 return UDISKS_ERROR_ISCSI_NOT_CONNECTED;
494 case ISCSI_ERR_NO_OBJS_FOUND: /* 21 */
495 return UDISKS_ERROR_ISCSI_NO_OBJECTS_FOUND;
496 case ISCSI_ERR_HOST_NOT_FOUND: /* 23 */
497 return UDISKS_ERROR_ISCSI_HOST_NOT_FOUND;
498 case ISCSI_ERR_LOGIN_AUTH_FAILED: /* 24 */
499 return UDISKS_ERROR_ISCSI_LOGIN_AUTH_FAILED;
500 case ISCSI_ERR_UNKNOWN_DISCOVERY_TYPE: /* 30 */
501 return UDISKS_ERROR_ISCSI_UNKNOWN_DISCOVERY_TYPE;
502
503 default:
504 return UDISKS_ERROR_FAILED;
505 }
506 }
507
508 UDisksObject *
509 wait_for_iscsi_object (UDisksDaemon *daemon,
510 gpointer user_data)
511 {
512 const gchar *device_iqn = user_data;
513 UDisksObject *ret = NULL;
514 GList *objects, *l;
515 const gchar *const *symlinks = NULL;
516
517 objects = udisks_daemon_get_objects (daemon);
518 for (l = objects; l != NULL; l = l->next)
519 {
520 UDisksObject *object = UDISKS_OBJECT (l->data);
521 UDisksBlock *block;
522
523 block = udisks_object_peek_block (object);
524 if (block != NULL)
525 {
526 symlinks = udisks_block_get_symlinks (UDISKS_BLOCK (block));
527 if (symlinks != NULL)
528 for (guint n = 0; symlinks[n] != NULL; n++)
529 if (g_str_has_prefix (symlinks[n], "/dev/disk/by-path/") &&
530 strstr (symlinks[n], device_iqn) != NULL)
531 {
532 ret = g_object_ref (object);
533 goto out;
534 }
535 }
536 }
537
538 out:
539 g_list_free_full (objects, g_object_unref);
540 return ret;
541 }
542
543 UDisksObject *
544 wait_for_iscsi_session_object (UDisksDaemon *daemon,
545 gpointer user_data)
546 {
547 const gchar *device_iqn = user_data;
548 UDisksObject *ret = NULL;
549 GList *objects, *l;
550
551 objects = udisks_daemon_get_objects (daemon);
552 for (l = objects; l != NULL; l = l->next)
553 {
554 UDisksObject *object = UDISKS_OBJECT (l->data);
555 UDisksISCSISession *session;
556
557 session = udisks_object_peek_iscsi_session (object);
558 if (session != NULL)
559 {
560 if (g_strcmp0 (udisks_iscsi_session_get_target_name (session), device_iqn) == 0)
561 {
562 ret = g_object_ref (object);
563 goto out;
564 }
565 }
566 }
567
568 out:
569 g_list_free_full (objects, g_object_unref);
570 return ret;
571 }