Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
DRC | 7f9ea27 | 2010-07-09 19:37:14 +0000 | [diff] [blame] | 2 | * Copyright (C) 2010 Antoine Martin. All Rights Reserved. |
| 3 | * Copyright (C) 2010 D. R. Commander. All Rights Reserved. |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 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> |
Adam Tkac | 21779fd | 2010-10-29 12:17:19 +0000 | [diff] [blame] | 26 | #include <os/os.h> |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 27 | #include <rfb/Password.h> |
| 28 | #include <rfb/util.h> |
| 29 | |
| 30 | #include <termios.h> |
| 31 | |
| 32 | |
| 33 | using namespace rfb; |
| 34 | |
| 35 | char* prog; |
| 36 | |
| 37 | static void usage() |
| 38 | { |
DRC | 7f9ea27 | 2010-07-09 19:37:14 +0000 | [diff] [blame] | 39 | fprintf(stderr,"usage: %s [file]\n", prog); |
| 40 | fprintf(stderr," %s -f\n", prog); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 41 | exit(1); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | static void enableEcho(bool enable) { |
| 46 | termios attrs; |
| 47 | tcgetattr(fileno(stdin), &attrs); |
| 48 | if (enable) |
| 49 | attrs.c_lflag |= ECHO; |
| 50 | else |
| 51 | attrs.c_lflag &= ~ECHO; |
| 52 | attrs.c_lflag |= ECHONL; |
| 53 | tcsetattr(fileno(stdin), TCSAFLUSH, &attrs); |
| 54 | } |
| 55 | |
| 56 | static char* getpassword(const char* prompt) { |
| 57 | PlainPasswd buf(256); |
DRC | c1afba7 | 2010-07-09 19:48:26 +0000 | [diff] [blame] | 58 | if (prompt) fputs(prompt, stdout); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 59 | enableEcho(false); |
| 60 | char* result = fgets(buf.buf, 256, stdin); |
| 61 | enableEcho(true); |
| 62 | if (result) { |
| 63 | if (result[strlen(result)-1] == '\n') |
| 64 | result[strlen(result)-1] = 0; |
| 65 | return buf.takeBuf(); |
| 66 | } |
| 67 | return 0; |
| 68 | } |
| 69 | |
Pierre Ossman | e58fcb9 | 2018-05-07 16:03:45 +0200 | [diff] [blame^] | 70 | // Reads passwords from stdin and prints encrypted passwords to stdout. |
DRC | 7f9ea27 | 2010-07-09 19:37:14 +0000 | [diff] [blame] | 71 | static int encrypt_pipe() { |
Pierre Ossman | e58fcb9 | 2018-05-07 16:03:45 +0200 | [diff] [blame^] | 72 | int i; |
| 73 | |
| 74 | // We support a maximum of two passwords right now |
| 75 | for (i = 0;i < 2;i++) { |
| 76 | char *result = getpassword(NULL); |
| 77 | if (!result) |
| 78 | break; |
| 79 | |
DRC | c1afba7 | 2010-07-09 19:48:26 +0000 | [diff] [blame] | 80 | ObfuscatedPasswd obfuscated(result); |
| 81 | if (fwrite(obfuscated.buf, obfuscated.length, 1, stdout) != 1) { |
| 82 | fprintf(stderr,"Writing to stdout failed\n"); |
| 83 | return 1; |
| 84 | } |
DRC | 7f9ea27 | 2010-07-09 19:37:14 +0000 | [diff] [blame] | 85 | } |
Pierre Ossman | e58fcb9 | 2018-05-07 16:03:45 +0200 | [diff] [blame^] | 86 | |
| 87 | // Did we fail to produce even one password? |
| 88 | if (i == 0) |
| 89 | return 1; |
| 90 | |
| 91 | return 0; |
DRC | 7f9ea27 | 2010-07-09 19:37:14 +0000 | [diff] [blame] | 92 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 93 | |
Michal Srb | dbf6355 | 2014-11-10 11:24:04 +0200 | [diff] [blame] | 94 | static ObfuscatedPasswd* readpassword() { |
| 95 | while (true) { |
| 96 | PlainPasswd passwd(getpassword("Password:")); |
| 97 | if (!passwd.buf) { |
| 98 | perror("getpassword error"); |
| 99 | exit(1); |
| 100 | } |
| 101 | if (strlen(passwd.buf) < 6) { |
| 102 | if (strlen(passwd.buf) == 0) { |
| 103 | fprintf(stderr,"Password not changed\n"); |
| 104 | exit(1); |
| 105 | } |
| 106 | fprintf(stderr,"Password must be at least 6 characters - try again\n"); |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | PlainPasswd passwd2(getpassword("Verify:")); |
| 111 | if (!passwd2.buf) { |
| 112 | perror("getpass error"); |
| 113 | exit(1); |
| 114 | } |
| 115 | if (strcmp(passwd.buf, passwd2.buf) != 0) { |
| 116 | fprintf(stderr,"Passwords don't match - try again\n"); |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | return new ObfuscatedPasswd(passwd); |
| 121 | } |
| 122 | } |
| 123 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 124 | int main(int argc, char** argv) |
| 125 | { |
| 126 | prog = argv[0]; |
| 127 | |
| 128 | char* fname = 0; |
| 129 | |
| 130 | for (int i = 1; i < argc; i++) { |
| 131 | if (strcmp(argv[i], "-q") == 0) { // allowed for backwards compatibility |
DRC | 7f9ea27 | 2010-07-09 19:37:14 +0000 | [diff] [blame] | 132 | } else if (strncmp(argv[i], "-f", 2) == 0) { |
| 133 | return encrypt_pipe(); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 134 | } else if (argv[i][0] == '-') { |
| 135 | usage(); |
| 136 | } else if (!fname) { |
| 137 | fname = argv[i]; |
| 138 | } else { |
| 139 | usage(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (!fname) { |
Adam Tkac | 21779fd | 2010-10-29 12:17:19 +0000 | [diff] [blame] | 144 | char *homeDir = NULL; |
Adam Tkac | af08172 | 2011-02-07 10:45:15 +0000 | [diff] [blame] | 145 | if (getvnchomedir(&homeDir) == -1) { |
| 146 | fprintf(stderr, "Can't obtain VNC home directory\n"); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 147 | exit(1); |
| 148 | } |
Adam Tkac | af08172 | 2011-02-07 10:45:15 +0000 | [diff] [blame] | 149 | mkdir(homeDir, 0777); |
| 150 | fname = new char[strlen(homeDir) + 7]; |
| 151 | sprintf(fname, "%spasswd", homeDir); |
Adam Tkac | 21779fd | 2010-10-29 12:17:19 +0000 | [diff] [blame] | 152 | delete [] homeDir; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | while (true) { |
Michal Srb | dbf6355 | 2014-11-10 11:24:04 +0200 | [diff] [blame] | 156 | ObfuscatedPasswd* obfuscated = readpassword(); |
| 157 | ObfuscatedPasswd* obfuscatedReadOnly = 0; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 158 | |
Michal Srb | dbf6355 | 2014-11-10 11:24:04 +0200 | [diff] [blame] | 159 | fprintf(stderr, "Would you like to enter a view-only password (y/n)? "); |
| 160 | char yesno[3]; |
| 161 | if (fgets(yesno, 3, stdin) != NULL && (yesno[0] == 'y' || yesno[0] == 'Y')) { |
| 162 | obfuscatedReadOnly = readpassword(); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | FILE* fp = fopen(fname,"w"); |
| 166 | if (!fp) { |
| 167 | fprintf(stderr,"Couldn't open %s for writing\n",fname); |
| 168 | exit(1); |
| 169 | } |
| 170 | chmod(fname, S_IRUSR|S_IWUSR); |
| 171 | |
Michal Srb | dbf6355 | 2014-11-10 11:24:04 +0200 | [diff] [blame] | 172 | if (fwrite(obfuscated->buf, obfuscated->length, 1, fp) != 1) { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 173 | fprintf(stderr,"Writing to %s failed\n",fname); |
| 174 | exit(1); |
| 175 | } |
| 176 | |
Michal Srb | dbf6355 | 2014-11-10 11:24:04 +0200 | [diff] [blame] | 177 | if (obfuscatedReadOnly) { |
| 178 | if (fwrite(obfuscatedReadOnly->buf, obfuscatedReadOnly->length, 1, fp) != 1) { |
| 179 | fprintf(stderr,"Writing to %s failed\n",fname); |
| 180 | exit(1); |
| 181 | } |
| 182 | } |
| 183 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 184 | fclose(fp); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | } |