changeset 0:b20384a94d7d

initial import
author Eris Caffee <discordia@eldalin.com>
date Sat, 16 Jul 2011 13:13:18 -0500
parents
children 706c20861682
files make-username.pl
diffstat 1 files changed, 112 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/make-username.pl	Sat Jul 16 13:13:18 2011 -0500
     1.3 @@ -0,0 +1,112 @@
     1.4 +#!/usr/bin/env perl
     1.5 +
     1.6 +use warnings;
     1.7 +use strict;
     1.8 +
     1.9 +my $base;
    1.10 +my $user ;
    1.11 +
    1.12 +$base = "";
    1.13 +$user = make_username($base);
    1.14 +printf("%30s %s\n", $base, $user);
    1.15 +
    1.16 +$base = "eightchr";
    1.17 +$user = make_username($base);
    1.18 +printf("%30s %s\n", $base, $user);
    1.19 +
    1.20 +$base = "short";
    1.21 +$user = make_username($base);
    1.22 +printf("%30s %s\n", $base, $user);
    1.23 +
    1.24 +$base = "this *&% is not allowed &^$%&^m";
    1.25 +$user = make_username($base);
    1.26 +printf("%30s %s\n", $base, $user);
    1.27 +
    1.28 +$base = "123digits";
    1.29 +$user = make_username($base);
    1.30 +printf("%30s %s\n", $base, $user);
    1.31 +
    1.32 +$base = "this-is-a-domain.com";
    1.33 +$user = make_username($base);
    1.34 +printf("%30s %s\n", $base, $user);
    1.35 +
    1.36 +$base = '&$%#&$*&$%*&';
    1.37 +$user = make_username($base);
    1.38 +printf("%30s %s\n", $base, $user);
    1.39 +
    1.40 +# For the next test, run this first in the shell as root (not on a production system, obviously!):
    1.41 +#
    1.42 +# [ ! -d /var/cpanel/users ] && mkdir -p /var/cpanel/users
    1.43 +# touch /var/cpanel/users/testqwer
    1.44 +# i=1 ; while [ $i -lt 10 ] ; do touch /var/cpanel/users/testabc${i} ; let i=$i+1 ; done
    1.45 +# i=1 ; while [ $i -lt 100 ] ; do touch /var/cpanel/users/testzx${i} ; let i=$i+1 ; done
    1.46 +# i=1 ; while [ $i -lt 100 ] ; do touch /var/cpanel/users/test${i} ; let i=$i+1 ; done
    1.47 +# i=1 ; while [ $i -lt 10346 ] ; do touch /var/cpanel/users/t${i} ; let i=$i+1 ; done
    1.48 +
    1.49 +$base = "testqwer";
    1.50 +$user = make_username($base);
    1.51 +printf("%30s %s\n", $base, $user);
    1.52 +
    1.53 +$base = "testabc";
    1.54 +$user = make_username($base);
    1.55 +printf("%30s %s\n", $base, $user);
    1.56 +
    1.57 +$base = "testzx";
    1.58 +$user = make_username($base);
    1.59 +printf("%30s %s\n", $base, $user);
    1.60 +
    1.61 +$base = "test";
    1.62 +$user = make_username($base);
    1.63 +printf("%30s %s\n", $base, $user);
    1.64 +
    1.65 +$base = "t";
    1.66 +$user = make_username($base);
    1.67 +printf("%30s %s\n", $base, $user);
    1.68 +
    1.69 +
    1.70 +sub make_username {
    1.71 +    # Arguments:
    1.72 +    # The string to use as the basis of the new username.
    1.73 +    # Can be any string (such as an existing username or a domain name)
    1.74 +    # But at most 8 characters will be used.  Invalid characters will
    1.75 +    # be removed.
    1.76 +    # If the first character would be a number, an 'a' is prepended to the name.
    1.77 +    # If blank, the string "user" will be used.
    1.78 +    #
    1.79 +    # Returns:
    1.80 +    # A string containing the new username or undef if no username 
    1.81 +    # could be generated  (unlikely)
    1.82 +
    1.83 +    my ($base) = @_;
    1.84 +
    1.85 +    if ($base eq "") {
    1.86 +	$base = "user";
    1.87 +    }
    1.88 +
    1.89 +    # Remove non-alpha-numeric characters and make sure the first character is a letter.
    1.90 +    $base =~ s/[^A-Za-z0-9]//g;
    1.91 +    if ($base !~ /^[a-z]/ ) { 
    1.92 +        $base = "a".$base;
    1.93 +    }
    1.94 +
    1.95 +    # Use the specified parameter as the base of the new username.
    1.96 +    # Print it into a string of exactly 8 characters.
    1.97 +    # Print the number into the last portion of the new username.
    1.98 +    # Delete spaces (in case the passed username candidate was short).
    1.99 +    # Churn until we find an unused name.
   1.100 +    my $i=1;
   1.101 +    my $numstr = sprintf("%d", $i);
   1.102 +    my $newuser = sprintf("%-8.8s", $base);
   1.103 +    substr($newuser, -length($numstr), length($numstr), $numstr);
   1.104 +    $newuser =~ s/ //g;
   1.105 +    while ( (-e "/var/cpanel/users/$newuser") && ($i < 10000000) ) { 
   1.106 +        $i++;
   1.107 +	$numstr = sprintf("%d", $i);
   1.108 +	$newuser = sprintf("%-8s", $base);
   1.109 +	substr($newuser, -length($numstr), length($numstr), $numstr);
   1.110 +	$newuser =~ s/ //g;
   1.111 +    }
   1.112 +
   1.113 +    $i == 10000000 && return undef;
   1.114 +    return $newuser;
   1.115 +}