xref: /titanic_52/usr/src/lib/libsmbfs/smb/file.c (revision 430b4c467020edf2445feb0c21db01c88b86243a)
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 /*
36*430b4c46SGordon Ross  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
37613a2f6bSGordon Ross  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
389c9af259SGordon Ross  * Use is subject to license terms.
399c9af259SGordon Ross  */
404bff34e3Sthurlow 
414bff34e3Sthurlow #include <sys/param.h>
424bff34e3Sthurlow #include <sys/ioctl.h>
434bff34e3Sthurlow #include <sys/time.h>
444bff34e3Sthurlow #include <sys/mount.h>
454bff34e3Sthurlow #include <fcntl.h>
464bff34e3Sthurlow #include <ctype.h>
474bff34e3Sthurlow #include <errno.h>
484bff34e3Sthurlow #include <stdio.h>
494bff34e3Sthurlow #include <string.h>
504bff34e3Sthurlow #include <strings.h>
514bff34e3Sthurlow #include <stdlib.h>
524bff34e3Sthurlow #include <pwd.h>
534bff34e3Sthurlow #include <grp.h>
544bff34e3Sthurlow #include <unistd.h>
559c9af259SGordon Ross #include <libintl.h>
564bff34e3Sthurlow 
574bff34e3Sthurlow #include <sys/types.h>
589c9af259SGordon Ross #include <sys/file.h>
594bff34e3Sthurlow 
60613a2f6bSGordon Ross #include <netsmb/smb.h>
614bff34e3Sthurlow #include <netsmb/smb_lib.h>
624bff34e3Sthurlow 
639c9af259SGordon Ross #include "private.h"
649c9af259SGordon Ross 
654bff34e3Sthurlow int
66*430b4c46SGordon Ross smb_fh_close(int fd)
679c9af259SGordon Ross {
68*430b4c46SGordon Ross 	return (close(fd));
699c9af259SGordon Ross }
709c9af259SGordon Ross 
719c9af259SGordon Ross int
729c9af259SGordon Ross smb_fh_ntcreate(
739c9af259SGordon Ross 	struct smb_ctx *ctx, char *path,
74*430b4c46SGordon Ross 	int req_acc, int efattr, int share_acc,
75*430b4c46SGordon Ross 	int open_disp, int create_opts)
769c9af259SGordon Ross {
77*430b4c46SGordon Ross 	smbioc_ntcreate_t ioc;
78*430b4c46SGordon Ross 	int err, nmlen;
79*430b4c46SGordon Ross 	int new_fd = -1;
80*430b4c46SGordon Ross 	int32_t from_fd;
819c9af259SGordon Ross 
82*430b4c46SGordon Ross 	nmlen = strlen(path);
83*430b4c46SGordon Ross 	if (nmlen >= SMBIOC_MAX_NAME) {
84*430b4c46SGordon Ross 		err = EINVAL;
85*430b4c46SGordon Ross 		goto errout;
86613a2f6bSGordon Ross 	}
879c9af259SGordon Ross 
889c9af259SGordon Ross 	/*
89*430b4c46SGordon Ross 	 * Will represent this SMB-level open as a new
90*430b4c46SGordon Ross 	 * open device handle.  Get one, then duplicate
91*430b4c46SGordon Ross 	 * the driver session and tree bindings.
929c9af259SGordon Ross 	 */
93*430b4c46SGordon Ross 	new_fd = smb_open_driver();
94*430b4c46SGordon Ross 	if (new_fd < 0) {
95*430b4c46SGordon Ross 		err = errno;
96*430b4c46SGordon Ross 		goto errout;
979c9af259SGordon Ross 	}
98*430b4c46SGordon Ross 	from_fd = ctx->ct_dev_fd;
99*430b4c46SGordon Ross 	if (ioctl(new_fd, SMBIOC_DUP_DEV, &from_fd) == -1) {
100*430b4c46SGordon Ross 		err = errno;
101*430b4c46SGordon Ross 		goto errout;
102*430b4c46SGordon Ross 	}
103613a2f6bSGordon Ross 
104*430b4c46SGordon Ross 	/*
105*430b4c46SGordon Ross 	 * Do the SMB-level open with the new dev handle.
106*430b4c46SGordon Ross 	 */
107*430b4c46SGordon Ross 	bzero(&ioc, sizeof (ioc));
108*430b4c46SGordon Ross 	strlcpy(ioc.ioc_name, path, SMBIOC_MAX_NAME);
109*430b4c46SGordon Ross 	ioc.ioc_req_acc = req_acc;
110*430b4c46SGordon Ross 	ioc.ioc_efattr = efattr;
111*430b4c46SGordon Ross 	ioc.ioc_share_acc = share_acc;
112*430b4c46SGordon Ross 	ioc.ioc_open_disp = open_disp;
113*430b4c46SGordon Ross 	ioc.ioc_creat_opts = create_opts;
114*430b4c46SGordon Ross 	if (ioctl(new_fd, SMBIOC_NTCREATE, &ioc) == -1) {
115*430b4c46SGordon Ross 		err = errno;
116*430b4c46SGordon Ross 		goto errout;
117*430b4c46SGordon Ross 	}
1189c9af259SGordon Ross 
119*430b4c46SGordon Ross 	return (new_fd);
1209c9af259SGordon Ross 
121*430b4c46SGordon Ross errout:
122*430b4c46SGordon Ross 	if (new_fd != -1)
123*430b4c46SGordon Ross 		close(new_fd);
124*430b4c46SGordon Ross 	errno = err;
125*430b4c46SGordon Ross 	return (-1);
1269c9af259SGordon Ross }
1279c9af259SGordon Ross 
1289c9af259SGordon Ross /*
1299c9af259SGordon Ross  * Conveinence wrapper for smb_fh_ntcreate
1309c9af259SGordon Ross  * Converts Unix-style open call to NTCreate.
1319c9af259SGordon Ross  */
1329c9af259SGordon Ross int
133*430b4c46SGordon Ross smb_fh_open(struct smb_ctx *ctx, const char *path, int oflag)
1349c9af259SGordon Ross {
135*430b4c46SGordon Ross 	int mode, open_disp, req_acc, share_acc;
1369c9af259SGordon Ross 	char *p, *ntpath = NULL;
137*430b4c46SGordon Ross 	int fd = -1;
138*430b4c46SGordon Ross 
139*430b4c46SGordon Ross 	/*
140*430b4c46SGordon Ross 	 * Convert Unix path to NT (backslashes)
141*430b4c46SGordon Ross 	 */
142*430b4c46SGordon Ross 	ntpath = strdup(path);
143*430b4c46SGordon Ross 	if (ntpath == NULL)
144*430b4c46SGordon Ross 		return (-1);	/* errno was set */
145*430b4c46SGordon Ross 	for (p = ntpath; *p; p++)
146*430b4c46SGordon Ross 		if (*p == '/')
147*430b4c46SGordon Ross 			*p = '\\';
1489c9af259SGordon Ross 
1499c9af259SGordon Ross 	/*
1509c9af259SGordon Ross 	 * Map O_RDONLY, O_WRONLY, O_RDWR
1519c9af259SGordon Ross 	 * to FREAD, FWRITE
1529c9af259SGordon Ross 	 */
1539c9af259SGordon Ross 	mode = (oflag & 3) + 1;
1549c9af259SGordon Ross 
1559c9af259SGordon Ross 	/*
1569c9af259SGordon Ross 	 * Compute requested access, share access.
1579c9af259SGordon Ross 	 */
1589c9af259SGordon Ross 	req_acc = (
1599c9af259SGordon Ross 	    STD_RIGHT_READ_CONTROL_ACCESS |
1609c9af259SGordon Ross 	    STD_RIGHT_SYNCHRONIZE_ACCESS);
1619c9af259SGordon Ross 	share_acc = NTCREATEX_SHARE_ACCESS_NONE;
1629c9af259SGordon Ross 	if (mode & FREAD) {
1639c9af259SGordon Ross 		req_acc |= (
1649c9af259SGordon Ross 		    SA_RIGHT_FILE_READ_DATA |
1659c9af259SGordon Ross 		    SA_RIGHT_FILE_READ_EA |
1669c9af259SGordon Ross 		    SA_RIGHT_FILE_READ_ATTRIBUTES);
1679c9af259SGordon Ross 		share_acc |= NTCREATEX_SHARE_ACCESS_READ;
1689c9af259SGordon Ross 	}
1699c9af259SGordon Ross 	if (mode & FWRITE) {
1709c9af259SGordon Ross 		req_acc |= (
1719c9af259SGordon Ross 		    SA_RIGHT_FILE_WRITE_DATA |
1729c9af259SGordon Ross 		    SA_RIGHT_FILE_APPEND_DATA |
1739c9af259SGordon Ross 		    SA_RIGHT_FILE_WRITE_EA |
1749c9af259SGordon Ross 		    SA_RIGHT_FILE_WRITE_ATTRIBUTES);
1759c9af259SGordon Ross 		share_acc |= NTCREATEX_SHARE_ACCESS_WRITE;
1769c9af259SGordon Ross 	}
1779c9af259SGordon Ross 
1789c9af259SGordon Ross 	/*
1799c9af259SGordon Ross 	 * Compute open disposition
1809c9af259SGordon Ross 	 */
1819c9af259SGordon Ross 	if (oflag & FCREAT) {
1829c9af259SGordon Ross 		/* Creat if necessary. */
1839c9af259SGordon Ross 		if (oflag & FEXCL) {
1849c9af259SGordon Ross 			/* exclusive */
1859c9af259SGordon Ross 			open_disp = NTCREATEX_DISP_CREATE;
1869c9af259SGordon Ross 		} else if (oflag & FTRUNC)
1879c9af259SGordon Ross 			open_disp = NTCREATEX_DISP_OVERWRITE_IF;
1889c9af259SGordon Ross 		else
1899c9af259SGordon Ross 			open_disp = NTCREATEX_DISP_OPEN_IF;
1909c9af259SGordon Ross 	} else {
1919c9af259SGordon Ross 		/* Not creating. */
1929c9af259SGordon Ross 		if (oflag & FTRUNC)
1939c9af259SGordon Ross 			open_disp = NTCREATEX_DISP_OVERWRITE;
1949c9af259SGordon Ross 		else
1959c9af259SGordon Ross 			open_disp = NTCREATEX_DISP_OPEN;
1969c9af259SGordon Ross 	}
1979c9af259SGordon Ross 
198*430b4c46SGordon Ross 	fd = smb_fh_ntcreate(ctx, ntpath,
1999c9af259SGordon Ross 	    req_acc, SMB_EFA_NORMAL, share_acc, open_disp,
200*430b4c46SGordon Ross 	    NTCREATEX_OPTIONS_NON_DIRECTORY_FILE);
2019c9af259SGordon Ross 
202*430b4c46SGordon Ross 	free(ntpath);
203*430b4c46SGordon Ross 	return (fd);
2049c9af259SGordon Ross }
2059c9af259SGordon Ross 
2069c9af259SGordon Ross int
207*430b4c46SGordon Ross smb_fh_read(int fd, off_t offset, size_t count,
2089c9af259SGordon Ross 	char *dst)
2094bff34e3Sthurlow {
2104bff34e3Sthurlow 	struct smbioc_rw rwrq;
2114bff34e3Sthurlow 
2124bff34e3Sthurlow 	bzero(&rwrq, sizeof (rwrq));
213*430b4c46SGordon Ross 	rwrq.ioc_fh = -1;	/* tell driver to supply this */
2144bff34e3Sthurlow 	rwrq.ioc_base = dst;
2154bff34e3Sthurlow 	rwrq.ioc_cnt = count;
2164bff34e3Sthurlow 	rwrq.ioc_offset = offset;
217*430b4c46SGordon Ross 	if (ioctl(fd, SMBIOC_READ, &rwrq) == -1) {
2184bff34e3Sthurlow 		return (-1);
2194bff34e3Sthurlow 	}
2204bff34e3Sthurlow 	return (rwrq.ioc_cnt);
2214bff34e3Sthurlow }
2224bff34e3Sthurlow 
2234bff34e3Sthurlow int
224*430b4c46SGordon Ross smb_fh_write(int fd, off_t offset, size_t count,
2254bff34e3Sthurlow 	const char *src)
2264bff34e3Sthurlow {
2274bff34e3Sthurlow 	struct smbioc_rw rwrq;
2284bff34e3Sthurlow 
2294bff34e3Sthurlow 	bzero(&rwrq, sizeof (rwrq));
230*430b4c46SGordon Ross 	rwrq.ioc_fh = -1;	/* tell driver to supply this */
2314bff34e3Sthurlow 	rwrq.ioc_base = (char *)src;
2324bff34e3Sthurlow 	rwrq.ioc_cnt = count;
2334bff34e3Sthurlow 	rwrq.ioc_offset = offset;
234*430b4c46SGordon Ross 	if (ioctl(fd, SMBIOC_WRITE, &rwrq) == -1) {
2354bff34e3Sthurlow 		return (-1);
2364bff34e3Sthurlow 	}
2374bff34e3Sthurlow 	return (rwrq.ioc_cnt);
2384bff34e3Sthurlow }
2399c9af259SGordon Ross 
2409c9af259SGordon Ross /*
2419c9af259SGordon Ross  * Do a TRANSACT_NAMED_PIPE, which is basically just a
2429c9af259SGordon Ross  * pipe write and pipe read, all in one round trip.
2439c9af259SGordon Ross  *
2449c9af259SGordon Ross  * tdlen, tdata describe the data to send.
2459c9af259SGordon Ross  * rdlen, rdata on input describe the receive buffer,
2469c9af259SGordon Ross  * and on output *rdlen is the received length.
2479c9af259SGordon Ross  */
2489c9af259SGordon Ross int
249*430b4c46SGordon Ross smb_fh_xactnp(int fd,
2509c9af259SGordon Ross 	int tdlen, const char *tdata,	/* transmit */
2519c9af259SGordon Ross 	int *rdlen, char *rdata,	/* receive */
2529c9af259SGordon Ross 	int *more)
2539c9af259SGordon Ross {
2549c9af259SGordon Ross 	int		err, rparamcnt;
2559c9af259SGordon Ross 	uint16_t	setup[2];
2569c9af259SGordon Ross 
2579c9af259SGordon Ross 	setup[0] = TRANS_TRANSACT_NAMED_PIPE;
258*430b4c46SGordon Ross 	setup[1] = 0xFFFF; /* driver replaces this */
2599c9af259SGordon Ross 	rparamcnt = 0;
2609c9af259SGordon Ross 
261*430b4c46SGordon Ross 	err = smb_t2_request(fd, 2, setup, "\\PIPE\\",
2629c9af259SGordon Ross 	    0, NULL,	/* TX paramcnt, params */
2639c9af259SGordon Ross 	    tdlen, (void *)tdata,
2649c9af259SGordon Ross 	    &rparamcnt, NULL,	/* no RX params */
2659c9af259SGordon Ross 	    rdlen, rdata, more);
2669c9af259SGordon Ross 
2679c9af259SGordon Ross 	if (err)
2689c9af259SGordon Ross 		*rdlen = 0;
2699c9af259SGordon Ross 
2709c9af259SGordon Ross 	return (err);
2719c9af259SGordon Ross }
272