changed: when winix demonizes it creates a three new descriptors (0, 1 and 3)

pointing to /dev/null
added:   DbBase::AssertValueBin(PGresult * r, int row, int col, std::string & result)
         it reads binary (bytea) data
added:   DbTextStream can handle 'bool' types now
         (is puts 'true' of 'false' to the stream)
changed: now passwords can be stored either as plain text, a hash or can be encrypted
         with RSA
         currently we have following hashes:
         md4, md5, sha1, sha224, sha256, sha384, sha512
         we are using openssl to manage them
         (look at config options for more info)
changed: winix version to 0.4.7         
added:   class Run - you can run any program from os and send a buffer to its standard input
         and read what the program put on its standard output
added:   class Crypt (in System) - calculating hashes, and crypting/decrypting




git-svn-id: svn://ttmath.org/publicrep/winix/trunk@734 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
2011-06-09 21:22:08 +00:00
parent af8fbdae72
commit 18ecd46a01
33 changed files with 2022 additions and 583 deletions

View File

@@ -2,7 +2,7 @@
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2008-2010, Tomasz Sowa
* Copyright (c) 2008-2011, Tomasz Sowa
* All rights reserved.
*
*/
@@ -15,18 +15,17 @@
bool Db::CheckUser(const std::wstring & login, const std::wstring & password, long & user_id)
bool Db::GetUserPass(const std::wstring & login, long & user_id, UserPass & up)
{
PGresult * r = 0;
bool user_ok = false;
user_id = -1;
try
{
query.Clear();
query << R("select id from core.user where login=")
query << R("select id, password, pass_encrypted, pass_type, pass_hash_salted from core.user where login=")
<< login
<< R(" and password=")
<< password
<< R(";");
r = AssertQuery(query);
@@ -39,13 +38,22 @@ bool Db::CheckUser(const std::wstring & login, const std::wstring & password, lo
if( rows > 1 )
{
log << log1 << "Db: there is more than one user: " << login << " (with the same password)" << logend;
log << log1 << "Db: there is more than one user: " << login << logend;
throw Error(WINIX_ERR_DB_MORE_THAN_ONE_LOGIN);
}
int cuser_id = AssertColumn(r, "id");
user_id = AssertValueLong(r, 0, cuser_id);
int cuser_id = AssertColumn(r, "id");
int cpass_type = AssertColumn(r, "pass_type");
int csalted = AssertColumn(r, "pass_hash_salted");
int cpassword = AssertColumn(r, "password");
int cpass_encrypted = AssertColumn(r, "pass_encrypted");
user_ok = true;
user_id = AssertValueLong(r, 0, cuser_id);
up.pass_type = AssertValueInt(r, 0, cpass_type);
up.pass_hash_salted = AssertValueBool(r, 0, csalted);
AssertValueWide(r, 0, cpassword, up.pass);
AssertValueBin(r, 0, cpass_encrypted, up.pass_encrypted);
}
catch(const Error &)
{
@@ -60,7 +68,8 @@ return user_ok;
Error Db::AddUser(User & user, const std::wstring & password)
Error Db::AddUser(User & user, const std::wstring & password, const std::string & password_encrypted,
int pass_type, bool pass_hash_salted)
{
PGresult * r = 0;
Error status = WINIX_ERR_OK;
@@ -68,12 +77,23 @@ Error Db::AddUser(User & user, const std::wstring & password)
try
{
query.Clear();
query << R("insert into core.user (login, password, super_user, email, notify) values (")
<< user.name
<< password
<< static_cast<int>(user.super_user)
query << R("insert into core.user (login, password, pass_encrypted, super_user, email,"
"notify, pass_type, pass_hash_salted) values (")
<< user.name;
// for safety
if( password_encrypted.empty() )
query << password;
else
query << "";
query.EPutBin(password_encrypted);
query << user.super_user
<< user.email
<< user.notify
<< pass_type
<< pass_hash_salted
<< R(");");
r = AssertQuery(query);

View File

@@ -2,7 +2,7 @@
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2008-2010, Tomasz Sowa
* Copyright (c) 2008-2011, Tomasz Sowa
* All rights reserved.
*
*/
@@ -41,8 +41,8 @@ public:
bool CheckUser(const std::wstring & login, const std::wstring & password, long & user_id);
Error AddUser(User & user, const std::wstring & password);
bool GetUserPass(const std::wstring & login, long & user_id, UserPass & up);
Error AddUser(User & user, const std::wstring & password, const std::string & password_encrypted, int pass_type, bool pass_hash_salted);
Error AddItem(Item & item);
Error EditItemById(Item & item, bool with_url = true);

View File

@@ -2,7 +2,7 @@
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* Copyright (c) 2010-2011, Tomasz Sowa
* All rights reserved.
*
*/
@@ -151,6 +151,21 @@ return temp_wide_value;
}
void DbBase::AssertValueBin(PGresult * r, int row, int col, std::string & result)
{
result.clear();
const char * res = AssertValue(r, row, col);
int len = PQgetlength(r, row, col);
if( len <= 0 )
return;
UnescapeBin(res, len, result);
}
void DbBase::AssertValueWide(PGresult * r, int row, int col, std::wstring & result)
{
const char * res = AssertValue(r, row, col);
@@ -172,7 +187,8 @@ int DbBase::AssertValueInt(PGresult * r, int row, int col)
bool DbBase::AssertValueBool(PGresult * r, int row, int col)
{
return strtol( AssertValue(r, row, col), 0, 10 ) != 0;
const char * s = AssertValue(r, row, col);
return (s[0]=='t' || s[0]=='y' || s[0]=='1');
}
@@ -429,3 +445,85 @@ Error DbBase::EndTrans(Error err)
return err;
}
/*
converting from a bytea
*/
int DbBase::CharToInt(char c)
{
return (int)(unsigned char)(c-'0');
}
bool DbBase::IsCorrectOctalDigit(char c)
{
return c>='0' && c<='7';
}
// moves 'i' at least once
// return -1 if there is en error
int DbBase::UnescapeBin(const char * str, size_t & i, size_t len)
{
if( str[i] != '\\' )
return str[i++];
i += 1;
if( i >= len )
return -1;
if( str[i] == '\\' )
return str[i++];
if( i+2 >= len )
{
i = len;
return -1;
}
if( !IsCorrectOctalDigit(str[i]) ||
!IsCorrectOctalDigit(str[i+1]) ||
!IsCorrectOctalDigit(str[i+2]) )
{
i += 3;
return -1;
}
int c = 8*8*CharToInt(str[i]) + 8*CharToInt(str[i+1]) + CharToInt(str[i+2]);
i += 3;
if( c<0 || c>255 )
return -1;
return c;
}
void DbBase::UnescapeBin(const char * str, size_t len, std::string & out, bool clear_out)
{
int c;
size_t i = 0;
if( clear_out )
out.clear();
while( i < len )
{
c = UnescapeBin(str, i, len);
if( c != -1 )
out += c;
}
}
/*
end of converting from bytea
*/

View File

@@ -2,7 +2,7 @@
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* Copyright (c) 2010-2011, Tomasz Sowa
* All rights reserved.
*
*/
@@ -39,6 +39,7 @@ public:
static const char * AssertValue(PGresult * r, int row, int col);
static const std::wstring & AssertValueWide(PGresult * r, int row, int col); // warning: this method uses a static buffer
static void AssertValueWide(PGresult * r, int row, int col, std::wstring & result);
static void AssertValueBin(PGresult * r, int row, int col, std::string & result);
static long AssertValueLong(PGresult * r, int row, int col);
static int AssertValueInt(PGresult * r, int row, int col);
static bool AssertValueBool(PGresult * r, int row, int col);
@@ -65,6 +66,9 @@ public:
Error CommitTrans();
Error EndTrans(Error err);
static void UnescapeBin(const char * str, size_t len, std::string & out, bool clear_out = true);
protected:
// a helper method for escaping strings
@@ -77,10 +81,14 @@ protected:
DbConn * db_conn;
bool log_queries;
private:
DbTextStream bquery;
static int CharToInt(char c);
static bool IsCorrectOctalDigit(char c);
static int UnescapeBin(const char * str, size_t & i, size_t len);
DbTextStream bquery;
};

View File

@@ -2,7 +2,7 @@
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* Copyright (c) 2010-2011, Tomasz Sowa
* All rights reserved.
*
*/
@@ -111,6 +111,17 @@ DbTextStream & DbTextStream::operator<<(RawText<std::wstring> raw)
}
DbTextStream & DbTextStream::operator<<(RawText<bool> raw)
{
if( raw.par )
PutText("true");
else
PutText("false");
return *this;
}
DbTextStream & DbTextStream::operator<<(RawText<char> raw)
{
TextStream<std::string>::operator<<(raw.par);
@@ -201,14 +212,13 @@ return *this;
*/
// !! sprawdzic jej dzialanie dla kolumn bytea (binarnych)
DbTextStream & DbTextStream::EBinPutChar(char c_)
{
char buf[10];
char buf[20];
int c = (unsigned char)c_;
if( (c>=0 && c<=31) || c>=127 || c==39 || c==92 )
if( (c>=0 && c<=31) || c>=127 || c=='\'' || c=='\\' )
{
sprintf(buf, "\\\\%03o", c);
buffer += buf;
@@ -391,6 +401,16 @@ DbTextStream & DbTextStream::operator<<(const std::wstring & str)
DbTextStream & DbTextStream::operator<<(bool v)
{
if( v )
EPutText("true");
else
EPutText("false");
return *this;
}
DbTextStream & DbTextStream::operator<<(char v)
{

View File

@@ -2,7 +2,7 @@
* This file is a part of Winix
* and is not publicly distributed
*
* Copyright (c) 2010, Tomasz Sowa
* Copyright (c) 2010-2011, Tomasz Sowa
* All rights reserved.
*
*/
@@ -104,6 +104,7 @@ public:
DbTextStream & operator<<(RawText<const std::wstring*> raw);
DbTextStream & operator<<(RawText<std::string> raw);
DbTextStream & operator<<(RawText<std::wstring> raw);
DbTextStream & operator<<(RawText<bool> raw);
DbTextStream & operator<<(RawText<char> raw);
DbTextStream & operator<<(RawText<wchar_t> raw);
DbTextStream & operator<<(RawText<int> raw);
@@ -140,6 +141,7 @@ public:
DbTextStream & operator<<(const wchar_t * str);
DbTextStream & operator<<(const std::wstring * str);
DbTextStream & operator<<(const std::wstring & str);
DbTextStream & operator<<(bool);
DbTextStream & operator<<(char);
DbTextStream & operator<<(wchar_t);
DbTextStream & operator<<(int);