1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * dhcpcd - DHCP client daemon 4 * Copyright (c) 2006-2023 Roy Marples <roy@marples.name> 5 * All rights reserved 6 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/stat.h> 30 #include <sys/statvfs.h> 31 32 #include <ctype.h> 33 #include <errno.h> 34 #include <fcntl.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 39 #include "common.h" 40 #include "dhcpcd.h" 41 #include "if-options.h" 42 43 const char * 44 hwaddr_ntoa(const void *hwaddr, size_t hwlen, char *buf, size_t buflen) 45 { 46 const unsigned char *hp, *ep; 47 char *p; 48 49 /* Allow a hwlen of 0 to be an empty string. */ 50 if (buf == NULL || buflen == 0) { 51 errno = ENOBUFS; 52 return NULL; 53 } 54 55 if (hwlen * 3 > buflen) { 56 /* We should still terminate the string just in case. */ 57 buf[0] = '\0'; 58 errno = ENOBUFS; 59 return NULL; 60 } 61 62 hp = hwaddr; 63 ep = hp + hwlen; 64 p = buf; 65 while (hp < ep) { 66 if (hp != hwaddr) 67 *p ++= ':'; 68 p += snprintf(p, 3, "%.2x", *hp++); 69 } 70 *p ++= '\0'; 71 return buf; 72 } 73 74 size_t 75 hwaddr_aton(uint8_t *buffer, const char *addr) 76 { 77 char c[3]; 78 const char *p = addr; 79 uint8_t *bp = buffer; 80 size_t len = 0; 81 82 c[2] = '\0'; 83 while (*p != '\0') { 84 /* Skip separators */ 85 c[0] = *p++; 86 switch (c[0]) { 87 case '\n': /* long duid split on lines */ 88 case ':': /* typical mac address */ 89 case '-': /* uuid */ 90 continue; 91 } 92 c[1] = *p++; 93 /* Ensure that digits are hex */ 94 if (isxdigit((unsigned char)c[0]) == 0 || 95 isxdigit((unsigned char)c[1]) == 0) 96 { 97 errno = EINVAL; 98 return 0; 99 } 100 /* We should have at least two entries 00:01 */ 101 if (len == 0 && *p == '\0') { 102 errno = EINVAL; 103 return 0; 104 } 105 if (bp) 106 *bp++ = (uint8_t)strtol(c, NULL, 16); 107 len++; 108 } 109 return len; 110 } 111 112 ssize_t 113 readfile(const char *file, void *data, size_t len) 114 { 115 int fd; 116 ssize_t bytes; 117 118 fd = open(file, O_RDONLY); 119 if (fd == -1) 120 return -1; 121 bytes = read(fd, data, len); 122 close(fd); 123 if ((size_t)bytes == len) { 124 errno = ENOBUFS; 125 return -1; 126 } 127 return bytes; 128 } 129 130 ssize_t 131 writefile(const char *file, mode_t mode, const void *data, size_t len) 132 { 133 int fd; 134 ssize_t bytes; 135 136 fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, mode); 137 if (fd == -1) 138 return -1; 139 bytes = write(fd, data, len); 140 close(fd); 141 return bytes; 142 } 143 144 int 145 filemtime(const char *file, time_t *time) 146 { 147 struct stat st; 148 149 if (stat(file, &st) == -1) 150 return -1; 151 *time = st.st_mtime; 152 return 0; 153 } 154 155 /* Handy routine to read very long lines in text files. 156 * This means we read the whole line and avoid any nasty buffer overflows. 157 * We strip leading space and avoid comment lines, making the code that calls 158 * us smaller. */ 159 char * 160 get_line(char ** __restrict buf, ssize_t * __restrict buflen) 161 { 162 char *p, *c; 163 bool quoted; 164 165 do { 166 p = *buf; 167 if (*buf == NULL) 168 return NULL; 169 c = memchr(*buf, '\n', (size_t)*buflen); 170 if (c == NULL) { 171 c = memchr(*buf, '\0', (size_t)*buflen); 172 if (c == NULL) 173 return NULL; 174 *buflen = c - *buf; 175 *buf = NULL; 176 } else { 177 *c++ = '\0'; 178 *buflen -= c - *buf; 179 *buf = c; 180 } 181 for (; *p == ' ' || *p == '\t'; p++) 182 ; 183 } while (*p == '\0' || *p == '\n' || *p == '#' || *p == ';'); 184 185 /* Strip embedded comments unless in a quoted string or escaped */ 186 quoted = false; 187 for (c = p; *c != '\0'; c++) { 188 if (*c == '\\') { 189 c++; /* escaped */ 190 continue; 191 } 192 if (*c == '"') 193 quoted = !quoted; 194 else if (*c == '#' && !quoted) { 195 *c = '\0'; 196 break; 197 } 198 } 199 return p; 200 } 201 202 203 int 204 is_root_local(void) 205 { 206 #ifdef ST_LOCAL 207 struct statvfs vfs; 208 209 if (statvfs("/", &vfs) == -1) 210 return -1; 211 return vfs.f_flag & ST_LOCAL ? 1 : 0; 212 #else 213 errno = ENOTSUP; 214 return -1; 215 #endif 216 } 217