| 1 | package Samba::LDAP::Config; | ||
| 2 | |||
| 3 | require 5.006; | ||
| 4 | |||
| 5 | 11 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 11 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 11 | ▶ | use Readonly; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 11 | ▶ | use Regexp::DefaultFlags; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 11 | ▶ | use base qw( Config::Tiny ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | our $VERSION = '0.05'; | ||
| 12 | |||
| 13 | # | ||
| 14 | # Add Log::Log4perl to all our classes!!!! | ||
| 15 | # | ||
| 16 | |||
| 17 | # Regexp to save param and value in param="value", excluding "" | ||
| 18 | Readonly my $SPLIT_ON_EQUALS_SIGN => qr{ | ||
| 19 | \A # Start of string | ||
| 20 | \s* # Whitespace 0 or more | ||
| 21 | ([^=]+?) # = is terminator of | ||
| 22 | # optionally match 1 | ||
| 23 | # or more | ||
| 24 | \s* | ||
| 25 | = # Actual = | ||
| 26 | \s* | ||
| 27 | " # Actual " | ||
| 28 | ([^"]*) # " is terminator of | ||
| 29 | # match 0 or more | ||
| 30 | " | ||
| 31 | \s* | ||
| 32 | \z # End of string | ||
| 33 | }; | ||
| 34 | |||
| 35 | # Regexp for ${suffix} | ||
| 36 | Readonly my $SUFFIX_CHARS => qr{ \$ [{] suffix [}] \z}; | ||
| 37 | |||
| 38 | # Move this up here from read_string. Will benchmark later. | ||
| 39 | Readonly my $COMMENTS_AND_EMPTY_LINES => qr{ | ||
| 40 | \A # Start of string | ||
| 41 | \s* # Whitespace 0 or more | ||
| 42 | |||
| 43 | # Match one or the other | ||
| 44 | ( ?: [#] | [;] | \z ) | ||
| 45 | }; | ||
| 46 | |||
| 47 | #======================================================================== | ||
| 48 | # -- PUBLIC METHODS -- | ||
| 49 | #======================================================================== | ||
| 50 | |||
| 51 | #------------------------------------------------------------------------ | ||
| 52 | # find_smbldap() | ||
| 53 | # | ||
| 54 | # Looks for smbldap.conf | ||
| 55 | #------------------------------------------------------------------------ | ||
| 56 | |||
| 57 | sub find_smbldap { | ||
| 58 | my $self = shift; | ||
| 59 | |||
| 60 | if ( -e '/etc/smbldap-tools/smbldap.conf' ) { | ||
| 61 | $self->{smbldap_conf} = '/etc/smbldap-tools/smbldap.conf'; | ||
| 62 | } | ||
| 63 | elsif ( -e '/etc/opt/IDEALX/smbldap-tools/smbldap.conf' ) { | ||
| 64 | $self->{smbldap_conf} = '/etc/opt/IDEALX/smbldap-tools/smbldap.conf'; | ||
| 65 | } | ||
| 66 | else { | ||
| 67 | if ( -d 't' ) { | ||
| 68 | chdir 't'; | ||
| 69 | } | ||
| 70 | $self->{smbldap_conf} = 'smbldap.conf'; | ||
| 71 | } | ||
| 72 | |||
| 73 | # Add more locations here (don't really like this technique) or use | ||
| 74 | # Config::Find - Same applies for below two methods | ||
| 75 | |||
| 76 | return $self->{smbldap_conf}; | ||
| 77 | } | ||
| 78 | |||
| 79 | #------------------------------------------------------------------------ | ||
| 80 | # find_smbldap_bind() | ||
| 81 | # | ||
| 82 | # Looks for smbldap_bind.conf | ||
| 83 | #------------------------------------------------------------------------ | ||
| 84 | |||
| 85 | sub find_smbldap_bind { | ||
| 86 | my $self = shift; | ||
| 87 | |||
| 88 | if ( -e '/etc/smbldap-tools/smbldap_bind.conf' ) { | ||
| 89 | $self->{smbldap_bind_conf} = '/etc/smbldap-tools/smbldap_bind.conf'; | ||
| 90 | } | ||
| 91 | elsif ( -e '/etc/opt/IDEALX/smbldap-tools/smbldap_bind.conf' ) { | ||
| 92 | $self->{smbldap_bind_conf} = | ||
| 93 | '/etc/opt/IDEALX/smbldap-tools/smbldap_bind.conf'; | ||
| 94 | } | ||
| 95 | else { | ||
| 96 | if ( -d 't' ) { | ||
| 97 | chdir 't'; | ||
| 98 | } | ||
| 99 | $self->{smbldap_bind_conf} = 'smbldap_bind.conf'; | ||
| 100 | } | ||
| 101 | |||
| 102 | return $self->{smbldap_bind_conf}; | ||
| 103 | } | ||
| 104 | |||
| 105 | #------------------------------------------------------------------------ | ||
| 106 | # find_samba() | ||
| 107 | # | ||
| 108 | # Looks for smb.conf | ||
| 109 | #------------------------------------------------------------------------ | ||
| 110 | |||
| 111 | sub find_samba { | ||
| 112 | my $self = shift; | ||
| 113 | |||
| 114 | if ( -e '/etc/samba/smb.conf' ) { | ||
| 115 | $self->{samba_conf} = '/etc/samba/smb.conf'; | ||
| 116 | } | ||
| 117 | elsif ( -e 'usr/local/samba/lib/smb.conf' ) { | ||
| 118 | $self->{samba_conf} = '/usr/local/samba/lib/smb.conf'; | ||
| 119 | } | ||
| 120 | else { | ||
| 121 | if ( -d 't' ) { | ||
| 122 | chdir 't'; | ||
| 123 | } | ||
| 124 | $self->{samba_conf} = 'smb.conf'; | ||
| 125 | } | ||
| 126 | |||
| 127 | return $self->{samba_conf}; | ||
| 128 | } | ||
| 129 | |||
| 130 | #------------------------------------------------------------------------ | ||
| 131 | # read_conf( $filename ) | ||
| 132 | # | ||
| 133 | # Wrapper to provide an instant error message as returned by the native | ||
| 134 | # Config::Tiny read method | ||
| 135 | #------------------------------------------------------------------------ | ||
| 136 | |||
| 137 | sub read_conf { | ||
| 138 | my $self = shift; | ||
| 139 | my $file = shift; | ||
| 140 | |||
| 141 | my $conf = $self->read($file); | ||
| 142 | |||
| 143 | # Nice instant user error message | ||
| 144 | die $self->errstr() . "\nPlease fix this to continue!\n" | ||
| 145 | if $self->errstr(); | ||
| 146 | |||
| 147 | return $conf; | ||
| 148 | } | ||
| 149 | |||
| 150 | #------------------------------------------------------------------------ | ||
| 151 | # read_string() | ||
| 152 | # | ||
| 153 | # Overrides Config::Tiny's read_string to exclude the " " marks found in | ||
| 154 | # smbldap.conf and smbldap_bind.conf and remove section handling, as we | ||
| 155 | # don't have any [sections] in either of these files. | ||
| 156 | # | ||
| 157 | # Also substitutes the suffix hash ( ${suffix} ) with its value | ||
| 158 | #------------------------------------------------------------------------ | ||
| 159 | |||
| 160 | sub read_string { | ||
| 161 | my $class = ref $_[0] ? ref shift: shift; | ||
| 162 | my $self = bless {}, $class; | ||
| 163 | return undef if !defined $_[0]; | ||
| 164 | |||
| 165 | # Parse the file | ||
| 166 | my $counter = 0; | ||
| 167 | foreach ( split /(?:\015{1,2}\012|\015|\012)/, shift ) { | ||
| 168 | $counter++; | ||
| 169 | |||
| 170 | # Skip comments and empty lines | ||
| 171 | next if /$COMMENTS_AND_EMPTY_LINES/; | ||
| 172 | |||
| 173 | # Handle properties, but don't save " " and S&R ${suffix} | ||
| 174 | if (/$SPLIT_ON_EQUALS_SIGN/) { | ||
| 175 | my ( $before_equals, $after_equals ) = ( $1, $2 ); | ||
| 176 | |||
| 177 | # Save what's after suffix=, temporarily. (don't like this | ||
| 178 | # technique, will come back to it later though) | ||
| 179 | if ( $before_equals eq 'suffix' ) { | ||
| 180 | $self->{sf_val} = $after_equals; | ||
| 181 | } | ||
| 182 | |||
| 183 | # Replace ${suffix} with what was saved above | ||
| 184 | $after_equals =~ s{ $SUFFIX_CHARS }{$self->{sf_val}}; | ||
| 185 | |||
| 186 | # as normal | ||
| 187 | $self->{$before_equals} = $after_equals; | ||
| 188 | next; | ||
| 189 | } | ||
| 190 | |||
| 191 | return $self->_error("Syntax error at line $counter: '$_'"); | ||
| 192 | } | ||
| 193 | |||
| 194 | # Not needed for returning | ||
| 195 | delete $self->{sf_val}; | ||
| 196 | |||
| 197 | return $self; | ||
| 198 | } | ||
| 199 | |||
| 200 | #======================================================================== | ||
| 201 | # -- PRIVATE METHODS -- | ||
| 202 | #======================================================================== | ||
| 203 | |||
| 204 | 1; # Magic true value required at end of module | ||
| 205 | |||
| 206 | __END__ | ||