Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 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 | // |
| 19 | // All-new VNC viewer for X. |
| 20 | // |
| 21 | |
| 22 | #include <string.h> |
| 23 | #include <stdio.h> |
| 24 | #include <ctype.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <sys/time.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include <unistd.h> |
| 31 | #include <errno.h> |
| 32 | #include <signal.h> |
Adam Tkac | 04b7fd2 | 2008-03-19 16:14:48 +0000 | [diff] [blame] | 33 | #include <locale.h> |
Adam Tkac | 21779fd | 2010-10-29 12:17:19 +0000 | [diff] [blame] | 34 | #include <os/os.h> |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 35 | #include <rfb/Logger_stdio.h> |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 36 | #include <rfb/SecurityClient.h> |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 37 | #include <rfb/LogWriter.h> |
| 38 | #include <network/TcpSocket.h> |
| 39 | #include "TXWindow.h" |
| 40 | #include "TXMsgBox.h" |
| 41 | #include "CConn.h" |
| 42 | |
Adam Tkac | 0c65d8a | 2008-04-17 16:37:08 +0000 | [diff] [blame] | 43 | #include "gettext.h" |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 44 | #define _(String) gettext (String) |
| 45 | #define gettext_noop(String) String |
| 46 | #define N_(String) gettext_noop (String) |
| 47 | |
| 48 | rfb::LogWriter vlog("main"); |
| 49 | |
| 50 | using namespace network; |
| 51 | using namespace rfb; |
| 52 | using namespace std; |
| 53 | |
| 54 | IntParameter pointerEventInterval("PointerEventInterval", |
| 55 | "Time in milliseconds to rate-limit" |
| 56 | " successive pointer events", 0); |
| 57 | IntParameter wmDecorationWidth("WMDecorationWidth", "Width of window manager " |
| 58 | "decoration around a window", 6); |
| 59 | IntParameter wmDecorationHeight("WMDecorationHeight", "Height of window " |
| 60 | "manager decoration around a window", 24); |
| 61 | StringParameter passwordFile("PasswordFile", |
| 62 | "Password file for VNC authentication", ""); |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 63 | AliasParameter passwd("passwd", "Alias for PasswordFile", &passwordFile); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 64 | |
| 65 | BoolParameter useLocalCursor("UseLocalCursor", |
| 66 | "Render the mouse cursor locally", true); |
| 67 | BoolParameter dotWhenNoCursor("DotWhenNoCursor", |
| 68 | "Show the dot cursor when the server sends an " |
| 69 | "invisible cursor", true); |
| 70 | BoolParameter autoSelect("AutoSelect", |
| 71 | "Auto select pixel format and encoding. " |
| 72 | "Default if PreferredEncoding and FullColor are not specified.", |
| 73 | true); |
| 74 | BoolParameter fullColour("FullColor", |
| 75 | "Use full color", true); |
| 76 | AliasParameter fullColourAlias("FullColour", "Alias for FullColor", &fullColour); |
| 77 | IntParameter lowColourLevel("LowColorLevel", |
| 78 | "Color level to use on slow connections. " |
| 79 | "0 = Very Low (8 colors), 1 = Low (64 colors), " |
| 80 | "2 = Medium (256 colors)", 2); |
| 81 | AliasParameter lowColourLevelAlias("LowColourLevel", "Alias for LowColorLevel", &lowColourLevel); |
| 82 | StringParameter preferredEncoding("PreferredEncoding", |
| 83 | "Preferred encoding to use (Tight, ZRLE, Hextile or" |
| 84 | " Raw)", "Tight"); |
| 85 | BoolParameter fullScreen("FullScreen", "Full screen mode", false); |
| 86 | BoolParameter viewOnly("ViewOnly", |
| 87 | "Don't send any mouse or keyboard events to the server", |
| 88 | false); |
| 89 | BoolParameter shared("Shared", |
| 90 | "Don't disconnect other viewers upon connection - " |
| 91 | "share the desktop instead", |
| 92 | false); |
| 93 | BoolParameter acceptClipboard("AcceptClipboard", |
| 94 | "Accept clipboard changes from the server", |
| 95 | true); |
| 96 | BoolParameter sendClipboard("SendClipboard", |
| 97 | "Send clipboard changes to the server", true); |
| 98 | BoolParameter sendPrimary("SendPrimary", |
| 99 | "Send the primary selection and cut buffer to the " |
| 100 | "server as well as the clipboard selection", |
| 101 | true); |
| 102 | |
Pierre Ossman | eb3cecb | 2009-03-23 16:49:47 +0000 | [diff] [blame] | 103 | StringParameter desktopSize("DesktopSize", |
| 104 | "Reconfigure desktop size on the server on " |
| 105 | "connect (if possible)", ""); |
| 106 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 107 | BoolParameter listenMode("listen", "Listen for connections from VNC servers", |
| 108 | false); |
| 109 | StringParameter geometry("geometry", "X geometry specification", ""); |
| 110 | StringParameter displayname("display", "The X display", ""); |
| 111 | |
| 112 | StringParameter via("via", "Gateway to tunnel via", ""); |
| 113 | |
| 114 | BoolParameter customCompressLevel("CustomCompressLevel", |
| 115 | "Use custom compression level. " |
| 116 | "Default if CompressLevel is specified.", false); |
| 117 | |
| 118 | IntParameter compressLevel("CompressLevel", |
| 119 | "Use specified compression level" |
| 120 | "0 = Low, 9 = High", |
| 121 | 6); |
| 122 | |
| 123 | BoolParameter noJpeg("NoJPEG", |
| 124 | "Disable lossy JPEG compression in Tight encoding.", |
| 125 | false); |
| 126 | |
| 127 | IntParameter qualityLevel("QualityLevel", |
| 128 | "JPEG quality level. " |
| 129 | "0 = Low, 9 = High", |
Pierre Ossman | 7dfa22e | 2009-03-12 13:03:22 +0000 | [diff] [blame] | 130 | 8); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 131 | |
| 132 | char aboutText[1024]; |
| 133 | char* programName; |
| 134 | extern char buildtime[]; |
| 135 | |
| 136 | static void CleanupSignalHandler(int sig) |
| 137 | { |
| 138 | // CleanupSignalHandler allows C++ object cleanup to happen because it calls |
| 139 | // exit() rather than the default which is to abort. |
| 140 | vlog.info("CleanupSignalHandler called"); |
| 141 | exit(1); |
| 142 | } |
| 143 | |
| 144 | // XLoginIconifier is a class which iconifies the XDM login window when it has |
| 145 | // grabbed the keyboard, thus releasing the grab, allowing the viewer to use |
| 146 | // the keyboard. It remaps the xlogin window on exit. |
| 147 | class XLoginIconifier { |
| 148 | public: |
| 149 | Display* dpy; |
| 150 | Window xlogin; |
| 151 | XLoginIconifier() : dpy(0), xlogin(0) {} |
| 152 | void iconify(Display* dpy_) { |
| 153 | dpy = dpy_; |
| 154 | if (XGrabKeyboard(dpy, DefaultRootWindow(dpy), False, GrabModeSync, |
| 155 | GrabModeSync, CurrentTime) == GrabSuccess) { |
| 156 | XUngrabKeyboard(dpy, CurrentTime); |
| 157 | } else { |
| 158 | xlogin = TXWindow::windowWithName(dpy, DefaultRootWindow(dpy), "xlogin"); |
| 159 | if (xlogin) { |
| 160 | XIconifyWindow(dpy, xlogin, DefaultScreen(dpy)); |
| 161 | XSync(dpy, False); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | ~XLoginIconifier() { |
| 166 | if (xlogin) { |
| 167 | fprintf(stderr,"~XLoginIconifier remapping xlogin\n"); |
| 168 | XMapWindow(dpy, xlogin); |
| 169 | XFlush(dpy); |
| 170 | sleep(1); |
| 171 | } |
| 172 | } |
| 173 | }; |
| 174 | |
| 175 | static XLoginIconifier xloginIconifier; |
| 176 | |
| 177 | static void usage() |
| 178 | { |
| 179 | fprintf(stderr, |
| 180 | "\nusage: %s [parameters] [host:displayNum] [parameters]\n" |
| 181 | " %s [parameters] -listen [port] [parameters]\n", |
| 182 | programName,programName); |
| 183 | fprintf(stderr,"\n" |
| 184 | "Parameters can be turned on with -<param> or off with -<param>=0\n" |
| 185 | "Parameters which take a value can be specified as " |
| 186 | "-<param> <value>\n" |
| 187 | "Other valid forms are <param>=<value> -<param>=<value> " |
| 188 | "--<param>=<value>\n" |
| 189 | "Parameter names are case-insensitive. The parameters are:\n\n"); |
| 190 | Configuration::listParams(79, 14); |
| 191 | exit(1); |
| 192 | } |
| 193 | |
| 194 | /* Tunnelling support. */ |
| 195 | static void |
| 196 | interpretViaParam (char **gatewayHost, char **remoteHost, |
| 197 | int *remotePort, char **vncServerName, |
| 198 | int localPort) |
| 199 | { |
| 200 | const int SERVER_PORT_OFFSET = 5900; |
| 201 | char *pos = strchr (*vncServerName, ':'); |
| 202 | if (pos == NULL) |
| 203 | *remotePort = SERVER_PORT_OFFSET; |
| 204 | else { |
| 205 | int portOffset = SERVER_PORT_OFFSET; |
| 206 | size_t len; |
| 207 | *pos++ = '\0'; |
| 208 | len = strlen (pos); |
| 209 | if (*pos == ':') { |
| 210 | /* Two colons is an absolute port number, not an offset. */ |
| 211 | pos++; |
| 212 | len--; |
| 213 | portOffset = 0; |
| 214 | } |
| 215 | if (!len || strspn (pos, "-0123456789") != len ) |
| 216 | usage (); |
| 217 | *remotePort = atoi (pos) + portOffset; |
| 218 | } |
| 219 | |
| 220 | if (**vncServerName != '\0') |
| 221 | *remoteHost = *vncServerName; |
| 222 | |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 223 | *gatewayHost = strDup (via.getValueStr ()); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 224 | *vncServerName = new char[50]; |
| 225 | sprintf (*vncServerName, "localhost::%d", localPort); |
| 226 | } |
| 227 | |
| 228 | #ifndef HAVE_SETENV |
| 229 | int |
| 230 | setenv(const char *envname, const char * envval, int overwrite) |
| 231 | { |
| 232 | if (envname && envval) { |
| 233 | char * envp = NULL; |
| 234 | envp = (char*)malloc(strlen(envname) + strlen(envval) + 2); |
| 235 | if (envp) { |
| 236 | // The putenv API guarantees memory leaks when |
| 237 | // changing environment variables repeatedly. |
| 238 | sprintf(envp, "%s=%s", envname, envval); |
| 239 | |
| 240 | // Cannot free envp |
| 241 | putenv(envp); |
| 242 | return(0); |
| 243 | } |
| 244 | } |
| 245 | return(-1); |
| 246 | } |
| 247 | #endif |
| 248 | |
| 249 | static void |
| 250 | createTunnel (const char *gatewayHost, const char *remoteHost, |
| 251 | int remotePort, int localPort) |
| 252 | { |
| 253 | char *cmd = getenv ("VNC_VIA_CMD"); |
| 254 | char *percent; |
| 255 | char lport[10], rport[10]; |
| 256 | sprintf (lport, "%d", localPort); |
| 257 | sprintf (rport, "%d", remotePort); |
| 258 | setenv ("G", gatewayHost, 1); |
| 259 | setenv ("H", remoteHost, 1); |
| 260 | setenv ("R", rport, 1); |
| 261 | setenv ("L", lport, 1); |
| 262 | if (!cmd) |
| 263 | cmd = "/usr/bin/ssh -f -L \"$L\":\"$H\":\"$R\" \"$G\" sleep 20"; |
Peter Åstrand | 4eacc02 | 2009-02-27 10:12:14 +0000 | [diff] [blame] | 264 | /* Compatibility with TigerVNC's method. */ |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 265 | while ((percent = strchr (cmd, '%')) != NULL) |
| 266 | *percent = '$'; |
| 267 | system (cmd); |
| 268 | } |
| 269 | |
| 270 | int main(int argc, char** argv) |
| 271 | { |
| 272 | setlocale(LC_ALL, ""); |
Adam Tkac | 932e09b | 2008-04-17 15:45:58 +0000 | [diff] [blame] | 273 | bindtextdomain(PACKAGE_NAME, LOCALEDIR); |
| 274 | textdomain(PACKAGE_NAME); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 275 | |
Peter Åstrand | 4eacc02 | 2009-02-27 10:12:14 +0000 | [diff] [blame] | 276 | const char englishAbout[] = N_("TigerVNC Viewer for X version %s - built %s\n" |
DRC | 07baad7 | 2011-06-28 05:42:30 +0000 | [diff] [blame^] | 277 | "Copyright (C) 1999-2011 TigerVNC Team and many others (see README.txt)\n" |
Peter Åstrand | 4eacc02 | 2009-02-27 10:12:14 +0000 | [diff] [blame] | 278 | "See http://www.tigervnc.org for information on TigerVNC."); |
Peter Åstrand | 3336ac4 | 2009-01-26 13:35:31 +0000 | [diff] [blame] | 279 | |
Adam Tkac | 27b2f77 | 2010-11-18 13:33:57 +0000 | [diff] [blame] | 280 | rfb::SecurityClient::setDefaults(); |
| 281 | |
Peter Åstrand | 3336ac4 | 2009-01-26 13:35:31 +0000 | [diff] [blame] | 282 | // Write about text to console, still using normal locale codeset |
| 283 | snprintf(aboutText, sizeof(aboutText), |
| 284 | gettext(englishAbout), PACKAGE_VERSION, buildtime); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 285 | fprintf(stderr,"\n%s\n", aboutText); |
| 286 | |
Peter Åstrand | 3336ac4 | 2009-01-26 13:35:31 +0000 | [diff] [blame] | 287 | // Set gettext codeset to what our GUI toolkit uses. Since we are |
| 288 | // passing strings from strerror/gai_strerror to the GUI, these must |
| 289 | // be in GUI codeset as well. |
Adam Tkac | 932e09b | 2008-04-17 15:45:58 +0000 | [diff] [blame] | 290 | bind_textdomain_codeset(PACKAGE_NAME, "iso-8859-1"); |
Peter Åstrand | aa409f1 | 2009-01-26 13:17:27 +0000 | [diff] [blame] | 291 | bind_textdomain_codeset("libc", "iso-8859-1"); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 292 | |
Peter Åstrand | 3336ac4 | 2009-01-26 13:35:31 +0000 | [diff] [blame] | 293 | // Re-create the aboutText for the GUI, now using GUI codeset |
| 294 | snprintf(aboutText, sizeof(aboutText), |
| 295 | gettext(englishAbout), PACKAGE_VERSION, buildtime); |
| 296 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 297 | rfb::initStdIOLoggers(); |
| 298 | rfb::LogWriter::setLogParams("*:stderr:30"); |
| 299 | |
| 300 | signal(SIGHUP, CleanupSignalHandler); |
| 301 | signal(SIGINT, CleanupSignalHandler); |
| 302 | signal(SIGTERM, CleanupSignalHandler); |
| 303 | |
| 304 | programName = argv[0]; |
| 305 | char* vncServerName = 0; |
| 306 | Display* dpy = 0; |
| 307 | |
Adam Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 308 | Configuration::enableViewerParams(); |
| 309 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 310 | for (int i = 1; i < argc; i++) { |
| 311 | if (Configuration::setParam(argv[i])) |
| 312 | continue; |
| 313 | |
| 314 | if (argv[i][0] == '-') { |
| 315 | if (i+1 < argc) { |
| 316 | if (Configuration::setParam(&argv[i][1], argv[i+1])) { |
| 317 | i++; |
| 318 | continue; |
| 319 | } |
| 320 | } |
| 321 | usage(); |
| 322 | } |
| 323 | |
| 324 | vncServerName = argv[i]; |
| 325 | } |
| 326 | |
| 327 | // Create .vnc in the user's home directory if it doesn't already exist |
Adam Tkac | 21779fd | 2010-10-29 12:17:19 +0000 | [diff] [blame] | 328 | char* homeDir = NULL; |
Adam Tkac | af08172 | 2011-02-07 10:45:15 +0000 | [diff] [blame] | 329 | if (getvnchomedir(&homeDir) == -1) { |
| 330 | vlog.error("Could not create VNC home directory: can't obtain home " |
| 331 | "directory path."); |
| 332 | } else { |
| 333 | int result = mkdir(homeDir, 0755); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 334 | if (result == -1 && errno != EEXIST) |
Adam Tkac | af08172 | 2011-02-07 10:45:15 +0000 | [diff] [blame] | 335 | vlog.error("Could not create VNC home directory: %s.", strerror(errno)); |
Adam Tkac | 21779fd | 2010-10-29 12:17:19 +0000 | [diff] [blame] | 336 | delete [] homeDir; |
| 337 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 338 | |
| 339 | if (!::autoSelect.hasBeenSet()) { |
| 340 | // Default to AutoSelect=0 if -PreferredEncoding or -FullColor is used |
| 341 | ::autoSelect.setParam(!::preferredEncoding.hasBeenSet() |
| 342 | && !::fullColour.hasBeenSet() |
| 343 | && !::fullColourAlias.hasBeenSet()); |
| 344 | } |
Adam Tkac | 21a5cc8 | 2009-10-07 15:14:33 +0000 | [diff] [blame] | 345 | if (!::fullColour.hasBeenSet() && !::fullColourAlias.hasBeenSet()) { |
| 346 | // Default to FullColor=0 if AutoSelect=0 && LowColorLevel is set |
| 347 | if (!::autoSelect && (::lowColourLevel.hasBeenSet() || |
| 348 | ::lowColourLevelAlias.hasBeenSet())) { |
| 349 | ::fullColour.setParam(false); |
| 350 | } |
| 351 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 352 | if (!::customCompressLevel.hasBeenSet()) { |
| 353 | // Default to CustomCompressLevel=1 if CompressLevel is used. |
| 354 | ::customCompressLevel.setParam(::compressLevel.hasBeenSet()); |
| 355 | } |
| 356 | |
| 357 | try { |
| 358 | /* Tunnelling support. */ |
| 359 | if (strlen (via.getValueStr ()) > 0) { |
| 360 | char *gatewayHost = ""; |
| 361 | char *remoteHost = "localhost"; |
| 362 | int localPort = findFreeTcpPort (); |
| 363 | int remotePort; |
| 364 | if (!vncServerName) |
| 365 | usage(); |
| 366 | interpretViaParam (&gatewayHost, &remoteHost, &remotePort, |
| 367 | &vncServerName, localPort); |
| 368 | createTunnel (gatewayHost, remoteHost, remotePort, localPort); |
| 369 | } |
| 370 | |
| 371 | Socket* sock = 0; |
| 372 | |
| 373 | if (listenMode) { |
| 374 | int port = 5500; |
| 375 | if (vncServerName && isdigit(vncServerName[0])) |
| 376 | port = atoi(vncServerName); |
| 377 | |
Adam Tkac | 93ff5db | 2010-02-05 15:54:10 +0000 | [diff] [blame] | 378 | TcpListener listener(NULL, port); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 379 | |
| 380 | vlog.info("Listening on port %d\n",port); |
| 381 | |
| 382 | while (true) { |
| 383 | sock = listener.accept(); |
| 384 | int pid = fork(); |
| 385 | if (pid < 0) { perror("fork"); exit(1); } |
| 386 | if (pid == 0) break; // child |
| 387 | delete sock; |
| 388 | int status; |
| 389 | while (wait3(&status, WNOHANG, 0) > 0) ; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | CharArray displaynameStr(displayname.getData()); |
| 394 | if (!(dpy = XOpenDisplay(TXWindow::strEmptyToNull(displaynameStr.buf)))) { |
| 395 | fprintf(stderr,"%s: unable to open display \"%s\"\n", |
| 396 | programName, XDisplayName(displaynameStr.buf)); |
| 397 | exit(1); |
| 398 | } |
| 399 | |
| 400 | TXWindow::init(dpy, "Vncviewer"); |
| 401 | xloginIconifier.iconify(dpy); |
| 402 | CConn cc(dpy, argc, argv, sock, vncServerName, listenMode); |
| 403 | |
| 404 | // X events are processed whenever reading from the socket would block. |
| 405 | |
| 406 | while (true) { |
| 407 | cc.getInStream()->check(1); |
| 408 | cc.processMsg(); |
| 409 | } |
| 410 | |
| 411 | } catch (rdr::EndOfStream& e) { |
| 412 | vlog.info(e.str()); |
| 413 | } catch (rdr::Exception& e) { |
| 414 | vlog.error(e.str()); |
| 415 | if (dpy) { |
| 416 | TXMsgBox msgBox(dpy, e.str(), MB_OK, "VNC Viewer: Information"); |
| 417 | msgBox.show(); |
| 418 | } |
| 419 | return 1; |
| 420 | } |
| 421 | |
| 422 | return 0; |
| 423 | } |