Merge commit '7d815089a43a963b49eaddf97e514194ec29805b'
[unleashed.git] / usr / src / lib / libsmbfs / smb / file.c
blob46cd27ec7b175fbc3737365362af7a9fb217dca4
1 /*
2 * Copyright (c) 2000, Boris Popov
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * $Id: file.c,v 1.4 2004/12/13 00:25:21 lindak Exp $
36 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
37 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
38 * Use is subject to license terms.
41 #include <sys/param.h>
42 #include <sys/ioctl.h>
43 #include <sys/time.h>
44 #include <sys/mount.h>
45 #include <fcntl.h>
46 #include <ctype.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <strings.h>
51 #include <stdlib.h>
52 #include <pwd.h>
53 #include <grp.h>
54 #include <unistd.h>
55 #include <libintl.h>
57 #include <sys/types.h>
58 #include <sys/file.h>
60 #include <netsmb/smb.h>
61 #include <netsmb/smb_lib.h>
63 #include "private.h"
65 int
66 smb_fh_close(int fd)
68 return (close(fd));
71 int
72 smb_fh_ntcreate(
73 struct smb_ctx *ctx, char *path,
74 int req_acc, int efattr, int share_acc,
75 int open_disp, int create_opts)
77 smbioc_ntcreate_t ioc;
78 int err, nmlen;
79 int new_fd = -1;
80 int32_t from_fd;
82 nmlen = strlen(path);
83 if (nmlen >= SMBIOC_MAX_NAME) {
84 err = EINVAL;
85 goto errout;
89 * Will represent this SMB-level open as a new
90 * open device handle. Get one, then duplicate
91 * the driver session and tree bindings.
93 new_fd = smb_open_driver();
94 if (new_fd < 0) {
95 err = errno;
96 goto errout;
98 from_fd = ctx->ct_dev_fd;
99 if (ioctl(new_fd, SMBIOC_DUP_DEV, &from_fd) == -1) {
100 err = errno;
101 goto errout;
105 * Do the SMB-level open with the new dev handle.
107 bzero(&ioc, sizeof (ioc));
108 strlcpy(ioc.ioc_name, path, SMBIOC_MAX_NAME);
109 ioc.ioc_req_acc = req_acc;
110 ioc.ioc_efattr = efattr;
111 ioc.ioc_share_acc = share_acc;
112 ioc.ioc_open_disp = open_disp;
113 ioc.ioc_creat_opts = create_opts;
114 if (ioctl(new_fd, SMBIOC_NTCREATE, &ioc) == -1) {
115 err = errno;
116 goto errout;
119 return (new_fd);
121 errout:
122 if (new_fd != -1)
123 close(new_fd);
124 errno = err;
125 return (-1);
129 * Conveinence wrapper for smb_fh_ntcreate
130 * Converts Unix-style open call to NTCreate.
133 smb_fh_open(struct smb_ctx *ctx, const char *path, int oflag)
135 int mode, open_disp, req_acc, share_acc;
136 char *p, *ntpath = NULL;
137 int fd = -1;
140 * Convert Unix path to NT (backslashes)
142 ntpath = strdup(path);
143 if (ntpath == NULL)
144 return (-1); /* errno was set */
145 for (p = ntpath; *p; p++)
146 if (*p == '/')
147 *p = '\\';
149 mode = FFLAGS(oflag) & (FREAD|FWRITE);
152 * Compute requested access, share access.
154 req_acc = (
155 STD_RIGHT_READ_CONTROL_ACCESS |
156 STD_RIGHT_SYNCHRONIZE_ACCESS);
157 share_acc = NTCREATEX_SHARE_ACCESS_NONE;
158 if (mode & FREAD) {
159 req_acc |= (
160 SA_RIGHT_FILE_READ_DATA |
161 SA_RIGHT_FILE_READ_EA |
162 SA_RIGHT_FILE_READ_ATTRIBUTES);
163 share_acc |= NTCREATEX_SHARE_ACCESS_READ;
165 if (mode & FWRITE) {
166 req_acc |= (
167 SA_RIGHT_FILE_WRITE_DATA |
168 SA_RIGHT_FILE_APPEND_DATA |
169 SA_RIGHT_FILE_WRITE_EA |
170 SA_RIGHT_FILE_WRITE_ATTRIBUTES);
171 share_acc |= NTCREATEX_SHARE_ACCESS_WRITE;
175 * Compute open disposition
177 if (oflag & O_CREAT) {
178 /* Creat if necessary. */
179 if (oflag & O_EXCL) {
180 /* exclusive */
181 open_disp = NTCREATEX_DISP_CREATE;
182 } else if (oflag & O_TRUNC)
183 open_disp = NTCREATEX_DISP_OVERWRITE_IF;
184 else
185 open_disp = NTCREATEX_DISP_OPEN_IF;
186 } else {
187 /* Not creating. */
188 if (oflag & O_TRUNC)
189 open_disp = NTCREATEX_DISP_OVERWRITE;
190 else
191 open_disp = NTCREATEX_DISP_OPEN;
194 fd = smb_fh_ntcreate(ctx, ntpath,
195 req_acc, SMB_EFA_NORMAL, share_acc, open_disp,
196 NTCREATEX_OPTIONS_NON_DIRECTORY_FILE);
198 free(ntpath);
199 return (fd);
203 smb_fh_read(int fd, off64_t offset, size_t count,
204 char *dst)
206 struct smbioc_rw rwrq;
208 bzero(&rwrq, sizeof (rwrq));
209 rwrq.ioc_fh = -1; /* tell driver to supply this */
210 rwrq.ioc_base = dst;
211 rwrq.ioc_cnt = count;
212 rwrq.ioc_offset = offset;
213 if (ioctl(fd, SMBIOC_READ, &rwrq) == -1) {
214 return (-1);
216 return (rwrq.ioc_cnt);
220 smb_fh_write(int fd, off64_t offset, size_t count,
221 const char *src)
223 struct smbioc_rw rwrq;
225 bzero(&rwrq, sizeof (rwrq));
226 rwrq.ioc_fh = -1; /* tell driver to supply this */
227 rwrq.ioc_base = (char *)src;
228 rwrq.ioc_cnt = count;
229 rwrq.ioc_offset = offset;
230 if (ioctl(fd, SMBIOC_WRITE, &rwrq) == -1) {
231 return (-1);
233 return (rwrq.ioc_cnt);
237 * Do a TRANSACT_NAMED_PIPE, which is basically just a
238 * pipe write and pipe read, all in one round trip.
240 * tdlen, tdata describe the data to send.
241 * rdlen, rdata on input describe the receive buffer,
242 * and on output *rdlen is the received length.
245 smb_fh_xactnp(int fd,
246 int tdlen, const char *tdata, /* transmit */
247 int *rdlen, char *rdata, /* receive */
248 int *more)
250 int err, rparamcnt;
251 uint16_t setup[2];
253 setup[0] = TRANS_TRANSACT_NAMED_PIPE;
254 setup[1] = 0xFFFF; /* driver replaces this */
255 rparamcnt = 0;
257 err = smb_t2_request(fd, 2, setup, "\\PIPE\\",
258 0, NULL, /* TX paramcnt, params */
259 tdlen, (void *)tdata,
260 &rparamcnt, NULL, /* no RX params */
261 rdlen, rdata, more);
263 if (err)
264 *rdlen = 0;
266 return (err);