Statistics
| Branch: | Revision:

root / mainbox / query.c @ 75cef49f

History | View | Annotate | Download (4.27 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_NOTHING);
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
/*
37
 * query_user_permission
38
 *
39
 * Makes an HTTP request to the CRM server to see if user_id has access to
40
 * tool_id. Returns 1 if the server replies with '1' or 0 otherwise.
41
 */
42
int query_user_permission(int tool_id, unsigned int user_id) {
43
  CURL* handle;
44
  CURLcode error_code;
45
  char url[1024];
46
  int result = 0;
47
  long response = 0;
48

    
49
  handle = curl_easy_init();
50
  if (handle == NULL)
51
    return 0;
52

    
53
  sprintf(url, "http://%s/roboauth/%08x/%d/", server, user_id, tool_id);
54
  error_code = curl_easy_setopt(handle, CURLOPT_URL, url);
55
  if (error_code) goto error;
56

    
57
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_bool);
58
  if (error_code) goto error;
59

    
60
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, &result);
61
  if (error_code) goto error;
62

    
63
  error_code = curl_easy_perform(handle);
64
  if (error_code) goto error;
65

    
66
  error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response);
67
  if (error_code) goto error;
68
  if (response >= 400)
69
    fprintf(stderr, "Error %ld from %s\n", response, url);
70
  else if (response > 200)
71
    fprintf(stderr, "Warning: response %ld from %s\n", response, url);
72

    
73
  curl_easy_cleanup(handle);
74
  return result;
75

    
76
error:
77
  fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
78
  fprintf(stderr, "      when authenticating user %08x on tool %d\n",
79
      user_id, tool_id);
80
  curl_easy_cleanup(handle);
81
  return 0;
82
}
83

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

    
103
  handle = curl_easy_init();
104
  if (handle == NULL)
105
    return 1;
106

    
107
  curl_formadd(&formpost, &lastptr,
108
      CURLFORM_COPYNAME, "type",
109
      CURLFORM_COPYCONTENTS, "usage",
110
      CURLFORM_END);
111

    
112
  timeinfo = localtime(&event->tstart);
113
  strftime(buf, sizeof(buf), "%F %T", timeinfo);
114
  curl_formadd(&formpost, &lastptr,
115
      CURLFORM_COPYNAME, "tstart",
116
      CURLFORM_COPYCONTENTS, buf,
117
      CURLFORM_END);
118

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

    
126
  sprintf(buf, "%08x", event->user);
127
  curl_formadd(&formpost, &lastptr,
128
      CURLFORM_COPYNAME, "user",
129
      CURLFORM_COPYCONTENTS, buf,
130
      CURLFORM_END);
131

    
132
  sprintf(buf, "%d", event->tool_id);
133
  curl_formadd(&formpost, &lastptr,
134
      CURLFORM_COPYNAME, "machine",
135
      CURLFORM_COPYCONTENTS, buf,
136
      CURLFORM_END);
137

    
138
  sprintf(buf, "https://%s/add_event/", server);
139
  error_code = curl_easy_setopt(handle, CURLOPT_URL, buf);
140
  if (error_code) goto error;
141

    
142
  error_code = curl_easy_setopt(handle, CURLOPT_HTTPPOST, formpost);
143
  if (error_code) goto error;
144

    
145
  error_code = curl_easy_perform(handle);
146
  if (error_code) goto error;
147

    
148
  error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response);
149
  if (error_code) goto error;
150
  if (response >= 400)
151
    fprintf(stderr, "Error %ld from %s\n", response, buf);
152
  else if (response > 200)
153
    fprintf(stderr, "Warning: response %ld from %s\n", response, buf);
154

    
155
  curl_easy_cleanup(handle);
156
  return response >= 300;
157

    
158
error:
159
  fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
160
  curl_easy_cleanup(handle);
161
  return 1;
162
}