blob: d21ea94224945353bb8f735d4f015c900e1d7a37 [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>
26#include <rfb/Password.h>
27#include <rfb/util.h>
28
29#include <termios.h>
30
31
32using namespace rfb;
33
34char* prog;
35
36static void usage()
37{
DRC7f9ea272010-07-09 19:37:14 +000038 fprintf(stderr,"usage: %s [file]\n", prog);
39 fprintf(stderr," %s -f\n", prog);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000040 exit(1);
41}
42
43
44static void enableEcho(bool enable) {
45 termios attrs;
46 tcgetattr(fileno(stdin), &attrs);
47 if (enable)
48 attrs.c_lflag |= ECHO;
49 else
50 attrs.c_lflag &= ~ECHO;
51 attrs.c_lflag |= ECHONL;
52 tcsetattr(fileno(stdin), TCSAFLUSH, &attrs);
53}
54
55static char* getpassword(const char* prompt) {
56 PlainPasswd buf(256);
DRCc1afba72010-07-09 19:48:26 +000057 if (prompt) fputs(prompt, stdout);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000058 enableEcho(false);
59 char* result = fgets(buf.buf, 256, stdin);
60 enableEcho(true);
61 if (result) {
62 if (result[strlen(result)-1] == '\n')
63 result[strlen(result)-1] = 0;
64 return buf.takeBuf();
65 }
66 return 0;
67}
68
DRC7f9ea272010-07-09 19:37:14 +000069// Reads password from stdin and prints encrypted password to stdout.
70static int encrypt_pipe() {
DRCc1afba72010-07-09 19:48:26 +000071 char *result = getpassword(NULL);
72 if (result) {
73 ObfuscatedPasswd obfuscated(result);
74 if (fwrite(obfuscated.buf, obfuscated.length, 1, stdout) != 1) {
75 fprintf(stderr,"Writing to stdout failed\n");
76 return 1;
77 }
78 return 0;
DRC7f9ea272010-07-09 19:37:14 +000079 }
DRCc1afba72010-07-09 19:48:26 +000080 else return 1;
DRC7f9ea272010-07-09 19:37:14 +000081}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000082
83int main(int argc, char** argv)
84{
85 prog = argv[0];
86
87 char* fname = 0;
88
89 for (int i = 1; i < argc; i++) {
90 if (strcmp(argv[i], "-q") == 0) { // allowed for backwards compatibility
DRC7f9ea272010-07-09 19:37:14 +000091 } else if (strncmp(argv[i], "-f", 2) == 0) {
92 return encrypt_pipe();
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000093 } else if (argv[i][0] == '-') {
94 usage();
95 } else if (!fname) {
96 fname = argv[i];
97 } else {
98 usage();
99 }
100 }
101
102 if (!fname) {
103 if (!getenv("HOME")) {
104 fprintf(stderr,"HOME is not set\n");
105 exit(1);
106 }
107 fname = new char[strlen(getenv("HOME")) + 20];
108 sprintf(fname, "%s/.vnc", getenv("HOME"));
109 mkdir(fname, 0777);
110 sprintf(fname, "%s/.vnc/passwd", getenv("HOME"));
111 }
112
113 while (true) {
114 PlainPasswd passwd(getpassword("Password:"));
115 if (!passwd.buf) {
116 perror("getpassword error");
117 exit(1);
118 }
119 if (strlen(passwd.buf) < 6) {
120 if (strlen(passwd.buf) == 0) {
121 fprintf(stderr,"Password not changed\n");
122 exit(1);
123 }
124 fprintf(stderr,"Password must be at least 6 characters - try again\n");
125 continue;
126 }
127
128 PlainPasswd passwd2(getpassword("Verify:"));
129 if (!passwd2.buf) {
130 perror("getpass error");
131 exit(1);
132 }
133 if (strcmp(passwd.buf, passwd2.buf) != 0) {
134 fprintf(stderr,"Passwords don't match - try again\n");
135 continue;
136 }
137
138 FILE* fp = fopen(fname,"w");
139 if (!fp) {
140 fprintf(stderr,"Couldn't open %s for writing\n",fname);
141 exit(1);
142 }
143 chmod(fname, S_IRUSR|S_IWUSR);
144
145 ObfuscatedPasswd obfuscated(passwd);
146
147 if (fwrite(obfuscated.buf, obfuscated.length, 1, fp) != 1) {
148 fprintf(stderr,"Writing to %s failed\n",fname);
149 exit(1);
150 }
151
152 fclose(fp);
153
154 return 0;
155 }
156}