- int request;
-cp
- if(sscanf(c->buffer, "%d", &request) == 1)
- {
- if((request < 0) || (request >= LAST) || (request_handlers[request] == NULL))
- {
- if(debug_lvl >= DEBUG_META)
- syslog(LOG_DEBUG, _("Unknown request from %s (%s): %s"),
- c->name, c->hostname, c->buffer);
- else
- syslog(LOG_ERR, _("Unknown request from %s (%s)"),
- c->name, c->hostname);
-
- return -1;
- }
- else
- {
- if(debug_lvl >= DEBUG_PROTOCOL)
- {
- if(debug_lvl >= DEBUG_META)
- syslog(LOG_DEBUG, _("Got %s from %s (%s): %s"),
- request_name[request], c->name, c->hostname, c->buffer);
- else
- syslog(LOG_DEBUG, _("Got %s from %s (%s)"),
- request_name[request], c->name, c->hostname);
- }
+ int request;
+
+ cp();
+
+ if(sscanf(c->buffer, "%d", &request) == 1) {
+ if((request < 0) || (request >= LAST) || !request_handlers[request]) {
+ if(debug_lvl >= DEBUG_META)
+ syslog(LOG_DEBUG, _("Unknown request from %s (%s): %s"),
+ c->name, c->hostname, c->buffer);
+ else
+ syslog(LOG_ERR, _("Unknown request from %s (%s)"),
+ c->name, c->hostname);
+
+ return -1;
+ } else {
+ if(debug_lvl >= DEBUG_PROTOCOL) {
+ if(debug_lvl >= DEBUG_META)
+ syslog(LOG_DEBUG, _("Got %s from %s (%s): %s"),
+ request_name[request], c->name, c->hostname,
+ c->buffer);
+ else
+ syslog(LOG_DEBUG, _("Got %s from %s (%s)"),
+ request_name[request], c->name, c->hostname);
+ }
+ }
+
+ if((c->allow_request != ALL) && (c->allow_request != request)) {
+ syslog(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name,
+ c->hostname);
+ return -1;
+ }
+
+ if(request_handlers[request] (c))
+ /* Something went wrong. Probably scriptkiddies. Terminate. */
+ {
+ syslog(LOG_ERR, _("Error while processing %s from %s (%s)"),
+ request_name[request], c->name, c->hostname);
+ return -1;
+ }
+ } else {
+ syslog(LOG_ERR, _("Bogus data received from %s (%s)"),
+ c->name, c->hostname);
+ return -1;
+ }
+
+ return 0;
+}
+
+int past_request_compare(past_request_t *a, past_request_t *b)
+{
+ return strcmp(a->request, b->request);
+}
+
+void free_past_request(past_request_t *r)
+{
+ cp();
+
+ if(r->request)
+ free(r->request);
+
+ free(r);
+}
+
+void init_requests(void)
+{
+ cp();
+
+ past_request_tree = avl_alloc_tree((avl_compare_t) past_request_compare, (avl_action_t) free_past_request);
+}
+
+void exit_requests(void)
+{
+ cp();
+
+ avl_delete_tree(past_request_tree);
+}
+
+int seen_request(char *request)
+{
+ past_request_t p, *new;
+
+ cp();
+
+ p.request = request;
+
+ if(avl_search(past_request_tree, &p)) {
+ if(debug_lvl >= DEBUG_SCARY_THINGS)
+ syslog(LOG_DEBUG, _("Already seen request"));
+ return 1;
+ } else {
+ new = (past_request_t *) xmalloc(sizeof(*new));
+ new->request = xstrdup(request);
+ new->firstseen = now;
+ avl_insert(past_request_tree, new);
+ return 0;
+ }
+}
+
+void age_past_requests(void)
+{
+ avl_node_t *node, *next;
+ past_request_t *p;
+ int left = 0, deleted = 0;
+
+ cp();
+
+ for(node = past_request_tree->head; node; node = next) {
+ next = node->next;
+ p = (past_request_t *) node->data;
+
+ if(p->firstseen + pingtimeout < now)
+ avl_delete_node(past_request_tree, node), deleted++;
+ else
+ left++;