Skip to content

Commit

Permalink
block: New bdrv_add_key(), convert monitor to use it
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Armbruster <[email protected]>
Reviewed-by: Eric Blake <[email protected]>
Message-id: [email protected]
Reviewed-by: Max Reitz <[email protected]>
Signed-off-by: Max Reitz <[email protected]>
  • Loading branch information
Markus Armbruster authored and XanClic committed Feb 6, 2015
1 parent 2e3a026 commit 4d2855a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 31 deletions.
29 changes: 29 additions & 0 deletions block.c
Original file line number Diff line number Diff line change
Expand Up @@ -3713,6 +3713,35 @@ int bdrv_set_key(BlockDriverState *bs, const char *key)
return ret;
}

/*
* Provide an encryption key for @bs.
* If @key is non-null:
* If @bs is not encrypted, fail.
* Else if the key is invalid, fail.
* Else set @bs's key to @key, replacing the existing key, if any.
* If @key is null:
* If @bs is encrypted and still lacks a key, fail.
* Else do nothing.
* On failure, store an error object through @errp if non-null.
*/
void bdrv_add_key(BlockDriverState *bs, const char *key, Error **errp)
{
if (key) {
if (!bdrv_is_encrypted(bs)) {
error_set(errp, QERR_DEVICE_NOT_ENCRYPTED,
bdrv_get_device_name(bs));
} else if (bdrv_set_key(bs, key) < 0) {
error_set(errp, QERR_INVALID_PASSWORD);
}
} else {
if (bdrv_key_required(bs)) {
error_set(errp, QERR_DEVICE_ENCRYPTED,
bdrv_get_device_name(bs),
bdrv_get_encrypted_filename(bs));
}
}
}

const char *bdrv_get_format_name(BlockDriverState *bs)
{
return bs->drv ? bs->drv->format_name : NULL;
Expand Down
24 changes: 2 additions & 22 deletions blockdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -1793,7 +1793,6 @@ void qmp_block_passwd(bool has_device, const char *device,
Error *local_err = NULL;
BlockDriverState *bs;
AioContext *aio_context;
int err;

bs = bdrv_lookup_bs(has_device ? device : NULL,
has_node_name ? node_name : NULL,
Expand All @@ -1806,16 +1805,8 @@ void qmp_block_passwd(bool has_device, const char *device,
aio_context = bdrv_get_aio_context(bs);
aio_context_acquire(aio_context);

err = bdrv_set_key(bs, password);
if (err == -EINVAL) {
error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
goto out;
} else if (err < 0) {
error_set(errp, QERR_INVALID_PASSWORD);
goto out;
}
bdrv_add_key(bs, password, errp);

out:
aio_context_release(aio_context);
}

Expand All @@ -1833,18 +1824,7 @@ static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename,
return;
}

if (bdrv_key_required(bs)) {
if (password) {
if (bdrv_set_key(bs, password) < 0) {
error_set(errp, QERR_INVALID_PASSWORD);
}
} else {
error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
bdrv_get_encrypted_filename(bs));
}
} else if (password) {
error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
}
bdrv_add_key(bs, password, errp);
}

void qmp_change_blockdev(const char *device, const char *filename,
Expand Down
1 change: 1 addition & 0 deletions include/block/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ BlockDriverState *bdrv_next(BlockDriverState *bs);
int bdrv_is_encrypted(BlockDriverState *bs);
int bdrv_key_required(BlockDriverState *bs);
int bdrv_set_key(BlockDriverState *bs, const char *key);
void bdrv_add_key(BlockDriverState *bs, const char *key, Error **errp);
int bdrv_query_missing_keys(void);
void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
void *opaque);
Expand Down
16 changes: 11 additions & 5 deletions monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -5368,9 +5368,12 @@ static void bdrv_password_cb(void *opaque, const char *password,
Monitor *mon = opaque;
BlockDriverState *bs = readline_opaque;
int ret = 0;
Error *local_err = NULL;

if (bdrv_set_key(bs, password) != 0) {
monitor_printf(mon, "invalid password\n");
bdrv_add_key(bs, password, &local_err);
if (local_err) {
monitor_printf(mon, "%s\n", error_get_pretty(local_err));
error_free(local_err);
ret = -EPERM;
}
if (mon->password_completion_cb)
Expand All @@ -5388,17 +5391,20 @@ int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
BlockCompletionFunc *completion_cb,
void *opaque)
{
Error *local_err = NULL;
int err;

if (!bdrv_key_required(bs)) {
bdrv_add_key(bs, NULL, &local_err);
if (!local_err) {
if (completion_cb)
completion_cb(opaque, 0);
return 0;
}

/* Need a key for @bs */

if (monitor_ctrl_mode(mon)) {
qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
bdrv_get_encrypted_filename(bs));
qerror_report_err(local_err);
return -1;
}

Expand Down
8 changes: 4 additions & 4 deletions qmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ SpiceInfo *qmp_query_spice(Error **errp)

void qmp_cont(Error **errp)
{
Error *local_err = NULL;
BlockDriverState *bs;

if (runstate_needs_reset()) {
Expand All @@ -167,10 +168,9 @@ void qmp_cont(Error **errp)
bdrv_iostatus_reset(bs);
}
for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
if (bdrv_key_required(bs)) {
error_set(errp, QERR_DEVICE_ENCRYPTED,
bdrv_get_device_name(bs),
bdrv_get_encrypted_filename(bs));
bdrv_add_key(bs, NULL, &local_err);
if (local_err) {
error_propagate(errp, local_err);
return;
}
}
Expand Down

0 comments on commit 4d2855a

Please sign in to comment.