Project

General

Profile

Statistics
| Branch: | Revision:

root / mainbox / query.c @ 38df0012

History | View | Annotate | Download (5.54 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 8f961e44 Tom Mullins
const char *server;
10 15928a3d Tom Mullins
char *tooltron_password;
11 8f961e44 Tom Mullins
12
int query_init(const char *server_name) {
13 9e764ac9 Tom Mullins
  CURLcode error_code;
14 15928a3d Tom Mullins
  int len;
15 9e764ac9 Tom Mullins
16 8f961e44 Tom Mullins
  server = server_name;
17
18 880dc54f Tom Mullins
  error_code = curl_global_init(CURL_GLOBAL_SSL);
19 15928a3d Tom Mullins
  if (error_code) {
20 9e764ac9 Tom Mullins
    fprintf(stderr, "curl_global_init: %s\n", curl_easy_strerror(error_code));
21 15928a3d Tom Mullins
    return error_code;
22
  }
23 9e764ac9 Tom Mullins
24 15928a3d Tom Mullins
  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 9e764ac9 Tom Mullins
}
33
34
void query_cleanup() {
35
  curl_global_cleanup();
36 15928a3d Tom Mullins
  if (tooltron_password)
37
    free(tooltron_password);
38 9e764ac9 Tom Mullins
}
39
40 75cef49f Tom Mullins
static size_t write_bool(void *buffer, size_t size, size_t nmemb, void *userp) {
41 9e764ac9 Tom Mullins
  int *resultp = userp;
42 8f961e44 Tom Mullins
  char *str = buffer;
43
44
  if (size*nmemb > 0 && str[0] == '1')
45
    *resultp = 1;
46
  else
47
    *resultp = 0;
48 9e764ac9 Tom Mullins
49 8f961e44 Tom Mullins
  return nmemb;
50 9e764ac9 Tom Mullins
}
51
52 880dc54f Tom Mullins
static size_t write_ignore(void *buffer, size_t size, size_t nmemb,
53
    void *userp) {
54
  return nmemb;
55
}
56
57 75cef49f Tom Mullins
/*
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 9e764ac9 Tom Mullins
  CURL* handle;
65
  CURLcode error_code;
66
  char url[1024];
67
  int result = 0;
68 cce97007 Tom Mullins
  long response = 0;
69 9e764ac9 Tom Mullins
70
  handle = curl_easy_init();
71
  if (handle == NULL)
72
    return 0;
73
74 8f961e44 Tom Mullins
  sprintf(url, "http://%s/roboauth/%08x/%d/", server, user_id, tool_id);
75 9e764ac9 Tom Mullins
  error_code = curl_easy_setopt(handle, CURLOPT_URL, url);
76
  if (error_code) goto error;
77
78 75cef49f Tom Mullins
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_bool);
79 9e764ac9 Tom Mullins
  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 cce97007 Tom Mullins
  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 9e764ac9 Tom Mullins
  curl_easy_cleanup(handle);
95
  return result;
96
97
error:
98
  fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
99 8f961e44 Tom Mullins
  fprintf(stderr, "      when authenticating user %08x on tool %d\n",
100 9e764ac9 Tom Mullins
      user_id, tool_id);
101
  curl_easy_cleanup(handle);
102
  return 0;
103
}
104 75cef49f Tom Mullins
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 15928a3d Tom Mullins
      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 75cef49f Tom Mullins
  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 38df0012 Tom Mullins
      CURLFORM_COPYNAME, "user_id",
155 75cef49f Tom Mullins
      CURLFORM_COPYCONTENTS, buf,
156
      CURLFORM_END);
157
158
  sprintf(buf, "%d", event->tool_id);
159
  curl_formadd(&formpost, &lastptr,
160 38df0012 Tom Mullins
      CURLFORM_COPYNAME, "machine_id",
161 75cef49f Tom Mullins
      CURLFORM_COPYCONTENTS, buf,
162
      CURLFORM_END);
163
164 38df0012 Tom Mullins
  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 75cef49f Tom Mullins
  error_code = curl_easy_setopt(handle, CURLOPT_URL, buf);
172
  if (error_code) goto error;
173
174 880dc54f Tom Mullins
  error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
175
  if (error_code) goto error;
176
177 38df0012 Tom Mullins
  /* 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 880dc54f Tom Mullins
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_ignore);
184
  if (error_code) goto error;
185 38df0012 Tom Mullins
  */
186 880dc54f Tom Mullins
187 75cef49f Tom Mullins
  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 880dc54f Tom Mullins
  curl_formfree(formpost);
202 38df0012 Tom Mullins
  fclose(tmp);
203 75cef49f Tom Mullins
  return response >= 300;
204
205
error:
206
  fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
207
  curl_easy_cleanup(handle);
208 880dc54f Tom Mullins
  curl_formfree(formpost);
209 75cef49f Tom Mullins
  return 1;
210
}