source: rtems/cpukit/libmisc/shell/login_prompt.c @ 7eada71

4.115
Last change on this file since 7eada71 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • 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.org/license/LICENSE.
67 */
68
69#ifdef HAVE_CONFIG_H
70#include "config.h"
71#endif
72
73#include <stdio.h>
74#include <termios.h>
75#include <unistd.h>
76#include <ctype.h>
77#include <errno.h>
78
79#include <rtems/shell.h>
80
81static int rtems_shell_discard( int c, FILE *stream)
82{
83  return c;
84}
85
86static bool rtems_shell_get_text(
87  FILE *in,
88  FILE *out,
89  char *line,
90  size_t size
91)
92{
93  int fd_in = fileno( in);
94  int (*put)( int, FILE *) =
95    out != NULL && isatty( fd_in)
96      ? fputc
97      : rtems_shell_discard;
98  size_t i = 0;
99
100  if (size < 1) {
101    return false;
102  }
103
104  tcdrain( fd_in);
105  if (out != NULL){
106    tcdrain( fileno(out) );
107  }
108
109  while (true) {
110    int c = fgetc(in);
111
112    switch (c) {
113      case EOF:
114        clearerr( in );
115        return false;
116      case '\n':
117      case '\r':
118        put('\n', out);
119        line [i] = '\0';
120        return true;
121      case  127:
122      case '\b':
123        if (i > 0) {
124          put('\b', out);
125          put(' ', out);
126          put('\b', out);
127          --i;
128        } else {
129          put('\a', out);
130        }
131        break;
132      default:
133        if (!iscntrl( c)) {
134          if (i < size - 1) {
135            line [i] = (char) c;
136            ++i;
137            put( c, out);
138          } else {
139            put('\a', out);
140          }
141        } else {
142          put('\a', out);
143        }
144        break;
145    }
146  }
147  return true;
148}
149
150bool rtems_shell_login_prompt(
151  FILE *in,
152  FILE *out,
153  const char *device,
154  rtems_shell_login_check_t check
155)
156{
157  int fd_in = fileno(in);
158  struct termios termios_previous;
159  bool restore_termios = false;
160  int i = 0;
161  bool result = false;
162
163  if (tcgetattr( fd_in, &termios_previous) == 0) {
164    struct termios termios_new = termios_previous;
165
166    /*
167     *  Stay in canonical mode so we can tell EOF and dropped connections.
168     *  But read one character at a time and do not echo it.
169     */
170    termios_new.c_lflag &= (unsigned char) ~ECHO;
171    termios_new.c_cc [VTIME] = 0;
172    termios_new.c_cc [VMIN] = 1;
173
174    restore_termios = tcsetattr( fd_in, TCSANOW, &termios_new) == 0;
175  }
176
177  for (i = 0; i < 3; ++i) {
178    char user [32];
179    char passphrase [128];
180
181    fprintf( out, "%s login: ", device );
182    fflush( out );
183    result = rtems_shell_get_text( in, out, user, sizeof(user) );
184    if ( !result )
185      break;
186
187    fflush( in);
188    fprintf( out, "Password: ");
189    fflush( out);
190    result = rtems_shell_get_text( in, NULL, passphrase, sizeof(passphrase) );
191    if ( !result )
192      break;
193    fputc( '\n', out);
194
195    result = check( user, passphrase );
196    if (result)
197      break;
198
199    fprintf( out, "Login incorrect\n\n");
200    sleep( 2);
201  }
202
203  if (restore_termios) {
204    /* What to do if restoring the flags fails? */
205    tcsetattr( fd_in, TCSANOW, &termios_previous);
206  }
207
208  return result;
209}
Note: See TracBrowser for help on using the repository browser.