You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ha...@apache.org on 2015/04/13 14:49:21 UTC

directory-kerby git commit: DIRKRB-176 Adding klist utility tool. Contributed by Liqi

Repository: directory-kerby
Updated Branches:
  refs/heads/master 8f312e5b3 -> 9bdb2c105


DIRKRB-176 Adding klist utility tool. Contributed by Liqi


Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/9bdb2c10
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/9bdb2c10
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/9bdb2c10

Branch: refs/heads/master
Commit: 9bdb2c105a3746abd0b544b0333149a18a8bfab4
Parents: 8f312e5
Author: Lin <li...@foxmail.com>
Authored: Mon Apr 13 20:48:51 2015 +0800
Committer: Lin <li...@foxmail.com>
Committed: Mon Apr 13 20:48:51 2015 +0800

----------------------------------------------------------------------
 kerby-tool/client-tool/pom.xml                  |   8 +-
 .../apache/kerby/kerberos/tool/klist/Klist.java | 160 +++++++++++++++++++
 .../kerby/kerberos/tool/klist/KlistOption.java  | 126 +++++++++++++++
 3 files changed, 293 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9bdb2c10/kerby-tool/client-tool/pom.xml
----------------------------------------------------------------------
diff --git a/kerby-tool/client-tool/pom.xml b/kerby-tool/client-tool/pom.xml
index a9f67ed..d188ae3 100644
--- a/kerby-tool/client-tool/pom.xml
+++ b/kerby-tool/client-tool/pom.xml
@@ -12,7 +12,7 @@
   See the License for the specific language governing permissions and
   limitations under the License. See accompanying LICENSE file.
 -->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
 
   <parent>
@@ -50,6 +50,7 @@
       <artifactId>token-provider</artifactId>
       <version>${project.version}</version>
     </dependency>
+
     <dependency>
       <groupId>org.apache.kerby</groupId>
       <artifactId>pki-provider</artifactId>
@@ -61,6 +62,11 @@
       <version>${project.version}</version>
     </dependency>
 
