Statistics
| Branch: | Revision:

root / mainbox / query.c @ 38df0012

History | View | Annotate | Download (5.54 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
const char *server;
10
char *tooltron_password;
11

    
12
int query_init(const char *server_name) {
13
  CURLcode error_code;
14
  int len;
15

    
16
  server = server_name;
17

    
18
  error_code = curl_global_init(CURL_GLOBAL_SSL);
19
  if (error_code) {
20
    fprintf(stderr, "curl_global_init: %s\n", curl_easy_strerror(error_code));
21
    return error_code;
22
  }
23

    
24
  tooltron_password = read_file("tooltron_password");
25
  if (!tooltron_password)
26
    return 1;
27
  len = strlen(tooltron_password);
28
  while (len > 0 && tooltron_password[len-1] == '\n')
29
    tooltron_password[--len] = '\0';
30

    
31
  return 0;
32
}
33

    
34
void query_cleanup() {
35
  curl_global_cleanup();
36
  if (tooltron_password)
37
    free(tooltron_password);
38
}
39

    
40
static size_t write_bool(void *buffer, size_t size, size_t nmemb, void *userp) {
41
  int *resultp = userp;
42
  char *str = buffer;
43

    
44
  if (size*nmemb > 0 && str[0] == '1')
45
    *resultp = 1;
46
  else
47
    *resultp = 0;
48

    
49
  return nmemb;
50
}
51

    
52
static size_t write_ignore(void *buffer, size_t size, size_t nmemb,
53
    void *userp) {
54
  return nmemb;
55
}
56

    
57
/*
58
 * query_user_permission
59
 *
60
 * Makes an HTTP request to the CRM server to see if user_id has access to
61
 * tool_id. Returns 1 if the server replies with '1' or 0 otherwise.
62
 */
63
int query_user_permission(int tool_id, unsigned int user_id) {
64
  CURL* handle;
65
  CURLcode error_code;
66
  char url[1024];
67
  int result = 0;
68
  long response = 0;
69

    
70
  handle = curl_easy_init();
71
  if (handle == NULL)
72
    return 0;
73

    
74
  sprintf(url, "http://%s/roboauth/%08x/%d/", server, user_id, tool_id);
75
  error_code = curl_easy_setopt(handle, CURLOPT_URL, url);
76
  if (error_code) goto error;
77

    
78
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_bool);
79
  if (error_code) goto error;
80

    
81
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, &result);
82
  if (error_code) goto error;
83

    
84
  error_code = curl_easy_perform(handle);
85
  if (error_code) goto error;
86

    
87
  error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response);
88
  if (error_code) goto error;
89
  if (response >= 400)
90
    fprintf(stderr, "Error %ld from %s\n", response, url);
91
  else if (response > 200)
92
    fprintf(stderr, "Warning: response %ld from %s\n", response, url);
93

    
94
  curl_easy_cleanup(handle);
95
  return result;
96

    
97
error:
98
  fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
99
  fprintf(stderr, "      when authenticating user %08x on tool %d\n",
100
      user_id, tool_id);
101
  curl_easy_cleanup(handle);
102
  return 0;
103
}
104

    
105
/*
106
 * query_add_event
107
 *
108
 * Makes an HTTPS POST request to add an event to the CRM server, including
109
 * user, tool, start time, and stop time. Reads the password from password.txt.
110
 * Returns 0 if successful, or 1 if there was an error and the caller should
111
 * try the same event again later.
112
 *
113
 * Times are represented as strftime's "%F %T", which is like "YYYY-MM-DD
114
 * HH:MM:SS" with 24-hour time
115
 */
116
int query_add_event(struct event_t *event) {
117
  CURL* handle;
118
  CURLcode error_code;
119
  struct curl_httppost *formpost = NULL, *lastptr = NULL;
120
  char buf[1024];
121
  struct tm *timeinfo;
122
  long response = 0;
123

    
124
  handle = curl_easy_init();
125
  if (handle == NULL)
126
    return 1;
127

    
128
  curl_formadd(&formpost, &lastptr,
129
      CURLFORM_COPYNAME, "username",
130
      CURLFORM_COPYCONTENTS, "tooltron",
131
      CURLFORM_END);
132

    
133
  curl_formadd(&formpost, &lastptr,
134
      CURLFORM_COPYNAME, "password",
135
      CURLFORM_COPYCONTENTS, tooltron_password,
136
      CURLFORM_END);
137

    
138
  timeinfo = localtime(&event->tstart);
139
  strftime(buf, sizeof(buf), "%F %T", timeinfo);
140
  curl_formadd(&formpost, &lastptr,
141
      CURLFORM_COPYNAME, "tstart",
142
      CURLFORM_COPYCONTENTS, buf,
143
      CURLFORM_END);
144

    
145
  timeinfo = localtime(&event->tend);
146
  strftime(buf, sizeof(buf), "%F %T", timeinfo);
147
  curl_formadd(&formpost, &lastptr,
148
      CURLFORM_COPYNAME, "tend",
149
      CURLFORM_COPYCONTENTS, buf,
150
      CURLFORM_END);
151

    
152
  sprintf(buf, "%08x", event->user);
153
  curl_formadd(&formpost, &lastptr,
154
      CURLFORM_COPYNAME, "user_id",
155
      CURLFORM_COPYCONTENTS, buf,
156
      CURLFORM_END);
157

    
158
  sprintf(buf, "%d", event->tool_id);
159
  curl_formadd(&formpost, &lastptr,
160
      CURLFORM_COPYNAME, "machine_id",
161
      CURLFORM_COPYCONTENTS, buf,
162
      CURLFORM_END);
163

    
164
  curl_formadd(&formpost, &lastptr,
165
      CURLFORM_COPYNAME, "succ",
166
      CURLFORM_COPYCONTENTS, event->succ? "1" : "0",
167
      CURLFORM_END);
168

    
169
  sprintf(buf, "https://%s/add_card_event/", server);
170
  //sprintf(buf, "http://%s/add_card_event/", server);
171
  error_code = curl_easy_setopt(handle, CURLOPT_URL, buf);
172
  if (error_code) goto error;
173

    
174
  error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
175
  if (error_code) goto error;
176

    
177
  /* TODO TEMPORARY */
178
  FILE *tmp = fopen("temp.html", "w");
179
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, tmp);
180
  if (error_code) goto error;
181

    
182
  /*
183
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_ignore);
184
  if (error_code) goto error;
185
  */
186

    
187
  error_code = curl_easy_setopt(handle, CURLOPT_HTTPPOST, formpost);
188
  if (error_code) goto error;
189

    
190
  error_code = curl_easy_perform(handle);
191
  if (error_code) goto error;
192

    
193
  error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response);
194
  if (error_code) goto error;
195
  if (response >= 400)
196
    fprintf(stderr, "Error %ld from %s\n", response, buf);
197
  else if (response > 200)
198
    fprintf(stderr, "Warning: response %ld from %s\n", response, buf);
199

    
200
  curl_easy_cleanup(handle);
201
  curl_formfree(formpost);
202
  fclose(tmp);
203
  return response >= 300;
204

    
205
error:
206
  fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
207
  curl_easy_cleanup(handle);
208
  curl_formfree(formpost);
209
  return 1;
210
}