15c0fb1343ba7c73a1b4e15b86b484e994567851
[public/netxms.git] / src / server / core / config.cpp
1 /*
2 ** Project X - Network Management System
3 ** Copyright (C) 2003 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 ** $module: config.cpp
20 **
21 **/
22
23 #include "nxcore.h"
24
25
26 //
27 // Static data
28 //
29
30 static TCHAR m_szCodePage[256] = ICONV_DEFAULT_CODEPAGE;
31
32
33 //
34 // Load and parse configuration file
35 // Returns TRUE on success and FALSE on failure
36 //
37
38 static NX_CFG_TEMPLATE m_cfgTemplate[] =
39 {
40 { "CodePage", CT_STRING, 0, 0, 256, 0, m_szCodePage },
41 { "DBDriver", CT_STRING, 0, 0, MAX_PATH, 0, g_szDbDriver },
42 { "DBDrvParams", CT_STRING, 0, 0, MAX_PATH, 0, g_szDbDrvParams },
43 { "DBLogin", CT_STRING, 0, 0, MAX_DB_LOGIN, 0, g_szDbLogin },
44 { "DBName", CT_STRING, 0, 0, MAX_DB_NAME, 0, g_szDbName },
45 { "DBPassword", CT_STRING, 0, 0, MAX_DB_PASSWORD, 0, g_szDbPassword },
46 { "DBServer", CT_STRING, 0, 0, MAX_PATH, 0, g_szDbServer },
47 { "LogFailedSQLQueries", CT_BOOLEAN, 0, 0, AF_LOG_SQL_ERRORS, 0, &g_dwFlags },
48 { "LogFile", CT_STRING, 0, 0, MAX_PATH, 0, g_szLogFile },
49 { "", CT_END_OF_LIST, 0, 0, 0, 0, NULL }
50 };
51
52 BOOL NXCORE_EXPORTABLE LoadConfig(void)
53 {
54 BOOL bSuccess = FALSE;
55
56 if (IsStandalone())
57 printf("Using configuration file \"%s\"\n", g_szConfigFile);
58
59 if (NxLoadConfig(g_szConfigFile, "", m_cfgTemplate, IsStandalone()) == NXCFG_ERR_OK)
60 {
61 if ((!stricmp(g_szLogFile,"{EventLog}")) ||
62 (!stricmp(g_szLogFile,"{syslog}")))
63 {
64 g_dwFlags |= AF_USE_EVENT_LOG;
65 }
66 else
67 {
68 g_dwFlags &= ~AF_USE_EVENT_LOG;
69 }
70 #ifndef _WIN32
71 SetDefaultCodepage(m_szCodePage);
72 #endif
73 bSuccess = TRUE;
74 }
75 return bSuccess;
76 }
77
78
79 //
80 // Read string value from configuration table
81 //
82
83 BOOL NXCORE_EXPORTABLE ConfigReadStr(TCHAR *szVar, TCHAR *szBuffer, int iBufSize, const TCHAR *szDefault)
84 {
85 DB_RESULT hResult;
86 TCHAR szQuery[256];
87 BOOL bSuccess = FALSE;
88
89 nx_strncpy(szBuffer, szDefault, iBufSize);
90 if (_tcslen(szVar) > 127)
91 return FALSE;
92
93 _sntprintf(szQuery, 256, _T("SELECT var_value FROM config WHERE var_name='%s'"), szVar);
94 hResult = DBSelect(g_hCoreDB, szQuery);
95 if (hResult == 0)
96 return FALSE;
97
98 if (DBGetNumRows(hResult) > 0)
99 {
100 DBGetField(hResult, 0, 0, szBuffer, iBufSize);
101 DecodeSQLString(szBuffer);
102 bSuccess = TRUE;
103 }
104
105 DBFreeResult(hResult);
106 return bSuccess;
107 }
108
109
110 //
111 // Read integer value from configuration table
112 //
113
114 int NXCORE_EXPORTABLE ConfigReadInt(TCHAR *szVar, int iDefault)
115 {
116 TCHAR szBuffer[64];
117
118 if (ConfigReadStr(szVar, szBuffer, 64, _T("")))
119 return _tcstol(szBuffer, NULL, 0);
120 else
121 return iDefault;
122 }
123
124
125 //
126 // Read unsigned long value from configuration table
127 //
128
129 DWORD NXCORE_EXPORTABLE ConfigReadULong(TCHAR *szVar, DWORD dwDefault)
130 {
131 TCHAR szBuffer[64];
132
133 if (ConfigReadStr(szVar, szBuffer, 64, _T("")))
134 return _tcstoul(szBuffer, NULL, 0);
135 else
136 return dwDefault;
137 }
138
139
140 //
141 // Read byte array (in hex form) from configuration table into integer array
142 //
143
144 BOOL NXCORE_EXPORTABLE ConfigReadByteArray(TCHAR *pszVar, int *pnArray, int nSize, int nDefault)
145 {
146 TCHAR szBuffer[256];
147 char pbBytes[128];
148 BOOL bResult;
149 int i, nLen;
150
151 if (ConfigReadStr(pszVar, szBuffer, 256, _T("")))
152 {
153 StrToBin(szBuffer, (BYTE *)pbBytes, 128);
154 nLen = (int)strlen(szBuffer) / 2;
155 for(i = 0; (i < nSize) && (i < nLen); i++)
156 pnArray[i] = pbBytes[i];
157 for(; i < nSize; i++)
158 pnArray[i] = nDefault;
159 bResult = TRUE;
160 }
161 else
162 {
163 for(i = 0; i < nSize; i++)
164 pnArray[i] = nDefault;
165 bResult = FALSE;
166 }
167 return bResult;
168 }
169
170
171 //
172 // Write string value to configuration table
173 //
174
175 BOOL NXCORE_EXPORTABLE ConfigWriteStr(TCHAR *szVar, TCHAR *szValue, BOOL bCreate)
176 {
177 DB_RESULT hResult;
178 TCHAR *pszEscValue, szQuery[1024];
179 BOOL bVarExist = FALSE;
180
181 if (strlen(szVar) > 127)
182 return FALSE;
183
184 // Check for variable existence
185 _sntprintf(szQuery, 1024, _T("SELECT var_value FROM config WHERE var_name='%s'"), szVar);
186 hResult = DBSelect(g_hCoreDB, szQuery);
187 if (hResult != NULL)
188 {
189 if (DBGetNumRows(hResult) > 0)
190 bVarExist = TRUE;
191 DBFreeResult(hResult);
192 }
193
194 // Don't create non-existing variable if creation flag not set
195 if (!bCreate && !bVarExist)
196 return FALSE;
197
198 // Create or update variable value
199 pszEscValue = EncodeSQLString(szValue);
200 if (bVarExist)
201 _sntprintf(szQuery, 1024, _T("UPDATE config SET var_value='%s' WHERE var_name='%s'"),
202 pszEscValue, szVar);
203 else
204 _sntprintf(szQuery, 1024, _T("INSERT INTO config (var_name,var_value,is_visible,")
205 _T("need_server_restart) VALUES ('%s','%s',1,1)"),
206 szVar, pszEscValue);
207 free(pszEscValue);
208 return DBQuery(g_hCoreDB, szQuery);
209 }
210
211
212 //
213 // Write integer value to configuration table
214 //
215
216 BOOL NXCORE_EXPORTABLE ConfigWriteInt(TCHAR *szVar, int iValue, BOOL bCreate)
217 {
218 TCHAR szBuffer[64];
219
220 _stprintf(szBuffer, _T("%d"), iValue);
221 return ConfigWriteStr(szVar, szBuffer, bCreate);
222 }
223
224
225 //
226 // Write unsigned long value to configuration table
227 //
228
229 BOOL NXCORE_EXPORTABLE ConfigWriteULong(TCHAR *szVar, DWORD dwValue, BOOL bCreate)
230 {
231 TCHAR szBuffer[64];
232
233 _stprintf(szBuffer, _T("%u"), dwValue);
234 return ConfigWriteStr(szVar, szBuffer, bCreate);
235 }
236
237
238 //
239 // Write integer array to configuration table
240 //
241
242 BOOL NXCORE_EXPORTABLE ConfigWriteByteArray(TCHAR *pszVar, int *pnArray, int nSize, BOOL bCreate)
243 {
244 TCHAR szBuffer[256];
245 int i, j;
246
247 for(i = 0, j = 0; (i < nSize) && (i < 127); i++, j += 2)
248 _stprintf(&szBuffer[j], _T("%02X"), (char)((pnArray[i] > 127) ? 127 : ((pnArray[i] < -127) ? -127 : pnArray[i])));
249 return ConfigWriteStr(pszVar, szBuffer, bCreate);
250 }