If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Sybase > logon trigger

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-28-10, 16:19
medamadhu medamadhu is offline
Registered User
 
Join Date: Sep 2010
Posts: 12
logon trigger

Hi, Can some one help me in writing this trigger.

The requirement is to ensure that functional accounts can only connect from approved clients, based on their IP subnet. I have to create a logon trigger to compare the client IP with a predefined table of approved subnets, and reject the logon if it’s not in the list.

Thanks
Reply With Quote
  #2 (permalink)  
Old 09-29-10, 04:21
Martijnvs Martijnvs is offline
Registered User
 
Join Date: Jan 2004
Location: The Hague/Utrecht, NL
Posts: 415
Lookup login-triggers in the Sybase-documentation, write a piece of code that verifies the ip and call function syb_quit() if it's not approved.
__________________
I'm not crazy, I'm an aeroplane!
Reply With Quote
  #3 (permalink)  
Old 10-04-10, 11:22
medamadhu medamadhu is offline
Registered User
 
Join Date: Sep 2010
Posts: 12
Hi Martijnvs,

Thanks for your reply.I am actually oracle guy and i have created a trigger for logon with respect to oracle perspective.I want the same trigger formatted in Sybase.Your help is appreciated.

CREATE OR REPLACE TRIGGER trig_after_logon
--- Logon trigger which accepts connections from users with valid ip addresses
--- Check if IP address belongs to a Valid subnet which can be found from the lookup table.

AFTER LOGON ON DATABASE
DECLARE
vusername varchar2(30);
vip varchar2(20);
vos_user varchar2(20);
BEGIN
vusername:=SYS_CONTEXT('USERENV','SESSION_USER');
vip:=SYS_CONTEXT('USERENV','IP_ADDRESS');
vos_user:=SYS_CONTEXT('USERENV','OS_USER');


IF vos_user <> 'oracle' THEN -- check if OS account is not oracle
--IF vusername in ('TESTVRU, 'TEST') THEN
IF check_user(vusername) THEN -- check username from a lookup table
IF NOT check_subnet(vip) THEN -- check ip/subnet from a lookup table
RAISE_APPLICATION_ERROR(-20003,'You are not allowed to connect to the database');
END IF;
END IF;
END IF;
END;
/
Reply With Quote
  #4 (permalink)  
Old 10-07-10, 04:30
Martijnvs Martijnvs is offline
Registered User
 
Join Date: Jan 2004
Location: The Hague/Utrecht, NL
Posts: 415
Try this:
Code:
--create table to store approved clients
use master
go
create table approved_clients (
ipadress char(15)
}
go
--insert approved ipadresses in table approved_clients
<...>

--create the logintrigger

create procedure sp__checkip 
as 
begin 

--declare variable
declare @ipaddress char(15)
--lookup ipaddress of current process
select @ipaddress = ipaddr  
from master..sysprocesses where spid = @@spid 
 
--check if ipaddress is in the approved-list
if not exists (select 1 from approved_clients where ipaddress = @ipaddress)
begin
--if not: terminate connection
    select syb_quit()
end
return 0
 
end
-- connect logintrigger to <loginname>
sp_modifylogin <loginname>, 'login script', master..sp__checkip
There is o custom error displayed, the connection is just dropped.
__________________
I'm not crazy, I'm an aeroplane!
Reply With Quote
  #5 (permalink)  
Old 10-19-10, 10:42
medamadhu medamadhu is offline
Registered User
 
Join Date: Sep 2010
Posts: 12
Martijnvs,

Thanks for the script.I have tested this and works fine.
Reply With Quote
  #6 (permalink)  
Old 10-20-10, 02:40
Martijnvs Martijnvs is offline
Registered User
 
Join Date: Jan 2004
Location: The Hague/Utrecht, NL
Posts: 415
Glad to hear that .
__________________
I'm not crazy, I'm an aeroplane!
Reply With Quote
  #7 (permalink)  
Old 11-03-10, 10:54
medamadhu medamadhu is offline
Registered User
 
Join Date: Sep 2010
Posts: 12
Hi Martijnvs,

I need to add something to this.

Here instead of ipaddress i need to take into consideration the subnet which is the first 3 octets of ip address.How can i modify this procedure.
and also have to capture the audit information of the users who will be logged out of the database by the use of the trigger.How can i do this?

I am oracle guy recently shifted to Sybase. Your help is highly appreciated.

please advise..

Thanks
Meda

Last edited by medamadhu; 11-03-10 at 11:04.
Reply With Quote
  #8 (permalink)  
Old 11-05-10, 05:09
Martijnvs Martijnvs is offline
Registered User
 
Join Date: Jan 2004
Location: The Hague/Utrecht, NL
Posts: 415
So if I understand it correctly, you want to 1) verify both ip-adress AND subnet? Or just the subnet? Sybase does not register the subnet of a process, only the ip-adress.

And 2) you want to log which user has been kicked out because he wasn't on the verified list?
__________________
I'm not crazy, I'm an aeroplane!
Reply With Quote
  #9 (permalink)  
Old 11-05-10, 08:55
medamadhu medamadhu is offline
Registered User
 
Join Date: Sep 2010
Posts: 12
Hi Martijnvs,

According to the requirement it requires only subnet.Since subnet is the first 3 octets of I.P,i think this below query gives me the desired out put

--lookup ipaddress of current process

select @ip=ipaddress from master..sysprocesses where spid=@@spid
select @ip=substring(@ip,1,char_length(@ip)-charindex(".",reverse(@ip)))
select @ip

and now my script looks like this:

create procedure sp__checkipcf
@user_info varchar(20)
as
begin
--declare variable
declare @ip varchar(15)

