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