/
groups-import.pl
executable file
·85 lines (72 loc) · 1.7 KB
/
groups-import.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/perl
use strict;
use EBox;
use EBox::Samba::Group;
use EBox::Samba::Container;
use File::Slurp;
use Cwd 'abs_path';
use TryCatch;
my $getParms;
my $getPath;
my $readCSV;
my $createLDAPGroups;
my $getLDAPContainer;
my @lines;
EBox::init();
sub createLDAPGroups
{
my(@lines) = @_;
for my $line(@lines) {
my($groupname, $parentDN, $description, $mail, $isSecurityGroup, $isSystemGroup, $gidNumber) = split(';', $line);
try {
EBox::Samba::Group -> create(
name => $groupname,
parent => getLDAPContainer($parentDN),
description => $description,
mail => $mail,
isSecurityGroup => $isSecurityGroup,
isSystemGroup => $isSystemGroup,
gidNumber => $gidNumber
);
print "$groupname OK\n";
} catch ($e){
warn "Caught error: $e";
}
}
}
sub getLDAPContainer
{
my ($parentDN) = @_;
my $container;
try {
$container = EBox::Samba::Container->new( dn => $parentDN );
}
catch ($e) {
print "$e\n";
$container = EBox::Samba::Group->defaultContainer();
print "LDAP Object with DN $parentDN not found, giving default container: " . $container->dn() . "\n";
}
return $container;
}
sub readCSV
{
my($p) = getPath(@_);
my @lines = read_file($p);
createLDAPGroups(@lines);
}
sub getPath
{
my($path) = @_;
$path = abs_path($path);
return $path;
}
sub getParms
{
my(@args) = @_;
if (scalar @args < 1 or scalar @args > 1) {
print "Usage: ./group-importer <source-file> \n";
} else {
readCSV($args[0]);
}
}
getParms(@ARGV);