xref: /titanic_44/usr/src/cmd/smbsrv/smbd/smbd_spool.c (revision 430b4c467020edf2445feb0c21db01c88b86243a)
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 (c) 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
24  */
25 
26 /*
27  * CUPS support for the SMB and SPOOLSS print services.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <cups/cups.h>
33 #include <strings.h>
34 #include <syslog.h>
35 #include <signal.h>
36 #include <pthread.h>
37 #include <synch.h>
38 #include <dlfcn.h>
39 #include <errno.h>
40 #include <smbsrv/smb.h>
41 #include <smbsrv/smb_share.h>
42 #include "smbd.h"
43 
44 #define	SMB_SPOOL_WAIT			2
45 #define	SMBD_PJOBLEN			256
46 #define	SMBD_PRINTER			"Postscript"
47 #define	SMBD_FN_PREFIX			"cifsprintjob-"
48 #define	SMBD_CUPS_SPOOL_DIR		"//var//spool//cups"
49 #define	SMBD_CUPS_DOCNAME		"generic_doc"
50 
51 typedef struct smbd_printjob {
52 	pid_t		pj_pid;
53 	int		pj_sysjob;
54 	int		pj_fd;
55 	time_t		pj_start_time;
56 	int		pj_status;
57 	size_t		pj_size;
58 	int		pj_page_count;
59 	boolean_t	pj_isspooled;
60 	boolean_t	pj_jobnum;
61 	char		pj_filename[SMBD_PJOBLEN];
62 	char		pj_jobname[SMBD_PJOBLEN];
63 	char		pj_username[SMBD_PJOBLEN];
64 	char		pj_queuename[SMBD_PJOBLEN];
65 } smbd_printjob_t;
66 
67 typedef struct smb_cups_ops {
68 	void		*cups_hdl;
69 	cups_lang_t	*(*cupsLangDefault)();
70 	const char	*(*cupsLangEncoding)(cups_lang_t *);
71 	void		(*cupsLangFree)(cups_lang_t *);
72 	ipp_status_t	(*cupsLastError)();
73 	int		(*cupsGetDests)(cups_dest_t **);
74 	void		(*cupsFreeDests)(int, cups_dest_t *);
75 	ipp_t		*(*cupsDoFileRequest)(http_t *, ipp_t *,
76 	    const char *, const char *);
77 	ipp_t		*(*ippNew)();
78 	void		(*ippDelete)();
79 	char		*(*ippErrorString)();
80 	ipp_attribute_t	*(*ippAddString)();
81 	void		(*httpClose)(http_t *);
82 	http_t		*(*httpConnect)(const char *, int);
83 } smb_cups_ops_t;
84 
85 static uint32_t smbd_cups_jobnum = 1;
86 static smb_cups_ops_t smb_cups;
87 static mutex_t smbd_cups_mutex;
88 
89 static void *smbd_spool_monitor(void *);
90 static smb_cups_ops_t *smbd_cups_ops(void);
91 static void smbd_print_share_comment(smb_share_t *, cups_dest_t *);
92 static void *smbd_share_printers(void *);
93 static void smbd_spool_copyfile(smb_inaddr_t *, char *, char *, char *);
94 
95 extern smbd_t smbd;
96 
97 /*
98  * Initialize the spool thread.
99  * Returns 0 on success, an error number if thread creation fails.
100  */
101 void
102 smbd_spool_init(void)
103 {
104 	pthread_attr_t	attr;
105 	int		rc;
106 
107 	(void) pthread_attr_init(&attr);
108 	(void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
109 	rc = pthread_create(&smbd.s_spool_tid, &attr, smbd_spool_monitor, NULL);
110 	(void) pthread_attr_destroy(&attr);
111 
112 	if (rc != 0)
113 		smb_log(smbd.s_loghd, LOG_NOTICE,
114 		    "failed to start print monitor: %s", strerror(errno));
115 }
116 
117 /*
118  * A single pthread_kill should be sufficient but we include
119  * a couple of retries to avoid implementation idiosyncrasies
120  * around signal delivery.
121  */
122 void
123 smbd_spool_fini(void)
124 {
125 	int	i;
126 
127 	if (pthread_self() == smbd.s_spool_tid)
128 		return;
129 
130 	for (i = 0; i < 3 && smbd.s_spool_tid != 0; ++i) {
131 		if (pthread_kill(smbd.s_spool_tid, SIGTERM) == ESRCH)
132 			break;
133 
134 		(void) sleep(1);
135 	}
136 }
137 
138 /*
139  * This thread blocks waiting for close print file in the kernel.
140  * It then uses the data returned from the ioctl to copy the spool file
141  * into the cups spooler.
142  *
143  * This mechanism is really only used by Windows Vista and Windows 7.
144  * Other versions of Windows create a zero size file, which is removed
145  * by smbd_spool_copyfile.
146  */
147 /*ARGSUSED*/
148 static void *
149 smbd_spool_monitor(void *arg)
150 {
151 	uint32_t	spool_num;
152 	char		username[MAXNAMELEN];
153 	char		path[MAXPATHLEN];
154 	smb_inaddr_t	ipaddr;
155 	int		error_retry_cnt = 5;
156 
157 	smbd_online_wait("smbd_spool_monitor");
158 
159 	spoolss_register_copyfile(smbd_spool_copyfile);
160 
161 	while (!smbd.s_shutting_down && (error_retry_cnt > 0)) {
162 		errno = 0;
163 
164 		if (smb_kmod_get_spool_doc(&spool_num, username,
165 		    path, &ipaddr) == 0) {
166 			smbd_spool_copyfile(&ipaddr,
167 			    username, path, SMBD_CUPS_DOCNAME);
168 			error_retry_cnt = 5;
169 		} else {
170 			if (errno == ECANCELED)
171 				break;
172 			if ((errno != EINTR) && (errno != EAGAIN))
173 				error_retry_cnt--;
174 			(void) sleep(SMB_SPOOL_WAIT);
175 		}
176 	}
177 
178 	spoolss_register_copyfile(NULL);
179 	smbd.s_spool_tid = 0;
180 	return (NULL);
181 }
182 
183 /*
184  * All versions of windows use this function to spool files to a printer
185  * via the cups interface
186  */
187 static void
188 smbd_spool_copyfile(smb_inaddr_t *ipaddr, char *username, char *path,
189     char *doc_name)
190 {
191 	smb_cups_ops_t	*cups;
192 	http_t		*http = NULL;		/* HTTP connection to server */
193 	ipp_t		*request = NULL;	/* IPP Request */
194 	ipp_t		*response = NULL;	/* IPP Response */
195 	cups_lang_t	*language = NULL;	/* Default language */
196 	char		uri[HTTP_MAX_URI];	/* printer-uri attribute */
197 	char		new_jobname[SMBD_PJOBLEN];
198 	smbd_printjob_t	pjob;
199 	char 		clientname[INET6_ADDRSTRLEN];
200 	struct stat 	sbuf;
201 	int		rc = 1;
202 
203 	if (stat(path, &sbuf)) {
204 		smb_log(smbd.s_loghd, LOG_INFO, "smbd_spool_copyfile: %s: %s",
205 		    path, strerror(errno));
206 		return;
207 	}
208 
209 	/*
210 	 * Remove zero size files and return; these were inadvertantly
211 	 * created by XP or 2000.
212 	 */
213 	if (sbuf.st_size == 0) {
214 		if (remove(path) != 0)
215 			smb_log(smbd.s_loghd, LOG_INFO,
216 			    "smbd_spool_copyfile: cannot remove %s: %s",
217 			    path, strerror(errno));
218 		return;
219 	}
220 
221 	if ((cups = smbd_cups_ops()) == NULL)
222 		return;
223 
224 	if ((http = cups->httpConnect("localhost", 631)) == NULL) {
225 		smb_log(smbd.s_loghd, LOG_INFO,
226 		    "smbd_spool_copyfile: cupsd not running");
227 		return;
228 	}
229 
230 	if ((request = cups->ippNew()) == NULL) {
231 		smb_log(smbd.s_loghd, LOG_INFO,
232 		    "smbd_spool_copyfile: ipp not running");
233 		return;
234 	}
235 
236 	request->request.op.operation_id = IPP_PRINT_JOB;
237 	request->request.op.request_id = 1;
238 	language = cups->cupsLangDefault();
239 
240 	cups->ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
241 	    "attributes-charset", NULL, cups->cupsLangEncoding(language));
242 
243 	cups->ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
244 	    "attributes-natural-language", NULL, language->language);
245 
246 	(void) snprintf(uri, sizeof (uri), "ipp://localhost/printers/%s",
247 	    SMBD_PRINTER);
248 	pjob.pj_pid = pthread_self();
249 	pjob.pj_sysjob = 10;
250 	(void) strlcpy(pjob.pj_filename, path, SMBD_PJOBLEN);
251 	pjob.pj_start_time = time(NULL);
252 	pjob.pj_status = 2;
253 	pjob.pj_size = sbuf.st_blocks * 512;
254 	pjob.pj_page_count = 1;
255 	pjob.pj_isspooled = B_TRUE;
256 	pjob.pj_jobnum = smbd_cups_jobnum;
257 
258 	(void) strlcpy(pjob.pj_jobname, doc_name, SMBD_PJOBLEN);
259 	(void) strlcpy(pjob.pj_username, username, SMBD_PJOBLEN);
260 	(void) strlcpy(pjob.pj_queuename, SMBD_CUPS_SPOOL_DIR, SMBD_PJOBLEN);
261 
262 	cups->ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
263 	    "printer-uri", NULL, uri);
264 
265 	cups->ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
266 	    "requesting-user-name", NULL, pjob.pj_username);
267 
268 	if (smb_inet_ntop(ipaddr, clientname,
269 	    SMB_IPSTRLEN(ipaddr->a_family)) == NULL) {
270 		smb_log(smbd.s_loghd, LOG_INFO,
271 		    "smbd_spool_copyfile: %s: unknown client", clientname);
272 		goto out;
273 	}
274 
275 	cups->ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
276 	    "job-originating-host-name", NULL, clientname);
277 
278 	(void) snprintf(new_jobname, SMBD_PJOBLEN, "%s%d",
279 	    SMBD_FN_PREFIX, pjob.pj_jobnum);
280 	cups->ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
281 	    "job-name", NULL, new_jobname);
282 
283 	(void) snprintf(uri, sizeof (uri) - 1, "/printers/%s", SMBD_PRINTER);
284 
285 	response = cups->cupsDoFileRequest(http, request, uri,
286 	    pjob.pj_filename);
287 	if (response != NULL) {
288 		if (response->request.status.status_code >= IPP_OK_CONFLICT) {
289 			smb_log(smbd.s_loghd, LOG_ERR,
290 			    "smbd_spool_copyfile: printer %s: %s",
291 			    SMBD_PRINTER,
292 			    cups->ippErrorString(cups->cupsLastError()));
293 		} else {
294 			atomic_inc_32(&smbd_cups_jobnum);
295 			rc = 0;
296 		}
297 	} else {
298 		smb_log(smbd.s_loghd, LOG_ERR,
299 		    "smbd_spool_copyfile: unable to print to %s",
300 		    cups->ippErrorString(cups->cupsLastError()));
301 	}
302 
303 	if (rc == 0)
304 		(void) unlink(pjob.pj_filename);
305 
306 out:
307 	if (response)
308 		cups->ippDelete(response);
309 
310 	if (language)
311 		cups->cupsLangFree(language);
312 
313 	if (http)
314 		cups->httpClose(http);
315 }
316 
317 int
318 smbd_cups_init(void)
319 {
320 	(void) mutex_lock(&smbd_cups_mutex);
321 
322 	if (smb_cups.cups_hdl != NULL) {
323 		(void) mutex_unlock(&smbd_cups_mutex);
324 		return (0);
325 	}
326 
327 	if ((smb_cups.cups_hdl = dlopen("libcups.so.2", RTLD_NOW)) == NULL) {
328 		(void) mutex_unlock(&smbd_cups_mutex);
329 		smb_log(smbd.s_loghd, LOG_DEBUG,
330 		    "smbd_cups_init: cannot open libcups");
331 		return (ENOENT);
332 	}
333 
334 	smb_cups.cupsLangDefault =
335 	    (cups_lang_t *(*)())dlsym(smb_cups.cups_hdl, "cupsLangDefault");
336 	smb_cups.cupsLangEncoding = (const char *(*)(cups_lang_t *))
337 	    dlsym(smb_cups.cups_hdl, "cupsLangEncoding");
338 	smb_cups.cupsDoFileRequest =
339 	    (ipp_t *(*)(http_t *, ipp_t *, const char *, const char *))
340 	    dlsym(smb_cups.cups_hdl, "cupsDoFileRequest");
341 	smb_cups.cupsLastError = (ipp_status_t (*)())
342 	    dlsym(smb_cups.cups_hdl, "cupsLastError");
343 	smb_cups.cupsLangFree = (void (*)(cups_lang_t *))
344 	    dlsym(smb_cups.cups_hdl, "cupsLangFree");
345 	smb_cups.cupsGetDests = (int (*)(cups_dest_t **))
346 	    dlsym(smb_cups.cups_hdl, "cupsGetDests");
347 	smb_cups.cupsFreeDests = (void (*)(int, cups_dest_t *))
348 	    dlsym(smb_cups.cups_hdl, "cupsFreeDests");
349 
350 	smb_cups.httpClose = (void (*)(http_t *))
351 	    dlsym(smb_cups.cups_hdl, "httpClose");
352 	smb_cups.httpConnect = (http_t *(*)(const char *, int))
353 	    dlsym(smb_cups.cups_hdl, "httpConnect");
354 
355 	smb_cups.ippNew = (ipp_t *(*)())dlsym(smb_cups.cups_hdl, "ippNew");
356 	smb_cups.ippDelete = (void (*)())dlsym(smb_cups.cups_hdl, "ippDelete");
357 	smb_cups.ippErrorString = (char *(*)())
358 	    dlsym(smb_cups.cups_hdl, "ippErrorString");
359 	smb_cups.ippAddString = (ipp_attribute_t *(*)())
360 	    dlsym(smb_cups.cups_hdl, "ippAddString");
361 
362 	if (smb_cups.cupsLangDefault == NULL ||
363 	    smb_cups.cupsLangEncoding == NULL ||
364 	    smb_cups.cupsDoFileRequest == NULL ||
365 	    smb_cups.cupsLastError == NULL ||
366 	    smb_cups.cupsLangFree == NULL ||
367 	    smb_cups.cupsGetDests == NULL ||
368 	    smb_cups.cupsFreeDests == NULL ||
369 	    smb_cups.ippNew == NULL ||
370 	    smb_cups.httpClose == NULL ||
371 	    smb_cups.httpConnect == NULL ||
372 	    smb_cups.ippDelete == NULL ||
373 	    smb_cups.ippErrorString == NULL ||
374 	    smb_cups.ippAddString == NULL) {
375 		(void) dlclose(smb_cups.cups_hdl);
376 		smb_cups.cups_hdl = NULL;
377 		(void) mutex_unlock(&smbd_cups_mutex);
378 		smb_log(smbd.s_loghd, LOG_DEBUG,
379 		    "smbd_cups_init: cannot load libcups");
380 		return (ENOENT);
381 	}
382 
383 	(void) mutex_unlock(&smbd_cups_mutex);
384 	return (0);
385 }
386 
387 void
388 smbd_cups_fini(void)
389 {
390 	(void) mutex_lock(&smbd_cups_mutex);
391 
392 	if (smb_cups.cups_hdl != NULL) {
393 		(void) dlclose(smb_cups.cups_hdl);
394 		smb_cups.cups_hdl = NULL;
395 	}
396 
397 	(void) mutex_unlock(&smbd_cups_mutex);
398 }
399 
400 static smb_cups_ops_t *
401 smbd_cups_ops(void)
402 {
403 	if (smb_cups.cups_hdl == NULL)
404 		return (NULL);
405 
406 	return (&smb_cups);
407 }
408 
409 void
410 smbd_load_printers(void)
411 {
412 	pthread_t	tid;
413 	pthread_attr_t	attr;
414 	int		rc;
415 
416 	if (!smb_config_getbool(SMB_CI_PRINT_ENABLE))
417 		return;
418 
419 	(void) pthread_attr_init(&attr);
420 	(void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
421 	rc = pthread_create(&tid, &attr, smbd_share_printers, &tid);
422 	(void) pthread_attr_destroy(&attr);
423 
424 	if (rc != 0)
425 		smb_log(smbd.s_loghd, LOG_NOTICE,
426 		    "unable to load printer shares: %s", strerror(errno));
427 }
428 
429 /*
430  * All print shares use the path from print$.
431  */
432 /*ARGSUSED*/
433 static void *
434 smbd_share_printers(void *arg)
435 {
436 	cups_dest_t	*dests;
437 	cups_dest_t	*dest;
438 	smb_cups_ops_t	*cups;
439 	smb_share_t	si;
440 	uint32_t	nerr;
441 	int		num_dests;
442 	int		i;
443 
444 	if (!smb_config_getbool(SMB_CI_PRINT_ENABLE))
445 		return (NULL);
446 
447 	if ((cups = smbd_cups_ops()) == NULL)
448 		return (NULL);
449 
450 	if (smb_shr_get(SMB_SHARE_PRINT, &si) != NERR_Success) {
451 		smb_log(smbd.s_loghd, LOG_DEBUG,
452 		    "smbd_share_printers unable to load %s", SMB_SHARE_PRINT);
453 		return (NULL);
454 	}
455 
456 	num_dests = cups->cupsGetDests(&dests);
457 
458 	for (i = num_dests, dest = dests; i > 0; i--, dest++) {
459 		if (dest->instance != NULL)
460 			continue;
461 
462 		(void) strlcpy(si.shr_name, dest->name, MAXPATHLEN);
463 		smbd_print_share_comment(&si, dest);
464 		si.shr_type = STYPE_PRINTQ;
465 
466 		nerr = smb_shr_add(&si);
467 		if (nerr == NERR_Success || nerr == NERR_DuplicateShare)
468 			smb_log(smbd.s_loghd, LOG_DEBUG,
469 			    "shared printer: %s", si.shr_name);
470 		else
471 			smb_log(smbd.s_loghd, LOG_DEBUG,
472 			    "smbd_share_printers: unable to add share %s: %u",
473 			    si.shr_name, nerr);
474 	}
475 
476 	cups->cupsFreeDests(num_dests, dests);
477 	return (NULL);
478 }
479 
480 static void
481 smbd_print_share_comment(smb_share_t *si, cups_dest_t *dest)
482 {
483 	cups_option_t	*options;
484 	char		*comment;
485 	char		*name;
486 	char		*value;
487 	int		i;
488 
489 	comment = "Print Share";
490 
491 	if ((options = dest->options) == NULL) {
492 		(void) strlcpy(si->shr_cmnt, comment, SMB_SHARE_CMNT_MAX);
493 		return;
494 	}
495 
496 	for (i = 0; i < dest->num_options; ++i) {
497 		name = options[i].name;
498 		value = options[i].value;
499 
500 		if (name == NULL || value == NULL ||
501 		    *name == '\0' || *value == '\0')
502 			continue;
503 
504 		if (strcasecmp(name, "printer-info") == 0) {
505 			comment = value;
506 			break;
507 		}
508 	}
509 
510 	(void) strlcpy(si->shr_cmnt, comment, SMB_SHARE_CMNT_MAX);
511 }
512