xref: /illumos-gate/usr/src/lib/libsmbfs/smb/print.c (revision adee678425979226b2b55d1a0b39ce4c989382e9)
14bff34e3Sthurlow /*
24bff34e3Sthurlow  * Copyright (c) 2000, Boris Popov
34bff34e3Sthurlow  * All rights reserved.
44bff34e3Sthurlow  *
54bff34e3Sthurlow  * Redistribution and use in source and binary forms, with or without
64bff34e3Sthurlow  * modification, are permitted provided that the following conditions
74bff34e3Sthurlow  * are met:
84bff34e3Sthurlow  * 1. Redistributions of source code must retain the above copyright
94bff34e3Sthurlow  *    notice, this list of conditions and the following disclaimer.
104bff34e3Sthurlow  * 2. Redistributions in binary form must reproduce the above copyright
114bff34e3Sthurlow  *    notice, this list of conditions and the following disclaimer in the
124bff34e3Sthurlow  *    documentation and/or other materials provided with the distribution.
134bff34e3Sthurlow  * 3. All advertising materials mentioning features or use of this software
144bff34e3Sthurlow  *    must display the following acknowledgement:
154bff34e3Sthurlow  *    This product includes software developed by Boris Popov.
164bff34e3Sthurlow  * 4. Neither the name of the author nor the names of any co-contributors
174bff34e3Sthurlow  *    may be used to endorse or promote products derived from this software
184bff34e3Sthurlow  *    without specific prior written permission.
194bff34e3Sthurlow  *
204bff34e3Sthurlow  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
214bff34e3Sthurlow  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224bff34e3Sthurlow  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234bff34e3Sthurlow  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
244bff34e3Sthurlow  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254bff34e3Sthurlow  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264bff34e3Sthurlow  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274bff34e3Sthurlow  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284bff34e3Sthurlow  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294bff34e3Sthurlow  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304bff34e3Sthurlow  * SUCH DAMAGE.
314bff34e3Sthurlow  *
324bff34e3Sthurlow  * $Id: print.c,v 1.1.1.3 2001/07/06 22:38:43 conrad Exp $
334bff34e3Sthurlow  */
344bff34e3Sthurlow 
358329232eSGordon Ross /*
36*adee6784SGordon Ross  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
378329232eSGordon Ross  */
388329232eSGordon Ross 
394bff34e3Sthurlow #include <sys/param.h>
404bff34e3Sthurlow #include <sys/ioctl.h>
414bff34e3Sthurlow #include <sys/time.h>
424bff34e3Sthurlow #include <sys/mount.h>
434bff34e3Sthurlow #include <fcntl.h>
444bff34e3Sthurlow #include <ctype.h>
454bff34e3Sthurlow #include <errno.h>
464bff34e3Sthurlow #include <stdio.h>
474bff34e3Sthurlow #include <string.h>
48430b4c46SGordon Ross #include <strings.h>
494bff34e3Sthurlow #include <stdlib.h>
504bff34e3Sthurlow #include <pwd.h>
514bff34e3Sthurlow #include <grp.h>
524bff34e3Sthurlow #include <unistd.h>
53430b4c46SGordon Ross #include <libintl.h>
544bff34e3Sthurlow 
55613a2f6bSGordon Ross #include <netsmb/smb.h>
564bff34e3Sthurlow #include <netsmb/smb_lib.h>
57430b4c46SGordon Ross 
589c9af259SGordon Ross #include "private.h"
594bff34e3Sthurlow 
60*adee6784SGordon Ross /*
61*adee6784SGordon Ross  * Replacing invalid characters in print job titles:
62*adee6784SGordon Ross  *
63*adee6784SGordon Ross  * The spec. is unclear about what characters are allowed in a
64*adee6784SGordon Ross  * print job title (used with NtCreate) so out of caution this
65*adee6784SGordon Ross  * makes sure the title contains none of the characters that
66*adee6784SGordon Ross  * are known to be illegal in a file name component.
67*adee6784SGordon Ross  */
68*adee6784SGordon Ross static const char invalid_chars[] = SMB_FILENAME_INVALID_CHARS;
69*adee6784SGordon Ross 
704bff34e3Sthurlow int
smb_open_printer(struct smb_ctx * ctx,const char * title,int setuplen,int mode)71430b4c46SGordon Ross smb_open_printer(struct smb_ctx *ctx, const char *title,
72430b4c46SGordon Ross 	int setuplen, int mode)
734bff34e3Sthurlow {
74430b4c46SGordon Ross 	smbioc_printjob_t ioc;
75*adee6784SGordon Ross 	char *p;
76*adee6784SGordon Ross 	int err, tlen;
77*adee6784SGordon Ross 	int new_fd = -1;
78430b4c46SGordon Ross 	int32_t from_fd;
794bff34e3Sthurlow 
80430b4c46SGordon Ross 	tlen = strlen(title);
81430b4c46SGordon Ross 	if (tlen >= SMBIOC_MAX_NAME)
82430b4c46SGordon Ross 		return (EINVAL);
83613a2f6bSGordon Ross 
84430b4c46SGordon Ross 	/*
85430b4c46SGordon Ross 	 * Will represent this SMB-level open as a new
86430b4c46SGordon Ross 	 * open device handle.  Get one, then duplicate
87430b4c46SGordon Ross 	 * the driver session and tree bindings.
88430b4c46SGordon Ross 	 */
89430b4c46SGordon Ross 	new_fd = smb_open_driver();
90430b4c46SGordon Ross 	if (new_fd < 0)
91430b4c46SGordon Ross 		return (errno);
92430b4c46SGordon Ross 	from_fd = ctx->ct_dev_fd;
938329232eSGordon Ross 	if (nsmb_ioctl(new_fd, SMBIOC_DUP_DEV, &from_fd) == -1) {
94430b4c46SGordon Ross 		err = errno;
95430b4c46SGordon Ross 		goto errout;
964bff34e3Sthurlow 	}
974bff34e3Sthurlow 
98613a2f6bSGordon Ross 	/*
99430b4c46SGordon Ross 	 * Do the SMB-level open with the new dev handle.
100613a2f6bSGordon Ross 	 */
101430b4c46SGordon Ross 	bzero(&ioc, sizeof (ioc));
102430b4c46SGordon Ross 	ioc.ioc_setuplen = setuplen;
103430b4c46SGordon Ross 	ioc.ioc_prmode = mode;
104430b4c46SGordon Ross 	strlcpy(ioc.ioc_title, title, SMBIOC_MAX_NAME);
1054bff34e3Sthurlow 
106*adee6784SGordon Ross 	/*
107*adee6784SGordon Ross 	 * The title is used in NtCreate so sanitize by
108*adee6784SGordon Ross 	 * replacing any illegal chars with spaces.
109*adee6784SGordon Ross 	 */
110*adee6784SGordon Ross 	for (p = ioc.ioc_title; *p != '\0'; p++)
111*adee6784SGordon Ross 		if (strchr(invalid_chars, *p) != NULL)
112*adee6784SGordon Ross 			*p = ' ';
113*adee6784SGordon Ross 
1148329232eSGordon Ross 	if (nsmb_ioctl(new_fd, SMBIOC_PRINTJOB, &ioc) == -1) {
115430b4c46SGordon Ross 		err = errno;
116430b4c46SGordon Ross 		goto errout;
117430b4c46SGordon Ross 	}
118613a2f6bSGordon Ross 
119430b4c46SGordon Ross 	return (new_fd);
120613a2f6bSGordon Ross 
121430b4c46SGordon Ross errout:
1228329232eSGordon Ross 	nsmb_close(new_fd);
123430b4c46SGordon Ross 	errno = err;
124430b4c46SGordon Ross 	return (-1);
1254bff34e3Sthurlow }
126