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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use Data::Dumper;
$| = 1;
my $SCIRE_CONFIG_FILE = '../etc/scireserver.conf'; #will be /etc/scire.conf when released.
my %conf;
my $conf_file = (defined($conf{config})) ? $conf{config} : $SCIRE_CONFIG_FILE;
read_config_file($conf_file);
Dumper(\%conf);
my $identified = 0; #Global variable to determine if already identified or not.
# Somehow this feels insecure.
sub debug {
my $line = shift;
if ($conf{debug}) {
print STDERR "$line\n";
}
}
#Connect to the Database.
my $connect_string = "DBI:$conf{db_type}:$conf{db_name};host=$conf{db_host}";
debug("Connecting to $connect_string");
#my $dbh = DBI->connect($connect_string, $conf{db_user}, $conf{db_passwd}, { RaiseError => 1 } )
# or die "Could not connect to database: $DBI::errstr";
while(<>) {
chomp( my $line = $_);
debug("DEBUG: line is: $line");
if($line =~ /^QUIT$/) {
print "OK\n";
exit;
}
if ($line =~ /^REGISTER "(.+?)" "(.+)"$/) {
my ($mac,$ip) = ($1, $2);
register_client($mac, $ip);
next; #End switch here. You can go no further.
}
if($line =~ /^IDENTIFY (.+)$/) {
my $fingerprint = $1;
identify_client($fingerprint);
next; #End switch here. You can go no further.
}
unless($identified == 1) {
print "ERROR This client has not yet been authorized. Please identify!\n";
next;
}
if ($line =~ /^GET_JOBS(.*)$/) {
my @existing_jobs = split(/ /,$1) if defined($1);
get_jobs(@existing_jobs);
} elsif ($line =~ /^GET_JOB (.+)$/) {
my $job = $1;
get_job($job);
} elsif ($line =~ /^SET_JOB_STATUS (.+?) "(.+)"$/) {
my ($jobid,$status) = ($1, $2);
set_job_status($jobid,$status);
} else {
print "ERROR This command $line, is unknown. Please try again.\n";
}
}
sub read_config_file {
my $conf_file = shift;
open(FH, "< ${conf_file}") or die("Couldn't open the config file ${conf_file}: $!");
while (<FH>) {
chomp;
next if /^\s*(?:#|$)/;
if(/^\s*(.+?)\s*=\s*(.+?)\s*(?:#.*)?$/) {
unless(defined($conf{lc($1)})) { #Don't overwrite anything specified in cmdline
$conf{lc($1)} = $2;
}
}
}
close(FH) or die("Couldn't close the config file ${conf_file}: $!");
# print "Conf file $conf_file read.\n";
}
#New clients must be registered so they can be given a key to use (perhaps for job file transfers?) for authentication. This must be allowed before identifying.
sub register_client {
my ($mac,$ip) = @_;
#Validate your inputs!
$mac =~ /^[a-zA-Z0-9\:]+$/ or print "ERROR invalid mac $mac!\n";
$ip =~ /^[a-zA-Z0-9\.\:]+$/ or print "ERROR invalid ip $ip!\n";
my ($query, $status_id, $id, $sth);
eval {
$query = 'SELECT statusid FROM client_status WHERE statusname = "Pending"';
debug("DEBUG: Query is $query");
$status_id = "4"; #db.conn.GetRow($query)
#$sth = $dbh->prepare($query);
#my @result = $sth->fetchrow_array();
#$status_id = $result[0];
};
($@) and print "ERROR Could not get status id: $DBI::errstr\n";
eval {
$query = 'LOCK TABLES `gacl_axo_seq` WRITE';
debug("DEBUG: Query is $query");
#execute it
#$dbh->do($query);
$query = 'SELECT id FROM `gacl_axo_seq`';
debug("DEBUG: Query is $query");
$id = "56"; #execute $query
#$sth = $dbh->prepare($query);
#my @result = $sth->fetchrow_array();
#$id = $result[0];
$query = 'UPDATE `gacl_axo_seq` SET id=?';
debug("DEBUG: Query is $query");
#execute with $id
#$sth = $dbh->prepare($query);
#$sth->execute($id);
$query = 'UNLOCK TABLES';
debug("DEBUG: Query is $query");
#$dbh->do($query);
};
($@) and print "ERROR during fetching of id sequence: $DBI::errstr\n";
eval {
$query = 'INSERT INFO `gacl_axo` (id,section_value,value,order_value,name,hidden VALUES (?,"clients",?,1,?,0)';
debug("DEBUG: Query is $query");
#$sth = $dbh->prepare($query);
#$sth->execute($id, $hostname, $hostname);
#execute with $id, $hostname, $hostname
#NOTE: not sure if this query is still valid. may be using id instead of hostname for one of those two now.
$query = 'INSERT INTO clients (clientid,digest,cert,hostname,mac,ip,status) VALUES (?,?,?,?,?,?,?)';
debug("DEBUG: Query is $query");
#execute with $id, client_cert.digest("sha1"),crypto.dump_certificate(crypto.FILETYPE_PEM,client_cert),$hostname,$mac,$ip,$status_id))
#$sth = $dbh->prepare($query);
#$sth->execute($id,$digest,$hostname,$mac,$ip,$status_id);
};
($@) and print "ERROR Could not insert client with $query: $DBI::errstr\n";
print "OK\n";
}
#Identify the client by looking up the fingerprint in the database, and matching it up.
sub identify_client {
my $fingerprint = shift;
#Validate your inputs!
$fingerprint =~ s/"//g; #Clear the quotes.
$fingerprint =~ /^[A-Za-z0-9]+$/ or print "ERROR invalid fingerprint!\n";
my $query = 'SELECT client_status.statusname FROM clients JOIN client_status on (clients.status = client_status.statusid) WHERE clients.digest=?';
debug("DEBUG: Query is $query");
#$sth = $dbh->prepare($query);
#$sth->execute($fingerprint);
$identified = 1;
print "OK\n";
}
sub get_jobs {
my (@existing_jobs) = (@_);
#Validate your inputs!
foreach my $jobid (@existing_jobs) {
unless($jobid =~ /^\d$/) {
print "ERROR Invalid jobid given as input $jobid\n";
return undef;
}
}
my $query = <<'EndOfQuery'
SELECT jobs.jobid, jobs.priority, job_conditions.job_dependency, job_conditions.deploy_time, job_conditions.expiration_time, job_history.statusid
FROM jobs NATURAL JOIN jobs_clients NATURAL JOIN job_conditions NATURAL JOIN job_history
WHERE jobs_clients.clientid = %s
AND jobs.jobid = jobs_clients.jobid
AND (job_conditions.deploy_time < now())
AND (job_conditions.expiration_time > now())
AND job_history.statusid = '%s'
ORDER BY jobs.priority,jobs.created
EndOfQuery
}
sub get_job {
my $job = shift;
#Validate your inputs!
}
sub set_job_status {
my ($jobid,$status) = @_;
#Validate your inputs!
}
|