3 # $0 -b "ou=People,dc=borgia,dc=com" -F '(attr=value)'
6 # applyQuotas.pl is a script solely for making the quota set within LDAP take
7 # affect by running the linuxquota tool edquota with the figures set in LDAP.
8 # This tool is capable of applying standard LDAP filters to the user-supplied
9 # base DN for applying multiple users' quotas at once.
12 # Apply the quotas using the linuxquota tool edquota for user stefan
13 # ./applySystemQuotas.pl -b "uid=stefan,ou=People,dc=borgia,dc=com"
15 # Apply the quotas using the linuxquota tool edquota for all People with description of Student
16 # ./applySystemQuotas.pl -b "ou=People,dc=borgia,dc=com" -F "(description=Student)"
22 chomp(my $Password = `cat /etc/ldap.secret`);
23 my $Host = 'localhost';
25 my $BindDN = 'cn=Manager,dc=borgia,dc=com';
27 my $edquota_editor = '/usr/sbin/edquota_editor';
28 my $edquota = '/usr/sbin/edquota';
37 die "Usage: $0 -b basedn [-F '(extrafilter)']\n" unless $b;
39 my $ldap = connectLDAP();
42 $search = $ldap->search(
44 filter => "(&(objectClass=systemQuotas)$F)",
45 attrs => ['uid', 'quota'],
47 $search->code && die $search->error;
49 my $max = $search->count;
50 for ( $i=0; $i<$max; $i++ ) {
51 my $entry = $search->entry($i);
52 my $editor = $ENV{'VISUAL'} if $ENV{'VISUAL'};
53 $ENV{'VISUAL'} = $edquota_editor;
54 $ENV{'QUOTA_USER'} = $entry->get_value('uid');
55 # Delete all existing quotas for QUOTA_USER
56 $ENV{'QUOTA_FILESYS'} = '*';
57 $ENV{'QUOTA_SBLOCKS'} = 0;
58 $ENV{'QUOTA_HBLOCKS'} = 0;
59 $ENV{'QUOTA_SFILES'} = 0;
60 $ENV{'QUOTA_HFILES'} = 0;
61 print "$ENV{'QUOTA_USER'}: $ENV{'QUOTA_FILESYS'}:$ENV{'QUOTA_SBLOCKS'},$ENV{'QUOTA_HBLOCKS'},$ENV{'QUOTA_SFILES'},$ENV{'QUOTA_HFILES'}\n";
62 qx(/usr/sbin/edquota -u $ENV{'QUOTA_USER'});
63 my @quotas = $entry->get_value('quota');
64 if ( $#quotas >= 0 ) {
66 my @quota = split /:/;
67 $ENV{'QUOTA_FILESYS'} = $quota[0];
68 $ENV{'QUOTA_SBLOCKS'} = $quota[1];
69 $ENV{'QUOTA_HBLOCKS'} = $quota[2];
70 $ENV{'QUOTA_SFILES'} = $quota[3];
71 $ENV{'QUOTA_HFILES'} = $quota[4];
72 print "$ENV{'QUOTA_USER'}: $ENV{'QUOTA_FILESYS'}:$ENV{'QUOTA_SBLOCKS'},$ENV{'QUOTA_HBLOCKS'},$ENV{'QUOTA_SFILES'},$ENV{'QUOTA_HFILES'}\n";
73 qx($edquota -u $ENV{'QUOTA_USER'});
77 $ENV{'VISUAL'} = $editor;
80 delete $ENV{'VISUAL'};
83 $search = $ldap->unbind;
86 # bind to a directory with dn and password
87 my $ldap = Net::LDAP->new(
92 ) or die "Can't contact LDAP server ($@)\n";
95 # verify => 'require',
96 # clientcert => 'mycert.pem',
97 # clientkey => 'mykey.pem',
98 # decryptkey => sub { 'secret'; },
99 # capath => '/usr/local/cacerts/'
102 $ldap->bind($BindDN, password=>$Password);