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