Initial commit.
authorAndreas Schneider <asn@cryptomilk.org>
Wed, 5 Jun 2013 15:19:59 +0000 (17:19 +0200)
committerAndreas Schneider <asn@cryptomilk.org>
Wed, 5 Jun 2013 15:19:59 +0000 (17:19 +0200)
18 files changed:
CMakeLists.txt [new file with mode: 0644]
COPYING [new file with mode: 0644]
CPackConfig.cmake [new file with mode: 0644]
CTestConfig.cmake [new file with mode: 0644]
ConfigureChecks.cmake [new file with mode: 0644]
DefineOptions.cmake [new file with mode: 0644]
README [new file with mode: 0644]
cmake/Modules/COPYING-CMAKE-SCRIPTS [new file with mode: 0644]
cmake/Modules/CheckCCompilerFlagSSP.cmake [new file with mode: 0644]
cmake/Modules/DefineCMakeDefaults.cmake [new file with mode: 0644]
cmake/Modules/DefineCompilerFlags.cmake [new file with mode: 0644]
cmake/Modules/DefineInstallationPaths.cmake [new file with mode: 0644]
cmake/Modules/DefinePlatformDefaults.cmake [new file with mode: 0644]
cmake/Modules/MacroEnsureOutOfSourceBuild.cmake [new file with mode: 0644]
config.h.cmake [new file with mode: 0644]
src/CMakeLists.txt [new file with mode: 0644]
src/uid_wrapper.c [new file with mode: 0644]
src/uid_wrapper.h [new file with mode: 0644]

diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644 (file)
index 0000000..11d460a
--- /dev/null
@@ -0,0 +1,51 @@
+project(uid_wrapper C)
+
+# Required cmake version
+cmake_minimum_required(VERSION 2.8.0)
+
+# global needed variables
+set(APPLICATION_NAME ${PROJECT_NAME})
+
+set(APPLICATION_VERSION_MAJOR "0")
+set(APPLICATION_VERSION_MINOR "1")
+set(APPLICATION_VERSION_PATCH "0")
+
+set(APPLICATION_VERSION "${APPLICATION_VERSION_MAJOR}.${APPLICATION_VERSION_MINOR}.${APPLICATION_VERSION_PATCH}")
+
+# SOVERSION scheme: CURRENT.AGE.REVISION
+#   If there was an incompatible interface change:
+#     Increment CURRENT. Set AGE and REVISION to 0
+#   If there was a compatible interface change:
+#     Increment AGE. Set REVISION to 0
+#   If the source code was changed, but there were no interface changes:
+#     Increment REVISION.
+set(LIBRARY_VERSION "0.0.1")
+set(LIBRARY_SOVERSION "0")
+
+# where to look first for cmake modules, before ${CMAKE_ROOT}/Modules/ is checked
+set(CMAKE_MODULE_PATH
+  ${CMAKE_SOURCE_DIR}/cmake/Modules
+)
+
+# add definitions
+include(DefineCMakeDefaults)
+include(DefinePlatformDefaults)
+include(DefineCompilerFlags)
+include(DefineInstallationPaths)
+include(DefineOptions.cmake)
+include(CPackConfig.cmake)
+
+# disallow in-source build
+include(MacroEnsureOutOfSourceBuild)
+macro_ensure_out_of_source_build("${PROJECT_NAME} requires an out of source build. Please create a separate build directory and run 'cmake /path/to/${PROJECT_NAME} [options]' there.")
+
+# Find out if we have threading available
+set(CMAKE_THREAD_PREFER_PTHREADS ON)
+find_package(Threads)
+
+# config.h checks
+include(ConfigureChecks.cmake)
+configure_file(config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h)
+
+# check subdirectories
+add_subdirectory(src)
diff --git a/COPYING b/COPYING
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/CPackConfig.cmake b/CPackConfig.cmake
new file mode 100644 (file)
index 0000000..2e4c9fe
--- /dev/null
@@ -0,0 +1,53 @@
+# For help take a look at:
+# http://www.cmake.org/Wiki/CMake:CPackConfiguration
+
+### general settings
+set(CPACK_PACKAGE_NAME ${APPLICATION_NAME})
+set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "The SSH library")
+set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_SOURCE_DIR}/README")
+set(CPACK_PACKAGE_VENDOR "The SSH Library Development Team")
+set(CPACK_PACKAGE_INSTALL_DIRECTORY ${CPACK_PACKAGE_NAME})
+set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/COPYING")
+
+
+### versions
+set(CPACK_PACKAGE_VERSION_MAJOR "0")
+set(CPACK_PACKAGE_VERSION_MINOR "5")
+set(CPACK_PACKAGE_VERSION_PATCH "90")
+set(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
+
+
+### source generator
+set(CPACK_SOURCE_GENERATOR "TGZ")
+set(CPACK_SOURCE_IGNORE_FILES "~$;[.]swp$;/[.]svn/;/[.]git/;.gitignore;/build/;tags;cscope.*")
+set(CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
+
+if (WIN32)
+    set(CPACK_GENERATOR "ZIP")
+
+    ### nsis generator
+    find_package(NSIS)
+    if (NSIS_MAKE)
+        set(CPACK_GENERATOR "${CPACK_GENERATOR};NSIS")
+        set(CPACK_NSIS_DISPLAY_NAME "The SSH Library")
+        set(CPACK_NSIS_COMPRESSOR "/SOLID zlib")
+        set(CPACK_NSIS_MENU_LINKS "http://www.libssh.org/" "libssh homepage")
+    endif (NSIS_MAKE)
+endif (WIN32)
+
+set(CPACK_PACKAGE_INSTALL_DIRECTORY "libssh")
+
+set(CPACK_PACKAGE_FILE_NAME ${APPLICATION_NAME}-${CPACK_PACKAGE_VERSION})
+
+set(CPACK_COMPONENT_LIBRARIES_DISPLAY_NAME "Libraries")
+set(CPACK_COMPONENT_HEADERS_DISPLAY_NAME "C/C++ Headers")
+set(CPACK_COMPONENT_LIBRARIES_DESCRIPTION
+  "Libraries used to build programs which use libssh")
+set(CPACK_COMPONENT_HEADERS_DESCRIPTION
+  "C/C++ header files for use with libssh")
+set(CPACK_COMPONENT_HEADERS_DEPENDS libraries)
+#set(CPACK_COMPONENT_APPLICATIONS_GROUP "Runtime")
+set(CPACK_COMPONENT_LIBRARIES_GROUP "Development")
+set(CPACK_COMPONENT_HEADERS_GROUP "Development")
+
+include(CPack)
diff --git a/CTestConfig.cmake b/CTestConfig.cmake
new file mode 100644 (file)
index 0000000..d8a4183
--- /dev/null
@@ -0,0 +1,9 @@
+set(UPDATE_TYPE "true")
+
+set(CTEST_PROJECT_NAME "libssh")
+set(CTEST_NIGHTLY_START_TIME "01:00:00 CET")
+
+set(CTEST_DROP_METHOD "http")
+set(CTEST_DROP_SITE "test.libssh.org")
+set(CTEST_DROP_LOCATION "/submit.php?project=libssh")
+set(CTEST_DROP_SITE_CDASH TRUE)
diff --git a/ConfigureChecks.cmake b/ConfigureChecks.cmake
new file mode 100644 (file)
index 0000000..1633543
--- /dev/null
@@ -0,0 +1,89 @@
+include(CheckIncludeFile)
+include(CheckSymbolExists)
+include(CheckFunctionExists)
+include(CheckLibraryExists)
+include(CheckTypeSize)
+include(CheckStructHasMember)
+include(CheckPrototypeDefinition)
+include(TestBigEndian)
+
+set(PACKAGE ${APPLICATION_NAME})
+set(VERSION ${APPLICATION_VERSION})
+set(DATADIR ${DATA_INSTALL_DIR})
+set(LIBDIR ${LIB_INSTALL_DIR})
+set(PLUGINDIR "${PLUGIN_INSTALL_DIR}-${LIBRARY_SOVERSION}")
+set(SYSCONFDIR ${SYSCONF_INSTALL_DIR})
+
+set(BINARYDIR ${CMAKE_BINARY_DIR})
+set(SOURCEDIR ${CMAKE_SOURCE_DIR})
+
+function(COMPILER_DUMPVERSION _OUTPUT_VERSION)
+    # Remove whitespaces from the argument.
+    # This is needed for CC="ccache gcc" cmake ..
+    string(REPLACE " " "" _C_COMPILER_ARG "${CMAKE_C_COMPILER_ARG1}")
+
+    execute_process(
+        COMMAND
+            ${CMAKE_C_COMPILER} ${_C_COMPILER_ARG} -dumpversion
+        OUTPUT_VARIABLE _COMPILER_VERSION
+    )
+
+    string(REGEX REPLACE "([0-9])\\.([0-9])(\\.[0-9])?" "\\1\\2"
+           _COMPILER_VERSION "${_COMPILER_VERSION}")
+
+    set(${_OUTPUT_VERSION} ${_COMPILER_VERSION} PARENT_SCOPE)
+endfunction()
+
+if(CMAKE_COMPILER_IS_GNUCC AND NOT MINGW AND NOT OS2)
+    compiler_dumpversion(GNUCC_VERSION)
+    if (NOT GNUCC_VERSION EQUAL 34)
+        set(CMAKE_REQUIRED_FLAGS "-fvisibility=hidden")
+        check_c_source_compiles(
+"void __attribute__((visibility(\"default\"))) test() {}
+int main(void){ return 0; }
+" WITH_VISIBILITY_HIDDEN)
+        set(CMAKE_REQUIRED_FLAGS "")
+    endif (NOT GNUCC_VERSION EQUAL 34)
+endif(CMAKE_COMPILER_IS_GNUCC AND NOT MINGW AND NOT OS2)
+
+# HEADERS
+check_include_file(sys/types.h HAVE_SYS_TYPES_H)
+check_include_file(sys/syscall.h HAVE_SYS_SYSCALL_H)
+check_include_file(syscall.h HAVE_SYSCALL_H)
+check_include_file(grp.h HAVE_GRP_H)
+check_include_file(unistd.h HAVE_UNISTD_H)
+
+# FUNCTIONS
+check_function_exists(strncpy HAVE_STRNCPY)
+check_function_exists(vsnprintf HAVE_VSNPRINTF)
+check_function_exists(snprintf HAVE_SNPRINTF)
+
+check_function_exists(seteuid HAVE_SETEUID)
+check_function_exists(setreuid HAVE_SETREUID)
+check_function_exists(setreuid HAVE_SETRESUID)
+
+check_function_exists(setegid HAVE_SETEGID)
+check_function_exists(setregid HAVE_SETREGID)
+check_function_exists(setregid HAVE_SETRESGID)
+
+check_function_exists(getgroups HAVE_GETGROUPS)
+check_function_exists(setgroups HAVE_SETGROUPS)
+
+check_function_exists(syscall HAVE_SYSCALL)
+
+if (HAVE_SYSCALL)
+    add_definitions(-D_GNU_SOURCE)
+endif (HAVE_SYSCALL)
+
+check_library_exists(dl dlopen "" HAVE_LIBDL)
+if (HAVE_LIBDL)
+    find_library(DLFCN_LIBRARY dl)
+    set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${DLFCN_LIBRARY})
+endif (HAVE_LIBDL)
+
+# ENDIAN
+if (NOT WIN32)
+    test_big_endian(WORDS_BIGENDIAN)
+endif (NOT WIN32)
+
+set(UIDWRAP_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CACHE INTERNAL "uidwrap required system libraries")
diff --git a/DefineOptions.cmake b/DefineOptions.cmake
new file mode 100644 (file)
index 0000000..6030e79
--- /dev/null
@@ -0,0 +1 @@
+option(UNIT_TESTING "Build with unit tests" OFF)
diff --git a/README b/README
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/cmake/Modules/COPYING-CMAKE-SCRIPTS b/cmake/Modules/COPYING-CMAKE-SCRIPTS
new file mode 100644 (file)
index 0000000..4b41776
--- /dev/null
@@ -0,0 +1,22 @@
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+1. Redistributions of source code must retain the copyright
+   notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+3. The name of the author may not be used to endorse or promote products 
+   derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/cmake/Modules/CheckCCompilerFlagSSP.cmake b/cmake/Modules/CheckCCompilerFlagSSP.cmake
new file mode 100644 (file)
index 0000000..2fe4395
--- /dev/null
@@ -0,0 +1,26 @@
+# - Check whether the C compiler supports a given flag in the
+# context of a stack checking compiler option.
+
+# CHECK_C_COMPILER_FLAG_SSP(FLAG VARIABLE)
+#
+#  FLAG - the compiler flag
+#  VARIABLE - variable to store the result
+#
+#  This actually calls check_c_source_compiles.
+#  See help for CheckCSourceCompiles for a listing of variables
+#  that can modify the build.
+
+# Copyright (c) 2006, Alexander Neundorf, <neundorf@kde.org>
+#
+# Redistribution and use is allowed according to the terms of the BSD license.
+# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
+
+
+include(CheckCSourceCompiles)
+
+function(CHECK_C_COMPILER_FLAG_SSP _FLAG _RESULT)
+   set(SAFE_CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS}")
+   set(CMAKE_REQUIRED_DEFINITIONS "${_FLAG}")
+   check_c_source_compiles("int main(int argc, char **argv) { char buffer[256]; return buffer[argc]=0;}" ${_RESULT})
+   set(CMAKE_REQUIRED_DEFINITIONS "${SAFE_CMAKE_REQUIRED_DEFINITIONS}")
+endfunction(CHECK_C_COMPILER_FLAG_SSP)
diff --git a/cmake/Modules/DefineCMakeDefaults.cmake b/cmake/Modules/DefineCMakeDefaults.cmake
new file mode 100644 (file)
index 0000000..72893c3
--- /dev/null
@@ -0,0 +1,27 @@
+# Always include srcdir and builddir in include path
+# This saves typing ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY} in
+# about every subdir
+# since cmake 2.4.0
+set(CMAKE_INCLUDE_CURRENT_DIR ON)
+
+# Put the include dirs which are in the source or build tree
+# before all other include dirs, so the headers in the sources
+# are prefered over the already installed ones
+# since cmake 2.4.1
+set(CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE ON)
+
+# Use colored output
+# since cmake 2.4.0
+set(CMAKE_COLOR_MAKEFILE ON)
+
+# Define the generic version of the libraries here
+set(GENERIC_LIB_VERSION "0.1.0")
+set(GENERIC_LIB_SOVERSION "0")
+
+# Set the default build type to release with debug info
+if (NOT CMAKE_BUILD_TYPE)
+  set(CMAKE_BUILD_TYPE RelWithDebInfo
+    CACHE STRING
+      "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
+  )
+endif (NOT CMAKE_BUILD_TYPE)
diff --git a/cmake/Modules/DefineCompilerFlags.cmake b/cmake/Modules/DefineCompilerFlags.cmake
new file mode 100644 (file)
index 0000000..582ea1c
--- /dev/null
@@ -0,0 +1,77 @@
+# define system dependent compiler flags
+
+include(CheckCCompilerFlag)
+include(CheckCCompilerFlagSSP)
+
+if (UNIX AND NOT WIN32)
+    #
+    # Define GNUCC compiler flags
+    #
+    if (${CMAKE_C_COMPILER_ID} MATCHES "(GNU|Clang)")
+
+        # add -Wconversion ?
+        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -pedantic -pedantic-errors")
+        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wshadow -Wmissing-prototypes -Wdeclaration-after-statement")
+        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wunused -Wfloat-equal -Wpointer-arith -Wwrite-strings -Wformat-security")
+        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-format-attribute")
+
+        # with -fPIC
+        check_c_compiler_flag("-fPIC" WITH_FPIC)
+        if (WITH_FPIC)
+            set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
+        endif (WITH_FPIC)
+
+        check_c_compiler_flag_ssp("-fstack-protector" WITH_STACK_PROTECTOR)
+        if (WITH_STACK_PROTECTOR)
+            set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector")
+        endif (WITH_STACK_PROTECTOR)
+
+        if (CMAKE_BUILD_TYPE)
+            string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
+            if (NOT CMAKE_BUILD_TYPE_LOWER MATCHES debug)
+                check_c_compiler_flag("-D_FORTIFY_SOURCE=2" WITH_FORTIFY_SOURCE)
+                if (WITH_FORTIFY_SOURCE)
+                    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_FORTIFY_SOURCE=2")
+                endif (WITH_FORTIFY_SOURCE)
+            endif()
+        endif()
+    endif (${CMAKE_C_COMPILER_ID} MATCHES "(GNU|Clang)")
+
+    #
+    # Check for large filesystem support
+    #
+    if (CMAKE_SIZEOF_VOID_P MATCHES "8")
+        # with large file support
+        execute_process(
+            COMMAND
+                getconf LFS64_CFLAGS
+            OUTPUT_VARIABLE
+                _lfs_CFLAGS
+            ERROR_QUIET
+            OUTPUT_STRIP_TRAILING_WHITESPACE
+        )
+    else (CMAKE_SIZEOF_VOID_P MATCHES "8")
+        # with large file support
+        execute_process(
+            COMMAND
+                getconf LFS_CFLAGS
+            OUTPUT_VARIABLE
+                _lfs_CFLAGS
+            ERROR_QUIET
+            OUTPUT_STRIP_TRAILING_WHITESPACE
+        )
+    endif (CMAKE_SIZEOF_VOID_P MATCHES "8")
+    if (_lfs_CFLAGS)
+        string(REGEX REPLACE "[\r\n]" " " "${_lfs_CFLAGS}" "${${_lfs_CFLAGS}}")
+        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_lfs_CFLAGS}")
+    endif (_lfs_CFLAGS)
+
+endif (UNIX AND NOT WIN32)
+
+if (MSVC)
+    # Use secure functions by defaualt and suppress warnings about
+    #"deprecated" functions
+    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES=1")
+    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT=1")
+    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D _CRT_NONSTDC_NO_WARNINGS=1 /D _CRT_SECURE_NO_WARNINGS=1")
+endif (MSVC)
diff --git a/cmake/Modules/DefineInstallationPaths.cmake b/cmake/Modules/DefineInstallationPaths.cmake
new file mode 100644 (file)
index 0000000..d857871
--- /dev/null
@@ -0,0 +1,104 @@
+if (WIN32)
+  # Same same
+  set(BIN_INSTALL_DIR "bin" CACHE PATH "-")
+  set(SBIN_INSTALL_DIR "." CACHE PATH "-")
+  set(LIB_INSTALL_DIR "lib" CACHE PATH "-")
+  set(INCLUDE_INSTALL_DIR "include" CACHE PATH "-")
+  set(PLUGIN_INSTALL_DIR "plugins" CACHE PATH "-")
+  set(HTML_INSTALL_DIR "doc/HTML" CACHE PATH "-")
+  set(ICON_INSTALL_DIR "." CACHE PATH "-")
+  set(SOUND_INSTALL_DIR "." CACHE PATH "-")
+  set(LOCALE_INSTALL_DIR "lang" CACHE PATH "-")
+elseif (UNIX OR OS2)
+  IF (NOT APPLICATION_NAME)
+    MESSAGE(STATUS "${PROJECT_NAME} is used as APPLICATION_NAME")
+    SET(APPLICATION_NAME ${PROJECT_NAME})
+  ENDIF (NOT APPLICATION_NAME)
+
+  # Suffix for Linux
+  SET(LIB_SUFFIX
+    CACHE STRING "Define suffix of directory name (32/64)"
+  )
+
+  SET(EXEC_INSTALL_PREFIX
+    "${CMAKE_INSTALL_PREFIX}"
+    CACHE PATH  "Base directory for executables and libraries"
+  )
+  SET(SHARE_INSTALL_PREFIX
+    "${CMAKE_INSTALL_PREFIX}/share"
+    CACHE PATH "Base directory for files which go to share/"
+  )
+  SET(DATA_INSTALL_PREFIX
+    "${SHARE_INSTALL_PREFIX}/${APPLICATION_NAME}"
+    CACHE PATH "The parent directory where applications can install their data")
+
+  # The following are directories where stuff will be installed to
+  SET(BIN_INSTALL_DIR
+    "${EXEC_INSTALL_PREFIX}/bin"
+    CACHE PATH "The ${APPLICATION_NAME} binary install dir (default prefix/bin)"
+  )
+  SET(SBIN_INSTALL_DIR
+    "${EXEC_INSTALL_PREFIX}/sbin"
+    CACHE PATH "The ${APPLICATION_NAME} sbin install dir (default prefix/sbin)"
+  )
+  SET(LIB_INSTALL_DIR
+    "${EXEC_INSTALL_PREFIX}/lib${LIB_SUFFIX}"
+    CACHE PATH "The subdirectory relative to the install prefix where libraries will be installed (default is prefix/lib)"
+  )
+  SET(LIBEXEC_INSTALL_DIR
+    "${EXEC_INSTALL_PREFIX}/libexec"
+    CACHE PATH "The subdirectory relative to the install prefix where libraries will be installed (default is prefix/libexec)"
+  )
+  SET(PLUGIN_INSTALL_DIR
+    "${LIB_INSTALL_DIR}/${APPLICATION_NAME}"
+    CACHE PATH "The subdirectory relative to the install prefix where plugins will be installed (default is prefix/lib/${APPLICATION_NAME})"
+  )
+  SET(INCLUDE_INSTALL_DIR
+    "${CMAKE_INSTALL_PREFIX}/include"
+    CACHE PATH "The subdirectory to the header prefix (default prefix/include)"
+  )
+
+  SET(DATA_INSTALL_DIR
+    "${DATA_INSTALL_PREFIX}"
+    CACHE PATH "The parent directory where applications can install their data (default prefix/share/${APPLICATION_NAME})"
+  )
+  SET(HTML_INSTALL_DIR
+    "${DATA_INSTALL_PREFIX}/doc/HTML"
+    CACHE PATH "The HTML install dir for documentation (default data/doc/html)"
+  )
+  SET(ICON_INSTALL_DIR
+    "${DATA_INSTALL_PREFIX}/icons"
+    CACHE PATH "The icon install dir (default data/icons/)"
+  )
+  SET(SOUND_INSTALL_DIR
+    "${DATA_INSTALL_PREFIX}/sounds"
+    CACHE PATH "The install dir for sound files (default data/sounds)"
+  )
+
+  SET(LOCALE_INSTALL_DIR
+    "${SHARE_INSTALL_PREFIX}/locale"
+    CACHE PATH "The install dir for translations (default prefix/share/locale)"
+  )
+
+  SET(XDG_APPS_DIR
+    "${SHARE_INSTALL_PREFIX}/applications/"
+    CACHE PATH "The XDG apps dir"
+  )
+  SET(XDG_DIRECTORY_DIR
+    "${SHARE_INSTALL_PREFIX}/desktop-directories"
+    CACHE PATH "The XDG directory"
+  )
+
+  SET(SYSCONF_INSTALL_DIR
+    "${EXEC_INSTALL_PREFIX}/etc"
+    CACHE PATH "The ${APPLICATION_NAME} sysconfig install dir (default prefix/etc)"
+  )
+  SET(MAN_INSTALL_DIR
+    "${SHARE_INSTALL_PREFIX}/man"
+    CACHE PATH "The ${APPLICATION_NAME} man install dir (default prefix/man)"
+  )
+  SET(INFO_INSTALL_DIR
+    "${SHARE_INSTALL_PREFIX}/info"
+    CACHE PATH "The ${APPLICATION_NAME} info install dir (default prefix/info)"
+  )
+endif ()
diff --git a/cmake/Modules/DefinePlatformDefaults.cmake b/cmake/Modules/DefinePlatformDefaults.cmake
new file mode 100644 (file)
index 0000000..502d936
--- /dev/null
@@ -0,0 +1,28 @@
+# Set system vars
+
+if (CMAKE_SYSTEM_NAME MATCHES "Linux")
+    set(LINUX TRUE)
+endif(CMAKE_SYSTEM_NAME MATCHES "Linux")
+
+if (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
+    set(FREEBSD TRUE)
+    set(BSD TRUE)
+endif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
+
+if (CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
+    set(OPENBSD TRUE)
+    set(BSD TRUE)
+endif (CMAKE_SYSTEM_NAME MATCHES "OpenBSD")
+
+if (CMAKE_SYSTEM_NAME MATCHES "NetBSD")
+    set(NETBSD TRUE)
+    set(BSD TRUE)
+endif (CMAKE_SYSTEM_NAME MATCHES "NetBSD")
+
+if (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
+    set(SOLARIS TRUE)
+endif (CMAKE_SYSTEM_NAME MATCHES "(Solaris|SunOS)")
+
+if (CMAKE_SYSTEM_NAME MATCHES "OS2")
+    set(OS2 TRUE)
+endif (CMAKE_SYSTEM_NAME MATCHES "OS2")
diff --git a/cmake/Modules/MacroEnsureOutOfSourceBuild.cmake b/cmake/Modules/MacroEnsureOutOfSourceBuild.cmake
new file mode 100644 (file)
index 0000000..a2e9480
--- /dev/null
@@ -0,0 +1,17 @@
+# - MACRO_ENSURE_OUT_OF_SOURCE_BUILD(<errorMessage>)
+# MACRO_ENSURE_OUT_OF_SOURCE_BUILD(<errorMessage>)
+
+# Copyright (c) 2006, Alexander Neundorf, <neundorf@kde.org>
+#
+# Redistribution and use is allowed according to the terms of the BSD license.
+# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
+
+macro (MACRO_ENSURE_OUT_OF_SOURCE_BUILD _errorMessage)
+
+   string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_BINARY_DIR}" _insource)
+   if (_insource)
+     message(SEND_ERROR "${_errorMessage}")
+     message(FATAL_ERROR "Remove the file CMakeCache.txt in ${CMAKE_SOURCE_DIR} first.")
+   endif (_insource)
+
+endmacro (MACRO_ENSURE_OUT_OF_SOURCE_BUILD)
diff --git a/config.h.cmake b/config.h.cmake
new file mode 100644 (file)
index 0000000..ffe2106
--- /dev/null
@@ -0,0 +1,58 @@
+/* Name of package */
+#cmakedefine PACKAGE "${APPLICATION_NAME}"
+
+/* Version number of package */
+#cmakedefine VERSION "${APPLICATION_VERSION}"
+
+#cmakedefine LOCALEDIR "${LOCALE_INSTALL_DIR}"
+#cmakedefine DATADIR "${DATADIR}"
+#cmakedefine LIBDIR "${LIBDIR}"
+#cmakedefine PLUGINDIR "${PLUGINDIR}"
+#cmakedefine SYSCONFDIR "${SYSCONFDIR}"
+#cmakedefine BINARYDIR "${BINARYDIR}"
+#cmakedefine SOURCEDIR "${SOURCEDIR}"
+
+/************************** HEADER FILES *************************/
+
+#cmakedefine HAVE_SYS_TYPES_H 1
+#cmakedefine HAVE_SYS_SYSCALL_H 1
+#cmakedefine HAVE_SYSCALL_H 1
+#cmakedefine HAVE_UNISTD_H 1
+#cmakedefine HAVE_GRP_H 1
+
+/*************************** FUNCTIONS ***************************/
+
+/* Define to 1 if you have the `seteuid' function. */
+#cmakedefine HAVE_SETEUID 1
+
+/* Define to 1 if you have the `setreuid' function. */
+#cmakedefine HAVE_SETREUID 1
+
+/* Define to 1 if you have the `setresuid' function. */
+#cmakedefine HAVE_SETREUID 1
+
+/* Define to 1 if you have the `setegid' function. */
+#cmakedefine HAVE_SETEGID 1
+
+/* Define to 1 if you have the `setregid' function. */
+#cmakedefine HAVE_SETREGID 1
+
+/* Define to 1 if you have the `setresgid' function. */
+#cmakedefine HAVE_SETREGID 1
+
+/* Define to 1 if you have the `syscall' function. */
+#cmakedefine HAVE_SYSCALL 1
+
+/*************************** LIBRARIES ***************************/
+
+
+/**************************** OPTIONS ****************************/
+
+#cmakedefine SOLARIS_GETPWENT_R 1
+#cmakedefine SOLARIS_GETGRENT_R 1
+
+/*************************** ENDIAN *****************************/
+
+/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
+   significant byte first (like Motorola and SPARC, unlike Intel). */
+#cmakedefine WORDS_BIGENDIAN 1
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644 (file)
index 0000000..73406a2
--- /dev/null
@@ -0,0 +1,13 @@
+project(libuid_wrapper C)
+
+include_directories(${CMAKE_BINARY_DIR})
+add_library(uid_wrapper SHARED uid_wrapper.c)
+target_link_libraries(uid_wrapper ${NSSWRAP_REQUIRED_LIBRARIES})
+
+install(
+  TARGETS
+    uid_wrapper
+  RUNTIME DESTINATION ${BIN_INSTALL_DIR}
+  LIBRARY DESTINATION ${LIB_INSTALL_DIR}
+  ARCHIVE DESTINATION ${LIB_INSTALL_DIR}
+)
diff --git a/src/uid_wrapper.c b/src/uid_wrapper.c
new file mode 100644 (file)
index 0000000..0e7d459
--- /dev/null
@@ -0,0 +1,402 @@
+/*
+ * Copyright (c) 2009      Andrew Tridgell
+ * Copyright (c) 2011-2013 Andreas Schneider <asn@samba.org>
+ *
+ * The SSH Library is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at your
+ * option) any later version.
+ *
+ * The SSH Library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with the SSH Library; see the file COPYING.  If not, write to
+ * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include <errno.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <grp.h>
+#ifdef HAVE_SYS_SYSCALL_H
+#include <sys/syscall.h>
+#endif
+#ifdef HAVE_SYSCALL_H
+#include <syscall.h>
+#endif
+#include <dlfcn.h>
+
+#define LIBC_NAME "libc.so"
+
+struct uwrap_libc_fns {
+#ifdef HAVE_SETEUID
+       int (*_libc_seteuid)(uid_t euid);
+#endif
+#ifdef HAVE_SETREUID
+       int (*_libc_setreuid)(uid_t ruid, uid_t euid);
+#endif
+#ifdef HAVE_SETREUID
+       int (*_libc_setresuid)(uid_t ruid, uid_t euid, uid_t suid);
+#endif
+       uid_t (*_libc_geteuid)(void);
+#ifdef HAVE_SETEGID
+       int (*_libc_setegid)(uid_t egid);
+#endif
+#ifdef HAVE_SETREGID
+       int (*_libc_setregid)(uid_t rgid, uid_t egid);
+#endif
+#ifdef HAVE_SETREGID
+       int (*_libc_setresgid)(uid_t rgid, uid_t egid, uid_t sgid);
+#endif
+       gid_t (*_libc_getegid)(void);
+       int (*_libc_getgroups)(int size, gid_t list[]);
+       int (*_libc_setgroups)(size_t size, const gid_t *list);
+       uid_t (*_libc_getuid)(void);
+       gid_t (*_libc_getgid)(void);
+#ifdef HAVE_SYSCALL
+       long int (*_libc_syscall)(long int sysno, ...);
+#endif
+};
+
+/*
+ * We keep the virtualised euid/egid/groups information here
+ */
+struct uwrap {
+       struct {
+               void *handle;
+               struct uwrap_libc_fns fns;
+       } libc;
+       bool initialised;
+       bool enabled;
+       uid_t myuid;
+       uid_t euid;
+       uid_t mygid;
+       gid_t egid;
+       gid_t *groups;
+       int ngroups;
+};
+
+static struct uwrap uwrap;
+
+static void *uwrap_libc_fn(struct uwrap *u, const char *fn_name)
+{
+       void *func;
+
+       if (u->libc.handle == NULL) {
+               return NULL;
+       }
+
+       func = dlsym(u->libc.handle, fn_name);
+       if (func == NULL) {
+               printf("Failed to find %s in %s: %s\n",
+                               fn_name, LIBC_NAME, dlerror());
+               exit(-1);
+       }
+
+       return func;
+}
+
+static void uwrap_libc_init(struct uwrap *u)
+{
+       unsigned int i;
+
+       for (u->libc.handle = NULL, i = 10; u->libc.handle == NULL; i--) {
+               char soname[256] = {0};
+
+               snprintf(soname, sizeof(soname), "%s.%u", LIBC_NAME, i);
+               u->libc.handle = dlopen(soname, RTLD_LAZY);
+       }
+
+       if (u->libc.handle == NULL) {
+               printf("Failed to dlopen %s.%u: %s\n", LIBC_NAME, i, dlerror());
+               exit(-1);
+       }
+
+#ifdef HAVE_SETEUID
+       *(void **) (&u->libc.fns._libc_seteuid) = uwrap_libc_fn(u, "seteuid");
+#endif
+#ifdef HAVE_SETREUID
+       *(void **) (&u->libc.fns._libc_setreuid) = uwrap_libc_fn(u, "setreuid");
+#endif
+#ifdef HAVE_SETRESUID
+       *(void **) (&u->libc.fns._libc_setresuid) = uwrap_libc_fn(u, "setresuid");
+#endif
+       *(void **) (&u->libc.fns._libc_geteuid) = uwrap_libc_fn(u, "geteuid");
+#ifdef HAVE_SETEGID
+       *(void **) (&u->libc.fns._libc_setegid) = uwrap_libc_fn(u, "setegid");
+#endif
+#ifdef HAVE_SETREGID
+       *(void **) (&u->libc.fns._libc_setregid) = uwrap_libc_fn(u, "setregid");
+#endif
+#ifdef HAVE_SETRESGID
+       *(void **) (&u->libc.fns._libc_setresgid) = uwrap_libc_fn(u, "setresgid");
+#endif
+       *(void **) (&u->libc.fns._libc_getegid) = uwrap_libc_fn(u, "getegid");
+       *(void **) (&u->libc.fns._libc_getgroups) = uwrap_libc_fn(u, "getgroups");
+       *(void **) (&u->libc.fns._libc_setgroups) = uwrap_libc_fn(u, "setgroups");
+       *(void **) (&u->libc.fns._libc_getuid) = uwrap_libc_fn(u, "getuid");
+       *(void **) (&u->libc.fns._libc_getgid) = uwrap_libc_fn(u, "getgid");
+#ifdef HAVE_SYSCALL
+       *(void **) (&u->libc.fns._libc_syscall) = uwrap_libc_fn(u, "syscall");
+#endif
+}
+
+static void uwrap_init(void)
+{
+       if (uwrap.initialised) {
+               return;
+       }
+
+       uwrap_libc_init(&uwrap);
+
+       uwrap.initialised = true;
+       uwrap.enabled = false;
+
+       if (getenv("UID_WRAPPER")) {
+               uwrap.enabled = true;
+               /* put us in one group */
+               uwrap.myuid = uwrap.euid = geteuid();
+               uwrap.mygid = uwrap.egid = getegid();
+               uwrap.ngroups = 1;
+               uwrap.groups = malloc(sizeof(gid_t) * uwrap.ngroups);
+               uwrap.groups[0] = 0;
+       }
+}
+
+static int uwrap_enabled(void)
+{
+       uwrap_init();
+
+       return uwrap.enabled ? 1 : 0;
+}
+
+#ifdef HAVE_SETEUID
+int seteuid(uid_t euid)
+{
+       if (!uwrap_enabled()) {
+               return uwrap.libc.fns._libc_seteuid(euid);
+       }
+
+       /* assume for now that the ruid stays as root */
+       if (euid == 0) {
+               uwrap.euid = uwrap.myuid;
+       } else {
+               uwrap.euid = euid;
+       }
+
+       return 0;
+}
+#endif
+
+#ifdef HAVE_SETREUID
+int setreuid(uid_t ruid, uid_t euid)
+{
+       if (!uwrap_enabled()) {
+               return uwrap.libc.fns._libc_setreuid(ruid, euid);
+       }
+       /* assume for now that the ruid stays as root */
+       if (euid == 0) {
+               uwrap.euid = uwrap.myuid;
+       } else {
+               uwrap.euid = euid;
+       }
+
+       return 0;
+}
+#endif
+
+#ifdef HAVE_SETRESUID
+int setresuid(uid_t ruid, uid_t euid, uid_t suid)
+{
+       if (!uwrap_enabled()) {
+               return uwrap.libc.fns._libc_setresuid(ruid, euid, suid);
+       }
+
+       /* assume for now that the ruid stays as root */
+       if (euid == 0) {
+               uwrap.euid = uwrap.myuid;
+       } else {
+               uwrap.euid = euid;
+       }
+
+       return 0;
+}
+#endif
+
+uid_t geteuid(void)
+{
+       if (!uwrap_enabled()) {
+               return uwrap.libc.fns._libc_geteuid();
+       }
+
+       return uwrap.euid;
+}
+
+#ifdef HAVE_SETEGID
+int setegid(gid_t egid)
+{
+       if (!uwrap_enabled()) {
+               return uwrap.libc.fns._libc_setegid(egid);
+       }
+
+       /* assume for now that the ruid stays as root */
+       if (egid == 0) {
+               uwrap.egid = uwrap.mygid;
+       } else {
+               uwrap.egid = egid;
+       }
+
+       return 0;
+}
+#endif
+
+#ifdef HAVE_SETREGID
+int setregid(gid_t rgid, gid_t egid)
+{
+       if (!uwrap_enabled()) {
+               return uwrap.libc.fns._libc_setregid(rgid, egid);
+       }
+
+       /* assume for now that the ruid stays as root */
+       if (egid == 0) {
+               uwrap.egid = uwrap.mygid;
+       } else {
+               uwrap.egid = egid;
+       }
+
+       return 0;
+}
+#endif
+
+#ifdef HAVE_SETRESGID
+int setresgid(gid_t rgid, gid_t egid, gid_t sgid)
+{
+       if (!uwrap_enabled()) {
+               return uwrap.libc.fns._libc_setregid(rgid, egid, sgid);
+       }
+
+       /* assume for now that the ruid stays as root */
+       if (egid == 0) {
+               uwrap.egid = uwrap.mygid;
+       } else {
+               uwrap.egid = egid;
+       }
+       return 0;
+}
+#endif
+
+uid_t getegid(void)
+{
+       if (!uwrap_enabled()) {
+               return uwrap.libc.fns._libc_getegid();
+       }
+
+       return uwrap.egid;
+}
+
+int setgroups(size_t size, const gid_t *list)
+{
+       if (!uwrap_enabled()) {
+               return uwrap.libc.fns._libc_setgroups(size, list);
+       }
+
+       free(uwrap.groups);
+       uwrap.groups = NULL;
+       uwrap.ngroups = 0;
+
+       if (size != 0) {
+               uwrap.groups = malloc(sizeof(gid_t) * size);
+               if (uwrap.groups == NULL) {
+                       errno = ENOMEM;
+                       return -1;
+               }
+               uwrap.ngroups = size;
+               memcpy(uwrap.groups, list, size*sizeof(gid_t));
+       }
+
+       return 0;
+}
+
+int getgroups(int size, gid_t *list)
+{
+       int ngroups;
+
+       if (!uwrap_enabled()) {
+               return uwrap.libc.fns._libc_getgroups(size, list);
+       }
+
+       ngroups = uwrap.ngroups;
+
+       if (size > ngroups) {
+               size = ngroups;
+       }
+       if (size == 0) {
+               return ngroups;
+       }
+       if (size < ngroups) {
+               errno = EINVAL;
+               return -1;
+       }
+       memcpy(list, uwrap.groups, size*sizeof(gid_t));
+
+       return ngroups;
+}
+
+uid_t getuid(void)
+{
+       if (!uwrap_enabled()) {
+               return uwrap.libc.fns._libc_getuid();
+       }
+
+       /* we don't simulate ruid changing */
+       return 0;
+}
+
+gid_t getgid(void)
+{
+       if (!uwrap_enabled()) {
+               return uwrap.libc.fns._libc_getgid();
+       }
+
+       /* we don't simulate rgid changing */
+       return 0;
+}
+
+#ifdef HAVE_SYSCALL
+long int syscall (long int sysno, ...)
+{
+       long int rc;
+       va_list va;
+
+       va_start(va, sysno);
+
+       switch (sysno) {
+               case SYS_setreuid:
+                       rc = setreuid(va_arg(va, uid_t),
+                                     va_arg(va, uid_t));
+                       break;
+               case SYS_setresuid:
+                       rc = setresuid(va_arg(va, uid_t),
+                                      va_arg(va, uid_t),
+                                      va_arg(va, uid_t));
+                       break;
+               default:
+                       rc = uwrap.libc.fns._libc_syscall(sysno, va);
+                       break;
+       }
+       va_end(va);
+
+       return rc;
+}
+#endif
diff --git a/src/uid_wrapper.h b/src/uid_wrapper.h
new file mode 100644 (file)
index 0000000..21d0795
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+   Copyright (C) Andrew Tridgell 2009
+   Copyright (c) 2011      Andreas Schneider <asn@samba.org>
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+   
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+   
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __UID_WRAPPER_H__
+#define __UID_WRAPPER_H__
+#ifndef uwrap_enabled
+
+int uwrap_enabled(void);
+int uwrap_seteuid(uid_t euid);
+int uwrap_setreuid(uid_t reuid, uid_t euid);
+int uwrap_setresuid(uid_t reuid, uid_t euid, uid_t suid);
+uid_t uwrap_geteuid(void);
+int uwrap_setegid(gid_t egid);
+int uwrap_setregid(gid_t rgid, gid_t egid);
+int uwrap_setresgid(gid_t regid, gid_t egid, gid_t sgid);
+uid_t uwrap_getegid(void);
+int uwrap_setgroups(size_t size, const gid_t *list);
+int uwrap_getgroups(int size, gid_t *list);
+uid_t uwrap_getuid(void);
+gid_t uwrap_getgid(void);
+
+#ifdef UID_WRAPPER_REPLACE
+
+#ifdef samba_seteuid
+#undef samba_seteuid
+#endif
+#define samba_seteuid  uwrap_seteuid
+
+#ifdef samba_setreuid
+#undef samba_setreuid
+#endif
+#define samba_setreuid uwrap_setreuid
+
+#ifdef samba_setresuid
+#undef samba_setresuid
+#endif
+#define samba_setresuid        uwrap_setresuid
+
+#ifdef samba_setegid
+#undef samba_setegid
+#endif
+#define samba_setegid  uwrap_setegid
+
+#ifdef samba_setregid
+#undef samba_setregid
+#endif
+#define samba_setregid uwrap_setregid
+
+#ifdef samba_setresgid
+#undef samba_setresgid
+#endif
+#define samba_setresgid        uwrap_setresgid
+
+#ifdef geteuid
+#undef geteuid
+#endif
+#define geteuid        uwrap_geteuid
+
+#ifdef getegid
+#undef getegid
+#endif
+#define getegid        uwrap_getegid
+
+#ifdef samba_setgroups
+#undef samba_setgroups
+#endif
+#define samba_setgroups uwrap_setgroups
+
+#ifdef getgroups
+#undef getgroups
+#endif
+#define getgroups uwrap_getgroups
+
+#ifdef getuid
+#undef getuid
+#endif
+#define getuid uwrap_getuid
+
+#ifdef getgid
+#undef getgid
+#endif
+#define getgid uwrap_getgid
+
+#endif /* UID_WRAPPER_REPLACE */
+#endif /* uwrap_enabled */
+#endif /* __UID_WRAPPER_H__ */