9 Microsoft SQL Server Driver : Authentication

Authentication
Authentication protects the identity of the user so that user credentials cannot be intercepted by malicious hackers when transmitted over the network. See “Authentication” for an overview.
The SQL Server driver supports the following methods of authentication:
■
SQL Server authentication, or user ID/password authentication, authenticates the user to the database using a database user name and password provided by the application.
■
Kerberos authentication uses Kerberos, a trusted third-party authentication service, to verify user identities. Kerberos authentication can take advantage of the user name and password maintained by the operating system to authenticate users to the database or use another set of user credentials specified by the application.
This method requires knowledge of how to configure your Kerberos environment and supports Windows Active Directory Kerberos only.
■
NOTE: NTLM authentication requires Microsoft SQL Server 2000, Service Pack 3 or higher.
The AuthenticationMethod connection property controls which authentication mechanism the driver uses when establishing connections. See “Using the AuthenticationMethod Property” for information about setting the value for this property.
Using the AuthenticationMethod Property
The AuthenticationMethod connection property controls which authentication mechanism the driver uses when establishing connections. When AuthenticationMethod=auto, the driver uses SQL Server authentication, Kerberos authentication, or NTLM authentication when establishing a connection based on the following criteria:
■
If a user ID and password is specified, the driver uses SQL Server authentication when establishing a connection. The User property provides the user ID. The Password property provides the password.
■
If a user ID and password is not specified and the driver is not running on a Windows platform, the driver uses Kerberos authentication when establishing a connection.
■
If a user ID and password is not specified and the driver is running on a Windows platform, the driver uses NTLM authentication when establishing a connection if the driver can load the DLL required for NTLM authentication. If the driver cannot load the DLL, the driver uses Kerberos authentication.
When AuthenticationMethod=kerberos, the driver uses Kerberos authentication when establishing a connection. The driver ignores any values specified by the User property and Password properties.
When AuthenticationMethod=ntlm, the driver uses NTLM authentication when establishing a connection if the driver can load the DLL required for NTLM authentication. If the driver cannot load the DLL, the driver throws an exception. The driver ignores any values specified by the User and Password properties. This value is supported only for Windows clients.
When AuthenticationMethod=ntlmjava, the driver uses NTLM when establishing a connection, but requires that the User and Password properties be specified. If they are unspecified, the driver throws an exception. In addition, the driver requires the name of the domain server that administers the database server. You can specify it using the Domain property. If the Domain property is unspecified, the driver attempts to determine the domain server name from the User property. If no domain is specified, the driver throws an exception. This value is supported for Windows and UNIX/Linux clients.
When AuthenticationMethod=userIdPassword (default), the driver uses SQL Server authentication when establishing a connection. The User property provides the user ID. The Password property provides the password. If a user ID is not specified, the driver throws an exception.
Configuring SQL Server Authentication
1
Set the AuthenticationMethod property to auto or userIdPassword (default). See “Using the AuthenticationMethod Property” for more information about setting a value for this property.
2
3
Configuring Kerberos Authentication
This section provides requirements and instructions for configuring Kerberos authentication for the SQL Server driver.
Product Requirements
Verify that your environment meets the requirements listed in Table 9-2 before you configure the driver for Kerberos authentication.
Microsoft SQL Server database server
The database server must be administered by the same domain controller that administers the client and must be running one of the following databases:
The Kerberos server is the machine where the user IDs for authentication are administered. The Kerberos server is also the location of the Kerberos KDC.
Network authentication must be provided by Windows Active Directory on one of the following operating systems:
The client must be administered by the same domain controller that administers the database server. In addition, J2SE 5 or higher must be installed.
Configuring the Driver
During installation, the following files required for Kerberos authentication are installed in the /lib subdirectory of your product installation directory:
■
krb5.conf is a Kerberos configuration file containing values for the Kerberos realm and the KDC name for that realm. A generic file is installed that you must modify for your environment.
■
JDBCDriverLogin.conf file is a configuration file that specifies which Java Authentication and Authorization Service (JAAS) login module to use for Kerberos authentication. This file is configured to load automatically unless the java.security.auth.login.config system property is set to load another configuration file. You can modify this file, but the driver must be able to find the JDBC_DRIVER_01 entry in this file or another specified login configuration file to configure the JAAS login module. Refer to your JDK documentation for information about setting configuration options in this file
To configure the driver:
1
Set the driver’s AuthenticationMethod property to auto (default) or kerberos. See “Using the AuthenticationMethod Property” for more information about setting a value for this property.
2
Modify the krb5.conf file to contain your Kerberos realm name and the KDC name for that Kerberos realm. Modify the krb5.conf file by editing the file with a text editor or by specifying the system properties, java.security.krb5.realm and java.security.krb5.kdc.
NOTE: In Windows Active Directory, the Kerberos realm name is the Windows domain name and the KDC name is the Windows domain controller name.
For example, if your Kerberos realm name is XYZ.COM and your KDC name is kdc1, your krb5.conf file would look like this:
[libdefaults]
   default_realm = XYZ.COM
 
[realms]
   XYZ.COM = {
   kdc = kdc1
   }
