Project

General

Profile

Statistics
| Branch: | Revision:

root / mainbox / query.c @ 8f961e44

History | View | Annotate | Download (1.49 KB)

1 9e764ac9 Tom Mullins
#include "query.h"
2
#include <stdio.h>
3
#include <curl/curl.h>
4
5 8f961e44 Tom Mullins
const char *server;
6
7
int query_init(const char *server_name) {
8 9e764ac9 Tom Mullins
  CURLcode error_code;
9
10 8f961e44 Tom Mullins
  server = server_name;
11
12 9e764ac9 Tom Mullins
  error_code = curl_global_init(CURL_GLOBAL_NOTHING);
13
  if (error_code)
14
    fprintf(stderr, "curl_global_init: %s\n", curl_easy_strerror(error_code));
15
16
  return error_code;
17
}
18
19
void query_cleanup() {
20
  curl_global_cleanup();
21
}
22
23
static size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
24
  int *resultp = userp;
25 8f961e44 Tom Mullins
  char *str = buffer;
26
27
  if (size*nmemb > 0 && str[0] == '1')
28
    *resultp = 1;
29
  else
30
    *resultp = 0;
31 9e764ac9 Tom Mullins
32 8f961e44 Tom Mullins
  return nmemb;
33 9e764ac9 Tom Mullins
}
34
35
int query_user_permission(int tool_id, int user_id) {
36
  CURL* handle;
37
  CURLcode error_code;
38
  char url[1024];
39
  int result = 0;
40
41
  handle = curl_easy_init();
42
  if (handle == NULL)
43
    return 0;
44
45 8f961e44 Tom Mullins
  sprintf(url, "http://%s/roboauth/%08x/%d/", server, user_id, tool_id);
46 9e764ac9 Tom Mullins
  error_code = curl_easy_setopt(handle, CURLOPT_URL, url);
47
  if (error_code) goto error;
48
49
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data);
50
  if (error_code) goto error;
51
52
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, &result);
53
  if (error_code) goto error;
54
55
  error_code = curl_easy_perform(handle);
56
  if (error_code) goto error;
57
58
  curl_easy_cleanup(handle);
59
  return result;
60
61
error:
62
  fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
63 8f961e44 Tom Mullins
  fprintf(stderr, "      when authenticating user %08x on tool %d\n",
64 9e764ac9 Tom Mullins
      user_id, tool_id);
65
  curl_easy_cleanup(handle);
66
  return 0;
67
}