# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / sshd / agentfwd.c
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
25 /* This file (agentfwd.c) handles authentication agent forwarding, for OpenSSH
26  * style agents. */
27
28 #include "includes.h"
29
30 #ifndef DISABLE_AGENTFWD
31
32 #include "agentfwd.h"
33 #include "session.h"
34 #include "ssh.h"
35 #include "util.h"
36 #include "chansession.h"
37 #include "channel.h"
38 #include "packet.h"
39 #include "buffer.h"
40 #include "random.h"
41
42 #define AGENTDIRPREFIX "/tmp/dropbear-"
43
44 static int send_msg_channel_open_agent(int fd);
45 static int bindagent(struct ChanSess * chansess);
46
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) {
50
51         if (chansess->agentfd != -1) {
52                 return DROPBEAR_FAILURE;
53         }
54
55         /* create listening socket */
56         chansess->agentfd = socket(PF_UNIX, SOCK_STREAM, 0);
57         if (chansess->agentfd < 0) {
58                 goto fail;
59         }
60
61         /* create the unix socket dir and file */
62         if (bindagent(chansess) == DROPBEAR_FAILURE) {
63                 return DROPBEAR_FAILURE;
64         }
65
66         /* listen */
67         if (listen(chansess->agentfd, 20) < 0) {
68                 goto fail;
69         }
70
71         /* set non-blocking */
72         if (fcntl(chansess->agentfd, F_SETFL, O_NONBLOCK) < 0) {
73                 goto fail;
74         }
75
76         /* channel.c's channel fd code will handle the socket now */
77
78         /* set the maxfd so that select() loop will notice it */
79         ses.maxfd = MAX(ses.maxfd, chansess->agentfd);
80
81         return DROPBEAR_SUCCESS;
82
83 fail:
84         /* cleanup */
85         agentcleanup(chansess);
86
87         return DROPBEAR_FAILURE;
88 }
89
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) {
94
95         int fd;
96
97         fd = accept(chansess->agentfd, NULL, NULL);
98         if (fd < 0) {
99                 return DROPBEAR_FAILURE;
100         }
101
102         return send_msg_channel_open_agent(fd);
103
104 }
105
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) {
109
110         char path[MAXPATHLEN];
111
112         if (chansess->agentfd == -1) {
113                 return;
114         }
115
116         snprintf(path, sizeof(path), "%s/%s", chansess->agentdir,
117                         chansess->agentfile);
118         addnewvar("SSH_AUTH_SOCK", path);
119 }
120
121 /* close the socket, remove the socket-file */
122 void agentcleanup(struct ChanSess * chansess) {
123
124         char path[MAXPATHLEN];
125         uid_t uid;
126         gid_t gid;
127
128         if (chansess->agentfd == -1) {
129                 return;
130         }
131
132         close(chansess->agentfd);
133
134         /* Remove the dir as the user. That way they can't cause problems except
135          * for themselves */
136         uid = getuid();
137         gid = getgid();
138         if ((setegid(ses.authstate.pw->pw_gid)) < 0 ||
139                 (seteuid(ses.authstate.pw->pw_uid)) < 0) {
140                 dropbear_exit("failed to set euid");
141         }
142
143         snprintf(path, sizeof(path),
144                         "%s/%s", chansess->agentdir, chansess->agentfile);
145
146         unlink(path);
147         rmdir(chansess->agentdir);
148
149         if ((seteuid(uid)) < 0 ||
150                 (setegid(gid)) < 0) {
151                 dropbear_exit("failed to revert euid");
152         }
153
154         m_free(chansess->agentfile);
155         m_free(chansess->agentdir);
156
157 }
158
159 /* helper for accepting an agent request */
160 static int send_msg_channel_open_agent(int fd) {
161
162         if (send_msg_channel_open_init(fd, "auth-agent@openssh.com") 
163                         == DROPBEAR_SUCCESS) {
164                 encrypt_packet();
165                 return DROPBEAR_SUCCESS;
166         } else {
167                 return DROPBEAR_FAILURE;
168         }
169 }
170
171 /* helper for creating the agent socket-file
172    returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
173 static int bindagent(struct ChanSess * chansess) {
174
175         struct sockaddr_un addr;
176         unsigned int prefix;
177         char path[sizeof(addr.sun_path)], sockfile[sizeof(addr.sun_path)];
178         mode_t mode;
179         int i;
180         uid_t uid;
181         gid_t gid;
182         int ret = DROPBEAR_FAILURE;
183
184         /* drop to user privs to make the dir/file */
185         uid = getuid();
186         gid = getgid();
187         if ((setegid(ses.authstate.pw->pw_gid)) < 0 ||
188                 (seteuid(ses.authstate.pw->pw_uid)) < 0) {
189                 dropbear_exit("failed to set euid");
190         }
191
192         addr.sun_family = AF_UNIX;
193
194         mode = S_IRWXU;
195
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);
200
201                 if (mkdir(path, mode) == 0) {
202                         goto bindsocket;
203                 }
204                 if (errno != EEXIST) {
205                         break;
206                 }
207         }
208         /* couldn't make a dir */
209         goto out;
210
211 bindsocket:
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
215          * 1/(2^64) */
216         genrandom((unsigned char*)&prefix, sizeof(prefix));
217         snprintf(sockfile, sizeof(sockfile), "auth-%.8x-%d", prefix,
218                         chansess->agentfd);
219         snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s", path, sockfile);
220
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;
225         }
226
227
228 out:
229         if ((seteuid(uid)) < 0 ||
230                 (setegid(gid)) < 0) {
231                 dropbear_exit("failed to revert euid");
232         }
233         return ret;
234 }
235
236 #endif