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 #include "config.h"
19 #include <cstring>
20 #include <string>
21 #include <cstdlib>
22 #include <cassert>
23
24 #include "boinc_db.h"
25 #include "error_numbers.h"
26 #include "parse.h"
27 #include "sched_config.h"
28 #include "str_replace.h"
29 #include "crypt.h"
30
31 #ifdef _USING_FCGI_
32 #include "fcgi_stdio.h"
33 #endif
34
35 #define OUTFILE_MACRO "<OUTFILE_"
36 #define UPLOAD_URL_MACRO "<UPLOAD_URL/>"
37
38 // Add a signature at the end of every <file_info> element,
39 //
add_signatures(char * xml,R_RSA_PRIVATE_KEY & key)40 int add_signatures(char* xml, R_RSA_PRIVATE_KEY& key) {
41 char* p = xml, *q1, *q2, buf[BLOB_SIZE], buf2[BLOB_SIZE];
42 char signature_hex[BLOB_SIZE];
43 char signature_xml[BLOB_SIZE];
44 char signed_xml[1024];
45 int retval, len;
46
47 while (1) {
48 q1 = strstr(p, "<file_info>\n");
49 if (!q1) break;
50 q2 = strstr(q1, "</file_info>");
51 if (!q2) {
52 fprintf(stderr, "add_signatures: malformed XML: %s\n", xml);
53 return ERR_XML_PARSE;
54 }
55
56 q1 += strlen("<file_info>\n");
57 len = q2 - q1;
58 memcpy(buf, q1, len);
59 buf[len] = 0;
60 char name[1024];
61 if (!parse_str(buf, "<name>", name, sizeof(name))) {
62 fprintf(stderr, "add_signatures: missing name: %s", buf);
63 return ERR_XML_PARSE;
64 }
65 double max_nbytes;
66 if (!parse_double(buf, "<max_nbytes>", max_nbytes)) {
67 fprintf(stderr, "add_signatures: missing max_nbytes: %s", buf);
68 return ERR_XML_PARSE;
69 }
70 sprintf(signed_xml, "<name>%s</name><max_nbytes>%.0f</max_nbytes>",
71 name, max_nbytes
72 );
73 retval = generate_signature(signed_xml, signature_hex, key);
74 sprintf(signature_xml,
75 "<xml_signature>\n%s</xml_signature>\n", signature_hex
76 );
77 if (retval) return retval;
78 safe_strcpy(buf2, q2);
79 strcpy(q1, buf);
80 strcat(q1, signature_xml);
81 strcat(q1, buf2);
82 p = q1;
83 }
84 return 0;
85 }
86
87 #if 0 // is this used anywhere??
88
89 // remove file upload signatures from a result XML doc
90 //
91 int remove_signatures(char* xml) {
92 char* p, *q;
93 while (1) {
94 p = strstr(xml, "<xml_signature>");
95 if (!p) break;
96 q = strstr(p, "</xml_signature>");
97 if (!q) {
98 fprintf(stderr, "remove_signatures: invalid XML:\n%s", xml);
99 return ERR_XML_PARSE;
100 }
101 q += strlen("</xml_signature>\n");
102 strcpy(p, q);
103 }
104 return 0;
105 }
106 #endif
107
108 // macro-substitute a result template:
109 // - replace OUTFILE_x with base_filename_x, etc.
110 // - add signatures for file uploads
111 // - strip enclosing <output_template> tags
112 //
113 // This is called only from the transitioner,
114 // to create a new result for a WU
115 //
process_result_template(char * result_template,R_RSA_PRIVATE_KEY & key,char * base_filename,SCHED_CONFIG & config_loc)116 int process_result_template(
117 char* result_template,
118 R_RSA_PRIVATE_KEY& key,
119 char* base_filename,
120 SCHED_CONFIG& config_loc
121 ) {
122 char* p,*q;
123 char temp[BLOB_SIZE], buf[256];
124 int retval;
125
126 while (1) {
127 p = strstr(result_template, OUTFILE_MACRO);
128 if (p) {
129 q = p+strlen(OUTFILE_MACRO);
130 char* endptr = strstr(q, "/>");
131 if (!endptr) return ERR_XML_PARSE;
132 if (strchr(q, '>') != endptr+1) return ERR_XML_PARSE;
133 *endptr = 0;
134 strcpy(buf, q);
135 strcpy(temp, endptr+2);
136 strcpy(p, base_filename);
137 strcat(p, buf);
138 strcat(p, temp);
139 continue;
140 }
141 p = strstr(result_template, UPLOAD_URL_MACRO);
142 if (p) {
143 strcpy(temp, p+strlen(UPLOAD_URL_MACRO));
144 strcpy(p, config_loc.upload_url);
145 strcat(p, temp);
146 continue;
147 }
148 break;
149 }
150 if (!config_loc.dont_generate_upload_certificates) {
151 retval = add_signatures(result_template, key);
152 if (retval) return retval;
153 }
154
155 // strip enclosing <output_template> tags, if any
156 //
157 p = strstr(result_template, "<output_template>");
158 if (p) {
159 safe_strcpy(temp, result_template+strlen("<output_template>"));
160 q = strstr(temp, "</output_template>");
161 if (q) *q = 0;
162 strcpy(result_template, temp);
163 }
164 strip_whitespace(result_template);
165 return 0;
166 }
167
168 const char *BOINC_RCSID_e5e1e879f3 = "$Id$";
169