applySystemQuotas.pl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/perl -w
  2. # $0 -b "ou=People,dc=borgia,dc=com" -F '(attr=value)'
  3. # Synopsis
  4. # applyQuotas.pl is a script solely for making the quota set within LDAP take
  5. # affect by running the linuxquota tool edquota with the figures set in LDAP.
  6. # This tool is capable of applying standard LDAP filters to the user-supplied
  7. # base DN for applying multiple users' quotas at once.
  8. # Examples:
  9. # Apply the quotas using the linuxquota tool edquota for user stefan
  10. # ./applySystemQuotas.pl -b "uid=stefan,ou=People,dc=borgia,dc=com"
  11. #
  12. # Apply the quotas using the linuxquota tool edquota for all People with description of Student
  13. # ./applySystemQuotas.pl -b "ou=People,dc=borgia,dc=com" -F "(description=Student)"
  14. use strict;
  15. use Net::LDAP;
  16. use Getopt::Long;
  17. chomp(my $Password = `cat /etc/ldap.secret`);
  18. my $Host = 'localhost';
  19. my $Port = '389';
  20. my $BindDN = 'cn=Manager,dc=borgia,dc=com';
  21. my $SSL = 0;
  22. my $edquota_editor = '/usr/sbin/edquota_editor';
  23. my $edquota = '/usr/sbin/edquota';
  24. my $b = '';
  25. my $F = '';
  26. GetOptions(
  27. 'b=s' => \$b,
  28. 'F=s' => \$F,
  29. );
  30. die "Usage: $0 -b basedn [-F '(extrafilter)']\n" unless $b;
  31. my $ldap = connectLDAP();
  32. my $search;
  33. $search = $ldap->search(
  34. base => $b,
  35. filter => "(&(objectClass=systemQuotas)$F)",
  36. attrs => ['uid', 'quota'],
  37. );
  38. $search->code && die $search->error;
  39. my $i = 0;
  40. my $max = $search->count;
  41. for ( $i=0; $i<$max; $i++ ) {
  42. my $entry = $search->entry($i);
  43. my $editor = $ENV{'VISUAL'} if $ENV{'VISUAL'};
  44. $ENV{'VISUAL'} = $edquota_editor;
  45. $ENV{'QUOTA_USER'} = $entry->get_value('uid');
  46. # Delete all existing quotas for QUOTA_USER
  47. $ENV{'QUOTA_FILESYS'} = '*';
  48. $ENV{'QUOTA_SBLOCKS'} = 0;
  49. $ENV{'QUOTA_HBLOCKS'} = 0;
  50. $ENV{'QUOTA_SFILES'} = 0;
  51. $ENV{'QUOTA_HFILES'} = 0;
  52. print "$ENV{'QUOTA_USER'}: $ENV{'QUOTA_FILESYS'}:$ENV{'QUOTA_SBLOCKS'},$ENV{'QUOTA_HBLOCKS'},$ENV{'QUOTA_SFILES'},$ENV{'QUOTA_HFILES'}\n";
  53. qx(/usr/sbin/edquota -u $ENV{'QUOTA_USER'});
  54. my @quotas = $entry->get_value('quota');
  55. if ( $#quotas >= 0 ) {
  56. foreach ( @quotas ) {
  57. my @quota = split /:/;
  58. $ENV{'QUOTA_FILESYS'} = $quota[0];
  59. $ENV{'QUOTA_SBLOCKS'} = $quota[1];
  60. $ENV{'QUOTA_HBLOCKS'} = $quota[2];
  61. $ENV{'QUOTA_SFILES'} = $quota[3];
  62. $ENV{'QUOTA_HFILES'} = $quota[4];
  63. print "$ENV{'QUOTA_USER'}: $ENV{'QUOTA_FILESYS'}:$ENV{'QUOTA_SBLOCKS'},$ENV{'QUOTA_HBLOCKS'},$ENV{'QUOTA_SFILES'},$ENV{'QUOTA_HFILES'}\n";
  64. qx($edquota -u $ENV{'QUOTA_USER'});
  65. }
  66. }
  67. if ($editor) {
  68. $ENV{'VISUAL'} = $editor;
  69. }
  70. else {
  71. delete $ENV{'VISUAL'};
  72. }
  73. }
  74. $search = $ldap->unbind;
  75. sub connectLDAP {
  76. # bind to a directory with dn and password
  77. my $ldap = Net::LDAP->new(
  78. $Host,
  79. port => $Port,
  80. version => 3,
  81. # debug => 0xffff,
  82. ) or die "Can't contact LDAP server ($@)\n";
  83. if ( $SSL ) {
  84. $ldap->start_tls(
  85. # verify => 'require',
  86. # clientcert => 'mycert.pem',
  87. # clientkey => 'mykey.pem',
  88. # decryptkey => sub { 'secret'; },
  89. # capath => '/usr/local/cacerts/'
  90. );
  91. }
  92. $ldap->bind($BindDN, password=>$Password);
  93. return $ldap;
  94. }