-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement decryption #24
Open
ashfire908
wants to merge
5
commits into
dasJ:decryption
Choose a base branch
from
ashfire908:decryption
base: decryption
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−10
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
81d03e3
Exit with error when we fail to mount anything
ashfire908 e87ecac
Allow command to prompt for a password
ashfire908 d920162
perror does not need a newline
ashfire908 82b3fcb
Implement decryption methods
ashfire908 1fcfb7f
Revert "Exit with error when we fail to mount anything"
ashfire908 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,29 +9,75 @@ | |
// Buffer for reading ZFS command output | ||
#define BUFSIZE 16 | ||
|
||
int execute(char *command, char needOutput, char **output, char *param[]) { | ||
int pip[2]; | ||
pid_t pid; | ||
int status; | ||
int execute(char *command, char needOutput, char **output, char *param[], char needPassword, char *prompt) { | ||
int pip[2], pwpip[2]; | ||
pid_t pid, pwpid; | ||
int status, pwstatus; | ||
char *linebuffer; | ||
size_t size; | ||
int nRead; | ||
char **cmdline; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need to take another look at this |
||
|
||
if (needPassword == 1) { | ||
// Execute password prompt | ||
if (pipe(pwpip) == -1) { | ||
perror("Cannot create password pipe"); | ||
exit(1); | ||
} | ||
pwpid = fork(); | ||
if (pwpid == 0) { | ||
// Redirect stdout to pipe | ||
while ((dup2(pwpip[1], STDOUT_FILENO) == -1) && (errno == EINTR)) {} | ||
if (errno != 0) { | ||
perror("Cannot connect stdout to parent password pipe"); | ||
exit(1); | ||
} | ||
// Close pipe, we won't need it anymore | ||
close(pwpip[1]); | ||
// Close stdin to prevent weird redirection | ||
close(0); | ||
// Execute | ||
if (prompt == NULL) { | ||
cmdline = (char*[]) { SYSTEMD_ASK_PASS_CMD, NULL }; | ||
} else { | ||
cmdline = (char*[]) { SYSTEMD_ASK_PASS_CMD, prompt, NULL }; | ||
} | ||
execv(SYSTEMD_ASK_PASS_EXE, cmdline); | ||
exit(254); | ||
} else if (pwpid < 0) { | ||
perror("Cannot fork"); | ||
close(pwpip[0]); | ||
close(pwpip[1]); | ||
return pwpid; | ||
} | ||
} | ||
|
||
// Execute | ||
if (needOutput == 1) { | ||
if (pipe(pip) == -1) { | ||
perror("Cannot create pipe\n"); | ||
perror("Cannot create output pipe"); | ||
ashfire908 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
exit(1); | ||
} | ||
} | ||
pid = fork(); | ||
if (pid == 0) { | ||
if (needPassword == 1) { | ||
// Redirect password pipe to stdin | ||
while ((dup2(pwpip[0], STDIN_FILENO) == -1) && (errno == EINTR)) {} | ||
if (errno != 0) { | ||
perror("Cannot connect stdin to parent password pipe"); | ||
exit(1); | ||
} | ||
// Close pipe, we won't need them anymore | ||
close(pwpip[0]); | ||
} | ||
|
||
// Set up pipe | ||
if (needOutput == 1) { | ||
// Redirect stdout to pipe | ||
while ((dup2(pip[1], STDOUT_FILENO) == -1) && (errno == EINTR)) {} | ||
if (errno != 0) { | ||
perror("Cannot connect to parent pipe\n"); | ||
perror("Cannot connect stdout to parent output pipe"); | ||
ashfire908 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
exit(1); | ||
} | ||
// Close pipe, we won't need it anymore | ||
|
@@ -45,13 +91,18 @@ int execute(char *command, char needOutput, char **output, char *param[]) { | |
execv(command, param); | ||
exit(254); | ||
} else if (pid < 0) { | ||
perror("Cannot fork\n"); | ||
perror("Cannot fork"); | ||
if (needOutput == 1) { | ||
close(pip[0]); | ||
close(pip[1]); | ||
} | ||
return pid; | ||
} | ||
// Close out the password pipe | ||
if (needPassword == 1) { | ||
close(pwpip[0]); | ||
close(pwpip[1]); | ||
} | ||
// Capture output | ||
if (needOutput == 1) { | ||
// We don't need to write to the pipe | ||
|
@@ -80,7 +131,13 @@ int execute(char *command, char needOutput, char **output, char *param[]) { | |
free(linebuffer); | ||
} | ||
// Wait for quit | ||
if (needPassword == 1) { | ||
waitpid(pwpid, &pwstatus, 0); | ||
} | ||
waitpid(pid, &status, 0); | ||
if (needPassword == 1 && pwstatus != 0) { | ||
return pwstatus; | ||
} | ||
return status; | ||
} | ||
|
||
|
@@ -92,7 +149,19 @@ int execute(char *command, char needOutput, char **output, char *param[]) { | |
* is ZFS_CMD | ||
*/ | ||
int executeZfs(char needOutput, char **output, char *param[]) { | ||
return execute(ZFS_EXE, needOutput, output, param); | ||
return execute(ZFS_EXE, needOutput, output, param, 0, NULL); | ||
} | ||
|
||
/* | ||
* Executes a zfs command, feeding it the input from a systemd password | ||
* prompt to stdin. | ||
* If needOutput is 1, the output of the command is written to output | ||
* which will be allocated. It must be NULL when passing in. | ||
* param must be a null-terminated array of parameters where the first | ||
* is ZFS_CMD | ||
*/ | ||
int executeZfsWithPassword(char needOutput, char **output, char *param[], char *prompt) { | ||
return execute(ZFS_EXE, needOutput, output, param, 1, prompt); | ||
} | ||
|
||
/* | ||
|
@@ -103,7 +172,7 @@ int executeZfs(char needOutput, char **output, char *param[]) { | |
* is ZPOOL_CMD | ||
*/ | ||
int executeZpool(char needOutput, char **output, char *param[]) { | ||
return execute(ZPOOL_EXE, needOutput, output, param); | ||
return execute(ZPOOL_EXE, needOutput, output, param, 0, NULL); | ||
} | ||
|
||
/* | ||
|
@@ -114,7 +183,7 @@ int executeZpool(char needOutput, char **output, char *param[]) { | |
* is MOUNT_CMD | ||
*/ | ||
int executeMount(char needOutput, char **output, char *param[]) { | ||
return execute(MOUNT_EXE, needOutput, output, param); | ||
return execute(MOUNT_EXE, needOutput, output, param, 0, NULL); | ||
} | ||
|
||
int zfs_destroy_recursively(char *dataset) { | ||
|
@@ -325,3 +394,52 @@ int zfs_get_alt_mp(char *dataset, char **mountpoint) { | |
} | ||
return status; | ||
} | ||
|
||
int zfs_ds_requires_password(char *dataset) { | ||
char *output = NULL; | ||
char *cmdline[] = { ZFS_CMD, "get", "-Ho", "value", "encryption,keyformat", dataset, NULL }; | ||
char *status = NULL; | ||
char *keytype = NULL; | ||
|
||
if (executeZfs(1, &output, cmdline) != 0) { | ||
return -1; | ||
} | ||
|
||
status = strtok(output, "\n"); | ||
if (strcmp(status, "off") != 0) { | ||
keytype = strtok(NULL, "\n"); | ||
if (strcmp(keytype, "passphrase") == 0) { | ||
return 1; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int zfs_decrypt_ds_with_password(char *dataset) { | ||
int ret; | ||
char *encroot = NULL; | ||
char **cmdline; | ||
const char *promptMessage = "Enter passphrase for '%s':"; | ||
char *prompt; | ||
|
||
cmdline = (char*[]) { ZFS_CMD, "get", "-Ho", "value", "encryptionroot", dataset, NULL }; | ||
if (executeZfs(1, &encroot, cmdline) != 0) { | ||
return 0; | ||
} | ||
|
||
if (encroot != NULL) { | ||
(encroot)[strlen(encroot) - 1] = '\0'; | ||
} | ||
|
||
cmdline = (char*[]) { ZFS_CMD, "load-key", encroot, NULL }; | ||
prompt = (char*)malloc((strlen(promptMessage) - 2 + strlen(encroot)) * sizeof(char)); | ||
sprintf(prompt, promptMessage, encroot); | ||
|
||
ret = executeZfsWithPassword(0, NULL, cmdline, prompt); | ||
if (ret == 0) { | ||
return 1; | ||
} else { | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
Wants
.See systemd/systemd#29053