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