2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2002
6 Copyright (C) James Myers 2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /****************************************************************************
26 Hard/Symlink a file (UNIX extensions).
27 ****************************************************************************/
29 static BOOL cli_link_internal(struct cli_state *cli,
30 const char *fname_src,
31 const char *fname_dst, BOOL hard_link)
33 union smb_setfileinfo parms;
37 parms.generic.level = SMB_SFILEINFO_UNIX_HLINK;
38 parms.unix_hlink.file.fname = fname_src;
39 parms.unix_hlink.in.link_dest = fname_dst;
41 parms.generic.level = SMB_SFILEINFO_UNIX_LINK;
42 parms.unix_link.file.fname = fname_src;
43 parms.unix_link.in.link_dest = fname_dst;
46 status = smb_raw_setpathinfo(cli->tree, &parms);
48 return NT_STATUS_IS_OK(status);
51 /****************************************************************************
52 Map standard UNIX permissions onto wire representations.
53 ****************************************************************************/
54 static uint32 unix_perms_to_wire(mode_t perms)
58 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
59 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
60 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
61 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
62 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
63 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
64 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
65 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
66 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
68 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
71 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
74 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
79 /****************************************************************************
80 Symlink a file (UNIX extensions).
81 ****************************************************************************/
82 BOOL cli_unix_symlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
84 return cli_link_internal(cli, fname_src, fname_dst, False);
87 /****************************************************************************
88 Hard a file (UNIX extensions).
89 ****************************************************************************/
90 BOOL cli_unix_hardlink(struct cli_state *cli, const char *fname_src, const char *fname_dst)
92 return cli_link_internal(cli, fname_src, fname_dst, True);
96 /****************************************************************************
97 Chmod or chown a file internal (UNIX extensions).
98 ****************************************************************************/
99 static BOOL cli_unix_chmod_chown_internal(struct cli_state *cli, const char *fname,
100 uint32 mode, uint32 uid, uint32 gid)
102 union smb_setfileinfo parms;
105 parms.generic.level = SMB_SFILEINFO_UNIX_BASIC;
106 parms.unix_basic.file.fname = fname;
107 parms.unix_basic.in.uid = uid;
108 parms.unix_basic.in.gid = gid;
109 parms.unix_basic.in.mode = mode;
111 status = smb_raw_setpathinfo(cli->tree, &parms);
113 return NT_STATUS_IS_OK(status);
116 /****************************************************************************
117 chmod a file (UNIX extensions).
118 ****************************************************************************/
120 BOOL cli_unix_chmod(struct cli_state *cli, const char *fname, mode_t mode)
122 return cli_unix_chmod_chown_internal(cli, fname,
123 unix_perms_to_wire(mode), SMB_UID_NO_CHANGE, SMB_GID_NO_CHANGE);
126 /****************************************************************************
127 chown a file (UNIX extensions).
128 ****************************************************************************/
129 BOOL cli_unix_chown(struct cli_state *cli, const char *fname, uid_t uid, gid_t gid)
131 return cli_unix_chmod_chown_internal(cli, fname, SMB_MODE_NO_CHANGE, (uint32)uid, (uint32)gid);
135 /****************************************************************************
137 ****************************************************************************/
138 BOOL cli_rename(struct cli_state *cli, const char *fname_src, const char *fname_dst)
140 struct smb_rename parms;
142 parms.in.attrib = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
143 parms.in.pattern1 = fname_src;
144 parms.in.pattern2 = fname_dst;
145 return NT_STATUS_IS_OK(smb_raw_rename(cli->tree, &parms));
149 /****************************************************************************
151 ****************************************************************************/
152 BOOL cli_unlink(struct cli_state *cli, const char *fname)
154 struct smb_unlink parms;
156 parms.in.pattern = fname;
157 if (strchr(fname, '*')) {
158 parms.in.attrib = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
160 parms.in.attrib = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_DIRECTORY;
162 return NT_STATUS_IS_OK(smb_raw_unlink(cli->tree, &parms));
165 /****************************************************************************
167 ****************************************************************************/
168 BOOL cli_mkdir(struct cli_state *cli, const char *dname)
170 union smb_mkdir parms;
172 parms.mkdir.level = RAW_MKDIR_MKDIR;
173 parms.mkdir.in.path = dname;
175 return NT_STATUS_IS_OK(smb_raw_mkdir(cli->tree, &parms));
179 /****************************************************************************
181 ****************************************************************************/
182 BOOL cli_rmdir(struct cli_state *cli, const char *dname)
184 struct smb_rmdir parms;
186 parms.in.path = dname;
187 return NT_STATUS_IS_OK(smb_raw_rmdir(cli->tree, &parms));
191 /****************************************************************************
192 Set or clear the delete on close flag.
193 ****************************************************************************/
194 BOOL cli_nt_delete_on_close(struct cli_state *cli, int fnum, BOOL flag)
196 union smb_setfileinfo parms;
199 parms.disposition_info.level = RAW_SFILEINFO_DISPOSITION_INFO;
200 parms.disposition_info.file.fnum = fnum;
201 parms.disposition_info.in.delete_on_close = flag;
203 status = smb_raw_setfileinfo(cli->tree, &parms);
205 return NT_STATUS_IS_OK(status);
209 /****************************************************************************
210 Create/open a file - exposing the full horror of the NT API :-).
211 Used in CIFS-on-CIFS NTVFS.
212 ****************************************************************************/
213 int cli_nt_create_full(struct cli_state *cli, const char *fname,
214 uint32 CreatFlags, uint32 DesiredAccess,
215 uint32 FileAttributes, uint32 ShareAccess,
216 uint32 CreateDisposition, uint32 CreateOptions,
219 union smb_open open_parms;
223 mem_ctx = talloc_init("raw_open");
224 if (!mem_ctx) return -1;
226 open_parms.ntcreatex.level = RAW_OPEN_NTCREATEX;
227 open_parms.ntcreatex.in.flags = CreatFlags;
228 open_parms.ntcreatex.in.root_fid = 0;
229 open_parms.ntcreatex.in.access_mask = DesiredAccess;
230 open_parms.ntcreatex.in.file_attr = FileAttributes;
231 open_parms.ntcreatex.in.alloc_size = 0;
232 open_parms.ntcreatex.in.share_access = ShareAccess;
233 open_parms.ntcreatex.in.open_disposition = CreateDisposition;
234 open_parms.ntcreatex.in.create_options = CreateOptions;
235 open_parms.ntcreatex.in.impersonation = 0;
236 open_parms.ntcreatex.in.security_flags = SecurityFlags;
237 open_parms.ntcreatex.in.fname = fname;
239 status = smb_raw_open(cli->tree, mem_ctx, &open_parms);
240 talloc_destroy(mem_ctx);
242 if (NT_STATUS_IS_OK(status)) {
243 return open_parms.ntcreatex.out.fnum;
250 /****************************************************************************
251 Open a file (using SMBopenx)
252 WARNING: if you open with O_WRONLY then getattrE won't work!
253 ****************************************************************************/
254 int cli_open(struct cli_state *cli, const char *fname, int flags, int share_mode)
256 union smb_open open_parms;
258 unsigned accessmode=0;
262 mem_ctx = talloc_init("raw_open");
263 if (!mem_ctx) return -1;
265 if (flags & O_CREAT) {
266 openfn |= OPENX_OPEN_FUNC_CREATE;
268 if (!(flags & O_EXCL)) {
269 if (flags & O_TRUNC) {
270 openfn |= OPENX_OPEN_FUNC_TRUNC;
272 openfn |= OPENX_OPEN_FUNC_OPEN;
276 accessmode = (share_mode<<OPENX_MODE_DENY_SHIFT);
278 if ((flags & O_ACCMODE) == O_RDWR) {
279 accessmode |= OPENX_MODE_ACCESS_RDWR;
280 } else if ((flags & O_ACCMODE) == O_WRONLY) {
281 accessmode |= OPENX_MODE_ACCESS_WRITE;
285 if ((flags & O_SYNC) == O_SYNC) {
286 accessmode |= OPENX_MODE_WRITE_THRU;
290 if (share_mode == DENY_FCB) {
291 accessmode = OPENX_MODE_ACCESS_FCB | OPENX_MODE_DENY_FCB;
294 open_parms.openx.level = RAW_OPEN_OPENX;
295 open_parms.openx.in.flags = 0;
296 open_parms.openx.in.open_mode = accessmode;
297 open_parms.openx.in.search_attrs = FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN;
298 open_parms.openx.in.file_attrs = 0;
299 open_parms.openx.in.write_time = 0;
300 open_parms.openx.in.open_func = openfn;
301 open_parms.openx.in.size = 0;
302 open_parms.openx.in.timeout = 0;
303 open_parms.openx.in.fname = fname;
305 status = smb_raw_open(cli->tree, mem_ctx, &open_parms);
306 talloc_destroy(mem_ctx);
308 if (NT_STATUS_IS_OK(status)) {
309 return open_parms.openx.out.fnum;
316 /****************************************************************************
318 ****************************************************************************/
319 BOOL cli_close(struct cli_state *cli, int fnum)
321 union smb_close close_parms;
324 close_parms.close.level = RAW_CLOSE_CLOSE;
325 close_parms.close.in.fnum = fnum;
326 close_parms.close.in.write_time = 0;
327 status = smb_raw_close(cli->tree, &close_parms);
328 return NT_STATUS_IS_OK(status);
331 /****************************************************************************
332 send a lock with a specified locktype
333 this is used for testing LOCKING_ANDX_CANCEL_LOCK
334 ****************************************************************************/
335 NTSTATUS cli_locktype(struct cli_state *cli, int fnum,
336 uint32 offset, uint32 len, int timeout, unsigned char locktype)
338 union smb_lock parms;
339 struct smb_lock_entry lock[1];
342 parms.lockx.level = RAW_LOCK_LOCKX;
343 parms.lockx.in.fnum = fnum;
344 parms.lockx.in.mode = locktype;
345 parms.lockx.in.timeout = timeout;
346 parms.lockx.in.ulock_cnt = 0;
347 parms.lockx.in.lock_cnt = 1;
348 lock[0].pid = cli->session->pid;
349 lock[0].offset = offset;
351 parms.lockx.in.locks = &lock[0];
353 status = smb_raw_lock(cli->tree, &parms);
359 /****************************************************************************
361 ****************************************************************************/
362 BOOL cli_lock(struct cli_state *cli, int fnum,
363 uint32 offset, uint32 len, int timeout, enum brl_type lock_type)
365 union smb_lock parms;
366 struct smb_lock_entry lock[1];
369 parms.lockx.level = RAW_LOCK_LOCKX;
370 parms.lockx.in.fnum = fnum;
371 parms.lockx.in.mode = (lock_type == READ_LOCK? 1 : 0);
372 parms.lockx.in.timeout = timeout;
373 parms.lockx.in.ulock_cnt = 0;
374 parms.lockx.in.lock_cnt = 1;
375 lock[0].pid = cli->session->pid;
376 lock[0].offset = offset;
378 parms.lockx.in.locks = &lock[0];
380 status = smb_raw_lock(cli->tree, &parms);
382 return NT_STATUS_IS_OK(status);
386 /****************************************************************************
388 ****************************************************************************/
389 BOOL cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len)
391 union smb_lock parms;
392 struct smb_lock_entry lock[1];
395 parms.lockx.level = RAW_LOCK_LOCKX;
396 parms.lockx.in.fnum = fnum;
397 parms.lockx.in.mode = 0;
398 parms.lockx.in.timeout = 0;
399 parms.lockx.in.ulock_cnt = 1;
400 parms.lockx.in.lock_cnt = 0;
401 lock[0].pid = cli->session->pid;
402 lock[0].offset = offset;
404 parms.lockx.in.locks = &lock[0];
406 status = smb_raw_lock(cli->tree, &parms);
407 return NT_STATUS_IS_OK(status);
411 /****************************************************************************
412 Lock a file with 64 bit offsets.
413 ****************************************************************************/
414 BOOL cli_lock64(struct cli_state *cli, int fnum,
415 SMB_OFF_T offset, SMB_OFF_T len, int timeout, enum brl_type lock_type)
417 union smb_lock parms;
419 struct smb_lock_entry lock[1];
422 if (!(cli->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
423 return cli_lock(cli, fnum, offset, len, timeout, lock_type);
426 parms.lockx.level = RAW_LOCK_LOCKX;
427 parms.lockx.in.fnum = fnum;
429 ltype = (lock_type == READ_LOCK? 1 : 0);
430 ltype |= LOCKING_ANDX_LARGE_FILES;
431 parms.lockx.in.mode = ltype;
432 parms.lockx.in.timeout = timeout;
433 parms.lockx.in.ulock_cnt = 0;
434 parms.lockx.in.lock_cnt = 1;
435 lock[0].pid = cli->session->pid;
436 lock[0].offset = offset;
438 parms.lockx.in.locks = &lock[0];
440 status = smb_raw_lock(cli->tree, &parms);
442 return NT_STATUS_IS_OK(status);
446 /****************************************************************************
447 Unlock a file with 64 bit offsets.
448 ****************************************************************************/
449 BOOL cli_unlock64(struct cli_state *cli, int fnum, SMB_OFF_T offset, SMB_OFF_T len)
451 union smb_lock parms;
452 struct smb_lock_entry lock[1];
455 if (!(cli->transport->negotiate.capabilities & CAP_LARGE_FILES)) {
456 return cli_unlock(cli, fnum, offset, len);
459 parms.lockx.level = RAW_LOCK_LOCKX;
460 parms.lockx.in.fnum = fnum;
461 parms.lockx.in.mode = LOCKING_ANDX_LARGE_FILES;
462 parms.lockx.in.timeout = 0;
463 parms.lockx.in.ulock_cnt = 1;
464 parms.lockx.in.lock_cnt = 0;
465 lock[0].pid = cli->session->pid;
466 lock[0].offset = offset;
468 parms.lockx.in.locks = &lock[0];
470 status = smb_raw_lock(cli->tree, &parms);
472 return NT_STATUS_IS_OK(status);
476 /****************************************************************************
477 Do a SMBgetattrE call.
478 ****************************************************************************/
479 BOOL cli_getattrE(struct cli_state *cli, int fd,
480 uint16 *attr, size_t *size,
481 time_t *c_time, time_t *a_time, time_t *m_time)
483 union smb_fileinfo parms;
486 parms.getattre.level = RAW_FILEINFO_GETATTRE;
487 parms.getattre.in.fnum = fd;
489 status = smb_raw_fileinfo(cli->tree, NULL, &parms);
491 if (!NT_STATUS_IS_OK(status))
495 *size = parms.getattre.out.size;
499 *attr = parms.getattre.out.attrib;
503 *c_time = parms.getattre.out.create_time;
507 *a_time = &parms.getattre.out.access_time;
511 *m_time = &parms.getattre.out.write_time;
517 /****************************************************************************
519 ****************************************************************************/
520 BOOL cli_getatr(struct cli_state *cli, const char *fname,
521 uint16 *attr, size_t *size, time_t *t)
523 union smb_fileinfo parms;
526 parms.getattr.level = RAW_FILEINFO_GETATTR;
527 parms.getattr.in.fname = fname;
529 status = smb_raw_pathinfo(cli->tree, NULL, &parms);
531 if (!NT_STATUS_IS_OK(status)) {
536 *size = parms.getattr.out.size;
540 *t = parms.getattr.out.write_time;
544 *attr = parms.getattr.out.attrib;
551 /****************************************************************************
553 ****************************************************************************/
554 BOOL cli_setatr(struct cli_state *cli, const char *fname, uint16 mode, time_t t)
556 union smb_setfileinfo parms;
559 parms.setattr.level = RAW_SFILEINFO_SETATTR;
560 parms.setattr.in.attrib = mode;
561 parms.setattr.in.write_time = t;
562 parms.setattr.file.fname = fname;
564 status = smb_raw_setpathinfo(cli->tree, &parms);
566 return NT_STATUS_IS_OK(status);
570 /****************************************************************************
571 Check for existence of a dir.
572 ****************************************************************************/
573 BOOL cli_chkpath(struct cli_state *cli, const char *path)
575 struct smb_chkpath parms;
579 path2 = strdup(path);
580 trim_string(path2,NULL,"\\");
583 path2 = strdup("\\");
586 parms.in.path = path2;
588 status = smb_raw_chkpath(cli->tree, &parms);
592 return NT_STATUS_IS_OK(status);
596 /****************************************************************************
598 ****************************************************************************/
599 BOOL cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
601 union smb_fsinfo fsinfo_parms;
605 mem_ctx = talloc_init("cli_dskattr");
607 fsinfo_parms.dskattr.level = RAW_QFS_DSKATTR;
608 status = smb_raw_fsinfo(cli->tree, mem_ctx, &fsinfo_parms);
609 if (NT_STATUS_IS_OK(status)) {
610 *bsize = fsinfo_parms.dskattr.out.block_size;
611 *total = fsinfo_parms.dskattr.out.units_total;
612 *avail = fsinfo_parms.dskattr.out.units_free;
615 talloc_destroy(mem_ctx);
617 return NT_STATUS_IS_OK(status);
621 /****************************************************************************
622 Create and open a temporary file.
623 ****************************************************************************/
624 int cli_ctemp(struct cli_state *cli, const char *path, char **tmp_path)
626 union smb_open open_parms;
630 mem_ctx = talloc_init("raw_open");
631 if (!mem_ctx) return -1;
633 open_parms.openx.level = RAW_OPEN_CTEMP;
634 open_parms.ctemp.in.attrib = 0;
635 open_parms.ctemp.in.directory = path;
637 status = smb_raw_open(cli->tree, mem_ctx, &open_parms);
639 *tmp_path = strdup(open_parms.ctemp.out.name);
641 talloc_destroy(mem_ctx);
642 if (NT_STATUS_IS_OK(status)) {
643 return open_parms.ctemp.out.fnum;