ilex.util.UserAuthFile Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opendcs Show documentation
Show all versions of opendcs Show documentation
A collection of software for aggregatting and processing environmental data such as from NOAA GOES satellites.
The newest version!
/*
* $Id: UserAuthFile.java,v 1.5 2020/04/28 18:18:58 mmaloney Exp $
*
* This is open-source software written by ILEX Engineering, Inc., under
* contract to the federal government. You are free to copy and use this
* source code for your own purposes, except that no part of the information
* contained in this file may be claimed to be proprietary.
*
* Except for specific contractual terms between ILEX and the federal
* government, this source code is provided completely without warranty.
* For more information contact: [email protected]
*
* $Log: UserAuthFile.java,v $
* Revision 1.5 2020/04/28 18:18:58 mmaloney
* Added -p pwfile feature for USACE NWP
*
* Revision 1.4 2016/08/05 14:46:06 mmaloney
* Was using the wrong Console class.
*
* Revision 1.3 2014/11/19 16:13:22 mmaloney
* Added constructor taking File object.
*
* Revision 1.2 2014/07/03 12:25:41 mmaloney
* Bug fix: use authFile.getAbsoluteFile().getParentFile(). That way it should never be null.
*
* Revision 1.1.1.1 2014/05/19 15:28:59 mmaloney
* OPENDCS 6.0 Initial Checkin
*
* Revision 1.6 2010/10/29 15:13:45 mmaloney
* debugs
*
* Revision 1.5 2010/08/17 17:31:22 mmaloney
* Handle case where file was created from CWD and encoded with null parent dir.
*
* Revision 1.4 2010/08/17 17:19:34 mmaloney
* Temporary debugs.
*
* Revision 1.3 2009/11/01 20:36:03 mjmaloney
* Updated docs
*
* Revision 1.2 2009/09/24 17:02:07 mjmaloney
* dev
*
* Revision 1.1 2008/04/04 18:21:10 cvs
* Added legacy code to repository
*
* Revision 1.7 2007/11/01 20:07:51 mmaloney
* dev
*
* Revision 1.6 2006/12/23 18:16:05 mmaloney
* dev
*
* Revision 1.5 2006/09/29 17:52:51 mmaloney
* release prep
*
* Revision 1.4 2006/04/22 21:02:13 mmaloney
* Removed debugs.
*
* Revision 1.3 2006/03/23 15:56:15 mmaloney
* version must be un-encrypted.
*
* Revision 1.2 2006/03/15 13:53:15 mmaloney
* UserAuthFile now handled file version 0 (old unsecure decodes format)
* as well as version 1 (DES encryption).
*
* Revision 1.1 2006/03/15 13:31:12 mmaloney
* dev
*
*/
package ilex.util;
import java.io.BufferedReader;
import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import org.opendcs.spi.authentication.AuthSource;
import decodes.util.CmdLineArgs;
import decodes.util.DecodesSettings;
import ilex.cmdline.BooleanToken;
import ilex.cmdline.StringToken;
import ilex.cmdline.TokenOptions;
import java.io.FileWriter;
import java.io.FileInputStream;
import java.io.FileReader;
/**
Daemons require the SQL username and password to be placed in the user's
home directory in an encrypted file. This class provides access to an
encrypted file in the user's home directory. This file should be protected
so that only the owner has access to it. The default file name is ".db.auth".
*/
public class UserAuthFile implements AuthSource
{
/** The file to read */
private File authFile;
/** The database user name extracted from the file */
private String username;
/** The database password extracted from the file */
private String password;
/** The version of the file just read. */
private int fileVersion;
/** The version of this code */
private static final int codeVersion = 1;
private static final int[] v0seed = { 5, 192, 31, 65, 255, 84, 21, 9, 111 };
/** Default constructor looks for .db.auth in current user's home. */
public UserAuthFile()
{
this(System.getProperty("user.home")
+ System.getProperty("file.separator") + ".decodes.auth");
}
/**
Construct with filename other than the default.
@param authFilename the filename
*/
public UserAuthFile(String authFilename)
{
this(new File(EnvExpander.expand(authFilename)));
}
public UserAuthFile(File af)
{
authFile = af;
username = null;
password = null;
fileVersion = 0;
}
private static final byte[] pp =
{ 0x55, 0x30, 0x31, 0x65, 0x4f, 0x7e, 0x70, 0x42, 0x77, 0x51, 0x5d,
0x34, 0x65, 0x78, 0x3a, 0x5a, 0x33, 0x6d };
/**
Writes the file containing the passed name and password.
@param nm the database user name (may not be null or zero length)
@param pw the database password (may not be null or zero length)
*/
public void write(String nm, String pw)
throws IOException, AuthException
{
if (nm.length() == 0 || pw.length() == 0)
throw new IOException(
"Cannot have zero length username or password");
username = nm;
password = pw;
// pre-fill data with random printable characters
byte data[] = new byte[256];
int i=0;
for(i=0; i 64)
pwlen = 64;
for(i=0; i
@param args command line args.
*/
public static void main(String args[])
throws Exception
{
final CmdLineArgs cla = new CmdLineArgs(false, "$DCSTOOL_USERDIR/password-update.log");
final BooleanToken showPassword = new BooleanToken("s", "Show user name and password", "", TokenOptions.optSwitch, false);
final StringToken authFileName = new StringToken("f", "auth file name", "", TokenOptions.optArgument, null);
final BooleanToken overwrite = new BooleanToken("y", "Assert that the file should be overwritten.", "", TokenOptions.optSwitch, false);
cla.addToken(showPassword);
cla.addToken(authFileName);
cla.parseArgs(args);
if (cla.getProfileSet() && authFileName.getValue() != null)
{
System.err.println("Please specify either the auth file name OR the profile. Not both.");
System.exit(1);
}
File authFile = null;
if (cla.getProfileSet())
{
DecodesSettings settings = DecodesSettings.fromProfile(cla.getProfile());
authFile = new File(EnvExpander.expand(settings.DbAuthFile));
}
else if (authFileName.getValue() != null )
{
authFile = new File(EnvExpander.expand(authFileName.getValue()));
}
if (authFile == null)
{
System.err.println("Please specify either the auth file name or the profile.");
System.exit(1);
}
else
{
if (showPassword.getValue() && authFile.exists())
{
UserAuthFile userAuthFile = new UserAuthFile(authFile);
try
{
userAuthFile.read();
System.out.println("Username '" + userAuthFile.getUsername()
+ "', password '" + userAuthFile.getPassword() + "'"
+ ", file version = " + userAuthFile.getFileVersion());
}
catch(Exception ex)
{
System.err.println(String.format("Error reading '%s'", userAuthFile.getAuthFile().getAbsolutePath()));
ex.printStackTrace(System.err);
System.exit(3);
}
}
else if (showPassword.getValue() && !authFile.exists())
{
System.err.println(String.format("No auth file at '%s'. Please write initial data first.", authFile.getAbsolutePath()));
System.exit(2);
}
else
{
System.out.println(String.format("Creating or Updating: %s", authFile.getAbsolutePath()));
Console console = System.console();
if (authFile.exists() && !overwrite.getValue())
{
final String response = console.readLine("Overwrite existing file? (y/N)");
if (!response.toLowerCase().startsWith("y"))
{
System.err.println("Not overwriting existing file.");
System.exit(0);
}
}
UserAuthFile userAuthFile = new UserAuthFile(authFile);
final String userName = console.readLine("Please enter a username: ");
boolean match = true;
String password;
do
{
if(!match)
{
console.writer().println("Passwords did not match, try again.");
}
char[] pw_chars = console.readPassword("Please provide a password:");
char[] pw2_chars = console.readPassword("Please repeat the password:");
String pw = new String(pw_chars);
String pw2 = new String(pw2_chars);
password = pw;
match = pw.equals(pw2);
}
while (!match);
userAuthFile.write(userName, password);
}
}
}
/**
* @returns Properties file with username and password fields.
*/
@Override
public Properties getCredentials()
{
Properties props = new Properties();
props.put("username",getUsername());
props.put("password",getPassword());
return props;
}
@Override
public boolean canWrite()
{
return true;
}
}