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