added persistent storage in agent; added agent policy registration in persistent...
[public/netxms.git] / src / agent / core / policy.cpp
1 /*
2 ** NetXMS multiplatform core agent
3 ** Copyright (C) 2003-2010 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 usefu,,
11 ** but ITHOUT 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: policy.cpp
20 **
21 **/
22
23 #include "nxagentd.h"
24
25 #ifdef _WIN32
26 #define write _write
27 #define close _close
28 #endif
29
30 #define POLICY_REGISTRY_PATH _T("/policyRegistry")
31
32
33 //
34 // Register policy in persistent storage
35 //
36
37 static void RegisterPolicy(CommSession *session, DWORD type, uuid_t guid)
38 {
39 TCHAR path[256], buffer[64];
40 int tail;
41
42 _sntprintf(path, 256, _T("/policyRegistry/%s/"), uuid_to_string(guid, buffer));
43 tail = _tcslen(path);
44
45 Config *registry = OpenRegistry();
46
47 _tcscpy(&path[tail], _T("type"));
48 registry->setValue(path, type);
49
50 _tcscpy(&path[tail], _T("server"));
51 registry->setValue(path, IpToStr(session->getServerAddress(), buffer));
52
53 CloseRegistry(true);
54 }
55
56
57 //
58 // Register policy in persistent storage
59 //
60
61 static void UnregisterPolicy(DWORD type, uuid_t guid)
62 {
63 TCHAR path[256], buffer[64];
64
65 _sntprintf(path, 256, _T("/policyRegistry/%s"), uuid_to_string(guid, buffer));
66 Config *registry = OpenRegistry();
67 registry->deleteEntry(path);
68 CloseRegistry(true);
69 }
70
71
72 //
73 // Deploy configuration file
74 //
75
76 static DWORD DeployConfig(DWORD session, uuid_t guid, CSCPMessage *msg)
77 {
78 TCHAR path[MAX_PATH], name[64], tail;
79 int fh;
80 DWORD rcc;
81
82 tail = g_szConfigIncludeDir[_tcslen(g_szConfigIncludeDir) - 1];
83 _sntprintf(path, MAX_PATH, _T("%s%s%s.conf"), g_szConfigIncludeDir,
84 ((tail != '\\') && (tail != '/')) ? FS_PATH_SEPARATOR : _T(""),
85 uuid_to_string(guid, name));
86
87 fh = _topen(path, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, S_IRUSR | S_IWUSR);
88 if (fh != -1)
89 {
90 DWORD size = msg->GetVariableBinary(VID_CONFIG_FILE_DATA, 0, NULL);
91 BYTE *data = (BYTE *)malloc(size);
92 if (data != NULL)
93 {
94 msg->GetVariableBinary(VID_CONFIG_FILE_DATA, data, size);
95 if (write(fh, data, size) == size)
96 {
97 DebugPrintf(session, 3, _T("Configuration file %s saved successfully"), path);
98 rcc = ERR_SUCCESS;
99 }
100 else
101 {
102 rcc = ERR_IO_FAILURE;
103 }
104 free(data);
105 }
106 else
107 {
108 rcc = ERR_MEM_ALLOC_FAILED;
109 }
110 close(fh);
111 }
112 else
113 {
114 DebugPrintf(session, 2, _T("DeployConfig(): Error opening file %s for writing (%s)"), path, _tcserror(errno));
115 rcc = ERR_FILE_OPEN_ERROR;
116 }
117
118 return rcc;
119 }
120
121
122 //
123 // Deploy policy on agent
124 //
125
126 DWORD DeployPolicy(CommSession *session, CSCPMessage *request)
127 {
128 DWORD type, rcc;
129 uuid_t guid;
130
131 type = request->GetVariableShort(VID_POLICY_TYPE);
132 request->GetVariableBinary(VID_GUID, guid, UUID_LENGTH);
133
134 switch(type)
135 {
136 case AGENT_POLICY_CONFIG:
137 rcc = DeployConfig(session->getIndex(), guid, request);
138 break;
139 default:
140 rcc = ERR_BAD_ARGUMENTS;
141 break;
142 }
143
144 if (rcc == RCC_SUCCESS)
145 RegisterPolicy(session, type, guid);
146
147 DebugPrintf(session->getIndex(), 3, _T("Policy deployment: TYPE=%d RCC=%d"), type, rcc);
148 return rcc;
149 }
150
151
152 //
153 // Remove configuration file
154 //
155
156 static DWORD RemoveConfig(DWORD session, uuid_t guid, CSCPMessage *msg)
157 {
158 TCHAR path[MAX_PATH], name[64], tail;
159 DWORD rcc;
160
161 tail = g_szConfigIncludeDir[_tcslen(g_szConfigIncludeDir) - 1];
162 _sntprintf(path, MAX_PATH, _T("%s%s%s.conf"), g_szConfigIncludeDir,
163 ((tail != '\\') && (tail != '/')) ? FS_PATH_SEPARATOR : _T(""),
164 uuid_to_string(guid, name));
165
166 if (_tremove(path) != 0)
167 {
168 rcc = (errno == ENOENT) ? ERR_SUCCESS : ERR_IO_FAILURE;
169 }
170 else
171 {
172 rcc = ERR_SUCCESS;
173 }
174 return rcc;
175 }
176
177
178 //
179 // Uninstall policy from agent
180 //
181
182 DWORD UninstallPolicy(CommSession *session, CSCPMessage *request)
183 {
184 DWORD type, rcc;
185 uuid_t guid;
186
187 type = request->GetVariableShort(VID_POLICY_TYPE);
188 request->GetVariableBinary(VID_GUID, guid, UUID_LENGTH);
189
190 switch(type)
191 {
192 case AGENT_POLICY_CONFIG:
193 rcc = RemoveConfig(session->getIndex(), guid, request);
194 break;
195 default:
196 rcc = ERR_BAD_ARGUMENTS;
197 break;
198 }
199
200 if (rcc == RCC_SUCCESS)
201 UnregisterPolicy(type, guid);
202
203 DebugPrintf(session->getIndex(), 3, _T("Policy uninstall: TYPE=%d RCC=%d"), type, rcc);
204 return rcc;
205 }