📄 xtunnelsauthentication.cpp
字号:
{ return 1; } const char* szSQLCommand = "SELECT * from getauthrules();"; /* if we would be going to get individual host rules char szSQLCommand[EMaxMediumBufferSize] = { 0 }; snprintf( szSQLCommand, EMaxMediumBufferSize, "SELECT * from getauthrules('%s');", szHost ); */ PGresult* pSQLResult = PQexec(g_pDBConnection, szSQLCommand); ExecStatusType tStatus = PGRES_FATAL_ERROR; int iResult = 1; if (pSQLResult) {#if DEBUG cout << "XTunnels: GetAllRulesFromDB (" << szSQLCommand << ") error message: " << PQresultErrorMessage(pSQLResult) << endl;#endif //DEBUG tStatus = PQresultStatus(pSQLResult); if (tStatus == PGRES_TUPLES_OK) { iResult = 0; int iRows = PQntuples(pSQLResult); int iCols = PQnfields(pSQLResult);#if DEBUG cout << "XTunnels: GetAllRulesFromDB got " << iRows << " rows of " << iCols << " columns " << endl;#endif // DEBUG if (iCols != 4) {#if DEBUG cout << "XTunnels: GetAllRulesFromDB did not get 4 columns -- aborting! " << endl;#endif // DEBUG return 1; } /* column contents 0 host name 1 rule type 2 from IP 3 to IP */ THostRule tNewRule; for (int iRuleIndex = 0; iRuleIndex < iRows; iRuleIndex++) { bzero(&tNewRule, sizeof(tNewRule)); char* szResult = PQgetvalue(pSQLResult, iRuleIndex, 0); long lStringLength = strlen(szResult) + 1; tNewRule.m_szHost = new char[lStringLength]; strncpy(tNewRule.m_szHost, szResult, lStringLength); szResult = PQgetvalue(pSQLResult, iRuleIndex, 1); tNewRule.m_iRuleType = atol(szResult); szResult = PQgetvalue(pSQLResult, iRuleIndex, 2); tNewRule.m_ulFromIP = inet_addr(szResult); szResult = PQgetvalue(pSQLResult, iRuleIndex, 3); tNewRule.m_ulToIP = inet_addr(szResult); switch (tNewRule.m_iRuleType) { case ERuleOnlyTransmitTo: g_cHostDestinationRules.push_back(tNewRule); break; /* case ERuleAcceptConnection: case ERuleAcceptAnonymousConnection: case ERuleDenyConnection: */ default: g_cHostSourceRules.push_back(tNewRule); break; }#if DEBUG const char* szRuleString = NULL; szRuleString = "???"; switch (tNewRule.m_iRuleType) { case ERuleAcceptConnection: szRuleString = "ERuleAcceptConnection"; break; case ERuleAcceptAnonymousConnection: szRuleString = "ERuleAcceptAnonymousConnection"; break; case ERuleDenyConnection: szRuleString = "ERuleDenyConnection"; break; case ERuleOnlyTransmitTo: szRuleString = "ERuleOnlyTransmitTo"; break; default: cout << "ERROR: GetAllRulesFromDB() found unknown rule type " << tNewRule.m_iRuleType << "!" << endl; break; } cout << "XTunnels: GetAllRulesFromDB rule " << iRuleIndex << ": type " << szRuleString << " (" << PQgetvalue(pSQLResult, iRuleIndex, 1) << "-" << szResult << ") " << endl;#endif // DEBUG } } else {#if DEBUG cout << "XTunnels: GetAllRulesFromDB (" << szSQLCommand << ") PQresultStatus tStatus was bad!" << endl;#endif //DEBUG } } else {#if DEBUG cout << "XTunnels: GetAllRulesFromDB (" << szSQLCommand << ") errored, returned NULL result!" << endl;#endif //DEBUG } PQclear(pSQLResult); return iResult; }int OpenDatabaseConnection() { char szDBConnectionString[EMaxMediumBufferSize] = { 0 }; sprintf(szDBConnectionString, "%s'%s' %s'%s' %s'%s' %s'%s' %s'%s'", g_szConfigFileLineDBHost, g_szDBHost, g_szConfigFileLineDBPort, g_szDBPort, g_szConfigFileLineDBUsername, g_szDBUsername, g_szConfigFileLineDBPassword, g_szDBPassword, g_szConfigFileLineDBName, g_szDBName); /* "Changing the password to an invalid one probably has no effect because postgres isn't looking at it anyway due to a trust relationship with localhost. If you want it to error out you'll have to give it an invalid username instead." */ #if DEBUG cout << "XTunnels: opening db with '" << szDBConnectionString << "' connection string" << endl;#endif //DEBUG int iResult = 1; ConnStatusType tStatus = CONNECTION_BAD; int iRetries = 5; while (iRetries && iResult) { g_pDBConnection = PQconnectdb(szDBConnectionString); iResult = (NULL != g_pDBConnection) ? 0 : 1; tStatus = PQstatus(g_pDBConnection); // can handle NULL if (!iResult && (tStatus != CONNECTION_OK)) { iResult = 1; iRetries--; if (iRetries) sleep(2 + (rand() % 4)); } } #if DEBUG cout << "XTunnels: opening db result " << iResult << ": status " << tStatus << " connection " << (g_pDBConnection ? "not null" : "null") << " PQErrorMessage: " << PQerrorMessage(g_pDBConnection) << endl;#endif //DEBUG return iResult; }int CloseDatabaseConnection() { PQfinish(g_pDBConnection); // can handle NULL g_pDBConnection = NULL; return 0; }int LoadConfigurationFile(const char* inAppName) {#if VS_TARGET_OS_MAC #pragma unused (inAppName)#endif // VS_TARGET_OS_MAC int error = 0; int configfile = open(g_szConfigFileName, O_RDONLY, 0); if (configfile < 1) {#if DEBUG cout << "WARNING: "<< inAppName << ": could not open '" << g_szConfigFileName << "' configuration file (" << configfile << ") " << endl;#endif //DEBUG error = 1; return error; } char configline[EMaxSmallBufferSize] = { 0 }; while (0 < readline(configfile, configline, EMaxSmallBufferSize)) { // remove line break configline[strlen(configline) - 1] = 0;/*#if DEBUG cout << "xtunnels.cfg: " << configline << endl; #endif //DEBUG*/ if ((configline[0] == '#') || !configline[0]) // comment or blank continue; // server configuration lines if (!strncmp(configline, g_szConfigFileLineListenPort, strlen(g_szConfigFileLineListenPort))) // listen port number { u_port_t listenPort; listenPort = strtol(&configline[strlen(g_szConfigFileLineListenPort)], NULL, 10); SetListenPort(listenPort);#if DEBUG cout << "xtunnels.cfg found listen port " << listenPort << endl;#endif //DEBUG } if (!strncmp(configline, g_szConfigFileLineMaxClients, strlen(g_szConfigFileLineMaxClients))) // maximum clients allowed { int maxClients; maxClients = strtol(&configline[strlen(g_szConfigFileLineMaxClients)], NULL, 10); SetMaximumClients(maxClients);#if DEBUG cout << "xtunnels.cfg found maximum client restriction " << maxClients << endl;#endif //DEBUG } else if (!strncmp(configline, g_szConfigFileLineXCipherEnabled, strlen(g_szConfigFileLineXCipherEnabled))) // X-Cipher enabled? { XCipher::XCipherEnabled() = configline[strlen(g_szConfigFileLineXCipherEnabled)] == 't';#if DEBUG cout << "xtunnels.cfg set X-Cipher enabled status to " << (XCipher::XCipherEnabled() ? "true" : "false") << endl;#endif //DEBUG } else if (!strncmp(configline, g_szConfigFileLineXCipherHost, strlen(g_szConfigFileLineXCipherHost))) // X-Cipher registered host { strncpy(XCipher::g_szLocalXCipherHost, &configline[strlen(g_szConfigFileLineXCipherHost)], EMaxSmallBufferSize);#if DEBUG cout << "xtunnels.cfg loaded X-Cipher hostname " << XCipher::g_szLocalXCipherHost << endl;#endif //DEBUG } else if (!strncmp(configline, g_szConfigFileLineXCipherGlobal, strlen(g_szConfigFileLineXCipherGlobal))) // X-Cipher global server { strncpy(XCipher::g_szXCipherGlobalServerSRVRequest, &configline[strlen(g_szConfigFileLineXCipherGlobal)], EMaxSmallBufferSize);#if DEBUG cout << "xtunnels.cfg loaded X-Cipher global server SRV request " << XCipher::g_szXCipherGlobalServerSRVRequest << endl;#endif //DEBUG } else if (!strncmp(configline, g_szConfigFileLineXCipherPassword, strlen(g_szConfigFileLineXCipherPassword))) // X-Cipher registered password { strncpy(XCipher::g_szLocalXCipherPassword, &configline[strlen(g_szConfigFileLineXCipherPassword)], EMaxSmallBufferSize);#if DEBUG cout << "xtunnels.cfg loaded X-Cipher password " << XCipher::g_szLocalXCipherPassword << endl;#endif //DEBUG } // database configuration lines else if (!strncmp(configline, g_szConfigFileLineDBHost, strlen(g_szConfigFileLineDBHost))) // database host { strncpy(g_szDBHost, &configline[strlen(g_szConfigFileLineDBHost)], EMaxSmallBufferSize);#if DEBUG cout << "xtunnels.cfg set database server to " << g_szDBHost << endl;#endif //DEBUG } else if (!strncmp(configline, g_szConfigFileLineDBPort, strlen(g_szConfigFileLineDBPort))) // database port { strncpy(g_szDBPort, &configline[strlen(g_szConfigFileLineDBPort)], EMaxSmallBufferSize);#if DEBUG cout << "xtunnels.cfg set database port to " << g_szDBPort << endl;#endif //DEBUG } else if (!strncmp(configline, g_szConfigFileLineDBUsername, strlen(g_szConfigFileLineDBUsername))) // database user { strncpy(g_szDBUsername, &configline[strlen(g_szConfigFileLineDBUsername)], EMaxSmallBufferSize);#if DEBUG cout << "xtunnels.cfg set database user to " << g_szDBUsername << endl;#endif //DEBUG } else if (!strncmp(configline, g_szConfigFileLineDBPassword, strlen(g_szConfigFileLineDBPassword))) // database password { strncpy(g_szDBPassword, &configline[strlen(g_szConfigFileLineDBPassword)], EMaxSmallBufferSize);#if DEBUG cout << "xtunnels.cfg set database password to " << g_szDBPassword << endl;#endif //DEBUG } else if (!strncmp(configline, g_szConfigFileLineDBName, strlen(g_szConfigFileLineDBName))) // database name { strncpy(g_szDBName, &configline[strlen(g_szConfigFileLineDBName)], EMaxSmallBufferSize);#if DEBUG cout << "xtunnels.cfg set database name to " << g_szDBName << endl;#endif //DEBUG } // done checking this line configline[0] = 0; } if (r_close(configfile)) {#if DEBUG cout << "WARNING:: Error closing xtunnels.cfg" << endl;#endif //DEBUG } return error; }} // end XTunnels namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -