Skip to content

Commit 14a2a7e

Browse files
committed
Implement decryption methods
1 parent c463923 commit 14a2a7e

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/zfs-util.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ int executeZfs(char needOutput, char **output, char *param[]) {
152152
return execute(ZFS_EXE, needOutput, output, param, 0, NULL);
153153
}
154154

155+
/*
156+
* Executes a zfs command, feeding it the input from a systemd password
157+
* prompt to stdin.
158+
* If needOutput is 1, the output of the command is written to output
159+
* which will be allocated. It must be NULL when passing in.
160+
* param must be a null-terminated array of parameters where the first
161+
* is ZFS_CMD
162+
*/
163+
int executeZfsWithPassword(char needOutput, char **output, char *param[], char *prompt) {
164+
return execute(ZFS_EXE, needOutput, output, param, 1, prompt);
165+
}
166+
155167
/*
156168
* Executes a zpool command.
157169
* If needOutput is 1, the output of the command is written to output
@@ -382,3 +394,52 @@ int zfs_get_alt_mp(char *dataset, char **mountpoint) {
382394
}
383395
return status;
384396
}
397+
398+
int zfs_ds_requires_password(char *dataset) {
399+
char *output = NULL;
400+
char *cmdline[] = { ZFS_CMD, "get", "-Ho", "value", "encryption,keyformat", dataset, NULL };
401+
char *status = NULL;
402+
char *keytype = NULL;
403+
404+
if (executeZfs(1, &output, cmdline) != 0) {
405+
return -1;
406+
}
407+
408+
status = strtok(output, "\n");
409+
if (strcmp(status, "off") != 0) {
410+
keytype = strtok(NULL, "\n");
411+
if (strcmp(keytype, "passphrase") == 0) {
412+
return 1;
413+
}
414+
}
415+
416+
return 0;
417+
}
418+
419+
int zfs_decrypt_ds_with_password(char *dataset) {
420+
int ret;
421+
char *encroot = NULL;
422+
char **cmdline;
423+
const char *promptMessage = "Enter passphrase for '%s':";
424+
char *prompt;
425+
426+
cmdline = (char*[]) { ZFS_CMD, "get", "-Ho", "value", "encryptionroot", dataset, NULL };
427+
if (executeZfs(1, &encroot, cmdline) != 0) {
428+
return 0;
429+
}
430+
431+
if (encroot != NULL) {
432+
(encroot)[strlen(encroot) - 1] = '\0';
433+
}
434+
435+
cmdline = (char*[]) { ZFS_CMD, "load-key", encroot, NULL };
436+
prompt = (char*)malloc((strlen(promptMessage) - 2 + strlen(encroot)) * sizeof(char));
437+
sprintf(prompt, promptMessage, encroot);
438+
439+
ret = executeZfsWithPassword(0, NULL, cmdline, prompt);
440+
if (ret == 0) {
441+
return 1;
442+
} else {
443+
return 0;
444+
}
445+
}

0 commit comments

Comments
 (0)