LCOV - code coverage report
Current view: top level - providers/ldap - sdap_utils.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 6 76 7.9 %
Date: 2016-06-29 Functions: 2 6 33.3 %

          Line data    Source code
       1             : /*
       2             :     Authors:
       3             :         Simo Sorce <ssorce@redhat.com>
       4             : 
       5             :     Copyright (C) 2013 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 <ctype.h>
      22             : #include "util/util.h"
      23             : #include "providers/ldap/sdap_async.h"
      24             : 
      25             : errno_t
      26           0 : sdap_attrs_add_ldap_attr(struct sysdb_attrs *ldap_attrs,
      27             :                          const char *attr_name,
      28             :                          const char *attr_desc,
      29             :                          bool multivalued,
      30             :                          const char *name,
      31             :                          struct sysdb_attrs *attrs)
      32             : {
      33             :     errno_t ret;
      34             :     struct ldb_message_element *el;
      35           0 :     const char *objname = name ?: "object";
      36           0 :     const char *desc = attr_desc ?: attr_name;
      37             :     unsigned int num_values, i;
      38             :     char *printable;
      39             : 
      40           0 :     ret = sysdb_attrs_get_el(ldap_attrs, attr_name, &el);
      41           0 :     if (ret) {
      42           0 :         DEBUG(SSSDBG_OP_FAILURE, "Could not get %s from the "
      43             :               "list of the LDAP attributes [%d]: %s\n",
      44             :               attr_name, ret, strerror(ret));
      45           0 :         return ret;
      46             :     }
      47             : 
      48           0 :     if (el->num_values == 0) {
      49           0 :         DEBUG(SSSDBG_TRACE_INTERNAL, "%s is not available "
      50             :               "for [%s].\n", desc, objname);
      51             :     } else {
      52           0 :         num_values = multivalued ? el->num_values : 1;
      53           0 :         for (i = 0; i < num_values; i++) {
      54           0 :             printable = ldb_binary_encode(ldap_attrs, el->values[i]);
      55           0 :             if (printable == NULL) {
      56           0 :                 DEBUG(SSSDBG_MINOR_FAILURE, "ldb_binary_encode failed..\n");
      57           0 :                 continue;
      58             :             }
      59             : 
      60           0 :             DEBUG(SSSDBG_TRACE_INTERNAL, "Adding %s [%s] to attributes "
      61             :                   "of [%s].\n", desc, printable, objname);
      62             : 
      63           0 :             talloc_zfree(printable);
      64             : 
      65           0 :             ret = sysdb_attrs_add_mem(attrs, attr_name, el->values[i].data,
      66           0 :                                       el->values[i].length);
      67           0 :             if (ret) {
      68           0 :                 return ret;
      69             :             }
      70             :         }
      71             :     }
      72             : 
      73           0 :     return EOK;
      74             : }
      75             : 
      76             : errno_t
      77           0 : sdap_save_all_names(const char *name,
      78             :                     struct sysdb_attrs *ldap_attrs,
      79             :                     struct sss_domain_info *dom,
      80             :                     struct sysdb_attrs *attrs)
      81             : {
      82           0 :     const char **aliases = NULL;
      83             :     const char *domname;
      84             :     errno_t ret;
      85             :     TALLOC_CTX *tmp_ctx;
      86             :     int i;
      87           0 :     bool lowercase = !dom->case_sensitive;
      88             : 
      89           0 :     tmp_ctx = talloc_new(NULL);
      90           0 :     if (!tmp_ctx) {
      91           0 :         ret = ENOMEM;
      92           0 :         goto done;
      93             :     }
      94             : 
      95           0 :     ret = sysdb_attrs_get_aliases(tmp_ctx, ldap_attrs, name,
      96             :                                   lowercase, &aliases);
      97           0 :     if (ret != EOK) {
      98           0 :         DEBUG(SSSDBG_OP_FAILURE, "Failed to get the alias list\n");
      99           0 :         goto done;
     100             :     }
     101             : 
     102           0 :     for (i = 0; aliases[i]; i++) {
     103           0 :         domname = sss_get_domain_name(tmp_ctx, aliases[i], dom);
     104           0 :         if (domname == NULL) {
     105           0 :             ret = ENOMEM;
     106           0 :             goto done;
     107             :         }
     108             : 
     109           0 :         if (lowercase) {
     110           0 :             ret = sysdb_attrs_add_lc_name_alias(attrs, domname);
     111           0 :             if (ret) {
     112           0 :                 DEBUG(SSSDBG_OP_FAILURE, "Failed to add lower-cased version "
     113             :                                           "of alias [%s] into the "
     114             :                                           "attribute list\n", aliases[i]);
     115           0 :                 goto done;
     116             :             }
     117             :         } else {
     118           0 :             ret = sysdb_attrs_add_string(attrs, SYSDB_NAME_ALIAS, domname);
     119           0 :             if (ret) {
     120           0 :                 DEBUG(SSSDBG_OP_FAILURE, "Failed to add alias [%s] into the "
     121             :                                           "attribute list\n", aliases[i]);
     122           0 :                 goto done;
     123             :             }
     124             :         }
     125             : 
     126             :     }
     127             : 
     128           0 :     ret = EOK;
     129             : done:
     130           0 :     talloc_free(tmp_ctx);
     131           0 :     return ret;
     132             : }
     133             : 
     134           0 : errno_t deref_string_to_val(const char *str, int *val)
     135             : {
     136           0 :     if (strcasecmp(str, "never") == 0) {
     137           0 :         *val = LDAP_DEREF_NEVER;
     138           0 :     } else if (strcasecmp(str, "searching") == 0) {
     139           0 :         *val = LDAP_DEREF_SEARCHING;
     140           0 :     } else if (strcasecmp(str, "finding") == 0) {
     141           0 :         *val = LDAP_DEREF_FINDING;
     142           0 :     } else if (strcasecmp(str, "always") == 0) {
     143           0 :         *val = LDAP_DEREF_ALWAYS;
     144             :     } else {
     145           0 :         DEBUG(SSSDBG_CRIT_FAILURE, "Illegal deref option [%s].\n", str);
     146           0 :         return EINVAL;
     147             :     }
     148             : 
     149           0 :     return EOK;
     150             : }
     151             : 
     152             : static char *
     153          19 : sdap_combine_filters_ex(TALLOC_CTX *mem_ctx,
     154             :                         char operator,
     155             :                         const char *base_filter,
     156             :                         const char *extra_filter)
     157             : {
     158          19 :     char *filter = NULL;
     159             : 
     160          19 :     if (extra_filter == NULL || extra_filter[0] == '\0') {
     161          19 :         return talloc_strdup(mem_ctx, base_filter);
     162           0 :     } else if (base_filter == NULL || base_filter[0] == '\0') {
     163           0 :         return talloc_strdup(mem_ctx, extra_filter);
     164             :     }
     165             : 
     166           0 :     if (extra_filter[0] == '(') {
     167           0 :         filter = talloc_asprintf(mem_ctx, "(%c%s%s)",
     168             :                                  operator, base_filter, extra_filter);
     169             :     } else {
     170           0 :         filter = talloc_asprintf(mem_ctx, "(%c%s(%s))",
     171             :                                  operator, base_filter, extra_filter);
     172             :     }
     173             : 
     174           0 :     return filter; /* NULL or not */
     175             : }
     176             : 
     177           0 : char *sdap_or_filters(TALLOC_CTX *mem_ctx,
     178             :                       const char *base_filter,
     179             :                       const char *extra_filter)
     180             : {
     181           0 :     return sdap_combine_filters_ex(mem_ctx, '|', base_filter, extra_filter);
     182             : }
     183             : 
     184          19 : char *sdap_combine_filters(TALLOC_CTX *mem_ctx,
     185             :                            const char *base_filter,
     186             :                            const char *extra_filter)
     187             : {
     188          19 :     return sdap_combine_filters_ex(mem_ctx, '&', base_filter, extra_filter);
     189             : }

Generated by: LCOV version 1.10