- Adding title (or comment) to the Resource object
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / aa / arpUtil / ArpUtil.java
index 1bcc664..5a60d87 100755 (executable)
@@ -1,24 +1,88 @@
+/* 
+ * The Shibboleth License, Version 1. 
+ * Copyright (c) 2002 
+ * University Corporation for Advanced Internet Development, Inc. 
+ * All rights reserved
+ * 
+ * 
+ * Redistribution and use in source and binary forms, with or without 
+ * modification, are permitted provided that the following conditions are met:
+ * 
+ * Redistributions of source code must retain the above copyright notice, this 
+ * list of conditions and the following disclaimer.
+ * 
+ * Redistributions in binary form must reproduce the above copyright notice, 
+ * this list of conditions and the following disclaimer in the documentation 
+ * and/or other materials provided with the distribution, if any, must include 
+ * the following acknowledgment: "This product includes software developed by 
+ * the University Corporation for Advanced Internet Development 
+ * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement 
+ * may appear in the software itself, if and wherever such third-party 
+ * acknowledgments normally appear.
+ * 
+ * Neither the name of Shibboleth nor the names of its contributors, nor 
+ * Internet2, nor the University Corporation for Advanced Internet Development, 
+ * Inc., nor UCAID may be used to endorse or promote products derived from this 
+ * software without specific prior written permission. For written permission, 
+ * please contact shibboleth@shibboleth.org
+ * 
+ * Products derived from this software may not be called Shibboleth, Internet2, 
+ * UCAID, or the University Corporation for Advanced Internet Development, nor 
+ * may Shibboleth appear in their name, without prior written permission of the 
+ * University Corporation for Advanced Internet Development.
+ * 
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
+ * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
+ * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK 
+ * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE. 
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY 
+ * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT, 
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package edu.internet2.middleware.shibboleth.aa.arpUtil;
+
+/**
+ *  Attribute Authority & Release Policy
+ *  A utility for managing ARPs
+ *
+ * @author     Parviz Dousti (dousti@cmu.edu)
+ * @created    June, 2002
+ */
+
 import edu.internet2.middleware.shibboleth.aa.*;
 import java.io.*;
 import java.util.*;
 import java.security.*;
 import javax.naming.*;
 import javax.naming.directory.*;
