Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2003 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | #include <stdio.h> |
| 19 | #include <string.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <unistd.h> |
| 24 | #include <rfb/vncAuth.h> |
| 25 | #include <rfb/util.h> |
| 26 | |
| 27 | using namespace rfb; |
| 28 | |
| 29 | char* prog; |
| 30 | |
| 31 | static void usage() |
| 32 | { |
| 33 | fprintf(stderr,"usage: %s [file]\n",prog); |
| 34 | exit(1); |
| 35 | } |
| 36 | |
| 37 | int main(int argc, char** argv) |
| 38 | { |
| 39 | prog = argv[0]; |
| 40 | |
| 41 | char* fname = 0; |
| 42 | |
| 43 | for (int i = 1; i < argc; i++) { |
| 44 | if (strcmp(argv[i], "-q") == 0) { // allowed for backwards compatibility |
| 45 | } else if (argv[i][0] == '-') { |
| 46 | usage(); |
| 47 | } else if (!fname) { |
| 48 | fname = argv[i]; |
| 49 | } else { |
| 50 | usage(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if (!fname) { |
| 55 | if (!getenv("HOME")) { |
| 56 | fprintf(stderr,"HOME is not set\n"); |
| 57 | exit(1); |
| 58 | } |
| 59 | fname = new char[strlen(getenv("HOME")) + 20]; |
| 60 | sprintf(fname, "%s/.vnc", getenv("HOME")); |
| 61 | mkdir(fname, 0777); |
| 62 | sprintf(fname, "%s/.vnc/passwd", getenv("HOME")); |
| 63 | } |
| 64 | |
| 65 | while (true) { |
| 66 | char* passwd = getpass("Password: "); |
| 67 | if (!passwd) { |
| 68 | perror("getpass error"); |
| 69 | exit(1); |
| 70 | } |
| 71 | if (strlen(passwd) < 6) { |
| 72 | if (strlen(passwd) == 0) { |
| 73 | fprintf(stderr,"Password not changed\n"); |
| 74 | exit(1); |
| 75 | } |
| 76 | fprintf(stderr,"Password must be at least 6 characters - try again\n"); |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | if (strlen(passwd) > 8) |
| 81 | passwd[8] = '\0'; |
| 82 | |
| 83 | CharArray passwdCopy(strDup(passwd)); |
| 84 | |
| 85 | passwd = getpass("Verify: "); |
| 86 | if (!passwd) { |
| 87 | perror("getpass error"); |
| 88 | exit(1); |
| 89 | } |
| 90 | if (strlen(passwd) > 8) |
| 91 | passwd[8] = '\0'; |
| 92 | |
| 93 | if (strcmp(passwdCopy.buf, passwd) != 0) { |
| 94 | fprintf(stderr,"Passwords don't match - try again\n"); |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | FILE* fp = fopen(fname,"w"); |
| 99 | if (!fp) { |
| 100 | fprintf(stderr,"Couldn't open %s for writing\n",fname); |
| 101 | exit(1); |
| 102 | } |
| 103 | chmod(fname, S_IRUSR|S_IWUSR); |
| 104 | |
| 105 | vncAuthObfuscatePasswd(passwd); |
| 106 | |
| 107 | if (fwrite(passwd, 8, 1, fp) != 1) { |
| 108 | fprintf(stderr,"Writing to %s failed\n",fname); |
| 109 | exit(1); |
| 110 | } |
| 111 | |
| 112 | fclose(fp); |
| 113 | |
| 114 | for (unsigned int i = 0; i < strlen(passwd); i++) |
| 115 | passwd[i] = passwdCopy.buf[i] = 0; |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | } |