huseyinkozan
January 1st, 2009, 11:06 PM
I couldnt find with searching, if this was spoken before sorry.
I want to make a server program which will run commands on remote console, (for now with telnet)
I wrote some code like below, but when I take the command with recv() and pass the char array to execlp() (or execve,execl...) stderr says:
:not found
But when I pass an direct string like "ls" it works.
Where I am wrong ?
Thanks
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#define MYPORT 3456
#define BACKLOG 10
void sigchild_handler(int s)
{
while(wait(NULL)>0);
}
int main(int argc, char * argv[])
{
int sockfd, newfd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
struct sigaction sa;
int yes = 1;
if( (sockfd = socket(AF_INET, SOCK_STREAM, 0) ) == -1 )
{
perror("socket");
return EXIT_FAILURE;
}
if( (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int))) == -1 )
{
perror("setsockopt");
return EXIT_FAILURE;
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(my_addr.sin_zero),'\0',8);
if( bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1 )
{
perror("bind");
return EXIT_FAILURE;
}
if( listen(sockfd, BACKLOG) == -1 )
{
perror("listen");
return EXIT_FAILURE;
}
sa.sa_handler = sigchild_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if( sigaction(SIGCHLD, &sa, NULL) == -1 )
{
perror("sigaction");
return EXIT_FAILURE;
}
while(1)
{
int st;
sin_size = sizeof(struct sockaddr_in);
if( (newfd = accept(sockfd, (struct sockaddr*) &their_addr, (socklen_t*)&sin_size)) == -1 )
{
perror("accept");
continue;
}
printf("Server got connection from %s\n", inet_ntoa(their_addr.sin_addr));
pid_t pid = fork();
if( pid == 0 )
{
close(sockfd);
char commbuff[80] = {0};
char commbuffer[80] = {0};
size_t r;
if( (r = recv(newfd, commbuff, 79, 0)) == -1)
{
perror("recv");
}
commbuff[r]='\n';
//sprintf(commbuffer, "/bin/sh -c \"%s\"",commbuff);
dup2(newfd, 2);
dup2(newfd, 1);
dup2(newfd, 0);
close(newfd);
//system( commbuffer );
execlp("/bin/sh", "sh", "-c", commbuff,(char *)NULL);
perror("Couldn't exec");
return EXIT_FAILURE;
}
else
{
close(newfd);
waitpid(pid, &st, 0);
}
}
return EXIT_SUCCESS;
}
I want to make a server program which will run commands on remote console, (for now with telnet)
I wrote some code like below, but when I take the command with recv() and pass the char array to execlp() (or execve,execl...) stderr says:
:not found
But when I pass an direct string like "ls" it works.
Where I am wrong ?
Thanks
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#define MYPORT 3456
#define BACKLOG 10
void sigchild_handler(int s)
{
while(wait(NULL)>0);
}
int main(int argc, char * argv[])
{
int sockfd, newfd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
struct sigaction sa;
int yes = 1;
if( (sockfd = socket(AF_INET, SOCK_STREAM, 0) ) == -1 )
{
perror("socket");
return EXIT_FAILURE;
}
if( (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int))) == -1 )
{
perror("setsockopt");
return EXIT_FAILURE;
}
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(my_addr.sin_zero),'\0',8);
if( bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1 )
{
perror("bind");
return EXIT_FAILURE;
}
if( listen(sockfd, BACKLOG) == -1 )
{
perror("listen");
return EXIT_FAILURE;
}
sa.sa_handler = sigchild_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if( sigaction(SIGCHLD, &sa, NULL) == -1 )
{
perror("sigaction");
return EXIT_FAILURE;
}
while(1)
{
int st;
sin_size = sizeof(struct sockaddr_in);
if( (newfd = accept(sockfd, (struct sockaddr*) &their_addr, (socklen_t*)&sin_size)) == -1 )
{
perror("accept");
continue;
}
printf("Server got connection from %s\n", inet_ntoa(their_addr.sin_addr));
pid_t pid = fork();
if( pid == 0 )
{
close(sockfd);
char commbuff[80] = {0};
char commbuffer[80] = {0};
size_t r;
if( (r = recv(newfd, commbuff, 79, 0)) == -1)
{
perror("recv");
}
commbuff[r]='\n';
//sprintf(commbuffer, "/bin/sh -c \"%s\"",commbuff);
dup2(newfd, 2);
dup2(newfd, 1);
dup2(newfd, 0);
close(newfd);
//system( commbuffer );
execlp("/bin/sh", "sh", "-c", commbuff,(char *)NULL);
perror("Couldn't exec");
return EXIT_FAILURE;
}
else
{
close(newfd);
waitpid(pid, &st, 0);
}
}
return EXIT_SUCCESS;
}