1 13 14 package org.ejbca.core.protocol.ws.client; 15 16 17 import java.util.Iterator ; 18 import java.util.List ; 19 20 import org.ejbca.core.model.SecConst; 21 import org.ejbca.core.model.ra.UserDataConstants; 22 import org.ejbca.core.protocol.ws.client.gen.AuthorizationDeniedException_Exception; 23 import org.ejbca.core.protocol.ws.client.gen.UserDataVOWS; 24 import org.ejbca.core.protocol.ws.client.gen.UserMatch; 25 import org.ejbca.ui.cli.ErrorAdminCommandException; 28 import org.ejbca.ui.cli.IAdminCommand; 29 import org.ejbca.ui.cli.IllegalAdminCommandException; 30 31 36 public class FindUserCommand extends EJBCAWSRABaseCommand implements IAdminCommand{ 37 38 39 private static final int ARG_MATCHWITH = 1; 40 private static final int ARG_MATCHTYPE = 2; 41 private static final int ARG_MATCHVALUE = 3; 42 43 44 45 46 private static final String [] MATCHWITHTEXTS = {"USERNAME","EMAIL", "STATUS", "ENDENTITYPROFILE", 47 "CERTIFICATEPROFILE", "CA", "TOKEN", "DN", "UID", 48 "COMMONNAME","DNSERIALNUMBER","GIVENNAME","INITIALS", 49 "SURNAME","TITLE","ORGANIZATIONUNIT","ORGANIZATION", 50 "LOCALE","STATE","DOMAINCOMPONENT","COUNTRY"}; 51 52 private static final int[] MATCHWITHVALUES = { org.ejbca.util.query.UserMatch.MATCH_WITH_USERNAME, 53 org.ejbca.util.query.UserMatch.MATCH_WITH_EMAIL, 54 org.ejbca.util.query.UserMatch.MATCH_WITH_STATUS, 55 org.ejbca.util.query.UserMatch.MATCH_WITH_ENDENTITYPROFILE, 56 org.ejbca.util.query.UserMatch.MATCH_WITH_CERTIFICATEPROFILE, 57 org.ejbca.util.query.UserMatch.MATCH_WITH_CA, 58 org.ejbca.util.query.UserMatch.MATCH_WITH_TOKEN, 59 org.ejbca.util.query.UserMatch.MATCH_WITH_DN, 60 org.ejbca.util.query.UserMatch.MATCH_WITH_UID, 61 org.ejbca.util.query.UserMatch.MATCH_WITH_COMMONNAME, 62 org.ejbca.util.query.UserMatch.MATCH_WITH_DNSERIALNUMBER, 63 org.ejbca.util.query.UserMatch.MATCH_WITH_GIVENNAME, 64 org.ejbca.util.query.UserMatch.MATCH_WITH_INITIALS, 65 org.ejbca.util.query.UserMatch.MATCH_WITH_SURNAME, 66 org.ejbca.util.query.UserMatch.MATCH_WITH_TITLE, 67 org.ejbca.util.query.UserMatch.MATCH_WITH_ORGANIZATIONUNIT, 68 org.ejbca.util.query.UserMatch.MATCH_WITH_ORGANIZATION, 69 org.ejbca.util.query.UserMatch.MATCH_WITH_LOCALE, 70 org.ejbca.util.query.UserMatch.MATCH_WITH_STATE, 71 org.ejbca.util.query.UserMatch.MATCH_WITH_DOMAINCOMPONENT, 72 org.ejbca.util.query.UserMatch.MATCH_WITH_COUNTRY}; 73 74 private static final String [] STATUS_TEXTS = {"NEW", "FAILED","INITIALIZED", 75 "INPROCESS","GENERATED","REVOKED", 76 "HISTORICAL","KEYRECOVERY"}; 77 private static final int[] STATUS_VALUES = {UserDataConstants.STATUS_NEW, UserDataConstants.STATUS_FAILED, UserDataConstants.STATUS_INITIALIZED, 78 UserDataConstants.STATUS_INPROCESS, UserDataConstants.STATUS_GENERATED, 79 UserDataConstants.STATUS_REVOKED, UserDataConstants.STATUS_HISTORICAL, 80 UserDataConstants.STATUS_KEYRECOVERY}; 81 82 87 public FindUserCommand(String [] args) { 88 super(args); 89 } 90 91 97 public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException { 98 99 try { 100 101 if(args.length != 4){ 102 usage(); 103 System.exit(-1); 104 } 105 106 int matchwith = getMatchWith(args[ARG_MATCHWITH]); 107 int matchtype = getMatchType(args[ARG_MATCHTYPE], matchwith); 108 String matchvalue = args[ARG_MATCHVALUE]; 109 110 111 try{ 112 UserMatch match = new UserMatch(); 113 match.setMatchtype(matchtype); 114 match.setMatchvalue(matchvalue); 115 match.setMatchwith(matchwith); 116 117 List <UserDataVOWS> result = getEjbcaRAWS().findUser(match); 118 119 Iterator iter = result.iterator(); 120 if(result==null || result.size() == 0){ 121 getPrintStream().println("No matching users could be found in database"); 122 }else{ 123 getPrintStream().println("The following users found in database :"); 124 for(int i=0;i<result.size();i++){ 125 UserDataVOWS next = (UserDataVOWS) iter.next(); 126 getPrintStream().println("\nUser : " + (i +1)); 127 getPrintStream().println(" Username: "+next.getUsername()); 128 getPrintStream().println(" Subject DN: "+next.getSubjectDN()); 129 if(next.getSubjectAltName() == null){ 130 getPrintStream().println(" Subject Altname: NONE"); 131 }else{ 132 getPrintStream().println(" Subject Altname: "+next.getSubjectAltName()); 133 } 134 if(next.getEmail() == null){ 135 getPrintStream().println(" Email: NONE"); 136 }else{ 137 getPrintStream().println(" Email: "+next.getEmail()); 138 } 139 getPrintStream().println(" CA Name: "+next.getCaName()); 140 getPrintStream().println(" Type: "+getType(next)); 141 getPrintStream().println(" Token: "+next.getTokenType()); 142 getPrintStream().println(" Status: "+ getStatus(next.getStatus())); 143 getPrintStream().println(" Certificate profile: "+next.getCertificateProfileName()); 144 getPrintStream().println(" End entity profile: "+next.getEndEntityProfileName()); 145 if(next.getHardTokenIssuerName() == null){ 146 getPrintStream().println(" Hard Token Issuer Alias: NONE"); 147 }else{ 148 getPrintStream().println(" Hard Token Issuer Alias: " + next.getHardTokenIssuerName()); 149 } 150 } 151 } 152 153 }catch(AuthorizationDeniedException_Exception e){ 154 getPrintStream().println("Error : " + e.getMessage()); 155 } 156 } catch (Exception e) { 157 throw new ErrorAdminCommandException(e); 158 } 159 } 160 161 private int getMatchType(String matchtype, int matchwith) { 162 if(matchtype.equalsIgnoreCase("EQUALS")){ 163 if(matchwith >= org.ejbca.util.query.UserMatch.MATCH_WITH_UID){ 164 getPrintStream().println("Error: Matchtype 'EQUALS' cannot be used with DN fields, only 'BEGINSWITH' can be used."); 165 usage(); 166 System.exit(-1); 167 }else{ 168 return org.ejbca.util.query.UserMatch.MATCH_TYPE_EQUALS; 169 } 170 } 171 if(matchtype.equalsIgnoreCase("BEGINSWITH")){ 172 if(matchwith > org.ejbca.util.query.UserMatch.MATCH_WITH_UID || matchwith == org.ejbca.util.query.UserMatch.MATCH_WITH_DN 173 || matchwith == org.ejbca.util.query.UserMatch.MATCH_WITH_EMAIL){ 174 return org.ejbca.util.query.UserMatch.MATCH_TYPE_BEGINSWITH; 175 }else{ 176 getPrintStream().println("Error: Matchtype 'BEGINSWITH' can only be used with DN fields, full DN and EMAIL, use EQUALS"); 177 usage(); 178 System.exit(-1); 179 } 180 } 181 if(matchtype.equalsIgnoreCase("CONTAINS")){ 182 if(matchwith == org.ejbca.util.query.UserMatch.MATCH_WITH_DN){ 183 return org.ejbca.util.query.UserMatch.MATCH_TYPE_BEGINSWITH; 184 }else{ 185 getPrintStream().println("Error: Matchtype 'CONTAINS' can only be used with matchwith 'DN'."); 186 usage(); 187 System.exit(-1); 188 } 189 } 190 usage(); 191 System.exit(-1); 192 return 0; } 194 195 private int getMatchWith(String matchwith) { 196 for(int i=0;i< MATCHWITHTEXTS.length; i++){ 197 if(MATCHWITHTEXTS[i].equalsIgnoreCase(matchwith)){ 198 return MATCHWITHVALUES[i]; 199 } 200 } 201 usage(); 202 203 System.exit(-1); 204 return 0; } 206 207 private String getStatus(int status){ 208 for(int i=0;i<STATUS_VALUES.length;i++){ 209 if(STATUS_VALUES[i] == status){ 210 return STATUS_TEXTS[i]; 211 } 212 } 213 return "ERROR : Status text not found"; 214 } 215 216 private int getType(UserDataVOWS userData) { 217 int type = 1; 218 219 if(userData.isSendNotification()) 220 type = type | SecConst.USER_SENDNOTIFICATION; 221 else 222 type = type & (~SecConst.USER_SENDNOTIFICATION); 223 224 if(userData.isKeyRecoverable()) 225 type = type | SecConst.USER_KEYRECOVERABLE; 226 else 227 type = type & (~SecConst.USER_KEYRECOVERABLE); 228 229 return type; 230 } 231 232 protected void usage() { 233 getPrintStream().println("Command used to find userdata, maximum of 100 users will be returned "); 234 getPrintStream().println("Usage : finduser <matchwith> <matchtype> <value> \n\n"); 235 getPrintStream().print("Matchwith can be : "); 236 for(int i=0;i<MATCHWITHTEXTS.length-1;i++){ 237 getPrintStream().print(MATCHWITHTEXTS[i] +", "); 238 } 239 getPrintStream().println(MATCHWITHTEXTS[MATCHWITHTEXTS.length-1]); 240 getPrintStream().println("Matchtype can be : EQUALS (not DN fields), BEGINSWITH (DN fields), CONTAINS (matchwith complete DN only)"); 241 } 242 243 244 } 245 | Popular Tags |