⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pg_main.cpp

📁 实现pg接口功能
💻 CPP
字号:
/**************************************************************** * PG接口主处理程序					 * Language:		C++					 * OS Platform:   	UNIX						 * Authors:							 * Date:						 * Copyight (C)  	 ****************************************************************/#include "pg_main.h"//#include "aa/md5.h"//#include "aa/d3des.h"#ifdef HP_UX#define __cplusplus  199708L #endif#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/time.h>#include <string.h>#include <strings.h>#include <errno.h>#ifdef HP_UX#include <sys/pstat.h>#include <mntent.h>#endif#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <signal.h>#include <sys/ddi.h>#include <sys/types.h>#include <sys/wait.h>#include <sys/ipc.h>#include <sys/shm.h>#include <alloca.h>#include <sys/signal.h>PGSERVER_CONFIG	server_config_info;DB_Proc	DB_proc;CRw_net	Rw_net;extern "C" md5_calc (unsigned char *, unsigned char *, unsigned int);extern "C" des3_decrypt(unsigned char*, unsigned char*);extern "C" crypt(const char *, const char *);int main(){	int 		listenfd,connfd;	struct sockaddr cliaddr;	socklen_t	clilen;	pid_t		pid;		if (PG_ReadConf() < 0)	{		printf("read config file failed\n");		return -1;	}//	printf("user = %s\tpasswd = %s\tserver = %s\n",server_config_info.db_user,server_config_info.db_passwd,server_config_info.db_server);	if (DB_proc.db_connect(server_config_info.db_user,server_config_info.db_passwd,server_config_info.db_server) < 0)	{		printf("Connect to database failed!\n");		DB_proc.db_disconnect();		return -1;	}			if (Rw_net.listen_start(&listenfd,server_config_info.port) != 0)	{		printf("Cannot builded listen sockfd at port %d!\n",server_config_info.port);		return -1;	}signal(SIGCLD, SIG_IGN);		printf("testtestetest\n");	while ((connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&clilen)) > 0)	{		//printf("get a client connect from a proxy!\n");		if ((pid = fork()) == 0)		{			PG_Auth(connfd);			close(connfd);			exit(1);		}		close(connfd);	}}void PG_Auth(int connfd){	PGAuthenReqDef	PGAuth_Req;	PGAuthenResDef	PGAuth_Res;	PGMSGHeadDef	*PGMsg_head;	ACCOUNT_INFO	account_info;	int 		ret,i_ret;	char		username[40];	char		passwd[20];	int		usertype,hashvalue;	//account_info = (ACCOUNT_INFO *)malloc(sizeof(ACCOUNT_INFO));	//bzero(account_info,sizeof(ACCOUNT_INFO));	memset((void *)&account_info,0,sizeof(ACCOUNT_INFO));	ret = Rw_net.read_all(connfd,&PGAuth_Req,sizeof(PGAuth_Req));	if (ret != sizeof(PGAuth_Req))	{		printf("Read PGAuth_Req packet error!\n");		close(connfd);		return;	}		PGAuth_Res.msg_head.msg_type = AUTHEN_RES_MSG;	PGAuth_Res.msg_head.msg_version = PGAuth_Req.msg_head.msg_version;	strcpy(PGAuth_Res.msg_head.transaction_id,PGAuth_Req.msg_head.transaction_id);	printf("Now have recieved a PGAuthReq msg..............................\n");	printf("..................................msg_type=%d\tmsg_version=%d\ttransaction_id=%s\n",PGAuth_Req.msg_head.msg_type,PGAuth_Req.msg_head.msg_version,PGAuth_Req.msg_head.transaction_id);	printf("..................................timestamp=%s:username=%s\tpassword=%s\tservicetype=%s\n",PGAuth_Req.timestamp,PGAuth_Req.account,PGAuth_Req.passwd,PGAuth_Req.account_type);	PGMsg_head = &(PGAuth_Req.msg_head);	if (PGMsg_head->msg_type != AUTHEN_REQ_MSG)	{		printf("PGAuth_Req packet msg_type error!\n");		strcpy(PGAuth_Res.timestamp,PGAuth_Req.timestamp);		PGAuth_Res.result = -10002;		sprintf(PGAuth_Res.info,"%d",PGAuth_Res.result);		Rw_net.write_all(connfd,&PGAuth_Res,sizeof(PGAuth_Res));		close(connfd);		return;	}		if ((strncmp(PGAuth_Req.account_type,"01",2) != 0) and (strncmp(PGAuth_Req.account_type,"02",2) != 0) and (strncmp(PGAuth_Req.account_type,"04",2) != 0))	{		printf("PGAuth_Req packet account_type error!\n");		strcpy(PGAuth_Res.timestamp,PGAuth_Req.timestamp);		PGAuth_Res.result = -10002;		sprintf(PGAuth_Res.info,"%d",PGAuth_Res.result);		Rw_net.write_all(connfd,&PGAuth_Res,sizeof(PGAuth_Res));		close(connfd);		return;	}		strcpy(username,PGAuth_Req.account);	strcpy(passwd,PGAuth_Req.passwd);	usertype = atoi(PGAuth_Req.account_type);	hashvalue = get_hash(username);		if ( DB_proc.get_account_info(username,&account_info,usertype,hashvalue) < 0)	{		printf("PG_Main get account_info failed!\n");		strcpy(PGAuth_Res.timestamp,PGAuth_Req.timestamp);		PGAuth_Res.result = -10002;		sprintf(PGAuth_Res.info,"%d",PGAuth_Res.result);		Rw_net.write_all(connfd,&PGAuth_Res,sizeof(PGAuth_Res));		close(connfd);		return;	}		printf("In Database:username=%s\tpasswd=%s\tstatus=%d\tpasswdtype=%d\n",account_info.username,account_info.passwd,account_info.status,account_info.passwdtype);//	printf("md5 encrypt key:%s\n",server_config_info.key);	PGAuth_Res.result = 0;	if (account_info.status != 0)	{		printf("This user %s status abnormal!\n",username);		PGAuth_Res.result = -2;	}	if (strncmp(account_info.username,"ssssssssssssssssssss",20) == 0)	{		printf("This user %s not exists in database!\n",username);		PGAuth_Res.result = -1;	}	if (account_info.passwdtype == 1)	{		printf("This user %s passwd in database cannot be decrypted!\n",username);		PGAuth_Res.result = -10002;	}		if (PGAuth_Res.result == 0){	i_ret = check_passwd(passwd,account_info.passwd,account_info.passwdtype,server_config_info.key);	printf("check_passwd return %d\n",i_ret);	if (i_ret < 0)	{		printf("This user %s passwd error!\n",username);		PGAuth_Res.result = -1001;	}	else	{		printf("This user %s Auth OK!\n",username);		PGAuth_Res.result = 0;	}	}		strcpy(PGAuth_Res.timestamp,PGAuth_Req.timestamp);	sprintf(PGAuth_Res.info,"%d",PGAuth_Res.result);	printf("result :%d\n",PGAuth_Res.result);	Rw_net.write_all(connfd,&PGAuth_Res,sizeof(PGAuth_Res));	close(connfd);	return;}				int PG_ReadConf(){	char	fname[50];	FILE	*fp;	char	buf[1024],*p1,*p2,name[100],value[100];	sprintf(fname,"%s/pg/bin/sysconfig",getenv("LCIMS50DIR"));	printf("config file : %s\n",fname);	fp = fopen(fname,"rb");	if (fp == (FILE *)NULL)	{		printf("Cannot open config file\n");		return -1;	}	while(fgets(buf,MAX_LINE_LENGTH,fp) != (char *)NULL)	{		if ((buf[0] == '\0') || (buf[0] == '#')|| (buf == NULL))		{			continue;		}		memset(name,0,sizeof(name));		memset(value,0,sizeof(value));		p1 = buf;		p2 = strchr(p1,'=');		if (p2 == NULL)		{			continue;		}		strncpy(name,p1,p2-p1);		name[p2-p1] = '\0';		//printf("name = %s\n",name);		p1 = p2 + 1;		if (p1 == NULL)		{			continue;		}		strcpy(value,p1);		//printf("value= %s\n",value);		if (strncmp(name,"dbuser",6) == 0)		{			strncpy(server_config_info.db_user,value,strlen(value)-1);		}		if (strncmp(name,"dbpasswd",8) == 0)		{			strncpy(server_config_info.db_passwd,value,strlen(value)-1);		}		if (strncmp(name,"dbserver",8) == 0)		{			strncpy(server_config_info.db_server,value,strlen(value)-1);		}		if (strncmp(name,"port",4) == 0)		{			server_config_info.port=atoi(value);		}		if (strncmp(name,"key",3) == 0)		{			strncpy(server_config_info.key,value,strlen(value)-1);		}	}	printf("dbuser = %s\tdbpasswd = %s\tdbserver = %s\tport=%d\tkey=%s\n",server_config_info.db_user,server_config_info.db_passwd,server_config_info.db_server,server_config_info.port,server_config_info.key);	return 1;}int check_passwd(char *p1,char *p2,int type,char *key){	char	out_passwd[9];	char	digest[50];	char	pw[50];	int	pwlen,i,len2,len1,result;	char	salt[3];	memset(pw,0,50);	memset(out_passwd,0,9);	memset(digest,0,50);//	printf("type = %d\n",type);		if (type == 2)	{		des3_decrypt((unsigned char *)p2,(unsigned char *)out_passwd);		len2 = strlen(out_passwd);		result = strncmp((const char *)p1,out_passwd,len2);	}	if (type == 0)	{		len2 = strlen((const char *)p2);		result = strncmp((const char *)p1,(const char *)p2,len2);	}	if (type == 1)	{		strncpy(salt,(const char *)p2,2);		salt[2] = '\0';		strcpy(digest,(const char *)crypt((const char *)p1,salt));		//digest = (char *)crypt((const char *)p1,salt);		result = strncmp((const char *)digest,(const char *)p2,strlen((const char *)p2));	}	if (result == 0)		return 1;	else		return -1;	/*	if (type == 2)	{		des3_decrypt((unsigned char *)p2,(unsigned char *)out_passwd);		strncpy(pw,out_passwd,8);		//len1=8;	printf("decrypted secret = %s\n",out_passwd);	}	else	{		strcpy(pw,p2);		//len1=strlen((const char *)p2);	}	len1=strlen(pw);	strcpy(pw+len1,key);	pwlen = len1+strlen((const char *)key);	pw[pwlen] = '\0';	printf("pw = %s结束pwlen=%d\n",pw,pwlen);	md5_calc((unsigned char *)digest,(unsigned char *)pw,(unsigned int)pwlen);	printf("digest =(%s )\n",digest);	if (strncmp((const char *)digest,(const char *)p1,strlen((const char *)p1)) != 0)	{		return -1;	}	else	{		return 1;	}*/}int get_hash(char *str){	static int l_power[40]={2,3,5,7,11,13,17,19,23,29,                31,37,41,43,47,53,59,61,67,71,73,79,                83,89,97,101,103,107,109,113,127,131,                137,139,149,151,157,163,167,173};        int s_sum,i;        strcpy(str,x_trim(str));        s_sum=0;        for(i=0;i<strlen(str);i++) {                s_sum+=str[i]*l_power[i];        }        return(s_sum%6);}char *x_trim(char *kk){        char *ptr;        char *ptr1;        int len;        static char buf[xMAX_STRING_LEN+1];        memset(buf,0,sizeof(buf));        if(kk==(char *)NULL) return NULL;        ptr=kk;        len=strlen(kk);        ptr1=ptr+len-1;        while(isspace(*ptr)) ptr++;        while(isspace(*ptr1)) ptr1--;        ptr1++;        *ptr1=0;        memcpy(buf,ptr,(ptr1-ptr));        return (char*)buf;}

⌨️ 快捷键说明

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