import com.novell.java.security.Authenticator;
import com.novell.java.security.Identity;
import com.novell.java.security.IdentityScope;
import com.novell.java.security.KeyManagementException;
import com.novell.service.security.NdsIdentity;
import com.novell.service.security.NdsPasswordIdentity;
import com.novell.service.security.NdsIdentityScope;
import com.novell.service.security.BinderyIdentity;
import com.novell.service.security.BinderyPasswordIdentity;
import com.novell.service.security.BinderyIdentityScope;
import com.novell.service.security.Password;
public class CreateTokensPI
{
static public void main(String args[])
{
if(args.length < 3)
{
System.out.println("Usage: CreateTokensPI [NDS]-or-[BINDERY] -login <domain admin-name admin-password> " +
"-create <domain user-name user-password>");
System.out.println("E.g.: CreateTokensPI [NDS] -login MyTree AdminOrgContext AdminGroupContext Admin ADMIN-PASSWORD" +
" -create MyTree MyOrgContext MyGroupContext Me MY-PASSWORD" );
System.out.println("E.g.: CreateTokensPI [BINDERY] -login MyServer Admin ADMIN-PASSWORD -create MyServer Me MY-PASSWORD");
System.exit(-1);
}
try
{
Identity loginIdentity = null;
Identity userIdentity = null;
if(args[0].equalsIgnoreCase(NdsIdentityScope.ADMINISTRATIVE_DOMAIN_NAME)) {
loginIdentity = buildNdsIdentity(null,0,getOption(args, "-login"));
userIdentity = buildNdsIdentity(null,0,getOption(args, "-create"));
}
else if(args[0].equalsIgnoreCase(BinderyIdentityScope.ADMINISTRATIVE_DOMAIN_NAME)) {
loginIdentity = buildBinderyIdentity(null,0,getOption(args, "-login"));
userIdentity = buildBinderyIdentity(null,0,getOption(args, "-create"));
}
else
{
System.out.println("You must specify either [NDS] or [BINDERY] as the first scope.");
System.exit(-1);
}
Authenticator.login(loginIdentity);
Authenticator.createTokens(loginIdentity, userIdentity);
}
catch(java.lang.Throwable e)
{
if (e instanceof java.lang.ExceptionInInitializerError)
((java.lang.ExceptionInInitializerError)e).getException().printStackTrace();
e.printStackTrace();
}
finally
{
System.exit(0);
}
}
static String [] getOption(String [] args, String option)
{
int begin = 0;
while ((begin < args.length) && (!args[begin].equalsIgnoreCase(option)))
begin ++;
if (!args[begin].equalsIgnoreCase(option))
return new String[] {args[0]};
int end = begin+1;
while ((end < args.length) && (!args[end].startsWith("-")))
end ++;
String [] params = new String [ end - begin ];
params[0] = args[0];
for (int i=1; i<params.length; i++)
params[i] = args[begin + i];
return params;
}
static Identity buildNdsIdentity(IdentityScope scope, int offset, String args[])
throws KeyManagementException
{
if( args.length == 0)
return new NdsIdentity("");
if(!(offset < args.length-2))
{
Identity pi = new NdsPasswordIdentity(args[offset],scope);
((NdsPasswordIdentity)pi).setPassword(new Password(args[offset+1]));
return pi;
}
return buildNdsIdentity(new NdsIdentityScope(args[offset],scope), ++offset, args);
}
static Identity buildBinderyIdentity(IdentityScope scope, int offset, String args[])
throws KeyManagementException
{
if( args.length == 0)
return new BinderyIdentity("");
if(!(offset < args.length-2))
{
Identity pi = new BinderyPasswordIdentity(args[offset],scope);
((BinderyPasswordIdentity)pi).setPassword(new Password(args[offset+1]));
return pi;
}
return buildBinderyIdentity(new BinderyIdentityScope(args[offset],scope), ++offset, args);
}
}