dtcm: Resolve CID 87822
[oweals/cde.git] / cde / programs / dtcm / server / lookup.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: lookup.c /main/4 1995/11/09 12:46:28 rswiston $ */
24 /*
25  *  (c) Copyright 1993, 1994 Hewlett-Packard Company
26  *  (c) Copyright 1993, 1994 International Business Machines Corp.
27  *  (c) Copyright 1993, 1994 Novell, Inc.
28  *  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
29  */
30
31 #include <EUSCompat.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <signal.h>
36 #include <sys/stat.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <pwd.h>
40 #include <time.h>
41 #if !defined(CSRG_BASED)
42 #include <values.h>
43 #endif
44 #ifdef SunOS
45 #include <sys/systeminfo.h>
46 #endif
47 #include "attr.h"
48 #include "cmscalendar.h"
49 #include "lookup.h"
50 #include "cm.h"
51 #include "tree.h"
52 #include "list.h"
53 #include "iso8601.h"
54 #include "rerule.h"
55 #include "reutil.h"
56 #include "access.h"
57 #include "cmsmatch.h"
58 #include "cmsdata.h"
59 #include "cmsentry.h"
60 #include "repeat.h"
61 #include "v5ops.h"
62 #include "misc.h"
63
64 #define TIME_BUF_LEN    20
65
66 /******************************************************************************
67  * forward declaration of static functions used within the file
68  ******************************************************************************/
69 static CSA_return_code _AddToLinkedEntries(cms_entry *eptr, cms_entry **head,
70                                         cms_entry **tail, boolean_t sort, boolean_t time_only);
71 static CSA_return_code _EnumerateSequence(char *sender, uint access,
72                                         List_node *lnode, time_t start1,
73                                         time_t start2,
74                                         boolean_t no_end_time_range, time_t end1,
75                                         time_t end2, CSA_uint32 num_attrs,
76                                         cms_attribute *attrs, CSA_enum *ops,
77                                         cms_entry **head, cms_entry **tail);
78 static CSA_return_code _GetEntryAttrsByName(_DtCmsCalendar *cal,
79                                         cms_entry *entry, uint num_names,
80                                         cms_attr_name *names, uint *num_attrs_r,
81                                         cms_attribute **attrs_r);
82 static CSA_return_code _GetAllEntryAttrs(cms_entry *entry, uint *num_attrs_r,
83                                         cms_attribute **attrs_r);
84
85 /*****************************************************************************
86  * extern functions used in the library
87  *****************************************************************************/
88
89 extern CSA_return_code
90 _DtCmsLookupEntriesById(
91         _DtCmsCalendar  *cal,
92         char            *sender,
93         uint            access,
94         boolean_t       no_start_time_range,
95         boolean_t       no_end_time_range,
96         time_t          start1,
97         time_t          start2,
98         time_t          end1,
99         time_t          end2,
100         long            id,
101         CSA_uint32      num_attrs,
102         cms_attribute   *attrs,
103         CSA_enum        *ops,
104         cms_entry       **entries)
105 {
106         CSA_return_code stat = CSA_SUCCESS;
107         CSA_return_code stat2 = CSA_SUCCESS;
108         cms_entry       *eptr, *head = NULL, *tail = NULL;
109         cms_key         key;
110         time_t          endtick;
111         cms_attribute   *aptr;
112
113         if (no_start_time_range) {
114                 start1 = _DtCM_BOT;
115                 start2 = _DtCM_EOT;
116         }
117
118         /* do lookup on repeating entries first */      
119         stat = _DtCmsEnumerateSequenceById(cal, sender, access,
120                 no_start_time_range, no_end_time_range, start1, start2,
121                 end1, end2, id, num_attrs, attrs, ops, &head);
122         
123         if (stat != CSA_X_DT_E_ENTRY_NOT_FOUND) {
124                 if (stat == CSA_SUCCESS)
125                         *entries = head;
126                 return (stat);
127         }
128
129         key.time = start1;
130         key.id = id;
131         *entries = NULL;
132         while ((eptr = (cms_entry *)rb_lookup_next_larger(cal->tree,
133             (caddr_t)&key)) && eptr->key.time < start2) {
134                 if (eptr->key.id != id) {
135                         key.time = eptr->key.time;
136                         key.id = eptr->key.id;
137                 } else {
138                         aptr = &eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I];
139                         if (!no_end_time_range) {
140                                 if (aptr->value == NULL)
141                                         return (CSA_SUCCESS);
142
143                                 _csa_iso8601_to_tick(
144                                         aptr->value->item.date_time_value,
145                                         &endtick);
146
147                                 if (endtick <= end1 || endtick >= end2)
148                                         return (CSA_SUCCESS);
149                         }
150                         
151                         if (_DtCmsMatchAttributes(eptr, num_attrs, attrs, ops)
152                             && ( ((stat2 = _DtCmsCheckViewAccess(sender, access, eptr)) == CSA_SUCCESS) || (stat2 == CSA_E_TIME_ONLY) ) ) {
153                                 stat = _AddToLinkedEntries(eptr, &head, &tail,
154                                         B_FALSE, B_FALSE);
155                         }
156                         *entries = head;
157                         return (stat);
158                 }
159         }
160
161         return (CSA_SUCCESS);
162 }
163
164 extern CSA_return_code
165 _DtCmsLookupEntries(
166         _DtCmsCalendar  *cal,
167         char            *sender,
168         uint            access,
169         time_t          start1,
170         time_t          start2,
171         boolean_t       no_end_time_range,
172         time_t          end1,
173         time_t          end2,
174         CSA_uint32      num_attrs,
175         cms_attribute   *attrs,
176         CSA_enum        *ops,
177         cms_entry       **entries)
178 {
179         CSA_return_code stat = CSA_SUCCESS;
180         CSA_return_code stat2 = CSA_SUCCESS;
181         cms_entry       *eptr, *head = NULL, *tail = NULL;
182         cms_key         key;
183         List_node       *lnode;
184         time_t          endtick;
185         cms_attribute   *aptr;
186
187         /* do lookup on one-time entries first */
188         key.time = start1;
189         key.id = 0;
190         while ((eptr = (cms_entry *)rb_lookup_next_larger(cal->tree,
191             (caddr_t)&key)) && eptr->key.time < start2) {
192
193                 aptr = &eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I];
194
195                 if (!no_end_time_range) {
196                         if (aptr->value == NULL)
197                                 goto nextone;
198
199                         _csa_iso8601_to_tick(aptr->value->item.date_time_value,
200                                 &endtick);
201
202                         if (endtick <= end1 || endtick >= end2)
203                                 goto nextone;
204                 }
205
206                 if (_DtCmsMatchAttributes(eptr, num_attrs, attrs, ops) &&
207                     _DtCmsCheckViewAccess(sender, access, eptr) == CSA_SUCCESS)
208                 {
209                         /* the last argument "sort" is set to B_FALSE,
210                          * because the entries are in order already
211                          * as we get them out from the tree;
212                          * we don't want to sort it again
213                          */
214                         if ((stat = _AddToLinkedEntries(eptr, &head, &tail,
215                             B_FALSE, B_FALSE)) != CSA_SUCCESS)
216                                 break;
217                 }
218
219 nextone:
220                 key.time = eptr->key.time;
221                 key.id = eptr->key.id;
222         }
223
224         /* do lookup on repeating entries */    
225         lnode = cal->list->root;
226         while (lnode != NULL && stat == CSA_SUCCESS) {
227
228                 stat = _EnumerateSequence(sender, access, lnode, start1, start2,
229                         no_end_time_range, end1, end2, num_attrs, attrs,
230                         ops, &head, &tail);
231
232                 lnode = hc_lookup_next(lnode);
233         }
234
235         if (stat == CSA_SUCCESS)
236                 *entries = head;
237         else if (head)
238                 _DtCm_free_cms_entries(head);
239
240         return (stat);
241 }
242
243 extern CSA_return_code
244 _DtCmsEnumerateSequenceById(
245         _DtCmsCalendar  *cal,
246         char            *sender,
247         uint            access,
248         boolean_t       no_start_time_range,
249         boolean_t       no_end_time_range,
250         time_t          start1,
251         time_t          start2,
252         time_t          end1,
253         time_t          end2,
254         long            id,
255         CSA_uint32      num_attrs,
256         cms_attribute   *attrs,
257         CSA_enum        *ops,
258         cms_entry       **entries)
259 {
260         CSA_return_code stat;
261         cms_entry       *head = NULL, *tail = NULL;
262         cms_key         key;
263         List_node       *lnode;
264
265         key.id = id;
266         lnode = hc_lookup_node(cal->list, (caddr_t)&key);
267         if (lnode == NULL)
268                 return (CSA_X_DT_E_ENTRY_NOT_FOUND);
269
270         if ( ((stat = _DtCmsCheckViewAccess(sender, access,
271             (cms_entry *)lnode->data)) != CSA_SUCCESS) && (stat != CSA_E_TIME_ONLY) )
272                 return (stat);
273
274         *entries = NULL;
275         if (no_start_time_range && no_end_time_range) {
276                 if (_DtCmsMatchAttributes((cms_entry *)lnode->data, num_attrs,
277                     attrs, ops)) {
278                         stat = _DtCmsGetCmsEntryForClient(
279                                 (cms_entry *)lnode->data, entries,B_FALSE);
280                 }
281         } else {
282                 stat = _EnumerateSequence(sender, access, lnode, start1, start2,
283                         no_end_time_range, end1, end2, num_attrs, attrs, ops,
284                         &head, &tail);
285
286                 if (stat == CSA_SUCCESS)
287                         *entries = head;
288                 else if (head)
289                         _DtCm_free_cms_entries(head);
290         }
291
292         return (stat);
293 }
294
295 extern CSA_return_code
296 _DtCmsLookupEntriesByKey(
297         _DtCmsCalendar  *cal,
298         char            *user,
299         uint            access,
300         uint            num_keys,
301         cms_key         *keys,
302         uint            num_names,
303         cms_attr_name   *names,
304         cms_get_entry_attr_res_item **res)
305 {
306         CSA_return_code                 stat = CSA_SUCCESS;
307         cms_get_entry_attr_res_item     *eptr, *head;
308         int                             i;
309
310         for (i = 0, head = NULL; i < num_keys; i++) {
311                 if ((stat = _DtCmsGetEntryAttrByKey(cal, user, access, keys[i],
312                     num_names, names, NULL, &eptr)) == CSA_SUCCESS) {
313                         eptr->next = head;
314                         head = eptr;
315                 } else {
316                         if (head) _DtCmsFreeEntryAttrResItem(head);
317                         return (stat);
318                 }
319         }
320
321         *res = head;
322         return (CSA_SUCCESS);
323 }
324
325 /*
326  * this routine returns either a cms_entry or 
327  * an cms_get_entry_attr_res_item structure
328  * depending which output argument is not null
329  */
330 extern CSA_return_code
331 _DtCmsGetEntryAttrByKey(
332         _DtCmsCalendar  *cal,
333         char            *user,
334         uint            access,
335         cms_key         key,
336         uint            num_names,
337         cms_attr_name   *names,
338         cms_entry       **entry_r,
339         cms_get_entry_attr_res_item **res_r)
340 {
341         CSA_return_code stat;
342         CSA_return_code stat2;
343         cms_entry       *entry = NULL;
344         char            *stime, *etime;
345         char            sbuf[TIME_BUF_LEN], ebuf[TIME_BUF_LEN];
346         time_t          firsttick = 0;
347         List_node       *lnode;
348         cms_get_entry_attr_res_item *res;
349
350         if (entry_r == NULL && res_r == NULL)
351                 return (CSA_E_INVALID_PARAMETER);
352
353         if (res_r && (res = (cms_get_entry_attr_res_item *)calloc(1,
354             sizeof(cms_get_entry_attr_res_item))) == NULL)
355                 return (CSA_E_INSUFFICIENT_MEMORY);
356
357         /* do lookup on one-time entries first */
358         if ((entry = (cms_entry *)rb_lookup(cal->tree, (caddr_t)&key)) == NULL)
359         {
360                 if ((lnode = (List_node *)hc_lookup_node(cal->list,
361                     (caddr_t)&key)) != NULL)
362                 {
363                         entry = (cms_entry *)lnode->data;
364                         if (_DtCmsInExceptionList(entry, key.time))
365                                 entry = NULL;
366                 }
367         }
368
369         if ( (entry == NULL) || ( ((stat2 = _DtCmsCheckViewAccess(user, access, entry)) != CSA_SUCCESS) && (stat2 != CSA_E_TIME_ONLY)) ) {
370                 if (entry == NULL)
371                         stat = CSA_X_DT_E_ENTRY_NOT_FOUND;
372                 else
373                         stat = CSA_E_NO_AUTHORITY;
374
375                 if (entry_r)
376                         return (stat);
377                 else {
378                         res->stat = stat;
379                         *res_r = res;
380                         return (CSA_SUCCESS);
381                 }
382         }
383
384         if (entry->key.time != key.time) {
385                 /* set start and end time of the instance */
386                 firsttick = entry->key.time;
387                 entry->key.time = key.time;
388
389                 stime = entry->attrs[CSA_ENTRY_ATTR_START_DATE_I].value->\
390                         item.date_time_value;
391                 _csa_tick_to_iso8601(key.time, sbuf);
392                 entry->attrs[CSA_ENTRY_ATTR_START_DATE_I].value->\
393                         item.date_time_value = sbuf;
394
395                 if (entry->attrs[CSA_ENTRY_ATTR_END_DATE_I].value) {
396                         etime = entry->attrs[CSA_ENTRY_ATTR_END_DATE_I].\
397                                 value->item.date_time_value;
398                         _csa_tick_to_iso8601(key.time + lnode->duration, ebuf);
399                         entry->attrs[CSA_ENTRY_ATTR_END_DATE_I].value->\
400                                 item.date_time_value = ebuf;
401                 }
402         }
403
404         if (entry_r)
405                 stat = _DtCmsGetCmsEntryForClient(entry, entry_r,B_FALSE);
406         else {
407                 if (num_names == 0)
408                         stat = _GetAllEntryAttrs(entry, &res->num_attrs,
409                                 &res->attrs);
410                 else
411                         stat = _GetEntryAttrsByName(cal, entry, num_names,
412                                 names, &res->num_attrs, &res->attrs);
413
414                 if (stat == CSA_SUCCESS) {
415                         res->key = key;
416                         *res_r = res;
417                 } else
418                         free(res);
419         }
420
421         if (firsttick > 0) {
422                 entry->key.time = firsttick;
423                 entry->attrs[CSA_ENTRY_ATTR_START_DATE_I].value->\
424                         item.date_time_value = stime;
425                 if (entry->attrs[CSA_ENTRY_ATTR_END_DATE_I].value)
426                         entry->attrs[CSA_ENTRY_ATTR_END_DATE_I].value->\
427                                 item.date_time_value = etime;
428         }
429
430         return (stat);
431 }
432
433 /*****************************************************************************
434  * static functions used within the file
435  *****************************************************************************/
436
437 static CSA_return_code
438 _AddToLinkedEntries(
439         cms_entry       *eptr,
440         cms_entry       **head,
441         cms_entry       **tail,
442         boolean_t       sort,
443         boolean_t       time_only)
444 {
445         CSA_return_code stat;
446         cms_entry       *newptr, *prev, *ptr;
447
448         if ((stat = _DtCmsGetCmsEntryForClient(eptr, &newptr,time_only)) != CSA_SUCCESS)
449                 return (stat);
450
451         if (sort == B_FALSE) {
452                 /* just add to the end of the list */
453
454                 if ((*head) == NULL)
455                         *head = newptr;
456                 else
457                         (*tail)->next = newptr;
458
459                 *tail = newptr;
460         } else {
461                 /* add item in ascending order */
462                 for (prev = NULL, ptr = *head; ptr != NULL;
463                     prev = ptr, ptr = ptr->next) {
464                         if (eptr->key.time <= ptr->key.time)
465                                 break;
466                 }
467
468                 newptr->next = ptr;
469                 if (prev == NULL)
470                         *head = newptr;
471                 else
472                         prev->next = newptr;
473
474                 if ((*tail) == prev)
475                         *tail = newptr;
476         }
477
478         return (CSA_SUCCESS);
479 }
480
481 static CSA_return_code
482 _EnumerateSequence(
483         char            *sender,
484         uint            access,
485         List_node       *lnode,
486         time_t          start1,
487         time_t          start2,
488         boolean_t       no_end_time_range,
489         time_t          end1,
490         time_t          end2,
491         CSA_uint32      num_attrs,
492         cms_attribute   *attrs,
493         CSA_enum        *ops,
494         cms_entry       **head,
495         cms_entry       **tail)
496 {
497         CSA_return_code stat = CSA_SUCCESS;
498         CSA_return_code stat2 = CSA_SUCCESS;
499         cms_entry       *eptr;
500         time_t          fsttick, tick;
501         RepeatEventState *restate;
502
503         eptr = (cms_entry *)lnode->data;
504         fsttick = eptr->key.time;
505
506         if (lnode->lasttick == 0) {
507                 lnode->lasttick = LastTick(fsttick, lnode->re);
508                 lnode->duration = _DtCmsGetDuration(eptr);
509         }
510
511         if (lnode->lasttick <= start1 || fsttick >= start2 ||
512             (!no_end_time_range &&
513              eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I].value == NULL) ||
514             (!no_end_time_range && (lnode->lasttick+lnode->duration) <= end1) ||
515             (!no_end_time_range && (fsttick + lnode->duration) >= end2) ||
516             _DtCmsCheckViewAccess(sender, access, eptr) ||
517             !_DtCmsMatchAttributes(eptr, num_attrs, attrs, ops))
518                 return (CSA_SUCCESS);
519
520         if (!no_end_time_range && start1 < (end1 - lnode->duration))
521                 start1 = end1 - lnode->duration;
522
523         if (!no_end_time_range && ((end2 - lnode->duration) < start2))
524                 start2 = end2 - lnode->duration;
525
526         for (tick = ClosestTick(start1, fsttick, lnode->re, &restate);
527             stat == CSA_SUCCESS && tick < start2;
528             tick = NextTick(tick, fsttick, lnode->re, restate))
529         {
530                 char *stime, *etime;
531                 char sbuf[TIME_BUF_LEN], ebuf[TIME_BUF_LEN];
532
533                 if (tick <= 0 || tick > lnode->lasttick)
534                         break;
535
536                 if (tick <= start1 || _DtCmsInExceptionList(eptr, tick))
537                         continue;
538
539                 /* set start and end time of the instance */
540                 eptr->key.time = tick;
541
542                 stime = eptr->attrs[CSA_ENTRY_ATTR_START_DATE_I].value->\
543                         item.date_time_value;
544                 _csa_tick_to_iso8601(tick, sbuf);
545                 eptr->attrs[CSA_ENTRY_ATTR_START_DATE_I].value->\
546                         item.date_time_value = sbuf;
547
548                 if (eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I].value) {
549                         etime = eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I].\
550                                 value->item.date_time_value;
551                         _csa_tick_to_iso8601(tick + lnode->duration, ebuf);
552                         eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I].value->\
553                                 item.date_time_value = ebuf;
554                 }
555
556                 stat = _AddToLinkedEntries(eptr, head, tail, B_TRUE,B_FALSE);
557
558                 eptr->key.time = fsttick;
559                 eptr->attrs[CSA_ENTRY_ATTR_START_DATE_I].value->\
560                         item.date_time_value = stime;
561                 if (eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I].value)
562                         eptr->attrs[CSA_ENTRY_ATTR_END_DATE_I].value->\
563                                 item.date_time_value = etime;
564         }
565
566         return (stat);
567 }
568
569 static CSA_return_code
570 _GetEntryAttrsByName(
571         _DtCmsCalendar  *cal,
572         cms_entry       *entry,
573         uint            num_names,
574         cms_attr_name   *names,
575         uint            *num_attrs_r,
576         cms_attribute   **attrs_r)
577 {
578         CSA_return_code stat = CSA_SUCCESS;
579         cms_attribute   *attrs;
580         int             i;
581
582         if ((attrs = calloc(1, sizeof(cms_attribute)*num_names)) == NULL)
583                 return (CSA_E_INSUFFICIENT_MEMORY);
584
585         /* return all attrs names with hash number
586          * so that caller knows which one does not
587          * have value and which one does not even exist
588          */
589         for (i = 0; i < num_names && stat == CSA_SUCCESS; i++) {
590                 if (names[i].name == NULL)
591                         continue;
592
593                 if (names[i].num <= 0)
594                         names[i].num = _DtCm_get_index_from_table(
595                                         cal->entry_tbl, names[i].name);
596
597                 if (names[i].num > 0) {
598                         stat = _DtCm_copy_cms_attribute(&attrs[i],
599                             &entry->attrs[names[i].num], B_TRUE);
600                 } else {
601                         attrs[i].name.num = -1;
602                         if ((attrs[i].name.name = strdup(names[i].name))==NULL)
603                                 stat = CSA_E_INSUFFICIENT_MEMORY;
604                 }
605         }
606
607         if (stat == CSA_SUCCESS) {
608                 *num_attrs_r = num_names;
609                 *attrs_r = attrs;
610         } else {
611                 _DtCm_free_cms_attributes(i, attrs);
612                 free(attrs);
613         }
614
615         return (stat);
616 }
617
618 static CSA_return_code
619 _GetAllEntryAttrs(
620         cms_entry       *entry,
621         uint            *num_attrs_r,
622         cms_attribute   **attrs_r)
623 {
624         CSA_return_code stat = CSA_SUCCESS;
625         cms_attribute   *attrs;
626         int             i, j;
627
628         if ((attrs = calloc(1, sizeof(cms_attribute)*entry->num_attrs)) == NULL)
629                 return (CSA_E_INSUFFICIENT_MEMORY);
630
631         /* first element is not used */
632         for (i = 1, j = 0; i <= entry->num_attrs && stat == CSA_SUCCESS; i++) {
633                 if (entry->attrs[i].value) {
634                         if ((stat = _DtCm_copy_cms_attribute(&attrs[j],
635                             &entry->attrs[i], B_TRUE))
636                             == CSA_SUCCESS)
637                         {
638                                 j++;
639                         }
640                 }
641         }
642
643         if (stat == CSA_SUCCESS) {
644                 if (j > 0) {
645                         *num_attrs_r = j;
646                         *attrs_r = attrs;
647                 } else {
648                         *num_attrs_r = 0;
649                         *attrs_r = NULL;
650                         free(attrs);
651                 }
652         } else {
653                 _DtCm_free_cms_attributes(j, attrs);
654                 free(attrs);
655         }
656
657         return (stat);
658 }
659
660