1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.cadi;
\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
36 import org.onap.aaf.cadi.util.Chmod;
\r
37 import org.onap.aaf.cadi.util.JsonOutputStream;
\r
42 * A Class to run on command line to determine suitability of environment for certain TAFs.
\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
50 public class CmdLine {
\r
55 public static void main(String[] args) {
\r
57 if("digest".equalsIgnoreCase(args[0]) && (args.length>2 || (args.length>1 && System.console()!=null))) {
\r
65 password = new String(System.console().readPassword("Type here (keystrokes hidden): "));
\r
70 FileInputStream fis = new FileInputStream(keyfile);
\r
72 symm = Symm.obtain(fis);
\r
76 symm.enpass(password, System.out);
\r
77 System.out.println();
\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
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
96 System.out.println();
\r
101 } catch (IOException e) {
\r
102 System.err.println("Cannot digest password");
\r
103 System.err.println(" \""+ e.getMessage() + '"');
\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
111 FileInputStream fis = new FileInputStream(args[2]);
\r
113 symm = Symm.obtain(fis);
\r
117 boolean isFile = false;
\r
118 if("-i".equals(args[1]) || (isFile="-f".equals(args[1]))) {
\r
121 if(args.length<4) {
\r
122 System.err.println("Filename in 4th position");
\r
125 br = new BufferedReader(new FileReader(args[3]));
\r
127 br = new BufferedReader(new InputStreamReader(System.in));
\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
137 if((end=line.indexOf('"'))>=0) {
\r
138 sb.append(line,0,end);
\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
150 System.out.println(line.substring(0, idx-5));
\r
153 if((end=line.indexOf('"',start))<0) {
\r
154 end = line.length();
\r
157 sb.append(line,start,end);
\r
160 if(sb.length()>0) {
\r
161 symm.depass(sb.toString(),jw);
\r
163 System.out.println();
\r
166 System.out.flush();
\r
178 symm.depass(args[1], System.out);
\r
180 System.out.println();
\r
181 System.out.flush();
\r
183 } catch (IOException e) {
\r
184 System.err.println("Cannot regurgitate password");
\r
185 System.err.println(" \""+ e.getMessage() + '"');
\r
187 } else if("encode64".equalsIgnoreCase(args[0]) && args.length>1) {
\r
189 Symm.base64.encode(args[1], System.out);
\r
190 System.out.println();
\r
191 System.out.flush();
\r
193 } catch (IOException e) {
\r
194 System.err.println("Cannot encode Base64 with " + args[1]);
\r
195 System.err.println(" \""+ e.getMessage() + '"');
\r
197 } else if("decode64".equalsIgnoreCase(args[0]) && args.length>1) {
\r
199 Symm.base64.decode(args[1], System.out);
\r
200 System.out.println();
\r
201 System.out.flush();
\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
207 } else if("encode64url".equalsIgnoreCase(args[0]) && args.length>1) {
\r
209 Symm.base64url.encode(args[1], System.out);
\r
210 System.out.println();
\r
211 System.out.flush();
\r
213 } catch (IOException e) {
\r
214 System.err.println("Cannot encode Base64url with " + args[1]);
\r
215 System.err.println(" \""+ e.getMessage() + '"');
\r
217 } else if("decode64url".equalsIgnoreCase(args[0]) && args.length>1) {
\r
219 Symm.base64url.decode(args[1], System.out);
\r
220 System.out.println();
\r
221 System.out.flush();
\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
227 } else if("md5".equalsIgnoreCase(args[0]) && args.length>1) {
\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
236 } else if("sha256".equalsIgnoreCase(args[0]) && args.length>1) {
\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
242 System.out.println(Hash.hashSHA256asStringHex(args[1]));
\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
248 System.out.flush();
\r
250 } else if("keygen".equalsIgnoreCase(args[0])) {
\r
252 if(args.length>1) {
\r
253 File f = new File(args[1]);
\r
254 FileOutputStream fos = new FileOutputStream(f);
\r
256 fos.write(Symm.baseCrypt().keygen());
\r
260 Chmod.to400.chmod(f);
\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
268 } catch (IOException e) {
\r
269 System.err.println("Cannot create a key " + args[0]);
\r
270 System.err.println(" \""+ e.getMessage() + '"');
\r
273 } else if("passgen".equalsIgnoreCase(args[0])) {
\r
275 if(args.length <= 1) {
\r
278 numDigits = Integer.parseInt(args[1]);
\r
279 if(numDigits<8)numDigits = 8;
\r
282 boolean noLower,noUpper,noDigits,noSpecial,repeats;
\r
284 pass = Symm.randomGen(numDigits);
\r
285 noLower=noUpper=noDigits=noSpecial=true;
\r
288 for(int i=0;i<numDigits;++i) {
\r
290 c = pass.charAt(i);
\r
297 noLower=!(c>=0x61 && c<=0x7A);
\r
301 noUpper=!(c>=0x41 && c<=0x5A);
\r
305 noDigits=!(c>=0x30 && c<=0x39);
\r
309 noSpecial = "+!@#$%^&*(){}[]?:;,.".indexOf(c)<0;
\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
319 if(args.length < 1) {
\r
322 numDigits = Integer.parseInt(args[1]);
\r
324 System.out.println(Symm.randomGen(Symm.base64url.codeset, numDigits).substring(0,numDigits));
\r
326 } else if("csptest".equalsIgnoreCase(args[0])) {
\r
328 System.out.println("CSP Compatibility test");
\r
330 String hostName = InetAddress.getLocalHost().getCanonicalHostName();
\r
332 System.out.println(" Your automatic hostname is reported as \"" + hostName + "\"\n");
\r
333 System.out.flush();
\r
335 } catch (UnknownHostException e) {
\r
336 e.printStackTrace(System.err);
\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