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