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