SetComment.java
-- BEGIN --
import org.ietf.jgss.*;
import java.lang.*;
import java.util.*;
import java.io.*;
import java.net.*;
import java.sql.*;
import stanford.netdb.utils.*;
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.security.Security;
import javax.security.auth.Subject;
import javax.security.auth.login.*;
import javax.security.auth.callback.*;
public class SetComment {
public static GSSManager manager;
public static GSSContext context;
public static KAction kAction;
// initial handle
public static NetDBi netdb_handle;
// actual handle
public static NetDBi netdb;
public static Timestamp lockedTm = null;
public SetComment() {}
public static void main(String[] cmd) {
if( cmd.length < 2 ) {
System.out.println("Usage: SetComment <fqdn node name> <comment>");
System.exit(-1);
}
try {
System.out.println("Connecting...");
netdb_handle = (NetDBi)Naming.lookup("rmi://steel.stanford.edu:20005/NetDB");
System.out.println("Authenticating...");
// set up login context
Security.setProperty("login.configuration.provider", "SecProvider");
LoginContext lc = new LoginContext("NetDBClient");
// attempt client authentication
lc.login();
netdb = netdb_handle.getInstance();
try {
// attempt client authentication
kAction = new KAction(netdb);
Subject.doAsPrivileged(lc.getSubject(), kAction, null);
// update node
System.out.println("Locking node...");
lockedTm = netdb.lock(Constants.NODE, cmd[0]);
if( lockedTm == null )
throw new Exception("Failed to lock node");
System.out.println("Loading node info...");
Hashtable oldNode = netdb.getInfo(Constants.NODE, cmd[0]);
if( oldNode == null || oldNode.get("id") == null )
throw new Exception("Failed to load node");
Hashtable newNode = Utils.copyHash(oldNode);
newNode.put("comment", cmd[1]);
newNode.put("locked", lockedTm); // netdb won't do update with out it
System.out.println("Updating node...");
netdb.update(oldNode, newNode);
// if we are here there is no need to unlock node becouse update when
// through, which automatically unlocks objects, thus set lockedTm to null
lockedTm = null;
} finally {
// try to unlock node if it is locked
if( lockedTm != null )
try {
System.out.println("Unlocking node...");
netdb.unlock(Constants.NODE, cmd[0], lockedTm);
} catch(Exception ex) {
};
// call logout to speed up clean up process on netdb server
try {
netdb.logout();
} catch(Exception ex) {
};
}
System.out.println("Done");
} catch(java.rmi.ServerException exx) {
if( exx.getCause() instanceof RemoteNetDBException ) {
RemoteNetDBException ne = (RemoteNetDBException)exx.getCause();
Vector errors = ne.getMessages();
System.out.println("ERROR(S):");
for( int i = 0; i < errors.size(); i++ )
System.out.println(errors.elementAt(i));
} else if( exx.getCause() instanceof RemoteException ) {
RemoteException re = (RemoteException)exx.getCause();
System.out.println("ERROR: "+re.getMessage());
} else {
System.out.println("ERROR: "+exx.getMessage());
}
} catch (Exception ex) {
ex.printStackTrace();
System.exit(-1);
}
}
public static class KAction implements java.security.PrivilegedExceptionAction {
public NetDBi netdb;
public KAction(NetDBi netdb) {
super();
this.netdb = netdb;
}
public Object run() throws Exception {
// This Oid is used to represent the Kerberos version 5 GSS-API mechanism.
Oid krb5Oid = new Oid("1.2.840.113554.1.2.2");
if( manager == null )
manager = GSSManager.getInstance();
GSSName serverName =
manager.createName(System.getProperty(Constants.K5_SERVICE_KEY,
"service/netdb@stanford.edu"),
null);
if( context == null )
context = manager.createContext(serverName,
krb5Oid,
null,
GSSContext.DEFAULT_LIFETIME);
context.requestMutualAuth(true); // Mutual authentication
context.requestConf(true); // Will use confidentiality later
context.requestInteg(true); // Will use integrity later
// Do the context eastablishment loop
byte[] token = new byte[0];
while (!context.isEstablished()) {
// token is ignored on the first call
token = context.initSecContext(token, 0, token.length);
// Send a token to the server if one was generated by
// initSecContext
if (token != null) {
Object retObject = netdb.klogin(token);
if( retObject instanceof byte[] )
token = (byte[])retObject;
}
}
if (context.getMutualAuthState()) {
Object retObject = netdb.klogin(null);
if( !(retObject instanceof NetDBi) )
throw new Exception("Failed to authenticate client.");
context.dispose();
}
// successful completion
return null;
} // end of run()
} // end of class KAction
}
-- END --