remove unused variable
[dropbear.git] / svr-agentfwd.c
blob940c4b7a255ba564e26dc1bbffbde0221f91f033
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:
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
23 * SOFTWARE. */
25 /* This file (agentfwd.c) handles authentication agent forwarding, for OpenSSH
26 * style agents. */
28 #include "includes.h"
30 #ifndef DISABLE_AGENTFWD
32 #include "agentfwd.h"
33 #include "session.h"
34 #include "ssh.h"
35 #include "dbutil.h"
36 #include "chansession.h"
37 #include "channel.h"
38 #include "packet.h"
39 #include "buffer.h"
40 #include "random.h"
41 #include "listener.h"
42 #include "auth.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) {
54 int fd;
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);
68 if (fd < 0) {
69 goto fail;
72 /* create the unix socket dir and file */
73 if (bindagent(fd, chansess) == DROPBEAR_FAILURE) {
74 goto fail;
77 /* listen */
78 if (listen(fd, 20) < 0) {
79 goto fail;
82 /* set non-blocking */
83 setnonblocking(fd);
85 /* pass if off to listener */
86 chansess->agentlistener = new_listener( &fd, 1, 0, chansess,
87 agentaccept, NULL);
89 if (chansess->agentlistener == NULL) {
90 goto fail;
93 return DROPBEAR_SUCCESS;
94 TRACE(("success"))
96 fail:
97 TRACE(("fail"))
98 /* cleanup */
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) {
109 int fd;
111 fd = accept(sock, NULL, NULL);
112 if (fd < 0) {
113 TRACE(("accept failed"))
114 return;
117 if (send_msg_channel_open_agent(fd) != DROPBEAR_SUCCESS) {
118 close(fd);
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) {
127 char *path = NULL;
128 int len;
130 if (chansess->agentlistener == NULL) {
131 return;
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);
140 m_free(path);
143 /* close the socket, remove the socket-file */
144 void svr_agentcleanup(struct ChanSess * chansess) {
146 char *path = NULL;
147 uid_t uid;
148 gid_t gid;
149 int len;
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
159 * for themselves */
160 uid = getuid();
161 gid = getgid();
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);
172 unlink(path);
173 m_free(path);
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 = {
189 0, /* sepfds */
190 "auth-agent@openssh.com",
191 NULL,
192 NULL,
193 NULL,
194 NULL
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) {
202 encrypt_packet();
203 return DROPBEAR_SUCCESS;
204 } else {
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;
214 unsigned int prefix;
215 char path[sizeof(addr.sun_path)], sockfile[sizeof(addr.sun_path)];
216 mode_t mode;
217 int i;
218 uid_t uid;
219 gid_t gid;
220 int ret = DROPBEAR_FAILURE;
222 /* drop to user privs to make the dir/file */
223 uid = getuid();
224 gid = getgid();
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;
233 mode = S_IRWXU;
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) {
241 goto bindsocket;
243 if (errno != EEXIST) {
244 break;
247 /* couldn't make a dir */
248 goto out;
250 bindsocket:
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
254 * 1/(2^64) */
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;
267 out:
268 if ((seteuid(uid)) < 0 ||
269 (setegid(gid)) < 0) {
270 dropbear_exit("failed to revert euid");
272 return ret;
275 #endif