Synchronize Unix/SMB passwords with 'synchpasswd'

Shawn A. Clifford sac at dvs.nuphase.com
Fri Jul 3 17:00:35 GMT 1998


Skipped content of type multipart/alternative-------------- next part --------------
#!/usr/local/bin/perl

#
#  Title:	synchpasswd
#  Author:	Shawn A. Clifford <sac at nuphase.com>
#  Date:	2-July-1998
#  Purpose:	Adds all users in the Unix password file to the SMB
#		password file.  If the user is already listed in
#		the SMB password, then no change is made to that entry.
#		All invalid accounts, either no longer present or system
#		accounts, will be removed from the SMB password file.
#
#  Externals:	Michal Jaegermann's 'addtosmbpass' awk/nawk script,
#		included in the Samba distribution.  Look in the
#		Samba binaries directory.  Eg. /usr/local/samba/bin
#
#  Disclaimer:	I am not an expert Perl programmer.  So this is perhaps
#		a brutish way to achieve our goal.  Please feel free
#		to modify this script.  I'd appreciate an email of any
#		revisions you make.
#
#		I take no responsibility for any "adverse" behavior of
#		this script.  USE AT YOUR OWN RISK !!
#
#  Modification History:
#
#  Who	Date		Description
#  ---	-----------	-----------------------------------------------------
#  SAC	03-Jul-1998	Handle comment lines correctly.
#

#
#  Modify these definitions to match your site's installation, and 
#  your preferences.
#
$smb_passwd  = "/usr/local/samba/private/smbpasswd";
$add_script  = "/usr/local/samba/bin/addtosmbpass";
$private_dir = "/usr/local/samba/bin/private";
$system_id   = 99;		# Remove user ID's below this value from SMB

#
#  Let's go...
#
use Text::ParseWords;		# For the 'quotewords' function
chdir($private_dir);

#
#  Scan through /etc/passwd.  Create an array of names to pass to
#  'addtosmbpass', but omit all system accounts.
#
printf("\nCreating a list of Unix users:\n");
setpwent;				# Initialize the scan
while (@list = getpwent) {		# Fetch the next entry
	($login,$uid) = @list[0,2];	# Grab login name and uid
	if ($uid <= $system_id) {
		printf ("\tOmmitting $login ...\n");
	} else {
		@USERS = (@USERS, "$login");
	}
}
endpwent;				# End of scan

#
#  Add the users
#
printf ("\nMerging new users ...\n");
unless (fork) {
	exec "$add_script @USERS < $smb_passwd > synch.tmp";
}
wait;				# Parent waits for child to complete

#
#  Remove all invalid entries from 'synch.tmp'.  I check the uid again
#  because the SMB password file may have these entries from before.
#
printf ("\nRemoving invalid accounts:\n");
open(IN, 'synch.tmp') || die "Can't open temp file:  synch.tmp";
open(OUT, '>synch.out') || die "Can't create output file:  synch.out";
while ($line = <IN>) {
	if ($line =~ /#/) {			# Keep comment lines
		printf ("\tWriting comment line ...\n");
		print OUT $line;
		next;
	}
	@tokens = quotewords(':', 0, $line);	# Tokenize the fields
	($login,$uid) = @tokens[0,1];		# Grab login name & uid
	if ($uid <= $system_id) {		# Remove system accounts
		printf ("\tRemoving $login ...\n");
		next;
	}
	if ( (@validate = getpwnam("$login")) == NULL) {
		printf ("\tNo such user:  $login\n");
		next;
	}
	print OUT $line;			# Keep this line
}

#
#  Clean up the files and store final version of the SMB password file
#
close (OUT);
close (IN);
unlink ('synch.tmp');
rename ('synch.out', $smb_passwd);
-------------- next part --------------
#!/bin/perl 
# 
# buildsmbpasswd Unix-password-file SMB-password-file 
# 
# build the smbpasswd file from the /var/yp/passwd file. 
# passwords in the smbpasswd file for accounts which exist in both files 
# are preserved. 
# account in the smbpasswd file which do not exist in /var/yp/passwd 
# are discarded. 
# the resulting smbpasswd file is printed to standard output. 
# 
# todd pfaff 
# may 28, 1997
# Todd Pfaff \ Email: pfaff at mcmaster.ca 
# Computing and Information Services \ Voice: (905) 525-9140 x22920 
# ABB 132 \ FAX: (905) 528-3773 
# McMaster University \ 
# Hamilton, Ontario, Canada L8S 4M1 \
# $passwd="/var/yp/passwd"; 
$passwd=@ARGV[0] || die; 
# $smbpasswd="/usr/local/samba/private/smbpasswd"; 
$smbpasswd=@ARGV[1] || die; 
$osmbpasswd="$smbpasswd.old";
open(PW,"<$passwd"); 
while(<PW>) { 
chop; 
push @pw, $_; 
} 
close PW;
rename $smbpasswd, $osmbpasswd;
open(PW,"<$osmbpasswd"); 
while(<PW>) { 
chop; 
($uname,$uid,$pw1,$pw2,$fname,$dir,$shell)=split(':'); 
$spw{$uname}=$_; 
} 
close PW;
open(PW,">$smbpasswd"); 
foreach $account (@pw) { 
($uname,$pw,$uid,$gid,$fname,$dir,$shell)=split(':',$account); 
$pw1="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 
$pw2="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 
if($spw{$uname}) { 
($xuname,$xuid,$pw1,$pw2,$xfname,$xdir,$xshell)=split(':',$spw{$uname}); 
} 
printf(PW 
"%s:%s:%s:%s:%s:%s:%s\n",$uname,$uid,$pw1,$pw2,$fname,$dir,$shell); 
} 
close(PW);


More information about the samba mailing list