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 agentreq(struct ChanSess
* chansess
) {
56 if (!svr_pubkey_allows_agentfwd()) {
57 return DROPBEAR_FAILURE
;
60 if (chansess
->agentlistener
!= NULL
) {
61 return DROPBEAR_FAILURE
;
64 /* create listening socket */
65 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
70 /* create the unix socket dir and file */
71 if (bindagent(fd
, chansess
) == DROPBEAR_FAILURE
) {
76 if (listen(fd
, 20) < 0) {
80 /* set non-blocking */
83 /* pass if off to listener */
84 chansess
->agentlistener
= new_listener( &fd
, 1, 0, chansess
,
87 if (chansess
->agentlistener
== NULL
) {
91 return DROPBEAR_SUCCESS
;
95 agentcleanup(chansess
);
97 return DROPBEAR_FAILURE
;
100 /* accepts a connection on the forwarded socket and opens a new channel for it
101 * back to the client */
102 /* returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
103 static void agentaccept(struct Listener
*UNUSED(listener
), int sock
) {
107 fd
= accept(sock
, NULL
, NULL
);
109 TRACE(("accept failed"))
113 if (send_msg_channel_open_agent(fd
) != DROPBEAR_SUCCESS
) {
119 /* set up the environment variable pointing to the socket. This is called
120 * just before command/shell execution, after dropping priveleges */
121 void agentset(struct ChanSess
* chansess
) {
126 if (chansess
->agentlistener
== NULL
) {
130 /* 2 for "/" and "\0" */
131 len
= strlen(chansess
->agentdir
) + strlen(chansess
->agentfile
) + 2;
133 path
= m_malloc(len
);
134 snprintf(path
, len
, "%s/%s", chansess
->agentdir
, chansess
->agentfile
);
135 addnewvar("SSH_AUTH_SOCK", path
);
139 /* close the socket, remove the socket-file */
140 void agentcleanup(struct ChanSess
* chansess
) {
147 if (chansess
->agentlistener
!= NULL
) {
148 remove_listener(chansess
->agentlistener
);
149 chansess
->agentlistener
= NULL
;
152 if (chansess
->agentfile
!= NULL
&& chansess
->agentdir
!= NULL
) {
154 /* Remove the dir as the user. That way they can't cause problems except
158 if ((setegid(ses
.authstate
.pw_gid
)) < 0 ||
159 (seteuid(ses
.authstate
.pw_uid
)) < 0) {
160 dropbear_exit("failed to set euid");
163 /* 2 for "/" and "\0" */
164 len
= strlen(chansess
->agentdir
) + strlen(chansess
->agentfile
) + 2;
166 path
= m_malloc(len
);
167 snprintf(path
, len
, "%s/%s", chansess
->agentdir
, chansess
->agentfile
);
171 rmdir(chansess
->agentdir
);
173 if ((seteuid(uid
)) < 0 ||
174 (setegid(gid
)) < 0) {
175 dropbear_exit("failed to revert euid");
178 m_free(chansess
->agentfile
);
179 m_free(chansess
->agentdir
);
184 static const struct ChanType chan_agent
= {
186 "auth-agent@openssh.com",
194 /* helper for accepting an agent request */
195 static int send_msg_channel_open_agent(int fd
) {
197 if (send_msg_channel_open_init(fd
, &chan_agent
) == DROPBEAR_SUCCESS
) {
199 return DROPBEAR_SUCCESS
;
201 return DROPBEAR_FAILURE
;
205 /* helper for creating the agent socket-file
206 returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
207 static int bindagent(int fd
, struct ChanSess
* chansess
) {
209 struct sockaddr_un addr
;
211 char path
[sizeof(addr
.sun_path
)], sockfile
[sizeof(addr
.sun_path
)];
216 int ret
= DROPBEAR_FAILURE
;
218 /* drop to user privs to make the dir/file */
221 if ((setegid(ses
.authstate
.pw_gid
)) < 0 ||
222 (seteuid(ses
.authstate
.pw_uid
)) < 0) {
223 dropbear_exit("failed to set euid");
226 memset((void*)&addr
, 0x0, sizeof(addr
));
227 addr
.sun_family
= AF_UNIX
;
231 for (i
= 0; i
< 20; i
++) {
232 genrandom((unsigned char*)&prefix
, sizeof(prefix
));
233 /* we want 32 bits (8 hex digits) - "/tmp/dropbear-f19c62c0" */
234 snprintf(path
, sizeof(path
), AGENTDIRPREFIX
"%.8x", prefix
);
236 if (mkdir(path
, mode
) == 0) {
239 if (errno
!= EEXIST
) {
243 /* couldn't make a dir */
247 /* Format is "/tmp/dropbear-0246dead/auth-d00f7654-23".
248 * The "23" is the file desc, the random data is to avoid collisions
249 * between subsequent user processes reusing socket fds (odds are now
251 genrandom((unsigned char*)&prefix
, sizeof(prefix
));
252 snprintf(sockfile
, sizeof(sockfile
), "auth-%.8x-%d", prefix
, fd
);
254 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s/%s", path
, sockfile
);
256 if (bind(fd
, (struct sockaddr
*)&addr
, sizeof(addr
)) == 0) {
257 chansess
->agentdir
= m_strdup(path
);
258 chansess
->agentfile
= m_strdup(sockfile
);
259 ret
= DROPBEAR_SUCCESS
;
264 if ((seteuid(uid
)) < 0 ||
265 (setegid(gid
)) < 0) {
266 dropbear_exit("failed to revert euid");