From bf1715973bb65b42bb4edad54d13fd2eb5efa6cf Mon Sep 17 00:00:00 2001 From: prozacx Date: Mon, 10 Oct 2005 04:33:30 +0000 Subject: [PATCH] Added prompt for pem file generation git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@557 726aef4b-f618-498e-8847-2d620e286838 --- Utils.cpp | 14 +++++++++++--- Utils.h | 2 +- main.cpp | 23 +---------------------- znc.cpp | 50 ++++++++++++++++++++++++++++++++------------------ znc.h | 1 + 5 files changed, 46 insertions(+), 44 deletions(-) diff --git a/Utils.cpp b/Utils.cpp index d753618a..dfd6a568 100644 --- a/Utils.cpp +++ b/Utils.cpp @@ -41,7 +41,7 @@ char *strcasestr(const char *big, const char *little) { #endif /* __sun */ #ifdef HAVE_LIBSSL -void CUtils::GenerateCert(FILE *pOut, bool bEncPrivKey) { +void CUtils::GenerateCert(FILE *pOut, bool bEncPrivKey, const CString& sHost) { EVP_PKEY *pKey = NULL; X509 *pCert = NULL; X509_NAME *pName = NULL; @@ -72,8 +72,16 @@ void CUtils::GenerateCert(FILE *pOut, bool bEncPrivKey) { pName = X509_get_subject_name( pCert ); - char *pLogName = getenv("LOGNAME"); - char *pHostName = getenv("HOSTNAME"); + const char *pLogName = getenv("LOGNAME"); + const char *pHostName = NULL; + + if (!sHost.empty()) { + pHostName = sHost.c_str(); + } + + if (!pHostName) { + pHostName = getenv("HOSTNAME"); + } if (!pLogName) { pLogName = "Unknown"; diff --git a/Utils.h b/Utils.h index 431b1bf0..8b85c9b1 100644 --- a/Utils.h +++ b/Utils.h @@ -51,7 +51,7 @@ public: return iTime; } #ifdef HAVE_LIBSSL - static void GenerateCert(FILE *pOut, bool bEncPrivKey = false); + static void GenerateCert(FILE *pOut, bool bEncPrivKey = false, const CString& sHost = ""); #endif /* HAVE_LIBSSL */ private: diff --git a/main.cpp b/main.cpp index 860feb4e..e443243a 100644 --- a/main.cpp +++ b/main.cpp @@ -138,28 +138,7 @@ int main(int argc, char** argv, char** envp) { if (bMakePem) { CZNC* pZNC = &CZNC::Get(); pZNC->InitDirs(""); - CString sPemFile = pZNC->GetPemLocation(); - - CUtils::PrintAction("Writing Pem file [" + sPemFile + "]"); - - if (CFile::Exists(sPemFile)) { - CUtils::PrintStatus(false, "File already exists"); - delete pZNC; - return 1; - } - - FILE *f = fopen(sPemFile.c_str(), "w"); - - if (!f) { - CUtils::PrintStatus(false, "Unable to open"); - delete pZNC; - return 1 ; - } - - CUtils::GenerateCert(f, bEncPem); - fclose(f); - - CUtils::PrintStatus(true); + pZNC->WritePemFile(); delete pZNC; return 0; diff --git a/znc.cpp b/znc.cpp index de549639..3d22d089 100644 --- a/znc.cpp +++ b/znc.cpp @@ -198,6 +198,37 @@ bool CZNC::WritePidFile(int iPid) { return false; } +bool CZNC::WritePemFile() { + CString sPemFile = GetPemLocation(); + const char* pHostName = getenv("HOSTNAME"); + CString sHost; + + if (pHostName) { + sHost = pHostName; + } + + if (CFile::Exists(sPemFile)) { + CUtils::PrintError("Pem file [" + sPemFile + "] already exists"); + return false; + } + + while (!CUtils::GetInput("hostname of your shell", sHost, sHost, "including the '.com' portion")); + + CUtils::PrintAction("Writing Pem file [" + sPemFile + "]"); + FILE *f = fopen(sPemFile.c_str(), "w"); + + if (!f) { + CUtils::PrintStatus(false, "Unable to open"); + return false; + } + + CUtils::GenerateCert(f, false, sHost); + fclose(f); + + CUtils::PrintStatus(true); + return true; +} + void CZNC::DeleteUsers() { for (map::iterator a = m_msUsers.begin(); a != m_msUsers.end(); a++) { delete a->second; @@ -924,24 +955,7 @@ bool CZNC::ParseConfig(const CString& sConfig) { CUtils::PrintStatus(false, "Unable to locate pem file: [" + sPemFile + "]"); if (CUtils::GetBoolInput("Would you like to create a new pem file?", true)) { - CUtils::PrintAction("Writing Pem file [" + sPemFile + "]"); - - if (CFile::Exists(sPemFile)) { - CUtils::PrintStatus(false, "File already exists"); - return false; - } - - FILE *f = fopen(sPemFile.c_str(), "w"); - - if (!f) { - CUtils::PrintStatus(false, "Unable to open"); - return false; - } - - CUtils::GenerateCert(f, false); - fclose(f); - - CUtils::PrintStatus(true); + WritePemFile(); } else { return false; } diff --git a/znc.h b/znc.h index 8ec71b61..6d7113e8 100644 --- a/znc.h +++ b/znc.h @@ -61,6 +61,7 @@ public: const CString& GetConfBackupPath() const { if (!CFile::Exists(m_sConfBackupPath)) { CUtils::MakeDir(m_sConfBackupPath); } return m_sConfBackupPath; } const CString& GetUserPath() const { if (!CFile::Exists(m_sUserPath)) { CUtils::MakeDir(m_sUserPath); } return m_sUserPath; } CString GetPemLocation() const { return GetZNCPath() + "/znc.pem"; } + bool WritePemFile(); const CString& GetISpoofFile() const { return m_sISpoofFile; } const CString& GetISpoofFormat() const { return m_sISpoofFormat; } const VCString& GetVHosts() const { return m_vsVHosts; }