2
0

randomize-passwords.pl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/perl
  2. #
  3. # randomize-passwords.pl
  4. #
  5. # Randomizes the auth passwords for any file in the file spec given by the user
  6. # Randomizes the vm passwords for the same files
  7. # Creates a backup copy of each file altered; optionally will remove backups
  8. #
  9. # This program uses only pure Perl modules so it should be portable.
  10. #
  11. # Michael S. Collins
  12. # 2009-11-11
  13. #
  14. # Freely contributed to the FreeSWITCH project for use as the developers and community see fit
  15. use strict;
  16. use warnings;
  17. use Getopt::Long;
  18. use File::Basename;
  19. use File::Copy;
  20. $|++;
  21. ## 'CHARACTERS' contains punctuation marks
  22. use constant CHARACTERS => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=+?></.,!@#$%^&*();:';
  23. my $numchars = length(CHARACTERS);
  24. ## 'ALPHACHARS' contains upper and lower case letters and digits but no punctuation
  25. use constant ALPHACHARS => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  26. my $numalphas = length(ALPHACHARS);
  27. my $vmlen = 4; # Length of VM password
  28. my $authlen = 10; # Length of auth password
  29. my $filespec; # File specification
  30. my $delbak; # Flag - delete backups (default = keep backups)
  31. my $nopunct; # Flag - set to true to disable punction marks (i.e. alphanumerics only) in auth passwords
  32. my $opts_ok = GetOptions ("h" => \&usage,
  33. "help" => \&usage,
  34. "vmlen=i" => \$vmlen,
  35. "authlen=i" => \$authlen,
  36. "files=s" => \$filespec,
  37. "D" => \$delbak,
  38. "nopunct" => \$nopunct,
  39. );
  40. ## Confirm that a file spec was provided
  41. if ( ! $filespec ) {
  42. warn "\nPlease provide a file specification.\n";
  43. die "Example: --files=/usr/local/freeswitch/conf/directory/default/1*.xml\n\n";
  44. }
  45. ## Collect the files
  46. my @FILELIST = glob($filespec);
  47. if ( ! @FILELIST ) {
  48. print "\nNo files found matching this spec:\n$filespec\n";
  49. exit(0);
  50. } else {
  51. print "\nFound " . @FILELIST . " file(s).\n\n";
  52. }
  53. ## Iterate through the list, process each file
  54. foreach my $file ( @FILELIST ) {
  55. print "Processing file: $file\n";
  56. my $bakfile = $file . '.bak';
  57. if ( move($file,$bakfile) ) {
  58. print " $file ===> $bakfile\n";
  59. } else {
  60. print " Unable to backup $file to $bakfile. Skipping...\n";
  61. next;
  62. }
  63. ## FILEIN is the backup file, FILEOUT is the updated file
  64. open(FILEIN ,'<',$bakfile) or die "Could not open $bakfile - aborting operation.\n";
  65. open(FILEOUT,'>',$file ) or die "Could not open $file - aborting operation.\n";
  66. ## Retrieve new passwords from random generators
  67. my $newauth = &get_random_chars($authlen);
  68. my $newvm = &get_random_digits($vmlen);
  69. ## Loop through "bak" file, replace passwords, write out to original file
  70. while(<FILEIN>) {
  71. ## Check for passwords; if found swap
  72. if ( m/param name="password"/ ) {
  73. # Found auth password, swap it
  74. s/value="(.*?)"/value="$newauth"/;
  75. print " Old/new auth pass: $1 ==> $newauth\n";
  76. }
  77. if ( m/param name="vm-password"/ ) {
  78. # Found vm password, swap it
  79. s/value="(.*?)"/value="$newvm"/;
  80. print " Old/new vm pass: $1 ==> $newvm\n";
  81. }
  82. print FILEOUT $_;
  83. } ## while(<FILEIN>)
  84. close(FILEIN);
  85. close(FILEOUT);
  86. ## Clear out the backup file if user asked for it
  87. if ( $delbak ) {
  88. print " Removing $bakfile...\n";
  89. unlink $bakfile;
  90. }
  91. print " Finished with $file.\n\n";
  92. } ## foreach my $file ( @FILELIST )
  93. exit(0);
  94. ## Return random chars for auth password
  95. sub get_random_chars () {
  96. my $length = shift;
  97. if ( ! $length ) { $length = $authlen; }
  98. my $chars;
  99. if ( $nopunct ) {
  100. foreach my $i (1 .. $length) {
  101. my $nextchar = substr( ALPHACHARS,int(rand $numalphas),1 );
  102. $chars .= $nextchar;
  103. }
  104. } else {
  105. foreach my $i (1 .. $length) {
  106. my $nextchar = substr( CHARACTERS,int(rand $numchars),1 );
  107. $chars .= $nextchar;
  108. }
  109. }
  110. return $chars;
  111. }
  112. ## Return only digits for vm password
  113. sub get_random_digits () {
  114. my $length = shift;
  115. if ( ! $length ) { $length = $vmlen; }
  116. my $digits;
  117. foreach my $i (1 .. $length) {
  118. my $nextdigit = int(rand 10);
  119. $digits .= $nextdigit;
  120. }
  121. return $digits;
  122. }
  123. sub usage () {
  124. print <<END_USAGE
  125. Randomize passwords for FreeSWITCH directory entries.
  126. Usage: ./randomize-passwords.pl --files=<file spec> [-D] [--vmlen=<vm pass length>] [--authlen=<auth pass length>]
  127. Options:
  128. -h, --help Display this help page
  129. -D Delete backups (default is to save backups)
  130. --files Specify files to process. Use typical file globs. On a standard Linux install it would look like:
  131. --files=/usr/local/freeswitch/conf/directory/default/1*.xml
  132. --vmlen Set length of voice mail password. (Default is 4 digits)
  133. --authlen Set length of auth password. (Default is 10 characters)
  134. --nopunct Disable punction marks in auth passwords, i.e. alphanumerics only
  135. Example:
  136. To randomize all the passwords for a default Linux install, with 6 digit VM passwords, use this command:
  137. ./randomize-passwords.pl --files=/usr/local/freeswitch/conf/directory/default/1*.xml -D --vmlen=6
  138. END_USAGE
  139. ;
  140. exit(0);
  141. }