File: oldgaa_policy_evaluator.c

package info (click to toggle)
globus-gsi-callback 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,756 kB
  • sloc: sh: 11,067; ansic: 5,658; makefile: 161
file content (874 lines) | stat: -rw-r--r-- 22,259 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
/*
 * Copyright 1999-2006 University of Chicago
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/**********************************************************************
 oldgaa_policy_evaluator.c:

Description:
	This file is used internally by the oldgaa routines
**********************************************************************/

/**********************************************************************
                             Include header files
**********************************************************************/
#include <stdio.h>	/* File reading and writing */
#include <errno.h>	/* For errno */
#include <stdlib.h>	/* For malloc() */
#include <string.h> 	/* strerror() and other string functions */

#include "globus_oldgaa.h"
#include "oldgaa_utils.h"
#include "globus_oldgaa_utils.h"
#include "oldgaa_policy_evaluator.h"
#include "oldgaa_gl_internal_err.h"

/**********************************************************************
                       Define module specific variables
**********************************************************************/

#define STRING_LENGTH			80


/**********************************************************************
             Helpers Static Function Declarations
 **********************************************************************/


static
oldgaa_error_code
evaluate_condition(oldgaa_sec_context_ptr sc,
                   oldgaa_conditions_ptr  condition,
                   oldgaa_options_ptr     options);

static
char *
get_day();

static
char *
get_hr_24();

/*  static */
/*  char * */
/*  get_hr_12() */

static
char *
get_minutes();

static
char *
get_seconds();

/*  static */
/*  char * */
/*  get_am_pm() */

static
int
day_to_val(char *str);

static
int
check_day(char *str1, char *str2, char *day);

static
char*
get_value(int *jj, const char *cond, const char delimiter);


/**********************************************************************

Function: oldgaa_find_matching_entry

Description:
	Finds policy corresponding to the given principal.

Parameters:
        minor_status, mechanism-specific error code.
	ptr, pointer to principal.
        policy, pointer to policy.

Returns:
	Pointer to a oldgaa_policy structure if successful

**********************************************************************/


oldgaa_policy_ptr
oldgaa_find_matching_entry(uint32             *minor_status,
                        oldgaa_principals_ptr  ptr,
                        oldgaa_policy_ptr      policy)
{
    oldgaa_policy_ptr entry = policy;

    /* Check arguments */
    if (!ptr)
    {
        errno = ERRNO_INVALID_ARGUMENT;
        *minor_status = -1;

        goto exit;
    }

    while(entry)
    {
        if(oldgaa_strings_match(entry->type, OLDGAA_ANYBODY)) return entry;

        /* do exact match */
        if(oldgaa_compare_principals(ptr, entry))  return entry;

#ifdef PRINCIPALS_REGEX_MATCH
        {
            char    **subject_regexes = NULL; /* NULL terminated regex list */
            int i;

#ifdef DEBUG
            fprintf(stderr, "%s %s\n", ptr->value, entry->value);
#endif /* DEBUG */

            subject_regexes = oldgaa_parse_regex(entry->value);

            if(subject_regexes)
            {
                if(oldgaa_check_reg_expr(ptr->value, subject_regexes))
                {
                    for (i=0; subject_regexes[i] != NULL; i++)
                        free(subject_regexes[i]);
                    free(subject_regexes);
                    return entry;
                }
                for (i=0; subject_regexes[i] != NULL; i++)
                    free(subject_regexes[i]);
                free(subject_regexes);
            }
        }
#endif /* #ifdef PRINCIPALS_REGEX_MATCH  */

        entry = entry->next;
    }

    exit:
    return NULL;
}

/**********************************************************************

Function: oldgaa_check_access_rights

Description:
         Checks if the requested access rights are granted. It checks
         conditions, if any.

Parameters:


Returns:
        OLDGAA atatus
	
**********************************************************************/


