Skip to content

Commit 2090067

Browse files
mobrembskiMichał Obrembski
authored andcommitted
Added feature to customize resolv.conf file path at configure
1 parent 0353e0b commit 2090067

2 files changed

Lines changed: 36 additions & 26 deletions

File tree

configure.ac

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,12 @@ int main(int argc, char **argv){
491491
AC_MSG_NOTICE([HAVE_SO_BINDTODEVICE... 0])
492492
])
493493

494+
AC_ARG_WITH([resolvconf-config-file],
495+
AS_HELP_STRING([--with-resolvconf-config-file],
496+
[Set the path to the resolv.conf config file. \
497+
Default is /etc/resolv.conf.]),
498+
AC_DEFINE_UNQUOTED([RESOLV_CONF_FILE_PATH],["$withval"])
499+
)
494500

495501
# prepare to get rid of obsolete code (FortiOS 4)
496502
AC_ARG_ENABLE([obsolete],

src/ipv4.c

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ static char show_route_buffer[SHOW_ROUTE_BUFFER_SIZE];
4545
#define ERR_IPV4_NO_SUCH_ROUTE -4
4646
#define ERR_IPV4_PROC_NET_ROUTE -5
4747

48+
#ifndef RESOLV_CONF_FILE_PATH
49+
#define RESOLV_CONF_FILE_PATH /etc/resolv.conf
50+
#endif
51+
4852
static inline const char *err_ipv4_str(int code)
4953
{
5054
if (code == ERR_IPV4_SEE_ERRNO)
@@ -1120,36 +1124,36 @@ int ipv4_add_nameservers_to_resolv_conf(struct tunnel *tunnel)
11201124
free(resolvconf_call);
11211125
} else {
11221126
#endif
1123-
log_debug("Attempting to modify /etc/resolv.conf directly.\n");
1124-
file = fopen("/etc/resolv.conf", "r+");
1127+
log_debug("Attempting to modify " RESOLV_CONF_FILE_PATH " directly.\n");
1128+
file = fopen(RESOLV_CONF_FILE_PATH, "r+");
11251129
if (file == NULL) {
1126-
log_warn("Could not open /etc/resolv.conf (%s).\n",
1130+
log_warn("Could not open " RESOLV_CONF_FILE_PATH " (%s).\n",
11271131
strerror(errno));
11281132
return 1;
11291133
}
11301134

11311135
if (fstat(fileno(file), &stat) == -1) {
1132-
log_warn("Could not stat /etc/resolv.conf (%s).\n",
1136+
log_warn("Could not stat " RESOLV_CONF_FILE_PATH " (%s).\n",
11331137
strerror(errno));
11341138
goto err_close;
11351139
}
11361140

11371141
if (stat.st_size == 0) {
1138-
log_warn("Could not read /etc/resolv.conf (%s).\n",
1142+
log_warn("Could not read " RESOLV_CONF_FILE_PATH " (%s).\n",
11391143
"Empty file");
11401144
goto err_close;
11411145
}
11421146

11431147
buffer = malloc(stat.st_size + 1);
11441148
if (buffer == NULL) {
1145-
log_warn("Could not read /etc/resolv.conf (%s).\n",
1149+
log_warn("Could not read " RESOLV_CONF_FILE_PATH " (%s).\n",
11461150
strerror(errno));
11471151
goto err_close;
11481152
}
11491153

11501154
// Copy all file contents at once
11511155
if (fread(buffer, stat.st_size, 1, file) != 1) {
1152-
log_warn("Could not read /etc/resolv.conf.\n");
1156+
log_warn("Could not read "RESOLV_CONF_FILE_PATH ".\n");
11531157
goto err_free;
11541158
}
11551159

@@ -1189,24 +1193,24 @@ int ipv4_add_nameservers_to_resolv_conf(struct tunnel *tunnel)
11891193
line = strtok_r(NULL, "\n", &saveptr)) {
11901194
if (strcmp(line, ns1) == 0) {
11911195
tunnel->ipv4.ns1_was_there = 1;
1192-
log_debug("ns1 already present in /etc/resolv.conf.\n");
1196+
log_debug("ns1 already present in " RESOLV_CONF_FILE_PATH ".\n");
11931197
}
11941198
}
11951199

11961200
if (tunnel->ipv4.ns1_was_there == 0)
1197-
log_debug("Adding \"%s\", to /etc/resolv.conf.\n", ns1);
1201+
log_debug("Adding \"%s\", to " RESOLV_CONF_FILE_PATH " .\n", ns1);
11981202

11991203
for (const char *line = strtok_r(buffer, "\n", &saveptr);
12001204
line != NULL;
12011205
line = strtok_r(NULL, "\n", &saveptr)) {
12021206
if (strcmp(line, ns2) == 0) {
12031207
tunnel->ipv4.ns2_was_there = 1;
1204-
log_debug("ns2 already present in /etc/resolv.conf.\n");
1208+
log_debug("ns2 already present in " RESOLV_CONF_FILE_PATH ".\n");
12051209
}
12061210
}
12071211

12081212
if (tunnel->ipv4.ns2_was_there == 0)
1209-
log_debug("Adding \"%s\", to /etc/resolv.conf.\n", ns2);
1213+
log_debug("Adding \"%s\", to " RESOLV_CONF_FILE_PATH ".\n", ns2);
12101214

12111215
if (dns_suffix[0] == '\0') {
12121216
tunnel->ipv4.dns_suffix_was_there = -1;
@@ -1217,17 +1221,17 @@ int ipv4_add_nameservers_to_resolv_conf(struct tunnel *tunnel)
12171221
if (dns_suffix[0] != '\0'
12181222
&& strcmp(line, dns_suffix) == 0) {
12191223
tunnel->ipv4.dns_suffix_was_there = 1;
1220-
log_debug("dns_suffix already present in /etc/resolv.conf.\n");
1224+
log_debug("dns_suffix already present in " RESOLV_CONF_FILE_PATH ".\n");
12211225
}
12221226
}
12231227
}
12241228

12251229
if (tunnel->ipv4.dns_suffix_was_there == 0)
1226-
log_debug("Adding \"%s\", to /etc/resolv.conf.\n", dns_suffix);
1230+
log_debug("Adding \"%s\", to " RESOLV_CONF_FILE_PATH ".\n", dns_suffix);
12271231

12281232
rewind(file);
12291233
if (fread(buffer, stat.st_size, 1, file) != 1) {
1230-
log_warn("Could not read /etc/resolv.conf.\n");
1234+
log_warn("Could not read " RESOLV_CONF_FILE_PATH ".\n");
12311235
goto err_free;
12321236
}
12331237

@@ -1263,7 +1267,7 @@ int ipv4_add_nameservers_to_resolv_conf(struct tunnel *tunnel)
12631267
if (use_resolvconf == 0) {
12641268
#endif
12651269
if (fclose(file))
1266-
log_warn("Could not close /etc/resolv.conf: %s\n",
1270+
log_warn("Could not close " RESOLV_CONF_FILE_PATH": %s\n",
12671271
strerror(errno));
12681272
#if HAVE_RESOLVCONF
12691273
} else {
@@ -1321,29 +1325,29 @@ int ipv4_del_nameservers_from_resolv_conf(struct tunnel *tunnel)
13211325
}
13221326
#endif
13231327

1324-
file = fopen("/etc/resolv.conf", "r+");
1328+
file = fopen(RESOLV_CONF_FILE_PATH, "r+");
13251329
if (file == NULL) {
1326-
log_warn("Could not open /etc/resolv.conf (%s).\n",
1330+
log_warn("Could not open " RESOLV_CONF_FILE_PATH " (%s).\n",
13271331
strerror(errno));
13281332
return 1;
13291333
}
13301334

13311335
if (fstat(fileno(file), &stat) == -1) {
1332-
log_warn("Could not stat /etc/resolv.conf (%s).\n",
1336+
log_warn("Could not stat " RESOLV_CONF_FILE_PATH " (%s).\n",
13331337
strerror(errno));
13341338
goto err_close;
13351339
}
13361340

13371341
buffer = malloc(stat.st_size + 1);
13381342
if (buffer == NULL) {
1339-
log_warn("Could not read /etc/resolv.conf (%s).\n",
1343+
log_warn("Could not read " RESOLV_CONF_FILE_PATH " (%s).\n",
13401344
strerror(errno));
13411345
goto err_close;
13421346
}
13431347

13441348
// Copy all file contents at once
13451349
if (fread(buffer, stat.st_size, 1, file) != 1) {
1346-
log_warn("Could not read /etc/resolv.conf.\n");
1350+
log_warn("Could not read " RESOLV_CONF_FILE_PATH ".\n");
13471351
goto err_free;
13481352
}
13491353

@@ -1365,9 +1369,9 @@ int ipv4_del_nameservers_from_resolv_conf(struct tunnel *tunnel)
13651369
strncat(dns_suffix, tunnel->ipv4.dns_suffix, MAX_DOMAIN_LENGTH);
13661370
}
13671371

1368-
file = freopen("/etc/resolv.conf", "w", file);
1372+
file = freopen(RESOLV_CONF_FILE_PATH, "w", file);
13691373
if (file == NULL) {
1370-
log_warn("Could not reopen /etc/resolv.conf (%s).\n",
1374+
log_warn("Could not reopen " RESOLV_CONF_FILE_PATH " (%s).\n",
13711375
strerror(errno));
13721376
goto err_free;
13731377
}
@@ -1377,13 +1381,13 @@ int ipv4_del_nameservers_from_resolv_conf(struct tunnel *tunnel)
13771381
line = strtok_r(NULL, "\n", &saveptr)) {
13781382
if (ns1[0] != '\0' && strcmp(line, ns1) == 0
13791383
&& (tunnel->ipv4.ns1_was_there == 0)) {
1380-
log_debug("Deleting \"%s\" from /etc/resolv.conf.\n", ns1);
1384+
log_debug("Deleting \"%s\" from " RESOLV_CONF_FILE_PATH ".\n", ns1);
13811385
} else if (ns2[0] != '\0' && strcmp(line, ns2) == 0
13821386
&& (tunnel->ipv4.ns2_was_there == 0)) {
1383-
log_debug("Deleting \"%s\" from /etc/resolv.conf.\n", ns2);
1387+
log_debug("Deleting \"%s\" from " RESOLV_CONF_FILE_PATH ".\n", ns2);
13841388
} else if (dns_suffix[0] != '\0' && strcmp(line, dns_suffix) == 0
13851389
&& (tunnel->ipv4.dns_suffix_was_there == 0)) {
1386-
log_debug("Deleting \"%s\" from /etc/resolv.conf.\n", dns_suffix);
1390+
log_debug("Deleting \"%s\" from " RESOLV_CONF_FILE_PATH ".\n", dns_suffix);
13871391
} else {
13881392
fputs(line, file);
13891393
fputs("\n", file);
@@ -1396,7 +1400,7 @@ int ipv4_del_nameservers_from_resolv_conf(struct tunnel *tunnel)
13961400
free(buffer);
13971401
err_close:
13981402
if (file && fclose(file))
1399-
log_warn("Could not close /etc/resolv.conf (%s).\n",
1403+
log_warn("Could not close " RESOLV_CONF_FILE_PATH " (%s).\n",
14001404
strerror(errno));
14011405
return ret;
14021406
}

0 commit comments

Comments
 (0)