dbusers_pgsql.pm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #####################################################
  2. # dbusers_pgsql.pm
  3. #
  4. # These routines handle the users "table" using
  5. # PostgreSQL.
  6. #
  7. # See COPYING for licensing information.
  8. #
  9. #####################################################
  10. # Perl modules
  11. use strict;
  12. use POSIX qw(strftime);
  13. # GnuDIP core database subroutines
  14. use dbcore;
  15. #############################################################
  16. # get multiple users
  17. #############################################################
  18. # initialize for getuserseach - all users
  19. sub getusers {
  20. return getuserwhere();
  21. }
  22. # initialize for getuserseach - by domain
  23. sub getusersdomain {
  24. my $domain = shift;
  25. return getuserwhere(qq*
  26. where domain = '$domain'
  27. *);
  28. }
  29. # initialize for getuserseach - by age in days
  30. sub getusersolder {
  31. my $days = shift;
  32. my $then = time() - $days * 86400;
  33. return getuserwhere("
  34. where
  35. domain != '' and
  36. update_secs <= $then
  37. ");
  38. }
  39. # initialize for getuserseach - matching pattern
  40. sub getuserspattern {
  41. my $user_pattern = shift;
  42. my $sortby = shift;
  43. my $order = shift;
  44. $sortby = 'username' if !$sortby;
  45. $order = 'asc' if !$order;
  46. $order = 'asc' if $order ne 'desc';
  47. my $where = '';
  48. if ($user_pattern) {
  49. # change match characters to the ones PostgreSQL uses
  50. $user_pattern =~ s/\*/\%/g;
  51. $user_pattern =~ s/\?/\_/g;
  52. $where = "where username LIKE \'$user_pattern\'";
  53. }
  54. return getuserwhere(qq*
  55. $where order by $sortby $order
  56. *);
  57. }
  58. # get next user
  59. sub getuserseach {
  60. my $sth = shift;
  61. if (my $uinfo = $sth->fetchrow_hashref) {
  62. setusrdflt($uinfo);
  63. return $uinfo;
  64. }
  65. $sth->finish;
  66. return undef;
  67. }
  68. #############################################################
  69. # get user by name and domain
  70. # this retrieval must be case insensitive!!
  71. #############################################################
  72. sub getuser {
  73. my $username = shift;
  74. my $domain = shift;
  75. my $sth = getuserwhere(qq*
  76. where
  77. username ILIKE '$username' and domain ILIKE '$domain'
  78. *);
  79. my $uinfo = $sth->fetchrow_hashref;
  80. $sth->finish;
  81. setusrdflt($uinfo) if $uinfo;
  82. return $uinfo;
  83. }
  84. #############################################################
  85. # get user by id
  86. #############################################################
  87. sub getuserbyid {
  88. my $id = shift;
  89. my $sth = getuserwhere(qq*
  90. where
  91. id = '$id'
  92. *);
  93. my $uinfo = $sth->fetchrow_hashref;
  94. $sth->finish;
  95. setusrdflt($uinfo) if $uinfo;
  96. return $uinfo;
  97. }
  98. #############################################################
  99. # create user
  100. #############################################################
  101. sub createuser {
  102. my $username = shift;
  103. my $domain = shift;
  104. my $password = shift;
  105. my $level = shift;
  106. my $email = shift;
  107. # already exists?
  108. if (getuser($username, $domain)) {
  109. writelog(
  110. "createuser: user $username in domain $domain already exists\n");
  111. dberror();
  112. }
  113. my %user;
  114. my $uinfo = \%user;
  115. $$uinfo{'username'} = $username;
  116. $$uinfo{'domain'} = $domain;
  117. $$uinfo{'password'} = $password;
  118. $$uinfo{'level'} = $level;
  119. $$uinfo{'email'} = $email;
  120. loaduser($uinfo);
  121. # get back from database with "id"
  122. return getuser($username, $domain);
  123. }
  124. #############################################################
  125. # get user(s) where ...
  126. #############################################################
  127. sub getuserwhere {
  128. my $where = shift;
  129. $where = '' if !defined $where;
  130. my $sth = dbexecute(qq*
  131. select
  132. id, username, domain, password, email, forwardurl,
  133. updated_secs, updated, level, currentip, autourlon,
  134. MXvalue, MXbackup, wildcard, allowwild, allowmx
  135. from users $where
  136. *);
  137. return $sth;
  138. }
  139. #############################################################
  140. # load user
  141. #############################################################
  142. sub loaduser {
  143. my $uinfo = shift;
  144. return ''
  145. if getuser($$uinfo{'username'}, $$uinfo{'domain'});
  146. # ensure fields all initialised
  147. setusrdflt($uinfo);
  148. my $sth = dbexecute(qq*
  149. insert into users (
  150. username,
  151. domain,
  152. password,
  153. email,
  154. forwardurl,
  155. updated_secs,
  156. updated,
  157. level,
  158. currentip,
  159. autourlon,
  160. MXvalue,
  161. MXbackup,
  162. wildcard,
  163. allowwild,
  164. allowmx
  165. ) values (
  166. '$$uinfo{'username'}',
  167. '$$uinfo{'domain'}',
  168. '$$uinfo{'password'}',
  169. '$$uinfo{'email'}',
  170. '$$uinfo{'forwardurl'}',
  171. $$uinfo{'updated_secs'},
  172. '$$uinfo{'updated'}',
  173. '$$uinfo{'level'}',
  174. '$$uinfo{'currentip'}',
  175. '$$uinfo{'autourlon'}',
  176. '$$uinfo{'MXvalue'}',
  177. '$$uinfo{'MXbackup'}',
  178. '$$uinfo{'wildcard'}',
  179. '$$uinfo{'allowwild'}',
  180. '$$uinfo{'allowmx'}'
  181. )
  182. *);
  183. $sth->finish;
  184. return 1;
  185. }
  186. #############################################################
  187. # update user
  188. #############################################################
  189. sub updateuser {
  190. my $uinfo = shift;
  191. $$uinfo{'updated_secs'} = time();
  192. $$uinfo{'updated'} = getdatetime($$uinfo{'updated_secs'});
  193. my $sth = dbexecute(qq*
  194. update users set
  195. username = '$$uinfo{'username'}',
  196. domain = '$$uinfo{'domain'}',
  197. password = '$$uinfo{'password'}',
  198. email = '$$uinfo{'email'}',
  199. forwardurl = '$$uinfo{'forwardurl'}',
  200. updated_secs = $$uinfo{'updated_secs'},
  201. updated = '$$uinfo{'updated'}',
  202. level = '$$uinfo{'level'}',
  203. currentip = '$$uinfo{'currentip'}',
  204. autourlon = '$$uinfo{'autourlon'}',
  205. MXvalue = '$$uinfo{'MXvalue'}',
  206. MXbackup = '$$uinfo{'MXbackup'}',
  207. wildcard = '$$uinfo{'wildcard'}',
  208. allowwild = '$$uinfo{'allowwild'}',
  209. allowmx = '$$uinfo{'allowmx'}'
  210. where
  211. id = $$uinfo{'id'}
  212. *);
  213. $sth->finish;
  214. }
  215. #############################################################
  216. # delete user
  217. #############################################################
  218. sub deleteuser {
  219. my $uinfo = shift;
  220. my $sth = dbexecute(qq*
  221. delete from users where id = $$uinfo{'id'}
  222. *);
  223. $sth->finish;
  224. }
  225. #############################################################
  226. # ensure all user fields initialised
  227. #############################################################
  228. sub setusrdflt {
  229. my $uinfo = shift;
  230. $$uinfo{'username'} = '' if ! defined $$uinfo{'username'};
  231. $$uinfo{'password'} = '' if ! defined $$uinfo{'password'};
  232. $$uinfo{'domain'} = '' if ! defined $$uinfo{'domain'};
  233. $$uinfo{'email'} = '' if ! defined $$uinfo{'email'};
  234. $$uinfo{'forwardurl'} = '' if ! defined $$uinfo{'forwardurl'};
  235. $$uinfo{'updated_secs'} = time() if ! defined $$uinfo{'updated_secs'};
  236. $$uinfo{'updated'} = getdatetime($$uinfo{'updated_secs'})
  237. if ! defined $$uinfo{'updated'};
  238. $$uinfo{'level'} = 'USER' if ! $$uinfo{'level'};
  239. $$uinfo{'currentip'} = '0.0.0.0' if ! $$uinfo{'currentip'};
  240. $$uinfo{'autourlon'} = '' if ! defined $$uinfo{'autourlon'};
  241. $$uinfo{'MXvalue'} = '' if ! defined $$uinfo{'MXvalue'};
  242. $$uinfo{'wildcard'} = 'NO' if ! $$uinfo{'wildcard'};
  243. $$uinfo{'MXbackup'} = 'NO' if ! $$uinfo{'MXbackup'};
  244. $$uinfo{'allowwild'} = 'NO' if ! $$uinfo{'allowwild'};
  245. $$uinfo{'allowmx'} = 'NO' if ! $$uinfo{'allowmx'};
  246. }
  247. #####################################################
  248. # convert time in seconds to character format
  249. #####################################################
  250. sub getdatetime {
  251. my $secs = shift;
  252. return strftime("%Y-%m-%d %H:%M:%S", localtime($secs));
  253. }
  254. #####################################################
  255. # must return 1
  256. #####################################################
  257. 1;