-
Notifications
You must be signed in to change notification settings - Fork 10
/
hda-usermap
executable file
·91 lines (66 loc) · 1.72 KB
/
hda-usermap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/perl -w
######################################################################
# Author: Carlos Puchol <cpg at amahi dot org>
# This code is property and copyright of Amahi Inc.
# It's licensed under the GNU AGPL v3 License
######################################################################
use strict;
use DBI();
my $DATABASE_NAME = "hda_production";
my $DATABASE_USER = "amahihda";
my $DATABASE_PASSWORD = "AmahiHDARulez";
my $dbh;
sub usage {
printf "usage: hda-usermap [-h] \"User Name\"\n";
printf "\treturns the unix name of the corresponding long or short name entered\n";
exit (0);
}
sub get_db_user_name {
my $user = shift;
my $sth = $dbh->prepare("SELECT login FROM users WHERE name = '$user'");
$sth->execute();
my $login = '';
my @row = ();
if (@row = $sth->fetchrow_array) {
$login = $row[0];
}
$sth->finish();
return $login;
}
sub get_db_user_login {
my $user = shift;
my $sth = $dbh->prepare("SELECT login FROM users WHERE login = '$user'");
$sth->execute();
my $login = '';
my @row = ();
if (@row = $sth->fetchrow_array) {
$login = $row[0];
}
$sth->finish();
return $login;
}
sub get_username {
my $user = shift;
my $login = &get_db_user_name($user);
return $login unless $login eq '';
return &get_db_user_login($user);
}
sub db_connect {
my $password = $DATABASE_PASSWORD;
# connect to the database.
$dbh = DBI->connect("DBI:mysql:database=$DATABASE_NAME;host=localhost",
$DATABASE_USER, $password, {RaiseError => 1})
or &logdie($DBI::errstr);
}
sub db_disconnect {
$dbh->disconnect();
}
sub main {
&usage() unless $#ARGV > -1;
my $user = $ARGV[0];
&db_connect ();
my $u = &get_username($user);
print "$u\n" unless $u eq '';
&db_disconnect ();
}
&main ();