source: rtems/cpukit/pppd/chat.c @ 660db8c8

5
Last change on this file since 660db8c8 was 163d1e8, checked in by Sebastian Huber <sebastian.huber@…>, on 04/25/16 at 05:59:34

pppd: Fix warnings

  • Property mode set to 100644
File size: 16.1 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/*      $Id$ */
86
87/*
88        Fixes and some Changes by Wilfried Busalski Lancier-Monitoring GmbH Germany
89        wilfried.busalski@muenster.de
90       
91        Fixes:  put_string()            Free memory allocated by clean()
92                        get_string()            Free memory allocated by clean()
93                        chat_main()                     Will Distroy's no more the chat-script
94                        getnextcommand()        sepatator changed to '@'
95*/
96
97#include <stdio.h>
98#include <ctype.h>
99#include <time.h>
100#include <fcntl.h>
101#include <errno.h>
102#include <string.h>
103#include <stdlib.h>
104#include <unistd.h>
105#include <sys/types.h>
106#include <sys/stat.h>
107#include <syslog.h>
108#include <termios.h>
109#include "pppd.h"
110
111#undef  TERMIOS
112#define TERMIOS
113
114
115#define STR_LEN 1024
116char temp2[STR_LEN];
117
118#ifndef SIGTYPE
119#define SIGTYPE void
120#endif
121
122#undef __V
123
124#ifdef __STDC__
125#include <stdarg.h>
126#define __V(x)  x
127#else
128#include <varargs.h>
129#define __V(x)  (va_alist) va_dcl
130#define const
131#endif
132
133#ifndef O_NONBLOCK
134#define O_NONBLOCK      O_NDELAY
135#endif
136
137
138/*************** Micro getopt() *********************************************/
139#define OPTION(c,v)     (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
140                                (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
141                                &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
142#define OPTARG(c,v)     (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
143                                (_O=4,(char*)0):(char*)0)
144#define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
145#define ARG(c,v)        (c?(--c,*v++):(char*)0)
146
147#if 0
148static int _O = 0;              /* Internal state */
149#endif
150/*************** Micro getopt() *********************************************/
151
152#define MAX_ABORTS              16
153#define MAX_REPORTS             16
154#define DEFAULT_CHAT_TIMEOUT    45
155#define MAX_TIMEOUTS            10
156
157static int echo             = 0;
158static int quiet            = 0;
159static int use_env          = 0;
160static int exit_code        = 0;
161static char *phone_num      = (char *) 0;
162static char *phone_num2     = (char *) 0;
163static int ttyfd;
164static int timeout   = DEFAULT_CHAT_TIMEOUT;
165
166#ifdef TERMIOS
167#define term_parms struct termios
168#define get_term_param(param) tcgetattr(0, param)
169#define set_term_param(param) tcsetattr(0, TCSANOW, param)
170struct termios saved_tty_parameters;
171#endif
172
173char *fail_reason = (char *)0;
174char  fail_buffer[50];
175char *abort_string[MAX_ABORTS]={"BUSY","NO DIALTONE","NO CARRIER","NO ANSWER","RING\r\nRING"};
176int n_aborts = 5;
177int abort_next = 0, timeout_next = 0, echo_next = 0;
178int clear_abort_next = 0;
179
180char *report_string[MAX_REPORTS] ;
181char  report_buffer[50] ;
182int n_reports = 0, report_next = 0, report_gathering = 0 ;
183int clear_report_next = 0;
184
185int say_next = 0, hup_next = 0;
186
187void *dup_mem(void *b, size_t c);
188void *copy_of(char *s);
189void break_sequence(void);
190static int  get_string(register char *string);
191static int  put_string(register char *s);
192static int  write_char(int c);
193static int  put_char(int c);
194static int  get_char(void);
195void chat_send(register char *s);
196/* static char *character(int c); */
197void chat_expect(register char *s);
198static char *clean(register char *s, int sending);
199char *expect_strtok(char *, char *);
200int chatmain(int, int, char *);
201
202
203void *dup_mem(
204  void *b,
205  size_t c)
206{
207    void *ans = malloc (c);
208    if (!ans)
209        return NULL;
210
211    memcpy(ans, b, c);
212    return ans;
213}
214
215void *copy_of(
216  char *s)
217{
218    return dup_mem(s, strlen (s) + 1);
219}
220
221static char *getnextcommand(char *string,char *buff)
222{
223        char *token;
224        int len;
225       
226        token=strchr(string,'@');
227        if (token==NULL){
228                return NULL;
229        }
230        len=token-string;
231        if(len > 78 ){
232                len=78;
233        }
234        memcpy(buff,string,len);
235        buff[len]=0;
236        return(token+1);
237}
238
239int chatmain(int fd, int mode, char *pScript)
240{
241  char    arg[80];
242  char    *script;
243
244  /* initialize exit code */
245  exit_code = 0;
246  ttyfd     = fd;
247
248  script=pScript;
249
250  if ( debug ) {
251    dbglog("chat_main: %s\n", script);
252  }
253
254  /* get first expect string */
255  script = getnextcommand(script,arg);
256  while (( script != NULL ) && ( exit_code == 0 )) {
257    /* process the expect string */
258    chat_expect(arg);
259    if ( exit_code == 0 ) {
260      /* get the next send string */
261      script = getnextcommand(script,arg);
262      if ( script != NULL ) {
263        /* process the send string */
264        chat_send(arg);
265
266        /* get the next expect string */
267        script = getnextcommand(script,arg);
268      }
269    }
270  }
271  ttyfd = (int)-1;
272
273  return ( exit_code );
274}
275
276void break_sequence(void)
277{
278  tcsendbreak(ttyfd, 0);
279}
280
281/*
282 *      'Clean up' this string.
283 */
284static char *clean(
285  char *s,
286  int sending )  /* set to 1 when sending (putting) this string. */
287{
288    char temp[STR_LEN], env_str[STR_LEN], cur_chr;
289    register char *s1, *phchar;
290    int add_return = sending;
291#define isoctal(chr)    (((chr) >= '0') && ((chr) <= '7'))
292#define isalnumx(chr)   ((((chr) >= '0') && ((chr) <= '9')) \
293                         || (((chr) >= 'a') && ((chr) <= 'z')) \
294                         || (((chr) >= 'A') && ((chr) <= 'Z')) \
295                         || (chr) == '_')
296
297    s1 = temp;
298    while (*s) {
299        cur_chr = *s++;
300        if (cur_chr == '^') {
301            cur_chr = *s++;
302            if (cur_chr == '\0') {
303                *s1++ = '^';
304                break;
305            }
306            cur_chr &= 0x1F;
307            if (cur_chr != 0) {
308                *s1++ = cur_chr;
309            }
310            continue;
311        }
312       
313        if (use_env && cur_chr == '$') {                /* ARI */
314            phchar = env_str;
315            while (isalnumx(*s))
316                *phchar++ = *s++;
317            *phchar = '\0';
318            phchar = getenv(env_str);
319            if (phchar)
320                while (*phchar)
321                    *s1++ = *phchar++;
322            continue;
323        }
324
325        if (cur_chr != '\\') {
326            *s1++ = cur_chr;
327            continue;
328        }
329
330        cur_chr = *s++;
331        if (cur_chr == '\0') {
332            if (sending) {
333                *s1++ = '\\';
334                *s1++ = '\\';
335            }
336            break;
337        }
338
339        switch (cur_chr) {
340        case 'b':
341            *s1++ = '\b';
342            break;
343
344        case 'c':
345            if (sending && *s == '\0')
346                add_return = 0;
347            else
348                *s1++ = cur_chr;
349            break;
350
351        case '\\':
352        case 'K':
353        case 'p':
354        case 'd':
355            if (sending)
356                *s1++ = '\\';
357            *s1++ = cur_chr;
358            break;
359
360        case 'T':
361            if (sending && phone_num) {
362                for (phchar = phone_num; *phchar != '\0'; phchar++)
363                    *s1++ = *phchar;
364            }
365            else {
366                *s1++ = '\\';
367                *s1++ = 'T';
368            }
369            break;
370
371        case 'U':
372            if (sending && phone_num2) {
373                for (phchar = phone_num2; *phchar != '\0'; phchar++)
374                    *s1++ = *phchar;
375            }
376            else {
377                *s1++ = '\\';
378                *s1++ = 'U';
379            }
380            break;
381
382        case 'q':
383            quiet = 1;
384            break;
385
386        case 'r':
387            *s1++ = '\r';
388            break;
389
390        case 'n':
391            *s1++ = '\n';
392            break;
393
394        case 's':
395            *s1++ = ' ';
396            break;
397
398        case 't':
399            *s1++ = '\t';
400            break;
401
402        case 'N':
403            if (sending) {
404                *s1++ = '\\';
405                *s1++ = '\0';
406            }
407            else
408                *s1++ = 'N';
409            break;
410       
411        case '$':                       /* ARI */
412            if (use_env) {
413                *s1++ = cur_chr;
414                break;
415            }
416            /* FALL THROUGH */
417
418        default:
419            if (isoctal (cur_chr)) {
420                cur_chr &= 0x07;
421                if (isoctal (*s)) {
422                    cur_chr <<= 3;
423                    cur_chr |= *s++ - '0';
424                    if (isoctal (*s)) {
425                        cur_chr <<= 3;
426                        cur_chr |= *s++ - '0';
427                    }
428                }
429
430                if (cur_chr != 0 || sending) {
431                    if (sending && (cur_chr == '\\' || cur_chr == 0))
432                        *s1++ = '\\';
433                    *s1++ = cur_chr;
434                }
435                break;
436            }
437
438            if (sending)
439                *s1++ = '\\';
440            *s1++ = cur_chr;
441            break;
442        }
443    }
444
445    if (add_return)
446        *s1++ = '\r';
447
448    *s1++ = '\0'; /* guarantee closure */
449    *s1++ = '\0'; /* terminate the string */
450    return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
451}
452
453/*
454 * A modified version of 'strtok'. This version skips \ sequences.
455 */
456char *expect_strtok (
457     char *s, char *term)
458{
459    static  char *str   = "";
460    int     escape_flag = 0;
461    char   *result;
462
463/*
464 * If a string was specified then do initial processing.
465 */
466    if (s)
467        str = s;
468
469/*
470 * If this is the escape flag then reset it and ignore the character.
471 */
472    if (*str)
473        result = str;
474    else
475        result = (char *) 0;
476
477    while (*str) {
478        if (escape_flag) {
479            escape_flag = 0;
480            ++str;
481            continue;
482        }
483
484        if (*str == '\\') {
485            ++str;
486            escape_flag = 1;
487            continue;
488        }
489
490/*
491 * If this is not in the termination string, continue.
492 */
493        if (strchr (term, *str) == (char *) 0) {
494            ++str;
495            continue;
496        }
497
498/*
499 * This is the terminator. Mark the end of the string and stop.
500 */
501        *str++ = '\0';
502        break;
503    }
504    return (result);
505}
506
507/*
508 * Process the expect string
509 */
510void chat_expect (
511  char *s)
512{
513    char *expect;
514    char *reply;
515
516    if (strcmp(s, "HANGUP") == 0) {
517                ++hup_next;
518        return;
519    }
520
521    if (strcmp(s, "ABORT") == 0) {
522                ++abort_next;
523                return;
524    }
525
526    if (strcmp(s, "CLR_ABORT") == 0) {
527                ++clear_abort_next;
528                return;
529    }
530
531    if (strcmp(s, "REPORT") == 0) {
532                ++report_next;
533                return;
534    }
535
536    if (strcmp(s, "CLR_REPORT") == 0) {
537                ++clear_report_next;
538                return;
539    }
540
541    if (strcmp(s, "TIMEOUT") == 0) {
542                ++timeout_next;
543                return;
544    }
545
546    if (strcmp(s, "ECHO") == 0) {
547                ++echo_next;
548                return;
549    }
550
551    if (strcmp(s, "SAY") == 0) {
552                ++say_next;
553                return;
554    }
555
556/*
557 * Fetch the expect and reply string.
558 */
559    for (;;) {
560        expect = expect_strtok (s, "-");
561        s      = (char *) 0;
562
563        if (expect == (char *) 0)
564            return;
565
566        reply = expect_strtok (s, "-");
567
568/*
569 * Handle the expect string. If successful then exit.
570 */
571        if (get_string (expect))
572            return;
573
574/*
575 * If there is a sub-reply string then send it. Otherwise any condition
576 * is terminal.
577 */
578        if (reply == (char *) 0 || exit_code != 3)
579            break;
580
581        chat_send (reply);
582    }
583}
584
585#if 0
586/*
587 * Translate the input character to the appropriate string for printing
588 * the data.
589 */
590
591static char *character(
592  int c)
593{
594    static char string[10];
595    char *meta;
596
597    meta = (c & 0x80) ? "M-" : "";
598    c &= 0x7F;
599
600    if (c < 32)
601        sprintf(string, "%s^%c", meta, (int)c + '@');
602    else if (c == 127)
603        sprintf(string, "%s^?", meta);
604    else
605        sprintf(string, "%s%c", meta, c);
606
607    return (string);
608}
609#endif
610
611/*
612 *  process the reply string
613 */
614void chat_send (
615  char *s)
616{
617/*  char file_data[STR_LEN];  */
618
619    if (say_next) {
620                say_next = 0;
621                s = clean(s, 1);
622                write(2, s, strlen(s));
623        free(s);
624                return;
625    }
626
627    if (hup_next) {
628        hup_next = 0;
629        return;
630    }
631
632    if (echo_next) {
633        echo_next = 0;
634        echo = (strcmp(s, "ON") == 0);
635        return;
636    }
637
638    if (abort_next) {
639                abort_next = 0;
640                if ( n_aborts < MAX_ABORTS ) {
641                        char *s1;
642                        s1 = clean(s, 0);
643                        if (( strlen(s1) <= strlen(s) ) && ( strlen(s1) <  sizeof(fail_buffer)))
644                                abort_string[n_aborts++] = s1;
645                        else
646                                free(s1);
647                }
648                return;
649    }
650
651    if (clear_abort_next) {
652                clear_abort_next = 0;
653                return;
654    }
655
656    if (report_next) {
657                report_next = 0;
658                return;
659    }
660
661    if (clear_report_next) {
662                clear_report_next = 0;
663                return;
664    }
665
666    if (timeout_next) {
667                timeout_next = 0;
668                timeout = atoi(s);
669       
670                if (timeout <= 0){
671                timeout = DEFAULT_CHAT_TIMEOUT;
672            }
673                return;
674    }
675
676    if (strcmp(s, "EOT") == 0){
677                s = "^D\\c";
678        }
679    else{
680        if (strcmp(s, "BREAK") == 0){
681                        s = "\\K\\c";
682                }
683
684        if (!put_string(s)) {
685                exit_code = 2;
686        }
687        }
688}
689
690static int get_char(void)
691{
692    int status;
693    char c;
694    int tries=MAX_TIMEOUTS;
695
696        while(tries)
697        {
698            status = read(ttyfd, &c, 1);
699            switch (status) {
700                   case 1:
701                                return ((int)c & 0x7F);
702                    default:
703                                tries--;                       
704            }
705    }
706        return -1;                                             
707}
708
709static int put_char(
710  int c)
711{
712  char ch = c;
713
714  return(write(ttyfd, &ch, 1));
715}
716
717static int write_char (
718  int c)
719{
720    if (put_char(c) < 1) {
721                return (0);
722    }
723    return (1);
724}
725
726static int put_string (
727  char *s)
728{
729        char *out,*free_ptr=0;
730       
731    quiet = 0;
732    out = free_ptr = clean(s, 1);
733    while (*out) {
734                register char c = *out++;
735
736                if (c != '\\') {
737                if (!write_char (c)){
738                        free(free_ptr);
739                                return 0;
740                        }
741                continue;
742                }
743
744                c = *out++;
745
746                switch (c) {
747                        case 'd':
748                        sleep(1);
749                    break;
750
751                        case 'K':
752                        break_sequence();
753                break;
754
755                        case 'p':
756#if 0 /* FIXME!!! */
757                        usleep(10000);  /* 1/100th of a second (arg is microseconds) */
758#else
759                        sleep(1);
760#endif
761                break;
762
763                        default:
764                        if (!write_char (c)){
765                                free(free_ptr);
766                                        return 0;
767                                }
768                break;
769                }
770    }
771    free(free_ptr);
772
773    return (1);
774}
775
776/*
777 *      'Wait for' this string to appear on this file descriptor.
778 */
779static int get_string(
780    char *in_string)
781{
782    int c, len, minlen;
783    register char *s = temp2, *end = s + STR_LEN;
784    char *logged = temp2;
785    char *string=0;
786    struct termios tios;
787
788    memset(temp2, 0, sizeof(temp2));
789
790    tcgetattr(ttyfd, &tios);
791    tios.c_cc[VMIN] = 0;
792    tios.c_cc[VTIME] = timeout*10/MAX_TIMEOUTS;
793    tcsetattr(ttyfd, TCSANOW, &tios);
794               
795    string = clean(in_string, 0);
796    len = strlen(string);
797    minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
798       
799    if (len > STR_LEN) {
800                exit_code = 1;
801                free(string);
802                return 0;
803    }
804
805    if (len == 0) {
806        free(string);
807        return (1);
808    }
809
810   while ( (c = get_char()) >= 0) {
811                int n, abort_len;
812
813        if(c == '\n' || c == '\r'){
814                s = temp2;
815                *s=0;
816        }
817        else{
818                *s++ = c;
819                *s=0;
820        }
821
822        if (s - temp2 >= len &&
823            c == string[len - 1] &&
824            strncmp(s - len, string, len) == 0) {
825            free(string);
826            return (1);
827        }
828
829        for (n = 0; n < n_aborts; ++n) {
830            if (s - temp2 >= (abort_len = strlen(abort_string[n])) &&
831                strncmp(s - abort_len, abort_string[n], abort_len) == 0) {
832
833                exit_code = n + 4;
834                strcpy(fail_reason = fail_buffer, abort_string[n]);
835                free(string);
836                return (0);
837            }
838        }
839
840        if (s >= end) {
841            if (logged < s - minlen) {
842                logged = s;
843            }
844            s -= minlen;
845            memmove(temp2, s, minlen);
846            logged = temp2 + (logged - s);
847            s = temp2 + minlen;
848        }
849    }
850
851    exit_code = 3;
852    free(string);
853    return (0);
854}
Note: See TracBrowser for help on using the repository browser.