blob: 7ba0b2257a324351ff3b18845fa232fb241488fd [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
DRC7f9ea272010-07-09 19:37:14 +000070// Reads password from stdin and prints encrypted password to stdout.
71static int encrypt_pipe() {
DRCc1afba72010-07-09 19:48:26 +000072 char *result = getpassword(NULL);
73 if (result) {
74 ObfuscatedPasswd obfuscated(result);
75 if (fwrite(obfuscated.buf, obfuscated.length, 1, stdout) != 1) {
76 fprintf(stderr,"Writing to stdout failed\n");
77 return 1;
78 }
79 return 0;
DRC7f9ea272010-07-09 19:37:14 +000080 }
DRCc1afba72010-07-09 19:48:26 +000081 else return 1;
DRC7f9ea272010-07-09 19:37:14 +000082}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000083
84int main(int argc, char** argv)
85{
86 prog = argv[0];
87
88 char* fname = 0;
89
90 for (int i = 1; i < argc; i++) {
91 if (strcmp(argv[i], "-q") == 0) { // allowed for backwards compatibility
DRC7f9ea272010-07-09 19:37:14 +000092 } else if (strncmp(argv[i], "-f", 2) == 0) {
93 return encrypt_pipe();
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000094 } else if (argv[i][0] == '-') {
95 usage();
96 } else if (!fname) {
97 fname = argv[i];
98 } else {
99 usage();
100 }
101 }
102
103 if (!fname) {
Adam Tkac21779fd2010-10-29 12:17:19 +0000104 char *homeDir = NULL;
Adam Tkacaf081722011-02-07 10:45:15 +0000105 if (getvnchomedir(&homeDir) == -1) {
106 fprintf(stderr, "Can't obtain VNC home directory\n");
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000107 exit(1);
108 }
Adam Tkacaf081722011-02-07 10:45:15 +0000109 mkdir(homeDir, 0777);
110 fname = new char[strlen(homeDir) + 7];
111 sprintf(fname, "%spasswd", homeDir);
Adam Tkac21779fd2010-10-29 12:17:19 +0000112 delete [] homeDir;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000113 }
114
115 while (true) {
116 PlainPasswd passwd(getpassword("Password:"));
117 if (!passwd.buf) {
118 perror("getpassword error");
119 exit(1);
120 }
121 if (strlen(passwd.buf) < 6) {
122 if (strlen(passwd.buf) == 0) {
123 fprintf(stderr,"Password not changed\n");
124 exit(1);
125 }
126 fprintf(stderr,"Password must be at least 6 characters - try again\n");
127 continue;
128 }
129
130 PlainPasswd passwd2(getpassword("Verify:"));
131 if (!passwd2.buf) {
132 perror("getpass error");
133 exit(1);
134 }
135 if (strcmp(passwd.buf, passwd2.buf) != 0) {
136 fprintf(stderr,"Passwords don't match - try again\n");
137 continue;
138 }
139
140 FILE* fp = fopen(fname,"w");
141 if (!fp) {
142 fprintf(stderr,"Couldn't open %s for writing\n",fname);
143 exit(1);
144 }
145 chmod(fname, S_IRUSR|S_IWUSR);
146
147 ObfuscatedPasswd obfuscated(passwd);
148
149 if (fwrite(obfuscated.buf, obfuscated.length, 1, fp) != 1) {
150 fprintf(stderr,"Writing to %s failed\n",fname);
151 exit(1);
152 }
153
154 fclose(fp);
155
156 return 0;
157 }
158}