00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00039 #include <xdasd.h>
00040
00041 #include "xdasd_log.h"
00042 #include "xdasd_event.h"
00043 #include "xdasd_parse.h"
00044 #include "xdasd_logger.h"
00045 #include "xdasd_trigger.h"
00046 #include "xdasd_filter.h"
00047
00048 #include <stdlib.h>
00049 #include <stddef.h>
00050
00075 static int ev_check(int * minorp, Parsed * parsed, int onlycheck)
00076 {
00080 switch(xdasd_filter_check(parsed, onlycheck))
00081 {
00082 case 1: return XDAS_S_COMPLETE;
00083 case 0: return XDAS_S_NO_AUDIT;
00084 case -1: return (*minorp = OXDAS_MS_OUT_OF_MEMORY), XDAS_S_FAILURE;
00085 case -2: return XDAS_S_NO_DECISION_YET;
00086 }
00087 return (*minorp = OXDAS_MS_UNKNOWN_ERROR), XDAS_S_FAILURE;
00088 }
00089
00105 static int ev_parse(int * minorp, int impflag,
00106 size_t msgsz, const char * msg, Parsed ** parsedp)
00107 {
00108 xdasd_log(10, "ev_parse: [%.*s]\n", msgsz, msg);
00109 switch(xdasd_parse_message(impflag? PMF_IMPORTED: 0, msgsz, msg, parsedp))
00110 {
00111 case 0: return XDAS_S_COMPLETE;
00112 case -1: return (*minorp = OXDAS_MS_OUT_OF_MEMORY), XDAS_S_FAILURE;
00113 case -2: return XDAS_S_INCOMPLETE_RECORD;
00114 }
00115 return (*minorp = OXDAS_MS_UNKNOWN_ERROR), XDAS_S_FAILURE;
00116 }
00117
00127 static int ev_logger_append(Parsed * parsed)
00128 {
00129 if (xdasd_logger_append(parsed) != 0)
00130 return XDAS_S_STORAGE_FAILURE;
00131 return XDAS_S_COMPLETE;
00132 }
00133
00154 static int xdasd_event_check(int * minorp, int impflag,
00155 size_t msgsz, const char * msg)
00156 {
00157 int err;
00158 Parsed * parsed;
00159
00160
00161 if ((err = ev_parse(minorp, impflag, msgsz, msg, &parsed)) != 0)
00162 return err;
00163
00164
00165 err = ev_check(minorp, parsed, 1);
00166 xdasd_parse_free(parsed);
00167 return err;
00168 }
00169
00197 static int xdasd_event_check_and_submit(int * minorp, int impflag,
00198 size_t msgsz, const char * msg)
00199 {
00200 int err;
00201 Parsed * parsed;
00202
00203 if ((err = ev_parse(minorp, impflag, msgsz, msg, &parsed)) != 0)
00204 return err;
00205
00206
00207 if ((err = ev_check(minorp, parsed, 0)) == XDAS_S_COMPLETE
00208 || err == XDAS_S_NO_DECISION_YET)
00209 {
00210
00211
00212
00213
00214
00215 if (parsed->triggers.head != 0)
00216 if (xdasd_trigger_append(parsed) != 0)
00217 xdasd_log(0, "event: Unable to execute script for message:\n[ %s ].\n",
00218 parsed->parsed[0]);
00219
00220 if (parsed->flags & PMF_LOG)
00221 err = ev_logger_append(parsed);
00222 }
00223 xdasd_parse_free(parsed);
00224
00225
00226
00227
00228 if (err == XDAS_S_NO_AUDIT || err == XDAS_S_NO_DECISION_YET)
00229 err = XDAS_S_COMPLETE;
00230
00231 return err;
00232 }
00233
00237
00238
00273 int xdasd_event_process(int * minorp, xdas_buffer req, xdas_buffer * rspp)
00274 {
00275 static int(*handler_table[])(int *, int, size_t, const char *) =
00276 {
00277 xdasd_event_check,
00278 xdasd_event_check_and_submit,
00279 xdasd_event_check_and_submit
00280 };
00281
00282 unsigned reqsfn;
00283 int impflag;
00284 size_t msgsz, reqsz = req->end - req->curpos;
00285
00286 (void)rspp;
00287
00288 if (reqsz < 12 || (reqsfn = xdas_buffer_get_uint32(req))
00289 >= xdas_elemcount(handler_table))
00290 {
00291 const char * errmsg = reqsz < 12?
00292 "request buffer too small": "unknown function requested";
00293 xdasd_log(1, "xdasd_event_process: Protocol error - %s.\n", errmsg);
00294 return (*minorp = OXDAS_MS_PROTOCOL), XDAS_S_FAILURE;
00295 }
00296
00297 impflag = (int)xdas_buffer_get_uint32(req);
00298 msgsz = xdas_buffer_get_uint32(req);
00299
00300 return handler_table[reqsfn](minorp, impflag, msgsz, req->curpos);
00301 }
00302