2020-07-19 00:02:25 +00:00
|
|
|
/*
|
|
|
|
* libifupdown/interface-file.h
|
|
|
|
* Purpose: /etc/network/interfaces parser
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* This software is provided 'as is' and without any warranty, express or
|
|
|
|
* implied. In no event shall the authors be liable for any damages arising
|
|
|
|
* from the use of this software.
|
|
|
|
*/
|
|
|
|
|
2020-07-19 00:48:49 +00:00
|
|
|
#include <ctype.h>
|
2020-07-19 00:02:25 +00:00
|
|
|
#include <stdio.h>
|
2020-07-19 00:48:49 +00:00
|
|
|
#include <string.h>
|
2020-07-19 00:02:25 +00:00
|
|
|
#include "libifupdown/interface-file.h"
|
2020-07-19 00:48:49 +00:00
|
|
|
#include "libifupdown/fgetline.h"
|
2020-07-24 10:12:42 +00:00
|
|
|
#include "libifupdown/tokenize.h"
|
2020-07-19 00:02:25 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
lif_interface_file_parse(struct lif_dict *collection, const char *filename)
|
|
|
|
{
|
|
|
|
lif_interface_collection_init(collection);
|
2020-07-19 00:48:49 +00:00
|
|
|
struct lif_interface *cur_iface = NULL;
|
|
|
|
|
|
|
|
FILE *f = fopen(filename, "r");
|
|
|
|
if (f == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char linebuf[4096];
|
|
|
|
while (lif_fgetline(linebuf, sizeof linebuf, f) != NULL)
|
|
|
|
{
|
|
|
|
char *bufp = linebuf;
|
2020-07-24 10:12:42 +00:00
|
|
|
char *token = lif_next_token(&bufp);
|
2020-07-19 00:48:49 +00:00
|
|
|
|
|
|
|
if (!*token || !isalpha(*token))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!strcmp(token, "source"))
|
|
|
|
{
|
2020-07-24 10:12:42 +00:00
|
|
|
char *source_filename = lif_next_token(&bufp);
|
2020-07-19 00:48:49 +00:00
|
|
|
if (!*source_filename)
|
|
|
|
goto parse_error;
|
2020-07-19 00:02:25 +00:00
|
|
|
|
2020-07-19 00:48:49 +00:00
|
|
|
if (!strcmp(filename, source_filename))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: attempt to source %s would create infinite loop\n",
|
|
|
|
filename, source_filename);
|
|
|
|
goto parse_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
lif_interface_file_parse(collection, source_filename);
|
|
|
|
}
|
|
|
|
else if (!strcmp(token, "auto"))
|
|
|
|
{
|
2020-07-24 10:12:42 +00:00
|
|
|
char *ifname = lif_next_token(&bufp);
|
2020-07-19 00:48:49 +00:00
|
|
|
if (!*ifname && cur_iface == NULL)
|
|
|
|
goto parse_error;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cur_iface = lif_interface_collection_find(collection, ifname);
|
|
|
|
if (cur_iface == NULL)
|
|
|
|
goto parse_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur_iface->is_auto = true;
|
|
|
|
}
|
|
|
|
else if (!strcmp(token, "iface"))
|
|
|
|
{
|
2020-07-24 10:12:42 +00:00
|
|
|
char *ifname = lif_next_token(&bufp);
|
2020-07-19 00:48:49 +00:00
|
|
|
if (!*ifname)
|
|
|
|
goto parse_error;
|
|
|
|
|
|
|
|
cur_iface = lif_interface_collection_find(collection, ifname);
|
|
|
|
if (cur_iface == NULL)
|
|
|
|
goto parse_error;
|
|
|
|
|
|
|
|
/* in original ifupdown config, we can have "inet loopback"
|
|
|
|
* or "inet dhcp" or such to designate hints. lets pick up
|
|
|
|
* those hints here.
|
|
|
|
*/
|
2020-08-04 17:32:34 +00:00
|
|
|
char *token = lif_next_token(&bufp);
|
|
|
|
while (*token)
|
2020-07-24 11:00:49 +00:00
|
|
|
{
|
2020-08-04 17:32:34 +00:00
|
|
|
if (!strcmp(token, "dhcp"))
|
|
|
|
{
|
|
|
|
cur_iface->is_dhcp = true;
|
|
|
|
lif_dict_add(&cur_iface->vars, "use", strdup("dhcp"));
|
|
|
|
}
|
2020-08-04 17:46:56 +00:00
|
|
|
else if (!strcmp(token, "inherits"))
|
|
|
|
{
|
|
|
|
token = lif_next_token(&bufp);
|
|
|
|
|
|
|
|
if (!*token)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: inherits without interface\n", filename);
|
|
|
|
goto parse_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lif_interface_collection_inherit(cur_iface, collection, token))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: could not inherit %s\n", filename, token);
|
|
|
|
goto parse_error;
|
|
|
|
}
|
|
|
|
}
|
2020-08-04 17:32:34 +00:00
|
|
|
|
|
|
|
token = lif_next_token(&bufp);
|
2020-07-24 11:00:49 +00:00
|
|
|
}
|
2020-07-19 00:48:49 +00:00
|
|
|
}
|
2020-07-24 10:37:14 +00:00
|
|
|
else if (!strcmp(token, "use"))
|
|
|
|
{
|
|
|
|
char *executor = lif_next_token(&bufp);
|
|
|
|
|
2020-07-25 14:49:37 +00:00
|
|
|
if (cur_iface == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: use '%s' without interface\n", filename, executor);
|
|
|
|
goto parse_error;
|
|
|
|
}
|
|
|
|
|
2020-07-24 10:37:14 +00:00
|
|
|
/* pass requires as compatibility env vars to appropriate executors (bridge, bond) */
|
|
|
|
if (!strcmp(executor, "dhcp"))
|
|
|
|
cur_iface->is_dhcp = true;
|
|
|
|
else if (!strcmp(executor, "bridge"))
|
|
|
|
cur_iface->is_bridge = true;
|
|
|
|
else if (!strcmp(executor, "bond"))
|
|
|
|
cur_iface->is_bond = true;
|
2020-07-26 08:32:00 +00:00
|
|
|
else if (!strcmp(executor, "static"))
|
2020-07-26 09:29:16 +00:00
|
|
|
{
|
2020-07-26 08:32:00 +00:00
|
|
|
cur_iface->is_static = true;
|
2020-07-26 09:29:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!strcmp(executor, "link"))
|
|
|
|
continue;
|
2020-07-24 10:37:14 +00:00
|
|
|
|
|
|
|
lif_dict_add(&cur_iface->vars, token, strdup(executor));
|
|
|
|
}
|
2020-08-04 17:46:56 +00:00
|
|
|
else if (!strcmp(token, "inherit"))
|
|
|
|
{
|
|
|
|
token = lif_next_token(&bufp);
|
|
|
|
|
|
|
|
if (!*token)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: inherits without interface\n", filename);
|
|
|
|
goto parse_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lif_interface_collection_inherit(cur_iface, collection, token))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: could not inherit %s\n", filename, token);
|
|
|
|
goto parse_error;
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 00:48:49 +00:00
|
|
|
else if (!strcmp(token, "address"))
|
|
|
|
{
|
2020-07-24 10:12:42 +00:00
|
|
|
char *addr = lif_next_token(&bufp);
|
2020-07-19 00:48:49 +00:00
|
|
|
|
|
|
|
if (cur_iface == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: address '%s' without interface\n", filename, addr);
|
|
|
|
goto parse_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
lif_interface_address_add(cur_iface, addr);
|
|
|
|
}
|
|
|
|
else if (cur_iface != NULL)
|
|
|
|
{
|
|
|
|
lif_dict_add(&cur_iface->vars, token, strdup(bufp));
|
2020-07-28 20:39:20 +00:00
|
|
|
|
|
|
|
/* Check if token looks like <word1>-<word*> and assume <word1> is an addon */
|
|
|
|
char *word_end = strchr(token, '-');
|
|
|
|
if (word_end)
|
|
|
|
{
|
|
|
|
/* Copy word1 to not mangle *token */
|
|
|
|
char *addon = strndup(token, word_end - token);
|
2020-07-28 21:57:51 +00:00
|
|
|
if (lif_dict_add_once(&cur_iface->vars, "use", addon, (lif_dict_cmp_t) strcmp) == NULL)
|
|
|
|
free(addon);
|
2020-07-28 20:39:20 +00:00
|
|
|
|
|
|
|
/* pass requires as compatibility env vars to appropriate executors (bridge, bond) */
|
|
|
|
if (!strcmp(addon, "dhcp"))
|
|
|
|
cur_iface->is_dhcp = true;
|
|
|
|
else if (!strcmp(addon, "bridge"))
|
|
|
|
cur_iface->is_bridge = true;
|
|
|
|
else if (!strcmp(addon, "bond"))
|
|
|
|
cur_iface->is_bond = true;
|
|
|
|
}
|
2020-07-19 00:48:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
2020-07-19 00:02:25 +00:00
|
|
|
return true;
|
2020-07-19 00:48:49 +00:00
|
|
|
|
|
|
|
parse_error:
|
|
|
|
fprintf(stderr, "libifupdown: %s: failed to parse line \"%s\"\n",
|
|
|
|
filename, linebuf);
|
|
|
|
fclose(f);
|
|
|
|
return false;
|
2020-07-19 00:02:25 +00:00
|
|
|
}
|