#!/usr/bin/perl use strict; my $GID = get_startGID(); my $UID = get_startUID(); my $shell = "/bin/bash"; open(FIL,"input.txt"); foreach() { chomp($_); my $username = get_username($_[0]); my $password = get_password(); my $hash = get_hash($password); my $userUID = ++$GID; my $userGID = ++$UID; print "############################################\n"; print "Username: $username \n"; print "password: $password \n"; print "Hash: $hash \n"; print "UID: $userUID \n"; print "GID: $userGID \n"; while(is_taken($username) eq "true") { print "Username taken - Generating new...\n"; $username = get_nextUsername(); print "... New username: $username \n"; } my $homedir = create_directory($username); create_group($username); create_user($username, $hash, $userUID, $userGID, $homedir, $shell); create_html($username); create_profile($username); create_htaccess($username); set_permission($username,$userGID); print "############################################\n"; } ############################## sub get_username() { my @name = split(", ", $_); my $f = substr($name[0], 0, 3); my $l = substr($name[1], 0, 3); my $result = "$f$l"; # For å strippe unordiske tegn; #$result =~ s/ø/oe/; #$result =~ s/æ/ae/g; #$result =~ s/å/aa/g; #$result =~ s//Oe/g; #$result =~ s//Ae/g; #$result =~ s//Aa/g; return $result; } sub get_nextUsername() { my @name = split(", ", $_); my $f = substr($name[0], 0, 2); my $l = substr($name[1], 0, 2); my $r = `cat /dev/urandom|tr -dc "a-zA-Z0-9"|fold -w 2 | head -n 1`; chomp($r); return "$f$l$r"; } sub get_password() { my $password = `cat /dev/urandom|tr -dc "a-zA-Z0-9"|fold -w 6 | head -n 1`; chomp($password); return $password; } sub get_hash() { my $salt = `cat /dev/urandom|tr -dc "a-zA-Z0-9"|fold -w 2 | head -n 1`; my $hash = crypt($_[0], $salt); return $hash; } sub get_startGID() { my $result = `cat /etc/passwd | tail -n 1 | cut -d":" -f 3`; return $result; } sub get_startUID() { my $result = `cat /etc/group | tail -n 1 | cut -d":" -f 3`; return $result; } sub create_directory() { `mkdir home/$_[0]`; print "... Creating home-dir: home/$_[0] \n"; `mkdir home/$_[0]/public_html`; print "... Creating HTML-dir: home/$_[0]/public_html \n"; `mkdir home/$_[0]/public_html/cgi-bin`; print "... Creating CGI-dir: home/$_[0]/public_html/cgi-bin \n"; return "/home/$_[0]"; } sub create_html() { my @input = split(":",$_); my $name = $input[0]; my $subjects = $input[2]; my $address = $input[3]; my $phone = $input[4]; print "... Creating HTML\n"; print "Name: $name \n"; print "Adr: $address \n"; print "Phone: $phone \n"; print "Subjects: $subjects \n"; open(HTML, ">>home/$_[0]/public_html/index.html"); print HTML "\n"; print HTML "Welcome to $name s homepage\n"; print HTML "\n"; print HTML "

Name: $name
\n

"; print HTML "Adr: $address
\n"; print HTML "Phone: $phone
\n"; print HTML "Subjects: $subjects
\n"; print HTML "\n"; print HTML "\n"; close(HTML); } sub is_taken() { my $user = $_[0]; my %names = (); open(PASS,"names.txt"); foreach() { chomp($_); split(":"); $names{$_[0]} = "true"; } close(PASS); foreach my $key (sort keys %names) { if($key eq $user) { return "true"; } else { #print "$key does not match $user\n"; } } return "false"; } sub create_profile { open(A, ">>home/$_[0]/.profile"); print A "PATH=/bin:/usr/bin:/usr/local/bin:/usr/share/bin:.\n"; print A "export PATH\n"; print A "\n"; print A "\n"; close(A); } sub create_htaccess { open(A, ">>home/$_[0]/public_html/cgi-bin/.htaccess"); print A "Options +ExecCGI\n"; close(A); } sub create_group { my($username, $gid) = @_; #open(GROUP, ">>/etc/group"); #print GROUP "$username:x:$gid:\n"; #close(GROUP); print "------ GROUP LINE ------\n"; print "$username:x:$gid:\n"; } sub create_user { my($username, $hash, $uid, $gid, $homedir, $shell) = @_; print "------ PASSWD LINE ------\n"; #open(PASS, ">>/etc/passwd"); #print PASS "$username:x:$uid:$gid:,,,:$homedir:$shell\n"; #close(PASS); print "$username:x:$uid:$gid:,,,:$homedir:$shell\n"; print "------ SHADOW LINE ------\n"; #open(SHADOW, ">>/etc/shadow"); #print SHADOW "$username:$hash"; #print SHADOW ":::::::\n"; #close(SHADOW); print "$username:$hash"; print ":::::::\n"; } sub set_permission { my ($username, $gid) = @_; print "... Setting permissions "; #`chown -hR $username:$gid home/$username`; print "... Done\n"; }