views:

279

answers:

1

Has anyone come across a Perl module that will parse (and write) kerberos configuration files (ie /etc/krb5.conf)? I have looked at quite a few parsing modules like Config::General, Config::Auto, etc., but none seem to be able to handle nested structures like the following:

pam = {
  debug = false
  ticket_lifetime = 36000
  renew_lifetime = 36000
  forwardable = true
  krb4_convert = false
}

It also needs to handle INI-style sections, for example:

[domain_realm]
 .example.com = EXAMPLE.COM
 example.com = EXAMPLE.COM

[appdefaults]
 pam = {
   debug = false
   ticket_lifetime = 36000
   renew_lifetime = 36000
   forwardable = true
   krb4_convert = false
 }

More details on the format can be found at the krb5 conf documentation

I’m at the point of writing my own parser, but I'd rather not have to do so if someone else has written a nice, elegant module already.

+3  A: 

I guess if it takes about 10 minutes to write a parser, it is probably not so interesting to make it a module. Here's a piece of code that probably does the job (disclaimer: I don't know anything about the config format for Kerberos, the code is based on what you posted here).

#!/usr/bin/perl -w
use strict;

my %conf;
my $section;

while (<>) {
    if (/^\s*(\S*)\s*=\s*\{\s*$/) {
        $section = $1;
        $conf{$section} = {};
    } elsif (/^\s*(\S*)\s*=\s*(\S*)\s*$/) {
        my $key = $1;
        my $value = $2;
        if ($section) {
            $conf{$section}{$key}=$value;
        }
    }
}

foreach (keys %conf) {
    $section = $_;
    foreach (keys %{$conf{$section}}) {
        print "$section:$_:$conf{$section}{$_}\n";
    }
}

EDIT: Parsing the ini format is not difficult either. You will just need to add a few more if's in the while loop, and make the data structure %conf slightly more complicated. Instead of a hash of hash, you will need a hash of hash of hash, where the first level key is the keyword in [...], and the second level is what I wrote here (for "pam = {").

PolyThinker
Thanks for your suggestions. I'm heading down a similar path, but as you probably guessed, the file format is slightly more complex. I've now added some more details to the question that give a bit more detail. Thanks again for your help though.
Horatio Alderaan
Yeah. That's exactly what I've ended up doing, with a bit of added logic to accept arbitrarily deep section levels.
Horatio Alderaan