001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.util;
027:
028: import com.sshtools.j2ssh.configuration.ConfigurationLoader;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: import java.io.IOException;
034: import java.io.InputStream;
035: import java.io.OutputStream;
036:
037: /**
038: *
039: *
040: * @author $author$
041: * @version $Revision: 1.13 $
042: */
043: public class X11Util {
044: // Logger
045:
046: /** */
047: protected static Log log = LogFactory.getLog(X11Util.class);
048: static byte[] table = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
049: 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
050:
051: /**
052: *
053: *
054: * @param displayNumber
055: *
056: * @return
057: *
058: * @throws IOException
059: */
060: public static String getCookie(int displayNumber)
061: throws IOException {
062: log.debug("Getting cookie for " + displayNumber
063: + " using xauth");
064:
065: Process process = null;
066: InputStream in = null;
067: InputStream err = null;
068: OutputStream out = null;
069:
070: // try {
071: byte[] foo = new byte[16];
072: ConfigurationLoader.getRND().nextBytes(foo);
073:
074: byte[] bar = new byte[32];
075:
076: for (int i = 0; i < 16; i++) {
077: bar[2 * i] = table[(foo[i] >>> 4) & 0xf];
078: bar[(2 * i) + 1] = table[(foo[i]) & 0xf];
079: }
080:
081: return new String(bar);
082:
083: /* String cmd = "xauth list :" + displayNumber;
084: log.debug("Executing " + cmd);
085: process = Runtime.getRuntime().exec(cmd);
086: IOStreamConnector connect = new IOStreamConnector(
087: err = process.getErrorStream(), System.out);
088: BufferedReader reader = new BufferedReader(
089: new InputStreamReader(in = process.getInputStream()));
090: out = process.getOutputStream();
091: String line = null;
092: String cookie = null;
093: while( ( line = reader.readLine() ) != null) {
094: log.debug(line);
095: StringTokenizer t = new StringTokenizer(line);
096: try {
097: String host = t.nextToken();
098: String type = t.nextToken();
099: String value = t.nextToken();
100: if(cookie == null) {
101: cookie = value;
102: log.debug("Using cookie " + cookie);
103: }
104: }
105: catch(Exception e) {
106: log.error("Unexpected response from xauth.", e);
107: }
108: }
109: return cookie;
110: }
111: finally {
112: IOUtil.closeStream(in);
113: IOUtil.closeStream(err);
114: IOUtil.closeStream(out);
115: }*/
116: }
117:
118: /**
119: *
120: *
121: * @param displayNumber
122: *
123: * @return
124: */
125: public static String createCookie(String displayNumber) {
126: log.error("Creating fake cookie");
127:
128: StringBuffer b = new StringBuffer();
129:
130: for (int i = 0; i < 16; i++) {
131: int r = (int) (Math.random() * 256);
132: String h = Integer.toHexString(r);
133:
134: if (h.length() == 1) {
135: b.append(0);
136: }
137:
138: b.append(h);
139: }
140:
141: log.error("Fake cookie is " + b.toString());
142:
143: return b.toString();
144: }
145: }
|