iis ftp redirection
3dbc
created: 2006-03-20 15:51:10

#!/usr/bin/perl
use Win32::OLE;
use Win32::NetAdmin;
use Win32::Lanman;
#use Win32::Perms;

$server = "dmz";

print "\nHello welcome to Chris Thomas' perl IIS FTP Redirection Creator :-)\n\n";
$go = 1;
while ($go==1) {

	print "Please enter FTP Virtual Directory Name : ";
	chomp($user=);
	print "Please enter FTP Password : ";
	chomp($psswd=);

	$Object = Win32::OLE->GetObject("IIS://localhost/MSFTPSVC/1/Root")||die Win32::OLE->LastError();
	
	$PathToYourNewDir = "D:\\ftproot\\$user";
	

	%Account = (
		name => "$user",
		password => "$psswd",
		home_dir => "$PathToYourNewDir",
		comment => "FTP User Account Redirection",
		usr_comment => "CRT perl implementation",
		flags => UF_DONT_EXPIRE_PASSWD | UF_PASSWD_CANT_CHANGE,
		params => '',
		script_path => '',
		full_name => "FTP account $user",
		workstations => "",
		profile => "",
		acct_expires => -1,
		#logon_hours => pack( "b168", "11111111" x 21 ),
		home_dir_drive => "",
		password_expired => 1,
	);
	

	#system ("net user $user $psswd /ADD /FULLNAME:\"FTP account $user\" /COMMENT:\"FTP User Account Redirection\" /usercomment:\"CRT perl implementation\" /homedir:$PathToYourNewDir /expires:NEVER /passwordchg:no"); 

	#cusrmgr.exe. (cusrmgr -u  [-m \\] +s PasswordNeverExpires

	if (Win32::NetAdmin::UsersExist("$server", $user)) { #### CRT
			
		print "$user already exists\n";
		die;
		
	} else {
		print "adding user $user\n";
		Win32::Lanman::NetUserAdd( "$server", \%Account );
	}

	my $group = "FTP Users";

	system ("net localgroup \"$group\" $user /ADD"); 
	
	#only needed when using net user to add a the user becuase it by default adds the user to the users group.
	#system ("net localgroup \"Users\" $user /DELETE"); 
	
	makeHomefolder($PathToYourNewDir);
	

	my $type = "IIsFtpVirtualDir";
	print "Creating Virtual Directory $user\n";
	$VDir = $Object->Create( $type,"$user");
	$VDir->{AccessRead} = 1;
	$VDir->{AccessWrite} = 1;
	$VDir->{Path} = "$PathToYourNewDir";
	$VDir->SetInfo();

	makePermissions($PathToYourNewDir,$user);

	sub makeHomefolder
	{
		#create the user directory and then eventually set security on it
		print "Creating FTP Folder $_[0]\n";
		system ("mkdir $_[0]");
		#system ("xcacls $userRoot /t /y /g administrators:f $domain\\$id:c \"Authenticated Users\":r");
		#system ("xcacls $userRoot\\private /e /y /R Everyone \"Authenticated Users\"");
		#print "\nSeting Ownership on Users Home Folder\n";
		#system ("setowner -r $domain\\$id $userRoot");
		#print "\nCreating Users Hidden Share\n";
		#system ("rmtshare \\\\$homeServer\\$id\$=d:\\users\\$id");
	}

	sub makePermissions
	{
		$stop=1;
		opendir DIR, "$_[0]" or &ERROR();;
		
		sub ERROR {
			$stop=0;
		}
		if($stop==1) {
			closedir DIR;
			$ftpuser = "\\\\$server\\$_[1]"; #machine account, pass in the server name beginning with double backslashes
											 #domain account, refer to it by specifying the domain followed by a backslash 
			print "\nModifying Security on FTP Folder for Administrators and $ftpuser\n";
			
			#$Dir = new Win32::Perms( "$_[0]") || die;
			#$Dir->Remove(-1);
			#$Dir->Allow(Administrators,Win32::Perms::FULL_CONTROL_DIR,Win32::Perms::DIR);
			#$Dir->Allow(Administrators,Win32::Perms::FULL_CONTROL_FILE,Win32::Perms::FILE);
			#$Dir->Set();
			#$Dir->Allow("$ftpuser",Win32::Perms::CHANGE_DIR,Win32::Perms::DIR); #READ_DIR
			#$Dir->Allow("$ftpuser",Win32::Perms::CHANGE_FILE,Win32::Perms::FILE);
			#$Dir->Set();
			system "xcacls $_[0] /T /G administrators:f dmz\\$_[1]:EWX dmz\\master_userFTP:ewxdcp /Y";
			# xcacls *.* /t /c /y /g administrators:f;f test:EX;EX
			#xcacls D:\Inetpub\ftproot\physical_directory\*.* /t /c /y /g administrators:f;f PC\user2:EX;EX PC\user:f;f
		}
	}
	
	$file = "\%userprofile\%\\Desktop\\ftp_access_list.txt";
	if (! open(OUTPUT, ">>$file") ) {
	print "\nCan Not Open $file :: FATAL ERROR!\n";  
	die;
	}
	#$i = 1;
	print OUTPUT "\n$user\t\t$psswd";
	
	# the below code will parse the ftp_access_list file for usernames and passwords.
	#while(){
	#	chomp;
	#	if (/(\S+)(\s+)(\S+)/) {
	#		$ftp_href->{$i}{user} = $1;
	#		$ftp_href->{$i}{password} = $3;
	#	}
		#print "$_\n";
	#	print "ftp_href->{$i}{user} = $ftp_href->{$i}{user}\n";
	#	print "ftp_href->{$i}{password} = $ftp_href->{$i}{password}\n";
	#	$i++;
	#}
	close OUTPUT;

	print "Would you like to create another FTP account? [y|n]: ";
	chomp($go=);
	$go =~ s/[Yy]/1/;
	$go =~ s/[Nn]/0/;
	
	
	
}

