| … | |
… | |
| 53 | or die "Could not connect to database: $DBI::errstr"; |
53 | or die "Could not connect to database: $DBI::errstr"; |
| 54 | |
54 | |
| 55 | while(<>) { |
55 | while(<>) { |
| 56 | my ($command, @args) = parse_command($_); |
56 | my ($command, @args) = parse_command($_); |
| 57 | # chomp( my $line = $_); |
57 | # chomp( my $line = $_); |
| 58 | # debug("DEBUG: line is: $line"); |
58 | debug("DEBUG: line is: $_"); |
| 59 | # SEE http://agaffney.org/mediawiki/index.php/SSH-based_protocol for documentation on the protocol. |
59 | # SEE http://agaffney.org/mediawiki/index.php/SSH-based_protocol for documentation on the protocol. |
| 60 | |
60 | |
| 61 | if($command eq "QUIT") { |
61 | if($command eq "QUIT") { |
| 62 | print "OK\n"; |
62 | print "OK\n"; |
| 63 | exit; |
63 | exit; |
| … | |
… | |
| 95 | } elsif ($command eq "RETURN_JOBFILE") { |
95 | } elsif ($command eq "RETURN_JOBFILE") { |
| 96 | my $jobid = $args[0]; |
96 | my $jobid = $args[0]; |
| 97 | my @filenames = ("$conf{job_dir}/$client_id/result/$jobid.stdout", "$conf{job_dir}/$client_id/result/$jobid.stderr"); |
97 | my @filenames = ("$conf{job_dir}/$client_id/result/$jobid.stdout", "$conf{job_dir}/$client_id/result/$jobid.stderr"); |
| 98 | print "OK " . join(" ", @filenames) . "\n"; |
98 | print "OK " . join(" ", @filenames) . "\n"; |
| 99 | } elsif ($command eq "JOBFILE_SENT") { |
99 | } elsif ($command eq "JOBFILE_SENT") { |
| 100 | my @filenames = @args; |
100 | my @jobfiles = @args; |
| 101 | process_jobfile($_) foreach(@jobfiles); |
101 | process_jobfile($_) foreach(@jobfiles); |
| 102 | print "OK\n" |
102 | print "OK\n" |
| 103 | } else { |
103 | } else { |
| 104 | print "ERROR The command $command is unknown. Please try again.\n"; |
104 | print "ERROR The command $command is unknown. Please try again.\n"; |
| 105 | } |
105 | } |
| … | |
… | |
| 142 | }; |
142 | }; |
| 143 | ($@) and print "ERROR Could not get status id: $DBI::errstr\n"; |
143 | ($@) and print "ERROR Could not get status id: $DBI::errstr\n"; |
| 144 | |
144 | |
| 145 | eval { |
145 | eval { |
| 146 | run_query('LOCK TABLES `gacl_axo_seq` WRITE'); |
146 | run_query('LOCK TABLES `gacl_axo_seq` WRITE'); |
| 147 | # debug("Query is $query"); |
|
|
| 148 | #execute it |
|
|
| 149 | # $dbh->do($query); |
|
|
| 150 | $query = 'SELECT id FROM `gacl_axo_seq`'; |
147 | $query = 'SELECT id FROM `gacl_axo_seq`'; |
| 151 | $sth = run_query($query); |
148 | $sth = run_query($query); |
| 152 | $id = $sth->fetchrow_hashref->{'id'}; |
149 | $id = $sth->fetchrow_hashref->{'id'}; |
| 153 | $id += 1; |
150 | $id += 1; |
| 154 | $query = 'UPDATE `gacl_axo_seq` SET id=?'; |
151 | $query = 'UPDATE `gacl_axo_seq` SET id=?'; |
| 155 | run_query($query,$id); |
152 | run_query($query,$id); |
| 156 | run_query('UNLOCK TABLES'); |
153 | run_query('UNLOCK TABLES'); |
| 157 | # debug("Query is $query"); |
|
|
| 158 | # $dbh->do($query); |
|
|
| 159 | }; |
154 | }; |
| 160 | ($@) and print "ERROR during fetching of id sequence: $DBI::errstr\n"; |
155 | ($@) and print "ERROR during fetching of id sequence: $DBI::errstr\n"; |
| 161 | |
156 | |
| 162 | eval { |
157 | eval { |
| 163 | $query = 'INSERT INTO `gacl_axo` (id,section_value,value,order_value,name,hidden) VALUES (?,"clients",?,"1",?,"0")'; |
158 | $query = 'INSERT INTO `gacl_axo` (id,section_value,value,order_value,name,hidden) VALUES (?,"clients",?,"1",?,"0")'; |
| 164 | run_query($query,$id,$hostname,$hostname); |
159 | run_query($query,$id,$hostname,$hostname); |
| 165 | #NOTE: not sure if this query is still valid. may be using id instead of hostname for one of those two now. |
160 | #NOTE: not sure if this query is still valid. may be using id instead of hostname for one of those two now. |
| 166 | |
161 | |
| 167 | $query = 'INSERT INTO clients (clientid,digest,hostname,mac,ip,status) VALUES (?,?,?,?,?,?)'; |
162 | $query = 'INSERT INTO clients (clientid,digest,hostname,mac,ip,status) VALUES (?,?,?,?,?,?)'; |
| 168 | #execute with $id, client_cert.digest("sha1"),crypto.dump_certificate(crypto.FILETYPE_PEM,client_cert),$hostname,$mac,$ip,$status_id)) |
|
|
| 169 | run_query($query,$id,$digest,$hostname,$mac,$ip,$status_id); |
163 | run_query($query,$id,$digest,$hostname,$mac,$ip,$status_id); |
| 170 | }; |
164 | }; |
| 171 | ($@) and print "ERROR Could not insert client with $query: $DBI::errstr\n"; |
165 | ($@) and print "ERROR Could not insert client with $query: $DBI::errstr\n"; |
| 172 | #FIXME look for "duplicate key" and if found fail and notify admin. |
166 | #FIXME look for "duplicate key" and if found fail and notify admin. |
| 173 | |
167 | |
| … | |
… | |
| 181 | $digest =~ s/"//g; #Clear the quotes. |
175 | $digest =~ s/"//g; #Clear the quotes. |
| 182 | $digest =~ /^[A-Za-z0-9]+$/ or print "ERROR invalid digest!\n"; |
176 | $digest =~ /^[A-Za-z0-9]+$/ or print "ERROR invalid digest!\n"; |
| 183 | |
177 | |
| 184 | my $query = 'SELECT client_status.statusname, clients.clientid FROM clients JOIN client_status on (clients.status = client_status.statusid) WHERE clients.digest=?'; |
178 | my $query = 'SELECT client_status.statusname, clients.clientid FROM clients JOIN client_status on (clients.status = client_status.statusid) WHERE clients.digest=?'; |
| 185 | my $sth = run_query($query,$digest); |
179 | my $sth = run_query($query,$digest); |
| 186 | #debug("Query is $query"); |
|
|
| 187 | #my $sth = $dbh->prepare($query); |
|
|
| 188 | #$sth->execute($digest); |
|
|
| 189 | my $hashref = $sth->fetchrow_hashref(); |
180 | my $hashref = $sth->fetchrow_hashref(); |
| 190 | debug(Dumper($hashref)); |
181 | debug(Dumper($hashref)); |
| 191 | my $status_name = $hashref->{'statusname'}; |
182 | my $status_name = $hashref->{'statusname'}; |
| 192 | $client_id = $hashref->{'clientid'}; |
183 | $client_id = $hashref->{'clientid'}; |
| 193 | if (defined($client_id) and $client_id > 0) { #and ($status_name eq 'Active') { |
184 | if (defined($client_id) and ($client_id > 0) and ($status_name eq 'Active')) { |
| 194 | $identified = 1; |
185 | $identified = 1; |
| 195 | print "OK\n"; |
186 | print "OK\n"; |
| 196 | } else { |
187 | } else { |
| 197 | print "ERROR Client could not be identified. Status was $status_name\n"; |
188 | print "ERROR Client could not be identified. Status was $status_name\n"; |
| 198 | } |
189 | } |
| … | |
… | |
| 214 | ORDER BY jobs.priority,jobs.created |
205 | ORDER BY jobs.priority,jobs.created |
| 215 | EndOfQuery |
206 | EndOfQuery |
| 216 | |
207 | |
| 217 | #FIXME ADD JOB DEPENDENCIES TO THIS QUERY. |
208 | #FIXME ADD JOB DEPENDENCIES TO THIS QUERY. |
| 218 | my $sth = run_query($query,$client_id); |
209 | my $sth = run_query($query,$client_id); |
| 219 | # debug("Query is $query"); |
|
|
| 220 | # my $sth = $dbh->prepare($query); |
|
|
| 221 | # $sth->execute($client_id); |
|
|
| 222 | my $jobs_ref = $sth->fetchall_arrayref(); |
210 | my $jobs_ref = $sth->fetchall_arrayref(); |
| 223 | # Don't ask me...ask the guys in #perl :P |
211 | # Don't ask me...ask the guys in #perl :P |
| 224 | my @jobs = map { @$_ } @$jobs_ref; |
212 | my @jobs = map { @$_ } @$jobs_ref; |
| 225 | return @jobs; |
213 | return @jobs; |
| 226 | } |
214 | } |