root / mainbox / query.c @ 5e8f735d
History | View | Annotate | Download (5.97 KB)
1 |
#include "query.h" |
---|---|
2 |
#include "event.h" |
3 |
#include "util.h" |
4 |
#include <stdlib.h> |
5 |
#include <stdio.h> |
6 |
#include <string.h> |
7 |
#include <curl/curl.h> |
8 |
|
9 |
/* Outputs the response to /add_card_event to debug.html */
|
10 |
//#define DEBUG_EVENT_RESPONSE
|
11 |
|
12 |
const char *server; |
13 |
char *tooltron_password;
|
14 |
|
15 |
int query_init(const char *server_name) { |
16 |
CURLcode error_code; |
17 |
int len;
|
18 |
|
19 |
server = server_name; |
20 |
|
21 |
error_code = curl_global_init(CURL_GLOBAL_SSL); |
22 |
if (error_code) {
|
23 |
fprintf(stderr, "curl_global_init: %s\n", curl_easy_strerror(error_code));
|
24 |
return error_code;
|
25 |
} |
26 |
|
27 |
tooltron_password = read_file("tooltron_password");
|
28 |
if (!tooltron_password)
|
29 |
return 1; |
30 |
len = strlen(tooltron_password); |
31 |
while (len > 0 && tooltron_password[len-1] == '\n') |
32 |
tooltron_password[--len] = '\0';
|
33 |
|
34 |
return 0; |
35 |
} |
36 |
|
37 |
void query_cleanup() {
|
38 |
curl_global_cleanup(); |
39 |
if (tooltron_password)
|
40 |
free(tooltron_password); |
41 |
} |
42 |
|
43 |
static size_t write_bool(void *buffer, size_t size, size_t nmemb, void *userp) { |
44 |
int *resultp = userp;
|
45 |
char *str = buffer;
|
46 |
|
47 |
if (size*nmemb > 0 && str[0] == '1') |
48 |
*resultp = 1;
|
49 |
else
|
50 |
*resultp = 0;
|
51 |
|
52 |
return nmemb;
|
53 |
} |
54 |
|
55 |
static size_t write_ignore(void *buffer, size_t size, size_t nmemb, |
56 |
void *userp) {
|
57 |
return nmemb;
|
58 |
} |
59 |
|
60 |
/*
|
61 |
* query_user_permission
|
62 |
*
|
63 |
* Makes an HTTP request to the CRM server to see if user_id has access to
|
64 |
* tool_id. Returns 1 if the server replies with '1' or 0 otherwise.
|
65 |
*/
|
66 |
int query_user_permission(int tool_id, unsigned int user_id) { |
67 |
CURL* handle; |
68 |
CURLcode error_code; |
69 |
char url[1024]; |
70 |
int result = 0; |
71 |
long response = 0; |
72 |
|
73 |
handle = curl_easy_init(); |
74 |
if (handle == NULL) |
75 |
return 0; |
76 |
|
77 |
sprintf(url, "http://%s/crm/roboauth/%08x/%d/", server, user_id, tool_id);
|
78 |
error_code = curl_easy_setopt(handle, CURLOPT_URL, url); |
79 |
if (error_code) goto error; |
80 |
|
81 |
error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_bool); |
82 |
if (error_code) goto error; |
83 |
|
84 |
error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, &result); |
85 |
if (error_code) goto error; |
86 |
|
87 |
error_code = curl_easy_perform(handle); |
88 |
if (error_code) goto error; |
89 |
|
90 |
error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response); |
91 |
if (error_code) goto error; |
92 |
if (response >= 400) |
93 |
fprintf(stderr, "Error %ld from %s\n", response, url);
|
94 |
else if (response > 200) |
95 |
fprintf(stderr, "Warning: response %ld from %s\n", response, url);
|
96 |
|
97 |
curl_easy_cleanup(handle); |
98 |
return result;
|
99 |
|
100 |
error:
|
101 |
fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
|
102 |
fprintf(stderr, " when authenticating user %08x on tool %d\n",
|
103 |
user_id, tool_id); |
104 |
curl_easy_cleanup(handle); |
105 |
return 0; |
106 |
} |
107 |
|
108 |
/*
|
109 |
* query_add_event
|
110 |
*
|
111 |
* Makes an HTTPS POST request to add an event to the CRM server, including
|
112 |
* user, tool, start time, and stop time. Reads the password from password.txt.
|
113 |
* Returns 0 if successful, or 1 if there was an error and the caller should
|
114 |
* try the same event again later.
|
115 |
*
|
116 |
* Times are represented as strftime's "%F %T", which is like "YYYY-MM-DD
|
117 |
* HH:MM:SS" with 24-hour time
|
118 |
*/
|
119 |
int query_add_event(struct event_t *event) { |
120 |
CURL* handle; |
121 |
CURLcode error_code; |
122 |
struct curl_httppost *formpost = NULL, *lastptr = NULL; |
123 |
char buf[1024]; |
124 |
struct tm *timeinfo;
|
125 |
long response = 0; |
126 |
|
127 |
return 0; |
128 |
|
129 |
#ifdef DEBUG_EVENT_RESPONSE
|
130 |
FILE *fdebug; |
131 |
fdebug = fopen("debug.html", "w"); |
132 |
#endif
|
133 |
|
134 |
handle = curl_easy_init(); |
135 |
if (handle == NULL) |
136 |
return 1; |
137 |
|
138 |
curl_formadd(&formpost, &lastptr, |
139 |
CURLFORM_COPYNAME, "username",
|
140 |
CURLFORM_COPYCONTENTS, "tooltron",
|
141 |
CURLFORM_END); |
142 |
|
143 |
curl_formadd(&formpost, &lastptr, |
144 |
CURLFORM_COPYNAME, "password",
|
145 |
CURLFORM_COPYCONTENTS, tooltron_password, |
146 |
CURLFORM_END); |
147 |
|
148 |
timeinfo = localtime(&event->tstart); |
149 |
strftime(buf, sizeof(buf), "%F %T", timeinfo); |
150 |
curl_formadd(&formpost, &lastptr, |
151 |
CURLFORM_COPYNAME, "tstart",
|
152 |
CURLFORM_COPYCONTENTS, buf, |
153 |
CURLFORM_END); |
154 |
|
155 |
timeinfo = localtime(&event->tend); |
156 |
strftime(buf, sizeof(buf), "%F %T", timeinfo); |
157 |
curl_formadd(&formpost, &lastptr, |
158 |
CURLFORM_COPYNAME, "tend",
|
159 |
CURLFORM_COPYCONTENTS, buf, |
160 |
CURLFORM_END); |
161 |
|
162 |
sprintf(buf, "%08x", event->user);
|
163 |
curl_formadd(&formpost, &lastptr, |
164 |
CURLFORM_COPYNAME, "user_id",
|
165 |
CURLFORM_COPYCONTENTS, buf, |
166 |
CURLFORM_END); |
167 |
|
168 |
sprintf(buf, "%d", event->tool_id);
|
169 |
curl_formadd(&formpost, &lastptr, |
170 |
CURLFORM_COPYNAME, "machine_id",
|
171 |
CURLFORM_COPYCONTENTS, buf, |
172 |
CURLFORM_END); |
173 |
|
174 |
curl_formadd(&formpost, &lastptr, |
175 |
CURLFORM_COPYNAME, "succ",
|
176 |
CURLFORM_COPYCONTENTS, event->succ? "1" : "0", |
177 |
CURLFORM_END); |
178 |
|
179 |
sprintf(buf, "https://%s/crm/add_card_event/", server);
|
180 |
//sprintf(buf, "http://%s/crm/add_card_event/", server);
|
181 |
error_code = curl_easy_setopt(handle, CURLOPT_URL, buf); |
182 |
if (error_code) goto error; |
183 |
|
184 |
/* TODO disabling host and peer verification should theoretically be removed
|
185 |
* eventually */
|
186 |
error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
|
187 |
if (error_code) goto error; |
188 |
|
189 |
error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
|
190 |
if (error_code) goto error; |
191 |
|
192 |
#ifdef DEBUG_EVENT_RESPONSE
|
193 |
error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, fdebug); |
194 |
#else
|
195 |
error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_ignore); |
196 |
#endif
|
197 |
if (error_code) goto error; |
198 |
|
199 |
error_code = curl_easy_setopt(handle, CURLOPT_HTTPPOST, formpost); |
200 |
if (error_code) goto error; |
201 |
|
202 |
error_code = curl_easy_perform(handle); |
203 |
if (error_code) goto error; |
204 |
|
205 |
error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response); |
206 |
if (error_code) goto error; |
207 |
if (response >= 400) |
208 |
fprintf(stderr, "Error %ld from %s\n", response, buf);
|
209 |
else if (response > 200) |
210 |
fprintf(stderr, "Warning: response %ld from %s\n", response, buf);
|
211 |
|
212 |
curl_easy_cleanup(handle); |
213 |
curl_formfree(formpost); |
214 |
#ifdef DEBUG_EVENT_RESPONSE
|
215 |
fclose(fdebug); |
216 |
#endif
|
217 |
return response >= 300; |
218 |
|
219 |
error:
|
220 |
fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
|
221 |
curl_easy_cleanup(handle); |
222 |
curl_formfree(formpost); |
223 |
#ifdef DEBUG_EVENT_RESPONSE
|
224 |
fclose(fdebug); |
225 |
#endif
|
226 |
return 1; |
227 |
} |