2007-01-16 12:21:37 +01:00
|
|
|
/***********************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Novell, Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; version 2.1
|
|
|
|
* of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, Novell, Inc.
|
|
|
|
*
|
|
|
|
* To contact Novell about this file by physical or electronic mail,
|
|
|
|
* you may find current contact information at www.novell.com.
|
|
|
|
*
|
2007-03-05 17:41:54 +01:00
|
|
|
* Author: Greg Richardson
|
2007-01-16 12:21:37 +01:00
|
|
|
*
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Summary description for Program
|
|
|
|
*/
|
2007-01-24 08:18:15 +01:00
|
|
|
public class MungeCryptoPropertiesFilePath
|
2007-01-16 12:21:37 +01:00
|
|
|
{
|
2007-03-05 17:41:54 +01:00
|
|
|
final static int ERROR_NO_ERROR = 0;
|
|
|
|
final static int ERROR_INVALID_NUMBER_OF_PARAMS = -1;
|
|
|
|
final static int ERROR_MISSING_INPUT_FILE = -2;
|
|
|
|
final static int ERROR_OUTPUT_COPY_FAILED = -3;
|
|
|
|
final static int ERROR_IO_EXCEPTION = -4;
|
|
|
|
final static int ERROR_BAD_INPUT_FILE_PARAM = -5;
|
|
|
|
final static int ERROR_BAD_OUTPUT_FILE_PARAM = -6;
|
|
|
|
final static int ERROR_MISSING_INPUT_FILE_PARAM = -7;
|
|
|
|
final static int ERROR_MISSING_OUTPUT_FILE_PARAM = -8;
|
|
|
|
final static int ERROR_CANNOT_READ_FILE = -9;
|
|
|
|
final static int ERROR_CANNOT_CREATE_FILE = -10;
|
|
|
|
|
|
|
|
final static String INPUT_FILE_PARAM = "input=";
|
|
|
|
final static String OUTPUT_FILE_PARAM = "output=";
|
2007-03-17 00:48:58 +01:00
|
|
|
final static String FILE_KEY = "com.novell.casa.authtoksvc.crypto.file=";
|
2007-03-05 17:41:54 +01:00
|
|
|
|
|
|
|
File fileInput;
|
|
|
|
File fileOutput;
|
2007-03-17 07:39:46 +01:00
|
|
|
//File file;
|
|
|
|
//FileWriter fw;
|
2007-03-05 17:41:54 +01:00
|
|
|
String sInput;
|
|
|
|
String sOutput;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
public static void main(String[] args)
|
|
|
|
{
|
|
|
|
MungeCryptoPropertiesFilePath p = new MungeCryptoPropertiesFilePath(args);
|
|
|
|
System.exit(p.rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
MungeCryptoPropertiesFilePath(String[] args)
|
|
|
|
{
|
|
|
|
rc = ERROR_NO_ERROR;
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
fileInput = null;
|
|
|
|
fileOutput = null;
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
try
|
|
|
|
{
|
2007-03-17 07:39:46 +01:00
|
|
|
//file = new File("c:\\test5.log");
|
|
|
|
//fw = new FileWriter(file);
|
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
log("Here we go: " + args.length);
|
|
|
|
for (int i = 0; i < args.length; i++)
|
|
|
|
{
|
|
|
|
log("Arg " + i + " = " + args[i]);
|
|
|
|
}
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
// Process the arguments
|
|
|
|
if (ERROR_NO_ERROR == (rc = processArgs(args)))
|
|
|
|
{
|
|
|
|
// Process the file
|
|
|
|
rc = createOutputFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
rc = ERROR_IO_EXCEPTION;
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
log(rc);
|
2007-03-17 07:39:46 +01:00
|
|
|
//fw.flush();
|
|
|
|
//fw.close();
|
2007-03-05 17:41:54 +01:00
|
|
|
}
|
|
|
|
catch (Exception e1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int processArgs(String[] argsOld)
|
|
|
|
{
|
|
|
|
int iOld;
|
|
|
|
int i;
|
|
|
|
String args[] = new String[argsOld.length];
|
|
|
|
int iNew;
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
log("Original arg count " + argsOld.length);
|
|
|
|
for (i = 0; i < argsOld.length; i++)
|
|
|
|
{
|
|
|
|
log("Arg " + i + " = " + argsOld[i] + "\r\n");
|
|
|
|
}
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
// Validate the number of parameters
|
|
|
|
if (argsOld.length < 2)
|
|
|
|
{
|
|
|
|
return ERROR_INVALID_NUMBER_OF_PARAMS;
|
|
|
|
}
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
iNew = -1;
|
|
|
|
for (iOld = 0; iOld < argsOld.length; iOld++)
|
|
|
|
{
|
|
|
|
if (0 <= argsOld[iOld].indexOf("="))
|
|
|
|
{
|
|
|
|
iNew++;
|
|
|
|
args[iNew] = argsOld[iOld];
|
|
|
|
for (i = iOld + 1; i < argsOld.length && (-1 == argsOld[i].indexOf("=")); i++)
|
|
|
|
{
|
|
|
|
args[iNew] += " " + argsOld[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
log("New arg count " + args.length);
|
|
|
|
for (i = 0; i < args.length; i++)
|
|
|
|
{
|
|
|
|
log("Arg " + i + " = " + args[i] + "\r\n");
|
|
|
|
}
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
for (i = 0; i <= iNew; i++)
|
|
|
|
{
|
|
|
|
log("arg[" + i + "] = " +args[i]);
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
if (args[i].startsWith(INPUT_FILE_PARAM))
|
|
|
|
{
|
|
|
|
// Make sure it is more than the param tag
|
|
|
|
if (args[i].length() <= INPUT_FILE_PARAM.length())
|
|
|
|
{
|
|
|
|
return ERROR_BAD_INPUT_FILE_PARAM;
|
|
|
|
}
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
sInput = args[i].substring(INPUT_FILE_PARAM.length()).trim();
|
|
|
|
fileInput = new File(sInput);
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
// Make sure the input file can be found
|
|
|
|
if (!fileInput.exists())
|
|
|
|
{
|
|
|
|
log(ERROR_MISSING_INPUT_FILE, sInput);
|
|
|
|
return ERROR_MISSING_INPUT_FILE;
|
|
|
|
}
|
|
|
|
}
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
else if (args[i].startsWith(OUTPUT_FILE_PARAM))
|
|
|
|
{
|
|
|
|
// Make sure it is more than the param tag
|
|
|
|
if (args[i].length() <= OUTPUT_FILE_PARAM.length())
|
|
|
|
{
|
|
|
|
return ERROR_BAD_OUTPUT_FILE_PARAM;
|
|
|
|
}
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
sOutput = args[i].substring(OUTPUT_FILE_PARAM.length()).trim();
|
|
|
|
fileOutput = new File(sOutput);
|
|
|
|
}
|
|
|
|
}
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
// Make sure we got an input file
|
|
|
|
if (null == fileInput)
|
|
|
|
{
|
|
|
|
return ERROR_MISSING_INPUT_FILE_PARAM;
|
|
|
|
}
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
// Make sure we got an output file
|
|
|
|
if (null == fileOutput)
|
|
|
|
{
|
|
|
|
return ERROR_MISSING_OUTPUT_FILE_PARAM;
|
|
|
|
}
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
return ERROR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int createOutputFile()
|
|
|
|
{
|
|
|
|
LineNumberReader lnr = null;
|
|
|
|
FileWriter fwOutput = null;
|
|
|
|
String sLineTemplate;
|
|
|
|
String sLineOutput;
|
|
|
|
int iSearchFor;
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// Open the file
|
|
|
|
lnr = new LineNumberReader(new FileReader(fileInput));
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
return ERROR_CANNOT_READ_FILE;
|
|
|
|
}
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
fwOutput = new FileWriter(fileOutput);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
return ERROR_CANNOT_CREATE_FILE;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// For each line of text in the template file...
|
|
|
|
while (null != (sLineTemplate = lnr.readLine()))
|
|
|
|
{
|
|
|
|
sLineOutput = sLineTemplate;
|
|
|
|
log("<-- " + sLineOutput);
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
if (sLineOutput.trim().startsWith(FILE_KEY))
|
|
|
|
{
|
|
|
|
// Replace all instances of the line separator on the line
|
|
|
|
while (-1 != (iSearchFor = sLineOutput.indexOf("\\")))
|
|
|
|
{
|
|
|
|
log("replacing \\ at position " + iSearchFor + " with //");
|
|
|
|
sLineOutput = sLineOutput.substring(0, iSearchFor) + "//" +
|
2007-03-17 07:39:46 +01:00
|
|
|
sLineOutput.substring(iSearchFor + 1);
|
2007-03-05 17:41:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
fwOutput.write(sLineOutput + "\r\n");
|
|
|
|
log("--> " + sLineOutput);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
return -42;
|
|
|
|
}
|
|
|
|
}
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
// Clean up
|
|
|
|
fwOutput.flush();
|
|
|
|
fwOutput.close();
|
|
|
|
lnr.close();
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
return ERROR_OUTPUT_COPY_FAILED;
|
|
|
|
}
|
|
|
|
return ERROR_NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
void log(int err)
|
|
|
|
{
|
|
|
|
log(err, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
void log(int err, String s)
|
|
|
|
{
|
|
|
|
String sMessage = "";
|
|
|
|
|
|
|
|
switch (err)
|
|
|
|
{
|
|
|
|
case ERROR_NO_ERROR:
|
|
|
|
sMessage = "No error";
|
|
|
|
break;
|
|
|
|
case ERROR_INVALID_NUMBER_OF_PARAMS:
|
|
|
|
sMessage = "Invalid number of parameters: 2 expected";
|
|
|
|
break;
|
|
|
|
case ERROR_MISSING_INPUT_FILE:
|
|
|
|
sMessage = "Missing input file";
|
|
|
|
break;
|
|
|
|
case ERROR_OUTPUT_COPY_FAILED:
|
|
|
|
sMessage = "Unable to create output file";
|
|
|
|
break;
|
|
|
|
case ERROR_IO_EXCEPTION:
|
|
|
|
sMessage = "IOException";
|
|
|
|
break;
|
|
|
|
case ERROR_BAD_INPUT_FILE_PARAM:
|
|
|
|
sMessage = "Invalid input file parameter";
|
|
|
|
break;
|
|
|
|
case ERROR_BAD_OUTPUT_FILE_PARAM:
|
|
|
|
sMessage = "Invalid output file parameter";
|
|
|
|
break;
|
|
|
|
case ERROR_MISSING_INPUT_FILE_PARAM:
|
|
|
|
sMessage = "Missing input file parameter";
|
|
|
|
break;
|
|
|
|
case ERROR_MISSING_OUTPUT_FILE_PARAM:
|
|
|
|
sMessage = "Missing output file parameter";
|
|
|
|
break;
|
|
|
|
case ERROR_CANNOT_READ_FILE:
|
|
|
|
sMessage = "Cannot read file";
|
|
|
|
break;
|
|
|
|
case ERROR_CANNOT_CREATE_FILE:
|
|
|
|
sMessage = "Cannot create file";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sMessage = "Unknown error: " + err;
|
|
|
|
break;
|
|
|
|
}
|
2007-03-17 07:39:46 +01:00
|
|
|
|
2007-03-05 17:41:54 +01:00
|
|
|
if (null != s)
|
|
|
|
{
|
|
|
|
sMessage = sMessage + s;
|
|
|
|
}
|
|
|
|
log(sMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
void log(String s)
|
|
|
|
{
|
2007-03-17 07:39:46 +01:00
|
|
|
/*
|
2007-03-05 17:41:54 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
fw.write(this.getClass().getName() + ": " + s + "\r\n");
|
|
|
|
}
|
|
|
|
catch (IOException ioe)
|
|
|
|
{
|
|
|
|
}
|
2007-03-17 07:39:46 +01:00
|
|
|
*/
|
2007-03-05 17:41:54 +01:00
|
|
|
}
|
2007-01-16 12:21:37 +01:00
|
|
|
}
|