您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. #if XORG < 112
  75. int n;
  76. swaps(&ev.sequenceNumber, n);
  77. swapl(&ev.window, n);
  78. #else
  79. swaps(&ev.sequenceNumber);
  80. swapl(&ev.window);
  81. #endif
  82. }
  83. WriteToClient(cur->client, sizeof(xVncExtQueryConnectNotifyEvent),
  84. (char *)&ev);
  85. count++;
  86. }
  87. }
  88. return count;
  89. }
  90. static int ProcVncExtSetParam(ClientPtr client)
  91. {
  92. char *param;
  93. xVncExtSetParamReply rep;
  94. REQUEST(xVncExtSetParamReq);
  95. REQUEST_FIXED_SIZE(xVncExtSetParamReq, stuff->paramLen);
  96. param = malloc(stuff->paramLen+1);
  97. if (param == NULL)
  98. return BadAlloc;
  99. strncpy(param, (char*)&stuff[1], stuff->paramLen);
  100. param[stuff->paramLen] = '\0';
  101. rep.type = X_Reply;
  102. rep.length = 0;
  103. rep.success = 0;
  104. rep.sequenceNumber = client->sequence;
  105. /*
  106. * Prevent change of clipboard related parameters if clipboard is disabled.
  107. */
  108. if (vncNoClipboard &&
  109. (strncasecmp(param, "SendCutText", 11) == 0 ||
  110. strncasecmp(param, "AcceptCutText", 13) == 0))
  111. goto deny;
  112. if (!vncOverrideParam(param))
  113. goto deny;
  114. rep.success = 1;
  115. // Send DesktopName update if desktop name has been changed
  116. if (strncasecmp(param, "desktop", 7) == 0)
  117. vncUpdateDesktopName();
  118. deny:
  119. free(param);
  120. if (client->swapped) {
  121. #if XORG < 112
  122. int n;
  123. swaps(&rep.sequenceNumber, n);
  124. swapl(&rep.length, n);
  125. #else
  126. swaps(&rep.sequenceNumber);
  127. swapl(&rep.length);
  128. #endif
  129. }
  130. WriteToClient(client, sizeof(xVncExtSetParamReply), (char *)&rep);
  131. return (client->noClientException);
  132. }
  133. static int SProcVncExtSetParam(ClientPtr client)
  134. {
  135. REQUEST(xVncExtSetParamReq);
  136. #if XORG < 112
  137. register char n;
  138. swaps(&stuff->length, n);
  139. #else
  140. swaps(&stuff->length);
  141. #endif
  142. REQUEST_AT_LEAST_SIZE(xVncExtSetParamReq);
  143. return ProcVncExtSetParam(client);
  144. }
  145. static int ProcVncExtGetParam(ClientPtr client)
  146. {
  147. char* param;
  148. char* value;
  149. size_t len;
  150. xVncExtGetParamReply rep;
  151. REQUEST(xVncExtGetParamReq);
  152. REQUEST_FIXED_SIZE(xVncExtGetParamReq, stuff->paramLen);
  153. param = malloc(stuff->paramLen+1);
  154. if (param == NULL)
  155. return BadAlloc;
  156. strncpy(param, (char*)&stuff[1], stuff->paramLen);
  157. param[stuff->paramLen] = 0;
  158. value = vncGetParam(param);
  159. len = value ? strlen(value) : 0;
  160. free(param);
  161. rep.type = X_Reply;
  162. rep.sequenceNumber = client->sequence;
  163. rep.success = 0;
  164. if (value)
  165. rep.success = 1;
  166. rep.length = (len + 3) >> 2;
  167. rep.valueLen = len;
  168. if (client->swapped) {
  169. #if XORG < 112
  170. int n;
  171. swaps(&rep.sequenceNumber, n);
  172. swapl(&rep.length, n);
  173. swaps(&rep.valueLen, n);
  174. #else
  175. swaps(&rep.sequenceNumber);
  176. swapl(&rep.length);
  177. swaps(&rep.valueLen);
  178. #endif
  179. }
  180. WriteToClient(client, sizeof(xVncExtGetParamReply), (char *)&rep);
  181. if (value)
  182. WriteToClient(client, len, value);
  183. free(value);
  184. return (client->noClientException);
  185. }
  186. static int SProcVncExtGetParam(ClientPtr client)
  187. {
  188. REQUEST(xVncExtGetParamReq);
  189. #if XORG < 112
  190. register char n;
  191. swaps(&stuff->length, n);
  192. #else
  193. swaps(&stuff->length);
  194. #endif
  195. REQUEST_AT_LEAST_SIZE(xVncExtGetParamReq);
  196. return ProcVncExtGetParam(client);
  197. }
  198. static int ProcVncExtGetParamDesc(ClientPtr client)
  199. {
  200. char* param;
  201. const char* desc;
  202. size_t len;
  203. xVncExtGetParamDescReply rep;
  204. REQUEST(xVncExtGetParamDescReq);
  205. REQUEST_FIXED_SIZE(xVncExtGetParamDescReq, stuff->paramLen);
  206. param = malloc(stuff->paramLen+1);
  207. if (param == NULL)
  208. return BadAlloc;
  209. strncpy(param, (char*)&stuff[1], stuff->paramLen);
  210. param[stuff->paramLen] = 0;
  211. desc = vncGetParamDesc(param);
  212. len = desc ? strlen(desc) : 0;
  213. free(param);
  214. rep.type = X_Reply;
  215. rep.sequenceNumber = client->sequence;
  216. rep.success = 0;
  217. if (desc)
  218. rep.success = 1;
  219. rep.length = (len + 3) >> 2;
  220. rep.descLen = len;
  221. if (client->swapped) {
  222. #if XORG < 112
  223. int n;
  224. swaps(&rep.sequenceNumber, n);
  225. swapl(&rep.length, n);
  226. swaps(&rep.descLen, n);
  227. #else
  228. swaps(&rep.sequenceNumber);
  229. swapl(&rep.length);
  230. swaps(&rep.descLen);
  231. #endif
  232. }
  233. WriteToClient(client, sizeof(xVncExtGetParamDescReply), (char *)&rep);
  234. if (desc)
  235. WriteToClient(client, len, desc);
  236. return (client->noClientException);
  237. }
  238. static int SProcVncExtGetParamDesc(ClientPtr client)
  239. {
  240. REQUEST(xVncExtGetParamDescReq);
  241. #if XORG < 112
  242. register char n;
  243. swaps(&stuff->length, n);
  244. #else
  245. swaps(&stuff->length);
  246. #endif
  247. REQUEST_AT_LEAST_SIZE(xVncExtGetParamDescReq);
  248. return ProcVncExtGetParamDesc(client);
  249. }
  250. static int ProcVncExtListParams(ClientPtr client)
  251. {
  252. xVncExtListParamsReply rep;
  253. char *params;
  254. size_t len;
  255. REQUEST_SIZE_MATCH(xVncExtListParamsReq);
  256. rep.type = X_Reply;
  257. rep.sequenceNumber = client->sequence;
  258. params = vncGetParamList();
  259. if (params == NULL)
  260. return BadAlloc;
  261. len = strlen(params);
  262. rep.length = (len + 3) >> 2;
  263. rep.nParams = vncGetParamCount();
  264. if (client->swapped) {
  265. #if XORG < 112
  266. int n;
  267. swaps(&rep.sequenceNumber, n);
  268. swapl(&rep.length, n);
  269. swaps(&rep.nParams, n);
  270. #else
  271. swaps(&rep.sequenceNumber);
  272. swapl(&rep.length);
  273. swaps(&rep.nParams);
  274. #endif
  275. }
  276. WriteToClient(client, sizeof(xVncExtListParamsReply), (char *)&rep);
  277. WriteToClient(client, len, (char*)params);
  278. free(params);
  279. return (client->noClientException);
  280. }
  281. static int SProcVncExtListParams(ClientPtr client)
  282. {
  283. REQUEST(xVncExtListParamsReq);
  284. #if XORG < 112
  285. register char n;
  286. swaps(&stuff->length, n);
  287. #else
  288. swaps(&stuff->length);
  289. #endif
  290. REQUEST_SIZE_MATCH(xVncExtListParamsReq);
  291. return ProcVncExtListParams(client);
  292. }
  293. static int ProcVncExtSelectInput(ClientPtr client)
  294. {
  295. struct VncInputSelect** nextPtr;
  296. struct VncInputSelect* cur;
  297. REQUEST(xVncExtSelectInputReq);
  298. REQUEST_SIZE_MATCH(xVncExtSelectInputReq);
  299. nextPtr = &vncInputSelectHead;
  300. for (cur = vncInputSelectHead; cur; cur = *nextPtr) {
  301. if (cur->client == client && cur->window == stuff->window) {
  302. cur->mask = stuff->mask;
  303. if (!cur->mask) {
  304. *nextPtr = cur->next;
  305. free(cur);
  306. }
  307. break;
  308. }
  309. nextPtr = &cur->next;
  310. }
  311. if (!cur) {
  312. cur = malloc(sizeof(struct VncInputSelect));
  313. if (cur == NULL)
  314. return BadAlloc;
  315. memset(cur, 0, sizeof(struct VncInputSelect));
  316. cur->client = client;
  317. cur->window = stuff->window;
  318. cur->mask = stuff->mask;
  319. cur->next = vncInputSelectHead;
  320. vncInputSelectHead = cur;
  321. }
  322. return (client->noClientException);
  323. }
  324. static int SProcVncExtSelectInput(ClientPtr client)
  325. {
  326. REQUEST(xVncExtSelectInputReq);
  327. #if XORG < 112
  328. register char n;
  329. swaps(&stuff->length, n);
  330. #else
  331. swaps(&stuff->length);
  332. #endif
  333. REQUEST_SIZE_MATCH(xVncExtSelectInputReq);
  334. #if XORG < 112
  335. swapl(&stuff->window, n);
  336. swapl(&stuff->mask, n);
  337. #else
  338. swapl(&stuff->window);
  339. swapl(&stuff->mask);
  340. #endif
  341. return ProcVncExtSelectInput(client);
  342. }
  343. static int ProcVncExtConnect(ClientPtr client)
  344. {
  345. char *address;
  346. xVncExtConnectReply rep;
  347. REQUEST(xVncExtConnectReq);
  348. REQUEST_FIXED_SIZE(xVncExtConnectReq, stuff->strLen);
  349. address = malloc(stuff->strLen+1);
  350. if (address == NULL)
  351. return BadAlloc;
  352. strncpy(address, (char*)&stuff[1], stuff->strLen);
  353. address[stuff->strLen] = 0;
  354. rep.success = 0;
  355. if (vncConnectClient(address) == 0)
  356. rep.success = 1;
  357. rep.type = X_Reply;
  358. rep.length = 0;
  359. rep.sequenceNumber = client->sequence;
  360. if (client->swapped) {
  361. #if XORG < 112
  362. int n;
  363. swaps(&rep.sequenceNumber, n);
  364. swapl(&rep.length, n);
  365. #else
  366. swaps(&rep.sequenceNumber);
  367. swapl(&rep.length);
  368. #endif
  369. }
  370. WriteToClient(client, sizeof(xVncExtConnectReply), (char *)&rep);
  371. free(address);
  372. return (client->noClientException);
  373. }
  374. static int SProcVncExtConnect(ClientPtr client)
  375. {
  376. REQUEST(xVncExtConnectReq);
  377. #if XORG < 112
  378. register char n;
  379. swaps(&stuff->length, n);
  380. #else
  381. swaps(&stuff->length);
  382. #endif
  383. REQUEST_AT_LEAST_SIZE(xVncExtConnectReq);
  384. return ProcVncExtConnect(client);
  385. }
  386. static int ProcVncExtGetQueryConnect(ClientPtr client)
  387. {
  388. uint32_t opaqueId;
  389. const char *qcAddress, *qcUsername;
  390. int qcTimeout;
  391. xVncExtGetQueryConnectReply rep;
  392. REQUEST_SIZE_MATCH(xVncExtGetQueryConnectReq);
  393. vncGetQueryConnect(&opaqueId, &qcAddress, &qcUsername, &qcTimeout);
  394. rep.type = X_Reply;
  395. rep.sequenceNumber = client->sequence;
  396. rep.timeout = qcTimeout;
  397. rep.addrLen = qcTimeout ? strlen(qcAddress) : 0;
  398. rep.userLen = qcTimeout ? strlen(qcUsername) : 0;
  399. rep.opaqueId = (CARD32)(long)opaqueId;
  400. rep.length = ((rep.userLen + 3) >> 2) + ((rep.addrLen + 3) >> 2);
  401. if (client->swapped) {
  402. #if XORG < 112
  403. int n;
  404. swaps(&rep.sequenceNumber, n);
  405. swapl(&rep.addrLen, n);
  406. swapl(&rep.userLen, n);
  407. swapl(&rep.timeout, n);
  408. swapl(&rep.opaqueId, n);
  409. swapl(&rep.length, n);
  410. #else
  411. swaps(&rep.sequenceNumber);
  412. swapl(&rep.addrLen);
  413. swapl(&rep.userLen);
  414. swapl(&rep.timeout);
  415. swapl(&rep.opaqueId);
  416. swapl(&rep.length);
  417. #endif
  418. }
  419. WriteToClient(client, sizeof(xVncExtGetQueryConnectReply), (char *)&rep);
  420. if (qcTimeout)
  421. WriteToClient(client, strlen(qcAddress), qcAddress);
  422. if (qcTimeout)
  423. WriteToClient(client, strlen(qcUsername), qcUsername);
  424. return (client->noClientException);
  425. }
  426. static int SProcVncExtGetQueryConnect(ClientPtr client)
  427. {
  428. REQUEST(xVncExtGetQueryConnectReq);
  429. #if XORG < 112
  430. register char n;
  431. swaps(&stuff->length, n);
  432. #else
  433. swaps(&stuff->length);
  434. #endif
  435. REQUEST_SIZE_MATCH(xVncExtGetQueryConnectReq);
  436. return ProcVncExtGetQueryConnect(client);
  437. }
  438. static int ProcVncExtApproveConnect(ClientPtr client)
  439. {
  440. REQUEST(xVncExtApproveConnectReq);
  441. REQUEST_SIZE_MATCH(xVncExtApproveConnectReq);
  442. vncApproveConnection(stuff->opaqueId, stuff->approve);
  443. // Inform other clients of the event and tidy up
  444. vncNotifyQueryConnect();
  445. return (client->noClientException);
  446. }
  447. static int SProcVncExtApproveConnect(ClientPtr client)
  448. {
  449. REQUEST(xVncExtApproveConnectReq);
  450. #if XORG < 112
  451. register char n;
  452. swaps(&stuff->length, n);
  453. swapl(&stuff->opaqueId, n);
  454. #else
  455. swaps(&stuff->length);
  456. swapl(&stuff->opaqueId);
  457. #endif
  458. REQUEST_SIZE_MATCH(xVncExtApproveConnectReq);
  459. return ProcVncExtApproveConnect(client);
  460. }
  461. static int ProcVncExtDispatch(ClientPtr client)
  462. {
  463. REQUEST(xReq);
  464. switch (stuff->data) {
  465. case X_VncExtSetParam:
  466. return ProcVncExtSetParam(client);
  467. case X_VncExtGetParam:
  468. return ProcVncExtGetParam(client);
  469. case X_VncExtGetParamDesc:
  470. return ProcVncExtGetParamDesc(client);
  471. case X_VncExtListParams:
  472. return ProcVncExtListParams(client);
  473. case X_VncExtSelectInput:
  474. return ProcVncExtSelectInput(client);
  475. case X_VncExtConnect:
  476. return ProcVncExtConnect(client);
  477. case X_VncExtGetQueryConnect:
  478. return ProcVncExtGetQueryConnect(client);
  479. case X_VncExtApproveConnect:
  480. return ProcVncExtApproveConnect(client);
  481. default:
  482. return BadRequest;
  483. }
  484. }
  485. static int SProcVncExtDispatch(ClientPtr client)
  486. {
  487. REQUEST(xReq);
  488. switch (stuff->data) {
  489. case X_VncExtSetParam:
  490. return SProcVncExtSetParam(client);
  491. case X_VncExtGetParam:
  492. return SProcVncExtGetParam(client);
  493. case X_VncExtGetParamDesc:
  494. return SProcVncExtGetParamDesc(client);
  495. case X_VncExtListParams:
  496. return SProcVncExtListParams(client);
  497. case X_VncExtSelectInput:
  498. return SProcVncExtSelectInput(client);
  499. case X_VncExtConnect:
  500. return SProcVncExtConnect(client);
  501. case X_VncExtGetQueryConnect:
  502. return SProcVncExtGetQueryConnect(client);
  503. case X_VncExtApproveConnect:
  504. return SProcVncExtApproveConnect(client);
  505. default:
  506. return BadRequest;
  507. }
  508. }
  509. static void vncResetProc(ExtensionEntry* extEntry)
  510. {
  511. vncExtensionClose();
  512. }
  513. static void vncClientStateChange(CallbackListPtr * l, void * d, void * p)
  514. {
  515. ClientPtr client = ((NewClientInfoRec*)p)->client;
  516. if (client->clientState == ClientStateGone) {
  517. struct VncInputSelect** nextPtr = &vncInputSelectHead;
  518. for (struct VncInputSelect* cur = vncInputSelectHead; cur; cur = *nextPtr) {
  519. if (cur->client == client) {
  520. *nextPtr = cur->next;
  521. free(cur);
  522. continue;
  523. }
  524. nextPtr = &cur->next;
  525. }
  526. }
  527. }