summaryrefslogtreecommitdiffstats
path: root/vncpasswd/vncpasswd.cxx
blob: c8dd777d533e9850793f8336def311c212e21b8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* Copyright (C) 2002-2003 RealVNC Ltd.  All Rights Reserved.
 *    
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <rfb/vncAuth.h>
#include <rfb/util.h>

using namespace rfb;

char* prog;

static void usage()
{
  fprintf(stderr,"usage: %s [file]\n",prog);
  exit(1);
}

int main(int argc, char** argv)
{
  prog = argv[0];

  char* fname = 0;

  for (int i = 1; i < argc; i++) {
    if (strcmp(argv[i], "-q") == 0) { // allowed for backwards compatibility
    } else if (argv[i][0] == '-') {
      usage();
    } else if (!fname) {
      fname = argv[i];
    } else {
      usage();
    }
  }

  if (!fname) {
    if (!getenv("HOME")) {
      fprintf(stderr,"HOME is not set\n");
      exit(1);
    }
    fname = new char[strlen(getenv("HOME")) + 20];
    sprintf(fname, "%s/.vnc", getenv("HOME"));
    mkdir(fname, 0777);
    sprintf(fname, "%s/.vnc/passwd", getenv("HOME"));
  }

  while (true) {
    char* passwd = getpass("Password: ");
    if (!passwd) {
      perror("getpass error");
      exit(1);
    }   
    if (strlen(passwd) < 6) {
      if (strlen(passwd) == 0) {
        fprintf(stderr,"Password not changed\n");
        exit(1);
      }
      fprintf(stderr,"Password must be at least 6 characters - try again\n");
      continue;
    }

    if (strlen(passwd) > 8)
      passwd[8] = '\0';

    CharArray passwdCopy(strDup(passwd));

    passwd = getpass("Verify: ");
    if (!passwd) {
      perror("getpass error");
      exit(1);
    }   
    if (strlen(passwd) > 8)
      passwd[8] = '\0';

    if (strcmp(passwdCopy.buf, passwd) != 0) {
      fprintf(stderr,"Passwords don't match - try again\n");
      continue;
    }

    FILE* fp = fopen(fname,"w");
    if (!fp) {
      fprintf(stderr,"Couldn't open %s for writing\n",fname);
      exit(1);
    }
    chmod(fname, S_IRUSR|S_IWUSR);

    vncAuthObfuscatePasswd(passwd);

    if (fwrite(passwd, 8, 1, fp) != 1) {
      fprintf(stderr,"Writing to %s failed\n",fname);
      exit(1);
    }

    fclose(fp);

    for (unsigned int i = 0; i < strlen(passwd); i++)
      passwd[i] = passwdCopy.buf[i] = 0;

    return 0;
  }
}