From: Oliver Kurth Date: Fri, 15 Sep 2017 18:23:25 +0000 (-0700) Subject: Multiple changes: X-Git-Tag: stable-10.2.0~347 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a391909df76d7e94e7ec768b5946d448dc4ce55b;p=thirdparty%2Fopen-vm-tools.git Multiple changes: lib/file/file.c: - Add new function File_ContainSymLink(). lib/file/filePosix.c: - Add new function File_GetMountPath() lib/include/file.h: - Add new function declarations. lib/include/dynbuf.h: - Clean up documentation. - Rename DynBuf_ConcatString() to DynBuf_Strcat(). - New inline static function DynBuf_GetString(). lib/misc/dynbuf.c: - Clean up documentation comments. - New function DynBuf_DetachString(). lib/misc/hostinfoPosix.c: - Utilize new DynBuf functions. lib/misc/strutil.c: - Clarify documentaion comments based on DynBuf changes. lib/misc/utilMem.c: - Adjustment to comments. Common source file changeis; not applicable to open-vm-tools. --- diff --git a/open-vm-tools/lib/file/file.c b/open-vm-tools/lib/file/file.c index 1739ad91f..456af1f62 100644 --- a/open-vm-tools/lib/file/file.c +++ b/open-vm-tools/lib/file/file.c @@ -2453,3 +2453,47 @@ File_GetFSMountInfo(const char *pathName, } +/* + *---------------------------------------------------------------------- + * + * File_ContainSymLink -- + * + * Check if the specified file path contains symbolic link. + * + * Results: + * return TRUE if pathName contains a symlink, + * return FALSE if pathName is not a symlink or error. + * + * Side effects: + * None + * + *---------------------------------------------------------------------- + */ + +Bool +File_ContainSymLink(const char *pathName) // IN: +{ + char *path = NULL; + char *base = NULL; + Bool retValue = FALSE; + + if (File_IsSymLink(pathName)) { + return TRUE; + } + + File_GetPathName(pathName, &path, &base); + + if ( (path != NULL) + && (base != NULL) + && (strcmp(path, "") != 0) + && (strcmp(base, "") != 0)) { + if (File_ContainSymLink(path)) { + retValue = TRUE; + } + } + + free(path); + free(base); + + return retValue; +} diff --git a/open-vm-tools/lib/file/filePosix.c b/open-vm-tools/lib/file/filePosix.c index 24f2a4a39..4cfc31275 100644 --- a/open-vm-tools/lib/file/filePosix.c +++ b/open-vm-tools/lib/file/filePosix.c @@ -3262,3 +3262,32 @@ File_IsCharDevice(const char *pathName) // IN: return (FileAttributes(pathName, &fileData) == 0) && (fileData.fileType == FILE_TYPE_CHARDEVICE); } + + +/* + *---------------------------------------------------------------------------- + * + * File_GetMountPath -- + * + * This function translates the path for a symlink to the physical path. + * If checkEntirePath is TRUE, this function will try to translate + * every parent directory to physical path. + * Caller must free the returned buffer if valid path is returned. + * + * Results: + * return valid physical path if successfully. + * return NULL if error. + * + * Side effects: + * The result is allocated. + * + *---------------------------------------------------------------------------- + */ + +char * +File_GetMountPath(const char *pathName, // IN: + Bool checkEntirePath) // IN: +{ + NOT_IMPLEMENTED(); + return NULL; +} diff --git a/open-vm-tools/lib/include/dynbuf.h b/open-vm-tools/lib/include/dynbuf.h index b2a96a6ca..55fc2d062 100644 --- a/open-vm-tools/lib/include/dynbuf.h +++ b/open-vm-tools/lib/include/dynbuf.h @@ -22,8 +22,8 @@ * Dynamic buffers */ -#ifndef __DYNBUF_H__ -# define __DYNBUF_H__ +#ifndef DYNBUF_H +# define DYNBUF_H #include #include "vm_basic_types.h" @@ -62,6 +62,9 @@ DynBuf_Attach(DynBuf *b, // IN void * DynBuf_Detach(DynBuf *b); // IN/OUT +char * +DynBuf_DetachString(DynBuf *b); // IN/OUT + Bool DynBuf_Enlarge(DynBuf *b, // IN/OUT size_t min_size); // IN @@ -118,6 +121,38 @@ DynBuf_Get(DynBuf const *b) // IN } +/* + *----------------------------------------------------------------------------- + * + * DynBuf_GetString -- + * + * Results: + * Returns a pointer to the dynamic buffer data as a NUL-terminated + * string. + * + * Side effects: + * DynBuf might allocate additional memory and will panic if it fails to. + * + *----------------------------------------------------------------------------- + */ + +#if defined(SWIG) +static char * +#else +static INLINE char * +#endif +DynBuf_GetString(DynBuf *b) // IN +{ + ASSERT(b); + + if (b->size == b->allocated) { + ASSERT_MEM_ALLOC(DynBuf_Enlarge(b, b->size + 1)); + } + b->data[b->size] = '\0'; + return b->data; +} + + /* *----------------------------------------------------------------------------- * @@ -212,16 +247,17 @@ DynBuf_GetAllocatedSize(DynBuf const *b) // IN * * DynBuf_AppendString -- * - * Append the string to the specified DynBuf object. Basically a - * fancy strcat(). + * Appends the string to the specified DynBuf object, including its NUL + * terminator. Note that this is NOT like strcat; repeated calls will + * leave embedded NULs in the middle of the buffer. (Compare to + * DynBuf_Strcat.) * * Results: * TRUE on success * FALSE on failure (not enough memory) * - * * Side effects: - * DynBuf may change its size or allocate additional memory. + * DynBuf may change its size or allocate additional memory. * *---------------------------------------------------------------------------- */ @@ -234,29 +270,26 @@ static INLINE Bool DynBuf_AppendString(DynBuf *buf, // IN/OUT const char *string) // IN { - /* - * Make sure to copy the NUL. - */ - return DynBuf_Append(buf, string, strlen(string) + 1); + return DynBuf_Append(buf, string, strlen(string) + 1 /* NUL */); } /* *---------------------------------------------------------------------------- * - * DynBuf_ConcatString -- + * DynBuf_Strcat -- * - * Append the string to the specified DynBuf object without adding a - * trailing NUL. The caller is responsible for NUL-terminating if so - * desired. + * A DynBuf version of strcat. Unlike DynBuf_AppendString, does NOT + * visibly NUL-terminate the DynBuf, thereby allowing future appends to + * do proper string concatenation without leaving embedded NULs in the + * middle. * * Results: * TRUE on success * FALSE on failure (not enough memory) * - * * Side effects: - * DynBuf may change its size or allocate additional memory. + * DynBuf may change its size or allocate additional memory. * *---------------------------------------------------------------------------- */ @@ -266,10 +299,24 @@ static Bool #else static INLINE Bool #endif -DynBuf_ConcatString(DynBuf *buf, // IN/OUT - const char *string) // IN +DynBuf_Strcat(DynBuf *buf, // IN/OUT + const char *string) // IN { - return DynBuf_Append(buf, string, strlen(string)); + Bool success; + + ASSERT(buf != NULL); + ASSERT(string != NULL); + + /* + * We actually do NUL-terminate the buffer internally, but this is not + * visible to callers, and they should not rely on this. + */ + success = DynBuf_AppendString(buf, string); + if (LIKELY(success)) { + ASSERT(buf->size > 0); + buf->size--; + } + return success; } @@ -277,4 +324,4 @@ DynBuf_ConcatString(DynBuf *buf, // IN/OUT } // extern "C" #endif -#endif /* __DYNBUF_H__ */ +#endif /* DYNBUF_H */ diff --git a/open-vm-tools/lib/include/file.h b/open-vm-tools/lib/include/file.h index b714ebaf4..26d872e21 100644 --- a/open-vm-tools/lib/include/file.h +++ b/open-vm-tools/lib/include/file.h @@ -212,6 +212,8 @@ Bool File_IsFile(const char *pathName); Bool File_IsSymLink(const char *pathName); +Bool File_ContainSymLink(const char *pathName); + Bool File_IsCharDevice(const char *pathName); Bool File_GetParent(char **canPath); @@ -249,6 +251,8 @@ char *File_GetModTimeString(const char *pathName); char *File_GetUniqueFileSystemID(const char *pathName); +char *File_GetMountPath(const char *pathName, Bool checkEntirePath); + #ifdef _WIN32 char *File_GetVolumeGUID(const char *pathName); #endif diff --git a/open-vm-tools/lib/include/productState.h b/open-vm-tools/lib/include/productState.h index 6570c3861..7d8c6f202 100644 --- a/open-vm-tools/lib/include/productState.h +++ b/open-vm-tools/lib/include/productState.h @@ -51,6 +51,7 @@ typedef enum { PRODUCT_GANTRY = 1 << 9, PRODUCT_VMACORETESTS = 1 << 10, PRODUCT_SRM = 1 << 11, + PRODUCT_VIEWCRT = 1 << 12, /* etc */ } Product; typedef uint64 ProductMask; diff --git a/open-vm-tools/lib/misc/dynbuf.c b/open-vm-tools/lib/misc/dynbuf.c index 8d88ce6ae..129384154 100644 --- a/open-vm-tools/lib/misc/dynbuf.c +++ b/open-vm-tools/lib/misc/dynbuf.c @@ -126,7 +126,8 @@ DynBuf_Destroy(DynBuf *b) // IN/OUT: * a copy of that data. * * Results: - * The pointer to the data. NULL on out of memory failure. + * The pointer to the data. NULL on out of memory failure or if the + * input DynBuf is empty. * * Side effects: * Allocates memory. @@ -140,6 +141,10 @@ DynBuf_AllocGet(DynBuf const *b) // IN: void *new_data; ASSERT(b); + if (b->size == 0) { + return NULL; + } + new_data = malloc(b->size); if (new_data) { memcpy(new_data, b->data, b->size); @@ -185,11 +190,11 @@ DynBuf_Attach(DynBuf *b, // IN/OUT: * * DynBuf_Detach -- * - * Releases ownership of the buffer stored in the DynBuf object, - * and returns a pointer to it. + * Transfers ownership of the buffer stored in the DynBuf object to the + * caller. * * Results: - * The pointer to the data. + * Returns a pointer to the data. The caller must free it with free(). * * Side effects: * None @@ -212,6 +217,33 @@ DynBuf_Detach(DynBuf *b) // IN/OUT: } +/* + *----------------------------------------------------------------------------- + * + * DynBuf_DetachString -- + * + * Transfers ownership of the buffer stored in the DynBuf object to the + * caller. + * + * Results: + * Returns a pointer to the data as a NUL-terminated string. The caller + * must free it with free(). + * + * Side effects: + * None + * + *----------------------------------------------------------------------------- + */ + +char * +DynBuf_DetachString(DynBuf *b) // IN/OUT +{ + char *data = DynBuf_GetString(b); + DynBuf_Detach(b); + return data; +} + + /* *----------------------------------------------------------------------------- * @@ -360,7 +392,7 @@ DynBuf_Append(DynBuf *b, // IN/OUT: if (new_size > b->allocated) { /* Not enough room */ - if (DynBuf_Enlarge(b, new_size) == FALSE) { + if (!DynBuf_Enlarge(b, new_size)) { return FALSE; } } diff --git a/open-vm-tools/lib/misc/hostinfoPosix.c b/open-vm-tools/lib/misc/hostinfoPosix.c index a0cdefb5e..472b20771 100644 --- a/open-vm-tools/lib/misc/hostinfoPosix.c +++ b/open-vm-tools/lib/misc/hostinfoPosix.c @@ -841,14 +841,12 @@ HostinfoGetCmdOutput(const char *cmd) // IN: break; } - /* size does -not- include the NUL terminator. */ - DynBuf_Append(&db, line, size + 1); + /* size does not include the NUL terminator. */ + DynBuf_Append(&db, line, size); free(line); } - if (DynBuf_Get(&db)) { - out = (char *) DynBuf_AllocGet(&db); - } + out = DynBuf_DetachString(&db); closeIt: pclose(stream); diff --git a/open-vm-tools/lib/misc/strutil.c b/open-vm-tools/lib/misc/strutil.c index d00e560f5..1ff111aaf 100644 --- a/open-vm-tools/lib/misc/strutil.c +++ b/open-vm-tools/lib/misc/strutil.c @@ -960,8 +960,7 @@ StrUtil_IsASCII(const char *s) // IN * StrUtil_VDynBufPrintf -- * * This is a vprintf() variant which appends directly into a - * dynbuf. The dynbuf is not NUL-terminated: The printf() result - * is written immediately after the last byte in the DynBuf. + * DynBuf. Does NOT visibly NUL-terminate the DynBuf. * * This function does not use any temporary buffer. The printf() * result can be arbitrarily large. This function automatically @@ -1012,6 +1011,11 @@ StrUtil_VDynBufPrintf(DynBuf *b, // IN/OUT va_list tmpArgs; va_copy(tmpArgs, args); + + /* + * We actually do NUL-terminate the buffer internally, but this is not + * visible to callers, and they should not rely on this. + */ i = Str_Vsnprintf((char *) DynBuf_Get(b) + size, allocSize - size, fmt, tmpArgs); va_end(tmpArgs); diff --git a/open-vm-tools/lib/misc/utilMem.c b/open-vm-tools/lib/misc/utilMem.c index 4c45cf524..ca80ace54 100644 --- a/open-vm-tools/lib/misc/utilMem.c +++ b/open-vm-tools/lib/misc/utilMem.c @@ -69,7 +69,8 @@ UtilAllocationFailure1(int bugNumber, const char *file, int lineno) * * UtilSafeMalloc0 -- * UtilSafeMalloc1 -- - * Helper function for malloc + * + * Helper functions for Util_SafeMalloc. * * Results: * Pointer to the dynamically allocated memory. @@ -110,7 +111,8 @@ UtilSafeMalloc1(size_t size, // IN: * * UtilSafeRealloc0 -- * UtilSafeRealloc1 -- - * Helper function for realloc + * + * Helper functions for Util_SafeRealloc. * * Results: * Pointer to the dynamically allocated memory. @@ -153,7 +155,8 @@ UtilSafeRealloc1(void *ptr, // IN: * * UtilSafeCalloc0 -- * UtilSafeCalloc1 -- - * Helper function for calloc + * + * Helper functions for Util_SafeCalloc. * * Results: * Pointer to the dynamically allocated memory. @@ -194,8 +197,10 @@ UtilSafeCalloc1(size_t nmemb, // IN: /* *----------------------------------------------------------------------------- * - * Util_SafeStrdup -- - * Helper function for strdup + * UtilSafeStrdup0 -- + * UtilSafeStrdup1 -- + * + * Helper functions for Util_SafeStrdup. * * Results: * Pointer to the dynamically allocated, duplicate string @@ -248,7 +253,10 @@ UtilSafeStrdup1(const char *s, // IN: /* *----------------------------------------------------------------------------- * - * Util_SafeStrndup -- + * UtilSafeStrndup0 -- + * UtilSafeStrndup1 -- + * + * Helper functions for Util_SafeStrndup. * * Returns a string consisting of first n characters of 's' if 's' has * length >= 'n', otherwise returns a string duplicate of 's'. diff --git a/open-vm-tools/lib/misc/util_misc.c b/open-vm-tools/lib/misc/util_misc.c index 00f83cb24..98389d4de 100644 --- a/open-vm-tools/lib/misc/util_misc.c +++ b/open-vm-tools/lib/misc/util_misc.c @@ -560,21 +560,26 @@ UtilDoTildeSubst(const char *user) // IN: name of user if (*user == '\0') { #if defined(__APPLE__) /* - * The HOME environment variable is not always set on Mac OS. - * (was bug 841728) + * This check mimics the checks and order of CFCopyHomeDirectoryURL(), + * which is unfortunately not callable directly since Apple has marked it + * as only in iOS despite the fact that they clearly ship it on macOS. */ + str = issetugid() ? NULL + : Unicode_Duplicate(Posix_Getenv("CFFIXED_USER_HOME")); + if (str == NULL) { pwd = Posix_Getpwuid(getuid()); if (pwd == NULL) { Log("Could not get passwd for current user.\n"); } } -#else // !defined(__APPLE__) - str = Unicode_Duplicate(Posix_Getenv("HOME")); - if (str == NULL) { - Log("Could not expand environment variable HOME.\n"); +#endif // defined(__APPLE__) + if (str == NULL && pwd == NULL) { + str = Unicode_Duplicate(Posix_Getenv("HOME")); + if (str == NULL) { + Log("Could not expand environment variable HOME.\n"); + } } -#endif // !defined(__APPLE__) } else { pwd = Posix_Getpwnam(user); if (pwd == NULL) {