Skip to content

SQL Server Windows Authentication (Kerberos) fails when the server's realm differs from the Mac's default_realm #1947

Closed

Description

@Seha16

What happened

SQL Server Windows Authentication (Kerberos) fails with the generic error

Failed to connect to sqlserver.example.com:1433: Check host, port, credentials, and TLS settings

whenever the SQL Server's Kerberos realm is different from the Mac's default_realm, even though the setup is valid and works from other clients:

  • The user holds a valid TGT in their own realm (CLIENT.REALM).
  • A cross-realm trust exists (CLIENT.REALM ⇄ RESOURCE.REALM, via capaths).
  • The same server connects fine from DataGrip / the MS JDBC driver using authenticationScheme=javaKerberos.

Root cause

FreeTDS builds the service principal without a realm — MSSQLSvc/<host>:<port>. On macOS, Heimdal assigns the client's default_realm to that unqualified principal and does not follow a cross-realm referral, so the KDC returns KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN (-1765328377) and the login packet is rejected.

DataGrip/Java succeed because Java's Kerberos resolves the SPN's realm from [domain_realm] (and/or follows AD referrals), so it requests the ticket from the realm where the SPN actually lives.

Evidence from a FreeTDS protocol dump (TDSDUMP):

login.c:1042: using GSS authentication
gssapi.c:299:  using kerberos name MSSQLSvc/sqlserver.example.com:1433
gssapi.c:417:  gss_init_sec_context: min_stat 2529638919 "Unknown error -1765328377"
gssapi.c:457:  gss_init_sec_context: GSS_S_FAILURE
login.c:670:   login packet rejected

Confirmed with Heimdal directly (the app never enters the picture):

# unqualified — Heimdal uses default_realm (CLIENT.REALM) and fails, no referral:
$ kgetcred MSSQLSvc/sqlserver.example.com:1433
kgetcred: krb5_get_creds: Server (MSSQLSvc/sqlserver.example.com:[email protected]) unknown

# realm named explicitly — succeeds via the trust:
$ kgetcred MSSQLSvc/sqlserver.example.com:[email protected]   # OK

[domain_realm] does not help, because a service principal parsed from a string gets default_realm; the host→realm mapping is only applied to host-based names, not to the MSSQLSvc/host:port string FreeTDS constructs.

Steps to reproduce

  1. Mac with default_realm = CLIENT.REALM in /etc/krb5.conf.
  2. A SQL Server whose SPN (MSSQLSvc/<host>:1433) is registered in a different realm (RESOURCE.REALM) with a cross-realm trust in place.
  3. kinit [email protected].
  4. Create a SQL Server connection, Windows Authentication (Kerberos), principal [email protected], connect by hostname.
  5. Connect → fails with the generic "Check host, port, credentials, and TLS settings".

Expected behavior

Connect succeeds (as it does from DataGrip/JDBC in the same environment). At minimum, the error should name the Kerberos cause (server principal unknown / wrong realm) rather than the generic message.

Proposed solution

  1. Per-connection Kerberos realm / SPN realm option. Let the user specify the realm so the SPN is built as MSSQLSvc/host:port@REALM. FreeTDS supports a realm setting for this. This avoids forcing users to change the machine-wide default_realm.
  2. Surface the real error. A login-phase failure currently shows only the generic "Check host, port, credentials, and TLS settings". The underlying FreeTDS/GSS reason (here S_PRINCIPAL_UNKNOWN) never reaches the UI, which made this very hard to diagnose. Propagating the FreeTDS message/GSS minor status would help a lot.

Workaround

Set default_realm to the SQL Server's realm in /etc/krb5.conf (downside: bare kinit then needs the user's own realm named explicitly, and other services in the client realm need their realm named). Verified this makes both kgetcred and TablePro succeed.

Environment

  • TablePro: 0.59.0
  • macOS: 26.5.2, Apple Silicon (M1)
  • Database: SQL Server (FreeTDS db-lib driver), Windows Authentication (Kerberos)
  • Kerberos: macOS Heimdal, cross-realm trust between client and resource realms

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions