Commit | Line | Data |
---|---|---|
5039dede AK |
1 | // nxconfig.cpp : Defines the class behaviors for the application. |
2 | // | |
3 | ||
4 | #include "stdafx.h" | |
5 | #include "nxconfig.h" | |
6 | ||
7 | #include "MainFrm.h" | |
8 | #include "ConfigWizard.h" | |
9 | #include "IntroPage.h" | |
10 | #include "DBSelectPage.h" | |
11 | #include "ODBCPage.h" | |
12 | #include "PollCfgPage.h" | |
13 | #include "SMTPPage.h" | |
14 | #include "SummaryPage.h" | |
15 | #include "ProcessingPage.h" | |
16 | #include "ConfigFilePage.h" | |
17 | #include "WinSrvPage.h" | |
18 | #include "SrvDepsPage.h" | |
19 | #include "LoggingPage.h" | |
20 | #include "FinishPage.h" | |
21 | ||
bb313f32 VK |
22 | #include <winsock2.h> |
23 | #include <afxsock.h> // MFC socket extensions | |
5039dede AK |
24 | #include <iphlpapi.h> |
25 | #include <iprtrmib.h> | |
26 | #include <rtinfo.h> | |
27 | ||
28 | #ifdef _DEBUG | |
29 | #define new DEBUG_NEW | |
30 | #undef THIS_FILE | |
31 | static char THIS_FILE[] = __FILE__; | |
32 | #endif | |
33 | ||
34 | ///////////////////////////////////////////////////////////////////////////// | |
35 | // CNxconfigApp | |
36 | ||
37 | BEGIN_MESSAGE_MAP(CNxconfigApp, CWinApp) | |
38 | //{{AFX_MSG_MAP(CNxconfigApp) | |
39 | ON_COMMAND(ID_FILE_CFG_WIZARD, OnFileCfgWizard) | |
40 | //}}AFX_MSG_MAP | |
41 | END_MESSAGE_MAP() | |
42 | ||
43 | ///////////////////////////////////////////////////////////////////////////// | |
44 | // CNxconfigApp construction | |
45 | ||
46 | CNxconfigApp::CNxconfigApp() | |
47 | { | |
48 | // TODO: add construction code here, | |
49 | // Place all significant initialization in InitInstance | |
50 | } | |
51 | ||
52 | ///////////////////////////////////////////////////////////////////////////// | |
53 | // The one and only CNxconfigApp object | |
54 | ||
55 | CNxconfigApp appNxConfig; | |
56 | ||
57 | ///////////////////////////////////////////////////////////////////////////// | |
58 | // CNxconfigApp initialization | |
59 | ||
60 | BOOL CNxconfigApp::InitInstance() | |
61 | { | |
62 | HKEY hKey; | |
63 | DWORD dwSize, dwData = 0; | |
64 | TCHAR *pszArg, szCmd[1024]; | |
65 | ||
66 | if (!AfxSocketInit()) | |
67 | { | |
68 | AfxMessageBox(IDP_SOCKETS_INIT_FAILED); | |
69 | return FALSE; | |
70 | } | |
71 | ||
72 | // Read installation data from registry | |
73 | if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\NetXMS\\Server"), 0, | |
74 | KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) | |
75 | { | |
76 | dwSize = sizeof(DWORD); | |
77 | RegQueryValueEx(hKey, _T("ServerIsConfigured"), NULL, NULL, (BYTE *)&dwData, &dwSize); | |
78 | ||
79 | dwSize = (MAX_PATH - 16) * sizeof(TCHAR); | |
80 | if (RegQueryValueEx(hKey, _T("InstallPath"), NULL, NULL, | |
81 | (BYTE *)m_szInstallDir, &dwSize) != ERROR_SUCCESS) | |
82 | { | |
83 | AfxMessageBox(_T("Unable to determine NetXMS installation directory")); | |
84 | m_szInstallDir[0] = 0; | |
85 | } | |
86 | ||
87 | RegCloseKey(hKey); | |
88 | } | |
89 | else | |
90 | { | |
91 | AfxMessageBox(_T("Unable to determine NetXMS installation directory")); | |
92 | m_szInstallDir[0] = 0; | |
93 | } | |
94 | ||
95 | // Parse command line | |
96 | if (!_tcsicmp(m_lpCmdLine, _T("--create-agent-config"))) | |
97 | { | |
98 | CreateAgentConfig(); | |
99 | return FALSE; | |
100 | } | |
101 | pszArg = ExtractWord(m_lpCmdLine, szCmd); | |
102 | if (!_tcsicmp(szCmd, _T("--create-nxhttpd-config"))) | |
103 | { | |
104 | CreateWebConfig(pszArg); | |
105 | return FALSE; | |
106 | } | |
107 | ||
108 | // Check if server is already configured or we cannot determine | |
109 | // installation directory | |
110 | if ((dwData != 0) || (m_szInstallDir[0] == 0)) | |
111 | { | |
112 | if ((dwData != 0) && (_tcsicmp(m_lpCmdLine, _T("--configure-if-needed")))) | |
113 | AfxMessageBox(_T("Server already configured")); | |
114 | return FALSE; | |
115 | } | |
116 | ||
117 | AfxEnableControlContainer(); | |
118 | ||
119 | // Standard initialization | |
120 | // If you are not using these features and wish to reduce the size | |
121 | // of your final executable, you should remove from the following | |
122 | // the specific initialization routines you do not need. | |
123 | ||
5039dede AK |
124 | // Change the registry key under which our settings are stored. |
125 | SetRegistryKey(_T("NetXMS")); | |
126 | ||
127 | // To create the main window, this code creates a new frame window | |
128 | // object and then sets it as the application's main window object. | |
129 | CMainFrame* pFrame = new CMainFrame; | |
130 | m_pMainWnd = pFrame; | |
131 | ||
132 | // create and load the frame with its resources | |
133 | pFrame->LoadFrame(IDR_MAINFRAME, | |
134 | WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, | |
135 | NULL); | |
136 | ||
137 | // The one and only window has been initialized, so show and update it. | |
138 | //pFrame->ShowWindow(SW_SHOW); | |
139 | //pFrame->UpdateWindow(); | |
140 | ||
141 | pFrame->PostMessage(WM_COMMAND, ID_FILE_CFG_WIZARD); | |
142 | ||
143 | return TRUE; | |
144 | } | |
145 | ||
146 | ///////////////////////////////////////////////////////////////////////////// | |
147 | // CNxconfigApp message handlers | |
148 | ||
149 | void CNxconfigApp::OnFileCfgWizard() | |
150 | { | |
151 | CConfigWizard dlg(_T("Configure NetXMS Server"), m_pMainWnd, 0); | |
152 | CIntroPage pgIntro; | |
153 | CConfigFilePage pgConfigFile; | |
154 | CDBSelectPage pgSelectDB; | |
155 | CODBCPage pgCheckODBC; | |
156 | CPollCfgPage pgPollConfig; | |
157 | CSMTPPage pgSMTP; | |
158 | CLoggingPage pgLogging; | |
159 | CWinSrvPage pgWinSrv; | |
160 | CSrvDepsPage pgSrvDeps; | |
161 | CSummaryPage pgSummary; | |
162 | CProcessingPage pgProcessing; | |
163 | CFinishPage pgFinish; | |
164 | ||
165 | dlg.m_psh.dwFlags |= PSH_WIZARD; | |
166 | dlg.AddPage(&pgIntro); | |
167 | dlg.AddPage(&pgConfigFile); | |
168 | dlg.AddPage(&pgSelectDB); | |
169 | dlg.AddPage(&pgCheckODBC); | |
170 | dlg.AddPage(&pgPollConfig); | |
171 | dlg.AddPage(&pgSMTP); | |
172 | dlg.AddPage(&pgLogging); | |
173 | dlg.AddPage(&pgWinSrv); | |
174 | dlg.AddPage(&pgSrvDeps); | |
175 | dlg.AddPage(&pgSummary); | |
176 | dlg.AddPage(&pgProcessing); | |
177 | dlg.AddPage(&pgFinish); | |
178 | ||
179 | if (dlg.DoModal() == ID_WIZFINISH) | |
180 | { | |
181 | HKEY hKey; | |
182 | DWORD dwData = 1; | |
183 | ||
184 | if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\NetXMS\\Server"), 0, | |
185 | KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) | |
186 | { | |
187 | RegSetValueEx(hKey, _T("ServerIsConfigured"), 0, REG_DWORD, (BYTE *)&dwData, sizeof(DWORD)); | |
188 | RegCloseKey(hKey); | |
189 | } | |
190 | } | |
191 | ||
192 | m_pMainWnd->PostMessage(WM_COMMAND, ID_APP_EXIT); | |
193 | } | |
194 | ||
195 | ||
196 | // | |
197 | // Create configuration file for local agent | |
198 | // | |
199 | ||
200 | void CNxconfigApp::CreateAgentConfig() | |
201 | { | |
202 | FILE *fp; | |
203 | time_t currTime; | |
204 | DWORD dwSize; | |
205 | IP_ADAPTER_INFO *pBuffer, *pInfo; | |
206 | IP_ADDR_STRING *pAddr; | |
207 | TCHAR szAddrList[4096], szFile[MAX_PATH]; | |
208 | ||
209 | _sntprintf(szFile, MAX_PATH, _T("%s\\etc\\nxagentd.conf"), m_szInstallDir); | |
210 | if (_taccess(szFile, 0) == 0) | |
211 | return; // File already exist, we shouldn't overwrite it | |
212 | ||
213 | // Get local interface list | |
214 | szAddrList[0] = 0; | |
215 | if (GetAdaptersInfo(NULL, &dwSize) == ERROR_BUFFER_OVERFLOW) | |
216 | { | |
217 | pBuffer = (IP_ADAPTER_INFO *)malloc(dwSize); | |
218 | if (GetAdaptersInfo(pBuffer, &dwSize) == ERROR_SUCCESS) | |
219 | { | |
220 | for(pInfo = pBuffer; pInfo != NULL; pInfo = pInfo->Next) | |
221 | { | |
222 | // Read all IP addresses for adapter | |
223 | for(pAddr = &pInfo->IpAddressList; pAddr != NULL; pAddr = pAddr->Next) | |
224 | { | |
225 | if (_tcscmp(pAddr->IpAddress.String, _T("0.0.0.0"))) | |
226 | { | |
227 | if (szAddrList[0] != 0) | |
228 | _tcscat(szAddrList, _T(", ")); | |
229 | _tcscat(szAddrList, pAddr->IpAddress.String); | |
230 | } | |
231 | } | |
232 | } | |
233 | } | |
234 | free(pBuffer); | |
235 | } | |
236 | ||
237 | fp = _tfopen(szFile, _T("w")); | |
238 | if (fp != NULL) | |
239 | { | |
240 | currTime = time(NULL); | |
241 | _ftprintf(fp, _T("#\n# NetXMS agent configuration file\n# Created by server installer at %s#\n\n"), | |
242 | _tctime(&currTime)); | |
243 | _ftprintf(fp, _T("LogFile = {syslog}\nServers = 127.0.0.1\n")); | |
244 | if (szAddrList[0] != 0) | |
245 | _ftprintf(fp, _T("InstallationServers = %s\n"), szAddrList); | |
246 | _ftprintf(fp, _T("FileStore = %s\\var\n"), m_szInstallDir); | |
247 | _ftprintf(fp, _T("RequireAuthentication = no\n")); | |
248 | _ftprintf(fp, _T("SubAgent = winperf.nsm\nSubAgent = portcheck.nsm\n")); | |
249 | fclose(fp); | |
250 | } | |
251 | } | |
252 | ||
253 | ||
254 | // | |
255 | // Create configuration file for nxhttpd | |
256 | // | |
257 | ||
258 | void CNxconfigApp::CreateWebConfig(TCHAR *pszServer) | |
259 | { | |
260 | FILE *fp; | |
261 | time_t currTime; | |
262 | TCHAR szFile[MAX_PATH]; | |
263 | ||
264 | _sntprintf(szFile, MAX_PATH, _T("%s\\etc\\nxhttpd.conf"), m_szInstallDir); | |
265 | if (_taccess(szFile, 0) == 0) | |
266 | return; // File already exist, we shouldn't overwrite it | |
267 | ||
268 | fp = _tfopen(szFile, _T("w")); | |
269 | if (fp != NULL) | |
270 | { | |
271 | currTime = time(NULL); | |
272 | _ftprintf(fp, _T("#\n# NetXMS web server configuration file\n# Created by server installer at %s#\n\n"), | |
273 | _tctime(&currTime)); | |
274 | _ftprintf(fp, _T("LogFile = {syslog}\nMasterServer = %s\n"), pszServer); | |
275 | _ftprintf(fp, _T("DocumentRoot = %s\\var\\www\n"), m_szInstallDir); | |
276 | fclose(fp); | |
277 | } | |
278 | } |