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

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