00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00097 
00098 
00099 
00100 
00101 
00102 
00103 
00104 
00105 
00106 
00107 
00108 
00109 
00110 
00111 
00112 
00113 
00114 
00115 
00116 #include "lcas_config.h"
00117 #include <stdio.h>
00118 #include <stdlib.h>
00119 #include <string.h>
00120 
00121 #if HAVE_MALLOC_H
00122 #include <malloc.h>
00123 #endif
00124 
00125 #if HAVE_TIME_H
00126 #include <time.h>
00127 #endif
00128 
00129 #if HAVE_VALUES_H
00130 #include <values.h>
00131 #endif
00132 
00133 #include "lcas_modules.h"
00134 
00135 
00136 
00137 
00138 
00139 
00140 
00141 
00142 #define LINELEN         65536
00143 #define FIELDLEN        10
00144 #define MAXSLOTS        100
00145 #define NFIELDS         6
00146 #define MINUTE_FIELD    0
00147 #define HOUR_FIELD      1
00148 #define MDAY_FIELD      2
00149 #define MONTH_FIELD     3
00150 #define YEAR_FIELD      4
00151 #define WDAY_FIELD      5
00152 #define WDAY_MIN        0
00153 #define WDAY_MAX        7
00154 
00155 #define HOUR_FAIL       (unsigned short) 0x0
00156 #define HOUR_OK         (unsigned short) 0x1
00157 #define WDAY_FAIL       (unsigned short) 0x0
00158 #define WDAY_OK         (unsigned short) 0x2
00159 #define DATE_FAIL       (unsigned short) 0x0
00160 #define DATE_OK         (unsigned short) 0x4
00161 #define ALL_FAIL        (unsigned short) 0x0
00162 #define ALL_OK          (unsigned short) 0x7
00163 
00164 
00165 
00166 
00167 static int            findrange(char * range_string, int * minval, int * maxval);
00168 static int            get_timeslots(char * timeslots_file);
00169 static unsigned short check_date(time_t clock, struct tm * pstart, struct tm * pstop);
00170 static unsigned short check_wday(time_t clock, struct tm * pstart, struct tm * pstop);
00171 static unsigned short check_hour(time_t clock, struct tm * pstart, struct tm * pstop);
00172 
00173 
00174 
00175 
00176 
00177 
00178 
00179 static int                    nslots;
00180 static struct tm              start_s[MAXSLOTS];
00181 static struct tm              stop_s[MAXSLOTS];
00182 static char *                 modname="lcas_timeslots.mod";
00183 static char *                 clockcheck_db = NULL;
00184 static char *                 days[7]=
00185                               {
00186                                   "Sunday",
00187                                   "Monday",
00188                                   "Tuesday",
00189                                   "Wednesday",
00190                                   "Thursday",
00191                                   "Friday",
00192                                   "Saturday"
00193                               };
00194 static char *                 months[12]=
00195                               {
00196                                   "Jan",
00197                                   "Feb",
00198                                   "Mar",
00199                                   "Apr",
00200                                   "May",
00201                                   "Jun",
00202                                   "Jul",
00203                                   "Aug",
00204                                   "Sep",
00205                                   "Oct",
00206                                   "Nov",
00207                                   "Dec",
00208                               };
00209 
00210 
00211 
00212 
00213 
00214 
00215 
00216 
00217 
00218 
00219 
00220 #if 0
00221 int plugin_initialize(char * plugin_db)
00222 #endif
00223 int plugin_initialize(int argc, char ** argv)
00224 {
00225     int                           i;
00226 
00227     lcas_log_debug(1,"%s-plugin_initialize(): passed arguments:\n",modname);
00228     for (i=0; i < argc; i++)
00229     {
00230         lcas_log_debug(1,"%s-plugin_initialize(): arg %d is %s\n",
00231              modname,i,argv[i]);
00232     }
00233 
00234     if (argc > 1)
00235         clockcheck_db = lcas_findfile(argv[1]);
00236 
00237     
00238     if (clockcheck_db == NULL)
00239     {
00240         lcas_log(0,"\t%s-plugin_initialize() error: timeslots file required !\n",
00241                  modname);
00242         return LCAS_MOD_NOFILE;
00243     }
00244     if (lcas_getfexist(1,clockcheck_db) == NULL)
00245     {
00246         lcas_log(0,
00247             "\t%s-plugin_initialize() error: Cannot find fabric availability (time slots) file: %s\n",
00248             modname,clockcheck_db);
00249         return LCAS_MOD_NOFILE;
00250     }
00251 
00252     
00253     lcas_log_debug(1,"\t%s-plugin_initialize(): looking for timeslots in %s\n",
00254              modname,clockcheck_db);
00255     nslots=get_timeslots(clockcheck_db);
00256     if (nslots < 0)
00257     {
00258         lcas_log(0,"\t%s-plugin_initialize(): Cannot read fabric availability file\n",
00259                  modname);
00260         return LCAS_MOD_FAIL;
00261     }
00262     else if (nslots==0)
00263     {
00264         
00265         lcas_log(0,"\t%s-plugin_initialize(): empty fabric availability file\n",
00266                  modname);
00267     }
00268     
00269     return LCAS_MOD_SUCCESS;
00270 }
00271 
00272 
00273 
00274 
00275 
00276 
00277 
00278 
00279 
00280 
00281 
00282 
00283 int
00284 plugin_confirm_authorization(lcas_request_t request, lcas_cred_id_t lcas_cred)
00285 {
00286     time_t                        clock;
00287     struct tm *                   clock_s;
00288     int                           passflag;
00289     int                           islot;
00290     unsigned short                slotflag;
00291 
00292     
00293     
00294     clock=time(NULL);
00295     clock_s = localtime(&clock);
00296 
00297     
00298 
00299 
00300 
00301 
00302 
00303 
00304 
00305 
00306 
00307 
00308 
00309 
00310 
00311     passflag=0;
00312     for (islot=0; islot < nslots; ++islot)
00313     {
00314         slotflag=ALL_FAIL;
00315         lcas_log_debug(0,"\t%s-plugin_confirm_authorization(): Checking slot %d out of %d in %s\n",
00316                  modname,islot+1,nslots,clockcheck_db);
00317 
00318         
00319         slotflag|=check_date(clock,start_s+islot,stop_s+islot);
00320         
00321         
00322         slotflag|=check_wday(clock,start_s+islot,stop_s+islot);
00323 
00324         
00325         slotflag|=check_hour(clock,start_s+islot,stop_s+islot);
00326 
00327         if (slotflag==ALL_OK) passflag=1;
00328     }
00329 
00330     if (passflag!=1) return LCAS_MOD_FAIL;
00331 
00332     return LCAS_MOD_SUCCESS;
00333 }
00334 
00335 
00336 
00337 
00338 
00339 
00340 
00341 
00342 
00343 
00344 
00345 int plugin_terminate()
00346 {
00347     lcas_log_debug(1,"%s-plugin_terminate(): terminating\n",modname);
00348     if (clockcheck_db) { free(clockcheck_db); clockcheck_db=NULL; }
00349 
00350     return LCAS_MOD_SUCCESS;
00351 }
00352 
00353 
00354 
00355 
00356 
00357 
00358 
00359 
00360 
00361 
00362 
00363 
00364 
00365 static int
00366 findrange(char * range_string, int * minval, int * maxval)
00367 {
00368     char *                        t=NULL;
00369     char *                        u=NULL;
00370 
00371     if (range_string==NULL) return 1;
00372 
00373     
00374 
00375 
00376     if (strcmp(range_string,"*")==0)
00377     {
00378         *minval=*maxval=-1;
00379         return 0;
00380     };
00381 
00382     
00383 
00384 
00385     if (strncmp(range_string,"-",1)==0)
00386     {
00387         *minval=-1;
00388         if (strlen(range_string)>1)
00389         {
00390             if ( (t=strtok(range_string,"-")) )
00391             {
00392                 *maxval=strtol(t,&u,0);
00393                 if(t==u) {
00394                     lcas_log(0,"\t%s-findrange(): Cannot read long integer from string %s\n",
00395                              modname,t);
00396                     return 1;
00397                 }
00398             }
00399             else
00400             {
00401                 lcas_log(0,"\t%s-findrange(): Cannot separate string %s\n",
00402                          modname,range_string);
00403                 return 1;
00404             }
00405         }
00406         else
00407         {
00408             
00409 
00410 
00411             *maxval=-1;
00412         }
00413         return 0;
00414     }
00415 
00416     
00417 
00418 
00419 
00420     if ( (t=strtok(range_string,"-")) )
00421     {
00422         *minval=strtol(t,&u,0);
00423         if(t==u) {
00424             lcas_log(0,"\t%s-findrange(): Cannot read long integer from string %s\n",
00425                      modname,t);
00426             return 1;
00427         }
00428     }
00429     else
00430     {
00431         lcas_log(0,"\t%s-findrange(): Cannot separate string %s\n",
00432                  modname,range_string);
00433         return 1;
00434     }
00435 
00436     
00437 
00438 
00439     if ( (t=strtok(NULL,"-")) )
00440     {
00441         *maxval=strtol(t,&u,0);
00442         if(t==u) {
00443             lcas_log(0,"\t%s-findrange(): Cannot read long integer from string %s\n",
00444                      modname,t);
00445             return 1;
00446         }
00447     }
00448     
00449 
00450 
00451     else
00452     {
00453         *maxval=-1;
00454     }
00455     return 0;
00456 }
00457 
00458 
00459 
00460 
00461 
00462 
00463 
00464 
00465 
00466 
00467 
00468 
00469 
00470 static int
00471 get_timeslots(char * timeslots_file)
00472 {
00473     time_t                        clock;
00474     struct tm *                   clock_s;
00475     FILE *                        fp;
00476     char                          lbuf[LINELEN];
00477     char *                        s;
00478     int                           iline,ifield;
00479     int                           islot;
00480     int                           rangemin[NFIELDS],rangemax[NFIELDS];
00481     char *                        fields[NFIELDS];
00482 
00483     time(&clock);
00484     clock_s=localtime(&clock);
00485 
00486     
00487 
00488 
00489 
00490 
00491 
00492 
00493 
00494 
00495 
00496 
00497     fp=fopen(timeslots_file,"r");
00498     if( fp == (FILE *)0)
00499     {
00500         lcas_log(0,"\t%s-get_timeslots(): FILE %s could not be opened for input\n",
00501                  modname,timeslots_file);
00502         return -1;
00503     }
00504     iline=0;
00505     islot=0;
00506     while(fgets(lbuf,LINELEN,fp))
00507     {
00508         
00509         if (lbuf[0]!='#') 
00510         {
00511             if (islot >= MAXSLOTS)
00512             {
00513                 lcas_log(0,"\t%s-get_timeslots() error: Too many time slots found (max=%d)\n",
00514                          modname,MAXSLOTS);
00515                 return -MAXSLOTS;
00516             }
00517             
00518 
00519 
00520             start_s[islot].tm_sec=0;               stop_s[islot].tm_sec=0;
00521             start_s[islot].tm_min=0;               stop_s[islot].tm_min=59;
00522             start_s[islot].tm_hour=0;              stop_s[islot].tm_hour=23;
00523             start_s[islot].tm_mday=1;              stop_s[islot].tm_mday=31;
00524             start_s[islot].tm_mon=0;               stop_s[islot].tm_mon=11;
00525             start_s[islot].tm_year=70;             stop_s[islot].tm_year=137;
00526             start_s[islot].tm_wday=0;              stop_s[islot].tm_wday=6;
00527             start_s[islot].tm_yday=0;              stop_s[islot].tm_yday=364;
00528             start_s[islot].tm_isdst=clock_s->tm_isdst; stop_s[islot].tm_isdst=clock_s->tm_isdst;
00529 
00530             
00531             ifield=0;
00532             lcas_log_debug(4,"\t%s-get_timeslots(): buffer: %s\n",modname,lbuf);
00533             s=strtok(lbuf," \t\n");
00534             if (strlen(s) > FIELDLEN)
00535             {
00536                 lcas_log(0,"\t%s-get_timeslots(): field %d: %s is too long (MAX=%d)\n",
00537                          modname,ifield,s,FIELDLEN);
00538                 return -1;
00539             }
00540             fields[ifield]=s;
00541             lcas_log_debug(4,"\t%s-get_timeslots(): ifield: %d, field: %s\n",modname,ifield,fields[ifield]);
00542             for(++ifield;(s=strtok(NULL," \t\n")) && (ifield < NFIELDS) ;++ifield)
00543             {
00544                 if (strlen(s) > FIELDLEN)
00545                 {
00546                     lcas_log(0,"\t%s-get_timeslots(): field %d: %s is too long (MAX=%d)\n",
00547                              modname,ifield,s,FIELDLEN);
00548                     return -1;
00549                 }
00550                 fields[ifield]=s;
00551                 lcas_log_debug(4,"\t%s-get_timeslots(): ifield: %d, field: %s\n",modname,ifield,fields[ifield]);
00552             }
00553             if (ifield != NFIELDS)
00554             {
00555                 lcas_log(0,"\t%s-get_timeslots(): Should have found %d fields, but found %d fields instead\n",
00556                     modname,NFIELDS,ifield);
00557                 return -1;
00558             }
00559 
00560             
00561             for(ifield=0;(ifield < NFIELDS) ;++ifield)
00562             {
00563                 if (findrange(fields[ifield],&rangemin[ifield],&rangemax[ifield]))
00564                 {
00565                     lcas_log(0,
00566                         "\t%s-get_timeslots(): Cannot extract time range of field %d in FILE %s\n",
00567                         modname,ifield,timeslots_file);
00568                     return -1;
00569                 }
00570                 lcas_log_debug(4,"\t%s-get_timeslots(): ifield: %d, rangemin-rangemax: %d - %d\n",modname,ifield,rangemin[ifield],rangemax[ifield]);
00571                 switch (ifield)
00572                 {
00573                     case MINUTE_FIELD:
00574                         if (rangemin[ifield] >= 0) start_s[islot].tm_min=rangemin[ifield];
00575                         if (rangemax[ifield] >= 0) stop_s[islot].tm_min=rangemax[ifield];
00576                         break;
00577                     case HOUR_FIELD:
00578                         if (rangemin[ifield] >= 0) start_s[islot].tm_hour=rangemin[ifield];
00579                         if (rangemax[ifield] >= 0) stop_s[islot].tm_hour=rangemax[ifield];
00580                         break;
00581                     case MDAY_FIELD:
00582                         if (rangemin[ifield] >= 0) start_s[islot].tm_mday=rangemin[ifield];
00583                         if (rangemax[ifield] >= 0) stop_s[islot].tm_mday=rangemax[ifield];
00584                         break;
00585                     case MONTH_FIELD:
00586                         if (rangemin[ifield] >= 0) start_s[islot].tm_mon=rangemin[ifield]-1;
00587                         if (rangemax[ifield] >= 0) stop_s[islot].tm_mon=rangemax[ifield]-1;
00588                         break;
00589                     case YEAR_FIELD:
00590                         if (rangemin[ifield] >= 0) start_s[islot].tm_year=rangemin[ifield]-1900;
00591                         if (rangemax[ifield] >= 0) stop_s[islot].tm_year=rangemax[ifield]-1900;
00592                         break;
00593                     case WDAY_FIELD:
00594                         if (rangemin[ifield] >= 0) start_s[islot].tm_wday=rangemin[ifield];
00595                         if (rangemax[ifield] >= 0) stop_s[islot].tm_wday=rangemax[ifield];
00596                         break;
00597                     default:
00598                         lcas_log_debug(0,"\t%s-get_timeslots(): fieldnr: %d is not used\n",
00599                         modname,ifield);
00600                         break;
00601                 }
00602             }
00603             islot++;
00604         }
00605         iline++;
00606     }
00607     fclose(fp);
00608 
00609     return islot;
00610 }
00611 
00612 
00613 
00614 
00615 
00616 
00617 
00618 
00619 
00620 
00621 
00622 
00623 
00624 
00625 static unsigned short
00626 check_date(time_t clock, struct tm * pstart, struct tm * pstop)
00627 {
00628     time_t                        date1,date2;
00629     struct tm *                   date1_s = (struct tm *) malloc(sizeof(struct tm));
00630     struct tm *                   date2_s = (struct tm *) malloc(sizeof(struct tm));
00631     struct tm *                   clock_s;
00632 
00633     clock_s = localtime(&clock);
00634 
00635     date1_s->tm_sec   = date2_s->tm_sec   = clock_s->tm_sec;
00636     date1_s->tm_min   = date2_s->tm_min   = clock_s->tm_min;
00637     date1_s->tm_hour  = date2_s->tm_hour  = clock_s->tm_hour;
00638     date1_s->tm_mday  = date2_s->tm_mday  = clock_s->tm_mday;
00639     date1_s->tm_mon   = date2_s->tm_mon   = clock_s->tm_mon;
00640     date1_s->tm_year  = date2_s->tm_year  = clock_s->tm_year;
00641     date1_s->tm_wday  = date2_s->tm_wday  = clock_s->tm_wday;
00642     date1_s->tm_yday  = date2_s->tm_yday  = clock_s->tm_yday;
00643     date1_s->tm_isdst = date2_s->tm_isdst = clock_s->tm_isdst;
00644 
00645     date1_s->tm_mday  = pstart->tm_mday;
00646     date1_s->tm_mon   = pstart->tm_mon;
00647     date1_s->tm_year  = pstart->tm_year;
00648     date2_s->tm_mday  = pstop->tm_mday;
00649     date2_s->tm_mon   = pstop->tm_mon;
00650     date2_s->tm_year  = pstop->tm_year;
00651 
00652     date1=mktime(date1_s);
00653     date2=mktime(date2_s);
00654 
00655     if (difftime(date2,date1) < 0.0)
00656     {
00657         lcas_log_debug(0,"\t%s-check_date():     Wrong dates: start date is later than stop date\n",
00658                  modname);
00659         free((void *) date1_s);
00660         free((void *) date2_s);
00661         return DATE_FAIL;
00662     }
00663     else if ( (difftime(date2,clock) < 0.0) ||
00664               (difftime(clock,date1) < 0.0) )
00665     {
00666         lcas_log(0,"\t%s-check_date():     Date (%d %s %d) out of range: (%d %s %d)-(%d %s %d)\n",
00667             modname,
00668             clock_s->tm_mday,
00669             months[clock_s->tm_mon],
00670             clock_s->tm_year+1900,
00671             date1_s->tm_mday,
00672             months[date1_s->tm_mon],
00673             date1_s->tm_year+1900,
00674             date2_s->tm_mday,
00675             months[date2_s->tm_mon],
00676             date2_s->tm_year+1900
00677         );
00678         free((void *) date1_s);
00679         free((void *) date2_s);
00680         return DATE_FAIL;
00681     }
00682 
00683     free((void *) date1_s);
00684     free((void *) date2_s);
00685     return DATE_OK;
00686 }
00687 
00688 
00689 
00690 
00691 
00692 
00693 
00694 
00695 
00696 
00697 
00698 
00699 
00700 
00701 static unsigned short
00702 check_wday(time_t clock, struct tm * pstart, struct tm * pstop)
00703 {
00704     struct tm *                   clock_s;
00705     int                           start_wday,stop_wday,wday;
00706 
00707     start_wday=pstart->tm_wday;
00708     stop_wday=pstop->tm_wday;
00709 
00710     
00711     if ( (start_wday < WDAY_MIN) || (start_wday > WDAY_MAX) )
00712     {
00713         lcas_log_debug(0,"\t%s-check_wday():     Start week day (%d) out of range (%d-%d)\n",
00714                  modname,start_wday,WDAY_MIN,WDAY_MAX);
00715         return WDAY_FAIL;
00716     }
00717     else if (start_wday == WDAY_MAX)
00718     {
00719         start_wday=WDAY_MIN;
00720     }
00721 
00722     
00723     if ( (stop_wday < WDAY_MIN) || (stop_wday > WDAY_MAX) )
00724     {
00725         lcas_log_debug(0,"\t%s-check_wday():     Stop week day (%d) out of range (%d-%d)\n",
00726                  modname,stop_wday,WDAY_MIN,WDAY_MAX);
00727         return WDAY_FAIL;
00728     }
00729     else if (stop_wday == WDAY_MAX)
00730     {
00731         stop_wday=WDAY_MIN;
00732     }
00733     
00734     
00735     clock_s = localtime(&clock);
00736     wday=clock_s->tm_wday;
00737     if (wday == WDAY_MAX) wday=WDAY_MIN;
00738 
00739     
00740     if (stop_wday >= start_wday)
00741     {
00742         if ( (wday > stop_wday) || (wday < start_wday) )
00743         {
00744             lcas_log_debug(0,"\t%s-check_wday():     Wday (%s) out of range (%s-%s)\n",
00745                      modname,days[wday],days[start_wday],days[stop_wday]);
00746             return WDAY_FAIL;
00747         }
00748     }
00749     else
00750     {
00751         stop_wday+=WDAY_MAX;
00752         if ( (wday > stop_wday) || (wday < start_wday) )
00753         {
00754             wday+=WDAY_MAX;
00755             if ( (wday > stop_wday) || (wday < start_wday) )
00756             {
00757                 lcas_log_debug(0,"\t%s-check_wday():     Wday (%s) out of range (%s-%s)\n",
00758                          modname,
00759                          days[wday-WDAY_MAX],days[start_wday],
00760                          days[stop_wday-WDAY_MAX]);
00761                 return WDAY_FAIL;
00762             }
00763         }
00764     }
00765 
00766     return WDAY_OK;
00767 }
00768 
00769 
00770 
00771 
00772 
00773 
00774 
00775 
00776 
00777 
00778 
00779 
00780 
00781 
00782 static unsigned short
00783 check_hour(time_t clock, struct tm * pstart, struct tm * pstop)
00784 {
00785     time_t                        date1,date2;
00786     struct tm *                   date1_s = (struct tm *) malloc(sizeof(struct tm));
00787     struct tm *                   date2_s = (struct tm *) malloc(sizeof(struct tm));
00788     struct tm *                   clock_s;
00789 
00790     clock_s = localtime(&clock);
00791 
00792     date1_s->tm_sec   = date2_s->tm_sec   = clock_s->tm_sec;
00793     date1_s->tm_min   = date2_s->tm_min   = clock_s->tm_min;
00794     date1_s->tm_hour  = date2_s->tm_hour  = clock_s->tm_hour;
00795     date1_s->tm_mday  = date2_s->tm_mday  = clock_s->tm_mday;
00796     date1_s->tm_mon   = date2_s->tm_mon   = clock_s->tm_mon;
00797     date1_s->tm_year  = date2_s->tm_year  = clock_s->tm_year;
00798     date1_s->tm_wday  = date2_s->tm_wday  = clock_s->tm_wday;
00799     date1_s->tm_yday  = date2_s->tm_yday  = clock_s->tm_yday;
00800     date1_s->tm_isdst = date2_s->tm_isdst = clock_s->tm_isdst;
00801 
00802     date1_s->tm_sec   = pstart->tm_sec;
00803     date1_s->tm_min   = pstart->tm_min;
00804     date1_s->tm_hour  = pstart->tm_hour;
00805     date2_s->tm_sec   = pstop->tm_sec;
00806     date2_s->tm_min   = pstop->tm_min;
00807     date2_s->tm_hour  = pstop->tm_hour;
00808 
00809     date1=mktime(date1_s);
00810     date2=mktime(date2_s);
00811     if (date2_s->tm_hour==0) date2_s->tm_hour=24;
00812 
00813     if (difftime(date2,date1) < 0.0)
00814     {
00815         lcas_log_debug(0,"\t%s-check_hour():     Wrong hours: start hour is later than stop hour\n",
00816                  modname);
00817         free((void *) date1_s);
00818         free((void *) date2_s);
00819         return HOUR_FAIL;
00820     }
00821     else if ( (difftime(date2,clock) < 0.0) ||
00822               (difftime(clock,date1) < 0.0) )
00823     {
00824         lcas_log_debug(0,"\t%s-check_hour():     Hour (%02d:%02d:%02d) out of range: (%02d:%02d:%02d)-(%02d:%02d:%02d)\n",
00825             modname,
00826             clock_s->tm_hour,
00827             clock_s->tm_min,
00828             clock_s->tm_sec,
00829             date1_s->tm_hour,
00830             date1_s->tm_min,
00831             date1_s->tm_sec,
00832             date2_s->tm_hour,
00833             date2_s->tm_min,
00834             date2_s->tm_sec
00835         );
00836         free((void *) date1_s);
00837         free((void *) date2_s);
00838         return HOUR_FAIL;
00839     }
00840 
00841     free((void *) date1_s);
00842     free((void *) date2_s);
00843     return HOUR_OK;
00844 }