Mercurial > dropbear
comparison svr-agentfwd.c @ 4:fe6bca95afa7
Makefile.in contains updated files required
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 01 Jun 2004 02:46:09 +0000 |
parents | |
children | bc6477a6c393 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 4:fe6bca95afa7 |
---|---|
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 "dbutil.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 = NULL; | |
111 int len; | |
112 | |
113 if (chansess->agentfd == -1) { | |
114 return; | |
115 } | |
116 | |
117 /* 2 for "/" and "\0" */ | |
118 len = strlen(chansess->agentdir) + strlen(chansess->agentfile) + 2; | |
119 | |
120 path = m_malloc(len); | |
121 snprintf(path, len, "%s/%s", chansess->agentdir, chansess->agentfile); | |
122 addnewvar("SSH_AUTH_SOCK", path); | |
123 m_free(path); | |
124 } | |
125 | |
126 /* close the socket, remove the socket-file */ | |
127 void agentcleanup(struct ChanSess * chansess) { | |
128 | |
129 char *path = NULL; | |
130 uid_t uid; | |
131 gid_t gid; | |
132 int len; | |
133 | |
134 if (chansess->agentfd == -1) { | |
135 return; | |
136 } | |
137 | |
138 close(chansess->agentfd); | |
139 | |
140 /* Remove the dir as the user. That way they can't cause problems except | |
141 * for themselves */ | |
142 uid = getuid(); | |
143 gid = getgid(); | |
144 if ((setegid(ses.authstate.pw->pw_gid)) < 0 || | |
145 (seteuid(ses.authstate.pw->pw_uid)) < 0) { | |
146 dropbear_exit("failed to set euid"); | |
147 } | |
148 | |
149 /* 2 for "/" and "\0" */ | |
150 len = strlen(chansess->agentdir) + strlen(chansess->agentfile) + 2; | |
151 | |
152 path = m_malloc(len); | |
153 snprintf(path, len, "%s/%s", chansess->agentdir, chansess->agentfile); | |
154 unlink(path); | |
155 m_free(path); | |
156 | |
157 rmdir(chansess->agentdir); | |
158 | |
159 if ((seteuid(uid)) < 0 || | |
160 (setegid(gid)) < 0) { | |
161 dropbear_exit("failed to revert euid"); | |
162 } | |
163 | |
164 m_free(chansess->agentfile); | |
165 m_free(chansess->agentdir); | |
166 | |
167 } | |
168 | |
169 /* helper for accepting an agent request */ | |
170 static int send_msg_channel_open_agent(int fd) { | |
171 | |
172 if (send_msg_channel_open_init(fd, CHANNEL_ID_AGENT, | |
173 "[email protected]") == DROPBEAR_SUCCESS) { | |
174 encrypt_packet(); | |
175 return DROPBEAR_SUCCESS; | |
176 } else { | |
177 return DROPBEAR_FAILURE; | |
178 } | |
179 } | |
180 | |
181 /* helper for creating the agent socket-file | |
182 returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ | |
183 static int bindagent(struct ChanSess * chansess) { | |
184 | |
185 struct sockaddr_un addr; | |
186 unsigned int prefix; | |
187 char path[sizeof(addr.sun_path)], sockfile[sizeof(addr.sun_path)]; | |
188 mode_t mode; | |
189 int i; | |
190 uid_t uid; | |
191 gid_t gid; | |
192 int ret = DROPBEAR_FAILURE; | |
193 | |
194 /* drop to user privs to make the dir/file */ | |
195 uid = getuid(); | |
196 gid = getgid(); | |
197 if ((setegid(ses.authstate.pw->pw_gid)) < 0 || | |
198 (seteuid(ses.authstate.pw->pw_uid)) < 0) { | |
199 dropbear_exit("failed to set euid"); | |
200 } | |
201 | |
202 memset((void*)&addr, 0x0, sizeof(addr)); | |
203 addr.sun_family = AF_UNIX; | |
204 | |
205 mode = S_IRWXU; | |
206 | |
207 for (i = 0; i < 20; i++) { | |
208 genrandom((unsigned char*)&prefix, sizeof(prefix)); | |
209 /* we want 32 bits (8 hex digits) - "/tmp/dropbear-f19c62c0" */ | |
210 snprintf(path, sizeof(path), AGENTDIRPREFIX "%.8x", prefix); | |
211 | |
212 if (mkdir(path, mode) == 0) { | |
213 goto bindsocket; | |
214 } | |
215 if (errno != EEXIST) { | |
216 break; | |
217 } | |
218 } | |
219 /* couldn't make a dir */ | |
220 goto out; | |
221 | |
222 bindsocket: | |
223 /* Format is "/tmp/dropbear-0246dead/auth-d00f7654-23". | |
224 * The "23" is the file desc, the random data is to avoid collisions | |
225 * between subsequent user processes reusing socket fds (odds are now | |
226 * 1/(2^64) */ | |
227 genrandom((unsigned char*)&prefix, sizeof(prefix)); | |
228 snprintf(sockfile, sizeof(sockfile), "auth-%.8x-%d", prefix, | |
229 chansess->agentfd); | |
230 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s", path, sockfile); | |
231 | |
232 if (bind(chansess->agentfd, (struct sockaddr*)&addr, sizeof(addr)) == 0) { | |
233 chansess->agentdir = strdup(path); | |
234 chansess->agentfile = strdup(sockfile); | |
235 ret = DROPBEAR_SUCCESS; | |
236 } | |
237 | |
238 | |
239 out: | |
240 if ((seteuid(uid)) < 0 || | |
241 (setegid(gid)) < 0) { | |
242 dropbear_exit("failed to revert euid"); | |
243 } | |
244 return ret; | |
245 } | |
246 | |
247 #endif |