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