1 13 14 package org.ejbca.ui.cli; 15 16 import java.beans.XMLDecoder ; 17 import java.io.File ; 18 import java.io.FileInputStream ; 19 20 import org.ejbca.core.model.SecConst; 21 import org.ejbca.core.model.ca.certificateprofiles.CertificateProfile; 22 import org.ejbca.core.model.ca.certificateprofiles.CertificateProfileExistsException; 23 import org.ejbca.core.model.ra.raadmin.EndEntityProfile; 24 import org.ejbca.core.model.ra.raadmin.EndEntityProfileExistsException; 25 26 27 28 33 public class CaImportProfilesCommand extends BaseCaAdminCommand { 34 39 public CaImportProfilesCommand(String [] args) { 40 super(args); 41 } 42 43 49 public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException { 50 try { 51 52 if (args.length < 2) { 53 getOutputStream().println("Usage: CA importprofiles <inpath>"); 54 getOutputStream().print("\n"); 55 return; 56 } 57 58 String inpath = args[1]; 59 60 getOutputStream().println("Importing certificate and entity profiles: "); 61 File inFile = new File (inpath); 62 File [] infiles = inFile.listFiles(); 64 for (int i = 0; i < infiles.length; i++) { 65 getOutputStream().println("Filename:"+infiles[i].getName()); 66 if ( infiles[i].isFile() && ((infiles[i].getName().indexOf("certprofile_") > -1) || (infiles[i].getName().indexOf("entityprofile_") > -1)) ) { 67 boolean entityprofile = false; 68 if (infiles[i].getName().indexOf("entityprofile_") > -1) { 69 entityprofile=true; 70 } 71 int index1 = infiles[i].getName().indexOf("_"); 72 int index2 = infiles[i].getName().lastIndexOf("-"); 73 int index3 = infiles[i].getName().lastIndexOf(".xml"); 74 if (index1 < 0 || index2 < 0 || index3 < 0) { 75 getOutputStream().println("Error: Filename not as expected (cert/entityprofile_<name>-<id>.xml)."); 76 } else { 77 String profilename = infiles[i].getName().substring(index1+1,index2); 78 int profileid = Integer.parseInt(infiles[i].getName().substring(index2+1,index3)); 81 if ( (!entityprofile && profileid == SecConst.CERTPROFILE_FIXED_ENDUSER) || 83 (!entityprofile && profileid == SecConst.CERTPROFILE_FIXED_SUBCA) || 84 (!entityprofile && profileid == SecConst.CERTPROFILE_FIXED_ROOTCA) || 85 (!entityprofile && profileid == SecConst.CERTPROFILE_FIXED_HARDTOKENAUTH) || 86 (!entityprofile && profileid == SecConst.CERTPROFILE_FIXED_HARDTOKENAUTHENC) || 87 (!entityprofile && profileid == SecConst.CERTPROFILE_FIXED_HARDTOKENENC) || 88 (!entityprofile && profileid == SecConst.CERTPROFILE_FIXED_HARDTOKENSIGN) ) { 89 getOutputStream().println("Not adding fixed certificate profile '"+profilename+"'."); 90 } else { 91 if (entityprofile && profileid == SecConst.EMPTY_ENDENTITYPROFILE) { 92 getOutputStream().println("Not adding fixed entity profile '"+profilename+"'."); 93 } else { 94 boolean error = false; 96 if (entityprofile) { 97 if (getRaAdminSession().getEndEntityProfileId(administrator, profilename) != SecConst.PROFILE_NO_PROFILE) { 98 getOutputStream().println("Error: Entity profile '"+profilename+"' already exist in database."); 99 error = true; 100 } 101 if (getRaAdminSession().getEndEntityProfile(administrator, profileid) != null) { 102 getOutputStream().println("Error: Entity profileid '"+profileid+"' already exist in database."); 103 error = true; 104 } 105 } else { 106 if (getCertificateStoreSession().getCertificateProfileId(administrator,profilename) != SecConst.PROFILE_NO_PROFILE) { 107 getOutputStream().println("Error: Certificate profile '"+profilename+"' already exist in database."); 108 error = true; 109 } 110 if (getCertificateStoreSession().getCertificateProfile(administrator,profileid) != null) { 111 getOutputStream().println("Error: Certificate profile id '"+profileid+"' already exist in database."); 112 error = true; 113 } 114 } 115 if (!error) { 116 CertificateProfile cprofile = null; 117 EndEntityProfile eprofile = null; 118 FileInputStream is = new FileInputStream (infiles[i]); 119 XMLDecoder decoder = new XMLDecoder ( is ); 120 if (entityprofile) { 121 eprofile = new EndEntityProfile(); 122 eprofile.loadData(decoder.readObject()); 123 try{ 124 getRaAdminSession().addEndEntityProfile(administrator,profileid,profilename,eprofile); 125 getOutputStream().println("Added entity profile '"+profilename+"' to database."); 126 }catch(EndEntityProfileExistsException eepee){ 127 getOutputStream().println("Error: Error adding entity profile '"+profilename+"' to database."); 128 } 129 } else { 130 cprofile = new CertificateProfile(); 131 cprofile.loadData(decoder.readObject()); 132 try{ 133 getCertificateStoreSession().addCertificateProfile(administrator,profileid,profilename,cprofile); 134 getOutputStream().println("Added certificate profile '"+profilename+"' to database."); 135 }catch(CertificateProfileExistsException cpee){ 136 getOutputStream().println("Error: Error adding certificate profile '"+profilename+"' to database."); 137 } 138 } 139 decoder.close(); 140 is.close(); 141 } 142 } 143 } 144 } 145 } 146 } 147 } catch (Exception e) { 148 throw new ErrorAdminCommandException(e); 149 } 150 } 151 } 152 | Popular Tags |