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