summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/smbsrv/smb_print.c
blob: 96156b4f7114e3780a206c1d3bc6e92052ce2bd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
 */

/*
 * SMB print interface.
 */

#include <smbsrv/smb_kproto.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <smbsrv/smb_share.h>

/*
 * Starts the creation of a new printer file, which will be deleted
 * automatically once it has been closed and printed.
 *
 * SetupLength is the number of bytes in the first part of the resulting
 * print spool file which contains printer-specific control strings.
 *
 * Mode can have the following values:
 *      0     Text mode.  The server may optionally
 *            expand tabs to a series of spaces.
 *      1     Graphics mode.  No conversion of data
 *            should be done by the server.
 *
 * IdentifierString can be used by the server to provide some sort of
 * per-client identifying component to the print file.
 *
 * When the file is closed, it will be sent to the spooler and printed.
 */
smb_sdrc_t
smb_pre_open_print_file(smb_request_t *sr)
{
	struct open_param	*op = &sr->arg.open;
	char			*path;
	char			*identifier;
	uint32_t		new_id;
	uint16_t		setup;
	uint16_t		mode;
	int			rc;
	static uint32_t		tmp_id = 10000;

	bzero(op, sizeof (sr->arg.open));
	rc = smbsr_decode_vwv(sr, "ww", &setup, &mode);
	if (rc == 0)
		rc = smbsr_decode_data(sr, "%S", sr, &identifier);

	if (rc == 0) {
		path = smb_srm_zalloc(sr, MAXPATHLEN);
		op->fqi.fq_path.pn_path = path;
		new_id = atomic_inc_32_nv(&tmp_id);
		(void) snprintf(path, MAXPATHLEN, "%s%05u", identifier, new_id);
	}

	op->create_disposition = FILE_OVERWRITE_IF;
	op->create_options = FILE_NON_DIRECTORY_FILE;
	DTRACE_SMB_START(op__OpenPrintFile, smb_request_t *, sr); /* arg.open */

	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
}

void
smb_post_open_print_file(smb_request_t *sr)
{
	DTRACE_SMB_DONE(op__OpenPrintFile, smb_request_t *, sr);
}

/*
 * Creates a new spool file which will be later copied and
 * deleted by cupsd.  After the file is created, information
 * related to the file will be placed in a spooldoc list
 * to be later used by cupsd
 *
 * Return values
 * 	rc 0 SDRC_SUCCESS
 *	rc non-zero SDRC_ERROR
 */

smb_sdrc_t
smb_com_open_print_file(smb_request_t *sr)
{
	int 		rc;
	smb_kspooldoc_t *sp;
	smb_kshare_t 	*si;
	struct open_param *op = &sr->arg.open;

	if (sr->sr_server->sv_cfg.skc_print_enable == 0 ||
	    !STYPE_ISPRN(sr->tid_tree->t_res_type)) {
		cmn_err(CE_WARN, "smb_com_open_print_file: bad device");
		smbsr_error(sr, NT_STATUS_BAD_DEVICE_TYPE,
		    ERRDOS, ERROR_BAD_DEV_TYPE);
		return (SDRC_ERROR);
	}
	if ((rc = smb_common_create(sr)) != NT_STATUS_SUCCESS) {
		cmn_err(CE_WARN, "smb_com_open_print_file: error rc=%d", rc);
		return (SDRC_ERROR);
	}
	if ((rc = smbsr_encode_result(sr, 1, 0,
	    "bww", 1, sr->smb_fid, 0)) == 0) {
		si = smb_kshare_lookup(sr->sr_server, SMB_SHARE_PRINT);
		if (si == NULL) {
			cmn_err(CE_NOTE, "smb_com_open_print_file: SDRC_ERROR");
			return (SDRC_ERROR);
		}
		sp = kmem_zalloc(sizeof (smb_kspooldoc_t), KM_SLEEP);
		(void) snprintf(sp->sd_path, MAXPATHLEN, "%s/%s", si->shr_path,
		    op->fqi.fq_path.pn_path);
		/* sp->sd_spool_num set by smb_spool_add_doc() */
		sp->sd_ipaddr = sr->session->ipaddr;
		(void) strlcpy(sp->sd_username, sr->uid_user->u_name,
		    MAXNAMELEN);
		sp->sd_fid = sr->smb_fid;
		if (smb_spool_add_doc(sr->tid_tree, sp))
			kmem_free(sp, sizeof (smb_kspooldoc_t));
		smb_kshare_release(sr->sr_server, si);
	}
	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
}

/*
 * Close the specified file handle and queue the file for printing.
 * The fid refers to a file previously created as a print spool file.
 * On successful completion of this request, the file is queued for
 * printing by the server.
 *
 * Servers that negotiate LANMAN1.0 or later allow all the the fid
 * to be closed and printed via any close request.
 */
smb_sdrc_t
smb_pre_close_print_file(smb_request_t *sr)
{
	int rc;

	rc = smbsr_decode_vwv(sr, "w", &sr->smb_fid);

	DTRACE_SMB_START(op__ClosePrintFile, smb_request_t *, sr);
	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
}

void
smb_post_close_print_file(smb_request_t *sr)
{
	DTRACE_SMB_DONE(op__ClosePrintFile, smb_request_t *, sr);
}

