svr-agentfwd.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. /* This file (agentfwd.c) handles authentication agent forwarding, for OpenSSH
  25. * style agents. */
  26. #include "includes.h"
  27. #if DROPBEAR_SVR_AGENTFWD
  28. #include "agentfwd.h"
  29. #include "session.h"
  30. #include "ssh.h"
  31. #include "dbutil.h"
  32. #include "chansession.h"
  33. #include "channel.h"
  34. #include "packet.h"
  35. #include "buffer.h"
  36. #include "dbrandom.h"
  37. #include "listener.h"
  38. #include "auth.h"
  39. #define AGENTDIRPREFIX "/tmp/dropbear-"
  40. static int send_msg_channel_open_agent(int fd);
  41. static int bindagent(int fd, struct ChanSess * chansess);
  42. static void agentaccept(const struct Listener * listener, int sock);
  43. /* Handles client requests to start agent forwarding, sets up listening socket.
  44. * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
  45. int svr_agentreq(struct ChanSess * chansess) {
  46. int fd = -1;
  47. if (!svr_pubkey_allows_agentfwd()) {
  48. return DROPBEAR_FAILURE;
  49. }
  50. if (chansess->agentlistener != NULL) {
  51. return DROPBEAR_FAILURE;
  52. }
  53. /* create listening socket */
  54. fd = socket(PF_UNIX, SOCK_STREAM, 0);
  55. if (fd < 0) {
  56. goto fail;
  57. }
  58. /* create the unix socket dir and file */
  59. if (bindagent(fd, chansess) == DROPBEAR_FAILURE) {
  60. goto fail;
  61. }
  62. /* listen */
  63. if (listen(fd, 20) < 0) {
  64. goto fail;
  65. }
  66. /* set non-blocking */
  67. setnonblocking(fd);
  68. /* pass if off to listener */
  69. chansess->agentlistener = new_listener( &fd, 1, 0, chansess,
  70. agentaccept, NULL);
  71. if (chansess->agentlistener == NULL) {
  72. goto fail;
  73. }
  74. return DROPBEAR_SUCCESS;
  75. fail:
  76. m_close(fd);
  77. /* cleanup */
  78. svr_agentcleanup(chansess);
  79. return DROPBEAR_FAILURE;
  80. }
  81. /* accepts a connection on the forwarded socket and opens a new channel for it
  82. * back to the client */
  83. /* returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
  84. static void agentaccept(const struct Listener *UNUSED(listener), int sock) {
  85. int fd;
  86. fd = accept(sock, NULL, NULL);
  87. if (fd < 0) {
  88. TRACE(("accept failed"))
  89. return;
  90. }
  91. if (send_msg_channel_open_agent(fd) != DROPBEAR_SUCCESS) {
  92. close(fd);
  93. }
  94. }
  95. /* set up the environment variable pointing to the socket. This is called
  96. * just before command/shell execution, after dropping privileges */
  97. void svr_agentset(const struct ChanSess * chansess) {
  98. char *path = NULL;
  99. int len;
  100. if (chansess->agentlistener == NULL) {
  101. return;
  102. }
  103. /* 2 for "/" and "\0" */
  104. len = strlen(chansess->agentdir) + strlen(chansess->agentfile) + 2;
  105. path = m_malloc(len);
  106. snprintf(path, len, "%s/%s", chansess->agentdir, chansess->agentfile);
  107. addnewvar("SSH_AUTH_SOCK", path);
  108. m_free(path);
  109. }
  110. /* close the socket, remove the socket-file */
  111. void svr_agentcleanup(struct ChanSess * chansess) {
  112. char *path = NULL;
  113. uid_t uid;
  114. gid_t gid;
  115. int len;
  116. if (chansess->agentlistener != NULL) {
  117. remove_listener(chansess->agentlistener);
  118. chansess->agentlistener = NULL;
  119. }
  120. if (chansess->agentfile != NULL && chansess->agentdir != NULL) {
  121. #if DROPBEAR_SVR_MULTIUSER
  122. /* Remove the dir as the user. That way they can't cause problems except
  123. * for themselves */
  124. uid = getuid();
  125. gid = getgid();
  126. if ((setegid(ses.authstate.pw_gid)) < 0 ||
  127. (seteuid(ses.authstate.pw_uid)) < 0) {
  128. dropbear_exit("Failed to set euid");
  129. }
  130. #endif
  131. /* 2 for "/" and "\0" */
  132. len = strlen(chansess->agentdir) + strlen(chansess->agentfile) + 2;
  133. path = m_malloc(len);
  134. snprintf(path, len, "%s/%s", chansess->agentdir, chansess->agentfile);
  135. unlink(path);
  136. m_free(path);
  137. rmdir(chansess->agentdir);
  138. #if DROPBEAR_SVR_MULTIUSER
  139. if ((seteuid(uid)) < 0 ||
  140. (setegid(gid)) < 0) {
  141. dropbear_exit("Failed to revert euid");
  142. }
  143. #endif
  144. m_free(chansess->agentfile);
  145. m_free(chansess->agentdir);
  146. }
  147. }
  148. static const struct ChanType chan_svr_agent = {
  149. "auth-agent@openssh.com",
  150. NULL,
  151. NULL,
  152. NULL,
  153. NULL,
  154. NULL
  155. };
  156. /* helper for accepting an agent request */
  157. static int send_msg_channel_open_agent(int fd) {
  158. if (send_msg_channel_open_init(fd, &chan_svr_agent) == DROPBEAR_SUCCESS) {
  159. encrypt_packet();
  160. return DROPBEAR_SUCCESS;
  161. } else {
  162. return DROPBEAR_FAILURE;
  163. }
  164. }
  165. /* helper for creating the agent socket-file
  166. returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
  167. static int bindagent(int fd, struct ChanSess * chansess) {
  168. struct sockaddr_un addr;
  169. unsigned int prefix;
  170. char path[(sizeof(addr.sun_path)-1)/2], sockfile[(sizeof(addr.sun_path)-1)/2];
  171. mode_t mode;
  172. int i;
  173. uid_t uid;
  174. gid_t gid;
  175. int ret = DROPBEAR_FAILURE;
  176. #if DROPBEAR_SVR_MULTIUSER
  177. /* drop to user privs to make the dir/file */
  178. uid = getuid();
  179. gid = getgid();
  180. if ((setegid(ses.authstate.pw_gid)) < 0 ||
  181. (seteuid(ses.authstate.pw_uid)) < 0) {
  182. dropbear_exit("Failed to set euid");
  183. }
  184. #endif
  185. memset((void*)&addr, 0x0, sizeof(addr));
  186. addr.sun_family = AF_UNIX;
  187. mode = S_IRWXU;
  188. for (i = 0; i < 20; i++) {
  189. genrandom((unsigned char*)&prefix, sizeof(prefix));
  190. /* we want 32 bits (8 hex digits) - "/tmp/dropbear-f19c62c0" */
  191. snprintf(path, sizeof(path), AGENTDIRPREFIX "%.8x", prefix);
  192. if (mkdir(path, mode) == 0) {
  193. goto bindsocket;
  194. }
  195. if (errno != EEXIST) {
  196. break;
  197. }
  198. }
  199. /* couldn't make a dir */
  200. goto out;
  201. bindsocket:
  202. /* Format is "/tmp/dropbear-0246dead/auth-d00f7654-23".
  203. * The "23" is the file desc, the random data is to avoid collisions
  204. * between subsequent user processes reusing socket fds (odds are now
  205. * 1/(2^64) */
  206. genrandom((unsigned char*)&prefix, sizeof(prefix));
  207. snprintf(sockfile, sizeof(sockfile), "auth-%.8x-%d", prefix, fd);
  208. snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s", path, sockfile);
  209. if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
  210. chansess->agentdir = m_strdup(path);
  211. chansess->agentfile = m_strdup(sockfile);
  212. ret = DROPBEAR_SUCCESS;
  213. }
  214. out:
  215. #if DROPBEAR_SVR_MULTIUSER
  216. if ((seteuid(uid)) < 0 ||
  217. (setegid(gid)) < 0) {
  218. dropbear_exit("Failed to revert euid");
  219. }
  220. #endif
  221. return ret;
  222. }
  223. #endif