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 2001-2002 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 package com.sun.dhcpmgr.cli.dhcpconfig;
  27 
  28 import com.sun.dhcpmgr.cli.common.*;
  29 
  30 import java.lang.IllegalArgumentException;
  31 import java.text.MessageFormat;
  32 
  33 /**
  34  * This class represents the entry point to the DHCP CLI dhcp configuration
  35  * administration.
  36  */
  37 public class DhcpCfg
  38     extends DhcpCliProgram {
  39 
  40     /**
  41      * The program signature.
  42      */
  43     public static final String SIGNATURE = "dhcpconfig: ";
  44 
  45     /**
  46      * The valid options for all DhcpCfg administration.
  47      */
  48     private static String optString =
  49         "DUCnfxbkgI:R:N:X:r;p:u:l:d;a:m:t:y:s:o:P;Seq";
  50 
  51     public static final int CONFIGURE_DHCP              = 'D';
  52     public static final int CONFIGURE_BOOTP             = 'R';
  53     public static final int UNCONFIGURE_DHCP            = 'U';
  54     public static final int CONFIGURE_NETWORK           = 'N';
  55     public static final int CONVERT_DATA_STORE          = 'C';
  56     public static final int EXPORT_DATA                 = 'X';
  57     public static final int IMPORT_DATA                 = 'I';
  58     public static final int CONFIGURE_SERVER_PARAMETER  = 'P';
  59     public static final int CONFIGURE_SERVICE           = 'S';
  60 
  61     public static final int NON_NEGOTIABLE_LEASE        = 'n';
  62     public static final int FORCE                       = 'f';
  63     public static final int DELETE_DATA                 = 'x';
  64     public static final int DELETE_TABLES               = 'x';
  65     public static final int KEEP_TABLES                 = 'k';
  66     public static final int POINT_TO_POINT              = 'b';
  67     public static final int RESOURCE                    = 'r';
  68     public static final int RESOURCE_CONFIG             = 'u';
  69     public static final int PATH                        = 'p';
  70     public static final int LEASE_LENGTH                = 'l';
  71     public static final int DNS_DOMAIN                  = 'd';
  72     public static final int SERVICE_DISABLE             = 'd';
  73     public static final int DNS_ADDRESSES               = 'a';
  74     public static final int NIS_ADDRESSES               = 'a';
  75     public static final int NETWORK_ADDRESSES           = 'a';
  76     public static final int SUBNET_MASK                 = 'm';
  77     public static final int MACRO_LIST                  = 'm';
  78     public static final int OPTION_LIST                 = 'o';
  79     public static final int ROUTER_ADDRESSES            = 't';
  80     public static final int NIS_DOMAIN                  = 'y';
  81     public static final int SIGHUP                      = 'g';
  82     public static final int SERVICE_ENABLE              = 'e';
  83     public static final int SERVICE_REENABLE            = 'r';
  84     public static final int SERVICE_QUERY               = 'q';
  85 
  86     /**
  87      * Constructs a dhcpconfig command.
  88      * @param args the options to the command.
  89      */
  90     public DhcpCfg(String args[]) {
  91 
  92         // Set the options.
  93         //
  94         options = new DhcpCliOptions();
  95         this.args = args;
  96 
  97     } // constructor
  98 
  99     /**
 100      * Returns the manpage signature for the program.
 101      * @return the manpage signature for the program.
 102      */
 103     public String getManPage() {
 104         return "dhcpconfig(1M)";
 105     }
 106 
 107     /**
 108      * Displays program usage.
 109      */
 110     public void usage() {
 111 
 112         DhcpCliPrint.printErrMessage(getString("dhcpcfg_usage"));
 113 
 114     } // usage
 115 
 116     /**
 117      * Executes the program function.
 118      * @return SUCCESS or FAILURE
 119      */
 120     public int execute() {
 121 
 122         int returnCode = SUCCESS;
 123 
 124         // Get the options and go exec the correct function.
 125         //
 126         GetOpt getopt = new GetOpt(args, optString);
 127         try {
 128             int option;
 129             while ((option = getopt.getNextOption()) != -1) {
 130                 processArg(option, getopt.getOptionArg());
 131             }
 132 
 133             int lastIndex = getopt.getNextOptionIndex();
 134             if (args.length != lastIndex) {
 135                 Object [] arguments = new Object[1];
 136                 arguments[0] = args[lastIndex];
 137                 MessageFormat form = new MessageFormat(
 138                     ResourceStrings.getString("dhcpcfg_invalid_arg"));
 139                 throw new IllegalArgumentException(form.format(arguments));
 140             }
 141 
 142             if (function == null) {
 143                 String msg = getString("dhcpcfg_no_function_error");
 144                 throw new IllegalArgumentException(msg);
 145             }
 146 
 147             function.setOptions(options);
 148             function.setStandardOptions();
 149             returnCode = function.execute();
 150 
 151         } catch (IllegalArgumentException e) {
 152             StringBuffer msg = new StringBuffer(SIGNATURE);
 153             msg.append(DhcpCliFunction.getMessage(e));
 154             DhcpCliPrint.printErrMessage(msg.toString());
 155             DhcpCliPrint.printErrMessage("");
 156             usage();
 157             returnCode = FAILURE;
 158         } catch (Throwable e) {
 159             StringBuffer msg = new StringBuffer(SIGNATURE);
 160             msg.append(DhcpCliFunction.getMessage(e));
 161             DhcpCliPrint.printErrMessage(msg.toString());
 162             returnCode = FAILURE;
 163         }
 164 
 165         return (returnCode);
 166 
 167     } // execute
 168 
 169     /**
 170      * Processes one program argument.
 171      * @param option the option flag
 172      * @param value the option value(if any)
 173      * @exception IllegalArgumentException if an invalid argument was entered
 174      */
 175     public void processArg(int option, String value)
 176         throws IllegalArgumentException {
 177 
 178         switch (option) {
 179         case CONFIGURE_DHCP:
 180             setFunction(new ConfigureDhcp());
 181             break;
 182         case CONFIGURE_BOOTP:
 183             setFunction(new ConfigureBootp(value));
 184             break;
 185         case UNCONFIGURE_DHCP:
 186             setFunction(new UnconfigureDhcp());
 187             break;
 188         case CONFIGURE_NETWORK:
 189             setFunction(new ConfigureNetwork(value));
 190             break;
 191         case CONVERT_DATA_STORE:
 192             setFunction(new ConvertDataStore());
 193             break;
 194         case EXPORT_DATA:
 195             setFunction(new ExportData(value));
 196             break;
 197         case IMPORT_DATA:
 198             setFunction(new ImportData(value));
 199             break;
 200         case CONFIGURE_SERVER_PARAMETER:
 201             setFunction(new ServerParameter(value));
 202             break;
 203         case CONFIGURE_SERVICE:
 204             setFunction(new ConfigureService());
 205             break;
 206         default:
 207             options.setOption(option, value);
 208         }
 209 
 210     } // processArg
 211 
 212     /**
 213      * Returns a localized string for this function
 214      * @param key the resource bundle string identifier
 215      * @return string from resource bundle.
 216      */
 217     public String getString(String key) {
 218 
 219         return ResourceStrings.getString(key);
 220 
 221     } // getString
 222 
 223     /**
 224      * The entry point for the program.
 225      * @param args the program arguments
 226      */
 227     public static void main(String[] args) {
 228 
 229         DhcpCfg dhcpconfig = new DhcpCfg(args);
 230         int returnCode = DhcpCfg.FAILURE;
 231         if (dhcpconfig.isValidUser()) {
 232             returnCode = dhcpconfig.execute();
 233         }
 234         System.exit(returnCode);
 235 
 236     } // main
 237 
 238 } // DhcpConfig