Skip to content
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

Allow fetching passwords from environment variables #338

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1448,22 +1448,31 @@ static FILE *open_file(const char *name, bool input) {

static bool get_input_data(const char *name, uint8_t **out, size_t *len,
cmd_format fmt) {
const char *fname = strncasecmp(name, "file:", 5) ? name : name + 5;
bool is_file = !strncasecmp(name, "file:", 5);
bool is_envvar = !strncasecmp(name, "env:", 4);
const char *fname = is_file ? name + 5 : name;
const char *envvar = is_envvar ? name + 4 : NULL;
struct stat sb = {0};
int st_res = stat(fname, &sb);
if (st_res == 0 && S_ISREG(sb.st_mode)) {
*len = sb.st_size;
is_file = true;
} else {
*len = ARGS_BUFFER_SIZE;
}
if (!is_file && !strcmp(name, "-")) {
is_file = true;
}
if (is_envvar) {
is_envvar = !!getenv(envvar);
}
*out = calloc(*len + 1, 1);
if (*out == 0) {
fprintf(stderr, "Failed to allocate %zu bytes memory for %s\n", *len,
fname);
return false;
}
if (!strcmp(name, "-") || fname != name ||
(st_res == 0 && S_ISREG(sb.st_mode))) {
if (is_file) {
bool ret = false;
FILE *file = open_file(fname, true);
if (!file) {
Expand Down Expand Up @@ -1502,6 +1511,15 @@ static bool get_input_data(const char *name, uint8_t **out, size_t *len,
if (ret == false) {
return ret;
}
} else if (is_envvar) {
const char *data = getenv(envvar);
size_t dlen = strlen(data);
if (dlen < *len) {
memcpy(*out, data, dlen);
*len = dlen;
} else {
return false;
}
} else {
if (strlen(name) < *len) {
memcpy(*out, name, strlen(name));
Expand Down