xref: /titanic_51/usr/src/uts/common/fs/smbsrv/smb_print.c (revision 8622ec4569457733001d4982ef7f5b44427069be)
1da6c28aaSamw /*
2da6c28aaSamw  * CDDL HEADER START
3da6c28aaSamw  *
4da6c28aaSamw  * The contents of this file are subject to the terms of the
5da6c28aaSamw  * Common Development and Distribution License (the "License").
6da6c28aaSamw  * You may not use this file except in compliance with the License.
7da6c28aaSamw  *
8da6c28aaSamw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da6c28aaSamw  * or http://www.opensolaris.org/os/licensing.
10da6c28aaSamw  * See the License for the specific language governing permissions
11da6c28aaSamw  * and limitations under the License.
12da6c28aaSamw  *
13da6c28aaSamw  * When distributing Covered Code, include this CDDL HEADER in each
14da6c28aaSamw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da6c28aaSamw  * If applicable, add the following below this CDDL HEADER, with the
16da6c28aaSamw  * fields enclosed by brackets "[]" replaced with your own identifying
17da6c28aaSamw  * information: Portions Copyright [yyyy] [name of copyright owner]
18da6c28aaSamw  *
19da6c28aaSamw  * CDDL HEADER END
20da6c28aaSamw  */
21da6c28aaSamw /*
22148c5f43SAlan Wright  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
235fd03bc0SGordon Ross  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
24da6c28aaSamw  */
25da6c28aaSamw 
26da6c28aaSamw /*
27da6c28aaSamw  * SMB print interface.
28da6c28aaSamw  */
29da6c28aaSamw 
30bbf6f00cSJordan Brown #include <smbsrv/smb_kproto.h>
31cb174861Sjoyce mcintosh #include <sys/unistd.h>
32cb174861Sjoyce mcintosh #include <sys/stat.h>
33cb174861Sjoyce mcintosh #include <sys/types.h>
34cb174861Sjoyce mcintosh #include <sys/fcntl.h>
35cb174861Sjoyce mcintosh #include <smbsrv/smb_share.h>
36da6c28aaSamw 
37da6c28aaSamw /*
38cb174861Sjoyce mcintosh  * Starts the creation of a new printer file, which will be deleted
39cb174861Sjoyce mcintosh  * automatically once it has been closed and printed.
40da6c28aaSamw  *
41da6c28aaSamw  * SetupLength is the number of bytes in the first part of the resulting
42da6c28aaSamw  * print spool file which contains printer-specific control strings.
43da6c28aaSamw  *
44da6c28aaSamw  * Mode can have the following values:
45da6c28aaSamw  *      0     Text mode.  The server may optionally
46da6c28aaSamw  *            expand tabs to a series of spaces.
47da6c28aaSamw  *      1     Graphics mode.  No conversion of data
48da6c28aaSamw  *            should be done by the server.
49da6c28aaSamw  *
50f96bd5c8SAlan Wright  * IdentifierString can be used by the server to provide some sort of
51f96bd5c8SAlan Wright  * per-client identifying component to the print file.
52da6c28aaSamw  *
53f96bd5c8SAlan Wright  * When the file is closed, it will be sent to the spooler and printed.
54da6c28aaSamw  */
55faa1795aSjb150015 smb_sdrc_t
56faa1795aSjb150015 smb_pre_open_print_file(smb_request_t *sr)
57da6c28aaSamw {
58f96bd5c8SAlan Wright 	struct open_param	*op = &sr->arg.open;
59f96bd5c8SAlan Wright 	char			*path;
60f96bd5c8SAlan Wright 	char			*identifier;
61148c5f43SAlan Wright 	uint32_t		new_id;
62f96bd5c8SAlan Wright 	uint16_t		setup;
63f96bd5c8SAlan Wright 	uint16_t		mode;
64f96bd5c8SAlan Wright 	int			rc;
65cb174861Sjoyce mcintosh 	static uint32_t		tmp_id = 10000;
66f96bd5c8SAlan Wright 
67f96bd5c8SAlan Wright 	bzero(op, sizeof (sr->arg.open));
68f96bd5c8SAlan Wright 	rc = smbsr_decode_vwv(sr, "ww", &setup, &mode);
69f96bd5c8SAlan Wright 	if (rc == 0)
70f96bd5c8SAlan Wright 		rc = smbsr_decode_data(sr, "%S", sr, &identifier);
71f96bd5c8SAlan Wright 
72148c5f43SAlan Wright 	if (rc == 0) {
739fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		path = smb_srm_zalloc(sr, MAXPATHLEN);
74f96bd5c8SAlan Wright 		op->fqi.fq_path.pn_path = path;
75148c5f43SAlan Wright 		new_id = atomic_inc_32_nv(&tmp_id);
76148c5f43SAlan Wright 		(void) snprintf(path, MAXPATHLEN, "%s%05u", identifier, new_id);
77148c5f43SAlan Wright 	}
78f96bd5c8SAlan Wright 
79f96bd5c8SAlan Wright 	op->create_disposition = FILE_OVERWRITE_IF;
80f96bd5c8SAlan Wright 	op->create_options = FILE_NON_DIRECTORY_FILE;
81f96bd5c8SAlan Wright 	DTRACE_SMB_2(op__OpenPrintFile__start, smb_request_t *, sr,
82f96bd5c8SAlan Wright 	    struct open_param *, op);
83f96bd5c8SAlan Wright 
84f96bd5c8SAlan Wright 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
85faa1795aSjb150015 }
86faa1795aSjb150015 
87faa1795aSjb150015 void
88faa1795aSjb150015 smb_post_open_print_file(smb_request_t *sr)
89faa1795aSjb150015 {
90faa1795aSjb150015 	DTRACE_SMB_1(op__OpenPrintFile__done, smb_request_t *, sr);
91faa1795aSjb150015 }
92faa1795aSjb150015 
93cb174861Sjoyce mcintosh /*
94cb174861Sjoyce mcintosh  * Creates a new spool file which will be later copied and
95cb174861Sjoyce mcintosh  * deleted by cupsd.  After the file is created, information
96cb174861Sjoyce mcintosh  * related to the file will be placed in a spooldoc list
97cb174861Sjoyce mcintosh  * to be later used by cupsd
98cb174861Sjoyce mcintosh  *
99cb174861Sjoyce mcintosh  * Return values
100cb174861Sjoyce mcintosh  * 	rc 0 SDRC_SUCCESS
101cb174861Sjoyce mcintosh  *	rc non-zero SDRC_ERROR
102cb174861Sjoyce mcintosh  */
103cb174861Sjoyce mcintosh 
104f96bd5c8SAlan Wright smb_sdrc_t
105faa1795aSjb150015 smb_com_open_print_file(smb_request_t *sr)
106faa1795aSjb150015 {
107f96bd5c8SAlan Wright 	int 		rc;
108cb174861Sjoyce mcintosh 	smb_kspooldoc_t *sp;
109cb174861Sjoyce mcintosh 	smb_kshare_t 	*si;
110cb174861Sjoyce mcintosh 	struct open_param *op = &sr->arg.open;
111f96bd5c8SAlan Wright 
112b7301bf5SGordon Ross 	if (sr->sr_server->sv_cfg.skc_print_enable == 0 ||
113b7301bf5SGordon Ross 	    !STYPE_ISPRN(sr->tid_tree->t_res_type)) {
114cb174861Sjoyce mcintosh 		cmn_err(CE_WARN, "smb_com_open_print_file: bad device");
115f96bd5c8SAlan Wright 		smbsr_error(sr, NT_STATUS_BAD_DEVICE_TYPE,
116f96bd5c8SAlan Wright 		    ERRDOS, ERROR_BAD_DEV_TYPE);
117f96bd5c8SAlan Wright 		return (SDRC_ERROR);
118da6c28aaSamw 	}
119cb174861Sjoyce mcintosh 	if ((rc = smb_common_create(sr)) != NT_STATUS_SUCCESS) {
120cb174861Sjoyce mcintosh 		cmn_err(CE_WARN, "smb_com_open_print_file: error rc=%d", rc);
121f96bd5c8SAlan Wright 		return (SDRC_ERROR);
122cb174861Sjoyce mcintosh 	}
123cb174861Sjoyce mcintosh 	if ((rc = smbsr_encode_result(sr, 1, 0,
124cb174861Sjoyce mcintosh 	    "bww", 1, sr->smb_fid, 0)) == 0) {
125*8622ec45SGordon Ross 		si = smb_kshare_lookup(sr->sr_server, SMB_SHARE_PRINT);
126*8622ec45SGordon Ross 		if (si == NULL) {
127cb174861Sjoyce mcintosh 			cmn_err(CE_NOTE, "smb_com_open_print_file: SDRC_ERROR");
128cb174861Sjoyce mcintosh 			return (SDRC_ERROR);
129cb174861Sjoyce mcintosh 		}
130cb174861Sjoyce mcintosh 		sp = kmem_zalloc(sizeof (smb_kspooldoc_t), KM_SLEEP);
131cb174861Sjoyce mcintosh 		(void) snprintf(sp->sd_path, MAXPATHLEN, "%s/%s", si->shr_path,
132cb174861Sjoyce mcintosh 		    op->fqi.fq_path.pn_path);
13323a9c295SGordon Ross 		/* sp->sd_spool_num set by smb_spool_add_doc() */
134cb174861Sjoyce mcintosh 		sp->sd_ipaddr = sr->session->ipaddr;
135cb174861Sjoyce mcintosh 		(void) strlcpy(sp->sd_username, sr->uid_user->u_name,
136cb174861Sjoyce mcintosh 		    MAXNAMELEN);
137cb174861Sjoyce mcintosh 		sp->sd_fid = sr->smb_fid;
138*8622ec45SGordon Ross 		if (smb_spool_add_doc(sr->tid_tree, sp))
139cb174861Sjoyce mcintosh 			kmem_free(sp, sizeof (smb_kspooldoc_t));
140*8622ec45SGordon Ross 		smb_kshare_release(sr->sr_server, si);
141cb174861Sjoyce mcintosh 	}
142f96bd5c8SAlan Wright 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
143f96bd5c8SAlan Wright }
144da6c28aaSamw 
145da6c28aaSamw /*
146f96bd5c8SAlan Wright  * Close the specified file handle and queue the file for printing.
147f96bd5c8SAlan Wright  * The fid refers to a file previously created as a print spool file.
148da6c28aaSamw  * On successful completion of this request, the file is queued for
149da6c28aaSamw  * printing by the server.
150da6c28aaSamw  *
151f96bd5c8SAlan Wright  * Servers that negotiate LANMAN1.0 or later allow all the the fid
152f96bd5c8SAlan Wright  * to be closed and printed via any close request.
153da6c28aaSamw  */
154faa1795aSjb150015 smb_sdrc_t
155faa1795aSjb150015 smb_pre_close_print_file(smb_request_t *sr)
156da6c28aaSamw {
157f96bd5c8SAlan Wright 	int rc;
158f96bd5c8SAlan Wright 
159f96bd5c8SAlan Wright 	rc = smbsr_decode_vwv(sr, "w", &sr->smb_fid);
160f96bd5c8SAlan Wright 
161faa1795aSjb150015 	DTRACE_SMB_1(op__ClosePrintFile__start, smb_request_t *, sr);
162f96bd5c8SAlan Wright 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
163faa1795aSjb150015 }
164faa1795aSjb150015 
165faa1795aSjb150015 void
166faa1795aSjb150015 smb_post_close_print_file(smb_request_t *sr)
167faa1795aSjb150015 {
168faa1795aSjb150015 	DTRACE_SMB_1(op__ClosePrintFile__done, smb_request_t *, sr);
169faa1795aSjb150015 }
170faa1795aSjb150015 
171cb174861Sjoyce mcintosh /*
172cb174861Sjoyce mcintosh  *
173cb174861Sjoyce mcintosh  * Adds the print file fid to a list to be used as a search
174cb174861Sjoyce mcintosh  * key in the spooldoc list.  It then wakes up the smbd
175cb174861Sjoyce mcintosh  * spool monitor thread to copy the spool file.
176cb174861Sjoyce mcintosh  *
177cb174861Sjoyce mcintosh  * Return values
178cb174861Sjoyce mcintosh  * rc - 0 success
179cb174861Sjoyce mcintosh  *
180cb174861Sjoyce mcintosh  */
181cb174861Sjoyce mcintosh 
182f96bd5c8SAlan Wright smb_sdrc_t
183faa1795aSjb150015 smb_com_close_print_file(smb_request_t *sr)
184faa1795aSjb150015 {
185cb174861Sjoyce mcintosh 	smb_sdrc_t rc;
186cb174861Sjoyce mcintosh 
18786d7016bSGordon Ross 	/*
18886d7016bSGordon Ross 	 * If sv_cfg.skc_print_enable somehow went false while
18986d7016bSGordon Ross 	 * we have a print FID open, close the FID.  In this
19086d7016bSGordon Ross 	 * situation, smb_spool_add_fid() will do nothing.
19186d7016bSGordon Ross 	 */
19286d7016bSGordon Ross 	if (!STYPE_ISPRN(sr->tid_tree->t_res_type)) {
193f96bd5c8SAlan Wright 		smbsr_error(sr, NT_STATUS_BAD_DEVICE_TYPE,
194f96bd5c8SAlan Wright 		    ERRDOS, ERROR_BAD_DEV_TYPE);
195cb174861Sjoyce mcintosh 		cmn_err(CE_WARN, "smb_com_close_print_file: SDRC_ERROR");
196f96bd5c8SAlan Wright 		return (SDRC_ERROR);
197da6c28aaSamw 	}
198cb174861Sjoyce mcintosh 	rc = smb_com_close(sr);
199da6c28aaSamw 
20086d7016bSGordon Ross 	smb_spool_add_fid(sr->sr_server, sr->smb_fid);
201cb174861Sjoyce mcintosh 
202cb174861Sjoyce mcintosh 	return (rc);
203f96bd5c8SAlan Wright }
204da6c28aaSamw 
205da6c28aaSamw /*
206f96bd5c8SAlan Wright  * Get a list of print queue entries on the server.  Support for
207f96bd5c8SAlan Wright  * this request is optional (not required for Windows clients).
208da6c28aaSamw  */
2097b59d02dSjb150015 smb_sdrc_t
210faa1795aSjb150015 smb_pre_get_print_queue(smb_request_t *sr)
211faa1795aSjb150015 {
212faa1795aSjb150015 	DTRACE_SMB_1(op__GetPrintQueue__start, smb_request_t *, sr);
213faa1795aSjb150015 	return (SDRC_SUCCESS);
214faa1795aSjb150015 }
215faa1795aSjb150015 
216faa1795aSjb150015 void
217faa1795aSjb150015 smb_post_get_print_queue(smb_request_t *sr)
218faa1795aSjb150015 {
219faa1795aSjb150015 	DTRACE_SMB_1(op__GetPrintQueue__done, smb_request_t *, sr);
220faa1795aSjb150015 }
221faa1795aSjb150015 
222faa1795aSjb150015 smb_sdrc_t
223faa1795aSjb150015 smb_com_get_print_queue(smb_request_t *sr)
224da6c28aaSamw {
225da6c28aaSamw 	unsigned short max_count, start_ix;
226da6c28aaSamw 
2277b59d02dSjb150015 	if (smbsr_decode_vwv(sr, "ww", &max_count, &start_ix) != 0)
228faa1795aSjb150015 		return (SDRC_ERROR);
229da6c28aaSamw 
2307b59d02dSjb150015 	if (smbsr_encode_result(sr, 2, 3, "bwwwbw", 2, 0, 0, 3, 1, 0))
231faa1795aSjb150015 		return (SDRC_ERROR);
2327b59d02dSjb150015 
233faa1795aSjb150015 	return (SDRC_SUCCESS);
234da6c28aaSamw }
235da6c28aaSamw 
236da6c28aaSamw /*
237f96bd5c8SAlan Wright  * Write (append) data to a print spool file.  The fid must refer to
238f96bd5c8SAlan Wright  * a print spool file.
239da6c28aaSamw  *
240f96bd5c8SAlan Wright  * The first SetupLength bytes (see SMB_COM_OPEN_PRINT_FILE) in the
241f96bd5c8SAlan Wright  * print spool file contain printer setup data.
242da6c28aaSamw  *
243f96bd5c8SAlan Wright  * Servers that negotiate LANMAN1.0 or later also support the use of
244f96bd5c8SAlan Wright  * normal write requests with print spool files.
245da6c28aaSamw  */
246faa1795aSjb150015 smb_sdrc_t
247faa1795aSjb150015 smb_pre_write_print_file(smb_request_t *sr)
248da6c28aaSamw {
249f96bd5c8SAlan Wright 	smb_rw_param_t	*param;
250f96bd5c8SAlan Wright 	int		rc;
251f96bd5c8SAlan Wright 
252f96bd5c8SAlan Wright 	param = kmem_zalloc(sizeof (smb_rw_param_t), KM_SLEEP);
253f96bd5c8SAlan Wright 	sr->arg.rw = param;
254f96bd5c8SAlan Wright 	param->rw_magic = SMB_RW_MAGIC;
255f96bd5c8SAlan Wright 
256f96bd5c8SAlan Wright 	rc = smbsr_decode_vwv(sr, "w", &sr->smb_fid);
257f96bd5c8SAlan Wright 
258faa1795aSjb150015 	DTRACE_SMB_1(op__WritePrintFile__start, smb_request_t *, sr);
259f96bd5c8SAlan Wright 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
260faa1795aSjb150015 }
261faa1795aSjb150015 
262faa1795aSjb150015 void
263faa1795aSjb150015 smb_post_write_print_file(smb_request_t *sr)
264faa1795aSjb150015 {
265faa1795aSjb150015 	DTRACE_SMB_1(op__WritePrintFile__done, smb_request_t *, sr);
266f96bd5c8SAlan Wright 
267f96bd5c8SAlan Wright 	kmem_free(sr->arg.rw, sizeof (smb_rw_param_t));
268faa1795aSjb150015 }
269faa1795aSjb150015 
270f96bd5c8SAlan Wright smb_sdrc_t
271faa1795aSjb150015 smb_com_write_print_file(smb_request_t *sr)
272faa1795aSjb150015 {
273f96bd5c8SAlan Wright 	smb_rw_param_t	*param = sr->arg.rw;
274f96bd5c8SAlan Wright 	smb_node_t	*node;
275f96bd5c8SAlan Wright 	smb_attr_t	attr;
276f96bd5c8SAlan Wright 	int		rc;
277f96bd5c8SAlan Wright 
278b7301bf5SGordon Ross 	if (sr->sr_server->sv_cfg.skc_print_enable == 0 ||
279b7301bf5SGordon Ross 	    !STYPE_ISPRN(sr->tid_tree->t_res_type)) {
280f96bd5c8SAlan Wright 		smbsr_error(sr, NT_STATUS_BAD_DEVICE_TYPE,
281f96bd5c8SAlan Wright 		    ERRDOS, ERROR_BAD_DEV_TYPE);
282f96bd5c8SAlan Wright 		return (SDRC_ERROR);
283f96bd5c8SAlan Wright 	}
284f96bd5c8SAlan Wright 
285f96bd5c8SAlan Wright 	smbsr_lookup_file(sr);
286f96bd5c8SAlan Wright 	if (sr->fid_ofile == NULL) {
287f96bd5c8SAlan Wright 		smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid);
288f96bd5c8SAlan Wright 		return (SDRC_ERROR);
289f96bd5c8SAlan Wright 	}
290f96bd5c8SAlan Wright 
291f96bd5c8SAlan Wright 	node = sr->fid_ofile->f_node;
292f96bd5c8SAlan Wright 	sr->user_cr = smb_ofile_getcred(sr->fid_ofile);
293f96bd5c8SAlan Wright 
2945fd03bc0SGordon Ross 	bzero(&attr, sizeof (attr));
2955fd03bc0SGordon Ross 	attr.sa_mask = SMB_AT_SIZE;
2965fd03bc0SGordon Ross 	rc = smb_node_getattr(sr, node, sr->user_cr, sr->fid_ofile, &attr);
2975fd03bc0SGordon Ross 	if (rc != 0) {
298f96bd5c8SAlan Wright 		smbsr_error(sr, NT_STATUS_INTERNAL_ERROR,
299f96bd5c8SAlan Wright 		    ERRDOS, ERROR_INTERNAL_ERROR);
300f96bd5c8SAlan Wright 		return (SDRC_ERROR);
301f96bd5c8SAlan Wright 	}
302f96bd5c8SAlan Wright 
303f96bd5c8SAlan Wright 	if ((smbsr_decode_data(sr, "D", &param->rw_vdb)) != 0) {
304f96bd5c8SAlan Wright 		smbsr_error(sr, NT_STATUS_INVALID_PARAMETER,
305f96bd5c8SAlan Wright 		    ERRDOS, ERROR_INVALID_PARAMETER);
306f96bd5c8SAlan Wright 		return (SDRC_ERROR);
307f96bd5c8SAlan Wright 	}
308f96bd5c8SAlan Wright 
309f96bd5c8SAlan Wright 	param->rw_count = param->rw_vdb.vdb_len;
310f96bd5c8SAlan Wright 	param->rw_offset = attr.sa_vattr.va_size;
311f96bd5c8SAlan Wright 	param->rw_vdb.vdb_uio.uio_loffset = (offset_t)param->rw_offset;
312f96bd5c8SAlan Wright 
313f96bd5c8SAlan Wright 	if ((rc = smb_common_write(sr, param)) != 0) {
314f96bd5c8SAlan Wright 		if (sr->smb_error.status != NT_STATUS_FILE_LOCK_CONFLICT)
315f96bd5c8SAlan Wright 			smbsr_errno(sr, rc);
316f96bd5c8SAlan Wright 		return (SDRC_ERROR);
317f96bd5c8SAlan Wright 	}
318f96bd5c8SAlan Wright 
319f96bd5c8SAlan Wright 	rc = smbsr_encode_empty_result(sr);
320f96bd5c8SAlan Wright 	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
321da6c28aaSamw }
322