Convert to using strnkc(at|py).

This commit is contained in:
Nicholas J. Kain 2013-05-06 07:07:54 -04:00
parent 7ab11e8295
commit 958a57d819
7 changed files with 37 additions and 41 deletions

10
README
View File

@ -214,16 +214,6 @@ most modern unix systems, but it is not standard.
7) Numerous socket options are used, and the AF_PACKET socket family is used
for raw sockets and ARP. These are largely Linux-specific, too.
8) ndhc uses strlcpy() and strlcat(). Native versions are provided.
Some standard C libraries include a native implementation of strlcpy() and
strlcat(). Such defines may conflict with my implementations in strl.c/strl.h.
It is up to the user whether the standard C library implementations should be
used. Note that some machines implement strlcpy() and strlcat() with
nonstandard semantics (notably Solaris). On these systems, using the
system-provided implementations may lead to security problems. Such problems
are the fault of the vendor. If you are unsure whether your system is correct
or not, I suggest using the implementation that I provide.
HISTORY
-------

View File

@ -198,7 +198,7 @@ static void write_resolve_conf(struct ifchd_client *cl)
q = strchr(p, '\0');
else
*q++ = '\0';
strlcpy(buf, p, sizeof buf);
strnkcpy(buf, p, sizeof buf);
writeordie(resolv_conf_fd, ns_str, strlen(ns_str));
writeordie(resolv_conf_fd, buf, strlen(buf));
@ -215,7 +215,7 @@ static void write_resolve_conf(struct ifchd_client *cl)
q = strchr(p, '\0');
else
*q++ = '\0';
strlcpy(buf, p, sizeof buf);
strnkcpy(buf, p, sizeof buf);
if (numdoms == 0) {
writeordie(resolv_conf_fd, dom_str, strlen(dom_str));
@ -270,7 +270,7 @@ static void perform_dns(struct ifchd_client *cl, char *str)
{
if (!str || resolv_conf_fd == -1)
return;
strlcpy(cl->namesvrs, str, MAX_BUF);
strnkcpy(cl->namesvrs, str, MAX_BUF);
write_resolve_conf(cl);
}
@ -292,7 +292,7 @@ static void perform_domain(struct ifchd_client *cl, char *str)
{
if (!str || resolv_conf_fd == -1)
return;
strlcpy(cl->domains, str, MAX_BUF);
strnkcpy(cl->domains, str, MAX_BUF);
write_resolve_conf(cl);
}
@ -387,8 +387,13 @@ static int execute_buffer(struct ifchd_client *cl, char *newbuf)
char *p = buf, *endp;
memset(buf, 0, sizeof buf);
strlcat(buf, cl->ibuf, sizeof buf);
strlcat(buf, newbuf, sizeof buf);
if (strnkcat(buf, cl->ibuf, sizeof buf))
goto buftooshort;
if (strnkcat(buf, newbuf, sizeof buf)) {
buftooshort:
log_line("error: input is too long for buffer");
return -1;
}
for (endp = p;;p = endp) {
if (cl->state == STATE_NOTHING) {
@ -528,7 +533,7 @@ static int execute_buffer(struct ifchd_client *cl, char *newbuf)
size_t remsize = strlen(endp);
if (remsize > MAX_BUF - 1)
return -1;
strlcpy(cl->ibuf, endp, MAX_BUF);
strnkcpy(cl->ibuf, endp, MAX_BUF);
return 0;
}
@ -842,15 +847,15 @@ int main(int argc, char** argv) {
break;
case 'c':
strlcpy(chrootd, optarg, MAX_PATH_LENGTH);
strnkcpy(chrootd, optarg, MAX_PATH_LENGTH);
break;
case 'p':
strlcpy(pidfile, optarg, MAX_PATH_LENGTH);
strnkcpy(pidfile, optarg, MAX_PATH_LENGTH);
break;
case 'r':
strlcpy(resolv_conf_d, optarg, MAX_PATH_LENGTH);
strnkcpy(resolv_conf_d, optarg, MAX_PATH_LENGTH);
break;
case 'o':

View File

@ -60,7 +60,7 @@ void add_permitted_if(char *s)
{
if (numokif >= MAX_IFACES)
return;
strlcpy(okif[numokif++], s, IFNAMSIZ);
strnkcpy(okif[numokif++], s, IFNAMSIZ);
}
/* Checks if changes are permitted to a given interface. 1 == allowed */
@ -110,7 +110,7 @@ void perform_interface(struct ifchd_client *cl, char *str)
/* Update interface name. */
memset(cl->ifnam, '\0', IFNAMSIZ);
strlcpy(cl->ifnam, str, IFNAMSIZ);
strnkcpy(cl->ifnam, str, IFNAMSIZ);
}
static int set_if_flag(struct ifchd_client *cl, short flag)
@ -128,13 +128,13 @@ static int set_if_flag(struct ifchd_client *cl, short flag)
goto out0;
}
strlcpy(ifrt.ifr_name, cl->ifnam, IFNAMSIZ);
strnkcpy(ifrt.ifr_name, cl->ifnam, IFNAMSIZ);
if (ioctl(fd, SIOCGIFFLAGS, &ifrt) < 0) {
log_line("%s: unknown interface: %s\n", cl->ifnam, strerror(errno));
goto out1;
}
if (((ifrt.ifr_flags & flag ) ^ flag) & flag) {
strlcpy(ifrt.ifr_name, cl->ifnam, IFNAMSIZ);
strnkcpy(ifrt.ifr_name, cl->ifnam, IFNAMSIZ);
ifrt.ifr_flags |= flag;
if (ioctl(fd, SIOCSIFFLAGS, &ifrt) < 0) {
log_line("%s: failed to set interface flags: %s\n",
@ -167,7 +167,7 @@ void perform_ip(struct ifchd_client *cl, char *str)
if (set_if_flag(cl, (IFF_UP | IFF_RUNNING)))
return;
strlcpy(ifrt.ifr_name, cl->ifnam, IFNAMSIZ);
strnkcpy(ifrt.ifr_name, cl->ifnam, IFNAMSIZ);
memset(&sin, 0, sizeof(struct sockaddr));
sin.sin_family = AF_INET;
sin.sin_addr = ipaddr;
@ -200,7 +200,7 @@ void perform_subnet(struct ifchd_client *cl, char *str)
if (!inet_pton(AF_INET, str, &subnet))
return;
strlcpy(ifrt.ifr_name, cl->ifnam, IFNAMSIZ);
strnkcpy(ifrt.ifr_name, cl->ifnam, IFNAMSIZ);
memset(&sin, 0, sizeof(struct sockaddr));
sin.sin_family = AF_INET;
sin.sin_addr = subnet;
@ -283,7 +283,7 @@ void perform_mtu(struct ifchd_client *cl, char *str)
if (mtu < 576)
return;
ifrt.ifr_mtu = mtu;
strlcpy(ifrt.ifr_name, cl->ifnam, IFNAMSIZ);
strnkcpy(ifrt.ifr_name, cl->ifnam, IFNAMSIZ);
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1) {
@ -311,7 +311,7 @@ void perform_broadcast(struct ifchd_client *cl, char *str)
if (!inet_pton(AF_INET, str, &broadcast))
return;
strlcpy(ifrt.ifr_name, cl->ifnam, IFNAMSIZ);
strnkcpy(ifrt.ifr_name, cl->ifnam, IFNAMSIZ);
memset(&sin, 0, sizeof(struct sockaddr));
sin.sin_family = AF_INET;
sin.sin_addr = broadcast;

View File

@ -81,7 +81,7 @@ static int create_udp_socket(uint32_t ip, uint16_t port, char *iface)
}
struct ifreq ifr;
memset(&ifr, 0, sizeof (struct ifreq));
strlcpy(ifr.ifr_name, iface, IFNAMSIZ);
strnkcpy(ifr.ifr_name, iface, IFNAMSIZ);
if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof ifr) < 0) {
log_error("create_udp_socket: Set bind to device failed: %s",
strerror(errno));

View File

@ -214,7 +214,7 @@ static size_t send_client_ip(char *out, size_t olen, struct dhcpmsg *packet)
return 0;
inet_ntop(AF_INET, &packet->yiaddr, ip, sizeof ip);
snprintf(ipb, sizeof ipb, "ip:%s;", ip);
strlcat(out, ipb, olen);
strnkcat(out, ipb, olen);
log_line("Sent to ifchd: %s", out);
return strlen(ipb);
}
@ -238,7 +238,7 @@ static size_t send_cmd(char *out, size_t olen, struct dhcpmsg *packet,
return 0;
if (ifchd_cmd(buf, sizeof buf, optdata, optlen, code) == -1)
return 0;
strlcat(out, buf, olen);
strnkcat(out, buf, olen);
log_line("Sent to ifchd: %s", buf);
return strlen(buf);
}

View File

@ -46,7 +46,7 @@ static int leasefilefd = -1;
void set_leasefile(char *lf)
{
strlcpy(leasefile, lf, sizeof leasefile);
strnkcpy(leasefile, lf, sizeof leasefile);
}
void open_leasefile()
@ -67,7 +67,7 @@ void write_leasefile(struct in_addr ipnum)
if (leasefilefd < 0)
return;
inet_ntop(AF_INET, &ipnum, ip, sizeof ip);
strlcat(ip, "\n", sizeof ip);
strnkcat(ip, "\n", sizeof ip);
retry_trunc:
ret = ftruncate(leasefilefd, 0);
switch (ret) {
@ -85,3 +85,4 @@ void write_leasefile(struct in_addr ipnum)
else
fsync(leasefilefd);
}

View File

@ -346,8 +346,8 @@ int main(int argc, char **argv)
switch (c) {
case 'c':
if (!get_clientid_mac_string(optarg, strlen(optarg)))
strlcpy(client_config.clientid, optarg,
sizeof client_config.clientid);
strnkcpy(client_config.clientid, optarg,
sizeof client_config.clientid);
else
client_config.clientid_mac = 1;
break;
@ -360,14 +360,14 @@ int main(int argc, char **argv)
gflags_detach = 1;
break;
case 'p':
strlcpy(pidfile, optarg, sizeof pidfile);
strnkcpy(pidfile, optarg, sizeof pidfile);
break;
case 'l':
set_leasefile(optarg);
break;
case 'h':
strlcpy(client_config.hostname, optarg,
sizeof client_config.hostname);
strnkcpy(client_config.hostname, optarg,
sizeof client_config.hostname);
break;
case 'i':
client_config.interface = optarg;
@ -392,7 +392,7 @@ int main(int argc, char **argv)
}
break;
case 'C':
strlcpy(chroot_dir, optarg, sizeof chroot_dir);
strnkcpy(chroot_dir, optarg, sizeof chroot_dir);
break;
case 'd':
arp_relentless_def = 1;
@ -447,8 +447,8 @@ int main(int argc, char **argv)
exit(EXIT_SUCCESS);
break;
case 'V':
strlcpy(client_config.vendor, optarg,
sizeof client_config.vendor);
strnkcpy(client_config.vendor, optarg,
sizeof client_config.vendor);
break;
default:
show_usage();