972034ec81c7e0e8ed9dce124c2fdc09fdea62ca
[public/netxms.git] / src / agent / core / policy.cpp
1 /*
2 ** NetXMS multiplatform core agent
3 ** Copyright (C) 2003-2009 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
31 //
32 // Deploy configuration file
33 //
34
35 static DWORD DeployConfig(DWORD session, CSCPMessage *msg)
36 {
37 TCHAR path[MAX_PATH], name[MAX_PATH], *fname, tail;
38 int i, fh;
39 DWORD rcc;
40
41 msg->GetVariableStr(VID_CONFIG_FILE_NAME, name, MAX_PATH);
42 DebugPrintf(session, 3, _T("DeployConfig(): original file name is %s"), name);
43 for(i = (int)_tcslen(name) - 1; i >= 0; i--)
44 if ((name[i] == '/') || (name[i] == '\\'))
45 {
46 break;
47 }
48 fname = &name[i + 1];
49 tail = g_szConfigIncludeDir[_tcslen(g_szConfigIncludeDir) - 1];
50 _sntprintf(path, MAX_PATH, _T("%s%s%s"), g_szConfigIncludeDir, ((tail != '\\') && (tail != '/')) ? FS_PATH_SEPARATOR : _T(""), fname);
51
52 fh = _topen(path, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, S_IRUSR | S_IWUSR);
53 if (fh != -1)
54 {
55 DWORD size = msg->GetVariableBinary(VID_CONFIG_FILE_DATA, 0, NULL);
56 BYTE *data = (BYTE *)malloc(size);
57 if (data != NULL)
58 {
59 msg->GetVariableBinary(VID_CONFIG_FILE_DATA, data, size);
60 if (write(fh, data, size) == size)
61 {
62 DebugPrintf(session, 3, _T("Configuration file %s saved successfully"), path);
63 rcc = ERR_SUCCESS;
64 }
65 else
66 {
67 rcc = ERR_IO_FAILURE;
68 }
69 free(data);
70 }
71 else
72 {
73 rcc = ERR_MEM_ALLOC_FAILED;
74 }
75 close(fh);
76 }
77 else
78 {
79 DebugPrintf(session, 2, _T("DeployConfig(): Error opening file %s for writing (%s)"), path, _tcserror(errno));
80 rcc = ERR_FILE_OPEN_ERROR;
81 }
82
83 return rcc;
84 }
85
86
87 //
88 // Deploy policy on agent
89 //
90
91 DWORD DeployPolicy(DWORD session, CSCPMessage *request)
92 {
93 DWORD type, rcc;
94
95 type = request->GetVariableShort(VID_POLICY_TYPE);
96 switch(type)
97 {
98 case AGENT_POLICY_CONFIG:
99 rcc = DeployConfig(session, request);
100 break;
101 default:
102 rcc = ERR_BAD_ARGUMENTS;
103 break;
104 }
105 DebugPrintf(session, 3, _T("Policy deployment: TYPE=%d RCC=%d"), type, rcc);
106 return rcc;
107 }
108
109
110 //
111 // Remove configuration file
112 //
113
114 static DWORD RemoveConfig(DWORD session, CSCPMessage *msg)
115 {
116 TCHAR path[MAX_PATH], name[MAX_PATH], *fname, tail;
117 int i;
118 DWORD rcc;
119
120 msg->GetVariableStr(VID_CONFIG_FILE_NAME, name, MAX_PATH);
121 DebugPrintf(session, 3, _T("RemoveConfig(): original file name is %s"), name);
122 for(i = (int)_tcslen(name) - 1; i >= 0; i--)
123 if ((name[i] == '/') || (name[i] == '\\'))
124 {
125 break;
126 }
127 fname = &name[i + 1];
128 tail = g_szConfigIncludeDir[_tcslen(g_szConfigIncludeDir) - 1];
129 _sntprintf(path, MAX_PATH, _T("%s%s%s"), g_szConfigIncludeDir, ((tail != '\\') && (tail != '/')) ? FS_PATH_SEPARATOR : _T(""), fname);
130 if (_tremove(path) != 0)
131 {
132 rcc = (errno == ENOENT) ? ERR_SUCCESS : ERR_IO_FAILURE;
133 }
134 else
135 {
136 rcc = ERR_SUCCESS;
137 }
138 return rcc;
139 }
140
141
142 //
143 // Uninstall policy from agent
144 //
145
146 DWORD UninstallPolicy(DWORD session, CSCPMessage *request)
147 {
148 DWORD type, rcc;
149
150 type = request->GetVariableShort(VID_POLICY_TYPE);
151 switch(type)
152 {
153 case AGENT_POLICY_CONFIG:
154 rcc = RemoveConfig(session, request);
155 break;
156 default:
157 rcc = ERR_BAD_ARGUMENTS;
158 break;
159 }
160 DebugPrintf(session, 3, _T("Policy uninstall: TYPE=%d RCC=%d"), type, rcc);
161 return rcc;
162 }