#include "DAC_Global.h"
char currentuser[20];
subject *hsubject;
subject *currentsubject;
object *hobject;
right *hright;
authorize *hauthorize;
void Dac()
{
hsubject=(subject *)malloc(sizeof(subject));
hsubject->groupid=1;
hsubject->next=NULL;
strcpy_s(hsubject->password,20,"admin");
strcpy_s(hsubject->username,20,"admin");
hobject=(object *)malloc(sizeof(object));
hright=(right *)malloc(sizeof(right));
hauthorize=(authorize *)malloc(sizeof(authorize));
hobject->next=NULL;
hright->next=NULL;
hauthorize->next=NULL;
Menu();
}
UCHAR Login(char *user,char *password)
{
subject *p=hsubject;
while(p!=NULL)
{
if(!strcmp(user,p->username)&&!strcmp(password,p->password))
{
strcpy_s(currentuser,20,p->username);
currentsubject=p;
printf("%s has login successful\n",currentuser);
return SUCCESS;
}
else
p=p->next;
}
printf("username or password error\n");
return USER_PASS_ERROR;
}
UCHAR AddSub(char *user,char *pass)
{
subject *p=hsubject;
if(strcmp("admin",currentuser)!=0)
{
printf("add subject failed,please check your right!\n");
return RIGHT_ERROR;
}
while(p->next!=NULL)
{
if(strcmp(user,p->username)==0)
{
printf("already have a %s\n",user);
return SUBJECT_RENAME;
}
else
p=p->next;
}
if(strcmp(user,p->username)==0)
{
printf("already have a %s\n",user);
return SUBJECT_RENAME;
}
p->next=(subject *)malloc(sizeof(subject));
p->next->groupid=0;
p->next->next=NULL;
strcpy_s(p->next->username,20,user);
strcpy_s(p->next->password,20,pass);
return SUCCESS;
}
UCHAR DelSub(char *user)
{
subject *current=hsubject,*p=hsubject;
if(strcmp("admin",currentuser)!=0)
{
printf("delete subject failed,please check your right!\n");
return RIGHT_ERROR;
}
while(current!=NULL&&strcmp(current->username,user))
{
p=current;
current=current->next;
}
if(!current)
{
printf("can't find the subject\n");
return NO_SUBJECT ;
}
else
{
p->next=current->next;
free(current);
}
return SUCCESS;
}
UCHAR Logout()
{
currentsubject=NULL;
strcpy_s(currentuser,20," ");
return SUCCESS;
}
UCHAR CreatObj(char *objname)
{
object *objp=hobject;
right *rightp=hright;
int i;
UCHAR r=1;
while(objp->next!=NULL)
{
if(!strcmp(objp->objname,objname))
{
printf("客体重名!\n");
return OBJECT_RENAME;
}
else
objp=objp->next;
}
objp->next=(object *)malloc(sizeof(object));
objp->next->next=NULL;
strcpy_s(objp->next->objname,20,objname);
strcpy_s(objp->next->owner,20,currentuser);
while(rightp->next!=NULL)
{
rightp=rightp->next;
}
for(i=0;i<5;i++)
{
rightp->next=(right *)malloc(sizeof(right));
strcpy_s(rightp->next->username,20,currentuser);
strcpy_s(rightp->next->objname,20,objname);
rightp->next->cangive=1;
rightp->next->next=NULL;
strcpy_s(rightp->next->whogive,20,currentuser);
rightp->next->rights=r;
r*=2;
rightp=rightp->next;
}
return SUCCESS;
}
UCHAR TestRight(char *objname,UCHAR rights)
{
right *rightp=hright;
while(rightp!=NULL)
{
if(!strcmp(rightp->objname,objname)&&!strcmp(rightp->username,currentuser))
{
if(((rightp->rights)&rights)!=0)
break;
else
rightp=rightp->next;
}
else
rightp=rightp->next;
}
if(rightp==NULL)
return NO_RIGHTS;
else
return HAVE_RIGHTS;
}
UCHAR GiveRights(char *objectname,char *authorizedsub,UCHAR rights,int cangive)
{
right *rightp=hright;
authorize *authorizep=hauthorize;
subject *subjectp=hsubject;
while(rightp)
{
if(!strcmp(rightp->username,currentuser)&&!strcmp(rightp->objname,objectname)&&rightp->rights==rights&&rightp->cangive==1)
break;
else
rightp=rightp->next;
}
if(rightp==NULL)
return NO_RIGHTS;
else
rightp=hright;
while(subjectp)
{
if(!strcmp(subjectp->username,authorizedsub))
break;
else
subjectp=subjectp->next;
}
if(subjectp==NULL)
return NO_SUBJECT;
else
{
while(rightp->next!=NULL)
{
if(rightp->cangive==cangive&&!strcmp(rightp->objname,objectname)&&!strcmp(rightp->username, authorizedsub)&&rightp->rights==rights&&!strcmp(rightp->whogive,currentuser))
return REAUTHORIZE;
else
rightp=rightp->next;
}
rightp->next=(right *)malloc(sizeof(right));
rightp->next->cangive=cangive;
rightp->next->next=NULL;
strcpy_s(rightp->next->objname,20,objectname);
strcpy_s(rightp->next->username,20,authorizedsub);
rightp->next->rights=rights;
strcpy_s(rightp->next->whogive,20,currentuser);
while(authorizep->next!=NULL)
{
authorizep=authorizep->next;
}
authorizep->next=(authorize *)malloc(sizeof(authorize));
authorizep=authorizep->next;
strcpy_s(authorizep->authorizer,20,currentuser);
strcpy_s(authorizep->authorizedsub,20,authorizedsub);
authorizep->next=NULL;
strcpy_s(authorizep->objectname,20,objectname);
authorizep->rights=rights;
authorizep->cangive=cangive;
return SUCCESS;
}
}
UCHAR RevokeRights(char *authorizer,char *authorizedsub,char *objname,UCHAR rights)
{
_deleteFromright(authorizer,authorizedsub,objname,rights);
_deleteFromauthorize(authorizer,authorizedsub,objname,rights);
if(_continue(authorizedsub,objname,rights))
_passRevoke(authorizer,authorizedsub,objname,rights);
else
return SUCCESS;
}
UCHAR _deleteFromright(char *authorizer,char *authorizedsub,char *objname,UCHAR rights)
{
right *pright=hright,*cpright=hright;
while(pright!=NULL)
{
if(!strcmp(pright->whogive,authorizer)&&!strcmp(pright->username,authorizedsub)&&!strcmp(pright-> objname,objname)&&pright->rights==rights)
{
break;
}
cpright=pright;
pright=pright->next;
}
if(!pright)
return NO_RIGHTS;
else
{
cpright->next=pright->next;
free(pright);
}
return SUCCESS;
}
UCHAR _deleteFromauthorize(char *authorizer,char *authorizedsub,char *objname,UCHAR rights)
{
authorize *pauthorize=hauthorize,*cauthorzie=hauthorize;
while(pauthorize!=NULL)
{
if(!strcmp(pauthorize->objectname,objname)&&!strcmp(pauthorize->authorizedsub,authorizedsub)&& pauthorize->rights==rights&&!strcmp(pauthorize->authorizer,authorizer))
break;
cauthorzie=pauthorize;
pauthorize=pauthorize->next;
}
if(pauthorize==NULL)
return NO_RIGHTS;
else
{
cauthorzie->next=pauthorize->next;
free(pauthorize);
return SUCCESS;
}
}
BOOL _continue(char *authorizedsub,char *objname,UCHAR rights)
{
authorize *pauthorize=hauthorize->next;
while(pauthorize!=NULL)
{
if(!strcmp(pauthorize->authorizedsub,authorizedsub)&&!strcmp(pauthorize->objectname,objname)&& pauthorize->rights==rights&&pauthorize->cangive==1)
break;
else
pauthorize=pauthorize->next;
}
if(pauthorize==NULL)
return TRUE;
else
return FALSE;
}
UCHAR _passRevoke(char *authorizer,char *authorizedsub,char *objname,UCHAR rights)
{
authorize *pauthorize=hauthorize->next;
char temp[20];
while(pauthorize!=NULL)
{
if(!strcmp(pauthorize->authorizer,authorizedsub)&&pauthorize->rights==rights&&!strcmp(pauthorize-> objectname,objname))
{
strcpy_s(temp,20,pauthorize->authorizedsub);
pauthorize=pauthorize->next;
RevokeRights(authorizedsub,temp,objname,rights);
}
else
pauthorize=pauthorize->next;
}
return SUCCESS;
}
UCHAR SaveData()
{
FILE *savesubject,*saveobject,*saveright,*saveauthorize;
subject *psubject=hsubject->next;
object *pobject=hobject->next;
right *pright=hright->next;
authorize *pauthorize=hauthorize->next;
_mkdir("\\database");
_chdir("\\database");
if((savesubject=fopen("subject.mdb","w"))==NULL)
return SAVE_ERROR;
if((saveobject=fopen("object.mdb","w"))==NULL)
return SAVE_ERROR;
if((saveright=fopen("right.mdb","w"))==NULL)
return SAVE_ERROR;
if((saveauthorize=fopen("authorize.mdb",