readln.c 2.0 KB

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