aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2020-04-29 23:10:11 +1200
committerTom Ryder <tom@sanctum.geek.nz>2020-04-29 23:11:06 +1200
commit4e10a60e49e9e5d5baa06df1fcbeb1b5fdb103ee (patch)
tree31b4f0c591cecb8755ab859c6b82ae73f250a64e
downloadinotifymask-4e10a60e49e9e5d5baa06df1fcbeb1b5fdb103ee.tar.gz
inotifymask-4e10a60e49e9e5d5baa06df1fcbeb1b5fdb103ee.zip
First commitv0.01
-rw-r--r--.gitignore17
-rw-r--r--LICENSE7
-rw-r--r--MANIFEST4
-rw-r--r--Makefile.PL33
-rw-r--r--README.md39
-rw-r--r--bin/inotifymask69
6 files changed, 169 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b859442
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,17 @@
+Makefile
+Makefile.old
+Build
+Build.bat
+META.*
+MYMETA.*
+.build/
+_build/
+cover_db/
+blib/
+inc/
+.lwpcookies
+.last_cover_stats
+nytprof.out
+pod2htm*.tmp
+pm_to_blib
+App-inotifymask-*
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..cb45c5d
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,7 @@
+Copyright 2020 Tom Ryder
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/MANIFEST b/MANIFEST
new file mode 100644
index 0000000..77c464d
--- /dev/null
+++ b/MANIFEST
@@ -0,0 +1,4 @@
+bin/inotifymask
+LICENSE
+MANIFEST
+Makefile.PL
diff --git a/Makefile.PL b/Makefile.PL
new file mode 100644
index 0000000..0bfc6f3
--- /dev/null
+++ b/Makefile.PL
@@ -0,0 +1,33 @@
+use 5.006;
+use strict;
+use warnings;
+use ExtUtils::MakeMaker;
+
+WriteMakefile(
+ NAME => 'App::inotifymask',
+ AUTHOR => 'Tom Ryder <tom@sanctum.geek.nz>',
+ VERSION_FROM => 'bin/inotifymask',
+ LICENSE => 'MIT',
+ EXE_FILES => ['bin/inotifymask'],
+ MIN_PERL_VERSION => '5.010_001',
+ CONFIGURE_REQUIRES => {
+ 'ExtUtils::MakeMaker' => '0',
+ },
+ PREREQ_PM => {
+ 'Const::Fast' => 0,
+ 'File::stat' => 0,
+ 'Linux::Inotify2' => 0,
+ },
+ META_MERGE => {
+ 'meta-spec' => { version => 2 },
+ resources => {
+ homepage => 'https://sanctum.geek.nz/cgit/inotifymask.git/',
+ repository => {
+ type => 'git',
+ url => 'https://sanctum.geek.nz/code/inotifymask.git/',
+ web => 'https://sanctum.geek.nz/cgit/inotifymask.git/',
+ },
+ },
+ },
+ clean => { FILES => 'App-inotifymask-*' },
+);
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..efc82b1
--- /dev/null
+++ b/README.md
@@ -0,0 +1,39 @@
+inotifymask
+===========
+
+This tool sets up Linux kernel `inotify` hooks on a set of directories to apply
+a bit mask to their permissions on file creation within or movement into the
+watched directories, logging each permission change. This can be used as a way
+to enforce permissions for uncooperative applications that don't allow
+specifying permissions or masking.
+
+Install
+-------
+
+ $ cpanm Const:Fast Linux::Inotify2
+ $ perl Makefile.PL
+ $ make
+ $ sudo make install
+
+Use
+---
+
+ $ inotifymask 0177 ~/.cache/private ~/.cache/private/templates
+
+Works well as a user-level systemd service; see included `inotifymask.service`
+template.
+
+ $ mkdir -p ~/.config/systemd/user
+ $ cp inotifymask.service ~/.config/systemd/user
+ $ vi inotifymask.service # Change command line to specify your paths
+ $ systemctl --user enable inotifymask.service
+ $ systemctl --user start inotifymask.service
+ $ systemctl --user status inotifymask.service
+
+License
+-------
+
+Copyright (c) [Tom Ryder][1]. Distributed under [MIT License][2].
+
+[1]: https://sanctum.geek.nz/
+[2]: https://opensource.org/licenses/MIT
diff --git a/bin/inotifymask b/bin/inotifymask
new file mode 100644
index 0000000..55e6dd7
--- /dev/null
+++ b/bin/inotifymask
@@ -0,0 +1,69 @@
+#!perl
+
+use 5.028_001;
+use strict;
+use warnings;
+use utf8;
+
+use Const::Fast;
+use File::stat;
+use Linux::Inotify2;
+
+our $VERSION = '0.01';
+
+const our $SELF => 'inotifymask';
+
+# Mask to remove the bits of stat->mode that we don't care about
+const our $STAT_MASK => oct '07777';
+
+# Check argument count
+if ( @ARGV < 2 ) {
+ printf {*STDERR} "%s: Need a mask and at least one file\n", $SELF;
+ exit 2;
+}
+
+# Shift off mask as octal, remaining arguments are files
+my $mask = oct shift;
+my @files = @ARGV;
+
+# Process creation and move-in events
+my $cb = sub {
+ my ($event) = @_;
+ my $name = $event->fullname;
+
+ # File must exist
+ -e $name
+ or return;
+
+ # Must not be a directory
+ not -d $name
+ or return;
+
+ # Get stats or give up
+ my $stat = stat $name
+ or return;
+
+ # Get the mode we want for the file, masking off irrelevant file type bits
+ my $mode = $stat->mode & $STAT_MASK;
+
+ # Check that at least one bit of the mask coincides with one bit of the
+ # present mode, i.e. that we'll be changing anything
+ $mode & $mask
+ or return;
+
+ # Using the masked mode, change the permissions and log to stderr
+ for ( $mode & ~$mask ) {
+ chmod $_, $name
+ or return;
+ printf {*STDERR} "secured %s %o\n", $name, $_;
+ }
+};
+
+# Create object and set up watches with callback, start polling
+my $in = Linux::Inotify2->new;
+for (@files) {
+ $in->watch( $_, IN_CREATE | IN_MOVED_TO, $cb );
+}
+while (1) {
+ $in->poll;
+}