svr-x11fwd.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Dropbear - a SSH2 server
  3. *
  4. * Copyright (c) 2002,2003 Matt Johnston
  5. * All rights reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE. */
  24. #include "includes.h"
  25. #if DROPBEAR_X11FWD
  26. #include "x11fwd.h"
  27. #include "session.h"
  28. #include "ssh.h"
  29. #include "dbutil.h"
  30. #include "chansession.h"
  31. #include "channel.h"
  32. #include "packet.h"
  33. #include "buffer.h"
  34. #include "auth.h"
  35. #define X11BASEPORT 6000
  36. #define X11BINDBASE 6010
  37. static void x11accept(const struct Listener* listener, int sock);
  38. static int bindport(int fd);
  39. static int send_msg_channel_open_x11(int fd, const struct sockaddr_in* addr);
  40. /* Check untrusted xauth strings for metacharacters */
  41. /* Returns DROPBEAR_SUCCESS/DROPBEAR_FAILURE */
  42. static int
  43. xauth_valid_string(const char *s)
  44. {
  45. size_t i;
  46. for (i = 0; s[i] != '\0'; i++) {
  47. if (!isalnum(s[i]) &&
  48. s[i] != '.' && s[i] != ':' && s[i] != '/' &&
  49. s[i] != '-' && s[i] != '_') {
  50. return DROPBEAR_FAILURE;
  51. }
  52. }
  53. return DROPBEAR_SUCCESS;
  54. }
  55. /* called as a request for a session channel, sets up listening X11 */
  56. /* returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
  57. int x11req(struct ChanSess * chansess) {
  58. int fd = -1;
  59. if (!svr_pubkey_allows_x11fwd()) {
  60. return DROPBEAR_FAILURE;
  61. }
  62. /* we already have an x11 connection */
  63. if (chansess->x11listener != NULL) {
  64. return DROPBEAR_FAILURE;
  65. }
  66. chansess->x11singleconn = buf_getbool(ses.payload);
  67. chansess->x11authprot = buf_getstring(ses.payload, NULL);
  68. chansess->x11authcookie = buf_getstring(ses.payload, NULL);
  69. chansess->x11screennum = buf_getint(ses.payload);
  70. if (xauth_valid_string(chansess->x11authprot) == DROPBEAR_FAILURE ||
  71. xauth_valid_string(chansess->x11authcookie) == DROPBEAR_FAILURE) {
  72. dropbear_log(LOG_WARNING, "Bad xauth request");
  73. goto fail;
  74. }
  75. /* create listening socket */
  76. fd = socket(PF_INET, SOCK_STREAM, 0);
  77. if (fd < 0) {
  78. goto fail;
  79. }
  80. /* allocate port and bind */
  81. chansess->x11port = bindport(fd);
  82. if (chansess->x11port < 0) {
  83. goto fail;
  84. }
  85. /* listen */
  86. if (listen(fd, 20) < 0) {
  87. goto fail;
  88. }
  89. /* set non-blocking */
  90. setnonblocking(fd);
  91. /* listener code will handle the socket now.
  92. * No cleanup handler needed, since listener_remove only happens
  93. * from our cleanup anyway */
  94. chansess->x11listener = new_listener( &fd, 1, 0, chansess, x11accept, NULL);
  95. if (chansess->x11listener == NULL) {
  96. goto fail;
  97. }
  98. return DROPBEAR_SUCCESS;
  99. fail:
  100. /* cleanup */
  101. m_free(chansess->x11authprot);
  102. m_free(chansess->x11authcookie);
  103. m_close(fd);
  104. return DROPBEAR_FAILURE;
  105. }
  106. /* accepts a new X11 socket */
  107. /* returns DROPBEAR_FAILURE or DROPBEAR_SUCCESS */
  108. static void x11accept(const struct Listener* listener, int sock) {
  109. int fd;
  110. struct sockaddr_in addr;
  111. socklen_t len;
  112. int ret;
  113. struct ChanSess * chansess = (struct ChanSess *)(listener->typedata);
  114. len = sizeof(addr);
  115. fd = accept(sock, (struct sockaddr*)&addr, &len);
  116. if (fd < 0) {
  117. return;
  118. }
  119. /* if single-connection we close it up */
  120. if (chansess->x11singleconn) {
  121. x11cleanup(chansess);
  122. }
  123. ret = send_msg_channel_open_x11(fd, &addr);
  124. if (ret == DROPBEAR_FAILURE) {
  125. close(fd);
  126. }
  127. }
  128. /* This is called after switching to the user, and sets up the xauth
  129. * and environment variables. */
  130. void x11setauth(const struct ChanSess *chansess) {
  131. char display[20]; /* space for "localhost:12345.123" */
  132. FILE * authprog = NULL;
  133. int val;
  134. if (chansess->x11listener == NULL) {
  135. return;
  136. }
  137. /* create the DISPLAY string */
  138. val = snprintf(display, sizeof(display), "localhost:%d.%u",
  139. chansess->x11port - X11BASEPORT, chansess->x11screennum);
  140. if (val < 0 || val >= (int)sizeof(display)) {
  141. /* string was truncated */
  142. return;
  143. }
  144. addnewvar("DISPLAY", display);
  145. /* create the xauth string */
  146. val = snprintf(display, sizeof(display), "unix:%d.%u",
  147. chansess->x11port - X11BASEPORT, chansess->x11screennum);
  148. if (val < 0 || val >= (int)sizeof(display)) {
  149. /* string was truncated */
  150. return;
  151. }
  152. /* code is strongly based on OpenSSH's */
  153. authprog = popen(XAUTH_COMMAND, "w");
  154. if (authprog) {
  155. fprintf(authprog, "add %s %s %s\n",
  156. display, chansess->x11authprot, chansess->x11authcookie);
  157. pclose(authprog);
  158. } else {
  159. fprintf(stderr, "Failed to run %s\n", XAUTH_COMMAND);
  160. }
  161. }
  162. void x11cleanup(struct ChanSess *chansess) {
  163. m_free(chansess->x11authprot);
  164. m_free(chansess->x11authcookie);
  165. TRACE(("chansess %p", (void*)chansess))
  166. if (chansess->x11listener != NULL) {
  167. remove_listener(chansess->x11listener);
  168. chansess->x11listener = NULL;
  169. }
  170. }
  171. static int x11_inithandler(struct Channel *channel) {
  172. channel->prio = DROPBEAR_CHANNEL_PRIO_INTERACTIVE;
  173. return 0;
  174. }
  175. static const struct ChanType chan_x11 = {
  176. "x11",
  177. x11_inithandler, /* inithandler */
  178. NULL, /* checkclose */
  179. NULL, /* reqhandler */
  180. NULL, /* closehandler */
  181. NULL /* cleanup */
  182. };
  183. static int send_msg_channel_open_x11(int fd, const struct sockaddr_in* addr) {
  184. char* ipstring = NULL;
  185. if (send_msg_channel_open_init(fd, &chan_x11) == DROPBEAR_SUCCESS) {
  186. ipstring = inet_ntoa(addr->sin_addr);
  187. buf_putstring(ses.writepayload, ipstring, strlen(ipstring));
  188. buf_putint(ses.writepayload, addr->sin_port);
  189. encrypt_packet();
  190. return DROPBEAR_SUCCESS;
  191. } else {
  192. return DROPBEAR_FAILURE;
  193. }
  194. }
  195. /* returns the port bound to, or -1 on failure.
  196. * Will attempt to bind to a port X11BINDBASE (6010 usually) or upwards */
  197. static int bindport(int fd) {
  198. struct sockaddr_in addr;
  199. uint16_t port;
  200. memset((void*)&addr, 0x0, sizeof(addr));
  201. addr.sin_family = AF_INET;
  202. addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  203. /* if we can't find one in 2000 ports free, something's wrong */
  204. for (port = X11BINDBASE; port < X11BINDBASE + 2000; port++) {
  205. addr.sin_port = htons(port);
  206. if (bind(fd, (struct sockaddr*)&addr,
  207. sizeof(struct sockaddr_in)) == 0) {
  208. /* success */
  209. return port;
  210. }
  211. if (errno == EADDRINUSE) {
  212. /* try the next port */
  213. continue;
  214. }
  215. /* otherwise it was an error we don't know about */
  216. dropbear_log(LOG_DEBUG, "Failed to bind x11 socket");
  217. break;
  218. }
  219. return -1;
  220. }
  221. #endif /* DROPBEAR_X11FWD */