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

📄 telnet.c

📁 Magic C++!的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
     	strcat( filesinfo , fileinfo );     	file = (char *)strtok( NULL, "\n" );   	}   	strcat(filesinfo , "\n");	if(debug)	   	util_log( "filesinfo:%s\n",filesinfo );   	write(sockfd, filesinfo, strlen( filesinfo ));	return 0;}extern char szInstallDir[200];/*query install path*/int query_path(int sockfd, char *clientip){	int n;	int permit;	char sent[3];		reply[ 0 ] = IAC;	permit = WILL;	reply[ 1 ] = permit;	reply[ 2 ] = QUERY_PATH;	write(sockfd, reply, 3);	n = read( sockfd , sent , 3 );	if (n == 0)	{		util_log("The client :%s has left\n",clientip );		if( getlicence )			release_licencecnt(clientip);		exit(0);  /* connection terminated */	}	else if (n < 0) 	{		util_err_log("socket read error",__FILE__,__LINE__,errno);		if( getlicence )			release_licencecnt(clientip);		exit(3);	}	if( permit == WONT )		return -1;	write( sockfd , szInstallDir , strlen( szInstallDir ));	return 0;	}/*release licence*/int release_licence(int sockfd, char *clientip){	reply[ 0 ] = IAC; 	util_log("Client :%s is Releasing a Licence \n",clientip );	if( release_licencecnt(clientip) == -1 )		reply[ 1 ] = WONT;	else		reply[ 1 ] = WILL;		reply[ 2 ] = RELEASE_LICENCE;	write(sockfd, reply, 3);	return 0;}int release_licencecnt(char *clientip){	int ret;	if( getlicence == 1 )	{		if( util_subtract_global() == -1 )		{			util_log("Client : %s Release licence failed!, licence count is zero\n",clientip );			return -1;		}		else 		{			util_log("Client : %s Release licence successfully!\n",clientip );			getlicence = 0;			return 0;		}	}		util_log("Client : %s Release licence failed , You can not apply licence before apply one!\n",clientip );	return -1;}/*get original command prompt,and setup new command prompt*//*newfd - socket descriptor*//*streamfd - teminal descriptor *//*clientip - ip address of client*//*prompt - new command prompt*//*pw - the passwd struct */int ret_command_prompt(int newfd ,int streamfd 	,char * clientip , char *prompt ,	struct passwd *pw){	int n ;	int first = 0;	char line[BUFSIZ];	char ret[100];	char discard[10240];	char change_prompt[100];   	char initprompt[100];   	int csh = 0;	int nlogin_timeout;	char ensure[4];	/*read in old command prompt*/	memset( line , 0 , sizeof( line ));	memset( ret , 0 , sizeof( ret ));	memset( change_prompt , 0 , sizeof( change_prompt ));	memset( discard , 0 , sizeof( discard ));	memset( initprompt , 0 , sizeof( initprompt ));	signal(SIGALRM, time_jump2); 	if ( setjmp(timejmp2) == 1) 	{ 		if( *line != 0 )		{			char oldprompt[100];			strcpy( oldprompt , initprompt );			get_last_line( line , initprompt);			util_log("Command line changed for user %s ,old=%s , new=%s \r\n",pw->pw_name, oldprompt , initprompt );			/*save line prompt to file*/			util_set_prompt( pw->pw_name , initprompt );						goto setcmd;		}		else		{			util_log("read command line timeout after %d millseconds\n",LOGINTIMEOUT );			expression_fail( newfd , 3 , clientip );			return -1;		}			}	if( debug )		util_log("Enter ret_command_prompt\n");	/*get the user's command prompt used by last time*/	if( util_get_prompt( pw->pw_name , initprompt ) == 0 )		util_log("Find the old saved prompt:%s for user :%s\n",initprompt , pw->pw_name );	else		util_log("can not find the old saved prompt for user :%s\n", pw->pw_name );	if( alarm( TIMEOUT ) == -1 )	{		util_err_log("setitimer error", __FILE__,__LINE__,errno );		exit(-3);	}			read_till_prompt(streamfd , initprompt, 1 , line , clientip , LOGINTIMEOUT);	alarm(0);	if(debug)	{		util_checkline(line , strlen( line ));		util_log("line = %s\n",line);	}	/*setup command line*/	/*csh using command: set PROMPT=xxx to setup command prompt*/setcmd:	if( pw->pw_shell )	{		if( strstr( pw->pw_shell , "csh" ) )			csh = 1;	}	if( csh == 1)	{		strcpy( change_prompt , "set prompt=\"");		strcat( change_prompt ,prompt ); 			strcat( change_prompt , "\"\r\n" );	}	else /*in the case of ksh ,sh or bash*/	{		strcpy( change_prompt , "PS1=" );		strcat( change_prompt ,prompt ); 		strcat( change_prompt , "\r\n" );	}	write( streamfd , change_prompt , strlen( change_prompt ));	if ( setjmp(timejmp2) == 1) 	{ 		util_log("wait for new set prompt time out after %d seconds \r\n", TIMEOUT);		expression_fail( newfd , 3 ,clientip);	}	/*setup timeout again*/	if( alarm( TIMEOUT ) == -1 )	{		util_err_log("setitimer error", __FILE__,__LINE__,errno );		exit(-3);	}	read_till_prompt(streamfd , prompt , 1 , line ,clientip, LOGINTIMEOUT);	ret[0] = IAC;	ret[1] = WILL;	ret[2] = TELOPT_USER_EX_LOGIN_RET;		memcpy( ret + 3 , initprompt , strlen( initprompt ) );	strcat( ret , "\r\n");	ret[ strlen( initprompt ) + 5 ] = 0;	write( newfd , ret , strlen( initprompt ) + 5 );	if(debug)		util_log( "expression login back str=%s\n",ret );	/*read in assurance infomation come from Magic C++ frontend*/	signal(SIGALRM, time_jump2);	if ( setjmp(timejmp2) == 1) 	{ 		util_log("wait for IAC DO TELOPT_USER_EX_LOGIN_RET time out after %d seconds \r\n", TIMEOUT);		expression_fail( newfd , 3 ,clientip);	}	alarm( TIMEOUT );	ensure[ 0 ]=IAC ;	ensure[ 1 ]=DO  ;	ensure[ 2 ]=TELOPT_USER_EX_LOGIN_RET ;	ensure[ 3 ] = 0;	read_till_prompt(newfd , ensure , 1 , line , clientip , -1);	util_log("Express Login Success for User:%s!\n",pw->pw_name);	alarm(0);	alarm( 0 );	signal(SIGALRM, SIG_IGN); 	return 0;}/*read data from term util the defined line appears*//*fd - term descriptor*//*prompt - the defined line to wait*//*explogin - non-zero for express login*//*line - the last come from terminal*//*timeout - timeout duration , -1 for no timeout*/int read_till_prompt(int fd , char * prompt , int explogin , char *line , char *clientip , int timeout){	int n ;	int first;	while(1)	{				n =	read(fd ,  line , BUFSIZ);		line[ n ] = 0;		if ( n == 0 )		{			if( explogin == 1 )				expression_fail( fd , 3 , clientip);#ifdef HAVE_UTMPX_H			clearuputmpx();#endif			util_err_log("read error",__FILE__,__LINE__,errno );			exit(0);		}		else if (n < 0)		{			if ( errno == EIO )			{				continue;			}			else 			{				if( explogin == 1 )					expression_fail( fd , 3 , clientip);#ifdef HAVE_UTMPX_H				clearuputmpx();#endif				util_err_log("pty read error", __FILE__,__LINE__ , errno);				if( getlicence )					release_licencecnt(clientip);				exit(3);			}		}		/*read in datas , check the last line...*/		else		{			char newprompt[BUFSIZ];			char doubleprompt[BUFSIZ] ; /*some times the command prompt is doubled*/			if( debug )			{				util_log("wait for command prompt...len=%d:\n",n );				util_checkline(line , n );			}			/*setup timeout again*/			if( timeout >= 0 )			{				if( alarm(  TIMEOUT ) == -1 )				{					util_err_log("setitimer error", __FILE__,__LINE__,errno );					exit(-3);				}			}			memset( newprompt , 0 , sizeof( newprompt ));			get_last_line( line , newprompt );			/*sometime there are double command prompt in SUN,so I had to check it twice...*/			strcpy(doubleprompt , prompt );			strcat(doubleprompt , prompt );			if( debug )				util_log( "doubleprompt=%s!\n",doubleprompt);				if( *prompt && ( !strcmp( newprompt , prompt) || !strcmp( newprompt , doubleprompt) ) )			{				if( debug )					util_log( "Command line matches!\n");				break;			}		}	}}/*express login failed*//* return to frontend:IAC WONT TELOPT_USER_EX_LOGIN_RET nn is the error no*/int expression_fail(int fd , int n , char *clientip){	char ret[ 6 ];	char back[3];	char buffer[BUFSIZ];	int len ;	int i;	int find = 0;	char ensure[3];	ret[0] = IAC;	ret[1] = WONT;	ret[2] = TELOPT_USER_EX_LOGIN_RET;	ret[3] = n + 48;	ret[4] = '\r';	ret[5] = '\n';	write( fd , ret , 6 );	util_log("ret a express error %s to foreground\n",ret);	signal(SIGALRM, time_jump2); 	alarm( TIMEOUT);	if ( setjmp(timejmp2) == 1) 	{		alarm( 0 );		util_log("get IAC DO TELOPT_USER_EX_LOGIN_RET time out!\n");		return 0;			}	ensure[ 0 ]=IAC ;	ensure[ 1 ]=DO  ;	ensure[ 2 ]=TELOPT_USER_EX_LOGIN_RET ;	read_till_prompt(fd , ensure , 0 , buffer , clientip ,-1 );	util_log( "get IAC DO TELOPT_USER_EX_LOGIN_RET:%s",back );	alarm( 0 );	return 0;}/*get the last line of a string*//*str - input string*//*line - the last line of string*/int get_last_line(char *str , char *line){	/*like 'aaa\nbbb\n*/	/*return bbb*/	char *ptr;	char *newstr;	int n;	newstr = strdup( str );	n = strlen( newstr ) ;	if( newstr[ n - 1 ] == '\n' )		newstr[ n - 1 ] = 0;	ptr = strrchr( newstr , '\n' );	if( !ptr )		ptr = newstr;	else		++ptr;	strcpy( line , ptr );	free( newstr );}

⌨️ 快捷键说明

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