Statistics
| Branch: | Revision:

root / mainbox / query.c @ 5305e5e7

History | View | Annotate | Download (5.94 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 8f961e44 Tom Mullins
  sprintf(url, "http://%s/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 5305e5e7 Tom Mullins
#ifdef DEBUG_EVENT_RESPONSE
128
  FILE *fdebug;
129
  fdebug = fopen("debug.html", "w");
130
#endif
131
132 75cef49f Tom Mullins
  handle = curl_easy_init();
133
  if (handle == NULL)
134
    return 1;
135
136
  curl_formadd(&formpost, &lastptr,
137 15928a3d Tom Mullins
      CURLFORM_COPYNAME, "username",
138
      CURLFORM_COPYCONTENTS, "tooltron",
139
      CURLFORM_END);
140
141
  curl_formadd(&formpost, &lastptr,
142
      CURLFORM_COPYNAME, "password",
143
      CURLFORM_COPYCONTENTS, tooltron_password,
144
      CURLFORM_END);
145
146 75cef49f Tom Mullins
  timeinfo = localtime(&event->tstart);
147
  strftime(buf, sizeof(buf), "%F %T", timeinfo);
148
  curl_formadd(&formpost, &lastptr,
149
      CURLFORM_COPYNAME, "tstart",
150
      CURLFORM_COPYCONTENTS, buf,
151
      CURLFORM_END);
152
153
  timeinfo = localtime(&event->tend);
154
  strftime(buf, sizeof(buf), "%F %T", timeinfo);
155
  curl_formadd(&formpost, &lastptr,
156
      CURLFORM_COPYNAME, "tend",
157
      CURLFORM_COPYCONTENTS, buf,
158
      CURLFORM_END);
159
160
  sprintf(buf, "%08x", event->user);
161
  curl_formadd(&formpost, &lastptr,
162 38df0012 Tom Mullins
      CURLFORM_COPYNAME, "user_id",
163 75cef49f Tom Mullins
      CURLFORM_COPYCONTENTS, buf,
164
      CURLFORM_END);
165
166
  sprintf(buf, "%d", event->tool_id);
167
  curl_formadd(&formpost, &lastptr,
168 38df0012 Tom Mullins
      CURLFORM_COPYNAME, "machine_id",
169 75cef49f Tom Mullins
      CURLFORM_COPYCONTENTS, buf,
170
      CURLFORM_END);
171
172 38df0012 Tom Mullins
  curl_formadd(&formpost, &lastptr,
173
      CURLFORM_COPYNAME, "succ",
174
      CURLFORM_COPYCONTENTS, event->succ? "1" : "0",
175
      CURLFORM_END);
176
177
  sprintf(buf, "https://%s/add_card_event/", server);
178
  //sprintf(buf, "http://%s/add_card_event/", server);
179 75cef49f Tom Mullins
  error_code = curl_easy_setopt(handle, CURLOPT_URL, buf);
180
  if (error_code) goto error;
181
182 5305e5e7 Tom Mullins
  /* TODO disabling host and peer verification should theoretically be removed
183
   * eventually */
184 880dc54f Tom Mullins
  error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
185
  if (error_code) goto error;
186
187 5305e5e7 Tom Mullins
  error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
188 38df0012 Tom Mullins
  if (error_code) goto error;
189
190 5305e5e7 Tom Mullins
#ifdef DEBUG_EVENT_RESPONSE
191
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, fdebug);
192
#else
193 880dc54f Tom Mullins
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_ignore);
194 5305e5e7 Tom Mullins
#endif
195 880dc54f Tom Mullins
  if (error_code) goto error;
196
197 75cef49f Tom Mullins
  error_code = curl_easy_setopt(handle, CURLOPT_HTTPPOST, formpost);
198
  if (error_code) goto error;
199
200
  error_code = curl_easy_perform(handle);
201
  if (error_code) goto error;
202
203
  error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response);
204
  if (error_code) goto error;
205
  if (response >= 400)
206
    fprintf(stderr, "Error %ld from %s\n", response, buf);
207
  else if (response > 200)
208
    fprintf(stderr, "Warning: response %ld from %s\n", response, buf);
209
210
  curl_easy_cleanup(handle);
211 880dc54f Tom Mullins
  curl_formfree(formpost);
212 5305e5e7 Tom Mullins
#ifdef DEBUG_EVENT_RESPONSE
213
  fclose(fdebug);
214
#endif
215 75cef49f Tom Mullins
  return response >= 300;
216
217
error:
218
  fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
219
  curl_easy_cleanup(handle);
220 880dc54f Tom Mullins
  curl_formfree(formpost);
221 5305e5e7 Tom Mullins
#ifdef DEBUG_EVENT_RESPONSE
222
  fclose(fdebug);
223
#endif
224 75cef49f Tom Mullins
  return 1;
225
}