reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
SqlDb.cxx
Go to the documentation of this file.
1#include <cassert>
2#include <fcntl.h>
3
4#ifdef HAVE_CONFIG_H
5#include "config.h"
6#endif
7
8#include "rutil/ResipAssert.h"
9#include "rutil/Data.hxx"
10#include "rutil/DataStream.hxx"
11#include "rutil/Logger.hxx"
12#include "rutil/ParseBuffer.hxx"
13
14#include "repro/AbstractDb.hxx"
15#include "repro/SqlDb.hxx"
16
17
18using namespace resip;
19using namespace repro;
20using namespace std;
21
22#define RESIPROCATE_SUBSYSTEM Subsystem::REPRO
23
24SqlDb::SqlDb(const resip::ConfigParse& config) : mConnected(false)
25{
26 mTlsPeerAuthorizationQuery = config.getConfigData("CustomTlsAuthQuery", "");
27 mTableNamePrefix = config.getConfigData("TableNamePrefix", "");
28}
29
30void
32{
33 Data command;
34 {
35 DataStream ds(command);
36 ds << "DELETE FROM " << tableName(UserTable) << " ";
37 userWhereClauseToDataStream(key, ds);
38 }
39 query(command);
40}
41
42void
44{
45 Data command;
46 {
47 DataStream ds(command);
48 ds << "DELETE FROM " << tableName(TlsPeerIdentityTable) << " ";
49 tlsPeerIdentityWhereClauseToDataStream(key, ds);
50 }
51 query(command);
52}
53
54void
55SqlDb::setToData(const std::set<resip::Data>& items, resip::Data& result, const resip::Data& sep, const char quote) const
56{
57 DataStream ds(result);
58 std::set<resip::Data>::const_iterator it = items.begin();
59 while(it != items.end())
60 {
61 if(it != items.begin())
62 {
63 ds << sep;
64 }
65 if(quote != 0)
66 {
67 ds << quote << *it << quote;
68 }
69 else
70 {
71 ds << *it;
72 }
73 it++;
74 }
75}
76
77bool
78SqlDb::isAuthorized(const std::set<resip::Data>& peerNames, const std::set<resip::Data>& identities) const
79{
80 std::vector<Data> ret;
81
82 Data peerNameSet;
83 setToData(peerNames, peerNameSet);
84 StackLog(<<"peerNameSet = " << peerNameSet);
85
86 Data identitySet;
87 setToData(identities, identitySet);
88 StackLog(<<"identitySet = " << identitySet);
89
90 Data command;
91 if(mTlsPeerAuthorizationQuery.empty())
92 {
93 DataStream ds(command);
94 ds << "SELECT count(1) FROM " << tableName(TlsPeerIdentityTable) << " WHERE peerName IN (" << peerNameSet << ") AND ";
95 ds << "authorizedIdentity IN (" << identitySet << ");";
96 }
97 else
98 {
99 command = mTlsPeerAuthorizationQuery;
100 command.replace("$peerNames", peerNameSet);
101 command.replace("$identities", identitySet);
102 }
103
104 if(singleResultQuery(command, ret) != 0 || ret.size() == 0)
105 {
106 return false;
107 }
108 int count = ret.front().convertInt();
109
110 DebugLog( << "Count is " << count);
111
112 return count > 0;
113}
114
115void
116SqlDb::dbEraseRecord(const Table table,
117 const resip::Data& pKey,
118 bool isSecondaryKey) // allows deleting records from a table that supports secondary keying using a secondary key
119{
120 Data command;
121 {
122 DataStream ds(command);
123 Data escapedKey;
124 ds << "DELETE FROM " << tableName(table);
125 if(isSecondaryKey)
126 {
127 ds << " WHERE attr2='" << escapeString(pKey, escapedKey) << "'";
128 }
129 else
130 {
131 ds << " WHERE attr='" << escapeString(pKey, escapedKey) << "'";
132 }
133 }
134 query(command);
135}
136
137bool
138SqlDb::dbCommitTransaction(const Table table)
139{
140 Data command("COMMIT");
141 return query(command) == 0;
142}
143
144bool
145SqlDb::dbRollbackTransaction(const Table table)
146{
147 Data command("ROLLBACK");
148 return query(command) == 0;
149}
150
151static const char userTable[] = "users";
152static const char tlsPeerIdentityTable[] = "tlsPeerIdentity";
153static const char routesavp[] = "routesavp";
154static const char aclsavp[] = "aclsavp";
155static const char configsavp[] = "configsavp";
156static const char staticregsavp[] = "staticregsavp";
157static const char filtersavp[] = "filtersavp";
158static const char siloavp[] = "siloavp";
159
160Data
162{
163 switch (table)
164 {
165 case UserTable:
166 return mTableNamePrefix + userTable;
168 return mTableNamePrefix + tlsPeerIdentityTable;
169 case RouteTable:
170 return mTableNamePrefix + routesavp;
171 case AclTable:
172 return mTableNamePrefix + aclsavp;
173 case ConfigTable:
174 return mTableNamePrefix + configsavp;
175 case StaticRegTable:
176 return mTableNamePrefix + staticregsavp;
177 case FilterTable:
178 return mTableNamePrefix + filtersavp;
179 case SiloTable:
180 return mTableNamePrefix + siloavp;
181 default:
182 resip_assert(0);
183 }
184 return 0;
185}
186
187/* ====================================================================
188 *
189 * Copyright (c) 2015 Daniel Pocock. All rights reserved.
190 *
191 * Redistribution and use in source and binary forms, with or without
192 * modification, are permitted provided that the following conditions
193 * are met:
194 *
195 * 1. Redistributions of source code must retain the above copyright
196 * notice, this list of conditions and the following disclaimer.
197 *
198 * 2. Redistributions in binary form must reproduce the above copyright
199 * notice, this list of conditions and the following disclaimer in
200 * the documentation and/or other materials provided with the
201 * distribution.
202 *
203 * 3. Neither the name of the author(s) nor the names of any contributors
204 * may be used to endorse or promote products derived from this software
205 * without specific prior written permission.
206 *
207 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS "AS IS" AND
208 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
210 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE
211 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
213 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
214 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
215 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
216 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
217 * SUCH DAMAGE.
218 *
219 * ====================================================================
220 */
221
static const Data sep("[]")
#define DebugLog(args_)
Definition Logger.hxx:128
#define StackLog(args_)
Definition Logger.hxx:125
#define resip_assert(x)
This file needs to work for both C and C++ code.
static const char staticregsavp[]
Definition SqlDb.cxx:156
static const char siloavp[]
Definition SqlDb.cxx:158
static const char configsavp[]
Definition SqlDb.cxx:155
static const char aclsavp[]
Definition SqlDb.cxx:154
static const char filtersavp[]
Definition SqlDb.cxx:157
static const char userTable[]
Definition SqlDb.cxx:151
static const char routesavp[]
Definition SqlDb.cxx:153
static const char tlsPeerIdentityTable[]
Definition SqlDb.cxx:152
virtual bool isAuthorized(const std::set< resip::Data > &peerNames, const std::set< resip::Data > &identities) const
Definition SqlDb.cxx:78
virtual void eraseUser(const Key &key)
Definition SqlDb.cxx:31
resip::Data tableName(Table table) const
Definition SqlDb.cxx:161
SqlDb(const resip::ConfigParse &config)
Definition SqlDb.cxx:24
virtual void eraseTlsPeerIdentity(const Key &key)
Definition SqlDb.cxx:43
virtual int singleResultQuery(const resip::Data &queryCommand, std::vector< resip::Data > &fields) const =0
void setToData(const std::set< resip::Data > &items, resip::Data &result, const resip::Data &sep=",", const char quote='\'') const
Definition SqlDb.cxx:55
resip::Data getConfigData(const resip::Data &name, const resip::Data &defaultValue, bool useDefaultIfEmpty=false) const
An iostream that operates on an existing Data.
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
int replace(const Data &match, const Data &target, int max=INT_MAX)
Replaces up to max occurrences of the bytes match with target.
Definition Data.cxx:1964
bool empty() const noexcept
Checks whether the Data is empty.
Definition Data.hxx:591
This Class is used to implement a primitive form of RPC using loose XML formatting.
dcm! – add UnusedChecking(_enum) below;