/
TraceService.cpp
408 lines (344 loc) · 9.46 KB
/
TraceService.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* MODULE: TraceService.cpp
* DESCRIPTION:
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Khorsun Vladyslav
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2008 Khorsun Vladyslav <hvlad@users.sourceforge.net>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*/
#include "firebird.h"
#include "consts_pub.h"
#include "fb_exception.h"
#include "iberror.h"
#include "../../common/classes/fb_string.h"
#include "../../common/classes/timestamp.h"
#include "../../common/config/config.h"
#include "../../common/StatusArg.h"
#include "../../common/ThreadStart.h"
#include "../../jrd/svc.h"
#include "../../common/os/guid.h"
#include "../../jrd/trace/TraceLog.h"
#include "../../jrd/trace/TraceManager.h"
#include "../../jrd/trace/TraceService.h"
#include "../../jrd/Mapping.h"
using namespace Firebird;
using namespace Jrd;
class TraceSvcJrd : public TraceSvcIntf
{
public:
explicit TraceSvcJrd(Service& svc) :
m_svc(svc),
m_admin(false),
m_chg_number(0)
{};
virtual ~TraceSvcJrd() {};
virtual void setAttachInfo(const string& service_name, const string& user, const string& pwd,
bool trusted);
virtual void startSession(TraceSession& session, bool interactive);
virtual void stopSession(ULONG id);
virtual void setActive(ULONG id, bool active);
virtual void listSessions();
private:
void readSession(TraceSession& session);
bool changeFlags(ULONG id, int setFlags, int clearFlags);
bool checkAliveAndFlags(ULONG sesId, int& flags);
// Check if current service is allowed to list\stop given other session
bool checkPrivileges(TraceSession& session);
Service& m_svc;
string m_user;
AuthReader::AuthBlock m_authBlock;
bool m_admin;
ULONG m_chg_number;
};
void TraceSvcJrd::setAttachInfo(const string& /*svc_name*/, const string& user, const string& /*pwd*/,
bool /*trusted*/)
{
const unsigned char* bytes;
unsigned int authBlockSize = m_svc.getAuthBlock(&bytes);
if (authBlockSize)
{
m_authBlock.add(bytes, authBlockSize);
m_user = "";
}
else
{
m_user = user;
m_admin = (m_user == SYSDBA_USER_NAME);
}
}
void TraceSvcJrd::startSession(TraceSession& session, bool interactive)
{
if (!TraceManager::pluginsCount())
{
m_svc.printf(false, "Can not start trace session. There are no trace plugins loaded\n");
return;
}
ConfigStorage* storage = TraceManager::getStorage();
{ // scope
StorageGuard guard(storage);
session.ses_auth = m_authBlock;
session.ses_user = m_user.hasData() ? m_user : m_svc.getUserName();
session.ses_flags = trs_active;
if (m_admin) {
session.ses_flags |= trs_admin;
}
if (interactive)
{
Guid guid;
GenerateGuid(&guid);
char* buff = session.ses_logfile.getBuffer(GUID_BUFF_SIZE);
GuidToString(buff, &guid);
session.ses_logfile.insert(0, "fb_trace.");
}
storage->addSession(session);
m_chg_number = storage->getChangeNumber();
}
m_svc.started();
m_svc.printf(false, "Trace session ID %ld started\n", session.ses_id);
if (interactive)
{
readSession(session);
{
StorageGuard guard(storage);
storage->removeSession(session.ses_id);
}
}
}
void TraceSvcJrd::stopSession(ULONG id)
{
m_svc.started();
ConfigStorage* storage = TraceManager::getStorage();
StorageGuard guard(storage);
storage->restart();
TraceSession session(*getDefaultMemoryPool());
while (storage->getNextSession(session))
{
if (id != session.ses_id)
continue;
if (checkPrivileges(session))
{
storage->removeSession(id);
m_svc.printf(false, "Trace session ID %ld stopped\n", id);
}
else
m_svc.printf(false, "No permissions to stop other user trace session\n");
return;
}
m_svc.printf(false, "Trace session ID %d not found\n", id);
}
void TraceSvcJrd::setActive(ULONG id, bool active)
{
if (active)
{
if (changeFlags(id, trs_active, 0)) {
m_svc.printf(false, "Trace session ID %ld resumed\n", id);
}
}
else
{
if (changeFlags(id, 0, trs_active)) {
m_svc.printf(false, "Trace session ID %ld paused\n", id);
}
}
}
bool TraceSvcJrd::changeFlags(ULONG id, int setFlags, int clearFlags)
{
ConfigStorage* storage = TraceManager::getStorage();
StorageGuard guard(storage);
storage->restart();
TraceSession session(*getDefaultMemoryPool());
while (storage->getNextSession(session))
{
if (id != session.ses_id)
continue;
if (checkPrivileges(session))
{
const int saveFlags = session.ses_flags;
session.ses_flags |= setFlags;
session.ses_flags &= ~clearFlags;
if (saveFlags != session.ses_flags) {
storage->updateSession(session);
}
return true;
}
m_svc.printf(false, "No permissions to change other user trace session\n");
return false;
}
m_svc.printf(false, "Trace session ID %d not found\n", id);
return false;
}
void TraceSvcJrd::listSessions()
{
m_svc.started();
ConfigStorage* storage = TraceManager::getStorage();
StorageGuard guard(storage);
storage->restart();
TraceSession session(*getDefaultMemoryPool());
while (storage->getNextSession(session))
{
if (checkPrivileges(session))
{
m_svc.printf(false, "\nSession ID: %d\n", session.ses_id);
if (!session.ses_name.empty()) {
m_svc.printf(false, " name: %s\n", session.ses_name.c_str());
}
m_svc.printf(false, " user: %s\n", session.ses_user.c_str());
struct tm* t = localtime(&session.ses_start);
m_svc.printf(false, " date: %04d-%02d-%02d %02d:%02d:%02d\n",
t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
t->tm_hour, t->tm_min, t->tm_sec);
string flags;
if (session.ses_flags & trs_active) {
flags = "active";
}
else {
flags = "suspend";
}
if (session.ses_flags & trs_admin) {
flags += ", admin";
}
if (session.ses_flags & trs_system) {
flags += ", system";
}
if (session.ses_logfile.empty()) {
flags += ", audit";
}
else {
flags += ", trace";
}
if (session.ses_flags & trs_log_full) {
flags += ", log full";
}
m_svc.printf(false, " flags: %s\n", flags.c_str());
}
}
}
void TraceSvcJrd::readSession(TraceSession& session)
{
const size_t maxLogSize = Config::getMaxUserTraceLogSize(); // in MB
if (session.ses_logfile.empty())
{
m_svc.printf(false, "Can't open trace data log file");
return;
}
MemoryPool& pool = *getDefaultMemoryPool();
AutoPtr<TraceLog> log(FB_NEW_POOL(pool) TraceLog(pool, session.ses_logfile, true));
UCHAR buff[1024];
int flags = session.ses_flags;
while (!m_svc.finished() && checkAliveAndFlags(session.ses_id, flags))
{
const FB_SIZE_T len = log->read(buff, sizeof(buff));
if (!len)
{
if (!checkAliveAndFlags(session.ses_id, flags))
break;
if (m_svc.svc_detach_sem.tryEnter(0, 250))
break;
}
else
{
m_svc.putBytes(buff, len);
const bool logFull = (flags & trs_log_full);
if (logFull && log->getApproxLogSize() <= maxLogSize)
{
// resume session
changeFlags(session.ses_id, 0, trs_log_full);
}
}
}
}
bool TraceSvcJrd::checkAliveAndFlags(ULONG sesId, int& flags)
{
ConfigStorage* storage = TraceManager::getStorage();
bool alive = (m_chg_number == storage->getChangeNumber());
if (!alive)
{
// look if our session still alive
StorageGuard guard(storage);
TraceSession readSession(*getDefaultMemoryPool());
storage->restart();
while (storage->getNextSession(readSession))
{
if (readSession.ses_id == sesId)
{
alive = true;
flags = readSession.ses_flags;
break;
}
}
m_chg_number = storage->getChangeNumber();
}
return alive;
}
bool TraceSvcJrd::checkPrivileges(TraceSession& session)
{
// Our service run in embedded mode and have no auth info - trust user name as is
if (m_admin || m_user.hasData() && (m_user == session.ses_user))
return true;
// Other session is fully authorized - try to map our auth info using other's
// security database
if (session.ses_auth.hasData())
{
AuthReader::Info info;
for (AuthReader rdr(session.ses_auth); rdr.getInfo(info); rdr.moveNext())
{
const char* secDb = info.secDb.hasData() ? info.secDb.c_str() :
Config::getDefaultConfig()->getSecurityDatabase();
string s_user, t_role;
try
{
mapUser(s_user, t_role, NULL, NULL, m_authBlock, "services manager",
NULL, secDb, m_svc.getCryptCallback(), NULL);
}
catch (const Firebird::Exception&)
{
// Error in mapUser() means missing context, therefore...
continue;
}
t_role.upper();
if (s_user == SYSDBA_USER_NAME || t_role == ADMIN_ROLE || s_user == session.ses_user)
return true;
}
}
// Other session's service run in embedded mode - check our user name as is
else if (m_svc.getUserName() == session.ses_user || m_svc.getUserAdminFlag())
return true;
return false;
}
// service entrypoint
int TRACE_main(UtilSvc* arg)
{
Service* svc = (Service*) arg;
int exit_code = FB_SUCCESS;
TraceSvcJrd traceSvc(*svc);
try
{
fbtrace(svc, &traceSvc);
}
catch (const Exception& e)
{
StaticStatusVector status;
e.stuffException(status);
svc->initStatus();
svc->setServiceStatus(status.begin());
exit_code = FB_FAILURE;
}
return exit_code;
}