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