]> git.sesse.net Git - freerainbowtables/blob - BOINC software/BOINC server apps/chain_checker_assimilator/chain_checker_assimilator.cpp
initial
[freerainbowtables] / BOINC software / BOINC server apps / chain_checker_assimilator / chain_checker_assimilator.cpp
1 // This file is part of BOINC.
2 // http://boinc.berkeley.edu
3 // Copyright (C) 2008 University of California
4 //
5 // BOINC is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License
7 // as published by the Free Software Foundation,
8 // either version 3 of the License, or (at your option) any later version.
9 //
10 // BOINC is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 // See the GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
17
18 // A sample assimilator that:
19 // 1) if success, copy the output file(s) to a directory
20 // 2) if failure, append a message to an error log
21
22 #include <vector>
23 #include <string>
24 #include <cstdlib>
25 #include <sys/types.h>
26 #include <pwd.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include "lib/util.h"
31 #include "boinc_db.h"
32 #include "error_numbers.h"
33 #include "filesys.h"
34 #include "sched_msgs.h"
35 #include "validate_util.h"
36 #include "sched_config.h"
37 using std::vector;
38 using std::string;
39
40 int write_error(char* p) {
41     static FILE* f = 0;
42     if (!f) {
43         f = fopen("../../results/errors", "a");
44         if (!f) return ERR_FOPEN;
45     }
46     fprintf(f, "%s", p);
47     fflush(f);
48     return 0;
49 }
50
51 int GetNumFilesInDir(char *dir)
52 {
53         DIR *partsdir;
54         struct dirent *part;
55         if((partsdir  = opendir(dir)) == NULL) {
56                 return 0;
57         }
58         int numfiles = 0;
59         while ((part = readdir(partsdir)) != NULL) 
60         {
61                 char *partname = part->d_name;
62                 if(partname != "." && partname != "..")
63                 {
64                         numfiles++;
65                 }
66         }
67         return numfiles;
68
69 }
70 int assimilate_handler(
71     WORKUNIT& wu, vector<RESULT>& /*results*/, RESULT& canonical_result
72 ) {
73     DB_CONN frt;
74     int retval;
75     char buf[1024];
76     char query[1024];
77     unsigned int i;
78     int tableid;
79     MYSQL_RES* resp;
80     MYSQL_ROW row;
81     retval = frt.open("rainbowtables-distrrtgen", config.db_host, config.db_user, config.db_passwd);
82     if (retval) {
83         log_messages.printf(MSG_CRITICAL, "can't open db rainbowtables-distrrtgen\n");
84         exit(1);
85     }
86
87     
88     const char *pos = strchr(wu.name, '_');
89     char partnum[256] = {0};
90     strncpy(partnum, wu.name, pos - wu.name);
91     int lookupid = atoi(partnum);
92     log_messages.printf(MSG_DEBUG, "Handling lookupid %i\n", lookupid);    
93
94     
95     if (wu.canonical_resultid) {
96         sprintf(query, "DELETE FROM rainbowcrack_cracker_verificationqueue WHERE lookupid = %i", lookupid);
97         frt.do_query(query);
98         if(retval)  goto cleanup;
99
100          
101         vector<FILE_INFO> output_files;
102         char copy_path[256];
103         get_output_file_infos(canonical_result, output_files);
104         unsigned int n = output_files.size();
105          if(n < 1)
106          {
107                 log_messages.printf(MSG_CRITICAL, "No files found!\n");
108                 retval = -3;
109                 goto cleanup;
110          }
111         FILE_INFO& fi = output_files[0];
112          string filedata;
113          read_file_string(fi.path.c_str(), filedata);
114          if(strcmp(filedata.c_str(),"0x00") != 0)
115          {
116                 sprintf(query, "UPDATE rainbowcrack_cracker_hashlist SET password = '%s' WHERE lookupid = %i", filedata.c_str(), lookupid);
117                 log_messages.printf(MSG_CRITICAL, "BINGO! Password %s is belonging to %i\n", filedata.c_str(), lookupid);
118                 log_messages.printf(MSG_CRITICAL, "%s\n", query);
119          }
120          else log_messages.printf(MSG_DEBUG, "Ew. Password NOT found in %i (%s)\n", lookupid, filedata.c_str());
121          sprintf(query, "DELETE FROM rainbowcrack_cracker_verificationqueue WHERE lookupid = %i", lookupid);    
122          frt.do_query(query);
123         if(retval)  goto cleanup;        
124          sprintf(query, "UPDATE rainbowcrack_cracker_lookups SET status = 2 WHERE lookupid = %i", lookupid);    
125          frt.do_query(query);
126         if(retval)  goto cleanup;
127
128     } else {
129         sprintf(buf, "%s: 0x%x\n", wu.name, wu.error_mask);
130         return write_error(buf);
131     }
132     retval = 0;
133 cleanup:
134     frt.close();        
135     return retval;
136 }