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