From 3ffe279bf7c9ba11922d1995b415e904656a42f3 Mon Sep 17 00:00:00 2001 From: manojampalam Date: Tue, 22 Mar 2016 20:31:03 -0700 Subject: [PATCH] pwd and statvfs functions moved out from openbsd --- contrib/win32/openssh/config.h.vs | 11 +- contrib/win32/openssh/openbsd_compat.vcxproj | 2 - .../openssh/openbsd_compat.vcxproj.filters | 6 - contrib/win32/openssh/win32compat.vcxproj | 19 +++ .../win32/openssh/win32compat.vcxproj.filters | 71 ++++++++++- contrib/win32/openssh/win32iocompat.vcxproj | 1 + .../openssh/win32iocompat.vcxproj.filters | 3 + contrib/win32/win32compat/inc/sys/statvfs.h | 26 +++++ contrib/win32/win32compat/includes/pwd.h | 4 + contrib/win32/win32compat/misc.c | 45 +++++++ contrib/win32/win32compat/pwd.c | 110 ++++++++++++++++++ openbsd-compat/bsd-statvfs.c | 46 -------- openbsd-compat/pwcache.c | 2 - openbsd-compat/realpath.c | 83 +------------ 14 files changed, 284 insertions(+), 145 deletions(-) create mode 100644 contrib/win32/win32compat/inc/sys/statvfs.h diff --git a/contrib/win32/openssh/config.h.vs b/contrib/win32/openssh/config.h.vs index 9711c5b..aa90d9b 100644 --- a/contrib/win32/openssh/config.h.vs +++ b/contrib/win32/openssh/config.h.vs @@ -390,7 +390,7 @@ /* #undef HAVE_FSFILCNT_T */ /* Define to 1 if you have the `fstatvfs' function. */ -/* #undef HAVE_FSTATVFS */ +#define HAVE_FSTATVFS 1 /* Define to 1 if you have the `futimes' function. */ /* #undef HAVE_FUTIMES */ @@ -770,7 +770,7 @@ /* #undef HAVE_READPASSPHRASE_H */ /* Define to 1 if you have the `realpath' function. */ -/* #undef HAVE_REALPATH */ +#define HAVE_REALPATH 1 /* Define to 1 if you have the `recvmsg' function. */ /* #undef HAVE_RECVMSG */ @@ -915,7 +915,7 @@ /* #undef HAVE_STATFS */ /* Define to 1 if you have the `statvfs' function. */ -/* #undef HAVE_STATVFS */ +#define HAVE_STATVFS 1 /* Define to 1 if you have the header file. */ #define HAVE_STDDEF_H 1 @@ -1050,7 +1050,7 @@ #define HAVE_SYS_SELECT_H 1 /* Define to 1 if you have the header file. */ -/* #undef HAVE_SYS_STATVFS_H */ +#define HAVE_SYS_STATVFS_H 1 /* Define to 1 if you have the header file. */ #define HAVE_SYS_STAT_H 1 @@ -1152,7 +1152,7 @@ /* #undef HAVE_USERSEC_H */ /* Define to 1 if you have the `user_from_uid' function. */ -/* #undef HAVE_USER_FROM_UID */ +#define HAVE_USER_FROM_UID 1 /* Define to 1 if you have the header file. */ /* #undef HAVE_UTIL_H */ @@ -1619,7 +1619,6 @@ #undef HAVE_PTY_H #define HAVE_NANOSLEEP 1 #define HAVE_READPASSPHRASE 1 -#define HAVE_REALPATH 1 #undef HAVE_SIG_ATOMIC_T #define HAVE_SIZE_T 1 #undef HAVE_STRERROR diff --git a/contrib/win32/openssh/openbsd_compat.vcxproj b/contrib/win32/openssh/openbsd_compat.vcxproj index bd56a2b..c828a9f 100644 --- a/contrib/win32/openssh/openbsd_compat.vcxproj +++ b/contrib/win32/openssh/openbsd_compat.vcxproj @@ -62,10 +62,8 @@ - - diff --git a/contrib/win32/openssh/openbsd_compat.vcxproj.filters b/contrib/win32/openssh/openbsd_compat.vcxproj.filters index 40d4db7..da78775 100644 --- a/contrib/win32/openssh/openbsd_compat.vcxproj.filters +++ b/contrib/win32/openssh/openbsd_compat.vcxproj.filters @@ -141,18 +141,12 @@ Source Files - - Source Files - Source Files Source Files - - Source Files - Source Files diff --git a/contrib/win32/openssh/win32compat.vcxproj b/contrib/win32/openssh/win32compat.vcxproj index 54e172e..cfcebf8 100644 --- a/contrib/win32/openssh/win32compat.vcxproj +++ b/contrib/win32/openssh/win32compat.vcxproj @@ -185,6 +185,25 @@ + + + + + + + + + + + + + + + + + + + diff --git a/contrib/win32/openssh/win32compat.vcxproj.filters b/contrib/win32/openssh/win32compat.vcxproj.filters index 4f54e21..8b7f797 100644 --- a/contrib/win32/openssh/win32compat.vcxproj.filters +++ b/contrib/win32/openssh/win32compat.vcxproj.filters @@ -13,6 +13,18 @@ {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav + + {7eee54fc-3739-4ebb-b15e-9558c1ec91d3} + + + {17007d09-bf66-4728-8780-b9a89f427dc8} + + + {a6170aac-573e-4d21-90ac-d55b2bcb2dde} + + + {4491864e-2a50-49ad-a8a6-0e4f822a45e9} + @@ -75,7 +87,7 @@ Source Files - + Source Files @@ -128,5 +140,62 @@ Header Files + + includes + + + includes + + + includes + + + includes + + + includes + + + includes + + + includes + + + includes + + + includes\sys + + + includes\sys + + + includes\sys + + + includes\sys + + + includes\sys + + + includes\arpa + + + includes\arpa + + + includes\netinet + + + includes\netinet + + + includes\netinet + + + includes\netinet + \ No newline at end of file diff --git a/contrib/win32/openssh/win32iocompat.vcxproj b/contrib/win32/openssh/win32iocompat.vcxproj index af3e97c..964598b 100644 --- a/contrib/win32/openssh/win32iocompat.vcxproj +++ b/contrib/win32/openssh/win32iocompat.vcxproj @@ -161,6 +161,7 @@ + diff --git a/contrib/win32/openssh/win32iocompat.vcxproj.filters b/contrib/win32/openssh/win32iocompat.vcxproj.filters index 2da4e4f..89d2ed0 100644 --- a/contrib/win32/openssh/win32iocompat.vcxproj.filters +++ b/contrib/win32/openssh/win32iocompat.vcxproj.filters @@ -43,6 +43,9 @@ inc\sys + + inc\sys + diff --git a/contrib/win32/win32compat/inc/sys/statvfs.h b/contrib/win32/win32compat/inc/sys/statvfs.h new file mode 100644 index 0000000..8f14361 --- /dev/null +++ b/contrib/win32/win32compat/inc/sys/statvfs.h @@ -0,0 +1,26 @@ +#pragma once + +#define ST_RDONLY 1 +#define ST_NOSUID 2 +typedef unsigned long fsblkcnt_t; +typedef unsigned long fsfilcnt_t; + +struct statvfs { + unsigned long f_bsize; /* File system block size. */ + unsigned long f_frsize; /* Fundamental file system block size. */ + fsblkcnt_t f_blocks; /* Total number of blocks on file system in */ + /* units of f_frsize. */ + fsblkcnt_t f_bfree; /* Total number of free blocks. */ + fsblkcnt_t f_bavail; /* Number of free blocks available to */ + /* non-privileged process. */ + fsfilcnt_t f_files; /* Total number of file serial numbers. */ + fsfilcnt_t f_ffree; /* Total number of free file serial numbers. */ + fsfilcnt_t f_favail; /* Number of file serial numbers available to */ + /* non-privileged process. */ + unsigned long f_fsid; /* File system ID. */ + unsigned long f_flag; /* BBit mask of f_flag values. */ + unsigned long f_namemax;/* Maximum filename length. */ +}; + +int statvfs(const char *, struct statvfs *); +int fstatvfs(int, struct statvfs *); \ No newline at end of file diff --git a/contrib/win32/win32compat/includes/pwd.h b/contrib/win32/win32compat/includes/pwd.h index 3fb810f..17be6f2 100644 --- a/contrib/win32/win32compat/includes/pwd.h +++ b/contrib/win32/win32compat/includes/pwd.h @@ -28,4 +28,8 @@ void endpwent(void); typedef int PWD_USER_TOKEN; /* This is really just a HANDLE, but we might not have windows.h included */ PWD_USER_TOKEN PwdCreateUserToken(const char *pUserName, const char *pDomainName, const char *pSourceName); + +const char * +user_from_uid(uid_t uid, int nouser); + #endif diff --git a/contrib/win32/win32compat/misc.c b/contrib/win32/win32compat/misc.c index 0ff4737..49bcbf5 100644 --- a/contrib/win32/win32compat/misc.c +++ b/contrib/win32/win32compat/misc.c @@ -1,5 +1,6 @@ #include #include "inc\defs.h" +#include "inc\sys\statvfs.h" int usleep(unsigned int useconds) { @@ -15,4 +16,48 @@ pid_t waitpid(pid_t pid, int *status, int options) { void explicit_bzero(void *b, size_t len) { SecureZeroMemory(b, len); +} + +int statvfs(const char *path, struct statvfs *buf) { + DWORD sectorsPerCluster; + DWORD bytesPerSector; + DWORD freeClusters; + DWORD totalClusters; + + if (GetDiskFreeSpace(path, §orsPerCluster, &bytesPerSector, + &freeClusters, &totalClusters) == TRUE) + { + debug3("path : [%s]", path); + debug3("sectorsPerCluster : [%lu]", sectorsPerCluster); + debug3("bytesPerSector : [%lu]", bytesPerSector); + debug3("bytesPerCluster : [%lu]", sectorsPerCluster * bytesPerSector); + debug3("freeClusters : [%lu]", freeClusters); + debug3("totalClusters : [%lu]", totalClusters); + + buf->f_bsize = sectorsPerCluster * bytesPerSector; + buf->f_frsize = sectorsPerCluster * bytesPerSector; + buf->f_blocks = totalClusters; + buf->f_bfree = freeClusters; + buf->f_bavail = freeClusters; + buf->f_files = -1; + buf->f_ffree = -1; + buf->f_favail = -1; + buf->f_fsid = 0; + buf->f_flag = 0; + buf->f_namemax = MAX_PATH - 1; + + return 0; + } + else + { + debug3("ERROR: Cannot get free space for [%s]. Error code is : %d.\n", + path, GetLastError()); + + return -1; + } +} + +int fstatvfs(int fd, struct statvfs *buf) { + errno = ENOSYS; + return -1; } \ No newline at end of file diff --git a/contrib/win32/win32compat/pwd.c b/contrib/win32/win32compat/pwd.c index 4768190..fe5aaeb 100644 --- a/contrib/win32/win32compat/pwd.c +++ b/contrib/win32/win32compat/pwd.c @@ -411,6 +411,38 @@ void endpwent(void) */ } +#define NCACHE 64 /* power of 2 */ +#define MASK (NCACHE - 1) /* bits to store with */ + +const char * +user_from_uid(uid_t uid, int nouser) +{ + static struct ncache { + uid_t uid; + char *name; + } c_uid[NCACHE]; + static int pwopen; + static char nbuf[15]; /* 32 bits == 10 digits */ + struct passwd *pw; + struct ncache *cp; + + cp = c_uid + (uid & MASK); + if (cp->uid != uid || cp->name == NULL) { + if (pwopen == 0) { + pwopen = 1; + } + if ((pw = getpwuid(uid)) == NULL) { + if (nouser) + return (NULL); + (void)snprintf(nbuf, sizeof(nbuf), "%u", uid); + } + cp->uid = uid; + if (cp->name != NULL) + free(cp->name); + cp->name = strdup(pw ? pw->pw_name : nbuf); + } + return (cp->name); +} #ifdef USE_NTCREATETOKEN @@ -426,3 +458,81 @@ PWD_USER_TOKEN PwdCreateUserToken(const char *pUserName, } #endif + +/* TODO - this is moved from realpath.c in openbsdcompat. Review and finalize its position*/ + +#include + +void backslashconvert(char *str) +{ + while (*str) { + if (*str == '/') + *str = '\\'; // convert forward slash to back slash + str++; + } + +} + +// convert back slash to forward slash +void slashconvert(char *str) +{ + while (*str) { + if (*str == '\\') + *str = '/'; // convert back slash to forward slash + str++; + } +} + +char *realpathWin32(const char *path, char resolved[PATH_MAX]) +{ + char realpath[PATH_MAX]; + + strlcpy(resolved, path + 1, sizeof(realpath)); + backslashconvert(resolved); + PathCanonicalizeA(realpath, resolved); + slashconvert(realpath); + + /* + * Store terminating slash in 'X:/' on Windows. + */ + + if (realpath[1] == ':' && realpath[2] == 0) + { + realpath[2] = '/'; + realpath[3] = 0; + } + + resolved[0] = *path; // will be our first slash in /x:/users/test1 format + strncpy(resolved + 1, realpath, sizeof(realpath)); + return resolved; +} + +// like realpathWin32() but takes out the first slash so that windows systems can work on the actual file or directory +char *realpathWin32i(const char *path, char resolved[PATH_MAX]) +{ + char realpath[PATH_MAX]; + + if (path[0] != '/') { + // absolute form x:/abc/def given, no first slash to take out + strlcpy(resolved, path, sizeof(realpath)); + } + else + strlcpy(resolved, path + 1, sizeof(realpath)); + + backslashconvert(resolved); + PathCanonicalizeA(realpath, resolved); + slashconvert(realpath); + + /* + * Store terminating slash in 'X:/' on Windows. + */ + + if (realpath[1] == ':' && realpath[2] == 0) + { + realpath[2] = '/'; + realpath[3] = 0; + } + + strncpy(resolved, realpath, sizeof(realpath)); + return resolved; +} diff --git a/openbsd-compat/bsd-statvfs.c b/openbsd-compat/bsd-statvfs.c index 69bc295..5540b69 100644 --- a/openbsd-compat/bsd-statvfs.c +++ b/openbsd-compat/bsd-statvfs.c @@ -30,9 +30,6 @@ static void copy_statfs_to_statvfs(struct statvfs *to, struct statfs *from) { -#ifdef WIN32_FIXME -//PRAGMA:TODO -#else /* WIN32_FIXME */ to->f_bsize = from->f_bsize; to->f_frsize = from->f_bsize; /* no exact equivalent */ @@ -45,53 +42,11 @@ copy_statfs_to_statvfs(struct statvfs *to, struct statfs *from) to->f_fsid = 0; /* XXX fix me */ to->f_flag = from->f_flags; to->f_namemax = MNAMELEN; -#endif /* !WIN32_FIXME */ } # ifndef HAVE_STATVFS int statvfs(const char *path, struct statvfs *buf) { -#ifdef WIN32_FIXME - - DWORD sectorsPerCluster; - DWORD bytesPerSector; - DWORD freeClusters; - DWORD totalClusters; - - if (GetDiskFreeSpace(path, §orsPerCluster, &bytesPerSector, - &freeClusters, &totalClusters) == TRUE) - { - debug3("path : [%s]", path); - debug3("sectorsPerCluster : [%lu]", sectorsPerCluster); - debug3("bytesPerSector : [%lu]", bytesPerSector); - debug3("bytesPerCluster : [%lu]", sectorsPerCluster * bytesPerSector); - debug3("freeClusters : [%lu]", freeClusters); - debug3("totalClusters : [%lu]", totalClusters); - - buf -> f_bsize = sectorsPerCluster * bytesPerSector; - buf -> f_frsize = sectorsPerCluster * bytesPerSector; - buf -> f_blocks = totalClusters; - buf -> f_bfree = freeClusters; - buf -> f_bavail = freeClusters; - buf -> f_files = -1; - buf -> f_ffree = -1; - buf -> f_favail = -1; - buf -> f_fsid = 0; - buf -> f_flag = 0; - buf -> f_namemax = MAX_PATH - 1; - - return 0; - } - else - { - debug3("ERROR: Cannot get free space for [%s]. Error code is : %d.\n", - path, GetLastError()); - - return -1; - } - -#else /* WIN32_FIXME */ - # ifdef HAVE_STATFS struct statfs fs; @@ -104,7 +59,6 @@ int statvfs(const char *path, struct statvfs *buf) errno = ENOSYS; return -1; # endif -#endif /* !WIN32_FIXME */ } # endif diff --git a/openbsd-compat/pwcache.c b/openbsd-compat/pwcache.c index 37221ba..fa7caa9 100644 --- a/openbsd-compat/pwcache.c +++ b/openbsd-compat/pwcache.c @@ -82,7 +82,6 @@ user_from_uid(uid_t uid, int nouser) char * group_from_gid(gid_t gid, int nogroup) { -#ifndef WIN32_FIXME static struct ncache { gid_t gid; char *name; @@ -119,6 +118,5 @@ group_from_gid(gid_t gid, int nogroup) return NULL; -#endif } #endif diff --git a/openbsd-compat/realpath.c b/openbsd-compat/realpath.c index a03c4a8..be4e9c5 100644 --- a/openbsd-compat/realpath.c +++ b/openbsd-compat/realpath.c @@ -31,8 +31,6 @@ #include "includes.h" -#ifndef WIN32_FIXME - #if !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH) #include @@ -199,83 +197,4 @@ realpath(const char *path, char resolved[PATH_MAX]) resolved[resolved_len - 1] = '\0'; return (resolved); } -#endif /* !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH) */ - -#else - -#include - -void backslashconvert(char *str) -{ - while (*str) { - if (*str == '/') - *str = '\\'; // convert forward slash to back slash - str++; - } - -} - -// convert back slash to forward slash -void slashconvert(char *str) -{ - while (*str) { - if (*str == '\\') - *str = '/'; // convert back slash to forward slash - str++; - } -} - -char *realpathWin32(const char *path, char resolved[PATH_MAX]) -{ - char realpath[PATH_MAX]; - - strlcpy(resolved, path + 1, sizeof(realpath)); - backslashconvert(resolved); - PathCanonicalizeA(realpath, resolved); - slashconvert(realpath); - - /* - * Store terminating slash in 'X:/' on Windows. - */ - - if (realpath[1] == ':' && realpath[2] == 0) - { - realpath[2] = '/'; - realpath[3] = 0; - } - - resolved[0] = *path; // will be our first slash in /x:/users/test1 format - strncpy(resolved + 1, realpath, sizeof(realpath)); - return resolved; -} - -// like realpathWin32() but takes out the first slash so that windows systems can work on the actual file or directory -char *realpathWin32i(const char *path, char resolved[PATH_MAX]) -{ - char realpath[PATH_MAX]; - - if (path[0] != '/') { - // absolute form x:/abc/def given, no first slash to take out - strlcpy(resolved, path, sizeof(realpath)); - } - else - strlcpy(resolved, path + 1, sizeof(realpath)); - - backslashconvert(resolved); - PathCanonicalizeA(realpath, resolved); - slashconvert(realpath); - - /* - * Store terminating slash in 'X:/' on Windows. - */ - - if (realpath[1] == ':' && realpath[2] == 0) - { - realpath[2] = '/'; - realpath[3] = 0; - } - - strncpy(resolved, realpath, sizeof(realpath)); - return resolved; -} -#endif /* WIN32_FIXME */ +#endif /* !defined(HAVE_REALPATH) || defined(BROKEN_REALPATH) */ \ No newline at end of file