source: rtems/cpukit/pppd/chat.c @ b5d91ad

4.104.114.84.95
Last change on this file since b5d91ad was 1a5787d, checked in by Joel Sherrill <joel.sherrill@…>, on 06/13/03 at 13:20:47

2003-06-13 Joel Sherrill <joel@…>

  • pppd/chat.c: Removed warnings.
  • Property mode set to 100644
File size: 15.4 KB
Line 
1/*
2 *      Chat -- a program for automatic session establishment (i.e. dial
3 *              the phone and log in).
4 *
5 * Standard termination codes:
6 *  0 - successful completion of the script
7 *  1 - invalid argument, expect string too large, etc.
8 *  2 - error on an I/O operation or fatal error condition.
9 *  3 - timeout waiting for a simple string.
10 *  4 - the first string declared as "ABORT"
11 *  5 - the second string declared as "ABORT"
12 *  6 - ... and so on for successive ABORT strings.
13 *
14 *      This software is in the public domain.
15 *
16 * -----------------
17 *      22-May-99 added environment substitutuion, enabled with -E switch.
18 *      Andreas Arens <andras@cityweb.de>.
19 *
20 *      12-May-99 added a feature to read data to be sent from a file,
21 *      if the send string starts with @.  Idea from gpk <gpk@onramp.net>.
22 *
23 *      added -T and -U option and \T and \U substitution to pass a phone
24 *      number into chat script. Two are needed for some ISDN TA applications.
25 *      Keith Dart <kdart@cisco.com>
26 *     
27 *
28 *      Added SAY keyword to send output to stderr.
29 *      This allows to turn ECHO OFF and to output specific, user selected,
30 *      text to give progress messages. This best works when stderr
31 *      exists (i.e.: pppd in nodetach mode).
32 *
33 *      Added HANGUP directives to allow for us to be called
34 *      back. When HANGUP is set to NO, chat will not hangup at HUP signal.
35 *      We rely on timeouts in that case.
36 *
37 *      Added CLR_ABORT to clear previously set ABORT string. This has been
38 *      dictated by the HANGUP above as "NO CARRIER" (for example) must be
39 *      an ABORT condition until we know the other host is going to close
40 *      the connection for call back. As soon as we have completed the
41 *      first stage of the call back sequence, "NO CARRIER" is a valid, non
42 *      fatal string. As soon as we got called back (probably get "CONNECT"),
43 *      we should re-arm the ABORT "NO CARRIER". Hence the CLR_ABORT command.
44 *      Note that CLR_ABORT packs the abort_strings[] array so that we do not
45 *      have unused entries not being reclaimed.
46 *
47 *      In the same vein as above, added CLR_REPORT keyword.
48 *
49 *      Allow for comments. Line starting with '#' are comments and are
50 *      ignored. If a '#' is to be expected as the first character, the
51 *      expect string must be quoted.
52 *
53 *
54 *              Francis Demierre <Francis@SwissMail.Com>
55 *              Thu May 15 17:15:40 MET DST 1997
56 *
57 *
58 *      Added -r "report file" switch & REPORT keyword.
59 *              Robert Geer <bgeer@xmission.com>
60 *
61 *      Added -s "use stderr" and -S "don't use syslog" switches.
62 *              June 18, 1997
63 *              Karl O. Pinc <kop@meme.com>
64 *
65 *
66 *      Added -e "echo" switch & ECHO keyword
67 *              Dick Streefland <dicks@tasking.nl>
68 *
69 *
70 *      Considerable updates and modifications by
71 *              Al Longyear <longyear@pobox.com>
72 *              Paul Mackerras <paulus@cs.anu.edu.au>
73 *
74 *
75 *      The original author is:
76 *
77 *              Karl Fox <karl@MorningStar.Com>
78 *              Morning Star Technologies, Inc.
79 *              1760 Zollinger Road
80 *              Columbus, OH  43221
81 *              (614)451-1883
82 *
83 */
84
85#include <stdio.h>
86#include <ctype.h>
87#include <time.h>
88#include <fcntl.h>
89#include <errno.h>
90#include <string.h>
91#include <stdlib.h>
92#include <unistd.h>
93#include <sys/types.h>
94#include <sys/stat.h>
95#include <syslog.h>
96#include <termios.h>
97#include "pppd.h"
98
99#undef  TERMIOS
100#define TERMIOS
101
102
103#define STR_LEN 1024
104char temp2[STR_LEN];
105
106#ifndef SIGTYPE
107#define SIGTYPE void
108#endif
109
110#undef __P
111#undef __V
112
113#ifdef __STDC__
114#include <stdarg.h>
115#define __V(x)  x
116#define __P(x)  x
117#else
118#include <varargs.h>
119#define __V(x)  (va_alist) va_dcl
120#define __P(x)  ()
121#define const
122#endif
123
124#ifndef O_NONBLOCK
125#define O_NONBLOCK      O_NDELAY
126#endif
127
128
129/*************** Micro getopt() *********************************************/
130#define OPTION(c,v)     (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
131                                (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
132                                &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
133#define OPTARG(c,v)     (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
134                                (_O=4,(char*)0):(char*)0)
135#define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
136#define ARG(c,v)        (c?(--c,*v++):(char*)0)
137
138#if 0
139static int _O = 0;              /* Internal state */
140#endif
141/*************** Micro getopt() *********************************************/
142
143#define MAX_ABORTS              16
144#define MAX_REPORTS             16
145#define DEFAULT_CHAT_TIMEOUT    45
146#define MAX_TIMEOUTS            10
147
148static int echo             = 0;
149static int quiet            = 0;
150static int use_env          = 0;
151static int exit_code        = 0;
152static char *phone_num      = (char *) 0;
153static char *phone_num2     = (char *) 0;
154static int ttyfd;
155static int timeout   = DEFAULT_CHAT_TIMEOUT;
156
157#ifdef TERMIOS
158#define term_parms struct termios
159#define get_term_param(param) tcgetattr(0, param)
160#define set_term_param(param) tcsetattr(0, TCSANOW, param)
161struct termios saved_tty_parameters;
162#endif
163
164char *fail_reason = (char *)0;
165char  fail_buffer[50];
166char *abort_string[MAX_ABORTS]={"BUSY","NO DIALTONE","NO CARRIER","NO ANSWER","RING\r\nRING"};
167int n_aborts = 5;
168int abort_next = 0, timeout_next = 0, echo_next = 0;
169int clear_abort_next = 0;
170
171char *report_string[MAX_REPORTS] ;
172char  report_buffer[50] ;
173int n_reports = 0, report_next = 0, report_gathering = 0 ;
174int clear_report_next = 0;
175
176int say_next = 0, hup_next = 0;
177
178void *dup_mem __P((void *b, size_t c));
179void *copy_of __P((char *s));
180void break_sequence __P((void));
181static int  get_string __P((register char *string));
182static int  put_string __P((register char *s));
183static int  write_char __P((int c));
184static int  put_char __P((int c));
185static int  get_char __P((void));
186void chat_send __P((register char *s));
187/* static char *character __P((int c)); */
188void chat_expect __P((register char *s));
189static char *clean __P((register char *s, int sending));
190char *expect_strtok __P((char *, char *));
191int chatmain __P((int, int, char *));
192
193
194void *dup_mem(b, c)
195void *b;
196size_t c;
197{
198    void *ans = malloc (c);
199    if (!ans)
200        return NULL;
201
202    memcpy(ans, b, c);
203    return ans;
204}
205
206void *copy_of (s)
207char *s;
208{
209    return dup_mem(s, strlen (s) + 1);
210}
211
212char *getnextcommand(char **string)
213{
214        char *buf=*string,*res;
215        res=strchr(buf,'|');
216        if (res==NULL)
217                return NULL;
218        *res='\0';
219        *string=res+1;
220        return buf;
221}
222
223int chatmain(int fd, int mode, char *pScript)
224{
225  char    *arg;
226
227  /* initialize exit code */
228  exit_code = 0;
229  ttyfd     = fd;
230
231  if ( debug ) {
232    dbglog("chat_main: %s\n", pScript);
233  }
234
235  /* get first expect string */
236  arg = getnextcommand(&pScript);
237  while (( arg != NULL ) && ( exit_code == 0 )) {
238    /* process the expect string */
239    chat_expect(arg);
240    if ( exit_code == 0 ) {
241      /* get the next send string */
242      arg = getnextcommand(&pScript);
243      if ( arg != NULL ) {
244        /* process the send string */
245        chat_send(arg);
246
247        /* get the next expect string */
248        arg = getnextcommand(&pScript);
249      }
250    }
251  }
252  ttyfd = (int)-1;
253
254  return ( exit_code );
255}
256
257void break_sequence()
258{
259  tcsendbreak(ttyfd, 0);
260}
261
262/*
263 *      'Clean up' this string.
264 */
265static char *clean(s, sending)
266register char *s;
267int sending;  /* set to 1 when sending (putting) this string. */
268{
269    char temp[STR_LEN], env_str[STR_LEN], cur_chr;
270    register char *s1, *phchar;
271    int add_return = sending;
272#define isoctal(chr)    (((chr) >= '0') && ((chr) <= '7'))
273#define isalnumx(chr)   ((((chr) >= '0') && ((chr) <= '9')) \
274                         || (((chr) >= 'a') && ((chr) <= 'z')) \
275                         || (((chr) >= 'A') && ((chr) <= 'Z')) \
276                         || (chr) == '_')
277
278    s1 = temp;
279    while (*s) {
280        cur_chr = *s++;
281        if (cur_chr == '^') {
282            cur_chr = *s++;
283            if (cur_chr == '\0') {
284                *s1++ = '^';
285                break;
286            }
287            cur_chr &= 0x1F;
288            if (cur_chr != 0) {
289                *s1++ = cur_chr;
290            }
291            continue;
292        }
293       
294        if (use_env && cur_chr == '$') {                /* ARI */
295            phchar = env_str;
296            while (isalnumx(*s))
297                *phchar++ = *s++;
298            *phchar = '\0';
299            phchar = getenv(env_str);
300            if (phchar)
301                while (*phchar)
302                    *s1++ = *phchar++;
303            continue;
304        }
305
306        if (cur_chr != '\\') {
307            *s1++ = cur_chr;
308            continue;
309        }
310
311        cur_chr = *s++;
312        if (cur_chr == '\0') {
313            if (sending) {
314                *s1++ = '\\';
315                *s1++ = '\\';
316            }
317            break;
318        }
319
320        switch (cur_chr) {
321        case 'b':
322            *s1++ = '\b';
323            break;
324
325        case 'c':
326            if (sending && *s == '\0')
327                add_return = 0;
328            else
329                *s1++ = cur_chr;
330            break;
331
332        case '\\':
333        case 'K':
334        case 'p':
335        case 'd':
336            if (sending)
337                *s1++ = '\\';
338            *s1++ = cur_chr;
339            break;
340
341        case 'T':
342            if (sending && phone_num) {
343                for (phchar = phone_num; *phchar != '\0'; phchar++)
344                    *s1++ = *phchar;
345            }
346            else {
347                *s1++ = '\\';
348                *s1++ = 'T';
349            }
350            break;
351
352        case 'U':
353            if (sending && phone_num2) {
354                for (phchar = phone_num2; *phchar != '\0'; phchar++)
355                    *s1++ = *phchar;
356            }
357            else {
358                *s1++ = '\\';
359                *s1++ = 'U';
360            }
361            break;
362
363        case 'q':
364            quiet = 1;
365            break;
366
367        case 'r':
368            *s1++ = '\r';
369            break;
370
371        case 'n':
372            *s1++ = '\n';
373            break;
374
375        case 's':
376            *s1++ = ' ';
377            break;
378
379        case 't':
380            *s1++ = '\t';
381            break;
382
383        case 'N':
384            if (sending) {
385                *s1++ = '\\';
386                *s1++ = '\0';
387            }
388            else
389                *s1++ = 'N';
390            break;
391           
392        case '$':                       /* ARI */
393            if (use_env) {
394                *s1++ = cur_chr;
395                break;
396            }
397            /* FALL THROUGH */
398
399        default:
400            if (isoctal (cur_chr)) {
401                cur_chr &= 0x07;
402                if (isoctal (*s)) {
403                    cur_chr <<= 3;
404                    cur_chr |= *s++ - '0';
405                    if (isoctal (*s)) {
406                        cur_chr <<= 3;
407                        cur_chr |= *s++ - '0';
408                    }
409                }
410
411                if (cur_chr != 0 || sending) {
412                    if (sending && (cur_chr == '\\' || cur_chr == 0))
413                        *s1++ = '\\';
414                    *s1++ = cur_chr;
415                }
416                break;
417            }
418
419            if (sending)
420                *s1++ = '\\';
421            *s1++ = cur_chr;
422            break;
423        }
424    }
425
426    if (add_return)
427        *s1++ = '\r';
428
429    *s1++ = '\0'; /* guarantee closure */
430    *s1++ = '\0'; /* terminate the string */
431    return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
432}
433
434/*
435 * A modified version of 'strtok'. This version skips \ sequences.
436 */
437char *expect_strtok (s, term)
438     char *s, *term;
439{
440    static  char *str   = "";
441    int     escape_flag = 0;
442    char   *result;
443
444/*
445 * If a string was specified then do initial processing.
446 */
447    if (s)
448        str = s;
449
450/*
451 * If this is the escape flag then reset it and ignore the character.
452 */
453    if (*str)
454        result = str;
455    else
456        result = (char *) 0;
457
458    while (*str) {
459        if (escape_flag) {
460            escape_flag = 0;
461            ++str;
462            continue;
463        }
464
465        if (*str == '\\') {
466            ++str;
467            escape_flag = 1;
468            continue;
469        }
470
471/*
472 * If this is not in the termination string, continue.
473 */
474        if (strchr (term, *str) == (char *) 0) {
475            ++str;
476            continue;
477        }
478
479/*
480 * This is the terminator. Mark the end of the string and stop.
481 */
482        *str++ = '\0';
483        break;
484    }
485    return (result);
486}
487
488/*
489 * Process the expect string
490 */
491void chat_expect (s)
492char *s;
493{
494    char *expect;
495    char *reply;
496
497    if (strcmp(s, "HANGUP") == 0) {
498        ++hup_next;
499        return;
500    }
501 
502    if (strcmp(s, "ABORT") == 0) {
503        ++abort_next;
504        return;
505    }
506
507    if (strcmp(s, "CLR_ABORT") == 0) {
508        ++clear_abort_next;
509        return;
510    }
511
512    if (strcmp(s, "REPORT") == 0) {
513        ++report_next;
514        return;
515    }
516
517    if (strcmp(s, "CLR_REPORT") == 0) {
518        ++clear_report_next;
519        return;
520    }
521
522    if (strcmp(s, "TIMEOUT") == 0) {
523        ++timeout_next;
524        return;
525    }
526
527    if (strcmp(s, "ECHO") == 0) {
528        ++echo_next;
529        return;
530    }
531
532    if (strcmp(s, "SAY") == 0) {
533        ++say_next;
534        return;
535    }
536
537/*
538 * Fetch the expect and reply string.
539 */
540    for (;;) {
541        expect = expect_strtok (s, "-");
542        s      = (char *) 0;
543
544        if (expect == (char *) 0)
545            return;
546
547        reply = expect_strtok (s, "-");
548
549/*
550 * Handle the expect string. If successful then exit.
551 */
552        if (get_string (expect))
553            return;
554
555/*
556 * If there is a sub-reply string then send it. Otherwise any condition
557 * is terminal.
558 */
559        if (reply == (char *) 0 || exit_code != 3)
560            break;
561
562        chat_send (reply);
563    }
564}
565
566#if 0
567/*
568 * Translate the input character to the appropriate string for printing
569 * the data.
570 */
571
572static char *character(c)
573int c;
574{
575    static char string[10];
576    char *meta;
577
578    meta = (c & 0x80) ? "M-" : "";
579    c &= 0x7F;
580
581    if (c < 32)
582        sprintf(string, "%s^%c", meta, (int)c + '@');
583    else if (c == 127)
584        sprintf(string, "%s^?", meta);
585    else
586        sprintf(string, "%s%c", meta, c);
587
588    return (string);
589}
590#endif
591
592/*
593 *  process the reply string
594 */
595void chat_send (s)
596register char *s;
597{
598/*  char file_data[STR_LEN];  */
599
600    if (say_next) {
601        say_next = 0;
602        s = clean(s, 1);
603        write(2, s, strlen(s));
604        free(s);
605        return;
606    }
607
608    if (hup_next) {
609        hup_next = 0;
610        return;
611    }
612
613    if (echo_next) {
614        echo_next = 0;
615        echo = (strcmp(s, "ON") == 0);
616        return;
617    }
618
619    if (abort_next) {
620        char *s1;
621       
622        abort_next = 0;
623        if ( n_aborts < MAX_ABORTS ) {
624          s1 = clean(s, 0);
625          if (( strlen(s1) <= strlen(s) ) &&
626              ( strlen(s1) <  sizeof(fail_buffer))) {
627
628            abort_string[n_aborts++] = s1;
629          }
630        }
631        return;
632    }
633
634    if (clear_abort_next) {
635        clear_abort_next = 0;
636        return;
637    }
638
639    if (report_next) {
640        report_next = 0;
641        return;
642    }
643
644    if (clear_report_next) {
645        clear_report_next = 0;
646        return;
647    }
648
649    if (timeout_next) {
650        timeout_next = 0;
651        timeout = atoi(s);
652       
653        if (timeout <= 0)
654            timeout = DEFAULT_CHAT_TIMEOUT;
655
656        return;
657    }
658
659    if (strcmp(s, "EOT") == 0)
660        s = "^D\\c";
661    else if (strcmp(s, "BREAK") == 0)
662        s = "\\K\\c";
663
664    if (!put_string(s)) {
665      exit_code = 2;
666    }
667}
668
669static int get_char()
670{
671    int status;
672    char c;
673    int tries=MAX_TIMEOUTS;
674
675        while(tries)
676        {
677            status = read(ttyfd, &c, 1);
678            switch (status) {
679                   case 1:
680                                return ((int)c & 0x7F);
681                    default:
682                                tries--;                       
683            }
684    }
685        return -1;                                             
686}
687
688static int put_char(c)
689int c;
690{
691  char ch = c;
692
693  write(ttyfd, &ch, 1);
694
695  return 0;
696}
697
698static int write_char (c)
699int c;
700{
701    if (put_char(c) < 0) {
702        return (0);
703    }
704    return (1);
705}
706
707static int put_string (s)
708register char *s;
709{
710    quiet = 0;
711    s = clean(s, 1);
712    while (*s) {
713        register char c = *s++;
714
715        if (c != '\\') {
716            if (!write_char (c))
717                return 0;
718            continue;
719        }
720
721        c = *s++;
722        switch (c) {
723        case 'd':
724            sleep(1);
725            break;
726
727        case 'K':
728            break_sequence();
729            break;
730
731        case 'p':
732#if 0 /* FIXME!!! */
733            usleep(10000);      /* 1/100th of a second (arg is microseconds) */
734#else
735            sleep(1);
736#endif
737            break;
738
739        default:
740            if (!write_char (c))
741                return 0;
742            break;
743        }
744    }
745
746    return (1);
747}
748
749/*
750 *      'Wait for' this string to appear on this file descriptor.
751 */
752static int get_string(string)
753register char *string;
754{
755    int c, len, minlen;
756    register char *s = temp2, *end = s + STR_LEN;
757    char *logged = temp2;
758    struct termios tios;
759
760    memset(temp2, 0, sizeof(temp2));
761
762    tcgetattr(ttyfd, &tios);
763    tios.c_cc[VMIN] = 0;
764    tios.c_cc[VTIME] = timeout*10/MAX_TIMEOUTS;
765    tcsetattr(ttyfd, TCSANOW, &tios);
766               
767    string = clean(string, 0);
768    len = strlen(string);
769    minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
770       
771    if (len > STR_LEN) {
772        exit_code = 1;
773        return 0;
774    }
775
776    if (len == 0) {
777                        return (1);
778    }
779
780   while ( (c = get_char()) >= 0) {
781                int n, abort_len;
782
783        *s++ = c;
784        *s=0;
785
786        if (s - temp2 >= len &&
787            c == string[len - 1] &&
788            strncmp(s - len, string, len) == 0) {
789            return (1);
790        }
791
792        for (n = 0; n < n_aborts; ++n) {
793            if (s - temp2 >= (abort_len = strlen(abort_string[n])) &&
794                strncmp(s - abort_len, abort_string[n], abort_len) == 0) {
795
796                exit_code = n + 4;
797                strcpy(fail_reason = fail_buffer, abort_string[n]);
798                return (0);
799            }
800        }
801
802        if (s >= end) {
803            if (logged < s - minlen) {
804                logged = s;
805            }
806            s -= minlen;
807            memmove(temp2, s, minlen);
808            logged = temp2 + (logged - s);
809            s = temp2 + minlen;
810        }
811    }
812
813    exit_code = 3;
814    return (0);
815}
Note: See TracBrowser for help on using the repository browser.