Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB |
DRC | b65bb93 | 2011-06-24 03:17:00 +0000 | [diff] [blame] | 3 | * Copyright (C) 2011 D. R. Commander. All Rights Reserved. |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 4 | * |
| 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 | |
Peter Åstrand | c359f36 | 2011-08-23 12:04:46 +0000 | [diff] [blame] | 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include <config.h> |
| 23 | #endif |
| 24 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 25 | #include <string.h> |
| 26 | #include <stdio.h> |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 27 | #include <ctype.h> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <errno.h> |
| 30 | #include <signal.h> |
| 31 | #include <locale.h> |
Pierre Ossman | 5bf17ad | 2015-02-17 13:23:00 +0100 | [diff] [blame] | 32 | #include <fcntl.h> |
Pierre Ossman | a71a276 | 2015-03-04 09:57:06 +0100 | [diff] [blame] | 33 | #include <unistd.h> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 34 | #include <sys/stat.h> |
| 35 | |
| 36 | #ifdef WIN32 |
Peter Åstrand (astrand) | 11167e1 | 2015-02-05 11:10:32 +0100 | [diff] [blame] | 37 | #include <os/winerrno.h> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 38 | #include <direct.h> |
| 39 | #define mkdir(path, mode) _mkdir(path) |
| 40 | #endif |
| 41 | |
Pierre Ossman | 6b9622d | 2014-07-21 16:42:12 +0200 | [diff] [blame] | 42 | #if !defined(WIN32) && !defined(__APPLE__) |
| 43 | #include <X11/Xlib.h> |
| 44 | #include <X11/XKBlib.h> |
| 45 | #endif |
| 46 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 47 | #include <rfb/Logger_stdio.h> |
| 48 | #include <rfb/SecurityClient.h> |
| 49 | #include <rfb/Security.h> |
| 50 | #ifdef HAVE_GNUTLS |
| 51 | #include <rfb/CSecurityTLS.h> |
| 52 | #endif |
| 53 | #include <rfb/LogWriter.h> |
| 54 | #include <rfb/Timer.h> |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 55 | #include <rfb/Exception.h> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 56 | #include <network/TcpSocket.h> |
DRC | 4426f00 | 2011-10-12 20:02:55 +0000 | [diff] [blame] | 57 | #include <os/os.h> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 58 | |
| 59 | #include <FL/Fl.H> |
| 60 | #include <FL/Fl_Widget.H> |
Pierre Ossman | 8eb3508 | 2012-03-27 12:50:54 +0000 | [diff] [blame] | 61 | #include <FL/Fl_PNG_Image.H> |
Pierre Ossman | a71a276 | 2015-03-04 09:57:06 +0100 | [diff] [blame] | 62 | #include <FL/Fl_Sys_Menu_Bar.H> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 63 | #include <FL/fl_ask.H> |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 64 | #include <FL/x.H> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 65 | |
| 66 | #include "i18n.h" |
| 67 | #include "parameters.h" |
| 68 | #include "CConn.h" |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 69 | #include "ServerDialog.h" |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 70 | #include "UserDialog.h" |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 71 | #include "vncviewer.h" |
Pierre Ossman | a71a276 | 2015-03-04 09:57:06 +0100 | [diff] [blame] | 72 | #include "fltk_layout.h" |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 73 | |
DRC | b65bb93 | 2011-06-24 03:17:00 +0000 | [diff] [blame] | 74 | #ifdef WIN32 |
Pierre Ossman | 8eb3508 | 2012-03-27 12:50:54 +0000 | [diff] [blame] | 75 | #include "resource.h" |
DRC | b65bb93 | 2011-06-24 03:17:00 +0000 | [diff] [blame] | 76 | #include "win32.h" |
| 77 | #endif |
| 78 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 79 | rfb::LogWriter vlog("main"); |
| 80 | |
| 81 | using namespace network; |
| 82 | using namespace rfb; |
| 83 | using namespace std; |
| 84 | |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 85 | char vncServerName[VNCSERVERNAMELEN] = { '\0' }; |
| 86 | |
Pierre Ossman | a71a276 | 2015-03-04 09:57:06 +0100 | [diff] [blame] | 87 | static const char *argv0 = NULL; |
| 88 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 89 | static bool exitMainloop = false; |
Pierre Ossman | e2ef5c1 | 2011-07-12 16:56:34 +0000 | [diff] [blame] | 90 | static const char *exitError = NULL; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 91 | |
Pierre Ossman | ccd09ba | 2015-04-07 14:58:45 +0200 | [diff] [blame] | 92 | static const char *about_text() |
| 93 | { |
| 94 | static char buffer[1024]; |
| 95 | |
| 96 | // This is used in multiple places with potentially different |
| 97 | // encodings, so we need to make sure we get a fresh string every |
| 98 | // time. |
| 99 | snprintf(buffer, sizeof(buffer), |
| 100 | _("TigerVNC Viewer %d-bit v%s\n" |
| 101 | "Built on: %s\n" |
Peter Åstrand (astrand) | 6011f96 | 2017-11-29 09:11:29 +0100 | [diff] [blame] | 102 | "Copyright (C) 1999-%d TigerVNC Team and many others (see README.rst)\n" |
Pierre Ossman | 32b96ff | 2018-10-19 14:56:53 +0200 | [diff] [blame^] | 103 | "See https://www.tigervnc.org for information on TigerVNC."), |
Pierre Ossman | ccd09ba | 2015-04-07 14:58:45 +0200 | [diff] [blame] | 104 | (int)sizeof(size_t)*8, PACKAGE_VERSION, |
Samuel Mannehed | 518698a | 2018-02-08 16:55:23 +0100 | [diff] [blame] | 105 | BUILD_TIMESTAMP, 2018); |
Pierre Ossman | ccd09ba | 2015-04-07 14:58:45 +0200 | [diff] [blame] | 106 | |
| 107 | return buffer; |
| 108 | } |
| 109 | |
Pierre Ossman | e2ef5c1 | 2011-07-12 16:56:34 +0000 | [diff] [blame] | 110 | void exit_vncviewer(const char *error) |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 111 | { |
Pierre Ossman | e2ef5c1 | 2011-07-12 16:56:34 +0000 | [diff] [blame] | 112 | // Prioritise the first error we get as that is probably the most |
| 113 | // relevant one. |
| 114 | if ((error != NULL) && (exitError == NULL)) |
| 115 | exitError = strdup(error); |
| 116 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 117 | exitMainloop = true; |
| 118 | } |
| 119 | |
Pierre Ossman | 1db7324 | 2015-09-23 12:20:32 +0200 | [diff] [blame] | 120 | bool should_exit() |
| 121 | { |
| 122 | return exitMainloop; |
| 123 | } |
| 124 | |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 125 | void about_vncviewer() |
| 126 | { |
| 127 | fl_message_title(_("About TigerVNC Viewer")); |
Pierre Ossman | ccd09ba | 2015-04-07 14:58:45 +0200 | [diff] [blame] | 128 | fl_message("%s", about_text()); |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Pierre Ossman | 5102fa9 | 2015-11-18 16:23:21 +0100 | [diff] [blame] | 131 | void run_mainloop() |
| 132 | { |
| 133 | int next_timer; |
| 134 | |
| 135 | next_timer = Timer::checkTimeouts(); |
| 136 | if (next_timer == 0) |
| 137 | next_timer = INT_MAX; |
| 138 | |
| 139 | if (Fl::wait((double)next_timer / 1000.0) < 0.0) { |
| 140 | vlog.error(_("Internal FLTK error. Exiting.")); |
| 141 | exit(-1); |
| 142 | } |
| 143 | } |
| 144 | |
Pierre Ossman | 8d713a9 | 2015-03-04 09:54:27 +0100 | [diff] [blame] | 145 | #ifdef __APPLE__ |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 146 | static void about_callback(Fl_Widget *widget, void *data) |
| 147 | { |
| 148 | about_vncviewer(); |
| 149 | } |
Pierre Ossman | a71a276 | 2015-03-04 09:57:06 +0100 | [diff] [blame] | 150 | |
| 151 | static void new_connection_cb(Fl_Widget *widget, void *data) |
| 152 | { |
| 153 | const char *argv[2]; |
| 154 | pid_t pid; |
| 155 | |
| 156 | pid = fork(); |
| 157 | if (pid == -1) { |
| 158 | vlog.error(_("Error starting new TigerVNC Viewer: %s"), strerror(errno)); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | if (pid != 0) |
| 163 | return; |
| 164 | |
| 165 | argv[0] = argv0; |
| 166 | argv[1] = NULL; |
| 167 | |
| 168 | execvp(argv[0], (char * const *)argv); |
| 169 | |
| 170 | vlog.error(_("Error starting new TigerVNC Viewer: %s"), strerror(errno)); |
| 171 | _exit(1); |
| 172 | } |
Pierre Ossman | 8d713a9 | 2015-03-04 09:54:27 +0100 | [diff] [blame] | 173 | #endif |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 174 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 175 | static void CleanupSignalHandler(int sig) |
| 176 | { |
| 177 | // CleanupSignalHandler allows C++ object cleanup to happen because it calls |
| 178 | // exit() rather than the default which is to abort. |
Pierre Ossman | c76bd2b | 2014-12-03 14:01:31 +0100 | [diff] [blame] | 179 | vlog.info(_("Termination signal %d has been received. TigerVNC Viewer will now exit."), sig); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 180 | exit(1); |
| 181 | } |
| 182 | |
| 183 | static void init_fltk() |
| 184 | { |
| 185 | // Basic text size (10pt @ 96 dpi => 13px) |
| 186 | FL_NORMAL_SIZE = 13; |
| 187 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 188 | // Select a FLTK scheme and background color that looks somewhat |
Pierre Ossman | 490b319 | 2018-01-22 15:12:41 +0100 | [diff] [blame] | 189 | // close to modern systems |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 190 | Fl::scheme("gtk+"); |
| 191 | Fl::background(220, 220, 220); |
Pierre Ossman | 490b319 | 2018-01-22 15:12:41 +0100 | [diff] [blame] | 192 | |
| 193 | // macOS has a slightly brighter default background though |
| 194 | #ifdef __APPLE__ |
| 195 | Fl::background(240, 240, 240); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 196 | #endif |
| 197 | |
Henrik Andersson | 3b83703 | 2011-09-19 13:46:55 +0000 | [diff] [blame] | 198 | // Proper Gnome Shell integration requires that we set a sensible |
| 199 | // WM_CLASS for the window. |
| 200 | Fl_Window::default_xclass("vncviewer"); |
| 201 | |
Pierre Ossman | 8eb3508 | 2012-03-27 12:50:54 +0000 | [diff] [blame] | 202 | // Set the default icon for all windows. |
Pierre Ossman | 8eb3508 | 2012-03-27 12:50:54 +0000 | [diff] [blame] | 203 | #ifdef WIN32 |
| 204 | HICON lg, sm; |
| 205 | |
| 206 | lg = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON), |
| 207 | IMAGE_ICON, GetSystemMetrics(SM_CXICON), |
| 208 | GetSystemMetrics(SM_CYICON), |
| 209 | LR_DEFAULTCOLOR | LR_SHARED); |
| 210 | sm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON), |
| 211 | IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), |
| 212 | GetSystemMetrics(SM_CYSMICON), |
| 213 | LR_DEFAULTCOLOR | LR_SHARED); |
| 214 | |
| 215 | Fl_Window::default_icons(lg, sm); |
| 216 | #elif ! defined(__APPLE__) |
| 217 | const int icon_sizes[] = {48, 32, 24, 16}; |
| 218 | |
| 219 | Fl_PNG_Image *icons[4]; |
| 220 | int count; |
| 221 | |
| 222 | count = 0; |
| 223 | |
| 224 | // FIXME: Follow icon theme specification |
| 225 | for (size_t i = 0;i < sizeof(icon_sizes)/sizeof(icon_sizes[0]);i++) { |
| 226 | char icon_path[PATH_MAX]; |
| 227 | bool exists; |
| 228 | |
Pierre Ossman | 6a007bd | 2012-09-11 10:56:21 +0000 | [diff] [blame] | 229 | sprintf(icon_path, "%s/icons/hicolor/%dx%d/apps/tigervnc.png", |
Pierre Ossman | 8eb3508 | 2012-03-27 12:50:54 +0000 | [diff] [blame] | 230 | DATA_DIR, icon_sizes[i], icon_sizes[i]); |
| 231 | |
| 232 | #ifndef WIN32 |
| 233 | struct stat st; |
| 234 | if (stat(icon_path, &st) != 0) |
| 235 | #else |
| 236 | struct _stat st; |
| 237 | if (_stat(icon_path, &st) != 0) |
| 238 | return(false); |
| 239 | #endif |
| 240 | exists = false; |
| 241 | else |
| 242 | exists = true; |
| 243 | |
| 244 | if (exists) { |
| 245 | icons[count] = new Fl_PNG_Image(icon_path); |
| 246 | if (icons[count]->w() == 0 || |
| 247 | icons[count]->h() == 0 || |
| 248 | icons[count]->d() != 4) { |
| 249 | delete icons[count]; |
| 250 | continue; |
| 251 | } |
| 252 | |
| 253 | count++; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | Fl_Window::default_icons((const Fl_RGB_Image**)icons, count); |
| 258 | |
| 259 | for (int i = 0;i < count;i++) |
| 260 | delete icons[i]; |
| 261 | #endif |
Pierre Ossman | 8eb3508 | 2012-03-27 12:50:54 +0000 | [diff] [blame] | 262 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 263 | // This makes the "icon" in dialogs rounded, which fits better |
| 264 | // with the above schemes. |
| 265 | fl_message_icon()->box(FL_UP_BOX); |
| 266 | |
| 267 | // Turn off the annoying behaviour where popups track the mouse. |
| 268 | fl_message_hotspot(false); |
| 269 | |
| 270 | // Avoid empty titles for popups |
| 271 | fl_message_title_default(_("TigerVNC Viewer")); |
| 272 | |
| 273 | #ifdef WIN32 |
| 274 | // Most "normal" Windows apps use this font for UI elements. |
| 275 | Fl::set_font(FL_HELVETICA, "Tahoma"); |
| 276 | #endif |
| 277 | |
| 278 | // FLTK exposes these so that we can translate them. |
| 279 | fl_no = _("No"); |
| 280 | fl_yes = _("Yes"); |
| 281 | fl_ok = _("OK"); |
| 282 | fl_cancel = _("Cancel"); |
| 283 | fl_close = _("Close"); |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 284 | |
| 285 | #ifdef __APPLE__ |
Peter Åstrand | b7c5524 | 2011-08-29 13:14:51 +0000 | [diff] [blame] | 286 | /* Needs trailing space */ |
| 287 | static char fltk_about[16]; |
| 288 | snprintf(fltk_about, sizeof(fltk_about), "%s ", _("About")); |
| 289 | Fl_Mac_App_Menu::about = fltk_about; |
| 290 | static char fltk_hide[16]; |
| 291 | snprintf(fltk_hide, sizeof(fltk_hide), "%s ", _("Hide")); |
| 292 | Fl_Mac_App_Menu::hide = fltk_hide; |
| 293 | static char fltk_quit[16]; |
| 294 | snprintf(fltk_quit, sizeof(fltk_quit), "%s ", _("Quit")); |
| 295 | Fl_Mac_App_Menu::quit = fltk_quit; |
| 296 | |
Pierre Ossman | 41ba603 | 2011-06-16 11:09:31 +0000 | [diff] [blame] | 297 | Fl_Mac_App_Menu::print = ""; // Don't want the print item |
| 298 | Fl_Mac_App_Menu::services = _("Services"); |
Pierre Ossman | 41ba603 | 2011-06-16 11:09:31 +0000 | [diff] [blame] | 299 | Fl_Mac_App_Menu::hide_others = _("Hide Others"); |
| 300 | Fl_Mac_App_Menu::show = _("Show All"); |
Pierre Ossman | 41ba603 | 2011-06-16 11:09:31 +0000 | [diff] [blame] | 301 | |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 302 | fl_mac_set_about(about_callback, NULL); |
Pierre Ossman | a71a276 | 2015-03-04 09:57:06 +0100 | [diff] [blame] | 303 | |
| 304 | Fl_Sys_Menu_Bar *menubar; |
| 305 | char buffer[1024]; |
| 306 | menubar = new Fl_Sys_Menu_Bar(0, 0, 500, 25); |
| 307 | // Fl_Sys_Menu_Bar overrides methods without them being virtual, |
| 308 | // which means we cannot use our generic Fl_Menu_ helpers. |
Pierre Ossman | ff626f8 | 2015-09-23 16:39:54 +0200 | [diff] [blame] | 309 | if (fltk_menu_escape(p_("SysMenu|", "&File"), |
| 310 | buffer, sizeof(buffer)) < sizeof(buffer)) |
Pierre Ossman | a71a276 | 2015-03-04 09:57:06 +0100 | [diff] [blame] | 311 | menubar->add(buffer, 0, 0, 0, FL_SUBMENU); |
Pierre Ossman | ff626f8 | 2015-09-23 16:39:54 +0200 | [diff] [blame] | 312 | if (fltk_menu_escape(p_("SysMenu|File|", "&New Connection"), |
| 313 | buffer, sizeof(buffer)) < sizeof(buffer)) |
Pierre Ossman | a71a276 | 2015-03-04 09:57:06 +0100 | [diff] [blame] | 314 | menubar->insert(1, buffer, FL_COMMAND | 'n', new_connection_cb); |
Pierre Ossman | b885822 | 2011-04-29 11:51:38 +0000 | [diff] [blame] | 315 | #endif |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static void mkvnchomedir() |
| 319 | { |
| 320 | // Create .vnc in the user's home directory if it doesn't already exist |
| 321 | char* homeDir = NULL; |
| 322 | |
| 323 | if (getvnchomedir(&homeDir) == -1) { |
| 324 | vlog.error(_("Could not create VNC home directory: can't obtain home " |
| 325 | "directory path.")); |
| 326 | } else { |
| 327 | int result = mkdir(homeDir, 0755); |
| 328 | if (result == -1 && errno != EEXIST) |
| 329 | vlog.error(_("Could not create VNC home directory: %s."), strerror(errno)); |
| 330 | delete [] homeDir; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | static void usage(const char *programName) |
| 335 | { |
Pierre Ossman | 5bf17ad | 2015-02-17 13:23:00 +0100 | [diff] [blame] | 336 | #ifdef WIN32 |
| 337 | // If we don't have a console then we need to create one for output |
| 338 | if (GetConsoleWindow() == NULL) { |
| 339 | HANDLE handle; |
| 340 | int fd; |
| 341 | |
| 342 | AllocConsole(); |
| 343 | |
| 344 | handle = GetStdHandle(STD_ERROR_HANDLE); |
| 345 | fd = _open_osfhandle((intptr_t)handle, O_TEXT); |
| 346 | *stderr = *fdopen(fd, "w"); |
| 347 | } |
| 348 | #endif |
| 349 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 350 | fprintf(stderr, |
Pierre Ossman | 2b69d38 | 2018-09-28 15:31:14 +0200 | [diff] [blame] | 351 | "\n" |
| 352 | "usage: %s [parameters] [host][:displayNum]\n" |
| 353 | " %s [parameters] [host][::port]\n" |
| 354 | #ifndef WIN32 |
| 355 | " %s [parameters] [unix socket]\n" |
| 356 | #endif |
| 357 | " %s [parameters] -listen [port]\n" |
Mathias Jonsson | 50439d7 | 2018-04-22 19:10:30 +0200 | [diff] [blame] | 358 | " %s [parameters] [.tigervnc file]\n", |
Pierre Ossman | 2b69d38 | 2018-09-28 15:31:14 +0200 | [diff] [blame] | 359 | programName, programName, |
| 360 | #ifndef WIN32 |
| 361 | programName, |
| 362 | #endif |
| 363 | programName, programName); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 364 | fprintf(stderr,"\n" |
| 365 | "Parameters can be turned on with -<param> or off with -<param>=0\n" |
| 366 | "Parameters which take a value can be specified as " |
| 367 | "-<param> <value>\n" |
| 368 | "Other valid forms are <param>=<value> -<param>=<value> " |
| 369 | "--<param>=<value>\n" |
| 370 | "Parameter names are case-insensitive. The parameters are:\n\n"); |
| 371 | Configuration::listParams(79, 14); |
Pierre Ossman | 5bf17ad | 2015-02-17 13:23:00 +0100 | [diff] [blame] | 372 | |
| 373 | #ifdef WIN32 |
| 374 | // Just wait for the user to kill the console window |
| 375 | Sleep(INFINITE); |
| 376 | #endif |
| 377 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 378 | exit(1); |
| 379 | } |
| 380 | |
Mathias Jonsson | 50439d7 | 2018-04-22 19:10:30 +0200 | [diff] [blame] | 381 | static void |
| 382 | potentiallyLoadConfigurationFile(char *vncServerName) |
| 383 | { |
| 384 | const bool hasPathSeparator = (strchr(vncServerName, '/') != NULL || |
| 385 | (strchr(vncServerName, '\\')) != NULL); |
| 386 | |
| 387 | if (hasPathSeparator) { |
Pierre Ossman | fe2e5fc | 2018-05-29 16:46:19 +0200 | [diff] [blame] | 388 | #ifndef WIN32 |
| 389 | struct stat sb; |
| 390 | |
| 391 | // This might be a UNIX socket, we need to check |
| 392 | if (stat(vncServerName, &sb) == -1) { |
| 393 | // Some access problem; let loadViewerParameters() deal with it... |
| 394 | } else { |
| 395 | if ((sb.st_mode & S_IFMT) == S_IFSOCK) |
| 396 | return; |
| 397 | } |
| 398 | #endif |
| 399 | |
Mathias Jonsson | 50439d7 | 2018-04-22 19:10:30 +0200 | [diff] [blame] | 400 | try { |
Pierre Ossman | cb99be5 | 2018-05-29 16:37:54 +0200 | [diff] [blame] | 401 | const char* newServerName; |
| 402 | newServerName = loadViewerParameters(vncServerName); |
| 403 | // This might be empty, but we still need to clear it so we |
| 404 | // don't try to connect to the filename |
| 405 | strncpy(vncServerName, newServerName, VNCSERVERNAMELEN); |
Mathias Jonsson | 50439d7 | 2018-04-22 19:10:30 +0200 | [diff] [blame] | 406 | } catch (rfb::Exception& e) { |
| 407 | vlog.error("%s", e.str()); |
| 408 | if (alertOnFatalError) |
| 409 | fl_alert("%s", e.str()); |
| 410 | exit(EXIT_FAILURE); |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 415 | #ifndef WIN32 |
| 416 | static int |
| 417 | interpretViaParam(char *remoteHost, int *remotePort, int localPort) |
| 418 | { |
| 419 | const int SERVER_PORT_OFFSET = 5900; |
| 420 | char *pos = strchr(vncServerName, ':'); |
| 421 | if (pos == NULL) |
| 422 | *remotePort = SERVER_PORT_OFFSET; |
| 423 | else { |
| 424 | int portOffset = SERVER_PORT_OFFSET; |
| 425 | size_t len; |
| 426 | *pos++ = '\0'; |
| 427 | len = strlen(pos); |
| 428 | if (*pos == ':') { |
| 429 | /* Two colons is an absolute port number, not an offset. */ |
| 430 | pos++; |
| 431 | len--; |
| 432 | portOffset = 0; |
| 433 | } |
| 434 | if (!len || strspn (pos, "-0123456789") != len ) |
| 435 | return 1; |
| 436 | *remotePort = atoi(pos) + portOffset; |
| 437 | } |
| 438 | |
| 439 | if (*vncServerName != '\0') |
| 440 | strncpy(remoteHost, vncServerName, VNCSERVERNAMELEN); |
| 441 | else |
| 442 | strncpy(remoteHost, "localhost", VNCSERVERNAMELEN); |
| 443 | |
| 444 | remoteHost[VNCSERVERNAMELEN - 1] = '\0'; |
| 445 | |
| 446 | snprintf(vncServerName, VNCSERVERNAMELEN, "localhost::%d", localPort); |
| 447 | vncServerName[VNCSERVERNAMELEN - 1] = '\0'; |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | static void |
| 453 | createTunnel(const char *gatewayHost, const char *remoteHost, |
| 454 | int remotePort, int localPort) |
| 455 | { |
Pierre Ossman | f8d525b | 2014-07-09 17:02:27 +0200 | [diff] [blame] | 456 | const char *cmd = getenv("VNC_VIA_CMD"); |
| 457 | char *cmd2, *percent; |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 458 | char lport[10], rport[10]; |
| 459 | sprintf(lport, "%d", localPort); |
| 460 | sprintf(rport, "%d", remotePort); |
| 461 | setenv("G", gatewayHost, 1); |
| 462 | setenv("H", remoteHost, 1); |
| 463 | setenv("R", rport, 1); |
| 464 | setenv("L", lport, 1); |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 465 | if (!cmd) |
| 466 | cmd = "/usr/bin/ssh -f -L \"$L\":\"$H\":\"$R\" \"$G\" sleep 20"; |
| 467 | /* Compatibility with TigerVNC's method. */ |
Pierre Ossman | f8d525b | 2014-07-09 17:02:27 +0200 | [diff] [blame] | 468 | cmd2 = strdup(cmd); |
| 469 | while ((percent = strchr(cmd2, '%')) != NULL) |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 470 | *percent = '$'; |
Pierre Ossman | f8d525b | 2014-07-09 17:02:27 +0200 | [diff] [blame] | 471 | system(cmd2); |
| 472 | free(cmd2); |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | static int mktunnel() |
| 476 | { |
| 477 | const char *gatewayHost; |
| 478 | char remoteHost[VNCSERVERNAMELEN]; |
| 479 | int localPort = findFreeTcpPort(); |
| 480 | int remotePort; |
| 481 | |
| 482 | gatewayHost = strDup(via.getValueStr()); |
| 483 | if (interpretViaParam(remoteHost, &remotePort, localPort) != 0) |
| 484 | return 1; |
| 485 | createTunnel(gatewayHost, remoteHost, remotePort, localPort); |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | #endif /* !WIN32 */ |
| 490 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 491 | int main(int argc, char** argv) |
| 492 | { |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 493 | UserDialog dlg; |
| 494 | |
Pierre Ossman | a71a276 | 2015-03-04 09:57:06 +0100 | [diff] [blame] | 495 | argv0 = argv[0]; |
| 496 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 497 | setlocale(LC_ALL, ""); |
Pierre Ossman | 0878eca | 2012-03-27 13:03:22 +0000 | [diff] [blame] | 498 | bindtextdomain(PACKAGE_NAME, LOCALE_DIR); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 499 | textdomain(PACKAGE_NAME); |
| 500 | |
| 501 | rfb::SecurityClient::setDefaults(); |
| 502 | |
| 503 | // Write about text to console, still using normal locale codeset |
Pierre Ossman | ccd09ba | 2015-04-07 14:58:45 +0200 | [diff] [blame] | 504 | fprintf(stderr,"\n%s\n", about_text()); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 505 | |
| 506 | // Set gettext codeset to what our GUI toolkit uses. Since we are |
| 507 | // passing strings from strerror/gai_strerror to the GUI, these must |
| 508 | // be in GUI codeset as well. |
| 509 | bind_textdomain_codeset(PACKAGE_NAME, "UTF-8"); |
| 510 | bind_textdomain_codeset("libc", "UTF-8"); |
| 511 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 512 | rfb::initStdIOLoggers(); |
Pierre Ossman | c8719ad | 2012-04-26 14:27:52 +0000 | [diff] [blame] | 513 | #ifdef WIN32 |
| 514 | rfb::initFileLogger("C:\\temp\\vncviewer.log"); |
| 515 | #else |
| 516 | rfb::initFileLogger("/tmp/vncviewer.log"); |
| 517 | #endif |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 518 | rfb::LogWriter::setLogParams("*:stderr:30"); |
| 519 | |
| 520 | #ifdef SIGHUP |
| 521 | signal(SIGHUP, CleanupSignalHandler); |
| 522 | #endif |
| 523 | signal(SIGINT, CleanupSignalHandler); |
| 524 | signal(SIGTERM, CleanupSignalHandler); |
| 525 | |
| 526 | init_fltk(); |
| 527 | |
| 528 | Configuration::enableViewerParams(); |
| 529 | |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 530 | /* Load the default parameter settings */ |
Pierre Ossman | b08a648 | 2018-06-13 12:58:59 +0200 | [diff] [blame] | 531 | char defaultServerName[VNCSERVERNAMELEN] = ""; |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 532 | try { |
Pierre Ossman | b08a648 | 2018-06-13 12:58:59 +0200 | [diff] [blame] | 533 | const char* configServerName; |
| 534 | configServerName = loadViewerParameters(NULL); |
| 535 | if (configServerName != NULL) |
| 536 | strncpy(defaultServerName, configServerName, VNCSERVERNAMELEN); |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 537 | } catch (rfb::Exception& e) { |
Dr. David Alan Gilbert | f4d1d89 | 2017-07-11 12:11:50 +0100 | [diff] [blame] | 538 | vlog.error("%s", e.str()); |
| 539 | if (alertOnFatalError) |
| 540 | fl_alert("%s", e.str()); |
Peter Åstrand | 8a2b081 | 2012-08-08 11:49:01 +0000 | [diff] [blame] | 541 | } |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 542 | |
Pierre Ossman | 8c2a744 | 2017-11-14 16:04:32 +0100 | [diff] [blame] | 543 | for (int i = 1; i < argc;) { |
Pierre Ossman | 8c2a744 | 2017-11-14 16:04:32 +0100 | [diff] [blame] | 544 | if (Configuration::setParam(argv[i])) { |
| 545 | i++; |
| 546 | continue; |
| 547 | } |
| 548 | |
| 549 | if (argv[i][0] == '-') { |
| 550 | if (i+1 < argc) { |
| 551 | if (Configuration::setParam(&argv[i][1], argv[i+1])) { |
| 552 | i += 2; |
| 553 | continue; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 556 | |
Pierre Ossman | 8c2a744 | 2017-11-14 16:04:32 +0100 | [diff] [blame] | 557 | usage(argv[0]); |
DRC | 5aa0650 | 2011-06-23 22:04:46 +0000 | [diff] [blame] | 558 | } |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 559 | |
Pierre Ossman | 8c2a744 | 2017-11-14 16:04:32 +0100 | [diff] [blame] | 560 | strncpy(vncServerName, argv[i], VNCSERVERNAMELEN); |
| 561 | vncServerName[VNCSERVERNAMELEN - 1] = '\0'; |
| 562 | i++; |
| 563 | } |
| 564 | |
Mathias Jonsson | 50439d7 | 2018-04-22 19:10:30 +0200 | [diff] [blame] | 565 | // Check if the server name in reality is a configuration file |
| 566 | potentiallyLoadConfigurationFile(vncServerName); |
| 567 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 568 | mkvnchomedir(); |
| 569 | |
Pierre Ossman | 727a70f | 2017-11-14 15:51:50 +0100 | [diff] [blame] | 570 | #if !defined(WIN32) && !defined(__APPLE__) |
Peter Åstrand (astrand) | 3ec76bd | 2018-01-29 10:42:44 +0100 | [diff] [blame] | 571 | if (strcmp(display, "") != 0) { |
| 572 | Fl::display(display); |
| 573 | } |
Pierre Ossman | 727a70f | 2017-11-14 15:51:50 +0100 | [diff] [blame] | 574 | fl_open_display(); |
| 575 | XkbSetDetectableAutoRepeat(fl_display, True, NULL); |
| 576 | #endif |
| 577 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 578 | CSecurity::upg = &dlg; |
| 579 | #ifdef HAVE_GNUTLS |
| 580 | CSecurityTLS::msg = &dlg; |
| 581 | #endif |
| 582 | |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 583 | Socket *sock = NULL; |
| 584 | |
DRC | 3e7ed81 | 2013-02-26 10:34:22 +0000 | [diff] [blame] | 585 | #ifndef WIN32 |
Adam Tkac | 571089b | 2013-02-19 14:30:32 +0000 | [diff] [blame] | 586 | /* Specifying -via and -listen together is nonsense */ |
| 587 | if (listenMode && strlen(via.getValueStr()) > 0) { |
Pierre Ossman | 744e55c | 2014-12-03 14:00:54 +0100 | [diff] [blame] | 588 | // TRANSLATORS: "Parameters" are command line arguments, or settings |
| 589 | // from a file or the Windows registry. |
Pierre Ossman | 8ca4c1d | 2014-09-22 12:54:26 +0200 | [diff] [blame] | 590 | vlog.error(_("Parameters -listen and -via are incompatible")); |
Dr. David Alan Gilbert | f4d1d89 | 2017-07-11 12:11:50 +0100 | [diff] [blame] | 591 | if (alertOnFatalError) |
| 592 | fl_alert(_("Parameters -listen and -via are incompatible")); |
Adam Tkac | 571089b | 2013-02-19 14:30:32 +0000 | [diff] [blame] | 593 | exit_vncviewer(); |
| 594 | return 1; |
| 595 | } |
DRC | 3e7ed81 | 2013-02-26 10:34:22 +0000 | [diff] [blame] | 596 | #endif |
Adam Tkac | 571089b | 2013-02-19 14:30:32 +0000 | [diff] [blame] | 597 | |
| 598 | if (listenMode) { |
Pierre Ossman | e3a2be6 | 2018-05-03 14:03:55 +0200 | [diff] [blame] | 599 | std::list<SocketListener*> listeners; |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 600 | try { |
| 601 | int port = 5500; |
| 602 | if (isdigit(vncServerName[0])) |
| 603 | port = atoi(vncServerName); |
| 604 | |
Tim Waugh | 892d10a | 2015-03-11 13:12:07 +0000 | [diff] [blame] | 605 | createTcpListeners(&listeners, 0, port); |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 606 | |
Peter Åstrand (astrand) | 4ab2f8a | 2015-03-18 10:31:01 +0100 | [diff] [blame] | 607 | vlog.info(_("Listening on port %d"), port); |
Tim Waugh | 892d10a | 2015-03-11 13:12:07 +0000 | [diff] [blame] | 608 | |
| 609 | /* Wait for a connection */ |
| 610 | while (sock == NULL) { |
| 611 | fd_set rfds; |
| 612 | FD_ZERO(&rfds); |
Pierre Ossman | e3a2be6 | 2018-05-03 14:03:55 +0200 | [diff] [blame] | 613 | for (std::list<SocketListener*>::iterator i = listeners.begin(); |
Tim Waugh | 892d10a | 2015-03-11 13:12:07 +0000 | [diff] [blame] | 614 | i != listeners.end(); |
| 615 | i++) |
Pierre Ossman | f7aa3f9 | 2015-09-29 15:40:49 +0200 | [diff] [blame] | 616 | FD_SET((*i)->getFd(), &rfds); |
Tim Waugh | 892d10a | 2015-03-11 13:12:07 +0000 | [diff] [blame] | 617 | |
| 618 | int n = select(FD_SETSIZE, &rfds, 0, 0, 0); |
| 619 | if (n < 0) { |
| 620 | if (errno == EINTR) { |
| 621 | vlog.debug("Interrupted select() system call"); |
| 622 | continue; |
| 623 | } else { |
| 624 | throw rdr::SystemException("select", errno); |
| 625 | } |
| 626 | } |
| 627 | |
Pierre Ossman | e3a2be6 | 2018-05-03 14:03:55 +0200 | [diff] [blame] | 628 | for (std::list<SocketListener*>::iterator i = listeners.begin (); |
Tim Waugh | 892d10a | 2015-03-11 13:12:07 +0000 | [diff] [blame] | 629 | i != listeners.end(); |
| 630 | i++) |
Pierre Ossman | f7aa3f9 | 2015-09-29 15:40:49 +0200 | [diff] [blame] | 631 | if (FD_ISSET((*i)->getFd(), &rfds)) { |
| 632 | sock = (*i)->accept(); |
Tim Waugh | 892d10a | 2015-03-11 13:12:07 +0000 | [diff] [blame] | 633 | if (sock) |
| 634 | /* Got a connection */ |
| 635 | break; |
| 636 | } |
| 637 | } |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 638 | } catch (rdr::Exception& e) { |
| 639 | vlog.error("%s", e.str()); |
Dr. David Alan Gilbert | f4d1d89 | 2017-07-11 12:11:50 +0100 | [diff] [blame] | 640 | if (alertOnFatalError) |
| 641 | fl_alert("%s", e.str()); |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 642 | exit_vncviewer(); |
| 643 | return 1; |
| 644 | } |
| 645 | |
Pierre Ossman | f7aa3f9 | 2015-09-29 15:40:49 +0200 | [diff] [blame] | 646 | while (!listeners.empty()) { |
| 647 | delete listeners.back(); |
| 648 | listeners.pop_back(); |
| 649 | } |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 650 | } else { |
| 651 | if (vncServerName[0] == '\0') { |
| 652 | ServerDialog::run(defaultServerName, vncServerName); |
| 653 | if (vncServerName[0] == '\0') |
| 654 | return 1; |
| 655 | } |
Pierre Ossman | 561ff0c | 2011-05-13 14:04:59 +0000 | [diff] [blame] | 656 | |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 657 | #ifndef WIN32 |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 658 | if (strlen (via.getValueStr()) > 0 && mktunnel() != 0) |
| 659 | usage(argv[0]); |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 660 | #endif |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 661 | } |
Adam Tkac | 8ac4b30 | 2013-01-23 13:55:46 +0000 | [diff] [blame] | 662 | |
Pierre Ossman | 2a7a8d6 | 2013-02-15 08:33:39 +0000 | [diff] [blame] | 663 | CConn *cc = new CConn(vncServerName, sock); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 664 | |
Pierre Ossman | 5102fa9 | 2015-11-18 16:23:21 +0100 | [diff] [blame] | 665 | while (!exitMainloop) |
| 666 | run_mainloop(); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 667 | |
Pierre Ossman | e2ef5c1 | 2011-07-12 16:56:34 +0000 | [diff] [blame] | 668 | delete cc; |
| 669 | |
Dr. David Alan Gilbert | f4d1d89 | 2017-07-11 12:11:50 +0100 | [diff] [blame] | 670 | if (exitError != NULL && alertOnFatalError) |
Pierre Ossman | f52740e | 2012-04-25 15:43:56 +0000 | [diff] [blame] | 671 | fl_alert("%s", exitError); |
Pierre Ossman | e2ef5c1 | 2011-07-12 16:56:34 +0000 | [diff] [blame] | 672 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 673 | return 0; |
| 674 | } |