/usr/src/cmd/cmd-inet/usr.sadm/dhcpmgr/com/sun/dhcpmgr/server/DhcptabMgrImpl.java

https://github.com/buffygb/illumos-gate · Java · 461 lines · 233 code · 54 blank · 174 comment · 30 complexity · 885a8fe87c8f98b736bd8e2e1da66b9c MD5 · raw file

  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. import java.util.*;
  27. import java.net.InetAddress;
  28. import com.sun.dhcpmgr.bridge.*;
  29. import com.sun.dhcpmgr.data.*;
  30. /**
  31. * This class provides methods to manage the contents of the dhcptab.
  32. */
  33. public class DhcptabMgrImpl implements DhcptabMgr {
  34. private Bridge bridge;
  35. /**
  36. * Create a new DhcptabMgr using the provided native bridge.
  37. * @param bridge the native bridge class which actually does the work.
  38. */
  39. public DhcptabMgrImpl(Bridge bridge) {
  40. this.bridge = bridge;
  41. }
  42. /**
  43. * Create an option.
  44. * @param name the name of the option.
  45. * @param value the value for the option in dhcptab(4) format.
  46. * @return the Option.
  47. */
  48. public Option createOption(String name, String value)
  49. throws BridgeException {
  50. return bridge.createOption(name, value);
  51. }
  52. /**
  53. * Retrieve all options currently defined in the dhcptab.
  54. * @return an array of Options
  55. */
  56. public Option [] getOptions() throws BridgeException {
  57. return getOptions(null);
  58. }
  59. /**
  60. * Retrieve all options currently defined in the dhcptab.
  61. * @param datastore user-supplied datastore attributes
  62. * @return an array of Options
  63. */
  64. public Option [] getOptions(DhcpDatastore datastore)
  65. throws BridgeException {
  66. return bridge.getOptions(datastore);
  67. }
  68. /**
  69. * Retrieve all the macros currently defined in the dhcptab.
  70. * @return an array of Macros
  71. */
  72. public Macro [] getMacros() throws BridgeException {
  73. return getMacros(null);
  74. }
  75. /**
  76. * Retrieve all the macros currently defined in the dhcptab.
  77. * @param datastore user-supplied datastore attributes
  78. * @return an array of Macros
  79. */
  80. public Macro [] getMacros(DhcpDatastore datastore)
  81. throws BridgeException {
  82. /*
  83. * Load the vendor and site options before loading the macros
  84. * so we can validate correctly, adding them to the standard options
  85. * table.
  86. */
  87. OptionsTable optionsTable = OptionsTable.getTable();
  88. optionsTable.add(bridge.getOptions(datastore));
  89. return bridge.getMacros(datastore);
  90. }
  91. /**
  92. * Create a given record in the dhcptab, and signal the server to
  93. * reload the dhcptab if so requested.
  94. * @param rec the record to add to the table
  95. * @param signalServer true if the server is to be sent a SIGHUP
  96. */
  97. public void createRecord(DhcptabRecord rec, boolean signalServer)
  98. throws BridgeException {
  99. createRecord(rec, signalServer, null);
  100. }
  101. /**
  102. * Create a given record in the dhcptab, and signal the server to
  103. * reload the dhcptab if so requested.
  104. * @param rec the record to add to the table
  105. * @param signalServer true if the server is to be sent a SIGHUP
  106. * @param datastore user-supplied datastore attributes
  107. */
  108. public void createRecord(DhcptabRecord rec, boolean signalServer,
  109. DhcpDatastore datastore) throws BridgeException {
  110. bridge.createDhcptabRecord(rec, datastore);
  111. if (signalServer) {
  112. bridge.reload();
  113. }
  114. }
  115. /**
  116. * Modify a given record in the dhcptab, and signal the server to reload
  117. * the dhcptab if so requested
  118. * @param oldRec the current record to modify
  119. * @param newRec the new record to be placed in the table
  120. * @param signalServer true if the server is to be sent a SIGHUP
  121. */
  122. public void modifyRecord(DhcptabRecord oldRec, DhcptabRecord newRec,
  123. boolean signalServer) throws BridgeException {
  124. modifyRecord(oldRec, newRec, signalServer, null);
  125. }
  126. /**
  127. * Modify a given record in the dhcptab, and signal the server to reload
  128. * the dhcptab if so requested
  129. * @param oldRec the current record to modify
  130. * @param newRec the new record to be placed in the table
  131. * @param signalServer true if the server is to be sent a SIGHUP
  132. * @param datastore user-supplied datastore attributes
  133. */
  134. public void modifyRecord(DhcptabRecord oldRec, DhcptabRecord newRec,
  135. boolean signalServer, DhcpDatastore datastore)
  136. throws BridgeException {
  137. bridge.modifyDhcptabRecord(oldRec, newRec, datastore);
  138. if (signalServer) {
  139. bridge.reload();
  140. }
  141. }
  142. /**
  143. * Delete a given record from the dhcptab, and signal the server to reload
  144. * the dhcptab if so requested
  145. * @param rec the record to delete
  146. * @param signalServer true if the server is to be sent a SIGHUP
  147. */
  148. public void deleteRecord(DhcptabRecord rec, boolean signalServer)
  149. throws BridgeException {
  150. deleteRecord(rec, signalServer, null);
  151. }
  152. /**
  153. * Delete a given record from the dhcptab, and signal the server to reload
  154. * the dhcptab if so requested
  155. * @param rec the record to delete
  156. * @param signalServer true if the server is to be sent a SIGHUP
  157. * @param datastore user-supplied datastore attributes
  158. */
  159. public void deleteRecord(DhcptabRecord rec, boolean signalServer,
  160. DhcpDatastore datastore) throws BridgeException {
  161. bridge.deleteDhcptabRecord(rec, datastore);
  162. if (signalServer) {
  163. bridge.reload();
  164. }
  165. }
  166. /**
  167. * Delete a record by name and type
  168. * @param key The key for the record
  169. * @param type The type of record; either MACRO or OPTION
  170. */
  171. private void deleteRecord(String name, String type) throws BridgeException {
  172. DhcptabRecord rec = null;
  173. if (type.equals(DhcptabRecord.MACRO)) {
  174. rec = getMacro(name);
  175. } else {
  176. rec = getOption(name);
  177. }
  178. deleteRecord(rec, false);
  179. }
  180. /**
  181. * Delete a set of records.
  182. * @return An array of ActionError, one error for each record not deleted
  183. */
  184. private ActionError [] deleteRecords(DhcptabRecord [] recs) {
  185. ArrayList errorList = new ArrayList();
  186. for (int i = 0; i < recs.length; ++i) {
  187. try {
  188. deleteRecord(recs[i], false);
  189. } catch (BridgeException e) {
  190. errorList.add(new ActionError(recs[i].getKey(), e));
  191. }
  192. }
  193. return (ActionError[])errorList.toArray(new ActionError[0]);
  194. }
  195. private ActionError [] deleteAllRecords(String type)
  196. throws BridgeException {
  197. DhcptabRecord [] recs;
  198. if (type.equals(DhcptabRecord.MACRO)) {
  199. recs = getMacros();
  200. } else {
  201. recs = getOptions();
  202. }
  203. return deleteRecords(recs);
  204. }
  205. /**
  206. * Delete all macros
  207. * @return An array of ActionError, one error for each macro not deleted
  208. */
  209. public ActionError [] deleteAllMacros() throws BridgeException {
  210. return deleteAllRecords(DhcptabRecord.MACRO);
  211. }
  212. /**
  213. * Delete all options
  214. * @return An array of ActionError, one error for each option not deleted
  215. */
  216. public ActionError [] deleteAllOptions() throws BridgeException {
  217. return deleteAllRecords(DhcptabRecord.OPTION);
  218. }
  219. /**
  220. * Delete a list of macros identified by name
  221. * @param macroNames Names of the macros to delete
  222. * @return An array of ActionError, one element per macro not deleted
  223. */
  224. public ActionError [] deleteMacros(String [] macroNames) {
  225. ArrayList errorList = new ArrayList();
  226. for (int i = 0; i < macroNames.length; ++i) {
  227. try {
  228. deleteRecord(macroNames[i], DhcptabRecord.MACRO);
  229. } catch (BridgeException e) {
  230. errorList.add(new ActionError(macroNames[i], e));
  231. }
  232. }
  233. return (ActionError [])errorList.toArray(new ActionError[0]);
  234. }
  235. /**
  236. * Delete a list of options identified by name
  237. * @param optionNames Names of options to delete
  238. * @return An array of ActionError, one element per option not deleted
  239. */
  240. public ActionError [] deleteOptions(String [] optionNames) {
  241. ArrayList errorList = new ArrayList();
  242. for (int i = 0; i < optionNames.length; ++i) {
  243. try {
  244. deleteRecord(optionNames[i], DhcptabRecord.OPTION);
  245. } catch (BridgeException e) {
  246. errorList.add(new ActionError(optionNames[i], e));
  247. }
  248. }
  249. return (ActionError [])errorList.toArray(new ActionError[0]);
  250. }
  251. /**
  252. * Retrieve a given macro from the dhcptab.
  253. * @param key the key of the record to retrieve
  254. * @return the Macro for the given key
  255. */
  256. public Macro getMacro(String key)
  257. throws BridgeException {
  258. return getMacro(key, null);
  259. }
  260. /**
  261. * Retrieve a given macro from the dhcptab.
  262. * @param key the key of the record to retrieve
  263. * @param datastore user-supplied datastore attributes
  264. * @return the Macro for the given key
  265. */
  266. public Macro getMacro(String key, DhcpDatastore datastore)
  267. throws BridgeException {
  268. OptionsTable optionsTable = OptionsTable.getTable();
  269. optionsTable.add(bridge.getOptions(datastore));
  270. return bridge.getMacro(key, datastore);
  271. }
  272. /**
  273. * Retrieve a given option from the dhcptab.
  274. * @param key the key of the record to retrieve
  275. * @return the Option for the given key
  276. */
  277. public Option getOption(String key)
  278. throws BridgeException {
  279. return getOption(key, null);
  280. }
  281. /**
  282. * Retrieve a given option from the dhcptab.
  283. * @param key the key of the record to retrieve
  284. * @param datastore user-supplied datastore attributes
  285. * @return the Option for the given key
  286. */
  287. public Option getOption(String key, DhcpDatastore datastore)
  288. throws BridgeException {
  289. return bridge.getOption(key, datastore);
  290. }
  291. /**
  292. * Create a new dhcptab converting the one in the server's data store,
  293. * into a new data store.
  294. * @param datastore user-supplied datastore attributes
  295. */
  296. public void cvtDhcptab(DhcpDatastore datastore)
  297. throws BridgeException {
  298. bridge.cvtDhcptab(datastore);
  299. }
  300. /**
  301. * Create a new empty dhcptab in the server's data store, which must
  302. * already be configured.
  303. */
  304. public void createDhcptab() throws BridgeException {
  305. createDhcptab(null);
  306. }
  307. /**
  308. * Create a new empty dhcptab in the server's data store, which must
  309. * already be configured.
  310. * @param datastore user-supplied datastore attributes
  311. */
  312. public void createDhcptab(DhcpDatastore datastore)
  313. throws BridgeException {
  314. bridge.createDhcptab(datastore);
  315. }
  316. /**
  317. * Delete the server's dhcptab in the current data store.
  318. */
  319. public void deleteDhcptab() throws BridgeException {
  320. deleteDhcptab(null);
  321. }
  322. /**
  323. * Delete the server's dhcptab in the current data store.
  324. * @param datastore user-supplied datastore attributes
  325. */
  326. public void deleteDhcptab(DhcpDatastore datastore)
  327. throws BridgeException {
  328. bridge.deleteDhcptab(datastore);
  329. }
  330. public void createLocaleMacro()
  331. throws BridgeException, ValidationException {
  332. createLocaleMacro(null);
  333. }
  334. public void createLocaleMacro(DhcpDatastore datastore)
  335. throws BridgeException, ValidationException {
  336. Macro macro = new Macro();
  337. macro.setKey("Locale");
  338. macro.storeOption(StandardOptions.CD_TIMEOFFSET,
  339. String.valueOf(TimeZone.getDefault().getRawOffset()/1000));
  340. createRecord(macro, false);
  341. }
  342. public void createServerMacro(String svrName,
  343. InetAddress svrAddress, int leaseLength,
  344. boolean leaseNegotiable, String dnsDomain, Vector dnsServs)
  345. throws BridgeException, ValidationException {
  346. createServerMacro(svrName, svrAddress, leaseLength, leaseNegotiable,
  347. dnsDomain, dnsServs, null);
  348. }
  349. public void createServerMacro(String svrName,
  350. InetAddress svrAddress, int leaseLength,
  351. boolean leaseNegotiable, String dnsDomain, Vector dnsServs,
  352. DhcpDatastore datastore)
  353. throws BridgeException, ValidationException {
  354. Macro macro = new Macro();
  355. macro.setKey(svrName);
  356. macro.storeOption("Include", "Locale");
  357. macro.storeOption(StandardOptions.CD_TIMESERV, svrAddress);
  358. macro.storeOption(StandardOptions.CD_LEASE_TIME,
  359. String.valueOf(leaseLength));
  360. if (leaseNegotiable) {
  361. macro.storeOption(StandardOptions.CD_BOOL_LEASENEG, null);
  362. }
  363. if (dnsDomain != null && dnsDomain.length() != 0 &&
  364. dnsServs != null && dnsServs.size() != 0) {
  365. macro.storeOption(StandardOptions.CD_DNSDOMAIN, dnsDomain);
  366. macro.storeOption(StandardOptions.CD_DNSSERV, dnsServs);
  367. }
  368. // First delete it in case it's already there
  369. try {
  370. deleteRecord(macro, false);
  371. } catch (Throwable e) {
  372. // Ignore any error
  373. }
  374. createRecord(macro, false);
  375. }
  376. public synchronized void createNetworkMacro(Network network,
  377. IPAddress [] routers, boolean isLan, String nisDomain, Vector nisServs)
  378. throws BridgeException, ValidationException {
  379. createNetworkMacro(network, routers, isLan, nisDomain, nisServs,
  380. null);
  381. }
  382. public void createNetworkMacro(Network network,
  383. IPAddress [] routers, boolean isLan, String nisDomain, Vector nisServs,
  384. DhcpDatastore datastore) throws BridgeException, ValidationException {
  385. Macro macro = new Macro();
  386. macro.setKey(network.toString());
  387. macro.storeOption(StandardOptions.CD_SUBNETMASK, network.getMask());
  388. if (routers == null) {
  389. macro.storeOption(StandardOptions.CD_ROUTER_DISCVRY_ON, "1");
  390. } else {
  391. for (int i = 0; i < routers.length; i++) {
  392. macro.storeOption(StandardOptions.CD_ROUTER, routers[i]);
  393. }
  394. }
  395. if (isLan) {
  396. macro.storeOption(StandardOptions.CD_BROADCASTADDR,
  397. network.getBroadcastAddress());
  398. }
  399. // NIS config
  400. if (nisDomain != null && nisDomain.length() != 0 &&
  401. nisServs != null && nisServs.size() != 0) {
  402. macro.storeOption(StandardOptions.CD_NIS_DOMAIN, nisDomain);
  403. macro.storeOption(StandardOptions.CD_NIS_SERV, nisServs);
  404. }
  405. createRecord(macro, false);
  406. }
  407. }