aboutsummaryrefslogtreecommitdiff
path: root/networking/udhcp/script.c
diff options
context:
space:
mode:
Diffstat (limited to 'networking/udhcp/script.c')
-rw-r--r--networking/udhcp/script.c44
1 files changed, 14 insertions, 30 deletions
diff --git a/networking/udhcp/script.c b/networking/udhcp/script.c
index 9c766a2e2..dcd2cad2d 100644
--- a/networking/udhcp/script.c
+++ b/networking/udhcp/script.c
@@ -32,7 +32,6 @@
32#include "options.h" 32#include "options.h"
33#include "dhcpd.h" 33#include "dhcpd.h"
34#include "dhcpc.h" 34#include "dhcpc.h"
35#include "script.h"
36#include "common.h" 35#include "common.h"
37 36
38/* get a rough idea of how long an option will be (rounding up...) */ 37/* get a rough idea of how long an option will be (rounding up...) */
@@ -56,14 +55,9 @@ static inline int upper_length(int length, int opt_index)
56} 55}
57 56
58 57
59static int sprintip(char *dest, unsigned char *ip) 58static int sprintip(char *dest, char *pre, unsigned char *ip)
60{ 59{
61 return sprintf(dest, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); 60 return sprintf(dest, "%s%d.%d.%d.%d", pre, ip[0], ip[1], ip[2], ip[3]);
62}
63
64static void asprintip(char **dest, char *pre, unsigned char *ip)
65{
66 asprintf(dest, "%s%d.%d.%d.%d", pre, ip[0], ip[1], ip[2], ip[3]);
67} 61}
68 62
69 63
@@ -96,12 +90,12 @@ static void fill_options(char *dest, unsigned char *option, struct dhcp_option *
96 for(;;) { 90 for(;;) {
97 switch (type) { 91 switch (type) {
98 case OPTION_IP_PAIR: 92 case OPTION_IP_PAIR:
99 dest += sprintip(dest, option); 93 dest += sprintip(dest, "", option);
100 *(dest++) = '/'; 94 *(dest++) = '/';
101 option += 4; 95 option += 4;
102 optlen = 4; 96 optlen = 4;
103 case OPTION_IP: /* Works regardless of host byte order. */ 97 case OPTION_IP: /* Works regardless of host byte order. */
104 dest += sprintip(dest, option); 98 dest += sprintip(dest, "", option);
105 break; 99 break;
106 case OPTION_BOOLEAN: 100 case OPTION_BOOLEAN:
107 dest += sprintf(dest, *option ? "yes" : "no"); 101 dest += sprintf(dest, *option ? "yes" : "no");
@@ -138,15 +132,6 @@ static void fill_options(char *dest, unsigned char *option, struct dhcp_option *
138} 132}
139 133
140 134
141static char *find_env(const char *prefix, char *defaultstr)
142{
143 char *ptr;
144
145 ptr = getenv(prefix);
146 return ptr ? ptr : defaultstr;
147}
148
149
150/* put all the paramaters into an environment */ 135/* put all the paramaters into an environment */
151static char **fill_envp(struct dhcpMessage *packet) 136static char **fill_envp(struct dhcpMessage *packet)
152{ 137{
@@ -167,21 +152,20 @@ static char **fill_envp(struct dhcpMessage *packet)
167 if ((temp = get_option(packet, DHCP_OPTION_OVER))) 152 if ((temp = get_option(packet, DHCP_OPTION_OVER)))
168 over = *temp; 153 over = *temp;
169 if (!(over & FILE_FIELD) && packet->file[0]) num_options++; 154 if (!(over & FILE_FIELD) && packet->file[0]) num_options++;
170 if (!(over & SNAME_FIELD) && packet->sname[0]) num_options++; 155 if (!(over & SNAME_FIELD) && packet->sname[0]) num_options++;
171 } 156 }
172 157
173 envp = xmalloc((num_options + 5) * sizeof(char *)); 158 envp = xcalloc(sizeof(char *), num_options + 5);
174 j = 0; 159 j = 0;
175 asprintf(&envp[j++], "interface=%s", client_config.interface); 160 asprintf(&envp[j++], "interface=%s", client_config.interface);
176 envp[j++] = find_env("PATH", "PATH=/bin:/usr/bin:/sbin:/usr/sbin"); 161 asprintf(&envp[j++], "%s=%s", "PATH",
177 envp[j++] = find_env("HOME", "HOME=/"); 162 getenv("PATH") ? : "/bin:/usr/bin:/sbin:/usr/sbin");
163 asprintf(&envp[j++], "%s=%s", "HOME", getenv("HOME") ? : "/");
178 164
179 if (packet == NULL) { 165 if (packet == NULL) return envp;
180 envp[j++] = NULL;
181 return envp;
182 }
183 166
184 asprintip(&envp[j++], "ip=", (unsigned char *) &packet->yiaddr); 167 envp[j] = xmalloc(sizeof("ip=255.255.255.255"));
168 sprintip(envp[j++], "ip=", (unsigned char *) &packet->yiaddr);
185 169
186 170
187 for (i = 0; dhcp_options[i].code; i++) { 171 for (i = 0; dhcp_options[i].code; i++) {
@@ -198,7 +182,8 @@ static char **fill_envp(struct dhcpMessage *packet)
198 } 182 }
199 } 183 }
200 if (packet->siaddr) { 184 if (packet->siaddr) {
201 asprintip(&envp[j++], "siaddr=", (unsigned char *) &packet->siaddr); 185 envp[j] = xmalloc(sizeof("siaddr=255.255.255.255"));
186 sprintip(envp[j++], "siaddr=", (unsigned char *) &packet->siaddr);
202 } 187 }
203 if (!(over & FILE_FIELD) && packet->file[0]) { 188 if (!(over & FILE_FIELD) && packet->file[0]) {
204 /* watch out for invalid packets */ 189 /* watch out for invalid packets */
@@ -210,7 +195,6 @@ static char **fill_envp(struct dhcpMessage *packet)
210 packet->sname[sizeof(packet->sname) - 1] = '\0'; 195 packet->sname[sizeof(packet->sname) - 1] = '\0';
211 asprintf(&envp[j++], "sname=%s", packet->sname); 196 asprintf(&envp[j++], "sname=%s", packet->sname);
212 } 197 }
213 envp[j] = NULL;
214 return envp; 198 return envp;
215} 199}
216 200