Project

General

Profile

Statistics
| Branch: | Revision:

root / mainbox / query.c @ 5e03b78d

History | View | Annotate | Download (5.97 KB)

1
#include "query.h"
2
#include "event.h"
3
#include "util.h"
4
#include "log.h"
5
#include <stdlib.h>
6
#include <stdio.h>
7
#include <string.h>
8
#include <curl/curl.h>
9

    
10
/* Outputs the response to /add_card_event to debug.html */
11
//#define DEBUG_EVENT_RESPONSE
12

    
13
const char *server;
14
char *tooltron_password;
15

    
16
int query_init(const char *server_name) {
17
  CURLcode error_code;
18
  int len;
19

    
20
  server = server_name;
21

    
22
  error_code = curl_global_init(CURL_GLOBAL_SSL);
23
  if (error_code) {
24
    log_print("ERROR: curl_global_init: %s\n", curl_easy_strerror(error_code));
25
    return error_code;
26
  }
27

    
28
  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
}
37

    
38
void query_cleanup() {
39
  curl_global_cleanup();
40
  if (tooltron_password)
41
    free(tooltron_password);
42
}
43

    
44
static size_t write_bool(void *buffer, size_t size, size_t nmemb, void *userp) {
45
  int *resultp = userp;
46
  char *str = buffer;
47

    
48
  if (size*nmemb > 0 && str[0] == '1')
49
    *resultp = 1;
50
  else
51
    *resultp = 0;
52

    
53
  return nmemb;
54
}
55

    
56
static size_t write_ignore(void *buffer, size_t size, size_t nmemb,
57
    void *userp) {
58
  return nmemb;
59
}
60

    
61
/*
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
  CURL* handle;
69
  CURLcode error_code;
70
  char url[1024];
71
  int result = 0;
72
  long response = 0;
73

    
74
  handle = curl_easy_init();
75
  if (handle == NULL)
76
    return 0;
77

    
78
  sprintf(url, "http://%s/crm/roboauth/%08x/%d/", server, user_id, tool_id);
79
  error_code = curl_easy_setopt(handle, CURLOPT_URL, url);
80
  if (error_code) goto error;
81

    
82
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_bool);
83
  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
  error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response);
92
  if (error_code) goto error;
93
  if (response >= 400)
94
    log_print("ERROR: response %ld from %s", response, url);
95
  else if (response > 200)
96
    log_print("WARNING: response %ld from %s", response, url);
97

    
98
  curl_easy_cleanup(handle);
99
  return result;
100

    
101
error:
102
  log_print("ERROR: curl: %s", curl_easy_strerror(error_code));
103
  log_print("ERROR:       when authenticating user %08x on tool %d",
104
      user_id, tool_id);
105
  curl_easy_cleanup(handle);
106
  return 0;
107
}
108

    
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
  return 0;
129

    
130
#ifdef DEBUG_EVENT_RESPONSE
131
  FILE *fdebug;
132
  fdebug = fopen("debug.html", "w");
133
#endif
134

    
135
  handle = curl_easy_init();
136
  if (handle == NULL)
137
    return 1;
138

    
139
  curl_formadd(&formpost, &lastptr,
140
      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
  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
      CURLFORM_COPYNAME, "user_id",
166
      CURLFORM_COPYCONTENTS, buf,
167
      CURLFORM_END);
168

    
169
  sprintf(buf, "%d", event->tool_id);
170
  curl_formadd(&formpost, &lastptr,
171
      CURLFORM_COPYNAME, "machine_id",
172
      CURLFORM_COPYCONTENTS, buf,
173
      CURLFORM_END);
174

    
175
  curl_formadd(&formpost, &lastptr,
176
      CURLFORM_COPYNAME, "succ",
177
      CURLFORM_COPYCONTENTS, event->succ? "1" : "0",
178
      CURLFORM_END);
179

    
180
  sprintf(buf, "https://%s/crm/add_card_event/", server);
181
  //sprintf(buf, "http://%s/crm/add_card_event/", server);
182
  error_code = curl_easy_setopt(handle, CURLOPT_URL, buf);
183
  if (error_code) goto error;
184

    
185
  /* TODO disabling host and peer verification should theoretically be removed
186
   * eventually */
187
  error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
188
  if (error_code) goto error;
189

    
190
  error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
191
  if (error_code) goto error;
192

    
193
#ifdef DEBUG_EVENT_RESPONSE
194
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, fdebug);
195
#else
196
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_ignore);
197
#endif
198
  if (error_code) goto error;
199

    
200
  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
    log_print("ERROR: response %ld from %s", response, buf);
210
  else if (response > 200)
211
    log_print("WARNING: response %ld from %s", response, buf);
212

    
213
  curl_easy_cleanup(handle);
214
  curl_formfree(formpost);
215
#ifdef DEBUG_EVENT_RESPONSE
216
  fclose(fdebug);
217
#endif
218
  return response >= 300;
219

    
220
error:
221
  log_print("ERROR: curl: %s", curl_easy_strerror(error_code));
222
  curl_easy_cleanup(handle);
223
  curl_formfree(formpost);
224
#ifdef DEBUG_EVENT_RESPONSE
225
  fclose(fdebug);
226
#endif
227
  return 1;
228
}