source: rtems/cpukit/libmisc/shell/login_prompt.c @ 5b91459

4.104.115
Last change on this file since 5b91459 was 5b91459, checked in by Joel Sherrill <joel.sherrill@…>, on 12/18/09 at 20:18:30

2009-12-18 Joel Sherrill <joel.sherrill@…>

  • libmisc/shell/login_prompt.c: Switch from non-canonical mode with timeout (Case C) to blocking IO waiting for single character on login. In Case C mode, you cannot tell EOF from no data available. This means we cannot tell when a telnet connection is dropped. This was changed from 4.9 and resulted in breakage.
  • Property mode set to 100644
File size: 5.1 KB
Line 
1/**
2 * @file
3 *
4 * @brief Shell login prompt functions.
5 */
6
7/*
8 * Authorship
9 * ----------
10 * Parts of this software was created by
11 *     Till Straumann <strauman@slac.stanford.edu>, 2003-2007
12 *         Stanford Linear Accelerator Center, Stanford University.
13 *
14 * Acknowledgement of sponsorship
15 * ------------------------------
16 * Parts of this software was produced by
17 *     the Stanford Linear Accelerator Center, Stanford University,
18 *         under Contract DE-AC03-76SFO0515 with the Department of Energy.
19 *
20 * Government disclaimer of liability
21 * ----------------------------------
22 * Neither the United States nor the United States Department of Energy,
23 * nor any of their employees, makes any warranty, express or implied, or
24 * assumes any legal liability or responsibility for the accuracy,
25 * completeness, or usefulness of any data, apparatus, product, or process
26 * disclosed, or represents that its use would not infringe privately owned
27 * rights.
28 *
29 * Stanford disclaimer of liability
30 * --------------------------------
31 * Stanford University makes no representations or warranties, express or
32 * implied, nor assumes any liability for the use of this software.
33 *
34 * Stanford disclaimer of copyright
35 * --------------------------------
36 * Stanford University, owner of the copyright, hereby disclaims its
37 * copyright and all other rights in this software.  Hence, anyone may
38 * freely use it for any purpose without restriction.
39 *
40 * Maintenance of notices
41 * ----------------------
42 * In the interest of clarity regarding the origin and status of this
43 * SLAC software, this and all the preceding Stanford University notices
44 * are to remain affixed to any copy or derivative of this software made
45 * or distributed by the recipient and are to be affixed to any copy of
46 * software made or distributed by the recipient that contains a copy or
47 * derivative of this software.
48 *
49 * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
50 *
51 * Copyright (c) 2009 embedded brains GmbH and others.
52 *
53 * embedded brains GmbH
54 * Obere Lagerstr. 30
55 * D-82178 Puchheim
56 * Germany
57 * <rtems@embedded-brains.de>
58 *
59 * Based on work from Chris Johns, Fernando Ruiz and Till Straumann.
60 *
61 * Derived from files "cpukit/libmisc/shell/shell.c" and
62 * "cpukit/telnetd/check_passwd.c".
63 *
64 * The license and distribution terms for this file may be
65 * found in the file LICENSE in this distribution or at
66 * http://www.rtems.com/license/LICENSE.
67 */
68
69#include <stdio.h>
70#include <termios.h>
71#include <unistd.h>
72#include <ctype.h>
73#include <errno.h>
74
75#include <rtems/shell.h>
76
77static int rtems_shell_discard( int c, FILE *stream)
78{
79  return c;
80}
81
82static bool rtems_shell_get_text(
83  FILE *in,
84  FILE *out,
85  char *line,
86  size_t size
87)
88{
89  int fd_in = fileno( in);
90  int (*put)( int, FILE *) =
91    out != NULL && isatty( fd_in)
92      ? fputc
93      : rtems_shell_discard;
94  size_t i = 0;
95
96  if (size < 1) {
97    return false;
98  }
99
100  tcdrain( fd_in);
101  if (out != NULL){
102    tcdrain( fileno(out) );
103  }
104
105  while (true) {
106    int c = fgetc(in);
107
108    switch (c) {
109      case EOF:
110        clearerr( in );
111        return false;
112      case '\n':
113      case '\r':
114        put('\n', out);
115        line [i] = '\0';
116        return true;
117      case  127:
118      case '\b':
119        if (i > 0) {
120          put('\b', out);
121          put(' ', out);
122          put('\b', out);
123          --i;
124        } else {
125          put('\a', out);
126        }
127        break;
128      default:
129        if (!iscntrl( c)) {
130          if (i < size - 1) {
131            line [i] = (char) c;
132            ++i;
133            put( c, out);
134          } else {
135            put('\a', out);
136          }
137        } else {
138          put('\a', out);
139        }
140        break;
141    }
142  }
143  return true;
144}
145
146bool rtems_shell_login_prompt(
147  FILE *in,
148  FILE *out,
149  const char *device,
150  rtems_shell_login_check_t check
151)
152{
153  int fd_in = fileno(in);
154  struct termios termios_previous;
155  bool restore_termios = false;
156  int i = 0;
157  bool result = false;
158
159  if (tcgetattr( fd_in, &termios_previous) == 0) {
160    struct termios termios_new = termios_previous;
161
162    /*
163     *  Stay in canonical mode so we can tell EOF and dropped connections.
164     *  But read one character at a time and do not echo it.
165     */
166    termios_new.c_lflag &= (unsigned char) ~ECHO;
167    termios_new.c_cc [VTIME] = 0;
168    termios_new.c_cc [VMIN] = 1;
169
170    restore_termios = tcsetattr( fd_in, TCSANOW, &termios_new) == 0;
171  }
172
173  for (i = 0; i < 3; ++i) {
174    char user [32];
175    char passphrase [128];
176
177    fprintf( out, "%s login: ", device );
178    fflush( out );
179    result = rtems_shell_get_text( in, out, user, sizeof(user) );
180    if ( !result )
181      break;
182
183    fflush( in);
184    fprintf( out, "Password: ");
185    fflush( out);
186    result = rtems_shell_get_text( in, NULL, passphrase, sizeof(passphrase) );
187    if ( !result )
188      break;
189    fputc( '\n', out);
190
191    result = check( user, passphrase );
192    if (result)
193      break;
194
195    fprintf( out, "Login incorrect\n\n");
196    sleep( 2);
197  }
198
199  if (restore_termios) {
200    /* What to do if restoring the flags fails? */
201    tcsetattr( fd_in, TCSANOW, &termios_previous);
202  }
203
204  return result;
205}
Note: See TracBrowser for help on using the repository browser.