2004-04-03 Wilfried Busalski <w.busalski@lancier-monitoring.de>

PR 599/pppd
	* pppd/chat.c: Fre memory that is allocated to fix leak.
This commit is contained in:
Joel Sherrill
2004-04-03 16:33:21 +00:00
parent 9cec348537
commit 869f6029d8

View File

@@ -82,6 +82,18 @@
* *
*/ */
/* $Id$ */
/*
Fixes and some Changes by Wilfried Busalski Lancier-Monitoring GmbH Germany
wilfried.busalski@muenster.de
Fixes: put_string() Free memory allocated by clean()
get_string() Free memory allocated by clean()
chat_main() Will Distroy's no more the chat-script
getnextcommand() sepatator changed to '@'
*/
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>
#include <time.h> #include <time.h>
@@ -209,43 +221,53 @@ char *s;
return dup_mem(s, strlen (s) + 1); return dup_mem(s, strlen (s) + 1);
} }
char *getnextcommand(char **string) char *getnextcommand(char *string,char *buff)
{ {
char *buf=*string,*res; char *token;
res=strchr(buf,'|'); int len;
if (res==NULL)
token=strchr(string,'@');
if (token==NULL){
return NULL; return NULL;
*res='\0'; }
*string=res+1; len=token-string;
return buf; if(len > 78 ){
len=78;
}
memcpy(buff,string,len);
buff[len]=0;
return(token+1);
} }
int chatmain(int fd, int mode, char *pScript) int chatmain(int fd, int mode, char *pScript)
{ {
char *arg; char arg[80];
char *script;
/* initialize exit code */ /* initialize exit code */
exit_code = 0; exit_code = 0;
ttyfd = fd; ttyfd = fd;
script=pScript;
if ( debug ) { if ( debug ) {
dbglog("chat_main: %s\n", pScript); dbglog("chat_main: %s\n", script);
} }
/* get first expect string */ /* get first expect string */
arg = getnextcommand(&pScript); script = getnextcommand(script,arg);
while (( arg != NULL ) && ( exit_code == 0 )) { while (( script != NULL ) && ( exit_code == 0 )) {
/* process the expect string */ /* process the expect string */
chat_expect(arg); chat_expect(arg);
if ( exit_code == 0 ) { if ( exit_code == 0 ) {
/* get the next send string */ /* get the next send string */
arg = getnextcommand(&pScript); script = getnextcommand(script,arg);
if ( arg != NULL ) { if ( script != NULL ) {
/* process the send string */ /* process the send string */
chat_send(arg); chat_send(arg);
/* get the next expect string */ /* get the next expect string */
arg = getnextcommand(&pScript); script = getnextcommand(script,arg);
} }
} }
} }
@@ -622,11 +644,10 @@ register char *s;
abort_next = 0; abort_next = 0;
if ( n_aborts < MAX_ABORTS ) { if ( n_aborts < MAX_ABORTS ) {
s1 = clean(s, 0); s1 = clean(s, 0);
if (( strlen(s1) <= strlen(s) ) && if (( strlen(s1) <= strlen(s) ) && ( strlen(s1) < sizeof(fail_buffer))) {
( strlen(s1) < sizeof(fail_buffer))) {
abort_string[n_aborts++] = s1; abort_string[n_aborts++] = s1;
} }
free(s1);
} }
return; return;
} }
@@ -650,20 +671,24 @@ register char *s;
timeout_next = 0; timeout_next = 0;
timeout = atoi(s); timeout = atoi(s);
if (timeout <= 0) if (timeout <= 0){
timeout = DEFAULT_CHAT_TIMEOUT; timeout = DEFAULT_CHAT_TIMEOUT;
}
return; return;
} }
if (strcmp(s, "EOT") == 0) if (strcmp(s, "EOT") == 0){
s = "^D\\c"; s = "^D\\c";
else if (strcmp(s, "BREAK") == 0) }
else{
if (strcmp(s, "BREAK") == 0){
s = "\\K\\c"; s = "\\K\\c";
}
if (!put_string(s)) { if (!put_string(s)) {
exit_code = 2; exit_code = 2;
} }
}
} }
static int get_char() static int get_char()
@@ -690,15 +715,13 @@ int c;
{ {
char ch = c; char ch = c;
write(ttyfd, &ch, 1); return(write(ttyfd, &ch, 1));
return 0;
} }
static int write_char (c) static int write_char (c)
int c; int c;
{ {
if (put_char(c) < 0) { if (put_char(c) < 1) {
return (0); return (0);
} }
return (1); return (1);
@@ -707,18 +730,23 @@ int c;
static int put_string (s) static int put_string (s)
register char *s; register char *s;
{ {
char *out,*free_ptr=0;
quiet = 0; quiet = 0;
s = clean(s, 1); out = free_ptr = clean(s, 1);
while (*s) { while (*out) {
register char c = *s++; register char c = *out++;
if (c != '\\') { if (c != '\\') {
if (!write_char (c)) if (!write_char (c)){
free(free_ptr);
return 0; return 0;
}
continue; continue;
} }
c = *s++; c = *out++;
switch (c) { switch (c) {
case 'd': case 'd':
sleep(1); sleep(1);
@@ -737,11 +765,14 @@ register char *s;
break; break;
default: default:
if (!write_char (c)) if (!write_char (c)){
free(free_ptr);
return 0; return 0;
}
break; break;
} }
} }
free(free_ptr);
return (1); return (1);
} }
@@ -749,12 +780,13 @@ register char *s;
/* /*
* 'Wait for' this string to appear on this file descriptor. * 'Wait for' this string to appear on this file descriptor.
*/ */
static int get_string(string) static int get_string(in_string)
register char *string; register char *in_string;
{ {
int c, len, minlen; int c, len, minlen;
register char *s = temp2, *end = s + STR_LEN; register char *s = temp2, *end = s + STR_LEN;
char *logged = temp2; char *logged = temp2;
char *string=0;
struct termios tios; struct termios tios;
memset(temp2, 0, sizeof(temp2)); memset(temp2, 0, sizeof(temp2));
@@ -764,12 +796,13 @@ register char *string;
tios.c_cc[VTIME] = timeout*10/MAX_TIMEOUTS; tios.c_cc[VTIME] = timeout*10/MAX_TIMEOUTS;
tcsetattr(ttyfd, TCSANOW, &tios); tcsetattr(ttyfd, TCSANOW, &tios);
string = clean(string, 0); string = clean(in_string, 0);
len = strlen(string); len = strlen(string);
minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1; minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
if (len > STR_LEN) { if (len > STR_LEN) {
exit_code = 1; exit_code = 1;
free(string);
return 0; return 0;
} }
@@ -780,12 +813,19 @@ register char *string;
while ( (c = get_char()) >= 0) { while ( (c = get_char()) >= 0) {
int n, abort_len; int n, abort_len;
if(c == '\n' || c == '\r'){
s = temp2;
*s=0;
}
else{
*s++ = c; *s++ = c;
*s=0; *s=0;
}
if (s - temp2 >= len && if (s - temp2 >= len &&
c == string[len - 1] && c == string[len - 1] &&
strncmp(s - len, string, len) == 0) { strncmp(s - len, string, len) == 0) {
free(string);
return (1); return (1);
} }
@@ -795,6 +835,7 @@ register char *string;
exit_code = n + 4; exit_code = n + 4;
strcpy(fail_reason = fail_buffer, abort_string[n]); strcpy(fail_reason = fail_buffer, abort_string[n]);
free(string);
return (0); return (0);
} }
} }
@@ -811,5 +852,7 @@ register char *string;
} }
exit_code = 3; exit_code = 3;
free(string);
return (0); return (0);
} }