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

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