static String mapHostToRealm(String name) {
   String result = null;
   try {
     String subname = null;
     Config c = Config.getInstance();
     if ((result = c.get("domain_realm", name)) != null) return result;
     else {
       for (int i = 1; i < name.length(); i++) {
         if ((name.charAt(i) == '.')
             && (i != name.length() - 1)) { // mapping could be .ibm.com = AUSTIN.IBM.COM
           subname = name.substring(i);
           result = c.get("domain_realm", subname);
           if (result != null) {
             break;
           } else {
             subname = name.substring(i + 1); // or mapping could be ibm.com = AUSTIN.IBM.COM
             result = c.get("domain_realm", subname);
             if (result != null) {
               break;
             }
           }
         }
       }
     }
   } catch (KrbException e) {
   }
   return result;
 }
Example #2
0
  /** Deletes an entry from the key table. */
  void deleteEntry() {
    PrincipalName pname = null;
    try {
      pname = new PrincipalName(principal);
      if (pname.getRealm() == null) {
        pname.setRealm(Config.getInstance().getDefaultRealm());
      }
      String answer;
      BufferedReader cis = new BufferedReader(new InputStreamReader(System.in));
      System.out.print(
          "Are you sure you want to "
              + " delete service key for "
              + pname.toString()
              + " in "
              + table.tabName()
              + "?(Y/N) :");

      System.out.flush();
      answer = cis.readLine();
      if (answer.equalsIgnoreCase("Y") || answer.equalsIgnoreCase("Yes")) ;
      else {
        // no error, the user did not want to delete the entry
        System.exit(0);
      }

    } catch (KrbException e) {
      System.err.println("Error occured while deleting the entry. " + "Deletion failed.");
      e.printStackTrace();
      System.exit(-1);
    } catch (IOException e) {
      System.err.println("Error occured while deleting the entry. " + " Deletion failed.");
      e.printStackTrace();
      System.exit(-1);
    }
    // admin.deleteEntry(pname);
    table.deleteEntry(pname);

    try {
      table.save();
    } catch (IOException e) {
      System.err.println("Error occurs while saving the keytab." + "Deletion fails.");
      e.printStackTrace();
      System.exit(-1);
    }
    System.out.println("Done!");
  }
Example #3
0
  /**
   * Adds a service key to key table. If the specified key table does not exist, the program will
   * automatically generate a new key table.
   */
  void addEntry() {
    PrincipalName pname = null;
    try {
      pname = new PrincipalName(principal);
      if (pname.getRealm() == null) {
        pname.setRealm(Config.getInstance().getDefaultRealm());
      }
    } catch (KrbException e) {
      System.err.println("Failed to add " + principal + " to keytab.");
      e.printStackTrace();
      System.exit(-1);
    }
    if (password == null) {
      try {
        BufferedReader cis = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Password for " + pname.toString() + ":");
        System.out.flush();
        password = new StringBuffer().append(cis.readLine());
      } catch (IOException e) {
        System.err.println("Failed to read the password.");
        e.printStackTrace();
        System.exit(-1);
      }
    }
    try {
      // admin.addEntry(pname, password);
      table.addEntry(pname, password);
      // admin.save();
      table.save();
      System.out.println("Done!");
      System.out.println("Service key for " + principal + " is saved in " + table.tabName());

    } catch (KrbCryptoException e) {
      System.err.println("Failed to add " + principal + " to keytab.");
      e.printStackTrace();
      System.exit(-1);
    } catch (IOException e) {
      System.err.println("Failed to save new entry.");
      e.printStackTrace();
      System.exit(-1);
    }
  }