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