123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- #####################################################
- # dbusers_pgsql.pm
- #
- # These routines handle the users "table" using
- # PostgreSQL.
- #
- # See COPYING for licensing information.
- #
- #####################################################
- # Perl modules
- use strict;
- use POSIX qw(strftime);
- # GnuDIP core database subroutines
- use dbcore;
- #############################################################
- # get multiple users
- #############################################################
- # initialize for getuserseach - all users
- sub getusers {
- return getuserwhere();
- }
- # initialize for getuserseach - by domain
- sub getusersdomain {
- my $domain = shift;
- return getuserwhere(qq*
- where domain = '$domain'
- *);
- }
- # initialize for getuserseach - by age in days
- sub getusersolder {
- my $days = shift;
- my $then = time() - $days * 86400;
- return getuserwhere("
- where
- domain != '' and
- update_secs <= $then
- ");
- }
- # initialize for getuserseach - matching pattern
- sub getuserspattern {
- my $user_pattern = shift;
- my $sortby = shift;
- my $order = shift;
- $sortby = 'username' if !$sortby;
- $order = 'asc' if !$order;
- $order = 'asc' if $order ne 'desc';
- my $where = '';
- if ($user_pattern) {
- # change match characters to the ones PostgreSQL uses
- $user_pattern =~ s/\*/\%/g;
- $user_pattern =~ s/\?/\_/g;
- $where = "where username LIKE \'$user_pattern\'";
- }
- return getuserwhere(qq*
- $where order by $sortby $order
- *);
- }
- # get next user
- sub getuserseach {
- my $sth = shift;
- if (my $uinfo = $sth->fetchrow_hashref) {
- setusrdflt($uinfo);
- return $uinfo;
- }
- $sth->finish;
- return undef;
- }
- #############################################################
- # get user by name and domain
- # this retrieval must be case insensitive!!
- #############################################################
- sub getuser {
- my $username = shift;
- my $domain = shift;
- my $sth = getuserwhere(qq*
- where
- username ILIKE '$username' and domain ILIKE '$domain'
- *);
- my $uinfo = $sth->fetchrow_hashref;
- $sth->finish;
- setusrdflt($uinfo) if $uinfo;
- return $uinfo;
- }
- #############################################################
- # get user by id
- #############################################################
- sub getuserbyid {
- my $id = shift;
- my $sth = getuserwhere(qq*
- where
- id = '$id'
- *);
- my $uinfo = $sth->fetchrow_hashref;
- $sth->finish;
- setusrdflt($uinfo) if $uinfo;
- return $uinfo;
- }
- #############################################################
- # create user
- #############################################################
- sub createuser {
- my $username = shift;
- my $domain = shift;
- my $password = shift;
- my $level = shift;
- my $email = shift;
- # already exists?
- if (getuser($username, $domain)) {
- writelog(
- "createuser: user $username in domain $domain already exists\n");
- dberror();
- }
- my %user;
- my $uinfo = \%user;
- $$uinfo{'username'} = $username;
- $$uinfo{'domain'} = $domain;
- $$uinfo{'password'} = $password;
- $$uinfo{'level'} = $level;
- $$uinfo{'email'} = $email;
- loaduser($uinfo);
- # get back from database with "id"
- return getuser($username, $domain);
- }
- #############################################################
- # get user(s) where ...
- #############################################################
- sub getuserwhere {
- my $where = shift;
- $where = '' if !defined $where;
- my $sth = dbexecute(qq*
- select
- id, username, domain, password, email, forwardurl,
- updated_secs, updated, level, currentip, autourlon,
- MXvalue, MXbackup, wildcard, allowwild, allowmx
- from users $where
- *);
- return $sth;
- }
- #############################################################
- # load user
- #############################################################
- sub loaduser {
- my $uinfo = shift;
- return ''
- if getuser($$uinfo{'username'}, $$uinfo{'domain'});
- # ensure fields all initialised
- setusrdflt($uinfo);
- my $sth = dbexecute(qq*
- insert into users (
- username,
- domain,
- password,
- email,
- forwardurl,
- updated_secs,
- updated,
- level,
- currentip,
- autourlon,
- MXvalue,
- MXbackup,
- wildcard,
- allowwild,
- allowmx
- ) values (
- '$$uinfo{'username'}',
- '$$uinfo{'domain'}',
- '$$uinfo{'password'}',
- '$$uinfo{'email'}',
- '$$uinfo{'forwardurl'}',
- $$uinfo{'updated_secs'},
- '$$uinfo{'updated'}',
- '$$uinfo{'level'}',
- '$$uinfo{'currentip'}',
- '$$uinfo{'autourlon'}',
- '$$uinfo{'MXvalue'}',
- '$$uinfo{'MXbackup'}',
- '$$uinfo{'wildcard'}',
- '$$uinfo{'allowwild'}',
- '$$uinfo{'allowmx'}'
- )
- *);
- $sth->finish;
- return 1;
- }
- #############################################################
- # update user
- #############################################################
- sub updateuser {
- my $uinfo = shift;
- $$uinfo{'updated_secs'} = time();
- $$uinfo{'updated'} = getdatetime($$uinfo{'updated_secs'});
- my $sth = dbexecute(qq*
- update users set
- username = '$$uinfo{'username'}',
- domain = '$$uinfo{'domain'}',
- password = '$$uinfo{'password'}',
- email = '$$uinfo{'email'}',
- forwardurl = '$$uinfo{'forwardurl'}',
- updated_secs = $$uinfo{'updated_secs'},
- updated = '$$uinfo{'updated'}',
- level = '$$uinfo{'level'}',
- currentip = '$$uinfo{'currentip'}',
- autourlon = '$$uinfo{'autourlon'}',
- MXvalue = '$$uinfo{'MXvalue'}',
- MXbackup = '$$uinfo{'MXbackup'}',
- wildcard = '$$uinfo{'wildcard'}',
- allowwild = '$$uinfo{'allowwild'}',
- allowmx = '$$uinfo{'allowmx'}'
- where
- id = $$uinfo{'id'}
- *);
- $sth->finish;
- }
- #############################################################
- # delete user
- #############################################################
- sub deleteuser {
- my $uinfo = shift;
- my $sth = dbexecute(qq*
- delete from users where id = $$uinfo{'id'}
- *);
- $sth->finish;
- }
- #############################################################
- # ensure all user fields initialised
- #############################################################
- sub setusrdflt {
- my $uinfo = shift;
- $$uinfo{'username'} = '' if ! defined $$uinfo{'username'};
- $$uinfo{'password'} = '' if ! defined $$uinfo{'password'};
- $$uinfo{'domain'} = '' if ! defined $$uinfo{'domain'};
- $$uinfo{'email'} = '' if ! defined $$uinfo{'email'};
- $$uinfo{'forwardurl'} = '' if ! defined $$uinfo{'forwardurl'};
- $$uinfo{'updated_secs'} = time() if ! defined $$uinfo{'updated_secs'};
- $$uinfo{'updated'} = getdatetime($$uinfo{'updated_secs'})
- if ! defined $$uinfo{'updated'};
- $$uinfo{'level'} = 'USER' if ! $$uinfo{'level'};
- $$uinfo{'currentip'} = '0.0.0.0' if ! $$uinfo{'currentip'};
- $$uinfo{'autourlon'} = '' if ! defined $$uinfo{'autourlon'};
- $$uinfo{'MXvalue'} = '' if ! defined $$uinfo{'MXvalue'};
- $$uinfo{'wildcard'} = 'NO' if ! $$uinfo{'wildcard'};
- $$uinfo{'MXbackup'} = 'NO' if ! $$uinfo{'MXbackup'};
- $$uinfo{'allowwild'} = 'NO' if ! $$uinfo{'allowwild'};
- $$uinfo{'allowmx'} = 'NO' if ! $$uinfo{'allowmx'};
- }
- #####################################################
- # convert time in seconds to character format
- #####################################################
- sub getdatetime {
- my $secs = shift;
- return strftime("%Y-%m-%d %H:%M:%S", localtime($secs));
- }
- #####################################################
- # must return 1
- #####################################################
- 1;
|