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