6d212acf9f09aefc365a1de0c83f4ac47908937f
[public/netxms.git] / src / agent / core / config.cpp
1 /*
2 ** NetXMS multiplatform core agent
3 ** Copyright (C) 2003-2013 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: config.cpp
20 **
21 **/
22
23 #include "nxagentd.h"
24
25 /**
26 * Externals
27 */
28 LONG H_PlatformName(const TCHAR *cmd, const TCHAR *arg, TCHAR *value);
29
30 /**
31 * Max NXCP message size
32 */
33 #define MAX_MSG_SIZE 262144
34
35 /**
36 * Save config to file
37 */
38 static BOOL SaveConfig(TCHAR *pszConfig)
39 {
40 FILE *fp;
41 BOOL bRet = FALSE;
42
43 fp = _tfopen(g_szConfigFile, _T("w"));
44 if (fp != NULL)
45 {
46 bRet = (_fputts(pszConfig, fp) >= 0);
47 fclose(fp);
48 }
49 return bRet;
50 }
51
52 /**
53 * Download agent's config from management server
54 */
55 BOOL DownloadConfig(TCHAR *pszServer)
56 {
57 UINT32 dwAddr;
58 SOCKET hSocket;
59 struct sockaddr_in sa;
60 BOOL bRet = FALSE;
61 TCHAR szBuffer[MAX_RESULT_LENGTH], *pszConfig;
62 CSCPMessage msg, *pResponse;
63 CSCP_MESSAGE *pRawMsg;
64 CSCP_BUFFER *pBuffer;
65 NXCPEncryptionContext *pDummyCtx = NULL;
66 int nLen;
67
68 #ifdef _WIN32
69 WSADATA wsaData;
70
71 if (WSAStartup(0x0202, &wsaData) != 0)
72 {
73 _tprintf(_T("ERROR: Unable to initialize Windows Sockets\n"));
74 return FALSE;
75 }
76 #endif
77
78 dwAddr = ResolveHostName(pszServer);
79 if (dwAddr == INADDR_NONE)
80 {
81 _tprintf(_T("ERROR: Unable to resolve name of management server\n"));
82 return FALSE;
83 }
84
85 hSocket = socket(AF_INET, SOCK_STREAM, 0);
86 if (hSocket != INVALID_SOCKET)
87 {
88 // Fill in address structure
89 memset(&sa, 0, sizeof(sa));
90 sa.sin_addr.s_addr = dwAddr;
91 sa.sin_family = AF_INET;
92 sa.sin_port = htons(4701);
93 if (connect(hSocket, (struct sockaddr *)&sa, sizeof(sa)) != -1)
94 {
95 // Prepare request
96 msg.SetCode(CMD_GET_MY_CONFIG);
97 msg.SetId(1);
98 if (H_PlatformName(NULL, NULL, szBuffer) != SYSINFO_RC_SUCCESS)
99 _tcscpy(szBuffer, _T("error"));
100 msg.SetVariable(VID_PLATFORM_NAME, szBuffer);
101 msg.SetVariable(VID_VERSION_MAJOR, (WORD)NETXMS_VERSION_MAJOR);
102 msg.SetVariable(VID_VERSION_MINOR, (WORD)NETXMS_VERSION_MINOR);
103 msg.SetVariable(VID_VERSION_RELEASE, (WORD)NETXMS_VERSION_BUILD);
104
105 // Send request
106 pRawMsg = msg.createMessage();
107 nLen = ntohl(pRawMsg->dwSize);
108 if (SendEx(hSocket, pRawMsg, nLen, 0, NULL) == nLen)
109 {
110 pRawMsg = (CSCP_MESSAGE *)realloc(pRawMsg, MAX_MSG_SIZE);
111 pBuffer = (CSCP_BUFFER *)malloc(sizeof(CSCP_BUFFER));
112 RecvNXCPMessage(0, NULL, pBuffer, 0, NULL, NULL, 0);
113
114 nLen = RecvNXCPMessage(hSocket, pRawMsg, pBuffer, MAX_MSG_SIZE,
115 &pDummyCtx, NULL, 30000);
116 if (nLen >= 16)
117 {
118 pResponse = new CSCPMessage(pRawMsg);
119 if ((pResponse->GetCode() == CMD_REQUEST_COMPLETED) &&
120 (pResponse->GetId() == 1) &&
121 (pResponse->GetVariableLong(VID_RCC) == 0))
122 {
123 pszConfig = pResponse->GetVariableStr(VID_CONFIG_FILE);
124 if (pszConfig != NULL)
125 {
126 bRet = SaveConfig(pszConfig);
127 free(pszConfig);
128 }
129 }
130 delete pResponse;
131 }
132 free(pBuffer);
133 }
134 free(pRawMsg);
135 }
136 else
137 {
138 printf("ERROR: Cannot connect to management server\n");
139 }
140 closesocket(hSocket);
141 }
142
143 #ifdef _WIN32
144 WSACleanup();
145 #endif
146 return bRet;
147 }