perl - How can I handle "include" in LDIF using `Net::LDAP::LDIF->read_entry()`? - Stack Overflow

I wrote some simple perl script that creates an Net::LDAP::LDIFobject on an input file and then reads t

I wrote some simple perl script that creates an Net::LDAP::LDIFobject on an input file and then reads the contents using $ldif->read_entry(). After some processing I'm writing the entries again to STDOUT using $ldif->write_entry($entry).

The code works fine for "normal" LDIF files, but when processing an LDIF file that contains include, it fails like this:

First line of LDIF entry does not begin with "dn:" at ...

Is there a way "pass though" such includelines?

An example of such input would be:

dn: cn=schema,cn=config
objectClass: olcSchemaConfig
cn: schema

include: file:///etc/openldap/schema/core.ldif

include: file:///etc/openldap/schema/cosine.ldif

Such include files would contain DNs themselves, like this:

dn: cn=core,cn=schema,cn=config
objectClass: olcSchemaConfig
cn: core
#...

The module version I'm using is perl-ldap-0.65-1.3.1.x86_64 (perl 5.26), just in case.

I wrote some simple perl script that creates an Net::LDAP::LDIFobject on an input file and then reads the contents using $ldif->read_entry(). After some processing I'm writing the entries again to STDOUT using $ldif->write_entry($entry).

The code works fine for "normal" LDIF files, but when processing an LDIF file that contains include, it fails like this:

First line of LDIF entry does not begin with "dn:" at ...

Is there a way "pass though" such includelines?

An example of such input would be:

dn: cn=schema,cn=config
objectClass: olcSchemaConfig
cn: schema

include: file:///etc/openldap/schema/core.ldif

include: file:///etc/openldap/schema/cosine.ldif

Such include files would contain DNs themselves, like this:

dn: cn=core,cn=schema,cn=config
objectClass: olcSchemaConfig
cn: core
#...

The module version I'm using is perl-ldap-0.65-1.3.1.x86_64 (perl 5.26), just in case.

Share Improve this question asked Mar 25 at 11:32 U. WindlU. Windl 4,46535 silver badges63 bronze badges 3
  • It doesn't look like that module processes include directives. You will have to do it yourself – Botje Commented Mar 25 at 12:01
  • I don't want to process include direktives; I want to pass them on to the output unchanged. – U. Windl Commented Mar 25 at 12:22
  • Don't think the module supports that, it needs a dn: line for writing entries. Copy the include: lines yourself and remove them from the input? – Botje Commented Mar 25 at 12:35
Add a comment  | 

1 Answer 1

Reset to default 0

Thinking about the problem I had the idea to "pre-convert" the includes in LDIF first, then process it, and finally "post-convert" the includes to the original syntax.

However my initial idea to make the includes comments failed, because comments are not copied to the output when using read_entry() and write_entry().

Then I played with an ugly idea (having no more sophisticated idea): What happens if I prefix "dn: " to the include statements?

Interestingly that was parsed correctly by read_entry() and copied to the output by write_entry().

So I "encode" the includes to a temporary LDIF file, do the usual processing of that temporary LDIF to another temporary file, and finally I "decode" the encoded includes in the last temporary file to the final output.

The essential code being used is:

while (<$ifh>) {
    if ($encode) {
        s/^include:\s/dn: $&/;
    } else {
        s/^dn: (include:\s)/$1/;
    }
    print $ofh $_;
}
close($ifh);

The code would not handle wrapped "include" directives correctly, however.

The only thing I noticed when comparing the results was that slapcat seems to BASE64-encode values that write_entry() does not, so I had different byte counts. For example:

-olcRootPW:: e1NTSEF9eVpBSkE4WUhxSHdSbUloTlFJMklQNGVZOWl4Q1JVRlNSdz09
+olcRootPW: {SSHA}yZAJA8YHqHwRmIhNQI2IP4eY9ixCRUFSRw==

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744199982a4562835.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信