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