Statistics
| Branch: | Revision:

root / mainbox / query.c @ 15928a3d

History | View | Annotate | Download (5.28 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
  curl_formadd(&formpost, &lastptr,
139
      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
  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
  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
  curl_formfree(formpost);
194
  return response >= 300;
195

    
196
error:
197
  fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
198
  curl_easy_cleanup(handle);
199
  curl_formfree(formpost);
200
  return 1;
201
}