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"
44 #define AGENTDIRPREFIX "/tmp/dropbear-"
46 static int send_msg_channel_open_agent(int fd
);
47 static int bindagent(int fd
, struct ChanSess
* chansess
);
48 static void agentaccept(struct Listener
* listener
, int sock
);
50 /* Handles client requests to start agent forwarding, sets up listening socket.
51 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
52 int svr_agentreq(struct ChanSess
* chansess
) {
56 TRACE(("enter svr_agentreq"))
58 if (!svr_pubkey_allows_agentfwd()) {
59 return DROPBEAR_FAILURE
;
62 if (chansess
->agentlistener
!= NULL
) {
63 return DROPBEAR_FAILURE
;
66 /* create listening socket */
67 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
72 /* create the unix socket dir and file */
73 if (bindagent(fd
, chansess
) == DROPBEAR_FAILURE
) {
78 if (listen(fd
, 20) < 0) {
82 /* set non-blocking */
85 /* pass if off to listener */
86 chansess
->agentlistener
= new_listener( &fd
, 1, 0, chansess
,
89 if (chansess
->agentlistener
== NULL
) {
93 return DROPBEAR_SUCCESS
;
99 svr_agentcleanup(chansess
);
101 return DROPBEAR_FAILURE
;
104 /* accepts a connection on the forwarded socket and opens a new channel for it
105 * back to the client */
106 /* returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
107 static void agentaccept(struct Listener
*UNUSED(listener
), int sock
) {
111 fd
= accept(sock
, NULL
, NULL
);
113 TRACE(("accept failed"))
117 if (send_msg_channel_open_agent(fd
) != DROPBEAR_SUCCESS
) {
123 /* set up the environment variable pointing to the socket. This is called
124 * just before command/shell execution, after dropping priveleges */
125 void svr_agentset(struct ChanSess
* chansess
) {
130 if (chansess
->agentlistener
== NULL
) {
134 /* 2 for "/" and "\0" */
135 len
= strlen(chansess
->agentdir
) + strlen(chansess
->agentfile
) + 2;
137 path
= m_malloc(len
);
138 snprintf(path
, len
, "%s/%s", chansess
->agentdir
, chansess
->agentfile
);
139 addnewvar("SSH_AUTH_SOCK", path
);
143 /* close the socket, remove the socket-file */
144 void svr_agentcleanup(struct ChanSess
* chansess
) {
151 if (chansess
->agentlistener
!= NULL
) {
152 remove_listener(chansess
->agentlistener
);
153 chansess
->agentlistener
= NULL
;
156 if (chansess
->agentfile
!= NULL
&& chansess
->agentdir
!= NULL
) {
158 /* Remove the dir as the user. That way they can't cause problems except
162 if ((setegid(ses
.authstate
.pw_gid
)) < 0 ||
163 (seteuid(ses
.authstate
.pw_uid
)) < 0) {
164 dropbear_exit("failed to set euid");
167 /* 2 for "/" and "\0" */
168 len
= strlen(chansess
->agentdir
) + strlen(chansess
->agentfile
) + 2;
170 path
= m_malloc(len
);
171 snprintf(path
, len
, "%s/%s", chansess
->agentdir
, chansess
->agentfile
);
175 rmdir(chansess
->agentdir
);
177 if ((seteuid(uid
)) < 0 ||
178 (setegid(gid
)) < 0) {
179 dropbear_exit("failed to revert euid");
182 m_free(chansess
->agentfile
);
183 m_free(chansess
->agentdir
);
188 static const struct ChanType chan_svr_agent
= {
190 "auth-agent@openssh.com",
198 /* helper for accepting an agent request */
199 static int send_msg_channel_open_agent(int fd
) {
201 if (send_msg_channel_open_init(fd
, &chan_svr_agent
) == DROPBEAR_SUCCESS
) {
203 return DROPBEAR_SUCCESS
;
205 return DROPBEAR_FAILURE
;
209 /* helper for creating the agent socket-file
210 returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
211 static int bindagent(int fd
, struct ChanSess
* chansess
) {
213 struct sockaddr_un addr
;
215 char path
[sizeof(addr
.sun_path
)], sockfile
[sizeof(addr
.sun_path
)];
220 int ret
= DROPBEAR_FAILURE
;
222 /* drop to user privs to make the dir/file */
225 if ((setegid(ses
.authstate
.pw_gid
)) < 0 ||
226 (seteuid(ses
.authstate
.pw_uid
)) < 0) {
227 dropbear_exit("failed to set euid");
230 memset((void*)&addr
, 0x0, sizeof(addr
));
231 addr
.sun_family
= AF_UNIX
;
235 for (i
= 0; i
< 20; i
++) {
236 genrandom((unsigned char*)&prefix
, sizeof(prefix
));
237 /* we want 32 bits (8 hex digits) - "/tmp/dropbear-f19c62c0" */
238 snprintf(path
, sizeof(path
), AGENTDIRPREFIX
"%.8x", prefix
);
240 if (mkdir(path
, mode
) == 0) {
243 if (errno
!= EEXIST
) {
247 /* couldn't make a dir */
251 /* Format is "/tmp/dropbear-0246dead/auth-d00f7654-23".
252 * The "23" is the file desc, the random data is to avoid collisions
253 * between subsequent user processes reusing socket fds (odds are now
255 genrandom((unsigned char*)&prefix
, sizeof(prefix
));
256 snprintf(sockfile
, sizeof(sockfile
), "auth-%.8x-%d", prefix
, fd
);
258 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s/%s", path
, sockfile
);
260 if (bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) == 0) {
261 chansess
->agentdir
= m_strdup(path
);
262 chansess
->agentfile
= m_strdup(sockfile
);
263 ret
= DROPBEAR_SUCCESS
;
268 if ((seteuid(uid
)) < 0 ||
269 (setegid(gid
)) < 0) {
270 dropbear_exit("failed to revert euid");