# Problem with pam\_ruby + postgres-pr (stack level too deep)

**URL:** <https://www.ruby-forum.com/t/problem-with-pam-ruby-postgres-pr-stack-level-too-deep/83228>\
**Category:** Ruby\
**Created:** [January 24, 2007, 2:42pm UTC](https://www.ruby-forum.com/t/problem-with-pam-ruby-postgres-pr-stack-level-too-deep/83228 "2007-01-24T14:42:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![prhlava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71e660/32.png) [@prhlava](https://www.ruby-forum.com/u/prhlava)\
**Post date:** [January 24, 2007, 2:42pm UTC](https://www.ruby-forum.com/t/problem-with-pam-ruby-postgres-pr-stack-level-too-deep/83228/1 "2007-01-24T14:42:16Z")

</div>

(code attached at the end)

i am developing a pam module for blocking multiple failed attempts from  
the same host. with sqlite3 i had run into database locking problem so i  
am trying to switch the database to postgresql (using postgres-pr 0.4.0  
driver with DBI).

the error in the auth.log is:  
Jan 24 13:27:34 whirpool sshd[5771]: [pam\_ruby] exception:  
#\<SystemStackError: stack level too deep\>  
Jan 24 13:27:34 whirpool sshd[5771]: [pam\_ruby]  
["/usr/lib/ruby/1.8/DBD/Pg/Pg.rb:476:in `==='", "/usr/lib/ruby/1.8/DBD/Pg/Pg.rb:476:in`load\_type\_map’",  
“/usr/lib/ruby/1.8/DBD/Pg/Pg.rb:467:in `each'", "/usr/lib/ruby/1.8/DBD/Pg/Pg.rb:467:in`load\_type\_map’”,  
“/usr/lib/ruby/1.8/DBD/Pg/Pg.rb:105:in `initialize'", "/usr/lib/ruby/1.8/DBD/Pg/Pg.rb:55:in`new’”,  
“/usr/lib/ruby/1.8/DBD/Pg/Pg.rb:55:in `connect'", "/usr/lib/ruby/1.8/dbi/dbi.rb:584:in`connect’”,  
“/usr/lib/ruby/1.8/dbi/dbi.rb:384:in `connect'", "/lib/security/ruby/hostblock.rb:76", "/lib/security/ruby/hostblock.rb:71:in`call’”]  
Jan 24 13:27:34 whirpool sshd[5771]: fatal: PAM: pam\_setcred(): System  
error

the :authenticate part works as it should but the :setcred part fails  
consistently on DBI.connect(…).

the DBI with postgres-pr works ok from irb.

any ideas how to progress on this?

vladimir

ps: the code:

["/usr/local/lib/site\_ruby/1.8",  
“/usr/local/lib/site\_ruby/1.8/i386-linux”, “/usr/local/lib/site\_ruby”,  
“/usr/lib/ruby/1.8”, “/usr/lib/ruby/1.8/i386-linux”,"."].each {|x|  
$:.push(x)}

require ‘rubygems’  
require ‘dbi’

# how many failed attemts to tolerate

MAX\_ATTEMPTS = 10

# database descriptor for the “host” table - passed to DBI.connect

DATABASE = ‘DBI:pg:hostblock:localhost’

# where is the system command for logging to files

LOGGER = ‘/usr/bin/logger’

PAM.dispatch(:authenticate) {|pamh, flags, args|  
dbh = DBI.connect(DATABASE, ‘postgres’, ‘postgres’)  
result = dbh.execute(“select \* from host where name =  
‘#{pamh.get\_item(PAM::PAM\_RHOST)}’”)

rows = result.fetch\_all()

if rows.empty?  
dbh.do(“insert into host values (’#{pamh.get\_item(PAM::PAM\_RHOST)}’,  
0, 0)”)  
end

dbh.do(“update host set attempt\_count = attempt\_count + 1 where name =  
‘#{pamh.get\_item(PAM::PAM\_RHOST)}’”)  
result = dbh.execute(“select \* from host where name =  
‘#{pamh.get\_item(PAM::PAM\_RHOST)}’”)  
rows = result.fetch\_all()

if rows[0][1] \> MAX\_ATTEMPTS  
system("#{LOGGER} -p auth.debug -t hostblock.rb  
#{pamh.get\_item(PAM::PAM\_SERVICE)} blocking: #{rows[0][0]},  
#{rows[0][1]}")  
dbh.disconnect()  
raise PAM::PAM\_AUTH\_ERR  
end

dbh.disconnect()

}

PAM.dispatch(:acct\_mgmt) {|pamh, flags, args|  
system("#{LOGGER} -p auth.debug -t hostblock.rb :acct\_mgmt")  
if false  
raise PAM::PAM\_PERM\_DENIED  
end  
}

PAM.dispatch(:open\_session){|pamh, flags, args|  
system("#{LOGGER} -p auth.debug -t hostblock.rb :open\_session")  
# raise PAM::PAM\_SESSION\_ERR, “not available”  
}  
PAM.dispatch(:close\_session){|pamh, flags, args|  
raise PAM::PAM\_SESSION\_ERR, “not available”  
}  
PAM.dispatch(:chauthtok){|pamh, flags, args|  
raise PAM::PAM\_AUTHTOK\_ERR, “not available”  
}  
PAM.dispatch(:setcred){|pamh, flags, args|  
if flags == PAM::PAM\_DELETE\_CRED  
return PAM::PAM\_SUCCESS  
end  
begin  
dbh = DBI.connect(DATABASE, ‘postgres’, ‘postgres’)  
dbh.do(“update host set attempt\_count = 0 where name =  
‘#{pamh.get\_item(PAM::PAM\_RHOST)}’”)  
rescue DBI::DatabaseError  
system("#{LOGGER} -p auth.debug -t hostblock.rb :setcred  
DBI::DatabaseError")  
ensure  
dbh.disconnect()  
end  
}

# eof

---

<div class="post-metadata">

**Author:** ![prhlava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71e660/32.png) [@prhlava](https://www.ruby-forum.com/u/prhlava)\
**Post date:** [January 25, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/problem-with-pam-ruby-postgres-pr-stack-level-too-deep/83228/2 "2007-01-25T22:36:58Z")

</div>

> i am developing a pam module for blocking multiple failed attempts from  
> the same host. with sqlite3 i had run into database locking problem so i  
> am trying to switch the database to postgresql (using postgres-pr 0.4.0  
> driver with DBI).

swapped the driver (used ruby-postgres - postgres.so C binding) and used  
it without DBI.

not really sure why this one works…

vlad
