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.

vncExt.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. #ifdef HAVE_DIX_CONFIG_H
  20. #include <dix-config.h>
  21. #endif
  22. #define NEED_EVENTS
  23. #include "misc.h"
  24. #include "os.h"
  25. #include "dixstruct.h"
  26. #include "extnsionst.h"
  27. #include "scrnintstr.h"
  28. #define _VNCEXT_SERVER_
  29. #define _VNCEXT_PROTO_
  30. #include "vncExt.h"
  31. #include "xorg-version.h"
  32. #include "vncExtInit.h"
  33. #include "RFBGlue.h"
  34. static int ProcVncExtDispatch(ClientPtr client);
  35. static int SProcVncExtDispatch(ClientPtr client);
  36. static void vncResetProc(ExtensionEntry* extEntry);
  37. static void vncClientStateChange(CallbackListPtr*, void *, void *);
  38. static int vncErrorBase = 0;
  39. static int vncEventBase = 0;
  40. int vncNoClipboard = 0;
  41. static struct VncInputSelect* vncInputSelectHead = NULL;
  42. struct VncInputSelect {
  43. ClientPtr client;
  44. Window window;
  45. int mask;
  46. struct VncInputSelect* next;
  47. };
  48. void vncAddExtension(void)
  49. {
  50. ExtensionEntry* extEntry;
  51. extEntry = AddExtension(VNCEXTNAME, VncExtNumberEvents, VncExtNumberErrors,
  52. ProcVncExtDispatch, SProcVncExtDispatch, vncResetProc,
  53. StandardMinorOpcode);
  54. if (!extEntry) {
  55. FatalError("vncAddExtension: AddExtension failed\n");
  56. }
  57. vncErrorBase = extEntry->errorBase;
  58. vncEventBase = extEntry->eventBase;
  59. if (!AddCallback(&ClientStateCallback, vncClientStateChange, 0)) {
  60. FatalError("Add ClientStateCallback failed\n");
  61. }
  62. }
  63. int vncNotifyQueryConnect(void)
  64. {
  65. int count;
  66. xVncExtQueryConnectNotifyEvent ev;
  67. ev.type = vncEventBase + VncExtQueryConnectNotify;
  68. count = 0;
  69. for (struct VncInputSelect* cur = vncInputSelectHead; cur; cur = cur->next) {
  70. if (cur->mask & VncExtQueryConnectMask) {
  71. ev.sequenceNumber = cur->client->sequence;
  72. ev.window = cur->window;
  73. if (cur->client->swapped) {
  74. swaps(&ev.sequenceNumber);
  75. swapl(&ev.window);
  76. }
  77. WriteToClient(cur->client, sizeof(xVncExtQueryConnectNotifyEvent),
  78. (char *)&ev);
  79. count++;
  80. }
  81. }
  82. return count;
  83. }
  84. static int ProcVncExtSetParam(ClientPtr client)
  85. {
  86. char *param;
  87. xVncExtSetParamReply rep;
  88. REQUEST(xVncExtSetParamReq);
  89. REQUEST_FIXED_SIZE(xVncExtSetParamReq, stuff->paramLen);
  90. param = malloc(stuff->paramLen+1);
  91. if (param == NULL)
  92. return BadAlloc;
  93. strncpy(param, (char*)&stuff[1], stuff->paramLen);
  94. param[stuff->paramLen] = '\0';
  95. rep.type = X_Reply;
  96. rep.length = 0;
  97. rep.success = 0;
  98. rep.sequenceNumber = client->sequence;
  99. /*
  100. * Prevent change of clipboard related parameters if clipboard is disabled.
  101. */
  102. if (vncNoClipboard &&
  103. (strncasecmp(param, "SendCutText", 11) == 0 ||
  104. strncasecmp(param, "AcceptCutText", 13) == 0))
  105. goto deny;
  106. if (!vncOverrideParam(param))
  107. goto deny;
  108. rep.success = 1;
  109. // Send DesktopName update if desktop name has been changed
  110. if (strncasecmp(param, "desktop", 7) == 0)
  111. vncUpdateDesktopName();
  112. deny:
  113. free(param);
  114. if (client->swapped) {
  115. swaps(&rep.sequenceNumber);
  116. swapl(&rep.length);
  117. }
  118. WriteToClient(client, sizeof(xVncExtSetParamReply), (char *)&rep);
  119. return (client->noClientException);
  120. }
  121. static int SProcVncExtSetParam(ClientPtr client)
  122. {
  123. REQUEST(xVncExtSetParamReq);
  124. swaps(&stuff->length);
  125. REQUEST_AT_LEAST_SIZE(xVncExtSetParamReq);
  126. return ProcVncExtSetParam(client);
  127. }
  128. static int ProcVncExtGetParam(ClientPtr client)
  129. {
  130. char* param;
  131. char* value;
  132. size_t len;
  133. xVncExtGetParamReply rep;
  134. REQUEST(xVncExtGetParamReq);
  135. REQUEST_FIXED_SIZE(xVncExtGetParamReq, stuff->paramLen);
  136. param = malloc(stuff->paramLen+1);
  137. if (param == NULL)
  138. return BadAlloc;
  139. strncpy(param, (char*)&stuff[1], stuff->paramLen);
  140. param[stuff->paramLen] = 0;
  141. value = vncGetParam(param);
  142. len = value ? strlen(value) : 0;
  143. free(param);
  144. rep.type = X_Reply;
  145. rep.sequenceNumber = client->sequence;
  146. rep.success = 0;
  147. if (value)
  148. rep.success = 1;
  149. rep.length = (len + 3) >> 2;
  150. rep.valueLen = len;
  151. if (client->swapped) {
  152. swaps(&rep.sequenceNumber);
  153. swapl(&rep.length);
  154. swaps(&rep.valueLen);
  155. }
  156. WriteToClient(client, sizeof(xVncExtGetParamReply), (char *)&rep);
  157. if (value)
  158. WriteToClient(client, len, value);
  159. free(value);
  160. return (client->noClientException);
  161. }
  162. static int SProcVncExtGetParam(ClientPtr client)
  163. {
  164. REQUEST(xVncExtGetParamReq);
  165. swaps(&stuff->length);
  166. REQUEST_AT_LEAST_SIZE(xVncExtGetParamReq);
  167. return ProcVncExtGetParam(client);
  168. }
  169. static int ProcVncExtGetParamDesc(ClientPtr client)
  170. {
  171. char* param;
  172. const char* desc;
  173. size_t len;
  174. xVncExtGetParamDescReply rep;
  175. REQUEST(xVncExtGetParamDescReq);
  176. REQUEST_FIXED_SIZE(xVncExtGetParamDescReq, stuff->paramLen);
  177. param = malloc(stuff->paramLen+1);
  178. if (param == NULL)
  179. return BadAlloc;
  180. strncpy(param, (char*)&stuff[1], stuff->paramLen);
  181. param[stuff->paramLen] = 0;
  182. desc = vncGetParamDesc(param);
  183. len = desc ? strlen(desc) : 0;
  184. free(param);
  185. rep.type = X_Reply;
  186. rep.sequenceNumber = client->sequence;
  187. rep.success = 0;
  188. if (desc)
  189. rep.success = 1;
  190. rep.length = (len + 3) >> 2;
  191. rep.descLen = len;
  192. if (client->swapped) {
  193. swaps(&rep.sequenceNumber);
  194. swapl(&rep.length);
  195. swaps(&rep.descLen);
  196. }
  197. WriteToClient(client, sizeof(xVncExtGetParamDescReply), (char *)&rep);
  198. if (desc)
  199. WriteToClient(client, len, desc);
  200. return (client->noClientException);
  201. }
  202. static int SProcVncExtGetParamDesc(ClientPtr client)
  203. {
  204. REQUEST(xVncExtGetParamDescReq);
  205. swaps(&stuff->length);
  206. REQUEST_AT_LEAST_SIZE(xVncExtGetParamDescReq);
  207. return ProcVncExtGetParamDesc(client);
  208. }
  209. static int ProcVncExtListParams(ClientPtr client)
  210. {
  211. xVncExtListParamsReply rep;
  212. char *params;
  213. size_t len;
  214. REQUEST_SIZE_MATCH(xVncExtListParamsReq);
  215. rep.type = X_Reply;
  216. rep.sequenceNumber = client->sequence;
  217. params = vncGetParamList();
  218. if (params == NULL)
  219. return BadAlloc;
  220. len = strlen(params);
  221. rep.length = (len + 3) >> 2;
  222. rep.nParams = vncGetParamCount();
  223. if (client->swapped) {
  224. swaps(&rep.sequenceNumber);
  225. swapl(&rep.length);
  226. swaps(&rep.nParams);
  227. }
  228. WriteToClient(client, sizeof(xVncExtListParamsReply), (char *)&rep);
  229. WriteToClient(client, len, (char*)params);
  230. free(params);
  231. return (client->noClientException);
  232. }
  233. static int SProcVncExtListParams(ClientPtr client)
  234. {
  235. REQUEST(xVncExtListParamsReq);
  236. swaps(&stuff->length);
  237. REQUEST_SIZE_MATCH(xVncExtListParamsReq);
  238. return ProcVncExtListParams(client);
  239. }
  240. static int ProcVncExtSelectInput(ClientPtr client)
  241. {
  242. struct VncInputSelect** nextPtr;
  243. struct VncInputSelect* cur;
  244. REQUEST(xVncExtSelectInputReq);
  245. REQUEST_SIZE_MATCH(xVncExtSelectInputReq);
  246. nextPtr = &vncInputSelectHead;
  247. for (cur = vncInputSelectHead; cur; cur = *nextPtr) {
  248. if (cur->client == client && cur->window == stuff->window) {
  249. cur->mask = stuff->mask;
  250. if (!cur->mask) {
  251. *nextPtr = cur->next;
  252. free(cur);
  253. }
  254. break;
  255. }
  256. nextPtr = &cur->next;
  257. }
  258. if (!cur) {
  259. cur = malloc(sizeof(struct VncInputSelect));
  260. if (cur == NULL)
  261. return BadAlloc;
  262. memset(cur, 0, sizeof(struct VncInputSelect));
  263. cur->client = client;
  264. cur->window = stuff->window;
  265. cur->mask = stuff->mask;
  266. cur->next = vncInputSelectHead;
  267. vncInputSelectHead = cur;
  268. }
  269. return (client->noClientException);
  270. }
  271. static int SProcVncExtSelectInput(ClientPtr client)
  272. {
  273. REQUEST(xVncExtSelectInputReq);
  274. swaps(&stuff->length);
  275. REQUEST_SIZE_MATCH(xVncExtSelectInputReq);
  276. swapl(&stuff->window);
  277. swapl(&stuff->mask);
  278. return ProcVncExtSelectInput(client);
  279. }
  280. static int ProcVncExtConnect(ClientPtr client)
  281. {
  282. char *address;
  283. xVncExtConnectReply rep;
  284. REQUEST(xVncExtConnectReq);
  285. REQUEST_FIXED_SIZE(xVncExtConnectReq, stuff->strLen);
  286. address = malloc(stuff->strLen+1);
  287. if (address == NULL)
  288. return BadAlloc;
  289. strncpy(address, (char*)&stuff[1], stuff->strLen);
  290. address[stuff->strLen] = 0;
  291. rep.success = 0;
  292. if (vncConnectClient(address) == 0)
  293. rep.success = 1;
  294. rep.type = X_Reply;
  295. rep.length = 0;
  296. rep.sequenceNumber = client->sequence;
  297. if (client->swapped) {
  298. swaps(&rep.sequenceNumber);
  299. swapl(&rep.length);
  300. }
  301. WriteToClient(client, sizeof(xVncExtConnectReply), (char *)&rep);
  302. free(address);
  303. return (client->noClientException);
  304. }
  305. static int SProcVncExtConnect(ClientPtr client)
  306. {
  307. REQUEST(xVncExtConnectReq);
  308. swaps(&stuff->length);
  309. REQUEST_AT_LEAST_SIZE(xVncExtConnectReq);
  310. return ProcVncExtConnect(client);
  311. }
  312. static int ProcVncExtGetQueryConnect(ClientPtr client)
  313. {
  314. uint32_t opaqueId;
  315. const char *qcAddress, *qcUsername;
  316. int qcTimeout;
  317. xVncExtGetQueryConnectReply rep;
  318. REQUEST_SIZE_MATCH(xVncExtGetQueryConnectReq);
  319. vncGetQueryConnect(&opaqueId, &qcAddress, &qcUsername, &qcTimeout);
  320. rep.type = X_Reply;
  321. rep.sequenceNumber = client->sequence;
  322. rep.timeout = qcTimeout;
  323. rep.addrLen = qcTimeout ? strlen(qcAddress) : 0;
  324. rep.userLen = qcTimeout ? strlen(qcUsername) : 0;
  325. rep.opaqueId = (CARD32)(long)opaqueId;
  326. rep.length = ((rep.userLen + 3) >> 2) + ((rep.addrLen + 3) >> 2);
  327. if (client->swapped) {
  328. swaps(&rep.sequenceNumber);
  329. swapl(&rep.addrLen);
  330. swapl(&rep.userLen);
  331. swapl(&rep.timeout);
  332. swapl(&rep.opaqueId);
  333. swapl(&rep.length);
  334. }
  335. WriteToClient(client, sizeof(xVncExtGetQueryConnectReply), (char *)&rep);
  336. if (qcTimeout)
  337. WriteToClient(client, strlen(qcAddress), qcAddress);
  338. if (qcTimeout)
  339. WriteToClient(client, strlen(qcUsername), qcUsername);
  340. return (client->noClientException);
  341. }
  342. static int SProcVncExtGetQueryConnect(ClientPtr client)
  343. {
  344. REQUEST(xVncExtGetQueryConnectReq);
  345. swaps(&stuff->length);
  346. REQUEST_SIZE_MATCH(xVncExtGetQueryConnectReq);
  347. return ProcVncExtGetQueryConnect(client);
  348. }
  349. static int ProcVncExtApproveConnect(ClientPtr client)
  350. {
  351. REQUEST(xVncExtApproveConnectReq);
  352. REQUEST_SIZE_MATCH(xVncExtApproveConnectReq);
  353. vncApproveConnection(stuff->opaqueId, stuff->approve);
  354. // Inform other clients of the event and tidy up
  355. vncNotifyQueryConnect();
  356. return (client->noClientException);
  357. }
  358. static int SProcVncExtApproveConnect(ClientPtr client)
  359. {
  360. REQUEST(xVncExtApproveConnectReq);
  361. swaps(&stuff->length);
  362. swapl(&stuff->opaqueId);
  363. REQUEST_SIZE_MATCH(xVncExtApproveConnectReq);
  364. return ProcVncExtApproveConnect(client);
  365. }
  366. static int ProcVncExtDispatch(ClientPtr client)
  367. {
  368. REQUEST(xReq);
  369. switch (stuff->data) {
  370. case X_VncExtSetParam:
  371. return ProcVncExtSetParam(client);
  372. case X_VncExtGetParam:
  373. return ProcVncExtGetParam(client);
  374. case X_VncExtGetParamDesc:
  375. return ProcVncExtGetParamDesc(client);
  376. case X_VncExtListParams:
  377. return ProcVncExtListParams(client);
  378. case X_VncExtSelectInput:
  379. return ProcVncExtSelectInput(client);
  380. case X_VncExtConnect:
  381. return ProcVncExtConnect(client);
  382. case X_VncExtGetQueryConnect:
  383. return ProcVncExtGetQueryConnect(client);
  384. case X_VncExtApproveConnect:
  385. return ProcVncExtApproveConnect(client);
  386. default:
  387. return BadRequest;
  388. }
  389. }
  390. static int SProcVncExtDispatch(ClientPtr client)
  391. {
  392. REQUEST(xReq);
  393. switch (stuff->data) {
  394. case X_VncExtSetParam:
  395. return SProcVncExtSetParam(client);
  396. case X_VncExtGetParam:
  397. return SProcVncExtGetParam(client);
  398. case X_VncExtGetParamDesc:
  399. return SProcVncExtGetParamDesc(client);
  400. case X_VncExtListParams:
  401. return SProcVncExtListParams(client);
  402. case X_VncExtSelectInput:
  403. return SProcVncExtSelectInput(client);
  404. case X_VncExtConnect:
  405. return SProcVncExtConnect(client);
  406. case X_VncExtGetQueryConnect:
  407. return SProcVncExtGetQueryConnect(client);
  408. case X_VncExtApproveConnect:
  409. return SProcVncExtApproveConnect(client);
  410. default:
  411. return BadRequest;
  412. }
  413. }
  414. static void vncResetProc(ExtensionEntry* extEntry)
  415. {
  416. vncExtensionClose();
  417. }
  418. static void vncClientStateChange(CallbackListPtr * l, void * d, void * p)
  419. {
  420. ClientPtr client = ((NewClientInfoRec*)p)->client;
  421. if (client->clientState == ClientStateGone) {
  422. struct VncInputSelect** nextPtr = &vncInputSelectHead;
  423. for (struct VncInputSelect* cur = vncInputSelectHead; cur; cur = *nextPtr) {
  424. if (cur->client == client) {
  425. *nextPtr = cur->next;
  426. free(cur);
  427. continue;
  428. }
  429. nextPtr = &cur->next;
  430. }
  431. }
  432. }