Initial commit.
[usenet/INN.git] / perl_mysql
1 #!/usr/bin/perl -w
2 #
3 # INN perl_auth script
4 # Authentication against MySQL Database
5 #
6 # Written by Thomas Hochstein <thh@inter.net>
7 # based on a script written by Sven Weise (sven@futzelnet.de).
8 # Covered under the same license as INN in general.
9 #
10 # user          = Username
11 # password      = Passwort
12 # active        = User active/inactive? (for temp. suspension)
13 #
14 ### database structure
15 # CREATE TABLE IF NOT EXISTS `users` (
16 #   `userid` int(11) NOT NULL auto_increment,
17 #   `user` varchar(16) collate latin1_bin NOT NULL default '',
18 #   `password` varchar(16) collate latin1_bin NOT NULL default '',
19 #   `active` tinyint(1) NOT NULL default '1',
20 #   `username` varchar(60) collate latin1_bin default NULL,
21 #   `usermail` varchar(60) collate latin1_bin default NULL,
22 #   `domain` varchar(40) collate latin1_bin default '<EDITME>',
23 #   `llo` date default NULL,
24 #   PRIMARY KEY  (`userid`),
25 #   UNIQUE KEY `user` (`user`)
26 # );
27 ####################################################################################################################################
28
29
30
31 # use strict;
32 use DBI;
33 use Time::localtime;
34
35 ### DB Vars - EDIT ME!
36 $conf{'dbdriver'} = "mysql";
37 $conf{'dbhost'}   = "localhost";
38 $conf{'database'} = "";
39 $conf{'dbuser'}   = "";
40 $conf{'dbpw'}     = "";
41 $conf{'dbtable'}  = "";
42 $conf{'actcheck'} = 1;
43
44 sub auth_init() {
45  #D open LOG, '>/usr/lib/news/bin/auth/passwd/test.log';
46  
47 };
48
49 sub authenticate() {
50  # $attributes{hostname}  hostname (or the IP address if it doesn't resolve) of the client machine
51  # $attributes{ipaddress} IP address (as a string)
52  # $attributes{port}      client port (as an integer)
53  # $attributes{interface} hostname of the interface the client connected on
54  # $attributes{intipaddr} IP address (as a string) of the interface the client connected on
55  # $attributes{intport}   port (as an integer) on the interface the client connected on
56  # $attributes{username}  username
57  # $attributes{password}  password
58
59  ### DB init
60  my $dbs = sprintf('DBI:%s:database=%s;host=%s',$conf{'dbdriver'},$conf{'database'},$conf{'dbhost'});
61  my $dbhandle = DBI->connect($dbs, $conf{'dbuser'}, $conf{'dbpw'}, { PrintError => 1 });
62
63  ### Query database and disconnect.
64
65  my(@result);
66
67  # quote SQL
68  my $sql_user = $dbhandle->quote($attributes{username});
69  my $sql_pass = $dbhandle->quote($attributes{password});
70
71  my $query = sprintf("SELECT domain FROM %s.%s WHERE user = %s AND password = %s",$conf{'database'},$conf{'dbtable'},$sql_user,$sql_pass);
72  if ($conf{'actcheck'}) {
73    $query .= ' AND active = 1';
74  };
75  my $dbquery = $dbhandle->prepare($query);
76  if ($dbquery->execute()) {
77   @result = $dbquery->fetchrow_array;
78   $dbquery->finish;
79  
80   if (@result) {
81    # log timestamp
82    my $tm = localtime;
83    my $today = sprintf('%04d-%02d-%02d', $tm->year+1900, ($tm->mon)+1, $tm->mday);
84    $query = sprintf("UPDATE %s.%s SET llo = '%s' WHERE user = %s",$conf{'database'},$conf{'dbtable'},$today,$sql_user);
85    $dbquery = $dbhandle->prepare($query);
86    $dbquery->execute();
87    $dbquery->finish;
88   };
89  };
90  $dbhandle->disconnect;
91  ###
92
93  ### check password and respond appropriate
94  if (@result) {
95   my $user = $attributes{username} . '@' . $result[0];
96   return (281, 'Authentication successful: '.$user, $user);
97  } else {
98   return (481, 'Authentication failure');
99  }
100
101  # code execution should never reach this point
102  return (481, 'Authentication failure');
103 };
104 ### EOF ###
This page took 0.013782 seconds and 4 git commands to generate.