mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-22 03:54:33 +02:00
6854308: more ktab options
Reviewed-by: mullan
This commit is contained in:
parent
20ee77e0d0
commit
ee5188c4c3
3 changed files with 112 additions and 42 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Portions Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
|
* Portions Copyright 2000-2009 Sun Microsystems, Inc. All Rights Reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -403,11 +403,11 @@ public class KeyTab implements KeyTabConstants {
|
||||||
/**
|
/**
|
||||||
* Retrieves the key table entry with the specified service name.
|
* Retrieves the key table entry with the specified service name.
|
||||||
* @param service the service which may have an entry in the key table.
|
* @param service the service which may have an entry in the key table.
|
||||||
|
* @param keyType the etype to match, returns the 1st one if -1 provided
|
||||||
* @return -1 if the entry is not found, else return the entry index
|
* @return -1 if the entry is not found, else return the entry index
|
||||||
* in the list.
|
* in the list.
|
||||||
*/
|
*/
|
||||||
private int retrieveEntry(PrincipalName service, int keyType) {
|
private int retrieveEntry(PrincipalName service, int keyType) {
|
||||||
int found = -1;
|
|
||||||
KeyTabEntry e;
|
KeyTabEntry e;
|
||||||
if (entries != null) {
|
if (entries != null) {
|
||||||
for (int i = 0; i < entries.size(); i++) {
|
for (int i = 0; i < entries.size(); i++) {
|
||||||
|
@ -418,7 +418,7 @@ public class KeyTab implements KeyTabConstants {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return found;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -476,12 +476,29 @@ public class KeyTab implements KeyTabConstants {
|
||||||
/**
|
/**
|
||||||
* Removes an entry from the key table.
|
* Removes an entry from the key table.
|
||||||
* @param service the service <code>PrincipalName</code>.
|
* @param service the service <code>PrincipalName</code>.
|
||||||
|
* @param etype the etype to match, first one if -1 provided
|
||||||
|
* @return 1 if removed successfully, 0 otherwise
|
||||||
*/
|
*/
|
||||||
public void deleteEntry(PrincipalName service) {
|
public int deleteEntry(PrincipalName service, int etype) {
|
||||||
int result = retrieveEntry(service, -1);
|
int result = retrieveEntry(service, etype);
|
||||||
if (result != -1) {
|
if (result != -1) {
|
||||||
entries.removeElementAt(result);
|
entries.removeElementAt(result);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes an entry from the key table.
|
||||||
|
* @param service the service <code>PrincipalName</code>.
|
||||||
|
* @return number of entries removed
|
||||||
|
*/
|
||||||
|
public int deleteEntry(PrincipalName service) {
|
||||||
|
int count = 0;
|
||||||
|
while (deleteEntry(service, -1) > 0) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
/*
|
/*
|
||||||
|
* Portions Copyright 2003-2009 Sun Microsystems, Inc. All Rights Reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
/*
|
/*
|
||||||
|
* Portions Copyright 2003-2009 Sun Microsystems, Inc. All Rights Reserved.
|
||||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
*
|
*
|
||||||
* This code is free software; you can redistribute it and/or modify it
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
@ -30,16 +31,15 @@
|
||||||
package sun.security.krb5.internal.tools;
|
package sun.security.krb5.internal.tools;
|
||||||
|
|
||||||
import sun.security.krb5.*;
|
import sun.security.krb5.*;
|
||||||
import sun.security.krb5.internal.*;
|
|
||||||
import sun.security.krb5.internal.ktab.*;
|
import sun.security.krb5.internal.ktab.*;
|
||||||
import sun.security.krb5.KrbCryptoException;
|
|
||||||
import java.lang.RuntimeException;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.text.DateFormat;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Date;
|
||||||
|
import sun.security.krb5.internal.crypto.EType;
|
||||||
/**
|
/**
|
||||||
* This class can execute as a command-line tool to help the user manage
|
* This class can execute as a command-line tool to help the user manage
|
||||||
* entires in the key table.
|
* entires in the key table.
|
||||||
|
@ -55,6 +55,9 @@ public class Ktab {
|
||||||
char action;
|
char action;
|
||||||
String name; // name and directory of key table
|
String name; // name and directory of key table
|
||||||
String principal;
|
String principal;
|
||||||
|
boolean showEType;
|
||||||
|
boolean showTime;
|
||||||
|
int etype = -1;
|
||||||
char[] password = null;
|
char[] password = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -62,13 +65,14 @@ public class Ktab {
|
||||||
* <br>Usage: ktab <options>
|
* <br>Usage: ktab <options>
|
||||||
* <br>available options to Ktab:
|
* <br>available options to Ktab:
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li><b>-l</b> list the keytab name and entries
|
* <li><b>-l [-e] [-t]</b> list the keytab name and entries, -e show
|
||||||
|
* encryption etypes, -t show timestamps.
|
||||||
* <li><b>-a</b> <<i>principal name</i>>
|
* <li><b>-a</b> <<i>principal name</i>>
|
||||||
* (<<i>password</i>>) add an entry to the keytab.
|
* (<<i>password</i>>) add an entry to the keytab.
|
||||||
* The entry is added only to the keytab. No changes are made to the
|
* The entry is added only to the keytab. No changes are made to the
|
||||||
* Kerberos database.
|
* Kerberos database.
|
||||||
* <li><b>-d</b> <<i>principal name</i>>
|
* <li><b>-d</b> <<i>principal name</i>> [<<i>etype</i>>]
|
||||||
* delete an entry from the keytab
|
* delete an entry from the keytab.
|
||||||
* The entry is deleted only from the keytab. No changes are made to the
|
* The entry is deleted only from the keytab. No changes are made to the
|
||||||
* Kerberos database.
|
* Kerberos database.
|
||||||
* <li><b>-k</b> <<i>keytab name</i> >
|
* <li><b>-k</b> <<i>keytab name</i> >
|
||||||
|
@ -182,6 +186,11 @@ public class Ktab {
|
||||||
i++;
|
i++;
|
||||||
if ((i < args.length) && (!args[i].startsWith("-"))) {
|
if ((i < args.length) && (!args[i].startsWith("-"))) {
|
||||||
principal = args[i];
|
principal = args[i];
|
||||||
|
int j = i + 1;
|
||||||
|
if ((j < args.length) && (!args[j].startsWith("-"))) {
|
||||||
|
etype = Integer.parseInt(args[j]);
|
||||||
|
i = j;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Please specify the principal" +
|
System.out.println("Please specify the principal" +
|
||||||
"name of the entry you want to " +
|
"name of the entry you want to " +
|
||||||
|
@ -207,6 +216,12 @@ public class Ktab {
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case 'e':
|
||||||
|
showEType = true;
|
||||||
|
break;
|
||||||
|
case 't':
|
||||||
|
showTime = true;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
printHelp();
|
printHelp();
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
|
@ -271,25 +286,54 @@ public class Ktab {
|
||||||
* Lists key table name and entries in it.
|
* Lists key table name and entries in it.
|
||||||
*/
|
*/
|
||||||
void listKt() {
|
void listKt() {
|
||||||
int version;
|
System.out.println("Keytab name: " + KeyTab.tabName());
|
||||||
String principal;
|
|
||||||
// System.out.println("Keytab name: " + admin.getKeyTabName());
|
|
||||||
System.out.println("Keytab name: " + table.tabName());
|
|
||||||
// KeyTabEntry[] entries = admin.getEntries();
|
|
||||||
KeyTabEntry[] entries = table.getEntries();
|
KeyTabEntry[] entries = table.getEntries();
|
||||||
if ((entries != null) && (entries.length > 0)) {
|
if ((entries != null) && (entries.length > 0)) {
|
||||||
System.out.println("KVNO Principal");
|
String[][] output = new String[entries.length+1][showTime?3:2];
|
||||||
|
int column = 0;
|
||||||
|
output[0][column++] = "KVNO";
|
||||||
|
if (showTime) output[0][column++] = "Timestamp";
|
||||||
|
output[0][column++] = "Principal";
|
||||||
for (int i = 0; i < entries.length; i++) {
|
for (int i = 0; i < entries.length; i++) {
|
||||||
version = entries[i].getKey().getKeyVersionNumber().intValue();
|
column = 0;
|
||||||
principal = entries[i].getService().toString();
|
output[i+1][column++] = entries[i].getKey().
|
||||||
if (i == 0) {
|
getKeyVersionNumber().toString();
|
||||||
StringBuffer separator = new StringBuffer();
|
if (showTime) output[i+1][column++] =
|
||||||
for (int j = 0; j < 9 + principal.length(); j++) {
|
DateFormat.getDateTimeInstance(
|
||||||
separator.append("-");
|
DateFormat.SHORT, DateFormat.SHORT).format(
|
||||||
|
new Date(entries[i].getTimeStamp().getTime()));
|
||||||
|
String princ = entries[i].getService().toString();
|
||||||
|
if (showEType) {
|
||||||
|
int e = entries[i].getKey().getEType();
|
||||||
|
output[i+1][column++] = princ + " (" + e + ":" +
|
||||||
|
EType.toString(e) + ")";
|
||||||
|
} else {
|
||||||
|
output[i+1][column++] = princ;
|
||||||
}
|
}
|
||||||
System.out.println(separator.toString());
|
|
||||||
}
|
}
|
||||||
System.out.println(" " + version + " " + principal);
|
int[] width = new int[column];
|
||||||
|
for (int j=0; j<column; j++) {
|
||||||
|
for (int i=0; i <= entries.length; i++) {
|
||||||
|
if (output[i][j].length() > width[j]) {
|
||||||
|
width[j] = output[i][j].length();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (j != 0) width[j] = -width[j];
|
||||||
|
}
|
||||||
|
for (int j=0; j<column; j++) {
|
||||||
|
System.out.printf("%" + width[j] + "s ", output[0][j]);
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
for (int j=0; j<column; j++) {
|
||||||
|
for (int k=0; k<Math.abs(width[j]); k++) System.out.print("-");
|
||||||
|
System.out.print(" ");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
for (int i=0; i<entries.length; i++) {
|
||||||
|
for (int j=0; j<column; j++) {
|
||||||
|
System.out.printf("%" + width[j] + "s ", output[i+1][j]);
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
System.out.println("0 entry.");
|
System.out.println("0 entry.");
|
||||||
|
@ -309,9 +353,10 @@ public class Ktab {
|
||||||
String answer;
|
String answer;
|
||||||
BufferedReader cis =
|
BufferedReader cis =
|
||||||
new BufferedReader(new InputStreamReader(System.in));
|
new BufferedReader(new InputStreamReader(System.in));
|
||||||
System.out.print("Are you sure you want to "+
|
System.out.print("Are you sure you want to"+
|
||||||
" delete service key for " + pname.toString() +
|
" delete service key for " + pname.toString() +
|
||||||
" in " + table.tabName() + "?(Y/N) :");
|
" (" + (etype==-1?"all etypes":("etype = "+etype)) +
|
||||||
|
") in " + table.tabName() + "?(Y/N): ");
|
||||||
|
|
||||||
System.out.flush();
|
System.out.flush();
|
||||||
answer = cis.readLine();
|
answer = cis.readLine();
|
||||||
|
@ -333,19 +378,26 @@ public class Ktab {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
}
|
}
|
||||||
// admin.deleteEntry(pname);
|
|
||||||
table.deleteEntry(pname);
|
|
||||||
|
|
||||||
|
int count;
|
||||||
|
if (etype == -1) count = table.deleteEntry(pname);
|
||||||
|
else count = table.deleteEntry(pname, etype);
|
||||||
|
|
||||||
|
if (count == 0) {
|
||||||
|
System.err.println("No matched entry in the keytab. " +
|
||||||
|
"Deletion fails.");
|
||||||
|
System.exit(-1);
|
||||||
|
} else {
|
||||||
try {
|
try {
|
||||||
table.save();
|
table.save();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Error occurs while saving the keytab." +
|
System.err.println("Error occurs while saving the keytab. " +
|
||||||
"Deletion fails.");
|
"Deletion fails.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
}
|
}
|
||||||
System.out.println("Done!");
|
System.out.println("Done!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -355,12 +407,12 @@ public class Ktab {
|
||||||
System.out.println("\nUsage: ktab " +
|
System.out.println("\nUsage: ktab " +
|
||||||
"<options>");
|
"<options>");
|
||||||
System.out.println("available options to Ktab:");
|
System.out.println("available options to Ktab:");
|
||||||
System.out.println("-l\t\t\t\tlist the keytab name and entries");
|
System.out.println("-l [-e] [-t]\t\t\tlist the keytab name and entries,\n\t\t\t\t-e with etype, -t with timestamp");
|
||||||
System.out.println("-a <principal name> (<password>)add an entry " +
|
System.out.println("-a <principal name> (<password>)add an entry " +
|
||||||
"to the keytab");
|
"to the keytab");
|
||||||
System.out.println("-d <principal name>\t\tdelete an entry from "+
|
System.out.println("-d <principal name> [<etype>]\tdelete an "+
|
||||||
"the keytab");
|
"entry from the keytab");
|
||||||
System.out.println("-k <keytab name>\t\tspecify keytab name and "+
|
System.out.println("-k <keytab name>\t\tspecify keytab name and "+
|
||||||
" path with prefix FILE:");
|
"path with prefix FILE:");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue