6

I have been required to configure a SQL Server to only allow one session per login. I have found a few references on creating login triggers in order to prevent a login from establishing more than 1 session, but I am wondering if there is some way to define this at a lower level, so that this session limit is the default, rather than having to define this in another login for each user?

I am seeing a lot of references to this topic in both "questions that may already have your answer" and "similar questions" here on stackoverflow, but so far have either not found or not understood a post that describes what I am trying to do. I have also seen a reference about Declarative Management Framework that allows you to configure SQL Server by policy I think.

I am going to keep on looking through articles here to try to learn this, but in the meantime... advice very much appreciated!

7
  • May be it is better to ask this question on http://security.stackexchange.com/
    – user3522371
    Dec 16, 2014 at 18:07
  • I’m assuming you mean Microsoft SQL Server.
    – Gumbo
    Dec 16, 2014 at 19:54
  • Do all of your users share the same login, are they individual logins or inherited from an AD group?
    – Brad D
    Dec 16, 2014 at 20:19
  • 1
    Yep, it's Microsoft SQL 2012 on Windows 2008 R2 Enterprise. Sorry for not including that. Also this server is not in production yet. A server is being set up in our hosting facility to be a back end of a web based inventory system. I came across this requirement designed to protect the server from DOS attacks by not allowing 1 login more than 1 session. This is where my search began.
    – Gabriel B.
    Dec 16, 2014 at 20:37
  • 1
    Along the lines of what Bacon Bits is saying, if they share one account (from web server to Database server) and the web server can't create multiple sessions you're probably going to be denying the service all together.
    – Brad D
    Dec 16, 2014 at 21:15

1 Answer 1

3

The example for a logon trigger in Books Online is pretty close to what I think you want, I've made a few changes to make it work for all logins.

-- Trigger must be created by a user with 'view server state' permission in order the trigger to have unrestricted access to sys.dm_exec_sessions.
create trigger connection_limit_trigger on all server with execute as self for logon
as
begin
    -- Check whether the caller is a SysAdmin.
    -- Note: The trigger is executing under an elevated security context so we must switch to the caller's context to test their SysAdmin status.
    declare @IsSysAdmin int
    execute as caller
    set @IsSysAdmin = isnull(is_srvrolemember ('sysadmin'), 0)
    revert

    -- If the user is not a SysAdmin and there are already too many non-dormant sessions for this login then 'rollback' the logon.
    -- Note: sys.dm_exec_sessions does not include an entry for this logon attempt.
    if ((@IsSysAdmin = 0) and ((select count(1) from sys.dm_exec_sessions where is_user_process = 1 and status <> 'Dormant' and original_login_name = original_login()) > 1))
    begin
        raiserror('This login has exceeded the maximum of 1 connections to this SQL Server.', 16, 1)
        rollback
    end
end

I've added in a check so the limit doesn't apply to SysAdmin logins and doesn't count dormant connection pool connections. A couple of things to note;

  • if a connection is not properly closed it can hang around in sys.dm_exec_sessions for a while, in this case the user won't be able to re-connect until the dead connection clears itself.
  • if you mess up a logon trigger you can lock yourself (and everyone else!) out of the SQL Server. See the logon trigger page for details of how to get back in :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.