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