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