#!/usr/bin/perl # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # lautre3dyndns.pl ( http://moules.olivierl.org/lautre3dyndns/ ) fournit un # service de DNS dynamique pour un (ou plusieurs) sous-domaine d'un domaine # hébergé par lautre.net. # Le script peut être utilisé en standalone (lancé directement ou via un cron, # par exemple) ou être appelé par ez-ipupdate. # Ligne de commande : # lautre3dyndns.pl [paramètres] # Paramètres : # --configfile=/path/to/configfile : précise un fichier de configuration à # utiliser à la place de /etc/lautre3dyndns.conf # --logfile=/path/to/logfile : précise un fichier de log à utiliser à la # place de /var/log/lautre3dyndns.log # --config="perl code;" : fournit une configuration à utiliser à la place de # celle contenue dans le fichier de configuration # Notes # - La configuration, qu'elle soit contenue dans un fichier de configuration # ou passée en ligne de commande, est du code perl. # - S'il est impossible d'écrire dans le fichier de log, la sortie erreur est # utilisée à la place. # Passage de la configuration en ligne de commande # /!\ Attention, c'est goret ! /!\ # - Format # Pour être exploitable, la configuration passée en ligne de commande doit # respecter ce format : # lautre3dyndns.pl --config='$config::login = "ptramo"; $config::password = "pensomatuer"; %config::domains = ("coincoin.org" => ["panpan"]); $config::netinterface = "ppp0";' # - Intégration à ez-ipupdate # Pour que lautre3dyndns.pl soit appelé à chaque fois que ez-ipupdate met à # jour un domaine (géré par un autre fournisseur de service de type DNS # dynamique), il suffit d'ajouter une ligne de ce type dans son fichier de # configuration (/etc/ez-ipupdate.conf ou /etc/ez-ipupdate/*) : # execute=/path/to/lautre3dyndns.pl --config='$config::login = "ptramo"; $config::password = "pensomatuer"; %config::domains = ("coincoin.org" => ["panpan"]); $config::netinterface = "ppp0";' # Note 1 : Cette façon de faire permet de ne pas utiliser de fichier de # configuration supplémentaire, sans affecter la flexibilité de la # configuration. # Note 2 : Il faut déjà gérer un domaine via un service de DNS dynamique # supporté par ez-ipupdate pour pouvoir profiter de cette méthode. # Changelog # 2003-22-09 1.0rc1 olo # * première version publique use strict; use Getopt::Long; use Fcntl ':flock'; use Time::localtime; use HTTP::Request::Common qw(POST); use LWP::UserAgent; use HTTP::Cookies; # Constantes my $configfile = '/etc/lautre3dyndns.conf'; my $logfile = '/var/log/lautre3dyndns.log'; my $ifconfig = 'LC_ALL="C" /sbin/ifconfig'; my $config; GetOptions("configfile=s" => \$configfile, "logfile=s" => \$logfile, "config=s" => \$config); # Initialisation log (fichier ou stderr) my $log_fh; (open(LOG, ">> $logfile") and flock(LOG, LOCK_EX) and $log_fh = *LOG ) or (print STDERR "unable to open file \"$logfile\" : $!\n" and $log_fh = *STDERR ); # Datation sub two($) { return substr("0$_[0]", -2); } my $tm = &Time::localtime::localtime; print $log_fh "\n".($tm->year+1900)."-".two($tm->mon+1)."-".two($tm->mday)." " .two($tm->hour).":".two($tm->min).":".two($tm->sec)."\n"; # Chargement config local ($config::login, $config::password, %config::domains, $config::netinterface); if (defined($config)) { # la configuration a été fournie en paramètre package config; eval $config; if ($@) { print $log_fh "syntax error in configuration : $@\n"; print $log_fh "configuration was : \"$config\"\n"; die; } } else { # la configuration est contenue dans un fichier package config; do $configfile or (print $log_fh "unable to open config file \"$configfile\" : $!\n" and die) or die; # (devrait jamais arriver) } # Récupération ip my $ip; open(IFCONFIG, "$ifconfig $config::netinterface |"); while () { if (m/inet addr:(\d+\.\d+\.\d+\.\d+)/) { $ip = $1; last; } } close(IFCONFIG); print $log_fh "ip : $ip\n"; # Préparation useragent my $ua = LWP::UserAgent->new(); my $cookies = HTTP::Cookies->new(); $ua->cookie_jar($cookies); my ($req, $resp); # Identification lautre3 (cookie) $req = POST 'http://lautre3.lautre.net/admin/login.php', [username => $config::login, password => $config::password]; $resp = $ua->request($req); $cookies->extract_cookies($resp); if ($resp->is_success()) { $resp->content =~ m/(.*)<\/font>/; print $log_fh "login : " . (defined($1) ? $1 : "OK") . "\n"; } else { print $log_fh "login : " . $resp->code . " - " . $resp->message . "\n"; } # Enregistrement ip my ($dom, $subs, $sub); while (($dom, $subs) = each(%config::domains)) { foreach $sub (@$subs) { $req = POST 'http://lautre3.lautre.net/admin/dom_subdoedit.php', [dom => $dom, sub => $sub, action => "edit", type => "2", # IP redirection sub_ip => $ip]; $resp = $ua->request($req); if ($resp->is_success()) { $resp->content =~ m/(.*)<\/font>/; print $log_fh "$sub . $dom -> $1\n"; } else { print $log_fh "$sub . $dom -> " . $resp->code . " - " . $resp->message . "\n"; } } } # Fermeture log close($log_fh) unless $log_fh eq *STDERR;