LCOV - code coverage report
Current view: top level - source4/libcli/ldap - ldap_ildap.c (source / functions) Hit Total Coverage
Test: coverage report for master 2b515b7d Lines: 50 58 86.2 %
Date: 2024-02-28 12:06:22 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    ildap api - an api similar to the traditional ldap api
       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             : 
      23             : #include "includes.h"
      24             : #include "libcli/ldap/libcli_ldap.h"
      25             : #include "libcli/ldap/ldap_client.h"
      26             : 
      27             : 
      28             : /*
      29             :   count the returned search entries
      30             : */
      31       25853 : _PUBLIC_ int ildap_count_entries(struct ldap_connection *conn, struct ldap_message **res)
      32             : {
      33         122 :         int i;
      34       51706 :         for (i=0;res && res[i];i++) /* noop */ ;
      35       25853 :         return i;
      36             : }
      37             : 
      38             : 
      39             : /*
      40             :   perform a synchronous ldap search
      41             : */
      42       25853 : _PUBLIC_ NTSTATUS ildap_search_bytree(struct ldap_connection *conn, const char *basedn, 
      43             :                              int scope, struct ldb_parse_tree *tree,
      44             :                              const char * const *attrs, bool attributesonly, 
      45             :                              struct ldb_control **control_req,
      46             :                              struct ldb_control ***control_res,
      47             :                              struct ldap_message ***results)
      48             : {
      49         122 :         struct ldap_message *msg;
      50         122 :         int n, i;
      51         122 :         NTSTATUS status;
      52         122 :         struct ldap_request *req;
      53             : 
      54       25853 :         if (control_res)
      55           0 :                 *control_res = NULL;
      56       25853 :         *results = NULL;
      57             : 
      58       25853 :         msg = new_ldap_message(conn);
      59       25853 :         NT_STATUS_HAVE_NO_MEMORY(msg);
      60             : 
      61       51706 :         for (n=0;attrs && attrs[n];n++) /* noop */ ;
      62             :         
      63       25853 :         msg->type = LDAP_TAG_SearchRequest;
      64       25853 :         msg->r.SearchRequest.basedn = basedn;
      65       25853 :         msg->r.SearchRequest.scope  = scope;
      66       25853 :         msg->r.SearchRequest.deref  = LDAP_DEREFERENCE_NEVER;
      67       25853 :         msg->r.SearchRequest.timelimit = 0;
      68       25853 :         msg->r.SearchRequest.sizelimit = 0;
      69       25853 :         msg->r.SearchRequest.attributesonly = attributesonly;
      70       25853 :         msg->r.SearchRequest.tree = tree;
      71       25853 :         msg->r.SearchRequest.num_attributes = n;
      72       25853 :         msg->r.SearchRequest.attributes = attrs;
      73       25853 :         msg->controls = control_req;
      74             : 
      75       25853 :         req = ldap_request_send(conn, msg);
      76       25853 :         talloc_reparent(conn, msg, req);
      77             :         
      78       51706 :         for (i=n=0;true;i++) {
      79         244 :                 struct ldap_message *res;
      80       51706 :                 status = ldap_result_n(req, i, &res);
      81       51706 :                 if (!NT_STATUS_IS_OK(status)) break;
      82             : 
      83       51706 :                 if (res->type == LDAP_TAG_SearchResultDone) {
      84       25853 :                         status = ldap_check_response(conn, &res->r.GeneralResult);
      85       25853 :                         if (control_res) {
      86           0 :                                 *control_res = talloc_steal(conn, res->controls);
      87             :                         }
      88       25731 :                         break;
      89             :                 }
      90             : 
      91       25853 :                 if (res->type != LDAP_TAG_SearchResultEntry &&
      92           0 :                     res->type != LDAP_TAG_SearchResultReference)
      93           0 :                         continue;
      94             :                 
      95       25853 :                 (*results) = talloc_realloc(conn, *results, struct ldap_message *, n+2);
      96       25853 :                 if (*results == NULL) {
      97           0 :                         talloc_free(msg);
      98           0 :                         return NT_STATUS_NO_MEMORY;
      99             :                 }
     100       25853 :                 (*results)[n] = talloc_steal(*results, res);
     101       25853 :                 (*results)[n+1] = NULL;
     102       25853 :                 n++;
     103             :         }
     104             : 
     105       25853 :         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
     106           0 :                 status = NT_STATUS_OK;
     107             :         }
     108             : 
     109       25853 :         return status;
     110             : }
     111             : 
     112             : /*
     113             :   perform a ldap search
     114             : */
     115       25853 : _PUBLIC_ NTSTATUS ildap_search(struct ldap_connection *conn, const char *basedn, 
     116             :                       int scope, const char *expression, 
     117             :                       const char * const *attrs, bool attributesonly, 
     118             :                       struct ldb_control **control_req,
     119             :                       struct ldb_control ***control_res,
     120             :                       struct ldap_message ***results)
     121             : {
     122         122 :         NTSTATUS status;
     123       25853 :         struct ldb_parse_tree *tree = ldb_parse_tree(conn, expression);
     124             : 
     125       25853 :         if (tree == NULL) {
     126           0 :                 return NT_STATUS_INVALID_PARAMETER;
     127             :         }
     128       25853 :         status = ildap_search_bytree(conn, basedn, scope, tree, attrs,
     129             :                                      attributesonly, control_req,
     130             :                                      control_res, results);
     131       25853 :         talloc_free(tree);
     132       25853 :         return status;
     133             : }

Generated by: LCOV version 1.14