+      <dependency>
+          <groupId>org.apache.kerby</groupId>
+          <artifactId>kerb-util</artifactId>
+          <version>${project.version}</version>
+      </dependency>
   </dependencies>
   
 </project>

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9bdb2c10/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/klist/Klist.java
----------------------------------------------------------------------
diff --git a/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/klist/Klist.java b/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/klist/Klist.java
new file mode 100644
index 0000000..fae97fd
--- /dev/null
+++ b/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/klist/Klist.java
@@ -0,0 +1,160 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.kerby.kerberos.tool.klist;
+
+import org.apache.kerby.KOptionType;
+import org.apache.kerby.kerberos.kerb.ccache.Credential;
+import org.apache.kerby.kerberos.kerb.ccache.CredentialCache;
+import org.apache.kerby.KOptions;
+import org.apache.kerby.kerberos.tool.ToolUtil;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.List;
+
+/**
+ * klist like tool
+ */
+public class Klist {
+
+    private static  final String USAGE =
+            "Usage: klist [-e] [-V] [[-c] [-l] [-A] [-d] [-f] [-s] " +
+                    "[-a [-n]]] [-k [-t] [-K]] [name]\n" +
+                    "\t-c specifies credentials cache\n" +
+                    "\t-k specifies keytab\n" +
+                    "\t   (Default is credentials cache)\n" +
+                    "\t-i uses default client keytab if no name given\n" +
+                    "\t-l lists credential caches in collection\n" +
+                    "\t-A shows content of all credential caches\n" +
+                    "\t-e shows the encryption type\n" +
+                    "\t-V shows the Kerberos version and exits\n" +
+                    "\toptions for credential caches:\n" +
+                    "\t\t-d shows the submitted authorization data types\n" +
+                    "\t\t-f shows credentials flags\n" +
+                    "\t\t-s sets exit status based on valid tgt existence\n" +
+                    "\t\t-a displays the address list\n" +
+                    "\t\t\t-n do not reverse-resolve\n" +
+                    "\toptions for keytabs:\n" +
+                    "\t\t-t shows keytab entry timestamps\n" +
+                    "\t\t-K shows keytab entry keys\n";
+
+
+
+    private static void printUsage(String error) {
+        System.err.println(error + "\n");
+        System.err.println(USAGE);
+        System.exit(-1);
+    }
+
+    private static int printInfo(String name, KOptions klOptions) {
+        CredentialCache cc = new CredentialCache();
+        List<Credential> credentials;
+        InputStream cis = null ;
+        String error;
+        String fileName = null;
+
+        if (!klOptions.contains(KlistOption.CREDENTIALS_CACHE)) {
+            error = "No credential cache path given.";
+            printUsage(error);
+        } else {
+            fileName = klOptions.getStringOption(KlistOption.CREDENTIALS_CACHE);
+            try {
+                cis = new FileInputStream(fileName);
+                cc.load(cis);
+            } catch (IOException e) {
+                System.err.println("Failed to open CredentialCache from file: " + fileName);
+                e.printStackTrace();
+            }
+
+        }
+
+        if (cc != null) {
+            credentials = cc.getCredentials();
+
+            System.out.println("Ticket cache: " + fileName);
+            System.out.println("Default principal: " + cc.getPrimaryPrincipal().getName());
+
+            if (credentials.isEmpty()) {
+                System.out.println("No credential has been cached.");
+            } else {
+                DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
+
+                System.out.println("Valid starting\t\tExpires\t\t\tService principal");
+
+                for (Credential crd : credentials) {
+                    System.out.println( df.format(crd.getStartTime().getTime()) + "\t" +
+                                        df.format(crd.getEndTime().getTime()) + "\t" +
+                                        crd.getServerName());
+                }
+            }
+
+        }
+
+        return 0;
+    }
+
+    public static void main(String[] args) throws Exception {
+        KOptions klOptions = new KOptions();
+        KlistOption klopt;
+        String name = null;
+
+        int i = 0;
+        String opt, value, error;
+        while (i < args.length) {
+            error = null;
+            opt = args[i++];
+
+            if (opt.startsWith("-")) {
+                klopt = KlistOption.fromName(opt);
+                if (klopt == KlistOption.NONE) {
+                    error = "Invalid option:" + opt;
+                    break;
+                }
+            } else {
+                name = opt;
+                break;
+            }
+
+            if (klopt.getType() != KOptionType.NOV) { //needs value for this parameter
+                value = null;
+                if (i < args.length) {
+                    value = args[i++];
+                }
+                if (value != null) {
+                    ToolUtil.parseSetValue(klopt, value);
+                } else {
+                    error = "Option" + klopt + "requires a following value";
+                }
+            }
+
+            if ( error != null ) {
+                printUsage(error);
+            }
+
+            klOptions.add(klopt);
+        }
+
+        int errNo = Klist.printInfo(name, klOptions);
+        System.exit(errNo);
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9bdb2c10/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/klist/KlistOption.java
----------------------------------------------------------------------
diff --git a/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/klist/KlistOption.java b/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/klist/KlistOption.java
new file mode 100644
index 0000000..ca46b04
--- /dev/null
+++ b/kerby-tool/client-tool/src/main/java/org/apache/kerby/kerberos/tool/klist/KlistOption.java
@@ -0,0 +1,126 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.kerby.kerberos.tool.klist;
+
+import org.apache.kerby.KOption;
+import org.apache.kerby.KOptionType;
+
+public enum KlistOption implements KOption{
+    NONE("NONE"),
+    CREDENTIALS_CACHE("-c", "specifies path of credentials cache", KOptionType.STR),
+    KEYTAB("-k", "specifies keytab"),
+    DEFAULT_CLIENT_KEYTAB("-i", "uses default client keytab if no name given"),
+    LIST_CREDENTIAL_CACHES("-l", "list credential caches in collection"),
+    ALL_CREDENTIAL_CACHES("-A", "shows content of all credential caches"),
+    ENCRYPTION_TYPE("-e", "shows encryption type"),
+    KERBEROS_VERSION("-V", "shows Kerberos version"),
+    AUTHORIZATION_DATA_TYPE("-d", "shows the submitted authorization data type"),
+    CREDENTIALS_FLAGS("-f", "show credential flags"),
+    EXIT_TGT_EXISTENCE("-s", "sets exit status based on valid tgt existence"),
+    DISPL_ADDRESS_LIST("-a", "displays the address list"),
+    NO_REVERSE_RESOLVE("-n","do not reverse resolve"),
+    SHOW_KTAB_ENTRY_TS("-t","shows keytab entry timestamps"),
+    SHOW_KTAB_ENTRY_KEY("-k", "show keytab entry keys"),
+    ;
+
+    private String name;
+    private KOptionType type = KOptionType.NONE;
+    private String description;
+    private Object value;
+
+    KlistOption(String description) {
+        this(description, KOptionType.NOV);
+    }
+
+    KlistOption(String description, KOptionType type) {
+        this.description = description;
+        this.type = type;
+    }
+
+    KlistOption(String name, String description) {
+        this(name, description, KOptionType.NOV);
+    }
+
+    KlistOption(String name, String description, KOptionType type) {
+        this.name = name;
+        this.description = description;
+        this.type = type;
+    }
+
+    public static KlistOption fromName(String name) {
+        if (name != null ) {
+            for (KlistOption klopt : values()) {
+                if (klopt.getName().equals(name)) {
+                    return (KlistOption) klopt;
+                }
+            }
+        }
+        return NONE;
+    }
+
+    @Override
+    public String getOptionName() {
+        return name();
+    }
+
+    @Override
+    public KOptionType getType() {
+        return this.type;
+    }
+
+    @Override
+    public void setType(KOptionType type) {
+        this.type = type;
+    }
+
+    @Override
+    public String getName() {
+        if (name != null) {
+            return name;
+        }
+        return name();
+    }
+
+    @Override
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    @Override
+    public String getDescription() {
+        return this.description;
+    }
+
+    @Override
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    @Override
+    public Object getValue() {
+        return value;
+    }
+
+    @Override
+    public void setValue(Object value) {
+        this.value = value;
+    }
+
+}