1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * ident "%Z%%M% %I% %E% SMI" 24*7c478bd9Sstevel@tonic-gate * 25*7c478bd9Sstevel@tonic-gate * Copyright (c) 1999-2000 by Sun Microsystems, Inc. 26*7c478bd9Sstevel@tonic-gate * All rights reserved. 27*7c478bd9Sstevel@tonic-gate */ 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate // 30*7c478bd9Sstevel@tonic-gate // Class representing the info from /etc/krb5/krb5.conf. 31*7c478bd9Sstevel@tonic-gate // Currently, the admin tool only needs to access all of the 32*7c478bd9Sstevel@tonic-gate // admin servers for all of the realms enumerated in the file, 33*7c478bd9Sstevel@tonic-gate // and the default realm. 34*7c478bd9Sstevel@tonic-gate // A sample file looks like this: 35*7c478bd9Sstevel@tonic-gate 36*7c478bd9Sstevel@tonic-gate /* 37*7c478bd9Sstevel@tonic-gate 38*7c478bd9Sstevel@tonic-gate [libdefaults] 39*7c478bd9Sstevel@tonic-gate default_realm = SUNSOFT.FOO.SUN.COM 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate [realms] 42*7c478bd9Sstevel@tonic-gate GENESIS.FOO.SUN.COM = { 43*7c478bd9Sstevel@tonic-gate kdc = xxxxx.eng.sun.com 44*7c478bd9Sstevel@tonic-gate admin_server = xxxxx.eng.sun.com 45*7c478bd9Sstevel@tonic-gate } 46*7c478bd9Sstevel@tonic-gate SUNSOFT.FOO.SUN.COM = { 47*7c478bd9Sstevel@tonic-gate kdc = gandolf.eng.sun.com 48*7c478bd9Sstevel@tonic-gate kdc = ulong.eng.sun.com 49*7c478bd9Sstevel@tonic-gate admin_server = gandolf.eng.sun.com:749 50*7c478bd9Sstevel@tonic-gate } 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate [domain_realm] 53*7c478bd9Sstevel@tonic-gate .eng.sun.com = SUNSOFT.FOO.SUN.COM 54*7c478bd9Sstevel@tonic-gate .sun.com = SUNSOFT.FOO.SUN.COM 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate [logging] 57*7c478bd9Sstevel@tonic-gate default = FILE:/var/krb5/kdc.log 58*7c478bd9Sstevel@tonic-gate kdc = FILE:/var/krb5/kdc.log 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate [appdefaults] 61*7c478bd9Sstevel@tonic-gate gkadmin = { 62*7c478bd9Sstevel@tonic-gate help_url = http:... 63*7c478bd9Sstevel@tonic-gate } 64*7c478bd9Sstevel@tonic-gate */ 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate import java.io.*; 67*7c478bd9Sstevel@tonic-gate import java.util.Vector; 68*7c478bd9Sstevel@tonic-gate import java.util.StringTokenizer; 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate public class Krb5Conf { 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate private String DefRealm = null; 73*7c478bd9Sstevel@tonic-gate private String HelpURL = null; 74*7c478bd9Sstevel@tonic-gate private Vector RealmVector = new Vector(10, 10); 75*7c478bd9Sstevel@tonic-gate Krb5Conf()76*7c478bd9Sstevel@tonic-gate public Krb5Conf() { 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gate FileReader fr = null; 79*7c478bd9Sstevel@tonic-gate 80*7c478bd9Sstevel@tonic-gate try { 81*7c478bd9Sstevel@tonic-gate fr = new FileReader("/etc/krb5/krb5.conf"); 82*7c478bd9Sstevel@tonic-gate } catch (FileNotFoundException e) { 83*7c478bd9Sstevel@tonic-gate // System.out.println("Error: " + e); 84*7c478bd9Sstevel@tonic-gate return; 85*7c478bd9Sstevel@tonic-gate } 86*7c478bd9Sstevel@tonic-gate BufferedReader in = new BufferedReader(fr); 87*7c478bd9Sstevel@tonic-gate 88*7c478bd9Sstevel@tonic-gate String line = null, Name = null, Server = "", Port = "0"; 89*7c478bd9Sstevel@tonic-gate boolean wantdef = false, wantrealm = false; 90*7c478bd9Sstevel@tonic-gate boolean wantadmin = false, skipcurly = false; 91*7c478bd9Sstevel@tonic-gate boolean wantapp = false, wanturl = false; 92*7c478bd9Sstevel@tonic-gate RealmInfo r = null; 93*7c478bd9Sstevel@tonic-gate 94*7c478bd9Sstevel@tonic-gate // Read each line of the file 95*7c478bd9Sstevel@tonic-gate do { 96*7c478bd9Sstevel@tonic-gate try { 97*7c478bd9Sstevel@tonic-gate line = in.readLine(); 98*7c478bd9Sstevel@tonic-gate } catch (IOException e) { 99*7c478bd9Sstevel@tonic-gate // System.out.println("Error: " + e); 100*7c478bd9Sstevel@tonic-gate return; 101*7c478bd9Sstevel@tonic-gate } 102*7c478bd9Sstevel@tonic-gate if (line == null) 103*7c478bd9Sstevel@tonic-gate break; 104*7c478bd9Sstevel@tonic-gate // System.out.println(line); 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate // Get some help with parsing 107*7c478bd9Sstevel@tonic-gate StringTokenizer t = new StringTokenizer(line); 108*7c478bd9Sstevel@tonic-gate if (!t.hasMoreTokens()) 109*7c478bd9Sstevel@tonic-gate continue; 110*7c478bd9Sstevel@tonic-gate String s = t.nextToken(); 111*7c478bd9Sstevel@tonic-gate if (s.charAt(0) == '#') 112*7c478bd9Sstevel@tonic-gate continue; 113*7c478bd9Sstevel@tonic-gate 114*7c478bd9Sstevel@tonic-gate // Look for [realm], [libdefaults] or [appdefaults] 115*7c478bd9Sstevel@tonic-gate if (s.charAt(0) == '[') { 116*7c478bd9Sstevel@tonic-gate wantdef = false; 117*7c478bd9Sstevel@tonic-gate wantrealm = false; 118*7c478bd9Sstevel@tonic-gate wantapp = false; 119*7c478bd9Sstevel@tonic-gate if (s.compareTo("[libdefaults]") == 0) 120*7c478bd9Sstevel@tonic-gate wantdef = true; 121*7c478bd9Sstevel@tonic-gate if (s.compareTo("[realms]") == 0) 122*7c478bd9Sstevel@tonic-gate wantrealm = true; 123*7c478bd9Sstevel@tonic-gate if (s.compareTo("[appdefaults]") == 0) 124*7c478bd9Sstevel@tonic-gate wantapp = true; 125*7c478bd9Sstevel@tonic-gate } else { 126*7c478bd9Sstevel@tonic-gate 127*7c478bd9Sstevel@tonic-gate // Have we seen [libdefaults]? 128*7c478bd9Sstevel@tonic-gate if (wantdef && s.compareTo("default_realm") == 0) { 129*7c478bd9Sstevel@tonic-gate if (t.hasMoreTokens()) { 130*7c478bd9Sstevel@tonic-gate DefRealm = t.nextToken(" \t\n\r="); 131*7c478bd9Sstevel@tonic-gate wantdef = false; 132*7c478bd9Sstevel@tonic-gate } 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate // Have we seen [realm] instead? 135*7c478bd9Sstevel@tonic-gate } else if (wantrealm) { 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate // We got what we needed; skip until "{" is balanced 138*7c478bd9Sstevel@tonic-gate if (skipcurly && s.compareTo("}") == 0) { 139*7c478bd9Sstevel@tonic-gate skipcurly = false; 140*7c478bd9Sstevel@tonic-gate continue; 141*7c478bd9Sstevel@tonic-gate } 142*7c478bd9Sstevel@tonic-gate // First the realm name, then the admin server 143*7c478bd9Sstevel@tonic-gate if (!wantadmin) { 144*7c478bd9Sstevel@tonic-gate Name = new String(s); 145*7c478bd9Sstevel@tonic-gate wantadmin = true; 146*7c478bd9Sstevel@tonic-gate Server = ""; 147*7c478bd9Sstevel@tonic-gate Port = "0"; 148*7c478bd9Sstevel@tonic-gate } else { 149*7c478bd9Sstevel@tonic-gate if (s.compareTo("admin_server") == 0) { 150*7c478bd9Sstevel@tonic-gate s = t.nextToken(" \t\n\r=:"); 151*7c478bd9Sstevel@tonic-gate Server = new String(s); 152*7c478bd9Sstevel@tonic-gate if (t.hasMoreTokens()) { 153*7c478bd9Sstevel@tonic-gate s = t.nextToken(" \t\n\r=:"); 154*7c478bd9Sstevel@tonic-gate Port = new String(s); 155*7c478bd9Sstevel@tonic-gate } 156*7c478bd9Sstevel@tonic-gate 157*7c478bd9Sstevel@tonic-gate // Store result in the vector 158*7c478bd9Sstevel@tonic-gate r = new RealmInfo(Name, Server, Port); 159*7c478bd9Sstevel@tonic-gate RealmVector.addElement(r); 160*7c478bd9Sstevel@tonic-gate wantadmin = false; 161*7c478bd9Sstevel@tonic-gate skipcurly = true; 162*7c478bd9Sstevel@tonic-gate } 163*7c478bd9Sstevel@tonic-gate } 164*7c478bd9Sstevel@tonic-gate } else if (wantapp) { 165*7c478bd9Sstevel@tonic-gate if (wanturl && s.compareTo("help_url") == 0) { 166*7c478bd9Sstevel@tonic-gate if (t.hasMoreTokens()) { 167*7c478bd9Sstevel@tonic-gate HelpURL = t.nextToken(" \t\n\r="); 168*7c478bd9Sstevel@tonic-gate wantapp = false; 169*7c478bd9Sstevel@tonic-gate wanturl = false; 170*7c478bd9Sstevel@tonic-gate } 171*7c478bd9Sstevel@tonic-gate } else if (s.compareTo("gkadmin") == 0) 172*7c478bd9Sstevel@tonic-gate wanturl = true; 173*7c478bd9Sstevel@tonic-gate } 174*7c478bd9Sstevel@tonic-gate } 175*7c478bd9Sstevel@tonic-gate } while (line != null); 176*7c478bd9Sstevel@tonic-gate } 177*7c478bd9Sstevel@tonic-gate getDefaultRealm()178*7c478bd9Sstevel@tonic-gate public String getDefaultRealm() { 179*7c478bd9Sstevel@tonic-gate return DefRealm; 180*7c478bd9Sstevel@tonic-gate } 181*7c478bd9Sstevel@tonic-gate getHelpURL()182*7c478bd9Sstevel@tonic-gate public String getHelpURL() { 183*7c478bd9Sstevel@tonic-gate return HelpURL; 184*7c478bd9Sstevel@tonic-gate } 185*7c478bd9Sstevel@tonic-gate getAllRealms()186*7c478bd9Sstevel@tonic-gate public String getAllRealms() { 187*7c478bd9Sstevel@tonic-gate String s = ""; 188*7c478bd9Sstevel@tonic-gate for (int i = 0; i < RealmVector.size(); i++) { 189*7c478bd9Sstevel@tonic-gate RealmInfo r = (RealmInfo)RealmVector.elementAt(i); 190*7c478bd9Sstevel@tonic-gate s = new String(s + " " + r.RealmName); 191*7c478bd9Sstevel@tonic-gate } 192*7c478bd9Sstevel@tonic-gate return s; 193*7c478bd9Sstevel@tonic-gate } 194*7c478bd9Sstevel@tonic-gate getRealmServer(String realm)195*7c478bd9Sstevel@tonic-gate public String getRealmServer(String realm) { 196*7c478bd9Sstevel@tonic-gate for (int i = 0; i < RealmVector.size(); i++) { 197*7c478bd9Sstevel@tonic-gate RealmInfo r = (RealmInfo)RealmVector.elementAt(i); 198*7c478bd9Sstevel@tonic-gate if (realm.compareTo(r.RealmName) == 0) 199*7c478bd9Sstevel@tonic-gate return r.AdminServer; 200*7c478bd9Sstevel@tonic-gate } 201*7c478bd9Sstevel@tonic-gate return null; 202*7c478bd9Sstevel@tonic-gate } 203*7c478bd9Sstevel@tonic-gate getRealmPort(String realm)204*7c478bd9Sstevel@tonic-gate public String getRealmPort(String realm) { 205*7c478bd9Sstevel@tonic-gate for (int i = 0; i < RealmVector.size(); i++) { 206*7c478bd9Sstevel@tonic-gate RealmInfo r = (RealmInfo)RealmVector.elementAt(i); 207*7c478bd9Sstevel@tonic-gate if (realm.compareTo(r.RealmName) == 0) 208*7c478bd9Sstevel@tonic-gate return r.ServerPort; 209*7c478bd9Sstevel@tonic-gate } 210*7c478bd9Sstevel@tonic-gate return null; 211*7c478bd9Sstevel@tonic-gate } 212*7c478bd9Sstevel@tonic-gate 213*7c478bd9Sstevel@tonic-gate class RealmInfo extends Object { 214*7c478bd9Sstevel@tonic-gate String RealmName; 215*7c478bd9Sstevel@tonic-gate String AdminServer; 216*7c478bd9Sstevel@tonic-gate String ServerPort; 217*7c478bd9Sstevel@tonic-gate RealmInfo(String name, String server, String port)218*7c478bd9Sstevel@tonic-gate public RealmInfo(String name, String server, String port) { 219*7c478bd9Sstevel@tonic-gate RealmName = new String(name); 220*7c478bd9Sstevel@tonic-gate AdminServer = new String(server); 221*7c478bd9Sstevel@tonic-gate ServerPort = new String(port); 222*7c478bd9Sstevel@tonic-gate } 223*7c478bd9Sstevel@tonic-gate } 224*7c478bd9Sstevel@tonic-gate main(String[] args)225*7c478bd9Sstevel@tonic-gate public static void main(String[] args) { 226*7c478bd9Sstevel@tonic-gate Krb5Conf c = new Krb5Conf(); 227*7c478bd9Sstevel@tonic-gate System.out.println("Default: " + c.getDefaultRealm()); 228*7c478bd9Sstevel@tonic-gate System.out.println("Realms: " + c.getAllRealms()); 229*7c478bd9Sstevel@tonic-gate StringTokenizer t = new StringTokenizer(c.getAllRealms()); 230*7c478bd9Sstevel@tonic-gate while (t.hasMoreTokens()) { 231*7c478bd9Sstevel@tonic-gate String r = t.nextToken(); 232*7c478bd9Sstevel@tonic-gate String s = c.getRealmServer(r); 233*7c478bd9Sstevel@tonic-gate String p = c.getRealmPort(r); 234*7c478bd9Sstevel@tonic-gate System.out.println("For realm " + r + ", server is " + s 235*7c478bd9Sstevel@tonic-gate + ", port is " + p); 236*7c478bd9Sstevel@tonic-gate } 237*7c478bd9Sstevel@tonic-gate System.out.println("HelpURL: " + c.getHelpURL()); 238*7c478bd9Sstevel@tonic-gate } 239*7c478bd9Sstevel@tonic-gate } 240