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