[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. |
---|
[0893220] | 13 | * |
---|
| 14 | * Acknowledgement of sponsorship |
---|
| 15 | * ------------------------------ |
---|
| 16 | * Parts of this software was produced by |
---|
[cbd1e87] | 17 | * the Stanford Linear Accelerator Center, Stanford University, |
---|
| 18 | * under Contract DE-AC03-76SFO0515 with the Department of Energy. |
---|
[0893220] | 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, |
---|
[cbd1e87] | 25 | * completeness, or usefulness of any data, apparatus, product, or process |
---|
| 26 | * disclosed, or represents that its use would not infringe privately owned |
---|
[0893220] | 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 | * |
---|
[cbd1e87] | 49 | * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03 |
---|
| 50 | * |
---|
[bcef89f2] | 51 | * Copyright (c) 2009 embedded brains GmbH & Co. KG |
---|
[cbd1e87] | 52 | * |
---|
[2649eef] | 53 | * Based on work from Chris Johns, Fernando Ruiz and Till Straumann. |
---|
| 54 | * |
---|
[cbd1e87] | 55 | * Derived from files "cpukit/libmisc/shell/shell.c" and |
---|
| 56 | * "cpukit/telnetd/check_passwd.c". |
---|
| 57 | * |
---|
[2649eef] | 58 | * The license and distribution terms for this file may be |
---|
| 59 | * found in the file LICENSE in this distribution or at |
---|
[c499856] | 60 | * http://www.rtems.org/license/LICENSE. |
---|
[2649eef] | 61 | */ |
---|
| 62 | |
---|
[9a77af8] | 63 | #ifdef HAVE_CONFIG_H |
---|
| 64 | #include "config.h" |
---|
| 65 | #endif |
---|
| 66 | |
---|
[2649eef] | 67 | #include <stdio.h> |
---|
| 68 | #include <termios.h> |
---|
| 69 | #include <unistd.h> |
---|
| 70 | #include <ctype.h> |
---|
[6f70c07] | 71 | #include <errno.h> |
---|
[2649eef] | 72 | |
---|
[cbd1e87] | 73 | #include <rtems/shell.h> |
---|
[2649eef] | 74 | |
---|
| 75 | static int rtems_shell_discard( int c, FILE *stream) |
---|
| 76 | { |
---|
| 77 | return c; |
---|
| 78 | } |
---|
| 79 | |
---|
[8a68b60] | 80 | static bool rtems_shell_get_text( |
---|
[2649eef] | 81 | FILE *in, |
---|
| 82 | FILE *out, |
---|
| 83 | char *line, |
---|
| 84 | size_t size |
---|
| 85 | ) |
---|
| 86 | { |
---|
| 87 | int fd_in = fileno( in); |
---|
| 88 | int (*put)( int, FILE *) = |
---|
| 89 | out != NULL && isatty( fd_in) |
---|
| 90 | ? fputc |
---|
| 91 | : rtems_shell_discard; |
---|
| 92 | size_t i = 0; |
---|
| 93 | |
---|
| 94 | if (size < 1) { |
---|
[8a68b60] | 95 | return false; |
---|
[2649eef] | 96 | } |
---|
| 97 | |
---|
| 98 | tcdrain( fd_in); |
---|
| 99 | if (out != NULL){ |
---|
[6f70c07] | 100 | tcdrain( fileno(out) ); |
---|
[2649eef] | 101 | } |
---|
| 102 | |
---|
| 103 | while (true) { |
---|
[6f70c07] | 104 | int c = fgetc(in); |
---|
[2649eef] | 105 | |
---|
| 106 | switch (c) { |
---|
| 107 | case EOF: |
---|
[6f70c07] | 108 | clearerr( in ); |
---|
[8a68b60] | 109 | return false; |
---|
[2649eef] | 110 | case '\n': |
---|
| 111 | case '\r': |
---|
[5b91459] | 112 | put('\n', out); |
---|
[2649eef] | 113 | line [i] = '\0'; |
---|
[8a68b60] | 114 | return true; |
---|
[2649eef] | 115 | case 127: |
---|
| 116 | case '\b': |
---|
| 117 | if (i > 0) { |
---|
[5b91459] | 118 | put('\b', out); |
---|
| 119 | put(' ', out); |
---|
| 120 | put('\b', out); |
---|
[2649eef] | 121 | --i; |
---|
| 122 | } else { |
---|
[5b91459] | 123 | put('\a', out); |
---|
[2649eef] | 124 | } |
---|
| 125 | break; |
---|
| 126 | default: |
---|
| 127 | if (!iscntrl( c)) { |
---|
| 128 | if (i < size - 1) { |
---|
| 129 | line [i] = (char) c; |
---|
| 130 | ++i; |
---|
| 131 | put( c, out); |
---|
| 132 | } else { |
---|
[5b91459] | 133 | put('\a', out); |
---|
[2649eef] | 134 | } |
---|
| 135 | } else { |
---|
[5b91459] | 136 | put('\a', out); |
---|
[2649eef] | 137 | } |
---|
| 138 | break; |
---|
| 139 | } |
---|
| 140 | } |
---|
[6f70c07] | 141 | return true; |
---|
[2649eef] | 142 | } |
---|
| 143 | |
---|
| 144 | bool rtems_shell_login_prompt( |
---|
| 145 | FILE *in, |
---|
| 146 | FILE *out, |
---|
| 147 | const char *device, |
---|
[cbd1e87] | 148 | rtems_shell_login_check_t check |
---|
[2649eef] | 149 | ) |
---|
| 150 | { |
---|
[6f70c07] | 151 | int fd_in = fileno(in); |
---|
[2649eef] | 152 | struct termios termios_previous; |
---|
| 153 | bool restore_termios = false; |
---|
| 154 | int i = 0; |
---|
| 155 | bool result = false; |
---|
| 156 | |
---|
| 157 | if (tcgetattr( fd_in, &termios_previous) == 0) { |
---|
| 158 | struct termios termios_new = termios_previous; |
---|
| 159 | |
---|
[5b91459] | 160 | /* |
---|
| 161 | * Stay in canonical mode so we can tell EOF and dropped connections. |
---|
| 162 | * But read one character at a time and do not echo it. |
---|
| 163 | */ |
---|
[6d8720c4] | 164 | termios_new.c_lflag &= (unsigned char) ~ECHO; |
---|
[5b91459] | 165 | termios_new.c_cc [VTIME] = 0; |
---|
| 166 | termios_new.c_cc [VMIN] = 1; |
---|
[2649eef] | 167 | |
---|
| 168 | restore_termios = tcsetattr( fd_in, TCSANOW, &termios_new) == 0; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | for (i = 0; i < 3; ++i) { |
---|
| 172 | char user [32]; |
---|
| 173 | char passphrase [128]; |
---|
| 174 | |
---|
[6f70c07] | 175 | fprintf( out, "%s login: ", device ); |
---|
| 176 | fflush( out ); |
---|
| 177 | result = rtems_shell_get_text( in, out, user, sizeof(user) ); |
---|
| 178 | if ( !result ) |
---|
[8a68b60] | 179 | break; |
---|
[2649eef] | 180 | |
---|
| 181 | fflush( in); |
---|
| 182 | fprintf( out, "Password: "); |
---|
| 183 | fflush( out); |
---|
[6f70c07] | 184 | result = rtems_shell_get_text( in, NULL, passphrase, sizeof(passphrase) ); |
---|
| 185 | if ( !result ) |
---|
[8a68b60] | 186 | break; |
---|
[2649eef] | 187 | fputc( '\n', out); |
---|
| 188 | |
---|
[6f70c07] | 189 | result = check( user, passphrase ); |
---|
| 190 | if (result) |
---|
[2649eef] | 191 | break; |
---|
| 192 | |
---|
| 193 | fprintf( out, "Login incorrect\n\n"); |
---|
| 194 | sleep( 2); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | if (restore_termios) { |
---|
| 198 | /* What to do if restoring the flags fails? */ |
---|
| 199 | tcsetattr( fd_in, TCSANOW, &termios_previous); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | return result; |
---|
| 203 | } |
---|