summaryrefslogtreecommitdiffstats
path: root/urunlevel/runlevel/udhcpc_script.c
blob: 5d5f592f2fa5d492bea4665c9eb4d453e1f94e9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Mini udhcpc_script implementation for busybox.
 *
 * Copyright (C) 2004 by David Seikel won_fang@yahoo.com.au
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA
 *
 */

#include <errno.h>
#include <string.h>
#include <unistd.h>

#include "busybox.h"
#include "lib_init_d.h"


static void printit(char *name, char *description, char *value)
{
    if (value != NULL)
	bb_printf("%s=%s\t\t- %s\n", name, value, description);
}

int udhcpc_script_main(int argc, char **argv)
{
    argv += optind;

#if 0
    printit("action", "What action the script should perform", *argv);
    printit("HOME", "The set $HOME env or '/'", getenv("HOME"));
    printit("PATH", "the set $PATH env or '/bin:/usr/bin:/sbin:/usr/sbin'", getenv("PATH"));
    printit("interface", "The interface this was obtained on", getenv("interface"));
    printit("ip", "The obtained IP", getenv("ip"));
    printit("mask", "The number of bits in the netmask (ie: 24)", getenv("mask"));
    printit("siaddr", "The bootp next server option", getenv("siaddr"));
    printit("sname", "The bootp server name option", getenv("sname"));
    printit("boot_file", "The bootp boot file option", getenv("boot_file"));
    printit("subnet", "The assigend subnet mask", getenv("subnet"));
    printit("timezone", "Offset in seconds from UTC", getenv("timezone"));
    printit("router", "A list of routers", getenv("router"));
    printit("timesvr", "A list of time servers", getenv("timesvr"));
    printit("namesvr", "A list of IEN 116 name servers", getenv("namesvr"));
    printit("dns", "A list of DNS server", getenv("dns"));
    printit("logsvr", "A list of MIT-LCS UDP log servers", getenv("logsvr"));
    printit("cookiesvr", "A list of RFC 865 cookie servers", getenv("cookiesvr"));
    printit("lprsvr", "A list of LPR servers", getenv("lprsvr"));
    printit("hostname", "The assigned hostname", getenv("hostname"));
    printit("bootsize", "The length in 512 octect blocks of the bootfile", getenv("bootsize"));
    printit("domain", "The domain name of the network", getenv("domain"));
    printit("swapsvr", "The IP address of the client's swap server", getenv("swapsvr"));
    printit("rootpath", "The path name of the client's root disk", getenv("rootpath"));
    printit("ipttl", "The TTL to use for this network", getenv("ipttl"));
    printit("mtu", "The MTU to use for this network", getenv("mtu"));
    printit("broadcast", "The broadcast address for this network", getenv("broadcast"));
    printit("ntpsrv", "A list of NTP servers", getenv("ntpsrv"));
    printit("wins", "A list of WINS servers", getenv("wins"));
    printit("lease", "The lease time, in seconds", getenv("lease"));
    printit("dhcptype", "DHCP message type (safely ignored)", getenv("dhcptype"));
    printit("serverid", "The IP of the server", getenv("serverid"));
    printit("message", "Reason for a DHCPNAK", getenv("message"));
    printit("tftp", "The TFTP server name", getenv("tftp"));
    printit("bootfile", "The bootfile name", getenv("bootfile"));
#endif

    if (!*argv)
	bb_printf("Error: should be called from udhcpc\n");
    else
    {
	char *interface = getenv("interface");

	if (strcmp(*argv, "deconfig") == 0)
	{
#ifdef CONFIG_FEATURE_IFUPDOWN_IP
	    doit(QUIET, "ip route flush dev %s", interface);
	    doit(QUIET, "ip addr flush dev %s", interface);
	    doit(QUIET, "ip link set %s up", interface);
#else
	    errno = 0;
	    while (errno == 0)
		doit(QUIET, "route del default gw 0.0.0.0 dev %s", interface);
	    doit(QUIET, "ifconfig %s 0.0.0.0", interface);
#endif
	}
	else if (strcmp(*argv, "nak") == 0)
	    bb_printf("udhcpc received a NAK: %s\n", getenv("message"));
	else if ((strcmp(*argv, "bound") == 0) || (strcmp(*argv, "renew") == 0))
	{
	    const char *RESOLV_CONF = "/etc/resolv.conf";
	    char *broadcast = getenv("broadcast");
	    char *subnet = getenv("subnet");
	    char *mask = getenv("mask");
	    char *ip = getenv("ip");
	    char *hostname = getenv("hostname");
	    char *domain = getenv("domain");
	    char *router = getenv("router");
	    char *dns = getenv("dns");
	    char *siaddr = getenv("siaddr");
	    char *tftp = getenv("tftp");
	    char *BROADCAST = "";
	    char *NETMASK = "";
	    char *MASK = "";
	    char *HOSTNAME = 0;
	    int i, metric = 0;
	    char *token;
	    char *strtok_temp;

	    if (broadcast != 0)
		bb_xasprintf(&BROADCAST, "broadcast %s", broadcast);
	    if (subnet != 0)
		bb_xasprintf(&NETMASK, "netmask %s", subnet);
	    if (mask != 0)
		bb_xasprintf(&MASK, "/%s", mask);
	    if (hostname != 0)
		bb_xasprintf(&HOSTNAME, "/%s", hostname);
#ifdef CONFIG_FEATURE_IFUPDOWN_IP
	    doit(0, "ip addr add %s%s %s dev %s", ip, MASK, BROADCAST, interface);
	    doit(0, "ip link set %s up", interface);
#else
	    doit(0, "/sbin/ifconfig %s %s %s %s", interface, ip, BROADCAST, NETMASK);
#endif
	    if (router != 0)
	    {
		token = strtok_r(router, " ,\t\r\n", &strtok_temp);
		for (i = 0; token != NULL; i++)
		{
#ifdef CONFIG_FEATURE_IFUPDOWN_IP
		    doit(0, "ip route add to default via %s dev %s", token, interface, metric++);
#else
		    doit(0, "route add default gw %s dev %s metric %d", token, interface, metric++);
#endif
		    token = strtok_r(NULL, " ,\t\r\n", &strtok_temp);
		}
	    }

	    doit(0, "echo -n > %s", RESOLV_CONF);
	    if (domain != 0)
		doit(0, "echo \"search %s\" > %s", domain, RESOLV_CONF);

	    token = strtok_r(dns, " \r\n", &strtok_temp);
	    for (i = 0; token != NULL; i++)
	    {
		doit(0, "echo \"nameserver %s\" >> %s", token, RESOLV_CONF);

		token = strtok_r(NULL, " \r\n", &strtok_temp);
	    }

	    if (HOSTNAME == 0)
	    {
		bb_xasprintf(&HOSTNAME, doit(REDIR, "nslookup %s | grep -v default | grep Name | cut -d\":\" -f2 | tr -d ' ' | tail -n 1", ip));
		if ((HOSTNAME == 0) && (stat("/var/lib/my_linux/config/hostname", &path_stat) == 0))
		    HOSTNAME = quick_read("/var/lib/my_linux/config/hostname");
		if (HOSTNAME == 0)
		    HOSTNAME = "my_linux";
	    }
	    doit(0, "hostname %s", HOSTNAME);
// Should remove old entry
	    doit(0, "echo \"%s      %s\" >> /etc/hosts", ip, HOSTNAME);

// Should dump these in a per interface file
	    if (siaddr != NULL)
		quick_write("/var/lib/network/nfsaddr", siaddr);
	    if (tftp != NULL)
		quick_write("/var/lib/network/tftpaddr", tftp);

	    /* This is what the network init script is waiting for. */
	    quick_write("/var/lib/network/ipaddr", ip);
	    bb_printf("IP %s HOSTNAME %s\n", ip, HOSTNAME);

	    free(HOSTNAME);
	}
	else
	{
	    bb_printf("Huh?\n");
	}
    }

    return EXIT_SUCCESS;
}