Thank you for your great module. I'd like to use it for a project I'm
working on but I would like to be able to change the location of the
smbpasswd file. I wrote a patch to do that and I'm wondering if you
would be willing to merge it in.
This patch adds the following changes:
1) A new method called smbpasswd_file
2) The option to pass smbpasswd to the object's constructor
3) Will use the configured smbpasswd path or fall back to the PASSWD
constant.
4) I bumped the version number but I'm not sure if that's how you want
it incremented.
5) I updated the POD to include the new method and constructor argument.
6) I tried to follow your code style from Passwd::Unix as closely as
possible to add this feature.
Thank you,
Andy
Subject: | samba.patch |
--- ../Passwd-Samba-0.141.orig/Samba.pm 2011-11-19 03:24:21.000000000 -0800
+++ Samba.pm 2012-07-09 11:31:32.000000000 -0700
@@ -6,19 +6,36 @@
use strict;
use Crypt::SmbHash;
+use File::Spec;
#======================================================================
-$VERSION = '0.141';
+$VERSION = '0.142';
@ISA = qw(Exporter);
-@EXPORT_OK = qw(del uid maxuid passwd rename user users);
+@EXPORT_OK = qw(del uid maxuid passwd rename user users smbpasswd_file);
#======================================================================
use constant PASSWD => '/etc/samba/smbpasswd';
#======================================================================
-sub new { return bless { }, __PACKAGE__; }
+my $Self = __PACKAGE__->new();
+#======================================================================
+sub new {
+ my ($class, %params) = @_;
+
+ return bless {
+ smbpasswd => (defined $params{smbpasswd} ? $params{smbpasswd} : PASSWD),
+ }, __PACKAGE__;
+}
+#======================================================================
+sub smbpasswd_file {
+ my $self = scalar @_ && ref $_[0] eq __PACKAGE__ ? shift : $Self;
+ my ($val) = @_;
+ return $self->{smbpasswd} unless defined $val;
+ $self->{smbpasswd} = File::Spec->canonpath($val);
+ return $self->{smbpasswd};
+}
#======================================================================
sub del {
- shift if $_[0] =~ __PACKAGE__;
+ my $self = scalar @_ && ref $_[0] eq __PACKAGE__ ? shift : $Self;
- open(my $fh, '<', PASSWD);
+ open(my $fh, '<', $self->smbpasswd_file);
my @a;
while(<$fh>){
push @a,$_ if /^[^:]+:/o;
@@ -28,7 +45,7 @@
my $re = '^'.join('$|^', @_).'$';
$re = qr/$re/;
- open($fh, '>', PASSWD);
+ open($fh, '>', $self->smbpasswd_file);
print $fh grep { (split(/:/,$_))[0] !~ $re } @a;
close($fh);
@@ -36,10 +53,10 @@
}
#======================================================================
sub rename {
- shift if $_[0] =~ __PACKAGE__;
+ my $self = scalar @_ && ref $_[0] eq __PACKAGE__ ? shift : $Self;
return unless defined $_[1];
- open(my $fh, '<', PASSWD);
+ open(my $fh, '<', $self->smbpasswd_file);
my @a;
while(<$fh>){
push @a,$_ if /^[^:]+:/o;
@@ -50,7 +67,7 @@
@a = map { s/^$_[0]:/$_[1]:/;$_ } @a;
- open($fh, '>', PASSWD);
+ open($fh, '>', $self->smbpasswd_file);
print $fh @a;
close($fh);
@@ -58,9 +75,9 @@
}
#======================================================================
sub uid {
- shift if $_[0] =~ __PACKAGE__;
+ my $self = scalar @_ && ref $_[0] eq __PACKAGE__ ? shift : $Self;
- open(my $fh, '<', PASSWD);
+ open(my $fh, '<', $self->smbpasswd_file);
if(not defined $_[1]){
while(<$fh>){
return (split(/:/,$_))[1] if /^$_[0]:/;
@@ -77,7 +94,7 @@
}
}
close($fh);
- open($fh, '>', PASSWD);
+ open($fh, '>', $self->smbpasswd_file);
print $fh @a;
}
close($fh);
@@ -86,8 +103,10 @@
}
#======================================================================
sub maxuid {
+ my $self = scalar @_ && ref $_[0] eq __PACKAGE__ ? shift : $Self;
+
my $max = 0;
- open(my $fh, '<', PASSWD);
+ open(my $fh, '<', $self->smbpasswd_file);
while(<$fh>){
my $tmp = (split(/:/,$_))[1];
$max = $tmp > $max ? $tmp : $max;
@@ -99,7 +118,7 @@
*user = \&passwd;
#======================================================================
sub passwd {
- shift if $_[0] =~ __PACKAGE__;
+ my $self = scalar @_ && ref $_[0] eq __PACKAGE__ ? shift : $Self;
my ($name, $passwd) = @_;
return unless defined $passwd;
@@ -108,7 +127,7 @@
ntlmgen $passwd, $lm, $nt;
__PACKAGE__->del($name);
- open(my $fh, '>>', PASSWD);
+ open(my $fh, '>>', $self->smbpasswd_file);
printf $fh "%s:%d:%s:%s:[%-11s]:LCT-%08X\n", $name, $uid, $lm, $nt, "U", time;
close($fh);
@@ -116,8 +135,10 @@
}
#======================================================================
sub users {
+ my $self = scalar @_ && ref $_[0] eq __PACKAGE__ ? shift : $Self;
+
my @a;
- open(my $fh, '<', PASSWD);
+ open(my $fh, '<', $self->smbpasswd_file);
push @a, (split(/:/,$_))[0] while <$fh>;
close($fh);
return @a;
@@ -166,7 +187,13 @@
=item B<new( )>
-Constructor.
+Constructor. Possible parameters are:
+
+=over 8
+
+=item B<smbpasswd> - path to smbpasswd file; default C</etc/samba/smbpasswd>
+
+=back
=item B<del( USERNAME0, USERNAME1... )>
@@ -196,6 +223,11 @@
This method returns a list of all existing usernames.
+=item B<smbpasswd_file([PATH])>
+
+This method, if called with an argument, sets path to the I<smbpasswd> file.
+Otherwise returns the current PATH.
+
=back
=head1 DEPENDENCIES