oldgaa_error_code
oldgaa_check_access_right(oldgaa_sec_context_ptr sc,
                    oldgaa_rights_ptr      requested_rights,
                    oldgaa_rights_ptr      rights,
                    oldgaa_answer_ptr      detailed_answer,
                    oldgaa_options_ptr     options)
{
 oldgaa_error_code oldgaa_status;
 int            was_no = FALSE, was_maybe = FALSE;

#ifdef DEBUG
fprintf(stderr, "\noldgaa_check_access_rights:\n");
#endif /* DEBUG */

  /* check if the requested rights match rights in the policy */

 if (!oldgaa_compare_rights(requested_rights, rights))
 return OLDGAA_NO; /* now we have just one type of rights: CA:sign */

#ifdef DEBUG
fprintf(stderr, "right is granted\n");
#endif /* DEBUG */

  detailed_answer->rights = rights;
  rights->reference_count++;

   if(rights->cond_bindings) /* operation is allowed and there are
                                          some conditions */

   {
#ifdef DEBUG
fprintf(stderr, "there are some conditions\n");
#endif /* DEBUG */

 oldgaa_status = oldgaa_evaluate_conditions(sc,
                                            rights->cond_bindings,
                                            options);

    if(oldgaa_status == OLDGAA_NO)    was_no    = TRUE;
    if(oldgaa_status == OLDGAA_MAYBE) was_maybe = TRUE;

   }

  if(was_no)    return OLDGAA_NO;
  if(was_maybe) return OLDGAA_MAYBE;

  return OLDGAA_YES;  /* operation is allowed and either there are NO any
                     or all conditions are met */
}

oldgaa_error_code
oldgaa_check_access_rights(oldgaa_sec_context_ptr sc,
                    oldgaa_rights_ptr      requested_rights,
                    oldgaa_rights_ptr      rights,
                    oldgaa_answer_ptr      detailed_answer,
                    oldgaa_options_ptr     options)
{
    /*** this does not do the right thing for negative rights */
    oldgaa_rights_ptr right;
    oldgaa_error_code status = OLDGAA_NO;

    for (right = rights; right; right = right->next)
	if ((status = oldgaa_check_access_right(sc, requested_rights, right, detailed_answer, options)) == OLDGAA_YES)
	    break;
    return(status);
}
/*****************************************************************************/

oldgaa_error_code
oldgaa_get_authorized_principals(oldgaa_sec_attrb_ptr *attributes,
                              oldgaa_policy_ptr     policy,
                              oldgaa_principals_ptr principal,
                              oldgaa_rights_ptr     rights)

{
  oldgaa_policy_ptr    entry  = policy;
  int               was_anybody    = 0;
  int               was_neg_rights = 0;
  int               number_of_entries = 1;
  oldgaa_sec_attrb_ptr attrb = NULL;
  uint32            minor_status;
  oldgaa_error_code    oldgaa_status = OLDGAA_SUCCESS;

#ifdef DEBUG
fprintf(stderr, "\noldgaa_get_authorized_principals:\n");
#endif /* DEBUG */

  minor_status = 0;

/* Check arguments */
  if (!policy && !attributes)
  {
    errno = ERRNO_INVALID_ARGUMENT;
    minor_status = -1;
    return OLDGAA_FAILURE;
  }


   while(entry)
  {
    if(oldgaa_strings_match(entry->type, OLDGAA_ANYBODY) &&
       oldgaa_compare_rights(entry->rights, rights)) was_anybody = 1;


   if(oldgaa_strings_match(entry->type,      principal->type)   &&
      oldgaa_strings_match(entry->authority, principal->authority))
  {
    if(oldgaa_compare_rights(entry->rights, rights))
       {
         oldgaa_allocate_sec_attrb(&attrb);
         attrb->type      = oldgaa_strcopy(entry->type,      attrb->type);
         attrb->authority = oldgaa_strcopy(entry->authority, attrb->authority);
         attrb->value     = oldgaa_strcopy(entry->value,     attrb->value);

       if(*attributes == NULL) { *attributes = attrb; }

         oldgaa_add_attribute(attributes, attrb);
         number_of_entries++;
       }
    else
      {
     if(oldgaa_strings_match(entry->rights->type,      NEGATIVE_RIGHTS)   &&
        oldgaa_strings_match(entry->rights->authority, rights->authority) &&
        oldgaa_strings_match(entry->rights->value,     rights->value) ) was_neg_rights = 1;
     }


  }

   entry = entry->next;

  } /* end of while */

    if(was_anybody && (number_of_entries == 1)) /* return ANYBODY only if it is the only entry of this type */
      {
         oldgaa_allocate_sec_attrb(&attrb);
         attrb->type      = oldgaa_strcopy(OLDGAA_ANYBODY, attrb->type);
         attrb->authority = oldgaa_strcopy(" ",         attrb->authority);
         attrb->value     = oldgaa_strcopy(" ",         attrb->value);

         if (*attributes == NULL) *attributes = attrb;
         else oldgaa_add_attribute(attributes, attrb);
       }

return  oldgaa_status;
}

/**********************************************************************

Function: oldgaa_evaluate_conditions()

Description:
	Walks throug condition list and evaluates each condition.

Parameters:
	security context, condition list and options

Returns:
	OLDGAA_YES   if all conditions are met.
	OLDGAA_NO    if at least one is not met.
	OLDGAA_MAYBE if some conditions are not evaluated.

**********************************************************************/

