source: rtems/cpukit/libmisc/shell/login_prompt.c @ 6f70c07

4.104.115
Last change on this file since 6f70c07 was 6f70c07, checked in by Joel Sherrill <joel.sherrill@…>, on 11/30/09 at 22:13:48

2009-11-30 Joel Sherrill <joel.sherrill@…>

  • libmisc/shell/login_prompt.c: Fix problem where timeout on login prompt at console results in tight loop repeating login prompt.
  • Property mode set to 100644
File size: 5.0 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    termios_new.c_lflag &= (unsigned char) ~ECHO;
163    termios_new.c_lflag &= (unsigned char) ~ICANON;
164    termios_new.c_cc [VTIME] = 255;
165    termios_new.c_cc [VMIN] = 0;
166
167    restore_termios = tcsetattr( fd_in, TCSANOW, &termios_new) == 0;
168  }
169
170  for (i = 0; i < 3; ++i) {
171    char user [32];
172    char passphrase [128];
173
174    fprintf( out, "%s login: ", device );
175    fflush( out );
176    result = rtems_shell_get_text( in, out, user, sizeof(user) );
177    if ( !result )
178      break;
179
180    fflush( in);
181    fprintf( out, "Password: ");
182    fflush( out);
183    result = rtems_shell_get_text( in, NULL, passphrase, sizeof(passphrase) );
184    if ( !result )
185      break;
186    fputc( '\n', out);
187
188    result = check( user, passphrase );
189    if (result)
190      break;
191
192    fprintf( out, "Login incorrect\n\n");
193    sleep( 2);
194  }
195
196  if (restore_termios) {
197    /* What to do if restoring the flags fails? */
198    tcsetattr( fd_in, TCSANOW, &termios_previous);
199  }
200
201  return result;
202}
Note: See TracBrowser for help on using the repository browser.