login.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2022, Egahp
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include <string.h>
  10. #include "csh.h"
  11. /*!< login */
  12. int csh_login(chry_shell_t *csh)
  13. {
  14. int ret = 0;
  15. char *password;
  16. char first = 0;
  17. #if defined(CONFIG_CSH_LNBUFF_STATIC) && CONFIG_CSH_LNBUFF_STATIC
  18. #define BUFFER_SIZE csh->buffsize
  19. char *buffer = csh->linebuff;
  20. uint16_t *linesize = &csh->linesize;
  21. #else
  22. #define BUFFER_SIZE 64
  23. static char buffer[BUFFER_SIZE];
  24. uint16_t _linesize;
  25. uint16_t *linesize = &_linesize;
  26. #endif
  27. #if defined(CONFIG_CSH_NOBLOCK) && CONFIG_CSH_NOBLOCK
  28. if (csh->rl.noblock && !csh->rl.block) {
  29. goto restore;
  30. }
  31. #endif
  32. retry:
  33. csh_printf(csh, "login as: %s\r\n", csh->user[csh->uid]);
  34. csh_printf(csh, "%s@%s's password:\r\n", csh->user[csh->uid], csh->host);
  35. chry_readline_mask(&csh->rl, true);
  36. first = csh->rl.prompt[0];
  37. csh->rl.prompt[0] = '\0';
  38. #if defined(CONFIG_CSH_NOBLOCK) && CONFIG_CSH_NOBLOCK
  39. restore:
  40. #endif
  41. password = chry_readline(&csh->rl, buffer, BUFFER_SIZE, linesize);
  42. #if defined(CONFIG_CSH_NOBLOCK) && CONFIG_CSH_NOBLOCK
  43. if (csh->rl.noblock && !csh->rl.block) {
  44. return 1;
  45. }
  46. #endif
  47. csh->rl.prompt[0] = first;
  48. chry_readline_mask(&csh->rl, false);
  49. if (password == NULL) {
  50. return -1;
  51. } else {
  52. ret = chry_shell_substitute_user(csh, csh->uid, password);
  53. if (ret != 0) {
  54. csh_printf(csh, "\033[31mincorrect password\033[0m\r\n\r\n");
  55. goto retry;
  56. }
  57. csh_printf(csh, "welcome to cherry shell\r\n");
  58. return ret;
  59. }
  60. }