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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 package com.sun.dhcpmgr.server;
26 
27 import java.util.*;
28 import java.util.jar.*;
29 import java.net.InetAddress;
30 import java.net.UnknownHostException;
31 
32 import com.sun.dhcpmgr.bridge.*;
33 import com.sun.dhcpmgr.data.*;
34 
35 /**
36  * This class provides the capabilities for managing the the basic service
37  * parameters which are not stored in the dhcptab or per-network tables.
38  */
39 public class DhcpServiceMgrImpl implements DhcpServiceMgr {
40     private Bridge bridge;
41 
42     private String serverName;
43     private String shortServerName;
44     private InetAddress serverAddress;
45 
DhcpServiceMgrImpl(Bridge bridge)46     public DhcpServiceMgrImpl(Bridge bridge) {
47 	this.bridge = bridge;
48 
49 	try {
50 	    serverAddress = InetAddress.getLocalHost();
51 	    serverName = serverAddress.getHostName();
52 
53 	    int i = serverName.indexOf('.');
54 	    if (i == -1) {
55 		shortServerName = serverName;
56 	    } else {
57 		shortServerName = serverName.substring(0, i);
58 	    }
59 	} catch (UnknownHostException e) {
60 	    serverName = shortServerName = "";
61 	}
62     }
63 
getServerName()64     public String getServerName() {
65 	return serverName;
66     }
67 
getShortServerName()68     public String getShortServerName() {
69 	return shortServerName;
70     }
71 
getServerAddress()72     public InetAddress getServerAddress() {
73 	return serverAddress;
74     }
75 
makeLocation(DhcpDatastore datastore)76     public void makeLocation(DhcpDatastore datastore)
77 	throws BridgeException {
78 	bridge.makeLocation(datastore);
79     }
80 
getDataStore(String resource)81     public DhcpDatastore getDataStore(String resource) throws BridgeException {
82 	return bridge.getDataStore(resource);
83     }
84 
85     /**
86      * Retrieve the list of possible data stores for this server
87      * @return an array of data store module names.
88      */
getDataStores()89     public DhcpDatastore [] getDataStores() throws BridgeException {
90 	return bridge.getDataStores();
91     }
92 
93     /**
94      * Retrieve a list of options from the DHCP inittab.
95      * @return an array of options
96      */
getInittabOptions(byte context)97     public Option [] getInittabOptions(byte context) throws BridgeException {
98 	return bridge.getInittabOptions(context);
99     }
100 
getDataStoreClassname(String dataStoreName)101     public String getDataStoreClassname(String dataStoreName)
102 	throws BridgeException {
103 
104 	String beansDirectory = new String("/usr/sadm/admin/dhcpmgr/");
105 	String jarPath = beansDirectory.concat(dataStoreName).concat(".jar");
106 	String className = null;
107 	try {
108 	    JarFile jarFile = new JarFile(jarPath);
109 	    Manifest manifest = jarFile.getManifest();
110 	    if (manifest == null) {
111 		throw new BridgeException();
112 	    }
113 	    Attributes attrs = manifest.getMainAttributes();
114 	    if (attrs == null) {
115 		throw new BridgeException();
116 	    }
117 	    className = attrs.getValue("Name");
118 	    if (!className.endsWith(".class")) {
119 		throw new BridgeException();
120 	    }
121 	    className = className.substring(0, className.length() - 6);
122 	    className = className.replace('/', '.');
123 	} catch (Throwable e) {
124 	    throw new BridgeException();
125 	}
126 
127 	return className;
128     }
129 
130     /**
131      * Retrieve the contents of the DHCP config file.
132      * @return the config settings
133      */
readDefaults()134     public DhcpdOptions readDefaults() throws BridgeException {
135 	return bridge.readDefaults();
136     }
137 
138     /**
139      * Write new settings to the DHCP config file.
140      * @param cfgs the new config settings
141      */
writeDefaults(DhcpdOptions cfgs)142     public void writeDefaults(DhcpdOptions cfgs) throws BridgeException {
143 	bridge.writeDefaults(cfgs);
144     }
145 
146     /**
147      * Remove the DHCP config file.
148      */
removeDefaults()149     public void removeDefaults() throws BridgeException {
150 	bridge.removeDefaults();
151     }
152 
153     /**
154      * Start the server
155      */
startup()156     public void startup() throws BridgeException {
157 	bridge.startup();
158     }
159 
160     /**
161      * Stop the server
162      */
shutdown()163     public void shutdown() throws BridgeException {
164 	bridge.shutdown();
165     }
166 
167     /**
168      * Send the server a SIGHUP to re-read the dhcptab
169      */
reload()170     public void reload() throws BridgeException {
171 	bridge.reload();
172     }
173 
174     /**
175      * Get the list of possible interfaces for the server to monitor
176      * @return an array of interfaces
177      */
getInterfaces()178     public IPInterface [] getInterfaces() throws BridgeException {
179 	return bridge.getInterfaces();
180     }
181 
182     /**
183      * Break up a line into a list of arguments
184      * @param input line
185      * @return an array of arguments
186      */
getArguments(String line)187     public String [] getArguments(String line) throws BridgeException {
188 	return bridge.getArguments(line);
189     }
190 
191     /**
192      * Get the default value for an option which would take a string
193      * @param optionName name of the option
194      * @param arg additional information needed for this code
195      */
getStringOption(String optionName, String arg)196     public synchronized String getStringOption(String optionName, String arg)
197 	    throws BridgeException {
198 	Option option = OptionsTable.getTable().get(optionName);
199 	return bridge.getStringOption(option.getCode(), arg);
200     }
201 
202     /**
203      * Get the default value for an option which would take one or more IP addrs
204      * @param optionName name of the option
205      * @param arg additional information needed for this code
206      */
getIPOption(String optionName, String arg)207     public synchronized IPAddress [] getIPOption(String optionName, String arg)
208 	    throws BridgeException {
209 	Option option = OptionsTable.getTable().get(optionName);
210 	return bridge.getIPOption(option.getCode(), arg);
211     }
212 
213     /**
214      * Get the default value for an option which would take one or more numbers
215      * @param optionName name of the option
216      * @param arg additional information needed for this code
217      */
getNumberOption(String optionName, String arg)218     public synchronized long [] getNumberOption(String optionName, String arg)
219 	    throws BridgeException {
220 	Option option = OptionsTable.getTable().get(optionName);
221 	return bridge.getNumberOption(option.getCode(), arg);
222     }
223 
224     /**
225      * Check if the datastore version is current.
226      * @return true if the datastore version if current.
227      */
isVersionCurrent()228     public boolean isVersionCurrent() throws BridgeException {
229 	return bridge.isVersionCurrent();
230     }
231 
232     /**
233      * Check if the server is currently running
234      * @return true if the server process is started
235      */
isServerRunning()236     public boolean isServerRunning() throws BridgeException {
237 	return bridge.isServerRunning();
238     }
239 
240     /**
241      * Determines the whether or not the hosts table under a given
242      * name service can be managed.
243      * @param resource name service resource (files, dns)
244      * @param domain the name service domain (ignored for files)
245      * @return true if the user can manage the table
246      */
isHostsValid(String resource, String domain)247     public boolean isHostsValid(String resource, String domain) {
248 
249 	return DhcpHostsTable.isHostsValid(resource, domain);
250 
251     } // isHostsManageable
252 }
253