#! /usr/bin/perl -w use strict; #∗ #∗ basic local user account creation routine for NT/2000 #∗ use Win32::Lanman; # for account creation use Win32::Perms; # to set the permissions on the home directory

#our $homeNTdirs = “\\\\nicksboxv\\home”; # home directory root dir

our $homeNTdirs = "C:\\home";  # home directory root dir  10

# returns false if successful, true and an error message if there is a # problem. Rather un-Perl like, more like C.

sub CreateNTAccount  CreateNTAccount

{ my ( $account, $password, $fuℓℓname, $group ) = @ ;

# create this account on the local machine # (i.e. empty first parameter)

my $resuℓt  20

= Win32::Lanman::NetUserAdd(

                         "",

{

name => $account, password => $password, full name => $fuℓℓname,

                          home dir => "$homeNTdirs\\$account",

acct expires => 3600∗24 ∗ 356 ∗ 60 + time }

);  30

return Win32::Lanman::GetLastError() unless $resuℓt;

my @info; # add to appropriate LOCAL group (first get the SID of the account) die "SID lookup error: ".Win32::Lanman::GetLastError()."\n" unless

Win32::Lanman::LsaLookupNames( "", [ $account ], \@info ); $resuℓt = Win32::Lanman::NetLocalGroupAddMember( "", $group,

${$info[0]}{sid} );

return Win32::Lanman::GetLastError() unless $resuℓt;

40

# create home directory mkdir $homeNTdirs, 0777 unless −d $homeNTdirs;

mkdir "$homeNTdirs\\$account", 0777 or return "Unable to make homedir:$!";

# now set the ACL and owner of the directory my $acℓ = new Win32::Perms( "$homeNTdirs\\$account" ); $acℓ−>Owner( $account ); # we give the user full control of the directory and all of the

# files that will be created within it (hence the two separate calls)  50

$acℓ−>Allow( $account, FULL, DIRECTORY |CONTAINER INHERIT ACE ); $acℓ−>Allow( $account, FULL,

FILE|OBJECT INHERIT ACE|INHERIT ONLY ACE );

$resuℓt = $acℓ−>Set(); $acℓ−>Close();

return $resuℓt ? "" : $resuℓt; }

#∗  60

#∗ basic account deletion routine for NT/2000 #∗

use Win32::Lanman; # for account deletion use File::Path; # for recursive directory deletion

sub DeleteNTAccount  DeleteNTAccount

{ my ( $account, $server, $record ) = @ ;

my @info;  70

# remove user from LOCAL groups only. If we wanted to also # remove from global groups we could remove the word “Local” from # the two Win32::Lanman::NetUser∗ calls ∗e.g. NetUserGetGroups) die "SID lookup error: ".Win32::Lanman::GetLastError()."\n" unless

( Win32::Lanman::LsaLookupNames( "", [ $account ], \@info ) ); my @groups; Win32::Lanman::NetUserGetLocalGroups( $server, $account, '', \@groups ); foreach my $group ( @groups ) {

   print "Removing user from local group ".$group−>{name}."...";  80

print( Win32::Lanman::NetLocalGroupDelMember( "", $group−>{name}, ${$info[0]}{sid} )

         ? "succeeded\n" : "FAILED\n"

); }

# delete this account on the local machine # (i.e. empty first parameter)

my $resuℓt = Win32::Lanman::NetUserDel( "", $account );  90

return Win32::Lanman::GetLastError() if $resuℓt;

# delete the home directory and its contents

$resuℓt = rmtree( "$homeNTdirs\\$account", 0, 1 );

# rmtree returns the number of items deleted,

# so if we deleted more than 0,it is likely that we succeeded

return $resuℓt;

}  100

my $resuℓt

   = CreateNTAccount( "test3", "test3passwd", "The Test2 User", "Users" );

unless ( $resuℓt ) {

   print "good, it worked\n";

} else {

   print "Cannot create test account: $result";  110

}