SQL Server Security concepts – logins, users, and principals¶
In this post I'd like to talk about some basic SQL Server security concepts. SQL Server has a less common design that can confuse users familiar with the security features of other software products, such as Microsoft Windows OS; in particular, the difference between logins and users is one that seems to invariably confuse most new SQL Server users.
The first important thing that needs to be understood about SQL Server security is that there are two security realms involved - the server and the database. The server realm encompasses multiple database realms. All work is done in the context of some database, but to get to do the work, one needs to first have access to the server and then to have access to the database.
Access to the server is granted via logins. There are two main categories of logins: SQL Server authenticated logins and Windows authenticated logins. I will usually refer to these using the shorter names of SQL logins and Windows logins. Windows authenticated logins can either be logins mapped to Windows users or logins mapped to Windows groups. So, to be able to connect to the server, one must have access via one of these types or logins - logins provide access to the server realm.
But logins are not enough, because work is usually done in a database and databases are separate realms. Access to databases is granted via users.
Users are mapped to logins and the mapping is expressed by the SID property of logins and users. A login maps to a user in a database if their SID values are identical. Depending on the type of login, we can therefore have a categorization of users that mimics the above categorization for logins; so, we have SQL users and Windows users and the latter category consists of users mapped to Windows user logins and of users mapped to Windows group logins.
Let's take a step back for a quick overview: a login provides access to the server and to further get access to a database, a user mapped to the login must exist in the database. It is important to know the difference between these two concepts of login and user and to not use these terms interchangeably.
Create a login using SSMS:¶
1.In Object Explorer, expand the folder of the server instance in which you want to create the new login.
2.Right-click the Security folder, point to New, and select Login….
3.In the Login – New dialog box, on the General page, enter the name of a user in the Login name box. Alternately, click Search… to open the Select User or Group dialog box.
If you click Search…:
a.Under Select this object type, click Object Types… to open the Object Types dialog box and select any or all of the following: Built-in security principals, Groups, and Users. Built-in security principals and Users are selected by default. When finished, click OK.
b.Under From this location, click Locations… to open the Locations dialog box and select one of the available server locations. When finished, click OK.
c.Under Enter the object name to select (examples), enter the user or group name that you want to find. For more information, see Select Users, Computers, or Groups Dialog Box.
d.Click Advanced… for more advanced search options. For more information, see Select Users, Computers, or Groups Dialog Box - Advanced Page.
e.Click OK.
4.To create a login based on a Windows principal, select Windows authentication. This is the default selection.
5.To create a login that is saved on a SQL Server database, select SQL Server authentication.
a.In the Password box, enter a password for the new user. Enter that password again into the Confirm Password box.
b.When changing an existing password, select Specify old password, and then type the old password in the Old password box.
c.To enforce password policy options for complexity and enforcement, select Enforce password policy. For more information, see Password Policy. This is a default option when SQL Server authentication is selected.
d.To enforce password policy options for expiration, select Enforce password expiration. Enforce password policy must be selected to enable this checkbox. This is a default option when SQL Server authentication is selected.
e.To force the user to create a new password after the first time the login is used, select User must change password at next login. Enforce password expiration must be selected to enable this checkbox. This is a default option when SQL Server authentication is selected.
6.To associate the login with a stand-alone security certificate, select Mapped to certificate and then select the name of an existing certificate from the list.
7.To associate the login with a stand-alone asymmetric key, select Mapped to asymmetric key to, and then select the name of an existing key from the list.
8.To associate the login with a security credential, select the Mapped to Credential check box, and then either select an existing credential from the list or click Add to create a new credential. To remove a mapping to a security credential from the login, select the credential from Mapped Credentials and click Remove. For more information about credentials in general, see Credentials (Database Engine).
9.From the Default database list, select a default database for the login. Master is the default for this option.
10.From the Default language list, select a default language for the login.
11.Click OK.
Additional Options:¶
The Login – New dialog box also offers options on four additional pages: Server Roles, User Mapping, Securables, and Status.
Server Roles:¶
The Server Roles page lists all possible roles that can be assigned to the new login. The following options are available:
bulkadmin check box¶
Members of the bulkadmin fixed server role can run the BULK INSERT statement.
dbcreator check box¶
Members of the dbcreator fixed server role can create, alter, drop, and restore any database.
diskadmin check box¶
Members of the diskadmin fixed server role can manage disk files.
processadmin check box¶
Members of the processadmin fixed server role can terminate processes running in an instance of the Database Engine.
public check box¶
All SQL Server users, groups, and roles belong to the public fixed server role by default.
securityadmin check box¶
Members of the securityadmin fixed server role manage logins and their properties. They can GRANT, DENY, and REVOKE server-level permissions. They can also GRANT, DENY, and REVOKE database-level permissions. Additionally, they can reset passwords for SQL Server logins.
serveradmin check box¶
Members of the serveradmin fixed server role can change server-wide configuration options and shut down the server.
setupadmin check box¶
Members of the setupadmin fixed server role can add and remove linked servers, and they can execute some system stored procedures.
sysadmin check box¶
Members of the sysadmin fixed server role can perform any activity in the Database Engine.
User Mapping¶
The User Mapping page lists all possible databases and the database role memberships on those databases that can be applied to the login. The databases selected determine the role memberships that are available for the login. The following options are available on this page:
Users mapped to this login¶
Select the databases that this login can access. When you select a database, its valid database roles are displayed in the Database role membership for: database_name pane.
Map¶
Allow the login to access the databases listed below.
Database¶
Lists the databases available on the server.
User¶
Specify a database user to map to the login. By default, the database user has the same name as the login.
Default Schema¶
Specifies the default schema of the user. When a user is first created, its default schema is dbo. It is possible to specify a default schema that does not yet exist. You cannot specify a default schema for a user that is mapped to a Windows group, a certificate, or an asymmetric key.
Guest account enabled for: database_name¶
Read-only attribute indicating whether the Guest account is enabled on the selected database. Use the Status page of the Login Properties dialog box of the Guest account to enable or disable the Guest account.
Database role membership for: database_name¶
Select the roles for the user in the specified database. All users are members of the public role in every database and cannot be removed. For more information about database roles, see Database-Level Roles.
Securables¶
The Securables page lists all possible securables and the permissions on those securables that can be granted to the login. The following options are available on this page:
Upper Grid¶
Contains one or more items for which permissions can be set. The columns that are displayed in the upper grid vary depending on the principal or securable.
To add items to the upper grid:
1.Click Search.
2.In the Add Objects dialog box, select one of the following options: Specific objects…, All objects of the types…, or The serverserver_name. Click OK.
Note
Selecting The serverserver_name automatically fills the upper grid with all of that servers' securable objects.
3.If you select Specific objects…:
a.In the Select Objects dialog box, under Select these object types, click Object Types….
b.In the Select Object Types dialog box, select any or all of the following object types: Endpoints, Logins, Servers, Availability Groups, and Server roles. Click OK.
c.Under Enter the object names to select (examples), click Browse….
d.In the Browse for Objects dialog box, select any of the available objects of the type that you selected in the Select Object Types dialog box, and then click OK.
e.In the Select Objects dialog box, click OK.
4.If you select All objects of the types…, in the Select Object Types dialog box, select any or all of the following object types: Endpoints, Logins, Servers, Availability Groups, and Server roles. Click OK.
Name¶
The name of each principal or securable that is added to the grid.
Type¶
Describes the type of each item.
Explicit Tab¶
Lists the possible permissions for the securable that are selected in the upper grid. Not all options are available for all explicit permissions.
Permissions¶
The name of the permission.
Grantor¶
The principal that granted the permission.
Grant¶
Select to grant this permission to the login. Clear to revoke this permission.
With Grant¶
Reflects the state of the WITH GRANT option for the listed permission. This box is read-only. To apply this permission, use the GRANT statement.
Deny¶
Select to deny this permission to the login. Clear to revoke this permission.
Status¶
The Status page lists some of the authentication and authorization options that can be configured on the selected SQL Server login.
The following options are available on this page:
Permission to connect to database engine¶
When you work with this setting, you should think of the selected login as a principal that can be granted or denied permission on a securable.
Select Grant to grant CONNECT SQL permission to the login. Select Deny to deny CONNECT SQL to the login.
Login¶
When you work with this setting, you should think of the selected login as a record in a table. Changes to the values listed here will be applied to the record.
A login that has been disabled continues to exist as a record. But if it tries to connect to SQL Server, the login will not be authenticated.
Select this option to enable or disable this login. This option uses the ALTER LOGIN statement with the either ENABLE or DISABLE option.
SQL Server Authentication¶
The check box Login is locked out is only available if the selected login connects using SQL Server Authentication and the login has been locked out. This setting is read-only. To unlock a login that is locked out, execute ALTER LOGIN with the UNLOCK option.