oldgaa_error_code
oldgaa_evaluate_conditions(oldgaa_sec_context_ptr    sc,
                           oldgaa_cond_bindings_ptr  conditions,
                           oldgaa_options_ptr        options)
{
   oldgaa_error_code         oldgaa_status = OLDGAA_NO;
   oldgaa_cond_bindings_ptr  cond       = conditions;
   int                    was_no     = FALSE, was_maybe = FALSE;

#ifdef DEBUG
fprintf(stderr, "\noldgaa_evaluate_conditions:\n");
#endif /* DEBUG */

 while(cond)/* walk throug condition list */
   {
     oldgaa_status = OLDGAA_MAYBE;

     oldgaa_status =  evaluate_condition(sc, cond->condition, options);

     if(oldgaa_status == OLDGAA_NO)    was_no    = TRUE;
     if(oldgaa_status == OLDGAA_MAYBE) was_maybe = TRUE;

     cond = cond->next;
   }

 if(was_no)    return OLDGAA_NO;
  if(was_maybe) return OLDGAA_MAYBE;

  return OLDGAA_YES;

}



oldgaa_error_code
oldgaa_evaluate_day_cond(oldgaa_conditions_ptr condition,
                  oldgaa_options_ptr    options)

{
   int            retval, j=0;
   oldgaa_error_code oldgaa_status = OLDGAA_NO;

   char *day = NULL, *str1 = NULL, *str2 = NULL, cond[MAX_COND_LENGTH] = {NUL};
   char *current_day = NULL;
   char *value;

   strcpy(cond, condition->value);

     /* get current day */
     current_day = get_day();
     day = oldgaa_strcopy(current_day, day);
     free(current_day);

     /* get first day delimiter */
     str1 = oldgaa_strcopy(get_value(&j, cond, '-'), str1);

     /* get second day delimiter */
     value = get_value(&j, cond, NUL);
     str2 = oldgaa_strcopy(value, str2);
     free(value);

     retval = check_day(str1, str2, day);

     if(retval == -1) return OLDGAA_MAYBE; /* unsupported day format */

     if(retval == 1) oldgaa_status = OLDGAA_YES;

   return oldgaa_status;

}

/**********************************************************************

Function: oldgaa_evaluate_regex_cond()

Description:
	Walks throug condition list and evaluates each condition.

Parameters:
	security context, condition list and options

Returns:
	OLDGAA_YES   if all conditions are met.
	OLDGAA_NO    if at least one is not met.
	OLDGAA_MAYBE if some conditions are not evaluated.

**********************************************************************/

oldgaa_error_code
oldgaa_evaluate_regex_cond(oldgaa_conditions_ptr condition,
                        oldgaa_options_ptr    options)
{
  char          **subject_regexes = NULL; /* NULL terminated list of regexes */
  int i;
  oldgaa_error_code  oldgaa_status      = OLDGAA_NO;

#ifdef DEBUG
fprintf(stderr, "oldgaa_evaluate_rege_cond:\n");
#endif /* DEBUG */

   subject_regexes = oldgaa_parse_regex(condition->value);

   if(!subject_regexes) return OLDGAA_FAILURE;

     if(oldgaa_check_reg_expr(options->value, subject_regexes))
      oldgaa_status = OLDGAA_YES;

     for (i=0; subject_regexes[i] != NULL; i++)
       free(subject_regexes[i]);
     free(subject_regexes);

return oldgaa_status;

}

/*****************************************************************************/

oldgaa_error_code
oldgaa_evaluate_time_cond(oldgaa_conditions_ptr condition,
                       oldgaa_options_ptr    options)

