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