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