Add basic pledge/unveil sandbox on OpenBSD
[tinc] / src / utils.c
index f14094f..231938d 100644 (file)
@@ -18,6 +18,8 @@
     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
+#include <assert.h>
+
 #include "logger.h"
 #include "system.h"
 #include "utils.h"
@@ -45,11 +47,13 @@ static const char base64_decode[256] = {
                -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
         };
 
-static int charhex2bin(char c) {
-       if(isdigit(c)) {
-               return c - '0';
+static uint8_t charhex2bin(char c) {
+       uint8_t cu = (uint8_t) c;
+
+       if(isdigit(cu)) {
+               return cu - '0';
        } else {
-               return toupper(c) - 'A' + 10;
+               return toupper(cu) - 'A' + 10;
        }
 }
 
@@ -57,7 +61,7 @@ size_t hex2bin(const char *src, void *vdst, size_t length) {
        uint8_t *dst = vdst;
        size_t i;
 
-       for(i = 0; i < length && isxdigit(src[i * 2]) && isxdigit(src[i * 2 + 1]); i++) {
+       for(i = 0; i < length && isxdigit((uint8_t) src[i * 2]) && isxdigit((uint8_t) src[i * 2 + 1]); i++) {
                dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
        }
 
@@ -77,7 +81,58 @@ size_t bin2hex(const void *vsrc, char *dst, size_t length) {
        return length * 2;
 }
 
-size_t b64decode(const char *src, void *dst, size_t length) {
+char *absolute_path(const char *path) {
+#ifdef HAVE_WINDOWS
+       // Works for nonexistent paths
+       return _fullpath(NULL, path, 0);
+#else
+
+       if(!path || !*path) {
+               return NULL;
+       }
+
+       // If an absolute path was passed, return its copy
+       if(*path == '/') {
+               return xstrdup(path);
+       }
+
+       // Try using realpath. If it fails for any reason
+       // other than that the file was not found, bail out.
+       char *abs = realpath(path, NULL);
+
+       if(abs || errno != ENOENT) {
+               return abs;
+       }
+
+       // Since the file does not exist, we're forced to use a fallback.
+       // Get current working directory and concatenate it with the argument.
+       char cwd[PATH_MAX];
+
+       if(!getcwd(cwd, sizeof(cwd))) {
+               return NULL;
+       }
+
+       // Remove trailing slash if present since we'll be adding our own
+       size_t cwdlen = strlen(cwd);
+
+       if(cwdlen && cwd[cwdlen - 1] == '/') {
+               cwd[cwdlen - 1] = '\0';
+       }
+
+       // We don't do any normalization because it's complicated, and the payoff is small.
+       // If user passed something like '.././../foo' — that's their choice; fopen works either way.
+       xasprintf(&abs, "%s/%s", cwd, path);
+
+       if(strlen(abs) >= PATH_MAX) {
+               free(abs);
+               abs = NULL;
+       }
+
+       return abs;
+#endif
+}
+
+size_t b64decode_tinc(const char *src, void *dst, size_t length) {
        size_t i;
        uint32_t triplet = 0;
        unsigned char *udst = (unsigned char *)dst;
@@ -117,7 +172,25 @@ size_t b64decode(const char *src, void *dst, size_t length) {
        }
 }
 
-static size_t b64encode_internal(const void *src, char *dst, size_t length, const char *alphabet) {
+bool is_decimal(const char *str) {
+       if(!str) {
+               return false;
+       }
+
+       errno = 0;
+       char *badchar = NULL;
+       strtol(str, &badchar, 10);
+       return !errno && badchar != str && !*badchar;
+}
+
+// itoa() conflicts with a similarly named function under MinGW.
+char *int_to_str(int num) {
+       char *str = NULL;
+       xasprintf(&str, "%d", num);
+       return str;
+}
+
+static size_t b64encode_tinc_internal(const void *src, char *dst, size_t length, const char *alphabet) {
        uint32_t triplet;
        const unsigned char *usrc = (unsigned char *)src;
        size_t si = length / 3 * 3;
@@ -166,15 +239,15 @@ static size_t b64encode_internal(const void *src, char *dst, size_t length, cons
        return length;
 }
 
-size_t b64encode(const void *src, char *dst, size_t length) {
-       return b64encode_internal(src, dst, length, base64_original);
+size_t b64encode_tinc(const void *src, char *dst, size_t length) {
+       return b64encode_tinc_internal(src, dst, length, base64_original);
 }
 
-size_t b64encode_urlsafe(const void *src, char *dst, size_t length) {
-       return b64encode_internal(src, dst, length, base64_urlsafe);
+size_t b64encode_tinc_urlsafe(const void *src, char *dst, size_t length) {
+       return b64encode_tinc_internal(src, dst, length, base64_urlsafe);
 }
 
-#ifdef HAVE_MINGW
+#ifdef HAVE_WINDOWS
 const char *winerror(int err) {
        static char buf[1024], *ptr;
 
@@ -193,24 +266,13 @@ const char *winerror(int err) {
 }
 #endif
 
-unsigned int bitfield_to_int(const void *bitfield, size_t size) {
-       unsigned int value = 0;
-
-       if(size > sizeof(value)) {
-               size = sizeof(value);
-       }
-
-       memcpy(&value, bitfield, size);
-       return value;
-}
-
 bool check_id(const char *id) {
        if(!id || !*id) {
                return false;
        }
 
        for(; *id; id++)
-               if(!isalnum(*id) && *id != '_') {
+               if(!isalnum((uint8_t) *id) && *id != '_') {
                        return false;
                }
 
@@ -223,7 +285,7 @@ bool check_netname(const char *netname, bool strict) {
        }
 
        for(const char *c = netname; *c; c++) {
-               if(iscntrl(*c)) {
+               if(iscntrl((uint8_t) *c)) {
                        return false;
                }
 
@@ -269,7 +331,7 @@ char *replace_name(const char *name) {
                ret_name = xstrdup(envname);
 
                for(char *c = ret_name; *c; c++)
-                       if(!isalnum(*c)) {
+                       if(!isalnum((uint8_t) *c)) {
                                *c = '_';
                        }
        } else {
@@ -284,3 +346,34 @@ char *replace_name(const char *name) {
 
        return ret_name;
 }
+
+/* Open a file with the desired permissions, minus the umask.
+   Also, if we want to create an executable file, we call fchmod()
+   to set the executable bits. */
+
+FILE *fopenmask(const char *filename, const char *mode, mode_t perms) {
+       mode_t mask = umask(0);
+       perms &= ~mask;
+       umask(~perms & 0777);
+       FILE *f = fopen(filename, mode);
+
+       if(!f) {
+               fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
+               return NULL;
+       }
+
+#ifdef HAVE_FCHMOD
+
+       if(perms & 0444) {
+               fchmod(fileno(f), perms);
+       }
+
+#endif
+       umask(mask);
+       return f;
+}
+
+bool string_eq(const char *first, const char *second) {
+       return !first == !second &&
+              !(first && second && strcmp(first, second));
+}