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

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