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 2005 Sun Microsystems, Inc. All rights reserved. 25 * Use is subject to license terms. 26 * 27 * DoPrinterAdd class 28 * Worker class for adding local and remote printers. 29 */ 30 31 package com.sun.admin.pm.server; 32 33 import java.io.*; 34 import java.util.*; 35 36 public class DoPrinterAdd { 37 38 public static void main(String[] args) { 39 // 40 // Set attributes for testing 41 // 42 NameService ns = new NameService(); 43 44 String file_contents[] = new String[1]; 45 file_contents[0] = "any"; 46 String user_allow_list[] = new String[1]; 47 user_allow_list[0] = "allow1"; 48 String user_deny_list[] = new String[1]; 49 user_deny_list[0] = "deny1"; 50 51 Printer p = new Printer(ns); 52 p.setPrinterName("javatest"); 53 p.setPrinterType("PS"); 54 p.setPrintServer("zelkova"); 55 p.setComment("This is a comment"); 56 p.setDevice("/var/tmp/test"); 57 p.setNotify("none"); 58 p.setProtocol("bsd"); 59 p.setDestination(null); 60 p.setFileContents(file_contents); 61 p.setUserAllowList(user_allow_list); 62 p.setIsDefaultPrinter(false); 63 p.setBanner("never"); 64 p.setEnable(true); 65 p.setAccept(true); 66 p.setLocale(null); 67 68 try { 69 add(p, ns); 70 } 71 catch (Exception e) 72 { 73 System.out.println(e); 74 System.exit(1); 75 } 76 System.out.println("Commands:\n" + p.getCmdLog()); 77 System.out.println("Errors:\n" + p.getErrorLog()); 78 System.out.println("Warnings:\n" + p.getWarnLog()); 79 System.exit(0); 80 } 81 82 // 83 // Interface to Printer object. 84 // 85 public static void add( 86 Printer p, 87 NameService ns) throws Exception 88 { 89 Debug.message("SVR: DoPrinterAdd.add()"); 90 91 String device = p.getDevice(); 92 if (device == null) { 93 addRemote(p, ns); 94 } else { 95 addLocal(p, ns); 96 } 97 return; 98 } 99 100 // 101 // Do the work of adding a local printer. 102 // 103 private static void addLocal( 104 Printer p, 105 NameService ns) throws Exception 106 { 107 Debug.message("SVR: DoPrinterAdd.addLocal()"); 108 109 int exitvalue = 0; 110 String err = null; 111 String cmd = null; 112 SysCommand syscmd = null; 113 114 // Since it's local set extensions. 115 // Eventually the gui should do this. 116 p.setExtensions("Solaris"); 117 118 String printername = p.getPrinterName(); 119 String printertype = p.getPrinterType(); 120 String printserver = p.getPrintServer(); 121 String comment = p.getComment(); 122 String device = p.getDevice(); 123 String make = p.getMake(); 124 String model = p.getModel(); 125 String ppd = p.getPPD(); 126 String notify = p.getNotify(); 127 String protocol = p.getProtocol(); 128 String destination = p.getDestination(); 129 String[] file_contents = p.getFileContents(); 130 String[] user_allow_list = p.getUserAllowList(); 131 String[] user_deny_list = p.getUserDenyList(); 132 boolean default_printer = p.getIsDefaultPrinter(); 133 String banner = p.getBanner(); 134 boolean enable = p.getEnable(); 135 boolean accept = p.getAccept(); 136 137 String nameservice = ns.getNameService(); 138 String ppdfile = null; 139 140 // 141 // "uri" is a pseudo protocol and means that the device is 142 // specified in the destination. 143 // 144 if ((protocol != null) && (protocol.equals("uri"))) { 145 device = destination; 146 destination = null; 147 protocol = null; 148 } 149 150 if (ppd != null) { 151 ppdfile = new String( 152 DoPrinterUtil.getPPDFile(make, model, ppd)); 153 } 154 155 cmd = "/usr/sbin/lpadmin -p " + printername; 156 if (printserver != null) 157 cmd = cmd.concat(" -s " + printserver); 158 if (device != null) { 159 cmd = cmd.concat(" -v " + device); 160 161 if (device.indexOf("://") != -1) 162 cmd = cmd.concat(" -m uri"); 163 else { 164 if (destination != null) 165 cmd = cmd.concat(" -m netstandard"); 166 else 167 cmd = cmd.concat(" -m standard"); 168 if (ppd != null) 169 cmd = cmd.concat("_foomatic"); 170 } 171 } 172 if (printertype != null) 173 cmd = cmd.concat(" -T " + printertype); 174 if (notify != null) 175 cmd = cmd.concat(" -A " + notify); 176 if (ppdfile != null) { 177 cmd = cmd.concat(" -n " + ppdfile); 178 } 179 180 181 if (destination != null) 182 cmd = cmd.concat(" -o dest=" + destination); 183 if (protocol != null) 184 cmd = cmd.concat(" -o protocol=" + protocol); 185 if (banner != null) 186 cmd = cmd.concat(" -o banner=" + banner); 187 188 if ((file_contents != null) && (file_contents.length != 0)) { 189 String tmpstr = file_contents[0]; 190 for (int i = 1; i < file_contents.length; i++) { 191 tmpstr = tmpstr.concat("," + file_contents[i]); 192 } 193 cmd = cmd.concat(" -I " + tmpstr); 194 } else { 195 cmd = cmd.concat(" -I postscript"); 196 } 197 if ((user_allow_list != null) && (user_allow_list.length != 0)) { 198 String tmpstr = user_allow_list[0]; 199 for (int i = 1; i < user_allow_list.length; i++) { 200 tmpstr = tmpstr.concat("," + user_allow_list[i]); 201 } 202 cmd = cmd.concat(" -u allow:" + tmpstr); 203 } 204 205 p.setCmdLog(cmd); 206 syscmd = new SysCommand(); 207 syscmd.exec(cmd); 208 209 err = syscmd.getError(); 210 if (syscmd.getExitValue() != 0) { 211 syscmd = null; 212 p.setErrorLog(err); 213 throw new pmCmdFailedException(err); 214 } else { 215 p.setWarnLog(err); 216 } 217 syscmd = null; 218 219 // 220 // lpadmin won't take allow and deny lists together 221 // so do the deny seperately. 222 // 223 if ((user_deny_list != null) && (user_deny_list.length != 0)) { 224 String tmpstr = user_deny_list[0]; 225 for (int i = 1; i < user_deny_list.length; i++) { 226 tmpstr = tmpstr.concat("," + user_deny_list[i]); 227 } 228 cmd = "/usr/sbin/lpadmin -p " + printername + 229 " -u deny:" + tmpstr; 230 p.setCmdLog(cmd); 231 syscmd = new SysCommand(); 232 syscmd.exec(cmd); 233 err = syscmd.getError(); 234 if (err != null) { 235 p.setWarnLog(err); 236 } 237 syscmd = null; 238 } 239 240 if ((comment != null) && (!comment.equals(""))) { 241 // 242 // Have to use a command array here since 243 // exec(String) doesn't parse quoted strings. 244 // 245 String cmd_array[] = 246 { "/usr/sbin/lpadmin", "-p", printername, 247 "-D", comment }; 248 cmd = "/usr/sbin/lpadmin -p " + printername + " -D " + 249 "\"" + comment + "\""; 250 p.setCmdLog(cmd); 251 252 syscmd = new SysCommand(); 253 syscmd.exec(cmd_array); 254 err = syscmd.getError(); 255 if (err != null) { 256 p.setWarnLog(err); 257 } 258 syscmd = null; 259 } 260 261 // If this is the default printer set it. 262 // If it fails warn user. 263 if (default_printer) { 264 cmd = "/usr/sbin/lpadmin -d " + printername; 265 p.setCmdLog(cmd); 266 syscmd = new SysCommand(); 267 syscmd.exec(cmd); 268 err = syscmd.getError(); 269 if (err != null) { 270 p.setWarnLog(err); 271 } 272 syscmd = null; 273 } 274 275 // Check to see if we should enable it. 276 // If it fails warn user. 277 if (enable) { 278 cmd = "/usr/bin/enable " + printername; 279 p.setCmdLog(cmd); 280 syscmd = new SysCommand(); 281 syscmd.exec(cmd); 282 err = syscmd.getError(); 283 if (err != null) { 284 p.setWarnLog(err); 285 } 286 syscmd = null; 287 } 288 // Check to see if we should accept it. 289 // If it fails warn user. 290 if (accept) { 291 cmd = "/usr/sbin/accept " + printername; 292 p.setCmdLog(cmd); 293 syscmd = new SysCommand(); 294 syscmd.exec(cmd); 295 err = syscmd.getError(); 296 if (err != null) { 297 p.setWarnLog(err); 298 } 299 syscmd = null; 300 } 301 302 doFilters(p); 303 304 // 305 // Take care of name service now. 306 // 307 if (!nameservice.equals("system")) { 308 try { 309 DoPrinterNS.set("add", p, ns); 310 } 311 catch (Exception e) { 312 p.clearLogs(); 313 NameService localns = new NameService(); 314 // 315 // Back out the local printer. 316 // 317 try { 318 DoPrinterDelete.delete(p, localns); 319 } 320 catch (Exception e2) { 321 Debug.message("SVR:" + e2.getMessage()); 322 } 323 p.clearLogs(); 324 throw (e); 325 } 326 } 327 return; 328 } 329 330 // 331 // Do the work of adding a remote printer. 332 // 333 private static void addRemote( 334 Printer p, 335 NameService ns) throws Exception 336 { 337 Debug.message("SVR: DoPrinterAdd.addRemote()"); 338 339 int exitvalue = 0; 340 String err = null; 341 String cmd = ""; 342 String cmd_array[] = new String[7]; 343 SysCommand syscmd = null; 344 345 String printername = p.getPrinterName(); 346 String printserver = p.getPrintServer(); 347 String comment = p.getComment(); 348 boolean default_printer = p.getIsDefaultPrinter(); 349 String nameservice = ns.getNameService(); 350 351 boolean isnismaster = false; 352 if (nameservice.equals("nis")) { 353 // 354 // Find out if we are the nis master 355 // 356 String nshost = ns.getNameServiceHost(); 357 Host h = new Host(); 358 String lh = h.getLocalHostName(); 359 if (lh.equals(nshost)) 360 isnismaster = true; 361 h = null; 362 } 363 364 // 365 // If the name service is not system and we are 366 // not the nis master then do the name service 367 // update and return. 368 // 369 if ((!nameservice.equals("system")) && (!isnismaster)) { 370 DoPrinterNS.set("add", p, ns); 371 return; 372 } 373 374 cmd_array[0] = "/usr/sbin/lpadmin"; 375 cmd_array[1] = "-p"; 376 cmd_array[2] = printername; 377 cmd_array[3] = "-s"; 378 cmd_array[4] = printserver; 379 380 if ((comment != null) && (!comment.equals(""))) { 381 cmd_array[5] = "-D"; 382 cmd_array[6] = comment; 383 } 384 385 // 386 // Fix up cmd so we can log it. 387 // 388 for (int i = 0; i < cmd_array.length; i++) { 389 if (cmd_array[i] == null) 390 continue; 391 if (i == 6) { 392 cmd = cmd.concat("\"" + comment + "\""); 393 continue; 394 } 395 cmd = cmd.concat(cmd_array[i] + " "); 396 } 397 398 p.setCmdLog(cmd); 399 syscmd = new SysCommand(); 400 syscmd.exec(cmd_array); 401 err = syscmd.getError(); 402 if (syscmd.getExitValue() != 0) { 403 p.setErrorLog(err); 404 syscmd = null; 405 throw new pmCmdFailedException(err); 406 } 407 if (err != null) { 408 p.setWarnLog(err); 409 } 410 syscmd = null; 411 412 // If this is the default printer set it. 413 // If it fails warn user. 414 if (default_printer) { 415 cmd = "/usr/sbin/lpadmin -d " + printername; 416 p.setCmdLog(cmd); 417 syscmd = new SysCommand(); 418 syscmd.exec(cmd); 419 err = syscmd.getError(); 420 if (err != null) { 421 p.setWarnLog(err); 422 } 423 syscmd = null; 424 } 425 426 // 427 // If it's nis and we are here then we are the nis 428 // master. This call will do the make for us. 429 // 430 if (nameservice.equals("nis")) { 431 try { 432 DoPrinterNS.set("add", p, ns); 433 } 434 catch (Exception e) { 435 p.clearLogs(); 436 try { 437 // 438 // Back out the local printer. 439 // 440 DoPrinterDelete.delete(p, ns); 441 } 442 catch (Exception e2) 443 { 444 Debug.message("SVR:" + e2.getMessage()); 445 } 446 p.clearLogs(); 447 throw e; 448 } 449 } 450 return; 451 } 452 453 454 // 455 // Configure filters 456 // Look in /etc/lp/fd and configure each filter if it hasn't 457 // already been configured. We'll add warning messages if 458 // there are problems but don't consider anything here fatal. 459 // 460 private static void doFilters(Printer p) throws Exception 461 { 462 Debug.message("SVR: DoPrinterAdd.doFilters()"); 463 464 int i = 0; 465 int j = 0; 466 String o = null; 467 String err = null; 468 String cmd = null; 469 String psfilters[] = null; 470 SysCommand syscmd = null; 471 472 // 473 // Get list of potential filters 474 // 475 cmd = "/usr/bin/ls /etc/lp/fd"; 476 syscmd = new SysCommand(); 477 syscmd.exec(cmd); 478 if (syscmd.getExitValue() != 0) { 479 syscmd = null; 480 return; 481 } 482 o = syscmd.getOutput(); 483 syscmd = null; 484 if (o == null) { 485 return; 486 } 487 488 StringTokenizer st = new StringTokenizer(o); 489 if (st.countTokens() == 0) { 490 return; 491 } 492 psfilters = new String[st.countTokens()]; 493 for (i = 0; st.hasMoreTokens(); i++) { 494 psfilters[i] = st.nextToken(); 495 } 496 // 497 // Remove .fd suffix and empty slots that aren't filters. 498 // 499 for (i = 0; i < psfilters.length; i++) { 500 if (psfilters[i].endsWith(".fd")) { 501 j = psfilters[i].indexOf(".fd"); 502 psfilters[i] = psfilters[i].substring(0, j); 503 } else { 504 psfilters[i] = ""; 505 } 506 } 507 508 // Get list of currently configured filters 509 cmd = "/usr/sbin/lpfilter -l -f all"; 510 syscmd = new SysCommand(); 511 syscmd.exec(cmd); 512 513 o = null; 514 if (syscmd.getExitValue() != 0) { 515 err = syscmd.getError(); 516 if (err != null) { 517 p.setWarnLog(err); 518 } 519 syscmd = null; 520 return; 521 } else { 522 o = syscmd.getOutput(); 523 } 524 525 for (i = 0; i < psfilters.length; i++) { 526 if (psfilters[i].equals("")) 527 continue; 528 529 // If we have filters see if this one is 530 // already configured. 531 if (o != null) { 532 if (o.indexOf("\"" + psfilters[i] + "\"") > -1) 533 continue; 534 } 535 536 // Add the filter 537 cmd = "/usr/sbin/lpfilter -f " + psfilters[i] + 538 " -F /etc/lp/fd/" + psfilters[i] + ".fd"; 539 p.setCmdLog(cmd); 540 syscmd = new SysCommand(); 541 syscmd.exec(cmd); 542 543 if (syscmd.getExitValue() != 0) { 544 err = syscmd.getError(); 545 if (err != null) { 546 p.setWarnLog(err); 547 } 548 } 549 syscmd = null; 550 } 551 } 552 } 553