Added configuration before file/folder overwrite. Fixed some small mistakes. Fixes...
[public/netxms.git] / src / server / libnxsrv / main.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Server Library
4 ** Copyright (C) 2003-2016 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU Lesser General Public License as published by
8 ** the Free Software Foundation; either version 3 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU Lesser General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: main.cpp
21 **
22 **/
23
24 #include "libnxsrv.h"
25
26 /**
27 * Global variables
28 */
29 UINT64 LIBNXSRV_EXPORTABLE g_flags = AF_USE_SYSLOG | AF_CATCH_EXCEPTIONS | AF_LOG_SQL_ERRORS | AF_ENABLE_LOCAL_CONSOLE;
30
31 /**
32 * Agent error codes
33 */
34 static struct
35 {
36 int iCode;
37 const TCHAR *pszText;
38 } m_agentErrors[] =
39 {
40 { ERR_SUCCESS, _T("Success") },
41 { ERR_UNKNOWN_COMMAND, _T("Unknown command") },
42 { ERR_AUTH_REQUIRED, _T("Authentication required") },
43 { ERR_ACCESS_DENIED, _T("Access denied") },
44 { ERR_UNKNOWN_PARAMETER, _T("Unknown parameter") },
45 { ERR_REQUEST_TIMEOUT, _T("Request timeout") },
46 { ERR_AUTH_FAILED, _T("Authentication failed") },
47 { ERR_ALREADY_AUTHENTICATED, _T("Already authenticated") },
48 { ERR_AUTH_NOT_REQUIRED, _T("Authentication not required") },
49 { ERR_INTERNAL_ERROR, _T("Internal error") },
50 { ERR_NOT_IMPLEMENTED, _T("Not implemented") },
51 { ERR_OUT_OF_RESOURCES, _T("Out of resources") },
52 { ERR_NOT_CONNECTED, _T("Not connected") },
53 { ERR_CONNECTION_BROKEN, _T("Connection broken") },
54 { ERR_BAD_RESPONSE, _T("Bad response") },
55 { ERR_IO_FAILURE, _T("I/O failure") },
56 { ERR_RESOURCE_BUSY, _T("Resource busy") },
57 { ERR_EXEC_FAILED, _T("External program execution failed") },
58 { ERR_ENCRYPTION_REQUIRED, _T("Encryption required") },
59 { ERR_NO_CIPHERS, _T("No acceptable ciphers") },
60 { ERR_INVALID_PUBLIC_KEY, _T("Invalid public key") },
61 { ERR_INVALID_SESSION_KEY, _T("Invalid session key") },
62 { ERR_CONNECT_FAILED, _T("Connect failed") },
63 { ERR_MALFORMED_COMMAND, _T("Malformed command") },
64 { ERR_SOCKET_ERROR, _T("Socket error") },
65 { ERR_BAD_ARGUMENTS, _T("Bad arguments") },
66 { ERR_SUBAGENT_LOAD_FAILED, _T("Subagent load failed") },
67 { ERR_FILE_OPEN_ERROR, _T("File open error") },
68 { ERR_FILE_STAT_FAILED, _T("File stat filed") },
69 { ERR_MEM_ALLOC_FAILED, _T("Memory allocation failed") },
70 { ERR_FILE_DELETE_FAILED, _T("File delete failed") },
71 { ERR_NO_SESSION_AGENT, _T("Session agent not available") },
72 { ERR_SERVER_ID_UNSET, _T("Server ID is not set") },
73 { ERR_NO_SUCH_INSTANCE, _T("No such instance") },
74 { ERR_OUT_OF_STATE_REQUEST, _T("Request is out of state") },
75 { ERR_ENCRYPTION_ERROR, _T("Encryption error") },
76 { -1, NULL }
77 };
78
79 /**
80 * Resolve agent's error code to text
81 */
82 const TCHAR LIBNXSRV_EXPORTABLE *AgentErrorCodeToText(UINT32 err)
83 {
84 int i;
85
86 for(i = 0; m_agentErrors[i].pszText != NULL; i++)
87 if (err == (UINT32)m_agentErrors[i].iCode)
88 return m_agentErrors[i].pszText;
89 return _T("Unknown error code");
90 }
91
92 /**
93 * Convert agent error code to client RCC
94 */
95 UINT32 LIBNXSRV_EXPORTABLE AgentErrorToRCC(UINT32 err)
96 {
97 switch(err)
98 {
99 case ERR_SUCCESS:
100 return RCC_SUCCESS;
101 case ERR_ACCESS_DENIED:
102 return RCC_ACCESS_DENIED;
103 case ERR_IO_FAILURE:
104 return RCC_IO_ERROR;
105 case ERR_ALREADY_AUTHENTICATED:
106 case ERR_AUTH_FAILED:
107 case ERR_AUTH_NOT_REQUIRED:
108 return RCC_COMM_FAILURE;
109 case ERR_NO_SUCH_INSTANCE:
110 return RCC_NO_SUCH_INSTANCE;
111 case ERR_REQUEST_TIMEOUT:
112 return RCC_TIMEOUT;
113 case ERR_ENCRYPTION_ERROR:
114 return RCC_ENCRYPTION_ERROR;
115 case ERR_OUT_OF_STATE_REQUEST:
116 return RCC_OUT_OF_STATE_REQUEST;
117 case ERR_FILE_ALREADY_EXISTS:
118 return RCC_FILE_ALREADY_EXISTS;
119 case ERR_FOLDER_ALREADY_EXISTS:
120 return RCC_FOLDER_ALREADY_EXISTS;
121 }
122 return RCC_AGENT_ERROR;
123 }
124
125 /**
126 * Destroy ARP cache created by discovery functions
127 */
128 void LIBNXSRV_EXPORTABLE DestroyArpCache(ARP_CACHE *pArpCache)
129 {
130 if (pArpCache != NULL)
131 {
132 if (pArpCache->pEntries != NULL)
133 free(pArpCache->pEntries);
134 free(pArpCache);
135 }
136 }
137
138 /**
139 * Destroy routing table
140 */
141 void LIBNXSRV_EXPORTABLE DestroyRoutingTable(ROUTING_TABLE *pRT)
142 {
143 if (pRT != NULL)
144 {
145 safe_free(pRT->pRoutes);
146 free(pRT);
147 }
148 }
149
150 /**
151 * Route comparision callback
152 */
153 static int CompareRoutes(const void *p1, const void *p2)
154 {
155 return -(COMPARE_NUMBERS(((ROUTE *)p1)->dwDestMask, ((ROUTE *)p2)->dwDestMask));
156 }
157
158 /**
159 * Sort routing table (put most specific routes first)
160 */
161 void LIBNXSRV_EXPORTABLE SortRoutingTable(ROUTING_TABLE *pRT)
162 {
163 qsort(pRT->pRoutes, pRT->iNumEntries, sizeof(ROUTE), CompareRoutes);
164 }
165
166 /**
167 * Log custom message (mostly used by modules)
168 */
169 void LIBNXSRV_EXPORTABLE WriteLogOther(WORD wType, const TCHAR *format, ...)
170 {
171 va_list args;
172 TCHAR buffer[4096];
173
174 va_start(args, format);
175 _vsntprintf(buffer, 4096, format, args);
176 va_end(args);
177 nxlog_write(MSG_OTHER, wType, "s", buffer);
178 }
179
180 #ifdef _WIN32
181
182 /**
183 * DLL entry point
184 */
185 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
186 {
187 if (dwReason == DLL_PROCESS_ATTACH)
188 DisableThreadLibraryCalls(hInstance);
189 return TRUE;
190 }
191
192 #endif /* _WIN32 */