Section 18.10. Canonical Mode

team bbl


18.10. Canonical Mode

Canonical mode is simple: we issue a read, and the terminal driver returns when a line has been entered. Several conditions cause the read to return.

  • The read returns when the requested number of bytes have been read. We don't have to read a complete line. If we read a partial line, no information is lost; the next read starts where the previous read stopped.

  • The read returns when a line delimiter is encountered. Recall from Section 18.3 that the following characters are interpreted as end of line in canonical mode: NL, EOL, EOL2, and EOF. Also, recall from Section 18.5 that if ICRNL is set and if IGNCR is not set, then the CR character also terminates a line, since it acts just like the NL character.

    Realize that of these five line delimiters, one (EOF) is discarded by the terminal driver when it's processed. The other four are returned to the caller as the last character of the line.

  • The read also returns if a signal is caught and if the function is not automatically restarted (Section 10.5).

Examplegetpass Function

We now show the function getpass, which reads a password of some type from the user at a terminal. This function is called by the login(1) and crypt(1) programs. To read the password, the function must turn off echoing, but it can leave the terminal in canonical mode, as whatever we type as the password forms a complete line. Figure 18.17 shows a typical implementation on a UNIX system.

There are several points to consider in this example.

  • Instead of hardwiring /dev/tty into the program, we call the function ctermid to open the controlling terminal.

  • We read and write only to the controlling terminal and return an error if we can't open this device for reading and writing. There are other conventions to use. The BSD version of getpass reads from standard input and writes to standard error if the controlling terminal can't be opened for reading and writing. The System V version always writes to standard error but reads only from the controlling terminal.

  • We block the two signals SIGINT and SIGTSTP. If we didn't do this, entering the INTR character would abort the program and leave the terminal with echoing disabled. Similarly, entering the SUSP character would stop the program and return to the shell with echoing disabled. We choose to block the signals while we have echoing disabled. If they are generated while we're reading the password, they are held until we return. There are other ways to handle these signals. Some versions just ignore SIGINT (saving its previous action) while in getpass, resetting the action for this signal to its previous value before returning. This means that any occurrence of the signal while it's ignored is lost. Other versions catch SIGINT (saving its previous action) and if the signal is caught, send themselves the signal with the kill function after resetting the terminal state and signal action. None of the versions of getpass catch, ignore, or block SIGQUIT, so entering the QUIT character aborts the program and probably leaves the terminal with echoing disabled.

  • Be aware that some shells, notably the Korn shell, turn echoing back on whenever they read interactive input. These shells are the ones that provide command-line editing and therefore manipulate the state of the terminal every time we enter an interactive command. So, if we invoke this program under one of these shells and abort it with the QUIT character, it may reenable echoing for us. Other shells that don't provide this form of command-line editing, such as the Bourne shell, will abort the program and leave the terminal in no-echo mode. If we do this to our terminal, the stty command can reenable echoing.

  • We use standard I/O to read and write the controlling terminal. We specifically set the stream to be unbuffered; otherwise, there might be some interactions between the writing and reading of the stream (we would need some calls to fflush). We could have also used unbuffered I/O (Chapter 3), but we would have to simulate the getc function using read.

  • We store only up to eight characters as the password. Any additional characters that are entered are ignored.

The program in Figure 18.18 calls getpass and prints what we enter to let us verify that the ERASE and KILL characters work (as they should in canonical mode).

Whenever a program that calls getpass is done with the cleartext password, the program should zero it out in memory, just to be safe. If the program were to generate a core file that others might be able to read or if some other process were somehow able to read our memory, they might be able to read the cleartext password. (By "cleartext," we mean the password that we type at the prompt that is printed by getpass. Most UNIX system programs then modify this cleartext password into an "encrypted" password. The field pw_passwd in the password file, for example, contains the encrypted password, not the cleartext password.)

Figure 18.17. Implementation of getpass function
 #include <signal.h> #include <stdio.h> #include <termios.h> #define MAX_PASS_LEN    8      /* max #chars for user to enter */ char * getpass(const char *prompt) {     static char     buf[MAX_PASS_LEN + 1]; /* null byte at end */     char            *ptr;     sigset_t        sig, osig;     struct termios  ts, ots;     FILE            *fp;     int             c;     if ((fp = fopen(ctermid(NULL), "r+")) == NULL)         return(NULL);     setbuf(fp, NULL);     sigemptyset(&sig);     sigaddset(&sig, SIGINT);        /* block SIGINT */     sigaddset(&sig, SIGTSTP);       /* block SIGTSTP */     sigprocmask(SIG_BLOCK, &sig, &osig);    /* and save mask */     tcgetattr(fileno(fp), &ts);     /* save tty state */     ots = ts;                       /* structure copy */     ts.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);     tcsetattr(fileno(fp), TCSAFLUSH, &ts);     fputs(prompt, fp);     ptr = buf;     while ((c = getc(fp)) != EOF && c != '\n')         if (ptr < &buf[MAX_PASS_LEN])             *ptr++ = c;     *ptr = 0;                  /* null terminate */     putc('\n', fp);            /* we echo a newline */     tcsetattr(fileno(fp), TCSAFLUSH, &ots); /* restore TTY state */     sigprocmask(SIG_SETMASK, &osig, NULL);  /* restore mask */     fclose(fp);         /* done with /dev/tty */     return(buf); }

Figure 18.18. Call the getpass function
 #include "apue.h" char    *getpass(const char *); int main(void) {     char   *ptr;     if ((ptr = getpass("Enter password:")) == NULL)         err_sys("getpass error");     printf("password: %s\n", ptr);     /* now use password (probably encrypt it) ... */     while (*ptr != 0)         *ptr++ = 0;      /* zero it out when we're done with it */     exit(0); } 

    team bbl



    Advanced Programming in the UNIX Environment
    Advanced Programming in the UNIX Environment, Second Edition (Addison-Wesley Professional Computing Series)
    ISBN: 0321525949
    EAN: 2147483647
    Year: 2005
    Pages: 370

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net