From 76d8f5f1476d772ecccdaef8ee5fe92405873279 Mon Sep 17 00:00:00 2001 From: Thomas Hooge Date: Sat, 21 Nov 2020 10:21:39 +0100 Subject: [PATCH] Added support for configuration files --- INSTALL | 3 +++ lib/GUI.pm | 2 +- lib/HELPERS.pm | 27 +++++++++++++++++++++++++++ tinyca.cnf.example | 8 ++++++++ tinyca2 | 23 ++++++++++++----------- 5 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 tinyca.cnf.example diff --git a/INSTALL b/INSTALL index 5f174e0..c05df98 100644 --- a/INSTALL +++ b/INSTALL @@ -2,6 +2,9 @@ 2. Configure the following paths for your setup. These variables are located in the file tinyca itself. + As of version 0.7.6 these settings should be configured in + /etc/tinyca/tinyca.cnf. So there is no more need to modify + the code itself. See tinyca.cnf.example. @INC (location of the directory lib) $init->{'opensslbin'} (location of your openssl binary) diff --git a/lib/GUI.pm b/lib/GUI.pm index 0510668..f390377 100644 --- a/lib/GUI.pm +++ b/lib/GUI.pm @@ -119,7 +119,7 @@ sub new { $self->{'cursor'} = Gtk2::Gdk::Cursor->new('left-ptr'); $self->{'rootwin'} = Gtk2::Gdk->get_default_root_window(); - # split window to add menu, toolbar and notebook + # split window horizontal to add menu, toolbar and notebook $self->{'mvb'} = Gtk2::VBox->new(); $self->{'mw'}->add($self->{'mvb'}); diff --git a/lib/HELPERS.pm b/lib/HELPERS.pm index 8c072fe..8070949 100644 --- a/lib/HELPERS.pm +++ b/lib/HELPERS.pm @@ -21,11 +21,38 @@ use strict; package HELPERS; use POSIX; +use Config::Tiny; my $version = "0.1"; my $true = 1; my $false = undef; +# +# read global configuration file +# +sub read_global_cfg { + my $cfg = Config::Tiny->read("/etc/tinyca/tinyca.cnf", 'utf8'); + return ($cfg); +} + +# +# read a per CA configuration +# +sub read_cfg { + my $base = shift; + my $cfg = Config::Tiny->read($base."/tinyca.cnf", 'utf8'); + return ($cfg); +} + +# +# read per user configuration +# e.g. last used ca, last window position +# +sub read_user_cfg { + my $cfg = Config::Tiny->read($ENV{HOME}."/.tinycarc", 'utf8'); + return ($cfg); +} + # # generate filename from Subject-DN # diff --git a/tinyca.cnf.example b/tinyca.cnf.example new file mode 100644 index 0000000..e1dafa7 --- /dev/null +++ b/tinyca.cnf.example @@ -0,0 +1,8 @@ +[paths] +basedir = /var/lib/tinyca +exportdir = /tmp +templatedir = /usr/share/tinyaca/templates + +opensslbin = /usr/bin/openssl +zipbin = /usr/bin/zip +tarbin = /bin/tar diff --git a/tinyca2 b/tinyca2 index 29308a2..1b39b3d 100755 --- a/tinyca2 +++ b/tinyca2 @@ -52,49 +52,50 @@ textdomain("tinyca2"); # https://bugs.gentoo.org/show_bug.cgi?id=78576 $ENV{XLIB_SKIP_ARGB_VISUALS}= '1'; +my $cfg = HELPERS::read_global_cfg(); my $init = {}; # location of openssl -$init->{'opensslbin'} = "/usr/bin/openssl"; -$init->{'zipbin'} = "/usr/bin/zip"; -$init->{'tarbin'} = "/bin/tar"; +$init->{'opensslbin'} = $cfg->{paths}{opensslbin} // "/usr/bin/openssl"; +$init->{'zipbin'} = $cfg->{paths}{zipbin} // "/usr/bin/zip"; +$init->{'tarbin'} = $cfg->{paths}{tarbin} // "/bin/tar"; if(not -x $init->{'opensslbin'}) { printf(gettext("Can't execute %s.\n"), $init->{'opensslbin'}); - print gettext("Configure correct path to openssl in tinyca.\n"); + print gettext("Configure correct path to openssl.\n"); exit(1); } if(not -x $init->{'zipbin'}) { print gettext("zip command not found, support disabled.\n"); - print gettext("Configure correct path to zip in tinyca.\n"); + print gettext("Configure correct path to zip.\n"); } if(not -x $init->{'tarbin'}) { print gettext("tar command not found, support disabled.\n"); - print gettext("Configure correct path to tar in tinyca.\n"); + print gettext("Configure correct path to tar.\n"); } # directory with the templates -$init->{'templatedir'} = "./templates"; +$init->{'templatedir'} = $cfg->{paths}{templatedir} // "./templates"; if(not -d $init->{'templatedir'}) { print gettext("Can't find templatedir.\n"); - print gettext("Please configure correct path with templates in tinyca.\n"); + print gettext("Please configure correct path with templates.\n"); exit(1); } # location for CA files if( exists $ENV{'TINYCA_BASEDIR'}) { - $init->{'basedir'} = $ENV{'TINYCA_BASEDIR'} + $init->{'basedir'} = $ENV{'TINYCA_BASEDIR'} } else { - $init->{'basedir'} = $ENV{HOME}."/.TinyCA"; + $init->{'basedir'} = $cfg->{paths}{basedir} // $ENV{HOME}."/.TinyCA"; } if( exists $ENV{'TINYCA_EXPORTDIR'}) { $init->{'exportdir'} = $ENV{'TINYCA_EXPORTDIR'}; } else { - $init->{'exportdir'} = $ENV{HOME}; + $init->{'exportdir'} = $cfg->{paths}{exportdir} // $ENV{HOME}; } umask(0077);