This commit was manufactured by cvs2svn to create tag
[Samba.git] / source / smbd / nttrans.c
blobdf989bd006a9f99e5bed32df18540eda5d2dbb91
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 SMB NT transaction handling
5 Copyright (C) Jeremy Allison 1994-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "includes.h"
23 #include "nterr.h"
24 #include "sids.h"
26 extern int DEBUGLEVEL;
27 extern int Protocol;
28 extern int smb_read_error;
29 extern int global_oplock_break;
30 extern BOOL case_sensitive;
31 extern BOOL case_preserve;
32 extern BOOL short_case_preserve;
34 static void remove_pending_change_notify_requests_by_mid(int mid);
36 static char *known_nt_pipes[] = {
37 "\\LANMAN",
38 "\\srvsvc",
39 "\\samr",
40 "\\wkssvc",
41 "\\NETLOGON",
42 "\\ntlsa",
43 "\\ntsvcs",
44 "\\lsass",
45 "\\lsarpc",
46 "\\winreg",
47 "\\spoolss",
48 NULL
51 /****************************************************************************
52 Send the required number of replies back.
53 We assume all fields other than the data fields are
54 set correctly for the type of call.
55 HACK ! Always assumes smb_setup field is zero.
56 ****************************************************************************/
58 static int send_nt_replies(char *inbuf, char *outbuf, int bufsize, uint32 nt_error, char *params,
59 int paramsize, char *pdata, int datasize)
61 extern int max_send;
62 int data_to_send = datasize;
63 int params_to_send = paramsize;
64 int useable_space;
65 char *pp = params;
66 char *pd = pdata;
67 int params_sent_thistime, data_sent_thistime, total_sent_thistime;
68 int alignment_offset = 3;
69 int data_alignment_offset = 0;
72 * Initially set the wcnt area to be 18 - this is true for all
73 * transNT replies.
76 set_message(outbuf,18,0,True);
78 if(nt_error != 0) {
79 /* NT Error. */
80 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
82 ERROR(0,nt_error);
85 /*
86 * If there genuinely are no parameters or data to send just send
87 * the empty packet.
90 if(params_to_send == 0 && data_to_send == 0) {
91 send_smb(smbd_server_fd(),outbuf);
92 return 0;
96 * When sending params and data ensure that both are nicely aligned.
97 * Only do this alignment when there is also data to send - else
98 * can cause NT redirector problems.
101 if (((params_to_send % 4) != 0) && (data_to_send != 0))
102 data_alignment_offset = 4 - (params_to_send % 4);
105 * Space is bufsize minus Netbios over TCP header minus SMB header.
106 * The alignment_offset is to align the param bytes on a four byte
107 * boundary (2 bytes for data len, one byte pad).
108 * NT needs this to work correctly.
111 useable_space = bufsize - ((smb_buf(outbuf)+
112 alignment_offset+data_alignment_offset) -
113 outbuf);
116 * useable_space can never be more than max_send minus the
117 * alignment offset.
120 useable_space = MIN(useable_space,
121 max_send - (alignment_offset+data_alignment_offset));
124 while (params_to_send || data_to_send) {
127 * Calculate whether we will totally or partially fill this packet.
130 total_sent_thistime = params_to_send + data_to_send +
131 alignment_offset + data_alignment_offset;
134 * We can never send more than useable_space.
137 total_sent_thistime = MIN(total_sent_thistime, useable_space);
139 set_message(outbuf, 18, total_sent_thistime, True);
142 * Set total params and data to be sent.
145 SIVAL(outbuf,smb_ntr_TotalParameterCount,paramsize);
146 SIVAL(outbuf,smb_ntr_TotalDataCount,datasize);
149 * Calculate how many parameters and data we can fit into
150 * this packet. Parameters get precedence.
153 params_sent_thistime = MIN(params_to_send,useable_space);
154 data_sent_thistime = useable_space - params_sent_thistime;
155 data_sent_thistime = MIN(data_sent_thistime,data_to_send);
157 SIVAL(outbuf,smb_ntr_ParameterCount,params_sent_thistime);
159 if(params_sent_thistime == 0) {
160 SIVAL(outbuf,smb_ntr_ParameterOffset,0);
161 SIVAL(outbuf,smb_ntr_ParameterDisplacement,0);
162 } else {
164 * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
165 * parameter bytes, however the first 4 bytes of outbuf are
166 * the Netbios over TCP header. Thus use smb_base() to subtract
167 * them from the calculation.
170 SIVAL(outbuf,smb_ntr_ParameterOffset,
171 ((smb_buf(outbuf)+alignment_offset) - smb_base(outbuf)));
173 * Absolute displacement of param bytes sent in this packet.
176 SIVAL(outbuf,smb_ntr_ParameterDisplacement,pp - params);
180 * Deal with the data portion.
183 SIVAL(outbuf,smb_ntr_DataCount, data_sent_thistime);
185 if(data_sent_thistime == 0) {
186 SIVAL(outbuf,smb_ntr_DataOffset,0);
187 SIVAL(outbuf,smb_ntr_DataDisplacement, 0);
188 } else {
190 * The offset of the data bytes is the offset of the
191 * parameter bytes plus the number of parameters being sent this time.
194 SIVAL(outbuf,smb_ntr_DataOffset,((smb_buf(outbuf)+alignment_offset) -
195 smb_base(outbuf)) + params_sent_thistime + data_alignment_offset);
196 SIVAL(outbuf,smb_ntr_DataDisplacement, pd - pdata);
200 * Copy the param bytes into the packet.
203 if(params_sent_thistime)
204 memcpy((smb_buf(outbuf)+alignment_offset),pp,params_sent_thistime);
207 * Copy in the data bytes
210 if(data_sent_thistime)
211 memcpy(smb_buf(outbuf)+alignment_offset+params_sent_thistime+
212 data_alignment_offset,pd,data_sent_thistime);
214 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
215 params_sent_thistime, data_sent_thistime, useable_space));
216 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
217 params_to_send, data_to_send, paramsize, datasize));
219 /* Send the packet */
220 send_smb(smbd_server_fd(),outbuf);
222 pp += params_sent_thistime;
223 pd += data_sent_thistime;
225 params_to_send -= params_sent_thistime;
226 data_to_send -= data_sent_thistime;
229 * Sanity check
232 if(params_to_send < 0 || data_to_send < 0) {
233 DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
234 params_to_send, data_to_send));
235 return -1;
239 return 0;
242 /****************************************************************************
243 (Hopefully) temporary call to fix bugs in NT5.0beta2. This OS sends unicode
244 strings in NT calls AND DOESN'T SET THE UNICODE BIT !!!!!!!
245 ****************************************************************************/
247 static void get_filename( char *fname, char *inbuf, int data_offset, int data_len, int fname_len)
250 * We need various heuristics here to detect a unicode string... JRA.
253 DEBUG(10,("get_filename: data_offset = %d, data_len = %d, fname_len = %d\n",
254 data_offset, data_len, fname_len ));
256 if(data_len - fname_len > 1) {
258 * NT 5.0 Beta 2 has kindly sent us a UNICODE string
259 * without bothering to set the unicode bit. How kind.
261 * Firstly - ensure that the data offset is aligned
262 * on a 2 byte boundary - add one if not.
264 fname_len = fname_len/2;
265 if(data_offset & 1)
266 data_offset++;
267 pstrcpy(fname, dos_unistrn2((uint16 *)(inbuf+data_offset), fname_len));
268 } else {
269 StrnCpy(fname,inbuf+data_offset,fname_len);
270 fname[fname_len] = '\0';
274 /****************************************************************************
275 Fix bugs in Win2000 final release. In trans calls this OS sends unicode
276 strings AND DOESN'T SET THE UNICODE BIT !!!!!!!
277 ****************************************************************************/
279 static void get_filename_transact( char *fname, char *inbuf, int data_offset, int data_len, int fname_len)
282 * We need various heuristics here to detect a unicode string... JRA.
285 DEBUG(10,("get_filename_transact: data_offset = %d, data_len = %d, fname_len = %d\n",
286 data_offset, data_len, fname_len ));
289 * Win2K sends a unicode filename plus one extra alingment byte.
290 * WinNT4.x send an ascii string with multiple garbage bytes on
291 * the end here.
294 if((data_len - fname_len == 1) || (inbuf[data_offset] == '\0')) {
296 * Ensure that the data offset is aligned
297 * on a 2 byte boundary - add one if not.
299 fname_len = fname_len/2;
300 if(data_offset & 1)
301 data_offset++;
302 pstrcpy(fname, dos_unistrn2((uint16 *)(inbuf+data_offset), fname_len));
303 } else {
304 StrnCpy(fname,inbuf+data_offset,fname_len);
305 fname[fname_len] = '\0';
309 /****************************************************************************
310 Save case statics.
311 ****************************************************************************/
313 static BOOL saved_case_sensitive;
314 static BOOL saved_case_preserve;
315 static BOOL saved_short_case_preserve;
317 /****************************************************************************
318 Save case semantics.
319 ****************************************************************************/
321 static void set_posix_case_semantics(uint32 file_attributes)
323 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
324 return;
326 saved_case_sensitive = case_sensitive;
327 saved_case_preserve = case_preserve;
328 saved_short_case_preserve = short_case_preserve;
330 /* Set to POSIX. */
331 case_sensitive = True;
332 case_preserve = True;
333 short_case_preserve = True;
336 /****************************************************************************
337 Restore case semantics.
338 ****************************************************************************/
340 static void restore_case_semantics(uint32 file_attributes)
342 if(!(file_attributes & FILE_FLAG_POSIX_SEMANTICS))
343 return;
345 case_sensitive = saved_case_sensitive;
346 case_preserve = saved_case_preserve;
347 short_case_preserve = saved_short_case_preserve;
350 /****************************************************************************
351 Utility function to map create disposition.
352 ****************************************************************************/
354 static int map_create_disposition( uint32 create_disposition)
356 int ret;
358 switch( create_disposition ) {
359 case FILE_CREATE:
360 /* create if not exist, fail if exist */
361 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_FAIL);
362 break;
363 case FILE_SUPERSEDE:
364 case FILE_OVERWRITE_IF:
365 /* create if not exist, trunc if exist */
366 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
367 break;
368 case FILE_OPEN:
369 /* fail if not exist, open if exists */
370 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_OPEN);
371 break;
372 case FILE_OPEN_IF:
373 /* create if not exist, open if exists */
374 ret = (FILE_CREATE_IF_NOT_EXIST|FILE_EXISTS_OPEN);
375 break;
376 case FILE_OVERWRITE:
377 /* fail if not exist, truncate if exists */
378 ret = (FILE_FAIL_IF_NOT_EXIST|FILE_EXISTS_TRUNCATE);
379 break;
380 default:
381 DEBUG(0,("map_create_disposition: Incorrect value for create_disposition = %d\n",
382 create_disposition ));
383 return -1;
386 DEBUG(10,("map_create_disposition: Mapped create_disposition %lx to %x\n",
387 (unsigned long)create_disposition, ret ));
389 return ret;
392 /****************************************************************************
393 Utility function to map share modes.
394 ****************************************************************************/
396 static int map_share_mode( BOOL *pstat_open_only, char *fname,
397 uint32 desired_access, uint32 share_access, uint32 file_attributes)
399 int smb_open_mode = -1;
401 *pstat_open_only = False;
403 switch( desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA) ) {
404 case FILE_READ_DATA:
405 smb_open_mode = DOS_OPEN_RDONLY;
406 break;
407 case FILE_WRITE_DATA:
408 case FILE_APPEND_DATA:
409 case FILE_WRITE_DATA|FILE_APPEND_DATA:
410 smb_open_mode = DOS_OPEN_WRONLY;
411 break;
412 case FILE_READ_DATA|FILE_WRITE_DATA:
413 case FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA:
414 case FILE_READ_DATA|FILE_APPEND_DATA:
415 smb_open_mode = DOS_OPEN_RDWR;
416 break;
420 * NB. For DELETE_ACCESS we should really check the
421 * directory permissions, as that is what controls
422 * delete, and for WRITE_DAC_ACCESS we should really
423 * check the ownership, as that is what controls the
424 * chmod. Note that this is *NOT* a security hole (this
425 * note is for you, Andrew) as we are not *allowing*
426 * the access at this point, the actual unlink or
427 * chown or chmod call would do this. We are just helping
428 * clients out by telling them if they have a hope
429 * of any of this succeeding. POSIX acls may still
430 * deny the real call. JRA.
433 if (smb_open_mode == -1) {
434 if(desired_access == WRITE_DAC_ACCESS || desired_access == READ_CONTROL_ACCESS)
435 *pstat_open_only = True;
437 if(desired_access & (DELETE_ACCESS|WRITE_DAC_ACCESS|WRITE_OWNER_ACCESS|
438 FILE_EXECUTE|FILE_READ_ATTRIBUTES|
439 FILE_READ_EA|FILE_WRITE_EA|SYSTEM_SECURITY_ACCESS|
440 FILE_WRITE_ATTRIBUTES|READ_CONTROL_ACCESS))
441 smb_open_mode = DOS_OPEN_RDONLY;
442 else {
443 DEBUG(0,("map_share_mode: Incorrect value %lx for desired_access to file %s\n",
444 (unsigned long)desired_access, fname));
445 return -1;
450 * Set the special bit that means allow share delete.
451 * This is held outside the normal share mode bits at 1<<15.
452 * JRA.
455 if(share_access & FILE_SHARE_DELETE)
456 smb_open_mode |= ALLOW_SHARE_DELETE;
458 /* Add in the requested share mode. */
459 switch( share_access & (FILE_SHARE_READ|FILE_SHARE_WRITE)) {
460 case FILE_SHARE_READ:
461 smb_open_mode |= SET_DENY_MODE(DENY_WRITE);
462 break;
463 case FILE_SHARE_WRITE:
464 smb_open_mode |= SET_DENY_MODE(DENY_READ);
465 break;
466 case (FILE_SHARE_READ|FILE_SHARE_WRITE):
467 smb_open_mode |= SET_DENY_MODE(DENY_NONE);
468 break;
469 case FILE_SHARE_NONE:
470 smb_open_mode |= SET_DENY_MODE(DENY_ALL);
471 break;
475 * Handle an O_SYNC request.
478 if(file_attributes & FILE_FLAG_WRITE_THROUGH)
479 smb_open_mode |= FILE_SYNC_OPENMODE;
481 DEBUG(10,("map_share_mode: Mapped desired access %lx, share access %lx, file attributes %lx \
482 to open_mode %x\n", (unsigned long)desired_access, (unsigned long)share_access,
483 (unsigned long)file_attributes, smb_open_mode ));
485 return smb_open_mode;
489 /****************************************************************************
490 Reply to an NT create and X call on a pipe.
491 ****************************************************************************/
492 static int nt_open_pipe(char *fname, connection_struct *conn,
493 char *inbuf, char *outbuf, int *ppnum)
495 pipes_struct *p = NULL;
496 vuser_key key;
497 uint16 vuid = SVAL(inbuf, smb_uid);
498 int i;
500 DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
502 /* See if it is one we want to handle. */
503 for( i = 0; known_nt_pipes[i]; i++ )
504 if( strequal(fname,known_nt_pipes[i]))
505 break;
507 if ( known_nt_pipes[i] == NULL )
508 return(ERROR(ERRSRV,ERRaccess));
510 /* Strip \\ off the name. */
511 fname++;
513 DEBUG(3,("nt_open_pipe: Known pipe %s opening.\n", fname));
515 key.pid = getpid();
516 key.vuid = vuid;
517 p = open_rpc_pipe_p(fname, &key, NULL);
518 if (!p)
519 return(ERROR(ERRSRV,ERRnofids));
521 *ppnum = p->pnum;
523 return 0;
526 /****************************************************************************
527 Reply to an NT create and X call.
528 ****************************************************************************/
530 int reply_ntcreate_and_X(connection_struct *conn,
531 char *inbuf,char *outbuf,int length,int bufsize)
533 pstring fname;
534 uint32 flags = IVAL(inbuf,smb_ntcreate_Flags);
535 uint32 desired_access = IVAL(inbuf,smb_ntcreate_DesiredAccess);
536 uint32 file_attributes = IVAL(inbuf,smb_ntcreate_FileAttributes);
537 uint32 share_access = IVAL(inbuf,smb_ntcreate_ShareAccess);
538 uint32 create_disposition = IVAL(inbuf,smb_ntcreate_CreateDisposition);
539 uint32 create_options = IVAL(inbuf,smb_ntcreate_CreateOptions);
540 uint32 fname_len = MIN(((uint32)SVAL(inbuf,smb_ntcreate_NameLength)),
541 ((uint32)sizeof(fname)-1));
542 uint16 root_dir_fid = (uint16)IVAL(inbuf,smb_ntcreate_RootDirectoryFid);
543 int smb_ofun;
544 int smb_open_mode;
545 int smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
546 /* Breakout the oplock request bits so we can set the
547 reply bits separately. */
548 int oplock_request = 0;
549 mode_t unixmode;
550 int pnum = -1;
551 int fmode=0,rmode=0;
552 SMB_OFF_T file_len = 0;
553 SMB_STRUCT_STAT sbuf;
554 int smb_action = 0;
555 BOOL bad_path = False;
556 files_struct *fsp=NULL;
557 char *p = NULL;
558 BOOL stat_open_only = False;
561 * We need to construct the open_and_X ofun value from the
562 * NT values, as that's what our code is structured to accept.
565 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
566 return(ERROR(ERRDOS,ERRbadaccess));
569 * Get the file name.
572 if(root_dir_fid != 0) {
574 * This filename is relative to a directory fid.
576 files_struct *dir_fsp = file_fsp(inbuf,smb_ntcreate_RootDirectoryFid);
577 size_t dir_name_len;
579 if(!dir_fsp)
580 return(ERROR(ERRDOS,ERRbadfid));
582 if(!dir_fsp->is_directory) {
584 * Check to see if this is a mac fork of some kind.
587 get_filename(&fname[0], inbuf, smb_buf(inbuf)-inbuf,
588 smb_buflen(inbuf),fname_len);
590 if( fname[0] == ':') {
591 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
592 return(ERROR(0, 0xc0000000|NT_STATUS_OBJECT_PATH_NOT_FOUND));
594 return(ERROR(ERRDOS,ERRbadfid));
598 * Copy in the base directory name.
601 pstrcpy( fname, dir_fsp->fsp_name );
602 dir_name_len = strlen(fname);
605 * Ensure it ends in a '\'.
608 if(fname[dir_name_len-1] != '\\' && fname[dir_name_len-1] != '/') {
609 pstrcat(fname, "\\");
610 dir_name_len++;
614 * This next calculation can refuse a correct filename if we're dealing
615 * with the Win2k unicode bug, but that would be rare. JRA.
618 if(fname_len + dir_name_len >= sizeof(pstring))
619 return(ERROR(ERRSRV,ERRfilespecs));
621 get_filename(&fname[dir_name_len], inbuf, smb_buf(inbuf)-inbuf,
622 smb_buflen(inbuf),fname_len);
624 } else {
626 get_filename(fname, inbuf, smb_buf(inbuf)-inbuf,
627 smb_buflen(inbuf),fname_len);
630 /* If it's an IPC, use the pipe handler. */
632 if (IS_IPC(conn) && lp_nt_pipe_support()) {
634 int ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum);
635 if(ret != 0)
636 return ret;
639 * Deal with pipe return.
642 set_message(outbuf,34,0,True);
644 p = outbuf + smb_vwv2;
645 p++;
646 SSVAL(p,0,pnum);
647 p += 2;
648 SIVAL(p,0,FILE_WAS_OPENED);
649 p += 4;
650 p += 32;
651 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
652 p += 20;
653 /* File type. */
654 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
655 /* Device state. */
656 SSVAL(p,2, 0x5FF); /* ? */
658 DEBUG(5,("reply_ntcreate_and_X: open pipe = %s\n", fname));
660 return chain_reply(inbuf,outbuf,length,bufsize);
664 * Now contruct the smb_open_mode value from the filename,
665 * desired access and the share access.
667 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
669 if((smb_open_mode = map_share_mode(&stat_open_only, fname, desired_access,
670 share_access,
671 file_attributes)) == -1)
672 return(ERROR(ERRDOS,ERRbadaccess));
674 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
675 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
678 * Ordinary file or directory.
682 * Check if POSIX semantics are wanted.
685 set_posix_case_semantics(file_attributes);
687 unix_convert(fname,conn,0,&bad_path,NULL);
689 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
692 * If it's a request for a directory open, deal with it separately.
695 if(create_options & FILE_DIRECTORY_FILE) {
696 oplock_request = 0;
698 fsp = open_directory(conn, fname, smb_ofun, unixmode, &smb_action);
700 restore_case_semantics(file_attributes);
702 if(!fsp) {
703 return(UNIXERROR(ERRDOS,ERRnoaccess));
705 } else {
707 * Ordinary file case.
710 /* NB. We have a potential bug here. If we
711 * cause an oplock break to ourselves, then we
712 * could end up processing filename related
713 * SMB requests whilst we await the oplock
714 * break response. As we may have changed the
715 * filename case semantics to be POSIX-like,
716 * this could mean a filename request could
717 * fail when it should succeed. This is a rare
718 * condition, but eventually we must arrange
719 * to restore the correct case semantics
720 * before issuing an oplock break request to
721 * our client. JRA. */
723 fsp = open_file_shared(conn,fname,smb_open_mode,
724 smb_ofun,unixmode, oplock_request,&rmode,&smb_action);
726 if (!fsp) {
727 /* We cheat here. There are two cases we
728 * care about. One is a directory rename,
729 * where the NT client will attempt to
730 * open the source directory for
731 * DELETE access. Note that when the
732 * NT client does this it does *not*
733 * set the directory bit in the
734 * request packet. This is translated
735 * into a read/write open
736 * request. POSIX states that any open
737 * for write request on a directory
738 * will generate an EISDIR error, so
739 * we can catch this here and open a
740 * pseudo handle that is flagged as a
741 * directory. The second is an open
742 * for a permissions read only, which
743 * we handle in the open_file_stat case. JRA.
746 if(errno == EISDIR) {
749 * Fail the open if it was explicitly a non-directory file.
752 if (create_options & FILE_NON_DIRECTORY_FILE) {
753 restore_case_semantics(file_attributes);
754 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
755 return(ERROR(0, 0xc0000000|NT_STATUS_FILE_IS_A_DIRECTORY));
758 oplock_request = 0;
759 fsp = open_directory(conn, fname, smb_ofun, unixmode, &smb_action);
761 if(!fsp) {
762 restore_case_semantics(file_attributes);
763 return(UNIXERROR(ERRDOS,ERRnoaccess));
765 #ifdef EROFS
766 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
767 #else /* !EROFS */
768 } else if (errno == EACCES && stat_open_only) {
769 #endif
771 * We couldn't open normally and all we want
772 * are the permissions. Try and do a stat open.
775 oplock_request = 0;
777 fsp = open_file_stat(conn,fname,smb_open_mode,&sbuf,&smb_action);
779 if(!fsp) {
780 restore_case_semantics(file_attributes);
781 return(UNIXERROR(ERRDOS,ERRnoaccess));
784 } else {
786 if((errno == ENOENT) && bad_path) {
787 unix_ERR_class = ERRDOS;
788 unix_ERR_code = ERRbadpath;
791 restore_case_semantics(file_attributes);
793 return(UNIXERROR(ERRDOS,ERRnoaccess));
798 if(fsp->is_directory) {
799 if(conn->vfs_ops.stat(dos_to_unix(fsp->fsp_name, False), &sbuf) != 0) {
800 close_file(fsp,True);
801 restore_case_semantics(file_attributes);
802 return(ERROR(ERRDOS,ERRnoaccess));
804 } else {
805 if (conn->vfs_ops.fstat(fsp->fd,&sbuf) != 0) {
806 close_file(fsp,False);
807 restore_case_semantics(file_attributes);
808 return(ERROR(ERRDOS,ERRnoaccess));
812 restore_case_semantics(file_attributes);
814 file_len = sbuf.st_size;
815 fmode = dos_mode(conn,fname,&sbuf);
816 if(fmode == 0)
817 fmode = FILE_ATTRIBUTE_NORMAL;
818 if (!fsp->is_directory && (fmode & aDIR)) {
819 close_file(fsp,False);
820 return(ERROR(ERRDOS,ERRnoaccess));
824 * If the caller set the extended oplock request bit
825 * and we granted one (by whatever means) - set the
826 * correct bit for extended oplock reply.
829 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
830 smb_action |= EXTENDED_OPLOCK_GRANTED;
832 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
833 smb_action |= EXTENDED_OPLOCK_GRANTED;
835 set_message(outbuf,34,0,True);
837 p = outbuf + smb_vwv2;
840 * Currently as we don't support level II oplocks we just report
841 * exclusive & batch here.
844 if (smb_action & EXTENDED_OPLOCK_GRANTED)
845 SCVAL(p,0, BATCH_OPLOCK_RETURN);
846 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
847 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
848 else
849 SCVAL(p,0,NO_OPLOCK_RETURN);
851 p++;
852 SSVAL(p,0,fsp->fnum);
853 p += 2;
854 SIVAL(p,0,smb_action);
855 p += 4;
857 /* Create time. */
858 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
859 p += 8;
860 put_long_date(p,sbuf.st_atime); /* access time */
861 p += 8;
862 put_long_date(p,sbuf.st_mtime); /* write time */
863 p += 8;
864 put_long_date(p,sbuf.st_mtime); /* change time */
865 p += 8;
866 SIVAL(p,0,fmode); /* File Attributes. */
867 p += 4;
868 SOFF_T(p, 0, file_len);
869 p += 8;
870 SOFF_T(p,0,file_len);
871 p += 12;
872 SCVAL(p,0,fsp->is_directory ? 1 : 0);
874 DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n", fsp->fnum, fsp->fsp_name));
876 return chain_reply(inbuf,outbuf,length,bufsize);
879 /****************************************************************************
880 Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
881 ****************************************************************************/
883 static int call_nt_transact_create(connection_struct *conn,
884 char *inbuf, char *outbuf, int length,
885 int bufsize, char **ppsetup, char **ppparams,
886 char **ppdata)
888 pstring fname;
889 char *params = *ppparams;
890 int total_parameter_count = (int)IVAL(inbuf, smb_nt_TotalParameterCount);
891 /* Breakout the oplock request bits so we can set the
892 reply bits separately. */
893 int oplock_request = 0;
894 mode_t unixmode;
895 int pnum = -1;
896 int fmode=0,rmode=0;
897 SMB_OFF_T file_len = 0;
898 SMB_STRUCT_STAT sbuf;
899 int smb_action = 0;
900 BOOL bad_path = False;
901 files_struct *fsp = NULL;
902 char *p = NULL;
903 BOOL stat_open_only = False;
904 uint32 flags;
905 uint32 desired_access;
906 uint32 file_attributes;
907 uint32 share_access;
908 uint32 create_disposition;
909 uint32 create_options;
910 uint32 fname_len;
911 uint16 root_dir_fid;
912 int smb_ofun;
913 int smb_open_mode;
914 int smb_attr;
916 DEBUG(5,("call_nt_transact_create\n"));
919 * Ensure minimum number of parameters sent.
922 if(total_parameter_count < 54) {
923 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)total_parameter_count));
924 return(ERROR(ERRDOS,ERRbadaccess));
927 flags = IVAL(params,0);
928 desired_access = IVAL(params,8);
929 file_attributes = IVAL(params,20);
930 share_access = IVAL(params,24);
931 create_disposition = IVAL(params,28);
932 create_options = IVAL(params,32);
933 fname_len = MIN(((uint32)IVAL(params,44)),((uint32)sizeof(fname)-1));
934 root_dir_fid = (uint16)IVAL(params,4);
935 smb_attr = (file_attributes & SAMBA_ATTRIBUTES_MASK);
938 * We need to construct the open_and_X ofun value from the
939 * NT values, as that's what our code is structured to accept.
942 if((smb_ofun = map_create_disposition( create_disposition )) == -1)
943 return(ERROR(ERRDOS,ERRbadmem));
946 * Get the file name.
949 if(root_dir_fid != 0) {
951 * This filename is relative to a directory fid.
954 files_struct *dir_fsp = file_fsp(params,4);
955 size_t dir_name_len;
957 if(!dir_fsp)
958 return(ERROR(ERRDOS,ERRbadfid));
960 if(!dir_fsp->is_directory) {
962 * Check to see if this is a mac fork of some kind.
965 get_filename_transact(&fname[0], params, 53,
966 total_parameter_count - 53 - fname_len, fname_len);
968 if( fname[0] == ':') {
969 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
970 return(ERROR(0, 0xc0000000|NT_STATUS_OBJECT_PATH_NOT_FOUND));
973 return(ERROR(ERRDOS,ERRbadfid));
977 * Copy in the base directory name.
980 pstrcpy( fname, dir_fsp->fsp_name );
981 dir_name_len = strlen(fname);
984 * Ensure it ends in a '\'.
987 if((fname[dir_name_len-1] != '\\') && (fname[dir_name_len-1] != '/')) {
988 pstrcat(fname, "\\");
989 dir_name_len++;
993 * This next calculation can refuse a correct filename if we're dealing
994 * with the Win2k unicode bug, but that would be rare. JRA.
997 if(fname_len + dir_name_len >= sizeof(pstring))
998 return(ERROR(ERRSRV,ERRfilespecs));
1000 get_filename_transact(&fname[dir_name_len], params, 53,
1001 total_parameter_count - 53 - fname_len, fname_len);
1003 } else {
1004 get_filename_transact(&fname[0], params, 53,
1005 total_parameter_count - 53 - fname_len, fname_len);
1008 /* If it's an IPC, use the pipe handler. */
1009 if (IS_IPC(conn)) {
1010 int ret = nt_open_pipe(fname, conn, inbuf, outbuf, &pnum);
1011 if(ret != 0)
1012 return ret;
1013 smb_action = FILE_WAS_OPENED;
1014 } else {
1017 * Now contruct the smb_open_mode value from the desired access
1018 * and the share access.
1021 if((smb_open_mode = map_share_mode( &stat_open_only, fname, desired_access,
1022 share_access, file_attributes)) == -1)
1023 return(ERROR(ERRDOS,ERRbadaccess));
1025 oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1026 oplock_request |= (flags & REQUEST_BATCH_OPLOCK) ? BATCH_OPLOCK : 0;
1029 * Check if POSIX semantics are wanted.
1032 set_posix_case_semantics(file_attributes);
1034 RESOLVE_DFSPATH(fname, conn, inbuf, outbuf);
1036 unix_convert(fname,conn,0,&bad_path,NULL);
1038 unixmode = unix_mode(conn,smb_attr | aARCH, fname);
1041 * If it's a request for a directory open, deal with it separately.
1044 if(create_options & FILE_DIRECTORY_FILE) {
1046 oplock_request = 0;
1049 * We will get a create directory here if the Win32
1050 * app specified a security descriptor in the
1051 * CreateDirectory() call.
1054 fsp = open_directory(conn, fname, smb_ofun, unixmode, &smb_action);
1056 if(!fsp) {
1057 restore_case_semantics(file_attributes);
1058 return(UNIXERROR(ERRDOS,ERRnoaccess));
1061 if(conn->vfs_ops.stat(dos_to_unix(fsp->fsp_name, False),
1062 &sbuf) != 0) {
1063 close_file(fsp,True);
1064 restore_case_semantics(file_attributes);
1065 return(ERROR(ERRDOS,ERRnoaccess));
1068 } else {
1071 * Ordinary file case.
1074 fsp = open_file_shared(conn,fname,smb_open_mode,smb_ofun,unixmode,
1075 oplock_request,&rmode,&smb_action);
1077 if (!fsp) {
1079 if(errno == EISDIR) {
1082 * Fail the open if it was explicitly a non-directory file.
1085 if (create_options & FILE_NON_DIRECTORY_FILE) {
1086 restore_case_semantics(file_attributes);
1087 SSVAL(outbuf, smb_flg2, FLAGS2_32_BIT_ERROR_CODES);
1088 return(ERROR(0, 0xc0000000|NT_STATUS_FILE_IS_A_DIRECTORY));
1091 oplock_request = 0;
1092 fsp = open_directory(conn, fname, smb_ofun, unixmode, &smb_action);
1094 if(!fsp) {
1095 restore_case_semantics(file_attributes);
1096 return(UNIXERROR(ERRDOS,ERRnoaccess));
1098 #ifdef EROFS
1099 } else if (((errno == EACCES) || (errno == EROFS)) && stat_open_only) {
1100 #else /* !EROFS */
1101 } else if (errno == EACCES && stat_open_only) {
1102 #endif
1105 * We couldn't open normally and all we want
1106 * are the permissions. Try and do a stat open.
1109 oplock_request = 0;
1111 fsp = open_file_stat(conn,fname,smb_open_mode,&sbuf,&smb_action);
1113 if(!fsp) {
1114 restore_case_semantics(file_attributes);
1115 return(UNIXERROR(ERRDOS,ERRnoaccess));
1117 } else {
1119 if((errno == ENOENT) && bad_path) {
1120 unix_ERR_class = ERRDOS;
1121 unix_ERR_code = ERRbadpath;
1124 restore_case_semantics(file_attributes);
1126 return(UNIXERROR(ERRDOS,ERRnoaccess));
1130 if(fsp->is_directory) {
1131 if(conn->vfs_ops.stat(dos_to_unix(fsp->fsp_name,False), &sbuf) != 0) {
1132 close_file(fsp,True);
1133 restore_case_semantics(file_attributes);
1134 return(ERROR(ERRDOS,ERRnoaccess));
1136 } else {
1137 if (!fsp->stat_open && conn->vfs_ops.fstat(fsp->fd,&sbuf) != 0) {
1138 close_file(fsp,False);
1139 restore_case_semantics(file_attributes);
1140 return(ERROR(ERRDOS,ERRnoaccess));
1144 file_len = sbuf.st_size;
1145 fmode = dos_mode(conn,fname,&sbuf);
1146 if(fmode == 0)
1147 fmode = FILE_ATTRIBUTE_NORMAL;
1149 if (fmode & aDIR) {
1150 close_file(fsp,False);
1151 restore_case_semantics(file_attributes);
1152 return(ERROR(ERRDOS,ERRnoaccess));
1156 * If the caller set the extended oplock request bit
1157 * and we granted one (by whatever means) - set the
1158 * correct bit for extended oplock reply.
1161 if (oplock_request && lp_fake_oplocks(SNUM(conn)))
1162 smb_action |= EXTENDED_OPLOCK_GRANTED;
1164 if(oplock_request && EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
1165 smb_action |= EXTENDED_OPLOCK_GRANTED;
1169 restore_case_semantics(file_attributes);
1171 /* Realloc the size of parameters and data we will return */
1172 params = *ppparams = Realloc(*ppparams, 69);
1173 if(params == NULL)
1174 return(ERROR(ERRDOS,ERRnomem));
1176 memset((char *)params,'\0',69);
1178 p = params;
1179 if (smb_action & EXTENDED_OPLOCK_GRANTED)
1180 SCVAL(p,0, BATCH_OPLOCK_RETURN);
1181 else if (LEVEL_II_OPLOCK_TYPE(fsp->oplock_type))
1182 SCVAL(p,0, LEVEL_II_OPLOCK_RETURN);
1183 else
1184 SCVAL(p,0,NO_OPLOCK_RETURN);
1186 p += 2;
1187 if (IS_IPC(conn)) {
1188 SSVAL(p,0,pnum);
1189 } else {
1190 SSVAL(p,0,fsp->fnum);
1192 p += 2;
1193 SIVAL(p,0,smb_action);
1194 p += 8;
1196 if (IS_IPC(conn)) {
1198 * Deal with pipe return.
1200 p += 32;
1201 SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
1202 p += 20;
1203 /* File type. */
1204 SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
1205 /* Device state. */
1206 SSVAL(p,2, 0x5FF); /* ? */
1207 } else {
1209 * Deal with file return.
1211 /* Create time. */
1212 put_long_date(p,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
1213 p += 8;
1214 put_long_date(p,sbuf.st_atime); /* access time */
1215 p += 8;
1216 put_long_date(p,sbuf.st_mtime); /* write time */
1217 p += 8;
1218 put_long_date(p,sbuf.st_mtime); /* change time */
1219 p += 8;
1220 SIVAL(p,0,fmode); /* File Attributes. */
1221 p += 4;
1222 SOFF_T(p,0,file_len);
1223 p += 8;
1224 SOFF_T(p,0,file_len);
1227 DEBUG(5,("call_nt_transact_create: open name = %s\n", fname));
1229 /* Send the required number of replies */
1230 send_nt_replies(inbuf, outbuf, bufsize, 0, params, 69, *ppdata, 0);
1232 return -1;
1235 /****************************************************************************
1236 Reply to a NT CANCEL request.
1237 ****************************************************************************/
1238 int reply_ntcancel(connection_struct *conn,
1239 char *inbuf,char *outbuf,int length,int bufsize)
1242 * Go through and cancel any pending change notifies.
1245 int mid = SVAL(inbuf,smb_mid);
1246 remove_pending_change_notify_requests_by_mid(mid);
1247 remove_pending_lock_requests_by_mid(mid);
1249 DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", mid));
1251 return(-1);
1254 /****************************************************************************
1255 Reply to an unsolicited SMBNTtranss - just ignore it!
1256 ****************************************************************************/
1257 int reply_nttranss(connection_struct *conn,
1258 char *inbuf,char *outbuf,int length,int bufsize)
1260 DEBUG(4,("Ignoring nttranss of length %d\n",length));
1261 return(-1);
1264 /****************************************************************************
1265 Reply to an NT transact rename command.
1266 ****************************************************************************/
1268 static int call_nt_transact_rename(connection_struct *conn,
1269 char *inbuf, char *outbuf, int length,
1270 int bufsize,
1271 char **ppsetup, char **ppparams, char **ppdata)
1273 char *params = *ppparams;
1274 pstring new_name;
1275 files_struct *fsp = file_fsp(params, 0);
1276 BOOL replace_if_exists = (SVAL(params,2) & RENAME_REPLACE_IF_EXISTS) ? True : False;
1277 uint32 fname_len = MIN((((uint32)IVAL(inbuf,smb_nt_TotalParameterCount)-4)),
1278 ((uint32)sizeof(new_name)-1));
1279 int outsize = 0;
1281 CHECK_FSP(fsp, conn);
1282 StrnCpy(new_name,params+4,fname_len);
1283 new_name[fname_len] = '\0';
1285 outsize = rename_internals(conn, inbuf, outbuf, fsp->fsp_name,
1286 new_name, replace_if_exists);
1287 if(outsize == 0) {
1289 * Rename was successful.
1291 send_nt_replies(inbuf, outbuf, bufsize, 0, NULL, 0, NULL, 0);
1293 DEBUG(3,("nt transact rename from = %s, to = %s succeeded.\n",
1294 fsp->fsp_name, new_name));
1296 outsize = -1;
1299 return(outsize);
1302 /****************************************************************************
1303 This is the structure to keep the information needed to
1304 determine if a directory has changed.
1305 *****************************************************************************/
1307 typedef struct {
1308 time_t modify_time; /* Info from the directory we're monitoring. */
1309 time_t status_time; /* Info from the directory we're monitoring. */
1310 time_t total_time; /* Total time of all directory entries - don't care if it wraps. */
1311 unsigned int num_entries; /* Zero or the number of files in the directory. */
1312 } change_hash_data;
1314 /****************************************************************************
1315 This is the structure to queue to implement NT change
1316 notify. It consists of smb_size bytes stored from the
1317 transact command (to keep the mid, tid etc around).
1318 Plus the fid to examine and the time to check next.
1319 *****************************************************************************/
1321 typedef struct {
1322 ubi_slNode msg_next;
1323 files_struct *fsp;
1324 connection_struct *conn;
1325 uint32 flags;
1326 time_t next_check_time;
1327 change_hash_data change_data;
1328 char request_buf[smb_size];
1329 } change_notify_buf;
1331 static ubi_slList change_notify_queue = { NULL, (ubi_slNodePtr)&change_notify_queue, 0};
1333 /****************************************************************************
1334 Setup the common parts of the return packet and send it.
1335 *****************************************************************************/
1337 static void change_notify_reply_packet(char *inbuf, int error_class, uint32 error_code)
1339 char outbuf[smb_size+38];
1341 memset(outbuf, '\0', sizeof(outbuf));
1342 construct_reply_common(inbuf, outbuf);
1345 * If we're returning a 'too much in the directory changed' we need to
1346 * set this is an NT error status flags. If we don't then the (probably
1347 * untested) code in the NT redirector has a bug in that it doesn't re-issue
1348 * the change notify.... Ah - I *love* it when I get so deeply into this I
1349 * can even determine how MS failed to test stuff and why.... :-). JRA.
1352 if(error_class == 0) /* NT Error. */
1353 SSVAL(outbuf,smb_flg2, SVAL(outbuf,smb_flg2) | FLAGS2_32_BIT_ERROR_CODES);
1355 ERROR(error_class,error_code);
1358 * Seems NT needs a transact command with an error code
1359 * in it. This is a longer packet than a simple error.
1361 set_message(outbuf,18,0,False);
1363 send_smb(smbd_server_fd(),outbuf);
1366 /****************************************************************************
1367 Create the hash we will use to determine if the contents changed.
1368 *****************************************************************************/
1370 static BOOL create_directory_notify_hash( change_notify_buf *cnbp, change_hash_data *change_data)
1372 SMB_STRUCT_STAT st;
1373 files_struct *fsp = cnbp->fsp;
1375 memset((char *)change_data, '\0', sizeof(change_data));
1378 * Store the current timestamp on the directory we are monitoring.
1381 if(dos_stat(fsp->fsp_name, &st) < 0) {
1382 DEBUG(0,("create_directory_notify_hash: Unable to stat name = %s. \
1383 Error was %s\n", fsp->fsp_name, strerror(errno) ));
1384 return False;
1387 change_data->modify_time = st.st_mtime;
1388 change_data->status_time = st.st_ctime;
1391 * If we are to watch for changes that are only stored
1392 * in inodes of files, not in the directory inode, we must
1393 * scan the directory and produce a unique identifier with
1394 * which we can determine if anything changed. We use the
1395 * modify and change times from all the files in the
1396 * directory, added together (ignoring wrapping if it's
1397 * larger than the max time_t value).
1400 if(cnbp->flags & (FILE_NOTIFY_CHANGE_SIZE|FILE_NOTIFY_CHANGE_LAST_WRITE)) {
1401 pstring full_name;
1402 char *p;
1403 char *fname;
1404 size_t remaining_len;
1405 size_t fullname_len;
1406 void *dp = OpenDir(cnbp->conn, fsp->fsp_name, True);
1408 if(dp == NULL) {
1409 DEBUG(0,("create_directory_notify_hash: Unable to open directory = %s. \
1410 Error was %s\n", fsp->fsp_name, strerror(errno) ));
1411 return False;
1414 change_data->num_entries = 0;
1416 pstrcpy(full_name, fsp->fsp_name);
1417 pstrcat(full_name, "/");
1419 fullname_len = strlen(full_name);
1420 remaining_len = sizeof(full_name) - fullname_len - 1;
1421 p = &full_name[fullname_len];
1423 while ((fname = ReadDirName(dp))) {
1424 if(strequal(fname, ".") || strequal(fname, ".."))
1425 continue;
1427 change_data->num_entries++;
1428 safe_strcpy( p, fname, remaining_len);
1430 memset(&st, '\0', sizeof(st));
1433 * Do the stat - but ignore errors.
1436 if(dos_stat(full_name, &st) < 0) {
1437 DEBUG(5,("create_directory_notify_hash: Unable to stat content file = %s. \
1438 Error was %s\n", fsp->fsp_name, strerror(errno) ));
1440 change_data->total_time += (st.st_mtime + st.st_ctime);
1443 CloseDir(dp);
1446 return True;
1449 /****************************************************************************
1450 Delete entries by fnum from the change notify pending queue.
1451 *****************************************************************************/
1453 void remove_pending_change_notify_requests_by_fid(files_struct *fsp)
1455 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1456 change_notify_buf *prev = NULL;
1458 while(cnbp != NULL) {
1459 if(cnbp->fsp->fnum == fsp->fnum) {
1460 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1461 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1462 continue;
1465 prev = cnbp;
1466 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1470 /****************************************************************************
1471 Delete entries by mid from the change notify pending queue. Always send reply.
1472 *****************************************************************************/
1474 static void remove_pending_change_notify_requests_by_mid(int mid)
1476 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1477 change_notify_buf *prev = NULL;
1479 while(cnbp != NULL) {
1480 if(SVAL(cnbp->request_buf,smb_mid) == mid) {
1481 change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_CANCELLED);
1482 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1483 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1484 continue;
1487 prev = cnbp;
1488 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1492 /****************************************************************************
1493 Delete entries by filename and cnum from the change notify pending queue.
1494 Always send reply.
1495 *****************************************************************************/
1497 void remove_pending_change_notify_requests_by_filename(files_struct *fsp)
1499 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1500 change_notify_buf *prev = NULL;
1502 while(cnbp != NULL) {
1504 * We know it refers to the same directory if the connection number and
1505 * the filename are identical.
1507 if((cnbp->fsp->conn == fsp->conn) && strequal(cnbp->fsp->fsp_name,fsp->fsp_name)) {
1508 change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_CANCELLED);
1509 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1510 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1511 continue;
1514 prev = cnbp;
1515 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1519 /****************************************************************************
1520 Process the change notify queue. Note that this is only called as root.
1521 Returns True if there are still outstanding change notify requests on the
1522 queue.
1523 *****************************************************************************/
1525 BOOL process_pending_change_notify_queue(time_t t)
1527 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1528 change_notify_buf *prev = NULL;
1530 if(cnbp == NULL)
1531 return False;
1533 if(cnbp->next_check_time >= t)
1534 return True;
1537 * It's time to check. Go through the queue and see if
1538 * the timestamps changed.
1541 while((cnbp != NULL) && (cnbp->next_check_time <= t)) {
1542 change_hash_data change_data;
1543 connection_struct *conn = cnbp->conn;
1544 uint16 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
1545 SVAL(cnbp->request_buf,smb_uid);
1547 ZERO_STRUCT(change_data);
1550 * Ensure we don't have any old chain_fsp values
1551 * sitting around....
1553 chain_size = 0;
1554 file_chain_reset();
1556 if(!become_user(conn,vuid)) {
1557 DEBUG(0,("process_pending_change_notify_queue: Unable to become user vuid=%d.\n",
1558 vuid ));
1560 * Remove the entry and return an error to the client.
1562 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1563 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1564 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1565 continue;
1568 if(!become_service(conn,True)) {
1569 DEBUG(0,("process_pending_change_notify_queue: Unable to become service Error was %s.\n", strerror(errno) ));
1571 * Remove the entry and return an error to the client.
1573 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1574 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1575 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1576 unbecome_user();
1577 continue;
1580 if(!create_directory_notify_hash( cnbp, &change_data)) {
1581 DEBUG(0,("process_pending_change_notify_queue: Unable to create change data for \
1582 directory %s\n", cnbp->fsp->fsp_name ));
1584 * Remove the entry and return an error to the client.
1586 change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
1587 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1588 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1589 unbecome_user();
1590 continue;
1593 if(memcmp( (char *)&cnbp->change_data, (char *)&change_data, sizeof(change_data))) {
1595 * Remove the entry and return a change notify to the client.
1597 DEBUG(5,("process_pending_change_notify_queue: directory name = %s changed.\n",
1598 cnbp->fsp->fsp_name ));
1599 change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_NOTIFY_ENUM_DIR);
1600 free((char *)ubi_slRemNext( &change_notify_queue, prev));
1601 cnbp = (change_notify_buf *)(prev ? ubi_slNext(prev) : ubi_slFirst(&change_notify_queue));
1602 unbecome_user();
1603 continue;
1606 unbecome_user();
1609 * Move to the next in the list.
1611 prev = cnbp;
1612 cnbp = (change_notify_buf *)ubi_slNext(cnbp);
1615 return (cnbp != NULL);
1618 /****************************************************************************
1619 Return true if there are pending change notifies.
1620 ****************************************************************************/
1622 BOOL change_notifies_pending(void)
1624 change_notify_buf *cnbp = (change_notify_buf *)ubi_slFirst( &change_notify_queue );
1625 return (cnbp != NULL);
1628 /****************************************************************************
1629 Reply to a notify change - queue the request and
1630 don't allow a directory to be opened.
1631 ****************************************************************************/
1633 static int call_nt_transact_notify_change(connection_struct *conn,
1634 char *inbuf, char *outbuf, int length,
1635 int bufsize,
1636 char **ppsetup,
1637 char **ppparams, char **ppdata)
1639 char *setup = *ppsetup;
1640 files_struct *fsp;
1641 change_notify_buf *cnbp;
1643 fsp = file_fsp(setup,4);
1645 DEBUG(3,("call_nt_transact_notify_change\n"));
1647 if(!fsp)
1648 return(ERROR(ERRDOS,ERRbadfid));
1650 if((!fsp->is_directory) || (conn != fsp->conn))
1651 return(ERROR(ERRDOS,ERRbadfid));
1654 * Now queue an entry on the notify change stack. We timestamp
1655 * the entry we are adding so that we know when to scan next.
1656 * We only need to save smb_size bytes from this incoming packet
1657 * as we will always by returning a 'read the directory yourself'
1658 * error.
1661 if((cnbp = (change_notify_buf *)malloc(sizeof(change_notify_buf))) == NULL) {
1662 DEBUG(0,("call_nt_transact_notify_change: malloc fail !\n" ));
1663 return -1;
1666 memset((char *)cnbp, '\0', sizeof(change_notify_buf));
1668 memcpy(cnbp->request_buf, inbuf, smb_size);
1669 cnbp->fsp = fsp;
1670 cnbp->conn = conn;
1671 cnbp->next_check_time = time(NULL) + lp_change_notify_timeout();
1672 cnbp->flags = IVAL(setup, 0);
1674 if(!create_directory_notify_hash( cnbp, &cnbp->change_data )) {
1675 free((char *)cnbp);
1676 return(UNIXERROR(ERRDOS,ERRbadfid));
1680 * Adding to the tail enables us to check only
1681 * the head when scanning for change, as this entry
1682 * is forced to have the first timeout expiration.
1685 ubi_slAddTail(&change_notify_queue, cnbp);
1687 DEBUG(3,("call_nt_transact_notify_change: notify change called on directory \
1688 name = %s\n", fsp->fsp_name ));
1690 return -1;
1693 /****************************************************************************
1694 Map unix perms to NT.
1695 ****************************************************************************/
1697 static SEC_ACCESS map_unix_perms( int *pacl_type, mode_t perm, int r_mask, int w_mask, int x_mask, BOOL is_directory)
1699 SEC_ACCESS sa;
1700 uint32 nt_mask = 0;
1702 *pacl_type = SEC_ACE_TYPE_ACCESS_ALLOWED;
1704 if((perm & (r_mask|w_mask|x_mask)) == (r_mask|w_mask|x_mask)) {
1705 nt_mask = UNIX_ACCESS_RWX;
1706 } else if((perm & (r_mask|w_mask|x_mask)) == 0) {
1707 nt_mask = UNIX_ACCESS_NONE;
1708 } else {
1709 nt_mask |= (perm & r_mask) ? UNIX_ACCESS_R : 0;
1710 if(is_directory)
1711 nt_mask |= (perm & w_mask) ? UNIX_ACCESS_W : 0;
1712 else
1713 nt_mask |= (perm & w_mask) ? UNIX_ACCESS_W : 0;
1714 nt_mask |= (perm & x_mask) ? UNIX_ACCESS_X : 0;
1716 make_sec_access(&sa,nt_mask);
1717 return sa;
1720 /****************************************************************************
1721 Function to create owner and group SIDs from a SMB_STRUCT_STAT.
1722 ****************************************************************************/
1724 static BOOL create_file_sids(SMB_STRUCT_STAT *psbuf, DOM_SID *powner_sid, DOM_SID *pgroup_sid)
1726 POSIX_ID id;
1728 ZERO_STRUCTP(powner_sid);
1729 ZERO_STRUCTP(pgroup_sid);
1730 DEBUG(0,("TODO: create_file_sids: not ok to assume gid is NT group\n"));
1732 id.type = SURS_POSIX_UID_AS_USR;
1733 id.id = (uint32)psbuf->st_uid;
1735 if (!surs_unixid_to_sam_sid(&id, powner_sid, False))
1737 DEBUG(3,("create_file_sids: map uid %d failed\n",
1738 (int)psbuf->st_uid));
1739 return False;
1742 id.type = SURS_POSIX_GID_AS_GRP;
1743 id.id = (uint32)psbuf->st_gid;
1745 if (!surs_unixid_to_sam_sid(&id, pgroup_sid, False))
1747 DEBUG(3,("create_file_sids: map gid %d to group failed\n",
1748 (int)psbuf->st_gid));
1750 id.type = SURS_POSIX_GID_AS_ALS;
1751 id.id = (uint32)psbuf->st_gid;
1753 if (surs_unixid_to_sam_sid(&id, pgroup_sid, False))
1755 return True;
1757 DEBUG(3,("create_file_sids: map gid %d to alias failed\n",
1758 (int)psbuf->st_gid));
1759 return False;
1761 return True;
1764 /****************************************************************************
1765 Reply to query a security descriptor from an fsp. If it succeeds it allocates
1766 the space for the return elements and returns True.
1767 ****************************************************************************/
1769 static size_t get_nt_acl(files_struct *fsp, SEC_DESC **ppdesc)
1771 SMB_STRUCT_STAT sbuf;
1772 SEC_ACE *ace_list = NULL;
1773 DOM_SID owner_sid;
1774 DOM_SID group_sid;
1775 size_t sec_desc_size;
1776 SEC_ACL *psa = NULL;
1777 SEC_ACCESS owner_access;
1778 int owner_acl_type;
1779 SEC_ACCESS group_access;
1780 int grp_acl_type;
1781 SEC_ACCESS other_access;
1782 int other_acl_type;
1783 int num_acls = 0;
1785 (*ppdesc) = NULL;
1787 if(!lp_nt_acl_support()) {
1788 sid_copy( &owner_sid, global_sid_everyone);
1789 sid_copy( &group_sid, global_sid_everyone);
1790 } else {
1792 if(fsp->is_directory || fsp->fd == -1) {
1793 if(dos_stat(fsp->fsp_name, &sbuf) != 0) {
1794 return 0;
1796 } else {
1797 if(fsp->conn->vfs_ops.fstat(fsp->fd,&sbuf) != 0) {
1798 return 0;
1803 * Get the owner, group and world SIDs.
1806 if (!create_file_sids(&sbuf, &owner_sid, &group_sid))
1808 DEBUG(3,("create_file_sids: uid or gid not mapped to SIDS\n"));
1809 return 0;
1813 * Create the generic 3 element UNIX acl.
1816 owner_access = map_unix_perms(&owner_acl_type, sbuf.st_mode,
1817 S_IRUSR, S_IWUSR, S_IXUSR, fsp->is_directory);
1818 group_access = map_unix_perms(&grp_acl_type, sbuf.st_mode,
1819 S_IRGRP, S_IWGRP, S_IXGRP, fsp->is_directory);
1820 other_access = map_unix_perms(&other_acl_type, sbuf.st_mode,
1821 S_IROTH, S_IWOTH, S_IXOTH, fsp->is_directory);
1823 if(owner_access.mask)
1825 ace_list = g_renew(SEC_ACE, ace_list, num_acls+1);
1826 if (ace_list == NULL)
1828 return 0;
1830 make_sec_ace(&ace_list[num_acls++], &owner_sid, owner_acl_type,
1831 owner_access, 0);
1834 if(group_access.mask)
1836 ace_list = g_renew(SEC_ACE, ace_list, num_acls+1);
1837 if (ace_list == NULL)
1839 return 0;
1842 make_sec_ace(&ace_list[num_acls++], &group_sid, grp_acl_type,
1843 group_access, 0);
1846 if(other_access.mask)
1848 ace_list = g_renew(SEC_ACE, ace_list, num_acls+1);
1849 if (ace_list == NULL)
1851 return 0;
1854 make_sec_ace(&ace_list[num_acls++], global_sid_everyone, other_acl_type,
1855 other_access, 0);
1858 if(fsp->is_directory) {
1860 * For directory ACLs we also add in the inherited permissions
1861 * ACE entries. These are the permissions a file would get when
1862 * being created in the directory.
1864 mode_t mode = unix_mode( fsp->conn, FILE_ATTRIBUTE_ARCHIVE, fsp->fsp_name);
1866 owner_access = map_unix_perms(&owner_acl_type, mode,
1867 S_IRUSR, S_IWUSR, S_IXUSR, fsp->is_directory);
1868 group_access = map_unix_perms(&grp_acl_type, mode,
1869 S_IRGRP, S_IWGRP, S_IXGRP, fsp->is_directory);
1870 other_access = map_unix_perms(&other_acl_type, mode,
1871 S_IROTH, S_IWOTH, S_IXOTH, fsp->is_directory);
1873 if(owner_access.mask)
1875 ace_list = g_renew(SEC_ACE, ace_list, num_acls+1);
1876 if (ace_list == NULL)
1878 return 0;
1881 make_sec_ace(&ace_list[num_acls++], &owner_sid, owner_acl_type,
1882 owner_access, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_INHERIT_ONLY);
1885 if(group_access.mask)
1887 ace_list = g_renew(SEC_ACE, ace_list, num_acls+1);
1888 if (ace_list == NULL)
1890 return 0;
1893 make_sec_ace(&ace_list[num_acls++], &group_sid, grp_acl_type,
1894 group_access, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_INHERIT_ONLY);
1897 if(other_access.mask)
1899 ace_list = g_renew(SEC_ACE, ace_list, num_acls+1);
1900 if (ace_list == NULL)
1902 return 0;
1905 make_sec_ace(&ace_list[num_acls++], global_sid_everyone, other_acl_type,
1906 other_access, SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_INHERIT_ONLY);
1910 if(num_acls)
1912 psa = g_new(SEC_ACL, 1);
1913 if (psa == NULL)
1915 safe_free(ace_list);
1917 if (!make_sec_acl( psa, 3, num_acls, ace_list))
1919 DEBUG(0,("get_nt_acl: Unable to malloc space for acl.\n"));
1920 safe_free(ace_list);
1921 safe_free(psa);
1922 return 0;
1927 (*ppdesc) = g_new(SEC_DESC, 1);
1929 if((*ppdesc) == NULL)
1931 DEBUG(0,("get_nt_acl: Unable to malloc space for security descriptor.\n"));
1932 sec_desc_size = 0;
1933 free_sec_acl(psa);
1934 safe_free(psa);
1935 return 0;
1938 sec_desc_size = make_sec_desc( (*ppdesc), 1,
1939 SEC_DESC_SELF_RELATIVE|SEC_DESC_DACL_PRESENT,
1940 sid_dup(&owner_sid),
1941 sid_dup(&group_sid),
1942 NULL, psa);
1944 return sec_desc_size;
1947 /****************************************************************************
1948 Reply to query a security descriptor - currently this is not implemented (it
1949 is planned to be though). Right now it just returns the same thing NT would
1950 when queried on a FAT filesystem. JRA.
1951 ****************************************************************************/
1953 static int call_nt_transact_query_security_desc(connection_struct *conn,
1954 char *inbuf, char *outbuf,
1955 int length, int bufsize,
1956 char **ppsetup, char **ppparams, char **ppdata)
1958 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
1959 char *params = *ppparams;
1960 char *data = *ppdata;
1961 prs_struct pd;
1962 SEC_DESC *psd = NULL;
1963 size_t sec_desc_size;
1965 files_struct *fsp = file_fsp(params,0);
1967 if(!fsp)
1968 return(ERROR(ERRDOS,ERRbadfid));
1970 DEBUG(3,("call_nt_transact_query_security_desc: file = %s\n", fsp->fsp_name ));
1972 params = *ppparams = Realloc(*ppparams, 4);
1973 if(params == NULL)
1974 return(ERROR(ERRDOS,ERRnomem));
1977 * Get the permissions to return.
1980 if((sec_desc_size = get_nt_acl(fsp, &psd)) == 0)
1981 return(UNIXERROR(ERRDOS,ERRnoaccess));
1983 DEBUG(3,("call_nt_transact_query_security_desc: sec_desc_size = %d.\n",(int)sec_desc_size));
1985 SIVAL(params,0,(uint32)sec_desc_size);
1987 if(max_data_count < sec_desc_size) {
1989 free_sec_desc(psd);
1990 safe_free(psd);
1992 send_nt_replies(inbuf, outbuf, bufsize, NT_STATUS_BUFFER_TOO_SMALL,
1993 params, 4, *ppdata, 0);
1994 return -1;
1998 * Allocate the data we will point this at.
2001 data = *ppdata = Realloc(*ppdata, sec_desc_size);
2002 if(data == NULL) {
2003 free_sec_desc(psd);
2004 safe_free(psd);
2005 return(ERROR(ERRDOS,ERRnomem));
2008 memset(data, '\0', sec_desc_size);
2011 * Init the parse struct we will marshall into.
2014 prs_init(&pd, sec_desc_size, 4, MARSHALL);
2017 * Finally, linearize into the outgoing buffer.
2020 if(!sec_io_desc( "sd data", psd, &pd, 1))
2022 free_sec_desc(psd);
2023 safe_free(psd);
2024 prs_mem_free(&pd);
2025 DEBUG(0,("call_nt_transact_query_security_desc: Error in marshalling \
2026 security descriptor.\n"));
2028 * Return access denied for want of a better error message..
2030 return(UNIXERROR(ERRDOS,ERRnoaccess));
2034 * copy the data out of the marshalled structure
2037 prs_link(NULL, &pd, NULL);
2038 prs_buf_copy(data, &pd, 0, sec_desc_size);
2041 * Now we can delete the security descriptor.
2044 prs_mem_free(&pd);
2045 free_sec_desc(psd);
2046 safe_free(psd);
2048 send_nt_replies(inbuf, outbuf, bufsize, 0, params, 4, data, (int)sec_desc_size);
2049 return -1;
2052 /****************************************************************************
2053 Map NT perms to UNIX.
2054 ****************************************************************************/
2056 #define FILE_SPECIFIC_READ_BITS (FILE_READ_DATA|FILE_READ_EA|FILE_READ_ATTRIBUTES)
2057 #define FILE_SPECIFIC_WRITE_BITS (FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_WRITE_EA|FILE_WRITE_ATTRIBUTES)
2058 #define FILE_SPECIFIC_EXECUTE_BITS (FILE_EXECUTE)
2060 static mode_t map_nt_perms( SEC_ACCESS sec_access, int type)
2062 mode_t mode = 0;
2064 switch(type) {
2065 case S_IRUSR:
2066 if(sec_access.mask & GENERIC_ALL_ACCESS)
2067 mode = S_IRUSR|S_IWUSR|S_IXUSR;
2068 else {
2069 mode |= (sec_access.mask & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IRUSR : 0;
2070 mode |= (sec_access.mask & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWUSR : 0;
2071 mode |= (sec_access.mask & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXUSR : 0;
2073 break;
2074 case S_IRGRP:
2075 if(sec_access.mask & GENERIC_ALL_ACCESS)
2076 mode = S_IRGRP|S_IWGRP|S_IXGRP;
2077 else {
2078 mode |= (sec_access.mask & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IRGRP : 0;
2079 mode |= (sec_access.mask & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWGRP : 0;
2080 mode |= (sec_access.mask & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXGRP : 0;
2082 break;
2083 case S_IROTH:
2084 if(sec_access.mask & GENERIC_ALL_ACCESS)
2085 mode = S_IROTH|S_IWOTH|S_IXOTH;
2086 else {
2087 mode |= (sec_access.mask & (GENERIC_READ_ACCESS|FILE_SPECIFIC_READ_BITS)) ? S_IROTH : 0;
2088 mode |= (sec_access.mask & (GENERIC_WRITE_ACCESS|FILE_SPECIFIC_WRITE_BITS)) ? S_IWOTH : 0;
2089 mode |= (sec_access.mask & (GENERIC_EXECUTE_ACCESS|FILE_SPECIFIC_EXECUTE_BITS)) ? S_IXOTH : 0;
2091 break;
2094 return mode;
2097 /****************************************************************************
2098 Unpack a SEC_DESC into a owner, group and set of UNIX permissions.
2099 ****************************************************************************/
2101 static BOOL unpack_nt_permissions(SMB_STRUCT_STAT *psbuf, uid_t *puser, gid_t *pgrp, mode_t *pmode,
2102 uint32 security_info_sent, SEC_DESC *psd, BOOL is_directory)
2104 DOM_SID file_owner_sid;
2105 DOM_SID file_grp_sid;
2106 SEC_ACL *dacl = psd->dacl;
2107 BOOL all_aces_are_inherit_only = (is_directory ? True : False);
2108 int i;
2109 POSIX_ID id;
2111 *pmode = 0;
2112 *puser = (uid_t)-1;
2113 *pgrp = (gid_t)-1;
2115 if(security_info_sent == 0) {
2116 DEBUG(0,("unpack_nt_permissions: no security info sent !\n"));
2117 return False;
2121 * Windows 2000 sends the owner and group SIDs as the logged in
2122 * user, not the connected user. But it still sends the file
2123 * owner SIDs on an ACL set. So we need to check for the file
2124 * owner and group SIDs as well as the owner SIDs. JRA.
2127 if (!create_file_sids(psbuf, &file_owner_sid, &file_grp_sid))
2129 DEBUG(3,("create_file_sids: uid or gid not mapped to SIDS\n"));
2130 return 0;
2134 * Don't immediately fail if the owner sid cannot be validated.
2135 * This may be a group chown only set.
2138 DEBUG(0,("TODO: LsaLookupSids to find type of owner_sid\n"));
2140 if (security_info_sent & OWNER_SECURITY_INFORMATION &&
2141 surs_sam_sid_to_unixid( psd->owner_sid, &id, False) &&
2142 id.type == SURS_POSIX_UID_AS_USR)
2144 *puser = (uid_t)id.id;
2148 * Don't immediately fail if the group sid cannot be validated.
2149 * This may be an owner chown only set.
2153 * assume support for RID_TYPE_ALIAS and RID_TYPE_GROUP only.
2154 * also assume that calls to sur_sam_sid_to_unixid are exclusive
2155 * and one will fail where the other succeeds
2158 if (security_info_sent & GROUP_SECURITY_INFORMATION &&
2159 surs_sam_sid_to_unixid( psd->grp_sid, &id, False) &&
2160 (id.type == SURS_POSIX_GID_AS_GRP || id.type == SURS_POSIX_GID_AS_ALS))
2162 *pgrp = (gid_t)id.id;
2166 * If no DACL then this is a chown only security descriptor.
2169 if(!(security_info_sent & DACL_SECURITY_INFORMATION) || !dacl) {
2170 *pmode = 0;
2171 return True;
2175 * Now go through the DACL and ensure that
2176 * any owner/group sids match.
2179 for(i = 0; i < dacl->num_aces; i++) {
2180 DOM_SID ace_sid;
2181 SEC_ACE *psa = &dacl->ace[i];
2183 if((psa->type != SEC_ACE_TYPE_ACCESS_ALLOWED) &&
2184 (psa->type != SEC_ACE_TYPE_ACCESS_DENIED)) {
2185 DEBUG(3,("unpack_nt_permissions: unable to set anything but an ALLOW or DENY ACE.\n"));
2186 return False;
2190 * Ignore or remove bits we don't care about on a directory ACE.
2193 if(is_directory) {
2194 if(psa->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
2195 DEBUG(3,("unpack_nt_permissions: ignoring inherit only ACE.\n"));
2196 continue;
2200 * At least one of the ACE entries wasn't inherit only.
2201 * Flag this so we know the returned mode is valid.
2204 all_aces_are_inherit_only = False;
2208 * Windows 2000 sets these flags even on *file* ACE's. This is wrong
2209 * but we can ignore them for now. Revisit this when we go to POSIX
2210 * ACLs on directories.
2213 psa->flags &= ~(SEC_ACE_FLAG_OBJECT_INHERIT|SEC_ACE_FLAG_CONTAINER_INHERIT);
2215 if(psa->flags != 0) {
2216 DEBUG(1,("unpack_nt_permissions: unable to set ACE flags (%x).\n",
2217 (unsigned int)psa->flags));
2218 return False;
2222 * The security mask may be UNIX_ACCESS_NONE which should map into
2223 * no permissions (we overload the WRITE_OWNER bit for this) or it
2224 * should be one of the ALL/EXECUTE/READ/WRITE bits. Arrange for this
2225 * to be so. Any other bits override the UNIX_ACCESS_NONE bit.
2228 psa->info.mask &= (GENERIC_ALL_ACCESS|GENERIC_EXECUTE_ACCESS|GENERIC_WRITE_ACCESS|
2229 GENERIC_READ_ACCESS|UNIX_ACCESS_NONE|FILE_ALL_ATTRIBUTES);
2231 if(psa->info.mask != UNIX_ACCESS_NONE)
2232 psa->info.mask &= ~UNIX_ACCESS_NONE;
2234 sid_copy(&ace_sid, &psa->sid);
2236 if(sid_equal(&ace_sid, &file_owner_sid)) {
2238 * Map the desired permissions into owner perms.
2241 if(psa->type == SEC_ACE_TYPE_ACCESS_ALLOWED)
2242 *pmode |= map_nt_perms( psa->info, S_IRUSR);
2243 else
2244 *pmode &= ~(map_nt_perms( psa->info, S_IRUSR));
2246 } else if( sid_equal(&ace_sid, &file_grp_sid)) {
2248 * Map the desired permissions into group perms.
2251 if(psa->type == SEC_ACE_TYPE_ACCESS_ALLOWED)
2252 *pmode |= map_nt_perms( psa->info, S_IRGRP);
2253 else
2254 *pmode &= ~(map_nt_perms( psa->info, S_IRGRP));
2256 } else if( sid_equal(&ace_sid, global_sid_everyone)) {
2258 * Map the desired permissions into other perms.
2261 if(psa->type == SEC_ACE_TYPE_ACCESS_ALLOWED)
2262 *pmode |= map_nt_perms( psa->info, S_IROTH);
2263 else
2264 *pmode &= ~(map_nt_perms( psa->info, S_IROTH));
2266 } else {
2267 DEBUG(0,("unpack_nt_permissions: unknown SID used in ACL.\n"));
2268 return False;
2272 if (is_directory && all_aces_are_inherit_only) {
2274 * Windows 2000 is doing one of these weird 'inherit acl'
2275 * traverses to conserve NTFS ACL resources. Just pretend
2276 * there was no DACL sent. JRA.
2279 DEBUG(10,("unpack_nt_permissions: Win2k inherit acl traverse. Ignoring DACL.\n"));
2280 free_sec_acl(psd->dacl);
2281 safe_free(psd->dacl);
2282 psd->dacl = NULL;
2285 return True;
2288 /****************************************************************************
2289 Reply to set a security descriptor. Map to UNIX perms.
2290 ****************************************************************************/
2292 static int call_nt_transact_set_security_desc(connection_struct *conn,
2293 char *inbuf, char *outbuf, int length,
2294 int bufsize, char **ppsetup,
2295 char **ppparams, char **ppdata)
2297 uint32 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
2298 char *params= *ppparams;
2299 char *data = *ppdata;
2300 prs_struct pd;
2301 SEC_DESC psd;
2302 uint32 total_data_count = (uint32)IVAL(inbuf, smb_nts_TotalDataCount);
2303 uid_t user = (uid_t)-1;
2304 gid_t grp = (gid_t)-1;
2305 mode_t perms = 0;
2306 SMB_STRUCT_STAT sbuf;
2307 files_struct *fsp = NULL;
2308 uint32 security_info_sent = 0;
2309 BOOL got_dacl = False;
2311 if(!lp_nt_acl_support())
2312 return(UNIXERROR(ERRDOS,ERRnoaccess));
2314 if(total_parameter_count < 8)
2315 return(ERROR(ERRDOS,ERRbadfunc));
2317 if((fsp = file_fsp(params,0)) == NULL)
2318 return(ERROR(ERRDOS,ERRbadfid));
2320 security_info_sent = IVAL(params,4);
2322 DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n", fsp->fsp_name,
2323 (unsigned int)security_info_sent ));
2326 * Init the parse struct we will unmarshall from.
2329 prs_init(&pd, 0, 4, UNMARSHALL);
2332 * Setup the prs_struct with a copy of the memory we just
2333 * allocated. unmarshall from the beginning.
2336 prs_append_data(&pd, data, total_data_count);
2339 * Finally, unmarshall from the data buffer.
2342 if(!sec_io_desc( "sd data", &psd, &pd, 1))
2344 free_sec_desc(&psd);
2345 prs_mem_free(&pd);
2346 DEBUG(0,("call_nt_transact_set_security_desc: Error in unmarshalling \
2347 security descriptor.\n"));
2349 * Return access denied for want of a better error message..
2351 return(UNIXERROR(ERRDOS,ERRnoaccess));
2355 * finished with the marshalling structure, already
2358 prs_mem_free(&pd);
2361 * Get the current state of the file.
2364 if(fsp->is_directory) {
2365 if(dos_stat(fsp->fsp_name, &sbuf) != 0) {
2366 free_sec_desc(&psd);
2367 return(UNIXERROR(ERRDOS,ERRnoaccess));
2369 } else {
2371 int ret;
2373 if(fsp->fd == -1)
2374 ret = conn->vfs_ops.stat(dos_to_unix(fsp->fsp_name,False), &sbuf);
2375 else
2376 ret = conn->vfs_ops.fstat(fsp->fd,&sbuf);
2378 if(ret != 0) {
2379 free_sec_desc(&psd);
2380 return(UNIXERROR(ERRDOS,ERRnoaccess));
2385 * Unpack the user/group/world id's and permissions.
2388 if(!unpack_nt_permissions( &sbuf, &user, &grp, &perms, security_info_sent, &psd, fsp->is_directory)) {
2389 free_sec_desc(&psd);
2390 return(UNIXERROR(ERRDOS,ERRnoaccess));
2393 if (psd.dacl != NULL)
2394 got_dacl = True;
2396 free_sec_desc(&psd);
2399 * Do we need to chown ?
2402 if((user != (uid_t)-1 || grp != (uid_t)-1) && (sbuf.st_uid != user || sbuf.st_gid != grp)) {
2404 DEBUG(3,("call_nt_transact_set_security_desc: chown %s. uid = %u, gid = %u.\n",
2405 fsp->fsp_name, (unsigned int)user, (unsigned int)grp ));
2407 if(dos_chown( fsp->fsp_name, user, grp) == -1) {
2408 DEBUG(3,("call_nt_transact_set_security_desc: chown %s, %u, %u failed. Error = %s.\n",
2409 fsp->fsp_name, (unsigned int)user, (unsigned int)grp, strerror(errno) ));
2410 return(UNIXERROR(ERRDOS,ERRnoaccess));
2414 * Recheck the current state of the file, which may have changed.
2415 * (suid/sgid bits, for instance)
2418 if(fsp->is_directory) {
2419 if(dos_stat(fsp->fsp_name, &sbuf) != 0) {
2420 return(UNIXERROR(ERRDOS,ERRnoaccess));
2422 } else {
2424 int ret;
2426 if(fsp->fd == -1)
2427 ret = conn->vfs_ops.stat(dos_to_unix(fsp->fsp_name,False), &sbuf);
2428 else
2429 ret = conn->vfs_ops.fstat(fsp->fd,&sbuf);
2431 if(ret != 0)
2432 return(UNIXERROR(ERRDOS,ERRnoaccess));
2437 * Only change security if we got a DACL.
2440 if((security_info_sent & DACL_SECURITY_INFORMATION) && got_dacl) {
2443 * Check to see if we need to change anything.
2444 * Enforce limits on modified bits *only*. Don't enforce masks
2445 * on bits not changed by the user.
2448 if(fsp->is_directory) {
2450 perms &= (lp_dir_security_mask(SNUM(conn)) | sbuf.st_mode);
2451 perms |= (lp_force_dir_security_mode(SNUM(conn)) & ( perms ^ sbuf.st_mode ));
2453 } else {
2455 perms &= (lp_security_mask(SNUM(conn)) | sbuf.st_mode);
2456 perms |= (lp_force_security_mode(SNUM(conn)) & ( perms ^ sbuf.st_mode ));
2461 * Preserve special bits.
2464 perms |= (sbuf.st_mode & ~0777);
2467 * Do we need to chmod ?
2470 if(sbuf.st_mode != perms) {
2472 DEBUG(3,("call_nt_transact_set_security_desc: chmod %s. perms = 0%o.\n",
2473 fsp->fsp_name, (unsigned int)perms ));
2475 if(conn->vfs_ops.chmod(dos_to_unix(fsp->fsp_name, False), perms) == -1) {
2476 DEBUG(3,("call_nt_transact_set_security_desc: chmod %s, 0%o failed. Error = %s.\n",
2477 fsp->fsp_name, (unsigned int)perms, strerror(errno) ));
2478 return(UNIXERROR(ERRDOS,ERRnoaccess));
2483 send_nt_replies(inbuf, outbuf, bufsize, 0, NULL, 0, NULL, 0);
2484 return -1;
2487 /****************************************************************************
2488 Reply to IOCTL - not implemented - no plans.
2489 ****************************************************************************/
2490 static int call_nt_transact_ioctl(connection_struct *conn,
2491 char *inbuf, char *outbuf, int length,
2492 int bufsize,
2493 char **ppsetup, char **ppparams, char **ppdata)
2495 static BOOL logged_message = False;
2497 if(!logged_message) {
2498 DEBUG(0,("call_nt_transact_ioctl: Currently not implemented.\n"));
2499 logged_message = True; /* Only print this once... */
2501 return(ERROR(ERRSRV,ERRnosupport));
2504 /****************************************************************************
2505 Reply to a SMBNTtrans.
2506 ****************************************************************************/
2507 int reply_nttrans(connection_struct *conn,
2508 char *inbuf,char *outbuf,int length,int bufsize)
2510 int outsize = 0;
2511 #if 0 /* Not used. */
2512 uint16 max_setup_count = CVAL(inbuf, smb_nt_MaxSetupCount);
2513 uint32 max_parameter_count = IVAL(inbuf, smb_nt_MaxParameterCount);
2514 uint32 max_data_count = IVAL(inbuf,smb_nt_MaxDataCount);
2515 #endif /* Not used. */
2516 uint32 total_parameter_count = IVAL(inbuf, smb_nt_TotalParameterCount);
2517 uint32 total_data_count = IVAL(inbuf, smb_nt_TotalDataCount);
2518 uint32 parameter_count = IVAL(inbuf,smb_nt_ParameterCount);
2519 uint32 parameter_offset = IVAL(inbuf,smb_nt_ParameterOffset);
2520 uint32 data_count = IVAL(inbuf,smb_nt_DataCount);
2521 uint32 data_offset = IVAL(inbuf,smb_nt_DataOffset);
2522 uint16 setup_count = 2*CVAL(inbuf,smb_nt_SetupCount); /* setup count is in *words* */
2523 uint16 function_code = SVAL( inbuf, smb_nt_Function);
2524 char *params = NULL, *data = NULL, *setup = NULL;
2525 uint32 num_params_sofar, num_data_sofar;
2527 if(global_oplock_break && (function_code == NT_TRANSACT_CREATE)) {
2529 * Queue this open message as we are the process of an oplock break.
2532 DEBUG(2,("reply_nttrans: queueing message NT_TRANSACT_CREATE \
2533 due to being in oplock break state.\n" ));
2535 push_oplock_pending_smb_message( inbuf, length);
2536 return -1;
2539 outsize = set_message(outbuf,0,0,True);
2542 * All nttrans messages we handle have smb_wct == 19 + setup_count.
2543 * Ensure this is so as a sanity check.
2546 if(CVAL(inbuf, smb_wct) != 19 + (setup_count/2)) {
2547 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2548 CVAL(inbuf, smb_wct), 19 + (setup_count/2)));
2549 return(ERROR(ERRSRV,ERRerror));
2552 /* Allocate the space for the setup, the maximum needed parameters and data */
2554 if(setup_count > 0)
2555 setup = (char *)malloc(setup_count);
2556 if (total_parameter_count > 0)
2557 params = (char *)malloc(total_parameter_count);
2558 if (total_data_count > 0)
2559 data = (char *)malloc(total_data_count);
2561 if ((total_parameter_count && !params) || (total_data_count && !data) ||
2562 (setup_count && !setup)) {
2563 DEBUG(0,("reply_nttrans : Out of memory\n"));
2564 return(ERROR(ERRDOS,ERRnomem));
2567 /* Copy the param and data bytes sent with this request into
2568 the params buffer */
2569 num_params_sofar = parameter_count;
2570 num_data_sofar = data_count;
2572 if (parameter_count > total_parameter_count || data_count > total_data_count)
2573 exit_server("reply_nttrans: invalid sizes in packet.\n");
2575 if(setup) {
2576 memcpy( setup, &inbuf[smb_nt_SetupStart], setup_count);
2577 DEBUG(10,("reply_nttrans: setup_count = %d\n", setup_count));
2578 dump_data(10, setup, setup_count);
2580 if(params) {
2581 memcpy( params, smb_base(inbuf) + parameter_offset, parameter_count);
2582 DEBUG(10,("reply_nttrans: parameter_count = %d\n", parameter_count));
2583 dump_data(10, params, parameter_count);
2585 if(data) {
2586 memcpy( data, smb_base(inbuf) + data_offset, data_count);
2587 DEBUG(10,("reply_nttrans: data_count = %d\n",data_count));
2588 dump_data(10, data, data_count);
2591 if(num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2592 /* We need to send an interim response then receive the rest
2593 of the parameter/data bytes */
2594 outsize = set_message(outbuf,0,0,True);
2595 send_smb(smbd_server_fd(),outbuf);
2597 while( num_data_sofar < total_data_count || num_params_sofar < total_parameter_count) {
2598 BOOL ret;
2600 ret = receive_next_smb(inbuf,bufsize,SMB_SECONDARY_WAIT);
2602 if((ret && (CVAL(inbuf, smb_com) != SMBnttranss)) || !ret) {
2603 outsize = set_message(outbuf,0,0,True);
2604 if(ret) {
2605 DEBUG(0,("reply_nttrans: Invalid secondary nttrans packet\n"));
2606 } else {
2607 DEBUG(0,("reply_nttrans: %s in getting secondary nttrans response.\n",
2608 (smb_read_error == READ_ERROR) ? "error" : "timeout" ));
2610 if(params)
2611 free(params);
2612 if(data)
2613 free(data);
2614 if(setup)
2615 free(setup);
2616 return(ERROR(ERRSRV,ERRerror));
2619 /* Revise total_params and total_data in case they have changed downwards */
2620 total_parameter_count = IVAL(inbuf, smb_nts_TotalParameterCount);
2621 total_data_count = IVAL(inbuf, smb_nts_TotalDataCount);
2622 num_params_sofar += (parameter_count = IVAL(inbuf,smb_nts_ParameterCount));
2623 num_data_sofar += ( data_count = IVAL(inbuf, smb_nts_DataCount));
2624 if (num_params_sofar > total_parameter_count || num_data_sofar > total_data_count)
2625 exit_server("reply_nttrans2: data overflow in secondary nttrans packet\n");
2627 memcpy( &params[ IVAL(inbuf, smb_nts_ParameterDisplacement)],
2628 smb_base(inbuf) + IVAL(inbuf, smb_nts_ParameterOffset), parameter_count);
2629 memcpy( &data[IVAL(inbuf, smb_nts_DataDisplacement)],
2630 smb_base(inbuf)+ IVAL(inbuf, smb_nts_DataOffset), data_count);
2634 if (Protocol >= PROTOCOL_NT1) {
2635 uint16 flg2 = SVAL(outbuf,smb_flg2);
2636 SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
2639 /* Now we must call the relevant NT_TRANS function */
2640 switch(function_code) {
2641 case NT_TRANSACT_CREATE:
2642 outsize = call_nt_transact_create(conn, inbuf, outbuf, length, bufsize,
2643 &setup, &params, &data);
2644 break;
2645 case NT_TRANSACT_IOCTL:
2646 outsize = call_nt_transact_ioctl(conn,
2647 inbuf, outbuf, length, bufsize,
2648 &setup, &params, &data);
2649 break;
2650 case NT_TRANSACT_SET_SECURITY_DESC:
2651 outsize = call_nt_transact_set_security_desc(conn, inbuf, outbuf,
2652 length, bufsize,
2653 &setup, &params, &data);
2654 break;
2655 case NT_TRANSACT_NOTIFY_CHANGE:
2656 outsize = call_nt_transact_notify_change(conn, inbuf, outbuf,
2657 length, bufsize,
2658 &setup, &params, &data);
2659 break;
2660 case NT_TRANSACT_RENAME:
2661 outsize = call_nt_transact_rename(conn, inbuf, outbuf, length,
2662 bufsize,
2663 &setup, &params, &data);
2664 break;
2666 case NT_TRANSACT_QUERY_SECURITY_DESC:
2667 outsize = call_nt_transact_query_security_desc(conn, inbuf, outbuf,
2668 length, bufsize,
2669 &setup, &params, &data);
2670 break;
2671 default:
2672 /* Error in request */
2673 DEBUG(0,("reply_nttrans: Unknown request %d in nttrans call\n", function_code));
2674 if(setup)
2675 free(setup);
2676 if(params)
2677 free(params);
2678 if(data)
2679 free(data);
2680 return (ERROR(ERRSRV,ERRerror));
2683 /* As we do not know how many data packets will need to be
2684 returned here the various call_nt_transact_xxxx calls
2685 must send their own. Thus a call_nt_transact_xxxx routine only
2686 returns a value other than -1 when it wants to send
2687 an error packet.
2690 if(setup)
2691 free(setup);
2692 if(params)
2693 free(params);
2694 if(data)
2695 free(data);
2696 return outsize; /* If a correct response was needed the call_nt_transact_xxxx
2697 calls have already sent it. If outsize != -1 then it is
2698 returning an error packet. */