aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-07-07 14:59:30 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2009-07-07 14:59:30 +0200
commit95cc814dbd37a4cb5a69b5eac80bd3e5173fe908 (patch)
treee5adfbc603dd9b70371a77c5f1a5c19ba937f4ae
parenta51543a3a486ca60018394dda2623fdf1f16a965 (diff)
downloadbusybox-w32-95cc814dbd37a4cb5a69b5eac80bd3e5173fe908.tar.gz
busybox-w32-95cc814dbd37a4cb5a69b5eac80bd3e5173fe908.tar.bz2
busybox-w32-95cc814dbd37a4cb5a69b5eac80bd3e5173fe908.zip
udhcpd: fix a bug in add_lease where it was reading at [-1]
It is not correct when we read lease file! Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--networking/udhcp/dhcpd.h5
-rw-r--r--networking/udhcp/files.c6
-rw-r--r--networking/udhcp/leases.c23
-rw-r--r--networking/udhcp/serverpacket.c22
4 files changed, 35 insertions, 21 deletions
diff --git a/networking/udhcp/dhcpd.h b/networking/udhcp/dhcpd.h
index 67cb78ce7..4f6b73e34 100644
--- a/networking/udhcp/dhcpd.h
+++ b/networking/udhcp/dhcpd.h
@@ -89,7 +89,7 @@ struct dyn_lease {
89 * (dhcp packet has chaddr[16], not [6]) 89 * (dhcp packet has chaddr[16], not [6])
90 */ 90 */
91 uint8_t lease_mac[6]; 91 uint8_t lease_mac[6];
92 uint8_t hostname[20]; 92 char hostname[20];
93 uint8_t pad[2]; 93 uint8_t pad[2];
94 /* total size is a multiply of 4 */ 94 /* total size is a multiply of 4 */
95} PACKED; 95} PACKED;
@@ -98,7 +98,8 @@ extern struct dyn_lease *g_leases;
98 98
99struct dyn_lease *add_lease( 99struct dyn_lease *add_lease(
100 const uint8_t *chaddr, uint32_t yiaddr, 100 const uint8_t *chaddr, uint32_t yiaddr,
101 leasetime_t leasetime, uint8_t *hostname 101 leasetime_t leasetime,
102 const char *hostname, int hostname_len
102 ) FAST_FUNC; 103 ) FAST_FUNC;
103int is_expired_lease(struct dyn_lease *lease) FAST_FUNC; 104int is_expired_lease(struct dyn_lease *lease) FAST_FUNC;
104struct dyn_lease *find_lease_by_mac(const uint8_t *mac) FAST_FUNC; 105struct dyn_lease *find_lease_by_mac(const uint8_t *mac) FAST_FUNC;
diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c
index 35592153b..1b2cc96f4 100644
--- a/networking/udhcp/files.c
+++ b/networking/udhcp/files.c
@@ -420,7 +420,11 @@ void FAST_FUNC read_leases(const char *file)
420 continue; 420 continue;
421 /* NB: add_lease takes "relative time", IOW, 421 /* NB: add_lease takes "relative time", IOW,
422 * lease duration, not lease deadline. */ 422 * lease duration, not lease deadline. */
423 if (!(add_lease(lease.lease_mac, lease.lease_nip, expires, lease.hostname))) { 423 if (add_lease(lease.lease_mac, lease.lease_nip,
424 expires,
425 lease.hostname, sizeof(lease.hostname)
426 ) == 0
427 ) {
424 bb_error_msg("too many leases while loading %s", file); 428 bb_error_msg("too many leases while loading %s", file);
425 break; 429 break;
426 } 430 }
diff --git a/networking/udhcp/leases.c b/networking/udhcp/leases.c
index 68fba726e..afd41bfd4 100644
--- a/networking/udhcp/leases.c
+++ b/networking/udhcp/leases.c
@@ -50,10 +50,10 @@ static void clear_lease(const uint8_t *chaddr, uint32_t yiaddr)
50/* Add a lease into the table, clearing out any old ones */ 50/* Add a lease into the table, clearing out any old ones */
51struct dyn_lease* FAST_FUNC add_lease( 51struct dyn_lease* FAST_FUNC add_lease(
52 const uint8_t *chaddr, uint32_t yiaddr, 52 const uint8_t *chaddr, uint32_t yiaddr,
53 leasetime_t leasetime, uint8_t *hostname) 53 leasetime_t leasetime,
54 const char *hostname, int hostname_len)
54{ 55{
55 struct dyn_lease *oldest; 56 struct dyn_lease *oldest;
56 uint8_t hostname_length;
57 57
58 /* clean out any old ones */ 58 /* clean out any old ones */
59 clear_lease(chaddr, yiaddr); 59 clear_lease(chaddr, yiaddr);
@@ -63,16 +63,15 @@ struct dyn_lease* FAST_FUNC add_lease(
63 if (oldest) { 63 if (oldest) {
64 oldest->hostname[0] = '\0'; 64 oldest->hostname[0] = '\0';
65 if (hostname) { 65 if (hostname) {
66 /* option size byte, + 1 for NUL */ 66 char *p;
67 hostname_length = hostname[-1] + 1; 67 if (hostname_len > sizeof(oldest->hostname))
68 if (hostname_length > sizeof(oldest->hostname)) 68 hostname_len = sizeof(oldest->hostname);
69 hostname_length = sizeof(oldest->hostname); 69 p = safe_strncpy(oldest->hostname, hostname, hostname_len);
70 hostname = (uint8_t*) safe_strncpy((char*)oldest->hostname, (char*)hostname, hostname_length);
71 /* sanitization (s/non-ASCII/^/g) */ 70 /* sanitization (s/non-ASCII/^/g) */
72 while (*hostname) { 71 while (*p) {
73 if (*hostname < ' ' || *hostname > 126) 72 if (*p < ' ' || *p > 126)
74 *hostname = '^'; 73 *p = '^';
75 hostname++; 74 p++;
76 } 75 }
77 } 76 }
78 memcpy(oldest->lease_mac, chaddr, 6); 77 memcpy(oldest->lease_mac, chaddr, 6);
@@ -137,7 +136,7 @@ static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac)
137 temp.s_addr = nip; 136 temp.s_addr = nip;
138 bb_info_msg("%s belongs to someone, reserving it for %u seconds", 137 bb_info_msg("%s belongs to someone, reserving it for %u seconds",
139 inet_ntoa(temp), (unsigned)server_config.conflict_time); 138 inet_ntoa(temp), (unsigned)server_config.conflict_time);
140 add_lease(blank_chaddr, nip, server_config.conflict_time, NULL); 139 add_lease(blank_chaddr, nip, server_config.conflict_time, NULL, 0);
141 return 0; 140 return 0;
142} 141}
143 142
diff --git a/networking/udhcp/serverpacket.c b/networking/udhcp/serverpacket.c
index c3724e0e2..b48e4159a 100644
--- a/networking/udhcp/serverpacket.c
+++ b/networking/udhcp/serverpacket.c
@@ -130,7 +130,8 @@ int FAST_FUNC send_offer(struct dhcp_packet *oldpacket)
130 uint32_t req_nip; 130 uint32_t req_nip;
131 uint32_t lease_time_sec = server_config.max_lease_sec; 131 uint32_t lease_time_sec = server_config.max_lease_sec;
132 uint32_t static_lease_ip; 132 uint32_t static_lease_ip;
133 uint8_t *req_ip_opt, *p_host_name; 133 uint8_t *req_ip_opt;
134 const char *p_host_name;
134 struct option_set *curr; 135 struct option_set *curr;
135 struct in_addr addr; 136 struct in_addr addr;
136 137
@@ -173,8 +174,13 @@ int FAST_FUNC send_offer(struct dhcp_packet *oldpacket)
173 bb_error_msg("no IP addresses to give - OFFER abandoned"); 174 bb_error_msg("no IP addresses to give - OFFER abandoned");
174 return -1; 175 return -1;
175 } 176 }
176 p_host_name = get_option(oldpacket, DHCP_HOST_NAME); 177 p_host_name = (const char*) get_option(oldpacket, DHCP_HOST_NAME);
177 if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time, p_host_name)) { 178 if (add_lease(packet.chaddr, packet.yiaddr,
179 server_config.offer_time,
180 p_host_name,
181 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
182 ) == 0
183 ) {
178 bb_error_msg("lease pool is full - OFFER abandoned"); 184 bb_error_msg("lease pool is full - OFFER abandoned");
179 return -1; 185 return -1;
180 } 186 }
@@ -218,7 +224,7 @@ int FAST_FUNC send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
218 struct option_set *curr; 224 struct option_set *curr;
219 uint32_t lease_time_sec; 225 uint32_t lease_time_sec;
220 struct in_addr addr; 226 struct in_addr addr;
221 uint8_t *p_host_name; 227 const char *p_host_name;
222 228
223 init_packet(&packet, oldpacket, DHCPACK); 229 init_packet(&packet, oldpacket, DHCPACK);
224 packet.yiaddr = yiaddr; 230 packet.yiaddr = yiaddr;
@@ -242,8 +248,12 @@ int FAST_FUNC send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
242 if (send_packet(&packet, 0) < 0) 248 if (send_packet(&packet, 0) < 0)
243 return -1; 249 return -1;
244 250
245 p_host_name = get_option(oldpacket, DHCP_HOST_NAME); 251 p_host_name = (const char*) get_option(oldpacket, DHCP_HOST_NAME);
246 add_lease(packet.chaddr, packet.yiaddr, lease_time_sec, p_host_name); 252 add_lease(packet.chaddr, packet.yiaddr,
253 lease_time_sec,
254 p_host_name,
255 p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
256 );
247 if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) { 257 if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
248 /* rewrite the file with leases at every new acceptance */ 258 /* rewrite the file with leases at every new acceptance */
249 write_leases(); 259 write_leases();