[2649eef] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
| 4 | * @brief Shell login prompt functions. |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | /* |
---|
[cbd1e87] | 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 | * |
---|
[2649eef] | 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 | * |
---|
[cbd1e87] | 61 | * Derived from files "cpukit/libmisc/shell/shell.c" and |
---|
| 62 | * "cpukit/telnetd/check_passwd.c". |
---|
| 63 | * |
---|
[2649eef] | 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 | |
---|
[cbd1e87] | 74 | #include <rtems/shell.h> |
---|
[2649eef] | 75 | |
---|
| 76 | static int rtems_shell_discard( int c, FILE *stream) |
---|
| 77 | { |
---|
| 78 | return c; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | static void rtems_shell_get_text( |
---|
| 82 | FILE *in, |
---|
| 83 | FILE *out, |
---|
| 84 | char *line, |
---|
| 85 | size_t size |
---|
| 86 | ) |
---|
| 87 | { |
---|
| 88 | int fd_in = fileno( in); |
---|
| 89 | int (*put)( int, FILE *) = |
---|
| 90 | out != NULL && isatty( fd_in) |
---|
| 91 | ? fputc |
---|
| 92 | : rtems_shell_discard; |
---|
| 93 | size_t i = 0; |
---|
| 94 | |
---|
| 95 | if (size < 1) { |
---|
| 96 | return; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | tcdrain( fd_in); |
---|
| 100 | if (out != NULL){ |
---|
| 101 | tcdrain( fileno( out)); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | while (true) { |
---|
| 105 | int c = fgetc( in); |
---|
| 106 | |
---|
| 107 | switch (c) { |
---|
| 108 | case EOF: |
---|
| 109 | /* Here comes an ugly hack: The Termios driver's read() handler returns |
---|
| 110 | * 0 to the C library's fgets() if it times out. fgets() interprets |
---|
| 111 | * this (correctly) as EOF, a condition we want to undo since it's not |
---|
| 112 | * really true since we really have a read error (Termios bug?). |
---|
| 113 | * |
---|
| 114 | * As a workaround we push something back and read it again. This |
---|
| 115 | * should simply reset the EOF condition. |
---|
| 116 | */ |
---|
| 117 | if (ungetc( '?', in) == '?') { |
---|
| 118 | fgetc( in); |
---|
| 119 | } |
---|
| 120 | break; |
---|
| 121 | case '\n': |
---|
| 122 | case '\r': |
---|
| 123 | put( '\n', out); |
---|
| 124 | line [i] = '\0'; |
---|
| 125 | return; |
---|
| 126 | case 127: |
---|
| 127 | case '\b': |
---|
| 128 | if (i > 0) { |
---|
| 129 | put( '\b', out); |
---|
| 130 | put( ' ', out); |
---|
| 131 | put( '\b', out); |
---|
| 132 | --i; |
---|
| 133 | } else { |
---|
| 134 | put( '\a', out); |
---|
| 135 | } |
---|
| 136 | break; |
---|
| 137 | default: |
---|
| 138 | if (!iscntrl( c)) { |
---|
| 139 | if (i < size - 1) { |
---|
| 140 | line [i] = (char) c; |
---|
| 141 | ++i; |
---|
| 142 | put( c, out); |
---|
| 143 | } else { |
---|
| 144 | put( '\a', out); |
---|
| 145 | } |
---|
| 146 | } else { |
---|
| 147 | put( '\a', out); |
---|
| 148 | } |
---|
| 149 | break; |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | bool rtems_shell_login_prompt( |
---|
| 155 | FILE *in, |
---|
| 156 | FILE *out, |
---|
| 157 | const char *device, |
---|
[cbd1e87] | 158 | rtems_shell_login_check_t check |
---|
[2649eef] | 159 | ) |
---|
| 160 | { |
---|
| 161 | int fd_in = fileno( in); |
---|
| 162 | struct termios termios_previous; |
---|
| 163 | bool restore_termios = false; |
---|
| 164 | int i = 0; |
---|
| 165 | bool result = false; |
---|
| 166 | |
---|
| 167 | if (tcgetattr( fd_in, &termios_previous) == 0) { |
---|
| 168 | struct termios termios_new = termios_previous; |
---|
| 169 | |
---|
| 170 | termios_new.c_lflag &= ~ECHO; |
---|
| 171 | termios_new.c_lflag &= ~ICANON; |
---|
| 172 | termios_new.c_cc [VTIME] = 255; |
---|
| 173 | termios_new.c_cc [VMIN] = 0; |
---|
| 174 | |
---|
| 175 | restore_termios = tcsetattr( fd_in, TCSANOW, &termios_new) == 0; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | for (i = 0; i < 3; ++i) { |
---|
| 179 | char user [32]; |
---|
| 180 | char passphrase [128]; |
---|
| 181 | |
---|
| 182 | fprintf( out, "%s login: ", device); |
---|
| 183 | fflush( out); |
---|
| 184 | rtems_shell_get_text( in, out, user, sizeof( user)); |
---|
| 185 | |
---|
| 186 | fflush( in); |
---|
| 187 | fprintf( out, "Password: "); |
---|
| 188 | fflush( out); |
---|
| 189 | rtems_shell_get_text( in, NULL, passphrase, sizeof( passphrase)); |
---|
| 190 | fputc( '\n', out); |
---|
| 191 | |
---|
| 192 | result = check( user, passphrase); |
---|
| 193 | if (result) { |
---|
| 194 | break; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | fprintf( out, "Login incorrect\n\n"); |
---|
| 198 | sleep( 2); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | if (restore_termios) { |
---|
| 202 | /* What to do if restoring the flags fails? */ |
---|
| 203 | tcsetattr( fd_in, TCSANOW, &termios_previous); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | return result; |
---|
| 207 | } |
---|