LCOV - code coverage report
Current view: top level - source4/libcli/smb2 - find.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 50 76 65.8 %
Date: 2024-06-13 04:01:37 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    SMB2 client find calls
       5             : 
       6             :    Copyright (C) Andrew Tridgell 2005
       7             :    
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             :    
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             :    
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include "includes.h"
      23             : #include "libcli/raw/libcliraw.h"
      24             : #include "libcli/raw/raw_proto.h"
      25             : #include "libcli/smb2/smb2.h"
      26             : #include "libcli/smb2/smb2_calls.h"
      27             : 
      28             : /*
      29             :   send a find request
      30             : */
      31         281 : struct smb2_request *smb2_find_send(struct smb2_tree *tree, struct smb2_find *io)
      32             : {
      33             :         struct smb2_request *req;
      34             :         NTSTATUS status;
      35             : 
      36         281 :         req = smb2_request_init_tree(tree, SMB2_OP_QUERY_DIRECTORY, 0x20, true, 0);
      37         281 :         if (req == NULL) return NULL;
      38         281 :         req->credit_charge = (MAX(io->in.max_response_size, 1) - 1)/ 65536 + 1;
      39             : 
      40         281 :         SCVAL(req->out.body, 0x02, io->in.level);
      41         281 :         SCVAL(req->out.body, 0x03, io->in.continue_flags);
      42         281 :         SIVAL(req->out.body, 0x04, io->in.file_index);
      43         281 :         smb2_push_handle(req->out.body+0x08, &io->in.file.handle);
      44             : 
      45         281 :         status = smb2_push_o16s16_string(&req->out, 0x18, io->in.pattern);
      46         281 :         if (!NT_STATUS_IS_OK(status)) {
      47           0 :                 talloc_free(req);
      48           0 :                 return NULL;
      49             :         }
      50             : 
      51         281 :         SIVAL(req->out.body, 0x1C, io->in.max_response_size);
      52             : 
      53         281 :         smb2_transport_send(req);
      54             : 
      55         281 :         return req;
      56             : }
      57             : 
      58             : 
      59             : /*
      60             :   recv a find reply
      61             : */
      62         281 : NTSTATUS smb2_find_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx,
      63             :                            struct smb2_find *io)
      64             : {
      65             :         NTSTATUS status;
      66             : 
      67         562 :         if (!smb2_request_receive(req) || 
      68         281 :             smb2_request_is_error(req)) {
      69           4 :                 return smb2_request_destroy(req);
      70             :         }
      71             : 
      72         277 :         SMB2_CHECK_PACKET_RECV(req, 0x08, true);
      73             : 
      74         553 :         status = smb2_pull_o16s32_blob(&req->in, mem_ctx, 
      75         277 :                                        req->in.body+0x02, &io->out.blob);
      76         277 :         if (!NT_STATUS_IS_OK(status)) {
      77           0 :                 return status;
      78             :         }
      79             : 
      80         277 :         return smb2_request_destroy(req);
      81             : }
      82             : 
      83             : /*
      84             :   sync find request
      85             : */
      86           0 : NTSTATUS smb2_find(struct smb2_tree *tree, TALLOC_CTX *mem_ctx,
      87             :                    struct smb2_find *io)
      88             : {
      89           0 :         struct smb2_request *req = smb2_find_send(tree, io);
      90           0 :         return smb2_find_recv(req, mem_ctx, io);
      91             : }
      92             : 
      93             : 
      94             : /*
      95             :   a varient of smb2_find_recv that parses the resulting blob into
      96             :   smb_search_data structures
      97             : */
      98         281 : NTSTATUS smb2_find_level_recv(struct smb2_request *req, TALLOC_CTX *mem_ctx,
      99             :                               uint8_t level, unsigned int *count,
     100             :                               union smb_search_data **io)
     101             : {
     102             :         struct smb2_find f;
     103             :         NTSTATUS status;
     104             :         DATA_BLOB b;
     105             :         enum smb_search_data_level smb_level;
     106         281 :         unsigned int next_ofs=0;
     107             : 
     108         281 :         switch (level) {
     109           0 :         case SMB2_FIND_DIRECTORY_INFO:
     110           0 :                 smb_level = RAW_SEARCH_DATA_DIRECTORY_INFO;
     111           0 :                 break;
     112           0 :         case SMB2_FIND_FULL_DIRECTORY_INFO:
     113           0 :                 smb_level = RAW_SEARCH_DATA_FULL_DIRECTORY_INFO;
     114           0 :                 break;
     115           5 :         case SMB2_FIND_BOTH_DIRECTORY_INFO:
     116           5 :                 smb_level = RAW_SEARCH_DATA_BOTH_DIRECTORY_INFO;
     117           5 :                 break;
     118         276 :         case SMB2_FIND_NAME_INFO:
     119         276 :                 smb_level = RAW_SEARCH_DATA_NAME_INFO;
     120         276 :                 break;
     121           0 :         case SMB2_FIND_ID_FULL_DIRECTORY_INFO:
     122           0 :                 smb_level = RAW_SEARCH_DATA_ID_FULL_DIRECTORY_INFO;
     123           0 :                 break;
     124           0 :         case SMB2_FIND_ID_BOTH_DIRECTORY_INFO:
     125           0 :                 smb_level = RAW_SEARCH_DATA_ID_BOTH_DIRECTORY_INFO;
     126           0 :                 break;
     127           0 :         default:
     128           0 :                 return NT_STATUS_INVALID_INFO_CLASS;
     129             :         }
     130             : 
     131         281 :         status = smb2_find_recv(req, mem_ctx, &f);
     132         281 :         NT_STATUS_NOT_OK_RETURN(status);
     133             :         
     134         154 :         b = f.out.blob;
     135         154 :         *io = NULL;
     136         154 :         *count = 0;
     137             : 
     138             :         do {
     139             :                 union smb_search_data *io2;
     140             : 
     141       65968 :                 io2 = talloc_realloc(mem_ctx, *io, union smb_search_data, (*count)+1);
     142       65968 :                 if (io2 == NULL) {
     143           0 :                         data_blob_free(&f.out.blob);
     144           0 :                         talloc_free(*io);
     145           0 :                         return NT_STATUS_NO_MEMORY;
     146             :                 }
     147       65968 :                 *io = io2;
     148             : 
     149       65968 :                 status = smb_raw_search_common(*io, smb_level, &b, (*io) + (*count), 
     150             :                                                &next_ofs, STR_UNICODE);
     151             : 
     152      131933 :                 if (NT_STATUS_IS_OK(status) &&
     153       65968 :                     next_ofs >= b.length) {
     154           0 :                         data_blob_free(&f.out.blob);
     155           0 :                         talloc_free(*io);
     156           0 :                         return NT_STATUS_INFO_LENGTH_MISMATCH;                  
     157             :                 }
     158             : 
     159       65968 :                 (*count)++;
     160             : 
     161       65968 :                 b = data_blob_const(b.data+next_ofs, b.length - next_ofs);
     162       65968 :         } while (NT_STATUS_IS_OK(status) && next_ofs != 0);
     163             : 
     164         154 :         data_blob_free(&f.out.blob);
     165             :         
     166         154 :         return NT_STATUS_OK;
     167             : }
     168             : 
     169             : /*
     170             :   a varient of smb2_find that parses the resulting blob into
     171             :   smb_search_data structures
     172             : */
     173         281 : NTSTATUS smb2_find_level(struct smb2_tree *tree, TALLOC_CTX *mem_ctx,
     174             :                          struct smb2_find *f, 
     175             :                          unsigned int *count, union smb_search_data **io)
     176             : {
     177             :         struct smb2_request *req;
     178             : 
     179         281 :         req = smb2_find_send(tree, f);
     180         281 :         return smb2_find_level_recv(req, mem_ctx, f->in.level, count, io);
     181             : }

Generated by: LCOV version 1.13