quick and dirty, didn't use strict
Post any questions and I will respond ASAP.


Enjoy,
3dbc


-----BEGIN PERL GEEK CODE BLOCK-----
Version: 0.01
P*c?P6?R M O MA--E-PU-BD--C D++S+++X WP
MO+PP?n-CO--PO--o+G+A--OL!OLC-OLCC-OLL-
-OLA--Ee---Ev----Eon++++Eot!Eob!Eoa!uL
uS w--m+
------END PERL GEEK CODE BLOCK------

Re: iis ftp redirection
created: 2006-03-21 04:03:56
quick and dirty, didn't use strict

Do you have any reason not to use strict or warnings? This script is large enough that I would be unwilling to maintain or enhance it without use strict in place.

Have you wasted any time debugging typos that strict would have found? This nearly always justifies starting off with strict - it certainly does for me.

--

Oh Lord, won’t you burn me a Knoppix CD ?
My friends all rate Windows, I must disagree.
Your powers of persuasion will set them all free,
So oh Lord, won’t you burn me a Knoppix CD ?
(Missquoting Janis Joplin)

Re^2: iis ftp redirection
created: 2006-03-22 23:25:52
I don't use strict because perl doesn't and didn’t require me to.

I can appreciate strict when considering maintenance within the perl community and that ideal is probably what makes perl such a great portable language with so many useful modules. But since I haven’t written any modules yet and am evolving my understanding of the perl global community this is just one small contribution for all it has done for me.

I like perl compared to VBS because it doesn't have as many strict rules although VBS is powerful in its own right on windows systems. Perl is different especially when compared to Microsoft based languages WSH, ASP, .NET etc. and I like to be creative. Perl's flexible but yet a defined set of rules, without much restriction, is what attracts me to a programming language... somewhat similarly that is why I like PL/SQL over T-SQL. Microsoft languages have many more standard formalities, declarations... Perhaps my implementation is obscure and somewhat obfuscated according to strict implementations but it does what it says it does and it can be used on IIS 4, 5, 5.1 and 6 to disable anonymous access via IIS FTP.

Is creative, useful, unconventional, functionality obscure and obfuscated and therefore frowned upon?
Re^3: iis ftp redirection
created: 2006-03-22 23:47:24
Is creative, useful, unconventional, functionality obscure and obfuscated and therefore frowned upon?

No, not using strict is just undisciplined. I feel pretty safe in saying that most good, creative, unconventional code is written by people who use strict strictly.

When I was a VB programmer, it was easy to divide a line between "stuff that was crap" and "stuff that we don't know about yet but might be crap or might be great stuff" by whether it used Option Declare. So I just utterly disbelieve you. Go write great stuff. I love great stuff. But use strict and disable it only for those small parts where you know it's the best thing to do at that moment.

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re^4: iis ftp redirection
created: 2006-03-23 19:19:08
"it was easy to divide a line between "stuff that was crap" and "stuff that we don't know about yet but might be crap or might be great stuff" by whether it used Option Declare."

"Implicit variable declaration can make writing scripts faster and easier; at the same time, however, it can lead to subtle errors that are difficult to diagnose and fix." Does this definition contradict your statement? (Surprising since it is on M$ TechNet).
Re^5: iis ftp redirection
created: 2006-03-23 19:28:06

Actually, in my experience, that definition contradicts itself. It's those subtle errors that are difficult to diagnose and fix that makes writing scripts slower and more tedious. Unless, I suppose, we use "M$"'s definition where writing does not include testing and fixing, nor does it imply writing correctly...

Re^5: iis ftp redirection
created: 2006-03-23 19:51:07

It's worth noting that one of the nicest, in a feel-good kind of way, improvement to my dev tool was when it could be configured to automatically insert Option Declare into everything it created. This prevented me from accidentally omitting it. Its when I'd forgotten to ensure that Option Declare was turned on that I'd just run across some bug allowed by the absence of the restriction. It's at those moments that I knew I had some pain ahead of me as I added Option Declare back in and went back to find the bugs that it had allowed to go undetected.

So no. There's absolutely no good reason whatsoever to ever neglect to use Option Declare. There is occasionally a reason to revoke strict but you should never write a script without loading it. Heck, these days I've taught emacs to automatically syntax check my perl on save because that's also really valuable. The more checks that I get automatically, the better code I can write and the less time I have to spend fiddling with stupid plumbing that I've installed wrong.

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

perlmonks.org content © perlmonks.org and 3dbc, diotalevi, rinceWind, Tanktalus

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03