Utils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.examples.mail;

  18. import java.io.BufferedReader;
  19. import java.io.Console;
  20. import java.io.IOException;
  21. import java.io.InputStreamReader;
  22. import java.nio.charset.Charset;
  23. import java.util.Locale;

  24. /**
  25.  * Utilities for mail examples
  26.  */
  27. final class Utils {

  28.     /**
  29.      * If the initial password is: '*' - replace it with a line read from the system console '-' - replace it with next line from STDIN 'ABCD' - if the input is
  30.      * all upper case, use the field as an environment variable name
  31.      *
  32.      * Note: there are no guarantees that the password cannot be snooped.
  33.      *
  34.      * Even using the console may be subject to memory snooping, however it should be safer than the other methods.
  35.      *
  36.      * STDIN may require creating a temporary file which could be read by others Environment variables may be visible by using PS
  37.      */
  38.     static String getPassword(final String user, String password) throws IOException {
  39.         if ("-".equals(password)) { // stdin
  40.             final BufferedReader in = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
  41.             password = in.readLine();
  42.         } else if ("*".equals(password)) { // console
  43.             final Console con = System.console(); // Java 1.6
  44.             if (con == null) {
  45.                 throw new IOException("Cannot access Console");
  46.             }
  47.             final char[] pwd = con.readPassword("Password for " + user + ": ");
  48.             password = new String(pwd);
  49.         } else if (password.equals(password.toUpperCase(Locale.ROOT))) { // environment variable name
  50.             final String tmp = System.getenv(password);
  51.             if (tmp != null) { // don't overwrite if variable does not exist (just in case password is all uppers)
  52.                 password = tmp;
  53.             }
  54.         }
  55.         return password;
  56.     }

  57.     private Utils() {
  58.         // not instantiable
  59.     }

  60. }