File Coverage

blib/lib/Sys/Config/Manage/Ownership.pm
Criterion Covered Total %
statement 15 636 2.3
branch 0 246 0.0
condition 0 3 0.0
subroutine 5 15 33.3
pod 10 10 100.0
total 30 910 3.3


line stmt bran cond sub pod time code
1             package Sys::Config::Manage::Ownership;
2              
3 1     1   28823 use warnings;
  1         2  
  1         30  
4 1     1   6 use strict;
  1         2  
  1         32  
5 1     1   7 use File::Basename;
  1         2  
  1         101  
6 1     1   6 use base 'Error::Helper';
  1         2  
  1         848  
7 1     1   1628 use String::ShellQuote;
  1         829  
  1         6139  
8              
9             =head1 NAME
10              
11             Sys::Config::Manage::Ownership - Handles file user/group ownership for files in the configuration directory.
12              
13             =head1 VERSION
14              
15             Version 0.0.0
16              
17             =cut
18              
19             our $VERSION = '0.0.0';
20              
21             =head1 METHODS
22              
23             =head2 new
24              
25             This initiates the object.
26              
27             One argument is required and it is a hash reference.
28              
29             =head3 args hash ref
30              
31             =head4 scm
32              
33             This is a initiated Sys::Config::Manage object.
34              
35             =head4 defaultUID
36              
37             This is the default user ID for a file.
38              
39             If not specified, the default is '0'.
40              
41             =head4 defaultGID
42              
43             This is the default group ID for a file.
44              
45             If not specified, the default is '0'.
46              
47             $foo=Sys::Config::Manage::Ownership->new(\%args);
48             if($foo->error){
49             warn('error:'.$foo->error.': '.$foo->errorString);
50             }
51              
52             =cut
53              
54             sub new{
55 0     0 1   my %args;
56 0 0         if(defined($_[1])){
57 0           %args= %{$_[1]};
  0            
58             };
59 0           my $method='new';
60              
61 0           my $self = {
62             module=>'Sys-Config-Manage-Ownership',
63             perror=>undef,
64             error=>undef,
65             errorString=>"",
66             defaultUID=>'0',
67             defaultGID=>'0',
68             };
69 0           bless $self;
70              
71             #make sure we have a Sys::Config::Manage
72 0 0         if(!defined( $args{scm} ) ){
73 0           $self->{perror}=1;
74 0           $self->{error}=1;
75 0           $self->{errorString}='Nothing passed for the Sys::Config::Manage object';
76 0           $self->warn;
77 0           return $self;
78             }
79              
80             #make sure that it is really a Sys::Config::Manage object
81 0 0         if( ref( $args{scm} ) ne "Sys::Config::Manage" ){
82 0           $self->{perror}=1;
83 0           $self->{error}=2;
84 0           $self->{errorString}='$args{scm} is not a Sys::Config::Manage object';
85 0           $self->warn;
86 0           return $self;
87             }
88              
89             #figures out what the defualt is
90 0 0         if ( defined( $args{defaultUID} ) ){
91             #make sure the perms are sane
92 0 0         if ( $args{default} !~ /^[0123456789]*$/ ){
93 0           $self->{perror}=1;
94 0           $self->{error}=12;
95 0           $self->{errorString}='"'.$args{default}.'" does not appear to be a valid value';
96 0           $self->warn;
97 0           return $self;
98             }
99 0           $self->{defaultUID}=$args{defaultUID};
100             }
101              
102             #figures out what the defualt is
103 0 0         if ( defined( $args{defaultGID} ) ){
104             #make sure the perms are sane
105 0 0         if ( $args{default} !~ /^[0123456789]*$/ ){
106 0           $self->{perror}=1;
107 0           $self->{error}=12;
108 0           $self->{errorString}='"'.$args{default}.'" does not appear to be a valid value';
109 0           $self->warn;
110 0           return $self;
111             }
112 0           $self->{defaultGID}=$args{defaultGID};
113             }
114              
115 0           $self->{scm}=$args{scm};
116              
117 0           return $self;
118             }
119              
120             =head2 downSync
121              
122             This syncs the group/user ownership down from the configuration
123             directory to the system.
124              
125             Two arguments can be used.
126              
127             The first is the configuration directory. If not specified, it will
128             be automaticallly choosen.
129              
130             The second is the files to sync. If not specifiedm, all files will
131             be synced.
132              
133             #sync the specified files
134             $foo->downSync( $configDir, \@files);
135             if($foo->error){
136             warn('error:'.$foo->error.': '.$foo->errorString);
137             }
138              
139             #syncs all the files
140             $foo->downSync( $configDir );
141             if($foo->error){
142             warn('error:'.$foo->error.': '.$foo->errorString);
143             }
144              
145             =cut
146              
147             sub downSync{
148 0     0 1   my $self=$_[0];
149 0           my $configDir=$_[1];
150 0           my @files;
151 0 0         if (defined($_[2])) {
152 0           @files=@{$_[2]};
  0            
153             }
154 0           my $method='downSync';
155              
156             #blank any previous errors
157 0 0         if (!$self->errorblank) {
158 0           return undef;
159             }
160            
161             #make sure we have a directory to use
162 0 0         if (!defined($configDir)) {
163 0           $configDir=$self->{scm}->selectConfigDir;
164 0 0         if ($self->{scm}->error) {
165 0           $self->{error}=5;
166 0           $self->{errorString}='Sys::Config::Manage->selectConfigDir errored error="'.
167             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
168 0           $self->warn;
169 0           return undef;
170             }
171             }
172            
173             #make sure the config directory is valid
174 0           my $valid=$self->{scm}->validConfigDirName($configDir);
175 0 0         if ($self->{scm}->error) {
176 0           $self->{error}=6;
177 0           $self->{errorString}='Sys::Config::Manage->validConfigDirName errored error="'.
178             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
179 0           $self->warn;
180 0           return undef;
181             }
182 0 0         if (defined( $valid )) {
183 0           $self->{error}=3;
184 0           $self->{errorString}='The configuration directory name '.$valid;
185 0           $self->warn;
186 0           return undef;
187             }
188            
189             #makes sure it exists
190 0 0         if ( ! -d $self->{scm}->{baseDir}.'/'.$configDir ) {
191 0           $self->{error}=4;
192 0           $self->{errorString}='The configuration directory, "'.$self->{baseDir}.'/'.$configDir.'", does not exist';
193 0           $self->warn;
194 0           return undef;
195             }
196              
197             #checks and make sure all the files exist
198 0           my $int=0;
199 0 0         if(defined( $files[$int] )){
200 0           my @allfiles=$self->{scm}->listConfigFiles($configDir);
201 0 0         if( $self->{scm}->error ){
202 0           $self->{error}='16';
203 0           $self->{errorString}='Sys::Config::Manage->listConfigFiles errored error="'.
204             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
205 0           $self->warn;
206 0           return undef;
207             }
208            
209             #make sure each file asked to be synced is tracked
210 0           while(defined( $files[$int] )){
211              
212 0           my $matched=0;
213 0           my $int2=0;
214 0           while(defined( $allfiles[$int2] )){
215 0 0         if( $files[$int] eq $allfiles[$int2] ){
216 0           $matched=1;
217             }
218              
219 0           $int2++;
220             }
221              
222 0 0         if(! $matched){
223 0           $self->{error}='8';
224 0           $self->{errorString}='"'.$files[$int].'" does not exist under the configuration directory, "'.$configDir.'",';
225 0           $self->warn;
226 0           return undef;
227             }
228              
229 0           $int++;
230             }
231              
232             }else{
233             #if we get here, no files have been specified so we do them all
234 0           @files=$self->{scm}->listConfigFiles($configDir);
235 0 0         if( $self->{scm}->error ){
236 0           $self->{error}='16';
237 0           $self->{errorString}='Sys::Config::Manage->listConfigFiles errored error="'.
238             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
239 0           $self->warn;
240 0           return undef;
241             }
242             }
243              
244             #process each file
245 0           $int=0;
246 0           while( defined( $files[$int] ) ){
247             #get the perms for the file we will set it on
248 0           my $uid=$self->getUID( $configDir, $files[$int] );
249 0 0         if( $self->error ){
250 0           warn($self->{module}.' '.$method.': Sys::Config::Manage::Ownership->getUID errored');
251 0           return undef;
252             }
253              
254             #get the perms for the file we will set it on
255 0           my $gid=$self->getGID( $configDir, $files[$int] );
256 0 0         if( $self->error ){
257 0           warn($self->{module}.' '.$method.': Sys::Config::Manage::Ownership->getGID errored');
258 0           return undef;
259             }
260              
261             #try to chmod it
262 0 0         if(!chmod( $uid, $gid, $files[$int] )){
263 0           $self->{error}='17';
264 0           $self->{errorString}='chown( '.$uid.', '.$gid.', "'.$files[$int].'") errored';
265 0           $self->warn;
266 0           return undef;
267             }
268            
269 0           $int++;
270             }
271              
272 0           return 1;
273             }
274              
275             =head2 getGID
276              
277             This retrieves the GID for a file.
278              
279             Two arguments are taken.The first is the configuration directory,
280             which if not defined is automatically chosen. The second is the
281             file in question.
282              
283             my $gid=$foo->getGID( $configDir, $file );
284             if($foo->error){
285             warn('error:'.$foo->error.': '.$foo->errorString);
286             }
287              
288             =cut
289              
290             sub getGID{
291 0     0 1   my $self=$_[0];
292 0           my $configDir=$_[1];
293 0           my $file=$_[2];
294 0           my $method='getGID';
295              
296             #blank any previous errors
297 0 0         if (!$self->errorblank) {
298 0           return undef;
299             }
300              
301             #make sure we have a directory to use
302 0 0         if (!defined($configDir)) {
303 0           $configDir=$self->{scm}->selectConfigDir;
304 0 0         if ($self->{scm}->error) {
305 0           $self->{error}=5;
306 0           $self->{errorString}='Sys::Config::Manage->selectConfigDir errored error="'.
307             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
308 0           $self->warn;
309 0           return undef;
310             }
311             }
312            
313             #make sure the config directory is valid
314 0           my $valid=$self->{scm}->validConfigDirName($configDir);
315 0 0         if ($self->{scm}->error) {
316 0           $self->{error}=6;
317 0           $self->{errorString}='Sys::Config::Manage->validConfigDirName errored error="'.
318             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
319 0           $self->warn;
320 0           return undef;
321             }
322 0 0         if (defined( $valid )) {
323 0           $self->{error}=3;
324 0           $self->{errorString}='The configuration directory name '.$valid;
325 0           $self->warn;
326 0           return undef;
327             }
328            
329             #makes sure it exists
330 0 0         if ( ! -d $self->{scm}->{baseDir}.'/'.$configDir ) {
331 0           $self->{error}=4;
332 0           $self->{errorString}='The configuration directory, "'.$self->{baseDir}.'/'.$configDir.'", does not exist';
333 0           $self->warn;
334 0           return undef;
335             }
336              
337             #make value for the file is specified
338 0 0         if (!defined( $file )) {
339 0           $self->{error}=7;
340 0           $self->{errorString}='No file specified';
341 0           $self->warn;
342 0           return undef;
343             }
344            
345             #make sure the file exists, under the config dir
346 0 0         if (! -f $self->{scm}->{baseDir}.'/'.$configDir.'/'.$file ) {
347 0           $self->{error}=8;
348 0           $self->{errorString}='The file does not exist in the configuration directory';
349 0           $self->warn;
350 0           return undef;
351             }
352              
353             #figure out what the perms file is
354 0           my ($name,$path,$suffix) = fileparse($file);
355 0           my $idfile=$self->{scm}->{baseDir}.'/'.$configDir.'/'.$path.'/.SysConfigManage/GID/'.$name;
356              
357             #make sure the file has some perms
358 0 0         if (! -f $idfile ){
359 0           return $self->{defaultGID};
360             }
361              
362             #read the file
363 0           my $fh;
364 0 0         if ( ! open( $fh, '<', $idfile ) ){
365 0           $self->{error}=15;
366 0           $self->{errorString}='Unable to open "'.$idfile.'"';
367 0           $self->warn;
368 0           return undef;
369             }
370 0           my $id=<$fh>;
371 0           chomp($id);
372 0           close( $fh );
373              
374 0           return $id;
375             }
376              
377             =head2 getUID
378              
379             This retrieves the UID for a file.
380              
381             Two arguments are taken.The first is the configuration directory,
382             which if not defined is automatically chosen. The second is the
383             file in question.
384              
385             my $UID=$foo->getUID( $configDir, $file );
386             if($foo->error){
387             warn('error:'.$foo->error.': '.$foo->errorString);
388             }
389              
390             =cut
391              
392             sub getUID{
393 0     0 1   my $self=$_[0];
394 0           my $configDir=$_[1];
395 0           my $file=$_[2];
396 0           my $method='getUID';
397              
398             #blank any previous errors
399 0 0         if (!$self->errorblank) {
400 0           return undef;
401             }
402              
403             #make sure we have a directory to use
404 0 0         if (!defined($configDir)) {
405 0           $configDir=$self->{scm}->selectConfigDir;
406 0 0         if ($self->{scm}->error) {
407 0           $self->{error}=5;
408 0           $self->{errorString}='Sys::Config::Manage->selectConfigDir errored error="'.
409             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
410 0           $self->warn;
411 0           return undef;
412             }
413             }
414            
415             #make sure the config directory is valid
416 0           my $valid=$self->{scm}->validConfigDirName($configDir);
417 0 0         if ($self->{scm}->error) {
418 0           $self->{error}=6;
419 0           $self->{errorString}='Sys::Config::Manage->validConfigDirName errored error="'.
420             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
421 0           $self->warn;
422 0           return undef;
423             }
424 0 0         if (defined( $valid )) {
425 0           $self->{error}=3;
426 0           $self->{errorString}='The configuration directory name '.$valid;
427 0           $self->warn;
428 0           return undef;
429             }
430            
431             #makes sure it exists
432 0 0         if ( ! -d $self->{scm}->{baseDir}.'/'.$configDir ) {
433 0           $self->{error}=4;
434 0           $self->{errorString}='The configuration directory, "'.$self->{baseDir}.'/'.$configDir.'", does not exist';
435 0           $self->warn;
436 0           return undef;
437             }
438              
439             #make value for the file is specified
440 0 0         if (!defined( $file )) {
441 0           $self->{error}=7;
442 0           $self->{errorString}='No file specified';
443 0           $self->warn;
444 0           return undef;
445             }
446            
447             #make sure the file exists, under the config dir
448 0 0         if (! -f $self->{scm}->{baseDir}.'/'.$configDir.'/'.$file ) {
449 0           $self->{error}=8;
450 0           $self->{errorString}='The file does not exist in the configuration directory';
451 0           $self->warn;
452 0           return undef;
453             }
454              
455             #figure out what the perms file is
456 0           my ($name,$path,$suffix) = fileparse($file);
457 0           my $idfile=$self->{scm}->{baseDir}.'/'.$configDir.'/'.$path.'/.SysConfigManage/UID/'.$name;
458              
459             #make sure the file has some perms
460 0 0         if (! -f $idfile ){
461 0           return $self->{defaultUID};
462             }
463              
464             #read the file
465 0           my $fh;
466 0 0         if ( ! open( $fh, '<', $idfile ) ){
467 0           $self->{error}=15;
468 0           $self->{errorString}='Unable to open "'.$idfile.'"';
469 0           $self->warn;
470 0           return undef;
471             }
472 0           my $id=<$fh>;
473 0           chomp($id);
474 0           close( $fh );
475              
476 0           return $id;
477             }
478              
479             =head2 listConfigFiles
480              
481             This lists the various config files that are being
482             tracked that actually have a value specified.
483              
484             Not all files returned by Sys::Config::Manage->listConfigFiles
485             will have a value specified.
486              
487             my @files=$foo->listConfigFiles;
488             if($foo->error){
489             warn('error:'.$foo->error.': '.$foo->errorString);
490             }
491              
492             =cut
493              
494             sub listConfigFiles{
495 0     0 1   my $self=$_[0];
496 0           my $configDir=$_[1];
497 0           my $method='listConfigFiles';
498              
499             #blank any previous errors
500 0 0         if (!$self->errorblank) {
501 0           return undef;
502             }
503              
504             #make sure we have a directory to use
505 0 0         if (!defined($configDir)) {
506 0           $configDir=$self->{scm}->selectConfigDir;
507 0 0         if ($self->{scm}->error) {
508 0           $self->{error}=5;
509 0           $self->{errorString}='Sys::Config::Manage->selectConfigDir errored error="'.
510             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
511 0           $self->warn;
512 0           return undef;
513             }
514             }
515              
516             #make sure the config directory is valid
517 0           my $valid=$self->{scm}->validConfigDirName($configDir);
518 0 0         if ($self->{scm}->error) {
519 0           $self->{error}=6;
520 0           $self->{errorString}='Sys::Config::Manage->validConfigDirName errored error="'.
521             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
522 0           $self->warn;
523 0           return undef;
524             }
525 0 0         if (defined( $valid )) {
526 0           $self->{error}=3;
527 0           $self->{errorString}='The configuration directory name '.$valid;
528 0           $self->warn;
529 0           return undef;
530             }
531              
532             #makes sure it exists
533 0 0         if ( ! -d $self->{scm}->{baseDir}.'/'.$configDir ) {
534 0           $self->{error}=4;
535 0           $self->{errorString}='The configuration directory, "'.$self->{baseDir}.'/'.$configDir.'", does not exist';
536 0           $self->warn;
537 0           return undef;
538             }
539              
540             #holds what will be returned
541 0           my @found;
542              
543             #get a list of files
544 0           my @files=$self->{scm}->listConfigFiles($configDir);
545              
546            
547             #process every found file
548 0           my $int=0;
549 0           while (defined( $files[$int] )) {
550 0           my ($name,$path,$suffix)=fileparse($self->{scm}->{baseDir}
551             .'/'.$configDir.'/'.$files[$int]);
552              
553 0           my $UIDfile=$path.'/.SysConfigManage/UID/'.$name;
554 0           $UIDfile=~s/\/\/*/\//g;
555              
556 0           my $GIDfile=$path.'/.SysConfigManage/GID/'.$name;
557 0           $GIDfile=~s/\/\/*/\//g;
558              
559             #make sure the perms file exists and if so add it
560 0 0 0       if (
561             ( -f $UIDfile ) ||
562             ( -f $GIDfile )
563             ) {
564 0           push(@found, $files[$int]);
565             }
566              
567 0           $int++;
568             }
569              
570 0           return @found;
571             }
572              
573             =head2 setGID
574              
575             This sets the GID for a file. This does require the numeric value.
576              
577             Three arguments are taken.
578              
579             The first one is the configuration directory to use. If none is
580             specified, it will automatically be choosen.
581              
582             The second is the config file to add the perms for.
583              
584             The third numeric value for the GID.
585              
586             $foo->setGID($configDir, $file, $gid);
587             if($foo->error){
588             warn('error:'.$foo->error.': '.$foo->errorString);
589             }
590              
591             =cut
592              
593             sub setGID{
594 0     0 1   my $self=$_[0];
595 0           my $configDir=$_[1];
596 0           my $file=$_[2];
597 0           my $id=$_[3];
598 0           my $method='setGID';
599              
600             #blank any previous errors
601 0 0         if (!$self->errorblank) {
602 0           return undef;
603             }
604              
605             #make sure we have a directory to use
606 0 0         if (!defined($configDir)) {
607 0           $configDir=$self->{scm}->selectConfigDir;
608 0 0         if ($self->{scm}->error) {
609 0           $self->{error}=5;
610 0           $self->{errorString}='Sys::Config::Manage->selectConfigDir errored error="'.
611             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
612 0           $self->warn;
613 0           return undef;
614             }
615             }
616              
617             #make sure the config directory is valid
618 0           my $valid=$self->{scm}->validConfigDirName($configDir);
619 0 0         if ($self->{scm}->error) {
620 0           $self->{error}=6;
621 0           $self->{errorString}='Sys::Config::Manage->validConfigDirName errored error="'.
622             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
623 0           $self->warn;
624 0           return undef;
625             }
626 0 0         if (defined( $valid )) {
627 0           $self->{error}=3;
628 0           $self->{errorString}='The configuration directory name '.$valid;
629 0           $self->warn;
630 0           return undef;
631             }
632              
633             #makes sure it exists
634 0 0         if ( ! -d $self->{scm}->{baseDir}.'/'.$configDir ) {
635 0           $self->{error}=4;
636 0           $self->{errorString}='The configuration directory, "'.$self->{baseDir}.'/'.$configDir.'", does not exist';
637 0           $self->warn;
638 0           return undef;
639             }
640              
641             #make value for the file is specified
642 0 0         if (!defined( $file )) {
643 0           $self->{error}=7;
644 0           $self->{errorString}='No file specified';
645 0           $self->warn;
646 0           return undef;
647             }
648              
649             #make sure the file exists, under the config dir
650 0 0         if (! -f $self->{scm}->{baseDir}.'/'.$configDir.'/'.$file ) {
651 0           $self->{error}=8;
652 0           $self->{errorString}='The file does not exist in the configuration directory';
653 0           $self->warn;
654 0           return undef;
655             }
656              
657             #make sure we have a some perms passed
658 0 0         if ( ! defined( $id ) ){
659 0           $self->{error}=11;
660 0           $self->{errorString}='No value for the permissions specified';
661 0           $self->warn;
662 0           return undef;
663             }
664              
665             #make sure the perms are sane
666 0 0         if ( $id !~ /^[0123456789]*$/ ){
667 0           $self->{error}=12;
668 0           $self->{errorString}='"'.$id.'" does not appear to be a valid value';
669 0           warn($self->{module}.' '.$method.':'.$self->error.': '.$self->errorString);
670 0           return undef;
671             }
672              
673             #creates the .SysConfigManage if needed
674 0           my ($name,$path,$suffix) = fileparse($file);
675 0           my $scmd=$self->{scm}->{baseDir}.'/'.$configDir.'/'.$path.'/.SysConfigManage';
676 0 0         if ( ! -d $scmd ){
677 0 0         if ( ! mkdir( $scmd ) ){
678 0           $self->{error}=13;
679 0           $self->{errorString}='Unable to create "'.$scmd.'"';
680 0           $self->warn;
681 0           return undef;
682             }
683             }
684              
685             #makes sure that the perms dir exists
686 0           my $iddir=$scmd.'/GID';
687 0 0         if ( ! -d $iddir ){
688 0 0         if ( ! mkdir( $iddir ) ){
689 0           $self->{error}=14;
690 0           $self->{errorString}='Unable to create "'.$iddir.'"';
691 0           $self->warn;
692 0           return undef;
693             }
694             }
695              
696             #this is the file that will store the perms
697 0           my $idfile=$iddir.'/'.$name;
698              
699             #check if it exists
700 0           my $exists=0;
701 0 0         if ( -f $idfile ){
702 0           $exists=1;
703             }
704              
705 0           my $fh;
706 0 0         if ( ! open( $fh, '>', $idfile ) ){
707 0           $self->{error}=15;
708 0           $self->{errorString}='Unable to open "'.$idfile.'"';
709 0           $self->warn;
710 0           return undef;
711             }
712 0           print $fh $id;
713 0           close( $fh );
714              
715             #add it if it, if it did not exist previously
716 0 0         if ( ! $exists ){
717 0 0         if (defined( $self->{scm}->{addCommand} )) {
718 0           my $command=$self->{scm}->{addCommand};
719 0           my $newfile=shell_quote($idfile);
720            
721 0           $command=~s/\%\%\%file\%\%\%/$newfile/g;
722 0           system($command);
723 0           my $exit = $?<<8;
724 0 0         if ($exit ne '0') {
725 0           $self->{error}=16;
726 0           $self->{errorString}='The add command failed. command="'.$command.'" exit="'.$exit.'"';
727 0           $self->warn;
728 0           return undef;
729             }
730             }
731             }
732            
733 0           return 1;
734             }
735              
736             =head2 setUID
737              
738             This sets the UID for a file. This does require the numeric value.
739              
740             Three arguments are taken.
741              
742             The first one is the configuration directory to use. If none is
743             specified, it will automatically be choosen.
744              
745             The second is the config file to add the perms for.
746              
747             The third numeric value for the UID.
748              
749             $foo->setUID($configDir, $file, $uid);
750             if($foo->error){
751             warn('error:'.$foo->error.': '.$foo->errorString);
752             }
753              
754             =cut
755              
756             sub setUID{
757 0     0 1   my $self=$_[0];
758 0           my $configDir=$_[1];
759 0           my $file=$_[2];
760 0           my $id=$_[3];
761 0           my $method='setUID';
762              
763             #blank any previous errors
764 0 0         if (!$self->errorblank) {
765 0           return undef;
766             }
767              
768             #make sure we have a directory to use
769 0 0         if (!defined($configDir)) {
770 0           $configDir=$self->{scm}->selectConfigDir;
771 0 0         if ($self->{scm}->error) {
772 0           $self->{error}=5;
773 0           $self->{errorString}='Sys::Config::Manage->selectConfigDir errored error="'.
774             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
775 0           $self->warn;
776 0           return undef;
777             }
778             }
779              
780             #make sure the config directory is valid
781 0           my $valid=$self->{scm}->validConfigDirName($configDir);
782 0 0         if ($self->{scm}->error) {
783 0           $self->{error}=6;
784 0           $self->{errorString}='Sys::Config::Manage->validConfigDirName errored error="'.
785             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
786 0           $self->warn;
787 0           return undef;
788             }
789 0 0         if (defined( $valid )) {
790 0           $self->{error}=3;
791 0           $self->{errorString}='The configuration directory name '.$valid;
792 0           $self->warn;
793 0           return undef;
794             }
795              
796             #makes sure it exists
797 0 0         if ( ! -d $self->{scm}->{baseDir}.'/'.$configDir ) {
798 0           $self->{error}=4;
799 0           $self->{errorString}='The configuration directory, "'.$self->{baseDir}.'/'.$configDir.'", does not exist';
800 0           $self->warn;
801 0           return undef;
802             }
803              
804             #make value for the file is specified
805 0 0         if (!defined( $file )) {
806 0           $self->{error}=7;
807 0           $self->{errorString}='No file specified';
808 0           $self->warn;
809 0           return undef;
810             }
811              
812             #make sure the file exists, under the config dir
813 0 0         if (! -f $self->{scm}->{baseDir}.'/'.$configDir.'/'.$file ) {
814 0           $self->{error}=8;
815 0           $self->{errorString}='The file does not exist in the configuration directory';
816 0           $self->warn;
817 0           return undef;
818             }
819              
820             #make sure we have a some perms passed
821 0 0         if ( ! defined( $id ) ){
822 0           $self->{error}=11;
823 0           $self->{errorString}='No value for the permissions specified';
824 0           $self->warn;
825 0           return undef;
826             }
827              
828             #make sure the perms are sane
829 0 0         if ( $id !~ /^[0123456789]*$/ ){
830 0           $self->{error}=12;
831 0           $self->{errorString}='"'.$id.'" does not appear to be a valid value';
832 0           warn($self->{module}.' '.$method.':'.$self->error.': '.$self->errorString);
833 0           return undef;
834             }
835              
836             #creates the .SysConfigManage if needed
837 0           my ($name,$path,$suffix) = fileparse($file);
838 0           my $scmd=$self->{scm}->{baseDir}.'/'.$configDir.'/'.$path.'/.SysConfigManage';
839 0 0         if ( ! -d $scmd ){
840 0 0         if ( ! mkdir( $scmd ) ){
841 0           $self->{error}=13;
842 0           $self->{errorString}='Unable to create "'.$scmd.'"';
843 0           $self->warn;
844 0           return undef;
845             }
846             }
847              
848             #makes sure that the perms dir exists
849 0           my $iddir=$scmd.'/UID';
850 0 0         if ( ! -d $iddir ){
851 0 0         if ( ! mkdir( $iddir ) ){
852 0           $self->{error}=14;
853 0           $self->{errorString}='Unable to create "'.$iddir.'"';
854 0           $self->warn;
855 0           return undef;
856             }
857             }
858              
859             #this is the file that will store the perms
860 0           my $idfile=$iddir.'/'.$name;
861              
862             #check if it exists
863 0           my $exists=0;
864 0 0         if ( -f $idfile ){
865 0           $exists=1;
866             }
867              
868 0           my $fh;
869 0 0         if ( ! open( $fh, '>', $idfile ) ){
870 0           $self->{error}=15;
871 0           $self->{errorString}='Unable to open "'.$idfile.'"';
872 0           $self->warn;
873 0           return undef;
874             }
875 0           print $fh $id;
876 0           close( $fh );
877              
878             #add it if it, if it did not exist previously
879 0 0         if ( ! $exists ){
880 0 0         if (defined( $self->{scm}->{addCommand} )) {
881 0           my $command=$self->{scm}->{addCommand};
882 0           my $newfile=shell_quote($idfile);
883            
884 0           $command=~s/\%\%\%file\%\%\%/$newfile/g;
885 0           system($command);
886 0           my $exit = $?<<8;
887 0 0         if ($exit ne '0') {
888 0           $self->{error}=16;
889 0           $self->{errorString}='The add command failed. command="'.$command.'" exit="'.$exit.'"';
890 0           $self->warn;
891 0           return undef;
892             }
893             }
894             }
895            
896 0           return 1;
897             }
898              
899             =head2 setGIDfromFile
900              
901             This sets the GID for a file, from a already existing file.
902              
903             Three arguments are taken.
904              
905             The first one is the configuration directory to use. If none is
906             specified, it will automatically be choosen.
907              
908             The second is the config file to add the perms for.
909              
910             $foo->setUIDfromFile($configDir, $file);
911             if($foo->error){
912             warn('error:'.$foo->error.': '.$foo->errorString);
913             }
914              
915             =cut
916              
917             sub setGIDfromFile{
918 0     0 1   my $self=$_[0];
919 0           my $configDir=$_[1];
920 0           my $file=$_[2];
921 0           my $method='setGIDfromFile';
922              
923             #blank any previous errors
924 0 0         if (!$self->errorblank) {
925 0           return undef;
926             }
927              
928             #make sure we have a directory to use
929 0 0         if (!defined($configDir)) {
930 0           $configDir=$self->{scm}->selectConfigDir;
931 0 0         if ($self->{scm}->error) {
932 0           $self->{error}=5;
933 0           $self->{errorString}='Sys::Config::Manage->selectConfigDir errored error="'.
934             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
935 0           $self->warn;
936 0           return undef;
937             }
938             }
939              
940             #make sure the config directory is valid
941 0           my $valid=$self->{scm}->validConfigDirName($configDir);
942 0 0         if ($self->{scm}->error) {
943 0           $self->{error}=6;
944 0           $self->{errorString}='Sys::Config::Manage->validConfigDirName errored error="'.
945             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
946 0           $self->warn;
947 0           return undef;
948             }
949 0 0         if (defined( $valid )) {
950 0           $self->{error}=3;
951 0           $self->{errorString}='The configuration directory name '.$valid;
952 0           $self->warn;
953 0           return undef;
954             }
955              
956             #makes sure it exists
957 0 0         if ( ! -d $self->{scm}->{baseDir}.'/'.$configDir ) {
958 0           $self->{error}=4;
959 0           $self->{errorString}='The configuration directory, "'.$self->{baseDir}.'/'.$configDir.'", does not exist';
960 0           $self->warn;
961 0           return undef;
962             }
963              
964             #make value for the file is specified
965 0 0         if (!defined( $file )) {
966 0           $self->{error}=7;
967 0           $self->{errorString}='No file specified';
968 0           $self->warn;
969 0           return undef;
970             }
971              
972             #make sure the file exists, under the config dir
973 0 0         if (! -f $self->{scm}->{baseDir}.'/'.$configDir.'/'.$file ) {
974 0           $self->{error}=8;
975 0           $self->{errorString}='The file does not exist in the configuration directory';
976 0           $self->warn;
977 0           return undef;
978             }
979              
980             #make sure the file exists, on the fs
981 0 0         if (! -f $file ) {
982 0           $self->{error}=9;
983 0           $self->{errorString}='The file does not exist in the configuration directory';
984 0           $self->warn;
985 0           return undef;
986             }
987              
988             #make sure it is not under the base directory
989 0 0         if ( ! $self->{scm}->notUnderBase($file) ){
990 0           $self->{error}=10;
991 0           $self->{errorString}='"'.$file.'" exists under the base directory, "'.$self->{scm}->{baseDir}.'"';
992 0           $self->warn;
993 0           return undef;
994             }
995              
996             #stat the file
997 0           my $id = (stat($file))[5];
998            
999 0           $self->setGID($configDir, $file, $id);
1000 0 0         if ( $self->error ){
1001 0           warn($self->{module}.' '.$method.': : setUID errored');
1002 0           return undef;
1003             }
1004              
1005 0           return 1;
1006             }
1007              
1008             =head2 setUIDfromFile
1009              
1010             This sets the UID for a file, from a already existing file.
1011              
1012             Three arguments are taken.
1013              
1014             The first one is the configuration directory to use. If none is
1015             specified, it will automatically be choosen.
1016              
1017             The second is the config file to add the perms for.
1018              
1019             $foo->setUIDfromFile($configDir, $file);
1020             if($foo->error){
1021             warn('error:'.$foo->error.': '.$foo->errorString);
1022             }
1023              
1024             =cut
1025              
1026             sub setUIDfromFile{
1027 0     0 1   my $self=$_[0];
1028 0           my $configDir=$_[1];
1029 0           my $file=$_[2];
1030 0           my $method='setUIDfromFile';
1031              
1032             #blank any previous errors
1033 0 0         if (!$self->errorblank) {
1034 0           return undef;
1035             }
1036              
1037             #make sure we have a directory to use
1038 0 0         if (!defined($configDir)) {
1039 0           $configDir=$self->{scm}->selectConfigDir;
1040 0 0         if ($self->{scm}->error) {
1041 0           $self->{error}=5;
1042 0           $self->{errorString}='Sys::Config::Manage->selectConfigDir errored error="'.
1043             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
1044 0           $self->warn;
1045 0           return undef;
1046             }
1047             }
1048              
1049             #make sure the config directory is valid
1050 0           my $valid=$self->{scm}->validConfigDirName($configDir);
1051 0 0         if ($self->{scm}->error) {
1052 0           $self->{error}=6;
1053 0           $self->{errorString}='Sys::Config::Manage->validConfigDirName errored error="'.
1054             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
1055 0           $self->warn;
1056 0           return undef;
1057             }
1058 0 0         if (defined( $valid )) {
1059 0           $self->{error}=3;
1060 0           $self->{errorString}='The configuration directory name '.$valid;
1061 0           $self->warn;
1062 0           return undef;
1063             }
1064              
1065             #makes sure it exists
1066 0 0         if ( ! -d $self->{scm}->{baseDir}.'/'.$configDir ) {
1067 0           $self->{error}=4;
1068 0           $self->{errorString}='The configuration directory, "'.$self->{baseDir}.'/'.$configDir.'", does not exist';
1069 0           $self->warn;
1070 0           return undef;
1071             }
1072              
1073             #make value for the file is specified
1074 0 0         if (!defined( $file )) {
1075 0           $self->{error}=7;
1076 0           $self->{errorString}='No file specified';
1077 0           $self->warn;
1078 0           return undef;
1079             }
1080              
1081             #make sure the file exists, under the config dir
1082 0 0         if (! -f $self->{scm}->{baseDir}.'/'.$configDir.'/'.$file ) {
1083 0           $self->{error}=8;
1084 0           $self->{errorString}='The file does not exist in the configuration directory';
1085 0           $self->warn;
1086 0           return undef;
1087             }
1088              
1089             #make sure the file exists, on the fs
1090 0 0         if (! -f $file ) {
1091 0           $self->{error}=9;
1092 0           $self->{errorString}='The file does not exist in the configuration directory';
1093 0           $self->warn;
1094 0           return undef;
1095             }
1096              
1097             #make sure it is not under the base directory
1098 0 0         if ( ! $self->{scm}->notUnderBase($file) ){
1099 0           $self->{error}=10;
1100 0           $self->{errorString}='"'.$file.'" exists under the base directory, "'.$self->{scm}->{baseDir}.'"';
1101 0           $self->warn;
1102 0           return undef;
1103             }
1104              
1105             #stat the file
1106 0           my $id = (stat($file))[4];
1107            
1108 0           $self->setUID($configDir, $file, $id);
1109 0 0         if ( $self->error ){
1110 0           warn($self->{module}.' '.$method.': : setUID errored');
1111 0           return undef;
1112             }
1113              
1114 0           return 1;
1115             }
1116              
1117             =head2 upSync
1118              
1119             This syncs the file user/group up from the file system to
1120             configuration directory.
1121              
1122             Two arguments can be used.
1123              
1124             The first is the configuration directory. If not specified, it will
1125             be automaticallly choosen.
1126              
1127             The second is the files to sync. If not specified, all files will
1128             be synced.
1129              
1130             #sync the specified files
1131             $foo->upSync( $configDir, \@files);
1132             if($foo->error){
1133             warn('error:'.$foo->error.': '.$foo->errorString);
1134             }
1135              
1136             #syncs all the files
1137             $foo->upSync( $configDir );
1138             if($foo->error){
1139             warn('error:'.$foo->error.': '.$foo->errorString);
1140             }
1141              
1142             =cut
1143              
1144             sub upSync{
1145 0     0 1   my $self=$_[0];
1146 0           my $configDir=$_[1];
1147 0           my @files;
1148 0 0         if (defined($_[2])) {
1149 0           @files=@{$_[2]};
  0            
1150             }
1151 0           my $method='upSync';
1152            
1153             #blank any previous errors
1154 0 0         if (!$self->errorblank) {
1155 0           return undef;
1156             }
1157            
1158             #make sure we have a directory to use
1159 0 0         if (!defined($configDir)) {
1160 0           $configDir=$self->{scm}->selectConfigDir;
1161 0 0         if ($self->{scm}->error) {
1162 0           $self->{error}=5;
1163 0           $self->{errorString}='Sys::Config::Manage->selectConfigDir errored error="'.
1164             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
1165 0           $self->warn;
1166 0           return undef;
1167             }
1168             }
1169            
1170             #make sure the config directory is valid
1171 0           my $valid=$self->{scm}->validConfigDirName($configDir);
1172 0 0         if ($self->{scm}->error) {
1173 0           $self->{error}=6;
1174 0           $self->{errorString}='Sys::Config::Manage->validConfigDirName errored error="'.
1175             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
1176 0           $self->warn;
1177 0           return undef;
1178             }
1179 0 0         if (defined( $valid )) {
1180 0           $self->{error}=3;
1181 0           $self->{errorString}='The configuration directory name '.$valid;
1182 0           $self->warn;
1183 0           return undef;
1184             }
1185            
1186             #makes sure it exists
1187 0 0         if ( ! -d $self->{scm}->{baseDir}.'/'.$configDir ) {
1188 0           $self->{error}=4;
1189 0           $self->{errorString}='The configuration directory, "'.$self->{baseDir}.'/'.$configDir.'", does not exist';
1190 0           $self->warn;
1191 0           return undef;
1192             }
1193              
1194             #checks and make sure all the files exist
1195 0           my $int=0;
1196 0 0         if(defined( $files[$int] )){
1197 0           my @allfiles=$self->{scm}->listConfigFiles($configDir);
1198 0 0         if( $self->{scm}->error ){
1199 0           $self->{error}='16';
1200 0           $self->{errorString}='Sys::Config::Manage->listConfigFiles errored error="'.
1201             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
1202 0           $self->warn;
1203 0           return undef;
1204             }
1205            
1206             #make sure each file asked to be synced is tracked
1207 0           while(defined( $files[$int] )){
1208              
1209 0           my $matched=0;
1210 0           my $int2=0;
1211 0           while(defined( $allfiles[$int2] )){
1212 0 0         if( $files[$int] eq $allfiles[$int2] ){
1213 0           $matched=1;
1214             }
1215              
1216 0           $int2++;
1217             }
1218              
1219 0 0         if(! $matched){
1220 0           $self->{error}='8';
1221 0           $self->{errorString}='"'.$files[$int].'" does not exist under the configuration directory, "'.$configDir.'",';
1222 0           $self->warn;
1223 0           return undef;
1224             }
1225              
1226 0           $int++;
1227             }
1228              
1229             }else{
1230             #if we get here, no files have been specified so we do them all
1231 0           @files=$self->{scm}->listConfigFiles($configDir);
1232 0 0         if( $self->{scm}->error ){
1233 0           $self->{error}='16';
1234 0           $self->{errorString}='Sys::Config::Manage->listConfigFiles errored error="'.
1235             $self->{scm}->error.'" errorString="'.$self->{scm}->errorString.'"';
1236 0           $self->warn;
1237 0           return undef;
1238             }
1239             }
1240              
1241             #process each file
1242 0           $int=0;
1243 0           while( defined( $files[$int] ) ){
1244             #try to sync the UID from what it currently is
1245 0           $self->setUIDfromFile( $configDir, $files[$int] );
1246 0 0         if( $self->error ){
1247 0           warn( $self->{module}.' '.$method.': $self->setUIDfromFile( $configDir, $files[$int] ) errored' );
1248 0           return undef;
1249             }
1250              
1251             #try to sync the GID from what it currently is
1252 0           $self->setGIDfromFile( $configDir, $files[$int] );
1253 0 0         if( $self->error ){
1254 0           warn( $self->{module}.' '.$method.': $self->setGIDfromFile( $configDir, $files[$int] ) errored' );
1255 0           return undef;
1256             }
1257            
1258 0           $int++;
1259             }
1260              
1261 0           return 1;
1262             }
1263              
1264             =head1 ERROR CODES
1265              
1266             =head2 1
1267              
1268             Nothing passed for the Sys::Config::Manage object.
1269              
1270             =head2 2
1271              
1272             $args{scm} is not a Sys::Config::Manage object.
1273              
1274             =head2 3
1275              
1276             Invalid configuration directory name.
1277              
1278             =head2 4
1279              
1280             The configuration directory does not exist.
1281              
1282             =head2 5
1283              
1284             Sys::Config::Manage->selectConfigDir errored.
1285              
1286             =head2 6
1287              
1288             Sys::Config::Manage->validConfigDirName errored.
1289              
1290             =head2 7
1291              
1292             No filename specified.
1293              
1294             =head2 8
1295              
1296             The specified file does not exist under the configuration directory.
1297              
1298             =head2 9
1299              
1300             The specified file does not exist on the file system.
1301              
1302             =head2 10
1303              
1304             The file specified file is under the base directory.
1305              
1306             =head2 11
1307              
1308             No value for the permissions specified.
1309              
1310             =head2 12
1311              
1312             The value specified for the [UG]ID does not appear to be valid.
1313              
1314             Validity is checked via the regexp below.
1315              
1316             /^[0123456789]*$/
1317              
1318             =head2 13
1319              
1320             The ".SysConfigManage" directory does not exist and could not be created.
1321              
1322             =head2 14
1323              
1324             The ".SysConfigManage/UID" directory does not exist and could not be created.
1325              
1326             =head2 15
1327              
1328             Unable to opens the [GU]ID file.
1329              
1330             =head2 16
1331              
1332             The add command failed.
1333              
1334             =head1 AUTHOR
1335              
1336             Zane C. Bowers-Hadley, C<< >>
1337              
1338             =head1 BUGS
1339              
1340             Please report any bugs or feature requests to C, or through
1341             the web interface at L. I will
1342             be notified, and then you'll automatically be notified of progress on your bug as I make changes.
1343              
1344             =head1 SUPPORT
1345              
1346             You can find documentation for this module with the perldoc command.
1347              
1348             perldoc Sys::Config::Manage::Ownership
1349              
1350              
1351             You can also look for information at:
1352              
1353             =over 4
1354              
1355             =item * RT: CPAN's request tracker
1356              
1357             L
1358              
1359             =item * AnnoCPAN: Annotated CPAN documentation
1360              
1361             L
1362              
1363             =item * CPAN Ratings
1364              
1365             L
1366              
1367             =item * Search CPAN
1368              
1369             L
1370              
1371             =back
1372              
1373              
1374             =head1 ACKNOWLEDGEMENTS
1375              
1376              
1377             =head1 LICENSE AND COPYRIGHT
1378              
1379             Copyright 2011 Zane C. Bowers-Hadley.
1380              
1381             This program is free software; you can redistribute it and/or modify it
1382             under the terms of either: the GNU General Public License as published
1383             by the Free Software Foundation; or the Artistic License.
1384              
1385             See http://dev.perl.org/licenses/ for more information.
1386              
1387              
1388             =cut
1389              
1390             1; # End of Sys::Config::Manage::Ownership