#!/usr/bin/perl -w
#
# A sample script to trigger smbd to change the machine trust account
# password.

# This program is put in the public domain by Jerry Murdock 
# <jmurdock@itraktech.com>. It is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Author:
#   Jerry Murdock <jmurdock@itraktech.com>
#
# Version history:
#   2002-07-11	Jerry Murdock <jmurdock@itraktech.com>
#		Initial release
#

# What does this do?
#
# Generate enough smb activity to trigger smbd's machine trust account
# password change code.
#
# Every 200 requests smbd triggers the timeout_processing code, which
# in turn check's the trust account password age.  If the password is 
# older than the smb.conf value "machine password timeout" smbd will 
# change the password. 
#
# The default value for "machine password timeout" is 604800 seconds 
# (7 days).  With this default, even if the script is scheduled daily, 
# the password will only be changed every seven days.  In practice it
# is likely to only reliably change every eight days as the timing is
# down to the second. Eight days is probably fine, but reduce the
# "machine password timeout" to something like 604500 
# (6 days, 23 hrs, 55 min) to be sure of a weekly change.
#
# A service(share) will need to be created for smbclient to connect to.
# The user doesn't need unix rights to the shared directory, and the 
# service can be retricted pretty tightly:
# 
# [fredsdir]
#   comment = Fred's Service
#   path = /some/path
#   valid users = myuser
#   public = no
#   writeable = no
#   printable = no
#   browseable = no
# 
# The goal is to generate requests to smbd.  It doesn't matter if
# smbd can actually do what we ask as long as it doesn't close the
# connection.
#
# Loading smbd with the  "-i" parameter will cause smbd to exit after
# the first connection attempt. We won't have to kill it when done.  
# There is potential someone else could connect before we do.  Use the
# smb.conf "hosts allow" directive to limit who can connect.


# remove if smbd normally running
system("smbd -i &");	

# let smbd initialize.  
sleep 2;		

#change service name, user and password as required
open (PIPE, "| smbclient //127.0.0.1/fredsdir -U mydomain/myuser%mypass");
select PIPE;
$|=1;
for ($i = 1; $i < 205; $i++) {
	print "mkdir dummydir\n";
	sleep 1;
}
print "rmdir dummydir\n";
print "exit\n";

