Clean up Sonar results 3
[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  * For instance, CSP supports services only in certain domains, and while dynamic host
42  * lookups on the machine work in most cases, sometimes, names and IPs are unexpected (and
43  * invalid) for CSP because of multiple NetworkInterfaces, etc
44  * 
45  * @author Jonathan
46  *
47  */
48 public class CmdLine {
49
50         public static Access access;
51         
52         /**
53          * @param args
54          */
55         public static void main(String[] args) {
56                 if(args.length>0) {
57                         if("digest".equalsIgnoreCase(args[0]) && (args.length>2 || (args.length>1 && System.console()!=null))) {
58                                 String keyfile;
59                                 String password;
60                                 if(args.length>2) {
61                                         password = args[1];
62                                         keyfile = args[2];
63                                         if("-i".equals(password)) {
64                                                 int c;
65                                                 StringBuilder sb = new StringBuilder();
66                                                 try {
67                                                         while((c=System.in.read())>=0) {
68                                                                 sb.append((char)c);
69                                                         }
70                                                 } catch (IOException e) {
71                                                         e.printStackTrace();
72                                                 }
73                                                 password = sb.toString();
74                                         }
75                                 } else {
76                                         keyfile = args[1];
77                                         password = new String(System.console().readPassword("Type here (keystrokes hidden): "));
78                                 }
79
80                                 try {
81                                         Symm symm;
82                                         FileInputStream fis = new FileInputStream(keyfile);
83                                         try {
84                                                 symm = Symm.obtain(fis);
85                                         } finally {
86                                                 fis.close();
87                                         }
88                                         symm.enpass(password, System.out);
89                                         System.out.println();
90                                         System.out.flush();
91                                         return;
92                                         /*  testing code... don't want it exposed
93                                         System.out.println(" ******** Testing *********");
94                                         for(int i=0;i<100000;++i) {
95                                                 System.out.println(args[1]);
96                                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
97                                                 b64.enpass(args[1], baos);
98                                                 String pass; 
99                                                 System.out.println(pass=new String(baos.toByteArray()));
100                                                 ByteArrayOutputStream reconstituted = new ByteArrayOutputStream();
101                                                 b64.depass(pass, reconstituted);
102                                                 String r = reconstituted.toString();
103                                                 System.out.println(r);
104                                                 if(!r.equals(args[1])) {
105                                                         System.err.println("!!!!! STOP - ERROR !!!!!");
106                                                         return;
107                                                 }
108                                                 System.out.println();
109                                         }
110                                         System.out.flush();
111                                         */
112                                          
113                                 } catch (IOException e) {
114                                         System.err.println("Cannot digest password");
115                                         System.err.println("   \""+ e.getMessage() + '"');
116                                 }
117 // DO NOT LEAVE THIS METHOD Compiled IN CODE... Do not want looking at passwords on disk too easy
118 // Jonathan.  Oh, well, Deployment services need this behavior.  I will put this code in, but leave it undocumented. 
119 // One still needs access to the keyfile to read.
120 // July 2016 - thought of a tool "CMPass" to regurgitate from properties, but only if allowed.
121                         } else if("regurgitate".equalsIgnoreCase(args[0]) && args.length>2) {
122                                 try {
123                                         Symm symm;
124                                         FileInputStream fis = new FileInputStream(args[2]);
125                                         try {
126                                                 symm = Symm.obtain(fis);
127                                         } finally {
128                                                 fis.close();
129                                         }
130                                         boolean isFile = false;
131                                         if("-i".equals(args[1]) || (isFile="-f".equals(args[1]))) {
132                                                 BufferedReader br;
133                                                 if(isFile) {
134                                                         if(args.length<4) {
135                                                                 System.err.println("Filename in 4th position");
136                                                                 return;
137                                                         }
138                                                         br = new BufferedReader(new FileReader(args[3]));
139                                                 } else {
140                                                         br = new BufferedReader(new InputStreamReader(System.in));
141                                                 }
142                                                 try {
143                                                         String line;
144                                                         boolean cont = false;
145                                                         StringBuffer sb = new StringBuffer();
146                                                         JsonOutputStream jw = new JsonOutputStream(System.out);
147                                                         while((line=br.readLine())!=null) {
148                                                                 if(cont) {
149                                                                         int end;
150                                                                         if((end=line.indexOf('"'))>=0) {
151                                                                                 sb.append(line,0,end);
152                                                                                 cont=false;
153                                                                         } else {
154                                                                                 sb.append(line);
155                                                                         }
156                                                                 } else {
157                                                                         int idx;
158                                                                         if((idx = line.indexOf(' '))>=0 
159                                                                                         && (idx = line.indexOf(' ',++idx))>0
160                                                                                         && (idx = line.indexOf('=',++idx))>0
161                                                                                         ) {
162                                                                                 System.out.println(line.substring(0, idx-5));
163                                                                                 int start = idx+2;
164                                                                                 int end;
165                                                                                 if((end=line.indexOf('"',start))<0) {
166                                                                                         end = line.length();
167                                                                                         cont = true;
168                                                                                 }
169                                                                                 sb.append(line,start,end);
170                                                                         }
171                                                                 }
172                                                                 if(sb.length()>0) {
173                                                                         symm.depass(sb.toString(),jw);
174                                                                         if(!cont) {
175                                                                                 System.out.println();
176                                                                         }
177                                                                 }
178                                                                 System.out.flush();
179                                                                 sb.setLength(0);
180                                                                 if(!cont) {
181                                                                         jw.resetIndent();
182                                                                 }
183                                                         }
184                                                 } finally {
185                                                         if(isFile) {
186                                                                 br.close();
187                                                         }
188                                                 }
189                                         } else {
190                                                 symm.depass(args[1], System.out);
191                                         }
192                                         System.out.println();
193                                         System.out.flush();
194                                         return;
195                                 } catch (IOException e) {
196                                         System.err.println("Cannot regurgitate password");
197                                         System.err.println("   \""+ e.getMessage() + '"');
198                                 }
199                         } else if("encode64".equalsIgnoreCase(args[0]) && args.length>1) {
200                                 try {
201                                         Symm.base64.encode(args[1], System.out);
202                                         System.out.println();
203                                         System.out.flush();
204                                         return;
205                                 } catch (IOException e) {
206                                         System.err.println("Cannot encode Base64 with " + args[1]);
207                                         System.err.println("   \""+ e.getMessage() + '"');
208                                 }
209                         } else if("decode64".equalsIgnoreCase(args[0]) && args.length>1) {
210                                 try {
211                                         Symm.base64.decode(args[1], System.out);
212                                         System.out.println();
213                                         System.out.flush();
214                                         return;
215                                 } catch (IOException e) {
216                                         System.err.println("Cannot decode Base64 text from " + args[1]);
217                                         System.err.println("   \""+ e.getMessage() + '"');
218                                 }
219                         } else if("encode64url".equalsIgnoreCase(args[0]) && args.length>1) {
220                                 try {
221                                         Symm.base64url.encode(args[1], System.out);
222                                         System.out.println();
223                                         System.out.flush();
224                                         return;
225                                 } catch (IOException e) {
226                                         System.err.println("Cannot encode Base64url with " + args[1]);
227                                         System.err.println("   \""+ e.getMessage() + '"');
228                                 }
229                         } else if("decode64url".equalsIgnoreCase(args[0]) && args.length>1) {
230                                 try {
231                                         Symm.base64url.decode(args[1], System.out);
232                                         System.out.println();
233                                         System.out.flush();
234                                         return;
235                                 } catch (IOException e) {
236                                         System.err.println("Cannot decode Base64url text from " + args[1]);
237                                         System.err.println("   \""+ e.getMessage() + '"');
238                                 }
239                         } else if("md5".equalsIgnoreCase(args[0]) && args.length>1) {
240                                 try {
241                                         System.out.println(Hash.hashMD5asStringHex(args[1]));
242                                         System.out.flush();
243                                 } catch (NoSuchAlgorithmException e) {
244                                         System.err.println("Cannot hash MD5 from " + args[1]);
245                                         System.err.println("   \""+ e.getMessage() + '"');
246                                 }
247                                 return;
248                         } else if("sha256".equalsIgnoreCase(args[0]) && args.length>1) {
249                                 try {
250                                         if(args.length>2) {
251                                                 int max = args.length>7?7:args.length;
252                                                 for(int i=2;i<max;++i) {
253                                                         int salt = Integer.parseInt(args[i]);
254                                                         System.out.println(Hash.hashSHA256asStringHex(args[1],salt));
255                                                 }
256                                         } else { 
257                                                 System.out.println(Hash.hashSHA256asStringHex(args[1]));
258                                         }
259                                 } catch (NoSuchAlgorithmException e) {
260                                         System.err.println("Cannot hash SHA256 text from " + args[1]);
261                                         System.err.println("   \""+ e.getMessage() + '"');
262                                 }
263                                 System.out.flush();
264                                 return;
265                         } else if("keygen".equalsIgnoreCase(args[0])) {
266                                 try {
267                                         if(args.length>1) {
268                                                 File f = new File(args[1]);
269                                                 FileOutputStream fos = new FileOutputStream(f);
270                                                 try {
271                                                         fos.write(Symm.keygen());
272                                                         fos.flush();
273                                                 } finally {
274                                                         fos.close();
275                                                         Chmod.to400.chmod(f);
276                                                 }
277                                         } else {
278                                                 // create a Symmetric Key out of same characters found in base64
279                                                 System.out.write(Symm.keygen());
280                                                 System.out.flush();
281                                         }
282                                         return;
283                                 } catch (IOException e) {
284                                         System.err.println("Cannot create a key " + args[0]);
285                                         System.err.println("   \""+ e.getMessage() + '"');
286                                 }
287                         
288                         } else if("passgen".equalsIgnoreCase(args[0])) {
289                                 int numDigits;
290                                 if(args.length <= 1) {
291                                         numDigits = 24;
292                                 } else {
293                                         numDigits = Integer.parseInt(args[1]); 
294                                         if(numDigits<8)numDigits = 8;
295                                 }
296                                 String pass;
297                                 boolean noLower,noUpper,noDigits,noSpecial,repeatingChars,missingChars;
298                                 do {
299                                         pass = Symm.randomGen(numDigits);
300                                         missingChars=noLower=noUpper=noDigits=noSpecial=true;
301                                         repeatingChars=false;
302                                         int c=-1,last;
303                                         for(int i=0;i<numDigits;++i) {
304                                                 last = c;
305                                                 c = pass.charAt(i);
306                                                 if(c==last) {
307                                                         repeatingChars=true;
308                                                         break;
309                                                 }
310                                                 if(noLower) {
311                                                         noLower=!(c>=0x61 && c<=0x7A);
312                                                 } 
313                                                 if(noUpper) {
314                                                         noUpper=!(c>=0x41 && c<=0x5A);
315                                                 } 
316                                                 if(noDigits) {
317                                                         noDigits=!(c>=0x30 && c<=0x39);
318                                                 } 
319                                                 if(noSpecial) {
320                                                         noSpecial = "+!@#$%^&*(){}[]?:;,.".indexOf(c)<0;
321                                                 } 
322                                                 
323                                                 missingChars = (noLower || noUpper || noDigits || noSpecial);
324                                         }
325                                 } while(missingChars || repeatingChars);
326                                 System.out.println(pass.substring(0,numDigits));
327                         } else if("urlgen".equalsIgnoreCase(args[0])) {
328                                 int numDigits;
329                                 if(args.length <= 1) {
330                                         numDigits = 24;
331                                 } else {
332                                         numDigits = Integer.parseInt(args[1]); 
333                                 }
334                                 System.out.println(Symm.randomGen(Symm.base64url.codeset, numDigits).substring(0,numDigits));
335                         }
336                 } else {
337                         System.out.println("Usage: java -jar <this jar> ...");
338                         System.out.println("  keygen [<keyfile>]                     (Generates Key on file, or Std Out)");
339                         System.out.println("  digest [<passwd>|-i|] <keyfile>        (Encrypts Password with \"keyfile\"");
340                         System.out.println("                                          if passwd = -i, will read StdIin");
341                         System.out.println("                                          if passwd is blank, will ask securely)");
342                         System.out.println("  passgen <digits>                       (Generate Password of given size)");
343                         System.out.println("  urlgen <digits>                        (Generate URL field of given size)");
344                         System.out.println("  csptest                                (Tests for CSP compatibility)");
345                         System.out.println("  encode64 <your text>                   (Encodes to Base64)");
346                         System.out.println("  decode64 <base64 encoded text>         (Decodes from Base64)");
347                         System.out.println("  encode64url <your text>                (Encodes to Base64 URL charset)");
348                         System.out.println("  decode64url <base64url encoded text>   (Decodes from Base64 URL charset)");
349                         System.out.println("  sha256 <text> <salts(s)>               (Digest String into SHA256 Hash)");
350                         System.out.println("  md5 <text>                             (Digest String into MD5 Hash)");
351                 }
352                 String forceExit = access.getProperty("force_exit", null);
353                 if (forceExit == null) {
354                         System.exit(1);
355                 }
356         }
357         
358 }