diff --git a/src/rpc/protocol.h b/src/rpc/protocol.h --- a/src/rpc/protocol.h +++ b/src/rpc/protocol.h @@ -130,8 +130,6 @@ const UniValue &id); UniValue JSONRPCError(int code, const std::string &message); -/** Get name of RPC authentication cookie file */ -fs::path GetAuthCookieFile(); /** Generate a new RPC authentication cookie and write it to disk */ bool GenerateAuthCookie(std::string *cookie_out); /** Read the RPC authentication cookie from disk */ diff --git a/src/rpc/protocol.cpp b/src/rpc/protocol.cpp --- a/src/rpc/protocol.cpp +++ b/src/rpc/protocol.cpp @@ -66,8 +66,13 @@ /** Default name for auth cookie file */ static const std::string COOKIEAUTH_FILE = ".cookie"; -fs::path GetAuthCookieFile() { - fs::path path(gArgs.GetArg("-rpccookiefile", COOKIEAUTH_FILE)); +/** Get name of RPC authentication cookie file */ +static fs::path GetAuthCookieFile(bool temp = false) { + std::string arg = gArgs.GetArg("-rpccookiefile", COOKIEAUTH_FILE); + if (temp) { + arg += ".tmp"; + } + fs::path path(arg); if (!path.is_complete()) { path = GetDataDir() / path; } @@ -85,15 +90,22 @@ * these are set to 077 in init.cpp unless overridden with -sysperms. */ std::ofstream file; - fs::path filepath = GetAuthCookieFile(); - file.open(filepath.string().c_str()); + fs::path filepath_tmp = GetAuthCookieFile(true); + file.open(filepath_tmp.string().c_str()); if (!file.is_open()) { LogPrintf("Unable to open cookie authentication file %s for writing\n", - filepath.string()); + filepath_tmp.string()); return false; } file << cookie; file.close(); + + fs::path filepath = GetAuthCookieFile(false); + if (!RenameOver(filepath_tmp, filepath)) { + LogPrintf("Unable to rename cookie authentication file %s to %s\n", + filepath_tmp.string(), filepath.string()); + return false; + } LogPrintf("Generated RPC authentication cookie %s\n", filepath.string()); if (cookie_out) {