Removed message "Server already configured" when upgrading already installed server
[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
62 if (!AfxSocketInit())
63 {
64 AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
65 return FALSE;
66 }
67
68 // Read installation data from registry
69 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\NetXMS\\Server"), 0,
70 KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
71 {
72 dwSize = sizeof(DWORD);
73 RegQueryValueEx(hKey, _T("ServerIsConfigured"), NULL, NULL, (BYTE *)&dwData, &dwSize);
74
75 dwSize = (MAX_PATH - 16) * sizeof(TCHAR);
76 if (RegQueryValueEx(hKey, _T("InstallPath"), NULL, NULL,
77 (BYTE *)m_szInstallDir, &dwSize) != ERROR_SUCCESS)
78 {
79 AfxMessageBox(_T("Unable to determine NetXMS installation directory"));
80 m_szInstallDir[0] = 0;
81 }
82
83 RegCloseKey(hKey);
84 }
85
86 // Parse command line
87 if (!_tcsicmp(m_lpCmdLine, _T("--create-agent-config")))
88 {
89 CreateAgentConfig();
90 return FALSE;
91 }
92
93 // Check if server is already configured or we cannot determine
94 // installation directory
95 if ((dwData != 0) || (m_szInstallDir[0] == 0))
96 {
97 if ((dwData != 0) && (_tcsicmp(m_lpCmdLine, _T("--configure-if-needed"))))
98 AfxMessageBox(_T("Server already configured"));
99 return FALSE;
100 }
101
102 AfxEnableControlContainer();
103
104 // Standard initialization
105 // If you are not using these features and wish to reduce the size
106 // of your final executable, you should remove from the following
107 // the specific initialization routines you do not need.
108
109 #ifdef _AFXDLL
110 Enable3dControls(); // Call this when using MFC in a shared DLL
111 #else
112 Enable3dControlsStatic(); // Call this when linking to MFC statically
113 #endif
114
115 // Change the registry key under which our settings are stored.
116 // TODO: You should modify this string to be something appropriate
117 // such as the name of your company or organization.
118 SetRegistryKey(_T("Local AppWizard-Generated Applications"));
119
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
124 CMainFrame* pFrame = new CMainFrame;
125 m_pMainWnd = pFrame;
126
127 // create and load the frame with its resources
128
129 pFrame->LoadFrame(IDR_MAINFRAME,
130 WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,
131 NULL);
132
133
134
135
136 // The one and only window has been initialized, so show and update it.
137 //pFrame->ShowWindow(SW_SHOW);
138 //pFrame->UpdateWindow();
139
140 pFrame->PostMessage(WM_COMMAND, ID_FILE_CFG_WIZARD);
141
142 return TRUE;
143 }
144
145 /////////////////////////////////////////////////////////////////////////////
146 // CNxconfigApp message handlers
147
148 void CNxconfigApp::OnFileCfgWizard()
149 {
150 CConfigWizard dlg(_T("Configure NetXMS Server"), m_pMainWnd, 0);
151 CIntroPage pgIntro;
152 CConfigFilePage pgConfigFile;
153 CDBSelectPage pgSelectDB;
154 CODBCPage pgCheckODBC;
155 CPollCfgPage pgPollConfig;
156 CSMTPPage pgSMTP;
157 CLoggingPage pgLogging;
158 CWinSrvPage pgWinSrv;
159 CSummaryPage pgSummary;
160 CProcessingPage pgProcessing;
161 CFinishPage pgFinish;
162
163 dlg.m_psh.dwFlags |= PSH_WIZARD;
164 dlg.AddPage(&pgIntro);
165 dlg.AddPage(&pgConfigFile);
166 dlg.AddPage(&pgSelectDB);
167 dlg.AddPage(&pgCheckODBC);
168 dlg.AddPage(&pgPollConfig);
169 dlg.AddPage(&pgSMTP);
170 dlg.AddPage(&pgLogging);
171 dlg.AddPage(&pgWinSrv);
172 dlg.AddPage(&pgSummary);
173 dlg.AddPage(&pgProcessing);
174 dlg.AddPage(&pgFinish);
175
176 if (dlg.DoModal() == ID_WIZFINISH)
177 {
178 HKEY hKey;
179 DWORD dwData = 1;
180
181 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\NetXMS\\Server"), 0,
182 KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
183 {
184 RegSetValueEx(hKey, _T("ServerIsConfigured"), 0, REG_DWORD, (BYTE *)&dwData, sizeof(DWORD));
185 RegCloseKey(hKey);
186 }
187 }
188
189 m_pMainWnd->PostMessage(WM_COMMAND, ID_APP_EXIT);
190 }
191
192
193 //
194 // Create configuration file for local agent
195 //
196
197 void CNxconfigApp::CreateAgentConfig()
198 {
199 FILE *fp;
200 time_t currTime;
201 DWORD dwSize;
202 IP_ADAPTER_INFO *pBuffer, *pInfo;
203 IP_ADDR_STRING *pAddr;
204 TCHAR szAddrList[4096], szFile[MAX_PATH];
205
206 if (_taccess(_T("nxagentd.conf"), 0) == 0)
207 return; // File already exist, we shouldn't overwrite it
208
209 // Get local interface list
210 szAddrList[0] = 0;
211 if (GetAdaptersInfo(NULL, &dwSize) == ERROR_BUFFER_OVERFLOW)
212 {
213 pBuffer = (IP_ADAPTER_INFO *)malloc(dwSize);
214 if (GetAdaptersInfo(pBuffer, &dwSize) == ERROR_SUCCESS)
215 {
216 for(pInfo = pBuffer; pInfo != NULL; pInfo = pInfo->Next)
217 {
218 // Read all IP addresses for adapter
219 for(pAddr = &pInfo->IpAddressList; pAddr != NULL; pAddr = pAddr->Next)
220 {
221 if (_tcscmp(pAddr->IpAddress.String, _T("0.0.0.0")))
222 {
223 if (szAddrList[0] != 0)
224 _tcscat(szAddrList, _T(", "));
225 _tcscat(szAddrList, pAddr->IpAddress.String);
226 }
227 }
228 }
229 }
230 free(pBuffer);
231 }
232
233 _sntprintf(szFile, MAX_PATH, _T("%s\\etc\\nxagentd.conf"), m_szInstallDir);
234 fp = _tfopen(szFile, _T("w"));
235 if (fp != NULL)
236 {
237 currTime = time(NULL);
238 _ftprintf(fp, _T("#\n# NetXMS agent configuration file\n# Created by server installer at %s#\n\n"),
239 _tctime(&currTime));
240 _ftprintf(fp, _T("LogFile = {syslog}\nServers = 127.0.0.1\n"));
241 if (szAddrList[0] != 0)
242 _ftprintf(fp, _T("InstallationServers = %s\n"), szAddrList);
243 _ftprintf(fp, _T("FileStore = %s\\var\n"), m_szInstallDir);
244 _ftprintf(fp, _T("RequireAuthentication = no\n"));
245 _ftprintf(fp, _T("SubAgent = winperf.nsm\nSubAgent = portcheck.nsm\n"));
246 fclose(fp);
247 }
248 }