Project

General

Profile

Statistics
| Branch: | Revision:

root / mainbox / query.c @ 15928a3d

History | View | Annotate | Download (5.28 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
  curl_formadd(&formpost, &lastptr,
139 75cef49f Tom Mullins
      CURLFORM_COPYNAME, "type",
140
      CURLFORM_COPYCONTENTS, "usage",
141
      CURLFORM_END);
142
143
  timeinfo = localtime(&event->tstart);
144
  strftime(buf, sizeof(buf), "%F %T", timeinfo);
145
  curl_formadd(&formpost, &lastptr,
146
      CURLFORM_COPYNAME, "tstart",
147
      CURLFORM_COPYCONTENTS, buf,
148
      CURLFORM_END);
149
150
  timeinfo = localtime(&event->tend);
151
  strftime(buf, sizeof(buf), "%F %T", timeinfo);
152
  curl_formadd(&formpost, &lastptr,
153
      CURLFORM_COPYNAME, "tend",
154
      CURLFORM_COPYCONTENTS, buf,
155
      CURLFORM_END);
156
157
  sprintf(buf, "%08x", event->user);
158
  curl_formadd(&formpost, &lastptr,
159
      CURLFORM_COPYNAME, "user",
160
      CURLFORM_COPYCONTENTS, buf,
161
      CURLFORM_END);
162
163
  sprintf(buf, "%d", event->tool_id);
164
  curl_formadd(&formpost, &lastptr,
165
      CURLFORM_COPYNAME, "machine",
166
      CURLFORM_COPYCONTENTS, buf,
167
      CURLFORM_END);
168
169
  sprintf(buf, "https://%s/add_event/", server);
170
  error_code = curl_easy_setopt(handle, CURLOPT_URL, buf);
171
  if (error_code) goto error;
172
173 880dc54f Tom Mullins
  error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
174
  if (error_code) goto error;
175
176
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_ignore);
177
  if (error_code) goto error;
178
179 75cef49f Tom Mullins
  error_code = curl_easy_setopt(handle, CURLOPT_HTTPPOST, formpost);
180
  if (error_code) goto error;
181
182
  error_code = curl_easy_perform(handle);
183
  if (error_code) goto error;
184
185
  error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response);
186
  if (error_code) goto error;
187
  if (response >= 400)
188
    fprintf(stderr, "Error %ld from %s\n", response, buf);
189
  else if (response > 200)
190
    fprintf(stderr, "Warning: response %ld from %s\n", response, buf);
191
192
  curl_easy_cleanup(handle);
193 880dc54f Tom Mullins
  curl_formfree(formpost);
194 75cef49f Tom Mullins
  return response >= 300;
195
196
error:
197
  fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
198
  curl_easy_cleanup(handle);
199 880dc54f Tom Mullins
  curl_formfree(formpost);
200 75cef49f Tom Mullins
  return 1;
201
}