Statistics
| Branch: | Revision:

root / mainbox / query.c @ 880dc54f

History | View | Annotate | Download (4.63 KB)

1
#include "query.h"
2
#include "event.h"
3
#include <stdio.h>
4
#include <curl/curl.h>
5

    
6
const char *server;
7

    
8
int query_init(const char *server_name) {
9
  CURLcode error_code;
10

    
11
  server = server_name;
12

    
13
  error_code = curl_global_init(CURL_GLOBAL_SSL);
14
  if (error_code)
15
    fprintf(stderr, "curl_global_init: %s\n", curl_easy_strerror(error_code));
16

    
17
  return error_code;
18
}
19

    
20
void query_cleanup() {
21
  curl_global_cleanup();
22
}
23

    
24
static size_t write_bool(void *buffer, size_t size, size_t nmemb, void *userp) {
25
  int *resultp = userp;
26
  char *str = buffer;
27

    
28
  if (size*nmemb > 0 && str[0] == '1')
29
    *resultp = 1;
30
  else
31
    *resultp = 0;
32

    
33
  return nmemb;
34
}
35

    
36
static size_t write_ignore(void *buffer, size_t size, size_t nmemb,
37
    void *userp) {
38
  return nmemb;
39
}
40

    
41
/*
42
 * query_user_permission
43
 *
44
 * Makes an HTTP request to the CRM server to see if user_id has access to
45
 * tool_id. Returns 1 if the server replies with '1' or 0 otherwise.
46
 */
47
int query_user_permission(int tool_id, unsigned int user_id) {
48
  CURL* handle;
49
  CURLcode error_code;
50
  char url[1024];
51
  int result = 0;
52
  long response = 0;
53

    
54
  handle = curl_easy_init();
55
  if (handle == NULL)
56
    return 0;
57

    
58
  sprintf(url, "http://%s/roboauth/%08x/%d/", server, user_id, tool_id);
59
  error_code = curl_easy_setopt(handle, CURLOPT_URL, url);
60
  if (error_code) goto error;
61

    
62
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_bool);
63
  if (error_code) goto error;
64

    
65
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, &result);
66
  if (error_code) goto error;
67

    
68
  error_code = curl_easy_perform(handle);
69
  if (error_code) goto error;
70

    
71
  error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response);
72
  if (error_code) goto error;
73
  if (response >= 400)
74
    fprintf(stderr, "Error %ld from %s\n", response, url);
75
  else if (response > 200)
76
    fprintf(stderr, "Warning: response %ld from %s\n", response, url);
77

    
78
  curl_easy_cleanup(handle);
79
  return result;
80

    
81
error:
82
  fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
83
  fprintf(stderr, "      when authenticating user %08x on tool %d\n",
84
      user_id, tool_id);
85
  curl_easy_cleanup(handle);
86
  return 0;
87
}
88

    
89
/*
90
 * query_add_event
91
 *
92
 * Makes an HTTPS POST request to add an event to the CRM server, including
93
 * user, tool, start time, and stop time. Reads the password from password.txt.
94
 * Returns 0 if successful, or 1 if there was an error and the caller should
95
 * try the same event again later.
96
 *
97
 * Times are represented as strftime's "%F %T", which is like "YYYY-MM-DD
98
 * HH:MM:SS" with 24-hour time
99
 */
100
int query_add_event(struct event_t *event) {
101
  CURL* handle;
102
  CURLcode error_code;
103
  struct curl_httppost *formpost = NULL, *lastptr = NULL;
104
  char buf[1024];
105
  struct tm *timeinfo;
106
  long response = 0;
107

    
108
  handle = curl_easy_init();
109
  if (handle == NULL)
110
    return 1;
111

    
112
  curl_formadd(&formpost, &lastptr,
113
      CURLFORM_COPYNAME, "type",
114
      CURLFORM_COPYCONTENTS, "usage",
115
      CURLFORM_END);
116

    
117
  timeinfo = localtime(&event->tstart);
118
  strftime(buf, sizeof(buf), "%F %T", timeinfo);
119
  curl_formadd(&formpost, &lastptr,
120
      CURLFORM_COPYNAME, "tstart",
121
      CURLFORM_COPYCONTENTS, buf,
122
      CURLFORM_END);
123

    
124
  timeinfo = localtime(&event->tend);
125
  strftime(buf, sizeof(buf), "%F %T", timeinfo);
126
  curl_formadd(&formpost, &lastptr,
127
      CURLFORM_COPYNAME, "tend",
128
      CURLFORM_COPYCONTENTS, buf,
129
      CURLFORM_END);
130

    
131
  sprintf(buf, "%08x", event->user);
132
  curl_formadd(&formpost, &lastptr,
133
      CURLFORM_COPYNAME, "user",
134
      CURLFORM_COPYCONTENTS, buf,
135
      CURLFORM_END);
136

    
137
  sprintf(buf, "%d", event->tool_id);
138
  curl_formadd(&formpost, &lastptr,
139
      CURLFORM_COPYNAME, "machine",
140
      CURLFORM_COPYCONTENTS, buf,
141
      CURLFORM_END);
142

    
143
  sprintf(buf, "https://%s/add_event/", server);
144
  error_code = curl_easy_setopt(handle, CURLOPT_URL, buf);
145
  if (error_code) goto error;
146

    
147
  error_code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
148
  if (error_code) goto error;
149

    
150
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_ignore);
151
  if (error_code) goto error;
152

    
153
  error_code = curl_easy_setopt(handle, CURLOPT_HTTPPOST, formpost);
154
  if (error_code) goto error;
155

    
156
  error_code = curl_easy_perform(handle);
157
  if (error_code) goto error;
158

    
159
  error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response);
160
  if (error_code) goto error;
161
  if (response >= 400)
162
    fprintf(stderr, "Error %ld from %s\n", response, buf);
163
  else if (response > 200)
164
    fprintf(stderr, "Warning: response %ld from %s\n", response, buf);
165

    
166
  curl_easy_cleanup(handle);
167
  curl_formfree(formpost);
168
  return response >= 300;
169

    
170
error:
171
  fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
172
  curl_easy_cleanup(handle);
173
  curl_formfree(formpost);
174
  return 1;
175
}