00001 #ifndef Impala_Util_ChannelAuthorization_h 00002 #define Impala_Util_ChannelAuthorization_h 00003 00004 #include "Basis/FileName.h" 00005 #include "Basis/FileExists.h" 00006 #include "Basis/FileReadString.h" 00007 00008 namespace Impala 00009 { 00010 namespace Util 00011 { 00012 00013 00018 class ChannelAuthorization 00019 { 00020 00021 public: 00022 00023 ChannelAuthorization() 00024 { 00025 mValid = false; 00026 } 00027 00028 ChannelAuthorization(CString readPassword, CString writePassword) 00029 { 00030 mReadPassword = readPassword; 00031 mWritePassword = writePassword; 00032 mValid = true; 00033 } 00034 00041 ChannelAuthorization(CString fileName) 00042 { 00043 mValid = false; 00044 if (fileName.empty()) 00045 { 00046 ILOG_ERROR("Need passwordfile"); 00047 return; 00048 } 00049 String path; 00050 if (FileExists(fileName)) 00051 { 00052 path = fileName; 00053 } 00054 else 00055 { 00056 char* ptr = getenv("IMPALAROOT"); 00057 if (ptr) 00058 { 00059 String root = String(ptr) + "/" + fileName; 00060 if (FileExists(root)) 00061 path = root; 00062 } 00063 } 00064 if (path.empty()) 00065 { 00066 ILOG_ERROR("Unable to open passwordfile " << fileName); 00067 return; 00068 } 00069 std::vector<String> lines; 00070 FileReadString(std::back_inserter(lines), path); 00071 if (lines.size() != 2) 00072 { 00073 ILOG_ERROR("passwordfile should contain 2 lines"); 00074 return; 00075 } 00076 for (int i=0 ; i<lines.size() ; i++) 00077 { 00078 String s = lines[i]; 00079 if (StringStartsWith(s, "read=")) 00080 mReadPassword = StringReplace(s, "read=", ""); 00081 else if (StringStartsWith(s, "write=")) 00082 mWritePassword = StringReplace(s, "write=", ""); 00083 else 00084 { 00085 ILOG_ERROR("wrong format password file"); 00086 return; 00087 } 00088 } 00089 mValid = true; 00090 } 00091 00092 bool 00093 Valid() const 00094 { 00095 return mValid; 00096 } 00097 00098 CString 00099 GetReadPassword() const 00100 { 00101 return mReadPassword; 00102 } 00103 00104 CString 00105 GetWritePassword() const 00106 { 00107 return mWritePassword; 00108 } 00109 00110 bool 00111 AllowRead(CString readPassword) const 00112 { 00113 if (mReadPassword.empty()) 00114 return true; 00115 if (readPassword == mReadPassword) 00116 return true; 00117 return false; 00118 } 00119 00120 bool 00121 AllowWrite(CString writePassword) const 00122 { 00123 if (mWritePassword.empty()) 00124 return true; 00125 if (writePassword == mWritePassword) 00126 return true; 00127 return false; 00128 } 00129 00130 private: 00131 00132 bool mValid; 00133 String mReadPassword; 00134 String mWritePassword; 00135 00136 ILOG_VAR_DECL; 00137 00138 }; // class 00139 00140 ILOG_VAR_INIT(ChannelAuthorization, Impala.Util); 00141 00142 } // namespace 00143 } // namespace 00144 00145 #endif