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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. const char *addr = interface;
  188. int port = rfbport;
  189. if (port == 0) port = 5900 + atoi(vncGetDisplay());
  190. port += 1000 * scr;
  191. if (strcasecmp(addr, "all") == 0)
  192. addr = 0;
  193. if (localhostOnly)
  194. network::createLocalTcpListeners(&listeners, port);
  195. else
  196. network::createTcpListeners(&listeners, addr, port);
  197. vlog.info("Listening for VNC connections on %s interface(s), port %d",
  198. localhostOnly ? "local" : (const char*)interface,
  199. port);
  200. }
  201. PixelFormat pf = vncGetPixelFormat(scr);
  202. vncSetGlueContext(scr);
  203. desktop[scr] = new XserverDesktop(scr,
  204. listeners,
  205. desktopName,
  206. pf,
  207. vncGetScreenWidth(),
  208. vncGetScreenHeight(),
  209. vncFbptr[scr],
  210. vncFbstride[scr]);
  211. vlog.info("created VNC server for screen %d", scr);
  212. if (scr == 0 && vncInetdSock != -1 && listeners.empty()) {
  213. network::Socket* sock = new network::TcpSocket(vncInetdSock);
  214. desktop[scr]->addClient(sock, false);
  215. vlog.info("added inetd sock");
  216. }
  217. }
  218. vncHooksInit(scr);
  219. }
  220. } catch (rdr::Exception& e) {
  221. vncFatalError("vncExtInit: %s\n",e.str());
  222. }
  223. vncRegisterBlockHandlers();
  224. }
  225. void vncExtensionClose(void)
  226. {
  227. try {
  228. for (int scr = 0; scr < vncGetScreenCount(); scr++) {
  229. delete desktop[scr];
  230. desktop[scr] = NULL;
  231. }
  232. } catch (rdr::Exception& e) {
  233. vncFatalError("vncExtInit: %s\n",e.str());
  234. }
  235. }
  236. void vncHandleSocketEvent(int fd, int scrIdx, int read, int write)
  237. {
  238. desktop[scrIdx]->handleSocketEvent(fd, read, write);
  239. }
  240. void vncCallBlockHandlers(int* timeout)
  241. {
  242. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  243. desktop[scr]->blockHandler(timeout);
  244. }
  245. int vncGetAvoidShiftNumLock(void)
  246. {
  247. return (bool)avoidShiftNumLock;
  248. }
  249. int vncGetSetPrimary(void)
  250. {
  251. return (bool)setPrimary;
  252. }
  253. int vncGetSendPrimary(void)
  254. {
  255. return (bool)sendPrimary;
  256. }
  257. void vncUpdateDesktopName(void)
  258. {
  259. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  260. desktop[scr]->setDesktopName(desktopName);
  261. }
  262. void vncRequestClipboard(void)
  263. {
  264. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  265. desktop[scr]->requestClipboard();
  266. }
  267. void vncAnnounceClipboard(int available)
  268. {
  269. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  270. desktop[scr]->announceClipboard(available);
  271. }
  272. void vncSendClipboardData(const char* data)
  273. {
  274. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  275. desktop[scr]->sendClipboardData(data);
  276. }
  277. int vncConnectClient(const char *addr)
  278. {
  279. if (strlen(addr) == 0) {
  280. try {
  281. desktop[0]->disconnectClients();
  282. } catch (rdr::Exception& e) {
  283. vlog.error("Disconnecting all clients: %s",e.str());
  284. return -1;
  285. }
  286. return 0;
  287. }
  288. std::string host;
  289. int port;
  290. getHostAndPort(addr, &host, &port, 5500);
  291. try {
  292. network::Socket* sock = new network::TcpSocket(host.c_str(), port);
  293. desktop[0]->addClient(sock, true);
  294. } catch (rdr::Exception& e) {
  295. vlog.error("Reverse connection: %s",e.str());
  296. return -1;
  297. }
  298. return 0;
  299. }
  300. void vncGetQueryConnect(uint32_t *opaqueId, const char**username,
  301. const char **address, int *timeout)
  302. {
  303. for (int scr = 0; scr < vncGetScreenCount(); scr++) {
  304. desktop[scr]->getQueryConnect(opaqueId, username, address, timeout);
  305. if (opaqueId != 0)
  306. break;
  307. }
  308. }
  309. void vncApproveConnection(uint32_t opaqueId, int approve)
  310. {
  311. for (int scr = 0; scr < vncGetScreenCount(); scr++) {
  312. desktop[scr]->approveConnection(opaqueId, approve,
  313. "Connection rejected by local user");
  314. }
  315. }
  316. void vncBell()
  317. {
  318. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  319. desktop[scr]->bell();
  320. }
  321. void vncSetLEDState(unsigned long leds)
  322. {
  323. unsigned int state;
  324. state = 0;
  325. if (leds & (1 << 0))
  326. state |= ledCapsLock;
  327. if (leds & (1 << 1))
  328. state |= ledNumLock;
  329. if (leds & (1 << 2))
  330. state |= ledScrollLock;
  331. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  332. desktop[scr]->setLEDState(state);
  333. }
  334. void vncAddChanged(int scrIdx, int nRects,
  335. const struct UpdateRect *rects)
  336. {
  337. for (int i = 0;i < nRects;i++) {
  338. desktop[scrIdx]->add_changed(Region(Rect(rects[i].x1, rects[i].y1,
  339. rects[i].x2, rects[i].y2)));
  340. }
  341. }
  342. void vncAddCopied(int scrIdx, int nRects,
  343. const struct UpdateRect *rects,
  344. int dx, int dy)
  345. {
  346. for (int i = 0;i < nRects;i++) {
  347. desktop[scrIdx]->add_copied(Region(Rect(rects[i].x1, rects[i].y1,
  348. rects[i].x2, rects[i].y2)),
  349. Point(dx, dy));
  350. }
  351. }
  352. void vncSetCursorSprite(int width, int height, int hotX, int hotY,
  353. const unsigned char *rgbaData)
  354. {
  355. for (int scr = 0; scr < vncGetScreenCount(); scr++)
  356. desktop[scr]->setCursor(width, height, hotX, hotY, rgbaData);
  357. }
  358. void vncSetCursorPos(int scrIdx, int x, int y)
  359. {
  360. desktop[scrIdx]->setCursorPos(x, y, true);
  361. }
  362. void vncPreScreenResize(int scrIdx)
  363. {
  364. // We need to prevent the RFB core from accessing the framebuffer
  365. // for a while as there might be updates thrown our way inside
  366. // the routines that change the screen (i.e. before we have a
  367. // pointer to the new framebuffer).
  368. desktop[scrIdx]->blockUpdates();
  369. }
  370. void vncPostScreenResize(int scrIdx, int success, int width, int height)
  371. {
  372. if (success) {
  373. // Let the RFB core know of the new dimensions and framebuffer
  374. try {
  375. desktop[scrIdx]->setFramebuffer(width, height,
  376. vncFbptr[scrIdx],
  377. vncFbstride[scrIdx]);
  378. } catch (rdr::Exception& e) {
  379. vncFatalError("vncPostScreenResize: %s\n", e.str());
  380. }
  381. }
  382. desktop[scrIdx]->unblockUpdates();
  383. if (success) {
  384. // Mark entire screen as changed
  385. desktop[scrIdx]->add_changed(Region(Rect(0, 0, width, height)));
  386. }
  387. }
  388. void vncRefreshScreenLayout(int scrIdx)
  389. {
  390. try {
  391. desktop[scrIdx]->refreshScreenLayout();
  392. } catch (rdr::Exception& e) {
  393. vncFatalError("vncRefreshScreenLayout: %s\n", e.str());
  394. }
  395. }
  396. int vncOverrideParam(const char *nameAndValue)
  397. {
  398. const char* equalSign = strchr(nameAndValue, '=');
  399. if (!equalSign)
  400. return 0;
  401. std::string key(nameAndValue, equalSign);
  402. if (allowOverrideSet.find(key) == allowOverrideSet.end())
  403. return 0;
  404. return rfb::Configuration::setParam(nameAndValue);
  405. }