Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

vncExtInit.cc 13KB

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