Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / csa / calendar.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: calendar.c /main/1 1996/04/21 19:21:47 drk $ */
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 /*****************************************************************************
32  * Functions that manage the calendar data structures.
33  *****************************************************************************/
34
35 #include <EUSCompat.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <pwd.h>
40 #include "calendar.h"
41 #include "entry.h"
42 #include "cmsdata.h"
43 #include "agent_p.h"
44 #include "rpccalls.h"
45 #include "debug.h"
46 #include "attr.h"
47 #include "xtclient.h"
48 #include "misc.h"
49 #include "free.h"
50 #include "nametbl.h"
51 #include "lutil.h"
52
53 /* linked list of calendar structure representing opened calendars */
54 Calendar *_DtCm_active_cal_list = NULL;
55
56 /******************************************************************************
57  * forward declaration of static functions used within the file
58  ******************************************************************************/
59 static CSA_return_code _get_owner_from_old_cal(Calendar *cal, char *buf);
60 static CSA_return_code _get_calendar_owner(Calendar *cal, uint num_names,
61                                         char **names, CSA_attribute *attr);
62 static CSA_return_code _get_calendar_name(Calendar *cal, CSA_attribute *attr);
63 static CSA_return_code _get_product_identifier(Calendar *cal,
64                                                 CSA_attribute *attr);
65 static CSA_return_code _get_version_supported(Calendar *cal,
66                                                 CSA_attribute *attr);
67 static CSA_return_code _get_server_version(Calendar *cal, CSA_attribute *attr);
68 static CSA_return_code _get_data_version(Calendar *cal, CSA_attribute *attr);
69 static CSA_return_code _get_access_list(Calendar *cal, uint num_names,
70                                         char ** names, CSA_attribute *attr);
71 static CSA_return_code _get_number_entries(Calendar *cal, uint num_names,
72                                         char ** names, CSA_attribute *attr);
73
74 /*****************************************************************************
75  * extern functions used in the library
76  *****************************************************************************/
77
78 /*
79  * Allocate a calendar structure and initialize it with
80  * the location, and name of the calendar.
81  */
82 extern Calendar *
83 _DtCm_new_Calendar(const char *calendar)
84 {
85         Calendar        *cal;
86         int i;
87
88         _DtCm_init_hash();
89
90         if ((cal = (Calendar *)malloc(sizeof(Calendar))) == NULL)
91                 return(NULL);
92
93         memset((void *)cal, NULL, sizeof(Calendar));
94
95         if ((cal->name = strdup(calendar)) == NULL) {
96                 free(cal);
97                 return(NULL);
98         }
99
100         cal->location = strchr(cal->name, '@');
101         cal->location++;
102
103         /* set up attribute array */
104         if ((cal->attrs = (cms_attribute *)calloc(1,
105             sizeof(cms_attribute) * (_DtCm_cal_name_tbl->size + 1))) == NULL) {
106                 free(cal->name);
107                 free(cal);
108                 return(NULL);
109         }
110
111         for (i = 1; i <= _DtCm_cal_name_tbl->size; i++) {
112                 if ((cal->attrs[i].name.name =
113                     strdup(_DtCm_cal_name_tbl->names[i])) == NULL) {
114                         /* clean up */
115                         cal->num_attrs = i - 1;
116                         _DtCm_free_Calendar(cal);
117                         return(NULL);
118                 } else
119                         cal->attrs[i].name.num = i;
120         }
121         cal->num_attrs = _DtCm_cal_name_tbl->size;
122
123         if (_DtCm_active_cal_list == NULL) {
124                 _DtCm_active_cal_list = cal;
125                 cal->next = NULL;
126                 cal->prev = NULL;
127         }
128         else {
129                 cal->next = _DtCm_active_cal_list;
130                 _DtCm_active_cal_list->prev = cal;
131                 _DtCm_active_cal_list = cal;
132         }
133
134         cal->cal_tbl = _DtCm_cal_name_tbl;
135         cal->entry_tbl = _DtCm_entry_name_tbl;
136
137         cal->handle = (void *)cal;
138
139         return(cal);
140 }
141
142 /*
143  * After freeing up memory pointed to by cal,
144  * put it in the free list.
145  */
146 extern void
147 _DtCm_free_Calendar(Calendar *cal)
148 {
149         _DtCmCallbackEntry      *cb, *ptr;
150
151         /* remove from active list */
152         if (_DtCm_active_cal_list == cal)
153                 _DtCm_active_cal_list = cal->next;
154
155         if (cal->prev != NULL)
156                 cal->prev->next = cal->next;
157
158         if (cal->next != NULL)
159                 cal->next->prev = cal->prev;
160
161         /* free up resources used by it */
162         if (cal->name) free(cal->name);
163         if (cal->ehead) _DtCm_free_libentries((_DtCm_libentry *)cal->ehead);
164
165         if (cal->cal_tbl != _DtCm_cal_name_tbl)
166                 _DtCm_free_name_table(cal->cal_tbl);
167
168         if (cal->entry_tbl != _DtCm_entry_name_tbl)
169                 _DtCm_free_name_table(cal->entry_tbl);
170
171         cb = cal->cb_list;
172         while (cb) {
173                 ptr = cb->next;
174                 free(cb);
175                 cb = ptr;
176         }
177
178         /*
179          * no need to free attribute values now
180          * since the values are passed to client
181          * directly.
182          * need to free attribute values if they
183          * are cached later
184          */
185         _DtCm_free_cms_attribute_values(cal->num_attrs, cal->attrs);
186         free(cal->attrs);
187
188         memset((void *)cal, '\0', sizeof(Calendar));
189
190         /* if no calendar is open, destroy rpc mapping */
191         if (_DtCm_active_cal_list == NULL)
192                 _DtCm_destroy_agent();
193 }
194
195 /*
196  * Given the calendar handle, return the internal calendar data structure.
197  */
198 extern Calendar *
199 _DtCm_get_Calendar(CSA_session_handle calhandle)
200 {
201         Calendar *cal = (Calendar *)calhandle;
202
203         if (cal == NULL || cal->handle != (void *)cal)
204                 return (NULL);
205         else
206                 return ((Calendar *)calhandle);
207 }
208
209 /*
210  * Add linked list of entry data structures to the calendar.
211  */
212 extern uint
213 _DtCm_add_to_entry_list(Calendar *cal, caddr_t elist)
214 {
215         int i;
216         _DtCm_libentry *entries = (_DtCm_libentry *)elist;
217
218         if (entries == NULL)
219                 return (0);
220
221         if (cal->ehead == NULL)
222                 cal->ehead = elist;
223         else {
224                 ((_DtCm_libentry *)cal->etail)->next = entries;
225                 entries->prev = (_DtCm_libentry *)cal->etail;
226         }
227
228         /* find tail of given entry list */
229         i = 1;
230         entries->cal = cal;
231         while (entries->next != NULL) {
232                 i++;
233                 entries->next->cal = cal;
234                 entries = entries->next;
235         }
236
237         cal->etail = (caddr_t)entries;
238
239 #ifdef CM_DEBUG
240         _DtCm_count_entry_in_list(cal->ehead);
241 #endif
242
243         return (i);
244 }
245
246 extern void
247 _DtCm_remove_from_entry_list(
248         Calendar *cal,
249         caddr_t head,
250         caddr_t tail)
251 {
252         _DtCm_libentry *ehead = (_DtCm_libentry *)head;
253         _DtCm_libentry *etail = (_DtCm_libentry *)tail;
254
255         if (ehead == NULL || tail == NULL)
256                 return;
257
258         if (ehead == (_DtCm_libentry *)cal->ehead)
259                 cal->ehead = (caddr_t)etail->next;
260
261         if (etail == (_DtCm_libentry *)cal->etail)
262                 cal->etail = (caddr_t)ehead->prev;
263
264         /* remove from list */
265         if (ehead->prev != NULL)
266                 ehead->prev->next = etail->next;
267
268         if (etail->next != NULL)
269                 etail->next->prev = ehead->prev;
270
271         etail->next = ehead->prev = NULL;
272
273 #ifdef CM_DEBUG
274         _DtCm_count_entry_in_list(cal->ehead);
275 #endif
276 }
277
278 extern void
279 _DtCm_reset_cal_attrs(Calendar *cal)
280 {
281         cal->got_attrs = B_FALSE;
282 }
283
284 /*
285  * Assume good parameters.  Caller should check before calling this.
286  */
287 extern CSA_return_code
288 _DtCm_list_old_cal_attr_names(
289         Calendar *cal,
290         CSA_uint32 *num_names_r,
291         char **names_r[])
292 {
293         CSA_return_code stat;
294         char            **names, buf[BUFSIZ];
295         int             i, j;
296
297         if ((names = _DtCm_alloc_character_pointers(_DtCM_OLD_CAL_ATTR_SIZE))
298             == NULL)
299                 return (CSA_E_INSUFFICIENT_MEMORY);
300
301         /* find out whether we know the owner of the calendar */
302         if ((stat = _get_owner_from_old_cal(cal, buf)) != CSA_SUCCESS)
303                 return (stat);
304
305         for (i = 1, j = 0; i <= _DtCM_DEFINED_CAL_ATTR_SIZE; i++) {
306                 if (_CSA_cal_attr_info[i].fst_vers > 0 &&
307                     _CSA_cal_attr_info[i].fst_vers <= cal->file_version) {
308                         if (i == CSA_CAL_ATTR_CALENDAR_OWNER_I && *buf == NULL)
309                                 continue;
310
311                         if ((names[j] =
312                             strdup(_CSA_calendar_attribute_names[i])) == NULL)
313                         {
314                                 _DtCm_free(names);
315                                 return (CSA_E_INSUFFICIENT_MEMORY);
316                         } else
317                                 j++;
318                 }
319         }
320
321         *names_r = names;
322         *num_names_r = j;
323
324         return (CSA_SUCCESS);
325 }
326
327 extern CSA_return_code
328 _DtCm_get_all_cal_attrs(
329         Calendar *cal,
330         CSA_uint32 *num_attrs,
331         CSA_attribute **attrs)
332 {
333         CSA_return_code stat;
334         int             i, j;
335         CSA_attribute   *attrs_r;
336
337         if (num_attrs == NULL || attrs == NULL)
338                 return (CSA_E_INVALID_PARAMETER);
339
340         if (cal->rpc_version >= _DtCM_FIRST_EXTENSIBLE_SERVER_VERSION) {
341                 if ((stat = _DtCm_rpc_get_cal_attrs(cal, 0, 0, NULL))
342                     != CSA_SUCCESS)
343                         return (stat);
344
345                 if ((attrs_r = _DtCm_alloc_attributes(cal->num_attrs)) == NULL)
346                         return (CSA_E_INSUFFICIENT_MEMORY);
347
348                 /* get attributes */
349                 for (i = 1, j = 0; i <= cal->num_attrs; i++) {
350                         if (cal->attrs[i].value != NULL) {
351
352                                 if ((stat = _DtCm_cms2csa_attribute(
353                                     cal->attrs[i], &attrs_r[j])) != CSA_SUCCESS)
354                                 {
355                                         _DtCm_free(attrs_r);
356                                         return (stat);
357                                 } else
358                                         j++;
359                         }
360                 }
361
362                 *num_attrs = j;
363                 *attrs = attrs_r;
364
365                 return (CSA_SUCCESS);
366         } else
367                 return (_DtCm_get_cal_attrs_by_name(cal,
368                         _DtCM_DEFINED_CAL_ATTR_SIZE + 1,
369                         _CSA_calendar_attribute_names, num_attrs, attrs));
370 }
371
372 /*
373  * If it's not found, the attribute value is set to NULL.
374  */
375 extern CSA_return_code
376 _DtCm_get_cal_attrs_by_name(
377         Calendar *cal,
378         CSA_uint32      num_names,
379         CSA_attribute_reference *names,
380         CSA_uint32      *num_attrs,
381         CSA_attribute **attrs)
382 {
383         CSA_return_code stat = CSA_SUCCESS;
384         CSA_attribute *attrs_r;
385         int i, j, index;
386
387         if (num_attrs == 0 || attrs == NULL)
388                 return (CSA_E_INVALID_PARAMETER);
389
390
391         if ((attrs_r = _DtCm_alloc_attributes(num_names)) == NULL)
392                 return (CSA_E_INSUFFICIENT_MEMORY);
393
394         for (i = 0, j = 0; i < num_names; i++) {
395                 if (names[i] == NULL)
396                         continue;
397
398                 index = _DtCm_get_index_from_table(cal->cal_tbl, names[i]);
399
400                 switch (index) {
401                 case CSA_X_DT_CAL_ATTR_SERVER_VERSION_I:
402
403                         stat = _get_server_version(cal, &attrs_r[j]);
404                         break;
405
406                 case CSA_X_DT_CAL_ATTR_DATA_VERSION_I:
407
408                         stat = _get_data_version(cal, &attrs_r[j]);
409                         break;
410
411                 case CSA_CAL_ATTR_ACCESS_LIST_I:
412
413                         stat = _get_access_list(cal, num_names, names,
414                                 &attrs_r[j]);
415                         break;
416
417                 case CSA_CAL_ATTR_NUMBER_ENTRIES_I:
418
419                         stat = _get_number_entries(cal, num_names,
420                                 names, &attrs_r[j]);
421                         break;
422
423                 case CSA_CAL_ATTR_CALENDAR_NAME_I:
424
425                         stat = _get_calendar_name(cal, &attrs_r[j]);
426                         break;
427
428                 case CSA_CAL_ATTR_CALENDAR_OWNER_I:
429
430                         stat = _get_calendar_owner(cal, num_names, names,
431                                 &attrs_r[j]);
432                         break;
433
434                 case CSA_CAL_ATTR_PRODUCT_IDENTIFIER_I:
435
436                         stat = _get_product_identifier(cal, &attrs_r[j]);
437                         break;
438
439                 case CSA_CAL_ATTR_VERSION_I:
440
441                         stat = _get_version_supported(cal, &attrs_r[j]);
442                         break;
443
444                 default:
445                         if (cal->rpc_version >=
446                             _DtCM_FIRST_EXTENSIBLE_SERVER_VERSION)
447                         {
448                                 if (cal->file_version <
449                                     _DtCM_FIRST_EXTENSIBLE_DATA_VERSION &&
450                                     index == -1)
451                                         break;
452
453                                 if (cal->got_attrs == B_FALSE &&
454                                     (stat = _DtCm_rpc_get_cal_attrs(cal, 0,
455                                     num_names, names)) == CSA_SUCCESS) {
456                                         /* there might by new attributes */
457                                         if (index == -1 &&
458                                             (index = _DtCm_get_index_from_table(
459                                             cal->cal_tbl, names[i])) == -1)
460                                                 break;
461                                 }
462
463                                 if (stat == CSA_SUCCESS &&
464                                     cal->attrs[index].value)
465                                         stat = _DtCm_cms2csa_attribute(
466                                                 cal->attrs[index], &attrs_r[j]);
467                         }
468                         break;
469                 }
470
471                 if (stat == CSA_SUCCESS) {
472                         if (attrs_r[j].value != NULL)
473                                 j++;
474                 } else {
475                         _DtCm_free(attrs_r);
476                         return (stat);
477                 }
478         }
479
480         *num_attrs = j;
481
482         if (j > 0) {
483                 *attrs = attrs_r;
484         } else {
485                 *attrs = NULL;
486                 _DtCm_free(attrs_r);
487         }
488
489         return (CSA_SUCCESS);
490 }
491
492 /*
493  * Debugging function - count the number of entries in the list and print it out
494  */
495 extern void
496 _DtCm_count_entry_in_list(caddr_t elist)
497 {
498         int i;
499         _DtCm_libentry *list;
500
501         for (i = 0, list = (_DtCm_libentry *)elist;
502             list != NULL;
503             i++, list = list->next)
504                 ;
505
506         fprintf(stderr, "number of entries = %d\n", i);
507 }
508
509 /******************************************************************************
510  * static functions used within in the file
511  ******************************************************************************/
512
513 /*
514  * owner should point to a buffer big enough to hold the owner name
515  * We test whether the calendar name is a user name, if so, the
516  * owner will be the same as the calendar name.
517  * Otherwise, we don't know the owner.
518  */
519 static CSA_return_code
520 _get_owner_from_old_cal(Calendar *cal, char *owner)
521 {
522         char            *calname;
523
524         if ((calname = _DtCmGetPrefix(cal->name, '@')) == NULL)
525                 return (CSA_E_INSUFFICIENT_MEMORY);
526
527         if (_DtCmIsUserName(calname) == B_TRUE)
528                 strcpy(owner, calname);
529         else
530                 *owner = NULL;
531
532         free(calname);
533         return (CSA_SUCCESS);
534 }
535
536 static CSA_return_code
537 _get_calendar_owner(
538         Calendar        *cal,
539         uint            num_names,
540         char            **names,
541         CSA_attribute   *attr)
542 {
543         CSA_return_code         stat;
544         CSA_attribute_value     *val;
545         char                    *owner, buf[BUFSIZ];
546
547         if (cal->rpc_version >= _DtCM_FIRST_EXTENSIBLE_SERVER_VERSION) {
548                 if (cal->got_attrs == B_FALSE) {
549                         if ((stat = _DtCm_rpc_get_cal_attrs(cal, 0, num_names,
550                             names)) != CSA_SUCCESS)
551                                 return (stat);
552                 }
553                 owner = cal->attrs[CSA_CAL_ATTR_CALENDAR_OWNER_I].value->\
554                         item.string_value;
555         } else {
556                 if ((stat = _get_owner_from_old_cal(cal, buf)) != CSA_SUCCESS)
557                         return (stat);
558                 else if (*buf == NULL)
559                         return (CSA_SUCCESS);
560                 else
561                         owner = buf;
562         }
563
564         if (attr->name = strdup(CSA_CAL_ATTR_CALENDAR_OWNER)) {
565                 if ((val = (CSA_attribute_value *)calloc(1,
566                     sizeof(CSA_attribute_value))) == NULL) {
567                         free(attr->name);
568                         return (CSA_E_INSUFFICIENT_MEMORY);
569                 }
570
571                 val->type = CSA_VALUE_CALENDAR_USER;
572
573                 if ((val->item.calendar_user_value = (CSA_calendar_user *)
574                     calloc(1, sizeof(CSA_calendar_user))) == NULL) {
575                         free(val);
576                         free(attr->name);
577                         return (CSA_E_INSUFFICIENT_MEMORY);
578                 }
579
580                 if (val->item.calendar_user_value->user_name = strdup(owner)) {
581                         attr->value = val;
582                         return (CSA_SUCCESS);
583                 } else {
584                         free(val->item.calendar_user_value);
585                         free(val);
586                         free(attr->name);
587                         return (CSA_E_INSUFFICIENT_MEMORY);
588                 }
589         } else
590                 return (CSA_E_INSUFFICIENT_MEMORY);
591 }
592
593 static CSA_return_code
594 _get_calendar_name(Calendar *cal, CSA_attribute *attr)
595 {
596         if (attr->name = strdup(CSA_CAL_ATTR_CALENDAR_NAME))
597                 return (_DtCm_set_csa_string_attrval(cal->name, &attr->value,
598                         CSA_VALUE_STRING));
599         else
600                 return (CSA_E_INSUFFICIENT_MEMORY);
601 }
602
603 static CSA_return_code
604 _get_product_identifier(Calendar *cal, CSA_attribute *attr)
605 {
606         if (attr->name = strdup(CSA_CAL_ATTR_PRODUCT_IDENTIFIER))
607                 return (_DtCm_set_csa_string_attrval(_DtCM_PRODUCT_IDENTIFIER,
608                         &attr->value, CSA_VALUE_STRING));
609         else
610                 return (CSA_E_INSUFFICIENT_MEMORY);
611 }
612
613 static CSA_return_code
614 _get_version_supported(Calendar *cal, CSA_attribute *attr)
615 {
616         char buf[10];
617
618         if (attr->name = strdup(CSA_CAL_ATTR_VERSION))
619                 return (_DtCm_set_csa_string_attrval(
620                         _DtCM_SPEC_VERSION_SUPPORTED, &attr->value,
621                         CSA_VALUE_STRING));
622         else
623                 return (CSA_E_INSUFFICIENT_MEMORY);
624 }
625
626 static CSA_return_code
627 _get_server_version(Calendar *cal, CSA_attribute *attr)
628 {
629         if (attr->name = strdup(CSA_X_DT_CAL_ATTR_SERVER_VERSION))
630                 return (_DtCm_set_csa_uint32_attrval(cal->rpc_version,
631                         &attr->value));
632         else
633                 return (CSA_E_INSUFFICIENT_MEMORY);
634 }
635
636 static CSA_return_code
637 _get_data_version(Calendar *cal, CSA_attribute *attr)
638 {
639         if (attr->name = strdup(CSA_X_DT_CAL_ATTR_DATA_VERSION))
640                 return (_DtCm_set_csa_uint32_attrval(cal->file_version,
641                         &attr->value));
642         else
643                 return (CSA_E_INSUFFICIENT_MEMORY);
644 }
645
646 static CSA_return_code
647 _get_access_list(
648         Calendar        *cal,
649         uint            num_names,
650         char            **names,
651         CSA_attribute   *attr)
652 {
653         CSA_return_code stat = CSA_SUCCESS;
654
655         if (cal->rpc_version < _DtCM_FIRST_EXTENSIBLE_SERVER_VERSION)
656                 stat = _DtCm_rpc_get_cal_attrs(cal, CSA_CAL_ATTR_ACCESS_LIST_I,
657                         0, NULL);
658         else if (cal->got_attrs == B_FALSE)
659                 stat = _DtCm_rpc_get_cal_attrs(cal, 0, num_names, names);
660
661         if (stat == CSA_SUCCESS)
662                 stat = _DtCm_cms2csa_attribute(
663                         cal->attrs[CSA_CAL_ATTR_ACCESS_LIST_I], attr);
664
665         return (stat);
666 }
667
668 static CSA_return_code
669 _get_number_entries(
670         Calendar        *cal,
671         uint            num_names,
672         char            **names,
673         CSA_attribute   *attr)
674 {
675         CSA_return_code stat = CSA_SUCCESS;
676
677         if (cal->rpc_version < _DtCM_FIRST_EXTENSIBLE_SERVER_VERSION)
678                 stat = _DtCm_rpc_get_cal_attrs(cal,
679                         CSA_CAL_ATTR_NUMBER_ENTRIES_I, 0, NULL);
680         else if (cal->got_attrs == B_FALSE)
681                 stat = _DtCm_rpc_get_cal_attrs(cal, 0, num_names, names);
682
683         if (stat == CSA_SUCCESS)
684                 stat = _DtCm_cms2csa_attribute(
685                         cal->attrs[CSA_CAL_ATTR_NUMBER_ENTRIES_I], attr);
686
687         return (stat);
688 }
689