if exists(select * from user_info where username= @user_info)
print 'user exists'
else
print 'doesnot exists'


--lookup ipaddress of current process

select @ip=ipaddress from master..sysprocesses where spid=@@spid
select @ip=substring(@ip,1,char_length(@ip)-charindex(".",reverse(@ip)))
select @ip


--check if ipaddress is in the approved-list
if not exists (select 1 from approved_clients where ipaddress = @ip)
begin
--if not: Login not on approved list - terminate
select syb_quit()
end
return 0

end
go

Let me know if i am wrong.

And 2) you want to log which user has been kicked out because he wasn't on the verified list?
yes..ur right,I want to know the info which user has been kicked out...

Thanks
Meda

Last edited by medamadhu; 11-08-10 at 10:48.
Reply With Quote
  #10 (permalink)  
Old 11-08-10, 12:24
medamadhu medamadhu is offline
Registered User
 
Join Date: Sep 2010
Posts: 12
sumerp inserted

Able to fix it..thanks

Hi Martijnvs,

Now Iam getting the below error when i tried to use the login i have created

=>isql -Urobo -Probo123 -Shappy_uat_HR -w400

Msg 1638, Level 16, State 1:
Server 'happy_uat_HR':
Execution of login script 'sp__checkipcf' failed with last error = 201. See server errorlog for details.
CT-LIBRARY error:
ct_results(): network packet layer: internal net library error: Net-Library operation terminated due to disconnect


I used the following script to execute this:


create procedure sp__checkipcf
@user_info varchar(20)
as
begin
--declare variable
declare @ip varchar(12)

if exists(select * from user_info where username= @user_info)
print 'user exists'
else
print 'doesnot exists'


--lookup ipaddress of current process

select @ip = ipaddr from master..sysprocesses where spid=@@spid
select @ip= substring(@ip,1,char_length(@ip)-charindex(".",reverse(@ip)))
select @ip

--check if ipaddress is in the approved-list
if not exists (select 1 from approved_clients where ipaddress = @ip)
begin
--if not: Login not on approved list - terminate
select syb_quit()
end
return 0

end
go


Thanks,
Meda

Last edited by medamadhu; 11-08-10 at 13:32.
Reply With Quote
  #11 (permalink)  
Old 11-10-10, 02:47
Martijnvs Martijnvs is offline
Registered User
 
Join Date: Jan 2004
Location: The Hague/Utrecht, NL
Posts: 415
To log the users that have been kicked, create an extra table with columns to match the information you want to log. Before select syb_quit(), do an insert of values into that table.
I hope this is enough info, if not, let me know, and I'll try to explain in more detail.
__________________
I'm not crazy, I'm an aeroplane!
Reply With Quote
  #12 (permalink)  
Old 11-12-10, 08:45
medamadhu medamadhu is offline
Registered User
 
Join Date: Sep 2010
Posts: 12
Hi Martijnvs,

Sorry for my late reply.Actually we are building automated health checks to monitor for failures so we can quickly respond to any production issues.

Do you have something similar for Sybase, where we can view the audit log?

Thank,
Meda

Last edited by medamadhu; 11-12-10 at 11:27.
Reply With Quote
  #13 (permalink)  
Old 11-15-10, 03:28
Martijnvs Martijnvs is offline
Registered User
 
Join Date: Jan 2004
Location: The Hague/Utrecht, NL
Posts: 415
You can try to implement Sybase Auditing, but that's quite a big task. I've never done it, so I can't help you with that.
To extend the logintrigger I previously posted with logging is not too hard. What do you want to log? ip-adress, date, time? Anything else?
__________________
I'm not crazy, I'm an aeroplane!
Reply With Quote
  #14 (permalink)  
Old 11-15-10, 14:25
medamadhu medamadhu is offline
Registered User
 
Join Date: Sep 2010
Posts: 12
Hi Martijnvs,

I realised sybase auditing is bit tedious process,i wouldn't go with it.I want to log the info of the loginname who has been kicked out and also dbname,ipadd,date,time etc.As per ur previous replies i understand that i need to create a table with above columns and insert into the stored procedure before syb_quit()...?Can you explain this in more detail
Reply With Quote
  #15 (permalink)  
Old 11-16-10, 04:07
Martijnvs Martijnvs is offline
Registered User
 
Join Date: Jan 2004
Location: The Hague/Utrecht, NL
Posts: 415
I've taken the first version I posted and added the extra logging:
Quote:
--create table to store approved clients
use master
go
create table approved_clients (
ipadress char(15)
}
go
--create loggingtable
create table client_logging(
loginname varchar(25),
source_ip char(15),
kickeddate datetime
}

--insert approved ipadresses in table approved_clients
<...>

--create the logintrigger

create procedure sp__checkip
as
begin

--declare variable
declare @ipaddress char(15)

--lookup ipaddress of current process
select @ipaddress = ipaddr
from master..sysprocesses where spid = @@spid

--check if ipaddress is in the approved-list
if not exists (select 1 from approved_clients where ipaddress = @ipaddress)
begin
--if not:
--log data into table
insert into client_logging(loginname, source_ip, kickeddate)
values (suser_name() , @ipaddress, getdate() )

--and terminate connection
select syb_quit()
end
return 0

end
-- connect logintrigger to <loginname>
sp_modifylogin <loginname>, 'login script', master..sp__checkip
I did not include the dbname in the logging since no other db than the logins default database has been used. Before any other db can be used, the trigger has already fired and killed the process.
I hope you can use this .
__________________
I'm not crazy, I'm an aeroplane!

Last edited by Martijnvs; 11-16-10 at 04:13.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On