readln.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <authsrv.h>
  4. #include "authcmdlib.h"
  5. void
  6. getpass(char *key, char *pass, int check, int confirm)
  7. {
  8. char rpass[32], npass[32];
  9. char *err;
  10. if(pass == nil)
  11. pass = npass;
  12. for(;;){
  13. readln("Password: ", pass, sizeof npass, 1);
  14. if(confirm){
  15. readln("Confirm password: ", rpass, sizeof rpass, 1);
  16. if(strcmp(pass, rpass) != 0){
  17. print("mismatch, try again\n");
  18. continue;
  19. }
  20. }
  21. if(!passtokey(key, pass)){
  22. print("bad password, try again\n");
  23. continue;
  24. }
  25. if(check)
  26. if(err = okpasswd(pass)){
  27. print("%s, try again\n", err);
  28. continue;
  29. }
  30. break;
  31. }
  32. }
  33. int
  34. getsecret(int passvalid, char *p9pass)
  35. {
  36. char answer[32];
  37. readln("assign Inferno/POP secret? (y/n) ", answer, sizeof answer, 0);
  38. if(*answer != 'y' && *answer != 'Y')
  39. return 0;
  40. if(passvalid){
  41. readln("make it the same as the plan 9 password? (y/n) ",
  42. answer, sizeof answer, 0);
  43. if(*answer == 'y' || *answer == 'Y')
  44. return 1;
  45. }
  46. for(;;){
  47. readln("Secret(0 to 256 characters): ", p9pass,
  48. sizeof answer, 1);
  49. readln("Confirm: ", answer, sizeof answer, 1);
  50. if(strcmp(p9pass, answer) == 0)
  51. break;
  52. print("mismatch, try again\n");
  53. }
  54. return 1;
  55. }
  56. void
  57. readln(char *prompt, char *line, int len, int raw)
  58. {
  59. char *p;
  60. int fdin, fdout, ctl, n, nr;
  61. fdin = open("/dev/cons", OREAD);
  62. fdout = open("/dev/cons", OWRITE);
  63. fprint(fdout, "%s", prompt);
  64. if(raw){
  65. ctl = open("/dev/consctl", OWRITE);
  66. if(ctl < 0)
  67. error("couldn't set raw mode");
  68. write(ctl, "rawon", 5);
  69. } else
  70. ctl = -1;
  71. nr = 0;
  72. p = line;
  73. for(;;){
  74. n = read(fdin, p, 1);
  75. if(n < 0){
  76. close(ctl);
  77. error("can't read cons\n");
  78. }
  79. if(*p == 0x7f)
  80. exits(0);
  81. if(n == 0 || *p == '\n' || *p == '\r'){
  82. *p = '\0';
  83. if(raw){
  84. write(ctl, "rawoff", 6);
  85. write(fdout, "\n", 1);
  86. }
  87. close(ctl);
  88. return;
  89. }
  90. if(*p == '\b'){
  91. if(nr > 0){
  92. nr--;
  93. p--;
  94. }
  95. }else{
  96. nr++;
  97. p++;
  98. }
  99. if(nr == len){
  100. fprint(fdout, "line too long; try again\n");
  101. nr = 0;
  102. p = line;
  103. }
  104. }
  105. }