You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vncExtInit.cc 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011-2019 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <unistd.h>
  25. #include <sys/types.h>
  26. #include <pwd.h>
  27. #include <set>
  28. #include <string>
  29. #include <rfb/Configuration.h>
  30. #include <rfb/Logger_stdio.h>
  31. #include <rfb/LogWriter.h>
  32. #include <rfb/ServerCore.h>
  33. #include <rdr/HexOutStream.h>
  34. #include <rfb/LogWriter.h>
  35. #include <rfb/Hostname.h>
  36. #include <rfb/Region.h>
  37. #include <rfb/ledStates.h>
  38. #include <network/TcpSocket.h>
  39. #include <network/UnixSocket.h>
  40. #include "XserverDesktop.h"
  41. #include "vncExtInit.h"
  42. #include "vncHooks.h"
  43. #include "vncBlockHandler.h"
  44. #include "vncSelection.h"
  45. #include "XorgGlue.h"
  46. #include "RandrGlue.h"
  47. #include "xorg-version.h"
  48. extern "C" {
  49. void vncSetGlueContext(int screenIndex);
  50. }
  51. using namespace rfb;
  52. static rfb::LogWriter vlog("vncext");
  53. // We can't safely get this from Xorg
  54. #define MAXSCREENS 16
  55. static unsigned long vncExtGeneration = 0;
  56. static bool initialised = false;
  57. static XserverDesktop* desktop[MAXSCREENS] = { 0, };
  58. void* vncFbptr[MAXSCREENS] = { 0, };
  59. int vncFbstride[MAXSCREENS];
  60. int vncInetdSock = -1;
  61. struct CaseInsensitiveCompare {
  62. bool operator() (const std::string &a, const std::string &b) const {
  63. return strcasecmp(a.c_str(), b.c_str()) < 0;
  64. }
  65. };
  66. typedef std::set<std::string, CaseInsensitiveCompare> ParamSet;
  67. static ParamSet allowOverrideSet;
  68. static const char* defaultDesktopName();
  69. rfb::IntParameter rfbport("rfbport", "TCP port to listen for RFB protocol",0);
  70. rfb::StringParameter rfbunixpath("rfbunixpath", "Unix socket to listen for RFB protocol", "");
  71. rfb::IntParameter rfbunixmode("rfbunixmode", "Unix socket access mode", 0600);
  72. rfb::StringParameter desktopName("desktop", "Name of VNC desktop", defaultDesktopName());
  73. rfb::BoolParameter localhostOnly("localhost",
  74. "Only allow connections from localhost",
  75. false);
  76. rfb::StringParameter interface("interface",
  77. "listen on the specified network address",
  78. "all");
  79. rfb::BoolParameter avoidShiftNumLock("AvoidShiftNumLock",
  80. "Avoid fake Shift presses for keys affected by NumLock.",
  81. true);
  82. rfb::StringParameter allowOverride("AllowOverride",
  83. "Comma separated list of parameters that can be modified using VNC extension.",
  84. "desktop,AcceptPointerEvents,SendCutText,AcceptCutText,SendPrimary,SetPrimary");
  85. rfb::BoolParameter setPrimary("SetPrimary", "Set the PRIMARY as well "
  86. "as the CLIPBOARD selection", true);
  87. rfb::BoolParameter sendPrimary("SendPrimary",
  88. "Send the PRIMARY as well as the CLIPBOARD selection",
  89. true);
  90. static const char* defaultDesktopName()
  91. {
  92. size_t host_max = sysconf(_SC_HOST_NAME_MAX);
  93. if (host_max < 0)
  94. return "";
  95. std::vector<char> hostname(host_max + 1);
  96. if (gethostname(hostname.data(), hostname.size()) == -1)
  97. return "";
  98. struct passwd* pwent = getpwuid(getuid());
  99. if (pwent == NULL)
  100. return "";
  101. size_t len = snprintf(NULL, 0, "%s@%s", pwent->pw_name, hostname.data());
  102. if (len < 0)
  103. return "";
  104. char* name = new char[len + 1];
  105. snprintf(name, len + 1, "%s@%s", pwent->pw_name, hostname.data());
  106. return name;
  107. }
  108. static PixelFormat vncGetPixelFormat(int scrIdx)
  109. {
  110. int depth, bpp;
  111. int trueColour, bigEndian;
  112. int redMask, greenMask, blueMask;
  113. int redShift, greenShift, blueShift;
  114. int redMax, greenMax, blueMax;
  115. vncGetScreenFormat(scrIdx, &depth, &bpp, &trueColour, &bigEndian,
  116. &redMask, &greenMask, &blueMask);
  117. if (!trueColour) {
  118. vlog.error("pseudocolour not supported");
  119. abort();
  120. }
  121. redShift = ffs(redMask) - 1;
  122. greenShift = ffs(greenMask) - 1;
  123. blueShift = ffs(blueMask) - 1;
  124. redMax = redMask >> redShift;
  125. greenMax = greenMask >> greenShift;
  126. blueMax = blueMask >> blueShift;
  127. return PixelFormat(bpp, depth, bigEndian, trueColour,
  128. redMax, greenMax, blueMax,
  129. redShift, greenShift, blueShift);
  130. }
  131. static void parseOverrideList(const char *text, ParamSet &out)
  132. {
  133. for (const char* iter = text; ; ++iter) {
  134. if (*iter == ',' || *iter == '\0') {
  135. out.insert(std::string(text, iter));
  136. text = iter + 1;
  137. if (*iter == '\0')
  138. break;
  139. }
  140. }
  141. }
  142. void vncExtensionInit(void)
  143. {
  144. if (vncExtGeneration == vncGetServerGeneration()) {
  145. vlog.error("vncExtensionInit: called twice in same generation?");
  146. return;
  147. }
  148. vncExtGeneration = vncGetServerGeneration();
  149. if (vncGetScreenCount() > MAXSCREENS)
  150. vncFatalError("vncExtensionInit: too many screens\n");
  151. vncAddExtension();
  152. vncSelectionInit();
  153. vlog.info("VNC extension running!");
  154. try {
  155. if (!initialised) {
  156. rfb::initStdIOLoggers();
  157. parseOverrideList(allowOverride, allowOverrideSet);
  158. allowOverride.setImmutable();
  159. initialised = true;
  160. }
  161. for (int scr = 0; scr < vncGetScreenCount(); scr++) {
  162. if (!desktop[scr]) {
  163. std::list<network::SocketListener*> listeners;
  164. bool inetd = false;
  165. if (scr == 0 && vncInetdSock != -1) {
  166. inetd = true;
  167. if (network::isSocketListening(vncInetdSock))
  168. {
  169. listeners.push_back(new network::TcpListener(vncInetdSock));
  170. vlog.info("inetd wait");
  171. }
  172. }
  173. if (!inetd && ((const char*)rfbunixpath)[0] != '\0') {
  174. char path[PATH_MAX];
  175. int mode = (int)rfbunixmode;
  176. if (scr == 0)
  177. strncpy(path, rfbunixpath, sizeof(path));
  178. else
  179. snprintf(path, sizeof(path), "%s.%d",
  180. (const char*)rfbunixpath, scr);
  181. path[sizeof(path)-1] = '\0';
  182. listeners.push_back(new network::UnixListener(path, mode));
  183. vlog.info("Listening for VNC connections on %s (mode %04o)",
  184. path, mode);
  185. }
  186. if (!inetd && rfbport != -1) {
  187. std::list<network::SocketListener*> tcp_listeners;
  188. const char *addr = interface;
  189. int port = rfbport;
  190. if (port == 0) port = 5900 + atoi(vncGetDisplay());
  191. port += 1000 * scr;
  192. if (strcasecmp(addr, "all") == 0)
  193. addr = 0;
  194. if (localhostOnly)
  195. network::createLocalTcpListeners(&tcp_listeners, port);
  196. else
  197. network::createTcpListeners(&tcp_listeners, addr, port);
  198. if (!tcp_listeners.empty()) {
  199. listeners.splice (listeners.end(), tcp_listeners);
  200. vlog.info("Listening for VNC connections on %s interface(s), port %d",
  201. localhostOnly ? "local" : (const char*)interface,
  202. port);
  203. }
  204. }
  205. if (!inetd && listeners.empty())
  206. throw rdr::Exception("No path or port configured for incoming connections");
  207. PixelFormat pf = vncGetPixelFormat(scr);
  208. vncSetGlueContext(scr);
  209. desktop[scr] = new XserverDesktop(scr,
  210. listeners,
  211. desktopName,
  212. pf,
  213. vncGetScreenWidth(),
  214. vncGetScreenHeight(),
  215. vncFbptr[scr],
  216. vncFbstride[scr]);
  217. vlog.info("created VNC server for screen %d", scr);
  218. if (scr == 0 && vncInetdSock != -1 && listeners.empty()) {
  219. network::Socket* sock = new network::TcpSocket(vncInetdSock);
  220. desktop[scr]->addClient(sock, false, false);
  221. vlog.info("added inetd sock");
  222. }
  223. }
  224. vncHooksInit(scr);
  225. }
  226. } catch (rdr::Exception& e) {
  227. vncFatalError("vncExtInit: %s\n",e.str());
  228. }
  229. vncRegisterBlockHandlers();
  230. }
  231. void vncExtensionClose(void)
  232. {
  233. try {
  234. for (int scr = 0; scr < vncGetScreenCount(); scr++) {
  235. delete desktop[scr];
  236. desktop[scr] = NULL;
  237. }
  238. } catch (rdr::Exception& e) {
  239. vncFatalError("vncExtInit: %s\n",e.str());
  240. }
  241. }
  242. void vncHandleSocketEvent(int fd, int scrIdx, int read, int write)
  243. {
  244. desktop[scrIdx]->handleSocketEvent(fd, read, write);
  245. }
  246. void vncCallBlockHandlers(int* timeout)
  247. {
  248. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  249. desktop[scr]->blockHandler(timeout);
  250. }
  251. int vncGetAvoidShiftNumLock(void)
  252. {
  253. return (bool)avoidShiftNumLock;
  254. }
  255. int vncGetSetPrimary(void)
  256. {
  257. return (bool)setPrimary;
  258. }
  259. int vncGetSendPrimary(void)
  260. {
  261. return (bool)sendPrimary;
  262. }
  263. void vncUpdateDesktopName(void)
  264. {
  265. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  266. desktop[scr]->setDesktopName(desktopName);
  267. }
  268. void vncRequestClipboard(void)
  269. {
  270. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  271. desktop[scr]->requestClipboard();
  272. }
  273. void vncAnnounceClipboard(int available)
  274. {
  275. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  276. desktop[scr]->announceClipboard(available);
  277. }
  278. void vncSendClipboardData(const char* data)
  279. {
  280. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  281. desktop[scr]->sendClipboardData(data);
  282. }
  283. int vncConnectClient(const char *addr, int viewOnly)
  284. {
  285. if (strlen(addr) == 0) {
  286. try {
  287. desktop[0]->disconnectClients();
  288. } catch (rdr::Exception& e) {
  289. vlog.error("Disconnecting all clients: %s",e.str());
  290. return -1;
  291. }
  292. return 0;
  293. }
  294. std::string host;
  295. int port;
  296. getHostAndPort(addr, &host, &port, 5500);
  297. try {
  298. network::Socket* sock = new network::TcpSocket(host.c_str(), port);
  299. vlog.info("Reverse connection: %s:%d%s", host.c_str(), port,
  300. viewOnly ? " (view only)" : "");
  301. desktop[0]->addClient(sock, true, (bool)viewOnly);
  302. } catch (rdr::Exception& e) {
  303. vlog.error("Reverse connection: %s",e.str());
  304. return -1;
  305. }
  306. return 0;
  307. }
  308. void vncGetQueryConnect(uint32_t *opaqueId, const char**username,
  309. const char **address, int *timeout)
  310. {
  311. for (int scr = 0; scr < vncGetScreenCount(); scr++) {
  312. desktop[scr]->getQueryConnect(opaqueId, username, address, timeout);
  313. if (opaqueId != 0)
  314. break;
  315. }
  316. }
  317. void vncApproveConnection(uint32_t opaqueId, int approve)
  318. {
  319. for (int scr = 0; scr < vncGetScreenCount(); scr++) {
  320. desktop[scr]->approveConnection(opaqueId, approve,
  321. "Connection rejected by local user");
  322. }
  323. }
  324. void vncBell()
  325. {
  326. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  327. desktop[scr]->bell();
  328. }
  329. void vncSetLEDState(unsigned long leds)
  330. {
  331. unsigned int state;
  332. state = 0;
  333. if (leds & (1 << 0))
  334. state |= ledCapsLock;
  335. if (leds & (1 << 1))
  336. state |= ledNumLock;
  337. if (leds & (1 << 2))
  338. state |= ledScrollLock;
  339. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  340. desktop[scr]->setLEDState(state);
  341. }
  342. void vncAddChanged(int scrIdx, int nRects,
  343. const struct UpdateRect *rects)
  344. {
  345. for (int i = 0;i < nRects;i++) {
  346. desktop[scrIdx]->add_changed(Region(Rect(rects[i].x1, rects[i].y1,
  347. rects[i].x2, rects[i].y2)));
  348. }
  349. }
  350. void vncAddCopied(int scrIdx, int nRects,
  351. const struct UpdateRect *rects,
  352. int dx, int dy)
  353. {
  354. for (int i = 0;i < nRects;i++) {
  355. desktop[scrIdx]->add_copied(Region(Rect(rects[i].x1, rects[i].y1,
  356. rects[i].x2, rects[i].y2)),
  357. Point(dx, dy));
  358. }
  359. }
  360. void vncSetCursorSprite(int width, int height, int hotX, int hotY,
  361. const unsigned char *rgbaData)
  362. {
  363. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  364. desktop[scr]->setCursor(width, height, hotX, hotY, rgbaData);
  365. }
  366. void vncSetCursorPos(int scrIdx, int x, int y)
  367. {
  368. desktop[scrIdx]->setCursorPos(x, y, true);
  369. }
  370. void vncPreScreenResize(int scrIdx)
  371. {
  372. // We need to prevent the RFB core from accessing the framebuffer
  373. // for a while as there might be updates thrown our way inside
  374. // the routines that change the screen (i.e. before we have a
  375. // pointer to the new framebuffer).
  376. desktop[scrIdx]->blockUpdates();
  377. }
  378. void vncPostScreenResize(int scrIdx, int success, int width, int height)
  379. {
  380. if (success) {
  381. // Let the RFB core know of the new dimensions and framebuffer
  382. try {
  383. desktop[scrIdx]->setFramebuffer(width, height,
  384. vncFbptr[scrIdx],
  385. vncFbstride[scrIdx]);
  386. } catch (rdr::Exception& e) {
  387. vncFatalError("vncPostScreenResize: %s\n", e.str());
  388. }
  389. }
  390. desktop[scrIdx]->unblockUpdates();
  391. if (success) {
  392. // Mark entire screen as changed
  393. desktop[scrIdx]->add_changed(Region(Rect(0, 0, width, height)));
  394. }
  395. }
  396. void vncRefreshScreenLayout(int scrIdx)
  397. {
  398. try {
  399. desktop[scrIdx]->refreshScreenLayout();
  400. } catch (rdr::Exception& e) {
  401. vncFatalError("vncRefreshScreenLayout: %s\n", e.str());
  402. }
  403. }
  404. int vncOverrideParam(const char *nameAndValue)
  405. {
  406. const char* equalSign = strchr(nameAndValue, '=');
  407. if (!equalSign)
  408. return 0;
  409. std::string key(nameAndValue, equalSign);
  410. if (allowOverrideSet.find(key) == allowOverrideSet.end())
  411. return 0;
  412. return rfb::Configuration::setParam(nameAndValue);
  413. }