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