nxhttpd traces removed
[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 TCHAR 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
102 // Check if server is already configured or we cannot determine
103 // installation directory
104 if ((dwData != 0) || (m_szInstallDir[0] == 0))
105 {
106 if ((dwData != 0) && (_tcsicmp(m_lpCmdLine, _T("--configure-if-needed"))))
107 AfxMessageBox(_T("Server already configured"));
108 return FALSE;
109 }
110
111 AfxEnableControlContainer();
112
113 // Standard initialization
114 // If you are not using these features and wish to reduce the size
115 // of your final executable, you should remove from the following
116 // the specific initialization routines you do not need.
117
118 // Change the registry key under which our settings are stored.
119 SetRegistryKey(_T("NetXMS"));
120
121 // To create the main window, this code creates a new frame window
122 // object and then sets it as the application's main window object.
123 CMainFrame* pFrame = new CMainFrame;
124 m_pMainWnd = pFrame;
125
126 // create and load the frame with its resources
127 pFrame->LoadFrame(IDR_MAINFRAME,
128 WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,
129 NULL);
130
131 // The one and only window has been initialized, so show and update it.
132 //pFrame->ShowWindow(SW_SHOW);
133 //pFrame->UpdateWindow();
134
135 pFrame->PostMessage(WM_COMMAND, ID_FILE_CFG_WIZARD);
136
137 return TRUE;
138 }
139
140 /////////////////////////////////////////////////////////////////////////////
141 // CNxconfigApp message handlers
142
143 void CNxconfigApp::OnFileCfgWizard()
144 {
145 CConfigWizard dlg(_T("Configure NetXMS Server"), m_pMainWnd, 0);
146 CIntroPage pgIntro;
147 CConfigFilePage pgConfigFile;
148 CDBSelectPage pgSelectDB;
149 CODBCPage pgCheckODBC;
150 CPollCfgPage pgPollConfig;
151 CSMTPPage pgSMTP;
152 CLoggingPage pgLogging;
153 CWinSrvPage pgWinSrv;
154 CSrvDepsPage pgSrvDeps;
155 CSummaryPage pgSummary;
156 CProcessingPage pgProcessing;
157 CFinishPage pgFinish;
158
159 dlg.m_psh.dwFlags |= PSH_WIZARD;
160 dlg.AddPage(&pgIntro);
161 dlg.AddPage(&pgConfigFile);
162 dlg.AddPage(&pgSelectDB);
163 dlg.AddPage(&pgCheckODBC);
164 dlg.AddPage(&pgPollConfig);
165 dlg.AddPage(&pgSMTP);
166 dlg.AddPage(&pgLogging);
167 dlg.AddPage(&pgWinSrv);
168 dlg.AddPage(&pgSrvDeps);
169 dlg.AddPage(&pgSummary);
170 dlg.AddPage(&pgProcessing);
171 dlg.AddPage(&pgFinish);
172
173 if (dlg.DoModal() == ID_WIZFINISH)
174 {
175 HKEY hKey;
176 DWORD dwData = 1;
177
178 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\NetXMS\\Server"), 0,
179 KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
180 {
181 RegSetValueEx(hKey, _T("ServerIsConfigured"), 0, REG_DWORD, (BYTE *)&dwData, sizeof(DWORD));
182 RegCloseKey(hKey);
183 }
184 }
185
186 m_pMainWnd->PostMessage(WM_COMMAND, ID_APP_EXIT);
187 }
188
189
190 //
191 // Create configuration file for local agent
192 //
193
194 void CNxconfigApp::CreateAgentConfig()
195 {
196 FILE *fp;
197 time_t currTime;
198 DWORD dwSize;
199 IP_ADAPTER_INFO *pBuffer, *pInfo;
200 IP_ADDR_STRING *pAddr;
201 TCHAR szAddrList[4096], szFile[MAX_PATH];
202
203 _sntprintf(szFile, MAX_PATH, _T("%s\\etc\\nxagentd.conf"), m_szInstallDir);
204 if (_taccess(szFile, 0) == 0)
205 return; // File already exist, we shouldn't overwrite it
206
207 // Get local interface list
208 szAddrList[0] = 0;
209 if (GetAdaptersInfo(NULL, &dwSize) == ERROR_BUFFER_OVERFLOW)
210 {
211 pBuffer = (IP_ADAPTER_INFO *)malloc(dwSize);
212 if (GetAdaptersInfo(pBuffer, &dwSize) == ERROR_SUCCESS)
213 {
214 for(pInfo = pBuffer; pInfo != NULL; pInfo = pInfo->Next)
215 {
216 // Read all IP addresses for adapter
217 for(pAddr = &pInfo->IpAddressList; pAddr != NULL; pAddr = pAddr->Next)
218 {
219 if (strcmp(pAddr->IpAddress.String, "0.0.0.0"))
220 {
221 if (szAddrList[0] != 0)
222 _tcscat(szAddrList, _T(", "));
223 #ifdef UNICODE
224 WCHAR ipaddr[32];
225 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, pAddr->IpAddress.String, -1, ipaddr, 32);
226 wcscat(szAddrList, ipaddr);
227 #else
228 strcat(szAddrList, pAddr->IpAddress.String);
229 #endif
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 }