aboutsummaryrefslogtreecommitdiffstats
path: root/g13/sh-cmd.c
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2016-08-13 10:49:54 +0000
committerWerner Koch <[email protected]>2016-08-13 11:49:41 +0000
commitb57f55321295846d47144bd6b39fbbcac0127421 (patch)
tree4469af47dd36b04c9e1a4309f127d02194b1ff54 /g13/sh-cmd.c
parentAvoid leading ": " in the log output when there are no prefixes. (diff)
downloadgnupg-b57f55321295846d47144bd6b39fbbcac0127421.tar.gz
gnupg-b57f55321295846d47144bd6b39fbbcac0127421.zip
g13: New command --find-device.
* common/status.h (STATUS_BLOCKDEV: New. * g13/call-syshelp.c: Include "call-syshelp.h". (finddevice_status_cb, call_syshelp_find_device): New. * g13/g13.c (aFindDevice): New. (opts): Add "--find-device". (main): Implement --find-device. * g13/sh-cmd.c (cmd_finddevice): New. (register_commands): Register new command. -- This might be useful for scripting. Signed-off-by: Werner Koch <[email protected]>
Diffstat (limited to 'g13/sh-cmd.c')
-rw-r--r--g13/sh-cmd.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/g13/sh-cmd.c b/g13/sh-cmd.c
index fe596f460..e00bb774c 100644
--- a/g13/sh-cmd.c
+++ b/g13/sh-cmd.c
@@ -157,6 +157,82 @@ reset_notify (assuan_context_t ctx, char *line)
}
+static const char hlp_finddevice[] =
+ "FINDDEVICE <name>\n"
+ "\n"
+ "Find the device matching NAME. NAME be any identifier from\n"
+ "g13tab permissable for the user. The corresponding block\n"
+ "device is retruned using a status line.";
+static gpg_error_t
+cmd_finddevice (assuan_context_t ctx, char *line)
+{
+ ctrl_t ctrl = assuan_get_pointer (ctx);
+ gpg_error_t err = 0;
+ tab_item_t ti;
+ const char *s;
+ const char *name;
+
+ name = skip_options (line);
+
+ /* Are we allowed to use the given device? We check several names:
+ * 1. The full block device
+ * 2. The label
+ * 3. The final part of the block device if NAME does not have a slash.
+ * 4. The mountpoint
+ */
+ for (ti=ctrl->client.tab; ti; ti = ti->next)
+ if (!strcmp (name, ti->blockdev))
+ break;
+ if (!ti)
+ {
+ for (ti=ctrl->client.tab; ti; ti = ti->next)
+ if (ti->label && !strcmp (name, ti->label))
+ break;
+ }
+ if (!ti && !strchr (name, '/'))
+ {
+ for (ti=ctrl->client.tab; ti; ti = ti->next)
+ {
+ s = strrchr (ti->blockdev, '/');
+ if (s && s[1] && !strcmp (name, s+1))
+ break;
+ }
+ }
+ if (!ti)
+ {
+ for (ti=ctrl->client.tab; ti; ti = ti->next)
+ if (ti->mountpoint && !strcmp (name, ti->mountpoint))
+ break;
+ }
+
+ if (!ti)
+ {
+ err = set_error (GPG_ERR_NOT_FOUND, "device not configured for user");
+ goto leave;
+ }
+
+ /* Check whether we have permissions to open the device. */
+ {
+ estream_t fp = es_fopen (ti->blockdev, "rb");
+ if (!fp)
+ {
+ err = gpg_error_from_syserror ();
+ log_error ("error opening '%s': %s\n",
+ ti->blockdev, gpg_strerror (err));
+ goto leave;
+ }
+ es_fclose (fp);
+ }
+
+ err = g13_status (ctrl, STATUS_BLOCKDEV, ti->blockdev, NULL);
+ if (err)
+ return err;
+
+ leave:
+ return leave_cmd (ctx, err);
+}
+
+
static const char hlp_device[] =
"DEVICE <name>\n"
"\n"
@@ -588,6 +664,7 @@ register_commands (assuan_context_t ctx, int fail_all)
assuan_handler_t handler;
const char * const help;
} table[] = {
+ { "FINDDEVICE", cmd_finddevice, hlp_finddevice },
{ "DEVICE", cmd_device, hlp_device },
{ "CREATE", cmd_create, hlp_create },
{ "MOUNT", cmd_mount, hlp_mount },