blob: 060a3ffab378f2335a853bf6cbbc529e6dafe659 [file] [log] [blame]
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
DRC7f9ea272010-07-09 19:37:14 +00002 * Copyright (C) 2010 Antoine Martin. All Rights Reserved.
3 * Copyright (C) 2010 D. R. Commander. All Rights Reserved.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00004 *
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 Tkac21779fd2010-10-29 12:17:19 +000026#include <os/os.h>
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000027#include <rfb/Password.h>
28#include <rfb/util.h>
29
30#include <termios.h>
31
32
33using namespace rfb;
34
35char* prog;
36
37static void usage()
38{
DRC7f9ea272010-07-09 19:37:14 +000039 fprintf(stderr,"usage: %s [file]\n", prog);
40 fprintf(stderr," %s -f\n", prog);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000041 exit(1);
42}
43
44
45static 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
56static char* getpassword(const char* prompt) {
57 PlainPasswd buf(256);
DRCc1afba72010-07-09 19:48:26 +000058 if (prompt) fputs(prompt, stdout);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000059 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 Ossmane58fcb92018-05-07 16:03:45 +020070// Reads passwords from stdin and prints encrypted passwords to stdout.
DRC7f9ea272010-07-09 19:37:14 +000071static int encrypt_pipe() {
Pierre Ossmane58fcb92018-05-07 16:03:45 +020072 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
DRCc1afba72010-07-09 19:48:26 +000080 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 }
DRC7f9ea272010-07-09 19:37:14 +000085 }
Pierre Ossmane58fcb92018-05-07 16:03:45 +020086
87 // Did we fail to produce even one password?
88 if (i == 0)
89 return 1;
90
91 return 0;
DRC7f9ea272010-07-09 19:37:14 +000092}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000093
Max Weninger6284c622019-11-15 19:49:33 +010094static int encrypt_pipe_for_password(char* plain, char* fname) {
95
96 if (!plain)
97 return 1;
98 PlainPasswd buf(256);
99 strcpy(buf.buf, plain);
100 ObfuscatedPasswd obfuscated(buf.takeBuf());
101
102 FILE* fp = fopen(fname,"w");
103 if (!fp) {
104 fprintf(stderr,"Couldn't open %s for writing\n",fname);
105 return 1;
106 }
107 chmod(fname, S_IRUSR|S_IWUSR);
108
109 if (fwrite(obfuscated.buf, obfuscated.length, 1, fp) != 1) {
110 fprintf(stderr,"Writing to %s failed\n",fname);
111 return 1;
112 }
113 return 0;
114}
115
Michal Srbdbf63552014-11-10 11:24:04 +0200116static ObfuscatedPasswd* readpassword() {
117 while (true) {
118 PlainPasswd passwd(getpassword("Password:"));
119 if (!passwd.buf) {
120 perror("getpassword error");
121 exit(1);
122 }
123 if (strlen(passwd.buf) < 6) {
124 if (strlen(passwd.buf) == 0) {
125 fprintf(stderr,"Password not changed\n");
126 exit(1);
127 }
128 fprintf(stderr,"Password must be at least 6 characters - try again\n");
129 continue;
130 }
131
132 PlainPasswd passwd2(getpassword("Verify:"));
133 if (!passwd2.buf) {
134 perror("getpass error");
135 exit(1);
136 }
137 if (strcmp(passwd.buf, passwd2.buf) != 0) {
138 fprintf(stderr,"Passwords don't match - try again\n");
139 continue;
140 }
141
142 return new ObfuscatedPasswd(passwd);
143 }
144}
145
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000146int main(int argc, char** argv)
147{
148 prog = argv[0];
149
150 char* fname = 0;
Max Weninger6284c622019-11-15 19:49:33 +0100151 fprintf(stderr, "argc = %d", argc);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000152 for (int i = 1; i < argc; i++) {
153 if (strcmp(argv[i], "-q") == 0) { // allowed for backwards compatibility
DRC7f9ea272010-07-09 19:37:14 +0000154 } else if (strncmp(argv[i], "-f", 2) == 0) {
155 return encrypt_pipe();
Max Weninger6284c622019-11-15 19:49:33 +0100156 } else if (strncmp(argv[i], "-g", 2) == 0) {
157 if (argc == 4) {
158 return encrypt_pipe_for_password(argv[i + 1], strDup(argv[i + 2]));
159 } else {
160 usage();
161 }
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000162 } else if (argv[i][0] == '-') {
163 usage();
164 } else if (!fname) {
Jan Grulicha5720e52018-11-20 10:44:39 +0100165 fname = strDup(argv[i]);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000166 } else {
167 usage();
168 }
169 }
170
171 if (!fname) {
Adam Tkac21779fd2010-10-29 12:17:19 +0000172 char *homeDir = NULL;
Adam Tkacaf081722011-02-07 10:45:15 +0000173 if (getvnchomedir(&homeDir) == -1) {
174 fprintf(stderr, "Can't obtain VNC home directory\n");
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000175 exit(1);
176 }
Adam Tkacaf081722011-02-07 10:45:15 +0000177 mkdir(homeDir, 0777);
178 fname = new char[strlen(homeDir) + 7];
179 sprintf(fname, "%spasswd", homeDir);
Adam Tkac21779fd2010-10-29 12:17:19 +0000180 delete [] homeDir;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000181 }
182
183 while (true) {
Michal Srbdbf63552014-11-10 11:24:04 +0200184 ObfuscatedPasswd* obfuscated = readpassword();
185 ObfuscatedPasswd* obfuscatedReadOnly = 0;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000186
Michal Srbdbf63552014-11-10 11:24:04 +0200187 fprintf(stderr, "Would you like to enter a view-only password (y/n)? ");
188 char yesno[3];
189 if (fgets(yesno, 3, stdin) != NULL && (yesno[0] == 'y' || yesno[0] == 'Y')) {
190 obfuscatedReadOnly = readpassword();
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000191 }
192
193 FILE* fp = fopen(fname,"w");
194 if (!fp) {
195 fprintf(stderr,"Couldn't open %s for writing\n",fname);
Jan Grulicha5720e52018-11-20 10:44:39 +0100196 delete [] fname;
197 delete obfuscated;
198 delete obfuscatedReadOnly;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000199 exit(1);
200 }
201 chmod(fname, S_IRUSR|S_IWUSR);
202
Michal Srbdbf63552014-11-10 11:24:04 +0200203 if (fwrite(obfuscated->buf, obfuscated->length, 1, fp) != 1) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000204 fprintf(stderr,"Writing to %s failed\n",fname);
Jan Grulicha5720e52018-11-20 10:44:39 +0100205 delete [] fname;
206 delete obfuscated;
207 delete obfuscatedReadOnly;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000208 exit(1);
209 }
210
Jan Grulicha5720e52018-11-20 10:44:39 +0100211 delete obfuscated;
212
Michal Srbdbf63552014-11-10 11:24:04 +0200213 if (obfuscatedReadOnly) {
214 if (fwrite(obfuscatedReadOnly->buf, obfuscatedReadOnly->length, 1, fp) != 1) {
215 fprintf(stderr,"Writing to %s failed\n",fname);
Jan Grulicha5720e52018-11-20 10:44:39 +0100216 delete [] fname;
217 delete obfuscatedReadOnly;
Michal Srbdbf63552014-11-10 11:24:04 +0200218 exit(1);
219 }
220 }
221
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000222 fclose(fp);
223
Jan Grulicha5720e52018-11-20 10:44:39 +0100224 delete [] fname;
225 delete obfuscatedReadOnly;
226
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000227 return 0;
228 }
229}