Subject: | [PATCH] user/group owner for socket |
On the madisonlinux.org mailing list, "Brad" posted the attached patch
with this note:
"I thought some folks on the list might find this useful. In working on a
project of mine which uses Net::Server, I wanted it to create a socket
readable/writeable by postfix smtpd. Currently, the only way to do that
is to either run Net::Server as the postfix user (ick), or set a umask
to make created files world-writeable (including the pid file, and
anything else it creates -- bigger ick!).
"I wrote a patch for Net::Server to allow specifying user/group socket
ownership and/or socket modes. The patch is attached; apply, and pass
the configuration options sock_owner, sock_group, and/or sock_mode
(octal) to your Net::Server et voila! If any options are omitted,
current behavior is unchanged."
Subject: | Net-Server.sock_owner.patch |
--- Server.pm.orig 2008-11-16 00:35:04.000000000 -0800
+++ Server.pm 2008-11-18 11:26:24.000000000 -0800
@@ -488,12 +488,64 @@
}
+ ### figure out group to own UNIX sockets
+ if ( defined $prop->{sock_group} ){
+ if ( $prop->{sock_group} =~ /^([\w-]+( [\w-]+)*)$/ ){
+ $prop->{sock_group} = eval { get_gid( $1 ) };
+ $self->fatal( $@ ) if $@;
+ }else{
+ $self->fatal("Invalid group socket owner \"$prop->{sock_group}\"");
+ }
+ }
+
+
+ ### figure out user to own UNIX sockets
+ if ( defined $prop->{sock_owner} ){
+ if ( $prop->{sock_owner} =~ /^([\w-]+)$/ ){
+ $prop->{sock_owner} = eval{ get_uid( $1 ) };
+ $self->fatal ( $@ ) if $@;
+ }else{
+ $self->fatal("Invalid socket owner \"$prop->{sock_owner}\"");
+ }
+ }
+
+
+ ### verify mode on UNIX sockets
+ if ( defined $prop->{sock_mode} ){
+ my $orig_mode = $prop->{sock_mode};
+ $prop->{sock_mode} = oct( $orig_mode );
+ if ( ($prop->{sock_mode} > 07777) || ($prop->{sock_mode} < 1) ){
+ $self->fatal("Invalid socket mode \"$orig_mode\"");
+ }
+ }
+
+
### chown any files or sockets that we need to
if( $prop->{group} ne $) || $prop->{user} ne $> ){
my @chown_files = ();
foreach my $sock ( @{ $prop->{sock} } ){
- push @chown_files, $sock->NS_unix_path
- if ($sock->NS_proto eq 'UNIX')
+ if ($sock->NS_proto eq 'UNIX') {
+ my $file = $sock->NS_unix_path;
+ if (( defined $prop->{sock_owner} ) && ( defined $prop->{sock_group} )){
+ chown( $prop->{sock_owner}, $prop->{sock_group}, $file )
+ or $self->fatal("Couldn't chown \"$file\" [$!]\n");
+ }
+ elsif ( defined $prop->{sock_owner} ){
+ chown( $prop->{sock_owner}, (split(/\ /, $prop->{group}))[0], $file )
+ or $self->fatal("Couldn't chown \"$file\" [$!]\n");
+ }
+ elsif ( defined $prop->{sock_group} ){
+ chown ( $prop->{user}, $prop->{sock_group}, $file )
+ or $self->fatal("Couldn't chown \"$file\" [$!]\n");
+ }
+ else {
+ push @chown_files, $file;
+ }
+ if ( defined $prop->{sock_mode} ){
+ chmod( $prop->{sock_mode}, $file )
+ or $self->fatal("Couldn't chmod \"$file\" [$!]\n");
+ }
+ }
}
if( $prop->{pid_file_unlink} ){
push @chown_files, $prop->{pid_file};
@@ -1335,6 +1387,7 @@
foreach ( qw(conf_file
user group chroot log_level
+ sock_owner sock_group sock_mode
log_file pid_file background setsid
listen reverse_lookups
syslog_logsock syslog_ident