89685e29900066aae8ab990d0cbd057d2137b943
[aaf/cadi.git] / core / src / main / java / com / att / cadi / CmdLine.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cadi;\r
24 \r
25 import java.io.BufferedReader;\r
26 import java.io.File;\r
27 import java.io.FileInputStream;\r
28 import java.io.FileOutputStream;\r
29 import java.io.FileReader;\r
30 import java.io.IOException;\r
31 import java.io.InputStreamReader;\r
32 import java.net.InetAddress;\r
33 import java.net.UnknownHostException;\r
34 import java.security.NoSuchAlgorithmException;\r
35 \r
36 import com.att.cadi.util.Chmod;\r
37 import com.att.cadi.util.JsonOutputStream;\r
38 \r
39 \r
40 \r
41 /**\r
42  * A Class to run on command line to determine suitability of environment for certain TAFs.\r
43  * \r
44  * For instance, CSP supports services only in certain domains, and while dynamic host\r
45  * lookups on the machine work in most cases, sometimes, names and IPs are unexpected (and\r
46  * invalid) for CSP because of multiple NetworkInterfaces, etc\r
47  * \r
48  *\r
49  */\r
50 public class CmdLine {\r
51 \r
52         /**\r
53          * @param args\r
54          */\r
55         public static void main(String[] args) {\r
56                 if(args.length>0) {\r
57                         if("digest".equalsIgnoreCase(args[0]) && (args.length>2 || (args.length>1 && System.console()!=null))) {\r
58                                 String keyfile;\r
59                                 String password;\r
60                                 if(args.length>2) {\r
61                                         password = args[1];\r
62                                         keyfile = args[2];\r
63                                 } else {\r
64                                         keyfile = args[1];\r
65                                         password = new String(System.console().readPassword("Type here (keystrokes hidden): "));\r
66                                 }\r
67 \r
68                                 try {\r
69                                         Symm symm;\r
70                                         FileInputStream fis = new FileInputStream(keyfile);\r
71                                         try {\r
72                                                 symm = Symm.obtain(fis);\r
73                                         } finally {\r
74                                                 fis.close();\r
75                                         }\r
76                                         symm.enpass(password, System.out);\r
77                                         System.out.println();\r
78                                         System.out.flush();\r
79                                         return;\r
80                                         /*  testing code... don't want it exposed\r
81                                         System.out.println(" ******** Testing *********");\r
82                                         for(int i=0;i<100000;++i) {\r
83                                                 System.out.println(args[1]);\r
84                                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
85                                                 b64.enpass(args[1], baos);\r
86                                                 String pass; \r
87                                                 System.out.println(pass=new String(baos.toByteArray()));\r
88                                                 ByteArrayOutputStream reconstituted = new ByteArrayOutputStream();\r
89                                                 b64.depass(pass, reconstituted);\r
90                                                 String r = reconstituted.toString();\r
91                                                 System.out.println(r);\r
92                                                 if(!r.equals(args[1])) {\r
93                                                         System.err.println("!!!!! STOP - ERROR !!!!!");\r
94                                                         return;\r
95                                                 }\r
96                                                 System.out.println();\r
97                                         }\r
98                                         System.out.flush();\r
99                                         */\r
100                                          \r
101                                 } catch (IOException e) {\r
102                                         System.err.println("Cannot digest password");\r
103                                         System.err.println("   \""+ e.getMessage() + '"');\r
104                                 }\r
105 // .  Oh, well, Deployment services need this behavior.  I will put this code in, but leave it undocumented. \r
106 // One still needs access to the keyfile to read.\r
107 // July 2016 - thought of a tool "CMPass" to reguritate from properties, but only if allowed.\r
108                         } else if("regurgitate".equalsIgnoreCase(args[0]) && args.length>2) {\r
109                                 try {\r
110                                         Symm symm;\r
111                                         FileInputStream fis = new FileInputStream(args[2]);\r
112                                         try {\r
113                                                 symm = Symm.obtain(fis);\r
114                                         } finally {\r
115                                                 fis.close();\r
116                                         }\r
117                                         boolean isFile = false;\r
118                                         if("-i".equals(args[1]) || (isFile="-f".equals(args[1]))) {\r
119                                                 BufferedReader br;\r
120                                                 if(isFile) {\r
121                                                         if(args.length<4) {\r
122                                                                 System.err.println("Filename in 4th position");\r
123                                                                 return;\r
124                                                         }\r
125                                                         br = new BufferedReader(new FileReader(args[3]));\r
126                                                 } else {\r
127                                                         br = new BufferedReader(new InputStreamReader(System.in));\r
128                                                 }\r
129                                                 try {\r
130                                                         String line;\r
131                                                         boolean cont = false;\r
132                                                         StringBuffer sb = new StringBuffer();\r
133                                                         JsonOutputStream jw = new JsonOutputStream(System.out);\r
134                                                         while((line=br.readLine())!=null) {\r
135                                                                 if(cont) {\r
136                                                                         int end;\r
137                                                                         if((end=line.indexOf('"'))>=0) {\r
138                                                                                 sb.append(line,0,end);\r
139                                                                                 cont=false;\r
140                                                                         } else {\r
141                                                                                 sb.append(line);\r
142                                                                         }\r
143                                                                 } else {\r
144                                                                         int idx;\r
145                                                                         if((idx = line.indexOf(' '))>=0 \r
146                                                                                         && (idx = line.indexOf(' ',++idx))>0\r
147                                                                                         && (idx = line.indexOf('=',++idx))>0\r
148                                                                                         && (idx = line.indexOf('=',++idx))>0\r
149                                                                                         ) {\r
150                                                                                 System.out.println(line.substring(0, idx-5));\r
151                                                                                 int start = idx+2;\r
152                                                                                 int end;\r
153                                                                                 if((end=line.indexOf('"',start))<0) {\r
154                                                                                         end = line.length();\r
155                                                                                         cont = true;\r
156                                                                                 }\r
157                                                                                 sb.append(line,start,end);\r
158                                                                         }\r
159                                                                 }\r
160                                                                 if(sb.length()>0) {\r
161                                                                         symm.depass(sb.toString(),jw);\r
162                                                                         if(!cont) {\r
163                                                                                 System.out.println();\r
164                                                                         }\r
165                                                                 }\r
166                                                                 System.out.flush();\r
167                                                                 sb.setLength(0);\r
168                                                                 if(!cont) {\r
169                                                                         jw.resetIndent();\r
170                                                                 }\r
171                                                         }\r
172                                                 } finally {\r
173                                                         if(isFile) {\r
174                                                                 br.close();\r
175                                                         }\r
176                                                 }\r
177                                         } else {\r
178                                                 symm.depass(args[1], System.out);\r
179                                         }\r
180                                         System.out.println();\r
181                                         System.out.flush();\r
182                                         return;\r
183                                 } catch (IOException e) {\r
184                                         System.err.println("Cannot regurgitate password");\r
185                                         System.err.println("   \""+ e.getMessage() + '"');\r
186                                 }\r
187                         } else if("encode64".equalsIgnoreCase(args[0]) && args.length>1) {\r
188                                 try {\r
189                                         Symm.base64.encode(args[1], System.out);\r
190                                         System.out.println();\r
191                                         System.out.flush();\r
192                                         return;\r
193                                 } catch (IOException e) {\r
194                                         System.err.println("Cannot encode Base64 with " + args[1]);\r
195                                         System.err.println("   \""+ e.getMessage() + '"');\r
196                                 }\r
197                         } else if("decode64".equalsIgnoreCase(args[0]) && args.length>1) {\r
198                                 try {\r
199                                         Symm.base64.decode(args[1], System.out);\r
200                                         System.out.println();\r
201                                         System.out.flush();\r
202                                         return;\r
203                                 } catch (IOException e) {\r
204                                         System.err.println("Cannot decode Base64 text from " + args[1]);\r
205                                         System.err.println("   \""+ e.getMessage() + '"');\r
206                                 }\r
207                         } else if("encode64url".equalsIgnoreCase(args[0]) && args.length>1) {\r
208                                 try {\r
209                                         Symm.base64url.encode(args[1], System.out);\r
210                                         System.out.println();\r
211                                         System.out.flush();\r
212                                         return;\r
213                                 } catch (IOException e) {\r
214                                         System.err.println("Cannot encode Base64url with " + args[1]);\r
215                                         System.err.println("   \""+ e.getMessage() + '"');\r
216                                 }\r
217                         } else if("decode64url".equalsIgnoreCase(args[0]) && args.length>1) {\r
218                                 try {\r
219                                         Symm.base64url.decode(args[1], System.out);\r
220                                         System.out.println();\r
221                                         System.out.flush();\r
222                                         return;\r
223                                 } catch (IOException e) {\r
224                                         System.err.println("Cannot decode Base64url text from " + args[1]);\r
225                                         System.err.println("   \""+ e.getMessage() + '"');\r
226                                 }\r
227                         } else if("md5".equalsIgnoreCase(args[0]) && args.length>1) {\r
228                                 try {\r
229                                         System.out.println(Hash.encryptMD5asStringHex(args[1]));\r
230                                         System.out.flush();\r
231                                 } catch (NoSuchAlgorithmException e) {\r
232                                         System.err.println("Cannot hash MD5 from " + args[1]);\r
233                                         System.err.println("   \""+ e.getMessage() + '"');\r
234                                 }\r
235                                 return;\r
236                         } else if("sha256".equalsIgnoreCase(args[0]) && args.length>1) {\r
237                                 try {\r
238                                         if(args.length>2) {\r
239                                                 int salt = Integer.parseInt(args[2]);\r
240                                                 System.out.println(Hash.hashSHA256asStringHex(args[1],salt));\r
241                                         } else { \r
242                                                 System.out.println(Hash.hashSHA256asStringHex(args[1]));\r
243                                         }\r
244                                 } catch (NoSuchAlgorithmException e) {\r
245                                         System.err.println("Cannot hash SHA256 text from " + args[1]);\r
246                                         System.err.println("   \""+ e.getMessage() + '"');\r
247                                 }\r
248                                 System.out.flush();\r
249                                 return;\r
250                         } else if("keygen".equalsIgnoreCase(args[0])) {\r
251                                 try {\r
252                                         if(args.length>1) {\r
253                                                 File f = new File(args[1]);\r
254                                                 FileOutputStream fos = new FileOutputStream(f);\r
255                                                 try {\r
256                                                         fos.write(Symm.baseCrypt().keygen());\r
257                                                         fos.flush();\r
258                                                 } finally {\r
259                                                         fos.close();\r
260                                                         Chmod.to400.chmod(f);\r
261                                                 }\r
262                                         } else {\r
263                                                 // create a Symmetric Key out of same characters found in base64\r
264                                                 System.out.write(Symm.baseCrypt().keygen());\r
265                                                 System.out.flush();\r
266                                         }\r
267                                         return;\r
268                                 } catch (IOException e) {\r
269                                         System.err.println("Cannot create a key " + args[0]);\r
270                                         System.err.println("   \""+ e.getMessage() + '"');\r
271                                 }\r
272                         \r
273                         } else if("passgen".equalsIgnoreCase(args[0])) {\r
274                                 int numDigits;\r
275                                 if(args.length <= 1) {\r
276                                         numDigits = 24;\r
277                                 } else {\r
278                                         numDigits = Integer.parseInt(args[1]); \r
279                                         if(numDigits<8)numDigits = 8;\r
280                                 }\r
281                                 String pass;\r
282                                 boolean noLower,noUpper,noDigits,noSpecial,repeats;\r
283                                 do {\r
284                                         pass = Symm.randomGen(numDigits);\r
285                                         noLower=noUpper=noDigits=noSpecial=true;\r
286                                         repeats=false;\r
287                                         int c=-1,last;\r
288                                         for(int i=0;i<numDigits;++i) {\r
289                                                 last = c;\r
290                                                 c = pass.charAt(i);\r
291                                                 if(c==last) {\r
292                                                         repeats=true;\r
293                                                         break;\r
294                                                 }\r
295                                                 \r
296                                                 if(noLower) {\r
297                                                         noLower=!(c>=0x61 && c<=0x7A);\r
298                                                         continue;\r
299                                                 } \r
300                                                 if(noUpper) {\r
301                                                         noUpper=!(c>=0x41 && c<=0x5A);\r
302                                                         continue;\r
303                                                 } \r
304                                                 if(noDigits) {\r
305                                                         noDigits=!(c>=0x30 && c<=0x39);\r
306                                                         continue;\r
307                                                 } \r
308                                                 if(noSpecial) {\r
309                                                         noSpecial = "+!@#$%^&*(){}[]?:;,.".indexOf(c)<0;\r
310                                                         continue;\r
311                                                 } \r
312                                                 \r
313                                                 break;\r
314                                         }\r
315                                 } while(noLower || noUpper || noDigits || noSpecial || repeats);\r
316                                 System.out.println(pass.substring(0,numDigits));\r
317                         } else if("urlgen".equalsIgnoreCase(args[0])) {\r
318                                 int numDigits;\r
319                                 if(args.length < 1) {\r
320                                         numDigits = 24;\r
321                                 } else {\r
322                                         numDigits = Integer.parseInt(args[1]); \r
323                                 }\r
324                                 System.out.println(Symm.randomGen(Symm.base64url.codeset, numDigits).substring(0,numDigits));\r
325                         \r
326                         } else if("csptest".equalsIgnoreCase(args[0])) {\r
327                                 try {\r
328                                         System.out.println("CSP Compatibility test");\r
329                                         \r
330                                         String hostName = InetAddress.getLocalHost().getCanonicalHostName();\r
331                                         \r
332                                         System.out.println("  Your automatic hostname is reported as \"" + hostName + "\"\n");\r
333                                         System.out.flush();\r
334                                         return;\r
335                                 } catch (UnknownHostException e) {\r
336                                         e.printStackTrace(System.err);\r
337                                 }\r
338                         }\r
339                 } else {\r
340                         System.out.println("Usage: java -jar <this jar> ...");\r
341                         System.out.println("  keygen [<keyfile>]                     (Generates Key on file, or Std Out)");\r
342                         System.out.println("  digest <keyfile>                       (Encrypts to Key with \"keyfile\")");\r
343                         System.out.println("  passgen <digits>                       (Generate Password of given size)");\r
344                         System.out.println("  urlgen <digits>                        (Generate URL field of given size)");\r
345                         System.out.println("  csptest                                (Tests for CSP compatibility)");\r
346                         System.out.println("  encode64 <your text>                   (Encodes to Base64)");\r
347                         System.out.println("  decode64 <base64 encoded text>         (Decodes from Base64)");\r
348                         System.out.println("  encode64url <your text>                (Encodes to Base64 URL charset)");\r
349                         System.out.println("  decode64url <base64url encoded text>   (Decodes from Base64 URL charset)");\r
350                         System.out.println("  sha256 <text>                          (Digest String into SHA256 Hash)");\r
351                         System.out.println("  md5 <text>                             (Digest String into MD5 Hash)");\r
352                 }\r
353                 System.exit(1);\r
354         }\r
355 \r
356 }\r