Proper fix for #6898 - Samba duplicates file content on appending. Pointed out by...
[kamenim/samba.git] / source3 / smbd / nttrans.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB NT transaction handling
4    Copyright (C) Jeremy Allison                 1994-2007
5    Copyright (C) Stefan (metze) Metzmacher      2003
6
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 3 of the License, or
10    (at your option) any later version.
11
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.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/globals.h"
23
24 extern const struct generic_mapping file_generic_mapping;
25
26 static char *nttrans_realloc(char **ptr, size_t size)
27 {
28         if (ptr==NULL) {
29                 smb_panic("nttrans_realloc() called with NULL ptr");
30         }
31
32         *ptr = (char *)SMB_REALLOC(*ptr, size);
33         if(*ptr == NULL) {
34                 return NULL;
35         }
36         memset(*ptr,'\0',size);
37         return *ptr;
38 }
39
40 /****************************************************************************
41  Send the required number of replies back.
42  We assume all fields other than the data fields are
43  set correctly for the type of call.
44  HACK ! Always assumes smb_setup field is zero.
45 ****************************************************************************/
46
47 void send_nt_replies(connection_struct *conn,
48                         struct smb_request *req, NTSTATUS nt_error,
49                      char *params, int paramsize,
50                      char *pdata, int datasize)
51 {
52         int data_to_send = datasize;
53         int params_to_send = paramsize;
54         int useable_space;
55         char *pp = params;
56         char *pd = pdata;
57         int params_sent_thistime, data_sent_thistime, total_sent_thistime;
58         int alignment_offset = 3;
59         int data_alignment_offset = 0;
60         struct smbd_server_connection *sconn = smbd_server_conn;
61         int max_send = sconn->smb1.sessions.max_send;
62
63         /*
64          * If there genuinely are no parameters or data to send just send
65          * the empty packet.
66          */
67
68         if(params_to_send == 0 && data_to_send == 0) {
69                 reply_outbuf(req, 18, 0);
70                 if (NT_STATUS_V(nt_error)) {
71                         error_packet_set((char *)req->outbuf,
72                                          0, 0, nt_error,
73                                          __LINE__,__FILE__);
74                 }
75                 show_msg((char *)req->outbuf);
76                 if (!srv_send_smb(smbd_server_fd(),
77                                 (char *)req->outbuf,
78                                 true, req->seqnum+1,
79                                 IS_CONN_ENCRYPTED(conn),
80                                 &req->pcd)) {
81                         exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
82                 }
83                 TALLOC_FREE(req->outbuf);
84                 return;
85         }
86
87         /*
88          * When sending params and data ensure that both are nicely aligned.
89          * Only do this alignment when there is also data to send - else
90          * can cause NT redirector problems.
91          */
92
93         if (((params_to_send % 4) != 0) && (data_to_send != 0)) {
94                 data_alignment_offset = 4 - (params_to_send % 4);
95         }
96
97         /*
98          * Space is bufsize minus Netbios over TCP header minus SMB header.
99          * The alignment_offset is to align the param bytes on a four byte
100          * boundary (2 bytes for data len, one byte pad).
101          * NT needs this to work correctly.
102          */
103
104         useable_space = max_send - (smb_size
105                                     + 2 * 18 /* wct */
106                                     + alignment_offset
107                                     + data_alignment_offset);
108
109         if (useable_space < 0) {
110                 char *msg = talloc_asprintf(
111                         talloc_tos(),
112                         "send_nt_replies failed sanity useable_space = %d!!!",
113                         useable_space);
114                 DEBUG(0, ("%s\n", msg));
115                 exit_server_cleanly(msg);
116         }
117
118         while (params_to_send || data_to_send) {
119
120                 /*
121                  * Calculate whether we will totally or partially fill this packet.
122                  */
123
124                 total_sent_thistime = params_to_send + data_to_send;
125
126                 /*
127                  * We can never send more than useable_space.
128                  */
129
130                 total_sent_thistime = MIN(total_sent_thistime, useable_space);
131
132                 reply_outbuf(req, 18,
133                              total_sent_thistime + alignment_offset
134                              + data_alignment_offset);
135
136                 /*
137                  * We might have had SMBnttranss in req->inbuf, fix that.
138                  */
139                 SCVAL(req->outbuf, smb_com, SMBnttrans);
140
141                 /*
142                  * Set total params and data to be sent.
143                  */
144
145                 SIVAL(req->outbuf,smb_ntr_TotalParameterCount,paramsize);
146                 SIVAL(req->outbuf,smb_ntr_TotalDataCount,datasize);
147
148                 /*
149                  * Calculate how many parameters and data we can fit into
150                  * this packet. Parameters get precedence.
151                  */
152
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);
156
157                 SIVAL(req->outbuf, smb_ntr_ParameterCount,
158                       params_sent_thistime);
159
160                 if(params_sent_thistime == 0) {
161                         SIVAL(req->outbuf,smb_ntr_ParameterOffset,0);
162                         SIVAL(req->outbuf,smb_ntr_ParameterDisplacement,0);
163                 } else {
164                         /*
165                          * smb_ntr_ParameterOffset is the offset from the start of the SMB header to the
166                          * parameter bytes, however the first 4 bytes of outbuf are
167                          * the Netbios over TCP header. Thus use smb_base() to subtract
168                          * them from the calculation.
169                          */
170
171                         SIVAL(req->outbuf,smb_ntr_ParameterOffset,
172                               ((smb_buf(req->outbuf)+alignment_offset)
173                                - smb_base(req->outbuf)));
174                         /*
175                          * Absolute displacement of param bytes sent in this packet.
176                          */
177
178                         SIVAL(req->outbuf, smb_ntr_ParameterDisplacement,
179                               pp - params);
180                 }
181
182                 /*
183                  * Deal with the data portion.
184                  */
185
186                 SIVAL(req->outbuf, smb_ntr_DataCount, data_sent_thistime);
187
188                 if(data_sent_thistime == 0) {
189                         SIVAL(req->outbuf,smb_ntr_DataOffset,0);
190                         SIVAL(req->outbuf,smb_ntr_DataDisplacement, 0);
191                 } else {
192                         /*
193                          * The offset of the data bytes is the offset of the
194                          * parameter bytes plus the number of parameters being sent this time.
195                          */
196
197                         SIVAL(req->outbuf, smb_ntr_DataOffset,
198                               ((smb_buf(req->outbuf)+alignment_offset) -
199                                smb_base(req->outbuf))
200                               + params_sent_thistime + data_alignment_offset);
201                         SIVAL(req->outbuf,smb_ntr_DataDisplacement, pd - pdata);
202                 }
203
204                 /*
205                  * Copy the param bytes into the packet.
206                  */
207
208                 if(params_sent_thistime) {
209                         if (alignment_offset != 0) {
210                                 memset(smb_buf(req->outbuf), 0,
211                                        alignment_offset);
212                         }
213                         memcpy((smb_buf(req->outbuf)+alignment_offset), pp,
214                                params_sent_thistime);
215                 }
216
217                 /*
218                  * Copy in the data bytes
219                  */
220
221                 if(data_sent_thistime) {
222                         if (data_alignment_offset != 0) {
223                                 memset((smb_buf(req->outbuf)+alignment_offset+
224                                         params_sent_thistime), 0,
225                                        data_alignment_offset);
226                         }
227                         memcpy(smb_buf(req->outbuf)+alignment_offset
228                                +params_sent_thistime+data_alignment_offset,
229                                pd,data_sent_thistime);
230                 }
231
232                 DEBUG(9,("nt_rep: params_sent_thistime = %d, data_sent_thistime = %d, useable_space = %d\n",
233                         params_sent_thistime, data_sent_thistime, useable_space));
234                 DEBUG(9,("nt_rep: params_to_send = %d, data_to_send = %d, paramsize = %d, datasize = %d\n",
235                         params_to_send, data_to_send, paramsize, datasize));
236
237                 if (NT_STATUS_V(nt_error)) {
238                         error_packet_set((char *)req->outbuf,
239                                          0, 0, nt_error,
240                                          __LINE__,__FILE__);
241                 }
242
243                 /* Send the packet */
244                 show_msg((char *)req->outbuf);
245                 if (!srv_send_smb(smbd_server_fd(),
246                                 (char *)req->outbuf,
247                                 true, req->seqnum+1,
248                                 IS_CONN_ENCRYPTED(conn),
249                                 &req->pcd)) {
250                         exit_server_cleanly("send_nt_replies: srv_send_smb failed.");
251                 }
252
253                 TALLOC_FREE(req->outbuf);
254
255                 pp += params_sent_thistime;
256                 pd += data_sent_thistime;
257
258                 params_to_send -= params_sent_thistime;
259                 data_to_send -= data_sent_thistime;
260
261                 /*
262                  * Sanity check
263                  */
264
265                 if(params_to_send < 0 || data_to_send < 0) {
266                         DEBUG(0,("send_nt_replies failed sanity check pts = %d, dts = %d\n!!!",
267                                 params_to_send, data_to_send));
268                         exit_server_cleanly("send_nt_replies: internal error");
269                 }
270         }
271 }
272
273 /****************************************************************************
274  Reply to an NT create and X call on a pipe
275 ****************************************************************************/
276
277 static void nt_open_pipe(char *fname, connection_struct *conn,
278                          struct smb_request *req, int *ppnum)
279 {
280         files_struct *fsp;
281         NTSTATUS status;
282
283         DEBUG(4,("nt_open_pipe: Opening pipe %s.\n", fname));
284
285         /* Strip \\ off the name. */
286         fname++;
287
288         status = open_np_file(req, fname, &fsp);
289         if (!NT_STATUS_IS_OK(status)) {
290                 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
291                         reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
292                                         ERRDOS, ERRbadpipe);
293                         return;
294                 }
295                 reply_nterror(req, status);
296                 return;
297         }
298
299         *ppnum = fsp->fnum;
300         return;
301 }
302
303 /****************************************************************************
304  Reply to an NT create and X call for pipes.
305 ****************************************************************************/
306
307 static void do_ntcreate_pipe_open(connection_struct *conn,
308                                   struct smb_request *req)
309 {
310         char *fname = NULL;
311         int pnum = -1;
312         char *p = NULL;
313         uint32 flags = IVAL(req->vwv+3, 1);
314         TALLOC_CTX *ctx = talloc_tos();
315
316         srvstr_pull_req_talloc(ctx, req, &fname, req->buf, STR_TERMINATE);
317
318         if (!fname) {
319                 reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
320                                 ERRDOS, ERRbadpipe);
321                 return;
322         }
323         nt_open_pipe(fname, conn, req, &pnum);
324
325         if (req->outbuf) {
326                 /* error reply */
327                 return;
328         }
329
330         /*
331          * Deal with pipe return.
332          */
333
334         if (flags & EXTENDED_RESPONSE_REQUIRED) {
335                 /* This is very strange. We
336                  * return 50 words, but only set
337                  * the wcnt to 42 ? It's definately
338                  * what happens on the wire....
339                  */
340                 reply_outbuf(req, 50, 0);
341                 SCVAL(req->outbuf,smb_wct,42);
342         } else {
343                 reply_outbuf(req, 34, 0);
344         }
345
346         p = (char *)req->outbuf + smb_vwv2;
347         p++;
348         SSVAL(p,0,pnum);
349         p += 2;
350         SIVAL(p,0,FILE_WAS_OPENED);
351         p += 4;
352         p += 32;
353         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
354         p += 20;
355         /* File type. */
356         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
357         /* Device state. */
358         SSVAL(p,2, 0x5FF); /* ? */
359         p += 4;
360
361         if (flags & EXTENDED_RESPONSE_REQUIRED) {
362                 p += 25;
363                 SIVAL(p,0,FILE_GENERIC_ALL);
364                 /*
365                  * For pipes W2K3 seems to return
366                  * 0x12019B next.
367                  * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
368                  */
369                 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
370         }
371
372         DEBUG(5,("do_ntcreate_pipe_open: open pipe = %s\n", fname));
373
374         chain_reply(req);
375 }
376
377 /****************************************************************************
378  Reply to an NT create and X call.
379 ****************************************************************************/
380
381 void reply_ntcreate_and_X(struct smb_request *req)
382 {
383         connection_struct *conn = req->conn;
384         struct smb_filename *smb_fname = NULL;
385         char *fname = NULL;
386         uint32 flags;
387         uint32 access_mask;
388         uint32 file_attributes;
389         uint32 share_access;
390         uint32 create_disposition;
391         uint32 create_options;
392         uint16 root_dir_fid;
393         uint64_t allocation_size;
394         /* Breakout the oplock request bits so we can set the
395            reply bits separately. */
396         uint32 fattr=0;
397         SMB_OFF_T file_len = 0;
398         int info = 0;
399         files_struct *fsp = NULL;
400         char *p = NULL;
401         struct timespec create_timespec;
402         struct timespec c_timespec;
403         struct timespec a_timespec;
404         struct timespec m_timespec;
405         struct timespec write_time_ts;
406         NTSTATUS status;
407         int oplock_request;
408         uint8_t oplock_granted = NO_OPLOCK_RETURN;
409         TALLOC_CTX *ctx = talloc_tos();
410
411         START_PROFILE(SMBntcreateX);
412
413         if (req->wct < 24) {
414                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
415                 return;
416         }
417
418         flags = IVAL(req->vwv+3, 1);
419         access_mask = IVAL(req->vwv+7, 1);
420         file_attributes = IVAL(req->vwv+13, 1);
421         share_access = IVAL(req->vwv+15, 1);
422         create_disposition = IVAL(req->vwv+17, 1);
423         create_options = IVAL(req->vwv+19, 1);
424         root_dir_fid = (uint16)IVAL(req->vwv+5, 1);
425
426         allocation_size = (uint64_t)IVAL(req->vwv+9, 1);
427 #ifdef LARGE_SMB_OFF_T
428         allocation_size |= (((uint64_t)IVAL(req->vwv+11, 1)) << 32);
429 #endif
430
431         srvstr_get_path_req(ctx, req, &fname, (const char *)req->buf,
432                             STR_TERMINATE, &status);
433
434         if (!NT_STATUS_IS_OK(status)) {
435                 reply_nterror(req, status);
436                 goto out;
437         }
438
439         DEBUG(10,("reply_ntcreate_and_X: flags = 0x%x, access_mask = 0x%x "
440                   "file_attributes = 0x%x, share_access = 0x%x, "
441                   "create_disposition = 0x%x create_options = 0x%x "
442                   "root_dir_fid = 0x%x, fname = %s\n",
443                         (unsigned int)flags,
444                         (unsigned int)access_mask,
445                         (unsigned int)file_attributes,
446                         (unsigned int)share_access,
447                         (unsigned int)create_disposition,
448                         (unsigned int)create_options,
449                         (unsigned int)root_dir_fid,
450                         fname));
451
452         /*
453          * we need to remove ignored bits when they come directly from the client
454          * because we reuse some of them for internal stuff
455          */
456         create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
457
458         /*
459          * If it's an IPC, use the pipe handler.
460          */
461
462         if (IS_IPC(conn)) {
463                 if (lp_nt_pipe_support()) {
464                         do_ntcreate_pipe_open(conn, req);
465                         goto out;
466                 }
467                 reply_doserror(req, ERRDOS, ERRnoaccess);
468                 goto out;
469         }
470
471         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
472         if (oplock_request) {
473                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
474                         ? BATCH_OPLOCK : 0;
475         }
476
477         status = filename_convert(ctx,
478                                 conn,
479                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
480                                 fname,
481                                 (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ?
482                                         UCF_POSIX_PATHNAMES : 0,
483                                 NULL,
484                                 &smb_fname);
485
486         if (!NT_STATUS_IS_OK(status)) {
487                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
488                         reply_botherror(req,
489                                 NT_STATUS_PATH_NOT_COVERED,
490                                 ERRSRV, ERRbadpath);
491                         goto out;
492                 }
493                 reply_nterror(req, status);
494                 goto out;
495         }
496
497         /*
498          * Bug #6898 - clients using Windows opens should
499          * never be able to set this attribute into the
500          * VFS.
501          */
502         file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
503
504         status = SMB_VFS_CREATE_FILE(
505                 conn,                                   /* conn */
506                 req,                                    /* req */
507                 root_dir_fid,                           /* root_dir_fid */
508                 smb_fname,                              /* fname */
509                 access_mask,                            /* access_mask */
510                 share_access,                           /* share_access */
511                 create_disposition,                     /* create_disposition*/
512                 create_options,                         /* create_options */
513                 file_attributes,                        /* file_attributes */
514                 oplock_request,                         /* oplock_request */
515                 allocation_size,                        /* allocation_size */
516                 NULL,                                   /* sd */
517                 NULL,                                   /* ea_list */
518                 &fsp,                                   /* result */
519                 &info);                                 /* pinfo */
520
521         if (!NT_STATUS_IS_OK(status)) {
522                 if (open_was_deferred(req->mid)) {
523                         /* We have re-scheduled this call, no error. */
524                         goto out;
525                 }
526                 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
527                         reply_botherror(req, status, ERRDOS, ERRfilexists);
528                 }
529                 else {
530                         reply_nterror(req, status);
531                 }
532                 goto out;
533         }
534
535         /* Ensure we're pointing at the correct stat struct. */
536         TALLOC_FREE(smb_fname);
537         smb_fname = fsp->fsp_name;
538
539         /*
540          * If the caller set the extended oplock request bit
541          * and we granted one (by whatever means) - set the
542          * correct bit for extended oplock reply.
543          */
544
545         if (oplock_request &&
546             (lp_fake_oplocks(SNUM(conn))
547              || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
548
549                 /*
550                  * Exclusive oplock granted
551                  */
552
553                 if (flags & REQUEST_BATCH_OPLOCK) {
554                         oplock_granted = BATCH_OPLOCK_RETURN;
555                 } else {
556                         oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
557                 }
558         } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
559                 oplock_granted = LEVEL_II_OPLOCK_RETURN;
560         } else {
561                 oplock_granted = NO_OPLOCK_RETURN;
562         }
563
564         file_len = smb_fname->st.st_ex_size;
565
566         if (flags & EXTENDED_RESPONSE_REQUIRED) {
567                 /* This is very strange. We
568                  * return 50 words, but only set
569                  * the wcnt to 42 ? It's definately
570                  * what happens on the wire....
571                  */
572                 reply_outbuf(req, 50, 0);
573                 SCVAL(req->outbuf,smb_wct,42);
574         } else {
575                 reply_outbuf(req, 34, 0);
576         }
577
578         p = (char *)req->outbuf + smb_vwv2;
579
580         SCVAL(p, 0, oplock_granted);
581
582         p++;
583         SSVAL(p,0,fsp->fnum);
584         p += 2;
585         if ((create_disposition == FILE_SUPERSEDE)
586             && (info == FILE_WAS_OVERWRITTEN)) {
587                 SIVAL(p,0,FILE_WAS_SUPERSEDED);
588         } else {
589                 SIVAL(p,0,info);
590         }
591         p += 4;
592
593         fattr = dos_mode(conn, smb_fname);
594         if (fattr == 0) {
595                 fattr = FILE_ATTRIBUTE_NORMAL;
596         }
597
598         /* Deal with other possible opens having a modified
599            write time. JRA. */
600         ZERO_STRUCT(write_time_ts);
601         get_file_infos(fsp->file_id, NULL, &write_time_ts);
602         if (!null_timespec(write_time_ts)) {
603                 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
604         }
605
606         /* Create time. */
607         create_timespec = get_create_timespec(conn, fsp, smb_fname);
608         a_timespec = smb_fname->st.st_ex_atime;
609         m_timespec = smb_fname->st.st_ex_mtime;
610         c_timespec = get_change_timespec(conn, fsp, smb_fname);
611
612         if (lp_dos_filetime_resolution(SNUM(conn))) {
613                 dos_filetime_timespec(&create_timespec);
614                 dos_filetime_timespec(&a_timespec);
615                 dos_filetime_timespec(&m_timespec);
616                 dos_filetime_timespec(&c_timespec);
617         }
618
619         put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
620         p += 8;
621         put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
622         p += 8;
623         put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
624         p += 8;
625         put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
626         p += 8;
627         SIVAL(p,0,fattr); /* File Attributes. */
628         p += 4;
629         SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn,fsp,&smb_fname->st));
630         p += 8;
631         SOFF_T(p,0,file_len);
632         p += 8;
633         if (flags & EXTENDED_RESPONSE_REQUIRED) {
634                 uint16_t file_status = (NO_EAS|NO_SUBSTREAMS|NO_REPARSETAG);
635                 size_t num_names = 0;
636                 unsigned int num_streams;
637                 struct stream_struct *streams = NULL;
638
639                 /* Do we have any EA's ? */
640                 status = get_ea_names_from_file(ctx, conn, fsp,
641                                 smb_fname->base_name, NULL, &num_names);
642                 if (NT_STATUS_IS_OK(status) && num_names) {
643                         file_status &= ~NO_EAS;
644                 }
645                 status = SMB_VFS_STREAMINFO(conn, NULL, smb_fname->base_name, ctx,
646                         &num_streams, &streams);
647                 /* There is always one stream, ::$DATA. */
648                 if (NT_STATUS_IS_OK(status) && num_streams > 1) {
649                         file_status &= ~NO_SUBSTREAMS;
650                 }
651                 TALLOC_FREE(streams);
652                 SSVAL(p,2,file_status);
653         }
654         p += 4;
655         SCVAL(p,0,fsp->is_directory ? 1 : 0);
656
657         if (flags & EXTENDED_RESPONSE_REQUIRED) {
658                 uint32 perms = 0;
659                 p += 25;
660                 if (fsp->is_directory ||
661                     can_write_to_file(conn, smb_fname)) {
662                         perms = FILE_GENERIC_ALL;
663                 } else {
664                         perms = FILE_GENERIC_READ|FILE_EXECUTE;
665                 }
666                 SIVAL(p,0,perms);
667         }
668
669         DEBUG(5,("reply_ntcreate_and_X: fnum = %d, open name = %s\n",
670                 fsp->fnum, smb_fname_str_dbg(smb_fname)));
671
672         chain_reply(req);
673  out:
674         END_PROFILE(SMBntcreateX);
675         return;
676 }
677
678 /****************************************************************************
679  Reply to a NT_TRANSACT_CREATE call to open a pipe.
680 ****************************************************************************/
681
682 static void do_nt_transact_create_pipe(connection_struct *conn,
683                                        struct smb_request *req,
684                                        uint16 **ppsetup, uint32 setup_count,
685                                        char **ppparams, uint32 parameter_count,
686                                        char **ppdata, uint32 data_count)
687 {
688         char *fname = NULL;
689         char *params = *ppparams;
690         int pnum = -1;
691         char *p = NULL;
692         NTSTATUS status;
693         size_t param_len;
694         uint32 flags;
695         TALLOC_CTX *ctx = talloc_tos();
696
697         /*
698          * Ensure minimum number of parameters sent.
699          */
700
701         if(parameter_count < 54) {
702                 DEBUG(0,("do_nt_transact_create_pipe - insufficient parameters (%u)\n", (unsigned int)parameter_count));
703                 reply_doserror(req, ERRDOS, ERRnoaccess);
704                 return;
705         }
706
707         flags = IVAL(params,0);
708
709         srvstr_get_path(ctx, params, req->flags2, &fname, params+53,
710                         parameter_count-53, STR_TERMINATE,
711                         &status);
712         if (!NT_STATUS_IS_OK(status)) {
713                 reply_nterror(req, status);
714                 return;
715         }
716
717         nt_open_pipe(fname, conn, req, &pnum);
718
719         if (req->outbuf) {
720                 /* Error return */
721                 return;
722         }
723
724         /* Realloc the size of parameters and data we will return */
725         if (flags & EXTENDED_RESPONSE_REQUIRED) {
726                 /* Extended response is 32 more byyes. */
727                 param_len = 101;
728         } else {
729                 param_len = 69;
730         }
731         params = nttrans_realloc(ppparams, param_len);
732         if(params == NULL) {
733                 reply_doserror(req, ERRDOS, ERRnomem);
734                 return;
735         }
736
737         p = params;
738         SCVAL(p,0,NO_OPLOCK_RETURN);
739
740         p += 2;
741         SSVAL(p,0,pnum);
742         p += 2;
743         SIVAL(p,0,FILE_WAS_OPENED);
744         p += 8;
745
746         p += 32;
747         SIVAL(p,0,FILE_ATTRIBUTE_NORMAL); /* File Attributes. */
748         p += 20;
749         /* File type. */
750         SSVAL(p,0,FILE_TYPE_MESSAGE_MODE_PIPE);
751         /* Device state. */
752         SSVAL(p,2, 0x5FF); /* ? */
753         p += 4;
754
755         if (flags & EXTENDED_RESPONSE_REQUIRED) {
756                 p += 25;
757                 SIVAL(p,0,FILE_GENERIC_ALL);
758                 /*
759                  * For pipes W2K3 seems to return
760                  * 0x12019B next.
761                  * This is ((FILE_GENERIC_READ|FILE_GENERIC_WRITE) & ~FILE_APPEND_DATA)
762                  */
763                 SIVAL(p,4,(FILE_GENERIC_READ|FILE_GENERIC_WRITE)&~FILE_APPEND_DATA);
764         }
765
766         DEBUG(5,("do_nt_transact_create_pipe: open name = %s\n", fname));
767
768         /* Send the required number of replies */
769         send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
770
771         return;
772 }
773
774 /****************************************************************************
775  Internal fn to set security descriptors.
776 ****************************************************************************/
777
778 static NTSTATUS set_sd(files_struct *fsp, uint8 *data, uint32 sd_len,
779                        uint32 security_info_sent)
780 {
781         SEC_DESC *psd = NULL;
782         NTSTATUS status;
783
784         if (sd_len == 0 || !lp_nt_acl_support(SNUM(fsp->conn))) {
785                 return NT_STATUS_OK;
786         }
787
788         status = unmarshall_sec_desc(talloc_tos(), data, sd_len, &psd);
789
790         if (!NT_STATUS_IS_OK(status)) {
791                 return status;
792         }
793
794         if (psd->owner_sid == NULL) {
795                 security_info_sent &= ~OWNER_SECURITY_INFORMATION;
796         }
797         if (psd->group_sid == NULL) {
798                 security_info_sent &= ~GROUP_SECURITY_INFORMATION;
799         }
800
801         /* Convert all the generic bits. */
802         security_acl_map_generic(psd->dacl, &file_generic_mapping);
803         security_acl_map_generic(psd->sacl, &file_generic_mapping);
804
805         if (DEBUGLEVEL >= 10) {
806                 DEBUG(10,("set_sd for file %s\n", fsp_str_dbg(fsp)));
807                 NDR_PRINT_DEBUG(security_descriptor, psd);
808         }
809
810         status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, psd);
811
812         TALLOC_FREE(psd);
813
814         return status;
815 }
816
817 /****************************************************************************
818  Read a list of EA names and data from an incoming data buffer. Create an ea_list with them.
819 ****************************************************************************/
820
821 struct ea_list *read_nttrans_ea_list(TALLOC_CTX *ctx, const char *pdata, size_t data_size)
822 {
823         struct ea_list *ea_list_head = NULL;
824         size_t offset = 0;
825
826         if (data_size < 4) {
827                 return NULL;
828         }
829
830         while (offset + 4 <= data_size) {
831                 size_t next_offset = IVAL(pdata,offset);
832                 struct ea_list *eal = read_ea_list_entry(ctx, pdata + offset + 4, data_size - offset - 4, NULL);
833
834                 if (!eal) {
835                         return NULL;
836                 }
837
838                 DLIST_ADD_END(ea_list_head, eal, struct ea_list *);
839                 if (next_offset == 0) {
840                         break;
841                 }
842                 offset += next_offset;
843         }
844
845         return ea_list_head;
846 }
847
848 /****************************************************************************
849  Reply to a NT_TRANSACT_CREATE call (needs to process SD's).
850 ****************************************************************************/
851
852 static void call_nt_transact_create(connection_struct *conn,
853                                     struct smb_request *req,
854                                     uint16 **ppsetup, uint32 setup_count,
855                                     char **ppparams, uint32 parameter_count,
856                                     char **ppdata, uint32 data_count,
857                                     uint32 max_data_count)
858 {
859         struct smb_filename *smb_fname = NULL;
860         char *fname = NULL;
861         char *params = *ppparams;
862         char *data = *ppdata;
863         /* Breakout the oplock request bits so we can set the reply bits separately. */
864         uint32 fattr=0;
865         SMB_OFF_T file_len = 0;
866         int info = 0;
867         files_struct *fsp = NULL;
868         char *p = NULL;
869         uint32 flags;
870         uint32 access_mask;
871         uint32 file_attributes;
872         uint32 share_access;
873         uint32 create_disposition;
874         uint32 create_options;
875         uint32 sd_len;
876         struct security_descriptor *sd = NULL;
877         uint32 ea_len;
878         uint16 root_dir_fid;
879         struct timespec create_timespec;
880         struct timespec c_timespec;
881         struct timespec a_timespec;
882         struct timespec m_timespec;
883         struct timespec write_time_ts;
884         struct ea_list *ea_list = NULL;
885         NTSTATUS status;
886         size_t param_len;
887         uint64_t allocation_size;
888         int oplock_request;
889         uint8_t oplock_granted;
890         TALLOC_CTX *ctx = talloc_tos();
891
892         DEBUG(5,("call_nt_transact_create\n"));
893
894         /*
895          * If it's an IPC, use the pipe handler.
896          */
897
898         if (IS_IPC(conn)) {
899                 if (lp_nt_pipe_support()) {
900                         do_nt_transact_create_pipe(
901                                 conn, req,
902                                 ppsetup, setup_count,
903                                 ppparams, parameter_count,
904                                 ppdata, data_count);
905                         goto out;
906                 }
907                 reply_doserror(req, ERRDOS, ERRnoaccess);
908                 goto out;
909         }
910
911         /*
912          * Ensure minimum number of parameters sent.
913          */
914
915         if(parameter_count < 54) {
916                 DEBUG(0,("call_nt_transact_create - insufficient parameters (%u)\n", (unsigned int)parameter_count));
917                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
918                 goto out;
919         }
920
921         flags = IVAL(params,0);
922         access_mask = IVAL(params,8);
923         file_attributes = IVAL(params,20);
924         share_access = IVAL(params,24);
925         create_disposition = IVAL(params,28);
926         create_options = IVAL(params,32);
927         sd_len = IVAL(params,36);
928         ea_len = IVAL(params,40);
929         root_dir_fid = (uint16)IVAL(params,4);
930         allocation_size = (uint64_t)IVAL(params,12);
931 #ifdef LARGE_SMB_OFF_T
932         allocation_size |= (((uint64_t)IVAL(params,16)) << 32);
933 #endif
934
935         /*
936          * we need to remove ignored bits when they come directly from the client
937          * because we reuse some of them for internal stuff
938          */
939         create_options &= ~NTCREATEX_OPTIONS_MUST_IGNORE_MASK;
940
941         /* Ensure the data_len is correct for the sd and ea values given. */
942         if ((ea_len + sd_len > data_count)
943             || (ea_len > data_count) || (sd_len > data_count)
944             || (ea_len + sd_len < ea_len) || (ea_len + sd_len < sd_len)) {
945                 DEBUG(10, ("call_nt_transact_create - ea_len = %u, sd_len = "
946                            "%u, data_count = %u\n", (unsigned int)ea_len,
947                            (unsigned int)sd_len, (unsigned int)data_count));
948                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
949                 goto out;
950         }
951
952         if (sd_len) {
953                 DEBUG(10, ("call_nt_transact_create - sd_len = %d\n",
954                            sd_len));
955
956                 status = unmarshall_sec_desc(ctx, (uint8_t *)data, sd_len,
957                                              &sd);
958                 if (!NT_STATUS_IS_OK(status)) {
959                         DEBUG(10, ("call_nt_transact_create: "
960                                    "unmarshall_sec_desc failed: %s\n",
961                                    nt_errstr(status)));
962                         reply_nterror(req, status);
963                         goto out;
964                 }
965         }
966
967         if (ea_len) {
968                 if (!lp_ea_support(SNUM(conn))) {
969                         DEBUG(10, ("call_nt_transact_create - ea_len = %u but "
970                                    "EA's not supported.\n",
971                                    (unsigned int)ea_len));
972                         reply_nterror(req, NT_STATUS_EAS_NOT_SUPPORTED);
973                         goto out;
974                 }
975
976                 if (ea_len < 10) {
977                         DEBUG(10,("call_nt_transact_create - ea_len = %u - "
978                                   "too small (should be more than 10)\n",
979                                   (unsigned int)ea_len ));
980                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
981                         goto out;
982                 }
983
984                 /* We have already checked that ea_len <= data_count here. */
985                 ea_list = read_nttrans_ea_list(talloc_tos(), data + sd_len,
986                                                ea_len);
987                 if (ea_list == NULL) {
988                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
989                         goto out;
990                 }
991         }
992
993         srvstr_get_path(ctx, params, req->flags2, &fname,
994                         params+53, parameter_count-53,
995                         STR_TERMINATE, &status);
996         if (!NT_STATUS_IS_OK(status)) {
997                 reply_nterror(req, status);
998                 goto out;
999         }
1000
1001         status = filename_convert(ctx,
1002                                 conn,
1003                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
1004                                 fname,
1005                                 (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ?
1006                                         UCF_POSIX_PATHNAMES : 0,
1007                                 NULL,
1008                                 &smb_fname);
1009
1010         if (!NT_STATUS_IS_OK(status)) {
1011                 if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
1012                         reply_botherror(req,
1013                                 NT_STATUS_PATH_NOT_COVERED,
1014                                 ERRSRV, ERRbadpath);
1015                         goto out;
1016                 }
1017                 reply_nterror(req, status);
1018                 goto out;
1019         }
1020
1021         oplock_request = (flags & REQUEST_OPLOCK) ? EXCLUSIVE_OPLOCK : 0;
1022         if (oplock_request) {
1023                 oplock_request |= (flags & REQUEST_BATCH_OPLOCK)
1024                         ? BATCH_OPLOCK : 0;
1025         }
1026
1027         /*
1028          * Bug #6898 - clients using Windows opens should
1029          * never be able to set this attribute into the
1030          * VFS.
1031          */
1032         file_attributes &= ~FILE_FLAG_POSIX_SEMANTICS;
1033
1034         status = SMB_VFS_CREATE_FILE(
1035                 conn,                                   /* conn */
1036                 req,                                    /* req */
1037                 root_dir_fid,                           /* root_dir_fid */
1038                 smb_fname,                              /* fname */
1039                 access_mask,                            /* access_mask */
1040                 share_access,                           /* share_access */
1041                 create_disposition,                     /* create_disposition*/
1042                 create_options,                         /* create_options */
1043                 file_attributes,                        /* file_attributes */
1044                 oplock_request,                         /* oplock_request */
1045                 allocation_size,                        /* allocation_size */
1046                 sd,                                     /* sd */
1047                 ea_list,                                /* ea_list */
1048                 &fsp,                                   /* result */
1049                 &info);                                 /* pinfo */
1050
1051         if(!NT_STATUS_IS_OK(status)) {
1052                 if (open_was_deferred(req->mid)) {
1053                         /* We have re-scheduled this call, no error. */
1054                         return;
1055                 }
1056                 reply_openerror(req, status);
1057                 goto out;
1058         }
1059
1060         /* Ensure we're pointing at the correct stat struct. */
1061         TALLOC_FREE(smb_fname);
1062         smb_fname = fsp->fsp_name;
1063
1064         /*
1065          * If the caller set the extended oplock request bit
1066          * and we granted one (by whatever means) - set the
1067          * correct bit for extended oplock reply.
1068          */
1069
1070         if (oplock_request &&
1071             (lp_fake_oplocks(SNUM(conn))
1072              || EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))) {
1073
1074                 /*
1075                  * Exclusive oplock granted
1076                  */
1077
1078                 if (flags & REQUEST_BATCH_OPLOCK) {
1079                         oplock_granted = BATCH_OPLOCK_RETURN;
1080                 } else {
1081                         oplock_granted = EXCLUSIVE_OPLOCK_RETURN;
1082                 }
1083         } else if (fsp->oplock_type == LEVEL_II_OPLOCK) {
1084                 oplock_granted = LEVEL_II_OPLOCK_RETURN;
1085         } else {
1086                 oplock_granted = NO_OPLOCK_RETURN;
1087         }
1088
1089         file_len = smb_fname->st.st_ex_size;
1090
1091         /* Realloc the size of parameters and data we will return */
1092         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1093                 /* Extended response is 32 more byyes. */
1094                 param_len = 101;
1095         } else {
1096                 param_len = 69;
1097         }
1098         params = nttrans_realloc(ppparams, param_len);
1099         if(params == NULL) {
1100                 reply_doserror(req, ERRDOS, ERRnomem);
1101                 goto out;
1102         }
1103
1104         p = params;
1105         SCVAL(p, 0, oplock_granted);
1106
1107         p += 2;
1108         SSVAL(p,0,fsp->fnum);
1109         p += 2;
1110         if ((create_disposition == FILE_SUPERSEDE)
1111             && (info == FILE_WAS_OVERWRITTEN)) {
1112                 SIVAL(p,0,FILE_WAS_SUPERSEDED);
1113         } else {
1114                 SIVAL(p,0,info);
1115         }
1116         p += 8;
1117
1118         fattr = dos_mode(conn, smb_fname);
1119         if (fattr == 0) {
1120                 fattr = FILE_ATTRIBUTE_NORMAL;
1121         }
1122
1123         /* Deal with other possible opens having a modified
1124            write time. JRA. */
1125         ZERO_STRUCT(write_time_ts);
1126         get_file_infos(fsp->file_id, NULL, &write_time_ts);
1127         if (!null_timespec(write_time_ts)) {
1128                 update_stat_ex_mtime(&smb_fname->st, write_time_ts);
1129         }
1130
1131         /* Create time. */
1132         create_timespec = get_create_timespec(conn, fsp, smb_fname);
1133         a_timespec = smb_fname->st.st_ex_atime;
1134         m_timespec = smb_fname->st.st_ex_mtime;
1135         c_timespec = get_change_timespec(conn, fsp, smb_fname);
1136
1137         if (lp_dos_filetime_resolution(SNUM(conn))) {
1138                 dos_filetime_timespec(&create_timespec);
1139                 dos_filetime_timespec(&a_timespec);
1140                 dos_filetime_timespec(&m_timespec);
1141                 dos_filetime_timespec(&c_timespec);
1142         }
1143
1144         put_long_date_timespec(conn->ts_res, p, create_timespec); /* create time. */
1145         p += 8;
1146         put_long_date_timespec(conn->ts_res, p, a_timespec); /* access time */
1147         p += 8;
1148         put_long_date_timespec(conn->ts_res, p, m_timespec); /* write time */
1149         p += 8;
1150         put_long_date_timespec(conn->ts_res, p, c_timespec); /* change time */
1151         p += 8;
1152         SIVAL(p,0,fattr); /* File Attributes. */
1153         p += 4;
1154         SOFF_T(p, 0, SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st));
1155         p += 8;
1156         SOFF_T(p,0,file_len);
1157         p += 8;
1158         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1159                 SSVAL(p,2,0x7);
1160         }
1161         p += 4;
1162         SCVAL(p,0,fsp->is_directory ? 1 : 0);
1163
1164         if (flags & EXTENDED_RESPONSE_REQUIRED) {
1165                 uint32 perms = 0;
1166                 p += 25;
1167                 if (fsp->is_directory ||
1168                     can_write_to_file(conn, smb_fname)) {
1169                         perms = FILE_GENERIC_ALL;
1170                 } else {
1171                         perms = FILE_GENERIC_READ|FILE_EXECUTE;
1172                 }
1173                 SIVAL(p,0,perms);
1174         }
1175
1176         DEBUG(5,("call_nt_transact_create: open name = %s\n",
1177                  smb_fname_str_dbg(smb_fname)));
1178
1179         /* Send the required number of replies */
1180         send_nt_replies(conn, req, NT_STATUS_OK, params, param_len, *ppdata, 0);
1181  out:
1182         return;
1183 }
1184
1185 /****************************************************************************
1186  Reply to a NT CANCEL request.
1187  conn POINTER CAN BE NULL HERE !
1188 ****************************************************************************/
1189
1190 void reply_ntcancel(struct smb_request *req)
1191 {
1192         /*
1193          * Go through and cancel any pending change notifies.
1194          */
1195
1196         START_PROFILE(SMBntcancel);
1197         srv_cancel_sign_response(smbd_server_conn);
1198         remove_pending_change_notify_requests_by_mid(req->mid);
1199         remove_pending_lock_requests_by_mid(req->mid);
1200
1201         DEBUG(3,("reply_ntcancel: cancel called on mid = %d.\n", req->mid));
1202
1203         END_PROFILE(SMBntcancel);
1204         return;
1205 }
1206
1207 /****************************************************************************
1208  Copy a file.
1209 ****************************************************************************/
1210
1211 static NTSTATUS copy_internals(TALLOC_CTX *ctx,
1212                                 connection_struct *conn,
1213                                 struct smb_request *req,
1214                                 struct smb_filename *smb_fname_src,
1215                                 struct smb_filename *smb_fname_dst,
1216                                 uint32 attrs)
1217 {
1218         files_struct *fsp1,*fsp2;
1219         uint32 fattr;
1220         int info;
1221         SMB_OFF_T ret=-1;
1222         NTSTATUS status = NT_STATUS_OK;
1223         char *parent;
1224
1225         if (!CAN_WRITE(conn)) {
1226                 status = NT_STATUS_MEDIA_WRITE_PROTECTED;
1227                 goto out;
1228         }
1229
1230         /* Source must already exist. */
1231         if (!VALID_STAT(smb_fname_src->st)) {
1232                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1233                 goto out;
1234         }
1235
1236         /* Ensure attributes match. */
1237         fattr = dos_mode(conn, smb_fname_src);
1238         if ((fattr & ~attrs) & (aHIDDEN | aSYSTEM)) {
1239                 status = NT_STATUS_NO_SUCH_FILE;
1240                 goto out;
1241         }
1242
1243         /* Disallow if dst file already exists. */
1244         if (VALID_STAT(smb_fname_dst->st)) {
1245                 status = NT_STATUS_OBJECT_NAME_COLLISION;
1246                 goto out;
1247         }
1248
1249         /* No links from a directory. */
1250         if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
1251                 status = NT_STATUS_FILE_IS_A_DIRECTORY;
1252                 goto out;
1253         }
1254
1255         DEBUG(10,("copy_internals: doing file copy %s to %s\n",
1256                   smb_fname_str_dbg(smb_fname_src),
1257                   smb_fname_str_dbg(smb_fname_dst)));
1258
1259         status = SMB_VFS_CREATE_FILE(
1260                 conn,                                   /* conn */
1261                 req,                                    /* req */
1262                 0,                                      /* root_dir_fid */
1263                 smb_fname_src,                          /* fname */
1264                 FILE_READ_DATA,                         /* access_mask */
1265                 (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
1266                     FILE_SHARE_DELETE),
1267                 FILE_OPEN,                              /* create_disposition*/
1268                 0,                                      /* create_options */
1269                 FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
1270                 NO_OPLOCK,                              /* oplock_request */
1271                 0,                                      /* allocation_size */
1272                 NULL,                                   /* sd */
1273                 NULL,                                   /* ea_list */
1274                 &fsp1,                                  /* result */
1275                 &info);                                 /* pinfo */
1276
1277         if (!NT_STATUS_IS_OK(status)) {
1278                 goto out;
1279         }
1280
1281         status = SMB_VFS_CREATE_FILE(
1282                 conn,                                   /* conn */
1283                 req,                                    /* req */
1284                 0,                                      /* root_dir_fid */
1285                 smb_fname_dst,                          /* fname */
1286                 FILE_WRITE_DATA,                        /* access_mask */
1287                 (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
1288                     FILE_SHARE_DELETE),
1289                 FILE_CREATE,                            /* create_disposition*/
1290                 0,                                      /* create_options */
1291                 fattr,                                  /* file_attributes */
1292                 NO_OPLOCK,                              /* oplock_request */
1293                 0,                                      /* allocation_size */
1294                 NULL,                                   /* sd */
1295                 NULL,                                   /* ea_list */
1296                 &fsp2,                                  /* result */
1297                 &info);                                 /* pinfo */
1298
1299         if (!NT_STATUS_IS_OK(status)) {
1300                 close_file(NULL, fsp1, ERROR_CLOSE);
1301                 goto out;
1302         }
1303
1304         if (smb_fname_src->st.st_ex_size) {
1305                 ret = vfs_transfer_file(fsp1, fsp2, smb_fname_src->st.st_ex_size);
1306         }
1307
1308         /*
1309          * As we are opening fsp1 read-only we only expect
1310          * an error on close on fsp2 if we are out of space.
1311          * Thus we don't look at the error return from the
1312          * close of fsp1.
1313          */
1314         close_file(NULL, fsp1, NORMAL_CLOSE);
1315
1316         /* Ensure the modtime is set correctly on the destination file. */
1317         set_close_write_time(fsp2, smb_fname_src->st.st_ex_mtime);
1318
1319         status = close_file(NULL, fsp2, NORMAL_CLOSE);
1320
1321         /* Grrr. We have to do this as open_file_ntcreate adds aARCH when it
1322            creates the file. This isn't the correct thing to do in the copy
1323            case. JRA */
1324         if (!parent_dirname(talloc_tos(), smb_fname_dst->base_name, &parent,
1325                             NULL)) {
1326                 status = NT_STATUS_NO_MEMORY;
1327                 goto out;
1328         }
1329         file_set_dosmode(conn, smb_fname_dst, fattr, parent, false);
1330         TALLOC_FREE(parent);
1331
1332         if (ret < (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
1333                 status = NT_STATUS_DISK_FULL;
1334                 goto out;
1335         }
1336  out:
1337         if (!NT_STATUS_IS_OK(status)) {
1338                 DEBUG(3,("copy_internals: Error %s copy file %s to %s\n",
1339                         nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
1340                         smb_fname_str_dbg(smb_fname_dst)));
1341         }
1342
1343         return status;
1344 }
1345
1346 /****************************************************************************
1347  Reply to a NT rename request.
1348 ****************************************************************************/
1349
1350 void reply_ntrename(struct smb_request *req)
1351 {
1352         connection_struct *conn = req->conn;
1353         struct smb_filename *smb_fname_old = NULL;
1354         struct smb_filename *smb_fname_new = NULL;
1355         char *oldname = NULL;
1356         char *newname = NULL;
1357         const char *p;
1358         NTSTATUS status;
1359         bool src_has_wcard = False;
1360         bool dest_has_wcard = False;
1361         uint32 attrs;
1362         uint32_t ucf_flags_src = 0;
1363         uint32_t ucf_flags_dst = 0;
1364         uint16 rename_type;
1365         TALLOC_CTX *ctx = talloc_tos();
1366
1367         START_PROFILE(SMBntrename);
1368
1369         if (req->wct < 4) {
1370                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1371                 goto out;
1372         }
1373
1374         attrs = SVAL(req->vwv+0, 0);
1375         rename_type = SVAL(req->vwv+1, 0);
1376
1377         p = (const char *)req->buf + 1;
1378         p += srvstr_get_path_req_wcard(ctx, req, &oldname, p, STR_TERMINATE,
1379                                        &status, &src_has_wcard);
1380         if (!NT_STATUS_IS_OK(status)) {
1381                 reply_nterror(req, status);
1382                 goto out;
1383         }
1384
1385         if (ms_has_wild(oldname)) {
1386                 reply_nterror(req, NT_STATUS_OBJECT_PATH_SYNTAX_BAD);
1387                 goto out;
1388         }
1389
1390         p++;
1391         p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
1392                                        &status, &dest_has_wcard);
1393         if (!NT_STATUS_IS_OK(status)) {
1394                 reply_nterror(req, status);
1395                 goto out;
1396         }
1397
1398         /* The newname must begin with a ':' if the oldname contains a ':'. */
1399         if (strchr_m(oldname, ':') && (newname[0] != ':')) {
1400                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1401                 goto out;
1402         }
1403
1404         /*
1405          * If this is a rename operation, allow wildcards and save the
1406          * destination's last component.
1407          */
1408         if (rename_type == RENAME_FLAG_RENAME) {
1409                 ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP;
1410                 ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP;
1411         }
1412
1413         /* rename_internals() calls unix_convert(), so don't call it here. */
1414         status = filename_convert(ctx, conn,
1415                                   req->flags2 & FLAGS2_DFS_PATHNAMES,
1416                                   oldname,
1417                                   ucf_flags_src,
1418                                   NULL,
1419                                   &smb_fname_old);
1420         if (!NT_STATUS_IS_OK(status)) {
1421                 if (NT_STATUS_EQUAL(status,
1422                                     NT_STATUS_PATH_NOT_COVERED)) {
1423                         reply_botherror(req,
1424                                         NT_STATUS_PATH_NOT_COVERED,
1425                                         ERRSRV, ERRbadpath);
1426                         goto out;
1427                 }
1428                 reply_nterror(req, status);
1429                 goto out;
1430         }
1431
1432         status = filename_convert(ctx, conn,
1433                                   req->flags2 & FLAGS2_DFS_PATHNAMES,
1434                                   newname,
1435                                   ucf_flags_dst,
1436                                   &dest_has_wcard,
1437                                   &smb_fname_new);
1438         if (!NT_STATUS_IS_OK(status)) {
1439                 if (NT_STATUS_EQUAL(status,
1440                                     NT_STATUS_PATH_NOT_COVERED)) {
1441                         reply_botherror(req,
1442                                         NT_STATUS_PATH_NOT_COVERED,
1443                                         ERRSRV, ERRbadpath);
1444                         goto out;
1445                 }
1446                 reply_nterror(req, status);
1447                 goto out;
1448         }
1449
1450         DEBUG(3,("reply_ntrename: %s -> %s\n",
1451                  smb_fname_str_dbg(smb_fname_old),
1452                  smb_fname_str_dbg(smb_fname_new)));
1453
1454         switch(rename_type) {
1455                 case RENAME_FLAG_RENAME:
1456                         status = rename_internals(ctx, conn, req,
1457                                                   smb_fname_old, smb_fname_new,
1458                                                   attrs, False, src_has_wcard,
1459                                                   dest_has_wcard,
1460                                                   DELETE_ACCESS);
1461                         break;
1462                 case RENAME_FLAG_HARD_LINK:
1463                         if (src_has_wcard || dest_has_wcard) {
1464                                 /* No wildcards. */
1465                                 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1466                         } else {
1467                                 status = hardlink_internals(ctx, conn,
1468                                                             smb_fname_old,
1469                                                             smb_fname_new);
1470                         }
1471                         break;
1472                 case RENAME_FLAG_COPY:
1473                         if (src_has_wcard || dest_has_wcard) {
1474                                 /* No wildcards. */
1475                                 status = NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
1476                         } else {
1477                                 status = copy_internals(ctx, conn, req,
1478                                                         smb_fname_old,
1479                                                         smb_fname_new,
1480                                                         attrs);
1481                         }
1482                         break;
1483                 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
1484                         status = NT_STATUS_INVALID_PARAMETER;
1485                         break;
1486                 default:
1487                         status = NT_STATUS_ACCESS_DENIED; /* Default error. */
1488                         break;
1489         }
1490
1491         if (!NT_STATUS_IS_OK(status)) {
1492                 if (open_was_deferred(req->mid)) {
1493                         /* We have re-scheduled this call. */
1494                         goto out;
1495                 }
1496
1497                 reply_nterror(req, status);
1498                 goto out;
1499         }
1500
1501         reply_outbuf(req, 0, 0);
1502  out:
1503         END_PROFILE(SMBntrename);
1504         return;
1505 }
1506
1507 /****************************************************************************
1508  Reply to a notify change - queue the request and
1509  don't allow a directory to be opened.
1510 ****************************************************************************/
1511
1512 static void smbd_smb1_notify_reply(struct smb_request *req,
1513                                    NTSTATUS error_code,
1514                                    uint8_t *buf, size_t len)
1515 {
1516         send_nt_replies(req->conn, req, error_code, (char *)buf, len, NULL, 0);
1517 }
1518
1519 static void call_nt_transact_notify_change(connection_struct *conn,
1520                                            struct smb_request *req,
1521                                            uint16 **ppsetup,
1522                                            uint32 setup_count,
1523                                            char **ppparams,
1524                                            uint32 parameter_count,
1525                                            char **ppdata, uint32 data_count,
1526                                            uint32 max_data_count,
1527                                            uint32 max_param_count)
1528 {
1529         uint16 *setup = *ppsetup;
1530         files_struct *fsp;
1531         uint32 filter;
1532         NTSTATUS status;
1533         bool recursive;
1534
1535         if(setup_count < 6) {
1536                 reply_doserror(req, ERRDOS, ERRbadfunc);
1537                 return;
1538         }
1539
1540         fsp = file_fsp(req, SVAL(setup,4));
1541         filter = IVAL(setup, 0);
1542         recursive = (SVAL(setup, 6) != 0) ? True : False;
1543
1544         DEBUG(3,("call_nt_transact_notify_change\n"));
1545
1546         if(!fsp) {
1547                 reply_doserror(req, ERRDOS, ERRbadfid);
1548                 return;
1549         }
1550
1551         {
1552                 char *filter_string;
1553
1554                 if (!(filter_string = notify_filter_string(NULL, filter))) {
1555                         reply_nterror(req,NT_STATUS_NO_MEMORY);
1556                         return;
1557                 }
1558
1559                 DEBUG(3,("call_nt_transact_notify_change: notify change "
1560                          "called on %s, filter = %s, recursive = %d\n",
1561                          fsp_str_dbg(fsp), filter_string, recursive));
1562
1563                 TALLOC_FREE(filter_string);
1564         }
1565
1566         if((!fsp->is_directory) || (conn != fsp->conn)) {
1567                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1568                 return;
1569         }
1570
1571         if (fsp->notify == NULL) {
1572
1573                 status = change_notify_create(fsp, filter, recursive);
1574
1575                 if (!NT_STATUS_IS_OK(status)) {
1576                         DEBUG(10, ("change_notify_create returned %s\n",
1577                                    nt_errstr(status)));
1578                         reply_nterror(req, status);
1579                         return;
1580                 }
1581         }
1582
1583         if (fsp->notify->num_changes != 0) {
1584
1585                 /*
1586                  * We've got changes pending, respond immediately
1587                  */
1588
1589                 /*
1590                  * TODO: write a torture test to check the filtering behaviour
1591                  * here.
1592                  */
1593
1594                 change_notify_reply(fsp->conn, req,
1595                                     NT_STATUS_OK,
1596                                     max_param_count,
1597                                     fsp->notify,
1598                                     smbd_smb1_notify_reply);
1599
1600                 /*
1601                  * change_notify_reply() above has independently sent its
1602                  * results
1603                  */
1604                 return;
1605         }
1606
1607         /*
1608          * No changes pending, queue the request
1609          */
1610
1611         status = change_notify_add_request(req,
1612                         max_param_count,
1613                         filter,
1614                         recursive, fsp,
1615                         smbd_smb1_notify_reply);
1616         if (!NT_STATUS_IS_OK(status)) {
1617                 reply_nterror(req, status);
1618         }
1619         return;
1620 }
1621
1622 /****************************************************************************
1623  Reply to an NT transact rename command.
1624 ****************************************************************************/
1625
1626 static void call_nt_transact_rename(connection_struct *conn,
1627                                     struct smb_request *req,
1628                                     uint16 **ppsetup, uint32 setup_count,
1629                                     char **ppparams, uint32 parameter_count,
1630                                     char **ppdata, uint32 data_count,
1631                                     uint32 max_data_count)
1632 {
1633         char *params = *ppparams;
1634         char *new_name = NULL;
1635         files_struct *fsp = NULL;
1636         bool dest_has_wcard = False;
1637         NTSTATUS status;
1638         TALLOC_CTX *ctx = talloc_tos();
1639
1640         if(parameter_count < 5) {
1641                 reply_doserror(req, ERRDOS, ERRbadfunc);
1642                 return;
1643         }
1644
1645         fsp = file_fsp(req, SVAL(params, 0));
1646         if (!check_fsp(conn, req, fsp)) {
1647                 return;
1648         }
1649         srvstr_get_path_wcard(ctx, params, req->flags2, &new_name, params+4,
1650                               parameter_count - 4,
1651                               STR_TERMINATE, &status, &dest_has_wcard);
1652         if (!NT_STATUS_IS_OK(status)) {
1653                 reply_nterror(req, status);
1654                 return;
1655         }
1656
1657         /*
1658          * W2K3 ignores this request as the RAW-RENAME test
1659          * demonstrates, so we do.
1660          */
1661         send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1662
1663         DEBUG(3,("nt transact rename from = %s, to = %s ignored!\n",
1664                  fsp_str_dbg(fsp), new_name));
1665
1666         return;
1667 }
1668
1669 /******************************************************************************
1670  Fake up a completely empty SD.
1671 *******************************************************************************/
1672
1673 static NTSTATUS get_null_nt_acl(TALLOC_CTX *mem_ctx, SEC_DESC **ppsd)
1674 {
1675         size_t sd_size;
1676
1677         *ppsd = make_standard_sec_desc( mem_ctx, &global_sid_World, &global_sid_World, NULL, &sd_size);
1678         if(!*ppsd) {
1679                 DEBUG(0,("get_null_nt_acl: Unable to malloc space for security descriptor.\n"));
1680                 return NT_STATUS_NO_MEMORY;
1681         }
1682
1683         return NT_STATUS_OK;
1684 }
1685
1686 /****************************************************************************
1687  Reply to query a security descriptor.
1688 ****************************************************************************/
1689
1690 static void call_nt_transact_query_security_desc(connection_struct *conn,
1691                                                  struct smb_request *req,
1692                                                  uint16 **ppsetup,
1693                                                  uint32 setup_count,
1694                                                  char **ppparams,
1695                                                  uint32 parameter_count,
1696                                                  char **ppdata,
1697                                                  uint32 data_count,
1698                                                  uint32 max_data_count)
1699 {
1700         char *params = *ppparams;
1701         char *data = *ppdata;
1702         SEC_DESC *psd = NULL;
1703         size_t sd_size;
1704         uint32 security_info_wanted;
1705         files_struct *fsp = NULL;
1706         NTSTATUS status;
1707         DATA_BLOB blob;
1708
1709         if(parameter_count < 8) {
1710                 reply_doserror(req, ERRDOS, ERRbadfunc);
1711                 return;
1712         }
1713
1714         fsp = file_fsp(req, SVAL(params,0));
1715         if(!fsp) {
1716                 reply_doserror(req, ERRDOS, ERRbadfid);
1717                 return;
1718         }
1719
1720         security_info_wanted = IVAL(params,4);
1721
1722         DEBUG(3,("call_nt_transact_query_security_desc: file = %s, "
1723                  "info_wanted = 0x%x\n", fsp_str_dbg(fsp),
1724                  (unsigned int)security_info_wanted));
1725
1726         params = nttrans_realloc(ppparams, 4);
1727         if(params == NULL) {
1728                 reply_doserror(req, ERRDOS, ERRnomem);
1729                 return;
1730         }
1731
1732         /*
1733          * Get the permissions to return.
1734          */
1735
1736         if (!lp_nt_acl_support(SNUM(conn))) {
1737                 status = get_null_nt_acl(talloc_tos(), &psd);
1738         } else {
1739                 status = SMB_VFS_FGET_NT_ACL(
1740                         fsp, security_info_wanted, &psd);
1741         }
1742         if (!NT_STATUS_IS_OK(status)) {
1743                 reply_nterror(req, status);
1744                 return;
1745         }
1746
1747         /* If the SACL/DACL is NULL, but was requested, we mark that it is
1748          * present in the reply to match Windows behavior */
1749         if (psd->sacl == NULL &&
1750             security_info_wanted & SACL_SECURITY_INFORMATION)
1751                 psd->type |= SEC_DESC_SACL_PRESENT;
1752         if (psd->dacl == NULL &&
1753             security_info_wanted & DACL_SECURITY_INFORMATION)
1754                 psd->type |= SEC_DESC_DACL_PRESENT;
1755
1756         sd_size = ndr_size_security_descriptor(psd, NULL, 0);
1757
1758         DEBUG(3,("call_nt_transact_query_security_desc: sd_size = %lu.\n",(unsigned long)sd_size));
1759
1760         if (DEBUGLEVEL >= 10) {
1761                 DEBUG(10,("call_nt_transact_query_security_desc for file %s\n",
1762                           fsp_str_dbg(fsp)));
1763                 NDR_PRINT_DEBUG(security_descriptor, psd);
1764         }
1765
1766         SIVAL(params,0,(uint32)sd_size);
1767
1768         if (max_data_count < sd_size) {
1769                 send_nt_replies(conn, req, NT_STATUS_BUFFER_TOO_SMALL,
1770                                 params, 4, *ppdata, 0);
1771                 return;
1772         }
1773
1774         /*
1775          * Allocate the data we will point this at.
1776          */
1777
1778         data = nttrans_realloc(ppdata, sd_size);
1779         if(data == NULL) {
1780                 reply_doserror(req, ERRDOS, ERRnomem);
1781                 return;
1782         }
1783
1784         status = marshall_sec_desc(talloc_tos(), psd,
1785                                    &blob.data, &blob.length);
1786
1787         if (!NT_STATUS_IS_OK(status)) {
1788                 reply_nterror(req, status);
1789                 return;
1790         }
1791
1792         SMB_ASSERT(sd_size == blob.length);
1793         memcpy(data, blob.data, sd_size);
1794
1795         send_nt_replies(conn, req, NT_STATUS_OK, params, 4, data, (int)sd_size);
1796
1797         return;
1798 }
1799
1800 /****************************************************************************
1801  Reply to set a security descriptor. Map to UNIX perms or POSIX ACLs.
1802 ****************************************************************************/
1803
1804 static void call_nt_transact_set_security_desc(connection_struct *conn,
1805                                                struct smb_request *req,
1806                                                uint16 **ppsetup,
1807                                                uint32 setup_count,
1808                                                char **ppparams,
1809                                                uint32 parameter_count,
1810                                                char **ppdata,
1811                                                uint32 data_count,
1812                                                uint32 max_data_count)
1813 {
1814         char *params= *ppparams;
1815         char *data = *ppdata;
1816         files_struct *fsp = NULL;
1817         uint32 security_info_sent = 0;
1818         NTSTATUS status;
1819
1820         if(parameter_count < 8) {
1821                 reply_doserror(req, ERRDOS, ERRbadfunc);
1822                 return;
1823         }
1824
1825         if((fsp = file_fsp(req, SVAL(params,0))) == NULL) {
1826                 reply_doserror(req, ERRDOS, ERRbadfid);
1827                 return;
1828         }
1829
1830         if(!lp_nt_acl_support(SNUM(conn))) {
1831                 goto done;
1832         }
1833
1834         security_info_sent = IVAL(params,4);
1835
1836         DEBUG(3,("call_nt_transact_set_security_desc: file = %s, sent 0x%x\n",
1837                  fsp_str_dbg(fsp), (unsigned int)security_info_sent));
1838
1839         if (data_count == 0) {
1840                 reply_doserror(req, ERRDOS, ERRnoaccess);
1841                 return;
1842         }
1843
1844         status = set_sd(fsp, (uint8 *)data, data_count, security_info_sent);
1845
1846         if (!NT_STATUS_IS_OK(status)) {
1847                 reply_nterror(req, status);
1848                 return;
1849         }
1850
1851   done:
1852         send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1853         return;
1854 }
1855
1856 /****************************************************************************
1857  Reply to NT IOCTL
1858 ****************************************************************************/
1859
1860 static void call_nt_transact_ioctl(connection_struct *conn,
1861                                    struct smb_request *req,
1862                                    uint16 **ppsetup, uint32 setup_count,
1863                                    char **ppparams, uint32 parameter_count,
1864                                    char **ppdata, uint32 data_count,
1865                                    uint32 max_data_count)
1866 {
1867         uint32 function;
1868         uint16 fidnum;
1869         files_struct *fsp;
1870         uint8 isFSctl;
1871         uint8 compfilter;
1872         char *pdata = *ppdata;
1873
1874         if (setup_count != 8) {
1875                 DEBUG(3,("call_nt_transact_ioctl: invalid setup count %d\n", setup_count));
1876                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
1877                 return;
1878         }
1879
1880         function = IVAL(*ppsetup, 0);
1881         fidnum = SVAL(*ppsetup, 4);
1882         isFSctl = CVAL(*ppsetup, 6);
1883         compfilter = CVAL(*ppsetup, 7);
1884
1885         DEBUG(10,("call_nt_transact_ioctl: function[0x%08X] FID[0x%04X] isFSctl[0x%02X] compfilter[0x%02X]\n", 
1886                  function, fidnum, isFSctl, compfilter));
1887
1888         fsp=file_fsp(req, fidnum);
1889         /* this check is done in each implemented function case for now
1890            because I don't want to break anything... --metze
1891         FSP_BELONGS_CONN(fsp,conn);*/
1892
1893         SMB_PERFCOUNT_SET_IOCTL(&req->pcd, function);
1894
1895         switch (function) {
1896         case FSCTL_SET_SPARSE:
1897                 /* pretend this succeeded - tho strictly we should
1898                    mark the file sparse (if the local fs supports it)
1899                    so we can know if we need to pre-allocate or not */
1900
1901                 DEBUG(10,("FSCTL_SET_SPARSE: called on FID[0x%04X](but not implemented)\n", fidnum));
1902                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
1903                 return;
1904
1905         case FSCTL_CREATE_OR_GET_OBJECT_ID:
1906         {
1907                 unsigned char objid[16];
1908
1909                 /* This should return the object-id on this file.
1910                  * I think I'll make this be the inode+dev. JRA.
1911                  */
1912
1913                 DEBUG(10,("FSCTL_CREATE_OR_GET_OBJECT_ID: called on FID[0x%04X]\n",fidnum));
1914
1915                 if (!fsp_belongs_conn(conn, req, fsp)) {
1916                         return;
1917                 }
1918
1919                 data_count = 64;
1920                 pdata = nttrans_realloc(ppdata, data_count);
1921                 if (pdata == NULL) {
1922                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1923                         return;
1924                 }
1925
1926                 /* For backwards compatibility only store the dev/inode. */
1927                 push_file_id_16(pdata, &fsp->file_id);
1928                 memcpy(pdata+16,create_volume_objectid(conn,objid),16);
1929                 push_file_id_16(pdata+32, &fsp->file_id);
1930                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
1931                                 pdata, data_count);
1932                 return;
1933         }
1934
1935         case FSCTL_GET_REPARSE_POINT:
1936                 /* pretend this fail - my winXP does it like this
1937                  * --metze
1938                  */
1939
1940                 DEBUG(10,("FSCTL_GET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1941                 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1942                 return;
1943
1944         case FSCTL_SET_REPARSE_POINT:
1945                 /* pretend this fail - I'm assuming this because of the FSCTL_GET_REPARSE_POINT case.
1946                  * --metze
1947                  */
1948
1949                 DEBUG(10,("FSCTL_SET_REPARSE_POINT: called on FID[0x%04X](but not implemented)\n",fidnum));
1950                 reply_nterror(req, NT_STATUS_NOT_A_REPARSE_POINT);
1951                 return;
1952
1953         case FSCTL_GET_SHADOW_COPY_DATA: /* don't know if this name is right...*/
1954         {
1955                 /*
1956                  * This is called to retrieve the number of Shadow Copies (a.k.a. snapshots)
1957                  * and return their volume names.  If max_data_count is 16, then it is just
1958                  * asking for the number of volumes and length of the combined names.
1959                  *
1960                  * pdata is the data allocated by our caller, but that uses
1961                  * total_data_count (which is 0 in our case) rather than max_data_count.
1962                  * Allocate the correct amount and return the pointer to let
1963                  * it be deallocated when we return.
1964                  */
1965                 SHADOW_COPY_DATA *shadow_data = NULL;
1966                 TALLOC_CTX *shadow_mem_ctx = NULL;
1967                 bool labels = False;
1968                 uint32 labels_data_count = 0;
1969                 uint32 i;
1970                 char *cur_pdata;
1971
1972                 if (!fsp_belongs_conn(conn, req, fsp)) {
1973                         return;
1974                 }
1975
1976                 if (max_data_count < 16) {
1977                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) < 16 is invalid!\n",
1978                                 max_data_count));
1979                         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
1980                         return;
1981                 }
1982
1983                 if (max_data_count > 16) {
1984                         labels = True;
1985                 }
1986
1987                 shadow_mem_ctx = talloc_init("SHADOW_COPY_DATA");
1988                 if (shadow_mem_ctx == NULL) {
1989                         DEBUG(0,("talloc_init(SHADOW_COPY_DATA) failed!\n"));
1990                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1991                         return;
1992                 }
1993
1994                 shadow_data = TALLOC_ZERO_P(shadow_mem_ctx,SHADOW_COPY_DATA);
1995                 if (shadow_data == NULL) {
1996                         DEBUG(0,("TALLOC_ZERO() failed!\n"));
1997                         talloc_destroy(shadow_mem_ctx);
1998                         reply_nterror(req, NT_STATUS_NO_MEMORY);
1999                         return;
2000                 }
2001
2002                 shadow_data->mem_ctx = shadow_mem_ctx;
2003
2004                 /*
2005                  * Call the VFS routine to actually do the work.
2006                  */
2007                 if (SMB_VFS_GET_SHADOW_COPY_DATA(fsp, shadow_data, labels)!=0) {
2008                         talloc_destroy(shadow_data->mem_ctx);
2009                         if (errno == ENOSYS) {
2010                                 DEBUG(5,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, not supported.\n", 
2011                                         conn->connectpath));
2012                                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2013                                 return;
2014                         } else {
2015                                 DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: connectpath %s, failed.\n", 
2016                                         conn->connectpath));
2017                                 reply_nterror(req, NT_STATUS_UNSUCCESSFUL);
2018                                 return;
2019                         }
2020                 }
2021
2022                 labels_data_count = (shadow_data->num_volumes*2*sizeof(SHADOW_COPY_LABEL))+2;
2023
2024                 if (!labels) {
2025                         data_count = 16;
2026                 } else {
2027                         data_count = 12+labels_data_count+4;
2028                 }
2029
2030                 if (max_data_count<data_count) {
2031                         DEBUG(0,("FSCTL_GET_SHADOW_COPY_DATA: max_data_count(%u) too small (%u) bytes needed!\n",
2032                                 max_data_count,data_count));
2033                         talloc_destroy(shadow_data->mem_ctx);
2034                         reply_nterror(req, NT_STATUS_BUFFER_TOO_SMALL);
2035                         return;
2036                 }
2037
2038                 pdata = nttrans_realloc(ppdata, data_count);
2039                 if (pdata == NULL) {
2040                         talloc_destroy(shadow_data->mem_ctx);
2041                         reply_nterror(req, NT_STATUS_NO_MEMORY);
2042                         return;
2043                 }
2044
2045                 cur_pdata = pdata;
2046
2047                 /* num_volumes 4 bytes */
2048                 SIVAL(pdata,0,shadow_data->num_volumes);
2049
2050                 if (labels) {
2051                         /* num_labels 4 bytes */
2052                         SIVAL(pdata,4,shadow_data->num_volumes);
2053                 }
2054
2055                 /* needed_data_count 4 bytes */
2056                 SIVAL(pdata, 8, labels_data_count+4);
2057
2058                 cur_pdata+=12;
2059
2060                 DEBUG(10,("FSCTL_GET_SHADOW_COPY_DATA: %u volumes for path[%s].\n",
2061                           shadow_data->num_volumes, fsp_str_dbg(fsp)));
2062                 if (labels && shadow_data->labels) {
2063                         for (i=0;i<shadow_data->num_volumes;i++) {
2064                                 srvstr_push(pdata, req->flags2,
2065                                             cur_pdata, shadow_data->labels[i],
2066                                             2*sizeof(SHADOW_COPY_LABEL),
2067                                             STR_UNICODE|STR_TERMINATE);
2068                                 cur_pdata+=2*sizeof(SHADOW_COPY_LABEL);
2069                                 DEBUGADD(10,("Label[%u]: '%s'\n",i,shadow_data->labels[i]));
2070                         }
2071                 }
2072
2073                 talloc_destroy(shadow_data->mem_ctx);
2074
2075                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0,
2076                                 pdata, data_count);
2077
2078                 return;
2079         }
2080
2081         case FSCTL_FIND_FILES_BY_SID: /* I hope this name is right */
2082         {
2083                 /* pretend this succeeded -
2084                  *
2085                  * we have to send back a list with all files owned by this SID
2086                  *
2087                  * but I have to check that --metze
2088                  */
2089                 DOM_SID sid;
2090                 uid_t uid;
2091                 size_t sid_len = MIN(data_count-4,SID_MAX_SIZE);
2092
2093                 DEBUG(10,("FSCTL_FIND_FILES_BY_SID: called on FID[0x%04X]\n",fidnum));
2094
2095                 if (!fsp_belongs_conn(conn, req, fsp)) {
2096                         return;
2097                 }
2098
2099                 /* unknown 4 bytes: this is not the length of the sid :-(  */
2100                 /*unknown = IVAL(pdata,0);*/
2101
2102                 sid_parse(pdata+4,sid_len,&sid);
2103                 DEBUGADD(10, ("for SID: %s\n", sid_string_dbg(&sid)));
2104
2105                 if (!sid_to_uid(&sid, &uid)) {
2106                         DEBUG(0,("sid_to_uid: failed, sid[%s] sid_len[%lu]\n",
2107                                  sid_string_dbg(&sid),
2108                                  (unsigned long)sid_len));
2109                         uid = (-1);
2110                 }
2111
2112                 /* we can take a look at the find source :-)
2113                  *
2114                  * find ./ -uid $uid  -name '*'   is what we need here
2115                  *
2116                  *
2117                  * and send 4bytes len and then NULL terminated unicode strings
2118                  * for each file
2119                  *
2120                  * but I don't know how to deal with the paged results
2121                  * (maybe we can hang the result anywhere in the fsp struct)
2122                  *
2123                  * we don't send all files at once
2124                  * and at the next we should *not* start from the beginning,
2125                  * so we have to cache the result
2126                  *
2127                  * --metze
2128                  */
2129
2130                 /* this works for now... */
2131                 send_nt_replies(conn, req, NT_STATUS_OK, NULL, 0, NULL, 0);
2132                 return;
2133         }
2134         default:
2135                 if (!logged_ioctl_message) {
2136                         logged_ioctl_message = true; /* Only print this once... */
2137                         DEBUG(0,("call_nt_transact_ioctl(0x%x): Currently not implemented.\n",
2138                                  function));
2139                 }
2140         }
2141
2142         reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
2143 }
2144
2145
2146 #ifdef HAVE_SYS_QUOTAS
2147 /****************************************************************************
2148  Reply to get user quota
2149 ****************************************************************************/
2150
2151 static void call_nt_transact_get_user_quota(connection_struct *conn,
2152                                             struct smb_request *req,
2153                                             uint16 **ppsetup,
2154                                             uint32 setup_count,
2155                                             char **ppparams,
2156                                             uint32 parameter_count,
2157                                             char **ppdata,
2158                                             uint32 data_count,
2159                                             uint32 max_data_count)
2160 {
2161         NTSTATUS nt_status = NT_STATUS_OK;
2162         char *params = *ppparams;
2163         char *pdata = *ppdata;
2164         char *entry;
2165         int data_len=0,param_len=0;
2166         int qt_len=0;
2167         int entry_len = 0;
2168         files_struct *fsp = NULL;
2169         uint16 level = 0;
2170         size_t sid_len;
2171         DOM_SID sid;
2172         bool start_enum = True;
2173         SMB_NTQUOTA_STRUCT qt;
2174         SMB_NTQUOTA_LIST *tmp_list;
2175         SMB_NTQUOTA_HANDLE *qt_handle = NULL;
2176
2177         ZERO_STRUCT(qt);
2178
2179         /* access check */
2180         if (conn->server_info->utok.uid != 0) {
2181                 DEBUG(1,("get_user_quota: access_denied service [%s] user "
2182                          "[%s]\n", lp_servicename(SNUM(conn)),
2183                          conn->server_info->unix_name));
2184                 reply_doserror(req, ERRDOS, ERRnoaccess);
2185                 return;
2186         }
2187
2188         /*
2189          * Ensure minimum number of parameters sent.
2190          */
2191
2192         if (parameter_count < 4) {
2193                 DEBUG(0,("TRANSACT_GET_USER_QUOTA: requires %d >= 4 bytes parameters\n",parameter_count));
2194                 reply_doserror(req, ERRDOS, ERRinvalidparam);
2195                 return;
2196         }
2197
2198         /* maybe we can check the quota_fnum */
2199         fsp = file_fsp(req, SVAL(params,0));
2200         if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2201                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2202                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2203                 return;
2204         }
2205
2206         /* the NULL pointer checking for fsp->fake_file_handle->pd
2207          * is done by CHECK_NTQUOTA_HANDLE_OK()
2208          */
2209         qt_handle = (SMB_NTQUOTA_HANDLE *)fsp->fake_file_handle->private_data;
2210
2211         level = SVAL(params,2);
2212
2213         /* unknown 12 bytes leading in params */
2214
2215         switch (level) {
2216                 case TRANSACT_GET_USER_QUOTA_LIST_CONTINUE:
2217                         /* seems that we should continue with the enum here --metze */
2218
2219                         if (qt_handle->quota_list!=NULL &&
2220                             qt_handle->tmp_list==NULL) {
2221
2222                                 /* free the list */
2223                                 free_ntquota_list(&(qt_handle->quota_list));
2224
2225                                 /* Realloc the size of parameters and data we will return */
2226                                 param_len = 4;
2227                                 params = nttrans_realloc(ppparams, param_len);
2228                                 if(params == NULL) {
2229                                         reply_doserror(req, ERRDOS, ERRnomem);
2230                                         return;
2231                                 }
2232
2233                                 data_len = 0;
2234                                 SIVAL(params,0,data_len);
2235
2236                                 break;
2237                         }
2238
2239                         start_enum = False;
2240
2241                 case TRANSACT_GET_USER_QUOTA_LIST_START:
2242
2243                         if (qt_handle->quota_list==NULL &&
2244                                 qt_handle->tmp_list==NULL) {
2245                                 start_enum = True;
2246                         }
2247
2248                         if (start_enum && vfs_get_user_ntquota_list(fsp,&(qt_handle->quota_list))!=0) {
2249                                 reply_doserror(req, ERRSRV, ERRerror);
2250                                 return;
2251                         }
2252
2253                         /* Realloc the size of parameters and data we will return */
2254                         param_len = 4;
2255                         params = nttrans_realloc(ppparams, param_len);
2256                         if(params == NULL) {
2257                                 reply_doserror(req, ERRDOS, ERRnomem);
2258                                 return;
2259                         }
2260
2261                         /* we should not trust the value in max_data_count*/
2262                         max_data_count = MIN(max_data_count,2048);
2263
2264                         pdata = nttrans_realloc(ppdata, max_data_count);/* should be max data count from client*/
2265                         if(pdata == NULL) {
2266                                 reply_doserror(req, ERRDOS, ERRnomem);
2267                                 return;
2268                         }
2269
2270                         entry = pdata;
2271
2272                         /* set params Size of returned Quota Data 4 bytes*/
2273                         /* but set it later when we know it */
2274
2275                         /* for each entry push the data */
2276
2277                         if (start_enum) {
2278                                 qt_handle->tmp_list = qt_handle->quota_list;
2279                         }
2280
2281                         tmp_list = qt_handle->tmp_list;
2282
2283                         for (;((tmp_list!=NULL)&&((qt_len +40+SID_MAX_SIZE)<max_data_count));
2284                                 tmp_list=tmp_list->next,entry+=entry_len,qt_len+=entry_len) {
2285
2286                                 sid_len = ndr_size_dom_sid(
2287                                         &tmp_list->quotas->sid, NULL, 0);
2288                                 entry_len = 40 + sid_len;
2289
2290                                 /* nextoffset entry 4 bytes */
2291                                 SIVAL(entry,0,entry_len);
2292
2293                                 /* then the len of the SID 4 bytes */
2294                                 SIVAL(entry,4,sid_len);
2295
2296                                 /* unknown data 8 bytes uint64_t */
2297                                 SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-metze*/
2298
2299                                 /* the used disk space 8 bytes uint64_t */
2300                                 SBIG_UINT(entry,16,tmp_list->quotas->usedspace);
2301
2302                                 /* the soft quotas 8 bytes uint64_t */
2303                                 SBIG_UINT(entry,24,tmp_list->quotas->softlim);
2304
2305                                 /* the hard quotas 8 bytes uint64_t */
2306                                 SBIG_UINT(entry,32,tmp_list->quotas->hardlim);
2307
2308                                 /* and now the SID */
2309                                 sid_linearize(entry+40, sid_len, &tmp_list->quotas->sid);
2310                         }
2311
2312                         qt_handle->tmp_list = tmp_list;
2313
2314                         /* overwrite the offset of the last entry */
2315                         SIVAL(entry-entry_len,0,0);
2316
2317                         data_len = 4+qt_len;
2318                         /* overwrite the params quota_data_len */
2319                         SIVAL(params,0,data_len);
2320
2321                         break;
2322
2323                 case TRANSACT_GET_USER_QUOTA_FOR_SID:
2324
2325                         /* unknown 4 bytes IVAL(pdata,0) */
2326
2327                         if (data_count < 8) {
2328                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %d bytes data\n",data_count,8));
2329                                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2330                                 return;
2331                         }
2332
2333                         sid_len = IVAL(pdata,4);
2334                         /* Ensure this is less than 1mb. */
2335                         if (sid_len > (1024*1024)) {
2336                                 reply_doserror(req, ERRDOS, ERRnomem);
2337                                 return;
2338                         }
2339
2340                         if (data_count < 8+sid_len) {
2341                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: requires %d >= %lu bytes data\n",data_count,(unsigned long)(8+sid_len)));
2342                                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2343                                 return;
2344                         }
2345
2346                         data_len = 4+40+sid_len;
2347
2348                         if (max_data_count < data_len) {
2349                                 DEBUG(0,("TRANSACT_GET_USER_QUOTA_FOR_SID: max_data_count(%d) < data_len(%d)\n",
2350                                         max_data_count, data_len));
2351                                 param_len = 4;
2352                                 SIVAL(params,0,data_len);
2353                                 data_len = 0;
2354                                 nt_status = NT_STATUS_BUFFER_TOO_SMALL;
2355                                 break;
2356                         }
2357
2358                         sid_parse(pdata+8,sid_len,&sid);
2359
2360                         if (vfs_get_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2361                                 ZERO_STRUCT(qt);
2362                                 /*
2363                                  * we have to return zero's in all fields
2364                                  * instead of returning an error here
2365                                  * --metze
2366                                  */
2367                         }
2368
2369                         /* Realloc the size of parameters and data we will return */
2370                         param_len = 4;
2371                         params = nttrans_realloc(ppparams, param_len);
2372                         if(params == NULL) {
2373                                 reply_doserror(req, ERRDOS, ERRnomem);
2374                                 return;
2375                         }
2376
2377                         pdata = nttrans_realloc(ppdata, data_len);
2378                         if(pdata == NULL) {
2379                                 reply_doserror(req, ERRDOS, ERRnomem);
2380                                 return;
2381                         }
2382
2383                         entry = pdata;
2384
2385                         /* set params Size of returned Quota Data 4 bytes*/
2386                         SIVAL(params,0,data_len);
2387
2388                         /* nextoffset entry 4 bytes */
2389                         SIVAL(entry,0,0);
2390
2391                         /* then the len of the SID 4 bytes */
2392                         SIVAL(entry,4,sid_len);
2393
2394                         /* unknown data 8 bytes uint64_t */
2395                         SBIG_UINT(entry,8,(uint64_t)0); /* this is not 0 in windows...-mezte*/
2396
2397                         /* the used disk space 8 bytes uint64_t */
2398                         SBIG_UINT(entry,16,qt.usedspace);
2399
2400                         /* the soft quotas 8 bytes uint64_t */
2401                         SBIG_UINT(entry,24,qt.softlim);
2402
2403                         /* the hard quotas 8 bytes uint64_t */
2404                         SBIG_UINT(entry,32,qt.hardlim);
2405
2406                         /* and now the SID */
2407                         sid_linearize(entry+40, sid_len, &sid);
2408
2409                         break;
2410
2411                 default:
2412                         DEBUG(0,("do_nt_transact_get_user_quota: fnum %d unknown level 0x%04hX\n",fsp->fnum,level));
2413                         reply_doserror(req, ERRSRV, ERRerror);
2414                         return;
2415                         break;
2416         }
2417
2418         send_nt_replies(conn, req, nt_status, params, param_len,
2419                         pdata, data_len);
2420 }
2421
2422 /****************************************************************************
2423  Reply to set user quota
2424 ****************************************************************************/
2425
2426 static void call_nt_transact_set_user_quota(connection_struct *conn,
2427                                             struct smb_request *req,
2428                                             uint16 **ppsetup,
2429                                             uint32 setup_count,
2430                                             char **ppparams,
2431                                             uint32 parameter_count,
2432                                             char **ppdata,
2433                                             uint32 data_count,
2434                                             uint32 max_data_count)
2435 {
2436         char *params = *ppparams;
2437         char *pdata = *ppdata;
2438         int data_len=0,param_len=0;
2439         SMB_NTQUOTA_STRUCT qt;
2440         size_t sid_len;
2441         DOM_SID sid;
2442         files_struct *fsp = NULL;
2443
2444         ZERO_STRUCT(qt);
2445
2446         /* access check */
2447         if (conn->server_info->utok.uid != 0) {
2448                 DEBUG(1,("set_user_quota: access_denied service [%s] user "
2449                          "[%s]\n", lp_servicename(SNUM(conn)),
2450                          conn->server_info->unix_name));
2451                 reply_doserror(req, ERRDOS, ERRnoaccess);
2452                 return;
2453         }
2454
2455         /*
2456          * Ensure minimum number of parameters sent.
2457          */
2458
2459         if (parameter_count < 2) {
2460                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= 2 bytes parameters\n",parameter_count));
2461                 reply_doserror(req, ERRDOS, ERRinvalidparam);
2462                 return;
2463         }
2464
2465         /* maybe we can check the quota_fnum */
2466         fsp = file_fsp(req, SVAL(params,0));
2467         if (!check_fsp_ntquota_handle(conn, req, fsp)) {
2468                 DEBUG(3,("TRANSACT_GET_USER_QUOTA: no valid QUOTA HANDLE\n"));
2469                 reply_nterror(req, NT_STATUS_INVALID_HANDLE);
2470                 return;
2471         }
2472
2473         if (data_count < 40) {
2474                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %d bytes data\n",data_count,40));
2475                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2476                 return;
2477         }
2478
2479         /* offset to next quota record.
2480          * 4 bytes IVAL(pdata,0)
2481          * unused here...
2482          */
2483
2484         /* sid len */
2485         sid_len = IVAL(pdata,4);
2486
2487         if (data_count < 40+sid_len) {
2488                 DEBUG(0,("TRANSACT_SET_USER_QUOTA: requires %d >= %lu bytes data\n",data_count,(unsigned long)40+sid_len));
2489                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2490                 return;
2491         }
2492
2493         /* unknown 8 bytes in pdata
2494          * maybe its the change time in NTTIME
2495          */
2496
2497         /* the used space 8 bytes (uint64_t)*/
2498         qt.usedspace = (uint64_t)IVAL(pdata,16);
2499 #ifdef LARGE_SMB_OFF_T
2500         qt.usedspace |= (((uint64_t)IVAL(pdata,20)) << 32);
2501 #else /* LARGE_SMB_OFF_T */
2502         if ((IVAL(pdata,20) != 0)&&
2503                 ((qt.usedspace != 0xFFFFFFFF)||
2504                 (IVAL(pdata,20)!=0xFFFFFFFF))) {
2505                 /* more than 32 bits? */
2506                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2507                 return;
2508         }
2509 #endif /* LARGE_SMB_OFF_T */
2510
2511         /* the soft quotas 8 bytes (uint64_t)*/
2512         qt.softlim = (uint64_t)IVAL(pdata,24);
2513 #ifdef LARGE_SMB_OFF_T
2514         qt.softlim |= (((uint64_t)IVAL(pdata,28)) << 32);
2515 #else /* LARGE_SMB_OFF_T */
2516         if ((IVAL(pdata,28) != 0)&&
2517                 ((qt.softlim != 0xFFFFFFFF)||
2518                 (IVAL(pdata,28)!=0xFFFFFFFF))) {
2519                 /* more than 32 bits? */
2520                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2521                 return;
2522         }
2523 #endif /* LARGE_SMB_OFF_T */
2524
2525         /* the hard quotas 8 bytes (uint64_t)*/
2526         qt.hardlim = (uint64_t)IVAL(pdata,32);
2527 #ifdef LARGE_SMB_OFF_T
2528         qt.hardlim |= (((uint64_t)IVAL(pdata,36)) << 32);
2529 #else /* LARGE_SMB_OFF_T */
2530         if ((IVAL(pdata,36) != 0)&&
2531                 ((qt.hardlim != 0xFFFFFFFF)||
2532                 (IVAL(pdata,36)!=0xFFFFFFFF))) {
2533                 /* more than 32 bits? */
2534                 reply_doserror(req, ERRDOS, ERRunknownlevel);
2535                 return;
2536         }
2537 #endif /* LARGE_SMB_OFF_T */
2538
2539         sid_parse(pdata+40,sid_len,&sid);
2540         DEBUGADD(8,("SID: %s\n", sid_string_dbg(&sid)));
2541
2542         /* 44 unknown bytes left... */
2543
2544         if (vfs_set_ntquota(fsp, SMB_USER_QUOTA_TYPE, &sid, &qt)!=0) {
2545                 reply_doserror(req, ERRSRV, ERRerror);
2546                 return;
2547         }
2548
2549         send_nt_replies(conn, req, NT_STATUS_OK, params, param_len,
2550                         pdata, data_len);
2551 }
2552 #endif /* HAVE_SYS_QUOTAS */
2553
2554 static void handle_nttrans(connection_struct *conn,
2555                            struct trans_state *state,
2556                            struct smb_request *req)
2557 {
2558         if (get_Protocol() >= PROTOCOL_NT1) {
2559                 req->flags2 |= 0x40; /* IS_LONG_NAME */
2560                 SSVAL(req->inbuf,smb_flg2,req->flags2);
2561         }
2562
2563
2564         SMB_PERFCOUNT_SET_SUBOP(&req->pcd, state->call);
2565
2566         /* Now we must call the relevant NT_TRANS function */
2567         switch(state->call) {
2568                 case NT_TRANSACT_CREATE:
2569                 {
2570                         START_PROFILE(NT_transact_create);
2571                         call_nt_transact_create(
2572                                 conn, req,
2573                                 &state->setup, state->setup_count,
2574                                 &state->param, state->total_param,
2575                                 &state->data, state->total_data,
2576                                 state->max_data_return);
2577                         END_PROFILE(NT_transact_create);
2578                         break;
2579                 }
2580
2581                 case NT_TRANSACT_IOCTL:
2582                 {
2583                         START_PROFILE(NT_transact_ioctl);
2584                         call_nt_transact_ioctl(
2585                                 conn, req,
2586                                 &state->setup, state->setup_count,
2587                                 &state->param, state->total_param,
2588                                 &state->data, state->total_data,
2589                                 state->max_data_return);
2590                         END_PROFILE(NT_transact_ioctl);
2591                         break;
2592                 }
2593
2594                 case NT_TRANSACT_SET_SECURITY_DESC:
2595                 {
2596                         START_PROFILE(NT_transact_set_security_desc);
2597                         call_nt_transact_set_security_desc(
2598                                 conn, req,
2599                                 &state->setup, state->setup_count,
2600                                 &state->param, state->total_param,
2601                                 &state->data, state->total_data,
2602                                 state->max_data_return);
2603                         END_PROFILE(NT_transact_set_security_desc);
2604                         break;
2605                 }
2606
2607                 case NT_TRANSACT_NOTIFY_CHANGE:
2608                 {
2609                         START_PROFILE(NT_transact_notify_change);
2610                         call_nt_transact_notify_change(
2611                                 conn, req,
2612                                 &state->setup, state->setup_count,
2613                                 &state->param, state->total_param,
2614                                 &state->data, state->total_data,
2615                                 state->max_data_return,
2616                                 state->max_param_return);
2617                         END_PROFILE(NT_transact_notify_change);
2618                         break;
2619                 }
2620
2621                 case NT_TRANSACT_RENAME:
2622                 {
2623                         START_PROFILE(NT_transact_rename);
2624                         call_nt_transact_rename(
2625                                 conn, req,
2626                                 &state->setup, state->setup_count,
2627                                 &state->param, state->total_param,
2628                                 &state->data, state->total_data,
2629                                 state->max_data_return);
2630                         END_PROFILE(NT_transact_rename);
2631                         break;
2632                 }
2633
2634                 case NT_TRANSACT_QUERY_SECURITY_DESC:
2635                 {
2636                         START_PROFILE(NT_transact_query_security_desc);
2637                         call_nt_transact_query_security_desc(
2638                                 conn, req,
2639                                 &state->setup, state->setup_count,
2640                                 &state->param, state->total_param,
2641                                 &state->data, state->total_data,
2642                                 state->max_data_return);
2643                         END_PROFILE(NT_transact_query_security_desc);
2644                         break;
2645                 }
2646
2647 #ifdef HAVE_SYS_QUOTAS
2648                 case NT_TRANSACT_GET_USER_QUOTA:
2649                 {
2650                         START_PROFILE(NT_transact_get_user_quota);
2651                         call_nt_transact_get_user_quota(
2652                                 conn, req,
2653                                 &state->setup, state->setup_count,
2654                                 &state->param, state->total_param,
2655                                 &state->data, state->total_data,
2656                                 state->max_data_return);
2657                         END_PROFILE(NT_transact_get_user_quota);
2658                         break;
2659                 }
2660
2661                 case NT_TRANSACT_SET_USER_QUOTA:
2662                 {
2663                         START_PROFILE(NT_transact_set_user_quota);
2664                         call_nt_transact_set_user_quota(
2665                                 conn, req,
2666                                 &state->setup, state->setup_count,
2667                                 &state->param, state->total_param,
2668                                 &state->data, state->total_data,
2669                                 state->max_data_return);
2670                         END_PROFILE(NT_transact_set_user_quota);
2671                         break;
2672                 }
2673 #endif /* HAVE_SYS_QUOTAS */
2674
2675                 default:
2676                         /* Error in request */
2677                         DEBUG(0,("handle_nttrans: Unknown request %d in "
2678                                  "nttrans call\n", state->call));
2679                         reply_doserror(req, ERRSRV, ERRerror);
2680                         return;
2681         }
2682         return;
2683 }
2684
2685 /****************************************************************************
2686  Reply to a SMBNTtrans.
2687 ****************************************************************************/
2688
2689 void reply_nttrans(struct smb_request *req)
2690 {
2691         connection_struct *conn = req->conn;
2692         uint32_t pscnt;
2693         uint32_t psoff;
2694         uint32_t dscnt;
2695         uint32_t dsoff;
2696         uint16 function_code;
2697         NTSTATUS result;
2698         struct trans_state *state;
2699
2700         START_PROFILE(SMBnttrans);
2701
2702         if (req->wct < 19) {
2703                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2704                 END_PROFILE(SMBnttrans);
2705                 return;
2706         }
2707
2708         pscnt = IVAL(req->vwv+9, 1);
2709         psoff = IVAL(req->vwv+11, 1);
2710         dscnt = IVAL(req->vwv+13, 1);
2711         dsoff = IVAL(req->vwv+15, 1);
2712         function_code = SVAL(req->vwv+18, 0);
2713
2714         if (IS_IPC(conn) && (function_code != NT_TRANSACT_CREATE)) {
2715                 reply_doserror(req, ERRSRV, ERRaccess);
2716                 END_PROFILE(SMBnttrans);
2717                 return;
2718         }
2719
2720         result = allow_new_trans(conn->pending_trans, req->mid);
2721         if (!NT_STATUS_IS_OK(result)) {
2722                 DEBUG(2, ("Got invalid nttrans request: %s\n", nt_errstr(result)));
2723                 reply_nterror(req, result);
2724                 END_PROFILE(SMBnttrans);
2725                 return;
2726         }
2727
2728         if ((state = TALLOC_P(conn, struct trans_state)) == NULL) {
2729                 reply_doserror(req, ERRSRV, ERRaccess);
2730                 END_PROFILE(SMBnttrans);
2731                 return;
2732         }
2733
2734         state->cmd = SMBnttrans;
2735
2736         state->mid = req->mid;
2737         state->vuid = req->vuid;
2738         state->total_data = IVAL(req->vwv+3, 1);
2739         state->data = NULL;
2740         state->total_param = IVAL(req->vwv+1, 1);
2741         state->param = NULL;
2742         state->max_data_return = IVAL(req->vwv+7, 1);
2743         state->max_param_return = IVAL(req->vwv+5, 1);
2744
2745         /* setup count is in *words* */
2746         state->setup_count = 2*CVAL(req->vwv+17, 1);
2747         state->setup = NULL;
2748         state->call = function_code;
2749
2750         DEBUG(10, ("num_setup=%u, "
2751                    "param_total=%u, this_param=%u, max_param=%u, "
2752                    "data_total=%u, this_data=%u, max_data=%u, "
2753                    "param_offset=%u, data_offset=%u\n",
2754                    (unsigned)state->setup_count,
2755                    (unsigned)state->total_param, (unsigned)pscnt,
2756                    (unsigned)state->max_param_return,
2757                    (unsigned)state->total_data, (unsigned)dscnt,
2758                    (unsigned)state->max_data_return,
2759                    (unsigned)psoff, (unsigned)dsoff));
2760
2761         /*
2762          * All nttrans messages we handle have smb_wct == 19 +
2763          * state->setup_count.  Ensure this is so as a sanity check.
2764          */
2765
2766         if(req->wct != 19 + (state->setup_count/2)) {
2767                 DEBUG(2,("Invalid smb_wct %d in nttrans call (should be %d)\n",
2768                          req->wct, 19 + (state->setup_count/2)));
2769                 goto bad_param;
2770         }
2771
2772         /* Don't allow more than 128mb for each value. */
2773         if ((state->total_data > (1024*1024*128)) ||
2774             (state->total_param > (1024*1024*128))) {
2775                 reply_doserror(req, ERRDOS, ERRnomem);
2776                 END_PROFILE(SMBnttrans);
2777                 return;
2778         }
2779
2780         if ((dscnt > state->total_data) || (pscnt > state->total_param))
2781                 goto bad_param;
2782
2783         if (state->total_data)  {
2784
2785                 if (trans_oob(state->total_data, 0, dscnt)
2786                     || trans_oob(smb_len(req->inbuf), dsoff, dscnt)) {
2787                         goto bad_param;
2788                 }
2789
2790                 /* Can't use talloc here, the core routines do realloc on the
2791                  * params and data. */
2792                 if ((state->data = (char *)SMB_MALLOC(state->total_data)) == NULL) {
2793                         DEBUG(0,("reply_nttrans: data malloc fail for %u "
2794                                  "bytes !\n", (unsigned int)state->total_data));
2795                         TALLOC_FREE(state);
2796                         reply_doserror(req, ERRDOS, ERRnomem);
2797                         END_PROFILE(SMBnttrans);
2798                         return;
2799                 }
2800
2801                 memcpy(state->data,smb_base(req->inbuf)+dsoff,dscnt);
2802         }
2803
2804         if (state->total_param) {
2805
2806                 if (trans_oob(state->total_param, 0, pscnt)
2807                     || trans_oob(smb_len(req->inbuf), psoff, pscnt)) {
2808                         goto bad_param;
2809                 }
2810
2811                 /* Can't use talloc here, the core routines do realloc on the
2812                  * params and data. */
2813                 if ((state->param = (char *)SMB_MALLOC(state->total_param)) == NULL) {
2814                         DEBUG(0,("reply_nttrans: param malloc fail for %u "
2815                                  "bytes !\n", (unsigned int)state->total_param));
2816                         SAFE_FREE(state->data);
2817                         TALLOC_FREE(state);
2818                         reply_doserror(req, ERRDOS, ERRnomem);
2819                         END_PROFILE(SMBnttrans);
2820                         return;
2821                 }
2822
2823                 memcpy(state->param,smb_base(req->inbuf)+psoff,pscnt);
2824         }
2825
2826         state->received_data  = dscnt;
2827         state->received_param = pscnt;
2828
2829         if(state->setup_count > 0) {
2830                 DEBUG(10,("reply_nttrans: state->setup_count = %d\n",
2831                           state->setup_count));
2832
2833                 /*
2834                  * No overflow possible here, state->setup_count is an
2835                  * unsigned int, being filled by a single byte from
2836                  * CVAL(req->vwv+13, 0) above. The cast in the comparison
2837                  * below is not necessary, it's here to clarify things. The
2838                  * validity of req->vwv and req->wct has been checked in
2839                  * init_smb_request already.
2840                  */
2841                 if ((state->setup_count/2) + 19 > (unsigned int)req->wct) {
2842                         goto bad_param;
2843                 }
2844
2845                 state->setup = (uint16 *)TALLOC(state, state->setup_count);
2846                 if (state->setup == NULL) {
2847                         DEBUG(0,("reply_nttrans : Out of memory\n"));
2848                         SAFE_FREE(state->data);
2849                         SAFE_FREE(state->param);
2850                         TALLOC_FREE(state);
2851                         reply_doserror(req, ERRDOS, ERRnomem);
2852                         END_PROFILE(SMBnttrans);
2853                         return;
2854                 }
2855
2856                 memcpy(state->setup, req->vwv+19, state->setup_count);
2857                 dump_data(10, (uint8 *)state->setup, state->setup_count);
2858         }
2859
2860         if ((state->received_data == state->total_data) &&
2861             (state->received_param == state->total_param)) {
2862                 handle_nttrans(conn, state, req);
2863                 SAFE_FREE(state->param);
2864                 SAFE_FREE(state->data);
2865                 TALLOC_FREE(state);
2866                 END_PROFILE(SMBnttrans);
2867                 return;
2868         }
2869
2870         DLIST_ADD(conn->pending_trans, state);
2871
2872         /* We need to send an interim response then receive the rest
2873            of the parameter/data bytes */
2874         reply_outbuf(req, 0, 0);
2875         show_msg((char *)req->outbuf);
2876         END_PROFILE(SMBnttrans);
2877         return;
2878
2879   bad_param:
2880
2881         DEBUG(0,("reply_nttrans: invalid trans parameters\n"));
2882         SAFE_FREE(state->data);
2883         SAFE_FREE(state->param);
2884         TALLOC_FREE(state);
2885         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2886         END_PROFILE(SMBnttrans);
2887         return;
2888 }
2889
2890 /****************************************************************************
2891  Reply to a SMBnttranss
2892  ****************************************************************************/
2893
2894 void reply_nttranss(struct smb_request *req)
2895 {
2896         connection_struct *conn = req->conn;
2897         uint32_t pcnt,poff,dcnt,doff,pdisp,ddisp;
2898         struct trans_state *state;
2899
2900         START_PROFILE(SMBnttranss);
2901
2902         show_msg((char *)req->inbuf);
2903
2904         if (req->wct < 18) {
2905                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2906                 END_PROFILE(SMBnttranss);
2907                 return;
2908         }
2909
2910         for (state = conn->pending_trans; state != NULL;
2911              state = state->next) {
2912                 if (state->mid == req->mid) {
2913                         break;
2914                 }
2915         }
2916
2917         if ((state == NULL) || (state->cmd != SMBnttrans)) {
2918                 reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2919                 END_PROFILE(SMBnttranss);
2920                 return;
2921         }
2922
2923         /* Revise state->total_param and state->total_data in case they have
2924            changed downwards */
2925         if (IVAL(req->vwv+1, 1) < state->total_param) {
2926                 state->total_param = IVAL(req->vwv+1, 1);
2927         }
2928         if (IVAL(req->vwv+3, 1) < state->total_data) {
2929                 state->total_data = IVAL(req->vwv+3, 1);
2930         }
2931
2932         pcnt = IVAL(req->vwv+5, 1);
2933         poff = IVAL(req->vwv+7, 1);
2934         pdisp = IVAL(req->vwv+9, 1);
2935
2936         dcnt = IVAL(req->vwv+11, 1);
2937         doff = IVAL(req->vwv+13, 1);
2938         ddisp = IVAL(req->vwv+15, 1);
2939
2940         state->received_param += pcnt;
2941         state->received_data += dcnt;
2942
2943         if ((state->received_data > state->total_data) ||
2944             (state->received_param > state->total_param))
2945                 goto bad_param;
2946
2947         if (pcnt) {
2948                 if (trans_oob(state->total_param, pdisp, pcnt)
2949                     || trans_oob(smb_len(req->inbuf), poff, pcnt)) {
2950                         goto bad_param;
2951                 }
2952                 memcpy(state->param+pdisp, smb_base(req->inbuf)+poff,pcnt);
2953         }
2954
2955         if (dcnt) {
2956                 if (trans_oob(state->total_data, ddisp, dcnt)
2957                     || trans_oob(smb_len(req->inbuf), doff, dcnt)) {
2958                         goto bad_param;
2959                 }
2960                 memcpy(state->data+ddisp, smb_base(req->inbuf)+doff,dcnt);
2961         }
2962
2963         if ((state->received_param < state->total_param) ||
2964             (state->received_data < state->total_data)) {
2965                 END_PROFILE(SMBnttranss);
2966                 return;
2967         }
2968
2969         handle_nttrans(conn, state, req);
2970
2971         DLIST_REMOVE(conn->pending_trans, state);
2972         SAFE_FREE(state->data);
2973         SAFE_FREE(state->param);
2974         TALLOC_FREE(state);
2975         END_PROFILE(SMBnttranss);
2976         return;
2977
2978   bad_param:
2979
2980         DEBUG(0,("reply_nttranss: invalid trans parameters\n"));
2981         DLIST_REMOVE(conn->pending_trans, state);
2982         SAFE_FREE(state->data);
2983         SAFE_FREE(state->param);
2984         TALLOC_FREE(state);
2985         reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
2986         END_PROFILE(SMBnttranss);
2987         return;
2988 }