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 (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 * 25 * Host class 26 * Methods associated with a host. 27 */ 28 29 package com.sun.admin.pm.server; 30 31 import java.io.*; 32 33 public class Host 34 { main(String[] args)35 public static void main(String[] args) 36 { 37 try { 38 System.out.println(getLocalHostName()); 39 System.out.println(getDomainName()); 40 System.out.println(getNisHost("master")); 41 } 42 catch (Exception e) { 43 System.out.println(e); 44 } 45 System.exit(0); 46 } 47 48 // 49 // Get the local hostname 50 // Return an empty string if we don't find one. 51 // getLocalHostName()52 public synchronized static String getLocalHostName() 53 throws Exception 54 { 55 Debug.message("SVR: Host.getLocalHostName()"); 56 57 String cmd = "/usr/bin/hostname"; 58 SysCommand syscmd = new SysCommand(); 59 syscmd.exec(cmd); 60 61 if (syscmd.getExitValue() != 0) { 62 String err = syscmd.getError(); 63 syscmd = null; 64 throw new pmCmdFailedException(err); 65 } 66 String o = syscmd.getOutput(); 67 syscmd = null; 68 69 if (o == null) 70 return (new String("")); 71 return (new String(o)); 72 } 73 74 // 75 // Get the domainname 76 // Return an empty string if we don't find one. 77 // getDomainName()78 public synchronized static String getDomainName() 79 throws Exception 80 { 81 Debug.message("SVR: Host.getDomainName()"); 82 83 String cmd = "/usr/bin/domainname"; 84 SysCommand syscmd = new SysCommand(); 85 syscmd.exec(cmd); 86 if (syscmd.getExitValue() != 0) { 87 String err = syscmd.getError(); 88 syscmd = null; 89 throw new pmCmdFailedException(err); 90 } 91 92 String o = syscmd.getOutput(); 93 syscmd = null; 94 95 if (o == null) 96 return (new String("")); 97 return (new String(o)); 98 } 99 pingHost(String host)100 public synchronized static void pingHost(String host) 101 throws Exception 102 { 103 int exitvalue; 104 105 Debug.message("SVR: Host.pingHost()"); 106 107 SysCommand syscmd = new SysCommand(); 108 syscmd.exec("/usr/sbin/ping " + host); 109 exitvalue = syscmd.getExitValue(); 110 syscmd = null; 111 112 if (exitvalue != 0) { 113 String err = syscmd.getError(); 114 throw new pmHostNotPingableException(err); 115 } 116 } 117 getNisMaster()118 public synchronized static String getNisMaster() 119 throws Exception 120 { 121 return (getNisHost("master")); 122 } 123 124 // 125 // Look for the nis server. 126 // If we are looking for the master server first try 127 // the printers.conf.byname map. If that fails 128 // look for passwd. 129 // getNisHost(String type)130 public synchronized static String getNisHost(String type) 131 throws Exception 132 { 133 Debug.message("SVR: Host.getNisHost() " + type); 134 135 SysCommand syscmd = null; 136 String cmd = null; 137 int exitvalue = 0; 138 139 if (type.equals("master")) { 140 cmd = "/usr/bin/ypwhich -m printers.conf.byname"; 141 } else { 142 cmd = "/usr/bin/ypwhich"; 143 } 144 syscmd = new SysCommand(); 145 syscmd.exec(cmd); 146 exitvalue = syscmd.getExitValue(); 147 if ((exitvalue != 0) && (type.equals("master"))) { 148 Debug.message("SVR: printers.conf NIS host not found."); 149 Debug.message("SVR: Looking for NIS passwd host."); 150 cmd = "/usr/bin/ypwhich -m passwd"; 151 152 syscmd = new SysCommand(); 153 syscmd.exec(cmd); 154 exitvalue = syscmd.getExitValue(); 155 } 156 if (exitvalue != 0) { 157 Debug.error("SVR: NIS server could not be found"); 158 String err = syscmd.getError(); 159 syscmd = null; 160 throw new pmNSNotConfiguredException(err); 161 } 162 163 String o = syscmd.getOutput(); 164 syscmd = null; 165 166 if (o == null) { 167 throw new pmCmdFailedException(syscmd.getError()); 168 } 169 o = o.trim(); 170 return (new String(o)); 171 } 172 173 /* 174 * Return the name of the first server listed by ldapclient 175 */ getLDAPMaster()176 public synchronized static String getLDAPMaster() 177 throws Exception 178 { 179 SysCommand syscmd = null; 180 String cmd = null; 181 int exitvalue = 0; 182 183 /* ldapclient will hang if we are not root. */ 184 if (!DoPrinterNS.isRoot()) { 185 Debug.error("SVR: Not root. Can't determine LDAP master."); 186 return null; 187 } 188 189 cmd = "/usr/sbin/ldapclient list"; 190 syscmd = new SysCommand(); 191 syscmd.exec(cmd); 192 exitvalue = syscmd.getExitValue(); 193 194 if (exitvalue != 0) { 195 Debug.error("SVR: ldapclient failed."); 196 Debug.error("SVR: " + syscmd.getError()); 197 syscmd = null; 198 return null; 199 } 200 String o = syscmd.getOutput(); 201 syscmd = null; 202 203 String master = DoPrinterView.getToken(o + "\n", "NS_LDAP_SERVERS="); 204 if (master == null) { 205 Debug.error("SVR: ldapclient did not return NS_LDAP_SERVERS."); 206 syscmd = null; 207 return null; 208 } 209 210 /* Extract the first address from the NS_LDAP_SERVERS list */ 211 212 for (int i = 0; i < master.length(); i++) { 213 if ((master.charAt(i) == ',') || 214 (master.charAt(i) == ' ') || 215 (master.charAt(i) == '\t')) { 216 master = master.substring(0, i); 217 break; 218 } 219 } 220 master = master.trim(); 221 222 return (new String(master)); 223 } 224 225 /* 226 * Get a default admin DN. 227 */ getDefaultAdminDN()228 public synchronized static String getDefaultAdminDN() 229 throws Exception 230 { 231 SysCommand syscmd = null; 232 String cmd = null; 233 int exitvalue = 0; 234 235 try { 236 String master = getLDAPMaster(); 237 cmd = "/usr/bin/ldapsearch -h " + master + 238 " -b o=NetScapeRoot o=NetscapeRoot"; 239 syscmd = new SysCommand(); 240 syscmd.exec(cmd); 241 exitvalue = syscmd.getExitValue(); 242 if (exitvalue == 0) { 243 String on = syscmd.getOutput(); 244 syscmd = null; 245 if (on != null) { 246 if (on.indexOf("NetscapeRoot") != -1) { 247 return ("cn=Directory Manager"); 248 } 249 } 250 } 251 syscmd = null; 252 } 253 catch (Exception e) { 254 Debug.message("SVR: ldapsearch for NSDS failed. Continuing"); 255 } 256 257 cmd = "/usr/bin/ldaplist -d printers"; 258 syscmd = new SysCommand(); 259 syscmd.exec(cmd); 260 exitvalue = syscmd.getExitValue(); 261 262 if (exitvalue != 0) { 263 Debug.error("SVR: ldaplist printers failed."); 264 Debug.error("SVR: " + syscmd.getError()); 265 syscmd = null; 266 return null; 267 } 268 String o = syscmd.getOutput(); 269 syscmd = null; 270 271 if (o == null) { 272 return null; 273 } 274 275 String dn = DoPrinterView.getToken(o + "\n", "ou=printers,"); 276 if (dn == null) { 277 return null; 278 } 279 dn = "cn=admin," + dn; 280 dn = dn.trim(); 281 282 return (new String(dn)); 283 } 284 285 // 286 // Check to see if a name service is configured 287 // isNSConfigured(String ns)288 public synchronized static void isNSConfigured(String ns) 289 throws Exception 290 { 291 Debug.message("SVR: Host.isNSConfigured() " + ns); 292 293 int exitvalue; 294 String cmd = null; 295 String err = null; 296 SysCommand syscmd = null; 297 298 if (ns.equals("system")) { 299 return; 300 } else if (ns.equals("nis")) { 301 cmd = "/usr/bin/ypwhich"; 302 syscmd = new SysCommand(); 303 syscmd.exec(cmd); 304 exitvalue = syscmd.getExitValue(); 305 err = syscmd.getError(); 306 syscmd = null; 307 308 if (exitvalue != 0) { 309 throw new pmNSNotConfiguredException(err); 310 } 311 312 cmd = "/usr/bin/ypcat cred"; 313 syscmd = new SysCommand(); 314 syscmd.exec(cmd); 315 exitvalue = syscmd.getExitValue(); 316 syscmd = null; 317 if (exitvalue == 0) { 318 Debug.warning( 319 "SVR: Unable to update this configuration."); 320 throw new pmNSNotConfiguredException(); 321 } 322 } else if (ns.equals("ldap")) { 323 /* 324 * Check if the ldap-client is configured by first checking 325 * if the config file exists and then invoking ldaplist 326 * Note: we need to check if the config file exists before 327 * invoking ldaplist so that we don't get its error message 328 */ 329 330 File ldapConfig = new File("/var/ldap/ldap_client_file"); 331 if (ldapConfig.isFile()) { 332 // Config file exists 333 334 cmd = "/usr/bin/ldaplist -d printers"; 335 syscmd = new SysCommand(); 336 syscmd.exec(cmd); 337 exitvalue = syscmd.getExitValue(); 338 syscmd = null; 339 340 if (exitvalue != 0) { 341 throw new pmNSNotConfiguredException(); 342 } 343 } else { 344 throw new pmNSNotConfiguredException(); 345 } 346 } else { 347 throw new pmInternalErrorException( 348 "Unkown name service " + ns); 349 } 350 } 351 } 352