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