+++ /dev/null
-/*******************************************************************************\r
- * ============LICENSE_START====================================================\r
- * * org.onap.aaf\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package org.onap.aaf.cadi;\r
-\r
-import java.io.BufferedReader;\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.FileOutputStream;\r
-import java.io.FileReader;\r
-import java.io.IOException;\r
-import java.io.InputStreamReader;\r
-import java.net.InetAddress;\r
-import java.net.UnknownHostException;\r
-import java.security.NoSuchAlgorithmException;\r
-\r
-import org.onap.aaf.cadi.util.Chmod;\r
-import org.onap.aaf.cadi.util.JsonOutputStream;\r
-\r
-\r
-\r
-/**\r
- * A Class to run on command line to determine suitability of environment for certain TAFs.\r
- * \r
- * For instance, CSP supports services only in certain domains, and while dynamic host\r
- * lookups on the machine work in most cases, sometimes, names and IPs are unexpected (and\r
- * invalid) for CSP because of multiple NetworkInterfaces, etc\r
- * \r
- *\r
- */\r
-public class CmdLine {\r
-\r
- /**\r
- * @param args\r
- */\r
- public static void main(String[] args) {\r
- if(args.length>0) {\r
- if("digest".equalsIgnoreCase(args[0]) && (args.length>2 || (args.length>1 && System.console()!=null))) {\r
- String keyfile;\r
- String password;\r
- if(args.length>2) {\r
- password = args[1];\r
- keyfile = args[2];\r
- } else {\r
- keyfile = args[1];\r
- password = new String(System.console().readPassword("Type here (keystrokes hidden): "));\r
- }\r
-\r
- try {\r
- Symm symm;\r
- FileInputStream fis = new FileInputStream(keyfile);\r
- try {\r
- symm = Symm.obtain(fis);\r
- } finally {\r
- fis.close();\r
- }\r
- symm.enpass(password, System.out);\r
- System.out.println();\r
- System.out.flush();\r
- return;\r
- /* testing code... don't want it exposed\r
- System.out.println(" ******** Testing *********");\r
- for(int i=0;i<100000;++i) {\r
- System.out.println(args[1]);\r
- ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
- b64.enpass(args[1], baos);\r
- String pass; \r
- System.out.println(pass=new String(baos.toByteArray()));\r
- ByteArrayOutputStream reconstituted = new ByteArrayOutputStream();\r
- b64.depass(pass, reconstituted);\r
- String r = reconstituted.toString();\r
- System.out.println(r);\r
- if(!r.equals(args[1])) {\r
- System.err.println("!!!!! STOP - ERROR !!!!!");\r
- return;\r
- }\r
- System.out.println();\r
- }\r
- System.out.flush();\r
- */\r
- \r
- } catch (IOException e) {\r
- System.err.println("Cannot digest password");\r
- System.err.println(" \""+ e.getMessage() + '"');\r
- }\r
-// . Oh, well, Deployment services need this behavior. I will put this code in, but leave it undocumented. \r
-// One still needs access to the keyfile to read.\r
-// July 2016 - thought of a tool "CMPass" to reguritate from properties, but only if allowed.\r
- } else if("regurgitate".equalsIgnoreCase(args[0]) && args.length>2) {\r
- try {\r
- Symm symm;\r
- FileInputStream fis = new FileInputStream(args[2]);\r
- try {\r
- symm = Symm.obtain(fis);\r
- } finally {\r
- fis.close();\r
- }\r
- boolean isFile = false;\r
- if("-i".equals(args[1]) || (isFile="-f".equals(args[1]))) {\r
- BufferedReader br;\r
- if(isFile) {\r
- if(args.length<4) {\r
- System.err.println("Filename in 4th position");\r
- return;\r
- }\r
- br = new BufferedReader(new FileReader(args[3]));\r
- } else {\r
- br = new BufferedReader(new InputStreamReader(System.in));\r
- }\r
- try {\r
- String line;\r
- boolean cont = false;\r
- StringBuffer sb = new StringBuffer();\r
- JsonOutputStream jw = new JsonOutputStream(System.out);\r
- while((line=br.readLine())!=null) {\r
- if(cont) {\r
- int end;\r
- if((end=line.indexOf('"'))>=0) {\r
- sb.append(line,0,end);\r
- cont=false;\r
- } else {\r
- sb.append(line);\r
- }\r
- } else {\r
- int idx;\r
- if((idx = line.indexOf(' '))>=0 \r
- && (idx = line.indexOf(' ',++idx))>0\r
- && (idx = line.indexOf('=',++idx))>0\r
- && (idx = line.indexOf('=',++idx))>0\r
- ) {\r
- System.out.println(line.substring(0, idx-5));\r
- int start = idx+2;\r
- int end;\r
- if((end=line.indexOf('"',start))<0) {\r
- end = line.length();\r
- cont = true;\r
- }\r
- sb.append(line,start,end);\r
- }\r
- }\r
- if(sb.length()>0) {\r
- symm.depass(sb.toString(),jw);\r
- if(!cont) {\r
- System.out.println();\r
- }\r
- }\r
- System.out.flush();\r
- sb.setLength(0);\r
- if(!cont) {\r
- jw.resetIndent();\r
- }\r
- }\r
- } finally {\r
- if(isFile) {\r
- br.close();\r
- }\r
- }\r
- } else {\r
- symm.depass(args[1], System.out);\r
- }\r
- System.out.println();\r
- System.out.flush();\r
- return;\r
- } catch (IOException e) {\r
- System.err.println("Cannot regurgitate password");\r
- System.err.println(" \""+ e.getMessage() + '"');\r
- }\r
- } else if("encode64".equalsIgnoreCase(args[0]) && args.length>1) {\r
- try {\r
- Symm.base64.encode(args[1], System.out);\r
- System.out.println();\r
- System.out.flush();\r
- return;\r
- } catch (IOException e) {\r
- System.err.println("Cannot encode Base64 with " + args[1]);\r
- System.err.println(" \""+ e.getMessage() + '"');\r
- }\r
- } else if("decode64".equalsIgnoreCase(args[0]) && args.length>1) {\r
- try {\r
- Symm.base64.decode(args[1], System.out);\r
- System.out.println();\r
- System.out.flush();\r
- return;\r
- } catch (IOException e) {\r
- System.err.println("Cannot decode Base64 text from " + args[1]);\r
- System.err.println(" \""+ e.getMessage() + '"');\r
- }\r
- } else if("encode64url".equalsIgnoreCase(args[0]) && args.length>1) {\r
- try {\r
- Symm.base64url.encode(args[1], System.out);\r
- System.out.println();\r
- System.out.flush();\r
- return;\r
- } catch (IOException e) {\r
- System.err.println("Cannot encode Base64url with " + args[1]);\r
- System.err.println(" \""+ e.getMessage() + '"');\r
- }\r
- } else if("decode64url".equalsIgnoreCase(args[0]) && args.length>1) {\r
- try {\r
- Symm.base64url.decode(args[1], System.out);\r
- System.out.println();\r
- System.out.flush();\r
- return;\r
- } catch (IOException e) {\r
- System.err.println("Cannot decode Base64url text from " + args[1]);\r
- System.err.println(" \""+ e.getMessage() + '"');\r
- }\r
- } else if("md5".equalsIgnoreCase(args[0]) && args.length>1) {\r
- try {\r
- System.out.println(Hash.encryptMD5asStringHex(args[1]));\r
- System.out.flush();\r
- } catch (NoSuchAlgorithmException e) {\r
- System.err.println("Cannot hash MD5 from " + args[1]);\r
- System.err.println(" \""+ e.getMessage() + '"');\r
- }\r
- return;\r
- } else if("sha256".equalsIgnoreCase(args[0]) && args.length>1) {\r
- try {\r
- if(args.length>2) {\r
- int salt = Integer.parseInt(args[2]);\r
- System.out.println(Hash.hashSHA256asStringHex(args[1],salt));\r
- } else { \r
- System.out.println(Hash.hashSHA256asStringHex(args[1]));\r
- }\r
- } catch (NoSuchAlgorithmException e) {\r
- System.err.println("Cannot hash SHA256 text from " + args[1]);\r
- System.err.println(" \""+ e.getMessage() + '"');\r
- }\r
- System.out.flush();\r
- return;\r
- } else if("keygen".equalsIgnoreCase(args[0])) {\r
- try {\r
- if(args.length>1) {\r
- File f = new File(args[1]);\r
- FileOutputStream fos = new FileOutputStream(f);\r
- try {\r
- fos.write(Symm.baseCrypt().keygen());\r
- fos.flush();\r
- } finally {\r
- fos.close();\r
- Chmod.to400.chmod(f);\r
- }\r
- } else {\r
- // create a Symmetric Key out of same characters found in base64\r
- System.out.write(Symm.baseCrypt().keygen());\r
- System.out.flush();\r
- }\r
- return;\r
- } catch (IOException e) {\r
- System.err.println("Cannot create a key " + args[0]);\r
- System.err.println(" \""+ e.getMessage() + '"');\r
- }\r
- \r
- } else if("passgen".equalsIgnoreCase(args[0])) {\r
- int numDigits;\r
- if(args.length <= 1) {\r
- numDigits = 24;\r
- } else {\r
- numDigits = Integer.parseInt(args[1]); \r
- if(numDigits<8)numDigits = 8;\r
- }\r
- String pass;\r
- boolean noLower,noUpper,noDigits,noSpecial,repeats;\r
- do {\r
- pass = Symm.randomGen(numDigits);\r
- noLower=noUpper=noDigits=noSpecial=true;\r
- repeats=false;\r
- int c=-1,last;\r
- for(int i=0;i<numDigits;++i) {\r
- last = c;\r
- c = pass.charAt(i);\r
- if(c==last) {\r
- repeats=true;\r
- break;\r
- }\r
- \r
- if(noLower) {\r
- noLower=!(c>=0x61 && c<=0x7A);\r
- continue;\r
- } \r
- if(noUpper) {\r
- noUpper=!(c>=0x41 && c<=0x5A);\r
- continue;\r
- } \r
- if(noDigits) {\r
- noDigits=!(c>=0x30 && c<=0x39);\r
- continue;\r
- } \r
- if(noSpecial) {\r
- noSpecial = "+!@#$%^&*(){}[]?:;,.".indexOf(c)<0;\r
- continue;\r
- } \r
- \r
- break;\r
- }\r
- } while(noLower || noUpper || noDigits || noSpecial || repeats);\r
- System.out.println(pass.substring(0,numDigits));\r
- } else if("urlgen".equalsIgnoreCase(args[0])) {\r
- int numDigits;\r
- if(args.length < 1) {\r
- numDigits = 24;\r
- } else {\r
- numDigits = Integer.parseInt(args[1]); \r
- }\r
- System.out.println(Symm.randomGen(Symm.base64url.codeset, numDigits).substring(0,numDigits));\r
- \r
- } else if("csptest".equalsIgnoreCase(args[0])) {\r
- try {\r
- System.out.println("CSP Compatibility test");\r
- \r
- String hostName = InetAddress.getLocalHost().getCanonicalHostName();\r
- \r
- System.out.println(" Your automatic hostname is reported as \"" + hostName + "\"\n");\r
- System.out.flush();\r
- return;\r
- } catch (UnknownHostException e) {\r
- e.printStackTrace(System.err);\r
- }\r
- }\r
- } else {\r
- System.out.println("Usage: java -jar <this jar> ...");\r
- System.out.println(" keygen [<keyfile>] (Generates Key on file, or Std Out)");\r
- System.out.println(" digest <keyfile> (Encrypts to Key with \"keyfile\")");\r
- System.out.println(" passgen <digits> (Generate Password of given size)");\r
- System.out.println(" urlgen <digits> (Generate URL field of given size)");\r
- System.out.println(" csptest (Tests for CSP compatibility)");\r
- System.out.println(" encode64 <your text> (Encodes to Base64)");\r
- System.out.println(" decode64 <base64 encoded text> (Decodes from Base64)");\r
- System.out.println(" encode64url <your text> (Encodes to Base64 URL charset)");\r
- System.out.println(" decode64url <base64url encoded text> (Decodes from Base64 URL charset)");\r
- System.out.println(" sha256 <text> (Digest String into SHA256 Hash)");\r
- System.out.println(" md5 <text> (Digest String into MD5 Hash)");\r
- }\r
- System.exit(1);\r
- }\r
-\r
-}\r