Changeset 279


Ignore:
Timestamp:
03/21/12 18:04:51 (12 years ago)
Author:
Kris Deugau
Message:

/trunk

Moving login SQL into DNSDB.pm complete. See #1
Checkpoint, moving logging into DNSDB.pm. See #1, #35.

Still requires a bit of commented-old-code cleanup

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r278 r279  
    3535@ISA            = qw(Exporter);
    3636@EXPORT_OK      = qw(
    37         &initGlobals &login
     37        &initGlobals &login &initActionLog
    3838        &initPermissions &getPermissions &changePermissions &comparePermissions
    3939        &changeGroup
     
    5757@EXPORT         = (); # Export nothing by default.
    5858%EXPORT_TAGS    = ( ALL => [qw(
    59                 &initGlobals &login
     59                &initGlobals &login &initActionLog
    6060                &initPermissions &getPermissions &changePermissions &comparePermissions
    6161                &changeGroup
     
    945945  my $pass = shift;
    946946
    947   %userdata = %{$dbh->selectrow_hashref("SELECT user_id,group_id,password,firstname,lastname FROM users WHERE username=?",
    948         undef, ($user) )};
    949   return if !%userdata;
    950 
    951   if ($userdata{password} =~ m|^\$1\$([A-Za-z0-9/.]+)\$|) {
     947  my $userinfo = $dbh->selectrow_hashref("SELECT user_id,group_id,password,firstname,lastname FROM users WHERE username=?",
     948        undef, ($user) );
     949  return if !$userinfo;
     950
     951  if ($userinfo->{password} =~ m|^\$1\$([A-Za-z0-9/.]+)\$|) {
    952952    # native passwords (crypt-md5)
    953     return if $userdata{password} ne unix_md5_crypt($pass,$1);
    954   } elsif ($userdata{password} =~ /^[0-9a-f]{32}$/) {
     953    return if $userinfo->{password} ne unix_md5_crypt($pass,$1);
     954  } elsif ($userinfo->{password} =~ /^[0-9a-f]{32}$/) {
    955955    # VegaDNS import (hex-coded MD5)
    956     return if $userdata{password} ne md5_hex($pass);
     956    return if $userinfo->{password} ne md5_hex($pass);
    957957  } else {
    958958    # plaintext (convenient now and then)
    959     return if $userdata{password} ne $pass;
    960   }
    961 
    962   return %userdata;
     959    return if $userinfo->{password} ne $pass;
     960  }
     961
     962  return $userinfo;
    963963} # end login()
     964
     965
     966## DNSDB::initActionLog()
     967# Set up action logging.  Takes a database handle and user ID
     968# Sets some internal globals and Does The Right Thing to set up a logging channel.
     969# This sets up _log() to spew out log entries to the defined channel without worrying
     970# about having to open a file or a syslog channel
     971##fixme Need to call _initActionLog_blah() for various logging channels, configured
     972# via dnsdb.conf, in $config{log_channel} or something
     973# See https://secure.deepnet.cx/trac/dnsadmin/ticket/21
     974sub initActionLog {
     975  my $dbh = shift;
     976  my $uid = shift;
     977
     978  return if !$uid;
     979
     980  # snag user info for logging.  there's got to be a way to not have to pass this back
     981  # and forth from a caller, but web usage means no persistence we can rely on from
     982  # the server side.
     983  my ($username,$fullname) = $dbh->selectrow_array("SELECT username, firstname || ' ' || lastname".
     984        " FROM users WHERE user_id=?", undef, ($uid));
     985##fixme: errors are unpossible!
     986
     987  $userdata{username} = $username;
     988  $userdata{userid} = $uid;
     989  $userdata{fullname} = $fullname;
     990
     991  # convert to real check once we have other logging channels
     992  # if ($config{log_channel} eq 'sql') {
     993  #   Open Log, Sez Me!
     994  # }
     995
     996} # end initActionLog
    964997
    965998
     
    11781211##fixme:  need better way(s?) to snag userinfo for log entries.  don't want to have
    11791212# to pass around yet *another* constant (already passing $dbh, shouldn't need to)
    1180   my $fullname;
    1181   if (!$args{user_id}) {
    1182     ($args{user_id}, $fullname) = $dbh->selectrow_array("SELECT user_id, firstname || ' ' || lastname FROM users".
    1183         " WHERE username=?", undef, ($args{username}));
    1184   }
    1185   if (!$args{username}) {
    1186     ($args{username}, $fullname) = $dbh->selectrow_array("SELECT username, firstname || ' ' || lastname FROM users".
    1187         " WHERE user_id=?", undef, ($args{user_id}));
    1188   }
    1189   if (!$args{fullname}) {
    1190     ($fullname) = $dbh->selectrow_array("SELECT firstname || ' ' || lastname FROM users".
    1191         " WHERE user_id=?", undef, ($args{user_id}));
    1192   }
    1193 
    1194   $args{name} = $fullname if !$args{name};
     1213#  my $fullname;
     1214#  if (!$args{user_id}) {
     1215#    ($args{user_id}, $fullname) = $dbh->selectrow_array("SELECT user_id, firstname || ' ' || lastname FROM users".
     1216#       " WHERE username=?", undef, ($args{username}));
     1217#  }
     1218#  if (!$args{username}) {
     1219#    ($args{username}, $fullname) = $dbh->selectrow_array("SELECT username, firstname || ' ' || lastname FROM users".
     1220#       " WHERE user_id=?", undef, ($args{user_id}));
     1221#  }
     1222#  if (!$args{fullname}) {
     1223#    ($fullname) = $dbh->selectrow_array("SELECT firstname || ' ' || lastname FROM users".
     1224#       " WHERE user_id=?", undef, ($args{user_id}));
     1225#  }
     1226#
     1227#  $args{name} = $fullname if !$args{name};
    11951228
    11961229##fixme:  farm out the actual logging to different subs for file, syslog, internal, etc based on config
    1197   $dbh->do("INSERT INTO log (domain_id,rdns_id,user_id,group_id,email,name,entry) VALUES (?,?,?,?,?,?,?)",
     1230#  if ($config{log_channel} eq 'sql') {
     1231  $dbh->do("INSERT INTO log (domain_id,rdns_id,group_id,entry,user_id,email,name) VALUES (?,?,?,?,?,?,?)",
    11981232        undef,
    1199         ($args{domain_id},$args{rdns_id},$args{user_id},$args{group_id},$args{username},$args{name},$args{entry}));
     1233        ($args{domain_id}, $args{rdns_id}, $args{group_id}, $args{entry},
     1234                $userdata{userid}, $userdata{username}, $userdata{fullname}) );
     1235#  } elsif ($config{log_channel} eq 'file') {
     1236#  } elsif ($config{log_channel} eq 'syslog') {
     1237#  }
    12001238
    12011239} # end _log
  • trunk/dns.cgi

    r277 r279  
    227227  if ($webvar{action} eq 'login') {
    228228    # Snag ACL/permissions here too
    229     my $sth = $dbh->prepare("SELECT user_id,group_id,password,firstname,lastname FROM users WHERE username=?");
    230     $sth->execute($webvar{username});
    231 
    232     if (my ($uid,$gid,$pass,$fname,$lname) = $sth->fetchrow_array) {
    233       $webvar{password} = '' if !$webvar{password};
    234 
    235       if ($pass =~ m|^\$1\$([A-Za-z0-9/.]+)\$|) {
    236         # native passwords (crypt-md5)
    237         $webvar{loginfailed} = 1 if $pass ne unix_md5_crypt($webvar{password},$1);
    238       } elsif ($pass =~ /^[0-9a-f]{32}$/) {
    239         # VegaDNS import (hex-coded MD5)
    240         $webvar{loginfailed} = 1 if $pass ne md5_hex($webvar{password});
    241       } else {
    242         # plaintext (convenient now and then)
    243         $webvar{loginfailed} = 1 if $pass ne $webvar{password};
    244       }
     229
     230#    my $sth = $dbh->prepare("SELECT user_id,group_id,password,firstname,lastname FROM users WHERE username=?");
     231#    $sth->execute($webvar{username});
     232#
     233#    if (my ($uid,$gid,$pass,$fname,$lname) = $sth->fetchrow_array) {
     234#      $webvar{password} = '' if !$webvar{password};
     235#
     236#      if ($pass =~ m|^\$1\$([A-Za-z0-9/.]+)\$|) {
     237#        # native passwords (crypt-md5)
     238#        $webvar{loginfailed} = 1 if $pass ne unix_md5_crypt($webvar{password},$1);
     239#      } elsif ($pass =~ /^[0-9a-f]{32}$/) {
     240#        # VegaDNS import (hex-coded MD5)
     241#        $webvar{loginfailed} = 1 if $pass ne md5_hex($webvar{password});
     242#      } else {
     243#        # plaintext (convenient now and then)
     244#        $webvar{loginfailed} = 1 if $pass ne $webvar{password};
     245#      }
     246
     247    my $userdata = login($dbh, $webvar{username}, $webvar{password});
     248
     249    if ($userdata) {
    245250
    246251      # set session bits
    247       $session->param('logingroup',$gid);
    248       $session->param('curgroup',$gid);
    249       $session->param('uid',$uid);
    250       $session->param('username',$webvar{username});
    251 
    252       changepage(page => "domlist") if !defined($webvar{loginfailed});
     252      $session->param('logingroup',$userdata->{group_id});
     253      $session->param('curgroup',$userdata->{group_id});
     254      $session->param('uid',$userdata->{user_id});
     255      $session->param('username',$userdata->{username});
     256
     257      changepage(page => "domlist");
    253258
    254259    } else {
     
    298303} # handle global webvar{action}s
    299304
    300 initPermissions($dbh,$session->param('uid'));
     305# Misc Things To Do on most pages
     306initPermissions($dbh, $session->param('uid'));
     307initActionLog($dbh, $session->param('uid'));
    301308
    302309$page->param(sid => $sid) unless $webvar{page} eq 'login';      # no session ID on the login page
Note: See TracChangeset for help on using the changeset viewer.