Commit 278dab96 authored by claes's avatar claes

New user database layout and new encryption of password

parent 57b2dd6f
/** /**
* Proview $Id: co_user.cpp,v 1.8 2005-11-22 12:19:47 claes Exp $ * Proview $Id: co_user.cpp,v 1.9 2008-06-24 07:04:33 claes Exp $
* Copyright (C) 2005 SSAB Oxelsund AB. * Copyright (C) 2005 SSAB Oxelsund AB.
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <float.h> #include <float.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include "co_user.h" #include "co_user.h"
...@@ -79,25 +80,28 @@ int GeUser::load( char *filename) ...@@ -79,25 +80,28 @@ int GeUser::load( char *filename)
return USER__FILEOPEN; return USER__FILEOPEN;
#endif #endif
for (;;) for (;;) {
{
fp >> type; fp >> type;
switch( type) { switch( type) {
case user_eData_GeUser: break; case user_eData_GeUser: break;
case user_eData_GeUserVersion: case user_eData_GeUserVersion:
fp.get(); fp.get();
fp.getline( version, sizeof(version)); fp.getline( version, sizeof(version));
break; if ( strcmp( version, user_cVersion) != 0) {
case user_eData_System: return USER__DBVERSION;
load_system( fp); break; }
case user_eData_End: end_found = 1; break; break;
default: case user_eData_GeNextId: fp >> next_id; break;
cout << "GeUser:open syntax error" << endl; case user_eData_System:
fp.getline( dummy, sizeof(dummy)); load_system( fp); break;
} case user_eData_End: end_found = 1; break;
if ( end_found) default:
break; cout << "GeUser:open syntax error" << endl;
fp.getline( dummy, sizeof(dummy));
}
if ( end_found)
break;
} }
fp.close(); fp.close();
...@@ -118,6 +122,7 @@ int GeUser::save( char *filename) ...@@ -118,6 +122,7 @@ int GeUser::save( char *filename)
fp << int(user_eData_GeUser) << endl; fp << int(user_eData_GeUser) << endl;
fp << int(user_eData_GeUserVersion) << FSPACE << user_cVersion << endl; fp << int(user_eData_GeUserVersion) << FSPACE << user_cVersion << endl;
fp << int(user_eData_GeNextId) << FSPACE << next_id << endl;
for ( sl = root; sl != NULL; sl = sl->next) for ( sl = root; sl != NULL; sl = sl->next)
sl->save( fp); sl->save( fp);
...@@ -130,7 +135,7 @@ int GeUser::save( char *filename) ...@@ -130,7 +135,7 @@ int GeUser::save( char *filename)
int GeUser::load_system( ifstream& fp) int GeUser::load_system( ifstream& fp)
{ {
// Insert // Insert
SystemList *system_list = new SystemList("", 0, 0); SystemList *system_list = new SystemList(0, "", 0, 0, 0);
SystemList *sl = root; SystemList *sl = root;
if ( !sl) if ( !sl)
root = system_list; root = system_list;
...@@ -143,28 +148,31 @@ int GeUser::load_system( ifstream& fp) ...@@ -143,28 +148,31 @@ int GeUser::load_system( ifstream& fp)
return 1; return 1;
} }
int GeUser::add_system( char *name, unsigned int attributes) int GeUser::add_system( char *name, pwr_tMask attributes, char *description, pwr_tOix id)
{ {
int sts; int sts;
SystemName *sn = new SystemName( name); SystemName *sn = new SystemName( name);
sts = sn->parse(); sts = sn->parse();
if ( EVEN(sts)) if ( EVEN(sts)) {
{
delete sn; delete sn;
return sts; return sts;
} }
if ( id == user_cNId)
id = get_next_id();
else if ( id >= next_id)
next_id = id + 1;
SystemList *sl = find_system( sn); SystemList *sl = find_system( sn);
if ( sl) if ( sl)
return USER__SYSTEMALREXIST; return USER__SYSTEMALREXIST;
SystemName *parent = sn->parent(); SystemName *parent = sn->parent();
if ( !parent) if ( !parent) {
{
SystemList *sl; SystemList *sl;
SystemList *system_list = new SystemList(name, 0, attributes); SystemList *system_list = new SystemList( id, name, 0, attributes, description);
// Insert as last sibling to root // Insert as last sibling to root
if ( !root) if ( !root)
root = system_list; root = system_list;
...@@ -173,8 +181,7 @@ int GeUser::add_system( char *name, unsigned int attributes) ...@@ -173,8 +181,7 @@ int GeUser::add_system( char *name, unsigned int attributes)
sl->next = system_list; sl->next = system_list;
} }
} }
else else {
{
parent->parse(); parent->parse();
SystemList *sl = find_system( parent); SystemList *sl = find_system( parent);
if ( !sl) if ( !sl)
...@@ -183,7 +190,7 @@ int GeUser::add_system( char *name, unsigned int attributes) ...@@ -183,7 +190,7 @@ int GeUser::add_system( char *name, unsigned int attributes)
delete parent; delete parent;
return USER__NOSUCHSYSTEM; return USER__NOSUCHSYSTEM;
} }
sl->add_system( sn, attributes); sl->add_system( id, sn, attributes, description);
delete parent; delete parent;
} }
delete sn; delete sn;
...@@ -245,7 +252,7 @@ int GeUser::remove_system( char *name) ...@@ -245,7 +252,7 @@ int GeUser::remove_system( char *name)
return USER__NOSUCHSYSTEM; return USER__NOSUCHSYSTEM;
} }
int GeUser::modify_system( char *name, unsigned int attributes) int GeUser::modify_system( char *name, pwr_tMask attributes, char *description)
{ {
int sts; int sts;
...@@ -264,11 +271,11 @@ int GeUser::modify_system( char *name, unsigned int attributes) ...@@ -264,11 +271,11 @@ int GeUser::modify_system( char *name, unsigned int attributes)
return USER__NOSUCHSYSTEM; return USER__NOSUCHSYSTEM;
} }
sl->modify( attributes); sl->modify( attributes, description);
return USER__SUCCESS; return USER__SUCCESS;
} }
int GeUser::get_system_data( char *name, unsigned int *attributes) int GeUser::get_system_data( char *name, pwr_tMask *attributes, pwr_tOix *id, char *description)
{ {
int sts; int sts;
...@@ -287,7 +294,7 @@ int GeUser::get_system_data( char *name, unsigned int *attributes) ...@@ -287,7 +294,7 @@ int GeUser::get_system_data( char *name, unsigned int *attributes)
return USER__NOSUCHSYSTEM; return USER__NOSUCHSYSTEM;
} }
sl->get_data( attributes); sl->get_data( attributes, id, description);
return USER__SUCCESS; return USER__SUCCESS;
} }
...@@ -314,7 +321,8 @@ int GeUser::remove_user( char *system, char *user) ...@@ -314,7 +321,8 @@ int GeUser::remove_user( char *system, char *user)
return sts; return sts;
} }
int GeUser::modify_user( char *system, char *user, char *password, unsigned int priv) int GeUser::modify_user( char *system, char *user, char *password, unsigned int priv, char *fullname,
char *description, char *email, char *phone, char *sms)
{ {
int sts; int sts;
UserList *ul; UserList *ul;
...@@ -335,13 +343,14 @@ int GeUser::modify_user( char *system, char *user, char *password, unsigned int ...@@ -335,13 +343,14 @@ int GeUser::modify_user( char *system, char *user, char *password, unsigned int
ul = (UserList *) sl->find_user( user); ul = (UserList *) sl->find_user( user);
if ( !ul) if ( !ul)
return USER__NOSUCHUSER; return USER__NOSUCHUSER;
ul->modify( password, priv); ul->modify( password, priv, fullname, description, email, phone, sms);
return USER__SUCCESS; return USER__SUCCESS;
} }
int GeUser::get_user_data( char *system, char *user, char *password, int GeUser::get_user_data( char *system, char *user, char *password,
unsigned int *priv) pwr_tMask *priv, pwr_tOix *id, char *fullname, char *description, char *email,
char *phone, char *sms)
{ {
int sts; int sts;
UserList *ul; UserList *ul;
...@@ -362,7 +371,7 @@ int GeUser::get_user_data( char *system, char *user, char *password, ...@@ -362,7 +371,7 @@ int GeUser::get_user_data( char *system, char *user, char *password,
ul = (UserList *) sl->find_user( user); ul = (UserList *) sl->find_user( user);
if ( !ul) if ( !ul)
return USER__NOSUCHUSER; return USER__NOSUCHUSER;
ul->get_data( password, priv); ul->get_data( password, priv, id, fullname, description, email, phone, sms);
return USER__SUCCESS; return USER__SUCCESS;
} }
...@@ -383,7 +392,8 @@ SystemList *GeUser::find_system( SystemName *name) ...@@ -383,7 +392,8 @@ SystemList *GeUser::find_system( SystemName *name)
return NULL; return NULL;
} }
int GeUser::add_user( char *system, char *user, char *password, unsigned int priv) int GeUser::add_user( char *system, char *user, char *password, pwr_tMask priv, char *fullname,
char *description, char *email, char *phone, char *sms, pwr_tOix id)
{ {
SystemList *sl; SystemList *sl;
int sts; int sts;
...@@ -399,7 +409,12 @@ int GeUser::add_user( char *system, char *user, char *password, unsigned int pri ...@@ -399,7 +409,12 @@ int GeUser::add_user( char *system, char *user, char *password, unsigned int pri
sl = find_system( sn); sl = find_system( sn);
if ( !sl) return USER__NOSUCHSYSTEM; if ( !sl) return USER__NOSUCHSYSTEM;
sl->add_user( user, password, priv); if ( id == user_cNId)
id = get_next_id();
else if ( id >= next_id)
next_id = id + 1;
sl->add_user( id, user, password, priv, fullname, description, email, phone, sms);
return USER__SUCCESS; return USER__SUCCESS;
} }
...@@ -415,7 +430,7 @@ int GeUser::get_user( char *gu_system, char *gu_user, char *gu_password, unsigne ...@@ -415,7 +430,7 @@ int GeUser::get_user( char *gu_system, char *gu_user, char *gu_password, unsigne
cdh_ToLower( system, gu_system); cdh_ToLower( system, gu_system);
cdh_ToLower( user, gu_user); cdh_ToLower( user, gu_user);
cdh_ToLower( password, gu_password); strcpy( password, gu_password);
// Find system // Find system
sn = new SystemName( system); sn = new SystemName( system);
...@@ -497,23 +512,23 @@ int GeUser::get_user_priv( char *gu_system, char *gu_user, unsigned int *priv) ...@@ -497,23 +512,23 @@ int GeUser::get_user_priv( char *gu_system, char *gu_user, unsigned int *priv)
return USER__NOSUCHUSER; return USER__NOSUCHUSER;
} }
void GeUser::print() void GeUser::print( int brief)
{ {
SystemList *sl; SystemList *sl;
cout << endl << "Proview user database " << user_cVersion << endl << endl; cout << endl << "Proview user database " << user_cVersion << endl << endl;
for ( sl = root; sl != NULL; sl = sl->next) for ( sl = root; sl != NULL; sl = sl->next)
sl->print(); sl->print( brief);
cout << endl; cout << endl;
} }
void GeUser::print_all() void GeUser::print_all( int brief)
{ {
SystemList *sl; SystemList *sl;
cout << endl << "Proview user database " << user_cVersion << endl << endl; cout << endl << "Proview user database " << user_cVersion << endl << endl;
for ( sl = root; sl != NULL; sl = sl->next) for ( sl = root; sl != NULL; sl = sl->next)
sl->print_all(); sl->print_all( brief);
cout << endl; cout << endl;
} }
...@@ -553,6 +568,11 @@ int SystemList::load( ifstream& fp) ...@@ -553,6 +568,11 @@ int SystemList::load( ifstream& fp)
break; break;
case user_eData_SystemLevel: fp >> level; break; case user_eData_SystemLevel: fp >> level; break;
case user_eData_SystemAttributes: fp >> attributes; break; case user_eData_SystemAttributes: fp >> attributes; break;
case user_eData_SystemId: fp >> id; break;
case user_eData_SystemDescription:
fp.get();
fp.getline( description, sizeof(description));
break;
case user_eData_User: case user_eData_User:
load_user( fp); break; load_user( fp); break;
case user_eData_System: case user_eData_System:
...@@ -571,7 +591,7 @@ int SystemList::load( ifstream& fp) ...@@ -571,7 +591,7 @@ int SystemList::load( ifstream& fp)
int SystemList::load_user( ifstream& fp) int SystemList::load_user( ifstream& fp)
{ {
// Insert // Insert
UserList *user_list = new UserList("", "", 0); UserList *user_list = new UserList(0, "", "", 0, 0, 0, 0, 0, 0);
UserList *ul = userlist; UserList *ul = userlist;
if ( !ul) if ( !ul)
userlist = user_list; userlist = user_list;
...@@ -587,7 +607,7 @@ int SystemList::load_user( ifstream& fp) ...@@ -587,7 +607,7 @@ int SystemList::load_user( ifstream& fp)
int SystemList::load_system( ifstream& fp) int SystemList::load_system( ifstream& fp)
{ {
// Insert // Insert
SystemList *system_list = new SystemList("", 0, 0); SystemList *system_list = new SystemList( 0, "", 0, 0, 0);
SystemList *sl = childlist; SystemList *sl = childlist;
if ( !sl) if ( !sl)
childlist = system_list; childlist = system_list;
...@@ -609,6 +629,8 @@ void SystemList::save( ofstream& fp) ...@@ -609,6 +629,8 @@ void SystemList::save( ofstream& fp)
fp << int(user_eData_SystemName) << FSPACE << name << endl; fp << int(user_eData_SystemName) << FSPACE << name << endl;
fp << int(user_eData_SystemLevel) << FSPACE << level << endl; fp << int(user_eData_SystemLevel) << FSPACE << level << endl;
fp << int(user_eData_SystemAttributes) << FSPACE << attributes << endl; fp << int(user_eData_SystemAttributes) << FSPACE << attributes << endl;
fp << int(user_eData_SystemId) << FSPACE << id << endl;
fp << int(user_eData_SystemDescription) << FSPACE << description << endl;
for ( ul = (UserList *) userlist; ul != NULL; ul = ul->next) for ( ul = (UserList *) userlist; ul != NULL; ul = ul->next)
ul->save( fp); ul->save( fp);
for ( sl = childlist; sl; sl = sl->next) for ( sl = childlist; sl; sl = sl->next)
...@@ -616,7 +638,7 @@ void SystemList::save( ofstream& fp) ...@@ -616,7 +638,7 @@ void SystemList::save( ofstream& fp)
fp << int(user_eData_End) << endl; fp << int(user_eData_End) << endl;
} }
void SystemList::print() void SystemList::print( int brief)
{ {
UserList *ul; UserList *ul;
SystemList *sl; SystemList *sl;
...@@ -632,12 +654,12 @@ void SystemList::print() ...@@ -632,12 +654,12 @@ void SystemList::print()
cout << "UserInherit"; cout << "UserInherit";
cout << endl; cout << endl;
for ( ul = (UserList *)userlist; ul; ul = ul->next) for ( ul = (UserList *)userlist; ul; ul = ul->next)
ul->print(); ul->print( brief);
for ( sl = childlist; sl; sl = sl->next) for ( sl = childlist; sl; sl = sl->next)
sl->print(); sl->print( brief);
} }
void SystemList::print_all() void SystemList::print_all( int brief)
{ {
UserList *ul; UserList *ul;
SystemList *sl; SystemList *sl;
...@@ -653,9 +675,9 @@ void SystemList::print_all() ...@@ -653,9 +675,9 @@ void SystemList::print_all()
cout << "UserInherit"; cout << "UserInherit";
cout << endl; cout << endl;
for ( ul = (UserList *)userlist; ul; ul = ul->next) for ( ul = (UserList *)userlist; ul; ul = ul->next)
ul->print_all(); ul->print_all( brief);
for ( sl = childlist; sl; sl = sl->next) for ( sl = childlist; sl; sl = sl->next)
sl->print_all(); sl->print_all( brief);
} }
SystemList *SystemList::find_system( SystemName *name) SystemList *SystemList::find_system( SystemName *name)
...@@ -686,7 +708,8 @@ void *SystemList::find_user( char *name) ...@@ -686,7 +708,8 @@ void *SystemList::find_user( char *name)
return NULL; return NULL;
} }
int SystemList::add_user( char *user, char *password, unsigned int priv) int SystemList::add_user( pwr_tOix ident, char *user, char *password, pwr_tMask priv, char *fullname, char *description,
char *email, char *phone, char *sms)
{ {
UserList *ul; UserList *ul;
...@@ -694,7 +717,7 @@ int SystemList::add_user( char *user, char *password, unsigned int priv) ...@@ -694,7 +717,7 @@ int SystemList::add_user( char *user, char *password, unsigned int priv)
ul = (UserList *) find_user( user); ul = (UserList *) find_user( user);
if ( ul) return USER__USERALREXIST; if ( ul) return USER__USERALREXIST;
ul = new UserList( user, password, priv); ul = new UserList( ident, user, password, priv, fullname, description, email, phone, sms);
UserList *u = userlist; UserList *u = userlist;
if ( !u) if ( !u)
userlist = ul; userlist = ul;
...@@ -706,7 +729,7 @@ int SystemList::add_user( char *user, char *password, unsigned int priv) ...@@ -706,7 +729,7 @@ int SystemList::add_user( char *user, char *password, unsigned int priv)
return USER__SUCCESS; return USER__SUCCESS;
} }
int SystemList::add_system( SystemName *name, unsigned int attributes) int SystemList::add_system( pwr_tOix ident, SystemName *name, pwr_tMask attributes, char *description)
{ {
SystemList *sl; SystemList *sl;
...@@ -714,7 +737,7 @@ int SystemList::add_system( SystemName *name, unsigned int attributes) ...@@ -714,7 +737,7 @@ int SystemList::add_system( SystemName *name, unsigned int attributes)
sl = find_system( name); sl = find_system( name);
if ( sl) return USER__SYSTEMALREXIST; if ( sl) return USER__SYSTEMALREXIST;
sl = new SystemList( name->segment(level+1), level + 1, attributes); sl = new SystemList( ident, name->segment(level+1), level + 1, attributes, description);
SystemList *s = childlist; SystemList *s = childlist;
if ( !s) if ( !s)
childlist = sl; childlist = sl;
...@@ -769,14 +792,18 @@ int SystemList::remove_user( char *user) ...@@ -769,14 +792,18 @@ int SystemList::remove_user( char *user)
return USER__NOSUCHUSER; return USER__NOSUCHUSER;
} }
void SystemList::modify( unsigned int attributes) void SystemList::modify( unsigned int attributes, char *description)
{ {
this->attributes = attributes; this->attributes = attributes;
if ( description)
strncpy( this->description, description, sizeof(this->description));
} }
void SystemList::get_data( unsigned int *attributes) void SystemList::get_data( pwr_tMask *attributes, pwr_tOix *id, char *description)
{ {
*attributes = this->attributes; *attributes = this->attributes;
*id = this->id;
strcpy( description, this->description);
} }
int UserList::load( ifstream& fp) int UserList::load( ifstream& fp)
...@@ -798,9 +825,29 @@ int UserList::load( ifstream& fp) ...@@ -798,9 +825,29 @@ int UserList::load( ifstream& fp)
case user_eData_UserPassword: case user_eData_UserPassword:
fp.get(); fp.get();
fp.getline( password, sizeof(password)); fp.getline( password, sizeof(password));
strcpy( password, decrypt( password));
break; break;
case user_eData_UserPrivilege: fp >> priv; priv = decrypt( priv); break; case user_eData_UserPrivilege: fp >> priv; priv = idecrypt( priv); break;
case user_eData_UserId: fp >> id; break;
case user_eData_UserFullName:
fp.get();
fp.getline( fullname, sizeof(fullname));
break;
case user_eData_UserDescription:
fp.get();
fp.getline( description, sizeof(description));
break;
case user_eData_UserEmail:
fp.get();
fp.getline( email, sizeof(email));
break;
case user_eData_UserPhone:
fp.get();
fp.getline( phone, sizeof(phone));
break;
case user_eData_UserSms:
fp.get();
fp.getline( sms, sizeof(sms));
break;
case user_eData_End: end_found = 1; break; case user_eData_End: end_found = 1; break;
default: default:
cout << "User:open syntax error" << endl; cout << "User:open syntax error" << endl;
...@@ -816,47 +863,86 @@ void UserList::save( ofstream& fp) ...@@ -816,47 +863,86 @@ void UserList::save( ofstream& fp)
{ {
fp << int(user_eData_User) << endl; fp << int(user_eData_User) << endl;
fp << int(user_eData_UserName) << FSPACE << name << endl; fp << int(user_eData_UserName) << FSPACE << name << endl;
fp << int(user_eData_UserPassword) << FSPACE << crypt( password) << endl; fp << int(user_eData_UserPassword) << FSPACE << password << endl;
fp << int(user_eData_UserPrivilege) << FSPACE << crypt( priv) << endl; fp << int(user_eData_UserPrivilege) << FSPACE << icrypt( priv) << endl;
fp << int(user_eData_UserId) << FSPACE << id << endl;
fp << int(user_eData_UserFullName) << FSPACE << fullname << endl;
fp << int(user_eData_UserDescription) << FSPACE << description << endl;
fp << int(user_eData_UserEmail) << FSPACE << email << endl;
fp << int(user_eData_UserPhone) << FSPACE << phone << endl;
fp << int(user_eData_UserSms) << FSPACE << sms << endl;
fp << int(user_eData_End) << endl; fp << int(user_eData_End) << endl;
} }
void UserList::print_all() void UserList::print_all( int brief)
{ {
int i; if ( brief)
char priv_str[300]; return;
print( brief);
for ( i = 0; i < 6; i++) for ( int i = 0; i < 6; i++)
cout << ". "; cout << ". ";
cout << name << " "; cout << " " << "Password " << password << endl;
for ( i = 0; i < int(10 - strlen(name)); i++)
cout << " ";
cout << password << " ";
for ( i = 0; i < int(10 - strlen(password)); i++)
cout << " ";
GeUser::priv_to_string( priv, priv_str, sizeof(priv_str));
cout << priv_str;
cout << "(" << priv << ")" << endl;
} }
void UserList::print() void UserList::print( int brief)
{ {
int i; int i;
char priv_str[300]; char priv_str[300];
for ( i = 0; i < 6; i++) if ( brief) {
cout << ". "; for ( i = 0; i < 6; i++)
cout << name << " "; cout << ". ";
for ( i = 0; i < int(10 - strlen(name)); i++) cout << name << " ";
cout << " "; for ( i = 0; i < int(10 - strlen(name)); i++)
GeUser::priv_to_string( priv, priv_str, sizeof(priv_str)); cout << " ";
cout << priv_str; GeUser::priv_to_string( priv, priv_str, sizeof(priv_str));
cout << "(" << priv << ")" << endl; cout << priv_str;
cout << "(" << priv << ")" << endl;
}
else {
for ( i = 0; i < 6; i++)
cout << ". ";
cout << name << endl;
for ( i = 0; i < 6; i++)
cout << ". ";
cout << " " << "Id " << id << endl;
for ( i = 0; i < 6; i++)
cout << ". ";
cout << " " << "Priv ";
GeUser::priv_to_string( priv, priv_str, sizeof(priv_str));
cout << priv_str;
cout << "(" << priv << ")" << endl;
for ( i = 0; i < 6; i++)
cout << ". ";
cout << " " << "FullName " << fullname << endl;
for ( i = 0; i < 6; i++)
cout << ". ";
cout << " " << "Description " << description << endl;
for ( i = 0; i < 6; i++)
cout << ". ";
cout << " " << "Email " << email << endl;
for ( i = 0; i < 6; i++)
cout << ". ";
cout << " " << "Phone " << phone << endl;
for ( i = 0; i < 6; i++)
cout << ". ";
cout << " " << "Sms " << sms << endl;
}
} }
char *UserList::crypt( char *str) char *UserList::pwcrypt( char *str)
{ {
static char cstr[200]; static char cstr[200];
strcpy( cstr, crypt( str, "aa"));
#if 0
char *s, *t; char *s, *t;
for ( s = str, t = cstr; *s; s++, t++) for ( s = str, t = cstr; *s; s++, t++)
...@@ -867,50 +953,53 @@ char *UserList::crypt( char *str) ...@@ -867,50 +953,53 @@ char *UserList::crypt( char *str)
*t = *s; *t = *s;
} }
*t = *s; *t = *s;
#endif
return cstr; return cstr;
} }
unsigned long UserList::crypt( unsigned long i) unsigned long UserList::icrypt( unsigned long i)
{ {
return ~(i + 123456); return ~(i + 123456);
} }
char *UserList::decrypt( char *str) unsigned long UserList::idecrypt( unsigned long i)
{
static char cstr[200];
char *s, *t;
for ( s = str, t = cstr; *s; s++, t++)
{
if ( *s >= 48 && *s <= 122)
*t = 122 - ( *s - 48);
else
*t = *s;
}
*t = *s;
return cstr;
}
unsigned long UserList::decrypt( unsigned long i)
{ {
return ~i - 123456; return ~i - 123456;
} }
void UserList::modify( char *password, unsigned int priv) void UserList::modify( char *password, pwr_tMask priv, char *fullname, char *description,
char *email, char *phone, char *sms)
{ {
strcpy( this->password, password); strcpy( this->password, password);
this->priv = priv; this->priv = priv;
if ( fullname)
strncpy( this->fullname, fullname, sizeof(this->fullname));
if ( description)
strncpy( this->description, description, sizeof(this->description));
if ( email)
strncpy( this->email, email, sizeof(this->email));
if ( phone)
strncpy( this->phone, phone, sizeof(this->phone));
if ( sms)
strncpy( this->sms, sms, sizeof(this->sms));
} }
void UserList::get_data( char *password, unsigned int *priv) void UserList::get_data( char *password, unsigned int *priv, pwr_tOix *id, char *fullname, char *description,
char *email, char *phone, char *sms)
{ {
strcpy( password, this->password); strcpy( password, this->password);
*priv = this->priv; *priv = this->priv;
*id = this->id;
strcpy( fullname, this->fullname);
strcpy( description, this->description);
strcpy( email, this->email);
strcpy( phone, this->phone);
strcpy( sms, this->sms);
} }
int UserList::check_password( char *password) int UserList::check_password( char *password)
{ {
if ( cdh_NoCaseStrcmp( this->password, password) == 0) if ( strcmp( this->password, password) == 0)
return USER__SUCCESS; return USER__SUCCESS;
return USER__NOTAUTHORIZED; return USER__NOTAUTHORIZED;
} }
......
/** /**
* Proview $Id: co_user.h,v 1.5 2005-09-20 13:21:45 claes Exp $ * Proview $Id: co_user.h,v 1.6 2008-06-24 07:04:33 claes Exp $
* Copyright (C) 2005 SSAB Oxelsund AB. * Copyright (C) 2005 SSAB Oxelsund AB.
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
...@@ -26,47 +26,59 @@ ...@@ -26,47 +26,59 @@
#include <fstream.h> #include <fstream.h>
#include <string.h> #include <string.h>
#include "pwr.h"
#if defined __cplusplus #if defined __cplusplus
extern "C" { extern "C" {
#endif #endif
#define user_cFilename "pwra_db:pwr_user.dat" #define user_cFilename "pwra_db:pwr_user2.dat"
#define user_cVersion "V1.0.0" #define user_cVersion "V2.0.0"
#define user_cNId ((pwr_tOix)(~0))
#define FSPACE " " #define FSPACE " "
typedef enum { typedef enum {
user_eData_GeUser = 1, user_eData_GeUser = 1,
user_eData_System = 2, user_eData_System = 2,
user_eData_User = 3, user_eData_User = 3,
user_eData_End = 99, user_eData_End = 99,
user_eData_GeUserVersion = 100, user_eData_GeUserVersion = 100,
user_eData_SystemName = 200, user_eData_GeNextId = 101,
user_eData_SystemLevel = 201, user_eData_SystemName = 200,
user_eData_SystemAttributes = 202, user_eData_SystemLevel = 201,
user_eData_UserName = 300, user_eData_SystemAttributes = 202,
user_eData_UserPassword = 301, user_eData_SystemId = 203,
user_eData_UserPrivilege = 302 user_eData_SystemDescription = 204,
} user_eData; user_eData_UserName = 300,
user_eData_UserPassword = 301,
user_eData_UserPrivilege = 302,
user_eData_UserId = 303,
user_eData_UserDescription = 304,
user_eData_UserFullName = 305,
user_eData_UserPhone = 306,
user_eData_UserEmail = 307,
user_eData_UserSms = 308
} user_eData;
typedef enum { typedef enum {
user_mSystemAttr_UserInherit = 1 user_mSystemAttr_UserInherit = 1
} user_mSystemAttr; } user_mSystemAttr;
class SystemName { class SystemName {
public: public:
SystemName( char *system_name) : segments(0) SystemName( char *system_name) : segments(0)
{ {
strcpy( pathname, system_name); strcpy( pathname, system_name);
}; };
char pathname[80]; char pathname[80];
char segname[10][40]; char segname[10][40];
int segments; int segments;
int parse(); int parse();
SystemName *parent(); SystemName *parent();
char *name() { return pathname; }; char *name() { return pathname; };
char *segment( int idx) { return segname[idx]; }; char *segment( int idx) { return segname[idx]; };
}; };
...@@ -78,136 +90,173 @@ class SystemList { ...@@ -78,136 +90,173 @@ class SystemList {
friend class GeUser; friend class GeUser;
friend class UserList; friend class UserList;
public: public:
SystemList( char *sl_name, int sl_level, SystemList( pwr_tOix sl_id, char *sl_name, int sl_level, unsigned int sl_attributes, char *sl_descr) :
unsigned int sl_attributes) : level(sl_level), attributes( sl_attributes), id(sl_id),
level(sl_level), attributes( sl_attributes), next(0), childlist(0), userlist(0)
next(0), childlist(0), userlist(0) {
{ strcpy( name, sl_name);
strcpy( name, sl_name); if ( sl_descr)
}; strncpy( description, sl_descr, sizeof(description));
else
strcpy( description, "");
private: };
char name[40];
int level;
unsigned long attributes; private:
SystemList *next; pwr_tObjName name;
SystemList *childlist; int level;
UserList *userlist; pwr_tMask attributes;
pwr_tOix id;
public: pwr_tString80 description;
int load( ifstream& fp); SystemList *next;
void save( ofstream& fp); SystemList *childlist;
void print(); UserList *userlist;
void print_all();
void *find_user( char *name); public:
int add_user( char *user, char *password, unsigned int priv); int load( ifstream& fp);
int add_system( SystemName *name, unsigned int attributes); void save( ofstream& fp);
int load_system( ifstream& fp); void print( int brief);
int load_user( ifstream& fp); void print_all( int brief);
SystemList *find_system( SystemName *name); void *find_user( char *name);
int remove_user( char *user); int add_user( pwr_tOix ident, char *user, char *password, pwr_tMask priv, char *fullname, char *descr, char *phone, char *email, char *sms);
int remove_system( SystemList *sys); int add_system( pwr_tOix ident, SystemName *name, pwr_tMask attributes, char *descr);
void modify( unsigned int attributes); int load_system( ifstream& fp);
void get_data( unsigned int *attributes); int load_user( ifstream& fp);
SystemList *first_system() { return childlist;} SystemList *find_system( SystemName *name);
SystemList *next_system() { return next;} int remove_user( char *user);
UserList *first_user() { return userlist;} int remove_system( SystemList *sys);
char *get_name() { return name;} void modify( pwr_tMask attributes, char *description);
unsigned long get_attributes() { return attributes;} void get_data( pwr_tMask *attributes, pwr_tOix *id, char *description);
~SystemList(); SystemList *first_system() { return childlist;}
SystemList *next_system() { return next;}
UserList *first_user() { return userlist;}
char *get_name() { return name;}
unsigned long get_attributes() { return attributes;}
~SystemList();
}; };
class UserList { class UserList {
friend class SystemList; friend class SystemList;
friend class GeUser; friend class GeUser;
public: public:
UserList( char *ul_name, char *ul_password, unsigned int ul_priv) : UserList( pwr_tOix ul_id, char *ul_name, char *ul_password, pwr_tMask ul_priv, char *ul_fullname, char *ul_description,
priv(ul_priv), next(NULL) char *ul_email, char *ul_phone, char *ul_sms) :
{ priv(ul_priv), id(ul_id), next(NULL)
strcpy( name, ul_name); {
strcpy( password, ul_password); strcpy( name, ul_name);
}; strcpy( password, ul_password);
if ( ul_fullname)
strncpy( fullname, ul_fullname, sizeof(fullname));
private: else
char name[40]; strcpy( fullname, "");
char password[40]; if ( ul_description)
unsigned long priv; strncpy( description, ul_description, sizeof(description));
UserList *next; else
strcpy( description, "");
char *crypt( char *str); if ( ul_email)
unsigned long crypt( unsigned long i); strncpy( email, ul_email, sizeof(email));
char *decrypt( char *str); else
unsigned long decrypt( unsigned long i); strcpy( email, "");
if ( ul_phone)
public: strncpy( phone, ul_phone, sizeof(phone));
int load( ifstream& fp); else
void save( ofstream& fp); strcpy( phone, "");
void print(); if ( ul_sms)
void print_all(); strncpy( sms, ul_sms, sizeof(sms));
void modify( char *password, unsigned int priv); else
int check_password( char *password); strcpy( sms, "");
void get_data( char *password, unsigned int *priv); };
UserList *next_user() { return next;}
char *get_name() { return name;}
unsigned long get_priv() { return priv;} private:
pwr_tObjName name;
char password[40];
pwr_tMask priv;
pwr_tOix id;
pwr_tString80 fullname;
pwr_tString80 description;
pwr_tString80 email;
pwr_tString40 phone;
pwr_tString40 sms;
UserList *next;
unsigned long icrypt( unsigned long i);
unsigned long idecrypt( unsigned long i);
public:
int load( ifstream& fp);
void save( ofstream& fp);
void print( int brief);
void print_all( int brief);
void modify( char *password, pwr_tMask priv, char *fullname, char *description,
char *email, char *phone, char *sms);
int check_password( char *password);
void get_data( char *password, pwr_tMask *priv, pwr_tOix *id, char *fullname, char *description,
char *email, char *phone, char *sms);
UserList *next_user() { return next;}
char *get_name() { return name;}
unsigned long get_priv() { return priv;}
static char *pwcrypt( char *str);
}; };
class GeUser { class GeUser {
friend class SystemList; friend class SystemList;
friend class UserList; friend class UserList;
public: public:
GeUser() : root(NULL) { strcpy( version, ""); strcpy( fname, "");} GeUser() : root(NULL) { strcpy( version, ""); strcpy( fname, "");}
~GeUser(); ~GeUser();
private: private:
SystemList *root; SystemList *root;
SystemList *last_system; SystemList *last_system;
char version[20]; char version[20];
char fname[256]; char fname[256];
pwr_tOix next_id;
bool get_system_name_child( SystemList *s, SystemList *system, char *name); bool get_system_name_child( SystemList *s, SystemList *system, char *name);
SystemList *get_system_child( SystemList *system, UserList *user); SystemList *get_system_child( SystemList *system, UserList *user);
public: public:
int load( char *filename); int load( char *filename);
void clear(); void clear();
int save() { return save( fname);} int save() { return save( fname);}
int save( char *filename); int save( char *filename);
int load_system( ifstream& fp); int load_system( ifstream& fp);
void print(); void print( int brief);
void print_all(); void print_all( int brief);
SystemList *find_system( SystemName *name); SystemList *find_system( SystemName *name);
int add_system( char *name, unsigned int attributes); int add_system( char *name, unsigned int attributes, char *description, pwr_tOix id = user_cNId);
int add_user( char *system, char *user, char *password, int add_user( char *system, char *user, char *password,
unsigned int priv); unsigned int priv, char *fullname, char *description, char *email, char *phone,
int remove_user( char *system, char *user); char *sms, pwr_tOix id = user_cNId);
int modify_user( char *system, char *user, char *password, int remove_user( char *system, char *user);
unsigned int priv); int modify_user( char *system, char *user, char *password,
int get_user_data( char *system, char *user, char *password, unsigned int priv, char *fullname, char *description, char *email, char *phone, char *sms);
unsigned int *priv); int get_user_data( char *system, char *user, char *password,
int modify_system( char *system, unsigned int attributes); pwr_tMask *priv, pwr_tOix *id, char *fullname, char *description, char *email,
int get_system_data( char *system, unsigned int *attributes); char *phone, char *sms);
int remove_system( char *name); int modify_system( char *system, unsigned int attributes, char *description);
int get_user( char *system, char *user, char *password, int get_system_data( char *system, pwr_tMask *attributes, pwr_tOix *id, char *description);
unsigned int *priv); int remove_system( char *name);
int get_user_priv( char *system, char *user, int get_user( char *system, char *user, char *password,
unsigned int *priv); unsigned int *priv);
char *get_status( int sts); int get_user_priv( char *system, char *user,
SystemList *root_system() { return root;} unsigned int *priv);
SystemList *get_system( UserList *user); char *get_status( int sts);
bool get_system_name( SystemList *system, char *name); SystemList *root_system() { return root;}
static void priv_to_string( unsigned int priv, char *str, int size); SystemList *get_system( UserList *user);
static void rt_priv_to_string( unsigned int priv, char *str, int size); bool get_system_name( SystemList *system, char *name);
static void dev_priv_to_string( unsigned int priv, char *str, int size); pwr_tOix get_next_id() { return next_id++;}
static void priv_to_string( unsigned int priv, char *str, int size);
static void rt_priv_to_string( unsigned int priv, char *str, int size);
static void dev_priv_to_string( unsigned int priv, char *str, int size);
}; };
#if defined __cplusplus #if defined __cplusplus
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment