|
|
@ -1,5 +1,8 @@ |
|
|
|
/*
|
|
|
|
netcfg.c : Configures the network |
|
|
|
netcfg.c - Configures the network |
|
|
|
Author - David Whedon, Karl Hammar, Aspö Data |
|
|
|
|
|
|
|
Copyright terms: GPL |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
@ -9,18 +12,27 @@ |
|
|
|
#include <unistd.h> |
|
|
|
#include <sys/types.h> |
|
|
|
#include <sys/stat.h> |
|
|
|
#include "debconf.h" |
|
|
|
#include <debconfclient.h> |
|
|
|
|
|
|
|
char *hostname = NULL; |
|
|
|
char *domain = NULL; |
|
|
|
u_int32_t ipaddress = 0; |
|
|
|
u_int32_t network_address = 0; |
|
|
|
u_int32_t netmask = 0; |
|
|
|
u_int32_t gateway = 0; |
|
|
|
static char *hostname = NULL; |
|
|
|
static char *domain = NULL; |
|
|
|
static u_int32_t ipaddress = 0; |
|
|
|
static u_int32_t network_address = 0; |
|
|
|
char network_address_str[16]; |
|
|
|
static u_int32_t netmask = 0; |
|
|
|
char netmask_str[16]; |
|
|
|
static u_int32_t gateway = 0; |
|
|
|
char gateway_str[16]; |
|
|
|
static u_int32_t nameservers[4]={0}; |
|
|
|
char nameservers_str[16]; |
|
|
|
static char address_buf[16]; |
|
|
|
|
|
|
|
//#define INTERFACES_FILE "/etc/network/interfaces"
|
|
|
|
#define INTERFACES_FILE "etc/network/interfaces" |
|
|
|
static struct debconfclient *client; |
|
|
|
|
|
|
|
#define INTERFACES_FILE "/etc/network/interfaces" |
|
|
|
#define HOSTS_FILE "/etc/hosts" |
|
|
|
#define NETWORKS_FILE "/etc/networks" |
|
|
|
#define RESOLV_FILE "/etc/resolv.conf" |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
@ -82,79 +94,114 @@ void |
|
|
|
get_static_cfg (void) |
|
|
|
{ |
|
|
|
int finished = 0; |
|
|
|
char *ptr; |
|
|
|
|
|
|
|
char *ptr ,*ns; |
|
|
|
|
|
|
|
do |
|
|
|
{ |
|
|
|
|
|
|
|
client->command (client, "input", "high", "netcfg/get_hostname", |
|
|
|
NULL); |
|
|
|
client->command (client, "go", NULL); |
|
|
|
client->command (client, "get", "netcfg/get_hostname", NULL); |
|
|
|
|
|
|
|
hostname = strdup(client->value); |
|
|
|
client->command (client, "subst", "netcfg/confirm_static_cfg", |
|
|
|
"hostname", hostname, NULL); |
|
|
|
|
|
|
|
debconf_command ("INPUT", "critical", "netcfg/get_hostname", NULL); |
|
|
|
debconf_command ("GO", NULL); |
|
|
|
debconf_command ("GET", "netcfg/get_hostname", NULL); |
|
|
|
hostname = debconf_ret (); |
|
|
|
debconf_command ("subst", "netcfg/confirm_static_cfg", "hostname", |
|
|
|
hostname, NULL); |
|
|
|
|
|
|
|
debconf_command ("INPUT", "critical", "netcfg/get_domain", NULL); |
|
|
|
debconf_command ("GO", NULL); |
|
|
|
debconf_command ("GET", "netcfg/get_domain", NULL); |
|
|
|
domain = debconf_ret (); |
|
|
|
debconf_command ("subst", "netcfg/confirm_static_cfg", "domain", domain, |
|
|
|
client->command (client, "input", "critical", "netcfg/get_domain", |
|
|
|
NULL); |
|
|
|
client->command (client, "go", NULL); |
|
|
|
client->command (client, "get", "netcfg/get_domain", NULL); |
|
|
|
domain = strdup(client->value); |
|
|
|
client->command (client, "subst", "netcfg/confirm_static_cfg", "domain", |
|
|
|
domain, NULL); |
|
|
|
|
|
|
|
|
|
|
|
debconf_command ("INPUT", "critical", "netcfg/get_ipaddress", NULL); |
|
|
|
debconf_command ("GO", NULL); |
|
|
|
debconf_command ("GET", "netcfg/get_ipaddress", NULL); |
|
|
|
ptr = debconf_ret (); |
|
|
|
debconf_command ("subst", "netcfg/confirm_static_cfg", "ipaddress", ptr, |
|
|
|
client->command (client, "input", "critical", "netcfg/get_ipaddress", |
|
|
|
NULL); |
|
|
|
client->command (client, "go", NULL); |
|
|
|
client->command (client, "get", "netcfg/get_ipaddress", NULL); |
|
|
|
ptr = client->value; |
|
|
|
dot2num (&ipaddress, ptr); |
|
|
|
client->command (client, "subst", "netcfg/confirm_static_cfg", |
|
|
|
"ipaddress", ptr, NULL); |
|
|
|
|
|
|
|
debconf_command ("INPUT", "critical", "netcfg/get_netmask", NULL); |
|
|
|
debconf_command ("GO", NULL); |
|
|
|
debconf_command ("GET", "netcfg/get_netmask", NULL); |
|
|
|
ptr = debconf_ret (); |
|
|
|
debconf_command ("subst", "netcfg/confirm_static_cfg", "netmask", ptr, |
|
|
|
NULL); |
|
|
|
dot2num (&netmask, debconf_ret ()); |
|
|
|
|
|
|
|
debconf_command ("INPUT", "critical", "netcfg/get_gateway", NULL); |
|
|
|
debconf_command ("GO", NULL); |
|
|
|
debconf_command ("GET", "netcfg/get_gateway", NULL); |
|
|
|
ptr = debconf_ret (); |
|
|
|
debconf_command ("subst", "netcfg/confirm_static_cfg", "gateway", ptr, |
|
|
|
client->command (client, "input", "critical", "netcfg/get_netmask", |
|
|
|
NULL); |
|
|
|
client->command (client, "go", NULL); |
|
|
|
client->command (client, "get", "netcfg/get_netmask", NULL); |
|
|
|
ptr = client->value; |
|
|
|
dot2num (&netmask, ptr); |
|
|
|
client->command (client, "subst", "netcfg/confirm_static_cfg", |
|
|
|
"netmask", ptr, NULL); |
|
|
|
|
|
|
|
network_address = ipaddress & netmask; |
|
|
|
num2dot(network_address, address_buf); |
|
|
|
client->command (client, "subst", "netcfg/confirm_static_cfg", |
|
|
|
"network", address_buf, NULL); |
|
|
|
client->command (client, "input", "critical", "netcfg/get_gateway", |
|
|
|
NULL); |
|
|
|
dot2num (&gateway, ptr); |
|
|
|
client->command (client, "go", NULL); |
|
|
|
client->command (client, "get", "netcfg/get_gateway", NULL); |
|
|
|
ptr = client->value; |
|
|
|
dot2num (&ipaddress, ptr); |
|
|
|
client->command (client, "subst", "netcfg/confirm_static_cfg", |
|
|
|
"gateway", ptr, NULL); |
|
|
|
|
|
|
|
|
|
|
|
debconf_command ("INPUT", "high", "netcfg/confirm_static_cfg", NULL); |
|
|
|
debconf_command ("GO", NULL); |
|
|
|
debconf_command ("GET", "netcfg/confirm_static_cfg", NULL); |
|
|
|
ptr = debconf_ret (); |
|
|
|
client->command (client, "input", "critical", "netcfg/get_nameservers", |
|
|
|
NULL); |
|
|
|
client->command (client, "go", NULL); |
|
|
|
client->command (client, "get", "netcfg/get_nameservers", NULL); |
|
|
|
ptr = strdup(client->value); |
|
|
|
|
|
|
|
ns = strtok(ptr, " "); |
|
|
|
client->command (client, "subst", "netcfg/confirm_static_cfg", |
|
|
|
"primary_DNS", ns, NULL); |
|
|
|
|
|
|
|
ns = strtok(NULL, " "); |
|
|
|
client->command (client, "subst", "netcfg/confirm_static_cfg", |
|
|
|
"secondary_DNS", ns, NULL); |
|
|
|
ns = strtok(NULL, " "); |
|
|
|
client->command (client, "subst", "netcfg/confirm_static_cfg", |
|
|
|
"tertiary_DNS", ns, NULL); |
|
|
|
|
|
|
|
free(ptr); |
|
|
|
|
|
|
|
|
|
|
|
client->command (client, "input", "high", "netcfg/confirm_static_cfg", |
|
|
|
NULL); |
|
|
|
client->command (client, "go", NULL); |
|
|
|
client->command (client, "get", "netcfg/confirm_static_cfg", NULL); |
|
|
|
ptr = client->value; |
|
|
|
|
|
|
|
if (strstr (ptr, "true")) |
|
|
|
finished = 1; |
|
|
|
else |
|
|
|
{ |
|
|
|
debconf_command ("fset", "netcfg/get_hostname", "seen", "false", |
|
|
|
NULL); |
|
|
|
debconf_command ("fset", "netcfg/get_domain", "seen", "false", |
|
|
|
NULL); |
|
|
|
debconf_command ("fset", "netcfg/get_ipaddress", "seen", "false", |
|
|
|
NULL); |
|
|
|
debconf_command ("fset", "netcfg/get_netmask", "seen", "false", |
|
|
|
NULL); |
|
|
|
debconf_command ("fset", "netcfg/get_gateway", "seen", "false", |
|
|
|
NULL); |
|
|
|
debconf_command ("fset", "netcfg/confirm_static_cfg", "seen", |
|
|
|
client->command (client, "fset", "netcfg/get_hostname", "seen", |
|
|
|
"false", NULL); |
|
|
|
client->command (client, "fset", "netcfg/get_domain", "seen", |
|
|
|
"false", NULL); |
|
|
|
client->command (client, "fset", "netcfg/get_ipaddress", "seen", |
|
|
|
"false", NULL); |
|
|
|
client->command (client, "fset", "netcfg/get_netmask", "seen", |
|
|
|
"false", NULL); |
|
|
|
client->command (client, "fset", "netcfg/get_gateway", "seen", |
|
|
|
"false", NULL); |
|
|
|
client->command (client, "fset", "netcfg/get_nameservers", "seen", |
|
|
|
"false", NULL); |
|
|
|
client->command (client, "fset", "netcfg/confirm_static_cfg", |
|
|
|
"seen", "false", NULL); |
|
|
|
free(hostname); |
|
|
|
free(domain); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
while (!finished); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -164,9 +211,51 @@ void |
|
|
|
write_static_cfg (void) |
|
|
|
{ |
|
|
|
FILE *fp; |
|
|
|
char buf[16]; |
|
|
|
|
|
|
|
/* /etc/hosts */ |
|
|
|
|
|
|
|
if ((fp = fopen (HOSTS_FILE, "w"))) |
|
|
|
{ |
|
|
|
fprintf(fp, "127.0.0.1\tlocalhost\n"); |
|
|
|
num2dot (ipaddress, address_buf); |
|
|
|
if (domain) { |
|
|
|
fprintf(fp, "%s\t%s.%s\t%s\n", address_buf, |
|
|
|
hostname, domain, hostname); |
|
|
|
} else { |
|
|
|
num2dot (ipaddress, address_buf); |
|
|
|
fprintf(fp, "%s\t%s\n", address_buf, hostname); |
|
|
|
} |
|
|
|
|
|
|
|
fclose(fp); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
perror ("fopen"); |
|
|
|
} |
|
|
|
|
|
|
|
/* /etc/networks */ |
|
|
|
if ((fp = fopen(NETWORKS_FILE, "w"))) { |
|
|
|
num2dot (network_address, address_buf); |
|
|
|
fprintf(fp, "localnet %s\n", address_buf); |
|
|
|
fclose(fp); |
|
|
|
} else { |
|
|
|
perror("fopen"); |
|
|
|
} |
|
|
|
|
|
|
|
/* /etc/resolv.conf */ |
|
|
|
if ((fp = fopen(RESOLV_FILE, "w"))) { |
|
|
|
int i=0; |
|
|
|
if (domain) fprintf(fp, "search %s\n", domain); |
|
|
|
while(nameservers[i]){ |
|
|
|
num2dot (nameservers[i++], address_buf); |
|
|
|
fprintf(fp, "nameserver %s\n", address_buf); |
|
|
|
} |
|
|
|
fclose(fp); |
|
|
|
} else { |
|
|
|
perror("fopen"); |
|
|
|
} |
|
|
|
|
|
|
|
/* /etc/network/interfaces */ |
|
|
|
if ((fp = fopen (INTERFACES_FILE, "w"))) |
|
|
|
{ |
|
|
|
fprintf (fp, |
|
|
@ -174,28 +263,28 @@ write_static_cfg (void) |
|
|
|
fprintf (fp, "# (network, broadcast and gateway are optional)\n"); |
|
|
|
fprintf (fp, "iface eth0 inet static\n"); |
|
|
|
|
|
|
|
num2dot (ipaddress, buf); |
|
|
|
fprintf (fp, "\taddress %s\n", buf); |
|
|
|
num2dot (ipaddress, address_buf); |
|
|
|
fprintf (fp, "\taddress %s\n", address_buf); |
|
|
|
|
|
|
|
num2dot (netmask, buf); |
|
|
|
fprintf (fp, "\tnetmask %s\n", buf); |
|
|
|
num2dot (netmask, address_buf); |
|
|
|
fprintf (fp, "\tnetmask %s\n", address_buf); |
|
|
|
|
|
|
|
num2dot (ipaddress, buf); |
|
|
|
fprintf (fp, "\tnetwork %s\n", buf); |
|
|
|
num2dot (ipaddress, address_buf); |
|
|
|
fprintf (fp, "\tnetwork %s\n", address_buf); |
|
|
|
|
|
|
|
num2dot (ipaddress, buf); |
|
|
|
fprintf (fp, "\tbroadcast %s\n", buf); |
|
|
|
num2dot (ipaddress, address_buf); |
|
|
|
fprintf (fp, "\tbroadcast %s\n", address_buf); |
|
|
|
|
|
|
|
num2dot (ipaddress, buf); |
|
|
|
fprintf (fp, "\tgateway %s\n", buf); |
|
|
|
num2dot (ipaddress, address_buf); |
|
|
|
fprintf (fp, "\tgateway %s\n", address_buf); |
|
|
|
|
|
|
|
fclose (fp); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
perror ("fopen"); |
|
|
|
fprintf (stderr, "Unable to write to '%s'\n", INTERFACES_FILE); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -203,9 +292,14 @@ write_static_cfg (void) |
|
|
|
int |
|
|
|
main (int argc, char *argv[]) |
|
|
|
{ |
|
|
|
int i; |
|
|
|
|
|
|
|
dot2num(&i,"192.168.0.1"); |
|
|
|
fprintf(stderr, "%x\n", i); |
|
|
|
|
|
|
|
client = debconfclient_new (); |
|
|
|
|
|
|
|
debconf_command ("TITLE", "Network Configuration", NULL); |
|
|
|
client->command (client, "title", "Network Configuration", NULL); |
|
|
|
|
|
|
|
get_static_cfg (); |
|
|
|
|
|
|
|