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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. #include <stdio.h>
  20. #include <errno.h>
  21. #include <set>
  22. #include <string>
  23. #include <rfb/Configuration.h>
  24. #include <rfb/Logger_stdio.h>
  25. #include <rfb/LogWriter.h>
  26. #include <rfb/util.h>
  27. #include <rfb/ServerCore.h>
  28. #include <rdr/HexOutStream.h>
  29. #include <rfb/LogWriter.h>
  30. #include <rfb/Hostname.h>
  31. #include <rfb/Region.h>
  32. #include <rfb/ledStates.h>
  33. #include <network/TcpSocket.h>
  34. #include <network/UnixSocket.h>
  35. #include "XserverDesktop.h"
  36. #include "vncExtInit.h"
  37. #include "vncHooks.h"
  38. #include "vncBlockHandler.h"
  39. #include "vncSelection.h"
  40. #include "XorgGlue.h"
  41. #include "RandrGlue.h"
  42. #include "xorg-version.h"
  43. extern "C" {
  44. void vncSetGlueContext(int screenIndex);
  45. }
  46. using namespace rfb;
  47. static rfb::LogWriter vlog("vncext");
  48. // We can't safely get this from Xorg
  49. #define MAXSCREENS 16
  50. static unsigned long vncExtGeneration = 0;
  51. static bool initialised = false;
  52. static XserverDesktop* desktop[MAXSCREENS] = { 0, };
  53. void* vncFbptr[MAXSCREENS] = { 0, };
  54. int vncFbstride[MAXSCREENS];
  55. int vncInetdSock = -1;
  56. struct CaseInsensitiveCompare {
  57. bool operator() (const std::string &a, const std::string &b) const {
  58. return strcasecmp(a.c_str(), b.c_str()) < 0;
  59. }
  60. };
  61. typedef std::set<std::string, CaseInsensitiveCompare> ParamSet;
  62. static ParamSet allowOverrideSet;
  63. rfb::IntParameter rfbport("rfbport", "TCP port to listen for RFB protocol",0);
  64. rfb::StringParameter rfbunixpath("rfbunixpath", "Unix socket to listen for RFB protocol", "");
  65. rfb::IntParameter rfbunixmode("rfbunixmode", "Unix socket access mode", 0600);
  66. rfb::StringParameter desktopName("desktop", "Name of VNC desktop","x11");
  67. rfb::BoolParameter localhostOnly("localhost",
  68. "Only allow connections from localhost",
  69. false);
  70. rfb::StringParameter interface("interface",
  71. "listen on the specified network address",
  72. "all");
  73. rfb::BoolParameter avoidShiftNumLock("AvoidShiftNumLock",
  74. "Avoid fake Shift presses for keys affected by NumLock.",
  75. true);
  76. rfb::StringParameter allowOverride("AllowOverride",
  77. "Comma separated list of parameters that can be modified using VNC extension.",
  78. "desktop,AcceptPointerEvents,SendCutText,AcceptCutText,SendPrimary,SetPrimary");
  79. rfb::BoolParameter setPrimary("SetPrimary", "Set the PRIMARY as well "
  80. "as the CLIPBOARD selection", true);
  81. rfb::BoolParameter sendPrimary("SendPrimary",
  82. "Send the PRIMARY as well as the CLIPBOARD selection",
  83. true);
  84. static PixelFormat vncGetPixelFormat(int scrIdx)
  85. {
  86. int depth, bpp;
  87. int trueColour, bigEndian;
  88. int redMask, greenMask, blueMask;
  89. int redShift, greenShift, blueShift;
  90. int redMax, greenMax, blueMax;
  91. vncGetScreenFormat(scrIdx, &depth, &bpp, &trueColour, &bigEndian,
  92. &redMask, &greenMask, &blueMask);
  93. if (!trueColour) {
  94. vlog.error("pseudocolour not supported");
  95. abort();
  96. }
  97. redShift = ffs(redMask) - 1;
  98. greenShift = ffs(greenMask) - 1;
  99. blueShift = ffs(blueMask) - 1;
  100. redMax = redMask >> redShift;
  101. greenMax = greenMask >> greenShift;
  102. blueMax = blueMask >> blueShift;
  103. return PixelFormat(bpp, depth, bigEndian, trueColour,
  104. redMax, greenMax, blueMax,
  105. redShift, greenShift, blueShift);
  106. }
  107. static void parseOverrideList(const char *text, ParamSet &out)
  108. {
  109. for (const char* iter = text; ; ++iter) {
  110. if (*iter == ',' || *iter == '\0') {
  111. out.insert(std::string(text, iter));
  112. text = iter + 1;
  113. if (*iter == '\0')
  114. break;
  115. }
  116. }
  117. }
  118. void vncExtensionInit(void)
  119. {
  120. if (vncExtGeneration == vncGetServerGeneration()) {
  121. vlog.error("vncExtensionInit: called twice in same generation?");
  122. return;
  123. }
  124. vncExtGeneration = vncGetServerGeneration();
  125. if (vncGetScreenCount() > MAXSCREENS)
  126. vncFatalError("vncExtensionInit: too many screens");
  127. if (sizeof(ShortRect) != sizeof(struct UpdateRect))
  128. vncFatalError("vncExtensionInit: Incompatible ShortRect size");
  129. vncAddExtension();
  130. vncSelectionInit();
  131. vlog.info("VNC extension running!");
  132. try {
  133. if (!initialised) {
  134. rfb::initStdIOLoggers();
  135. parseOverrideList(allowOverride, allowOverrideSet);
  136. allowOverride.setImmutable();
  137. initialised = true;
  138. }
  139. for (int scr = 0; scr < vncGetScreenCount(); scr++) {
  140. if (!desktop[scr]) {
  141. std::list<network::SocketListener*> listeners;
  142. if (scr == 0 && vncInetdSock != -1) {
  143. if (network::isSocketListening(vncInetdSock))
  144. {
  145. listeners.push_back(new network::TcpListener(vncInetdSock));
  146. vlog.info("inetd wait");
  147. }
  148. } else if (((const char*)rfbunixpath)[0] != '\0') {
  149. char path[PATH_MAX];
  150. int mode = (int)rfbunixmode;
  151. if (scr == 0)
  152. strncpy(path, rfbunixpath, sizeof(path));
  153. else
  154. snprintf(path, sizeof(path), "%s.%d",
  155. (const char*)rfbunixpath, scr);
  156. path[sizeof(path)-1] = '\0';
  157. listeners.push_back(new network::UnixListener(path, mode));
  158. vlog.info("Listening for VNC connections on %s (mode %04o)",
  159. path, mode);
  160. } else {
  161. const char *addr = interface;
  162. int port = rfbport;
  163. if (port == 0) port = 5900 + atoi(vncGetDisplay());
  164. port += 1000 * scr;
  165. if (strcasecmp(addr, "all") == 0)
  166. addr = 0;
  167. if (localhostOnly)
  168. network::createLocalTcpListeners(&listeners, port);
  169. else
  170. network::createTcpListeners(&listeners, addr, port);
  171. vlog.info("Listening for VNC connections on %s interface(s), port %d",
  172. localhostOnly ? "local" : (const char*)interface,
  173. port);
  174. }
  175. CharArray desktopNameStr(desktopName.getData());
  176. PixelFormat pf = vncGetPixelFormat(scr);
  177. vncSetGlueContext(scr);
  178. desktop[scr] = new XserverDesktop(scr,
  179. listeners,
  180. desktopNameStr.buf,
  181. pf,
  182. vncGetScreenWidth(),
  183. vncGetScreenHeight(),
  184. vncFbptr[scr],
  185. vncFbstride[scr]);
  186. vlog.info("created VNC server for screen %d", scr);
  187. if (scr == 0 && vncInetdSock != -1 && listeners.empty()) {
  188. network::Socket* sock = new network::TcpSocket(vncInetdSock);
  189. desktop[scr]->addClient(sock, false);
  190. vlog.info("added inetd sock");
  191. }
  192. }
  193. vncHooksInit(scr);
  194. }
  195. } catch (rdr::Exception& e) {
  196. vncFatalError("vncExtInit: %s",e.str());
  197. }
  198. vncRegisterBlockHandlers();
  199. }
  200. void vncExtensionClose(void)
  201. {
  202. try {
  203. for (int scr = 0; scr < vncGetScreenCount(); scr++) {
  204. delete desktop[scr];
  205. desktop[scr] = NULL;
  206. }
  207. } catch (rdr::Exception& e) {
  208. vncFatalError("vncExtInit: %s",e.str());
  209. }
  210. }
  211. void vncHandleSocketEvent(int fd, int scrIdx, int read, int write)
  212. {
  213. desktop[scrIdx]->handleSocketEvent(fd, read, write);
  214. }
  215. void vncCallBlockHandlers(int* timeout)
  216. {
  217. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  218. desktop[scr]->blockHandler(timeout);
  219. }
  220. int vncGetAvoidShiftNumLock(void)
  221. {
  222. return (bool)avoidShiftNumLock;
  223. }
  224. int vncGetSetPrimary(void)
  225. {
  226. return (bool)setPrimary;
  227. }
  228. int vncGetSendPrimary(void)
  229. {
  230. return (bool)sendPrimary;
  231. }
  232. void vncUpdateDesktopName(void)
  233. {
  234. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  235. desktop[scr]->setDesktopName(desktopName);
  236. }
  237. void vncRequestClipboard(void)
  238. {
  239. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  240. desktop[scr]->requestClipboard();
  241. }
  242. void vncAnnounceClipboard(int available)
  243. {
  244. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  245. desktop[scr]->announceClipboard(available);
  246. }
  247. void vncSendClipboardData(const char* data)
  248. {
  249. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  250. desktop[scr]->sendClipboardData(data);
  251. }
  252. int vncConnectClient(const char *addr)
  253. {
  254. if (strlen(addr) == 0) {
  255. try {
  256. desktop[0]->disconnectClients();
  257. } catch (rdr::Exception& e) {
  258. vlog.error("Disconnecting all clients: %s",e.str());
  259. return -1;
  260. }
  261. return 0;
  262. }
  263. char *host;
  264. int port;
  265. getHostAndPort(addr, &host, &port, 5500);
  266. try {
  267. network::Socket* sock = new network::TcpSocket(host, port);
  268. delete [] host;
  269. desktop[0]->addClient(sock, true);
  270. } catch (rdr::Exception& e) {
  271. vlog.error("Reverse connection: %s",e.str());
  272. return -1;
  273. }
  274. return 0;
  275. }
  276. void vncGetQueryConnect(uint32_t *opaqueId, const char**username,
  277. const char **address, int *timeout)
  278. {
  279. for (int scr = 0; scr < vncGetScreenCount(); scr++) {
  280. desktop[scr]->getQueryConnect(opaqueId, username, address, timeout);
  281. if (opaqueId != 0)
  282. break;
  283. }
  284. }
  285. void vncApproveConnection(uint32_t opaqueId, int approve)
  286. {
  287. for (int scr = 0; scr < vncGetScreenCount(); scr++) {
  288. desktop[scr]->approveConnection(opaqueId, approve,
  289. "Connection rejected by local user");
  290. }
  291. }
  292. void vncBell()
  293. {
  294. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  295. desktop[scr]->bell();
  296. }
  297. void vncSetLEDState(unsigned long leds)
  298. {
  299. unsigned int state;
  300. state = 0;
  301. if (leds & (1 << 0))
  302. state |= ledCapsLock;
  303. if (leds & (1 << 1))
  304. state |= ledNumLock;
  305. if (leds & (1 << 2))
  306. state |= ledScrollLock;
  307. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  308. desktop[scr]->setLEDState(state);
  309. }
  310. void vncAddChanged(int scrIdx, const struct UpdateRect *extents,
  311. int nRects, const struct UpdateRect *rects)
  312. {
  313. Region reg;
  314. reg.setExtentsAndOrderedRects((const ShortRect*)extents,
  315. nRects, (const ShortRect*)rects);
  316. desktop[scrIdx]->add_changed(reg);
  317. }
  318. void vncAddCopied(int scrIdx, const struct UpdateRect *extents,
  319. int nRects, const struct UpdateRect *rects,
  320. int dx, int dy)
  321. {
  322. Region reg;
  323. reg.setExtentsAndOrderedRects((const ShortRect*)extents,
  324. nRects, (const ShortRect*)rects);
  325. desktop[scrIdx]->add_copied(reg, rfb::Point(dx, dy));
  326. }
  327. void vncSetCursor(int width, int height, int hotX, int hotY,
  328. const unsigned char *rgbaData)
  329. {
  330. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  331. desktop[scr]->setCursor(width, height, hotX, hotY, rgbaData);
  332. }
  333. void vncPreScreenResize(int scrIdx)
  334. {
  335. // We need to prevent the RFB core from accessing the framebuffer
  336. // for a while as there might be updates thrown our way inside
  337. // the routines that change the screen (i.e. before we have a
  338. // pointer to the new framebuffer).
  339. desktop[scrIdx]->blockUpdates();
  340. }
  341. void vncPostScreenResize(int scrIdx, int success, int width, int height)
  342. {
  343. if (success) {
  344. // Let the RFB core know of the new dimensions and framebuffer
  345. desktop[scrIdx]->setFramebuffer(width, height,
  346. vncFbptr[scrIdx], vncFbstride[scrIdx]);
  347. }
  348. desktop[scrIdx]->unblockUpdates();
  349. if (success) {
  350. // Mark entire screen as changed
  351. desktop[scrIdx]->add_changed(Region(Rect(0, 0, width, height)));
  352. }
  353. }
  354. void vncRefreshScreenLayout(int scrIdx)
  355. {
  356. try {
  357. desktop[scrIdx]->refreshScreenLayout();
  358. } catch (rdr::Exception& e) {
  359. vncFatalError("%s", e.str());
  360. }
  361. }
  362. int vncOverrideParam(const char *nameAndValue)
  363. {
  364. const char* equalSign = strchr(nameAndValue, '=');
  365. if (!equalSign)
  366. return 0;
  367. std::string key(nameAndValue, equalSign);
  368. if (allowOverrideSet.find(key) == allowOverrideSet.end())
  369. return 0;
  370. return rfb::Configuration::setParam(nameAndValue);
  371. }