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