libxdas_sessions.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 <xdasd.h>
00040 
00041 #include "libxdas_impl.h"
00042 
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045 #include <time.h>
00046 
00047 #ifdef _WIN32
00048 # define WIN32_LEAN_AND_MEAN
00049 # include <windows.h>
00050 #endif
00051 
00065 xdas_session * xdas_validate_session(xdas_audit_ref_t das_ref)
00066 {
00067    xdas_session * xs = (xdas_session *)das_ref;
00068    return (xs && xs->signature == XDAS_SESSION_SIG)? xs: 0;
00069 }
00070 
00077 void xdas_set_session_rights(xdas_session * xs)
00078 {
00079    /* for now, give everyone all rights */
00080    xs->security = XDAS_AUDIT_SERVICE | XDAS_AUDIT_SUBMIT 
00081          | XDAS_AUDIT_IMPORT | XDAS_AUDIT_CONTROL | XDAS_AUDIT_READ;
00082 
00083    /* get security level information here 
00084     *    this is platform-specific -
00085     *    we'll need an #ifdef, or an abstraction
00086     *
00087     * store security bits in xs->security
00088     * if (xs->security == 0) return auth failure.
00089     */
00090 }
00091 
00102 int xdas_session_has_rights(xdas_session * xs, unsigned rights)
00103 {
00104    return (xs->security & rights) == rights? 1: 0;
00105 }
00106 
00107 #ifdef _WIN32
00108 
00126 static const char * xdas_abbreviate_win32_tzname(const wchar_t * tzstr, 
00127       char * buf, size_t bufsz)
00128 {
00129    wchar_t outbuf[32];
00130    wchar_t * p = outbuf;
00131 
00132    *p++ = *tzstr++;
00133    while (*tzstr)
00134    {
00135       while (*tzstr && *tzstr != L' ')
00136          tzstr++;
00137       if (*tzstr)
00138          *p++ = *++tzstr;
00139    }
00140    *p = 0;
00141    wcstombs(buf, outbuf, bufsz);
00142    return buf;
00143 }
00144 
00145 #endif
00146 
00159 static void escape_str(char * dst, const char * src)
00160 {
00161    do 
00162    {
00163       if (*src == ':' || *src == '%')
00164          *dst++ = '%';
00165       *dst++ = *src++;
00166    } while (*src);
00167 }
00168 
00190 int xdas_set_time_info(xdas_session * xs)
00191 {
00192    char tztmp[sizeof xs->time_zone] = "";
00193 
00194    /* @todo: Timezone info has properly been escaped now, but still may
00195     *        not contain the correct information or format. See Java impl.
00196     */
00197 
00198 #ifdef _WIN32
00199 
00200    HKEY key;
00201    DWORD tzschema;
00202    TIME_ZONE_INFORMATION tzi;
00203 
00204    /* read registry time server name for time_source field */
00205    *xs->time_source = 0;
00206    if (RegOpenKeyA(HKEY_LOCAL_MACHINE, 
00207          "SYSTEM\\CurrentControlSet\\Services\\w32time\\Parameters", 
00208          &key) == ERROR_SUCCESS) 
00209    {
00210       char * p;
00211       DWORD dwBufSz = (DWORD)sizeof(xs->time_source);
00212       RegQueryValueExA(key, "NtpServer", 0, 0, (LPBYTE)xs->time_source, &dwBufSz);
00213       RegCloseKey(key);
00214       if ((p = strchr(xs->time_source, ',')) != 0)
00215          *p = 0;
00216    }
00217 
00218    /* get time zone information for time_zone field */
00219    tzschema = GetTimeZoneInformation(&tzi);
00220    if (tzschema != TIME_ZONE_ID_INVALID)
00221    {
00222       char stz[32];     /* Bias variables are minutes west of GMT */
00223       LONG bias = tzi.Bias + tzi.StandardBias;
00224       int ccnt = sprintf(tztmp, "%s%ld", 
00225             xdas_abbreviate_win32_tzname(tzi.StandardName, stz, sizeof(stz)), 
00226             bias / 60);
00227       if (bias % 60)
00228          ccnt += sprintf(tztmp + ccnt, "%%:%ld", bias % 60);
00229 
00230       if (tzschema != TIME_ZONE_ID_UNKNOWN)
00231          sprintf(tztmp + ccnt, "%s", 
00232                xdas_abbreviate_win32_tzname(tzi.DaylightName, stz, sizeof(stz)));
00233    }
00234 
00235 #else
00236 
00239    /* timezone variable is seconds west of GMT */
00240    int ccnt = sprintf(tztmp, "%s%ld", tzname[0], timezone / 3600);
00241    if (timezone % 3600)
00242       ccnt += sprintf(tztmp + ccnt, "%%:%ld", timezone % 3600 / 60);
00243    if (*tzname[1])
00244       sprintf(tztmp + ccnt, "%s", tzname[1]);
00245 
00246 #endif
00247 
00248    escape_str(xs->time_zone, tztmp);
00249 
00251    xs->time_uncert_int = 0;
00252    xs->time_uncert_ind = 0;
00253 
00254    return 0;
00255 }
00256 
00263 void xdas_internal_terminate_session(xdas_session * xs)
00264 {
00265    xdas_service_disconnect(xs);
00266    xdas_buffer_free(xs->req);
00267    xdas_buffer_free(xs->rsp);
00268    free(xs->org_info[0]);
00269    free(xs);
00270 }
00271 

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