{
   int   j = 0;
   int   hr, min, sec;
   int   cond_hr, cond_min, cond_sec;
   char  cond[MAX_COND_LENGTH] = {NUL};

   strcpy(cond, condition->value);

  if(oldgaa_strings_match(condition->authority, HOUR_SCALE_24))
   {
     char *hr_str;
     char *min_str;
     char *sec_str;
     char *value;

     /* current hour    */
     hr_str = get_hr_24();
     hr  = atoi(hr_str);
     free(hr_str);

     /* current minutes */
     min_str = get_minutes();
     min = atoi(min_str);
     free(min_str);

     /* current seconds */
     sec_str = get_seconds();
     sec = atoi(sec_str);
     free(sec_str);

     /* get hours from condition value */
     value = get_value(&j, cond, ':');
     cond_hr = atoi(value);
     free(value);

     if (hr < cond_hr) return OLDGAA_NO;

     /* get minutes from condition value */
     value = get_value(&j, cond, ':');
     cond_min = atoi(value);
     free(value);

     /* get seconds from condition value */
     value = get_value(&j, cond, '-');
     cond_sec = atoi(value);
     free(value);


    if (cond_hr == hr) /* if hours are equal, check minutes */
    {
      if (min < cond_min) return OLDGAA_NO;

      if (cond_min == min) /* if minutes are equal, check seconds */
	{
          if (sec < cond_sec) return OLDGAA_NO;
          else goto success;
	}
    }

      /* hours are greater, check second time value */

     /* get hours from condition value */
     value = get_value(&j, cond, ':');
     cond_hr = atoi(value);
     free(value);

     if  (cond_hr < hr) return OLDGAA_NO;

    /* get minutes from condition value */
     value = get_value(&j, cond, ':');
     cond_min = atoi(value);
     free(value);

    /* get seconds from condition value */
     value = get_value(&j, cond, ':');
     cond_sec = atoi(value);
     free(value);


    if (cond_hr == hr) /* if hours are equal, check minutes */
    {
      if (cond_min < min) return OLDGAA_NO;

      if (cond_min == min) /* if minutes are equal, check seconds */
	{
          if (cond_sec <  sec) return OLDGAA_NO;
          else  goto success;
	}
    }


 success:

   return OLDGAA_YES;
   }

 return OLDGAA_MAYBE; /* unsupported time format */

}

/*****************************************************************************/


oldgaa_error_code
oldgaa_evaluate_sech_mech_cond(oldgaa_principals_ptr  principal,
                        oldgaa_conditions_ptr  condition,
                        oldgaa_options_ptr     options)

{
   oldgaa_error_code oldgaa_status = OLDGAA_NO;

   if (oldgaa_strings_match(condition->value, principal->authority))
   oldgaa_status = OLDGAA_YES;

   return oldgaa_status;

}



/**********************************************************************
             Helpers Static Functions
 **********************************************************************/


/**********************************************************************

Function: evaluate_condition()

Description:
	Invokes apropriate evaluation function for each condition.

Parameters:
	security context, condition list and options

Returns:
	OLDGAA_YES   if condition is met.
	OLDGAA_NO    if condition is not met.
	OLDGAA_MAYBE if evaluation function was not found.

**********************************************************************/

static
oldgaa_error_code
evaluate_condition(oldgaa_sec_context_ptr sc,
                   oldgaa_conditions_ptr  condition,
                   oldgaa_options_ptr     options)
{
  oldgaa_error_code oldgaa_status = OLDGAA_MAYBE;

#ifdef DEBUG
fprintf(stderr, "evaluate_condition: %s %s %s\n",
        condition->type,
        condition->authority,
        condition->value);
#endif /* DEBUG */


  if(!strcmp(condition->type,      COND_SUBJECTS) &&
     !strcmp(condition->authority, AUTH_GLOBUS))
     oldgaa_status = oldgaa_evaluate_regex_cond(condition, options);

  if(!strcmp(condition->type,      COND_BANNED_SUBJECTS) &&
     !strcmp(condition->authority, AUTH_GLOBUS))
    {
     oldgaa_status = oldgaa_evaluate_regex_cond(condition, options);
     if(oldgaa_status == OLDGAA_YES) oldgaa_status = OLDGAA_NO;
    }

#ifdef OLDGAA_COND_DAY
     if(!strcmp(condition->type, COND_DAY))
     oldgaa_status = oldgaa_evaluate_day_cond(condition, options);
#endif

#ifdef OLDGAA_COND_TIME
     if(!strcmp(condition->type, COND_TIME))
     oldgaa_status = oldgaa_evaluate_time_cond(condition, options);
#endif

#ifdef OLDGAA_COND_SEC_MECH
     if(!strcmp(condition->type, COND_SEC_MECH))
     oldgaa_status = oldgaa_evaluate_sech_mech_cond(sc->identity_cred->principal,
                                              condition, options);
#endif

   /* check if condition evaluation function for upcall was passed in the security context */
  if(sc->condition_evaluation)
    sc->condition_evaluation(sc, options, condition, &oldgaa_status);

  if(oldgaa_status != OLDGAA_MAYBE)
  condition->status |= COND_FLG_EVALUATED; /* evaluated */

  if(oldgaa_status == OLDGAA_YES)
  condition->status |= COND_FLG_MET;       /* met */

return oldgaa_status;

}

