1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8001104
  27  * @summary Unbound SASL service: the GSSAPI/krb5 mech
  28  * @compile -XDignore.symbol.file UnboundService.java
  29  * @run main/othervm UnboundService null null
  30  * @run main/othervm UnboundService server/host.rabbit.hole null
  31  * @run main/othervm UnboundService server/host.rabbit.hole@RABBIT.HOLE null
  32  * @run main/othervm/fail UnboundService backend/host.rabbit.hole null
  33  * @run main/othervm UnboundService null server@host.rabbit.hole
  34  * @run main/othervm UnboundService server/host.rabbit.hole server@host.rabbit.hole
  35  * @run main/othervm UnboundService server/host.rabbit.hole@RABBIT.HOLE server@host.rabbit.hole
  36  * @run main/othervm/fail UnboundService backend/host.rabbit.hole server@host.rabbit.hole
  37  */
  38 
  39 import java.io.File;
  40 import java.io.FileOutputStream;
  41 import sun.security.jgss.GSSUtil;
  42 
  43 public class UnboundService {
  44 
  45     /**
  46      * @param args JAAS config pricipal and GSSCredential creation name
  47      */
  48     public static void main(String[] args) throws Exception {
  49 
  50         String principal = args[0];
  51         if (principal.equals("null")) principal = null;
  52 
  53         String server = args[1];
  54         if (server.equals("null")) server = null;
  55 
  56         new OneKDC(null).writeJAASConf();
  57         File f = new File(OneKDC.JAAS_CONF);
  58         try (FileOutputStream fos = new FileOutputStream(f)) {
  59             fos.write((
  60                 "client {\n" +
  61                 "    com.sun.security.auth.module.Krb5LoginModule required;\n};\n" +
  62                 "unbound {\n" +
  63                 "    com.sun.security.auth.module.Krb5LoginModule required\n" +
  64                 "    useKeyTab=true\n" +
  65                 "    principal=" +
  66                     (principal==null? "*" :("\"" + principal + "\"")) + "\n" +
  67                 "    doNotPrompt=true\n" +
  68                 "    isInitiator=false\n" +
  69                 "    storeKey=true;\n};\n"
  70                 ).getBytes());
  71         }
  72 
  73         Context c, s;
  74         c = Context.fromJAAS("client");
  75         s = Context.fromJAAS("unbound");
  76 
  77         c.startAsClient(OneKDC.SERVER, GSSUtil.GSS_KRB5_MECH_OID);
  78         s.startAsServer(server, GSSUtil.GSS_KRB5_MECH_OID);
  79 
  80         Context.handshake(c, s);
  81 
  82         s.dispose();
  83         c.dispose();
  84     }
  85 }