1 /*
2 * SSHTools - Java SSH2 API
3 *
4 * Copyright (C) 2002-2003 Lee David Painter and Contributors.
5 *
6 * Contributions made by:
7 *
8 * Brett Smith
9 * Richard Pernavas
10 * Erwin Bolwidt
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 */
26 package com.sshtools.common.util;
27
28 import com.sshtools.j2ssh.configuration.ConfigurationLoader;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.OutputStream;
36
37
38 /**
39 *
40 *
41 * @author $author$
42 * @version $Revision: 1.13 $
43 */
44 public class X11Util {
45 // Logger
46
47 /** */
48 protected static Log log = LogFactory.getLog(X11Util.class);
49 static byte[] table = {
50 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62,
51 0x63, 0x64, 0x65, 0x66
52 };
53
54 /**
55 *
56 *
57 * @param displayNumber
58 *
59 * @return
60 *
61 * @throws IOException
62 */
63 public static String getCookie(int displayNumber) throws IOException {
64 log.debug("Getting cookie for " + displayNumber + " using xauth");
65
66 Process process = null;
67 InputStream in = null;
68 InputStream err = null;
69 OutputStream out = null;
70
71 // try {
72 byte[] foo = new byte[16];
73 ConfigurationLoader.getRND().nextBytes(foo);
74
75 byte[] bar = new byte[32];
76
77 for (int i = 0; i < 16; i++) {
78 bar[2 * i] = table[(foo[i] >>> 4) & 0xf];
79 bar[(2 * i) + 1] = table[(foo[i]) & 0xf];
80 }
81
82 return new String(bar);
83
84 /* String cmd = "xauth list :" + displayNumber;
85 log.debug("Executing " + cmd);
86 process = Runtime.getRuntime().exec(cmd);
87 IOStreamConnector connect = new IOStreamConnector(
88 err = process.getErrorStream(), System.out);
89 BufferedReader reader = new BufferedReader(
90 new InputStreamReader(in = process.getInputStream()));
91 out = process.getOutputStream();
92 String line = null;
93 String cookie = null;
94 while( ( line = reader.readLine() ) != null) {
95 log.debug(line);
96 StringTokenizer t = new StringTokenizer(line);
97 try {
98 String host = t.nextToken();
99 String type = t.nextToken();
100 String value = t.nextToken();
101 if(cookie == null) {
102 cookie = value;
103 log.debug("Using cookie " + cookie);
104 }
105 }
106 catch(Exception e) {
107 log.error("Unexpected response from xauth.", e);
108 }
109 }
110 return cookie;
111 }
112 finally {
113 IOUtil.closeStream(in);
114 IOUtil.closeStream(err);
115 IOUtil.closeStream(out);
116 }*/
117 }
118
119 /**
120 *
121 *
122 * @param displayNumber
123 *
124 * @return
125 */
126 public static String createCookie(String displayNumber) {
127 log.error("Creating fake cookie");
128
129 StringBuffer b = new StringBuffer();
130
131 for (int i = 0; i < 16; i++) {
132 int r = (int) (Math.random() * 256);
133 String h = Integer.toHexString(r);
134
135 if (h.length() == 1) {
136 b.append(0);
137 }
138
139 b.append(h);
140 }
141
142 log.error("Fake cookie is " + b.toString());
143
144 return b.toString();
145 }
146 }