Revision cc7646f9
Major changes to mainbox code.
-Added local cache for RFID -> tool permissions
-Queries new page /crm/roboauth/%08x, which gives bitmask of all tool
permissions instead of just one
-Added pid file at /var/run/tooltron.pid
-Now must be run as "tooltron run" and also has "tooltron refresh" and
"tooltron clear" which send signals to the pid in the pid file
-Other things I'm forgetting
mainbox/query.c | ||
---|---|---|
1 | 1 |
#include "query.h" |
2 |
#include "cache.h" |
|
2 | 3 |
#include "event.h" |
3 | 4 |
#include "util.h" |
4 | 5 |
#include "log.h" |
... | ... | |
10 | 11 |
/* Outputs the response to /add_card_event to debug.html */ |
11 | 12 |
//#define DEBUG_EVENT_RESPONSE |
12 | 13 |
|
13 |
const char *server; |
|
14 |
char *tooltron_password; |
|
14 |
/* Size of buffer written to by write_buffer(). We are only reading integers |
|
15 |
* back from the server, so we don't need much of the response and anything |
|
16 |
* else can be ignored */ |
|
17 |
#define WRITE_BUFFER_SIZE 30 |
|
18 |
|
|
19 |
static const char *server; |
|
20 |
static char *tooltron_password; |
|
21 |
static char buffer[WRITE_BUFFER_SIZE]; |
|
22 |
static int buffer_idx; |
|
15 | 23 |
|
16 | 24 |
int query_init(const char *server_name) { |
17 | 25 |
CURLcode error_code; |
... | ... | |
41 | 49 |
free(tooltron_password); |
42 | 50 |
} |
43 | 51 |
|
44 |
static size_t write_bool(void *buffer, size_t size, size_t nmemb, void *userp) { |
|
45 |
int *resultp = userp; |
|
46 |
char *str = buffer; |
|
52 |
static size_t write_buffer(void *buf_in, size_t size, size_t nmemb, void *userp) { |
|
53 |
size_t to_read; |
|
47 | 54 |
|
48 |
if (size*nmemb > 0 && str[0] == '1') |
|
49 |
*resultp = 1; |
|
50 |
else |
|
51 |
*resultp = 0; |
|
55 |
to_read = nmemb; |
|
52 | 56 |
|
53 |
return nmemb; |
|
57 |
if (buffer_idx + to_read*size > WRITE_BUFFER_SIZE) |
|
58 |
to_read = (WRITE_BUFFER_SIZE - buffer_idx)/size; |
|
59 |
|
|
60 |
if (to_read > 0) { |
|
61 |
memcpy(buffer+buffer_idx, buf_in, to_read*size); |
|
62 |
buffer_idx += to_read*size; |
|
63 |
} |
|
64 |
|
|
65 |
return to_read; |
|
54 | 66 |
} |
55 | 67 |
|
56 | 68 |
static size_t write_ignore(void *buffer, size_t size, size_t nmemb, |
... | ... | |
59 | 71 |
} |
60 | 72 |
|
61 | 73 |
/* |
62 |
* query_user_permission
|
|
74 |
* do_q_user_perm
|
|
63 | 75 |
* |
64 |
* Makes an HTTP request to the CRM server to see if user_id has access to
|
|
65 |
* tool_id. Returns 1 if the server replies with '1' or 0 otherwise.
|
|
76 |
* Makes an HTTP request to the CRM server to see what tools user_id has access
|
|
77 |
* to. Returns a bitmask, and returns 0 if there was a problem.
|
|
66 | 78 |
*/ |
67 |
int query_user_permission(int tool_id, unsigned int user_id) {
|
|
79 |
unsigned int do_q_user_perm(unsigned int user_id) {
|
|
68 | 80 |
CURL* handle; |
69 | 81 |
CURLcode error_code; |
70 | 82 |
char url[1024]; |
... | ... | |
75 | 87 |
if (handle == NULL) |
76 | 88 |
return 0; |
77 | 89 |
|
78 |
sprintf(url, "http://%s/crm/roboauth/%08x/%d/", server, user_id, tool_id);
|
|
90 |
sprintf(url, "http://%s/crm/roboauth/%08x/", server, user_id);
|
|
79 | 91 |
error_code = curl_easy_setopt(handle, CURLOPT_URL, url); |
80 | 92 |
if (error_code) goto error; |
81 | 93 |
|
82 |
error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_bool); |
|
94 |
buffer_idx = 0; |
|
95 |
error_code = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_buffer); |
|
83 | 96 |
if (error_code) goto error; |
84 | 97 |
|
85 |
error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, &result);
|
|
98 |
error_code = curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL);
|
|
86 | 99 |
if (error_code) goto error; |
87 | 100 |
|
88 | 101 |
error_code = curl_easy_perform(handle); |
... | ... | |
95 | 108 |
else if (response > 200) |
96 | 109 |
log_print("WARNING: response %ld from %s", response, url); |
97 | 110 |
|
111 |
result = atoi(buffer); |
|
112 |
cache_update(user_id, result); |
|
113 |
|
|
98 | 114 |
curl_easy_cleanup(handle); |
99 | 115 |
return result; |
100 | 116 |
|
101 | 117 |
error: |
102 | 118 |
log_print("ERROR: curl: %s", curl_easy_strerror(error_code)); |
103 |
log_print("ERROR: when authenticating user %08x on tool %d", |
|
104 |
user_id, tool_id); |
|
119 |
log_print("ERROR: when authenticating user %08x", user_id); |
|
105 | 120 |
curl_easy_cleanup(handle); |
106 | 121 |
return 0; |
107 | 122 |
} |
108 | 123 |
|
124 |
void do_refresh(unsigned int key) { |
|
125 |
do_q_user_perm(key); |
|
126 |
} |
|
127 |
|
|
128 |
/* |
|
129 |
* query_refresh_cache |
|
130 |
* |
|
131 |
* Queries the CRM server to update every user permission entry in the cache. |
|
132 |
*/ |
|
133 |
void query_refresh_cache() { |
|
134 |
cache_foreach(do_refresh); |
|
135 |
} |
|
136 |
|
|
137 |
/* |
|
138 |
* query_user_permission |
|
139 |
* |
|
140 |
* Checks whether user_id has permission for tool_id. First checks the cache, |
|
141 |
* then if that fails, calls do_q_user_perm to make an HTTP request to the CRM |
|
142 |
* server. |
|
143 |
*/ |
|
144 |
int query_user_permission(int tool_id, unsigned int user_id) { |
|
145 |
unsigned int result; |
|
146 |
|
|
147 |
if (cache_lookup(user_id, &result)) |
|
148 |
log_print("Serving permissions for %08x from cache", user_id); |
|
149 |
else { |
|
150 |
log_print("Requesting permissions for %08x from server", user_id); |
|
151 |
result = do_q_user_perm(user_id); |
|
152 |
} |
|
153 |
|
|
154 |
return (result >> tool_id) & 1; |
|
155 |
} |
|
156 |
|
|
109 | 157 |
/* |
110 | 158 |
* query_add_event |
111 | 159 |
* |
... | ... | |
125 | 173 |
struct tm *timeinfo; |
126 | 174 |
long response = 0; |
127 | 175 |
|
128 |
return 0; |
|
129 |
|
|
130 | 176 |
#ifdef DEBUG_EVENT_RESPONSE |
131 | 177 |
FILE *fdebug; |
132 | 178 |
fdebug = fopen("debug.html", "w"); |
Also available in: Unified diff