-//import javax.security.auth.kerberos.*;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
 
 class ArpUtil{
+
+    private static Logger log = Logger.getLogger(ArpUtil.class.getName());
     static Principal user;
     static ArpFactory arpFactory;
-    static String listUsage = "\tArpUtil list <arp name> [-acls] [-dir <ldap url> <user id>] [-sql <sql url> <user id>]";
-    static String addUsage = "\tArpUtil add <arp name> [-admin] <shar name> [-default] <url> <attribute name> [-exclude] [-filter [!]<val1> [!]<val2> ...]";    
+    static String listUsage = "\tArpUtil list <arp name> [-acls] [-dir <ldap url> <user id>]";
+    static String addUsage = "\tArpUtil add <arp name> [-admin] <shar name> [-default] <url> [-title comment] <attribute name> [-exclude] [-filter [!]<val1> [!]<val2> ...]";    
     static String removeUsage = "\tArpUtil remove <arp name> [<shar name> [<url> [<attribute name>]]]";
     static String setAclUsage = "\tArpUtil setAcl <user> <acl> <arp name> [<shar name> [<url>]]";
     static String attrUsage = "\tArpUtil listAttributes <jar file name>";
 
     public static void main(String [] args)throws AAException{
 
-       arpFactory = ArpRepository.getInstance("file", "/tmp/shib2/");
+       arpFactory = ArpRepository.getInstance("file",  System.getProperty("arp.dir"));
 
+       PropertyConfigurator.configure(System.getProperty("log.config"));
+       
        //user = new KerberosPrincipal(System.getProperty("user.name"));
        user = new AA_Identity(System.getProperty("user.name"));
 
@@ -58,14 +122,18 @@ class ArpUtil{
            if(len > 2){
                if(args[2].equalsIgnoreCase("-acls"))
                    acls = true;
-               if(args[2].equalsIgnoreCase("-dir") || args[2].equalsIgnoreCase("-sql"))
+               if(args[2].equalsIgnoreCase("-dir")){
                    if(len < 4){
-                       System.out.println("Usage:\n"+listUsage);                                       return;
+                       System.out.println("Usage:\n"+listUsage);
+                       return;
                    }else{
                        ctx = getUserContext(args);
                    }
-               if(ctx == null)
-                   return;
+                   if(ctx == null){
+                       System.out.println("Failed to get Directory Context.");
+                       return;
+                   }
+               }
            }
 
            Arp arp = arpFactory.getInstance(arpName, false);
@@ -84,6 +152,8 @@ class ArpUtil{
                ArpResource[] resources = shars[i].getResources();
                for(int j=0; j < resources.length; j++){
                    System.out.println("\t\tURL: "+resources[j]);
+                   if(resources[j].getComment() != null)
+                       System.out.println("\t\tTITLE: "+resources[j].getComment());
                    if(acls)
                        System.out.println("\t\tACL: "+resources[j].getAcl());
                    ArpAttribute[] attributes = resources[j].getAttributes();
@@ -124,9 +194,11 @@ class ArpUtil{
        boolean isDefault = false;
        boolean doExclude = false;
        boolean hasFilter = false;
+       boolean showTitle = false;
        String resourceName = null;
        String sharName = null;
        String attrName = null;
+       String title = null;
 
        String arpName = args[i++];
        if(args[i].equalsIgnoreCase("-admin")){
@@ -140,6 +212,14 @@ class ArpUtil{
        }
        if(i < args.length)
            resourceName = args[i++];
+
+       if(i < args.length && args[i].equalsIgnoreCase("-title")){
+           showTitle = true;
+           i++;
+           if(i <args.length)
+               title = args[i++];
+       }
+       
        if(i < args.length)
            attrName = args[i++];
        if(i < args.length && args[i].equalsIgnoreCase("-exclude")){
@@ -177,7 +257,7 @@ class ArpUtil{
                s = new ArpShar(sharName, isDefault);
            ArpResource r = s.getResource(resourceName);
            if(r == null)
-               r = new ArpResource(resourceName);
+               r = new ArpResource(resourceName, title);
            ArpAttribute a = r.getAttribute(attrName);
            if(a == null)
                a = new ArpAttribute(attrName, doExclude);
@@ -382,46 +462,22 @@ class ArpUtil{
     public static DirContext getUserContext(String[] args)
     throws Exception{
 
+       if(args.length <5){
+           System.out.println("Usage:\n"+listUsage);
+           return null;
+       }
+
        String dirUrl = args[3];
        String uid = args[4];
        
         Hashtable env = new Hashtable(11);
 
-       if(args[2].equalsIgnoreCase("-dir")){
-           env.put(Context.INITIAL_CONTEXT_FACTORY,
-                   "com.sun.jndi.ldap.LdapCtxFactory");
-           env.put(Context.PROVIDER_URL, dirUrl);
-
-           DirContext ctx = new InitialDirContext(env);
-           
-           NamingEnumeration ne = ctx.search("", "cmuAndrewId="+uid, null, null);
-           if(ne.hasMoreElements()){
-               SearchResult rs = (SearchResult)ne.nextElement();
-               String guid = (String)rs.getAttributes().get("GUID").get();
-               return (DirContext)ctx.lookup("guid="+guid);
-           }else{
-               System.out.println("Search for "+uid+" failed!");
-               return null;
-           }
-
-       }else if(args[2].equalsIgnoreCase("-sql")){
-           env.put(Context.INITIAL_CONTEXT_FACTORY,
-                   "SQLCtxFactory");
-
-           // a Sample of possible args to pass to context
-           env.put(Context.PROVIDER_URL, dirUrl);
-           env.put("SQL_DRIVER", "oracle.jdbc.OracleDriver");
-           env.put("SECURITY_PRINCIPAL", "dousti");
-           env.put("SECURITY_CREDENTIALS", "foobar");
-           env.put("USER_IDENTIFIER", args[4]);
-           DirContext ctx = new InitialDirContext(env);
-           return ctx;
-
-       }else{
-           System.out.println("Usage:\n"+listUsage);
-           return null;
-       }
-       
+       env.put(Context.INITIAL_CONTEXT_FACTORY,
+               "com.sun.jndi.ldap.LdapCtxFactory");
+       env.put(Context.PROVIDER_URL, dirUrl);
+       DirContext ctx = new InitialDirContext(env);
+       return (DirContext)ctx.lookup("uid="+uid);
+               
     }
        
 }