KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > cli > RaKeyRecoverCommand


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13  
14 package org.ejbca.ui.cli;
15
16 import java.math.BigInteger JavaDoc;
17 import java.security.cert.X509Certificate JavaDoc;
18
19 import javax.naming.InitialContext JavaDoc;
20
21 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionHome;
22 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionRemote;
23 import org.ejbca.core.ejb.keyrecovery.IKeyRecoverySessionHome;
24 import org.ejbca.core.ejb.keyrecovery.IKeyRecoverySessionRemote;
25 import org.ejbca.core.model.ra.UserDataVO;
26
27 /**
28  * Find details of a user in the database.
29  *
30  * @version $Id: RaKeyRecoverCommand.java,v 1.5 2006/11/02 08:03:22 anatom Exp $
31  */

32 public class RaKeyRecoverCommand extends BaseRaAdminCommand {
33     /**
34      * Creates a new instance of RaFindUserCommand
35      *
36      * @param args command line arguments
37      */

38     public RaKeyRecoverCommand(String JavaDoc[] args) {
39         super(args);
40     }
41
42     /**
43      * Runs the command
44      *
45      * @throws IllegalAdminCommandException Error in command args
46      * @throws ErrorAdminCommandException Error running command
47      */

48     public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException {
49         try {
50             if (args.length != 3) {
51                 getOutputStream().println("Usage: RA keyrecover <CertificateSN (HEX)> <IssuerDN>");
52
53                 return;
54             }
55
56             //InitialContext jndicontext = new InitialContext();
57
InitialContext JavaDoc jndicontext = getInitialContext();
58
59             Object JavaDoc obj1 = jndicontext.lookup("CertificateStoreSession");
60             ICertificateStoreSessionHome certificatesessionhome = (ICertificateStoreSessionHome) javax.rmi.PortableRemoteObject.narrow(obj1,
61                     ICertificateStoreSessionHome.class);
62             ICertificateStoreSessionRemote certificatesession = certificatesessionhome.create();
63
64             obj1 = jndicontext.lookup("KeyRecoverySession");
65
66             IKeyRecoverySessionHome keyrecoverysessionhome = (IKeyRecoverySessionHome) javax.rmi.PortableRemoteObject.narrow(jndicontext.lookup(
67                         "KeyRecoverySession"), IKeyRecoverySessionHome.class);
68             IKeyRecoverySessionRemote keyrecoverysession = keyrecoverysessionhome.create();
69
70             BigInteger JavaDoc certificatesn = new BigInteger JavaDoc(args[1], 16);
71             String JavaDoc issuerdn = args[2];
72
73              boolean usekeyrecovery = getRaAdminSession().loadGlobalConfiguration(administrator).getEnableKeyRecovery();
74              if(!usekeyrecovery){
75                getOutputStream().println("Keyrecovery have to be enabled in the system configuration in order to use this command.");
76                return;
77              }
78               
79              X509Certificate JavaDoc cert = (X509Certificate JavaDoc) certificatesession.findCertificateByIssuerAndSerno(
80                                                                              administrator, issuerdn,
81                                                                              certificatesn);
82               
83              if(cert == null){
84                getOutputStream().println("Certificate couldn't be found in database.");
85                return;
86              }
87               
88              String JavaDoc username = certificatesession.findUsernameByCertSerno(administrator, certificatesn, issuerdn);
89               
90              if(!keyrecoverysession.existsKeys(administrator,cert)){
91                getOutputStream().println("Specified keys doesn't exist in database.");
92                return;
93              }
94               
95              if(keyrecoverysession.isUserMarked(administrator,username)){
96                getOutputStream().println("User is already marked for recovery.");
97                return;
98              }
99              
100              UserDataVO userdata = getAdminSession().findUser(administrator, username);
101              if(userdata == null){
102                  getOutputStream().println("Error, The user doesn't exist.");
103                  return;
104              }
105   
106              keyrecoverysession.markAsRecoverable(administrator,
107                                                   cert, userdata.getEndEntityProfileId());
108                       
109  
110              getOutputStream().println("Keys corresponding to given certificate has been marked for recovery.");
111
112         } catch (Exception JavaDoc e) {
113             throw new ErrorAdminCommandException(e);
114         }
115     }
116
117     // execute
118
}
119
Popular Tags