113498266Sopenharmony_ci#***************************************************************************
213498266Sopenharmony_ci#                                  _   _ ____  _
313498266Sopenharmony_ci#  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci#                             / __| | | | |_) | |
513498266Sopenharmony_ci#                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci#                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci#
813498266Sopenharmony_ci# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci#
1013498266Sopenharmony_ci# This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci# you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci# are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci#
1413498266Sopenharmony_ci# You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci# copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci# furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci#
1813498266Sopenharmony_ci# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci# KIND, either express or implied.
2013498266Sopenharmony_ci#
2113498266Sopenharmony_ci# SPDX-License-Identifier: curl
2213498266Sopenharmony_ci#
2313498266Sopenharmony_ci###########################################################################
2413498266Sopenharmony_ci# - Try to find the GSS Kerberos library
2513498266Sopenharmony_ci# Once done this will define
2613498266Sopenharmony_ci#
2713498266Sopenharmony_ci#  GSS_ROOT_DIR - Set this variable to the root installation of GSS
2813498266Sopenharmony_ci#
2913498266Sopenharmony_ci# Read-Only variables:
3013498266Sopenharmony_ci#  GSS_FOUND - system has the Heimdal library
3113498266Sopenharmony_ci#  GSS_FLAVOUR - "MIT" or "Heimdal" if anything found.
3213498266Sopenharmony_ci#  GSS_INCLUDE_DIR - the Heimdal include directory
3313498266Sopenharmony_ci#  GSS_LIBRARIES - The libraries needed to use GSS
3413498266Sopenharmony_ci#  GSS_LINK_DIRECTORIES - Directories to add to linker search path
3513498266Sopenharmony_ci#  GSS_LINKER_FLAGS - Additional linker flags
3613498266Sopenharmony_ci#  GSS_COMPILER_FLAGS - Additional compiler flags
3713498266Sopenharmony_ci#  GSS_VERSION - This is set to version advertised by pkg-config or read from manifest.
3813498266Sopenharmony_ci#                In case the library is found but no version info available it'll be set to "unknown"
3913498266Sopenharmony_ci
4013498266Sopenharmony_ciset(_MIT_MODNAME mit-krb5-gssapi)
4113498266Sopenharmony_ciset(_HEIMDAL_MODNAME heimdal-gssapi)
4213498266Sopenharmony_ci
4313498266Sopenharmony_ciinclude(CheckIncludeFile)
4413498266Sopenharmony_ciinclude(CheckIncludeFiles)
4513498266Sopenharmony_ciinclude(CheckTypeSize)
4613498266Sopenharmony_ci
4713498266Sopenharmony_ciset(_GSS_ROOT_HINTS
4813498266Sopenharmony_ci    "${GSS_ROOT_DIR}"
4913498266Sopenharmony_ci    "$ENV{GSS_ROOT_DIR}"
5013498266Sopenharmony_ci)
5113498266Sopenharmony_ci
5213498266Sopenharmony_ci# try to find library using system pkg-config if user didn't specify root dir
5313498266Sopenharmony_ciif(NOT GSS_ROOT_DIR AND NOT "$ENV{GSS_ROOT_DIR}")
5413498266Sopenharmony_ci  if(UNIX)
5513498266Sopenharmony_ci    find_package(PkgConfig QUIET)
5613498266Sopenharmony_ci    pkg_search_module(_GSS_PKG ${_MIT_MODNAME} ${_HEIMDAL_MODNAME})
5713498266Sopenharmony_ci    list(APPEND _GSS_ROOT_HINTS "${_GSS_PKG_PREFIX}")
5813498266Sopenharmony_ci  elseif(WIN32)
5913498266Sopenharmony_ci    list(APPEND _GSS_ROOT_HINTS "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MIT\\Kerberos;InstallDir]")
6013498266Sopenharmony_ci  endif()
6113498266Sopenharmony_ciendif()
6213498266Sopenharmony_ci
6313498266Sopenharmony_ciif(NOT _GSS_FOUND) #not found by pkg-config. Let's take more traditional approach.
6413498266Sopenharmony_ci  find_file(_GSS_CONFIGURE_SCRIPT
6513498266Sopenharmony_ci      NAMES
6613498266Sopenharmony_ci          "krb5-config"
6713498266Sopenharmony_ci      HINTS
6813498266Sopenharmony_ci          ${_GSS_ROOT_HINTS}
6913498266Sopenharmony_ci      PATH_SUFFIXES
7013498266Sopenharmony_ci          bin
7113498266Sopenharmony_ci      NO_CMAKE_PATH
7213498266Sopenharmony_ci      NO_CMAKE_ENVIRONMENT_PATH
7313498266Sopenharmony_ci  )
7413498266Sopenharmony_ci
7513498266Sopenharmony_ci  # if not found in user-supplied directories, maybe system knows better
7613498266Sopenharmony_ci  find_file(_GSS_CONFIGURE_SCRIPT
7713498266Sopenharmony_ci      NAMES
7813498266Sopenharmony_ci          "krb5-config"
7913498266Sopenharmony_ci      PATH_SUFFIXES
8013498266Sopenharmony_ci          bin
8113498266Sopenharmony_ci  )
8213498266Sopenharmony_ci
8313498266Sopenharmony_ci  if(_GSS_CONFIGURE_SCRIPT)
8413498266Sopenharmony_ci    execute_process(
8513498266Sopenharmony_ci          COMMAND ${_GSS_CONFIGURE_SCRIPT} "--cflags" "gssapi"
8613498266Sopenharmony_ci          OUTPUT_VARIABLE _GSS_CFLAGS
8713498266Sopenharmony_ci          RESULT_VARIABLE _GSS_CONFIGURE_FAILED
8813498266Sopenharmony_ci          OUTPUT_STRIP_TRAILING_WHITESPACE
8913498266Sopenharmony_ci      )
9013498266Sopenharmony_ci    message(STATUS "CFLAGS: ${_GSS_CFLAGS}")
9113498266Sopenharmony_ci    if(NOT _GSS_CONFIGURE_FAILED) # 0 means success
9213498266Sopenharmony_ci      # should also work in an odd case when multiple directories are given
9313498266Sopenharmony_ci      string(STRIP "${_GSS_CFLAGS}" _GSS_CFLAGS)
9413498266Sopenharmony_ci      string(REGEX REPLACE " +-I" ";" _GSS_CFLAGS "${_GSS_CFLAGS}")
9513498266Sopenharmony_ci      string(REGEX REPLACE " +-([^I][^ \\t;]*)" ";-\\1" _GSS_CFLAGS "${_GSS_CFLAGS}")
9613498266Sopenharmony_ci
9713498266Sopenharmony_ci      foreach(_flag ${_GSS_CFLAGS})
9813498266Sopenharmony_ci        if(_flag MATCHES "^-I.*")
9913498266Sopenharmony_ci          string(REGEX REPLACE "^-I" "" _val "${_flag}")
10013498266Sopenharmony_ci          list(APPEND _GSS_INCLUDE_DIR "${_val}")
10113498266Sopenharmony_ci        else()
10213498266Sopenharmony_ci          list(APPEND _GSS_COMPILER_FLAGS "${_flag}")
10313498266Sopenharmony_ci        endif()
10413498266Sopenharmony_ci      endforeach()
10513498266Sopenharmony_ci    endif()
10613498266Sopenharmony_ci
10713498266Sopenharmony_ci    execute_process(
10813498266Sopenharmony_ci        COMMAND ${_GSS_CONFIGURE_SCRIPT} "--libs" "gssapi"
10913498266Sopenharmony_ci        OUTPUT_VARIABLE _GSS_LIB_FLAGS
11013498266Sopenharmony_ci        RESULT_VARIABLE _GSS_CONFIGURE_FAILED
11113498266Sopenharmony_ci        OUTPUT_STRIP_TRAILING_WHITESPACE
11213498266Sopenharmony_ci    )
11313498266Sopenharmony_ci    message(STATUS "LDFLAGS: ${_GSS_LIB_FLAGS}")
11413498266Sopenharmony_ci
11513498266Sopenharmony_ci    if(NOT _GSS_CONFIGURE_FAILED) # 0 means success
11613498266Sopenharmony_ci      # this script gives us libraries and link directories. Blah. We have to deal with it.
11713498266Sopenharmony_ci      string(STRIP "${_GSS_LIB_FLAGS}" _GSS_LIB_FLAGS)
11813498266Sopenharmony_ci      string(REGEX REPLACE " +-(L|l)" ";-\\1" _GSS_LIB_FLAGS "${_GSS_LIB_FLAGS}")
11913498266Sopenharmony_ci      string(REGEX REPLACE " +-([^Ll][^ \\t;]*)" ";-\\1" _GSS_LIB_FLAGS "${_GSS_LIB_FLAGS}")
12013498266Sopenharmony_ci
12113498266Sopenharmony_ci      foreach(_flag ${_GSS_LIB_FLAGS})
12213498266Sopenharmony_ci        if(_flag MATCHES "^-l.*")
12313498266Sopenharmony_ci          string(REGEX REPLACE "^-l" "" _val "${_flag}")
12413498266Sopenharmony_ci          list(APPEND _GSS_LIBRARIES "${_val}")
12513498266Sopenharmony_ci        elseif(_flag MATCHES "^-L.*")
12613498266Sopenharmony_ci          string(REGEX REPLACE "^-L" "" _val "${_flag}")
12713498266Sopenharmony_ci          list(APPEND _GSS_LINK_DIRECTORIES "${_val}")
12813498266Sopenharmony_ci        else()
12913498266Sopenharmony_ci          list(APPEND _GSS_LINKER_FLAGS "${_flag}")
13013498266Sopenharmony_ci        endif()
13113498266Sopenharmony_ci      endforeach()
13213498266Sopenharmony_ci    endif()
13313498266Sopenharmony_ci
13413498266Sopenharmony_ci    execute_process(
13513498266Sopenharmony_ci        COMMAND ${_GSS_CONFIGURE_SCRIPT} "--version"
13613498266Sopenharmony_ci        OUTPUT_VARIABLE _GSS_VERSION
13713498266Sopenharmony_ci        RESULT_VARIABLE _GSS_CONFIGURE_FAILED
13813498266Sopenharmony_ci        OUTPUT_STRIP_TRAILING_WHITESPACE
13913498266Sopenharmony_ci    )
14013498266Sopenharmony_ci
14113498266Sopenharmony_ci    # older versions may not have the "--version" parameter. In this case we just don't care.
14213498266Sopenharmony_ci    if(_GSS_CONFIGURE_FAILED)
14313498266Sopenharmony_ci      set(_GSS_VERSION 0)
14413498266Sopenharmony_ci    endif()
14513498266Sopenharmony_ci
14613498266Sopenharmony_ci    execute_process(
14713498266Sopenharmony_ci        COMMAND ${_GSS_CONFIGURE_SCRIPT} "--vendor"
14813498266Sopenharmony_ci        OUTPUT_VARIABLE _GSS_VENDOR
14913498266Sopenharmony_ci        RESULT_VARIABLE _GSS_CONFIGURE_FAILED
15013498266Sopenharmony_ci        OUTPUT_STRIP_TRAILING_WHITESPACE
15113498266Sopenharmony_ci    )
15213498266Sopenharmony_ci
15313498266Sopenharmony_ci    # older versions may not have the "--vendor" parameter. In this case we just don't care.
15413498266Sopenharmony_ci    if(_GSS_CONFIGURE_FAILED)
15513498266Sopenharmony_ci      set(GSS_FLAVOUR "Heimdal") # most probably, shouldn't really matter
15613498266Sopenharmony_ci    else()
15713498266Sopenharmony_ci      if(_GSS_VENDOR MATCHES ".*H|heimdal.*")
15813498266Sopenharmony_ci        set(GSS_FLAVOUR "Heimdal")
15913498266Sopenharmony_ci      else()
16013498266Sopenharmony_ci        set(GSS_FLAVOUR "MIT")
16113498266Sopenharmony_ci      endif()
16213498266Sopenharmony_ci    endif()
16313498266Sopenharmony_ci
16413498266Sopenharmony_ci  else() # either there is no config script or we are on a platform that doesn't provide one (Windows?)
16513498266Sopenharmony_ci
16613498266Sopenharmony_ci    find_path(_GSS_INCLUDE_DIR
16713498266Sopenharmony_ci        NAMES
16813498266Sopenharmony_ci            "gssapi/gssapi.h"
16913498266Sopenharmony_ci        HINTS
17013498266Sopenharmony_ci            ${_GSS_ROOT_HINTS}
17113498266Sopenharmony_ci        PATH_SUFFIXES
17213498266Sopenharmony_ci            include
17313498266Sopenharmony_ci            inc
17413498266Sopenharmony_ci    )
17513498266Sopenharmony_ci
17613498266Sopenharmony_ci    if(_GSS_INCLUDE_DIR) #jay, we've found something
17713498266Sopenharmony_ci      set(CMAKE_REQUIRED_INCLUDES "${_GSS_INCLUDE_DIR}")
17813498266Sopenharmony_ci      check_include_files( "gssapi/gssapi_generic.h;gssapi/gssapi_krb5.h" _GSS_HAVE_MIT_HEADERS)
17913498266Sopenharmony_ci
18013498266Sopenharmony_ci      if(_GSS_HAVE_MIT_HEADERS)
18113498266Sopenharmony_ci        set(GSS_FLAVOUR "MIT")
18213498266Sopenharmony_ci      else()
18313498266Sopenharmony_ci        # prevent compiling the header - just check if we can include it
18413498266Sopenharmony_ci        list(APPEND CMAKE_REQUIRED_DEFINITIONS -D__ROKEN_H__)
18513498266Sopenharmony_ci        check_include_file( "roken.h" _GSS_HAVE_ROKEN_H)
18613498266Sopenharmony_ci
18713498266Sopenharmony_ci        check_include_file( "heimdal/roken.h" _GSS_HAVE_HEIMDAL_ROKEN_H)
18813498266Sopenharmony_ci        if(_GSS_HAVE_ROKEN_H OR _GSS_HAVE_HEIMDAL_ROKEN_H)
18913498266Sopenharmony_ci          set(GSS_FLAVOUR "Heimdal")
19013498266Sopenharmony_ci        endif()
19113498266Sopenharmony_ci        list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D__ROKEN_H__)
19213498266Sopenharmony_ci      endif()
19313498266Sopenharmony_ci    else()
19413498266Sopenharmony_ci      # I'm not convinced if this is the right way but this is what autotools do at the moment
19513498266Sopenharmony_ci      find_path(_GSS_INCLUDE_DIR
19613498266Sopenharmony_ci          NAMES
19713498266Sopenharmony_ci              "gssapi.h"
19813498266Sopenharmony_ci          HINTS
19913498266Sopenharmony_ci              ${_GSS_ROOT_HINTS}
20013498266Sopenharmony_ci          PATH_SUFFIXES
20113498266Sopenharmony_ci              include
20213498266Sopenharmony_ci              inc
20313498266Sopenharmony_ci      )
20413498266Sopenharmony_ci
20513498266Sopenharmony_ci      if(_GSS_INCLUDE_DIR)
20613498266Sopenharmony_ci        set(GSS_FLAVOUR "Heimdal")
20713498266Sopenharmony_ci      endif()
20813498266Sopenharmony_ci    endif()
20913498266Sopenharmony_ci
21013498266Sopenharmony_ci    # if we have headers, check if we can link libraries
21113498266Sopenharmony_ci    if(GSS_FLAVOUR)
21213498266Sopenharmony_ci      set(_GSS_LIBDIR_SUFFIXES "")
21313498266Sopenharmony_ci      set(_GSS_LIBDIR_HINTS ${_GSS_ROOT_HINTS})
21413498266Sopenharmony_ci      get_filename_component(_GSS_CALCULATED_POTENTIAL_ROOT "${_GSS_INCLUDE_DIR}" PATH)
21513498266Sopenharmony_ci      list(APPEND _GSS_LIBDIR_HINTS ${_GSS_CALCULATED_POTENTIAL_ROOT})
21613498266Sopenharmony_ci
21713498266Sopenharmony_ci      if(WIN32)
21813498266Sopenharmony_ci        if(CMAKE_SIZEOF_VOID_P EQUAL 8)
21913498266Sopenharmony_ci          list(APPEND _GSS_LIBDIR_SUFFIXES "lib/AMD64")
22013498266Sopenharmony_ci          if(GSS_FLAVOUR STREQUAL "MIT")
22113498266Sopenharmony_ci            set(_GSS_LIBNAME "gssapi64")
22213498266Sopenharmony_ci          else()
22313498266Sopenharmony_ci            set(_GSS_LIBNAME "libgssapi")
22413498266Sopenharmony_ci          endif()
22513498266Sopenharmony_ci        else()
22613498266Sopenharmony_ci          list(APPEND _GSS_LIBDIR_SUFFIXES "lib/i386")
22713498266Sopenharmony_ci          if(GSS_FLAVOUR STREQUAL "MIT")
22813498266Sopenharmony_ci            set(_GSS_LIBNAME "gssapi32")
22913498266Sopenharmony_ci          else()
23013498266Sopenharmony_ci            set(_GSS_LIBNAME "libgssapi")
23113498266Sopenharmony_ci          endif()
23213498266Sopenharmony_ci        endif()
23313498266Sopenharmony_ci      else()
23413498266Sopenharmony_ci        list(APPEND _GSS_LIBDIR_SUFFIXES "lib;lib64") # those suffixes are not checked for HINTS
23513498266Sopenharmony_ci        if(GSS_FLAVOUR STREQUAL "MIT")
23613498266Sopenharmony_ci          set(_GSS_LIBNAME "gssapi_krb5")
23713498266Sopenharmony_ci        else()
23813498266Sopenharmony_ci          set(_GSS_LIBNAME "gssapi")
23913498266Sopenharmony_ci        endif()
24013498266Sopenharmony_ci      endif()
24113498266Sopenharmony_ci
24213498266Sopenharmony_ci      find_library(_GSS_LIBRARIES
24313498266Sopenharmony_ci          NAMES
24413498266Sopenharmony_ci              ${_GSS_LIBNAME}
24513498266Sopenharmony_ci          HINTS
24613498266Sopenharmony_ci              ${_GSS_LIBDIR_HINTS}
24713498266Sopenharmony_ci          PATH_SUFFIXES
24813498266Sopenharmony_ci              ${_GSS_LIBDIR_SUFFIXES}
24913498266Sopenharmony_ci      )
25013498266Sopenharmony_ci
25113498266Sopenharmony_ci    endif()
25213498266Sopenharmony_ci  endif()
25313498266Sopenharmony_cielse()
25413498266Sopenharmony_ci  if(_GSS_PKG_${_MIT_MODNAME}_VERSION)
25513498266Sopenharmony_ci    set(GSS_FLAVOUR "MIT")
25613498266Sopenharmony_ci    set(_GSS_VERSION _GSS_PKG_${_MIT_MODNAME}_VERSION)
25713498266Sopenharmony_ci  else()
25813498266Sopenharmony_ci    set(GSS_FLAVOUR "Heimdal")
25913498266Sopenharmony_ci    set(_GSS_VERSION _GSS_PKG_${_MIT_HEIMDAL}_VERSION)
26013498266Sopenharmony_ci  endif()
26113498266Sopenharmony_ciendif()
26213498266Sopenharmony_ci
26313498266Sopenharmony_ciset(GSS_INCLUDE_DIR ${_GSS_INCLUDE_DIR})
26413498266Sopenharmony_ciset(GSS_LIBRARIES ${_GSS_LIBRARIES})
26513498266Sopenharmony_ciset(GSS_LINK_DIRECTORIES ${_GSS_LINK_DIRECTORIES})
26613498266Sopenharmony_ciset(GSS_LINKER_FLAGS ${_GSS_LINKER_FLAGS})
26713498266Sopenharmony_ciset(GSS_COMPILER_FLAGS ${_GSS_COMPILER_FLAGS})
26813498266Sopenharmony_ciset(GSS_VERSION ${_GSS_VERSION})
26913498266Sopenharmony_ci
27013498266Sopenharmony_ciif(GSS_FLAVOUR)
27113498266Sopenharmony_ci  if(NOT GSS_VERSION AND GSS_FLAVOUR STREQUAL "Heimdal")
27213498266Sopenharmony_ci    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
27313498266Sopenharmony_ci      set(HEIMDAL_MANIFEST_FILE "Heimdal.Application.amd64.manifest")
27413498266Sopenharmony_ci    else()
27513498266Sopenharmony_ci      set(HEIMDAL_MANIFEST_FILE "Heimdal.Application.x86.manifest")
27613498266Sopenharmony_ci    endif()
27713498266Sopenharmony_ci
27813498266Sopenharmony_ci    if(EXISTS "${GSS_INCLUDE_DIR}/${HEIMDAL_MANIFEST_FILE}")
27913498266Sopenharmony_ci      file(STRINGS "${GSS_INCLUDE_DIR}/${HEIMDAL_MANIFEST_FILE}" heimdal_version_str
28013498266Sopenharmony_ci           REGEX "^.*version=\"[0-9]\\.[^\"]+\".*$")
28113498266Sopenharmony_ci
28213498266Sopenharmony_ci      string(REGEX MATCH "[0-9]\\.[^\"]+"
28313498266Sopenharmony_ci             GSS_VERSION "${heimdal_version_str}")
28413498266Sopenharmony_ci    endif()
28513498266Sopenharmony_ci
28613498266Sopenharmony_ci    if(NOT GSS_VERSION)
28713498266Sopenharmony_ci      set(GSS_VERSION "Heimdal Unknown")
28813498266Sopenharmony_ci    endif()
28913498266Sopenharmony_ci  elseif(NOT GSS_VERSION AND GSS_FLAVOUR STREQUAL "MIT")
29013498266Sopenharmony_ci    get_filename_component(_MIT_VERSION "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MIT\\Kerberos\\SDK\\CurrentVersion;VersionString]" NAME CACHE)
29113498266Sopenharmony_ci    if(WIN32 AND _MIT_VERSION)
29213498266Sopenharmony_ci      set(GSS_VERSION "${_MIT_VERSION}")
29313498266Sopenharmony_ci    else()
29413498266Sopenharmony_ci      set(GSS_VERSION "MIT Unknown")
29513498266Sopenharmony_ci    endif()
29613498266Sopenharmony_ci  endif()
29713498266Sopenharmony_ciendif()
29813498266Sopenharmony_ci
29913498266Sopenharmony_ciinclude(FindPackageHandleStandardArgs)
30013498266Sopenharmony_ci
30113498266Sopenharmony_ciset(_GSS_REQUIRED_VARS GSS_LIBRARIES GSS_FLAVOUR)
30213498266Sopenharmony_ci
30313498266Sopenharmony_cifind_package_handle_standard_args(GSS
30413498266Sopenharmony_ci    REQUIRED_VARS
30513498266Sopenharmony_ci        ${_GSS_REQUIRED_VARS}
30613498266Sopenharmony_ci    VERSION_VAR
30713498266Sopenharmony_ci        GSS_VERSION
30813498266Sopenharmony_ci    FAIL_MESSAGE
30913498266Sopenharmony_ci        "Could NOT find GSS, try to set the path to GSS root folder in the system variable GSS_ROOT_DIR"
31013498266Sopenharmony_ci)
31113498266Sopenharmony_ci
31213498266Sopenharmony_cimark_as_advanced(GSS_INCLUDE_DIR GSS_LIBRARIES)
313