Solved

Sockets etc

Posted on 2002-03-03
7
212 Views
Last Modified: 2010-04-22
I've written a simple program that telnets to a server and executes a couple of commands. The program uses sockets.
When I run the program as a normal user it works fine, but when Itry to run it as someone chrooted to let's say /testdir it returns can't get hostname.
I've included the source for the program below.
Any ideas how to make the program run under a chrooted user?

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/param.h>

#define SIZE 2048

#define TIME_PORT 6789

int main(int argc,
         char *argv[])
{
  int sockfd;
  int nread;
  int port;
  char message[SIZE], tempmess[SIZE];
  char * host, name[ SIZE ];
  char * pathet;
  struct hostent * ptrh;
  struct sockaddr_in serv_addr;
  char buf[ SIZE ];
  int count;

  port = TIME_PORT;
  serv_addr.sin_port = htons( (u_short) port );

   
  if ( argc > 1 )
    pathet  = argv[ 1 ];
     

  /* get the server host entry */
  if ((char *) ( ptrh = gethostbyname( "mail" ) ) == NULL ){
     fprintf(stderr, "No host name info\n");
     exit( 2 );
  }

  /* create socket */
  if ((sockfd = socket(PF_INET, SOCK_STREAM, 0))< 0) {
    fprintf(stderr, "Socket creation failed\n");
    exit(3);
  }

  /* connect to server */
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr = * ( struct in_addr * ) ptrh->h_addr_list[ 0 ];

  if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) <
0) {
    fprintf(stderr, "Connection failed \n");
    exit(4);
  }

  /* transfer data */
  nread = read(sockfd, buf, SIZE);
  write(1, buf, nread);

  write(sockfd, "USERNAME\r\n", nread);
  write(sockfd, "PASSWORD\r\n", nread);

  sleep(1);

  write(sockfd, pathet, 512);

  nread = read(sockfd, buf, SIZE);
  write(1, buf, nread); //output answer
  close(sockfd);

  exit(0);
}
0
Comment
Question by:nikitin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 5

Expert Comment

by:bryanh
ID: 6838208
Can you be more specific about "it returns can't get hostname"?
0
 

Author Comment

by:nikitin
ID: 6838593
This is the part that fails, it returns 'No host name info'

 /* get the server host entry */
 if ((char *) ( ptrh = gethostbyname( "mail" ) ) == NULL ){
    fprintf(stderr, "No host name info\n");
    exit( 2 );
 }
0
 
LVL 5

Expert Comment

by:garboua
ID: 6838851
pardon my ignorance, but what do you mean by "chrooted user"
your code segment looks good to me and no need to ask about the entries in your /etc/host or dns since it works when you run it as a normal user.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:MFCRich
ID: 6838949
If its chrooted to /testdir it can no longer find /etc/hosts to resolve the host name. Either use DNS or create '/test/etc/hosts'
0
 

Author Comment

by:nikitin
ID: 6839026
MFCRich> it still doesn't work, I've a added /etc/hosts in /testdir, with the following entry 192.168.1.1     mail     mail.

Any ideas on how I can make it work?
0
 
LVL 5

Accepted Solution

by:
bryanh earned 150 total points
ID: 6840709
I think it's pretty clear that you're just missing some files.  All you need to do is figure out which ones.  gethostbyname() is much more complex than it used to be (a few years ago) because of the GNU C library's NSS (Name Service Switch).  I spent a week one day getting it to work on my system.  Do you have, for example, /etc/nsswitch in your chroot environment?  Also, there are libraries that gethostbyname() dynamically loads to do the lookups.  E.g. libnss-files.so.  If it can't find them, it won't work.

I think the quickest way out of this is to run the 'strace' program on your program, both in the working case and in the non-working case.  Strace will show you what files it is accessing or attempting to access.

You might also try to build the name service from scratch by reading the GNU C library manual.
0
 

Author Comment

by:nikitin
ID: 6841149
strace worked like magic (strace -o output myprogram args)
I've copied all the necessary libs and files now and the program runs as smooth as ever.

thanks man!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question