1 13 14 package org.ejbca.core.model.ca.publisher; 15 16 import java.io.BufferedReader ; 17 import java.io.File ; 18 import java.io.FileNotFoundException ; 19 import java.io.FileOutputStream ; 20 import java.io.IOException ; 21 import java.io.InputStreamReader ; 22 import java.security.cert.Certificate ; 23 import java.security.cert.CertificateEncodingException ; 24 import java.util.Properties ; 25 26 import org.apache.log4j.Logger; 27 import org.ejbca.core.ejb.ca.store.CertificateDataBean; 28 import org.ejbca.core.model.InternalResources; 29 import org.ejbca.core.model.log.Admin; 30 import org.ejbca.core.model.ra.ExtendedInformation; 31 32 37 public class GeneralPurposeCustomPublisher implements ICustomPublisher{ 38 private static Logger log = Logger.getLogger(GeneralPurposeCustomPublisher.class); 39 private static final InternalResources intres = InternalResources.getInstance(); 40 41 public static final String crlExternalCommandPropertyName = "crl.application"; 42 public static final String certExternalCommandPropertyName = "cert.application"; 43 public static final String revokeExternalCommandPropertyName = "revoke.application"; 44 public static final String crlFailOnErrorCodePropertyName = "crl.failOnErrorCode"; 45 public static final String certFailOnErrorCodePropertyName = "cert.failOnErrorCode"; 46 public static final String revokeFailOnErrorCodePropertyName = "revoke.failOnErrorCode"; 47 public static final String crlFailOnStandardErrorPropertyName = "crl.failOnStandardError"; 48 public static final String certFailOnStandardErrorPropertyName = "cert.failOnStandardError"; 49 public static final String revokeFailOnStandardErrorPropertyName = "revoke.failOnStandardError"; 50 51 private String crlExternalCommandFileName = null; 52 private String certExternalCommandFileName = null; 53 private String revokeExternalCommandFileName = null; 54 private boolean crlFailOnErrorCode = true; 55 private boolean certFailOnErrorCode = true; 56 private boolean revokeFailOnErrorCode = true; 57 private boolean crlFailOnStandardError = true; 58 private boolean certFailOnStandardError = true; 59 private boolean revokeFailOnStandardError = true; 60 61 64 public GeneralPurposeCustomPublisher() {} 65 66 72 public void init(Properties properties) { 73 log.debug("Initializing GeneralPurposeCustomPublisher"); 74 if ( properties.getProperty(crlFailOnErrorCodePropertyName) != null ) { 76 crlFailOnErrorCode = properties.getProperty(crlFailOnErrorCodePropertyName).equalsIgnoreCase("true"); 77 } 78 if ( properties.getProperty(crlFailOnStandardErrorPropertyName) != null ) { 79 crlFailOnStandardError = properties.getProperty(crlFailOnStandardErrorPropertyName).equalsIgnoreCase("true"); 80 } 81 if ( properties.getProperty(crlExternalCommandPropertyName) != null ) { 82 crlExternalCommandFileName = properties.getProperty(crlExternalCommandPropertyName); 83 } 84 if ( properties.getProperty(certFailOnErrorCodePropertyName) != null ) { 85 certFailOnErrorCode = properties.getProperty(certFailOnErrorCodePropertyName).equalsIgnoreCase("true"); 86 } 87 if ( properties.getProperty(certFailOnStandardErrorPropertyName) != null ) { 88 certFailOnStandardError = properties.getProperty(certFailOnStandardErrorPropertyName).equalsIgnoreCase("true"); 89 } 90 if ( properties.getProperty(certExternalCommandPropertyName) != null ) { 91 certExternalCommandFileName = properties.getProperty(certExternalCommandPropertyName); 92 } 93 if ( properties.getProperty(revokeFailOnErrorCodePropertyName) != null ) { 94 revokeFailOnErrorCode = properties.getProperty(revokeFailOnErrorCodePropertyName).equalsIgnoreCase("true"); 95 } 96 if ( properties.getProperty(revokeFailOnStandardErrorPropertyName) != null ) { 97 revokeFailOnStandardError = properties.getProperty(revokeFailOnStandardErrorPropertyName).equalsIgnoreCase("true"); 98 } 99 if ( properties.getProperty(revokeExternalCommandPropertyName) != null ) { 100 revokeExternalCommandFileName = properties.getProperty(revokeExternalCommandPropertyName); 101 } 102 } 104 114 public boolean storeCertificate(Admin admin, Certificate incert, String username, String password, String cafp, int status, int type, long revocationDate, int revocationReason, ExtendedInformation extendedinformation) throws PublisherException { 115 log.debug(">storeCertificate, Storing Certificate for user: " + username); 116 if (status != CertificateDataBean.CERT_ACTIVE) { 118 return true; 119 } 120 if ( certExternalCommandFileName == null ) { 122 String msg = intres.getLocalizedMessage("publisher.errormissingproperty", certExternalCommandPropertyName); 123 log.error(msg); 124 throw new PublisherException(msg); 125 } 126 try { 128 runWithTempFile(certExternalCommandFileName, incert.getEncoded(), certFailOnErrorCode, certFailOnStandardError, String.valueOf(type)); 129 } catch (CertificateEncodingException e) { 130 String msg = intres.getLocalizedMessage("publisher.errorcertconversion"); 131 log.error(msg); 132 throw new PublisherException(msg); 133 } 134 return true; 135 } 137 144 public boolean storeCRL(Admin admin, byte[] incrl, String cafp, int number) throws PublisherException { 145 log.debug(">storeCRL, Storing CRL"); 146 if ( crlExternalCommandFileName == null ) { 148 String msg = intres.getLocalizedMessage("publisher.errormissingproperty", crlExternalCommandPropertyName); 149 log.error(msg); 150 throw new PublisherException(msg); 151 } 152 runWithTempFile(crlExternalCommandFileName, incrl, crlFailOnErrorCode, crlFailOnStandardError, null); 154 return true; 155 } 156 157 165 public void revokeCertificate(Admin admin, Certificate cert, int reason) throws PublisherException { 166 log.debug(">revokeCertificate, Rekoving Certificate"); 167 if ( revokeExternalCommandFileName == null ) { 169 String msg = intres.getLocalizedMessage("publisher.errormissingproperty", revokeExternalCommandPropertyName); 170 log.error(msg); 171 throw new PublisherException(msg); 172 } 173 try { 175 runWithTempFile(revokeExternalCommandFileName, cert.getEncoded(), revokeFailOnErrorCode, revokeFailOnStandardError, String.valueOf(reason)); 176 } catch (CertificateEncodingException e) { 177 String msg = intres.getLocalizedMessage("publisher.errorcertconversion"); 178 log.error(msg); 179 throw new PublisherException(msg); 180 } 181 } 183 189 public void testConnection(Admin admin) throws PublisherConnectionException { 190 log.debug("testConnection, Testing connection"); 191 if ( crlExternalCommandFileName != null ) { 193 if ( !(new File (crlExternalCommandFileName)).exists() ) { 194 String msg = intres.getLocalizedMessage("publisher.commandnotfound", crlExternalCommandFileName); 195 log.error(msg); 196 throw new PublisherConnectionException(msg); 197 } 198 } 199 if ( certExternalCommandFileName != null ) { 200 if ( !(new File (certExternalCommandFileName)).exists() ) { 201 String msg = intres.getLocalizedMessage("publisher.commandnotfound", certExternalCommandFileName); 202 log.error(msg); 203 throw new PublisherConnectionException(msg); 204 } 205 } 206 if ( revokeExternalCommandFileName != null ) { 207 if ( !(new File (revokeExternalCommandFileName)).exists() ) { 208 String msg = intres.getLocalizedMessage("publisher.commandnotfound", revokeExternalCommandFileName); 209 log.error(msg); 210 throw new PublisherConnectionException(msg); 211 } 212 } 213 } 215 218 protected void finalize() throws Throwable { 219 log.debug(">finalize, doing nothing"); 220 super.finalize(); 221 } 223 235 private void runWithTempFile(String externalCommand, byte[] bytes, boolean failOnCode, boolean failOnOutput, String additionalArguments) throws PublisherException { 236 File tempFile = null; 238 FileOutputStream fos = null; 239 try { 240 tempFile = File.createTempFile("GeneralPurposeCustomPublisher", ".tmp"); 241 fos = new FileOutputStream (tempFile); 242 fos.write(bytes); 243 } catch (FileNotFoundException e) { 245 String msg = intres.getLocalizedMessage("publisher.errortempfile"); 246 log.error(msg, e); 247 throw new PublisherException(msg); 248 } catch (IOException e) { 249 try { 250 fos.close(); 251 } catch (IOException e1) { 252 } 253 tempFile.delete(); 254 String msg = intres.getLocalizedMessage("publisher.errortempfile"); 255 log.error(msg, e); 256 throw new PublisherException(msg); 257 } 258 String tempFileName = null; 260 try { 261 tempFileName = tempFile.getCanonicalPath(); 262 String [] cmdcommand = (externalCommand).split("\\s"); 263 String [] cmdargs; 264 if ( additionalArguments == null ) { 265 String [] cmdargst = { tempFileName }; 266 cmdargs = cmdargst; 267 } else { 268 String [] cmdargst = { tempFileName, additionalArguments }; 269 cmdargs = cmdargst; 270 } 271 String [] cmdarray = new String [cmdcommand.length+cmdargs.length]; 272 System.arraycopy(cmdcommand, 0, cmdarray, 0, cmdcommand.length); 273 System.arraycopy(cmdargs, 0, cmdarray, cmdcommand.length, cmdargs.length); 274 Process externalProcess = Runtime.getRuntime().exec( cmdarray, null, null); 275 BufferedReader stdError = new BufferedReader ( new InputStreamReader ( externalProcess.getErrorStream() ) ); 277 BufferedReader stdInput = new BufferedReader ( new InputStreamReader ( externalProcess.getInputStream() ) ); 278 while ( stdInput.readLine() != null ) { } String stdErrorOutput = null; 280 if ( ((externalProcess.waitFor() != 0) && failOnCode) || (stdError.ready() && failOnOutput )) { 282 tempFile.delete(); 283 String errTemp = null; 284 while ( stdError.ready() && (errTemp = stdError.readLine()) != null ) { 285 if (stdErrorOutput == null) { 286 stdErrorOutput = errTemp; 287 } else { 288 stdErrorOutput += "\n" + errTemp; 289 } 290 } 291 String msg = intres.getLocalizedMessage("publisher.errorexternalapp", externalCommand); 292 if ( stdErrorOutput != null ) { 293 msg += " - " + stdErrorOutput + " - "+ tempFileName; 294 } 295 log.error(msg); 296 throw new PublisherException(msg); 297 } 298 } catch (IOException e) { 299 String msg = intres.getLocalizedMessage("publisher.errorexternalapp", externalCommand); 300 log.error(msg, e); 301 throw new PublisherException(msg); 302 } catch (InterruptedException e) { 303 String msg = intres.getLocalizedMessage("publisher.errorexternalapp", externalCommand); 304 log.error(msg, e); 305 throw new PublisherException(msg); 306 } finally { 307 try { 308 fos.close(); 309 } catch (IOException e1) { 310 } 311 if ( !tempFile.delete() ) { 313 tempFile.deleteOnExit(); 314 log.info( intres.getLocalizedMessage("publisher.errordeletetempfile", tempFileName) ); 315 } 316 } 317 } } | Popular Tags |