2 * Dropbear - a SSH2 server
4 * Copyright (c) 2002,2003 Matt Johnston
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
25 /* This file (agentfwd.c) handles authentication agent forwarding, for OpenSSH
30 #ifndef DISABLE_AGENTFWD
36 #include "chansession.h"
42 #define AGENTDIRPREFIX "/tmp/dropbear-"
44 static int send_msg_channel_open_agent(int fd);
45 static int bindagent(struct ChanSess * chansess);
47 /* Handles client requests to start agent forwarding, sets up listening socket.
48 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
49 int agentreq(struct ChanSess * chansess) {
51 if (chansess->agentfd != -1) {
52 return DROPBEAR_FAILURE;
55 /* create listening socket */
56 chansess->agentfd = socket(PF_UNIX, SOCK_STREAM, 0);
57 if (chansess->agentfd < 0) {
61 /* create the unix socket dir and file */
62 if (bindagent(chansess) == DROPBEAR_FAILURE) {
63 return DROPBEAR_FAILURE;
67 if (listen(chansess->agentfd, 20) < 0) {
71 /* set non-blocking */
72 if (fcntl(chansess->agentfd, F_SETFL, O_NONBLOCK) < 0) {
76 /* channel.c's channel fd code will handle the socket now */
78 /* set the maxfd so that select() loop will notice it */
79 ses.maxfd = MAX(ses.maxfd, chansess->agentfd);
81 return DROPBEAR_SUCCESS;
85 agentcleanup(chansess);
87 return DROPBEAR_FAILURE;
90 /* accepts a connection on the forwarded socket and opens a new channel for it
91 * back to the client */
92 /* returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
93 int agentaccept(struct ChanSess * chansess) {
97 fd = accept(chansess->agentfd, NULL, NULL);
99 return DROPBEAR_FAILURE;
102 return send_msg_channel_open_agent(fd);
106 /* set up the environment variable pointing to the socket. This is called
107 * just before command/shell execution, after dropping priveleges */
108 void agentset(struct ChanSess * chansess) {
110 char path[MAXPATHLEN];
112 if (chansess->agentfd == -1) {
116 snprintf(path, sizeof(path), "%s/%s", chansess->agentdir,
117 chansess->agentfile);
118 addnewvar("SSH_AUTH_SOCK", path);
121 /* close the socket, remove the socket-file */
122 void agentcleanup(struct ChanSess * chansess) {
124 char path[MAXPATHLEN];
128 if (chansess->agentfd == -1) {
132 close(chansess->agentfd);
134 /* Remove the dir as the user. That way they can't cause problems except
138 if ((setegid(ses.authstate.pw->pw_gid)) < 0 ||
139 (seteuid(ses.authstate.pw->pw_uid)) < 0) {
140 dropbear_exit("failed to set euid");
143 snprintf(path, sizeof(path),
144 "%s/%s", chansess->agentdir, chansess->agentfile);
147 rmdir(chansess->agentdir);
149 if ((seteuid(uid)) < 0 ||
150 (setegid(gid)) < 0) {
151 dropbear_exit("failed to revert euid");
154 m_free(chansess->agentfile);
155 m_free(chansess->agentdir);
159 /* helper for accepting an agent request */
160 static int send_msg_channel_open_agent(int fd) {
162 if (send_msg_channel_open_init(fd, "auth-agent@openssh.com")
163 == DROPBEAR_SUCCESS) {
165 return DROPBEAR_SUCCESS;
167 return DROPBEAR_FAILURE;
171 /* helper for creating the agent socket-file
172 returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
173 static int bindagent(struct ChanSess * chansess) {
175 struct sockaddr_un addr;
177 char path[sizeof(addr.sun_path)], sockfile[sizeof(addr.sun_path)];
182 int ret = DROPBEAR_FAILURE;
184 /* drop to user privs to make the dir/file */
187 if ((setegid(ses.authstate.pw->pw_gid)) < 0 ||
188 (seteuid(ses.authstate.pw->pw_uid)) < 0) {
189 dropbear_exit("failed to set euid");
192 addr.sun_family = AF_UNIX;
196 for (i = 0; i < 200; i++) {
197 genrandom((unsigned char*)&prefix, sizeof(prefix));
198 /* we want 32 bits (8 hex digits) - "/tmp/dropbear-f19c62c0" */
199 snprintf(path, sizeof(path), AGENTDIRPREFIX "%.8x", prefix);
201 if (mkdir(path, mode) == 0) {
204 if (errno != EEXIST) {
208 /* couldn't make a dir */
212 /* Format is "/tmp/dropbear-0246dead/auth-d00f7654-23".
213 * The "23" is the file desc, the random data is to avoid collisions
214 * between subsequent user processes reusing socket fds (odds are now
216 genrandom((unsigned char*)&prefix, sizeof(prefix));
217 snprintf(sockfile, sizeof(sockfile), "auth-%.8x-%d", prefix,
219 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s", path, sockfile);
221 if (bind(chansess->agentfd, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
222 chansess->agentdir = strdup(path);
223 chansess->agentfile = strdup(sockfile);
224 ret = DROPBEAR_SUCCESS;
229 if ((seteuid(uid)) < 0 ||
230 (setegid(gid)) < 0) {
231 dropbear_exit("failed to revert euid");