#!/usr/bin/perl

use strict;
use SNMP_util;
use Net::SMTP;

my $username = $ARGV[0];
my $sponsorname = $ARGV[1];
my $lifetime = $ARGV[2] * 3600 * 100;
my $usernamelength = length($username);
my $userpassword = randomPassword(8);
my @userchar = split("", $username);
my $usernameiso;
my $snmpcommunity = "flw";
my $wlc = "10.140.8.200";
my $snmpversion = "2c";
my $emailending = "geisslermail.de";
my $emailserver = "mscp.agadmin.de";
my $mailfrom = "wireless.andre";

# Note that a email is send to sponsorname@emailending later in this script
# Sponsorname is also used in the comment field of the guest user in the WLC

#OID used
# These are reversed engineered with wireshark.
# Note that each of these OID calls have to be prepended with OID+username_length+username_chars.
# Eg. username ABC will be OID.3.65.66.67
# Make a short break between the OID calls as the WLC seems to need a bit of time to process the user creation

# A undocumented xxx.24, if user exsist it fails, if user is new its okay, TYPE = i, On my WLC this value is 4
my $bsnLocalNetUserUnknownOID = ".1.3.6.1.4.1.14179.2.5.10.1.24";

# TYPE = i, On my WLC this value is 1
my $bsnLocalNetUserWlanId = ".1.3.6.1.4.1.14179.2.5.10.1.2";

# TYPE = s, a description of the user - should be the sponsor's first part of email
my $bsnLocalNetUserDescription = ".1.3.6.1.4.1.14179.2.5.10.1.4";

# TYPE = s, user password, will be generated automatic and send to sponsor's email
my $bsnLocalNetUserPassword = ".1.3.6.1.4.1.14179.2.5.10.1.3";

# TYPE = i, Value on shall be 24h == 8620921
my $bsnLocalNetUserLifetime = ".1.3.6.1.4.1.14179.2.5.10.1.5";

# TYPE = i, Unknown Cisco specific username, On my WLC this value is 1
my $cLWALocalNetUserIsGuest = ".1.3.6.1.4.1.9.9.515.2.3.1.1.2";

# TYPE = s, Cisco specific undocumented OID, Value on CPHGWC01 = default
my $cLWALocalNetUserIsUnknown = ".1.3.6.1.4.1.9.9.515.2.3.1.1.3";

foreach (@userchar) {
$usernameiso .= ".".ord($_);
}

$bsnLocalNetUserUnknownOID = $bsnLocalNetUserUnknownOID.".".$usernamelength.$usernameiso;
$bsnLocalNetUserWlanId = $bsnLocalNetUserWlanId.".".$usernamelength.$usernameiso;
$bsnLocalNetUserDescription = $bsnLocalNetUserDescription.".".$usernamelength.$usernameiso;
$bsnLocalNetUserPassword = $bsnLocalNetUserPassword.".".$usernamelength.$usernameiso;
$bsnLocalNetUserLifetime = $bsnLocalNetUserLifetime.".".$usernamelength.$usernameiso;
$cLWALocalNetUserIsGuest = $cLWALocalNetUserIsGuest.".".$usernamelength.$usernameiso;
$cLWALocalNetUserIsUnknown = $cLWALocalNetUserIsUnknown.".".$usernamelength.$usernameiso;

my ($response) = &snmpget("$snmpcommunity\@$wlc:161:5:1:1:$snmpversion", $bsnLocalNetUserUnknownOID);
if ($response) {
        my ($extention) = &snmpset("$snmpcommunity\@$wlc:161:5:1:1:$snmpversion", $bsnLocalNetUserLifetime, "int", "$lifetime",
               $bsnLocalNetUserPassword, "string", $userpassword
               );
} else {
        my (@response) = &snmpset("$snmpcommunity\@$wlc:161:5:1:1:$snmpversion",
               $bsnLocalNetUserUnknownOID, "int", "4",
               $bsnLocalNetUserWlanId, "int", "1",
               $bsnLocalNetUserDescription, "string", $sponsorname,
               $bsnLocalNetUserPassword, "string", $userpassword
        );

        if (@response) {
               print "SET part1: OK\n";
               } else {
               print "$wlc did not respond to SNMP set\n";
        }

        my (@response) = &snmpset("$snmpcommunity\@$wlc:161:5:1:1:$snmpversion",
                $cLWALocalNetUserIsGuest, "int", "1",
               $cLWALocalNetUserIsUnknown, "string", "default",
               $bsnLocalNetUserLifetime, "int", "$lifetime"
        );

        if (@response) {
               print "SET part2: OK\n";
               } else {
               print "$wlc did not respond to SNMP set\n";
        }
}

#Create a new object with 'new'.
my $smtp = Net::SMTP->new($emailserver);

#Send the MAIL command to the server.
#$smtp->mail("$mailfrom\@$emailending");
$smtp->mail("andre\@geisslermail.de");

#Send the server the 'Mail To' address.
#$smtp->to("$sponsorname\@$emailending");
$smtp->to("andre\@geisslermail.de");

#Start the message.
$smtp->data();
#Send the subject
$smtp->datasend("Subject: regard the wireless user $username\n");
#Send the message.
$smtp->datasend("Hello!\n\nThe user: $username with password: $userpassword\nhave a $ARGV[2]h access to wireless\n\nEnjoy\n\nNetwork Department");
#End the message.
$smtp->dataend();
#Close the connection to your server.
$smtp->quit();

print "The user: $username with password: $userpassword\nhave a $ARGV[2]h access to wireless";

#password generator from the internet
sub randomPassword {
	my $password;
	my $_rand;
	my $password_length = $_[0];
        if (!$password_length) {
		$password_length = 10;
	}
	my @chars = split(" ", "a b c d e f g h i j k l m n o p q r s t u v w x y z - _ % # | 0 1 2 3 4 5 6 7 8 9");
	srand;
	for (my $i=0; $i <= $password_length ;$i++) {
		$_rand = int(rand 41);
		$password .= $chars[$_rand];
	}
	return $password;
}