schema-related information (like schema version, DB syntax, etc.) moved from "config...
[public/netxms.git] / src / server / core / config.cpp
1 /* $Id$ */
2 /*
3 ** NetXMS - Network Management System
4 ** Copyright (C) 2003, 2004, 2005, 2006, 2007 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 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 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: config.cpp
21 **
22 **/
23
24 #include "nxcore.h"
25
26
27 //
28 // Externals
29 //
30
31 extern TCHAR g_szCodePage[];
32 extern TCHAR *g_pszModLoadList;
33
34
35 //
36 // Load and parse configuration file
37 // Returns TRUE on success and FALSE on failure
38 //
39
40 static NX_CFG_TEMPLATE m_cfgTemplate[] =
41 {
42 { "CodePage", CT_STRING, 0, 0, 256, 0, g_szCodePage },
43 { "CreateCrashDumps", CT_BOOLEAN, 0, 0, AF_CATCH_EXCEPTIONS, 0, &g_dwFlags },
44 { "DBDriver", CT_STRING, 0, 0, MAX_PATH, 0, g_szDbDriver },
45 { "DBDrvParams", CT_STRING, 0, 0, MAX_PATH, 0, g_szDbDrvParams },
46 { "DBLogin", CT_STRING, 0, 0, MAX_DB_LOGIN, 0, g_szDbLogin },
47 { "DBName", CT_STRING, 0, 0, MAX_DB_NAME, 0, g_szDbName },
48 { "DBPassword", CT_STRING, 0, 0, MAX_DB_PASSWORD, 0, g_szDbPassword },
49 { "DBServer", CT_STRING, 0, 0, MAX_PATH, 0, g_szDbServer },
50 { "DumpDirectory", CT_STRING, 0, 0, MAX_PATH, 0, g_szDumpDir },
51 { "ListenAddress", CT_STRING, 0, 0, MAX_PATH, 0, g_szListenAddress },
52 { "LogFailedSQLQueries", CT_BOOLEAN, 0, 0, AF_LOG_SQL_ERRORS, 0, &g_dwFlags },
53 { "LogFile", CT_STRING, 0, 0, MAX_PATH, 0, g_szLogFile },
54 { "Module", CT_STRING_LIST, '\n', 0, 0, 0, &g_pszModLoadList },
55 { "", CT_END_OF_LIST, 0, 0, 0, 0, NULL }
56 };
57
58 BOOL NXCORE_EXPORTABLE LoadConfig(void)
59 {
60 BOOL bSuccess = FALSE;
61
62 #if !defined(_WIN32) && !defined(_NETWARE)
63 if (!_tcscmp(g_szConfigFile, _T("{search}")))
64 {
65 if (access(PREFIX "/etc/netxmsd.conf", 4) == 0)
66 {
67 _tcscpy(g_szConfigFile, PREFIX "/etc/netxmsd.conf");
68 }
69 else if (access("/usr/etc/netxmsd.conf", 4) == 0)
70 {
71 _tcscpy(g_szConfigFile, "/usr/etc/netxmsd.conf");
72 }
73 else
74 {
75 _tcscpy(g_szConfigFile, "/etc/netxmsd.conf");
76 }
77 }
78 #endif
79
80 if (IsStandalone())
81 printf("Using configuration file \"%s\"\n", g_szConfigFile);
82
83 if (NxLoadConfig(g_szConfigFile, "", m_cfgTemplate, IsStandalone()) == NXCFG_ERR_OK)
84 {
85 if ((!stricmp(g_szLogFile,"{EventLog}")) ||
86 (!stricmp(g_szLogFile,"{syslog}")))
87 {
88 g_dwFlags |= AF_USE_SYSLOG;
89 }
90 else
91 {
92 g_dwFlags &= ~AF_USE_SYSLOG;
93 }
94 bSuccess = TRUE;
95 }
96 return bSuccess;
97 }
98
99
100 //
101 // Read string value from metadata table
102 //
103
104 BOOL NXCORE_EXPORTABLE MetaDataReadStr(const TCHAR *szVar, TCHAR *szBuffer, int iBufSize, const TCHAR *szDefault)
105 {
106 DB_RESULT hResult;
107 TCHAR szQuery[256];
108 BOOL bSuccess = FALSE;
109
110 nx_strncpy(szBuffer, szDefault, iBufSize);
111 if (_tcslen(szVar) > 127)
112 return FALSE;
113
114 _sntprintf(szQuery, 256, _T("SELECT var_value FROM metadata WHERE var_name='%s'"), szVar);
115 hResult = DBSelect(g_hCoreDB, szQuery);
116 if (hResult == 0)
117 return FALSE;
118
119 if (DBGetNumRows(hResult) > 0)
120 {
121 DBGetField(hResult, 0, 0, szBuffer, iBufSize);
122 DecodeSQLString(szBuffer);
123 bSuccess = TRUE;
124 }
125
126 DBFreeResult(hResult);
127 return bSuccess;
128 }
129
130
131 //
132 // Read string value from configuration table
133 //
134
135 BOOL NXCORE_EXPORTABLE ConfigReadStr(const TCHAR *szVar, TCHAR *szBuffer, int iBufSize, const TCHAR *szDefault)
136 {
137 DB_RESULT hResult;
138 TCHAR szQuery[256];
139 BOOL bSuccess = FALSE;
140
141 nx_strncpy(szBuffer, szDefault, iBufSize);
142 if (_tcslen(szVar) > 127)
143 return FALSE;
144
145 _sntprintf(szQuery, 256, _T("SELECT var_value FROM config WHERE var_name='%s'"), szVar);
146 hResult = DBSelect(g_hCoreDB, szQuery);
147 if (hResult == 0)
148 return FALSE;
149
150 if (DBGetNumRows(hResult) > 0)
151 {
152 DBGetField(hResult, 0, 0, szBuffer, iBufSize);
153 DecodeSQLString(szBuffer);
154 bSuccess = TRUE;
155 }
156
157 DBFreeResult(hResult);
158 return bSuccess;
159 }
160
161
162 //
163 // Read integer value from configuration table
164 //
165
166 int NXCORE_EXPORTABLE ConfigReadInt(const TCHAR *szVar, int iDefault)
167 {
168 TCHAR szBuffer[64];
169
170 if (ConfigReadStr(szVar, szBuffer, 64, _T("")))
171 return _tcstol(szBuffer, NULL, 0);
172 else
173 return iDefault;
174 }
175
176
177 //
178 // Read unsigned long value from configuration table
179 //
180
181 DWORD NXCORE_EXPORTABLE ConfigReadULong(const TCHAR *szVar, DWORD dwDefault)
182 {
183 TCHAR szBuffer[64];
184
185 if (ConfigReadStr(szVar, szBuffer, 64, _T("")))
186 return _tcstoul(szBuffer, NULL, 0);
187 else
188 return dwDefault;
189 }
190
191
192 //
193 // Read byte array (in hex form) from configuration table into integer array
194 //
195
196 BOOL NXCORE_EXPORTABLE ConfigReadByteArray(const TCHAR *pszVar, int *pnArray, int nSize, int nDefault)
197 {
198 TCHAR szBuffer[256];
199 char pbBytes[128];
200 BOOL bResult;
201 int i, nLen;
202
203 if (ConfigReadStr(pszVar, szBuffer, 256, _T("")))
204 {
205 StrToBin(szBuffer, (BYTE *)pbBytes, 128);
206 nLen = (int)strlen(szBuffer) / 2;
207 for(i = 0; (i < nSize) && (i < nLen); i++)
208 pnArray[i] = pbBytes[i];
209 for(; i < nSize; i++)
210 pnArray[i] = nDefault;
211 bResult = TRUE;
212 }
213 else
214 {
215 for(i = 0; i < nSize; i++)
216 pnArray[i] = nDefault;
217 bResult = FALSE;
218 }
219 return bResult;
220 }
221
222
223 //
224 // Write string value to configuration table
225 //
226
227 BOOL NXCORE_EXPORTABLE ConfigWriteStr(const TCHAR *szVar, const TCHAR *szValue, BOOL bCreate,
228 BOOL isVisible, BOOL needRestart)
229 {
230 DB_RESULT hResult;
231 TCHAR *pszEscValue, szQuery[1024];
232 BOOL bVarExist = FALSE;
233
234 if (_tcslen(szVar) > 127)
235 return FALSE;
236
237 // Check for variable existence
238 _sntprintf(szQuery, 1024, _T("SELECT var_value FROM config WHERE var_name='%s'"), szVar);
239 hResult = DBSelect(g_hCoreDB, szQuery);
240 if (hResult != NULL)
241 {
242 if (DBGetNumRows(hResult) > 0)
243 bVarExist = TRUE;
244 DBFreeResult(hResult);
245 }
246
247 // Don't create non-existing variable if creation flag not set
248 if (!bCreate && !bVarExist)
249 return FALSE;
250
251 // Create or update variable value
252 pszEscValue = EncodeSQLString(szValue);
253 if (bVarExist)
254 _sntprintf(szQuery, 1024, _T("UPDATE config SET var_value='%s' WHERE var_name='%s'"),
255 pszEscValue, szVar);
256 else
257 _sntprintf(szQuery, 1024, _T("INSERT INTO config (var_name,var_value,is_visible,")
258 _T("need_server_restart) VALUES ('%s','%s',%d,%d)"),
259 szVar, pszEscValue, isVisible, needRestart);
260 free(pszEscValue);
261 return DBQuery(g_hCoreDB, szQuery);
262 }
263
264
265 //
266 // Write integer value to configuration table
267 //
268
269 BOOL NXCORE_EXPORTABLE ConfigWriteInt(const TCHAR *szVar, int iValue, BOOL bCreate, BOOL isVisible, BOOL needRestart)
270 {
271 TCHAR szBuffer[64];
272
273 _stprintf(szBuffer, _T("%d"), iValue);
274 return ConfigWriteStr(szVar, szBuffer, bCreate, isVisible, needRestart);
275 }
276
277
278 //
279 // Write unsigned long value to configuration table
280 //
281
282 BOOL NXCORE_EXPORTABLE ConfigWriteULong(const TCHAR *szVar, DWORD dwValue, BOOL bCreate, BOOL isVisible, BOOL needRestart)
283 {
284 TCHAR szBuffer[64];
285
286 _stprintf(szBuffer, _T("%u"), dwValue);
287 return ConfigWriteStr(szVar, szBuffer, bCreate, isVisible, needRestart);
288 }
289
290
291 //
292 // Write integer array to configuration table
293 //
294
295 BOOL NXCORE_EXPORTABLE ConfigWriteByteArray(const TCHAR *pszVar, int *pnArray, int nSize, BOOL bCreate, BOOL isVisible, BOOL needRestart)
296 {
297 TCHAR szBuffer[256];
298 int i, j;
299
300 for(i = 0, j = 0; (i < nSize) && (i < 127); i++, j += 2)
301 _stprintf(&szBuffer[j], _T("%02X"), (char)((pnArray[i] > 127) ? 127 : ((pnArray[i] < -127) ? -127 : pnArray[i])));
302 return ConfigWriteStr(pszVar, szBuffer, bCreate, isVisible, needRestart);
303 }
304
305
306 //
307 // Read large string (clob) value from configuration table
308 //
309
310 TCHAR NXCORE_EXPORTABLE *ConfigReadCLOB(const TCHAR *var, const TCHAR *defValue)
311 {
312 DB_RESULT hResult;
313 TCHAR query[256], *result = NULL;
314 BOOL bSuccess = FALSE;
315
316 if (_tcslen(var) <= 127)
317 {
318 _sntprintf(query, 256, _T("SELECT var_value FROM config_clob WHERE var_name='%s'"), var);
319 hResult = DBSelect(g_hCoreDB, query);
320 if (hResult != NULL)
321 {
322 if (DBGetNumRows(hResult) > 0)
323 {
324 result = DBGetField(hResult, 0, 0, NULL, 0);
325 if (result != NULL)
326 DecodeSQLString(result);
327 }
328 DBFreeResult(hResult);
329 }
330 }
331
332 // Return default value in case of error
333 if ((result == NULL) && (defValue != NULL))
334 result = _tcsdup(defValue);
335
336 return result;
337 }
338
339
340 //
341 // Write large string (clob) value to configuration table
342 //
343
344 BOOL NXCORE_EXPORTABLE ConfigWriteCLOB(const TCHAR *var, const TCHAR *value, BOOL bCreate)
345 {
346 DB_RESULT hResult;
347 TCHAR *escValue, *query;
348 size_t len;
349 BOOL bVarExist = FALSE, success = FALSE;
350
351 if (_tcslen(var) > 127)
352 return FALSE;
353
354 escValue = EncodeSQLString(CHECK_NULL_EX(value));
355 len = _tcslen(escValue) + 256;
356 query = (TCHAR *)malloc(len * sizeof(TCHAR));
357
358 // Check for variable existence
359 _sntprintf(query, len, _T("SELECT var_value FROM config_clob WHERE var_name='%s'"), var);
360 hResult = DBSelect(g_hCoreDB, query);
361 if (hResult != NULL)
362 {
363 if (DBGetNumRows(hResult) > 0)
364 bVarExist = TRUE;
365 DBFreeResult(hResult);
366 }
367
368 // Don't create non-existing variable if creation flag not set
369 if (bCreate || bVarExist)
370 {
371 // Create or update variable value
372 if (bVarExist)
373 _sntprintf(query, len, _T("UPDATE config_clob SET var_value='%s' WHERE var_name='%s'"),
374 escValue, var);
375 else
376 _sntprintf(query, len, _T("INSERT INTO config_clob (var_name,var_value) VALUES ('%s','%s')"),
377 var, escValue);
378 success = DBQuery(g_hCoreDB, query);
379 }
380
381 free(query);
382 free(escValue);
383 return success;
384 }