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 <xdas.h>
00040 #include <xdasd.h>
00041 #include <xdas_buf.h>
00042 #include <xdas_wire.h>
00043
00044 #include "libxdas_impl.h"
00045
00046 #include <stdlib.h>
00047 #include <stdio.h>
00048 #include <time.h>
00049
00050 #ifdef _WIN32
00051 # define snprintf _snprintf
00052 #endif
00053
00073 static size_t xdas_calc_max_record_len(xdas_session * xs, xdas_record * xr)
00074 {
00075 unsigned i;
00076 size_t fmtsz;
00077
00078 fmtsz = 4
00079 + 5
00080 + sizeof(XDAS_RECORD_VERSION)
00081
00082 + 9
00083 + 9
00084 + 9
00085 + strlen(xs->time_source) + 1
00086
00087 + strlen(xs->time_zone) + 1
00088
00089 + 9
00090 + 9;
00091
00092 fmtsz += 4;
00093
00094
00095
00096
00097
00098
00099
00100 for (i = 0; i < xdas_elemcount(xs->org_info) && xs->org_info[i]; i++)
00101 fmtsz += strlen(xs->org_info[i]) + 1;
00102 for (; i< xdas_elemcount(xs->org_info); i++)
00103 fmtsz++;
00104
00105 fmtsz += 4;
00106
00107
00108
00109
00110 for (i = 0; i < xdas_elemcount(xr->int_info) && xr->int_info[i]; i++)
00111 fmtsz += strlen(xr->int_info[i]) + 1;
00112 for (; i< xdas_elemcount(xr->int_info); i++)
00113 fmtsz++;
00114
00115 fmtsz += 4;
00116
00117
00118
00119
00120
00121
00122
00123 for (i = 0; i < xdas_elemcount(xr->tgt_info) && xr->tgt_info[i]; i++)
00124 fmtsz += strlen(xr->tgt_info[i]) + 1;
00125 for (; i< xdas_elemcount(xr->tgt_info); i++)
00126 fmtsz++;
00127
00128 fmtsz += 4
00129 + (xr->src_ref? strlen(xr->src_ref): 0) + 1;
00130
00131
00132 fmtsz +=4
00133 + (xr->evt_info? strlen(xr->evt_info): 0) + 1
00134
00135 + 4;
00136
00137
00138 if (fmtsz > MAX_XDAS_REC_SIZE)
00139 fmtsz = MAX_XDAS_REC_SIZE;
00140
00141 return fmtsz;
00142 }
00143
00155 static int xdas_format_record(xdas_session * xs, xdas_record * xr)
00156 {
00157 char * fmt;
00158 int sret;
00159 unsigned fmtlen, i, ccnt;
00160 size_t fmtsz = xdas_calc_max_record_len(xs, xr);
00161
00162
00163 if ((fmt = (char *)malloc(fmtsz)) == 0)
00164 return -1;
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 sret = snprintf(fmt, fmtsz,
00178 "HDR:0000:" XDAS_RECORD_VERSION ":%x:%x:%x:%s:%s:%x:%x:",
00179 xr->time_offset, xs->time_uncert_int,
00180 xs->time_uncert_ind, xs->time_source, xs->time_zone,
00181 xr->event_number, xr->outcome);
00182 if (sret < 0 || sret > (int)fmtsz) return -1;
00183 ccnt = sret;
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 sret = snprintf(fmt + ccnt, fmtsz - ccnt, "ORG:");
00194 if (sret < 0 || sret > (int)(fmtsz - ccnt)) return -1;
00195 ccnt += sret;
00196 for (i = 0; i < xdas_elemcount(xs->org_info) && xs->org_info[i]; i++)
00197 {
00198 sret = snprintf(fmt + ccnt, fmtsz - ccnt, "%s:", xs->org_info[i]);
00199 if (sret < 0 || sret > (int)(fmtsz - ccnt)) return -1;
00200 ccnt += sret;
00201 }
00202 for (; i < xdas_elemcount(xs->org_info); i++)
00203 fmt[ccnt++] = ':';
00204
00205
00206
00207
00208
00209
00210 sret = snprintf(fmt + ccnt, fmtsz - ccnt, "INT:");
00211 if (sret < 0 || sret > (int)(fmtsz - ccnt)) return -1;
00212 ccnt += sret;
00213 for (i = 0; i < xdas_elemcount(xr->int_info) && xr->int_info[i]; i++)
00214 {
00215 sret = snprintf(fmt + ccnt, fmtsz - ccnt, "%s:", xr->int_info[i]);
00216 if (sret < 0 || sret > (int)(fmtsz - ccnt)) return -1;
00217 ccnt += sret;
00218 }
00219 for (; i < xdas_elemcount(xr->int_info); i++)
00220 fmt[ccnt++] = ':';
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230 sret = snprintf(fmt + ccnt, fmtsz - ccnt, "TGT:");
00231 if (sret < 0 || sret > (int)(fmtsz - ccnt)) return -1;
00232 ccnt += sret;
00233 for (i = 0; i < xdas_elemcount(xr->tgt_info) && xr->tgt_info[i]; i++)
00234 {
00235 sret = snprintf(fmt + ccnt, fmtsz - ccnt, "%s:", xr->tgt_info[i]);
00236 if (sret < 0 || sret > (int)(fmtsz - ccnt)) return -1;
00237 ccnt += sret;
00238 }
00239 for (; i < xdas_elemcount(xr->tgt_info); i++)
00240 fmt[ccnt++] = ':';
00241
00242
00243
00244
00245
00246
00247
00248 sret = snprintf(fmt + ccnt, fmtsz - ccnt, "SRC:%s:EVT:%s:END",
00249 xr->src_ref? xr->src_ref: "", xr->evt_info? xr->evt_info: "");
00250 if (sret < 0 || sret > (int)(fmtsz - ccnt)) return -1;
00251 ccnt += sret;
00252
00253
00254 fmtlen = ccnt;
00255 sprintf(fmt + 4, "%04hx", (unsigned short)ccnt);
00256 fmt[4 + 4] = ':';
00257
00258
00259 if (fmtlen == xr->fmtlen && strcmp(fmt, xr->fmt) == 0)
00260 {
00261 free(fmt);
00262 return 1;
00263 }
00264
00265
00266 free(xr->fmt);
00267 xr->fmt = fmt;
00268 xr->fmtlen = fmtlen;
00269 return 0;
00270 }
00271
00280 xdas_record * xdas_validate_record(xdas_audit_rec_desc_t record_ref)
00281 {
00282 xdas_record * xr = (xdas_record *)record_ref;
00283 return (xr && xr->signature == XDAS_RECORD_SIG)? xr: 0;
00284 }
00285
00294 unsigned xdas_get_next_record_number(void)
00295 {
00296 static unsigned g_recnum = 0;
00297 return ++g_recnum;
00298 }
00299
00312 void xdas_set_record_timestamp(xdas_record * xr)
00313 {
00314 xr->time_offset = (unsigned)time(0);
00315 }
00316
00332 int xdas_is_valid_outcome(unsigned outcome)
00333 {
00334 switch(outcome & 0x000000FF)
00335 {
00336 case XDAS_OUT_SUCCESS: return !(outcome & ~0x00003FFF);
00337 case XDAS_OUT_FAILURE: return !(outcome & ~0x000FFFFF);
00338 case XDAS_OUT_DENIAL : return !(outcome & ~0x000007FF);
00339 }
00340 return 0;
00341 }
00342
00349 void xdas_internal_discard_record(xdas_record * xr)
00350 {
00351 free(xr->int_info[0]);
00352 free(xr->tgt_info[0]);
00353 free(xr->src_ref);
00354 free(xr->evt_info);
00355 free(xr->fmt);
00356 free(xr);
00357 }
00358
00388 int xdas_send_record(int * minorp, xdas_session * xs,
00389 xdas_record * xr, int eval, int import)
00390 {
00391 int rv;
00392
00393
00394 if ((rv = xdas_format_record(xs, xr)) < 0)
00395 return (*minorp = OXDAS_MS_OUT_OF_MEMORY), XDAS_S_FAILURE;
00396
00397
00398 if (rv == 0 || !eval)
00399 {
00400 xdas_buffer req;
00401 size_t reqsz = 5 * sizeof(uint32_t) + xr->fmtlen;
00402
00403
00404 if ((req = xdas_buffer_realloc(xs->req, reqsz)) == 0)
00405 return (*minorp = OXDAS_MS_OUT_OF_MEMORY), XDAS_S_FAILURE;
00406 req->curpos = req->start;
00407 xs->req = req;
00408
00409
00410 xdas_buffer_put_uint32(req, reqsz);
00411 xdas_buffer_put_uint32(req, XDASD_REQ_EVENT);
00412 xdas_buffer_put_uint32(req, eval? XDASD_EVT_SRQ_CHECK: XDASD_EVT_SRQ_SUBMIT);
00413 xdas_buffer_put_uint32(req, import? 1: 0);
00414 xdas_buffer_put_uint32(req, xr->fmtlen);
00415 memcpy(req->curpos, xr->fmt, xr->fmtlen);
00416 req->curpos += xr->fmtlen;
00417
00418
00419 xr->last_status = xdas_req_rsp(minorp, xs);
00420 }
00421 return xr->last_status;
00422 }
00423