/*
 *
 * Adds the print file fid to a list to be used as a search
 * key in the spooldoc list.  It then wakes up the smbd
 * spool monitor thread to copy the spool file.
 *
 * Return values
 * rc - 0 success
 *
 */

smb_sdrc_t
smb_com_close_print_file(smb_request_t *sr)
{
	smb_sdrc_t rc;

	/*
	 * If sv_cfg.skc_print_enable somehow went false while
	 * we have a print FID open, close the FID.  In this
	 * situation, smb_spool_add_fid() will do nothing.
	 */
	if (!STYPE_ISPRN(sr->tid_tree->t_res_type)) {
		smbsr_error(sr, NT_STATUS_BAD_DEVICE_TYPE,
		    ERRDOS, ERROR_BAD_DEV_TYPE);
		cmn_err(CE_WARN, "smb_com_close_print_file: SDRC_ERROR");
		return (SDRC_ERROR);
	}
	rc = smb_com_close(sr);

	smb_spool_add_fid(sr->sr_server, sr->smb_fid);

	return (rc);
}

/*
 * Get a list of print queue entries on the server.  Support for
 * this request is optional (not required for Windows clients).
 */
smb_sdrc_t
smb_pre_get_print_queue(smb_request_t *sr)
{
	DTRACE_SMB_START(op__GetPrintQueue, smb_request_t *, sr);
	return (SDRC_SUCCESS);
}

void
smb_post_get_print_queue(smb_request_t *sr)
{
	DTRACE_SMB_DONE(op__GetPrintQueue, smb_request_t *, sr);
}

smb_sdrc_t
smb_com_get_print_queue(smb_request_t *sr)
{
	unsigned short max_count, start_ix;

	if (smbsr_decode_vwv(sr, "ww", &max_count, &start_ix) != 0)
		return (SDRC_ERROR);

	if (smbsr_encode_result(sr, 2, 3, "bwwwbw", 2, 0, 0, 3, 1, 0))
		return (SDRC_ERROR);

	return (SDRC_SUCCESS);
}

/*
 * Write (append) data to a print spool file.  The fid must refer to
 * a print spool file.
 *
 * The first SetupLength bytes (see SMB_COM_OPEN_PRINT_FILE) in the
 * print spool file contain printer setup data.
 *
 * Servers that negotiate LANMAN1.0 or later also support the use of
 * normal write requests with print spool files.
 */
smb_sdrc_t
smb_pre_write_print_file(smb_request_t *sr)
{
	smb_rw_param_t	*param;
	int		rc;

	param = kmem_zalloc(sizeof (smb_rw_param_t), KM_SLEEP);
	sr->arg.rw = param;
	param->rw_magic = SMB_RW_MAGIC;

	rc = smbsr_decode_vwv(sr, "w", &sr->smb_fid);

	DTRACE_SMB_START(op__WritePrintFile, smb_request_t *, sr);
	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
}

void
smb_post_write_print_file(smb_request_t *sr)
{
	DTRACE_SMB_DONE(op__WritePrintFile, smb_request_t *, sr);

	kmem_free(sr->arg.rw, sizeof (smb_rw_param_t));
}

smb_sdrc_t
smb_com_write_print_file(smb_request_t *sr)
{
	smb_rw_param_t	*param = sr->arg.rw;
	smb_node_t	*node;
	smb_attr_t	attr;
	int		rc;

	if (sr->sr_server->sv_cfg.skc_print_enable == 0 ||
	    !STYPE_ISPRN(sr->tid_tree->t_res_type)) {
		smbsr_error(sr, NT_STATUS_BAD_DEVICE_TYPE,
		    ERRDOS, ERROR_BAD_DEV_TYPE);
		return (SDRC_ERROR);
	}

	smbsr_lookup_file(sr);
	if (sr->fid_ofile == NULL) {
		smbsr_error(sr, NT_STATUS_INVALID_HANDLE, ERRDOS, ERRbadfid);
		return (SDRC_ERROR);
	}

	node = sr->fid_ofile->f_node;
	sr->user_cr = smb_ofile_getcred(sr->fid_ofile);

	bzero(&attr, sizeof (attr));
	attr.sa_mask = SMB_AT_SIZE;
	rc = smb_node_getattr(sr, node, sr->user_cr, sr->fid_ofile, &attr);
	if (rc != 0) {
		smbsr_error(sr, NT_STATUS_INTERNAL_ERROR,
		    ERRDOS, ERROR_INTERNAL_ERROR);
		return (SDRC_ERROR);
	}

	if ((smbsr_decode_data(sr, "D", &param->rw_vdb)) != 0) {
		smbsr_error(sr, NT_STATUS_INVALID_PARAMETER,
		    ERRDOS, ERROR_INVALID_PARAMETER);
		return (SDRC_ERROR);
	}

	param->rw_count = param->rw_vdb.vdb_len;
	param->rw_offset = attr.sa_vattr.va_size;
	param->rw_vdb.vdb_uio.uio_loffset = (offset_t)param->rw_offset;

	if ((rc = smb_common_write(sr, param)) != 0) {
		if (sr->smb_error.status != NT_STATUS_FILE_LOCK_CONFLICT)
			smbsr_errno(sr, rc);
		return (SDRC_ERROR);
	}

	rc = smbsr_encode_empty_result(sr);
	return ((rc == 0) ? SDRC_SUCCESS : SDRC_ERROR);
}