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