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  * Copyright (c) 2001 by Sun Microsystems, Inc.
  24  * All rights reserved.
  25  */
  26 package com.sun.dhcpmgr.cli.dhcpconfig;
  27 
  28 import com.sun.dhcpmgr.cli.common.DhcpCliFunction;
  29 import com.sun.dhcpmgr.cli.common.Console;
  30 import com.sun.dhcpmgr.data.ValidationException;
  31 import com.sun.dhcpmgr.data.Network;
  32 import com.sun.dhcpmgr.data.Macro;
  33 import com.sun.dhcpmgr.data.DhcpdOptions;
  34 
  35 /**
  36  * The main class for the "unconfigure DHCP server" functionality of
  37  * dhcpconfig.
  38  */
  39 public class UnconfigureDhcp extends DhcpCfgFunction {
  40 
  41     /**
  42      * The valid options associated with unconfiguring a DHCP server.
  43      */
  44     static final int supportedOptions[] = {
  45         DhcpCfg.FORCE,
  46         DhcpCfg.DELETE_TABLES
  47     };
  48 
  49     /**
  50      * Constructs a UnconfigureDhcp object.
  51      */
  52     public UnconfigureDhcp() {
  53 
  54         validOptions = supportedOptions;
  55 
  56     } // constructor
  57 
  58     /**
  59      * Returns the option flag for this function.
  60      * @returns the option flag for this function.
  61      */
  62     public int getFunctionFlag() {
  63         return (DhcpCfg.UNCONFIGURE_DHCP);
  64     }
  65 
  66     /**
  67      * Executes the "unconfigure DHCP server" functionality.
  68      * @return DhcpCfg.SUCCESS or DhcpCfg.FAILURE
  69      */
  70     public int execute() {
  71 
  72         // Confirm?
  73         //
  74         if (!options.isSet(DhcpCfg.FORCE)) {
  75             String confirmationMsg = getString("unconfigure_confirmation");
  76             String affirmative = getString("affirmative");
  77             String negative = getString("negative");
  78             boolean doit = Console.promptUser(confirmationMsg, affirmative,
  79                 negative, true);
  80             if (!doit) {
  81                 return (DhcpCfg.FAILURE);
  82             }
  83         }
  84 
  85         // Retrieve the configuration values from the server.
  86         //
  87         boolean isRelay = false;
  88         try {
  89             DhcpdOptions opts = getSvcMgr().readDefaults();
  90             isRelay = opts.isRelay();
  91         } catch (Throwable e) {
  92             Object [] arguments = new Object[1];
  93             arguments[0] = getMessage(e);
  94             printErrMessage(getString("unconfigure_read_conf_error"),
  95                 arguments);
  96             return (DhcpCfg.FAILURE);
  97         }
  98 
  99         // Shut down the server if it is running
 100         //
 101         try {
 102             if (getSvcMgr().isServerRunning()) {
 103                 getSvcMgr().shutdown();
 104                 printMessage(getString("unconfigure_shutdown_progress"));
 105             } else {
 106                 printMessage(getString("unconfigure_no_shutdown_progress"));
 107             }
 108         } catch (Throwable e) {
 109             Object [] arguments = new Object[1];
 110             arguments[0] = getMessage(e);
 111             printErrMessage(getString("unconfigure_shutdown_error"),
 112                 arguments);
 113         }
 114 
 115         // Should we delete the dhcptab and the network tables?
 116         //
 117         boolean deleteTables = options.isSet(DhcpCfg.DELETE_TABLES);
 118 
 119         // If this server is just acting as a relay then we don't need to
 120         // clean up the dhcptab or the networks.
 121         //
 122         if (!isRelay) {
 123             // Delete the server macro.
 124             //
 125             try {
 126                 Macro serverMacro =
 127                     new Macro(getSvcMgr().getShortServerName());
 128                 getDhcptabMgr().deleteRecord(serverMacro, false);
 129                 printMessage(getString("unconfigure_server_macro_progress"));
 130             } catch (Throwable e) {
 131                 Object [] arguments = new Object[1];
 132                 arguments[0] = getMessage(e);
 133                 printErrMessage(getString("unconfigure_server_macro_error"),
 134                     arguments);
 135             }
 136 
 137             // Delete the dhcptab and the network tables if requested.
 138             //
 139             if (deleteTables) {
 140                 // Go get a list of the network tables to delete.
 141                 //
 142                 Network[] networks = new Network[0];
 143                 try {
 144                     networks = getNetMgr().getNetworks();
 145                     if (networks == null) {
 146                         networks = new Network[0];
 147                     }
 148                 } catch (Throwable e) {
 149                     Object [] arguments = new Object[1];
 150                     arguments[0] = getMessage(e);
 151                     printErrMessage(getString("unconfigure_get_nets_error"),
 152                         arguments);
 153                 }
 154 
 155                 // Delete the network tables
 156                 //
 157                 for (int i = 0; i < networks.length; ++i) {
 158                     String netString = networks[i].toString();
 159                     try {
 160                         getNetMgr().deleteNetwork(netString, false);
 161                         Object [] arguments = new Object[1];
 162                         arguments[0] = netString;
 163                         printMessage(getString("unconfigure_network_progress"),
 164                             arguments);
 165                     } catch (Throwable e) {
 166                         Object [] arguments = new Object[2];
 167                         arguments[0] = netString;
 168                         arguments[1] = getMessage(e);
 169                         printErrMessage(getString("unconfigure_network_error"),
 170                             arguments);
 171                     }
 172                 }
 173 
 174                 // Delete the dhcptab
 175                 //
 176                 try {
 177                     getDhcptabMgr().deleteDhcptab();
 178                     printMessage(getString("unconfigure_dhcptab_progress"));
 179                 } catch (Throwable e) {
 180                     Object [] arguments = new Object[1];
 181                     arguments[0] = getMessage(e);
 182                     printErrMessage(getString("unconfigure_dhcptab_error"),
 183                         arguments);
 184                 }
 185             }
 186         }
 187 
 188         // Remove the configuration file
 189         //
 190         try {
 191             getSvcMgr().removeDefaults();
 192             printMessage(getString("unconfigure_remove_conf_progress"));
 193         } catch (Throwable e) {
 194             Object [] arguments = new Object[1];
 195             arguments[0] = getMessage(e);
 196             printErrMessage(getString("unconfigure_remove_conf_error"),
 197                 arguments);
 198         }
 199 
 200         return (DhcpCfg.SUCCESS);
 201 
 202     } // execute
 203 
 204 } // UnconfigureDhcp