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.OpenSSHPublicKeyFormat;
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 OpenSSHAuthorizedKeysFormat implements
049: AuthorizedKeysFormat {
050: /**
051: *
052: *
053: * @param keys
054: * @param saver
055: *
056: * @return
057: *
058: * @throws IOException
059: * @throws InvalidSshKeyException
060: * @throws UnsupportedOperationException
061: */
062: public byte[] format(AuthorizedKeys keys,
063: AuthorizedKeysFileSaver saver) throws IOException,
064: InvalidSshKeyException {
065: throw new UnsupportedOperationException(
066: "The OpenSSH authorized key file does not support additional key files!");
067: }
068:
069: /**
070: *
071: *
072: * @param formatted
073: * @param loader
074: *
075: * @return
076: *
077: * @throws IOException
078: * @throws InvalidSshKeyException
079: * @throws UnsupportedOperationException
080: */
081: public AuthorizedKeys unformat(byte[] formatted,
082: AuthorizedKeysFileLoader loader) throws IOException,
083: InvalidSshKeyException {
084: throw new UnsupportedOperationException(
085: "The OpenSSH authorized key file does not support additional key files!");
086: }
087:
088: /**
089: *
090: *
091: * @param keys
092: *
093: * @return
094: *
095: * @throws IOException
096: * @throws InvalidSshKeyException
097: */
098: public byte[] format(AuthorizedKeys keys) throws IOException,
099: InvalidSshKeyException {
100: ByteArrayOutputStream out = new ByteArrayOutputStream();
101: SshPublicKeyFile pubfile;
102: OpenSSHPublicKeyFormat openssh = new OpenSSHPublicKeyFormat();
103: Map.Entry entry;
104:
105: for (Iterator it = keys.getAuthorizedKeys().entrySet()
106: .iterator(); (it != null) && it.hasNext();) {
107: entry = (Map.Entry) it.next();
108: openssh.setComment((String) entry.getValue());
109: pubfile = SshPublicKeyFile.create((SshPublicKey) entry
110: .getKey(), openssh);
111: out.write(pubfile.toString().getBytes("US-ASCII"));
112: out.write('\n');
113: }
114:
115: return out.toByteArray();
116: }
117:
118: /**
119: *
120: *
121: * @param formatted
122: *
123: * @return
124: *
125: * @throws IOException
126: * @throws InvalidSshKeyException
127: */
128: public AuthorizedKeys unformat(byte[] formatted)
129: throws IOException, InvalidSshKeyException {
130: AuthorizedKeys keys = new AuthorizedKeys();
131: BufferedReader reader = new BufferedReader(
132: new InputStreamReader(new ByteArrayInputStream(
133: formatted)));
134: String line;
135: SshPublicKeyFile pubfile;
136:
137: while ((line = reader.readLine()) != null) {
138: pubfile = SshPublicKeyFile.parse(line.getBytes("US-ASCII"));
139: keys.addKey(pubfile.getComment(), pubfile.toPublicKey());
140: }
141:
142: return keys;
143: }
144:
145: /**
146: *
147: *
148: * @return
149: */
150: public boolean requiresKeyFiles() {
151: return false;
152: }
153: }
|