s4:kdc: Add functionality to log client and server authentication policies
[Samba.git] / libcli / dns / resolvconf.c
blob5cf8b4e7882978a9eda55f7c4acde9308c307e61
1 /*
2 * Unix SMB/CIFS implementation.
3 * Internal DNS query structures
4 * Copyright (C) Volker Lendecke 2018
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.
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.
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/>.
20 #include "replace.h"
21 #include <stdio.h>
22 #include <errno.h>
23 #include "libcli/dns/resolvconf.h"
24 #include "lib/util/memory.h"
26 int parse_resolvconf_fp(
27 FILE *fp,
28 TALLOC_CTX *mem_ctx,
29 char ***pnameservers,
30 size_t *pnum_nameservers)
32 char *line = NULL;
33 size_t len = 0;
34 char **nameservers = NULL;
35 size_t num_nameservers = 0;
36 int ret = 0;
38 while (true) {
39 char *saveptr = NULL, *option = NULL, *ns = NULL;
40 char **tmp = NULL;
41 ssize_t n = 0;
43 n = getline(&line, &len, fp);
44 if (n < 0) {
45 if (!feof(fp)) {
46 /* real error */
47 ret = errno;
49 break;
51 if ((n > 0) && (line[n-1] == '\n')) {
52 line[n-1] = '\0';
55 if ((line[0] == '#') || (line[0] == ';')) {
56 continue;
59 option = strtok_r(line, " \t", &saveptr);
60 if (option == NULL) {
61 continue;
64 if (strcmp(option, "nameserver") != 0) {
65 continue;
68 ns = strtok_r(NULL, " \t", &saveptr);
69 if (ns == NULL) {
70 continue;
73 tmp = talloc_realloc(
74 mem_ctx,
75 nameservers,
76 char *,
77 num_nameservers+1);
78 if (tmp == NULL) {
79 ret = ENOMEM;
80 break;
82 nameservers = tmp;
84 nameservers[num_nameservers] = talloc_strdup(nameservers, ns);
85 if (nameservers[num_nameservers] == NULL) {
86 ret = ENOMEM;
87 break;
89 num_nameservers += 1;
92 SAFE_FREE(line);
94 if (ret == 0) {
95 *pnameservers = nameservers;
96 *pnum_nameservers = num_nameservers;
97 } else {
98 TALLOC_FREE(nameservers);
101 return ret;
104 int parse_resolvconf(
105 const char *resolvconf,
106 TALLOC_CTX *mem_ctx,
107 char ***pnameservers,
108 size_t *pnum_nameservers)
110 FILE *fp;
111 int ret;
113 fp = fopen(resolvconf ? resolvconf : "/etc/resolv.conf", "r");
114 if (fp == NULL) {
115 return errno;
118 ret = parse_resolvconf_fp(fp, mem_ctx, pnameservers, pnum_nameservers);
120 fclose(fp);
122 return ret;