libxdas_records.c

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------------
00002  * Copyright (c) 2006, Novell, Inc.
00003  * All rights reserved.
00004  * 
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions are 
00007  * met:
00008  * 
00009  *     * Redistributions of source code must retain the above copyright 
00010  *       notice, this list of conditions and the following disclaimer.
00011  *     * Redistributions in binary form must reproduce the above copyright 
00012  *       notice, this list of conditions and the following disclaimer in the 
00013  *       documentation and/or other materials provided with the distribution.
00014  *     * Neither the name of the Novell nor the names of its contributors 
00015  *       may be used to endorse or promote products derived from this 
00016  *       software without specific prior written permission.
00017  * 
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00019  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00020  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
00021  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
00022  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
00025  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
00026  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
00027  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   /* HDR: */
00079          + 5   /* <hex_4_character_zero_padded_length_in_bytes>: */
00080          + sizeof(XDAS_RECORD_VERSION)
00081                /* <XDAS record format version>: */
00082          + 9   /* <hex_time_offset>: */
00083          + 9   /* <hex_time_uncertainty_interval>: */
00084          + 9   /* <hex_time_uncertainty_indicator>: */
00085          + strlen(xs->time_source) + 1   
00086                /* <str_time_source>: */
00087          + strlen(xs->time_zone) + 1       
00088                /* <str_time_zone>: */
00089          + 9   /* <hex_event_number>: */
00090          + 9;  /* <hex_outcome>: */
00091 
00092    fmtsz += 4; /* ORG:
00093                 * <org_location_name>:
00094                 * <org_location_address>:
00095                 * <org_service-type>:
00096                 * <org_auth_authority>:
00097                 * <org_principal_name>:
00098                 * <org_principal_id>:
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; /* INT:
00106                 * <int_auth_authority>:
00107                 * <int_domain_specific_name>:
00108                 * <int_domain_specific_id>:
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; /* TGT:
00116                 * <tgt_location_name>:
00117                 * <tgt_location_address>:
00118                 * <tgt_service-type>:
00119                 * <tgt_auth_authority>:
00120                 * <tgt_principal_name>:
00121                 * <tgt_principal_id>:
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  /* SRC: */
00129          + (xr->src_ref? strlen(xr->src_ref): 0) + 1; 
00130                /* <pointer_to_source_domain>: */
00131 
00132    fmtsz +=4   /* EVT: */
00133          + (xr->evt_info? strlen(xr->evt_info): 0) + 1   
00134                /* <event_specific_information>: */
00135          + 4;  /* END\0 */
00136 
00137    /* XDAS records can never be larger than 64K */
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    /* allocate buffer for record string */
00163    if ((fmt = (char *)malloc(fmtsz)) == 0)
00164       return -1;
00165 
00166    /* HDR:
00167     * <hex_4_character_zero_padded_length_in_bytes>:
00168     * <max_16_char_version_str>:
00169     * <hex_time_offset>:
00170     * <hex_time_uncertainty_interval>:
00171     * <hex_time_uncertainty_indicator>:
00172     * <str_time_source>:
00173     * <str_time_zone>:
00174     * <hex_event_number>:
00175     * <hex_outcome>:
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    /* ORG:
00186     * <org_location_name>:
00187     * <org_location_address>:
00188     * <org_service-type>:
00189     * <org_auth_authority>:
00190     * <org_principal_name>:
00191     * <org_principal_id>:
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    /* INT:
00206     * <int_auth_authority>:
00207     * <int_domain_specific_name>:
00208     * <int_domain_specific_id>:
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    /* TGT:
00223     * <tgt_location_name>:
00224     * <tgt_location_address>:
00225     * <tgt_service-type>:
00226     * <tgt_auth_authority>:
00227     * <tgt_principal_name>:
00228     * <tgt_principal_id>:
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    /* SRC:
00243     * <pointer_to_source_domain>:
00244     * EVT:
00245     * <event_specific_information>:
00246     * "END\0"
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    /* put the record length, and replace the colon character */
00254    fmtlen = ccnt;
00255    sprintf(fmt + 4, "%04hx", (unsigned short)ccnt);
00256    fmt[4 + 4] = ':';
00257 
00258    /* is data different? */
00259    if (fmtlen == xr->fmtlen && strcmp(fmt, xr->fmt) == 0)
00260    {
00261       free(fmt);  /* no, free temp buffer, return "no change" */
00262       return 1;
00263    }
00264 
00265    /* yes, free existing, set new */
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;   /* no outcome class matched */
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    /* format record to add in string and length */
00394    if ((rv = xdas_format_record(xs, xr)) < 0)
00395       return (*minorp = OXDAS_MS_OUT_OF_MEMORY), XDAS_S_FAILURE;
00396 
00397    /* if record modified or if we're submitting send request */
00398    if (rv == 0 || !eval)
00399    {
00400       xdas_buffer req;
00401       size_t reqsz = 5 * sizeof(uint32_t) + xr->fmtlen;
00402    
00403       /* resize request buffer */
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       /* put header and data request fields */
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       /* Send request, return status and response buffer. */
00419       xr->last_status = xdas_req_rsp(minorp, xs);
00420    }
00421    return xr->last_status;
00422 }
00423 

Generated on Thu Aug 20 22:33:05 2009 for OpenXDAS by  doxygen 1.5.6