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) 2001 by Sun Microsystems, Inc.
  26  * All rights reserved.
  27  */
  28 package com.sun.dhcpmgr.cli.dhcpconfig;
  29 
  30 import com.sun.dhcpmgr.cli.common.DhcpCliFunction;
  31 import com.sun.dhcpmgr.data.DhcpdOptions;
  32 import com.sun.dhcpmgr.data.ValidationException;
  33 
  34 /**
  35  * The main class for the "configure BOOTP relay" functionality of dhcpconfig.
  36  */
  37 public class ConfigureBootp extends DhcpCfgFunction {
  38 
  39     /**
  40      * The valid options associated with configuring a BOOTP relay agent.
  41      */
  42     static final int supportedOptions[] = {
  43     };
  44 
  45     /**
  46      * The addresses of servers for which to serve as a relay.
  47      */
  48     String addresses;
  49 
  50     /**
  51      * Constructs a ConfigureBootp object.
  52      * @param addresses the addresses of servers for which to serve as a relay.
  53      */
  54     public ConfigureBootp(String addresses) {
  55 
  56         validOptions = supportedOptions;
  57         this.addresses = addresses;
  58 
  59     } // constructor
  60 
  61     /**
  62      * Returns the option flag for this function.
  63      * @returns the option flag for this function.
  64      */
  65     public int getFunctionFlag() {
  66         return (DhcpCfg.CONFIGURE_BOOTP);
  67     }
  68 
  69     /**
  70      * Executes the "configure BOOTP relay" functionality.
  71      * @return DhcpCfg.SUCCESS or DhcpCfg.FAILURE
  72      */
  73     public int execute() {
  74 
  75         // Check to see if DHCP or BOOTP relay is already configured.
  76         //
  77         boolean isServer = false;
  78         boolean isRelay = false;
  79         try {
  80             DhcpdOptions opts = getSvcMgr().readDefaults();
  81             if (opts.isRelay()) {
  82                 isRelay = true;
  83             } else {
  84                 isServer = true;
  85             }
  86         } catch (Throwable e) {
  87             // this is to be expected
  88         }
  89 
  90         if (isServer) {
  91             printErrMessage(getString("config_dhcp_configured_error"));
  92             return (DhcpCfg.FAILURE);
  93         }
  94 
  95         if (isRelay) {
  96             printErrMessage(getString("config_bootp_configured_error"));
  97             return (DhcpCfg.FAILURE);
  98         }
  99 
 100         // Write the information to the DHCP configuration file.
 101         //
 102         try {
 103             IPAddressList list = new IPAddressList(addresses);
 104             DhcpdOptions options = new DhcpdOptions();
 105             options.setDaemonEnabled(true);
 106             options.setRelay(true, list.toString());
 107             getSvcMgr().writeDefaults(options);
 108             printMessage(getString("config_create_conf_progress"));
 109         } catch (ValidationException e) {
 110             printErrMessage(getMessage(e));
 111             return (DhcpCfg.FAILURE);
 112         } catch (Throwable e) {
 113             Object [] arguments = new Object[1];
 114             arguments[0] = getMessage(e);
 115             printErrMessage(getString("config_writing_conf_error"), arguments);
 116             return (DhcpCfg.FAILURE);
 117         }
 118 
 119         // Start it up.
 120         //
 121         try {
 122             getSvcMgr().startup();
 123             printMessage(getString("config_startup_progress"));
 124         } catch (Throwable e) {
 125             Object [] arguments = new Object[1];
 126             arguments[0] = getMessage(e);
 127             printErrMessage(getString("config_startup_error"), arguments);
 128             return (DhcpCfg.FAILURE);
 129         }
 130 
 131         return (DhcpCfg.SUCCESS);
 132 
 133     } // execute
 134 
 135 } // ConfigureBootp