LCOV - code coverage report
Current view: top level - lib/addns - dnsutils.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 35 54 64.8 %
Date: 2024-04-21 15:09:00 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :   Linux DNS client library implementation
       3             : 
       4             :   Copyright (C) 2006 Krishna Ganugapati <krishnag@centeris.com>
       5             :   Copyright (C) 2006 Gerald Carter <jerry@samba.org>
       6             : 
       7             :      ** NOTE! The following LGPL license applies to the libaddns
       8             :      ** library. This does NOT imply that all of Samba is released
       9             :      ** under the LGPL
      10             : 
      11             :   This library is free software; you can redistribute it and/or
      12             :   modify it under the terms of the GNU Lesser General Public
      13             :   License as published by the Free Software Foundation; either
      14             :   version 2.1 of the License, or (at your option) any later version.
      15             : 
      16             :   This library is distributed in the hope that it will be useful,
      17             :   but WITHOUT ANY WARRANTY; without even the implied warranty of
      18             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      19             :   Lesser General Public License for more details.
      20             : 
      21             :   You should have received a copy of the GNU Lesser General Public
      22             :   License along with this library; if not, see <http://www.gnu.org/licenses/>.
      23             : */
      24             : 
      25             : #include "includes.h"
      26             : #include "librpc/ndr/libndr.h"
      27             : #include "librpc/gen_ndr/ndr_misc.h"
      28             : 
      29             : #include "dns.h"
      30             : #include <ctype.h>
      31             : 
      32             : 
      33        2986 : static DNS_ERROR LabelList( TALLOC_CTX *mem_ctx,
      34             :                             const char *name,
      35             :                             struct dns_domain_label **presult )
      36             : {
      37           0 :         struct dns_domain_label *result;
      38           0 :         const char *dot;
      39             : 
      40       25808 :         for (dot = name; *dot != '\0'; dot += 1) {
      41       25001 :                 char c = *dot;
      42             : 
      43       25001 :                 if (c == '.')
      44        2179 :                         break;
      45             : 
      46       44972 :                 if (c == '-') continue;
      47       22150 :                 if ((c >= 'a') && (c <= 'z')) continue;
      48        4496 :                 if ((c >= 'A') && (c <= 'Z')) continue;
      49        4496 :                 if ((c >= '0') && (c <= '9')) continue;
      50             : 
      51           0 :                 return ERROR_DNS_INVALID_NAME;
      52             :         }
      53             : 
      54        2986 :         if ((dot - name) > 63) {
      55             :                 /*
      56             :                  * DNS labels can only be 63 chars long
      57             :                  */
      58           0 :                 return ERROR_DNS_INVALID_NAME;
      59             :         }
      60             : 
      61        2986 :         if (!(result = talloc_zero(mem_ctx, struct dns_domain_label))) {
      62           0 :                 return ERROR_DNS_NO_MEMORY;
      63             :         }
      64             : 
      65        2986 :         if (*dot == '\0') {
      66             :                 /*
      67             :                  * No dot around, so this is the last component
      68             :                  */
      69             : 
      70         807 :                 if (!(result->label = talloc_strdup(result, name))) {
      71           0 :                         TALLOC_FREE(result);
      72           0 :                         return ERROR_DNS_NO_MEMORY;
      73             :                 }
      74         807 :                 result->len = strlen(result->label);
      75         807 :                 *presult = result;
      76         807 :                 return ERROR_DNS_SUCCESS;
      77             :         }
      78             : 
      79        2179 :         if (dot[1] == '.') {
      80             :                 /*
      81             :                  * Two dots in a row, reject
      82             :                  */
      83             : 
      84           0 :                 TALLOC_FREE(result);
      85           0 :                 return ERROR_DNS_INVALID_NAME;
      86             :         }
      87             : 
      88        2179 :         if (dot[1] != '\0') {
      89             :                 /*
      90             :                  * Something follows, get the rest
      91             :                  */
      92             : 
      93        2179 :                 DNS_ERROR err = LabelList(result, dot+1, &result->next);
      94             : 
      95        2179 :                 if (!ERR_DNS_IS_OK(err)) {
      96           0 :                         TALLOC_FREE(result);
      97           0 :                         return err;
      98             :                 }
      99             :         }
     100             : 
     101        2179 :         result->len = (dot - name);
     102             : 
     103        2179 :         if (!(result->label = talloc_strndup(result, name, result->len))) {
     104           0 :                 TALLOC_FREE(result);
     105           0 :                 return ERROR_DNS_NO_MEMORY;
     106             :         }
     107             : 
     108        2179 :         *presult = result;
     109        2179 :         return ERROR_DNS_SUCCESS;
     110             : }
     111             : 
     112         807 : DNS_ERROR dns_domain_name_from_string( TALLOC_CTX *mem_ctx,
     113             :                                        const char *pszDomainName,
     114             :                                        struct dns_domain_name **presult )
     115             : {
     116           0 :         struct dns_domain_name *result;
     117           0 :         DNS_ERROR err;
     118             : 
     119         807 :         if (!(result = talloc(mem_ctx, struct dns_domain_name))) {
     120           0 :                 return ERROR_DNS_NO_MEMORY;
     121             :         }
     122             : 
     123         807 :         err = LabelList( result, pszDomainName, &result->pLabelList );
     124         807 :         if (!ERR_DNS_IS_OK(err)) {
     125           0 :                 TALLOC_FREE(result);
     126           0 :                 return err;
     127             :         }
     128             : 
     129         807 :         *presult = result;
     130         807 :         return ERROR_DNS_SUCCESS;
     131             : }
     132             : 
     133             : /*********************************************************************
     134             : *********************************************************************/
     135             : 
     136          42 : char *dns_generate_keyname( TALLOC_CTX *mem_ctx )
     137             : {
     138          42 :         char *result = NULL;
     139             : #if defined(HAVE_KRB5)
     140             : 
     141           0 :         struct GUID guid;
     142             : 
     143          42 :         guid = GUID_random();
     144          42 :         result = GUID_string(mem_ctx, &guid);
     145             : 
     146             : #endif
     147             : 
     148          42 :         return result;
     149             : }

Generated by: LCOV version 1.14