01052da023611b1710f1a71cb2013d874a40b209
[mirror/scst/.git] / scstadmin / scstadmin
1 #!/usr/bin/perl
2 $Version  = 'SCST Configurator v1.0.2';
3
4 # Configures SCST
5 #
6 # Author:       Mark R. Buechler
7 # License:      GPLv2
8 # Copyright (c) 2005-2007 Mark R. Buechler
9
10 sub usage
11   {
12     die <<"EndUsage";
13 $Version
14
15 Usage:
16 General Operations
17      -config <config>     : Configure SCST given the specified configuration file.
18      -ClearConfig         : Clear all SCST configuration.
19      -WriteConfig <file>  : Writes the current configuration out to the specified file.
20      -checkConfig <file>  : Checks the saved configuration in the specified file.
21      -sessions            : List current initiator sessions.
22      
23 Target Driver Operations
24      -enable <wwn|host>   : Enable target mode for driver at specified WWN or host.
25      -disable <wwn|host>  : Disable target mode for driver at specified WWN or host.
26
27 Devices Operations
28      -adddev <device>     : Adds a device to a handler.
29          -handler <handler>
30          -path <path>
31          -options <options>
32          -blocksize <bytes>
33      -RemoveDev <device>  : Remove a device from a handler.
34          -handler <handler>
35  
36 Users Operations
37      -adduser <user>      : Adds a user to a security group.
38          -group <group>
39      -RemoveUser <user>   : Delete a user from a security group.
40          -group <group>
41      -ClearUsers          : Clear all users from a given security group.
42          -group <group>
43
44 Groups Operations
45      -addgroup <group>    : Add a given group to available security groups.
46      -RemoveGroup <group> : Remove a give group from available security groups.
47      
48 Assignments Operations
49      -assigndev <device>  : Assign a given device to a security group.
50          -group <group>
51          -lun <lun>
52      -ReleaseDev <device> : Remove a given device from a security group.
53          -group <group>
54      -ClearDevs           : Clear all device assignments for a security group.
55          -group <group>
56
57 Options
58      -ForceConfig         : Force all confuration changes, even deletions (DANGER!).
59   
60 Debugging (limited support)
61      -debug               : Debug mode - don\'t do anything destructive.
62
63 Available Handlers:
64       disk, vdisk, disk_perf, cdrom, vcdrom, changer, modisk, modisk_perf, tape, tape_perf
65
66 Available Options for create and open:
67       WRITE_THROUGH, READ_ONLY, O_DIRECT, NULLIO, NV_CACHE, BLOCK_IO, REMOVABLE
68
69 Examples:
70      Enable target mode for fibre card specifying its WWN
71        scstadmin -enable 50:06:0B:00:00:39:71:78
72
73      Disable target mode for SCSI host specifying host number
74        scstadmin -disable host4 
75
76      Create a new security group:
77        scstadmin -addgroup HOST01
78        
79      Create a device given an already existing disk file:
80        scstadmin -adddev DISK01 -handler vdisk -path /vdisks/disk01.dsk -options READ_ONLY,WRITE_THROUGH
81        
82      Assign a device to a security group:
83        scstadmin -assigndev DISK01 -group HOST01 -lun 1
84
85 EndUsage
86   }
87
88 use SCST::SCST;
89 use Getopt::Long;
90 use IO::File;
91 use IO::Dir;
92 use POSIX;
93 use strict;
94
95 my $_DEF_CONFIG_ = '/etc/scst.conf';
96
97 my $TRUE  = 1;
98 my $FALSE = 0;
99
100 my $_MAX_LUNS_      = 255;
101 my $_DEFAULT_GROUP_ = 'Default';
102
103 my $_SCSI_CLASS_    = '/sys/class/scsi_host';
104 my $_FC_CLASS_      = '/sys/class/fc_host';
105
106 my $SCST;
107 my $DEVICES;
108 my $TARGETS;
109 my %USERS;
110 my %ASSIGNMENTS;
111 my %HANDLERS;
112 my %GROUPS;
113 my $_DEBUG_;
114
115 my %_HANDLER_MAP_ = ('cdrom' => $SCST::SCST::CDROM_TYPE,
116                      'changer' => $SCST::SCST::CHANGER_TYPE,
117                      'disk' => $SCST::SCST::DISK_TYPE,
118                      'vdisk' => $SCST::SCST::VDISK_TYPE,
119                      'vcdrom' => $SCST::SCST::VCDROM_TYPE,
120                      'disk_perf' => $SCST::SCST::DISKPERF_TYPE,
121                      'modisk' => $SCST::SCST::MODISK_TYPE,
122                      'modisk_perf' => $SCST::SCST::MODISKPERF_TYPE,
123                      'tape' => $SCST::SCST::TAPE_TYPE,
124                      'tape_perf' => $SCST::SCST::TAPEPERF_TYPE,
125                      'processor' => $SCST::SCST::PROCESSOR_TYPE,
126                 # Add in the dev_ names as well
127                      'dev_cdrom' => $SCST::SCST::CDROM_TYPE,
128                      'dev_changer' => $SCST::SCST::CHANGER_TYPE,
129                      'dev_disk' => $SCST::SCST::DISK_TYPE,
130                      'dev_disk_perf' => $SCST::SCST::DISKPERF_TYPE,
131                      'dev_modisk' => $SCST::SCST::MODISK_TYPE,
132                      'dev_modisk_perf' => $SCST::SCST::MODISKPERF_TYPE,
133                      'dev_tape' => $SCST::SCST::TAPE_TYPE,
134                      'dev_tape_perf' => $SCST::SCST::TAPEPERF_TYPE,
135                      'dev_processor' => $SCST::SCST::PROCESSOR_TYPE);
136                      
137 my %_REVERSE_MAP_ = ($SCST::SCST::CDROM_TYPE => 'cdrom',
138                      $SCST::SCST::CHANGER_TYPE => 'changer',
139                      $SCST::SCST::DISK_TYPE => 'disk',
140                      $SCST::SCST::VDISK_TYPE => 'vdisk',
141                      $SCST::SCST::VCDROM_TYPE => 'vcdrom',
142                      $SCST::SCST::DISKPERF_TYPE => 'disk_perf',
143                      $SCST::SCST::MODISK_TYPE => 'modisk',
144                      $SCST::SCST::MODISKPERF_TYPE => 'modisk_perf',
145                      $SCST::SCST::TAPE_TYPE => 'tape',
146                      $SCST::SCST::TAPEPERF_TYPE => 'tape_perf',
147                      $SCST::SCST::PROCESSOR_TYPE => 'processor');
148
149 my %_HANDLER_TYPE_MAP_ = ($SCST::SCST::IOTYPE_PHYSICAL => 'physical',
150                           $SCST::SCST::IOTYPE_VIRTUAL => 'virtual',
151                           $SCST::SCST::IOTYPE_PERFORMANCE => 'performance');
152
153 $SIG{INT} = \&commitSuicide;
154
155 use vars qw($Version);
156
157 POSIX::setsid();
158
159 &main();
160
161 sub getArgs {
162         my $applyConfig;
163         my $forceConfig;
164         my $clearConfig;
165         my $writeConfig;
166         my $checkConfig;
167         my $showSessions;
168         my $addDev;
169         my $devPath;
170         my $removeDev;
171         my $addUser;
172         my $removeUser;
173         my $clearUsers;
174         my $addGroup;
175         my $removeGroup;
176         my $assignDev;
177         my $releaseDev;
178         my $clearDevs;
179         my $devLun;
180         my $handler;
181         my $group;
182         my $options;
183         my $blocksize;
184         my $enable;
185         my $disable;
186
187         my $p = new Getopt::Long::Parser;
188
189         if (!$p->getoptions('config:s'          => \$applyConfig,
190                             'ClearConfig'       => \$clearConfig,
191                             'ForceConfig'       => \$forceConfig,
192                             'WriteConfig=s'     => \$writeConfig,
193                             'checkConfig=s'     => \$checkConfig,
194                             'sessions'          => \$showSessions,
195                             'adddev=s'          => \$addDev,
196                             'path=s'            => \$devPath,
197                             'RemoveDev=s'       => \$removeDev,
198                             'lun=s'             => \$devLun,
199                             'adduser=s'         => \$addUser,
200                             'RemoveUser=s'      => \$removeUser,
201                             'ClearUsers'        => \$clearUsers,
202                             'addgroup=s'        => \$addGroup,
203                             'RemoveGroup=s'     => \$removeGroup,
204                             'assigndev=s'       => \$assignDev,
205                             'ReleaseDev=s'      => \$releaseDev,
206                             'ClearDevs'         => \$clearDevs,
207                             'handler=s'         => \$handler,
208                             'group=s'           => \$group,
209                             'options=s'         => \$options,
210                             'blocksize=s'       => \$blocksize,
211                             'enable=s'          => \$enable,
212                             'disable=s'         => \$disable,
213                             'debug'             => \$_DEBUG_)) {
214                 &usage();
215         }
216
217         if ((defined($enable) && !$enable) || (defined($disable) && !$disable)) {
218                 print "Argument -enable/-disable requires a WWN or host.\n\n";
219                 usage();
220         }
221
222         if ($handler && !$_HANDLER_MAP_{$handler}) {
223                 print "Invalid handler '$handler' specified. Available handlers are:\n\n";
224                 foreach my $_handler (keys %_HANDLER_MAP_) {
225                         print "\t$_handler\n";
226                 }
227                 print "\n";
228                 exit 1;
229         }
230
231         if ($addDev && !($handler && $devPath)) {
232                 print "Please specify -handler and -path with -adddev.\n\n";
233                 usage();
234         }
235
236         if (defined($blocksize) && !$blocksize) {
237                 print "Please specify bytes with -blocksize.\n\n";
238                 usage();
239         }
240
241         if ($blocksize && !$addDev) {
242                 print "Please specify -adddev with -blocksize.\n";
243                 usage();
244         }
245
246         if (defined($forceConfig) && !defined($applyConfig)) {
247                 print "Please specify -config with -ForceConfig.\n\n";
248                 usage();
249         }
250
251         if ($removeDev && !$handler) {
252                 print "Please specify -handler with -RemoveDev.\n\n";
253                 usage();
254         }
255
256         if ($addUser && !defined($group)) {
257                 print "Please specify -group with -adduser.\n\n";
258                 usage();
259         }
260
261         if ($removeUser && !defined($group)) {
262                 print "Please specify -group with -RemoveUser.\n\n";
263                 usage();
264         }
265
266         if ($clearUsers && !defined($group)) {
267                 print "Please specify -group with -ClearUsers.\n\n";
268                 usage();
269         }
270
271         if ($assignDev && !(defined($group) && defined($devLun))) {
272                 print "Please specify -group and -lun with -assigndev.\n\n";
273                 usage();
274         }
275
276         if ($releaseDev && !defined($group)) {
277                 print "Please specify -group with -ReleaseDev.\n\n";
278                 usage();
279         }
280
281         if ($clearDevs && !defined($group)) {
282                 print "Please specify -group with -ClearDevs.\n\n";
283                 usage();
284         }
285
286         if (defined($writeConfig) && !$writeConfig) {
287                 print "Please specify a file name to write configuration to..\n\n";
288                 usage();
289         }
290
291         $_DEBUG_ = $TRUE if (defined($_DEBUG_));
292
293         $forceConfig = $TRUE if (defined($forceConfig));
294         $showSessions = $TRUE if (defined($showSessions));
295
296         $enable =~ tr/A-Z/a-z/; $disable =~ tr/A-Z/a-z/;
297         $options =~ tr/a-z/A-Z/ if ($options);
298
299         if ((defined($showSessions) + defined($addDev) + defined($removeDev) +
300              defined($addUser) + defined($enable) + defined($disable) +
301              defined($removeUser) + defined($clearUsers) + defined($assignDev) +
302              defined($releaseDev) + defined($clearDevs) + defined($applyConfig) +
303              defined($clearConfig) + defined($writeConfig) + defined($checkConfig)) > 1) {
304                 print "Please specify only one operation at a time.\n";
305                 usage();
306         }
307
308         $applyConfig = $_DEF_CONFIG_ if (defined($applyConfig) && !$applyConfig);
309         $checkConfig = $_DEF_CONFIG_ if (defined($checkConfig) && !$checkConfig);
310
311         return ($enable, $disable, $addDev, $devPath, $devLun, $removeDev, $addUser, $removeUser,
312                 $clearUsers, $addGroup, $removeGroup, $assignDev, $releaseDev, $clearDevs,
313                 $handler, $group, $options, $blocksize, $applyConfig, $forceConfig,
314                 $clearConfig, $writeConfig, $checkConfig, $showSessions);
315 }
316
317 sub main {
318         my $rc;
319
320         STDOUT->autoflush(1);
321
322         # We need to run as root
323         if ( $> ) {die("This program must run as root.\n");}
324
325         my ($enable, $disable, $addDev, $devPath, $devLun, $removeDev, $addUser, $removeUser,
326             $clearUsers, $addGroup, $removeGroup, $assignDev, $releaseDev, $clearDevs,
327             $handler, $group, $options, $blocksize, $applyConfig, $forceConfig,
328             $clearConfig, $writeConfig, $checkConfig, $showSessions) = getArgs();
329
330         $SCST = new SCST::SCST($_DEBUG_);
331
332         readWorkingConfig();
333
334         SWITCH: {
335                 $applyConfig && do {
336                         if ($forceConfig) {
337                                 $rc = applyConfiguration($applyConfig, $FALSE, $TRUE);
338                                 die("Configuration errors found, aborting.\n") if ($rc);
339
340                                 print "\nConfiguration will apply in 10 seconds, type ctrl-c to abort..\n";
341                                 sleep 10;
342                         }
343
344                         readWorkingConfig();
345                         $rc = applyConfiguration($applyConfig, $forceConfig, $FALSE);
346                         last SWITCH;
347                 };
348                 $checkConfig && do {
349                         $rc = applyConfiguration($checkConfig, $FALSE, $TRUE);
350                         last SWITCH;
351                 };
352                 $writeConfig && do {
353                         $rc = writeConfiguration($writeConfig);
354                         last SWITCH;
355                 };
356                 $showSessions && do {
357                         $rc = showSessions();
358                         last SWITCH;
359                 };
360                 defined($clearConfig) && do {
361                         $rc = clearConfiguration();
362                         last SWITCH;
363                 };
364                 $addDev && do {
365                         $rc = addDevice($handler, $addDev, $devPath, $options, $blocksize);
366                         last SWITCH;
367                 };
368                 $removeDev && do {
369                         $rc = removeDevice($handler, $removeDev);
370                         last SWITCH;
371                 };
372                 $addUser && do {
373                         $rc = addUser($group, $addUser);
374                         last SWITCH;
375                 };
376                 $removeUser && do {
377                         $rc = removeUser($group, $removeUser);
378                         last SWITCH;
379                 };
380                 defined($clearUsers) && do {
381                         $rc = clearUsers($group);
382                         last SWITCH;
383                 };
384                 $addGroup && do {
385                         $rc = addGroup($addGroup);
386                         last SWITCH;
387                 };
388                 $removeGroup && do {
389                         $rc = removeGroup($removeGroup);
390                         last SWITCH;
391                 };
392                 $assignDev && do {
393                         $rc = assignDevice($group, $assignDev, $devLun);
394                         last SWITCH;
395                 };
396                 $releaseDev && do {
397                         $rc = releaseDevice($group, $releaseDev);
398                         last SWITCH;
399                 };
400                 defined($clearDevs) && do {
401                         $rc = clearDevices($group);
402                         last SWITCH;
403                 };
404                 $enable && do {
405                         $enable = unformatTarget($enable);
406                         $rc = enableTarget($enable, $TRUE);
407                         last SWITCH;
408                 };
409                 $disable && do {
410                         $disable = unformatTarget($disable);
411                         $rc = enableTarget($disable, $FALSE);
412                         last SWITCH;
413                 };
414
415                 print "No valid operations specified.\n";
416                 usage();
417                 exit $TRUE;
418         }
419
420         print "All done.\n";
421
422         exit $rc;
423 }
424
425 sub readWorkingConfig {
426         my %empty;
427
428         print "Collecting current configuration.. ";
429
430         $TARGETS  = undef;
431         $DEVICES  = undef;
432         %HANDLERS = ();
433         %GROUPS   = ();
434         %USERS    = ();
435
436         my $eHandlers = $SCST->handlers();
437
438         immediateExit($SCST->errorString());
439
440         foreach my $handler (@{$eHandlers}) {
441                 $HANDLERS{$handler}++; # For quick lookups
442         }
443
444         $TARGETS = targets();
445
446         $DEVICES = $SCST->devices();
447         immediateExit($SCST->errorString());
448
449         my $_eGroups = $SCST->groups();
450         immediateExit($SCST->errorString());
451
452         foreach my $group (@{$_eGroups}) {
453                 $GROUPS{$group}++;
454                 $ASSIGNMENTS{$group} = $SCST->groupDevices($group);
455                 my $eUsers = $SCST->users($group);
456
457                 foreach my $user (@{$eUsers}) {
458                         $USERS{$group}->{$user}++; # For quick lookups
459                 }
460                 $USERS{$group} = \%empty if (!$USERS{$group});
461         }
462
463         print "done.\n\n";
464 }
465
466 sub writeConfiguration {
467         my $file = shift;
468
469         if (-f $file) {
470                 if (!unlink $file) {
471                         print "Failed to save current configuration, specified ".
472                           "file exists and cannot be deleted.\n";
473                         return 1;
474                 }
475         }
476
477         my $io = new IO::File $file, O_CREAT|O_WRONLY;
478
479         if (!$io) {
480                 print "Failed to save configuration to file '$file': $!\n";
481                 return 1;
482         }
483
484         print "Writing current configuration to file '$file'.. ";
485
486         print $io "# Automatically generated by $Version.\n\n";
487
488         # Device information
489         foreach my $handler (sort keys %HANDLERS) {
490                 print $io "[HANDLER ".$_REVERSE_MAP_{$handler}."]\n";
491
492                 if ($SCST->handlerType($handler) == $SCST::SCST::IOTYPE_VIRTUAL) {
493                         print $io "#DEVICE <vdisk name>,<device path>";
494                         if ($handler == $SCST::SCST::VDISK_TYPE) {
495                                 print $io ",<options>,<block size>\n";
496                         } else {
497                                 print $io "\n";
498                         }
499                 } else {
500                         print $io "#DEVICE <H:C:I:L>\n";
501                 }
502
503                 my $devices = $SCST->handlerDevices($handler);
504
505                 immediateExit($SCST->errorString());
506
507                 foreach my $device (sort keys %{$devices}) {
508                         my $options = $$devices{$device}->{'OPTIONS'};
509
510                         $options =~ s/\,/\|/g;
511
512                         print $io "DEVICE $device,".$$devices{$device}->{'PATH'};
513                         print $io ",$options";
514                         print $io ",".$$devices{$device}->{'BLOCKSIZE'};
515                         print $io "\n";
516                 }
517
518                 print $io "\n";
519         }
520
521         # User configuration
522         foreach my $group (sort keys %USERS) {
523                 print $io "[GROUP $group]\n";
524                 print $io "#USER <user wwn>\n";
525
526                 foreach my $user (keys %{$USERS{$group}}) {
527                         print $io "USER $user\n";
528                 }
529
530                 print $io "\n";
531         }
532
533         # Assignments configuration
534         foreach my $group (sort keys %ASSIGNMENTS) {
535                 print $io "[ASSIGNMENT $group]\n";
536                 print $io "#DEVICE <device name>,<lun>\n";
537
538                 my $pointer = $ASSIGNMENTS{$group};
539                 foreach my $device (sort keys %{$pointer}) {
540                         print $io "DEVICE $device,".$$pointer{$device}."\n";
541                 }
542
543                 print $io "\n";
544         }
545
546         # Targets configuration
547         foreach my $type ('enable', 'disable') {
548                 print $io "[TARGETS $type]\n";
549                 print $io "#HOST <wwn identifier>\n";
550
551                 foreach my $target (sort keys %{$TARGETS}) {
552                         if ((($type eq 'enable') && $$TARGETS{$target}->{'enabled'}) ||
553                             (($type eq 'disable') && !$$TARGETS{$target}->{'enabled'})) {
554                                 my $f_target = formatTarget($target);
555                                 print $io "HOST $f_target\n" if (!$$TARGETS{$target}->{'duplicate'});
556                         }
557                 }
558
559                 print $io "\n";
560         }
561
562         print "done\n";
563
564         close $io;
565
566         return 0;
567 }
568
569 sub applyConfiguration {
570         my $confile = shift;
571         my $force = shift;
572         my $check = shift;
573         my $config = readConfig($confile);
574         my $errs;
575         my $changes = 0;
576
577         my %used_devs;
578         my %used_users;
579         my %used_assignments;
580         my %empty;
581
582         # Cache device/handler configuration
583         foreach my $entry (keys %{$$config{'HANDLER'}}) {
584                 foreach my $device (@{$$config{'HANDLER'}->{$entry}->{'DEVICE'}}) {
585                         my($vname, undef) = split(/\,/, $device, 2);
586                         $vname = cleanupString($vname);
587                         $used_devs{$vname} = $entry;
588                 }
589         }
590
591         # Cache user/group configuration
592         foreach my $group (keys %{$$config{'GROUP'}}) {
593                 foreach my $user (@{$$config{'GROUP'}->{$group}->{'USER'}}) {
594                         $used_users{$group}->{$user}++;
595                 }
596                 $used_users{$group} = \%empty if (!$used_users{$group});
597         }
598
599         # Cache device association configuration
600         foreach my $group (keys %{$$config{'ASSIGNMENT'}}) {
601                 foreach my $device (@{$$config{'ASSIGNMENT'}->{$group}->{'DEVICE'}}) {
602                         my($vname, $lun) = split(/\,/, $device);
603                         $vname = cleanupString($vname);
604                         $used_assignments{$group}->{$vname} = $lun;
605                 }
606         }
607
608         # If -ForceConfig is used, check for configurations which we've deleted but are still active.
609         if ($force || $check) {
610                 # Associations
611                 foreach my $group (sort keys %ASSIGNMENTS) {
612                         if (!defined($used_assignments{$group}) && (keys %{$ASSIGNMENTS{$group}})) {
613                                 print "\t-> WARNING: Group '$group' has no associations in saved configuration";
614
615                                 if (!$check) {
616                                         print ", clearing all associations.\n";
617                                         if (clearDevices($group)) {
618                                                 $errs++;
619                                         } else {
620                                                 $changes++;
621                                         }
622                                 } else {
623                                         print ".\n";
624                                         $changes++;
625                                 }
626                         } else {
627                                 my $_assignments = $ASSIGNMENTS{$group};
628
629                                 foreach my $device (sort keys %{$_assignments}) {
630                                         if (!defined($used_assignments{$group}->{$device}) ||
631                                            ($$_assignments{$device} != $used_assignments{$group}->{$device})) {
632                                                 if ($$_assignments{$device} != $used_assignments{$group}->{$device}) {
633                                                         print "\t-> WARNING: Device '$device' assigned to group '$group' is at LUN ".
634                                                           $used_assignments{$group}->{$device}.
635                                                           " whereas working configuration reflects LUN ".$$_assignments{$device}; 
636                                                 } else {
637                                                         print "\t-> WARNING: Device '$device' is not associated with group ".
638                                                           "'$group' in saved configuration";
639                                                 }
640
641                                                 if (!$check) {
642                                                         print ", releasing.\n";
643                                                         if (releaseDevice($group, $device)) {
644                                                                 $errs++;
645                                                         } else {
646                                                                 $changes++;
647                                                         }
648                                                 } else {
649                                                         print ".\n";
650                                                         $changes++;
651                                                 }
652                                         }
653                                 }
654                         }
655                 }
656
657                 # Users & Groups
658                 foreach my $group (sort keys %USERS) {
659                         if (!defined($used_users{$group})) {
660                                 print "\t-> WARNING: Group '$group' does not exist in saved configuration";
661
662                                 if (!$check) {
663                                         print ", removing.\n";
664                                         if (clearUsers($group)) {
665                                                 $errs++;
666                                         } else {
667                                                 $changes++;
668                                         }
669
670                                         if (removeGroup($group)) {
671                                                 $errs++;
672                                         } else {
673                                                 $changes++;
674                                         }
675                                 } else {
676                                         print ".\n";
677                                         $changes++;
678                                 }
679                         } else {
680                                 foreach my $user (sort keys %{$USERS{$group}}) {
681                                         if (!defined($used_users{$group}->{$user})) {
682                                                 print "\t-> WARNING: User '$user' is not defined as part of group '$group' ".
683                                                   "in saved configuration";
684
685                                                 if (!$check) {
686                                                         print ", removing.\n";
687                                                         if (removeUser($group, $user)) {
688                                                                 $errs++;
689                                                         } else {
690                                                                 $changes++;
691                                                         }
692                                                 } else {
693                                                         print ".\n";
694                                                         $changes++;
695                                                 }
696                                         }
697                                 }
698                         }
699                 }
700
701                 # Devices
702                 foreach my $device (sort keys %{$DEVICES}) {
703                         if ($$DEVICES{$device} && !defined($used_devs{$device})) {
704                                 # Device gone, but is it still assigned to a group?
705                                 my $isAssigned = $FALSE;
706                                 foreach my $group (sort keys %used_assignments) {
707                                         if (defined($used_assignments{$group}->{$device})) {
708                                                 print "\t-> WARNING: Device '$device' is not defined in saved configuration, ".
709                                                   "however, it is still assigned to group '$group'! Ignoring removal.\n";
710                                                 $isAssigned = $TRUE;
711                                         }
712                                 }
713
714                                 if (!$isAssigned && ($SCST->handlerType($$DEVICES{$device}) == $SCST::SCST::IOTYPE_VIRTUAL)) {
715                                         print "\t-> WARNING: Device '$device' is not defined in saved configuration";
716
717                                         if (!$check) {
718                                                 print ", removing.\n";
719                                                 if (removeDevice($device)) {
720                                                         $errs++;
721                                                 } else {
722                                                         $changes++;
723                                                 }
724                                         } else {
725                                                 print ".\n";
726                                                 $changes++;
727                                         }
728                                 }
729                         } else {
730                                 # Handler change
731                                 if ($_HANDLER_MAP_{$used_devs{$device}} != $$DEVICES{$device}) {
732                                         my $handler = $used_devs{$device};
733
734                                         if ($HANDLERS{$_HANDLER_MAP_{$handler}}) {
735                                                 print "\t-> WARNING: Device '$device' changes handler to '$handler'";
736
737                                                 if (!$check) {
738                                                         print ", changing.\n";
739                                                         if ($SCST->assignDeviceToHandler($device,
740                                                                                          $_HANDLER_MAP_{$handler})) {
741                                                                 $errs++;
742                                                         } else {
743                                                                 $changes++;
744                                                         }
745                                                 } else {
746                                                         print ".\n";
747                                                         $changes++;
748                                                 }
749                                         }
750                                 }
751                         }
752                 }
753         }
754
755         print "Applying configuration additions..\n" if (!$check);
756         print "\n";
757
758         readWorkingConfig() if ($force);
759
760         foreach my $_handler (sort keys %{$$config{'HANDLER'}}) {
761                 if (!$HANDLERS{$_HANDLER_MAP_{$_handler}}) {
762                         print "\t-> WARNING: Handler '$_handler' does not exist.\n";
763                         $errs += 1;
764                         next;
765                 }
766
767                 foreach my $device (@{$$config{'HANDLER'}->{$_handler}->{'DEVICE'}}) {
768                         my($vname, $path, $options, $blocksize) = split(/\,/, $device);
769                         $path = cleanupString($path);
770                         $options =~ s/\s+//g;
771
772                         if (defined($$DEVICES{$vname}) && ($_HANDLER_MAP_{$_handler} == $$DEVICES{$vname})) {
773                                 next;
774                         } elsif (defined($$DEVICES{$vname}) && ($_HANDLER_MAP_{$_handler} != $$DEVICES{$vname})) {
775                                 if ($HANDLERS{$_HANDLER_MAP_{$_handler}}) {
776                                         print "\t-> WARNING: Device '$vname' changes handler from '".
777                                           $_REVERSE_MAP_{$$DEVICES{$vname}}."' to '$_handler'.\n".
778                                           "\t   Use -ForceConfig to change device handler.\n" if (!$force && !$check);
779                                 }
780                                 next;
781                         }
782
783                         if ($check) {
784                                 print "\t-> New device '$_handler:$vname' at path '$path', options '$options', ".
785                                   "blocksize $blocksize.\n";
786                                 $$DEVICES{$vname} = $_HANDLER_MAP_{$_handler};
787                                 $changes++;
788                         } else {
789                                 if (addDevice($_handler, $vname, $path, $options, $blocksize)) {
790                                         $errs++;
791                                 } else {
792                                         $changes++;
793                                 }
794                         }
795                 }
796         }
797
798         # Create new groups and add users..
799         foreach my $group (keys %used_users) {
800                 if (!defined($USERS{$group})) {
801                         if ($check) {
802                                 print "\t-> New group definition '$group.'\n";
803                                 $GROUPS{$group}++;
804                                 $changes++;
805                         } else {
806                                 if (addGroup($group)) {
807                                         $errs++;
808                                 } else {
809                                         $changes++;
810                                 }
811                         }
812                 }
813
814                 foreach my $user (keys %{$used_users{$group}}) {
815                         if (!defined($USERS{$group}->{$user})) {
816                                 if ($check) {
817                                         print "\t-> New user definition '$user' for group '$group'.\n";
818                                         $USERS{$group}->{$user}++;
819                                         $changes++;
820                                 } else {
821                                         if (addUser($group, $user)) {
822                                                 $errs++;
823                                         } else {
824                                                 $changes++;
825                                         }
826                                 }
827                         }
828                 }
829         }
830
831         # Assign new devices to groups..
832         foreach my $group (keys %used_assignments) {
833                 if (!defined($GROUPS{$group})) {
834                         # Looks like we're lacking a group. We'll create an empty one
835
836                         print "\t-> WARNING: Auto-creating an empty group '$group' since none was configured.\n";
837
838                         if (addGroup($group)) {
839                                 $errs++;
840                         } else {
841                                 $changes++;
842                         }
843                 }
844
845                 if (!defined($GROUPS{$group})) {
846                         print "\t-> WARNING: Unable to assign to non-existant group '$group'.\n";
847                         $errs += 1;
848                         next;
849                 }
850
851                 foreach my $vname (keys %{$used_assignments{$group}}) {
852                         my $lun = $used_assignments{$group}->{$vname};
853                         my $_assignments = $ASSIGNMENTS{$group};
854
855                         if (defined($$_assignments{$vname}) && ($$_assignments{$vname} == $lun)) {
856                                 next;
857                         } elsif (defined($$_assignments{$vname}) && ($$_assignments{$vname} != $lun)) {
858                                 print "\t-> Device '$vname' assigned to group '$group' is at LUN ".$$_assignments{$vname}.
859                                   ", whereas the working configuration reflects LUN $lun.\n".
860                                   "\t   Use -ForceConfig to force this LUN change.\n" if (!$force && !$check);
861                         } else {
862                                 if ($check) {
863                                         $lun = 'auto' if (!defined($lun));
864                                         print "\t-> New device assignment for '$vname' to group '$group' at LUN $lun.\n";
865                                         $changes++;
866                                 } else {
867                                         if (assignDevice($group, $vname, $lun)) {
868                                                 $errs++;
869                                         } else {
870                                                 $changes++;
871                                         }
872                                 }
873                         }
874                 }
875         }
876
877         # Enable/Disable configured targets
878         foreach my $type (keys %{$$config{'TARGETS'}}) {
879                 my $enable;
880
881                 if ($type eq 'enable') {
882                         $enable = $TRUE;
883                 } elsif ($type eq 'disable') {
884                         $enable = $FALSE;
885                 } else {
886                         print "\t-> WARNING: Ignoring invalid TARGETS specifier '$type'. ".
887                           "Should be one of enable,disable.\n";
888                         next;
889                 }
890
891                 foreach my $target (@{$$config{'TARGETS'}->{$type}->{'HOST'}}) {
892                         my $i_target = unformatTarget($target);
893
894                         if (!defined($$TARGETS{$i_target})) {
895                                 print "\t-> WARNING: Target '$target' not found on system.\n";
896                                 $errs += 1;
897                                 next;
898                         }
899
900                         next if ($enable == targetEnabled($i_target));
901
902                         if (!$enable && targetEnabled($target)) {
903                                 if ($force || $check) {
904                                         print "\t-> WARNING: Target mode for '$target' is currently enabled, ".
905                                           "however configuration file wants it disabled";
906
907                                         if (!$check) {
908                                                 print ", disabling.\n";
909                                                 if (enableTarget($target, $enable)) {
910                                                         $errs++;
911                                                 } else {
912                                                         $changes++;
913                                                 }
914                                         } else {
915                                                 print ".\n";
916                                                 $changes++;
917                                         }
918                                 }
919                         } else {
920                                 print "\t-> Target '$target' is enabled in configuration file, ".
921                                   "however is currently disabled";
922
923                                 if (!$check) {
924                                         print ", enabling.\n";
925                                         if (enableTarget($target, $enable)) {
926                                                 $errs++;
927                                         } else {
928                                                 $changes++;
929                                         }
930                                 } else {
931                                         print ".\n";
932                                         $changes++;
933                                 }
934                         }
935                 }
936         }
937
938         print "\nEncountered $errs error(s) while processing.\n" if ($errs);
939
940         if ($check) {
941                 print "Configuration checked, $changes difference(s) found with working configuration.\n";
942         } else {
943                 $changes = 0 if ($_DEBUG_);
944                 print "Configuration applied, $changes changes made.\n";
945         }
946
947         return $TRUE if ($errs);
948         return $FALSE;
949 }
950
951 sub clearConfiguration {
952         my $errs;
953
954         print "WARNING: This removes ALL applied SCST configuration and may result in data loss!\n";
955         print "If this is not what you intend, press ctrl-c now. Waiting 10 seconds.\n\n";
956         sleep 10;
957
958         print "\nRemoving all user and groups:\n\n";
959         foreach my $group (keys %GROUPS) {
960                 $errs += removeGroup($group) if ($group ne $_DEFAULT_GROUP_);
961         }
962
963         print "\nRemoving all handler devices:\n\n";
964         foreach my $device (keys %{$DEVICES}) {
965                 next if (!$$DEVICES{$device});
966                 next if ($SCST->handlerType($$DEVICES{$device}) != $SCST::SCST::IOTYPE_VIRTUAL);
967                 $errs += removeDevice($_REVERSE_MAP_{$$DEVICES{$device}}, $device);
968         }
969
970         print "\nEncountered $errs error(s) while processing.\n" if ($errs);
971         print "\nConfiguration cleared.\n";
972
973         return $TRUE if ($errs);
974         return $FALSE;
975 }
976
977 sub showSessions {
978         my $sessions = $SCST->sessions();
979         immediateExit($SCST->errorString());
980
981         print "\n\tTarget Name\tInitiator Name\t\t\tGroup Name\t\tCommand Count\n";
982
983         foreach my $target (keys %{$sessions}) {
984                 foreach my $group (keys %{$$sessions{$target}}) {
985                         foreach my $user (keys %{$$sessions{$target}->{$group}}) {
986                                 my $commands = $$sessions{$target}->{$group}->{$user};
987
988                                 print "\t$target\t$user\t\t$group\t\t$commands\n";
989                         }
990                 }
991         }
992
993         print "\n";
994
995         return $FALSE;
996 }
997
998 sub addDevice {
999         my $handler = shift;
1000         my $device = shift;
1001         my $path = shift;
1002         my $options = shift;
1003         my $blocksize = shift;
1004
1005         my $_handler = $_HANDLER_MAP_{$handler};
1006         my $htype = $SCST->handlerType($_handler);
1007
1008         if (!$htype) {
1009                 print "WARNING: Internal error occured: ".$SCST->errorString()."\n";
1010                 return $TRUE;
1011         }
1012
1013         if ($htype != $SCST::SCST::IOTYPE_VIRTUAL) {
1014                 my $typeString = $_HANDLER_TYPE_MAP_{$htype};
1015                 my $validType = $_HANDLER_TYPE_MAP_{$SCST::SCST::IOTYPE_VIRTUAL};
1016                 print "WARNING: Handler $handler of type $typeString is incapable of ".
1017                   "opening/closing devices. Valid handlers are:\n".
1018                   validHandlerTypes($SCST::SCST::IOTYPE_VIRTUAL)."\n";
1019                 return $TRUE;
1020         }
1021
1022         if (defined($$DEVICES{$device})) {
1023                 print "WARNING: Device '$device' already defined.\n";
1024                 return $TRUE;
1025         }
1026
1027         print "\t-> Opening virtual device '$device' at path '$path' using handler '$handler'..\n";
1028
1029         if ($SCST->openDevice($_handler, $device, $path, $options, $blocksize)) {
1030                 print "WARNING: Failed to open virtual device '$device' at path '$path': ".
1031                   $SCST->errorString()."\n";
1032                 return $TRUE;
1033         }
1034
1035         $$DEVICES{$device} = $_handler;
1036
1037         return $FALSE;
1038 }
1039
1040 sub removeDevice {
1041         my $handler = shift;
1042         my $device = shift;
1043
1044         my $_handler = $_HANDLER_MAP_{$handler};
1045         my $htype = $SCST->handlerType($_handler);
1046
1047         if (!$htype) {
1048                 print "WARNING: Internal error occured: ".$SCST->errorString()."\n";
1049                 return $TRUE;
1050         }
1051
1052         if ($htype != $SCST::SCST::IOTYPE_VIRTUAL) {
1053                 my $typeString = $_HANDLER_TYPE_MAP_{$htype};
1054                 my $validType = $_HANDLER_TYPE_MAP_{$SCST::SCST::IOTYPE_VIRTUAL};
1055                 print "WARNING: Handler $handler of type $typeString is incapable of ".
1056                   "opening/closing devices. Valid handlers are:\n".
1057                   validHandlerTypes($SCST::SCST::IOTYPE_VIRTUAL)."\n";
1058                 return $TRUE;
1059         }
1060
1061         if (!defined($$DEVICES{$device})) {
1062                 print "WARNING: Device '$device' not defined.\n";
1063                 return $TRUE;
1064         }
1065
1066         print "\t-> Closing virtual device '$device'..\n";
1067
1068         if ($SCST->closeDevice($_handler, $device)) {
1069                 print "WARNING: Failed to close virtual device '$device': ".
1070                   $SCST->errorString()."\n";
1071                 return $TRUE;
1072         }
1073
1074         undef $$DEVICES{$device};
1075
1076         return $FALSE;
1077 }
1078
1079 sub addGroup {
1080         my $group = shift;
1081
1082         if (defined($GROUPS{$group})) {
1083                 print "WARNING: Group '$group' already exists.\n";
1084                 return $TRUE;
1085         }
1086
1087         print "\t-> Creating security group '$group'..\n";
1088
1089         if ($SCST->addGroup($group)) {
1090                 print "WARNING: Failed to create security group '$group': ".
1091                   $SCST->errorString()."\n";
1092                 return $TRUE;
1093         }
1094
1095         $GROUPS{$group}++;
1096
1097         return $FALSE;
1098 }
1099
1100 sub removeGroup {
1101         my $group = shift;
1102
1103         if (!defined($GROUPS{$group})) {
1104                 print "WARNING: Group '$group' does not exist.\n";
1105                 return $TRUE;
1106         }
1107
1108         print "\t-> Removing security group '$group'..\n";
1109
1110         if ($SCST->removeGroup($group)) {
1111                 print "WARNING: Failed to remove security group '$group': ".
1112                   $SCST->errorString()."\n";
1113                 return $TRUE;
1114         }
1115
1116         undef $GROUPS{$group};
1117
1118         return $FALSE;
1119 }
1120
1121 sub addUser {
1122         my $group = shift;
1123         my $user = shift;
1124
1125         if (!defined($GROUPS{$group})) {
1126                 print "WARNING: Failed to add user '$user' to group '$group', group does not exist.\n";
1127                 return $TRUE;
1128         }
1129
1130         if (defined($USERS{$group}->{$user})) {
1131                 print "WARNING: User '$user' already exists in security group '$group'.\n";
1132                 return $TRUE;
1133         }
1134
1135         print "\t-> Adding user '$user' to security group '$group'..\n";
1136
1137         if ($SCST->addUser($user, $group)) {
1138                 print "WARNING: Failed to add user '$user' to security group '$group': ".
1139                   $SCST->errorString()."\n";
1140                 return $TRUE;
1141         }
1142
1143         $USERS{$group}->{$user}++;
1144
1145         return $FALSE;
1146 }
1147
1148 sub removeUser {
1149         my $group = shift;
1150         my $user = shift;
1151
1152         if (!defined($GROUPS{$group})) {
1153                 print "WARNING: Failed to remove user '$user' from group '$group', group does not exist.\n";
1154                 return $TRUE;
1155         }
1156
1157         if (!defined($USERS{$group}->{$user})) {
1158                 print "WARNING: User '$user' doesn\'t exist in security group '$group'.\n";
1159                 return $TRUE;
1160         }
1161
1162         print "\t-> Removing user '$user' from security group '$group'..\n";
1163
1164         if ($SCST->removeUser($user, $group)) {
1165                 print "WARNING: Failed to add user '$user' to security group '$group': ".
1166                   $SCST->errorString()."\n";
1167                 return $TRUE;
1168         }
1169
1170         undef $USERS{$group}->{$user};
1171
1172         return $FALSE;
1173 }
1174
1175 sub clearUsers {
1176         my $group = shift;
1177
1178         if (!defined($GROUPS{$group})) {
1179                 print "WARNING: Failed to clear users from group '$group', group does not exist.\n";
1180                 return $TRUE;
1181         }
1182
1183         print "\t-> Clearing users from security group '$group'..\n";
1184
1185         if ($SCST->clearUsers($group)) {
1186                 print "WARNING: Failed to clear users from security group '$group': ".
1187                   $SCST->errorString()."\n";
1188                 return $TRUE;
1189         }
1190
1191         undef $USERS{$group};
1192
1193         return $FALSE;
1194 }
1195
1196 sub assignDevice {
1197         my $group = shift;
1198         my $device = shift;
1199         my $lun = shift;
1200         my %allLuns;
1201
1202         # Put luns into something easier to parse..
1203         foreach my $_group (keys %ASSIGNMENTS) {
1204                 my $_gAssigns = $ASSIGNMENTS{$_group};
1205
1206                 foreach my $_device (keys %{$_gAssigns}) {
1207                         @{$allLuns{$_group}}[$$_gAssigns{$_device}] = $_device;
1208                 }
1209         }
1210
1211         # Use the next available LUN if none specified
1212         if ($lun !~ /\d+/) {
1213                 $lun = ($#{$allLuns{$group}} + 1);
1214                 if ($lun > $_MAX_LUNS_) {
1215                         print "ERROR: Unable to use next available LUN of $lun, lun out of range.\n";
1216                         return $TRUE;
1217                 }
1218
1219                 print "\t-> Device '$device': Using next available LUN of $lun for group '$group'.\n";
1220         }
1221                         
1222         if (($lun < 0) || ($lun > $_MAX_LUNS_)) {
1223                 print "ERROR: Unable to assign device '$device', lun '$lun' is out of range.\n";
1224                 return $TRUE;
1225         }
1226
1227         if (!defined($$DEVICES{$device})) {
1228                 print "WARNING: Unable to assign non-existant device '$device' to group '$group'.\n";
1229                 return $TRUE;
1230         }
1231
1232         if (@{$allLuns{$group}}[$lun]) {
1233                 print "ERROR: Device '$device': Lun '$lun' is already assigned to device '".@{$allLuns{$group}}[$lun]."'.\n";
1234                 return $TRUE;
1235         }
1236
1237         print "\t-> Assign virtual device '$device' to group '$group' at LUN '$lun'..\n";
1238
1239         if ($SCST->assignDeviceToGroup($device, $group, $lun)) {
1240                 print "WARNING: Failed to assign device '$device' to group '$group': ".
1241                   $SCST->errorString()."\n";
1242                 return $TRUE;
1243         }
1244
1245         if (!defined($ASSIGNMENTS{$group})) {
1246                 my %assignments_t;
1247                 $ASSIGNMENTS{$group} = \%assignments_t;
1248         }
1249
1250         my $_assignments = $ASSIGNMENTS{$group};
1251
1252         $$_assignments{$device} = $lun; 
1253
1254         return $FALSE;
1255 }
1256
1257 sub releaseDevice {
1258         my $group = shift;
1259         my $device = shift;
1260
1261         if (!defined($GROUPS{$group})) {
1262                 print "WARNING: Failed to release device '$device' from group '$group', group does not exist.\n";
1263                 return $TRUE;
1264         }
1265
1266         if (!defined($$DEVICES{$device})) {
1267                 print "WARNING: Failed to release device '$device', device not defined.\n";
1268                 return $TRUE;
1269         }
1270
1271         print "\t-> Release virtual device '$device' from group '$group'..\n";
1272
1273         if ($SCST->removeDeviceFromGroup($device, $group)) {
1274                 print "WARNING: Failed to release device '$device' from group '$group': ".
1275                   $SCST->errorString()."\n";
1276                 return $TRUE;
1277         }
1278
1279         my $_assignments = $ASSIGNMENTS{$group};
1280
1281         undef $$_assignments{$device};
1282
1283         return $FALSE;
1284 }
1285
1286 sub clearDevices {
1287         my $group = shift;
1288
1289         if (!defined($GROUPS{$group})) {
1290                 print "WARNING: Failed to clear devices from group '$group', group does not exist.\n";
1291                 return $TRUE;
1292         }
1293
1294         print "\t-> Clear virtual devices from group '$group'..\n";
1295
1296         if ($SCST->clearGroupDevices($group)) {
1297                 print "WARNING: Failed to clear devices from group '$group': ".
1298                   $SCST->errorString()."\n";
1299                 return $TRUE;
1300         }
1301
1302         undef $ASSIGNMENTS{$group};
1303
1304         return $FALSE;
1305 }
1306
1307 sub targets {
1308         my %targets;
1309         my %fcards;
1310         my $root = new IO::Dir $_FC_CLASS_;
1311
1312         if ($root) {
1313                 while (my $entry = $root->read()) {
1314                         next if (($entry eq '.') || ($entry eq '..'));
1315
1316                         my $io = new IO::File "$_FC_CLASS_/$entry/port_name", O_RDONLY;
1317
1318                         if ($io) {
1319                                 my $wwn = <$io>;
1320                                 chomp $wwn;
1321                                 close $io;
1322
1323                                 $fcards{$entry} = $wwn;
1324                         }
1325                 }
1326         }
1327
1328         $root = new IO::Dir $_SCSI_CLASS_;
1329
1330         if ($root) {
1331                 while (my $entry = $root->read()) {
1332                         next if (($entry eq '.') || ($entry eq '..'));
1333
1334                         my $io = new IO::File "$_SCSI_CLASS_/$entry/target_mode_enabled", O_RDONLY;
1335
1336                         if ($io) {
1337                                 my $enabled = <$io>;
1338                                 chomp $enabled;
1339                                 close $io;
1340
1341                                 $targets{$entry}->{'path'} = "$_SCSI_CLASS_/$entry/target_mode_enabled";
1342                                 $targets{$entry}->{'enabled'} = $enabled;
1343
1344                                 if ($fcards{$entry}) {
1345                                         $targets{$fcards{$entry}}->{'enabled'} = $enabled;
1346                                         $targets{$fcards{$entry}}->{'path'} =
1347                                           "$_SCSI_CLASS_/$entry/target_mode_enabled";
1348                                         $targets{$entry}->{'duplicate'} = $TRUE;
1349                                 } else {
1350                                         $targets{$entry}->{'duplicate'} = $FALSE;
1351                                 }
1352                         }
1353                 }
1354         }
1355
1356         return \%targets;
1357 }
1358
1359 sub targetEnabled {
1360         my $target = shift;
1361
1362         return undef if (!defined($$TARGETS{$target}));
1363         return $$TARGETS{$target}->{'enabled'};
1364 }
1365
1366 sub enableTarget {
1367         my $target = shift;
1368         my $enable = shift;
1369
1370         return undef if (!defined($$TARGETS{$target}));
1371
1372         my $io = new IO::File $$TARGETS{$target}->{'path'}, O_WRONLY;
1373         return $TRUE if (!$io);
1374
1375         print $enable ? "\t-> Enabling" : "\t-> Disabling";
1376         print " target mode for SCST host '$target'.\n";
1377
1378         if ($_DEBUG_) {
1379                 print "DBG($$): ".$$TARGETS{$target}->{'path'}." -> $enable\n\n";
1380         } else {
1381                 print $io $enable;
1382         }
1383
1384         close $io;
1385
1386         $$TARGETS{unformatTarget($target)}->{'enabled'} = $enable;
1387
1388         return $FALSE;
1389 }
1390
1391 sub readConfig {
1392         my $confile = shift;
1393         my %config;
1394         my $section;
1395         my $last_section;
1396         my $arg;
1397         my $last_arg;
1398         my %empty;
1399
1400         my $io = new IO::File $confile, O_RDONLY;
1401
1402         die("FATAL: Unable to open specified configuration file $confile: $!\n") if (!$io);
1403
1404         while (my $line = <$io>) {
1405                 ($line, undef) = split(/\#/, $line, 2);
1406                 $line = cleanupString($line);
1407
1408                 if ($line =~ /^\[(.*)\]$/) {
1409                         ($section, $arg) = split(/\s+/, $1, 2);
1410
1411                         if ($last_arg && ($last_section ne $section) &&
1412                             !defined($config{$last_section}->{$last_arg})) {
1413                                 $config{$last_section}->{$last_arg} = \%empty;
1414                         }   
1415
1416                         $last_arg = $arg;
1417                         $last_section = $section;
1418                 } elsif ($section && $arg && $line) {
1419                         my($parameter, $value) = split(/\s+/, $line, 2);
1420
1421                         push @{$config{$section}->{$arg}->{$parameter}}, $value;
1422                 }
1423         }
1424
1425         close $io;
1426
1427         return \%config;
1428 }
1429
1430 sub cleanupString {
1431         my $string = shift;
1432
1433         $string =~ s/^\s+//;
1434         $string =~ s/\s+$//;
1435
1436         return $string;
1437 }
1438
1439 sub formatTarget {
1440         my $target = shift;
1441
1442         if ($target =~ /^0x/) {
1443                 $target =~ s/^0x//;
1444                 my($o1, $o2, $o3, $o4, $o5, $o6, $o7, $o8) = unpack("A2A2A2A2A2A2A2A2", $target);
1445                 $target = "$o1:$o2:$o3:$o4:$o5:$o6:$o7:$o8";
1446         }
1447
1448         $target =~ tr/A-Z/a-z/;
1449
1450         return $target;
1451 }
1452
1453 sub unformatTarget {
1454         my $target = shift;
1455
1456         if ($target =~ /^.{2}\:.{2}\:.{2}\:.{2}\:.{2}\:.{2}\:.{2}\:.{2}/) {
1457                 $target =~ s/\://g;
1458                 $target = "0x$target";
1459         }
1460
1461         $target =~ tr/A-Z/a-z/;
1462
1463         return $target;
1464 }
1465
1466 sub validHandlerTypes {
1467         my $type = shift;
1468         my $buffer = "\n";
1469
1470         foreach my $handler (keys %_REVERSE_MAP_) {
1471                 $buffer .= "\t".$_REVERSE_MAP_{$handler}."\n" if ($SCST->handlerType($handler) == $type);
1472         }
1473
1474         return $buffer;
1475 }
1476
1477 # If we have an unread error from SCST, exit immediately
1478 sub immediateExit {
1479         my $error = shift;
1480
1481         return if (!$error);
1482
1483         print "\n\nFATAL: Received the following error:\n\n\t";
1484         print "$error\n\n";
1485
1486         exit 1;
1487 }
1488
1489 # Hey! Stop that!
1490 sub commitSuicide {
1491         print "\n\nAborting immediately.\n";
1492         exit 1;
1493 }