리눅스 원격에서 쉘 뛰우는 프로그램(c언어 )ㅡ 질문 (이전글 잘못 올림 )
글쓴이: cksdnd0987 / 작성시간: 일, 2018/07/08 - 1:18오전
안녕하세요.
리눅스환경에서 원격에서 쉘을 뛰어서 명령어를 내릴 수 있는 프로그램을 작성중입니다.
dup2를 이용해서 표준 입출력을 소켓으로 바꿔놓고 원격에서 명령어를 내리고 결과를 원격에서 받는것을 목적으로 삼고있습니다.현재 상황은 몇가지 명령어는 구현되고있으나, login,vi 등의 명령어는 구현이 안되더군요..
무엇이 문제일까요?코드 첨부합니다.
이것때문에 몇일동안 고민중이네요..
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <signal.h> #include <sys/wait.h> #include <arpa/inet.h> #include <sys/socket.h> void error_handling(char *message); void zombie_kill(int sig); int main(int argc,char *argv[]){ struct sigaction act; // signal handle act.sa_handler=zombie_kill; sigemptyset(&act.sa_mask); act.sa_flags=0; if(sigaction(SIGCHLD,&act,0)==-1) error_handling("sigaction error"); // signal handle int server_sock,client_sock; struct sockaddr_in server_addr,client_addr; pid_t pid; socklen_t sock_len; char buf[500]; // 수정 필요 if(argc!=2){ printf("how to use:%s <port>\n",argv[0]); exit(1); } server_sock=socket(PF_INET,SOCK_STREAM,0); int option; socklen_t optlen; optlen=sizeof(option); option=1; setsockopt(server_sock,SOL_SOCKET,SO_REUSEADDR,(void*)&option,optlen); memset(&server_addr,0,sizeof(server_addr)); server_addr.sin_family=AF_INET; server_addr.sin_addr.s_addr=htonl(INADDR_ANY); server_addr.sin_port=htons(atoi(argv[1])); if(bind(server_sock,(struct sockaddr*)&server_addr,sizeof(server_addr))==-1) error_handling("bind error"); if(listen(server_sock,5)==-1) error_handling("listen error"); while(1){ // while start ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ sock_len=sizeof(client_addr); client_sock=accept(server_sock,(struct sockaddr*)&client_addr,&sock_len); if(client_sock==-1) continue; else printf("client connected: %d\n",client_sock); pid=fork(); if(pid==-1){ close(client_sock); continue; } if(pid==0){ close(server_sock); read(client_sock,buf,sizeof(buf)); int out,in,err; dup2(client_sock, 2); dup2(client_sock, 1); dup2(client_sock, 0); close(client_sock); char *command1[4]={0,}; int i; char *pch; while(1){ memset(buf,0,sizeof(buf)); fgets(buf,sizeof(buf),stdin); buf[strlen(buf)-2]=0; i=0; pch=strtok(buf," "); while(pch !=NULL && i<3){ command1[i]=pch; pch = strtok (NULL, " "); i++; } // while end command1[i]=(char*)0; if(fork()==0){ execvp(command1[0], command1); printf("command not found \n"); exit(1); } // 자식 부분 } // while } // 자식 프로세스 부분 끝 else close(client_sock); } // while end ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ }//main void zombie_kill(int sig){ pid_t pid; int status; pid=waitpid(-1,&status,WNOHANG); //printf("%d\n",pid); } void error_handling(char *message){ fputs(message,stderr); fputc('\n',stderr); exit(1); }
Forums:
댓글 달기