chkusrpwd.c

来自「linux下的ftp服务端,支持多种ftp版本」· C语言 代码 · 共 141 行

C
141
字号
#include "chkusrpwd.h"
#include "global.h"
#include <unistd.h>
#include <shadow.h>
#include <pwd.h>



char usr[1024]="";
char pwd[1024]="";


int Chkusrpwd(int sockfd)
{
	
	struct passwd *pw; // used for /etc/passwd
	struct spwd *spw; // used for /etc/shadow
	
	char *passwd;
	char tmp[13];
	char path[PATH_MAX+1];
	
	
	
	if (getuid() != 0) 
	{ 
		/* 只有root有权作此操作 */

		return 0;
	};

	/* 取得/etc/passwd中间对应于本用户的数据结构 */
	
	if ((pw = getpwnam(usr)) == NULL)
	{
		return 0;
	};
	
	/* 取得/etc/shadow中被隐藏的密码 */
	spw = getspnam(usr);
	if (spw == NULL || (passwd = spw->sp_pwdp) == NULL)
	{
		return 0;
	};
	
	/* 取得passwd的前12个字节作为种子 */
	strncpy(tmp,passwd, 12);
	
	/* 判断输入的密码加密后是否与原密码相同 */
	if(strcmp(usr,"ftp")!=0)
	{
		if (strcmp(passwd,crypt(pwd,tmp))!= 0)
		{
			/* 密码错误 */
			return 0;
		};
	}
	
	/* 密码正确,改变程序权限 */
	//setuid(pw->pw_uid);
	setegid(pw->pw_gid);
	seteuid(pw->pw_uid);
	
	/* 获得并进入用户根目录 */
	if (pw->pw_dir) 
		strncpy(path, pw->pw_dir, PATH_MAX);
	else 
		strcpy(path, "/");
	/*if(strcmp(usr,"ftp")==0)
		strcpy(path,"/")*/
	chdir(path);
	
	
	
	return 1;






	

	//Write(sockfd,"230 Login successful. Have fun.\r\n",strlen("230 Login successful. Have fun.\r\n"));
	

}



void GetUsr(char *user)
{
	
	strcpy(usr,user);
	
	return;
}



void GetPwd(int sockfd,char *pass)
{
	
	strcpy(pwd,pass);
	
	
	if(strcmp(usr,"anonymous")==0)
	{
		
		strcpy(usr,"ftp");
		

	}
	

	int flag=0;
	
	flag=Chkusrpwd(sockfd);
	

	if(flag==1)
	{
		
		Write(sockfd,"230 Login successful. Have fun.\r\n",strlen("230 Login successful. Have fun.\r\n"));
		islogin=1;
		if(strcmp(usr,"ftp")==0)
			isany=1;
	}
	else
	{
		
		Write(sockfd,"530 Login incorrect.\r\n",strlen("530 Login incorrect.\r\n"));
		
	}
	return;

}



⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?