You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vncpasswd.cxx 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright (C) 2010 Antoine Martin. All Rights Reserved.
  3. * Copyright (C) 2010 D. R. Commander. All Rights Reserved.
  4. *
  5. * This is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This software is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this software; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  18. * USA.
  19. */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #include <os/os.h>
  27. #include <rfb/Password.h>
  28. #include <rfb/util.h>
  29. #include <termios.h>
  30. using namespace rfb;
  31. char* prog;
  32. static void usage()
  33. {
  34. fprintf(stderr,"usage: %s [file]\n", prog);
  35. fprintf(stderr," %s -f\n", prog);
  36. exit(1);
  37. }
  38. static void enableEcho(bool enable) {
  39. termios attrs;
  40. tcgetattr(fileno(stdin), &attrs);
  41. if (enable)
  42. attrs.c_lflag |= ECHO;
  43. else
  44. attrs.c_lflag &= ~ECHO;
  45. attrs.c_lflag |= ECHONL;
  46. tcsetattr(fileno(stdin), TCSAFLUSH, &attrs);
  47. }
  48. static char* getpassword(const char* prompt) {
  49. PlainPasswd buf(256);
  50. if (prompt) fputs(prompt, stdout);
  51. enableEcho(false);
  52. char* result = fgets(buf.buf, 256, stdin);
  53. enableEcho(true);
  54. if (result) {
  55. if (result[strlen(result)-1] == '\n')
  56. result[strlen(result)-1] = 0;
  57. return buf.takeBuf();
  58. }
  59. return 0;
  60. }
  61. // Reads passwords from stdin and prints encrypted passwords to stdout.
  62. static int encrypt_pipe() {
  63. int i;
  64. // We support a maximum of two passwords right now
  65. for (i = 0;i < 2;i++) {
  66. char *result = getpassword(NULL);
  67. if (!result)
  68. break;
  69. ObfuscatedPasswd obfuscated(result);
  70. if (fwrite(obfuscated.buf, obfuscated.length, 1, stdout) != 1) {
  71. fprintf(stderr,"Writing to stdout failed\n");
  72. return 1;
  73. }
  74. }
  75. // Did we fail to produce even one password?
  76. if (i == 0)
  77. return 1;
  78. return 0;
  79. }
  80. static ObfuscatedPasswd* readpassword() {
  81. while (true) {
  82. PlainPasswd passwd(getpassword("Password:"));
  83. if (!passwd.buf) {
  84. perror("getpassword error");
  85. exit(1);
  86. }
  87. if (strlen(passwd.buf) < 6) {
  88. if (strlen(passwd.buf) == 0) {
  89. fprintf(stderr,"Password not changed\n");
  90. exit(1);
  91. }
  92. fprintf(stderr,"Password must be at least 6 characters - try again\n");
  93. continue;
  94. }
  95. PlainPasswd passwd2(getpassword("Verify:"));
  96. if (!passwd2.buf) {
  97. perror("getpass error");
  98. exit(1);
  99. }
  100. if (strcmp(passwd.buf, passwd2.buf) != 0) {
  101. fprintf(stderr,"Passwords don't match - try again\n");
  102. continue;
  103. }
  104. return new ObfuscatedPasswd(passwd);
  105. }
  106. }
  107. int main(int argc, char** argv)
  108. {
  109. prog = argv[0];
  110. char* fname = 0;
  111. for (int i = 1; i < argc; i++) {
  112. if (strcmp(argv[i], "-q") == 0) { // allowed for backwards compatibility
  113. } else if (strncmp(argv[i], "-f", 2) == 0) {
  114. return encrypt_pipe();
  115. } else if (argv[i][0] == '-') {
  116. usage();
  117. } else if (!fname) {
  118. fname = argv[i];
  119. } else {
  120. usage();
  121. }
  122. }
  123. if (!fname) {
  124. char *homeDir = NULL;
  125. if (getvnchomedir(&homeDir) == -1) {
  126. fprintf(stderr, "Can't obtain VNC home directory\n");
  127. exit(1);
  128. }
  129. mkdir(homeDir, 0777);
  130. fname = new char[strlen(homeDir) + 7];
  131. sprintf(fname, "%spasswd", homeDir);
  132. delete [] homeDir;
  133. }
  134. while (true) {
  135. ObfuscatedPasswd* obfuscated = readpassword();
  136. ObfuscatedPasswd* obfuscatedReadOnly = 0;
  137. fprintf(stderr, "Would you like to enter a view-only password (y/n)? ");
  138. char yesno[3];
  139. if (fgets(yesno, 3, stdin) != NULL && (yesno[0] == 'y' || yesno[0] == 'Y')) {
  140. obfuscatedReadOnly = readpassword();
  141. }
  142. FILE* fp = fopen(fname,"w");
  143. if (!fp) {
  144. fprintf(stderr,"Couldn't open %s for writing\n",fname);
  145. exit(1);
  146. }
  147. chmod(fname, S_IRUSR|S_IWUSR);
  148. if (fwrite(obfuscated->buf, obfuscated->length, 1, fp) != 1) {
  149. fprintf(stderr,"Writing to %s failed\n",fname);
  150. exit(1);
  151. }
  152. if (obfuscatedReadOnly) {
  153. if (fwrite(obfuscatedReadOnly->buf, obfuscatedReadOnly->length, 1, fp) != 1) {
  154. fprintf(stderr,"Writing to %s failed\n",fname);
  155. exit(1);
  156. }
  157. }
  158. fclose(fp);
  159. return 0;
  160. }
  161. }