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.pntadm; 27 28 import com.sun.dhcpmgr.cli.common.Util; 29 import com.sun.dhcpmgr.data.DhcpClientRecord; 30 import com.sun.dhcpmgr.data.Macro; 31 import com.sun.dhcpmgr.data.Network; 32 import com.sun.dhcpmgr.bridge.BridgeException; 33 import com.sun.dhcpmgr.bridge.NoEntryException; 34 35 import java.lang.IllegalArgumentException; 36 37 /** 38 * The main class for the "modify client" functionality of pntadm. 39 */ 40 public class ModifyClientEntry extends PntAdmFunction { 41 42 /** 43 * The valid options associated with modifying a client entry. 44 */ 45 static final int supportedOptions[] = { 46 PntAdm.NEW_IP, 47 PntAdm.COMMENT, 48 PntAdm.LEASE_EXPIRATION, 49 PntAdm.FLAGS, 50 PntAdm.CLIENTID, 51 PntAdm.CONVERT_CLIENTID, 52 PntAdm.MACRO_NAME, 53 PntAdm.VERIFY_MACRO, 54 PntAdm.SERVER, 55 PntAdm.RESOURCE, 56 PntAdm.RESOURCE_CONFIG, 57 PntAdm.PATH 58 }; 59 60 /** 61 * The client entry to modify. 62 */ 63 String clientIP; 64 65 /** 66 * Constructs a ModifyClientEntry object for the client, clientIP. 67 * @param clientIP the client name or IP address. 68 */ 69 public ModifyClientEntry(String clientIP) { 70 71 this.clientIP = clientIP; 72 validOptions = supportedOptions; 73 74 } // constructor 75 76 /** 77 * Returns the option flag for this function. 78 * @returns the option flag for this function. 79 */ 80 public int getFunctionFlag() { 81 return (PntAdm.MODIFY_CLIENT_ENTRY); 82 } 83 84 /** 85 * Executes the "modify client" functionality. 86 * @return PntAdm.SUCCESS, PntAdm.ENOENT, PntAdm.WARNING, or 87 * PntAdm.CRITICAL 88 */ 89 public int execute() 90 throws IllegalArgumentException { 91 92 int returnCode = PntAdm.SUCCESS; 93 94 // Build up a DhcpClientRecord so that we can retrieve the current 95 // client record from the network table. 96 // 97 try { 98 DhcpClientRecord oldDhcpClientRecord = new DhcpClientRecord(); 99 oldDhcpClientRecord.setClientIP(clientIP); 100 101 // Create a Network object. 102 // 103 Network network = getNetMgr().getNetwork(networkName); 104 if (network == null) { 105 printErrMessage(getString("network_name_error")); 106 return (PntAdm.WARNING); 107 } 108 109 // Go and get the current client record from the network table. 110 // 111 oldDhcpClientRecord = 112 getNetMgr().getClient(oldDhcpClientRecord, network.toString(), 113 getDhcpDatastore()); 114 115 // Build up the new DhcpClientRecord from the original and the 116 // user specified options. 117 // 118 DhcpClientRecord newDhcpClientRecord = 119 (DhcpClientRecord)oldDhcpClientRecord.clone(); 120 121 String newClientIP = options.valueOf(PntAdm.NEW_IP); 122 if (newClientIP != null) { 123 newDhcpClientRecord.setClientIP(newClientIP); 124 } else { 125 newDhcpClientRecord.setClientIP(clientIP); 126 } 127 128 String clientId = options.valueOf(PntAdm.CLIENTID); 129 boolean convertClientId = options.isSet(PntAdm.CONVERT_CLIENTID); 130 if (convertClientId) { 131 if (clientId == null) { 132 String msg = getString("no_clientid_specified"); 133 throw new IllegalArgumentException(msg); 134 } 135 clientId = Util.asciiToHex(clientId); 136 } 137 if (clientId != null) { 138 newDhcpClientRecord.setClientId(clientId); 139 } 140 141 String flags = options.valueOf(PntAdm.FLAGS); 142 if (flags != null) { 143 newDhcpClientRecord.setFlags(flags); 144 } 145 146 String serverIP = options.valueOf(PntAdm.SERVER); 147 if (serverIP == null) { 148 serverIP = getSvcMgr().getServerName(); 149 } 150 newDhcpClientRecord.setServerIP(serverIP); 151 152 String expiration = options.valueOf(PntAdm.LEASE_EXPIRATION); 153 if (expiration != null) { 154 newDhcpClientRecord.setExpiration(shortFormat, expiration); 155 } 156 157 boolean verifyMacro = options.isSet(PntAdm.VERIFY_MACRO); 158 String macro = options.valueOf(PntAdm.MACRO_NAME); 159 if (verifyMacro) { 160 if (macro == null) { 161 String msg = getString("no_macro_specified"); 162 throw new IllegalArgumentException(msg); 163 } 164 165 // Create a Macro entry so that we can check to see if it 166 // exists in the dhcptab. 167 // 168 try { 169 Macro existingMacro = 170 getDhcptabMgr().getMacro(macro); 171 } 172 catch (BridgeException e) { 173 printErrMessage(getString("macro_not_found")); 174 return (PntAdm.WARNING); 175 } 176 } 177 if (macro != null) { 178 newDhcpClientRecord.setMacro(macro); 179 } 180 181 String comment = options.valueOf(PntAdm.COMMENT); 182 if (comment != null) { 183 newDhcpClientRecord.setComment(comment); 184 } 185 186 // Modify the client and adds host if necessary. 187 // 188 getNetMgr().modifyClient(oldDhcpClientRecord, newDhcpClientRecord, 189 network.toString(), getDhcpDatastore()); 190 191 } catch (NoEntryException e) { 192 printErrMessage(getMessage(e)); 193 returnCode = PntAdm.ENOENT; 194 } catch (Throwable e) { 195 printErrMessage(getMessage(e)); 196 returnCode = PntAdm.WARNING; 197 } 198 199 return (returnCode); 200 201 } // execute 202 203 } // ModifyClientEntry