From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stefan Schantl To: development@lists.ipfire.org Subject: [PATCHv2] ddns.cgi: Make dealing with auth tokens more user-friendly. Date: Wed, 06 Jan 2021 15:18:27 +0100 Message-ID: <20210106141827.3947-1-stefan.schantl@ipfire.org> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============1066692623536927781==" List-Id: --===============1066692623536927781== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable If a provider supports authentication with a token, now the username and password fileds will be swapped by some Java Script code in favour of an input field for the token. Signed-off-by: Stefan Schantl --- html/cgi-bin/ddns.cgi | 112 ++++++++++++++++++++++++++++++++++++----- langs/de/cgi-bin/de.pl | 2 + langs/en/cgi-bin/en.pl | 2 + 3 files changed, 104 insertions(+), 12 deletions(-) diff --git a/html/cgi-bin/ddns.cgi b/html/cgi-bin/ddns.cgi index 024eaf7f6..9b58db895 100644 --- a/html/cgi-bin/ddns.cgi +++ b/html/cgi-bin/ddns.cgi @@ -59,14 +59,18 @@ $settings{'HOSTNAME'} =3D ''; $settings{'DOMAIN'} =3D ''; $settings{'LOGIN'} =3D ''; $settings{'PASSWORD'} =3D ''; +$settings{'TOKEN'} =3D ''; $settings{'ENABLED'} =3D ''; $settings{'PROXY'} =3D ''; $settings{'SERVICE'} =3D ''; =20 $settings{'ACTION'} =3D ''; =20 -# Get supported ddns providers. -my @providers =3D &GetProviders(); +# Get all supported ddns providers. +my @providers =3D &GetProviders("all"); + +# Get provider which support a token based authentication mechanism. +my @token_provider =3D &GetProviders("token-providers"); =20 # Hook to regenerate the configuration files, if cgi got called from command= line. if ($ENV{"REMOTE_ADDR"} eq "") { @@ -178,6 +182,11 @@ if (($settings{'ACTION'} eq $Lang::tr{'add'}) || ($setti= ngs{'ACTION'} eq $Lang:: $errormessage =3D $Lang::tr{'password not set'}; } =20 + # Check if a token has been given for provider which support tokens. + if (($settings{'SERVICE'} ~~ @token_provider) && ($settings{'TOKEN'} eq '')= ) { + $errormessage =3D $Lang::tr{'token not set'}; + } + # Go furter if there was no error. if (!$errormessage) { # Splitt hostname field into 2 parts for storrage. @@ -189,6 +198,16 @@ if (($settings{'ACTION'} eq $Lang::tr{'add'}) || ($setti= ngs{'ACTION'} eq $Lang:: $settings{'ENABLED'} =3D 'off'; } =20 + # Handle token provider. + if($settings{'SERVICE'} ~~ @token_provider) { + # Clear username and password if they contain values. + undef($settings{'LOGIN'}); + undef($settings{'PASSWORD'}); + + # Assign the token as a password for saving. + $settings{'PASSWORD'} =3D $settings{'TOKEN'}; + } + # Handle adding new accounts. if ($settings{'ACTION'} eq $Lang::tr{'add'}) { # Open /var/ipfire/ddns/config for writing. @@ -234,7 +253,8 @@ if (($settings{'ACTION'} eq $Lang::tr{'add'}) || ($settin= gs{'ACTION'} eq $Lang:: # Write out notice to logfile. &General::log($Lang::tr{'ddns hostname modified'}); } - undef $settings{'ID'}; + # Clear settings hash. + %settings =3D ''; =20 # Update ddns config file. &GenerateDDNSConfigFile(); @@ -307,6 +327,7 @@ if ($settings{'ACTION'} eq $Lang::tr{'edit'}) { $settings{'WILDCARDS'} =3D $temp[4]; $settings{'LOGIN'} =3D $temp[5]; $settings{'PASSWORD'} =3D $temp[6]; + $settings{'TOKEN'} =3D $temp[6]; $settings{'ENABLED'} =3D $temp[7]; } =20 @@ -334,6 +355,58 @@ if (!$settings{'ACTION'}) { } =20 &Header::openpage($Lang::tr{'dynamic dns'}, 1, ''); + +### Java Script ### +print" +END +; + &Header::openbigbox('100%', 'left', '', $errormessage); =20 # Read file for general ddns settings. @@ -414,7 +487,7 @@ print <\n"; + print"= - $Lang::tr{'username'} - + + $Lang::tr{'username'} + + + $Lang::tr{'token'} + =20 - + $Lang::tr{'password'} @@ -665,8 +742,8 @@ sub GenerateDDNSConfigFile { =20 my $use_token =3D 0; =20 - # Check if token based auth is configured. - if ($username eq "token") { + # Handle token based auth for various providers. + if ($provider ~~ @token_provider) { $use_token =3D 1; } =20 @@ -707,9 +784,20 @@ sub GenerateDDNSConfigFile { } =20 # Function which generates an array (@providers) which contains the supporte= d providers. -sub GetProviders { - # Get supported providers. - open(PROVIDERS, "/usr/bin/ddns list-providers |"); +sub GetProviders ($) { + my ($type) =3D @_; + + # Set default type to get all providers + $type =3D $type ? $type : "all"; + + # Check if the requested type is "token-providers". + if ($type eq "token-providers") { + # Call ddns util to only get providers which supports token based auth. + open(PROVIDERS, "/usr/bin/ddns list-token-providers |"); + } else { + # Get all supported providers. + open(PROVIDERS, "/usr/bin/ddns list-providers |"); + } =20 # Create new array to store the providers. my @providers =3D (); diff --git a/langs/de/cgi-bin/de.pl b/langs/de/cgi-bin/de.pl index 87181c184..307b8a97c 100644 --- a/langs/de/cgi-bin/de.pl +++ b/langs/de/cgi-bin/de.pl @@ -2374,6 +2374,8 @@ 'to warn email bad' =3D> 'An E-Mail-Adresse ist nicht g=C3=BCltig', 'toggle' =3D> 'Pause/Resume', 'toggle enable disable' =3D> 'Aktivieren oder Deaktivieren', +'token' =3D> 'Token:', +'token not set' =3D> 'Kein Token angegeben.', 'tone' =3D> 'Ton', 'tone dial' =3D> 'Tonwahl:', 'too long 80 char max' =3D> ' ist zu lang, es sind maximal 80 Zeichen erlaub= t', diff --git a/langs/en/cgi-bin/en.pl b/langs/en/cgi-bin/en.pl index 625c6899f..4d23f7aac 100644 --- a/langs/en/cgi-bin/en.pl +++ b/langs/en/cgi-bin/en.pl @@ -2412,6 +2412,8 @@ 'to warn email bad' =3D> 'To e-mail address is not valid', 'toggle' =3D> 'pause/resume', 'toggle enable disable' =3D> 'Enable or disable', +'token' =3D> 'Token:', +'token not set' =3D> 'No Token has been given.', 'tone' =3D> 'Tone', 'tone dial' =3D> 'Tone dial:', 'too long 80 char max' =3D> ' is too long, maximum allowed is 80 characters', --=20 2.20.1 --===============1066692623536927781==--