2004-10-22 08:24:40 +02:00
|
|
|
/*
|
|
|
|
* PROGRAM: Client/Server Common Code
|
|
|
|
* MODULE: ClumpletReader.h
|
|
|
|
* DESCRIPTION: Secure handling of clumplet buffers
|
|
|
|
*
|
|
|
|
* 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 Nickolay Samofatov
|
|
|
|
* for the Firebird Open Source RDBMS project.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004 Nickolay Samofatov <nickolay@broadviewsoftware.com>
|
|
|
|
* and all contributors signed below.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2004-10-23 03:21:11 +02:00
|
|
|
#ifndef CLUMPLETREADER_H
|
|
|
|
#define CLUMPLETREADER_H
|
2004-10-22 08:24:40 +02:00
|
|
|
|
|
|
|
#include "../common/classes/alloc.h"
|
|
|
|
#include "../common/classes/array.h"
|
2004-12-17 06:41:47 +01:00
|
|
|
#include "../common/classes/fb_string.h"
|
2004-10-22 08:24:40 +02:00
|
|
|
|
2005-11-30 18:11:23 +01:00
|
|
|
#define DEBUG_CLUMPLETS
|
2005-11-27 21:53:09 +01:00
|
|
|
#if defined(DEV_BUILD) && !defined(DEBUG_CLUMPLETS)
|
|
|
|
#define DEBUG_CLUMPLETS
|
|
|
|
#endif
|
|
|
|
|
2004-10-22 08:24:40 +02:00
|
|
|
namespace Firebird {
|
|
|
|
|
2004-11-03 09:38:09 +01:00
|
|
|
// This class provides read access for clumplet structure
|
2004-10-22 08:24:40 +02:00
|
|
|
// Note: it doesn't make a copy of buffer it reads
|
2004-12-17 06:41:47 +01:00
|
|
|
class ClumpletReader : protected AutoStorage
|
|
|
|
{
|
2004-10-22 08:24:40 +02:00
|
|
|
public:
|
2011-10-04 14:51:57 +02:00
|
|
|
enum Kind {EndOfList, Tagged, UnTagged, SpbAttach, SpbStart, Tpb, WideTagged, WideUnTagged, SpbSendItems, SpbReceiveItems};
|
2010-01-01 21:23:52 +01:00
|
|
|
|
2009-12-30 16:24:16 +01:00
|
|
|
struct KindList
|
|
|
|
{
|
|
|
|
Kind kind;
|
|
|
|
UCHAR tag;
|
|
|
|
};
|
2010-01-01 21:23:52 +01:00
|
|
|
|
2009-12-30 16:24:16 +01:00
|
|
|
struct SingleClumplet
|
|
|
|
{
|
|
|
|
UCHAR tag;
|
|
|
|
size_t size;
|
|
|
|
const UCHAR* data;
|
|
|
|
};
|
2005-11-27 21:53:09 +01:00
|
|
|
|
|
|
|
// Constructor prepares an object from plain PB
|
|
|
|
ClumpletReader(Kind k, const UCHAR* buffer, size_t buffLen);
|
2006-09-26 17:09:46 +02:00
|
|
|
ClumpletReader(MemoryPool& pool, Kind k, const UCHAR* buffer, size_t buffLen);
|
2011-10-04 14:51:57 +02:00
|
|
|
|
2009-12-30 16:24:16 +01:00
|
|
|
// Different versions of clumplets may have different kinds
|
|
|
|
ClumpletReader(const KindList* kl, const UCHAR* buffer, size_t buffLen, FPTR_VOID raise = NULL);
|
2010-01-22 15:55:11 +01:00
|
|
|
ClumpletReader(MemoryPool& pool, const KindList* kl, const UCHAR* buffer, size_t buffLen, FPTR_VOID raise = NULL);
|
2006-01-16 16:39:55 +01:00
|
|
|
virtual ~ClumpletReader() { }
|
2004-10-22 08:24:40 +02:00
|
|
|
|
2011-10-04 14:51:57 +02:00
|
|
|
// Create a copy of reader
|
|
|
|
ClumpletReader(MemoryPool& pool, const ClumpletReader& from);
|
|
|
|
|
2004-10-22 08:24:40 +02:00
|
|
|
// Navigation in clumplet buffer
|
2004-12-12 07:50:10 +01:00
|
|
|
bool isEof() const { return cur_offset >= getBufferLength(); }
|
2004-10-22 08:24:40 +02:00
|
|
|
void moveNext();
|
2004-12-09 20:19:47 +01:00
|
|
|
void rewind();
|
|
|
|
bool find(UCHAR tag);
|
2004-10-22 08:24:40 +02:00
|
|
|
|
|
|
|
// Methods which work with currently selected clumplet
|
2004-12-12 07:50:10 +01:00
|
|
|
UCHAR getClumpTag() const;
|
|
|
|
size_t getClumpLength() const;
|
2004-10-22 08:24:40 +02:00
|
|
|
|
2004-12-12 07:50:10 +01:00
|
|
|
SLONG getInt() const;
|
|
|
|
bool getBoolean() const;
|
|
|
|
SINT64 getBigInt() const;
|
|
|
|
string& getString(string& str) const;
|
|
|
|
PathName& getPath(PathName& str) const;
|
2005-11-27 21:53:09 +01:00
|
|
|
const UCHAR* getBytes() const;
|
2008-06-24 15:07:12 +02:00
|
|
|
double getDouble() const;
|
|
|
|
ISC_TIMESTAMP getTimeStamp() const;
|
|
|
|
ISC_TIME getTime() const { return getInt(); }
|
|
|
|
ISC_DATE getDate() const { return getInt(); }
|
2004-10-22 08:24:40 +02:00
|
|
|
|
2011-10-04 14:51:57 +02:00
|
|
|
// get the most generic representation of clumplet
|
|
|
|
SingleClumplet getClumplet() const;
|
|
|
|
|
2004-10-22 08:24:40 +02:00
|
|
|
// Return the tag for buffer (usually structure version)
|
2004-12-12 07:50:10 +01:00
|
|
|
UCHAR getBufferTag() const;
|
2011-08-09 14:11:17 +02:00
|
|
|
// true if buffer has tag
|
|
|
|
bool isTagged() const;
|
2008-12-05 01:56:15 +01:00
|
|
|
size_t getBufferLength() const
|
2004-12-09 20:19:47 +01:00
|
|
|
{
|
|
|
|
size_t rc = getBufferEnd() - getBuffer();
|
2007-04-21 05:27:36 +02:00
|
|
|
if (rc == 1 && kind != UnTagged && kind != SpbStart &&
|
2011-10-04 14:51:57 +02:00
|
|
|
kind != WideUnTagged && kind != SpbSendItems &&
|
|
|
|
kind != SpbReceiveItems)
|
2004-12-09 20:19:47 +01:00
|
|
|
{
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
2004-11-27 04:30:09 +01:00
|
|
|
size_t getCurOffset() const { return cur_offset; }
|
2004-11-03 09:38:09 +01:00
|
|
|
void setCurOffset(size_t newOffset) { cur_offset = newOffset; }
|
2008-12-05 01:56:15 +01:00
|
|
|
|
2005-11-27 21:53:09 +01:00
|
|
|
#ifdef DEBUG_CLUMPLETS
|
|
|
|
// Sometimes it's really useful to have it in case of errors
|
|
|
|
void dump() const;
|
|
|
|
#endif
|
|
|
|
|
2008-04-09 22:18:47 +02:00
|
|
|
// it is not exact comparison as clumplets may have same tags
|
2008-12-05 01:56:15 +01:00
|
|
|
// but in different order
|
2008-04-09 22:18:47 +02:00
|
|
|
bool simpleCompare(const ClumpletReader &other) const
|
|
|
|
{
|
|
|
|
const size_t len = getBufferLength();
|
2009-02-03 12:02:00 +01:00
|
|
|
return (len == other.getBufferLength()) && (memcmp(getBuffer(), other.getBuffer(), len) == 0);
|
2008-04-09 22:18:47 +02:00
|
|
|
}
|
|
|
|
|
2004-10-22 08:24:40 +02:00
|
|
|
protected:
|
2006-09-26 17:09:46 +02:00
|
|
|
enum ClumpletType {TraditionalDpb, SingleTpb, StringSpb, IntSpb, ByteSpb, Wide};
|
2005-11-27 21:53:09 +01:00
|
|
|
ClumpletType getClumpletType(UCHAR tag) const;
|
|
|
|
size_t getClumpletSize(bool wTag, bool wLength, bool wData) const;
|
|
|
|
void adjustSpbState();
|
2006-09-26 17:09:46 +02:00
|
|
|
|
2004-10-22 08:24:40 +02:00
|
|
|
size_t cur_offset;
|
2009-12-30 16:24:16 +01:00
|
|
|
Kind kind;
|
2005-11-27 21:53:09 +01:00
|
|
|
UCHAR spbState; // Reflects state of spb parser/writer
|
2004-10-22 08:24:40 +02:00
|
|
|
|
|
|
|
// Methods are virtual so writer can override 'em
|
2009-07-21 15:59:45 +02:00
|
|
|
virtual const UCHAR* getBuffer() const;
|
|
|
|
virtual const UCHAR* getBufferEnd() const;
|
2004-10-22 08:24:40 +02:00
|
|
|
|
2008-12-05 01:56:15 +01:00
|
|
|
// These functions are called when error condition is detected by this class.
|
2004-10-22 08:24:40 +02:00
|
|
|
// They may throw exceptions. If they don't reader tries to do something
|
|
|
|
// sensible, certainly not overwrite memory or read past the end of buffer
|
2004-11-03 09:38:09 +01:00
|
|
|
|
|
|
|
// This appears to be a programming error in buffer access pattern
|
2004-12-12 07:50:10 +01:00
|
|
|
virtual void usage_mistake(const char* what) const;
|
2004-11-03 09:38:09 +01:00
|
|
|
|
|
|
|
// This is called when passed buffer appears invalid
|
2005-11-30 18:11:23 +01:00
|
|
|
virtual void invalid_structure(const char* what) const;
|
2005-11-27 21:53:09 +01:00
|
|
|
|
2004-10-22 08:24:40 +02:00
|
|
|
private:
|
|
|
|
// Assignment and copy constructor not implemented.
|
|
|
|
ClumpletReader(const ClumpletReader& from);
|
|
|
|
ClumpletReader& operator=(const ClumpletReader& from);
|
|
|
|
|
|
|
|
const UCHAR* static_buffer;
|
|
|
|
const UCHAR* static_buffer_end;
|
2008-06-24 15:07:12 +02:00
|
|
|
|
2008-06-29 17:52:46 +02:00
|
|
|
static SINT64 fromVaxInteger(const UCHAR* ptr, size_t length);
|
2010-01-22 15:55:11 +01:00
|
|
|
void create(const KindList* kl, size_t buffLen, FPTR_VOID raise);
|
2009-12-30 16:24:16 +01:00
|
|
|
|
|
|
|
public:
|
2010-01-01 21:23:52 +01:00
|
|
|
static const KindList dpbList[]; // Some frequently used kind lists
|
2010-01-22 15:55:11 +01:00
|
|
|
static const KindList spbList[]; // Some frequently used kind lists
|
|
|
|
};
|
|
|
|
|
|
|
|
class AuthReader : public ClumpletReader
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef Array<UCHAR> AuthBlock;
|
2011-08-09 14:11:17 +02:00
|
|
|
|
|
|
|
// name and method are required attributes for any record
|
2011-08-13 19:07:01 +02:00
|
|
|
// name is set by plugin when it calls add()
|
2011-08-09 14:11:17 +02:00
|
|
|
static const unsigned char AUTH_NAME = 0;
|
|
|
|
// method is just a name of plugin which created this record
|
|
|
|
static const unsigned char AUTH_METHOD = 1;
|
|
|
|
|
|
|
|
// additional attributes - only security database is used currently
|
|
|
|
static const unsigned char AUTH_SECURE_DB = 100;
|
2010-01-22 15:55:11 +01:00
|
|
|
|
2010-01-26 09:20:27 +01:00
|
|
|
explicit AuthReader(const AuthBlock& authBlock);
|
2010-01-22 15:55:11 +01:00
|
|
|
|
2011-08-09 14:11:17 +02:00
|
|
|
bool getInfo(string* name, string* method, PathName* secDb);
|
2004-10-23 03:21:11 +02:00
|
|
|
};
|
2004-10-22 08:24:40 +02:00
|
|
|
|
|
|
|
} // namespace Firebird
|
|
|
|
|
2004-12-12 07:50:10 +01:00
|
|
|
#endif // CLUMPLETREADER_H
|