Initial commit to Gerrit
[profile/ivi/quota.git] / ldap-scripts / applySystemQuotas.pl
1 #!/usr/bin/perl -w
2
3 # $0 -b "ou=People,dc=borgia,dc=com" -F '(attr=value)'
4
5 # Synopsis
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. 
10
11 # Examples:
12 # Apply the quotas using the linuxquota tool edquota for user stefan
13 # ./applySystemQuotas.pl -b "uid=stefan,ou=People,dc=borgia,dc=com"
14 #
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)"
17
18 use strict;
19 use Net::LDAP;
20 use Getopt::Long;
21
22 chomp(my $Password = `cat /etc/ldap.secret`);
23 my $Host = 'localhost';
24 my $Port = '389';
25 my $BindDN = 'cn=Manager,dc=borgia,dc=com';
26 my $SSL = 0;
27 my $edquota_editor = '/usr/sbin/edquota_editor';
28 my $edquota = '/usr/sbin/edquota';
29
30 my $b = '';
31 my $F = '';
32 GetOptions(
33         'b=s' => \$b,
34         'F=s' => \$F,
35 );
36
37 die "Usage: $0 -b basedn [-F '(extrafilter)']\n" unless $b;
38
39 my $ldap = connectLDAP();
40
41 my $search;
42 $search = $ldap->search(
43         base => $b,
44         filter => "(&(objectClass=systemQuotas)$F)",
45         attrs => ['uid', 'quota'],
46 );
47 $search->code && die $search->error;
48 my $i = 0;
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 ) {
65                 foreach ( @quotas ) {
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'});
74                 }
75         }
76         if ($editor) {
77                 $ENV{'VISUAL'} = $editor;
78         }
79         else {
80                 delete $ENV{'VISUAL'};
81         }
82 }
83 $search = $ldap->unbind;
84
85 sub connectLDAP {
86         # bind to a directory with dn and password
87         my $ldap = Net::LDAP->new(
88                 $Host,
89                 port => $Port,
90                 version => 3,
91 #                debug => 0xffff,
92         ) or die "Can't contact LDAP server ($@)\n";
93         if ( $SSL ) {
94                 $ldap->start_tls(
95                         # verify => 'require',
96                         # clientcert => 'mycert.pem',
97                         # clientkey => 'mykey.pem',
98                         # decryptkey => sub { 'secret'; },
99                         # capath => '/usr/local/cacerts/'
100                 ); 
101         }
102         $ldap->bind($BindDN, password=>$Password);
103         return $ldap;
104 }