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