145 lines
3.2 KiB
C
145 lines
3.2 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <termios.h>
|
|
#include <errno.h>
|
|
#include <sys/types.h>
|
|
#include <pwd.h>
|
|
#include <sys/time.h>
|
|
#include <sys/file.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <netdb.h>
|
|
#include <signal.h>
|
|
#include "config.h"
|
|
|
|
extern char buf[512];
|
|
extern char sockbuf[512],*sptr;
|
|
extern int PORTNUM;
|
|
extern int level;
|
|
|
|
|
|
char *linetok[256];
|
|
int USEIDENT= 0;
|
|
int call_socket(hostname,portnum)
|
|
char *hostname;
|
|
int portnum;
|
|
{ struct sockaddr_in sa;
|
|
struct hostent *hp;
|
|
int a, s;
|
|
if((hp=gethostbyname(hostname))==NULL) {
|
|
errno=ECONNREFUSED;
|
|
return(-1); }
|
|
bzero(&sa, sizeof(sa));
|
|
bcopy(hp->h_addr, (char *)&sa.sin_addr, hp->h_length);
|
|
sa.sin_family = hp->h_addrtype;
|
|
sa.sin_port = htons((u_short)portnum);
|
|
if((s=socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) return(-1);
|
|
if(connect(s,(struct sockaddr *) &sa, sizeof(sa)) < 0) {
|
|
close(s);
|
|
return(-1); }
|
|
return(s);
|
|
}
|
|
|
|
int readln(int sckfd)
|
|
{ int i=0,valid=1; char c;
|
|
while(valid) {
|
|
if(read(sckfd,&c,1)<1) return(0);
|
|
if(i<511 && c != '\n') sockbuf[i++]=c; else valid=0; }
|
|
sockbuf[i]='\0'; return(1);
|
|
}
|
|
|
|
int writeln(sckfd,outbuf)
|
|
int sckfd;
|
|
char *outbuf;
|
|
{ int to=0;
|
|
if(write(sckfd, outbuf, strlen(outbuf)) < to )
|
|
return(0);
|
|
return(1);
|
|
}
|
|
|
|
int readcom(sck,tout)
|
|
int sck;
|
|
int tout;
|
|
{
|
|
int sok;
|
|
time_t time_in,time_out;
|
|
int dumb = 1;
|
|
fd_set readfs;
|
|
struct timeval *zimeout;
|
|
time_in=time(NULL);
|
|
while(1) {
|
|
time_out = time(NULL);
|
|
if(time_out - time_in > tout) return 0;
|
|
FD_ZERO(&readfs);
|
|
FD_SET(sck,&readfs);
|
|
if(select(FD_SETSIZE, &readfs, NULL, NULL,(dumb ? NULL : zimeout))) {
|
|
|
|
if(FD_ISSET(sck,&readfs)) {
|
|
if(!BACKGROUND) puts("READING REPLY");
|
|
sok = readln(sck);
|
|
if (sok) {
|
|
sptr=sockbuf;
|
|
*(sptr+strlen(sockbuf)-1)='\0';
|
|
return 1;
|
|
} }
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
int do_auth(hostname,sockfd)
|
|
char *hostname;
|
|
int sockfd;
|
|
{ int authfd,ulen,tlen,lcnt,ct;
|
|
struct sockaddr_in sock,us,them;
|
|
|
|
writeln(sockfd,"Checking your identification\n");
|
|
|
|
if( (authfd=call_socket(hostname,113)) < 0) /* host not running identd */
|
|
{
|
|
USEIDENT=0;
|
|
sprintf(buf,"[%d] [%s] You should ask your sysadmin to instal identd\n",
|
|
authfd,hostname);
|
|
writeln(sockfd,buf);
|
|
return 0;
|
|
}
|
|
|
|
if(getsockname(sockfd,(struct sockaddr *)&us,&ulen))
|
|
{ if(!BACKGROUND) puts("getsockname failed");
|
|
USEIDENT=0;
|
|
return 0;
|
|
}
|
|
if(getpeername(sockfd,(struct sockaddr *)&them,&tlen))
|
|
{ if(!BACKGROUND) puts("getpeername failed");
|
|
USEIDENT=0;
|
|
return 0;
|
|
}
|
|
|
|
sprintf(buf,"%u,%u \r\n",
|
|
(unsigned int) ntohs(them.sin_port),
|
|
(unsigned int) PORTNUM);
|
|
|
|
writeln(authfd,buf);
|
|
writeln(sockfd,"CHECKING IDENTD\n");
|
|
|
|
readcom(authfd,15);
|
|
if(!BACKGROUND) puts(sockbuf);
|
|
|
|
linetok[lcnt=0]=strtok(sockbuf," ");
|
|
while(linetok[++lcnt]=strtok(NULL, " ")); linetok[lcnt]=NULL;
|
|
|
|
if((lcnt > 7) && (!strcmp(linetok[4],"USERID")))
|
|
{ USEIDENT = 1;
|
|
sprintf(buf,"Greetings %s@%s, Welcome to our system\n",linetok[8],
|
|
hostname);
|
|
writeln(sockfd,buf);
|
|
if(!BACKGROUND)
|
|
{ printf("GOT IT %s %d\n",linetok[8],lcnt);
|
|
}
|
|
}
|
|
else USEIDENT = 0;
|
|
close(authfd);
|
|
}
|