Mercurial > dropbear
comparison svr-x11fwd.c @ 12:7a37cff27258
merge of a585c2284e9ad17bfe6c6fd8f18b1c5042b2df47
and e3f735bb16fbd5cfb6bcad70885550c1b79b874c
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 03 Jun 2004 17:29:17 +0000 |
parents | 0f7d69d31b9d |
children | db2c8e6fb284 |
comparison
equal
deleted
inserted
replaced
11:f76c9389e9e0 | 12:7a37cff27258 |
---|---|
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 #include "includes.h" | |
26 | |
27 #ifndef DISABLE_X11FWD | |
28 #include "x11fwd.h" | |
29 #include "session.h" | |
30 #include "ssh.h" | |
31 #include "dbutil.h" | |
32 #include "chansession.h" | |
33 #include "channel.h" | |
34 #include "packet.h" | |
35 #include "buffer.h" | |
36 | |
37 #define X11BASEPORT 6000 | |
38 #define X11BINDBASE 6010 | |
39 | |
40 static void x11accept(struct Listener* listener); | |
41 static int bindport(int fd); | |
42 static int send_msg_channel_open_x11(int fd, struct sockaddr_in* addr); | |
43 | |
44 /* called as a request for a session channel, sets up listening X11 */ | |
45 /* returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ | |
46 int x11req(struct ChanSess * chansess) { | |
47 | |
48 int fd; | |
49 | |
50 /* we already have an x11 connection */ | |
51 if (chansess->x11listener != NULL) { | |
52 return DROPBEAR_FAILURE; | |
53 } | |
54 | |
55 chansess->x11singleconn = buf_getbyte(ses.payload); | |
56 chansess->x11authprot = buf_getstring(ses.payload, NULL); | |
57 chansess->x11authcookie = buf_getstring(ses.payload, NULL); | |
58 chansess->x11screennum = buf_getint(ses.payload); | |
59 | |
60 /* create listening socket */ | |
61 fd = socket(PF_INET, SOCK_STREAM, 0); | |
62 if (fd < 0) { | |
63 goto fail; | |
64 } | |
65 | |
66 /* allocate port and bind */ | |
67 chansess->x11port = bindport(fd); | |
68 if (chansess->x11port < 0) { | |
69 goto fail; | |
70 } | |
71 | |
72 /* listen */ | |
73 if (listen(fd, 20) < 0) { | |
74 goto fail; | |
75 } | |
76 | |
77 /* set non-blocking */ | |
78 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { | |
79 goto fail; | |
80 } | |
81 | |
82 /* listener code will handle the socket now. | |
83 * No cleanup handler needed, since listener_remove only happens | |
84 * from our cleanup anyway */ | |
85 chansess->x11listener = new_listener( fd, 0, chansess, x11accept, NULL); | |
86 if (chansess->x11listener == NULL) { | |
87 goto fail; | |
88 } | |
89 | |
90 return DROPBEAR_SUCCESS; | |
91 | |
92 fail: | |
93 /* cleanup */ | |
94 m_free(chansess->x11authprot); | |
95 m_free(chansess->x11authcookie); | |
96 close(fd); | |
97 | |
98 return DROPBEAR_FAILURE; | |
99 } | |
100 | |
101 /* accepts a new X11 socket */ | |
102 /* returns DROPBEAR_FAILURE or DROPBEAR_SUCCESS */ | |
103 static void x11accept(struct Listener* listener) { | |
104 | |
105 int fd; | |
106 struct sockaddr_in addr; | |
107 int len; | |
108 int ret; | |
109 | |
110 len = sizeof(addr); | |
111 | |
112 fd = accept(listener->sock, (struct sockaddr*)&addr, &len); | |
113 if (fd < 0) { | |
114 return; | |
115 } | |
116 | |
117 /* if single-connection we close it up */ | |
118 if (((struct ChanSess *)(listener->typedata))->x11singleconn) { | |
119 x11cleanup(listener); | |
120 } | |
121 | |
122 ret = send_msg_channel_open_x11(fd, &addr); | |
123 if (ret == DROPBEAR_FAILURE) { | |
124 close(fd); | |
125 } | |
126 } | |
127 | |
128 /* This is called after switching to the user, and sets up the xauth | |
129 * and environment variables. */ | |
130 void x11setauth(struct ChanSess *chansess) { | |
131 | |
132 char display[20]; /* space for "localhost:12345.123" */ | |
133 FILE * authprog; | |
134 int val; | |
135 | |
136 if (chansess->x11listener == NULL) { | |
137 return; | |
138 } | |
139 | |
140 /* create the DISPLAY string */ | |
141 val = snprintf(display, sizeof(display), "localhost:%d.%d", | |
142 chansess->x11port - X11BASEPORT, chansess->x11screennum); | |
143 if (val < 0 || val >= (int)sizeof(display)) { | |
144 /* string was truncated */ | |
145 return; | |
146 } | |
147 | |
148 addnewvar("DISPLAY", display); | |
149 | |
150 /* create the xauth string */ | |
151 val = snprintf(display, sizeof(display), "unix:%d.%d", | |
152 chansess->x11port - X11BASEPORT, chansess->x11screennum); | |
153 if (val < 0 || val >= (int)sizeof(display)) { | |
154 /* string was truncated */ | |
155 return; | |
156 } | |
157 | |
158 /* popen is a nice function - code is strongly based on OpenSSH's */ | |
159 authprog = popen(XAUTH_COMMAND, "w"); | |
160 if (authprog) { | |
161 fprintf(authprog, "add %s %s %s\n", | |
162 display, chansess->x11authprot, chansess->x11authcookie); | |
163 pclose(authprog); | |
164 } else { | |
165 fprintf(stderr, "Failed to run %s\n", XAUTH_COMMAND); | |
166 } | |
167 } | |
168 | |
169 void x11cleanup(struct Listener *listener) { | |
170 | |
171 struct ChanSess *chansess = (struct ChanSess*)listener->typedata; | |
172 | |
173 m_free(chansess->x11authprot); | |
174 m_free(chansess->x11authcookie); | |
175 remove_listener(listener); | |
176 chansess->x11listener = NULL; | |
177 } | |
178 | |
179 static const struct ChanType chan_x11 = { | |
180 0, /* sepfds */ | |
181 "x11", | |
182 NULL, /* inithandler */ | |
183 NULL, /* checkclose */ | |
184 NULL, /* reqhandler */ | |
185 NULL /* closehandler */ | |
186 }; | |
187 | |
188 | |
189 static int send_msg_channel_open_x11(int fd, struct sockaddr_in* addr) { | |
190 | |
191 char* ipstring; | |
192 | |
193 if (send_msg_channel_open_init(fd, &chan_x11) == DROPBEAR_SUCCESS) { | |
194 ipstring = inet_ntoa(addr->sin_addr); | |
195 buf_putstring(ses.writepayload, ipstring, strlen(ipstring)); | |
196 buf_putint(ses.writepayload, addr->sin_port); | |
197 | |
198 encrypt_packet(); | |
199 return DROPBEAR_SUCCESS; | |
200 } else { | |
201 return DROPBEAR_FAILURE; | |
202 } | |
203 | |
204 } | |
205 | |
206 /* returns the port bound to, or -1 on failure. | |
207 * Will attempt to bind to a port X11BINDBASE (6010 usually) or upwards */ | |
208 static int bindport(int fd) { | |
209 | |
210 struct sockaddr_in addr; | |
211 uint16_t port; | |
212 | |
213 memset((void*)&addr, 0x0, sizeof(addr)); | |
214 addr.sin_family = AF_INET; | |
215 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
216 | |
217 /* if we can't find one in 2000 ports free, something's wrong */ | |
218 for (port = X11BINDBASE; port < X11BINDBASE + 2000; port++) { | |
219 addr.sin_port = htons(port); | |
220 if (bind(fd, (struct sockaddr*)&addr, | |
221 sizeof(struct sockaddr_in)) == 0) { | |
222 /* success */ | |
223 return port; | |
224 } | |
225 if (errno == EADDRINUSE) { | |
226 /* try the next port */ | |
227 continue; | |
228 } | |
229 /* otherwise it was an error we don't know about */ | |
230 dropbear_log(LOG_DEBUG, "failed to bind x11 socket"); | |
231 break; | |
232 } | |
233 return -1; | |
234 } | |
235 #endif /* DROPBEAR_X11FWD */ |