Q: Error in system command traceroute c I'm trying to develop a traceroute server which accepts argument from a telnet client performs traceroute and send the information back to the telnet client. I'm receiving the argument from telnet client at the server for e.g. traceroute www.google.com but somehow when I try to execute it, it gives me a weird error:
: Name or service not known
' on position 1 (argc 1)line arg `www.google.com

What I found strange is when I hard code the command at the server end it works fine, also when I receive the command at the server end and print it out, that works fine too. However, the same command received in a character array it fails to execute with the above error.
Here's my code:
int main() {
int sockfd,new_fd;
char client_arg[100];

//Create a socket and establish the connection

if (recv(new_fd,client_arg,100,0)== -1)
            perror("recv");

printf("%s\n",client_arg); // prints traceroute www.google.com
system(client_arg);

return 0;
}


A: Many things could be wrong here:


*

*traceroute might not be in the default PATH of /bin/sh,

*the string might not be zero-terminated,

*the buffer might have some unprintable characters that printf would not show to you,

*you might receive a short read with partial command,

*anything else ...


It is a very Bad IdeaTM to blindly pass user (or network) input for execution like that. Do explicit checking. Pass only allowed commands. Check correctness of the arguments.
