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

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