Check method before access and release ctx in error paths
[openssl.git] / apps / rehash.c
index 8417449235484631dbd409d1044764d045c03e6f..38084a247fb854601144a38569a33193afb923a2 100644 (file)
@@ -59,7 +59,7 @@
 
 #include "apps.h"
 
-#if defined(unix) || defined(__APPLE__)
+#if defined(OPENSSL_SYS_UNIX) || defined(__APPLE__)
 # include <unistd.h>
 # include <stdio.h>
 # include <limits.h>
@@ -74,6 +74,9 @@
 # include <openssl/x509.h>
 
 
+# ifndef NAME_MAX
+#  define NAME_MAX 255
+# endif
 # define MAX_COLLISIONS  256
 
 typedef struct hentry_st {
@@ -172,7 +175,7 @@ static int add_entry(enum Type type, unsigned int hash, const char *filename,
         ep = app_malloc(sizeof(*ep), "collision bucket");
         *ep = nilhentry;
         ep->old_id = ~0;
-        ep->filename = BUF_strdup(filename);
+        ep->filename = OPENSSL_strdup(filename);
         if (bp->last_entry)
             bp->last_entry->next = ep;
         if (bp->first_entry == NULL)
@@ -199,7 +202,7 @@ static int handle_symlink(const char *filename, const char *fullpath)
     unsigned int hash = 0;
     int i, type, id;
     unsigned char ch;
-    char linktarget[NAME_MAX], *endptr;
+    char linktarget[PATH_MAX], *endptr;
     ssize_t n;
 
     for (i = 0; i < 8; i++) {
@@ -313,9 +316,13 @@ static int do_dir(const char *dirname, enum Hash h)
     const char *filename;
     char *buf;
 
+    if (app_access(dirname, W_OK) < 0) {
+        BIO_printf(bio_err, "Skipping %s, can't write\n", dirname);
+        return 1;
+    }
     buflen = strlen(dirname);
     pathsep = (buflen && dirname[buflen - 1] == '/') ? "" : "/";
-    buflen += NAME_MAX + 2;
+    buflen += NAME_MAX + 1 + 1;
     buf = app_malloc(buflen, "filename buffer");
 
     if (verbose)
@@ -461,7 +468,7 @@ int rehash_main(int argc, char **argv)
         while (*argv)
             errs += do_dir(*argv++, h);
     } else if ((env = getenv("SSL_CERT_DIR")) != NULL) {
-        m = BUF_strdup(env);
+        m = OPENSSL_strdup(env);
         for (e = strtok(m, ":"); e != NULL; e = strtok(NULL, ":"))
             errs += do_dir(e, h);
         OPENSSL_free(m);
@@ -484,4 +491,4 @@ int rehash_main(int argc, char **argv)
     return (1);
 }
 
-#endif /* defined(unix) || defined(__APPLE__) */
+#endif /* defined(OPENSSL_SYS_UNIX) || defined(__APPLE__) */