|
| 1 | +#include "globals.h" |
| 2 | +#include <string> |
| 3 | +#include <unistd.h> |
| 4 | +#include <cstring> |
| 5 | +#include <cstdio> |
| 6 | + |
| 7 | +static |
| 8 | +void rtrim(char *line) { |
| 9 | + if (line == nullptr) |
| 10 | + return; |
| 11 | + size_t line_length = strlen(line); |
| 12 | + char *lhelper = line+line_length-1; |
| 13 | + if (line_length == 0) |
| 14 | + return; |
| 15 | + |
| 16 | + while (1) { |
| 17 | + if (lhelper < line) |
| 18 | + break; |
| 19 | + |
| 20 | + // space allowed |
| 21 | + if (isspace(*lhelper)) { |
| 22 | + *lhelper = 0; |
| 23 | + lhelper--; |
| 24 | + continue; |
| 25 | + } else { |
| 26 | + break; |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +static |
| 32 | +char* ltrim(char *line) { |
| 33 | + if (line == nullptr) |
| 34 | + return line; |
| 35 | + size_t line_length = strlen(line); |
| 36 | + char *line_new = line; |
| 37 | + for (size_t i=0, e=line_length; i<e; i++) { |
| 38 | + if (!isspace(line[i])) { |
| 39 | + line_new = line+i; |
| 40 | + break; |
| 41 | + } |
| 42 | + } |
| 43 | + return line_new; |
| 44 | +} |
| 45 | + |
| 46 | +static |
| 47 | +bool is_valid_key(char *key) { |
| 48 | + if (!key) |
| 49 | + return false; |
| 50 | + |
| 51 | + size_t key_len = strlen(key); |
| 52 | + if (key_len == 0) |
| 53 | + return false; |
| 54 | + |
| 55 | + |
| 56 | + for (size_t i=0; i<key_len; i++) { |
| 57 | + if (isalnum(key[i]) || key[i] == '_') { |
| 58 | + continue; |
| 59 | + } else { |
| 60 | + return false; |
| 61 | + } |
| 62 | + } |
| 63 | + return true; |
| 64 | +} |
| 65 | + |
| 66 | +static |
| 67 | +void load_environment_line(char *line, size_t length) { |
| 68 | + rtrim(line); |
| 69 | + char *line_start = ltrim(line); |
| 70 | + size_t line_length = strlen(line_start); |
| 71 | + if (line_length < 3) |
| 72 | + return; |
| 73 | + |
| 74 | + if (line_start[0] == '#') { |
| 75 | + return; |
| 76 | + } |
| 77 | + char *eq_pos = strchr(line_start, '='); |
| 78 | + if (eq_pos == nullptr) |
| 79 | + return; |
| 80 | + if (line_length-(eq_pos-line_start) <= 1) |
| 81 | + return; |
| 82 | + |
| 83 | + *eq_pos = 0; |
| 84 | + char *key = line_start; |
| 85 | + if (!is_valid_key(key)) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + char *val = eq_pos+1; |
| 90 | + rtrim(key); |
| 91 | + size_t val_len = strlen(val); |
| 92 | + if (val_len >= 2) { |
| 93 | + if ((val[0] == '"' || val[0] == '\'') && val[0] == val[val_len-1]) { |
| 94 | + val[val_len-1] = 0; |
| 95 | + val++; |
| 96 | + val_len -= 2; |
| 97 | + } |
| 98 | + } |
| 99 | + if (val_len == 0) { |
| 100 | + unsetenv(key); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + setenv(key, val, 1); |
| 105 | +} |
| 106 | + |
| 107 | +void load_environment_file() |
| 108 | +{ |
| 109 | + std::string env_file = ENV_FILE_PRELOAD_DEFAULT; |
| 110 | + char *env_file_from_env = secure_getenv(ENV_FILE_PRELOAD_ENV_KEY); |
| 111 | + FILE *f = nullptr; |
| 112 | + char linebuf[4096]{}; |
| 113 | + |
| 114 | + if (env_file_from_env != nullptr) { |
| 115 | + env_file.assign(env_file_from_env); |
| 116 | + } |
| 117 | + if (access(env_file.c_str(), R_OK) != 0) |
| 118 | + return; |
| 119 | + f = fopen(env_file.c_str(), "r"); |
| 120 | + if (f == nullptr) { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + while (fgets(linebuf, sizeof(linebuf), f)) { |
| 125 | + load_environment_line(linebuf, sizeof(linebuf)); |
| 126 | + } |
| 127 | + fclose(f); |
| 128 | +} |
0 commit comments