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