xref: /illumos-gate/usr/src/lib/libsmbfs/smb/file.c (revision adee6784)
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: file.c,v 1.4 2004/12/13 00:25:21 lindak Exp $
334bff34e3Sthurlow  */
344bff34e3Sthurlow 
359c9af259SGordon Ross /*
36613a2f6bSGordon Ross  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
379c9af259SGordon Ross  * Use is subject to license terms.
38*40c0e231SGordon Ross  *
39*40c0e231SGordon Ross  * Copyright 2018 Nexenta Systems, Inc.  All rights reserved.
409c9af259SGordon Ross  */
414bff34e3Sthurlow 
424bff34e3Sthurlow #include <sys/param.h>
434bff34e3Sthurlow #include <sys/ioctl.h>
444bff34e3Sthurlow #include <sys/time.h>
454bff34e3Sthurlow #include <sys/mount.h>
464bff34e3Sthurlow #include <fcntl.h>
474bff34e3Sthurlow #include <ctype.h>
484bff34e3Sthurlow #include <errno.h>
494bff34e3Sthurlow #include <stdio.h>
504bff34e3Sthurlow #include <string.h>
514bff34e3Sthurlow #include <strings.h>
524bff34e3Sthurlow #include <stdlib.h>
534bff34e3Sthurlow #include <pwd.h>
544bff34e3Sthurlow #include <grp.h>
554bff34e3Sthurlow #include <unistd.h>
569c9af259SGordon Ross #include <libintl.h>
574bff34e3Sthurlow 
584bff34e3Sthurlow #include <sys/types.h>
599c9af259SGordon Ross #include <sys/file.h>
604bff34e3Sthurlow 
61613a2f6bSGordon Ross #include <netsmb/smb.h>
624bff34e3Sthurlow #include <netsmb/smb_lib.h>
634bff34e3Sthurlow 
649c9af259SGordon Ross #include "private.h"
659c9af259SGordon Ross 
66*40c0e231SGordon Ross /*
67*40c0e231SGordon Ross  * It's not actually necessary to call the CLOSEFH ioctl, but doing it
68*40c0e231SGordon Ross  * makes debugging a little easier.  If we were to skip the ioctl,
69*40c0e231SGordon Ross  * nsmb_close would cleanup the handle, here or in process exit.
70*40c0e231SGordon Ross  */
714bff34e3Sthurlow int
smb_fh_close(int fd)72430b4c46SGordon Ross smb_fh_close(int fd)
739c9af259SGordon Ross {
74*40c0e231SGordon Ross 	(void) nsmb_ioctl(fd, SMBIOC_CLOSEFH, NULL);
758329232eSGordon Ross 	return (nsmb_close(fd));
769c9af259SGordon Ross }
779c9af259SGordon Ross 
789c9af259SGordon Ross int
smb_fh_ntcreate(struct smb_ctx * ctx,char * path,int req_acc,int efattr,int share_acc,int open_disp,int create_opts)799c9af259SGordon Ross smb_fh_ntcreate(
809c9af259SGordon Ross 	struct smb_ctx *ctx, char *path,
81430b4c46SGordon Ross 	int req_acc, int efattr, int share_acc,
82430b4c46SGordon Ross 	int open_disp, int create_opts)
839c9af259SGordon Ross {
84430b4c46SGordon Ross 	smbioc_ntcreate_t ioc;
85430b4c46SGordon Ross 	int err, nmlen;
86430b4c46SGordon Ross 	int new_fd = -1;
87430b4c46SGordon Ross 	int32_t from_fd;
88430b4c46SGordon Ross 
89430b4c46SGordon Ross 	nmlen = strlen(path);
90430b4c46SGordon Ross 	if (nmlen >= SMBIOC_MAX_NAME) {
91430b4c46SGordon Ross 		err = EINVAL;
92430b4c46SGordon Ross 		goto errout;
93613a2f6bSGordon Ross 	}
949c9af259SGordon Ross 
959c9af259SGordon Ross 	/*
96430b4c46SGordon Ross 	 * Will represent this SMB-level open as a new
97430b4c46SGordon Ross 	 * open device handle.  Get one, then duplicate
98430b4c46SGordon Ross 	 * the driver session and tree bindings.
999c9af259SGordon Ross 	 */
100430b4c46SGordon Ross 	new_fd = smb_open_driver();
101430b4c46SGordon Ross 	if (new_fd < 0) {
102430b4c46SGordon Ross 		err = errno;
103430b4c46SGordon Ross 		goto errout;
104430b4c46SGordon Ross 	}
105430b4c46SGordon Ross 	from_fd = ctx->ct_dev_fd;
1068329232eSGordon Ross 	if (nsmb_ioctl(new_fd, SMBIOC_DUP_DEV, &from_fd) == -1) {
107430b4c46SGordon Ross 		err = errno;
108430b4c46SGordon Ross 		goto errout;
1099c9af259SGordon Ross 	}
110613a2f6bSGordon Ross 
111430b4c46SGordon Ross 	/*
112430b4c46SGordon Ross 	 * Do the SMB-level open with the new dev handle.
113430b4c46SGordon Ross 	 */
114430b4c46SGordon Ross 	bzero(&ioc, sizeof (ioc));
115430b4c46SGordon Ross 	strlcpy(ioc.ioc_name, path, SMBIOC_MAX_NAME);
116430b4c46SGordon Ross 	ioc.ioc_req_acc = req_acc;
117430b4c46SGordon Ross 	ioc.ioc_efattr = efattr;
118430b4c46SGordon Ross 	ioc.ioc_share_acc = share_acc;
119430b4c46SGordon Ross 	ioc.ioc_open_disp = open_disp;
120430b4c46SGordon Ross 	ioc.ioc_creat_opts = create_opts;
1218329232eSGordon Ross 	if (nsmb_ioctl(new_fd, SMBIOC_NTCREATE, &ioc) == -1) {
122430b4c46SGordon Ross 		err = errno;
123430b4c46SGordon Ross 		goto errout;
124430b4c46SGordon Ross 	}
1259c9af259SGordon Ross 
126430b4c46SGordon Ross 	return (new_fd);
1279c9af259SGordon Ross 
128430b4c46SGordon Ross errout:
129430b4c46SGordon Ross 	if (new_fd != -1)
1308329232eSGordon Ross 		nsmb_close(new_fd);
131430b4c46SGordon Ross 	errno = err;
132430b4c46SGordon Ross 	return (-1);
1339c9af259SGordon Ross }
1349c9af259SGordon Ross 
1359c9af259SGordon Ross /*
1369c9af259SGordon Ross  * Conveinence wrapper for smb_fh_ntcreate
1379c9af259SGordon Ross  * Converts Unix-style open call to NTCreate.
1389c9af259SGordon Ross  */
1399c9af259SGordon Ross int
smb_fh_open(struct smb_ctx * ctx,const char * path,int oflag)140430b4c46SGordon Ross smb_fh_open(struct smb_ctx *ctx, const char *path, int oflag)
1419c9af259SGordon Ross {
142430b4c46SGordon Ross 	int mode, open_disp, req_acc, share_acc;
1439c9af259SGordon Ross 	char *p, *ntpath = NULL;
144430b4c46SGordon Ross 	int fd = -1;
145430b4c46SGordon Ross 
146430b4c46SGordon Ross 	/*
147430b4c46SGordon Ross 	 * Convert Unix path to NT (backslashes)
148430b4c46SGordon Ross 	 */
149430b4c46SGordon Ross 	ntpath = strdup(path);
150430b4c46SGordon Ross 	if (ntpath == NULL)
151430b4c46SGordon Ross 		return (-1);	/* errno was set */
152430b4c46SGordon Ross 	for (p = ntpath; *p; p++)
153430b4c46SGordon Ross 		if (*p == '/')
154430b4c46SGordon Ross 			*p = '\\';
1559c9af259SGordon Ross 
1569c9af259SGordon Ross 	/*
1579c9af259SGordon Ross 	 * Map O_RDONLY, O_WRONLY, O_RDWR
1589c9af259SGordon Ross 	 * to FREAD, FWRITE
1599c9af259SGordon Ross 	 */
1609c9af259SGordon Ross 	mode = (oflag & 3) + 1;
1619c9af259SGordon Ross 
1629c9af259SGordon Ross 	/*
1639c9af259SGordon Ross 	 * Compute requested access, share access.
1649c9af259SGordon Ross 	 */
1659c9af259SGordon Ross 	req_acc = (
1669c9af259SGordon Ross 	    STD_RIGHT_READ_CONTROL_ACCESS |
1679c9af259SGordon Ross 	    STD_RIGHT_SYNCHRONIZE_ACCESS);
1689c9af259SGordon Ross 	share_acc = NTCREATEX_SHARE_ACCESS_NONE;
1699c9af259SGordon Ross 	if (mode & FREAD) {
1709c9af259SGordon Ross 		req_acc |= (
1719c9af259SGordon Ross 		    SA_RIGHT_FILE_READ_DATA |
1729c9af259SGordon Ross 		    SA_RIGHT_FILE_READ_EA |
1739c9af259SGordon Ross 		    SA_RIGHT_FILE_READ_ATTRIBUTES);
1749c9af259SGordon Ross 		share_acc |= NTCREATEX_SHARE_ACCESS_READ;
1759c9af259SGordon Ross 	}
1769c9af259SGordon Ross 	if (mode & FWRITE) {
1779c9af259SGordon Ross 		req_acc |= (
1789c9af259SGordon Ross 		    SA_RIGHT_FILE_WRITE_DATA |
1799c9af259SGordon Ross 		    SA_RIGHT_FILE_APPEND_DATA |
1809c9af259SGordon Ross 		    SA_RIGHT_FILE_WRITE_EA |
1819c9af259SGordon Ross 		    SA_RIGHT_FILE_WRITE_ATTRIBUTES);
1829c9af259SGordon Ross 		share_acc |= NTCREATEX_SHARE_ACCESS_WRITE;
1839c9af259SGordon Ross 	}
1849c9af259SGordon Ross 
1859c9af259SGordon Ross 	/*
1869c9af259SGordon Ross 	 * Compute open disposition
1879c9af259SGordon Ross 	 */
1889c9af259SGordon Ross 	if (oflag & FCREAT) {
1899c9af259SGordon Ross 		/* Creat if necessary. */
1909c9af259SGordon Ross 		if (oflag & FEXCL) {
1919c9af259SGordon Ross 			/* exclusive */
1929c9af259SGordon Ross 			open_disp = NTCREATEX_DISP_CREATE;
1939c9af259SGordon Ross 		} else if (oflag & FTRUNC)
1949c9af259SGordon Ross 			open_disp = NTCREATEX_DISP_OVERWRITE_IF;
1959c9af259SGordon Ross 		else
1969c9af259SGordon Ross 			open_disp = NTCREATEX_DISP_OPEN_IF;
1979c9af259SGordon Ross 	} else {
1989c9af259SGordon Ross 		/* Not creating. */
1999c9af259SGordon Ross 		if (oflag & FTRUNC)
2009c9af259SGordon Ross 			open_disp = NTCREATEX_DISP_OVERWRITE;
2019c9af259SGordon Ross 		else
2029c9af259SGordon Ross 			open_disp = NTCREATEX_DISP_OPEN;
2039c9af259SGordon Ross 	}
2049c9af259SGordon Ross 
205430b4c46SGordon Ross 	fd = smb_fh_ntcreate(ctx, ntpath,
2069c9af259SGordon Ross 	    req_acc, SMB_EFA_NORMAL, share_acc, open_disp,
207430b4c46SGordon Ross 	    NTCREATEX_OPTIONS_NON_DIRECTORY_FILE);
2089c9af259SGordon Ross 
209430b4c46SGordon Ross 	free(ntpath);
210430b4c46SGordon Ross 	return (fd);
2119c9af259SGordon Ross }
2129c9af259SGordon Ross 
2139c9af259SGordon Ross int
smb_fh_read(int fd,off64_t offset,size_t count,char * dst)2143299f39fSGordon Ross smb_fh_read(int fd, off64_t offset, size_t count,
2159c9af259SGordon Ross 	char *dst)
2164bff34e3Sthurlow {
2174bff34e3Sthurlow 	struct smbioc_rw rwrq;
2184bff34e3Sthurlow 
2194bff34e3Sthurlow 	bzero(&rwrq, sizeof (rwrq));
2204bff34e3Sthurlow 	rwrq.ioc_base = dst;
2214bff34e3Sthurlow 	rwrq.ioc_cnt = count;
2224bff34e3Sthurlow 	rwrq.ioc_offset = offset;
2238329232eSGordon Ross 	if (nsmb_ioctl(fd, SMBIOC_READ, &rwrq) == -1) {
2244bff34e3Sthurlow 		return (-1);
2254bff34e3Sthurlow 	}
2264bff34e3Sthurlow 	return (rwrq.ioc_cnt);
2274bff34e3Sthurlow }
2284bff34e3Sthurlow 
2294bff34e3Sthurlow int
smb_fh_write(int fd,off64_t offset,size_t count,const char * src)2303299f39fSGordon Ross smb_fh_write(int fd, off64_t offset, size_t count,
2314bff34e3Sthurlow 	const char *src)
2324bff34e3Sthurlow {
2334bff34e3Sthurlow 	struct smbioc_rw rwrq;
2344bff34e3Sthurlow 
2354bff34e3Sthurlow 	bzero(&rwrq, sizeof (rwrq));
2364bff34e3Sthurlow 	rwrq.ioc_base = (char *)src;
2374bff34e3Sthurlow 	rwrq.ioc_cnt = count;
2384bff34e3Sthurlow 	rwrq.ioc_offset = offset;
2398329232eSGordon Ross 	if (nsmb_ioctl(fd, SMBIOC_WRITE, &rwrq) == -1) {
2404bff34e3Sthurlow 		return (-1);
2414bff34e3Sthurlow 	}
2424bff34e3Sthurlow 	return (rwrq.ioc_cnt);
2434bff34e3Sthurlow }
2449c9af259SGordon Ross 
2459c9af259SGordon Ross /*
2469c9af259SGordon Ross  * Do a TRANSACT_NAMED_PIPE, which is basically just a
2479c9af259SGordon Ross  * pipe write and pipe read, all in one round trip.
2489c9af259SGordon Ross  *
2499c9af259SGordon Ross  * tdlen, tdata describe the data to send.
2509c9af259SGordon Ross  * rdlen, rdata on input describe the receive buffer,
2519c9af259SGordon Ross  * and on output *rdlen is the received length.
2529c9af259SGordon Ross  */
2539c9af259SGordon Ross int
smb_fh_xactnp(int fd,int tdlen,const char * tdata,int * rdlen,char * rdata,int * more)254430b4c46SGordon Ross smb_fh_xactnp(int fd,
2559c9af259SGordon Ross 	int tdlen, const char *tdata,	/* transmit */
2569c9af259SGordon Ross 	int *rdlen, char *rdata,	/* receive */
2579c9af259SGordon Ross 	int *more)
2589c9af259SGordon Ross {
259*40c0e231SGordon Ross 	smbioc_xnp_t	ioc;
2609c9af259SGordon Ross 
261*40c0e231SGordon Ross 	/* this gets copyin & copyout */
262*40c0e231SGordon Ross 	bzero(&ioc, sizeof (ioc));
263*40c0e231SGordon Ross 	ioc.ioc_tdlen = tdlen;
264*40c0e231SGordon Ross 	ioc.ioc_rdlen = *rdlen;
265*40c0e231SGordon Ross 	ioc.ioc_more = 0;
266*40c0e231SGordon Ross 	ioc.ioc_tdata = (char *)tdata;
267*40c0e231SGordon Ross 	ioc.ioc_rdata = rdata;
2689c9af259SGordon Ross 
269*40c0e231SGordon Ross 	if (nsmb_ioctl(fd, SMBIOC_XACTNP, &ioc) == -1) {
2709c9af259SGordon Ross 		*rdlen = 0;
271*40c0e231SGordon Ross 		return (-1);
272*40c0e231SGordon Ross 	}
273*40c0e231SGordon Ross 
274*40c0e231SGordon Ross 	*rdlen = ioc.ioc_rdlen;
275*40c0e231SGordon Ross 	*more  = ioc.ioc_more;
2769c9af259SGordon Ross 
277*40c0e231SGordon Ross 	return (0);
2789c9af259SGordon Ross }
279