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