# Net ldap problem?

**URL:** <https://www.ruby-forum.com/t/net-ldap-problem/194187>\
**Category:** Ruby\
**Created:** [August 31, 2010, 6:41pm UTC](https://www.ruby-forum.com/t/net-ldap-problem/194187 "2010-08-31T18:41:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![donpiotr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@donpiotr](https://www.ruby-forum.com/u/donpiotr)\
**Post date:** [August 31, 2010, 6:41pm UTC](https://www.ruby-forum.com/t/net-ldap-problem/194187/1 "2010-08-31T18:41:56Z")

</div>

Hi!  
I have server with Active Directory.  
I wanted to do somethink like thet:

def changelogonhours(name,ldap)  
filtr = Net::LDAP::Filter.eq(“cn”,name)  
ldap.search(:base =\> “dc=example,dc=local”, :filter =\> filtr){|ut|  
p ut[:dn]  
p ut[:logonhours]  
ut1,ut2 = Marshal.dump(ut[:logonhours]).split("\032")  
ut1 = ut1 + “\032”  
ut2, ut3 = ut2.split("\006:")  
ut3 = “\006:” + ut3  
ut2a=[]  
(0…6).each{|g|  
ut2a[g] = ut2[g_3…g_3+2]  
}  
tmp=ut2a[1]  
(2…6).each{|g|  
ut2a[g-1]=ut2a[g]  
}  
ut2a[6]=tmp  
ut\_new = ut1 + ut2a.join + ut3

# here begins my problem

ldap.replace\_attribute ut[:dn], :logonhours, Marshal.load(ut\_new)  
p ut[:logonhours]  
}  
end

Ruby responds:

[“CN=Tom B.,OU=Users,DC=exmple,DC=local”]  
["\300\377\017\000\000\000\000\000\000\000\000\b\000\000\004\000\000\001\000\000\000"]  
/var/lib/gems/1.8/gems/net-ldap-0.1.1/lib/net/ldap.rb:1502:in `modify': response missing or invalid (Net::LDAP::LdapError) from /var/lib/gems/1.8/gems/net-ldap-0.1.1/lib/net/ldap.rb:912:in`modify’  
from  
/var/lib/gems/1.8/gems/net-ldap-0.1.1/lib/net/ldap.rb:962:in  
`replace_attribute' from or.rb:24:in`trova\_orari’  
from  
/var/lib/gems/1.8/gems/net-ldap-0.1.1/lib/net/ldap.rb:630:in `search' from /var/lib/gems/1.8/gems/net-ldap-0.1.1/lib/net/ldap.rb:1426:in`search’  
from  
/var/lib/gems/1.8/gems/net-ldap-0.1.1/lib/net/ldap.rb:1385:in `loop' from /var/lib/gems/1.8/gems/net-ldap-0.1.1/lib/net/ldap.rb:1385:in`search’  
from  
/var/lib/gems/1.8/gems/net-ldap-0.1.1/lib/net/ldap.rb:628:in `search' from or.rb:6:in`trova\_orari’  
from or.rb:38  
from or.rb:37:in `each' from or.rb:37 from /var/lib/gems/1.8/gems/net-ldap-0.1.1/lib/net/ldap.rb:534:in`open’  
from or.rb:36

If you have some idea how to fix it i will be grateful.

Piotr

P.S. of course this code run after authentication where i use user that  
can change logon howers through “Active Directory Users and Computers”

ldap = Net::LDAP.new  
ldap.host = “192.168.0.2”  
ldap.port = 389  
ldap.auth “CN=User for Maintenance,OU=Other Users,DC=example,DC=local”,  
“pass”

if ldap.bind  
ldap.open{|ldap|  
changelogonhours(“Tom B.”,ldap)  
}  
else  
puts “nooooooo”  
end

---

<div class="post-metadata">

**Author:** ![donpiotr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@donpiotr](https://www.ruby-forum.com/u/donpiotr)\
**Post date:** [August 31, 2010, 7:34pm UTC](https://www.ruby-forum.com/t/net-ldap-problem/194187/2 "2010-08-31T19:34:55Z")

</div>

Is u2 getting set?

irb(main):124:0\> u1,u2 =  
“\300\377\017\000\000\000\000\000\000\000\000\b\000\000\004\000\000\001\000\000\000”.split(“\032”)  
=\>  
[“\300\377\017\000\000\000\000\000\000\000\000\b\000\000\004\000\000\001\000\000\000”]  
irb(main):125:0\> u1  
=\>  
“\300\377\017\000\000\000\000\000\000\000\000\b\000\000\004\000\000\001\000\000\000”  
irb(main):126:0\> u2  
=\> nil  
irb(main):127:0\>

* * *

From: Piotr T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Reply-To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Tue, 31 Aug 2010 11:47:57 -0500  
To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: net ldap problem?

Hi!  
I have server with Active Directory.  
I wanted to do somethink like thet:

def changelogonhours(name,ldap)  
filtr = Net::LDAP::Filter.eq(“cn”,name)  
ldap.search(:base =\> “dc=example,dc=local”, :filter =\> filtr){|ut|  
p ut[:dn]  
p ut[:logonhours]  
ut1,ut2 = Marshal.dump(ut[:logonhours]).split(“\032”)  
ut1 = ut1 + “\032”  
ut2, ut3 = ut2.split(“\006:”)  
ut3 = “\006:” + ut3  
ut2a=[]  
(0…6).each{|g|  
ut2a[g] = ut2[g_3…g_3+2]  
}  
tmp=ut2a[1]  
(2…6).each{|g|  
ut2a[g-1]=ut2a[g]  
}  
ut2a[6]=tmp  
ut\_new = ut1 + ut2a.join + ut3

# here begins my problem

ldap.replace\_attribute ut[:dn], :logonhours, Marshal.load(ut\_new)  
p ut[:logonhours]  
}  
end

Ruby responds:

[“CN=Tom B.,OU=Users,DC=exmple,DC=local”]  
[“\300\377\017\000\000\000\000\000\000\000\000\b\000\000\004\000\000\001\000\000\000”]  
/var/lib/gems/1.8/gems/net-ldap-0.1.1/lib/net/ldap.rb:1502:in `modify': response missing or invalid (Net::LDAP::LdapError) from /var/lib/gems/1.8/gems/net-ldap-0.1.1/lib/net/ldap.rb:912:in `modify’  
from  
/var/lib/gems/1.8/gems/net-ldap-0.1.1/lib/net/ldap.rb:962:in  
`replace_attribute' from or.rb:24:in `trova\_orari’  
from  
/var/lib/gems/1.8/gems/net-ldap-0.1.1/lib/net/ldap.rb:630:in `search' from /var/lib/gems/1.8/gems/net-ldap-0.1.1/lib/net/ldap.rb:1426:in `search’  
from  
/var/lib/gems/1.8/gems/net-ldap-0.1.1/lib/net/ldap.rb:1385:in `loop' from /var/lib/gems/1.8/gems/net-ldap-0.1.1/lib/net/ldap.rb:1385:in `search’  
from  
/var/lib/gems/1.8/gems/net-ldap-0.1.1/lib/net/ldap.rb:628:in `search' from or.rb:6:in `trova\_orari’  
from or.rb:38  
from or.rb:37:in `each' from or.rb:37 from /var/lib/gems/1.8/gems/net-ldap-0.1.1/lib/net/ldap.rb:534:in `open’  
from or.rb:36

If you have some idea how to fix it i will be grateful.

Piotr

P.S. of course this code run after authentication where i use user that  
can change logon howers through “Active Directory Users and Computers”

ldap = Net::LDAP.new  
ldap.host = “192.168.0.2”  
ldap.port = 389  
ldap.auth “CN=User for Maintenance,OU=Other Users,DC=example,DC=local”,  
“pass”

if ldap.bind  
ldap.open{|ldap|  
changelogonhours(“Tom B.”,ldap)  
}  
else  
puts “nooooooo”  
end

---

<div class="post-metadata">

**Author:** ![donpiotr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ec9cab/32.png) [@donpiotr](https://www.ruby-forum.com/u/donpiotr)\
**Post date:** [September 1, 2010, 9:53am UTC](https://www.ruby-forum.com/t/net-ldap-problem/194187/3 "2010-09-01T09:53:55Z")

</div>

Savard, Joseph wrote:

> Is u2 getting set?
> 
> irb(main):124:0\> u1,u2 =  
> “\300\377\017\000\000\000\000\000\000\000\000\b\000\000\004\000\000\001\000\000\000”.split(“\032”)  
> =\>  
> [“\300\377\017\000\000\000\000\000\000\000\000\b\000\000\004\000\000\001\000\000\000”]  
> irb(main):125:0\> u1  
> =\>  
> “\300\377\017\000\000\000\000\000\000\000\000\b\000\000\004\000\000\001\000\000\000”  
> irb(main):126:0\> u2  
> =\> nil  
> irb(main):127:0\>
> 
> * * *
> 
> From: Piotr T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Reply-To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Date: Tue, 31 Aug 2010 11:47:57 -0500  
> To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Subject: net ldap problem?

…

> ut1,ut2 = Marshal.dump(ut[:logonhours]).split(“\032”)  
> ut1 = ut1 + “\032”  
> ut2, ut3 = ut2.split(“\006:”)  
> ut3 = “\006:” + ut3  
> ut2a=[]  
> …

I didn’t see that i can convert ut[:logonhours]  
(Net::BER::BerIdentifiedArray) to string. Cuting it with “\032” make  
sense only with Marshal.dump.  
Now my code is more simple:

p ut[:logonhours]  
utstr = ut[:logonhours].to\_s  
utarr=[]  
(0…6).each{|g|  
utarr[g] = utstr[g_3…g_3+2]  
} # divide by day  
tmp=utarr[1]  
(2…6).each{|g|  
utarr[g-1]=utarr[g]  
}  
utarr[6]=tmp  
ldap.replace\_attribute ut[:dn], :logonhours, utarr.join.to\_ber  
p ut[:logonhours]

But i still have problem with replace\_attribute that give me the same  
error as written above
