| 328 | } |
| 329 | |
| 330 | static void finsh_wait_auth(void) |
| 331 | { |
| 332 | int ch; |
| 333 | rt_bool_t input_finish = RT_FALSE; |
| 334 | char password[FINSH_PASSWORD_MAX] = { 0 }; |
| 335 | rt_size_t cur_pos = 0; |
| 336 | /* password not set */ |
| 337 | if (rt_strlen(finsh_get_password()) == 0) return; |
| 338 | |
| 339 | while (1) |
| 340 | { |
| 341 | rt_kprintf("Password for login: "); |
| 342 | while (!input_finish) |
| 343 | { |
| 344 | while (1) |
| 345 | { |
| 346 | /* read one character from device */ |
| 347 | ch = (int)finsh_getchar(); |
| 348 | if (ch < 0) |
| 349 | { |
| 350 | continue; |
| 351 | } |
| 352 | |
| 353 | if (ch >= ' ' && ch <= '~' && cur_pos < FINSH_PASSWORD_MAX) |
| 354 | { |
| 355 | /* change the printable characters to '*' */ |
| 356 | rt_kprintf("*"); |
| 357 | password[cur_pos++] = ch; |
| 358 | } |
| 359 | else if (ch == '\b' && cur_pos > 0) |
| 360 | { |
| 361 | /* backspace */ |
| 362 | cur_pos--; |
| 363 | password[cur_pos] = '\0'; |
| 364 | rt_kprintf("\b \b"); |
| 365 | } |
| 366 | else if (ch == '\r' || ch == '\n') |
| 367 | { |
| 368 | rt_kprintf("\n"); |
| 369 | input_finish = RT_TRUE; |
| 370 | break; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | if (!rt_strncmp(shell->password, password, FINSH_PASSWORD_MAX)) return; |
| 375 | else |
| 376 | { |
| 377 | /* authentication failed, delay 2S for retry */ |
| 378 | rt_thread_delay(2 * RT_TICK_PER_SECOND); |
| 379 | rt_kprintf("Sorry, try again.\n"); |
| 380 | cur_pos = 0; |
| 381 | input_finish = RT_FALSE; |
| 382 | rt_memset(password, '\0', FINSH_PASSWORD_MAX); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | #endif /* FINSH_USING_AUTH */ |
| 387 |
no test coverage detected