/*****************************************************************************/
static
char *
get_day()
{
  time_t     tt;
  struct tm *t;
  char      *str;

  str = malloc(STRING_LENGTH + 1 /* for NUL */);
  if (!str)
      out_of_memory();

  time(&tt);
  t = localtime(&tt);
  strftime(str,STRING_LENGTH,"%A",t);

  return str;
}


/*****************************************************************************/
static
char *
get_hr_24()
{
  time_t     tt;
  struct tm *t;
  char      *str;

  str = malloc(STRING_LENGTH + 1 /* for NUL */);
  if (!str)
      out_of_memory();

  time(&tt);
  t = localtime(&tt);
  strftime(str,STRING_LENGTH,"%H",t);

  return str;
}
/*****************************************************************************/
/* commented out by SLANG - not currently used */
/*  static */
/*  char * */
/*  get_hr_12() */
/*  { */
/*    time_t     tt; */
/*    struct tm *t; */
/*    char      *str; */

/*    str = malloc(STRING_LENGTH + 1); */
/*    if (!str) */
/*        out_of_memory(); */

/*    time(&tt);  */
/*    t = localtime(&tt);  */
/*    strftime(str,STRING_LENGTH,"%I",t); */

/*    return str;  */
/*  } */

/*****************************************************************************/
static
char *
get_minutes()
{
  time_t     tt;
  struct tm *t;
  char      *str;

  str = malloc(STRING_LENGTH + 1 /* for NUL */);
  if (!str)
      out_of_memory();


  time(&tt);
  t = localtime(&tt);
  strftime(str,STRING_LENGTH,"%M",t);

  return str;
}
/*****************************************************************************/
static
char *
get_seconds()
{
  time_t     tt;
  struct tm *t;
  char      *str;

  str = malloc(STRING_LENGTH + 1 /* for NUL */);
  if (!str)
      out_of_memory();

  time(&tt);
  t = localtime(&tt);
  strftime(str,STRING_LENGTH,"%S",t);

  return str;
}

/*****************************************************************************/
/* commented out by SLANG - not currently used */
/*  static */
/*  char * */
/*  get_am_pm() */
/*  { */
/*    time_t     tt; */
/*    struct tm *t; */
/*    char      *str; */

/*    str = malloc(STRING_LENGTH + 1); */
/*    if (!str) */
/*        out_of_memory(); */

/*    time(&tt);  */
/*    t = localtime(&tt);  */
/*    strftime(str,STRING_LENGTH,"%p",t); */

/*    return str;  */
/*  }  */
/*****************************************************************************/
static
int
day_to_val(char *str)
{

 if (oldgaa_regex_matches_string(str, "Su") ||
     oldgaa_regex_matches_string(str, "su")) return 1;

 if (oldgaa_regex_matches_string(str, "Mo") ||
     oldgaa_regex_matches_string(str, "mo")) return 2;

 if (oldgaa_regex_matches_string(str, "Tu") ||
     oldgaa_regex_matches_string(str, "tu")) return 3;

 if (oldgaa_regex_matches_string(str, "We") ||
     oldgaa_regex_matches_string(str, "we")) return 4;

 if (oldgaa_regex_matches_string(str, "Th") ||
     oldgaa_regex_matches_string(str, "th")) return 5;

 if (oldgaa_regex_matches_string(str, "Fr") ||
     oldgaa_regex_matches_string(str, "fr")) return 6;

 if (oldgaa_regex_matches_string(str, "Sa") ||
     oldgaa_regex_matches_string(str, "sa")) return 7;

 return 0;
}


/*****************************************************************************/
static
int
check_day(char *str1, char *str2, char *day)

{
 int val, val1, val2;

 val  = day_to_val(day);  /* current day          */
 val1 = day_to_val(str1); /* first day delimiter  */
 val2 = day_to_val(str2); /* second day delimiter */

 if (!val1) return -1;

 if((val == val1) ||
    (val == val2) ||
    ((val1 < val2) && (val > val1) && (val < val2)) ||
    ((val1 > val2) && val2 && ((val > val1) || (val < val2)))
   ) return 1;

 return 0;

}


/*****************************************************************************/

static
char*
get_value(int *jj, const char *cond, const char delimiter)
 {
  int  i,j  = *jj, length = strlen(cond);
  char *str = NULL;

  str = malloc(length + 1 /* for NUL */);
  if(!str) out_of_memory();

  for(i=0; j <= length; i++)
	{
          str[i] = cond[j];
          j++;
          if((cond[j] == delimiter)) { j++; /* omit delimiter */ break; }
	}

 str[i+1] = NUL; /* terminate the string */
 *jj = j;
 return str;
  }

/*****************************************************************************/