Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.common.automate » [javadoc | source]
    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.automate;
   27   
   28   import com.sshtools.common.configuration.Authorization;
   29   
   30   import com.sshtools.j2ssh.transport.publickey.InvalidSshKeyException;
   31   import com.sshtools.j2ssh.transport.publickey.SECSHPublicKeyFormat;
   32   import com.sshtools.j2ssh.transport.publickey.SshPublicKey;
   33   import com.sshtools.j2ssh.transport.publickey.SshPublicKeyFile;
   34   
   35   import org.xml.sax.SAXException;
   36   
   37   import java.io.ByteArrayInputStream;
   38   import java.io.IOException;
   39   
   40   import java.util.Iterator;
   41   import java.util.List;
   42   import java.util.Map;
   43   
   44   import javax.xml.parsers.ParserConfigurationException;
   45   
   46   
   47   /**
   48    *
   49    *
   50    * @author $author$
   51    * @version $Revision: 1.15 $
   52    */
   53   public class SshtoolsAuthorizedKeysFormat implements AuthorizedKeysFormat {
   54       /**
   55   *
   56   *
   57   * @param keys
   58   *
   59   * @return
   60   *
   61   * @throws java.lang.UnsupportedOperationException
   62   */
   63       public byte[] format(AuthorizedKeys keys) {
   64           throw new java.lang.UnsupportedOperationException(
   65               "SSHTools authorized keys format requries seperate key files!");
   66       }
   67   
   68       /**
   69   *
   70   *
   71   * @param formatted
   72   *
   73   * @return
   74   *
   75   * @throws java.lang.UnsupportedOperationException
   76   */
   77       public AuthorizedKeys unformat(byte[] formatted) {
   78           throw new java.lang.UnsupportedOperationException(
   79               "SSHTools authorized keys format requries seperate key files!");
   80       }
   81   
   82       /**
   83   *
   84   *
   85   * @param keys
   86   * @param saver
   87   *
   88   * @return
   89   *
   90   * @throws IOException
   91   * @throws InvalidSshKeyException
   92   */
   93       public byte[] format(AuthorizedKeys keys, AuthorizedKeysFileSaver saver)
   94           throws IOException, InvalidSshKeyException {
   95           Authorization authorization = new Authorization();
   96           SshPublicKeyFile pubfile;
   97           SECSHPublicKeyFormat secsh = new SECSHPublicKeyFormat();
   98           Map.Entry entry;
   99   
  100           for (Iterator it = keys.getAuthorizedKeys().entrySet().iterator();
  101                   (it != null) && it.hasNext();) {
  102               entry = (Map.Entry) it.next();
  103   
  104               // Write out the public key file
  105               String username = (String) entry.getValue();
  106               String filename = username + ".pub";
  107               secsh.setComment(username);
  108               pubfile = SshPublicKeyFile.create((SshPublicKey) entry.getKey(),
  109                       secsh);
  110               saver.saveFile(filename, pubfile.toString().getBytes("US-ASCII"));
  111   
  112               // Write out the key entry
  113               authorization.addKey(filename);
  114           }
  115   
  116           return authorization.toString().getBytes("US-ASCII");
  117       }
  118   
  119       /**
  120   *
  121   *
  122   * @param formatted
  123   * @param loader
  124   *
  125   * @return
  126   *
  127   * @throws IOException
  128   * @throws InvalidSshKeyException
  129   */
  130       public AuthorizedKeys unformat(byte[] formatted,
  131           AuthorizedKeysFileLoader loader)
  132           throws IOException, InvalidSshKeyException {
  133           try {
  134               AuthorizedKeys keys = new AuthorizedKeys();
  135               Authorization authorization = new Authorization(new ByteArrayInputStream(
  136                           formatted));
  137               List keyfiles = authorization.getAuthorizedKeys();
  138               Iterator it = keyfiles.iterator();
  139               String filename;
  140               SshPublicKeyFile pubfile;
  141               String username;
  142   
  143               while (it.hasNext()) {
  144                   filename = (String) it.next();
  145                   pubfile = SshPublicKeyFile.parse(loader.loadFile(filename));
  146                   username = filename.substring(0, filename.length() - 4);
  147                   keys.addKey(username, pubfile.toPublicKey());
  148               }
  149   
  150               return keys;
  151           } catch (ParserConfigurationException ex) {
  152               throw new IOException("Failed to read authorization file: " +
  153                   ex.getMessage());
  154           } catch (SAXException ex) {
  155               throw new IOException("Failed to read authorization file: " +
  156                   ex.getMessage());
  157           }
  158       }
  159   
  160       /**
  161   *
  162   *
  163   * @return
  164   */
  165       public boolean requiresKeyFiles() {
  166           return true;
  167       }
  168   }

Save This Page
Home » j2ssh-0.2.9-src » com.sshtools.common.automate » [javadoc | source]