xref: /titanic_51/usr/src/cmd/print/printmgr/com/sun/admin/pm/server/DoPrinterView.java (revision 1c3a0e9dd9f5bb14378d99eee9ed261ce24fc4d3)
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  *
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * DoPrinterView class
27  * Worker class for gathering printer information.
28  */
29 
30 package com.sun.admin.pm.server;
31 
32 import java.io.*;
33 import java.util.*;
34 
35 import com.sun.admin.pm.client.pmNeedPPDCacheException;
36 import com.sun.admin.pm.client.pmCacheMissingPPDException;
37 // import com.sun.admin.pm.client.pmGuiException;
38 
39 public class DoPrinterView {
40 
41     public static void main(String[] args) {
42 	Debug.setDebugLevel(Debug.ALL);
43 
44 	Printer p = new Printer();
45 	p.setPrinterName("petite");
46 	NameService ns = new NameService();
47 
48 	try {
49 		view(p, ns);
50 	}
51 	catch (Exception e)
52 	{
53 		System.out.println(e);
54 		System.exit(1);
55 	}
56 	PrinterDebug.printObj(p);
57 
58 	System.out.println("Commands:\n" + p.getCmdLog());
59 	System.out.println("Errors:\n" + p.getErrorLog());
60 	System.out.println("Warnings:\n" + p.getWarnLog());
61 	System.exit(0);
62     }
63 
64     //
65     // Interface to Printer object.
66     //
67     public static void view(
68 	Printer p,
69 	NameService ns) throws Exception
70     {
71 	boolean islocal =
72 		DoPrinterUtil.isLocal(p.getPrinterName());
73 	if (islocal) {
74 		viewLocal(p, ns);
75 	} else {
76 		viewRemote(p, ns);
77 	}
78 
79 	return;
80     }
81 
82     //
83     // Do the work getting Remote printer attributes.
84     //
85     private static void viewRemote(
86 	Printer p,
87 	NameService ns) throws Exception
88     {
89 	Debug.message("SVR: DoPrinterView.viewRemote()");
90 
91 	int exitvalue = 0;
92 	int i, j;
93 	String o = null;
94 	String printername = p.getPrinterName();
95 	String printserver = null;
96 	String comment = null;
97 	String extensions = null;
98 	boolean default_printer = false;
99 
100 	String nsarg = ns.getNameService();
101 
102 	String err = null;
103 
104 	SysCommand syscmd = new SysCommand();
105 	syscmd.exec("/usr/bin/lpget -n " + nsarg + " " +
106 		printername, "LC_ALL=C");
107 
108 	if (syscmd.getExitValue() != 0) {
109 		err = syscmd.getError();
110 		p.setErrorLog(err);
111 		// Add stdout since thats where lpget sends errors.
112 		err = syscmd.getOutput();
113 		p.setErrorLog(err);
114 		syscmd = null;
115 		throw new pmCmdFailedException(err);
116 	}
117 	o = syscmd.getOutput();
118 	syscmd = null;
119 
120 	if (o == null) {
121 		throw new pmCmdFailedException(err);
122 	}
123 	// For easier parsing.
124 	o = o.concat("\n");
125 
126 	i = o.indexOf("bsdaddr=");
127 	if (i == -1) {
128 		Debug.message("SVR: Can't parse bsdaddr for " + printername);
129 		throw new pmException();
130 	}
131 	i = i + 8;
132 	j = o.indexOf(",", i);
133 	if (j == -1) {
134 		Debug.message("SVR: Can't parse bsdaddr for " + printername);
135 		throw new pmException();
136 	}
137 	printserver = o.substring(i, j);
138 
139 	i = o.indexOf(",Solaris");
140 	if (i != -1) {
141 		extensions = "Solaris";
142 	}
143 
144 	i = o.indexOf("description=");
145 	if (i != -1) {
146 		i = i + 12;
147 		j = o.indexOf("\n", i);
148 		if (j != -1) {
149 			comment = o.substring(i, j);
150 		}
151 	}
152 
153 	String def = DoPrinterUtil.getDefault(nsarg);
154 	if ((def != null) && (def.equals(printername))) {
155 		default_printer = true;
156 	}
157 	p.setPrintServer(printserver);
158 	p.setExtensions(extensions);
159 	p.setComment(comment);
160 	p.setIsDefaultPrinter(default_printer);
161 	return;
162     }
163 
164     //
165     // Do the work getting printer attributes.
166     //
167     private static void viewLocal(
168 	Printer p, NameService ns) throws Exception
169     {
170 	Debug.message("SVR: DoPrinterView.viewLocal()");
171 
172 	int i = -1;
173 	int exitvalue = 0;
174 	String str = null;
175 	String o = null;
176 
177 	String printername = p.getPrinterName();
178 	String printertype = null;
179 	String printserver = null;
180 	String comment = null;
181 	String device = null;
182 	String make = null;
183 	String model = null;
184 	String ppd = null;
185 	String notify = null;
186 	String protocol = null;
187 	String destination = null;
188 	String extensions = "Solaris";
189 	String[] file_contents = null;
190 	String[] user_allow_list = null;
191 	String[] user_deny_list = null;
192 	boolean default_printer = false;
193 	String banner = null;
194 	boolean enable = false;
195 	boolean accept = false;
196 
197 	String ppdfile = null;
198 
199 	String def = DoPrinterUtil.getDefault("system");
200 	if ((def != null) && (def.equals(printername))) {
201 		default_printer = true;
202 	}
203 
204 	//
205 	// Parse lpstat output
206 	//
207 	SysCommand syscmd = new SysCommand();
208 	syscmd.exec("/usr/bin/lpstat -L -l -a " +
209 		printername + " -p " + printername, "LC_ALL=C");
210 
211 	if (syscmd.getExitValue() != 0) {
212 		String err = syscmd.getError();
213 		p.setErrorLog(err);
214 		syscmd = null;
215 		throw new pmCmdFailedException(err);
216 	}
217 
218 	o = syscmd.getOutput();
219 	syscmd = null;
220 
221 	// Append a newline to make parsing easier.
222 	o = o.concat("\n");
223 
224 	comment = getToken(o, "\tDescription:");
225 	if (comment == null || comment.equals("")) {
226 		comment = null;
227 	}
228 
229 	// Get the PPD path/filename from lpstat
230 	// Get the make/model/ppd nickname using ppd-filename
231 	ppdfile = getToken(o, "\tPPD:");
232 	if ((ppdfile == null) || (ppdfile.equals("none")) ||
233 					(ppdfile.equals(""))) {
234 		ppdfile = null;
235 	} else {
236 	// Set the make/model/ppd
237 
238 	    if (!pmMisc.isppdCachefile()) {
239 		throw new pmNeedPPDCacheException("ppdcache missing");
240 	    }
241 		String ret[];
242 		ret = new String[3];
243 		ret = DoPrinterUtil.getMakeModelNick(ppdfile);
244 
245 		if ((ret != null) && (!ret.equals(""))) {
246 			make = ret[0];
247 			model = ret[1];
248 			ppd = ret[2];
249 		} else {
250 			throw new pmCacheMissingPPDException(
251 					"PPD file not in cache");
252 		}
253 	}
254 
255 	int j = -1;
256 	printserver = p.getPrintServer();
257 
258 	printertype = getToken(o, "Printer types:");
259 	i = o.indexOf("enabled since");
260 	if (i != -1) {
261 		enable = true;
262 	}
263 	i = o.indexOf("not accepting requests");
264 	if (i == -1) {
265 		accept = true;
266 	}
267 
268 	i = o.indexOf("Banner not required");
269 	if (i != -1) {
270 		banner = "optional";
271 	}
272 	i = o.indexOf("Banner required");
273 	if (i != -1) {
274 		banner = "always";
275 	}
276 	i = o.indexOf("Banner page never printed");
277 	if (i != -1) {
278 		banner = "never";
279 	}
280 
281 
282 
283 	// If we have Options then look for destination and protocol.
284 	protocol = "bsd";
285 	str = "Options:";
286 	i = o.indexOf(str);
287 	if (i != -1) {
288 		// Set str to the substring containing only the options line.
289 		j = o.indexOf("\n", i);
290 		str = o.substring(i, j);
291 
292 		// Append a comma to make parsing easier.
293 		str = str.concat(",");
294 		i = str.indexOf("dest=");
295 		if (i != -1) {
296 			i += 5;
297 			j = str.indexOf(",", i);
298 			destination = str.substring(i, j);
299 			destination = destination.trim();
300 		}
301 		i = str.indexOf("protocol=");
302 		if (i != -1) {
303 			i += 9;
304 			j = str.indexOf(",", i);
305 			protocol = str.substring(i, j);
306 			protocol = protocol.trim();
307 		}
308 	}
309 
310 	StringTokenizer st;
311 
312 	// Build array of content types.
313 	str = getToken(o, "Content types:");
314 	if (str != null) {
315 		str = str.replace(',', ' ');
316 		st = new StringTokenizer(str);
317 		if (st.countTokens() != 0) {
318 			file_contents = new String[st.countTokens()];
319 			for (i = 0; st.hasMoreTokens(); i++) {
320 				file_contents[i] = st.nextToken();
321 			}
322 		}
323 	}
324 
325 	//
326 	// User allow list.
327 	//
328 	str = "Users allowed:\n";
329 	i = o.indexOf(str);
330 	if (i != -1) {
331 		i += str.length();
332 		// Grab the substring containing only users.
333 		j = o.indexOf("\tForms");
334 		if (j != -1) {
335 			str = o.substring(i, j);
336 			st = new StringTokenizer(str);
337 			if (st.countTokens() != 0) {
338 				user_allow_list = new String[st.countTokens()];
339 				for (i = 0; st.hasMoreTokens(); i++) {
340 					user_allow_list[i] = st.nextToken();
341 				}
342 			}
343 		}
344 	}
345 	if (user_allow_list == null) {
346 	} else if (user_allow_list[0].equals("(all)")) {
347 		user_allow_list[0] = "all";
348 	} else if (user_allow_list[0].equals("(none)")) {
349 		user_allow_list[0] = "none";
350 	}
351 	//
352 	// User deny list
353 	//
354 	syscmd = new SysCommand();
355 	String cmd = "/bin/cat /etc/lp/printers/" + printername + "/users.deny";
356 	syscmd.exec(cmd);
357 	if (syscmd.getExitValue() == 0) {
358 		str = syscmd.getOutput();
359 		if ((str != null) && (str.length() != 0)) {
360 			st = new StringTokenizer(str);
361 			if (st.countTokens() != 0) {
362 				user_deny_list = new String[st.countTokens()];
363 				for (i = 0; st.hasMoreTokens(); i++) {
364 					user_deny_list[i] = st.nextToken();
365 				}
366 			}
367 		}
368 	}
369 	syscmd = null;
370 
371 	//
372 	// Get fault action
373 	//
374 	str = getToken(o, "On fault:");
375 	if (str != null) {
376 		if (!str.equals("")) {
377 			if (str.indexOf("write") != -1) {
378 				notify = "write";
379 			} else if (str.indexOf("mail") != -1) {
380 				notify = "mail";
381 			} else if (str.indexOf("no alert") != -1) {
382 				notify = "none";
383 			} else if (str.indexOf("alert with") != -1) {
384 				i = str.indexOf("\"");
385 				if (i != -1) {
386 					j = str.lastIndexOf("\"");
387 					if (j > i) {
388 						notify = str.substring(++i, j);
389 					}
390 				}
391 			} else if (str.indexOf(" quiet ") != -1) {
392 				notify = "quiet";
393 			} else {
394 				notify = "unknown";
395 			}
396 		}
397 	}
398 	syscmd = null;
399 	//
400 	// Get the printers device
401 	//
402 	syscmd = new SysCommand();
403 	syscmd.exec("/usr/bin/lpstat -L -v " + printername,
404 	    "LC_ALL=C");
405 
406 	o = syscmd.getOutput();
407 	if (o != null) {
408 		o = o.concat("\n");
409 		device = getToken(o, ":");
410 	}
411 
412 	Debug.message("SVR: DEVICE (" + device + ")");
413 	//
414 	// If the device is in URI form (scheme:// ...), set the protocol
415 	// for the "network attached" modify screen.
416 	//
417 	if (device != null && device.indexOf("://") != -1) {
418 		protocol = "uri";
419 		destination = device;
420 		device = null;
421 	}
422 
423 	syscmd = null;
424 
425 	p.setPrinterType(printertype);
426 	p.setPrintServer(printserver);
427 	p.setFileContents(file_contents);
428 	p.setComment(comment);
429 	p.setDevice(device);
430 	p.setMake(make);
431 	p.setModel(model);
432 	p.setPPD(ppd);
433 	p.setPPDFile(ppdfile);
434 	p.setNotify(notify);
435 	p.setProtocol(protocol);
436 	p.setDestination(destination);
437 	p.setExtensions(extensions);
438 	p.setIsDefaultPrinter(default_printer);
439 	p.setBanner(banner);
440 	p.setEnable(enable);
441 	p.setAccept(accept);
442 	p.setUserAllowList(user_allow_list);
443 	p.setUserDenyList(user_deny_list);
444 
445 	if (ns.getNameService().equals("system"))
446 		return;
447 	Debug.message(
448 	    "SVR: Overlaying name service attributes on local printer");
449 	try {
450 		viewRemote(p, ns);
451 	}
452 	catch (Exception e)
453 	{
454 		Debug.warning(
455 		    "SVR: Overlay of name service attributes failed.");
456 		Debug.warning("SVR: " + e.getMessage());
457 	}
458 	return;
459     }
460 
461     //
462     // Return substring starting at sub + 1 and ending with
463     // a newline.
464     //
465     public static String getToken(String str, String sub)
466     {
467 	int i = -1;
468 	int j = -1;
469 	String result = null;
470 
471 	i = str.indexOf(sub);
472 	if (i != -1) {
473 		if (str.charAt(i + sub.length()) == '\n') {
474 			return (null);
475 		}
476 		i = i + sub.length();
477 		j = str.indexOf("\n", i);
478 		if (j != -1) {
479 			result = str.substring(i, j);
480 			result = result.trim();
481 		}
482 	}
483 	return (result);
484     }
485 }
486