LCOV - code coverage report
Current view: top level - providers/ipa - ipa_dn.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 46 53 86.8 %
Date: 2016-06-29 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :     Authors:
       3             :         Pavel Březina <pbrezina@redhat.com>
       4             : 
       5             :     Copyright (C) 2015 Red Hat
       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 <talloc.h>
      22             : #include <ldb.h>
      23             : #include "db/sysdb.h"
      24             : #include "providers/ipa/ipa_dn.h"
      25             : 
      26          24 : static bool check_dn(struct ldb_dn *dn,
      27             :                      const char *rdn_attr,
      28             :                      va_list in_ap)
      29             : {
      30             :     const struct ldb_val *ldbval;
      31             :     const char *strval;
      32             :     const char *ldbattr;
      33             :     const char *attr;
      34             :     const char *val;
      35             :     va_list ap;
      36             :     int num_comp;
      37             :     int comp;
      38             : 
      39             :     /* check RDN attribute */
      40          24 :     ldbattr = ldb_dn_get_rdn_name(dn);
      41          24 :     if (ldbattr == NULL || strcasecmp(ldbattr, rdn_attr) != 0) {
      42           3 :         return false;
      43             :     }
      44             : 
      45             :     /* Check DN components. First we check if all attr=value pairs match input.
      46             :      * Then we check that the next attribute is a domain component.
      47             :      */
      48             : 
      49          21 :     comp = 1;
      50          21 :     num_comp = ldb_dn_get_comp_num(dn);
      51             : 
      52          21 :     va_copy(ap, in_ap);
      53          54 :     while ((attr = va_arg(ap, const char *)) != NULL) {
      54          21 :         val = va_arg(ap, const char *);
      55          21 :         if (val == NULL) {
      56           3 :             goto vafail;
      57             :         }
      58             : 
      59          18 :         if (comp > num_comp) {
      60           0 :             goto vafail;
      61             :         }
      62             : 
      63          18 :         ldbattr = ldb_dn_get_component_name(dn, comp);
      64          18 :         if (ldbattr == NULL || strcasecmp(ldbattr, attr) != 0) {
      65             :             goto vafail;
      66             :         }
      67             : 
      68          15 :         ldbval = ldb_dn_get_component_val(dn, comp);
      69          15 :         if (ldbval == NULL) {
      70           0 :             goto vafail;
      71             :         }
      72             : 
      73          15 :         strval = (const char *)ldbval->data;
      74          15 :         if (strval == NULL || strncasecmp(strval, val, ldbval->length) != 0) {
      75             :             goto vafail;
      76             :         }
      77             : 
      78          12 :         comp++;
      79             :     }
      80          12 :     va_end(ap);
      81             : 
      82          12 :     ldbattr = ldb_dn_get_component_name(dn, comp);
      83          12 :     if (ldbattr == NULL || strcmp(ldbattr, "dc") != 0) {
      84           3 :         return false;
      85             :     }
      86             : 
      87           9 :     return true;
      88             : 
      89             : vafail:
      90           9 :     va_end(ap);
      91           9 :     return false;
      92             : }
      93             : 
      94          24 : errno_t _ipa_get_rdn(TALLOC_CTX *mem_ctx,
      95             :                       struct sysdb_ctx *sysdb,
      96             :                       const char *obj_dn,
      97             :                       char **_rdn_val,
      98             :                       const char *rdn_attr,
      99             :                       ...)
     100             : {
     101             :     const struct ldb_val *val;
     102             :     struct ldb_dn *dn;
     103             :     errno_t ret;
     104             :     bool bret;
     105             :     va_list ap;
     106             :     char *rdn;
     107             : 
     108          24 :     dn = ldb_dn_new(mem_ctx, sysdb_ctx_get_ldb(sysdb), obj_dn);
     109          24 :     if (dn == NULL) {
     110           0 :         return ENOMEM;
     111             :     }
     112             : 
     113          24 :     va_start(ap, rdn_attr);
     114          24 :     bret = check_dn(dn, rdn_attr, ap);
     115          24 :     va_end(ap);
     116          24 :     if (bret == false) {
     117          15 :         ret = ENOENT;
     118          15 :         goto done;
     119             :     }
     120             : 
     121           9 :     if (_rdn_val == NULL) {
     122           6 :         ret = EOK;
     123           6 :         goto done;
     124             :     }
     125             : 
     126           3 :     val = ldb_dn_get_rdn_val(dn);
     127           3 :     if (val == NULL || val->data == NULL) {
     128           0 :         ret = EINVAL;
     129           0 :         goto done;
     130             :     }
     131             : 
     132           3 :     rdn = talloc_strndup(mem_ctx, (const char*)val->data, val->length);
     133           3 :     if (rdn == NULL) {
     134           0 :         ret = ENOMEM;
     135           0 :         goto done;
     136             :     }
     137             : 
     138           3 :     *_rdn_val = rdn;
     139             : 
     140           3 :     ret = EOK;
     141             : 
     142             : done:
     143          24 :     talloc_free(dn);
     144          24 :     return ret;
     145             : }

Generated by: LCOV version 1.10