LCOV - code coverage report
Current view: top level - libcli/dns - resolvconf.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 35 52 67.3 %
Date: 2024-04-21 15:09:00 Functions: 1 2 50.0 %

          Line data    Source code
       1             : /*
       2             :  *  Unix SMB/CIFS implementation.
       3             :  *  Internal DNS query structures
       4             :  *  Copyright (C) Volker Lendecke 2018
       5             :  *
       6             :  *  This program is free software; you can redistribute it and/or modify
       7             :  *  it under the terms of the GNU General Public License as published by
       8             :  *  the Free Software Foundation; either version 3 of the License, or
       9             :  *  (at your option) any later version.
      10             :  *
      11             :  *  This program is distributed in the hope that it will be useful,
      12             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  *  GNU General Public License for more details.
      15             :  *
      16             :  *  You should have received a copy of the GNU General Public License
      17             :  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : #include "replace.h"
      21             : #include <stdio.h>
      22             : #include <errno.h>
      23             : #include "libcli/dns/resolvconf.h"
      24             : #include "lib/util/memory.h"
      25             : 
      26        1412 : int parse_resolvconf_fp(
      27             :         FILE *fp,
      28             :         TALLOC_CTX *mem_ctx,
      29             :         char ***pnameservers,
      30             :         size_t *pnum_nameservers)
      31             : {
      32        1412 :         char *line = NULL;
      33        1412 :         size_t len = 0;
      34        1412 :         char **nameservers = NULL;
      35        1412 :         size_t num_nameservers = 0;
      36        1412 :         int ret = 0;
      37             : 
      38        2827 :         while (true) {
      39        4238 :                 char *saveptr = NULL, *option = NULL, *ns = NULL;
      40        4238 :                 char **tmp = NULL;
      41        4238 :                 ssize_t n = 0;
      42             : 
      43        4238 :                 n = getline(&line, &len, fp);
      44        4238 :                 if (n < 0) {
      45        1412 :                         if (!feof(fp)) {
      46             :                                 /* real error */
      47           0 :                                 ret = errno;
      48             :                         }
      49        1412 :                         break;
      50             :                 }
      51        2826 :                 if ((n > 0) && (line[n-1] == '\n')) {
      52        2825 :                         line[n-1] = '\0';
      53             :                 }
      54             : 
      55        2826 :                 if ((line[0] == '#') || (line[0] == ';')) {
      56           2 :                         continue;
      57             :                 }
      58             : 
      59        2825 :                 option = strtok_r(line, " \t", &saveptr);
      60        2825 :                 if (option == NULL) {
      61           0 :                         continue;
      62             :                 }
      63             : 
      64        2825 :                 if (strcmp(option, "nameserver") != 0) {
      65           1 :                         continue;
      66             :                 }
      67             : 
      68        2824 :                 ns = strtok_r(NULL, " \t", &saveptr);
      69        2824 :                 if (ns == NULL) {
      70           0 :                         continue;
      71             :                 }
      72             : 
      73        2824 :                 tmp = talloc_realloc(
      74             :                         mem_ctx,
      75             :                         nameservers,
      76             :                         char *,
      77             :                         num_nameservers+1);
      78        2824 :                 if (tmp == NULL) {
      79           0 :                         ret = ENOMEM;
      80           0 :                         break;
      81             :                 }
      82        2824 :                 nameservers = tmp;
      83             : 
      84        2824 :                 nameservers[num_nameservers] = talloc_strdup(nameservers, ns);
      85        2824 :                 if (nameservers[num_nameservers] == NULL) {
      86           0 :                         ret = ENOMEM;
      87           0 :                         break;
      88             :                 }
      89        2824 :                 num_nameservers += 1;
      90             :         }
      91             : 
      92        1412 :         SAFE_FREE(line);
      93             : 
      94        1412 :         if (ret == 0) {
      95        1412 :                 *pnameservers = nameservers;
      96        1412 :                 *pnum_nameservers = num_nameservers;
      97             :         } else {
      98           0 :                 TALLOC_FREE(nameservers);
      99             :         }
     100             : 
     101        1412 :         return ret;
     102             : }
     103             : 
     104           0 : int parse_resolvconf(
     105             :         const char *resolvconf,
     106             :         TALLOC_CTX *mem_ctx,
     107             :         char ***pnameservers,
     108             :         size_t *pnum_nameservers)
     109             : {
     110           0 :         FILE *fp;
     111           0 :         int ret;
     112             : 
     113           0 :         fp = fopen(resolvconf ? resolvconf : "/etc/resolv.conf", "r");
     114           0 :         if (fp == NULL) {
     115           0 :                 return errno;
     116             :         }
     117             : 
     118           0 :         ret = parse_resolvconf_fp(fp, mem_ctx, pnameservers, pnum_nameservers);
     119             : 
     120           0 :         fclose(fp);
     121             : 
     122           0 :         return ret;
     123             : }

Generated by: LCOV version 1.14