Revert commit #17603, it should have been part of #17617.
[openssl.git] / crypto / rand / rand_egd.c
index c4b8bae2ed5558f8c0d62f8a07ad3dd14be39268..d53b916ebee87c7ae77700c2fb5464920ae12cc1 100644 (file)
@@ -54,7 +54,9 @@
  *
  */
 
+#include <openssl/e_os2.h>
 #include <openssl/rand.h>
+#include <openssl/buffer.h>
 
 /*
  * Query the EGD <URL: http://www.lothar.com/tech/crypto/>.
@@ -93,7 +95,7 @@
  *   RAND_egd() is a wrapper for RAND_egd_bytes() with numbytes=255.
  */
 
-#if defined(OPENSSL_SYS_WIN32) || defined(VMS) || defined(__VMS)
+#if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_VOS) || defined(OPENSSL_SYS_BEOS)
 int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
        {
        return(-1);
@@ -112,7 +114,18 @@ int RAND_egd_bytes(const char *path,int bytes)
 #include OPENSSL_UNISTD
 #include <sys/types.h>
 #include <sys/socket.h>
-#include <sys/un.h>
+#ifndef NO_SYS_UN_H
+# ifdef OPENSSL_SYS_VXWORKS
+#   include <streams/un.h>
+# else
+#   include <sys/un.h>
+# endif
+#else
+struct sockaddr_un {
+       short   sun_family;             /* AF_UNIX */
+       char    sun_path[108];          /* path name (gag) */
+};
+#endif /* NO_SYS_UN_H */
 #include <string.h>
 #include <errno.h>
 
@@ -131,9 +144,9 @@ int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
 
        memset(&addr, 0, sizeof(addr));
        addr.sun_family = AF_UNIX;
-       if (strlen(path) > sizeof(addr.sun_path))
+       if (strlen(path) >= sizeof(addr.sun_path))
                return (-1);
-       strcpy(addr.sun_path,path);
+       BUF_strlcpy(addr.sun_path,path,sizeof addr.sun_path);
        len = offsetof(struct sockaddr_un, sun_path) + strlen(path);
        fd = socket(AF_UNIX, SOCK_STREAM, 0);
        if (fd == -1) return (-1);
@@ -203,7 +216,9 @@ int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
            while (numbytes != 1)
                {
                num = read(fd, egdbuf, 1);
-               if (num >= 0)
+               if (num == 0)
+                       goto err;       /* descriptor closed */
+               else if (num > 0)
                    numbytes += num;
                else
                    {
@@ -233,7 +248,9 @@ int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
            while (numbytes != egdbuf[0])
                {
                num = read(fd, retrievebuf + numbytes, egdbuf[0] - numbytes);
-               if (num >= 0)
+               if (num == 0)
+                       goto err;       /* descriptor closed */
+               else if (num > 0)
                    numbytes += num;
                else
                    {