| 81 | static bool g_generated_cookie = false; |
| 82 | |
| 83 | bool GenerateAuthCookie(std::string *cookie_out) |
| 84 | { |
| 85 | const size_t COOKIE_SIZE = 32; |
| 86 | unsigned char rand_pwd[COOKIE_SIZE]; |
| 87 | GetRandBytes(rand_pwd, COOKIE_SIZE); |
| 88 | std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd); |
| 89 | |
| 90 | /** the umask determines what permissions are used to create this file - |
| 91 | * these are set to 077 in init.cpp unless overridden with -sysperms. |
| 92 | */ |
| 93 | std::ofstream file; |
| 94 | fs::path filepath_tmp = GetAuthCookieFile(true); |
| 95 | file.open(filepath_tmp); |
| 96 | if (!file.is_open()) { |
| 97 | LogPrintf("Unable to open cookie authentication file %s for writing\n", fs::PathToString(filepath_tmp)); |
| 98 | return false; |
| 99 | } |
| 100 | file << cookie; |
| 101 | file.close(); |
| 102 | |
| 103 | fs::path filepath = GetAuthCookieFile(false); |
| 104 | if (!RenameOver(filepath_tmp, filepath)) { |
| 105 | LogPrintf("Unable to rename cookie authentication file %s to %s\n", fs::PathToString(filepath_tmp), fs::PathToString(filepath)); |
| 106 | return false; |
| 107 | } |
| 108 | g_generated_cookie = true; |
| 109 | LogPrintf("Generated RPC authentication cookie %s\n", fs::PathToString(filepath)); |
| 110 | |
| 111 | if (cookie_out) |
| 112 | *cookie_out = cookie; |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | bool GetAuthCookie(std::string *cookie_out) |
| 117 | { |
no test coverage detected