| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package MToken::Config; # $Id: Config.pm 103 2021-10-10 11:04:34Z minus $ |
|
2
|
1
|
|
|
1
|
|
7
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
32
|
|
|
3
|
1
|
|
|
1
|
|
5
|
use utf8; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=encoding utf-8 |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
MToken::Config - MToken local configuration |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 VERSION |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Version 1.03 |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use MToken::Config; |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $config = MToken::Config->new( |
|
20
|
|
|
|
|
|
|
file => '/my/device/mtoken.conf', |
|
21
|
|
|
|
|
|
|
name => 'foo', # Optional. See device config file first |
|
22
|
|
|
|
|
|
|
); |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my $foo = $config->get('foo'); |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my $foo = $config->set('foo' => 'bar'); |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my $status = $config->save(); # Local file only |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
The module works with the local configuration data |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 METHODS |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=over 8 |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=item B |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my $config = WWW::MLite::Config->new( |
|
41
|
|
|
|
|
|
|
file => '/my/device/mtoken.conf', |
|
42
|
|
|
|
|
|
|
name => 'foo', # Optional. See device config file first |
|
43
|
|
|
|
|
|
|
); |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Returns configuration object |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=item B |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my $value = $config->get( 'key' ); |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Returns value by keyname |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=item B |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my %config = $config->getall; |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
Returns all configuration pairs - key and value |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=item B |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
print $self->is_loaded ? 'loaded' : 'not loaded'; |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Returns status of local config |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=item B |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
$config->set( 'key', $value ); |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Set new value for key. Returns status of the operation |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=item B |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
$config->save; |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Save current configuration to local_file and returns status of the operation |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=back |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 HISTORY |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
See C file |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 AUTHOR |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Serż Minus (Sergey Lepenkov) L Eabalama@cpan.orgE |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 COPYRIGHT |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Copyright (C) 1998-2021 D&D Corporation. All Rights Reserved |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 LICENSE |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
|
94
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
See C file and L |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=cut |
|
99
|
|
|
|
|
|
|
|
|
100
|
1
|
|
|
1
|
|
34
|
use Carp; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
63
|
|
|
101
|
1
|
|
|
1
|
|
7
|
use File::HomeDir; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
51
|
|
|
102
|
1
|
|
|
1
|
|
830
|
use Config::General; |
|
|
1
|
|
|
|
|
15990
|
|
|
|
1
|
|
|
|
|
104
|
|
|
103
|
1
|
|
|
1
|
|
15
|
use Try::Tiny; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
73
|
|
|
104
|
1
|
|
|
1
|
|
8
|
use Cwd; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
55
|
|
|
105
|
1
|
|
|
1
|
|
6
|
use File::Spec; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
28
|
|
|
106
|
1
|
|
|
1
|
|
5
|
use CTK::Util qw/preparedir/; |
|
|
1
|
|
|
|
|
4
|
|
|
|
1
|
|
|
|
|
57
|
|
|
107
|
1
|
|
|
1
|
|
7
|
use MToken::Const qw/ :GENERAL :MATH /; |
|
|
1
|
|
|
|
|
37
|
|
|
|
1
|
|
|
|
|
421
|
|
|
108
|
|
|
|
|
|
|
|
|
109
|
1
|
|
|
1
|
|
8
|
use vars qw/$VERSION/; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
84
|
|
|
110
|
|
|
|
|
|
|
$VERSION = '1.03'; |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
use constant { |
|
113
|
1
|
|
|
|
|
1121
|
ALLOWED_KEYS => [qw/ |
|
114
|
|
|
|
|
|
|
token |
|
115
|
|
|
|
|
|
|
server_url |
|
116
|
|
|
|
|
|
|
gpgbin opensslbin |
|
117
|
|
|
|
|
|
|
fingerprint |
|
118
|
|
|
|
|
|
|
/], |
|
119
|
1
|
|
|
1
|
|
8
|
}; |
|
|
1
|
|
|
|
|
3
|
|
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub new { |
|
122
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
|
123
|
0
|
|
|
|
|
|
my %args = (@_); |
|
124
|
0
|
|
|
|
|
|
my $is_loaded = 0; |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
my $config_file = $args{file} || $args{config_file} || $args{device_file} |
|
127
|
0
|
|
0
|
|
|
|
|| File::Spec->catfile(cwd(), DIR_PRIVATE, DEVICE_CONF_FILE); |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
# Load device config |
|
130
|
0
|
|
|
|
|
|
my %dev = _loadconfig($config_file); |
|
131
|
0
|
|
0
|
|
|
|
my $name = $dev{token} || $dev{token_name} || $dev{name}; |
|
132
|
0
|
0
|
|
|
|
|
$is_loaded = 1 if $name; |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# Get path of local config |
|
136
|
0
|
|
0
|
|
|
|
$name ||= $args{name} || "noname"; |
|
|
|
|
0
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
$name =~ s/\s+//g; |
|
138
|
0
|
|
|
|
|
|
$name =~ s/[^a-z0-9]//g; |
|
139
|
0
|
|
0
|
|
|
|
$name ||= "noname"; |
|
140
|
0
|
|
|
|
|
|
my $local_dir = File::Spec->catdir(File::HomeDir->my_data(), PROJECTNAMEL); |
|
141
|
0
|
|
|
|
|
|
my $local_file = File::Spec->catfile($local_dir, sprintf("%s.conf", $name)); |
|
142
|
0
|
|
|
|
|
|
my %lkl = (); |
|
143
|
0
|
0
|
|
|
|
|
if (-f $local_file) { # Ok! File exists, try load local config |
|
144
|
0
|
|
|
|
|
|
%lkl = _loadconfig($local_file); |
|
145
|
0
|
|
|
|
|
|
while (my ($k,$v) = each %lkl) { |
|
146
|
0
|
0
|
0
|
|
|
|
$dev{$k} //= $v if defined $v; |
|
147
|
|
|
|
|
|
|
} |
|
148
|
|
|
|
|
|
|
} |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# Set data |
|
151
|
0
|
|
|
|
|
|
my %cfg = ( |
|
152
|
|
|
|
|
|
|
name => $name, |
|
153
|
|
|
|
|
|
|
is_loaded => $is_loaded, |
|
154
|
|
|
|
|
|
|
device_config_file => $config_file, |
|
155
|
|
|
|
|
|
|
local_config_dir => $local_dir, |
|
156
|
|
|
|
|
|
|
local_config_file => $local_file, |
|
157
|
|
|
|
|
|
|
_config => {%dev}, |
|
158
|
|
|
|
|
|
|
); |
|
159
|
|
|
|
|
|
|
|
|
160
|
0
|
|
|
|
|
|
my $self = bless { %cfg }, $class; |
|
161
|
0
|
|
|
|
|
|
return $self; |
|
162
|
|
|
|
|
|
|
} |
|
163
|
|
|
|
|
|
|
sub save { |
|
164
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
165
|
0
|
0
|
0
|
|
|
|
if (!$self->{name} || $self->{name} eq "noname") { |
|
166
|
0
|
|
|
|
|
|
carp("Can't use nonamed devices"); |
|
167
|
0
|
|
|
|
|
|
return FALSE; |
|
168
|
|
|
|
|
|
|
} |
|
169
|
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
my %svh = (); |
|
171
|
0
|
|
|
|
|
|
foreach my $k (@{(ALLOWED_KEYS)}) { |
|
|
0
|
|
|
|
|
|
|
|
172
|
0
|
|
|
|
|
|
my $v = $self->get($k); |
|
173
|
0
|
0
|
|
|
|
|
$svh{$k} = $v if defined $v; |
|
174
|
|
|
|
|
|
|
} |
|
175
|
0
|
0
|
|
|
|
|
preparedir($self->{local_config_dir}, 0755) or do { |
|
176
|
0
|
|
|
|
|
|
carp(sprintf("Can't prepare directory %s", $self->{local_config_dir})); |
|
177
|
0
|
|
|
|
|
|
return FALSE; |
|
178
|
|
|
|
|
|
|
}; |
|
179
|
|
|
|
|
|
|
|
|
180
|
0
|
|
|
|
|
|
return _saveconfig($self->{local_config_file}, {%svh}); |
|
181
|
|
|
|
|
|
|
} |
|
182
|
|
|
|
|
|
|
sub get { |
|
183
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
184
|
0
|
|
|
|
|
|
my $key = shift; |
|
185
|
0
|
0
|
|
|
|
|
return undef unless $key; |
|
186
|
0
|
|
|
|
|
|
return $self->{_config}{$key}; |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
sub getall { |
|
189
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
190
|
0
|
|
|
|
|
|
my $lh = $self->{_config}; |
|
191
|
0
|
|
|
|
|
|
return (%$lh); |
|
192
|
|
|
|
|
|
|
} |
|
193
|
0
|
|
|
0
|
1
|
|
sub conf { goto &getall }; |
|
194
|
0
|
|
|
0
|
1
|
|
sub config { goto &getall }; |
|
195
|
|
|
|
|
|
|
sub set { |
|
196
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
197
|
0
|
|
|
|
|
|
my $key = shift; |
|
198
|
0
|
|
|
|
|
|
my $val = shift; |
|
199
|
0
|
0
|
|
|
|
|
unless ($key) { |
|
200
|
0
|
|
|
|
|
|
carp("Key not specified"); |
|
201
|
0
|
|
|
|
|
|
return FALSE; |
|
202
|
|
|
|
|
|
|
} |
|
203
|
0
|
|
|
|
|
|
$self->{_config}{$key} = $val; |
|
204
|
0
|
|
|
|
|
|
return TRUE; |
|
205
|
|
|
|
|
|
|
} |
|
206
|
|
|
|
|
|
|
sub is_loaded { |
|
207
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
208
|
0
|
|
|
|
|
|
$self->{is_loaded}; |
|
209
|
|
|
|
|
|
|
} |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
sub _loadconfig { |
|
212
|
0
|
|
|
0
|
|
|
my $file = shift; |
|
213
|
0
|
0
|
|
|
|
|
unless ($file) { |
|
214
|
0
|
|
|
|
|
|
carp("Filename not specified!"); |
|
215
|
0
|
|
|
|
|
|
return (); |
|
216
|
|
|
|
|
|
|
} |
|
217
|
0
|
|
|
|
|
|
my %config = (); |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
# Load |
|
220
|
0
|
0
|
0
|
|
|
|
if ($file && -f $file) { |
|
221
|
0
|
|
|
|
|
|
my $gconf; |
|
222
|
|
|
|
|
|
|
try { |
|
223
|
0
|
|
|
0
|
|
|
$gconf = Config::General->new( |
|
224
|
|
|
|
|
|
|
-ConfigFile => $file, |
|
225
|
|
|
|
|
|
|
-ApacheCompatible => TRUE, |
|
226
|
|
|
|
|
|
|
-LowerCaseNames => TRUE, |
|
227
|
|
|
|
|
|
|
-AutoTrue => TRUE, |
|
228
|
|
|
|
|
|
|
); |
|
229
|
|
|
|
|
|
|
} catch { |
|
230
|
0
|
|
|
0
|
|
|
carp($_); |
|
231
|
0
|
|
|
|
|
|
}; |
|
232
|
0
|
0
|
0
|
|
|
|
if ($gconf && $gconf->can('getall')) { |
|
233
|
0
|
|
|
|
|
|
%config = ($gconf->getall); |
|
234
|
|
|
|
|
|
|
} |
|
235
|
|
|
|
|
|
|
} |
|
236
|
|
|
|
|
|
|
|
|
237
|
0
|
|
|
|
|
|
return %config; |
|
238
|
|
|
|
|
|
|
} |
|
239
|
|
|
|
|
|
|
sub _saveconfig { |
|
240
|
0
|
|
|
0
|
|
|
my $file = shift; |
|
241
|
0
|
|
|
|
|
|
my $config = shift; |
|
242
|
0
|
0
|
|
|
|
|
unless ($file) { |
|
243
|
0
|
|
|
|
|
|
carp("Filename not specified!"); |
|
244
|
0
|
|
|
|
|
|
return 0; |
|
245
|
|
|
|
|
|
|
} |
|
246
|
0
|
0
|
0
|
|
|
|
unless ($config && ref($config) eq 'HASH') { |
|
247
|
0
|
|
|
|
|
|
carp("No configuration data for saving"); |
|
248
|
0
|
|
|
|
|
|
return 0; |
|
249
|
|
|
|
|
|
|
} |
|
250
|
|
|
|
|
|
|
|
|
251
|
0
|
|
|
|
|
|
my $conf = Config::General->new( |
|
252
|
|
|
|
|
|
|
-ConfigHash => $config, |
|
253
|
|
|
|
|
|
|
-ApacheCompatible => TRUE, |
|
254
|
|
|
|
|
|
|
-LowerCaseNames => TRUE, |
|
255
|
|
|
|
|
|
|
-AutoTrue => TRUE, |
|
256
|
|
|
|
|
|
|
); |
|
257
|
0
|
|
|
|
|
|
$conf->save_file($file); |
|
258
|
0
|
|
|
|
|
|
return 1; |
|
259
|
|
|
|
|
|
|
} |
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
1; |
|
262
|
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
__END__ |