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