Statistics
| Branch: | Revision:

root / mainbox / query.c @ cce97007

History | View | Annotate | Download (1.79 KB)

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

    
5
const char *server;
6

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

    
10
  server = server_name;
11

    
12
  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
  char *str = buffer;
26

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

    
32
  return nmemb;
33
}
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
  long response = 0;
41

    
42
  handle = curl_easy_init();
43
  if (handle == NULL)
44
    return 0;
45

    
46
  sprintf(url, "http://%s/roboauth/%08x/%d/", server, user_id, tool_id);
47
  error_code = curl_easy_setopt(handle, CURLOPT_URL, url);
48
  if (error_code) goto error;
49

    
50
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data);
51
  if (error_code) goto error;
52

    
53
  error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, &result);
54
  if (error_code) goto error;
55

    
56
  error_code = curl_easy_perform(handle);
57
  if (error_code) goto error;
58

    
59
  error_code = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &response);
60
  if (error_code) goto error;
61
  if (response >= 400)
62
    fprintf(stderr, "Error %ld from %s\n", response, url);
63
  else if (response > 200)
64
    fprintf(stderr, "Warning: response %ld from %s\n", response, url);
65

    
66
  curl_easy_cleanup(handle);
67
  return result;
68

    
69
error:
70
  fprintf(stderr, "curl: %s\n", curl_easy_strerror(error_code));
71
  fprintf(stderr, "      when authenticating user %08x on tool %d\n",
72
      user_id, tool_id);
73
  curl_easy_cleanup(handle);
74
  return 0;
75
}