libDtSearch: Coverity 86579
[oweals/cde.git] / cde / lib / csa / api.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 libraries 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: api.c /main/1 1996/04/21 19:21:31 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  * Implements the calendar manager API functions.
33  */
34
35 #include <EUSCompat.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <pwd.h>
40 #ifdef SunOS
41 #include <stropts.h>
42 #endif
43 #include "csa.h"
44 #include "agent_p.h"
45 #include "entry.h"
46 #include "rpccalls.h"
47 #include "connection.h"
48 #include "debug.h"
49 #include "attr.h"
50 #include "xtclient.h"
51 #include "misc.h"
52 #include "free.h"
53 #include "iso8601.h"
54 #include "match.h"
55
56 /******************************************************************************
57  * forward declaration of static functions used within the file
58  ******************************************************************************/
59 static CSA_return_code _handle_register_callback_ext(CSA_extension *ext);
60 static CSA_return_code _handle_logon_ext(CSA_extension *ext,
61                                         CSA_extension **pext);
62 static CSA_return_code _handle_query_config_ext(CSA_extension *ext);
63 static void _handle_com_support_ext(CSA_extension *ext);
64
65 /******************************************************************************
66  * Calendar Manager API
67  ******************************************************************************/
68
69 /*
70  * List calendars supported by a server
71  */
72 extern CSA_return_code
73 csa_list_calendars(
74         CSA_service_reference   calendar_service,
75         CSA_uint32      *number_names,
76         CSA_calendar_user       **calendar_names,
77         CSA_extension   *list_calendars_extensions)
78 {
79         DP(("api.c: csa_list_calendars\n"));
80
81         if (calendar_service == NULL || number_names == NULL ||
82             calendar_names == NULL)
83                 return (CSA_E_INVALID_PARAMETER);
84
85         /* no function extension is supported */
86         if (list_calendars_extensions != NULL)
87                 return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
88
89         return (_DtCm_rpc_list_calendars(calendar_service, number_names,
90                 calendar_names));
91 }
92
93 /*
94  * Logon to a calendar.
95  * Returns a calendar session handle.
96  *
97  * arguments not used by this implementation:
98  * calendar_servce, password, character_set, caller_csa_version,
99  * logon_extensions
100  * 
101  * arguments ignored for now
102  * character_set, caller_csa_version
103  *
104  * user - only the calendar_address field is used, other fields are ignored
105  *      - format is calendar_name@host
106  */
107 extern CSA_return_code
108 csa_logon(
109         CSA_service_reference   calendar_service,
110         CSA_calendar_user       *user,
111         CSA_string              password,
112         CSA_string              character_set,
113         CSA_string              caller_csa_version,
114         CSA_session_handle      *session,
115         CSA_extension           *logon_extensions)
116 {
117         CSA_return_code stat;
118         Calendar        *cal;
119         CSA_extension   *pext = NULL;
120
121         DP(("api.c: csa_logon\n"));
122
123         /* check validity of arguments */
124         if (user == NULL || user->calendar_address == NULL || session == NULL
125            || strchr(user->calendar_address, '@') == NULL)
126                 return (CSA_E_INVALID_PARAMETER);
127
128         /* create calendar object */
129         if ((cal = _DtCm_new_Calendar(user->calendar_address)) == NULL) {
130                 return (CSA_E_INSUFFICIENT_MEMORY);
131         }
132
133         if (logon_extensions != NULL) {
134                 if ((stat = _handle_logon_ext(logon_extensions, &pext))
135                     != CSA_SUCCESS)
136                         return (stat);
137         }
138
139         /* open calendar */
140         if ((stat = _DtCm_rpc_open_calendar(cal)) == CSA_SUCCESS) {
141
142                 if (pext) pext->item_data = cal->access;
143
144                 *session = (CSA_session_handle)cal;
145
146         } else {
147                 _DtCm_free_Calendar(cal);
148         }
149
150         return (stat);
151 }
152
153 /*
154  * Create a calendar.
155  *
156  * arguments not used by this implementation:
157  * session - always ignored
158  * add_calendar_extensions
159  * 
160  * user - only the calendar_address field is used, other fields are ignored
161  *      - format is calendar_name@host
162  */
163 extern CSA_return_code
164 csa_add_calendar(
165         CSA_session_handle      session,
166         CSA_calendar_user       *user,
167         CSA_uint32              number_attributes,
168         CSA_attribute           *calendar_attributes,
169         CSA_extension           *add_calendar_extensions)
170 {
171         CSA_return_code stat;
172         Calendar        *cal;
173         char            *host;
174
175         DP(("api.c: csa_add_calendar\n"));
176
177         /* check validity of arguments */
178         if (user == NULL || user->calendar_address == NULL ||
179             (host = strchr(user->calendar_address, '@')) == NULL)
180                 return (CSA_E_INVALID_PARAMETER);
181
182         /* check add_calendar_extensions and return appropriate return code */
183         /* no function extension is supported */
184         if (add_calendar_extensions != NULL)
185                 return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
186
187         /* create calendar object */
188         if ((cal = _DtCm_new_Calendar(user->calendar_address)) == NULL) {
189                 return (CSA_E_INSUFFICIENT_MEMORY);
190         }
191
192         host++;
193         if ((stat = _DtCm_get_server_rpc_version(host, &cal->rpc_version))
194             != CSA_SUCCESS)
195                 return (stat);
196
197         /* check validity of attributes */
198         if (number_attributes > 0 &&
199             (stat = _DtCm_check_cal_csa_attributes(cal->rpc_version - 1,
200             number_attributes, calendar_attributes, user->calendar_address,
201             B_TRUE, B_TRUE, B_FALSE)) != CSA_SUCCESS) {
202                 return (stat);
203         }
204
205         /* create calendar */
206         stat = _DtCm_rpc_create_calendar(cal, number_attributes,
207                 calendar_attributes);
208
209         _DtCm_free_Calendar(cal);
210
211         return (stat);
212 }
213
214 /*
215  * Logoff a calendar.
216  * The calendar handle becomes invalid
217  *
218  * argument not used by this implementation:
219  * logoff_extensions
220  */
221 extern CSA_return_code
222 csa_logoff(
223         CSA_session_handle session,
224         CSA_extension *logoff_extensions)
225 {
226         Calendar        *cal;
227
228         DP(("api.c: csa_logoff\n"));
229
230         /* get calendar object */
231         if ((cal = _DtCm_get_Calendar(session)) == NULL)
232                 return (CSA_E_INVALID_SESSION_HANDLE);
233
234         /* check logoff_extensions and return appropriate return code */
235         /* no function extension is supported */
236         if (logoff_extensions != NULL)
237                 return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
238
239         /* unregister with server */
240         if (cal->all_reasons)
241                 (void) _DtCm_rpc_unregister_client(cal, cal->all_reasons);
242
243         /* clean up */
244         _DtCm_free_Calendar(cal);
245
246         return (CSA_SUCCESS);
247 }
248
249 /*
250  * delete calendar
251  */
252 extern CSA_return_code
253 csa_delete_calendar(
254         CSA_session_handle      session,
255         CSA_extension   *delete_calendar_extensions)
256 {
257         Calendar        *cal;
258
259         DP(("api.c: csa_delete_calendar\n"));
260
261         /* get calendar object */
262         if ((cal = _DtCm_get_Calendar(session)) == NULL)
263                 return (CSA_E_INVALID_SESSION_HANDLE);
264
265         /* check extensions and return appropriate return code */
266         /* no function extension is supported */
267         if (delete_calendar_extensions != NULL)
268                 return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
269
270         if (cal->rpc_version < 5)
271                 return (CSA_E_NOT_SUPPORTED);
272
273         return (_DtCm_rpc_delete_calendar(cal));
274 }
275
276 extern CSA_return_code
277 csa_list_calendar_attributes(
278         CSA_session_handle      session,
279         CSA_uint32      *number_names,
280         CSA_attribute_reference **calendar_attributes_names,
281         CSA_extension   *list_calendar_attributes_extensions)
282 {
283         Calendar        *cal;
284
285         DP(("api.c: csa_list_calendar_attributes\n"));
286
287         if (number_names == NULL || calendar_attributes_names == NULL)
288                 return (CSA_E_INVALID_PARAMETER);
289
290         /* no function extension is supported */
291         if (list_calendar_attributes_extensions != NULL)
292                 return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
293
294         /* get calendar object */
295         if ((cal = _DtCm_get_Calendar(session)) == NULL)
296                 return (CSA_E_INVALID_SESSION_HANDLE);
297
298         if (cal->rpc_version < _DtCM_FIRST_EXTENSIBLE_SERVER_VERSION) {
299                 return (_DtCm_list_old_cal_attr_names(cal, number_names,
300                         calendar_attributes_names));
301         } else
302                 return (_DtCm_rpc_list_calendar_attributes(cal, number_names,
303                         calendar_attributes_names));
304 }
305
306 extern CSA_return_code
307 csa_save(
308         CSA_session_handle      session,
309         CSA_string      archive_name,
310         CSA_uint32      number_attributes,
311         CSA_attribute   *attributes,
312         CSA_enum        *operators,
313         CSA_boolean     delete_entry,
314         CSA_extension   *save_extensions)
315 {
316         DP(("api.c: csa_save\n"));
317
318         return (CSA_E_NOT_SUPPORTED);
319 }
320
321 extern CSA_return_code
322 csa_restore(
323         CSA_session_handle      session,
324         CSA_string      archive_name,
325         CSA_uint32      number_attributes,
326         CSA_attribute   *attributes,
327         CSA_enum        *operators,
328         CSA_extension   *restore_extensions)
329 {
330         DP(("api.c: csa_restore\n"));
331
332         return (CSA_E_NOT_SUPPORTED);
333 }
334
335 /*
336  * If list_operators is NULL, the operator is default to be CSA_MATCH_EQUAL_TO
337  * for all attributes.
338  * *** might want to check operator for conflicts that won't result in no match
339  */
340 extern CSA_return_code
341 csa_list_entries(
342         CSA_session_handle      session,
343         CSA_uint32      number_attributes,
344         CSA_attribute   *entry_attributes,
345         CSA_enum        *list_operators,
346         CSA_uint32      *number_entries,
347         CSA_entry_handle        **entries,
348         CSA_extension   *list_entries_extensions)
349 {
350         CSA_return_code         stat;
351         Calendar                *cal;
352         _DtCm_libentry          *elist;
353
354         DP(("api.c: csa_list_entries\n"));
355
356         if (entries == NULL || number_entries == NULL)
357                 return (CSA_E_INVALID_PARAMETER);
358         else {
359                 *entries = NULL;
360                 *number_entries = 0;
361         }
362
363         /* check list_entries_extensions
364          * and return appropriate return code
365          * no function extension is supported
366          */
367         if (list_entries_extensions != NULL)
368                 return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
369
370         /* get calendar object */
371         if ((cal = _DtCm_get_Calendar(session)) == NULL)
372                 return (CSA_E_INVALID_SESSION_HANDLE);
373
374         if (list_operators && (stat = _DtCm_check_operator(number_attributes,
375             entry_attributes, NULL, list_operators)) != CSA_SUCCESS) {
376
377                 return (stat);
378         }
379
380         /* check data type */
381         if ((stat = _DtCm_check_entry_attributes(cal->file_version,
382             number_attributes, entry_attributes, 0, B_FALSE)) != CSA_SUCCESS) {
383                 /*
384                  * if attribute not supported by old backends
385                  * are specified, just fail the match,
386                  * i.e. return NULL and CSA_SUCCESS
387                  */
388                 if (stat == CSA_E_UNSUPPORTED_ATTRIBUTE)
389                         stat = CSA_SUCCESS;
390
391                 return (stat);
392         }
393
394         /* lookup entries */
395         if ((stat = _DtCm_rpc_lookup_entries(cal, number_attributes,
396             entry_attributes, list_operators, &elist)) == CSA_SUCCESS) {
397
398                 if (elist) {
399                         *number_entries = _DtCm_add_to_entry_list(cal, (caddr_t)elist);
400                         stat = _DtCm_libentry_to_entryh(elist, number_entries,
401                                 entries);
402                 }
403         }
404
405         return (stat);
406 }
407
408 extern CSA_return_code
409 csa_list_entry_attributes(
410         CSA_session_handle      session,
411         CSA_entry_handle        entryh,
412         CSA_uint32      *number_names,
413         CSA_attribute_reference **entry_attribute_names,
414         CSA_extension   *list_entry_attributes_extensions)
415 {
416         CSA_return_code stat;
417         _DtCm_libentry  *entry;
418
419         DP(("api.c: csa_list_entry_attributes\n"));
420
421         if (number_names == NULL || entry_attribute_names == NULL)
422                 return (CSA_E_INVALID_PARAMETER);
423
424         /* check list_entry_attributes_extensions
425          * and return appropriate return code
426          * no function extension is supported
427          */
428         if (list_entry_attributes_extensions != NULL)
429                 return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
430
431         /* get appointment */
432         if ((entry = _DtCm_get_libentry(entryh)) == NULL)
433                 return (CSA_E_INVALID_ENTRY_HANDLE);
434
435         if ((stat = _DtCm_get_entry_detail(entry)) != CSA_SUCCESS)
436                 return (stat);
437
438         return (_DtCm_get_entry_attr_names(entry, number_names,
439                 entry_attribute_names));
440 }
441
442
443 extern CSA_return_code
444 csa_read_entry_attributes(
445         CSA_session_handle      session,
446         CSA_entry_handle        entryh,
447         CSA_uint32      number_names,
448         CSA_attribute_reference *attribute_names,
449         CSA_uint32      *number_attributes,
450         CSA_attribute   **entry_attributes,
451         CSA_extension   *read_entry_attributes_extensions)
452 {
453         CSA_return_code stat;
454         _DtCm_libentry  *entry;
455
456         DP(("api.c: csa_read_entry_attributes\n"));
457
458         if (number_attributes == 0 || entry_attributes == NULL)
459                 return (CSA_E_INVALID_PARAMETER);
460
461         /* check read_entry_attributes_extensions
462          * and return appropriate return code
463          * no function extension is supported
464          */
465         if (read_entry_attributes_extensions != NULL)
466                 return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
467
468         /* get entry object */
469         if ((entry = _DtCm_get_libentry(entryh)) == NULL)
470                 return (CSA_E_INVALID_ENTRY_HANDLE);
471
472         if ((stat = _DtCm_get_entry_detail(entry)) != CSA_SUCCESS)
473                 return (stat);
474
475         if (number_names > 0) {
476                 return (_DtCm_get_entry_attrs_by_name(entry, number_names,
477                         attribute_names, number_attributes, entry_attributes));
478         } else {
479                 return (_DtCm_get_all_entry_attrs(entry, number_attributes,
480                         entry_attributes));
481         }
482 }
483
484 extern CSA_return_code
485 csa_free(CSA_buffer memory)
486 {
487         DP(("api.c: csa_free\n"));
488
489         return (_DtCm_free(memory));
490 }
491
492 extern CSA_return_code
493 csa_look_up(
494         CSA_session_handle      session,
495         CSA_calendar_user       *users,
496         CSA_flags       look_up_flags,
497         CSA_uint32      *number_users,
498         CSA_calendar_user       **user_list,
499         CSA_extension   *look_up_extensions)
500 {
501         DP(("api.c: csa_look_up\n"));
502
503         return (CSA_E_NOT_SUPPORTED);
504 }
505
506 extern CSA_return_code
507 csa_query_configuration(
508         CSA_session_handle      session,
509         CSA_enum        item,
510         CSA_buffer      *reference,
511         CSA_extension   *query_configuration_extensions)
512 {
513         CSA_return_code stat = CSA_SUCCESS;
514         Calendar        *cal;
515         DP(("api.c: csa_query_configuration\n"));
516
517         /* get calendar object */
518         if ((cal = _DtCm_get_Calendar(session)) == NULL)
519                 return (CSA_E_INVALID_SESSION_HANDLE);
520
521         if (item < CSA_CONFIG_CHARACTER_SET || item > CSA_CONFIG_VER_SPEC)
522                 return (CSA_E_INVALID_ENUM);
523
524         if (reference == NULL)
525                 return (CSA_E_INVALID_PARAMETER);
526
527         if (query_configuration_extensions) {
528                 if ((stat = _handle_query_config_ext(
529                     query_configuration_extensions)) != CSA_SUCCESS)
530                         return (stat);
531         }
532
533         switch (item) {
534         case CSA_CONFIG_DEFAULT_SERVICE:
535         case CSA_CONFIG_DEFAULT_USER:
536                 *reference = NULL;
537                 break;
538
539         case CSA_CONFIG_REQ_PASSWORD:
540                 *reference = (CSA_buffer)CSA_REQUIRED_NO;
541                 break;
542
543         case CSA_CONFIG_REQ_SERVICE:
544         case CSA_CONFIG_REQ_USER:
545                 *reference = (CSA_buffer)CSA_REQUIRED_YES;
546                 break;
547
548         case CSA_CONFIG_UI_AVAIL:
549                 *reference = (CSA_buffer)CSA_FALSE;
550                 break;
551
552         case CSA_CONFIG_VER_SPEC:
553                 *reference = (CSA_buffer)strdup(_DtCM_SPEC_VERSION_SUPPORTED);
554                 break;
555
556         case CSA_CONFIG_CHARACTER_SET:
557         case CSA_CONFIG_LINE_TERM:
558         case CSA_CONFIG_VER_IMPLEM:
559                 stat = CSA_E_UNSUPPORTED_ENUM;
560         }
561         
562         return (stat);
563 }
564
565 extern CSA_return_code
566 csa_read_calendar_attributes(
567         CSA_session_handle      session,
568         CSA_uint32      number_names,
569         CSA_attribute_reference *attribute_names,
570         CSA_uint32      *number_attributes,
571         CSA_attribute   **calendar_attributes,
572         CSA_extension   *read_calendar_attributes_extensions)
573 {
574         Calendar        *cal;
575
576         DP(("api.c: csa_read_calendar_attributes\n"));
577
578         if (number_attributes == 0 || calendar_attributes == NULL)
579                 return (CSA_E_INVALID_PARAMETER);
580
581         /* check read_calendar_attributes_extensions
582          * and return appropriate return code
583          * no function extension is supported
584          */
585         if (read_calendar_attributes_extensions != NULL)
586                 return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
587
588         /* get calendar object */
589         if ((cal = _DtCm_get_Calendar(session)) == NULL)
590                 return (CSA_E_INVALID_SESSION_HANDLE);
591
592         _DtCm_reset_cal_attrs(cal);
593         if (number_names > 0) {
594                 return (_DtCm_get_cal_attrs_by_name(cal, number_names,
595                         attribute_names, number_attributes,
596                         calendar_attributes));
597         } else {
598                 return (_DtCm_get_all_cal_attrs(cal, number_attributes,
599                         calendar_attributes));
600         }
601 }
602
603 extern CSA_return_code
604 csa_register_callback(
605         CSA_session_handle      session,
606         CSA_flags               reason,
607         CSA_callback            callback,
608         CSA_buffer              client_data,
609         CSA_extension           *register_callback_extensions)
610 {
611         CSA_return_code         stat = CSA_SUCCESS;
612         _DtCmCallbackEntry      *cb_entry;
613         Calendar                *cal;
614         boolean_t               async = B_FALSE;
615
616         /* get calendar object */
617         if ((cal = _DtCm_get_Calendar(session)) == NULL)
618                 return (CSA_E_INVALID_SESSION_HANDLE);
619
620         /* make sure some valid flags are set
621          * and only entry added, deleted or updated is specified for
622          * servers support up to version 4 rpc protocol
623          */
624         if (reason == 0 || reason >= (CSA_CB_ENTRY_UPDATED << 1) ||
625             (cal->rpc_version < _DtCM_FIRST_EXTENSIBLE_SERVER_VERSION &&
626              (reason & ~(CSA_CB_ENTRY_ADDED|CSA_CB_ENTRY_DELETED|
627              CSA_CB_ENTRY_UPDATED))))
628                 return (CSA_E_INVALID_FLAG);
629
630         /* must specify a callback function otherwise
631          * there is no point to make this call
632          */
633         if (callback == NULL)
634                 return (CSA_E_INVALID_PARAMETER);
635
636         /* need to initialize agent before doing XtAppAddInput */
637         _DtCm_init_agent();
638
639         if (register_callback_extensions != NULL) {
640                 if ((stat = _handle_register_callback_ext(
641                     register_callback_extensions)) != CSA_SUCCESS)
642                         return (stat);
643                 else
644                         async = B_TRUE;
645         }
646
647         /*
648          * register interest only if we have a new flag
649          * NOTE: It's OK to register more than once, even if it's
650          * the same calendar, same reason.  The customer is always right.
651          */
652         if (((cal->all_reasons | reason) ^ cal->all_reasons) &&
653             (stat = _DtCm_rpc_register_client(cal, reason)) != CSA_SUCCESS)
654                 return stat;
655
656         if ((cb_entry = (_DtCmCallbackEntry*)malloc(sizeof(_DtCmCallbackEntry)))
657             == NULL)
658                 return (CSA_E_INSUFFICIENT_MEMORY);
659
660         /* update info in calendar structure */
661         cal->all_reasons |= reason;
662
663         /* don't just do cal->async_process = async, since cal->async_process
664          * might have been set to B_TRUE before.
665          */
666         if (async == B_TRUE)
667                 cal->async_process = B_TRUE;
668         if (cal->async_process == B_TRUE)
669                 cal->do_reasons = cal->all_reasons;
670
671         /* fill in the callback record */
672         cb_entry->reason = reason;
673         cb_entry->handler = callback;
674         cb_entry->client_data = client_data;
675         
676         /* insert it at head of list */
677         cb_entry->next = cal->cb_list;
678         cb_entry->prev = (_DtCmCallbackEntry*) NULL;
679         if (cal->cb_list != (_DtCmCallbackEntry*) NULL)
680                 cal->cb_list->prev = cb_entry;
681         cal->cb_list = cb_entry;
682
683         return CSA_SUCCESS;
684 }
685
686 /*
687  * csa_unregister_callback
688  *
689  * removes a previsouly registered callback from the callback list
690  * of the specified calendar.
691  */
692 extern CSA_return_code
693 csa_unregister_callback(
694         CSA_session_handle      session,
695         CSA_flags               reason,
696         CSA_callback            handler,
697         CSA_buffer              client_data,
698         CSA_extension           *unregister_callback_extensions)
699 {
700         Calendar        *cal;
701         boolean_t       match_one = B_FALSE;
702         boolean_t       match_all = B_FALSE;
703         boolean_t       matched;
704         CSA_flags       all = 0, unreg;
705         _DtCmCallbackEntry *cb, *ncb;
706
707         /* get calendar object */
708         if ((cal = _DtCm_get_Calendar(session)) == NULL)
709                 return (CSA_E_INVALID_SESSION_HANDLE);
710
711         if (reason == 0 || reason >= (CSA_CB_ENTRY_UPDATED << 1))
712                 return (CSA_E_INVALID_FLAG);
713
714         /* check unregister_callback_extensions
715          * and return appropriate return code
716          * no function extension is supported
717          */
718         if (unregister_callback_extensions != NULL)
719                 return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
720
721         if (handler == NULL && client_data == NULL)
722                 match_all = B_TRUE;
723         else if (handler == NULL)       /* but client data is not NULL */
724                 return (CSA_E_INVALID_PARAMETER);
725
726         /*
727          * Removal policy: If both handler and client data are NULL,
728          * match all callbacks for the specified reason.
729          * Otherwise, all of reason, handler and client data must
730          * match.
731          */
732         cb = cal->cb_list;
733         while (cb) {
734                 matched = B_TRUE;
735                 ncb = cb->next;
736
737                 if (!(reason & cb->reason) ||
738                     (match_all == B_FALSE && (handler != cb->handler ||
739                     client_data != cb->client_data))) {
740                         all = all | cb->reason;
741                         cb = cb->next;
742                         continue;
743                 }
744
745                 match_one = B_TRUE;
746
747                 if (cb->reason = (cb->reason | reason) ^ reason) {
748                         all = all | cb->reason;
749                 } else {
750                         /* remove entry */
751                         if (cb->prev != NULL)
752                                 cb->prev->next = cb->next;
753                         if (cb->next != NULL)
754                                 cb->next->prev = cb->prev;
755                         if (cb == cal->cb_list)
756                                 cal->cb_list = cb->next;
757                         free(cb);
758                 }
759
760                 cb = ncb;
761         }
762
763         unreg = all ^ cal->all_reasons;
764         cal->all_reasons = all;
765
766         if (unreg)
767                 (void) _DtCm_rpc_unregister_client(cal, unreg);
768
769         if (match_one == B_TRUE)
770                 return (CSA_SUCCESS);
771         else
772                 return (CSA_E_CALLBACK_NOT_REGISTERED);
773 }
774
775 CSA_return_code
776 csa_call_callbacks(
777         CSA_session_handle      session,
778         CSA_flags               reason,
779         CSA_extension           *call_callbacks_extensions)
780 {
781         Calendar        *cal;
782
783         DP(("api.c: csa_call_callbacks\n"));
784
785         /* get calendar object */
786         if ((cal = _DtCm_get_Calendar(session)) == NULL)
787                 return (CSA_E_INVALID_SESSION_HANDLE);
788
789         /* no function extension is supported */
790         if (call_callbacks_extensions != NULL)
791                 return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
792
793         /* set up information in calendar structure */
794         if (cal->async_process == B_FALSE)
795                 cal->do_reasons = reason;
796
797         /* trigger callback mechanism */
798         _DtCm_process_updates();
799
800         if (cal->async_process == B_FALSE)
801                 cal->do_reasons = 0;
802
803         return (CSA_SUCCESS);
804 }
805
806 extern CSA_return_code
807 csa_update_calendar_attributes(
808         CSA_session_handle      session,
809         CSA_uint32      num_attrs,
810         CSA_attribute   *attrs,
811         CSA_extension   *update_calendar_attributes_extensions)
812 {
813         CSA_return_code stat = CSA_SUCCESS;
814         Calendar        *cal;
815
816         DP(("api.c: csa_update_calendar_attributes\n"));
817
818         /* get calendar object */
819         if ((cal = _DtCm_get_Calendar(session)) == NULL)
820                 return (CSA_E_INVALID_SESSION_HANDLE);
821
822         if (num_attrs == 0 || attrs == NULL)
823                 return (CSA_E_INVALID_PARAMETER);
824
825         /*
826          * check update_calendar_attributes_extensions
827          * return appropriate return code
828          * no function extension is supported
829          */
830         if (update_calendar_attributes_extensions != NULL)
831                 return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
832
833         /* check authority */
834         if ((cal->file_version >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION &&
835             !(cal->access & (CSA_OWNER_RIGHTS | CSA_INSERT_CALENDAR_ATTRIBUTES |
836             CSA_CHANGE_CALENDAR_ATTRIBUTES))) ||
837             (cal->file_version < _DtCM_FIRST_EXTENSIBLE_DATA_VERSION &&
838             !(cal->access & CSA_OWNER_RIGHTS)))
839                 return (CSA_E_NO_AUTHORITY);
840
841         /* check data type */
842         if ((stat = _DtCm_check_cal_csa_attributes(cal->file_version,
843             num_attrs, attrs, cal->name, B_TRUE, B_FALSE, B_TRUE))
844             != CSA_SUCCESS) {
845                 return (stat);
846         }
847
848         /* set attributes */
849         stat = _DtCm_rpc_set_cal_attrs(cal, num_attrs, attrs);
850
851         return (stat);
852 }
853
854 extern CSA_return_code
855 csa_add_entry(
856         CSA_session_handle      session,
857         CSA_uint32      num_attrs,
858         CSA_attribute   *attrs,
859         CSA_entry_handle        *entry_r,
860         CSA_extension   *add_entry_extensions)
861 {
862         CSA_return_code stat;
863         Calendar        *cal;
864         _DtCm_libentry  *entry;
865
866         DP(("api.c: csa_add_entry\n"));
867
868         if (num_attrs == 0 || attrs == NULL)
869                 return (CSA_E_INVALID_PARAMETER);
870
871         /* get calendar object */
872         if ((cal = _DtCm_get_Calendar(session)) == NULL)
873                 return (CSA_E_INVALID_SESSION_HANDLE);
874
875         /* check data type, readonly attributes, etc */
876         if ((stat = _DtCm_check_entry_attributes(cal->file_version,
877             num_attrs, attrs, CSA_CB_ENTRY_ADDED, B_TRUE)) != CSA_SUCCESS) {
878                 return (stat);
879         }
880
881         /* no function extension is supported */
882         if (add_entry_extensions != NULL)
883                 return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
884
885         /* insert in calendar */
886         if ((stat = _DtCm_rpc_insert_entry(cal, num_attrs, attrs, &entry))
887             == CSA_SUCCESS) {
888
889                 if (entry_r != NULL) {
890                         if ((entry = _DtCm_convert_entry_wheader(entry))==NULL)
891                                 stat = CSA_E_INSUFFICIENT_MEMORY;
892                         else {
893                                 _DtCm_add_to_entry_list(cal, (caddr_t)entry);
894                                 *entry_r = (CSA_entry_handle)entry;
895                         }
896                 } else
897                         _DtCm_free_libentries(entry);
898         }
899
900         return (stat);
901 }
902
903 /*
904  * The session argument is ignored in this implementation.
905  */
906 extern CSA_return_code
907 csa_delete_entry(
908         CSA_session_handle      session,
909         CSA_entry_handle        entryh,
910         CSA_enum        delete_scope,
911         CSA_extension   *delete_entry_extensions)
912 {
913         CSA_return_code stat;
914         _DtCm_libentry  *entry;
915
916         DP(("api.c: csa_delete_entry\n"));
917
918         /* get entry object */
919         if ((entry = _DtCm_get_libentry(entryh)) == NULL)
920                 return (CSA_E_INVALID_ENTRY_HANDLE);
921
922         if (delete_scope < CSA_SCOPE_ALL || delete_scope > CSA_SCOPE_FORWARD)
923                 return (CSA_E_INVALID_ENUM);
924
925         /* check delete_entry_extensions and return appropriate return code */
926         /* no function extension is supported */
927         if (delete_entry_extensions != NULL)
928                 return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
929
930         /* delete entry from calendar */
931         stat = _DtCm_rpc_delete_entry(entry->cal, entry, delete_scope);
932
933         return (stat);
934 }
935
936 extern CSA_return_code
937 csa_free_time_search(
938         CSA_session_handle      session,
939         CSA_date_time_range     date_time_range,
940         CSA_time_duration       time_duration,
941         CSA_uint32              number_users,
942         CSA_calendar_user       *users,
943         CSA_free_time   **free_time,
944         CSA_extension   *free_time_search_extensions)
945 {
946         DP(("api.c: csa_free_time_search\n"));
947
948         return (CSA_E_NOT_SUPPORTED);
949 }
950
951 extern CSA_return_code
952 csa_list_entry_sequence(
953         CSA_session_handle      session,
954         CSA_entry_handle        entryh,
955         CSA_date_time_range     time_range,
956         CSA_uint32              *number_entries,
957         CSA_entry_handle        **entries,
958         CSA_extension   *list_entry_sequence_extensions)
959 {
960         CSA_return_code stat;
961         _DtCm_libentry  *entry, *elist;
962         cms_attribute   *rtype;
963         cms_attribute   *rtimes;
964         /* needed temporaryly */
965         time_t start = 0, end = 0;
966
967         DP(("api.c: csa_list_entry_sequence\n"));
968
969         /* get entry object */
970         if ((entry = _DtCm_get_libentry(entryh)) == NULL)
971                 return (CSA_E_INVALID_ENTRY_HANDLE);
972
973         if (number_entries == NULL || entries == NULL)
974                 return (CSA_E_INVALID_PARAMETER);
975         else {
976                 *number_entries = 0;
977                 *entries = NULL;
978         }
979
980         /* check whether this entry repeats */
981         /* fail it if it's not a repeating event */
982         if (entry->cal->file_version < _DtCM_FIRST_EXTENSIBLE_DATA_VERSION) {
983                 rtype = &entry->e->attrs[CSA_X_DT_ENTRY_ATTR_REPEAT_TYPE_I];
984                 if (rtype->value == NULL ||
985                     rtype->value->item.sint32_value == CSA_X_DT_REPEAT_ONETIME)
986                 {
987                         return (CSA_E_INVALID_PARAMETER);
988                 }
989         } else {
990                 rtype = &entry->e->attrs[CSA_ENTRY_ATTR_RECURRENCE_RULE_I];
991                 if (rtype->value == NULL ||
992                     rtype->value->item.string_value == NULL ||
993                     *(rtype->value->item.string_value) == '\0' ) {
994                         return (CSA_E_INVALID_PARAMETER);
995                 }
996         }
997
998
999         /* if this entry repeats indefinitely and time range is not
1000          * specified, fail it
1001          */
1002         if (time_range) {
1003                 if (_csa_iso8601_to_range(time_range, &start, &end) != 0)
1004                         return (CSA_E_INVALID_DATE_TIME);
1005         }
1006
1007         if (entry->cal->file_version < _DtCM_FIRST_EXTENSIBLE_DATA_VERSION)
1008                 rtimes = &entry->e->attrs[CSA_X_DT_ENTRY_ATTR_REPEAT_TIMES_I];
1009         else
1010                 rtimes = &entry->e->attrs[CSA_ENTRY_ATTR_NUMBER_RECURRENCES_I];
1011
1012         if (start == 0 && end == 0 &&
1013             (rtimes->value && rtimes->value->item.uint32_value
1014             == CSA_X_DT_DT_REPEAT_FOREVER))
1015                 return (CSA_E_INVALID_PARAMETER);
1016
1017         /* no function extension is supported */
1018         if (list_entry_sequence_extensions != NULL)
1019                 return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
1020
1021         /* lookup sequence */
1022         if ((stat = _DtCm_rpc_enumerate_sequence(entry->cal, entry, start, end,
1023             &elist)) == CSA_SUCCESS) {
1024
1025                 if (elist) {
1026                         *number_entries = _DtCm_add_to_entry_list(entry->cal,
1027                                         (caddr_t)elist);
1028                         stat = _DtCm_libentry_to_entryh(elist, number_entries,
1029                                         entries);
1030                 }
1031         }
1032
1033         return (stat);
1034 }
1035
1036 /*
1037  * Due to the implementation of existing backends (versions 2-4)
1038  * which will unmanage any reminders that happens before the
1039  * the given tick, the user specified tick is ignore and
1040  * we will pass in the current time.
1041  */
1042 extern CSA_return_code
1043 csa_read_next_reminder(
1044         CSA_session_handle      session,
1045         CSA_uint32      number_names,
1046         CSA_attribute_reference *reminder_names,
1047         CSA_date_time   given_time,
1048         CSA_uint32      *number_reminders,
1049         CSA_reminder_reference  **reminders,
1050         CSA_extension   *read_next_reminder_extensions)
1051 {
1052         CSA_return_code stat;
1053         Calendar        *cal;
1054         _DtCm_libentry  *eptr, *prev = NULL, *head = NULL;
1055         int     i;
1056         time_t  timeval;
1057
1058         DP(("api.c: csa_read_next_reminder\n"));
1059
1060         /* get calendar object */
1061         if ((cal = _DtCm_get_Calendar(session)) == NULL)
1062                 return (CSA_E_INVALID_SESSION_HANDLE);
1063
1064         if (number_reminders == 0 || reminders == NULL)
1065                 return (CSA_E_INVALID_PARAMETER);
1066
1067         if (given_time == NULL || _csa_iso8601_to_tick(given_time, &timeval))
1068                 return (CSA_E_INVALID_DATE_TIME);
1069
1070         /*
1071          * check read_next_reminder_extensions and
1072          * return appropriate return code
1073          * no function extension is supported
1074          */
1075         if (read_next_reminder_extensions != NULL)
1076                 return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
1077
1078         /* lookup reminders */
1079         if ((stat = _DtCm_rpc_lookup_reminder(cal, timeval,
1080             number_names, reminder_names, number_reminders, reminders))
1081             == CSA_SUCCESS) {
1082
1083                 /*
1084                  * link up all associated entries
1085                  * and add to calendar structure
1086                  */
1087                 for (i = 0; i < *number_reminders; i++) {
1088                         eptr = (_DtCm_libentry *)((*reminders)[i]).entry;
1089
1090                         /* link entry back to cal */
1091                         eptr->cal = cal;
1092
1093                         /* link up entries in the same order as
1094                          * the associated reminders
1095                          */
1096                         if (head == NULL)
1097                                 head = eptr;
1098                         else {
1099                                 prev->next = eptr;
1100                                 eptr->prev = prev;
1101                         }
1102                         prev = eptr;
1103
1104                 }
1105
1106                 (void)_DtCm_add_to_entry_list(cal, (caddr_t)head);
1107         }
1108
1109         return (stat);
1110 }
1111
1112 /*
1113  * These arguments are ignored in this implementation:
1114  *      session, update_propagation
1115  */
1116 extern CSA_return_code
1117 csa_update_entry_attributes(
1118         CSA_session_handle      session,
1119         CSA_entry_handle        entry,
1120         CSA_enum                scope,
1121         CSA_boolean             update_propagation,
1122         CSA_uint32              num_attrs,
1123         CSA_attribute           *attrs,
1124         CSA_entry_handle        *new_entry,
1125         CSA_extension           *update_entry_attributes_extensions)
1126 {
1127         CSA_return_code stat;
1128         _DtCm_libentry  *oentry, *nentry;
1129
1130         DP(("api.c: csa_update_entry_attributes\n"));
1131
1132         /* get entry object */
1133         if ((oentry = _DtCm_get_libentry(entry)) == NULL)
1134                 return (CSA_E_INVALID_ENTRY_HANDLE);
1135
1136         if (scope < CSA_SCOPE_ALL || scope > CSA_SCOPE_FORWARD)
1137                 return (CSA_E_INVALID_ENUM);
1138
1139         if (num_attrs == 0 || attrs == NULL)
1140                 return (CSA_E_INVALID_PARAMETER);
1141
1142         /*
1143          * check update_entry_attributes_extensions and
1144          * return appropriate return code
1145          * no function extension is supported
1146          */
1147         if (update_entry_attributes_extensions != NULL)
1148                 return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
1149
1150         /* check data type, readonly attributes, etc */
1151         if ((stat = _DtCm_check_entry_attributes(oentry->cal->file_version,
1152             num_attrs, attrs, CSA_CB_ENTRY_UPDATED, B_TRUE)) != CSA_SUCCESS) {
1153                 return (stat);
1154         }
1155
1156         /* change entry in calendar */
1157         if ((stat = _DtCm_rpc_update_entry(oentry->cal, oentry, num_attrs,
1158             attrs, scope, &nentry)) == CSA_SUCCESS) {
1159                 if (new_entry) {
1160                         if ((nentry = _DtCm_convert_entry_wheader(nentry))
1161                             == NULL)
1162                                 stat = CSA_E_INSUFFICIENT_MEMORY;
1163                         else {
1164                                 _DtCm_add_to_entry_list(oentry->cal,
1165                                         (caddr_t)nentry);
1166                                 *new_entry = (CSA_entry_handle)nentry;
1167                         }
1168                 } else
1169                         _DtCm_free_libentries(nentry);
1170         }
1171
1172         return (stat);
1173 }
1174
1175 /* ADD EVENT */
1176 extern CSA_return_code
1177 csa_add_event(
1178         CSA_service_reference   calendar_service,
1179         CSA_string              calendar_address,
1180         CSA_string              logon_user,
1181         CSA_string              logon_password,
1182         CSA_string              attendee,
1183         CSA_enum                attendee_priority,
1184         CSA_enum                attendee_status,
1185         CSA_boolean             attendee_rsvp_requested,
1186         CSA_date_time           start_date,
1187         CSA_date_time           end_date,
1188         CSA_string              organizer,
1189         CSA_string              sponsor,
1190         CSA_string              summary,
1191         CSA_string              description,
1192         CSA_string              recurrence_rule,
1193         CSA_string              exception_rule,
1194         CSA_string              subtype,
1195         CSA_enum                classification,
1196         CSA_string              delimiters,
1197         CSA_string              add_event_extensions)
1198 {
1199         DP(("api.c: csa_add_event\n"));
1200
1201         return (CSA_E_NOT_SUPPORTED);
1202 }
1203
1204 /* ADD TODO */
1205 CSA_return_code
1206 csa_add_todo(
1207         CSA_service_reference   calendar_service,
1208         CSA_string              calendar_address,
1209         CSA_string              logon_user,
1210         CSA_string              logon_password,
1211         CSA_enum                attendee_priority,
1212         CSA_enum                attendee_status,
1213         CSA_boolean             attendee_rsvp_requested,
1214         CSA_date_time           start_date,
1215         CSA_date_time           due_date,
1216         CSA_uint32              priority,
1217         CSA_string              summary,
1218         CSA_string              description,
1219         CSA_enum                classification,
1220         CSA_string              delimiters,
1221         CSA_string              add_todo_extensions)
1222 {
1223         DP(("api.c: csa_add_todo\n"));
1224
1225         return (CSA_E_NOT_SUPPORTED);
1226 }
1227
1228 /* ADD MEMO */
1229 CSA_return_code
1230 csa_add_memo(
1231         CSA_service_reference   calendar_service,
1232         CSA_string              calendar_address,
1233         CSA_string              logon_user,
1234         CSA_string              logon_password,
1235         CSA_date_time           start_date,
1236         CSA_string              summary,
1237         CSA_string              delimiters,
1238         CSA_string              add_memo_extensions)
1239 {
1240         DP(("api.c: csa_add_memo\n"));
1241
1242         return (CSA_E_NOT_SUPPORTED);
1243 }
1244
1245 /******************************************************************************
1246  * static functions used within in the file
1247  ******************************************************************************/
1248
1249 static CSA_return_code
1250 _handle_register_callback_ext(CSA_extension *ext)
1251 {
1252         int             i;
1253
1254         for (i = 0; ; i++) {
1255                 if (ext[i].item_code == CSA_X_XT_APP_CONTEXT_EXT) {
1256                         _DtCm_register_xtcallback(
1257                                 (XtAppContext)ext[i].item_data);
1258                 } else
1259                         return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
1260
1261                 if (ext[i].extension_flags & CSA_EXT_LAST_ELEMENT)
1262                         break;
1263         }
1264
1265         return (CSA_SUCCESS);
1266 }
1267
1268 static CSA_return_code
1269 _handle_logon_ext(CSA_extension *ext, CSA_extension **pext)
1270 {
1271         int             i;
1272         int             get_access_index = -1;
1273         int             com_support_index = -1;
1274
1275         for (i = 0; ; i++) {
1276                 if (ext[i].item_code == CSA_X_DT_GET_USER_ACCESS_EXT)
1277                         get_access_index = i;
1278                 else if (ext[i].item_code == CSA_X_COM_SUPPORT_EXT)
1279                         com_support_index = i;
1280                 else
1281                         return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
1282
1283                 if (ext[i].extension_flags & CSA_EXT_LAST_ELEMENT)
1284                         break;
1285         }
1286
1287         if (get_access_index >= 0)
1288                 *pext = &ext[get_access_index];
1289
1290         if (com_support_index >= 0)
1291                 _handle_com_support_ext(&ext[com_support_index]);
1292
1293         return (CSA_SUCCESS);
1294 }
1295
1296 static CSA_return_code
1297 _handle_query_config_ext(CSA_extension *ext)
1298 {
1299         int             i;
1300         int             com_support_index = -1;
1301
1302         for (i = 0; ; i++) {
1303                 if (ext[i].item_code == CSA_X_COM_SUPPORT_EXT) {
1304                         com_support_index = i;
1305                 } else
1306                         return (CSA_E_UNSUPPORTED_FUNCTION_EXT);
1307
1308                 if (ext[i].extension_flags & CSA_EXT_LAST_ELEMENT)
1309                         break;
1310         }
1311
1312         if (com_support_index >= 0)
1313                 _handle_com_support_ext(&ext[com_support_index]);
1314
1315         return (CSA_SUCCESS);
1316 }
1317
1318 static void
1319 _handle_com_support_ext(CSA_extension *ext)
1320 {
1321         int                     i;
1322         CSA_X_COM_support       *xcom;
1323
1324         for (i = 0, xcom = ext->item_reference; i < ext->item_data; i++) {
1325                 switch (xcom[i].item_code) {
1326                 case CSA_X_COM_SUPPORT_EXT:
1327                 case CSA_X_XT_APP_CONTEXT_EXT:
1328                 case CSA_XS_DT:
1329                 case CSA_X_DT_GET_USER_ACCESS_EXT:
1330                         xcom[i].flags = CSA_X_COM_SUPPORTED;
1331                         break;
1332
1333                 case CSA_XS_COM:
1334                 case CSA_X_UI_ID_EXT:
1335                 default:
1336                         xcom[i].flags = CSA_X_COM_NOT_SUPPORTED;
1337                         break;
1338                 }
1339         }
1340 }
1341