From f9e9546ae51a4669a4783ea8d1c158c0b93456a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Groenvall?= Date: Tue, 19 Dec 1995 09:31:22 +0000 Subject: [PATCH] Make popper timeout after 120 seconds. git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@204 ec53bebd-3082-4978-b11e-865c3cabbd6b --- appl/popper/pop_init.c | 5 ++++ appl/popper/pop_send.c | 6 +++++ appl/popper/popper.c | 58 +++++++++++++++++++++++++++++++++++++++--- appl/popper/popper.h | 5 ++++ 4 files changed, 70 insertions(+), 4 deletions(-) diff --git a/appl/popper/pop_init.c b/appl/popper/pop_init.c index 6d14d6296..c1b624aac 100644 --- a/appl/popper/pop_init.c +++ b/appl/popper/pop_init.c @@ -84,6 +84,11 @@ char ** argmessage; trace_file_name = optarg; break; + /* Timeout value passed. Default changed */ + case 'T': + pop_timeout = atoi(optarg); + break; + /* Unknown option received */ default: errflag++; diff --git a/appl/popper/pop_send.c b/appl/popper/pop_send.c index a760536a3..7e32c5edd 100644 --- a/appl/popper/pop_send.c +++ b/appl/popper/pop_send.c @@ -88,6 +88,8 @@ POP * p; strcat(tmpbuf, "\n"); if (strlen(tmpbuf) < MAXMSGLINELEN) { pop_sendline (p,tmpbuf); + if (hangup) + return (pop_msg (p,POP_FAILURE,"SIGHUP or SIGPIPE flagged")); return_path_sent++; } } @@ -107,6 +109,8 @@ POP * p; end of the header. sendline() converts this to a NULL, so that's what we look for. */ if (*buffer == 0) break; + if (hangup) + return (pop_msg (p,POP_FAILURE,"SIGHUP or SIGPIPE flagged")); } /* Send the message body */ while (fgets(buffer,MAXMSGLINELEN,p->drop)) { @@ -115,6 +119,8 @@ POP * p; /* Decrement the lines sent (for a TOP command) */ if (msg_lines >= 0 && msg_lines-- == 0) break; pop_sendline(p,buffer); + if (hangup) + return (pop_msg (p,POP_FAILURE,"SIGHUP or SIGPIPE flagged")); } /* "." signals the end of a multi-line transmission */ (void)fputs(".\r\n",p->output); diff --git a/appl/popper/popper.c b/appl/popper/popper.c index 540ab0a44..b31e079eb 100644 --- a/appl/popper/popper.c +++ b/appl/popper/popper.c @@ -11,10 +11,52 @@ static char SccsId[] = "@(#)@(#)popper.c 2.1 2.1 3/18/91"; #include #include +#include +#include #include "popper.h" extern state_table * pop_get_command(); +int hangup = FALSE ; + +static int +catchSIGHUP() +{ + hangup = TRUE ; + + /* This should not be a problem on BSD systems */ + signal(SIGHUP, (void *)catchSIGHUP); + signal(SIGPIPE, (void *)catchSIGHUP); +} + +int pop_timeout = POP_TIMEOUT; + +jmp_buf env; + +static int +ring() +{ + longjmp(env,1); +} + +/* + * fgets, but with a timeout + */ +static char * +tgets(char *str, int size, FILE *fp, int timeout) +{ + int ring(); + (void) signal(SIGALRM, (void *)ring); + alarm(timeout); + if (setjmp(env)) + str = NULL; + else + str = fgets(str,size,fp); + alarm(0); + signal(SIGALRM,SIG_DFL); + return(str); +} + /* * popper: Handle a Post Office Protocol version 3 session */ @@ -26,6 +68,9 @@ char ** argv; state_table * s; char message[MAXLINELEN]; + (void) signal(SIGHUP,(void *)catchSIGHUP); + (void) signal(SIGPIPE,(void *)catchSIGHUP); + /* Start things rolling */ pop_init(&p,argc,argv); @@ -40,11 +85,16 @@ char ** argv; until the client quits or an error occurs. */ for (p.CurrentState=auth1;p.CurrentState!=halt&&p.CurrentState!=error;) { - - /* Obtain a line from the client */ - if (fgets(message,MAXLINELEN,p.input) == NULL) { + if (hangup) { + pop_msg(&p,POP_FAILURE,"POP hangup",p.myhost); + if (p.CurrentState > auth2 && !pop_updt(&p)) + pop_msg(&p,POP_FAILURE,"POP mailbox update failed.",p.myhost); + p.CurrentState = error; + } else if (tgets(message,MAXLINELEN,p.input,pop_timeout) == NULL) { + pop_msg(&p,POP_FAILURE,"POP timeout",p.myhost); + if (p.CurrentState > auth2 && !pop_updt(&p)) + pop_msg(&p,POP_FAILURE,"POP mailbox update failed!",p.myhost); p.CurrentState = error; - pop_msg(&p,POP_FAILURE,"POP server at %s signing off.",p.myhost); } else { /* Search for the command in the command/state table */ diff --git a/appl/popper/popper.h b/appl/popper/popper.h index e52648a9b..6c193e246 100644 --- a/appl/popper/popper.h +++ b/appl/popper/popper.h @@ -75,9 +75,14 @@ #define POP_SUCCESS 1 #define POP_FAILURE 0 #define POP_TERMINATE '.' +#define POP_TIMEOUT 120 /* timeout connection after this many secs */ extern int errno; +extern int pop_timeout; + +extern int hangup; + #define pop_command pop_parm[0] /* POP command is first token */ #define pop_subcommand pop_parm[1] /* POP XTND subcommand is the second token */