If the krb5.conf file does not contain a valid Kerberos realm and KDC name, the following exception is thrown:
Message:[DataDirect][SQLServer JDBC Driver]Could not establish a connection using integrated security: No valid credentials provided
The krb5.conf file is configured to load automatically unless the java.security.krb5.conf system property is set to point to another Kerberos configuration file.
3
See the following URL for more information about configuring and testing your environment for Windows authentication with the SQL Server driver:
http://www.datadirect.com/resources/jdbc/windows-auth/index.html
Specifying User Credentials for Kerberos Authentication (Delegation of Credentials)
By default, the SQL Server driver takes advantage of the user name and password maintained by the operating system to authenticate users to the database. By allowing the database to share the user name and password used for the operating system, users with a valid operating system account can log into the database without supplying a user name and password.
Many application servers or Web servers act on behalf of the client user logged on the machine on which the application is running, rather than the server user. If you want the driver to use a set of user credentials other than the operating system user name and password, include code in your application to obtain and pass a javax.security.auth.Subject used for authentication as shown in the following example.
import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;
import java.sql.*;
 
// The following code creates a javax.security.auth.Subject instance
// used for authentication. Refer to the Java Authentication
// and Authorization Service documentation for details on using a
// LoginContext to obtain a Subject.
 
LoginContext lc = null;
Subject subject = null;
 
try {
 
   lc = new LoginContext("JaasSample", new TextCallbackHandler());
   lc.login();
   subject = lc.getSubject();
}
catch (Exception le) {
    ... // display login error
}
 
// This application passes the javax.security.auth.Subject
// to the driver by executing the driver code as the subject
 
Connection con =
   (Connection) Subject.doAs(subject, new PrivilegedExceptionAction() {
   
   public Object run() {
   
      Connection con = null;
      try {
 
         Class.forName("com.ddtek.jdbc.sqlserver.SQLServerDriver");
         String url = "jdbc:datadirect:sqlserver://myServer:1433";
         con = DriverManager.getConnection(url);
       }
      catch (Exception except) {
 
      ... //log the connection error
            return null;
         }
      
         return con;
    }
});
 
//  This application now has a connection that was authenticated with
//  the subject. The application can now use the connection.
Statement   stmt = con.createStatement();
String      sql = "SELECT * FROM employee";
ResultSet   rs = stmt.executeQuery(sql);
 
... // do something with the results
Obtaining a Kerberos Ticket Granting Ticket
To use Kerberos authentication, the application user first must obtain a Kerberos Ticket Granting Ticket (TGT) from the Kerberos server. The Kerberos server verifies the identity of the user and controls access to services using the credentials contained in the TGT.
If the application uses Kerberos authentication from a Windows client, the application user is not required to log onto the Kerberos server and explicitly obtain a TGT. Windows Active Directory automatically obtains a TGT for the user.
If an application uses Kerberos authentication from a UNIX or Linux client, the user must log onto the Kerberos server using the kinit command to obtain a TGT. For example, the following command requests a TGT from the server with a lifetime of 10 hours, which is renewable for 5 days:
kinit -l 10h -r 5d user
where user is the application user.
Refer to your Kerberos documentation for more information about using the kinit command and obtaining TGTs for users.
Configuring NTLM Authentication
How you configure the driver for NTLM depends on your client’s operating system:
■
●
●
■
Product Requirements
Verify that your environment meets the requirements listed in Table 9-3 before you configure your environment for NTLM authentication.
Database server
Configuring the Driver: Not Specifying User Credentials (Windows Only)
To use NTLM authentication without specifying user credentials, the driver must load an NTLM authentication DLL. The product provides the following NTLM authentication DLLs (where xx is a 2-digit number):
■
DDJDBCAuthxx.dll (32-bit)
■
DDJDBC64Authxx.dll (Itanium 64-bit)
■
DDJDBCx64Authxx.dll (AMD64 and Intel EM64T 64-bit)
The DLLs are located in the install_dir/lib directory (where install_dir is your product installation directory). If the application using NTLM authentication is running in a 32-bit JVM, the driver automatically uses DDJDBCAuthxx.dll. Similarly, if the application is running in a 64-bit JVM, the driver uses DDJDBC64Authxx.dll or DDJDBCx64Authxx.dll.
To configure the driver:
1
Set the AuthenticationMethod property to auto (default) or ntlm. See “Using the AuthenticationMethod Property” for more information about setting a value for this property.
2
By default, the driver looks for the NTLM authentication DLLs in a directory on the Windows system path defined by the PATH environment variable. If you install the driver in a directory that is not on the Windows system path, perform one of the following actions to ensure the driver can load the DLLs:
■
Add the install_dir/lib directory to the Windows system path, where install_dir is your product installation directory.
■
Copy the NTLM authentication DLLs from install_dir/lib to a directory that is on the Windows system path, where install_dir is your product installation directory.
■
Set the LoadLibraryPath property to specify the location of the NTLM authentication DLLs. For example, if you install the driver in a directory named "DataDirect" that is not on the Windows system path, you can use the LoadLibraryPath property to specify the directory containing the NTLM authentication DLLs:
jdbc:datadirect:sqlserver://server3:1521;DatabaseName=test;
LoadLibraryPath=C:\DataDirect\lib;User=test;Password=secret
3
If using NTLM authentication with a Security Manager on a Java 2 Platform, security permissions must be granted to allow the driver to establish connections. See “Permissions for Establishing Connections” for an example.
Configuring the Driver: Specifying User Credentials
1
Set the AuthenticationMethod property to ntlmjava. See “Using the AuthenticationMethod Property” for more information about setting a value for this property.
2
NOTE: Alternatively, you can set the domain server name using the User property.
3
4
5
If using NTLM authentication with a Security Manager on a Java 2 Platform, security permissions must be granted to allow the driver to establish connections. See “Permissions for Establishing Connections” for an example.