Statistics
| Branch: | Revision:

root / mainbox / query.c @ 5305e5e7

History | View | Annotate | Download (5.94 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/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
#ifdef DEBUG_EVENT_RESPONSE
128
  FILE *fdebug;
129
  fdebug = fopen("debug.html", "w");
130
#endif
131

    
132
  handle = curl_easy_init();
133
  if (handle == NULL)
134
    return 1;
135

    
136
  curl_formadd(&formpost, &lastptr,
137
      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
  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
      CURLFORM_COPYNAME, "user_id",
163
      CURLFORM_COPYCONTENTS, buf,
164
      CURLFORM_END);
165

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

    
172
  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
  error_code = curl_easy_setopt(handle, CURLOPT_URL, buf);
180
  if (error_code) goto error;
181

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

    
187
  error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
188
  if (error_code) goto error;
189

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

    
197
  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
  curl_formfree(formpost);
212
#ifdef DEBUG_EVENT_RESPONSE
213
  fclose(fdebug);
214
#endif
215
  return response >= 300;
216

    
217
error:
218
  fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
219
  curl_easy_cleanup(handle);
220
  curl_formfree(formpost);
221
#ifdef DEBUG_EVENT_RESPONSE
222
  fclose(fdebug);
223
#endif
224
  return 1;
225
}