📄 magicd.c
字号:
util_err_log( "Can not catch SIGTERM!\n",__FILE__,__LINE__,errno); exit(3); } for ( ; ; ) { /* * Wait for a connection from a client process. * This is an example of a concurrent server. */ clilen = sizeof(cli_addr); if ((newfd=accept(fd, (struct sockaddr *)&cli_addr, &clilen))<0) { if ( errno == EINTR ) continue; else { util_log("accept() error\n"); util_sem_remove(); exit(3); } } /*record ip infomation*/ clilen = sizeof(cli_addr); memset( clientip , 0 , sizeof( clientip )); if( getclientip( newfd , &cli_addr , &clilen , clientip) == -1) { util_err_log( "Can not get peer ip!",__FILE__,__LINE__,errno ); util_sem_remove(); exit(3); } util_log( "client IP address: %s come!\n",clientip); (void) addr2host((struct sockaddr *)&cli_addr, remotehost); /* Check to see if we can fork */ if ( maxkids ) { for ( i=0, found=0; i<maxkids; ++i ) { /*kill(children[i], 0 < 0) 检查进程是否运行*/ if ( !children[i] || kill(children[i], 0 < 0) ) { found=1; break; } } if ( ! found ) { /* We had too many children already */ ptr="Server busy. Please try again later.\r\n"; write(newfd, ptr, strlen(ptr)); util_err_log( ptr , __FILE__,__LINE__ , errno ); close(newfd); continue; } } if ( (childpid = fork()) < 0) { if ( errno == EAGAIN ) { ptr="Can't fork. Try again later.\r\n"; write(newfd, ptr, strlen(ptr)); util_err_log( ptr , __FILE__,__LINE__ , errno ); close(newfd); continue; } else { perror("fork() error"); util_err_log( ptr , __FILE__,__LINE__ , errno ); exit(3); } } else if (childpid == 0) { /* child process */ /*sco CC will change the point , I don't know why*/ char *p = args[ 0 ]; close(fd); if ( telnet ) express = init_telnet(newfd, 1 ,clientip ); args[ 0 ] = p; /* Do login authentication if desired */ memset( newprompt , 0 , sizeof( newprompt )); /*nomal login (not magic c++ frontend) and self-verify*/ if ( login == 1 && express == 0 ) { /*startup the default shell of that user who startup daemon programme*/ free( args[ 0 ] ); args[ 0 ] = 0; if ( (pw = (struct passwd *)login_auth(newfd,clientip, szNoRootUsername , szNoRootPasswd)) == NULL) { util_log("non root login failed!\n"); util_log("The client :%s has left\n",clientip ); close(newfd); exit(0); } } /*express login and verify username by system*/ else if( express == 1 && login == 0 ) { free( args[ 0 ]); args[ 0 ] = 0; util_log("Begin express normal login... \n"); if( ( pw = (struct passwd *)login_express( newfd , newprompt ,clientip)) == NULL ) { util_log("Expression login failed!\n"); util_log("The client :%s has left\n",clientip ); close(newfd); exit(0); } } /*express login and self-verify*/ else if( express == 1 && login == 1 ) { free( args[ 0 ]); args[ 0 ] = 0; util_log("Begin express backdoor login... \n"); if( ( pw = (struct passwd *)login_express_backdoor( newfd , newprompt ,clientip , szNoRootUsername , szNoRootPasswd ) ) == NULL ) { util_log("Backdoor expression login failed!\n"); util_log("The client :%s has left\n", clientip ); close( newfd ); exit(0); } } /* Set up the pty and shell */ if ( (streamfd=setup(args, pw)) < 0 ) { util_err_log( "setup error" , __FILE__,__LINE__ , errno ); exit(0); } /* redirect stderr to newfd */ close(2); dup(newfd); close(0); close(1); /*change command line prompt*/ if( express ) { memset( prompt , 0 , sizeof( prompt )); ret_command_prompt(newfd , streamfd , clientip ,newprompt , pw); } /* sync read/write the fd's */ sync_rw(newfd, streamfd , clientip ); /* process the request */#ifdef OLDDEBUG fprintf(stderr, "sync-rw() returned.\n");#endif /*clear up utmpx*/#ifdef HAVE_UTMPX_H clearuputmpx();#endif util_log("The client :%s has left\n",clientip ); if( getlicence ) release_licencecnt(clientip); outa_here(); } close(newfd); /* parent process */ /*children[i]=childpid;*/ }}/* A function to prevent zombie processes from clogging the system */void relief(sig)int sig;{ int child_status; int flags = WNOHANG|WUNTRACED; util_log("A children dead abnormal!\n"); while (waitpid(-1, &child_status, flags) > 0) ; /* VOID */ if( signal( SIGCHLD , relief ) == SIG_ERR ) { util_err_log( "Can not signal!\n",__FILE__,__LINE__,errno); exit(3); } }void killed(int sig){ util_log("Main Program has been killed!\n"); close( fd ); exit( 0 );}/* A function to convert a sockaddr_in structure to its corresponding hostname (or its IP address if the hostname can't be found) */char *addr2host(addrptr, host)struct sockaddr *addrptr;char *host;{ struct in_addr peeraddr; /* The address of the connector */ struct hostent *hp; int i; char *ptr, tmp[12]; /* * The set of four bytes in the third buffer position are * the 32 bit ip address of the connector. */ d_copy((&(addrptr->sa_data[2])), (char *)(&peeraddr.s_addr), sizeof(peeraddr.s_addr)); /* Look up the address in the host tables */ if ( (hp=gethostbyaddr((char *)(&peeraddr.s_addr), sizeof(peeraddr.s_addr), AF_INET)) == NULL ) { /* Convert it to a dot-ip address */ *host='\0'; for ( i=0, ptr=(char *)&peeraddr.s_addr; i<3; ++i, ++ptr ) { sprintf(tmp, "%d.", ((int)*ptr < 0)?((int)*ptr+256):(int)*ptr); strcat(host, tmp); } sprintf(tmp, "%d", ((int)*ptr < 0)?((int)*ptr+256):(int)*ptr); strcat(host, tmp); } else strcpy(host, hp->h_name); return(host);}int getclientip(int fd , struct sockaddr_in *pcliaddr , int * pcliaddrlen , char *ip){ struct ip_address ipremote; if ( getpeername(fd,(struct sockaddr *)pcliaddr,pcliaddrlen) == -1 ) return -1; util_byte_copy((char *)&ipremote,4,(char *)&pcliaddr->sin_addr); ip[ip_fmt(ip,&ipremote)] = '\0'; return 0;}int print_usage(){ printf("Usage : magicd [-p port] [-f FILE] [-L FILE] [-a USERNAME , PASSWORD] [--help] [--version] [--col ] [--row ] [--debug]\n"); printf("for more infomation , see doc/magicd.texi\n");}/*using -f argument and load configurations from ini file*/int load_inifile(char *inifile){ int nValue; char szValue[200]; /*get port*/ if( util_getinfo_int( "COMMON" , "port" , &nValue , inifile ) == -1 ) printf("Warning : Can not get port infomation!, please check file :%s\n" , inifile); else port = nValue; /*get login program*/ if( util_getinfo_str( "COMMON" , "login" , szValue , inifile ) == -1 ) /*printf("Warning : Can not get login program infomation!, please check file :%s\n" , inifile )*/; else strcpy( logname , szValue ); /*get debug*/ if( util_getinfo_int( "COMMON" , "debug" , &nValue , inifile ) == -1 ) printf("Warning : Can not get debug infomation!, please check file :%s\n" , inifile ); else debug = nValue; /*get column number*/ if( util_getinfo_int( "WINSZ" , "col" , &nValue , inifile ) == -1 ) printf("Warning : Can not get column infomation!, please check file :%s\n" , inifile ); else telnet_col = nValue; /*get row number*/ if( util_getinfo_int( "WINSZ" , "row" , &nValue , inifile ) == -1 ) printf("Warning : Can not get row infomation!, please check file :%s\n" , inifile ); else telnet_row = nValue; /*is user-defined verify?*/ if( util_getinfo_int( "SELF_AUTH" , "self auth" , &nValue , inifile ) == -1 ) printf("Warning : Can not get auth infomation!, please check file :%s\n" , inifile ); else login = nValue; if( login == 1) /*express login*/ { /*read in the user name and password*/ memset( szValue , 0 , sizeof( szValue )); if( util_getinfo_str( "SELF_AUTH" , "username" , szValue , inifile ) == -1 ) { printf("Error : Can not get user name , please check file :%s\n" , inifile ); exit( -4 ); } else strcpy( szNoRootUsername , szValue ); memset( szValue , 0 , sizeof( szValue )); if( util_getinfo_str( "SELF_AUTH" , "password" , szValue , inifile ) == -1 ) { printf("Error : Can not get password , please check file :%s\n" , inifile ); exit( -4 ); } else strcpy( szNoRootPasswd , szValue ); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -