root / mainbox / query.c @ 5e8f735d
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 | #include <stdlib.h> |
||
5 | 9e764ac9 | Tom Mullins | #include <stdio.h> |
6 | 15928a3d | Tom Mullins | #include <string.h> |
7 | 9e764ac9 | Tom Mullins | #include <curl/curl.h> |
8 | |||
9 | 5305e5e7 | Tom Mullins | /* Outputs the response to /add_card_event to debug.html */
|
10 | //#define DEBUG_EVENT_RESPONSE
|
||
11 | |||
12 | 8f961e44 | Tom Mullins | const char *server; |
13 | 15928a3d | Tom Mullins | char *tooltron_password;
|
14 | 8f961e44 | Tom Mullins | |
15 | int query_init(const char *server_name) { |
||
16 | 9e764ac9 | Tom Mullins | CURLcode error_code; |
17 | 15928a3d | Tom Mullins | int len;
|
18 | 9e764ac9 | Tom Mullins | |
19 | 8f961e44 | Tom Mullins | server = server_name; |
20 | |||
21 | 880dc54f | Tom Mullins | error_code = curl_global_init(CURL_GLOBAL_SSL); |
22 | 15928a3d | Tom Mullins | if (error_code) {
|
23 | 9e764ac9 | Tom Mullins | fprintf(stderr, "curl_global_init: %s\n", curl_easy_strerror(error_code));
|
24 | 15928a3d | Tom Mullins | return error_code;
|
25 | } |
||
26 | 9e764ac9 | Tom Mullins | |
27 | 15928a3d | Tom Mullins | 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 | 9e764ac9 | Tom Mullins | } |
36 | |||
37 | void query_cleanup() {
|
||
38 | curl_global_cleanup(); |
||
39 | 15928a3d | Tom Mullins | if (tooltron_password)
|
40 | free(tooltron_password); |
||
41 | 9e764ac9 | Tom Mullins | } |
42 | |||
43 | 75cef49f | Tom Mullins | static size_t write_bool(void *buffer, size_t size, size_t nmemb, void *userp) { |
44 | 9e764ac9 | Tom Mullins | int *resultp = userp;
|
45 | 8f961e44 | Tom Mullins | char *str = buffer;
|
46 | |||
47 | if (size*nmemb > 0 && str[0] == '1') |
||
48 | *resultp = 1;
|
||
49 | else
|
||
50 | *resultp = 0;
|
||
51 | 9e764ac9 | Tom Mullins | |
52 | 8f961e44 | Tom Mullins | return nmemb;
|
53 | 9e764ac9 | Tom Mullins | } |
54 | |||
55 | 880dc54f | Tom Mullins | static size_t write_ignore(void *buffer, size_t size, size_t nmemb, |
56 | void *userp) {
|
||
57 | return nmemb;
|
||
58 | } |
||
59 | |||
60 | 75cef49f | Tom Mullins | /*
|
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 | 9e764ac9 | Tom Mullins | CURL* handle; |
68 | CURLcode error_code; |
||
69 | char url[1024]; |
||
70 | int result = 0; |
||
71 | cce97007 | Tom Mullins | long response = 0; |
72 | 9e764ac9 | Tom Mullins | |
73 | handle = curl_easy_init(); |
||
74 | if (handle == NULL) |
||
75 | return 0; |
||
76 | |||
77 | 5e8f735d | Tom Mullins | sprintf(url, "http://%s/crm/roboauth/%08x/%d/", server, user_id, tool_id);
|
78 | 9e764ac9 | Tom Mullins | error_code = curl_easy_setopt(handle, CURLOPT_URL, url); |
79 | if (error_code) goto error; |
||
80 | |||
81 | 75cef49f | Tom Mullins | error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_bool); |
82 | 9e764ac9 | Tom Mullins | 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 | cce97007 | Tom Mullins | 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 | 9e764ac9 | Tom Mullins | curl_easy_cleanup(handle); |
98 | return result;
|
||
99 | |||
100 | error:
|
||
101 | fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
|
||
102 | 8f961e44 | Tom Mullins | fprintf(stderr, " when authenticating user %08x on tool %d\n",
|
103 | 9e764ac9 | Tom Mullins | user_id, tool_id); |
104 | curl_easy_cleanup(handle); |
||
105 | return 0; |
||
106 | } |
||
107 | 75cef49f | Tom Mullins | |
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 | 5e8f735d | Tom Mullins | return 0; |
128 | |||
129 | 5305e5e7 | Tom Mullins | #ifdef DEBUG_EVENT_RESPONSE
|
130 | FILE *fdebug; |
||
131 | fdebug = fopen("debug.html", "w"); |
||
132 | #endif
|
||
133 | |||
134 | 75cef49f | Tom Mullins | handle = curl_easy_init(); |
135 | if (handle == NULL) |
||
136 | return 1; |
||
137 | |||
138 | curl_formadd(&formpost, &lastptr, |
||
139 | 15928a3d | Tom Mullins | 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 | 75cef49f | Tom Mullins | 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 | 38df0012 | Tom Mullins | CURLFORM_COPYNAME, "user_id",
|
165 | 75cef49f | Tom Mullins | CURLFORM_COPYCONTENTS, buf, |
166 | CURLFORM_END); |
||
167 | |||
168 | sprintf(buf, "%d", event->tool_id);
|
||
169 | curl_formadd(&formpost, &lastptr, |
||
170 | 38df0012 | Tom Mullins | CURLFORM_COPYNAME, "machine_id",
|
171 | 75cef49f | Tom Mullins | CURLFORM_COPYCONTENTS, buf, |
172 | CURLFORM_END); |
||
173 | |||
174 | 38df0012 | Tom Mullins | curl_formadd(&formpost, &lastptr, |
175 | CURLFORM_COPYNAME, "succ",
|
||
176 | CURLFORM_COPYCONTENTS, event->succ? "1" : "0", |
||
177 | CURLFORM_END); |
||
178 | |||
179 | 5e8f735d | Tom Mullins | sprintf(buf, "https://%s/crm/add_card_event/", server);
|
180 | //sprintf(buf, "http://%s/crm/add_card_event/", server);
|
||
181 | 75cef49f | Tom Mullins | error_code = curl_easy_setopt(handle, CURLOPT_URL, buf); |
182 | if (error_code) goto error; |
||
183 | |||
184 | 5305e5e7 | Tom Mullins | /* TODO disabling host and peer verification should theoretically be removed
|
185 | * eventually */
|
||
186 | 880dc54f | Tom Mullins | error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
|
187 | if (error_code) goto error; |
||
188 | |||
189 | 5305e5e7 | Tom Mullins | error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
|
190 | 38df0012 | Tom Mullins | if (error_code) goto error; |
191 | |||
192 | 5305e5e7 | Tom Mullins | #ifdef DEBUG_EVENT_RESPONSE
|
193 | error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, fdebug); |
||
194 | #else
|
||
195 | 880dc54f | Tom Mullins | error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_ignore); |
196 | 5305e5e7 | Tom Mullins | #endif
|
197 | 880dc54f | Tom Mullins | if (error_code) goto error; |
198 | |||
199 | 75cef49f | Tom Mullins | 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 | 880dc54f | Tom Mullins | curl_formfree(formpost); |
214 | 5305e5e7 | Tom Mullins | #ifdef DEBUG_EVENT_RESPONSE
|
215 | fclose(fdebug); |
||
216 | #endif
|
||
217 | 75cef49f | Tom Mullins | return response >= 300; |
218 | |||
219 | error:
|
||
220 | fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
|
||
221 | curl_easy_cleanup(handle); |
||
222 | 880dc54f | Tom Mullins | curl_formfree(formpost); |
223 | 5305e5e7 | Tom Mullins | #ifdef DEBUG_EVENT_RESPONSE
|
224 | fclose(fdebug); |
||
225 | #endif
|
||
226 | 75cef49f | Tom Mullins | return 1; |
227 | } |