snmpUSMKeyChange.java
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:38k
源码类别:

SNMP编程

开发平台:

C/C++

  1. /* $Id: snmpUSMKeyChange.src,v 1.4.2.5 2009/01/28 13:24:48 tmanoj Exp $ */
  2. /*
  3.  * @(#)snmpUSMKeyChange.java
  4.  * Copyright (c) 1996-2009 AdventNet, Inc. All Rights Reserved.
  5.  * Please read the associated COPYRIGHTS file for more details.
  6.  */
  7. /**
  8.  * This is an example program to explain how remotely configure users
  9.  * on the agent. The procedure followed is a five step procedure.
  10.  * Step 1. GET(usmUserSpinLock.0) Value
  11.  * Strp 2. generate the keyChange value based on the secret
  12.  *         privKey of the clone-from user and the secret key
  13.  *         to be used for the new user. Let us call this keyChangeValue.
  14.  * Strp 3. GET(usmUserSpinLock.0) value
  15.  *              SET(usmUserSpinLock.0=spinLockValue,
  16.  *              usmUserKeyChange=keyChangeValue
  17.  *              usmUserPublic=randomValue)
  18.  * Strp 4. GET(usmUserPulic) and check it has randomValue
  19.  * The application sends request with version v3. 
  20.  * The user could run this application by giving any of the following usage.
  21.  *  
  22.  * java snmpUSMKeyChange [options] userName hostname 
  23.  *
  24.  * iava snmpUSMKeyChange [-d] [-p port] [-r retries] [-t timeout] [-a auth_protocol] [-w auth_password] [-s priv_password] [-pp priv_protocol(DES/AES-128/AES-192/AES-256/3DES) [-n contextName] [-i contextID] [-y new_auth_password] [-z new_priv_password] userName host
  25.  *
  26.  * e.g.
  27.  * java snmpUSMKeyChange -a MD5 -w initial2Pass -y initial2NewPass initial2 10.3.2.120
  28.  * Where, initial2 user already configured on the agent whose authProtocol is
  29.  * MD5 and authPassword is initial2Pass. newInitial is the name of the new
  30.  * user who will be configured with authProtocol=MD5 and 
  31.  * authPassword=initial2NewPass.
  32.  * Here the clone-from user is initial2, the user on whose behalf all the
  33.  * requests will be sent.
  34.  * 
  35.  * Options:
  36.  * [-d]                   - Debug output. By default off.
  37.  * [-p] <port>            - remote port no. By default 161.
  38.  * [-t] <Timeout>         - Timeout. By default 5000ms.
  39.  * [-r] <Retries>         - Retries. By default 0.      
  40.  * [-a] <autProtocol>     - The authProtocol(MD5/SHA) of the template user. Mandatory if authPassword is specified
  41.  * [-w] <authPassword>    - The authentication password of the template user.
  42.  * [-s] <privPassword>    - The privacy protocol password of the template user. Must be accompanied with auth password and authProtocol fields.
  43.  * [-n] <contextName>     - The contextName to be used for the v3 pdu.
  44.  * [-i] <contextID>       - The contextID to be used for the v3 pdu.
  45.  * [-w] <newAuthPassword> - The authentication password for the new user.
  46.  * [-s] <newPrivPassword> - The privacy protocol password of the new user. Must be accompanied with auth password and authProtocol fields.
  47.  * username Mandatory     - The user who is already configured on the agent.
  48.  *                          (template user)
  49.  * newusername Mandatory  - The user name of the new user who will be 
  50.                             Remotely configured on the agent.
  51.  * host Mandatory         - The RemoteHost (agent).Format (string without double qoutes/IpAddress).
  52.  */
  53. import java.lang.*;
  54. import java.util.*;
  55. import java.net.*;
  56. import com.adventnet.snmp.snmp2.*;
  57. import com.adventnet.snmp.snmp2.usm.*;
  58. import com.adventnet.utils.*;
  59. public class snmpUSMKeyChange
  60. {
  61.     private static final int USM_SECURITY_MODEL = 3;
  62.     private static final String ENC = "8859_1";
  63.     private static final int DEBUG = 0;
  64.     private static final int PORT = 1;
  65.     private static final int RETRIES = 2;
  66.     private static final int TIMEOUT = 3;
  67.     private static final int AUTH_PROTOCOL = 4;
  68.     private static final int AUTH_PASSWORD = 5;
  69.     private static final int PRIV_PASSWORD = 6;
  70.     private static final int CONTEXT_NAME = 7;
  71.     private static final int CONTEXT_ID = 8;
  72.     private static final int NEW_AUTH_PASSWORD = 9;
  73.     private static final int NEW_PRIV_PASSWORD = 10;
  74.     private static final int OLD_USERNAME = 11;
  75.     private static final int OLD_AUTH_PASSWORD = 12;
  76.     private static final int OLD_PRIV_PASSWORD = 13;
  77.     private static final int PRIV_PROTOCOL = 14;
  78.     private static final String SPIN_LOCK_OID = ".1.3.6.1.6.3.15.1.2.1.0";
  79.     private static final String USM_TABLE = ".1.3.6.1.6.3.15.1.2.2";
  80.     private static final String USM_ENTRY = ".1.3.6.1.6.3.15.1.2.2.1";
  81.     private static final String AUTH_OWN_KEY_CHANGE_COL = "7";
  82.     private static final String AUTH_KEY_CHANGE_COL = "6";
  83.     private static final String PRIV_OWN_KEY_CHANGE_COL = "10";
  84.     private static final String PRIV_KEY_CHANGE_COL = "9";
  85.     private static final String USM_PUBLIC_COL = "11";
  86.     private static final String ROW_STATUS_COL = "13";
  87.     private static final int AUTH_MD5_LEN = 16;
  88.     private static final int AUTH_SHA_LEN = 20;
  89.     boolean debug = false;
  90.     public static void main(String args[])
  91.     {
  92.         snmpUSMKeyChange surg = new snmpUSMKeyChange();
  93.         
  94.         // Take care of getting options
  95.         String usage =
  96.         "snmpUSMKeyChange [-d] [-p port] [-r retries] [-t timeout] n" +
  97. "[-a auth_protocol] [-w auth_password] [-s priv_password] [-pp priv_protocol(DES/AES-128/AES-192/AES-256/3DES)]n" +
  98.         "[-n contextName] [-i contextID] [-y new_auth_password] n" +
  99.         "[-z new_priv_password] [ -ou user_name] [ -ow old_auth_password] n" +
  100.         "[ -oz old_priv_password] userName  host ";
  101.         String options[] =
  102.         {
  103.             "-d", "-p", "-r", "-t", "-a",
  104.             "-w", "-s", "-n", "-i", "-y",
  105.             "-z", "-ou", "-ow", "-oz", "-pp"
  106.         };
  107.         String values[] =
  108.         {
  109.             "None", null, null, null, null,
  110.             null,   null, null, null, null,
  111.             null,   null, null,null, null
  112.         };
  113.         String userName = new String("");
  114.         int authProtocol = USMUserEntry.NO_AUTH;
  115. int privProtocol = USMUserEntry.NO_PRIV;
  116.         String authPassword = null;
  117.         String privPassword = null;
  118.         String contextName = null;
  119.         String contextID = null;
  120.         //int newAuthProtocol = USMUserEntry.NO_AUTH;
  121.         String newAuthPassword = null;
  122.         String newPrivPassword = null;
  123.         
  124.         // Old sec Parameters for the user
  125.         byte oldAuthProtocol = USMUserEntry.MD5_AUTH;
  126. byte oldPrivProtocol = USMUserEntry.CBC_DES;
  127.         String oldAuthPassword = null;
  128.         String oldPrivPassword = null;
  129.         String oldUserName = null;
  130.         boolean ownKeyChange=false;
  131.         ParseOptions opt = new ParseOptions(args,options,values, usage);
  132.         if (opt.remArgs.length<2)
  133.         {
  134.             opt.usage_error();
  135.         }
  136.         // Start SNMP API
  137.         SnmpAPI api;
  138.         api = new SnmpAPI();
  139.         if (values[DEBUG].equals("Set"))
  140.         {
  141.             api.setDebug( true );
  142.             surg.debug = true;
  143.         }
  144.     
  145.         userName = opt.remArgs[0];
  146.         // Open session
  147.         SnmpSession session = new SnmpSession(api);        
  148.         
  149.         // set remote Host 
  150.         UDPProtocolOptions ses_opt = new UDPProtocolOptions();
  151.         ses_opt.setRemoteHost(opt.remArgs[1]);
  152.         // Set the values accepted from the command line
  153.         //boolean usage_error = false;
  154.     
  155.         //set remote Port, timeout,retries if needed.
  156.         try
  157.         {
  158.             if (values[PORT] != null)
  159.             {
  160.                 ses_opt.setRemotePort( Integer.parseInt(values[PORT]) );
  161.             }
  162.             if (values[RETRIES] != null)
  163.             {
  164.                 session.setRetries( Integer.parseInt(values[RETRIES]) );
  165.             }
  166.             if (values[TIMEOUT] != null)
  167.             {
  168.                 session.setTimeout( Integer.parseInt(values[TIMEOUT]) );
  169.             }
  170.         }
  171.         catch (NumberFormatException ex)
  172.         {
  173.             System.err.println("Invalid Integer Arg: " + ex.getMessage());
  174.             System.exit(1);
  175.         }
  176.         session.setProtocolOptions(ses_opt);
  177.         session.setVersion( SnmpAPI.SNMP_VERSION_3 );
  178.     
  179.         
  180.         if ((values[AUTH_PROTOCOL] != null))
  181.         {
  182.             if(values[AUTH_PROTOCOL].equals("SHA"))
  183.             {
  184.                 authProtocol = USMUserEntry.SHA_AUTH;
  185.             }
  186.             else
  187.             {
  188.                 authProtocol = USMUserEntry.MD5_AUTH;
  189.             }
  190.         }
  191.         if(values[AUTH_PASSWORD]!=null)
  192.         {
  193.             authPassword=values[AUTH_PASSWORD];
  194.         }
  195.         if(values[PRIV_PASSWORD]!=null)
  196.         {
  197.             privPassword=values[PRIV_PASSWORD];
  198.               privProtocol= USMUserEntry.CBC_DES;
  199.    
  200.                if( values[PRIV_PROTOCOL] != null)
  201.                {
  202.                        if(values[PRIV_PROTOCOL].equals("DES"))
  203.                        {
  204.                        privProtocol= USMUserEntry.CBC_DES;
  205.                        }
  206.                        else if(values[PRIV_PROTOCOL].equals("AES-128"))
  207.                        {
  208.                                privProtocol= USMUserEntry.CFB_AES_128;
  209.                        }
  210.                        else if(values[PRIV_PROTOCOL].equals("AES-192"))
  211.                        {
  212.                                privProtocol= USMUserEntry.CFB_AES_192;
  213.                        }
  214.                        else if(values[PRIV_PROTOCOL].equals("AES-256"))
  215.                        {
  216.                                privProtocol= USMUserEntry.CFB_AES_256;
  217.                        }
  218.      else if(values[PRIV_PROTOCOL].equals("3DES"))
  219.                        {
  220.                                privProtocol= USMUserEntry.CBC_3DES;
  221.                        }
  222.                        else
  223.                        {
  224.                                System.out.println("Invalid privProtocol is given ");
  225.                                System.exit(1);
  226.                        }
  227.                }
  228. }
  229.         /// for old user
  230.         if(values[OLD_USERNAME]==null)
  231.         {
  232.             ownKeyChange=true;
  233.         }
  234.         else
  235.         {
  236.             oldUserName=values[OLD_USERNAME];
  237.         }
  238.         if(!ownKeyChange)
  239.         {
  240.             if(!(values[OLD_AUTH_PASSWORD]!=null && values[AUTH_PASSWORD]!=null
  241.                     && values[NEW_AUTH_PASSWORD]!=null))
  242.             {
  243.                 opt.usage_error();
  244.             }
  245.             else
  246.             {
  247.                 oldAuthPassword = values[OLD_AUTH_PASSWORD];
  248.                 newAuthPassword = values[NEW_AUTH_PASSWORD];
  249.                 authPassword = values[AUTH_PASSWORD];
  250.             }
  251.         
  252.             if(values[OLD_PRIV_PASSWORD]!=null)
  253.             {
  254.                 if(values[PRIV_PASSWORD]==null)
  255.                 {
  256.                     opt.usage_error();
  257.                 }
  258.                 else
  259.                 {
  260.                     if(values[NEW_PRIV_PASSWORD]==null)
  261.                     {
  262.                         opt.usage_error();
  263.                     }
  264.                     else
  265.                     {
  266.                         oldPrivPassword = values[OLD_AUTH_PASSWORD];
  267.                         newPrivPassword = values[NEW_AUTH_PASSWORD];
  268.                         privPassword = values[AUTH_PASSWORD];
  269.                     }
  270.                 }
  271.             }
  272.             else
  273.             {
  274.                 privPassword=null;
  275.             }
  276.         }
  277.         else
  278.         {
  279.             if(values[AUTH_PASSWORD]==null)
  280.             {
  281.                 opt.usage_error();
  282.             }
  283.             else
  284.             {
  285.                 authPassword=values[AUTH_PASSWORD];
  286.                 if(values[NEW_AUTH_PASSWORD]!=null)
  287.                 {
  288.                     newAuthPassword=values[NEW_AUTH_PASSWORD];
  289.                 }
  290.                 else
  291.                 {
  292.                     opt.usage_error();
  293.                 }
  294.             }
  295.             if(values[PRIV_PASSWORD]!=null)
  296.             {
  297.                 privPassword=values[PRIV_PASSWORD];
  298.                 if(values[NEW_PRIV_PASSWORD]!=null)
  299.                 {
  300.                     newPrivPassword=values[NEW_PRIV_PASSWORD];
  301.                 }
  302.             }
  303.         }
  304.         
  305.         if (values[CONTEXT_NAME] != null)
  306.         {
  307.             contextName = values[CONTEXT_NAME];
  308.         }
  309.         if (values[CONTEXT_ID] != null)
  310.         {
  311.             contextID = values[CONTEXT_ID];
  312.         }
  313.         // Build Get request PDU
  314.         SnmpPDU pdu = new SnmpPDU();
  315.         try
  316.         {
  317.             //Open session
  318.             session.open();
  319.         }
  320.         catch (SnmpException e)
  321.         {
  322.             System.err.println("Error opening session:"+e.getMessage());
  323.             System.exit(1);
  324.         }
  325.         // inititialize the manager by adding the user. All requests will
  326.         // sent with this username 
  327.         pdu.setUserName(userName.getBytes());
  328.         try
  329.         {
  330.             USMUtils.init_v3_parameters(
  331.                 userName,
  332. null,
  333.                 authProtocol,
  334.                 authPassword, 
  335.                 privPassword,
  336. ses_opt,
  337. session,
  338. false,
  339. privProtocol);
  340.         }
  341.         catch(Exception exp)
  342.         {
  343.             System.out.println(exp.getMessage());
  344.             System.exit(1);
  345.         }
  346.         if(contextName!=null)
  347.         {
  348.             pdu.setContextName(contextName.getBytes());
  349.         }
  350.         if(contextID!=null)
  351.         {
  352.             pdu.setContextID(contextID.getBytes());
  353.         }
  354.         // A valid user is now configured.on the manager. 
  355.         System.out.println("A new user " + userName + " is now " +
  356.                              "configured on the manager"); 
  357.         // Get the SpinLock to use in the next SET request.
  358.         int spinLock = surg.sendSpinLockRequest(pdu,session);
  359.         if(spinLock < 0)
  360.         {
  361.             System.out.println("Error in retriving SnmpLock");
  362.             System.exit(1);
  363.         }
  364.         // Since we are reusing the PDU, we will remove the varbinds
  365.         // and set the reqid to 0.
  366.         surg.removeAllVarBinds(pdu);
  367.         //pdu.setReqid(0);
  368.         byte[] engineID = ((Snmp3Message)pdu.getMsg()).
  369.                                         getSecurity().getEngineID();
  370.         String engID;
  371.         try
  372.         {
  373.             engID = new String(engineID, ENC);
  374.         }
  375.         catch(Exception e)
  376.         {
  377.             engID = new String(engineID);
  378.         }
  379.         int[] firstindex = surg.stringToIntegerArray(engID);
  380.         String engIDOID = surg.intArrayToString(firstindex);
  381.         int[] userIndex=null;
  382.         if(ownKeyChange)
  383.         {
  384.             userIndex = surg.stringToIntegerArray(userName);
  385.         }
  386.         else
  387.         {
  388.             userIndex = surg.stringToIntegerArray(oldUserName);
  389.         }
  390.                 
  391.         String userNameOID = surg.intArrayToString(userIndex);
  392.         
  393.         // Initialize the random value based on the protocol used.
  394.         byte[] random;
  395.         if(authProtocol == USMUserEntry.MD5_AUTH)
  396.         {
  397.             random = new byte[AUTH_MD5_LEN];
  398.         }
  399.         else
  400.         {
  401.             random = new byte[AUTH_SHA_LEN];
  402.         }
  403.         byte[] engID2 = null;
  404.         try
  405.         {
  406.             engID2 = engID.getBytes(ENC);
  407.         }
  408.         catch(Exception ex)
  409.         {
  410.             engID2 = engID.getBytes();  
  411.         }
  412.         USMUserTable userTable = (USMUserTable)api.getSecurityProvider().
  413.                                     getTable(USM_SECURITY_MODEL);
  414.         USMUserEntry entry = userTable.getEntry(userName.getBytes(),
  415.                                                 engID2);
  416.         
  417.         byte[] oldKey=null;
  418.         if(ownKeyChange)
  419.         {
  420.             oldKey = entry.getAuthKey();
  421.         }
  422.         else
  423.         {
  424.             oldKey = USMUtils.password_to_key(
  425.                             oldAuthProtocol,
  426.                             oldAuthPassword.getBytes(),
  427.                             oldAuthPassword.getBytes().length,
  428.                             engineID);
  429.         }
  430.         // Generate the keyChange value based on the secret
  431.         // authKey of the  user and the new secret key
  432.         // to be used for the user. Let us call this akcValue.
  433.     
  434.         String akcValue = surg.getKeyChangeValue(
  435.                                 engID,
  436.                                 authProtocol,
  437.                                 newAuthPassword,
  438.                                 oldKey,
  439.                                 random,
  440. false, privProtocol);
  441.     
  442.         if(authProtocol == USMUserEntry.MD5_AUTH)
  443.         {
  444.             try
  445.             {
  446.                 random = akcValue.substring(0,AUTH_MD5_LEN).getBytes(ENC);
  447.             }
  448.             catch(Exception e)
  449.             {
  450.                 random = akcValue.substring(0,AUTH_MD5_LEN).getBytes();
  451.             }
  452.         }
  453.         else
  454.         {
  455.             try
  456.             {
  457.                 random = akcValue.substring(0,AUTH_SHA_LEN).getBytes(ENC);
  458.             }
  459.             catch(Exception e)
  460.             {
  461.                 random = akcValue.substring(0,AUTH_SHA_LEN).getBytes();
  462.             }
  463.         }
  464.         String keyChangeOID=null;
  465.         if(ownKeyChange)
  466.         {
  467.             keyChangeOID = USM_ENTRY + "." + AUTH_OWN_KEY_CHANGE_COL + "." 
  468.                 + firstindex.length + engIDOID + "." 
  469.                 + userIndex.length + userNameOID;
  470.         }
  471.         else
  472.         {
  473.             keyChangeOID = USM_ENTRY + "." + AUTH_KEY_CHANGE_COL + "." 
  474.                 + firstindex.length + engIDOID + "." 
  475.                 + userIndex.length + userNameOID;
  476.         }
  477.         String randomOID = USM_ENTRY + "." + USM_PUBLIC_COL + "." +
  478.                 + firstindex.length + engIDOID + "." + 
  479.                 userIndex.length + userNameOID;
  480.                 
  481.         SnmpOID setOID1 = new SnmpOID(SPIN_LOCK_OID);
  482.         surg.addvarbind(pdu, setOID1,"INTEGER",
  483.                             new Integer(spinLock).toString());
  484.         
  485.         SnmpOID setOID2 = new SnmpOID(keyChangeOID); //check this up
  486.         surg.addvarbind(pdu, setOID2,"STRING",akcValue);
  487.         SnmpOID setOID3 = new SnmpOID(randomOID);
  488.         String randomString;
  489.         try
  490.         {
  491.             randomString = new String(random,ENC);
  492.         }
  493.         catch(Exception e)
  494.         {
  495.             randomString = new String(random);
  496.         }
  497.         surg.addvarbind(pdu, setOID3,"STRING", randomString);
  498.         System.out.println("Sending a request to set the authKeyChangen");
  499.         pdu.setCommand( SnmpAPI.SET_REQ_MSG );
  500.         try
  501.         {
  502.             // Send PDU and receive response PDU
  503.             System.out.println("Sending Request for KeyChange");
  504.             pdu = session.syncSend(pdu);
  505.         }
  506.         catch (SnmpException e)
  507.         {
  508.             System.err.println("Sending PDU"+e.getMessage());
  509.             System.exit(1);
  510.         }    
  511.         if (pdu == null)
  512.         {
  513.             // timeout
  514.             System.out.println("Request timed out to: " + opt.remArgs[0] );
  515.             System.exit(1);
  516.         }
  517.         System.out.println("Response PDU for keyChange  received from " +
  518.                             pdu.getProtocolOptions().getSessionId());
  519.         if (pdu.getErrstat() != 0)
  520.         {
  521.             System.out.println("KeyChange SET request returned error "
  522.                                     + "User NOT Successfully cloned");
  523.             System.err.println(pdu.getError());
  524.             System.exit(1);
  525.         }
  526.         else
  527.         {
  528.             // print the response pdu varbinds
  529.             System.out.println(pdu.printVarBinds());
  530.         }
  531.         // Since we are reusing the PDU, we will remove the varbinds
  532.         // and set the reqid to 0.
  533.         surg.removeAllVarBinds(pdu);
  534.         //pdu.setReqid(0);
  535.         // This is to set the new AuthKey on our side for receiving 
  536.         //the usmUserPublic
  537.         // value after it has been set.
  538.         if(ownKeyChange)
  539.         {
  540.             entry.setAuthPassword(newAuthPassword.getBytes());
  541.             byte[] engID1 = null;
  542.             try
  543.             {
  544.                 engID1 = engID.getBytes(ENC);
  545.             }
  546.             catch(Exception ex)
  547.             {
  548.                 engID1 = engID.getBytes();  
  549.             }           
  550.             byte[] newKey = USMUtils.password_to_key(authProtocol,
  551.                             newAuthPassword.getBytes(),
  552.                             newAuthPassword.getBytes().length,
  553.                             engID1);
  554.             entry.setAuthKey(newKey);
  555.         }
  556.         // Get the usmUserPublic value
  557.         pdu.setCommand( SnmpAPI.GET_REQ_MSG );
  558.         
  559.         SnmpOID oid = new SnmpOID(randomOID);
  560.         if (oid.toValue() == null) 
  561.         {
  562.             System.err.println("Invalid OID argument: " + randomOID);
  563.         }
  564.         else
  565.         {
  566.             pdu.addNull(oid);
  567.         }
  568.          
  569.         try
  570.         {
  571.             // Send PDU and receive response PDU
  572.             pdu = session.syncSend(pdu);
  573.         }
  574.         catch (SnmpException e)
  575.         {
  576.             System.err.println("Sending PDU "+e.getMessage());
  577.             System.exit(1);
  578.         }    
  579.         if (pdu == null)
  580.         {
  581.             // timeout
  582.             System.out.println("Request timed out to: " + opt.remArgs[0] );
  583.             System.exit(1);
  584.         }
  585.         // print and exit
  586.         System.out.println("Response PDU for usmUserPublic  received from " +
  587.                             pdu.getProtocolOptions().getSessionId());
  588.             
  589.         // Check for error in response
  590.         if (pdu.getErrstat() != 0)
  591.         {
  592.             System.out.println("usmUserPublic GET request returned error "
  593.                                     + "User NOT Successfully cloned");
  594.             System.err.println(pdu.getError());
  595.             System.exit(1);
  596.         }
  597.         else
  598.         {
  599.             // print the response pdu varbinds
  600.             System.out.println(pdu.printVarBinds());
  601.         }
  602.         String userPublic = (pdu.getVariable(0)).toString();
  603.         String tempRandom;
  604.         try
  605.         {
  606.             tempRandom = new String(random,ENC);
  607.         }
  608.         catch(Exception e)
  609.         {
  610.             tempRandom = new String(random);
  611.         }
  612.         if(userPublic.equals(tempRandom))
  613.         {
  614.             System.out.println("usmUserPulic value is set appropriatelyn");
  615.         }
  616.         else
  617.         {
  618.             System.out.println("usmUserPulic value is NOT set appropriately");
  619.             System.out.println("User NOT Successfully cloned");
  620.             System.exit(1);
  621.         }
  622.         // Since we are reusing the PDU, we will remove the varbinds
  623.         // and set the reqid to 0.
  624.         surg.removeAllVarBinds(pdu);
  625.         //pdu.setReqid(0);
  626.         // Start the privKeyChange
  627.         if(privPassword!=null && privPassword.length()>0 && newPrivPassword!=null && newPrivPassword.length()>0)
  628.         {
  629.             newPrivPassword = values[NEW_PRIV_PASSWORD];
  630.             SnmpPDU pdu1 = new SnmpPDU();
  631.             pdu1.setUserName(userName.getBytes());
  632.             if(contextName!=null)
  633.             {
  634.                 pdu1.setContextName(contextName.getBytes());
  635.             }
  636.             if(contextID!=null)
  637.             {
  638.                 pdu1.setContextID(contextID.getBytes());
  639.             }
  640.             // Step 1. Retrive the USMUserSpinLock
  641.             spinLock = surg.sendSpinLockRequest(pdu1,session);
  642.             if(spinLock < 0)
  643.             {
  644.                 System.out.println("Error in retriving SnmpLock");
  645.                 System.exit(1);
  646.             }
  647.             System.out.println("Spin lock value retrived successfullyn");
  648.             // Since we are reusing the PDU, we will remove the varbinds
  649.             // and set the reqid to 0.
  650.             surg.removeAllVarBinds(pdu1);
  651.             // Initialize the random value based on the protocol used.
  652.             byte[] priv_random = null;
  653.              // To support privProtocols such as DES/AES-128/AES-192/AES-256/3DES.
  654.                if( values[PRIV_PROTOCOL] != null)
  655.                {
  656.                        if(values[PRIV_PROTOCOL].equals("DES") ||values[PRIV_PROTOCOL].equals("AES-128") )
  657.                        {
  658.                          priv_random = new byte[16];
  659.                        }
  660.                        else if(values[PRIV_PROTOCOL].equals("AES-192"))
  661.                        {
  662.                               priv_random = new byte[24];
  663.                        }
  664.                         else if(values[PRIV_PROTOCOL].equals("AES-256"))
  665.                        {
  666.                                priv_random = new byte[32];
  667.                        }
  668.      else if(values[PRIV_PROTOCOL].equals("3DES"))
  669.                        {
  670.                                priv_random = new byte[32];
  671.                        }
  672.                      
  673.                }
  674.                else
  675.                {
  676.                    priv_random = new byte[16];            
  677.                }
  678.             byte[] oldPrivKey=null;
  679.             if(ownKeyChange)
  680.             {
  681.                 oldPrivKey = entry.getPrivKey();
  682.             }
  683.             else
  684.             {
  685.                 
  686.                 oldPrivKey = USMUtils.password_to_key(
  687.                                 oldAuthProtocol,
  688.                                 oldPrivPassword.getBytes(),
  689.                                 oldPrivPassword.getBytes().length,
  690. engineID, privProtocol);
  691.             }
  692.             String pkcValue=null;
  693.             // Generate the keyChange value based on the secret
  694.             // privKey of the user and the new secret key
  695.             // to be used for the user. Let us call this pkcValue.
  696.             pkcValue = surg.getKeyChangeValue(
  697.                             engID,
  698.                             authProtocol,
  699.                             newPrivPassword,
  700.                             oldPrivKey,
  701.                             priv_random,
  702.                             true, privProtocol);
  703.             try
  704.             {
  705.                 priv_random = pkcValue.substring(0,priv_random.length).getBytes(ENC);
  706.             }
  707.             catch(Exception e)
  708.             {
  709.                 priv_random = pkcValue.substring(0,priv_random.length).getBytes();
  710.             }
  711.             if(ownKeyChange)
  712.             {
  713.                 keyChangeOID = USM_ENTRY + "." + PRIV_OWN_KEY_CHANGE_COL + "." 
  714.                         + firstindex.length + engIDOID + "." 
  715.                         + userIndex.length + userNameOID;
  716.             }
  717.             else
  718.             {
  719.                 
  720.                 keyChangeOID = USM_ENTRY + "." + PRIV_KEY_CHANGE_COL + "." 
  721.                     + firstindex.length + engIDOID + "." 
  722.                     + userIndex.length + userNameOID;
  723.             }
  724.             randomOID = USM_ENTRY + "." + USM_PUBLIC_COL + "." +
  725.                     + firstindex.length + engIDOID + "." + 
  726.                     userIndex.length + userNameOID;
  727.             setOID1 = new SnmpOID(SPIN_LOCK_OID);
  728.             surg.addvarbind(pdu1, setOID1,"INTEGER",
  729.                             new Integer(spinLock).toString());
  730.             setOID2 = new SnmpOID(keyChangeOID); //check this up
  731.             surg.addvarbind(pdu1, setOID2,"STRING",pkcValue);
  732.             setOID3 = new SnmpOID(randomOID);
  733.             try
  734.             {
  735.                 randomString = new String(priv_random,ENC);
  736.             }
  737.             catch(Exception e)
  738.             {
  739.                 randomString = new String(priv_random);
  740.             }
  741.             surg.addvarbind(pdu1, setOID3,"STRING", randomString);
  742.             System.out.println("Sending a request to set the privKeyChangen");
  743.             pdu1.setCommand( SnmpAPI.SET_REQ_MSG );
  744.             try
  745.             {
  746.                 // Send PDU and receive response PDU
  747.                 pdu1 = session.syncSend(pdu1);
  748.             }
  749.             catch (SnmpException e)
  750.             {
  751.                 System.err.println("Sending PDU"+e.getMessage());
  752.                 System.exit(1);
  753.             }    
  754.             if (pdu1 == null)
  755.             {
  756.                 // timeout
  757.                 System.out.println("Request timed out to: " + opt.remArgs[0] );
  758.                 System.exit(1);
  759.             }
  760.             System.out.println("Response PDU for keyChange  received from " +
  761.                             pdu1.getProtocolOptions().getSessionId());
  762.             if (pdu1.getErrstat() != 0)
  763.             {
  764.                 System.out.println("KeyChange SET request returned error "
  765.                                 + "User NOT Successfully cloned");
  766.                 System.err.println(pdu1.getError());
  767.                 System.exit(1);
  768.             }
  769.             else 
  770.             {
  771.                 // print the response pdu varbinds
  772.                 System.out.println(pdu1.printVarBinds());
  773.             }
  774.             // Since we are reusing the PDU, we will remove the varbinds
  775.             // and set the reqid to 0.
  776.             surg.removeAllVarBinds(pdu1);
  777.             //pdu.setReqid(0);
  778.             if(ownKeyChange)
  779.             {
  780.                 entry.setPrivPassword(newPrivPassword.getBytes());
  781.                 byte[] engID1 = null;
  782.                 try
  783.                 {
  784.                     engID1 = engID.getBytes(ENC);
  785.                 }
  786.                 catch(Exception ex)
  787.                 {
  788.                     engID1 = engID.getBytes();
  789.                 }               
  790.                 // This is to set the new PrivKey on our side for receiving 
  791.                 //the usmUserPublic
  792.                 // value after it has been set on the Agent.
  793.                 byte[] newPrivKey = USMUtils.password_to_key(authProtocol,
  794.                                 newPrivPassword.getBytes(),
  795.                                 newPrivPassword.getBytes().length,
  796.                                 engID1,privProtocol);
  797.                 //  byte[] newPrivKey = new byte[16];
  798.                 //  System.arraycopy(tempKey,0,newPrivKey,0,16);
  799. entry.setPrivProtocol(privProtocol);
  800.                 entry.setPrivKey(newPrivKey);
  801.             }
  802.             // Get the usmUserPublic value
  803.             pdu1.setCommand( SnmpAPI.GET_REQ_MSG );
  804.             oid = new SnmpOID(randomOID);
  805.             if (oid.toValue() == null) 
  806.             {
  807.                 System.err.println("Invalid OID argument: " + randomOID);
  808.             }
  809.             else
  810.             {
  811.                 pdu1.addNull(oid);
  812.             }
  813.             try
  814.             {
  815.                 // Send PDU and receive response PDU
  816.                 pdu1 = session.syncSend(pdu1);
  817.             }
  818.             catch (SnmpException e)
  819.             {
  820.                 System.err.println("Sending PDU "+e.getMessage());
  821.                 System.exit(1);
  822.             }    
  823.             if (pdu1 == null)
  824.             {
  825.                 // timeout
  826.                 System.out.println("Request timed out to: " + opt.remArgs[0] );
  827.                 System.exit(1);
  828.             }
  829.             // print and exit
  830.             System.out.println("Response PDU for usmUserPublic  received from " +
  831.                             pdu1.getProtocolOptions().getSessionId());
  832.             // Check for error in response
  833.             if (pdu1.getErrstat() != 0)
  834.             {
  835.                 System.out.println("usmUserPublic GET request returned error "
  836.                                 + "User NOT Successfully cloned");
  837.                 System.err.println(pdu1.getError());
  838.                 System.exit(1);
  839.             }
  840.             else 
  841.             {
  842.                 // print the response pdu varbinds
  843.                 System.out.println(pdu1.printVarBinds());
  844.             }
  845.             userPublic = (pdu1.getVariable(0)).toString();
  846.             try
  847.             {
  848.                 tempRandom = new String(priv_random,ENC);
  849.             }
  850.             catch(Exception e)
  851.             {
  852.                 tempRandom = new String(priv_random);
  853.             }
  854.             if(userPublic.equals(tempRandom))
  855.             {
  856.                     System.out.println("usmUserPulic value is set appropriatelyn");
  857.             }
  858.             else
  859.             {
  860.                 System.out.println("usmUserPulic value is NOT set appropriately");
  861.                 System.out.println("User NOT Successfully cloned");
  862.                 System.exit(1);
  863.             }
  864.         }
  865.         System.out.println("Key Change SUCCESSFULL!!!");
  866.         // close session
  867.         session.close();
  868.         // stop api thread
  869.         api.close();
  870.     
  871.     }
  872.     int sendSpinLockRequest(SnmpPDU pdu,SnmpSession session)
  873.     {
  874.         // Get the usmUserSpinLock value. 
  875.         System.out.println("nSending a request for retriving the " +
  876.                                             " usmUserSpinLock valuen");
  877.         pdu.setCommand( SnmpAPI.GET_REQ_MSG );
  878.         SnmpOID oid = new SnmpOID(SPIN_LOCK_OID);
  879.         if (oid.toValue() == null) 
  880.         {
  881.             System.err.println("Invalid OID argument: " + SPIN_LOCK_OID);
  882.         }
  883.         else 
  884.         {
  885.             pdu.addNull(oid);
  886.         }
  887.          
  888.         try
  889.         {
  890.             // Send PDU and receive response PDU
  891.             pdu = session.syncSend(pdu);
  892.         }
  893.         catch (SnmpException e)
  894.         {
  895.             System.err.println("Sending PDU "+e.getMessage());
  896.             return -1;
  897.         }    
  898.         if (pdu == null)
  899.         {
  900.             // timeout
  901.             System.out.println("Request timed out to: remote host ");
  902.             return -1;
  903.         }
  904.         // print and exit
  905.         System.out.println("Response PDU for usmUserSpinLock received from " +
  906.                             pdu.getProtocolOptions().getSessionId());
  907.             
  908.         // Check for error in response
  909.         if (pdu.getErrstat() != 0)
  910.         {
  911.             System.err.println(pdu.getError());
  912.             return -1;
  913.         }
  914.         else 
  915.         {
  916.             // print the response pdu varbinds
  917.             System.out.println(pdu.printVarBinds());
  918.         }
  919.         SnmpVarBind vb = pdu.getVariableBinding(0);
  920.         SnmpVar var = vb.getVariable();
  921.         int spinLock = Integer.parseInt(var.toString()); 
  922.         return spinLock;
  923.     }
  924.     String getKeyChangeValue(String engID,
  925.                             int protocol,String password,
  926.                             byte[] keyOld,byte[] random,boolean isPriv, int privProtocol)
  927.     {
  928.         byte[] engID1 = null;
  929.         try
  930.         {
  931.             engID1 = engID.getBytes(ENC);
  932.         }
  933.         catch(Exception exp)
  934.         {
  935.             engID1 = engID.getBytes();  
  936.         }   
  937.         byte[] localizedKey = null;
  938. int keyLength = AUTH_MD5_LEN;
  939. int hashLength = AUTH_MD5_LEN;
  940. if(isPriv)
  941. {
  942. localizedKey = USMUtils.password_to_key(
  943. protocol,
  944. password.getBytes(),
  945. password.getBytes().length,
  946. engID1,privProtocol);
  947.                 if(privProtocol != USMUserEntry.NO_PRIV )
  948.           {
  949.            if(privProtocol == USMUserEntry.CFB_AES_192 )
  950.                   {
  951.                    keyLength = 24;
  952.                           hashLength =24;
  953.                   }
  954.                   else if(privProtocol == USMUserEntry.CFB_AES_256)
  955.                   {
  956.                    keyLength  = 32;
  957.                           hashLength = 32;
  958.                   }
  959. else if(privProtocol == USMUserEntry.CBC_3DES)
  960.                   {
  961.                    keyLength  = 32;
  962.                           hashLength = 32;
  963.                   }
  964.           }
  965.    
  966.   }
  967.   else
  968.   {
  969.   localizedKey = USMUtils.password_to_key(
  970.                                        protocol,
  971.                                        password.getBytes(),
  972.                                        password.getBytes().length,
  973.                                        engID1);
  974.           if(protocol == USMUserEntry.SHA_AUTH && !isPriv)
  975.           {
  976.            keyLength = AUTH_SHA_LEN;
  977.                   hashLength = AUTH_SHA_LEN;
  978.           }
  979.   }
  980.     
  981.         if(debug)
  982.         {
  983.             System.out.println("The old localized key is " + 
  984.             USMUtils.printOctets(keyOld, keyOld.length) + "n");
  985.             System.out.println("The new localized key is " + 
  986.             USMUtils.printOctets(localizedKey, localizedKey.length) + "n"); 
  987.         }
  988.         byte[] keyChange = USMUtils.getKeyChange(
  989.                                 protocol,
  990.                                 true,
  991.                                 keyLength,
  992.                                 hashLength,
  993.                                 localizedKey,
  994.                                 keyOld,
  995.                                 random);
  996.         if(debug)
  997.         {
  998.             System.out.println("The keyChange is " + 
  999.             USMUtils.printOctets(keyChange, keyChange.length) + "n");
  1000.             System.out.println("The random is " + 
  1001.             USMUtils.printOctets(random, random.length) + "n");
  1002.         }
  1003.         String kChange;
  1004.         try
  1005.         {
  1006.             kChange =  new String(keyChange,ENC);
  1007.         }
  1008.         catch(Exception e)
  1009.         {
  1010.             kChange =  new String(keyChange);
  1011.         }
  1012.         return kChange;
  1013.     }
  1014.     void removeAllVarBinds(SnmpPDU pdu)
  1015.     {
  1016.         int size = pdu.getVariableBindings().size();
  1017.         for(int i = 0; i < size; i++)
  1018.         {
  1019.             pdu.removeVariableBinding(0);
  1020.         }
  1021.     }
  1022.     static public int[] stringToIntegerArray(String str)
  1023.     {
  1024.         int[] instanceOID = new int[str.length()];
  1025.         byte[] temp = null;
  1026.         try
  1027.         {
  1028.             temp = str.getBytes(ENC);
  1029.         }
  1030.         catch(Exception ex)
  1031.         {
  1032.             temp = str.getBytes();  
  1033.         }
  1034.         for (int i=0;i<temp.length;i++)
  1035.         {
  1036.             instanceOID[i] = (int)(temp[i]) & 0xff;
  1037.         }
  1038.         return instanceOID;
  1039.     }
  1040.     static String intArrayToString(int[] intArray)
  1041.     {
  1042.         StringBuffer s = new StringBuffer();
  1043.         for (int i=0;i<intArray.length;i++)
  1044.         {
  1045.             s.append("."+Integer.toString(intArray[i]));
  1046.         }
  1047.         return s.toString();
  1048.     }
  1049.     /** adds the varbind  with specified oid, type and value to the pdu */
  1050.     static void addvarbind(SnmpPDU pdu, SnmpOID oid, String type, String value)
  1051.     {
  1052.         byte dataType ;
  1053.         if (type.equals("INTEGER"))
  1054.         {
  1055.             dataType = SnmpAPI.INTEGER;
  1056.         }
  1057.         else if (type.equals("STRING"))
  1058.         {
  1059.             dataType = SnmpAPI.STRING;
  1060.         }
  1061.         else if (type.equals("GAUGE"))
  1062.         {
  1063.             dataType = SnmpAPI.GAUGE;
  1064.         }
  1065.         else if (type.equals("TIMETICKS"))
  1066.         {
  1067.             dataType = SnmpAPI.TIMETICKS;
  1068.         }
  1069.         else if (type.equals("OPAQUE"))
  1070.         {
  1071.             dataType = SnmpAPI.OPAQUE;
  1072.         }
  1073.         else if (type.equals("IPADDRESS"))
  1074.         {
  1075.             dataType = SnmpAPI.IPADDRESS;
  1076.         }
  1077.         else if (type.equals("COUNTER"))
  1078.         {
  1079.             dataType = SnmpAPI.COUNTER;
  1080.         }
  1081.         else if (type.equals("OID"))
  1082.         {
  1083.             dataType = SnmpAPI.OBJID;
  1084.         }
  1085.         else if (type.equals("BITS"))
  1086.         { 
  1087.             dataType = SnmpAPI.STRING;
  1088.         }
  1089.         else
  1090.         { 
  1091.             System.err.println("Invalid variable type: " + type);
  1092.             return;
  1093.         }
  1094.         SnmpVar var = null;
  1095.         try
  1096.         {
  1097.             // create SnmpVar instance for the value and the type
  1098.             var = SnmpVar.createVariable( value, dataType );
  1099.         }
  1100.         catch(SnmpException e)
  1101.         {
  1102.             System.err.println("Cannot create variable: " + 
  1103.             oid + " with value: " + value);
  1104.             return;
  1105.         }
  1106.         //create varbind
  1107.         SnmpVarBind varbind = new SnmpVarBind(oid, var);
  1108.         // add variable binding
  1109.         pdu.addVariableBinding(varbind);
  1110.     }
  1111. }