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.automate;
027:
028: import com.sshtools.j2ssh.transport.publickey.InvalidSshKeyException;
029: import com.sshtools.j2ssh.transport.publickey.SECSHPublicKeyFormat;
030: import com.sshtools.j2ssh.transport.publickey.SshPublicKey;
031: import com.sshtools.j2ssh.transport.publickey.SshPublicKeyFile;
032:
033: import java.io.BufferedReader;
034: import java.io.ByteArrayInputStream;
035: import java.io.ByteArrayOutputStream;
036: import java.io.IOException;
037: import java.io.InputStreamReader;
038:
039: import java.util.Iterator;
040: import java.util.Map;
041:
042: /**
043: *
044: *
045: * @author $author$
046: * @version $Revision: 1.14 $
047: */
048: public class SSH2AuthorizedKeysFormat implements AuthorizedKeysFormat {
049: private static final String header = "# Open3SP auto-generated authorization file\n";
050: private static final String key = "key ";
051:
052: /**
053: *
054: *
055: * @param keys
056: *
057: * @return
058: *
059: * @throws IOException
060: * @throws InvalidSshKeyException
061: * @throws java.lang.UnsupportedOperationException
062: */
063: public byte[] format(AuthorizedKeys keys) throws IOException,
064: InvalidSshKeyException {
065: throw new java.lang.UnsupportedOperationException(
066: "SSH2 authorized keys format requries seperate key files!");
067: }
068:
069: /**
070: *
071: *
072: * @param formatted
073: *
074: * @return
075: *
076: * @throws IOException
077: * @throws InvalidSshKeyException
078: * @throws java.lang.UnsupportedOperationException
079: */
080: public AuthorizedKeys unformat(byte[] formatted)
081: throws IOException, InvalidSshKeyException {
082: throw new java.lang.UnsupportedOperationException(
083: "SSH2 authorized keys format requries seperate key files!");
084: }
085:
086: /**
087: *
088: *
089: * @param keys
090: * @param saver
091: *
092: * @return
093: *
094: * @throws IOException
095: * @throws InvalidSshKeyException
096: */
097: public byte[] format(AuthorizedKeys keys,
098: AuthorizedKeysFileSaver saver) throws IOException,
099: InvalidSshKeyException {
100: ByteArrayOutputStream out = new ByteArrayOutputStream();
101: out.write(header.getBytes("US-ASCII"));
102:
103: SshPublicKeyFile pubfile;
104: SECSHPublicKeyFormat secsh = new SECSHPublicKeyFormat();
105: Map.Entry entry;
106:
107: for (Iterator it = keys.getAuthorizedKeys().entrySet()
108: .iterator(); (it != null) && it.hasNext();) {
109: entry = (Map.Entry) it.next();
110:
111: // Write out the public key file
112: String username = (String) entry.getValue();
113: String filename = username + ".pub";
114: secsh.setComment(username);
115: pubfile = SshPublicKeyFile.create((SshPublicKey) entry
116: .getKey(), secsh);
117: saver.saveFile(filename, pubfile.toString().getBytes(
118: "US-ASCII"));
119:
120: // Write out the key entry
121: out.write(key.getBytes("US-ASCII"));
122: out.write(filename.getBytes("US-ASCII"));
123: out.write('\n');
124: }
125:
126: return out.toByteArray();
127: }
128:
129: /**
130: *
131: *
132: * @param formatted
133: * @param loader
134: *
135: * @return
136: *
137: * @throws IOException
138: * @throws InvalidSshKeyException
139: */
140: public AuthorizedKeys unformat(byte[] formatted,
141: AuthorizedKeysFileLoader loader) throws IOException,
142: InvalidSshKeyException {
143: AuthorizedKeys keys = new AuthorizedKeys();
144: BufferedReader reader = new BufferedReader(
145: new InputStreamReader(new ByteArrayInputStream(
146: formatted)));
147: String line;
148: SshPublicKeyFile pubfile;
149: String filename;
150: String username;
151:
152: while ((line = reader.readLine()) != null) {
153: if (line.trim().startsWith("key")) {
154: // Get the filename and load
155: filename = line.substring(
156: line.trim().lastIndexOf(" ") + 1).trim();
157: pubfile = SshPublicKeyFile.parse(loader
158: .loadFile(filename));
159:
160: // Get the username from the filename - .pub
161: username = filename.substring(0, filename.length() - 4);
162: keys.addKey(username, pubfile.toPublicKey());
163: }
164: }
165:
166: return keys;
167: }
168:
169: /**
170: *
171: *
172: * @return
173: */
174: public boolean requiresKeyFiles() {
175: return true;
176: }
177: }
|