📄 checkpasswd.c
字号:
/*
*File: checkPasswd.c
*-------------------
*This part defines all the functions that is or will be
*used in the project CP Program.
*CP here means check password
*/
#include "checkPasswd.h"
#include <ctype.h>
/*
*Constants
*---------
*mNumPatterns--Stand by the lines of the forbiddenPasswd file
*mPasswdForbidden[MaxLine][MaxPatternLen]--Stand by the forbidden passwd array
*mPatternPlus[MaxLine][MaxPatternLen]--Stand by the special string array
*/
//static int mNumPatternsPlus;
static int mNumPatterns;
static char mPatternPlus[MaxLine][MaxPatternLen];
static char mPasswdForbidden[MaxLine][MaxPatternLen];
static struct configstate sg_confState[ConfigCapacity+100];
/*
*name:zhangjiwang
*ProjectName:checkPasswd
*time:2004-9-8
*/
//-----------------------------------------------------------------------------------
/*
*Function: putStringToArray
*Usage: putStringToArray(string,confStateNum);
*----------------------------------------
*Arithmetic: get the first part of the string
*to configName and get the other part to configState
*
*/
void putStringToArray(char *str,int confStateNum)
{
int i = 0,j=0;
//
//get the symbol one by one until you have meet a blank
//put them into configName[] as configName
//
for( ; i<(int)strlen(str); i++ )
{
if(str[i]!=' ')
{
sg_confState[confStateNum].configName[i] = str[i];
}
else
{
sg_confState[confStateNum].configName[i] = '\0';
while(str[i]==' ')
{
i++;
}
break;
}
}
//
//put the other part after the blank as configState
//
for( ; i<(int)strlen(str); i++ )
{
if(str[i]!=' ')
{
sg_confState[confStateNum].configState[j] = str[i];
j++;
}
}
sg_confState[confStateNum].configState[j] = '\0';
}
/*
*Fuction: readConfigFile
*Usage: int readConfigRslt=readConfigFile( FileName );
*-----------------------------------------------------
*Arithmetic: In the config file there are notations with the entry char '#'
*
*
*/
int readConfigFile( char* FileName )
{
//
//read config file and
//use the function to save it in the struct sg_confState
//
FILE *fp;
int i=0;
int confStateNum = 0;
if((fp = fopen(FileName,"r")) == NULL)
{
printf("Can not open file %s\n",FileName);
fclose(fp);
return 0;
}
while(!feof(fp))
{
char ch = getc(fp);
char StructStrbuffer[200];
//
//if get a '#' get the function to change string to struct
//and clear the buffer to read next string!
//
if(ch == '#')
{
while (1)
{
StructStrbuffer[i] = '\0';
if(strcasecmp(StructStrbuffer,""))
{
putStringToArray(StructStrbuffer,confStateNum);
confStateNum++;
i = 0;
}
if(getc(fp)=='\n'||feof(fp))
{
break;
}
}
}
else
{
StructStrbuffer[i] = ch;
i++;
}
}
fclose(fp);
return 1;
}
/*
*Function: readForbidPasswdFile
*Usage: int readForbidPasswdFileRslt=readForbidPasswdFile(forbidPasswdFile);
*--------------------------------------------------------------------------
*Arithmetic: read files to check if the password is in the forbidden password file
*or the password is invalid like this password=username+appendix Or
*password=appendix+username
*
*/
int readForbidPasswdFile(char *forbidPasswdFile)
{
FILE *fp;
//
//open the file forbidPasswdFile
//this file contains the forbidden password that has
//low complexity and low security , like "12345678"
//or "12345678" or "helloworld" and so on!
//
fp=fopen( forbidPasswdFile , "r");
if ( fp==NULL )
{
printf( "file %s not found!!!\n", forbidPasswdFile);
exit(0);
}
mNumPatterns = 0;
while( !feof(fp) )
{
fscanf(fp,"%s",mPasswdForbidden[mNumPatterns]);
mNumPatterns=mNumPatterns+1;
}
fclose(fp);
return 1;
}
/*
*Function: checkPasswordAppendix
*Usage: int checkPasswordAppendixRslt=checkPasswordAppendix(username,password,filename);
*--------------------------------------------------------------------------------------
*Arithmetic or functions: to check if the password is username+specialstring
*or specialstring+username
*If so then the password is invalid
*/
int checkPasswordAppendix(char *userName,char*passWord,char *fileNameOfSpecString)
{
FILE *fp;
char tempUsername[MaxPatternLen];
char string[MaxPatternLen];
if((fp=fopen(fileNameOfSpecString,"r"))==NULL)
{
printf("Can not open the file %s!\n",fileNameOfSpecString);
exit (0);
}
else
{
//
//Read special string one by one from the file and
//check if password is
//username + specialstring or specialstring+username.
//
while (!feof(fp))
{
fscanf(fp,"%s",string);
strcpy(tempUsername,userName);
if(strcasecmp(passWord,strcat(tempUsername,string))==0)
{
fclose (fp);
return 0;
}
else if(strcasecmp(passWord,strcat(string,userName))==0)
{
fclose (fp);
return 0;
}
}
}
fclose (fp);
return 1;
}
/*
*Function: checkIfForbidden
*Usage: int checkIfForbiddenRslt=checkIfForbidden(password);
*-----------------------------------------------------------
*Arithmetic or functions: To check if the password is forbidden. There are many passwords that
*we do not permit to use beacause of its low complexity and low security!
*/
int checkIfForbidden(char *passWord)
{
int i;
for( i=0; i<mNumPatterns;i++)
{
if(strcasecmp(passWord, mPasswdForbidden[i]) == 0)
{
return 0;
break;
}
}
return 1;
}
/*
*Function: checkMinLen
*Usage: int checkMinLenRslt=checkMinLen(password,minimum);
*---------------------------------------------------------
*Arithmetic or functions: to check if the password is shorter than a minimum length
*/
int checkMinLen(char *passWord,int minimum)
{
if((int)strlen(passWord)<minimum)
{
return 0;
}
else
{
return 1;
}
}
/*
*Function: checkIfSame
*Usage: int checkIfSameRslt=checkIfSame(username,password);
*---------------------------------------------------------
*Arithmetic or functions: To check if the password is same to the username,
*if so then the password is an invalid one
*/
int checkIfSame(char *userName,char* passWord)
{
if(strcasecmp(userName,passWord)==0)
{
return 0;
}
else
{
return 1;
}
}
/*
*Function: checkAllDigitsOrAllLetters
*Usage: int checkAllDigitsOrAllLettersRslt=checkAllDigitsOrAllLetters(password);
*-------------------------------------------------------------------------------
*Arithmetic or functions: To check if the password is all digits or all letters
*if so then the password is an invalid one
*/
int checkAllDigitsOrAllLetters(char *passWord)
{
int firstSymbolType=0;
int i=0;
char tempPassword=passWord[0];
if(isdigit(tempPassword))
firstSymbolType=1;
else if(isalpha(tempPassword))
firstSymbolType=2;
else
firstSymbolType=0;
for(i=0;i<(int)strlen(passWord);i++)
{
int otherSymbolType=0;
char oneSymbolOfPasswd=passWord[i];
if(isdigit(oneSymbolOfPasswd))
{
otherSymbolType=1;
}
else if(isalpha(oneSymbolOfPasswd))
{
otherSymbolType=2;
}
else
{
otherSymbolType=0;
}
if(otherSymbolType!=firstSymbolType)
{
return 1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -