/* * whoisserver.c - main * from Doug Comer Internetworking with TCP/IP vol 1 pp. 359 */ #include #include #include #include #include #include #define bcopy(a,b,c) strncpy(b,a,c) /* for those who don't have bcopy */ /*---------------------------------------------------------- * Program : whoisserver * Purpose : UNIX application program that acts as a server * for 'whois' service on the local machine. It listens * on well-known WHOIS port (43) and answers queries from * clients. This program requires super-user privilege to * run. * Use : whois hostname username * Author : Barry Shein, Boston University * Date : January, 1987 * * Modification : Since the original program requires super-user privilege * we modify the program to use a non-privilege port so * every one can execute it. We use the port 2500 (randomly * chosen. * Xiannong Meng, Bucknell University, 1/16/93 *---------------------------------------------------------- */ #define BACKLOG 5 /* # of requests to queue */ #define MAXHOSTNAME 32 /* maximum host name length */ #define WHOISPORT 2500 /* a non-privilege port so all can use */ main(argc,argv) int argc; char *argv[]; { int s, t; /* socket descriptor */ int i; /* general purpose integer */ struct sockaddr_in sa, isa; /* internet socket structure */ struct hostent *hp; /* host name structure */ char *myname; /* pointer to the name of this program */ struct servent *sp; /* pointer to srevice entry */ char localhost[MAXHOSTNAME+1]; /* local host name as character string */ myname = argv[0]; /* * Get our own host information */ gethostname(localhost,MAXHOSTNAME); if ((hp = gethostbyname(localhost)) == NULL) { fprintf(stderr,"%s: Cannnot get local host info.\n",myname); exit(1); } /* * Put the WHOIS socket number and our address info * into the socket structure */ sa.sin_port = WHOISPORT; bcopy((char *)hp->h_addr,(char *)&sa.sin_addr,hp->h_length); sa.sin_family = hp->h_addrtype; /* * The following line is added by X. Meng * without it, bind() fails. */ sa.sin_addr.s_addr = htonl(INADDR_ANY); /* * Allocate an open socket for the incoming connections */ if ((s = socket(hp->h_addrtype,SOCK_STREAM,0)) < 0) { perror("socket"); exit(1); } /* * Bind the socket to the service port so * we can hear incoming connection */ if (bind(s,(struct sockaddr *)(&sa),sizeof sa) < 0) { perror("bind"); exit(1); } /* * Set maximum connections we will fall behind */ listen(s, BACKLOG); /* * Go into an infinite loop waiting for new connections */ while (1) { i = sizeof sa; /* * We hang in accept() while waiting for new customers */ if ((t = accept(s,(struct sockaddr *)(&isa),&i)) < 0) { perror("accept"); exit(1); } whois(t); /* perform the actual WHOIS service */ close(t); } } /* * Get the WHOIS request from remote host and format a reply */ whois(sock) int sock; { struct passwd *p; char buf[BUFSIZ+1]; int i; /* * Get one line request */ if ((i = read(sock,buf,BUFSIZ)) <= 0) return; buf[i] = '\0'; /* * Look up the requested user and format a reply */ if ((p = getpwnam(buf)) == NULL) strcpy(buf,"User not found\n"); else /* * 'pw_name' is the user login name * 'pw_gecos' is the real name section */ sprintf(buf,"%s: %s\n",p->pw_name,p->pw_gecos); /* * Return reply */ write(sock,buf,strlen(buf)); return; }