www.pudn.com > DTMS.rar > USRPASS.H



#include 
#include 
/*
#define _XOPEN_SOURCE
#include 
#include
#include 
#include
#include
#define PASWDFILE "passwd"
#define SEP ':'
#define SALT "dt"
#define newline '\n'
#define UIDLEN 4

#define HDR_OFF 0
*/
int PASFD=-1;

/*static void err_doit(int,const char *,va_list);
void err_ret(const char *fmt,...);
void err_sys(const char *fmt,...);
void err_dump(const char *fmt,...);
void err_msg(const char *fmt,...);
void err_quit(const char *fmt,...);
static void err_doit(int errnoflag,const char *fmt,va_list ap);

*/
/*
struct password
{
 char *user;
 char *password;
};

int UsrStore(char *psd,char *usr);
int GetUid();
int _pas_Store(char *buf,int paslen);
int TestPsd(char *psd,char *usr);
struct password *_pas_GetOneRecd(char *buf,struct password *pp);
char *_pas_GetOneLine(char *buf);
void _pas_SetWorkPoint(off_t offset);
*/
int
ReadLine(fd, ptr, maxlen)
register int	fd;
register char	*ptr;
register int	maxlen;
{
	int	n, rc;
	char	c;

	for (n = 1; n < maxlen; n++) 
         {
		if ( (rc = read(fd, &c, 1)) == 1) 
                   {
			/* *ptr++ = c; */
			
                                      if (c == '\n')
                          	break;
                                *ptr++=c; 
                            /* we will instead of th '\n'with '\0'in the end*/
		    } else if (rc == 0) 
			{
			  if (n == 1)
				return(0);	/* EOF, no data read */
			     else
				break;		/* EOF, some data was read */
		        } else
		  return(-1);	/* error */
	  }

	*ptr = 0;
	return(n);
}


int UsrStore(char*usr,char *psd)
{
 char buf[MAXLINE+2];
 struct password passwd;
 FILE *fp,*up;
 int plen,ps1len,pslen,uslen,us1len;
 MAIL *mail;
 
 passwd.password=psd;
 passwd.user=usr;  
 
 sprintf(buf,"%s%c%s\n",passwd.user,SEP,passwd.password);
 pslen=strlen(psd);
 uslen=strlen(usr);
 plen=pslen+uslen+2;

 mail= newfile(usr); 
 mail_close(mail);
  
 return(_pas_Store(buf,plen));   
}
/* 
  Write the password record in the passwd file,append the file
*/
int _pas_Store(char * buf,int paslen)
{
 FILE *fp;
 int n,passbuflen;
 char passbuf[paslen];

 strcpy(passbuf,buf);
 passbuflen=strlen(passbuf); 
 if((fp=fopen(PASWDFILE,"a+"))==NULL)
   err_sys("error in fopen()");

 if((n=write(fileno(fp),passbuf,paslen))!=paslen)
  err_dump("write error of password record");

 fclose(fp);
 return n;  
}
/*
  Check the user and password.If OK return1,else return 0. 
*/
int TestPasd(/*int sd,*/char*usr,char *psd)
{
 struct password pasd,*pp;
 off_t offset;
 int buflen;
 FILE *fp;
 char buf[MAXLINE],*bufp;

 if((fp=fopen(PASWDFILE,"r"))==NULL)
  err_sys("open PASWDFILE error in TestPasd()");
  PASFD=fileno(fp);


 offset=HDR_OFF;
 _pas_SetWorkPoint(offset);
            /*a problem of line number convert to file offset*/
 while((bufp=_pas_GetOneLine(buf))!=NULL)
     {
        buflen=strlen(bufp)+1; 
        pp=_pas_GetOneRecd(bufp,&pasd);
        if((strcmp(pp->user,usr)==0)&&(strcmp(pp->password,psd)==0))
          break;           /*found a match*/
        offset=offset+buflen;  
       _pas_SetWorkPoint(offset);
     }
 PASFD=-1;
 close(fileno(fp));
 if(!bufp) return 0; /*to the end of the file, not find a match*/
 return 1;
}
/* return the point to the password struct*/
struct password  *_pas_GetOneRecd(char *buf,struct password *pp)
{
  char *user,*pas/**uidp*/;
  int nuid;

  user=buf;
  if((pas=strchr(buf,SEP))==NULL)
    err_dump("missing second seperator");
  *pas++=0;
  
  if(strchr(pas,SEP)!=NULL)
    err_dump("too many seperator");    
  pp->user=user;
 /*pas[strlen(pas)-1]='\0';*/
  pp->password=pas; 
  return pp;
} 

/* 
  a function for get a line from PASWDFILE ,if OK return buffer point
   else if EOF return NULL
*/
char *_pas_GetOneLine(char *buf)
{
 int n;
 if((n=ReadLine(PASFD,buf,MAXLINE))<0)
    err_dump("error in read password record from PASWDFILE");
   else
      if(n==0)    return NULL;
   else 
      return buf;
}
/*
 Set the Work point for read a record.
*/
void _pas_SetWorkPoint(off_t offset)
{
 int i;
 if((i=lseek(PASFD,offset,SEEK_SET))==-1)
    err_dump("lseek error in _pas_SetWorkPoint");
}