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