OSDN Git Service

Complete Send mail coding and mail send have some problem.
[tortoisegit/TortoiseGitJp.git] / src / TortoisePlink / X11FWD.C
1 /*\r
2  * Platform-independent bits of X11 forwarding.\r
3  */\r
4 \r
5 #include <stdio.h>\r
6 #include <stdlib.h>\r
7 #include <assert.h>\r
8 #include <time.h>\r
9 \r
10 #include "putty.h"\r
11 #include "ssh.h"\r
12 #include "tree234.h"\r
13 \r
14 #define GET_16BIT(endian, cp) \\r
15   (endian=='B' ? GET_16BIT_MSB_FIRST(cp) : GET_16BIT_LSB_FIRST(cp))\r
16 \r
17 #define PUT_16BIT(endian, cp, val) \\r
18   (endian=='B' ? PUT_16BIT_MSB_FIRST(cp, val) : PUT_16BIT_LSB_FIRST(cp, val))\r
19 \r
20 const char *const x11_authnames[] = {\r
21     "", "MIT-MAGIC-COOKIE-1", "XDM-AUTHORIZATION-1"\r
22 };\r
23 \r
24 struct XDMSeen {\r
25     unsigned int time;\r
26     unsigned char clientid[6];\r
27 };\r
28 \r
29 struct X11Auth {\r
30     unsigned char fakedata[64], realdata[64];\r
31     int fakeproto, realproto;\r
32     int fakelen, reallen;\r
33     tree234 *xdmseen;\r
34 };\r
35 \r
36 struct X11Private {\r
37     const struct plug_function_table *fn;\r
38     /* the above variable absolutely *must* be the first in this structure */\r
39     unsigned char firstpkt[12];        /* first X data packet */\r
40     struct X11Auth *auth;\r
41     char *auth_protocol;\r
42     unsigned char *auth_data;\r
43     int data_read, auth_plen, auth_psize, auth_dlen, auth_dsize;\r
44     int verified;\r
45     int throttled, throttle_override;\r
46     unsigned long peer_ip;\r
47     int peer_port;\r
48     void *c;                           /* data used by ssh.c */\r
49     Socket s;\r
50 };\r
51 \r
52 static int xdmseen_cmp(void *a, void *b)\r
53 {\r
54     struct XDMSeen *sa = a, *sb = b;\r
55     return sa->time > sb->time ? 1 :\r
56            sa->time < sb->time ? -1 :\r
57            memcmp(sa->clientid, sb->clientid, sizeof(sa->clientid));\r
58 }\r
59 \r
60 void *x11_invent_auth(char *proto, int protomaxlen,\r
61                       char *data, int datamaxlen, int proto_id)\r
62 {\r
63     struct X11Auth *auth = snew(struct X11Auth);\r
64     char ourdata[64];\r
65     int i;\r
66 \r
67     if (proto_id == X11_MIT) {\r
68         auth->fakeproto = X11_MIT;\r
69 \r
70         /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */\r
71         auth->fakelen = 16;\r
72         for (i = 0; i < 16; i++)\r
73             auth->fakedata[i] = random_byte();\r
74         auth->xdmseen = NULL;\r
75     } else {\r
76         assert(proto_id == X11_XDM);\r
77         auth->fakeproto = X11_XDM;\r
78 \r
79         /* XDM-AUTHORIZATION-1. Cookie size is 16 bytes; byte 8 is zero. */\r
80         auth->fakelen = 16;\r
81         for (i = 0; i < 16; i++)\r
82             auth->fakedata[i] = (i == 8 ? 0 : random_byte());\r
83         auth->xdmseen = newtree234(xdmseen_cmp);\r
84     }\r
85 \r
86     /* Now format for the recipient. */\r
87     strncpy(proto, x11_authnames[auth->fakeproto], protomaxlen);\r
88     ourdata[0] = '\0';\r
89     for (i = 0; i < auth->fakelen; i++)\r
90         sprintf(ourdata + strlen(ourdata), "%02x", auth->fakedata[i]);\r
91     strncpy(data, ourdata, datamaxlen);\r
92 \r
93     return auth;\r
94 }\r
95 \r
96 void x11_free_auth(void *authv)\r
97 {\r
98     struct X11Auth *auth = (struct X11Auth *)authv;\r
99     struct XDMSeen *seen;\r
100 \r
101     if (auth->xdmseen != NULL) {\r
102         while ((seen = delpos234(auth->xdmseen, 0)) != NULL)\r
103             sfree(seen);\r
104         freetree234(auth->xdmseen);\r
105     }\r
106     sfree(auth);\r
107 }\r
108 \r
109 /*\r
110  * Fetch the real auth data for a given display string, and store\r
111  * it in an X11Auth structure. Returns NULL on success, or an error\r
112  * string.\r
113  */\r
114 void x11_get_real_auth(void *authv, char *display)\r
115 {\r
116     struct X11Auth *auth = (struct X11Auth *)authv;\r
117 \r
118     auth->realproto = X11_NO_AUTH;     /* in case next call does nothing */\r
119 \r
120     auth->reallen = sizeof(auth->realdata);\r
121     platform_get_x11_auth(display, &auth->realproto,\r
122                           auth->realdata, &auth->reallen);\r
123 }\r
124 \r
125 #define XDM_MAXSKEW 20*60      /* 20 minute clock skew should be OK */\r
126 \r
127 static char *x11_verify(unsigned long peer_ip, int peer_port,\r
128                         struct X11Auth *auth, char *proto,\r
129                         unsigned char *data, int dlen)\r
130 {\r
131     if (strcmp(proto, x11_authnames[auth->fakeproto]) != 0)\r
132         return "wrong authentication protocol attempted";\r
133     if (auth->fakeproto == X11_MIT) {\r
134         if (dlen != auth->fakelen)\r
135             return "MIT-MAGIC-COOKIE-1 data was wrong length";\r
136         if (memcmp(auth->fakedata, data, dlen) != 0)\r
137             return "MIT-MAGIC-COOKIE-1 data did not match";\r
138     }\r
139     if (auth->fakeproto == X11_XDM) {\r
140         unsigned long t;\r
141         time_t tim;\r
142         int i;\r
143         struct XDMSeen *seen, *ret;\r
144 \r
145         if (dlen != 24)\r
146             return "XDM-AUTHORIZATION-1 data was wrong length";\r
147         if (peer_port == -1)\r
148             return "cannot do XDM-AUTHORIZATION-1 without remote address data";\r
149         des_decrypt_xdmauth(auth->fakedata+9, data, 24);\r
150         if (memcmp(auth->fakedata, data, 8) != 0)\r
151             return "XDM-AUTHORIZATION-1 data failed check"; /* cookie wrong */\r
152         if (GET_32BIT_MSB_FIRST(data+8) != peer_ip)\r
153             return "XDM-AUTHORIZATION-1 data failed check";   /* IP wrong */\r
154         if ((int)GET_16BIT_MSB_FIRST(data+12) != peer_port)\r
155             return "XDM-AUTHORIZATION-1 data failed check";   /* port wrong */\r
156         t = GET_32BIT_MSB_FIRST(data+14);\r
157         for (i = 18; i < 24; i++)\r
158             if (data[i] != 0)          /* zero padding wrong */\r
159                 return "XDM-AUTHORIZATION-1 data failed check";\r
160         tim = time(NULL);\r
161         if (abs(t - tim) > XDM_MAXSKEW)\r
162             return "XDM-AUTHORIZATION-1 time stamp was too far out";\r
163         seen = snew(struct XDMSeen);\r
164         seen->time = t;\r
165         memcpy(seen->clientid, data+8, 6);\r
166         assert(auth->xdmseen != NULL);\r
167         ret = add234(auth->xdmseen, seen);\r
168         if (ret != seen) {\r
169             sfree(seen);\r
170             return "XDM-AUTHORIZATION-1 data replayed";\r
171         }\r
172         /* While we're here, purge entries too old to be replayed. */\r
173         for (;;) {\r
174             seen = index234(auth->xdmseen, 0);\r
175             assert(seen != NULL);\r
176             if (t - seen->time <= XDM_MAXSKEW)\r
177                 break;\r
178             sfree(delpos234(auth->xdmseen, 0));\r
179         }\r
180     }\r
181     /* implement other protocols here if ever required */\r
182     return NULL;\r
183 }\r
184 \r
185 static void x11_log(Plug p, int type, SockAddr addr, int port,\r
186                     const char *error_msg, int error_code)\r
187 {\r
188     /* We have no interface to the logging module here, so we drop these. */\r
189 }\r
190 \r
191 static int x11_closing(Plug plug, const char *error_msg, int error_code,\r
192                        int calling_back)\r
193 {\r
194     struct X11Private *pr = (struct X11Private *) plug;\r
195 \r
196     /*\r
197      * We have no way to communicate down the forwarded connection,\r
198      * so if an error occurred on the socket, we just ignore it\r
199      * and treat it like a proper close.\r
200      */\r
201     sshfwd_close(pr->c);\r
202     x11_close(pr->s);\r
203     return 1;\r
204 }\r
205 \r
206 static int x11_receive(Plug plug, int urgent, char *data, int len)\r
207 {\r
208     struct X11Private *pr = (struct X11Private *) plug;\r
209 \r
210     if (sshfwd_write(pr->c, data, len) > 0) {\r
211         pr->throttled = 1;\r
212         sk_set_frozen(pr->s, 1);\r
213     }\r
214 \r
215     return 1;\r
216 }\r
217 \r
218 static void x11_sent(Plug plug, int bufsize)\r
219 {\r
220     struct X11Private *pr = (struct X11Private *) plug;\r
221 \r
222     sshfwd_unthrottle(pr->c, bufsize);\r
223 }\r
224 \r
225 /*\r
226  * When setting up X forwarding, we should send the screen number\r
227  * from the specified local display. This function extracts it from\r
228  * the display string.\r
229  */\r
230 int x11_get_screen_number(char *display)\r
231 {\r
232     int n;\r
233 \r
234     n = strcspn(display, ":");\r
235     if (!display[n])\r
236         return 0;\r
237     n = strcspn(display, ".");\r
238     if (!display[n])\r
239         return 0;\r
240     return atoi(display + n + 1);\r
241 }\r
242 \r
243 /* Find the right display, returns an allocated string */\r
244 char *x11_display(const char *display) {\r
245     char *ret;\r
246     if(!display || !*display) {\r
247         /* try to find platform-specific local display */\r
248         if((ret = platform_get_x_display())==0 || !*ret)\r
249             /* plausible default for all platforms */\r
250             ret = dupstr(":0");\r
251     } else\r
252         ret = dupstr(display);\r
253     if(ret[0] == ':') {\r
254         /* no transport specified, use whatever we think is best */\r
255         char *s = dupcat(platform_x11_best_transport, ret, (char *)0);\r
256         sfree(ret);\r
257         return s;\r
258     } else\r
259         return ret;\r
260 }\r
261 \r
262 /*\r
263  * Called to set up the raw connection.\r
264  * \r
265  * Returns an error message, or NULL on success.\r
266  * also, fills the SocketsStructure\r
267  */\r
268 const char *x11_init(Socket * s, char *display, void *c, void *auth,\r
269                      const char *peeraddr, int peerport, const Config *cfg)\r
270 {\r
271     static const struct plug_function_table fn_table = {\r
272         x11_log,\r
273         x11_closing,\r
274         x11_receive,\r
275         x11_sent,\r
276         NULL\r
277     };\r
278 \r
279     SockAddr addr;\r
280     int port;\r
281     const char *err;\r
282     char *dummy_realhost;\r
283     char host[128];\r
284     int n, displaynum;\r
285     struct X11Private *pr;\r
286 \r
287     /* default display */\r
288     display = x11_display(display);\r
289     /*\r
290      * Split up display name into host and display-number parts.\r
291      */\r
292     n = strcspn(display, ":");\r
293     assert(n != 0);             /* x11_display() promises this */\r
294     if (display[n])\r
295         displaynum = atoi(display + n + 1);\r
296     else\r
297         displaynum = 0;                /* sensible default */\r
298     if (n > sizeof(host) - 1)\r
299         n = sizeof(host) - 1;\r
300     strncpy(host, display, n);\r
301     host[n] = '\0';\r
302     sfree(display);\r
303     \r
304     if(!strcmp(host, "unix")) {\r
305         /* use AF_UNIX sockets (doesn't make sense on all platforms) */\r
306         addr = platform_get_x11_unix_address(displaynum,\r
307                                              &dummy_realhost);\r
308         port = 0;               /* to show we are not confused */\r
309     } else {\r
310         port = 6000 + displaynum;\r
311         \r
312         /*\r
313          * Try to find host.\r
314          */\r
315         addr = name_lookup(host, port, &dummy_realhost, cfg, ADDRTYPE_UNSPEC);\r
316         if ((err = sk_addr_error(addr)) != NULL) {\r
317             sk_addr_free(addr);\r
318             return err;\r
319         }\r
320     }\r
321 \r
322     /*\r
323      * Open socket.\r
324      */\r
325     pr = snew(struct X11Private);\r
326     pr->fn = &fn_table;\r
327     pr->auth_protocol = NULL;\r
328     pr->auth = (struct X11Auth *)auth;\r
329     pr->verified = 0;\r
330     pr->data_read = 0;\r
331     pr->throttled = pr->throttle_override = 0;\r
332     pr->c = c;\r
333 \r
334     pr->s = *s = new_connection(addr, dummy_realhost, port,\r
335                                 0, 1, 0, 0, (Plug) pr, cfg);\r
336     if ((err = sk_socket_error(*s)) != NULL) {\r
337         sfree(pr);\r
338         return err;\r
339     }\r
340 \r
341     /*\r
342      * See if we can make sense of the peer address we were given.\r
343      */\r
344     {\r
345         int i[4];\r
346         if (peeraddr &&\r
347             4 == sscanf(peeraddr, "%d.%d.%d.%d", i+0, i+1, i+2, i+3)) {\r
348             pr->peer_ip = (i[0] << 24) | (i[1] << 16) | (i[2] << 8) | i[3];\r
349             pr->peer_port = peerport;\r
350         } else {\r
351             pr->peer_ip = 0;\r
352             pr->peer_port = -1;\r
353         }\r
354     }\r
355 \r
356     sk_set_private_ptr(*s, pr);\r
357     return NULL;\r
358 }\r
359 \r
360 void x11_close(Socket s)\r
361 {\r
362     struct X11Private *pr;\r
363     if (!s)\r
364         return;\r
365     pr = (struct X11Private *) sk_get_private_ptr(s);\r
366     if (pr->auth_protocol) {\r
367         sfree(pr->auth_protocol);\r
368         sfree(pr->auth_data);\r
369     }\r
370 \r
371     sfree(pr);\r
372 \r
373     sk_close(s);\r
374 }\r
375 \r
376 void x11_unthrottle(Socket s)\r
377 {\r
378     struct X11Private *pr;\r
379     if (!s)\r
380         return;\r
381     pr = (struct X11Private *) sk_get_private_ptr(s);\r
382 \r
383     pr->throttled = 0;\r
384     sk_set_frozen(s, pr->throttled || pr->throttle_override);\r
385 }\r
386 \r
387 void x11_override_throttle(Socket s, int enable)\r
388 {\r
389     struct X11Private *pr;\r
390     if (!s)\r
391         return;\r
392     pr = (struct X11Private *) sk_get_private_ptr(s);\r
393 \r
394     pr->throttle_override = enable;\r
395     sk_set_frozen(s, pr->throttled || pr->throttle_override);\r
396 }\r
397 \r
398 /*\r
399  * Called to send data down the raw connection.\r
400  */\r
401 int x11_send(Socket s, char *data, int len)\r
402 {\r
403     struct X11Private *pr;\r
404     if (!s)\r
405         return 0;\r
406     pr = (struct X11Private *) sk_get_private_ptr(s);\r
407 \r
408     /*\r
409      * Read the first packet.\r
410      */\r
411     while (len > 0 && pr->data_read < 12)\r
412         pr->firstpkt[pr->data_read++] = (unsigned char) (len--, *data++);\r
413     if (pr->data_read < 12)\r
414         return 0;\r
415 \r
416     /*\r
417      * If we have not allocated the auth_protocol and auth_data\r
418      * strings, do so now.\r
419      */\r
420     if (!pr->auth_protocol) {\r
421         pr->auth_plen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 6);\r
422         pr->auth_dlen = GET_16BIT(pr->firstpkt[0], pr->firstpkt + 8);\r
423         pr->auth_psize = (pr->auth_plen + 3) & ~3;\r
424         pr->auth_dsize = (pr->auth_dlen + 3) & ~3;\r
425         /* Leave room for a terminating zero, to make our lives easier. */\r
426         pr->auth_protocol = snewn(pr->auth_psize + 1, char);\r
427         pr->auth_data = snewn(pr->auth_dsize, unsigned char);\r
428     }\r
429 \r
430     /*\r
431      * Read the auth_protocol and auth_data strings.\r
432      */\r
433     while (len > 0 && pr->data_read < 12 + pr->auth_psize)\r
434         pr->auth_protocol[pr->data_read++ - 12] = (len--, *data++);\r
435     while (len > 0 && pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)\r
436         pr->auth_data[pr->data_read++ - 12 -\r
437                       pr->auth_psize] = (unsigned char) (len--, *data++);\r
438     if (pr->data_read < 12 + pr->auth_psize + pr->auth_dsize)\r
439         return 0;\r
440 \r
441     /*\r
442      * If we haven't verified the authentication, do so now.\r
443      */\r
444     if (!pr->verified) {\r
445         char *err;\r
446 \r
447         pr->auth_protocol[pr->auth_plen] = '\0';        /* ASCIZ */\r
448         err = x11_verify(pr->peer_ip, pr->peer_port,\r
449                          pr->auth, pr->auth_protocol,\r
450                          pr->auth_data, pr->auth_dlen);\r
451 \r
452         /*\r
453          * If authentication failed, construct and send an error\r
454          * packet, then terminate the connection.\r
455          */\r
456         if (err) {\r
457             char *message;\r
458             int msglen, msgsize;\r
459             unsigned char *reply;\r
460 \r
461             message = dupprintf("PuTTY X11 proxy: %s", err);\r
462             msglen = strlen(message);\r
463             reply = snewn(8 + msglen+1 + 4, unsigned char); /* include zero */\r
464             msgsize = (msglen + 3) & ~3;\r
465             reply[0] = 0;              /* failure */\r
466             reply[1] = msglen;         /* length of reason string */\r
467             memcpy(reply + 2, pr->firstpkt + 2, 4);     /* major/minor proto vsn */\r
468             PUT_16BIT(pr->firstpkt[0], reply + 6, msgsize >> 2);/* data len */\r
469             memset(reply + 8, 0, msgsize);\r
470             memcpy(reply + 8, message, msglen);\r
471             sshfwd_write(pr->c, (char *)reply, 8 + msgsize);\r
472             sshfwd_close(pr->c);\r
473             x11_close(s);\r
474             sfree(reply);\r
475             sfree(message);\r
476             return 0;\r
477         }\r
478 \r
479         /*\r
480          * Now we know we're going to accept the connection. Strip\r
481          * the fake auth data, and optionally put real auth data in\r
482          * instead.\r
483          */\r
484         {\r
485             char realauthdata[64];\r
486             int realauthlen = 0;\r
487             int authstrlen = strlen(x11_authnames[pr->auth->realproto]);\r
488             int buflen = 0;            /* initialise to placate optimiser */\r
489             static const char zeroes[4] = { 0,0,0,0 };\r
490             void *buf;\r
491 \r
492             if (pr->auth->realproto == X11_MIT) {\r
493                 assert(pr->auth->reallen <= lenof(realauthdata));\r
494                 realauthlen = pr->auth->reallen;\r
495                 memcpy(realauthdata, pr->auth->realdata, realauthlen);\r
496             } else if (pr->auth->realproto == X11_XDM &&\r
497                        pr->auth->reallen == 16 &&\r
498                        ((buf = sk_getxdmdata(s, &buflen))!=0)) {\r
499                 time_t t;\r
500                 realauthlen = (buflen+12+7) & ~7;\r
501                 assert(realauthlen <= lenof(realauthdata));\r
502                 memset(realauthdata, 0, realauthlen);\r
503                 memcpy(realauthdata, pr->auth->realdata, 8);\r
504                 memcpy(realauthdata+8, buf, buflen);\r
505                 t = time(NULL);\r
506                 PUT_32BIT_MSB_FIRST(realauthdata+8+buflen, t);\r
507                 des_encrypt_xdmauth(pr->auth->realdata+9,\r
508                                     (unsigned char *)realauthdata,\r
509                                     realauthlen);\r
510                 sfree(buf);\r
511             }\r
512             /* implement other auth methods here if required */\r
513 \r
514             PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 6, authstrlen);\r
515             PUT_16BIT(pr->firstpkt[0], pr->firstpkt + 8, realauthlen);\r
516         \r
517             sk_write(s, (char *)pr->firstpkt, 12);\r
518 \r
519             if (authstrlen) {\r
520                 sk_write(s, x11_authnames[pr->auth->realproto], authstrlen);\r
521                 sk_write(s, zeroes, 3 & (-authstrlen));\r
522             }\r
523             if (realauthlen) {\r
524                 sk_write(s, realauthdata, realauthlen);\r
525                 sk_write(s, zeroes, 3 & (-realauthlen));\r
526             }\r
527         }\r
528         pr->verified = 1;\r
529     }\r
530 \r
531     /*\r
532      * After initialisation, just copy data simply.\r
533      */\r
534 \r
535     return sk_write(s, data, len);\r
536 }\r