dtcm: Resolve CID 87822
[oweals/cde.git] / cde / programs / dtcm / server / cmsfunc.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: cmsfunc.c /main/4 1995/11/09 12:42:12 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 <errno.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <time.h>
38 #include <sys/resource.h>
39 #include <rpc/rpc.h>
40 #include <dirent.h>
41 #include "csa.h"
42 #include "cm.h"
43 #include "rtable4.h"
44 #include "attr.h"
45 #include "access.h"
46 #include "cmscalendar.h"
47 #include "updateattrs.h"
48 #include "cmsdata.h"
49 #include "cmsentry.h"
50 #include "match.h"
51 #include "rpcextras.h"
52 #include "v5ops.h"
53 #include "v4ops.h"
54 #include "iso8601.h"
55 #include "log.h"
56 #include "lutil.h"
57 #include "cm_tbl.i"
58 #include "callback.h"
59 #include "insert.h"
60 #include "update.h"
61 #include "delete.h"
62 #include "lookup.h"
63 #include "misc.h"
64 #include "convert4-5.h"
65 #include "convert5-4.h"
66 #include "appt4.h"
67 #include "cmsconvert.h"
68
69 extern int debug;
70
71 /******************************************************************************
72  * forward declaration of static functions used within the file
73  ******************************************************************************/
74
75 static CSA_return_code _DtCmsCreateCallog(char *user, cms_create_args *args,
76                                         _DtCmsCalendar **cal);
77 static CSA_return_code _ListCalendarNames(uint *num_names, char ***names);
78 static void * _grow_char_array(void *ptr, uint oldcount, uint newcount);
79 static void _free_char_array(uint num_elem, char **ptr);
80 static CSA_return_code _DtCmsGetOldCalAttrNames(_DtCmsCalendar *cal,
81                                 uint *num_names_r, cms_attr_name **names_r);
82
83 /*****************************************************************************
84  * extern functions used in the library
85  *****************************************************************************/
86
87 extern void *
88 cms_ping_5_svc(void *args, struct svc_req *svcrq)
89 {
90         if (debug)
91                 fprintf(stderr, "cms_ping_5_svc called\n");
92
93         return(NULL); /* for RPC reply */
94 }
95
96
97 extern cms_list_calendars_res *
98 cms_list_calendars_5_svc(void *dummy, struct svc_req *svcrq)
99 {
100         static cms_list_calendars_res   res;
101
102         if (debug)
103                 fprintf(stderr, "cms_list_calendars_5_svc called\n");
104
105         if (res.num_names > 0) {
106                 _free_char_array(res.num_names, res.names);
107                 res.num_names = 0;
108         }
109
110         res.stat = _ListCalendarNames(&res.num_names, &res.names);
111
112         return (&res);
113 }
114
115 extern  cms_open_res *
116 cms_open_calendar_5_svc(cms_open_args *args, struct svc_req *svcrq)
117 {
118         static cms_open_res     res;
119         static char             sversion[80];
120         _DtCmsCalendar          *cal;
121         char                    *user;
122
123         if (debug)
124                 fprintf(stderr, "cms_open_calendar_5_svc called\n");
125
126         if (res.num_attrs > 0) {
127                 _DtCm_free_cms_attributes(res.num_attrs, res.attrs);
128                 free(res.attrs);
129                 res.num_attrs = 0;
130         }
131
132         /* check parameter */
133         if (args->cal == NULL) {
134                 res.stat = CSA_E_INVALID_PARAMETER;
135                 return (&res);
136         }
137
138         if ((res.stat = _DtCmsV5LoadAndCheckAccess(svcrq, args->cal, &user,
139             &res.user_access, &cal)) == CSA_SUCCESS) {
140
141                 res.svr_vers = TABLEVERS;
142
143                 if (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION) {
144
145                         res.file_vers = cal->fversion;
146
147                 } else {
148                         /* old format data */
149                         res.file_vers = _DtCM_FIRST_EXTENSIBLE_DATA_VERSION - 1;
150                 }
151
152                 /* return attribute names */
153                 res.stat = _DtCmsGetAllCalAttrs(cal, &res.num_attrs,
154                                         &res.attrs, B_FALSE);
155         }
156
157         if (res.stat == CSA_SUCCESS) {
158                 /* do callbacks */
159                 cal->rlist = _DtCmsDoOpenCalCallback(cal->rlist,
160                                 cal->calendar, user, args->pid);
161         }
162
163
164         return (&res);
165 }
166
167 extern  CSA_return_code *
168 cms_create_calendar_5_svc(cms_create_args *args, struct svc_req *svcrq)
169 {
170         static CSA_return_code  res;
171         _DtCmsCalendar          *cal;
172         char                    *user;
173
174         if (debug)
175                 fprintf(stderr, "cms_create_calendar_5_svc called\n");
176
177         /* check parameter */
178         if (args->cal == NULL) {
179                 res = CSA_E_INVALID_PARAMETER;
180                 return (&res);
181         }
182
183         /* need to check whether we know about the sender,
184          * if not, fail the request
185          */
186         if ((res = _DtCmsGetClientInfo(svcrq, &user)) != CSA_SUCCESS) {
187                 return (&res);
188         }
189
190         if ((res = _DtCm_check_cal_cms_attributes(_DtCMS_VERSION4,
191             args->num_attrs, args->attrs, user, args->cal, B_TRUE, B_TRUE,
192             B_FALSE)) != CSA_SUCCESS)
193                 return (&res);
194
195         if ((res = _DtCmsGetCalendarByName(args->cal, B_FALSE, &cal))
196             == CSA_SUCCESS && cal != NULL) {
197
198                 res = CSA_E_CALENDAR_EXISTS;
199
200         } else {
201
202                 /* create callog file for new calendar */
203                 res = _DtCmsCreateCallog(user, args, &cal);
204         }
205
206         return (&res);
207 }
208
209 extern  CSA_return_code *
210 cms_remove_calendar_5_svc(cms_remove_args *args, struct svc_req *svcrq)
211 {
212         static CSA_return_code  res;
213         _DtCmsCalendar          *cal;
214         char                    *user;
215         uint                    access;
216
217         if (debug)
218                 fprintf(stderr, "cms_remove_calendar_5_svc called\n");
219
220         if ((res = _DtCmsV5LoadAndCheckAccess(svcrq, args->cal, &user,
221             &access, &cal)) != CSA_SUCCESS)
222                 return (&res);
223
224         if (!(access & CSA_OWNER_RIGHTS)) {
225
226                 res = CSA_E_NO_AUTHORITY;
227
228         } else {
229
230                 /* move callog.name file to calrm.name */
231                 res = _DtCmsRemoveLog(cal->calendar, user);
232
233                 if (res != CSA_SUCCESS)
234                         return (&res);
235
236                 /* do callbacks */
237                 cal->rlist = _DtCmsDoRemoveCalCallback(cal->rlist,
238                                 cal->calendar, user, args->pid);
239
240                 /* free up internal structures */
241                 _DtCmsFreeCalendar(cal);
242         } 
243
244         return (&res);
245 }
246
247 extern  CSA_return_code *
248 cms_register_5_svc(cms_register_args *args, struct svc_req *svcrq)
249 {
250         static CSA_return_code  res;
251         char                    *user;
252         _DtCmsCalendar          *cal;
253         _DtCmsRegistrationInfo  *rinfo;
254
255         if (debug)
256                 fprintf(stderr, "cms_register_5_svc called\n");
257
258         if (args->cal == NULL ||
259             args->update_type >= (CSA_CB_ENTRY_UPDATED << 1)) {
260                 res = CSA_E_INVALID_PARAMETER;
261                 return (&res);
262         }
263
264         if ((res = _DtCmsGetClientInfo(svcrq, &user)) != CSA_SUCCESS)
265                 return (&res);
266
267         if ((res = _DtCmsGetCalendarByName(args->cal, B_TRUE, &cal))
268             != CSA_SUCCESS)
269                 return (&res);
270
271         if ((rinfo = _DtCmsGetRegistration(&(cal->rlist), user, args->prognum,
272             args->versnum, args->procnum, args->pid)) == NULL) {
273                 /* this client has not registered */
274
275                 if ((rinfo = _DtCmsMakeRegistrationInfo(user,
276                     args->update_type, args->prognum, args->versnum,
277                     args->procnum, args->pid)) == NULL) {
278                         res = CSA_E_INSUFFICIENT_MEMORY;
279                         return (&res);
280                 }
281
282                 /* put in the calendar's registration list */
283                 rinfo->next = cal->rlist;
284                 cal->rlist = rinfo;
285         } else {
286                 if (debug) {
287                         fprintf(stderr, "%s registered on %s, old types = %d\n",
288                                 user, args->cal, rinfo->types);
289                 }
290
291                 /* add new type to the registration */
292                 rinfo->types = rinfo->types | args->update_type;
293         }
294
295         if (debug) {
296                 fprintf(stderr, "%s registered on %s, types = %d\n",
297                         user, args->cal, rinfo->types);
298         }
299
300         res = CSA_SUCCESS;
301         return (&res);
302 }
303
304 extern  CSA_return_code *
305 cms_unregister_5_svc(cms_register_args *args, struct svc_req *svcrq)
306 {
307         static CSA_return_code  res;
308         char                    *user;
309         _DtCmsCalendar          *cal;
310         _DtCmsRegistrationInfo  *rinfo;
311
312         if (debug)
313                 fprintf(stderr, "cms_unregister_5_svc called\n");
314
315         if (args->cal == NULL ||
316             args->update_type >= (CSA_CB_ENTRY_UPDATED << 1)) {
317                 res = CSA_E_INVALID_PARAMETER;
318                 return (&res);
319         }
320
321         if ((res = _DtCmsGetClientInfo(svcrq, &user)) != CSA_SUCCESS)
322                 return (&res);
323
324         if ((res = _DtCmsGetCalendarByName(args->cal, B_FALSE, &cal))
325             != CSA_SUCCESS)
326                 return (&res);
327
328         if (cal == NULL || (rinfo = _DtCmsGetRegistration(&(cal->rlist), user,
329             args->prognum, args->versnum, args->procnum, args->pid)) == NULL) {
330                 res = CSA_E_CALLBACK_NOT_REGISTERED;
331                 return (&res);
332         }
333
334         /* update registration info */
335         if (debug) {
336                 fprintf(stderr, "%s registered on %s, old types = %d\n",
337                         user, args->cal, rinfo->types);
338         }
339
340         /* registered bits are cleared, unregistered bits are ignored */
341         rinfo->types = (rinfo->types | args->update_type) ^ args->update_type;
342
343         if (debug) {
344                 fprintf(stderr, "%s unregistered types %d on %s, new types = %d\n",
345                         user, args->update_type, args->cal, rinfo->types);
346         }
347
348         if (rinfo->types == 0) {
349                 cal->rlist = _DtCmsRemoveRegistration(cal->rlist, rinfo);
350         }
351
352         res = CSA_SUCCESS;
353         return (&res);
354 }
355
356 extern  cms_enumerate_calendar_attr_res *
357 cms_enumerate_calendar_attr_5_svc(buffer *args, struct svc_req *svcrq)
358 {
359         static cms_enumerate_calendar_attr_res  res;
360         _DtCmsCalendar          *cal;
361         char                    *user;
362         uint                    access;
363
364
365         if (debug)
366                 fprintf(stderr, "cms_enumerate_calendar_attr_5_svc called\n");
367
368         if (res.num_names > 0) {
369                 _DtCmsFreeCmsAttrNames(res.num_names, res.names);
370                 res.num_names = 0;
371         }
372
373         if ((res.stat = _DtCmsV5LoadAndCheckAccess(svcrq, *args, &user,
374             &access, &cal)) != CSA_SUCCESS)
375                 return (&res);
376
377         if (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION) {
378                 if (!_DTCMS_HAS_VIEW_CALENDAR_ATTR_ACCESS(access)) {
379                         res.stat = CSA_E_NO_AUTHORITY;
380                         return (&res);
381                 }
382
383                 res.stat = _DtCmsGetCalAttrNames(cal, &res.num_names,
384                                 &res.names);
385         } else {
386                 res.stat = _DtCmsGetOldCalAttrNames(cal, &res.num_names,
387                                 &res.names);
388         }
389
390         return (&res);
391 }
392
393 extern  cms_get_cal_attr_res *
394 cms_get_calendar_attr_5_svc(cms_get_cal_attr_args *args, struct svc_req *svcrq)
395 {
396         static cms_get_cal_attr_res     res;
397         _DtCmsCalendar          *cal;
398         char                    *user;
399         uint                    access;
400
401         if (debug)
402                 fprintf(stderr, "cms_get_calendar_attr_5_svc called\n");
403
404         if (res.num_attrs > 0) {
405                 _DtCm_free_cms_attributes(res.num_attrs, res.attrs);
406                 free(res.attrs);
407                 res.num_attrs = 0;
408         }
409
410         if ((res.stat = _DtCmsV5LoadAndCheckAccess(svcrq, args->cal, &user,
411             &access, &cal)) != CSA_SUCCESS)
412                 return (&res);
413
414         if (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION &&
415             !_DTCMS_HAS_VIEW_CALENDAR_ATTR_ACCESS(access)) {
416                 res.stat = CSA_E_NO_AUTHORITY;
417                 return (&res);
418         }
419
420         if (args->num_names > 0)
421                 res.stat = _DtCmsGetCalAttrsByName(cal, args->num_names,
422                                 args->names, &res.num_attrs, &res.attrs);
423         else
424                 res.stat = _DtCmsGetAllCalAttrs(cal, &res.num_attrs,
425                                 &res.attrs, B_TRUE);
426
427         return (&res);
428 }
429
430 extern  CSA_return_code *
431 cms_set_calendar_attr_5_svc(cms_set_cal_attr_args *args, struct svc_req *svcrq)
432 {
433         static CSA_return_code  res;
434         _DtCmsCalendar          *cal;
435         char                    *user;
436         uint                    access;
437
438         if (debug)
439                 fprintf(stderr, "cms_set_calendar_attr_5_svc called\n");
440
441         if ((res = _DtCmsV5LoadAndCheckAccess(svcrq, args->cal, &user,
442             &access, &cal)) != CSA_SUCCESS)
443                 return (&res);
444
445         if ((cal->fversion < _DtCM_FIRST_EXTENSIBLE_DATA_VERSION &&
446             !(access & CSA_OWNER_RIGHTS)) ||
447             (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION &&
448             !(access & (CSA_OWNER_RIGHTS | CSA_INSERT_CALENDAR_ATTRIBUTES |
449             CSA_CHANGE_CALENDAR_ATTRIBUTES)))) {
450                 res = CSA_E_NO_AUTHORITY;
451                 return (&res);
452         }
453
454         /* check argument */
455         if (args->cal == NULL || args->num_attrs == 0) {
456                 res = CSA_E_INVALID_PARAMETER;
457                 return (&res);
458         }
459
460         /* check validity of attribute values */
461         if ((res = _DtCm_check_cal_cms_attributes(
462             (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION ?
463             cal->fversion : _DtCM_FIRST_EXTENSIBLE_DATA_VERSION - 1),
464             args->num_attrs, args->attrs, NULL, NULL, B_TRUE, B_FALSE, B_TRUE))
465             != CSA_SUCCESS)
466                 return (&res);
467
468         if (cal->fversion >=_DtCM_FIRST_EXTENSIBLE_DATA_VERSION) {
469                 if ((res = _DtCmsUpdateCalAttributesAndLog(cal, args->num_attrs,
470                     args->attrs, access)) != CSA_SUCCESS)
471                 {
472                         return (&res);
473                 }
474         } else {
475                 int             i;
476                 Access_Entry_4  *v4list;
477
478                 /* for old format file, the only settable calendar attribute
479                  * is access list
480                  */
481                 for (i = args->num_attrs - 1; i >= 0; i--) {
482                         if (args->attrs[i].name.name)
483                                 break;
484                 }
485
486                 if (args->attrs[i].value == NULL ||
487                     args->attrs[i].value->item.access_list_value == NULL) {
488                         res = _DtCmsSetV4AccessListAndLog(cal, NULL);
489                 } else {
490                         if ((res = _DtCmsCmsAccessToV4Access(
491                             args->attrs[i].value->item.access_list_value,
492                             &v4list)) == CSA_SUCCESS)
493                                 res = _DtCmsSetV4AccessListAndLog(cal, v4list);
494                 }
495
496                 if (res != CSA_SUCCESS)
497                         return (&res);
498         }
499
500         /* do callback */
501         cal->rlist = _DtCmsDoUpdateCalAttrsCallback(cal->rlist, cal->calendar,
502                         user, args->num_attrs, args->attrs, args->pid);
503
504         /* reply */
505         res = CSA_SUCCESS;
506
507         return (&res);
508 }
509
510 extern  cms_archive_res *
511 cms_archive_5_svc(cms_archive_args *args, struct svc_req *svcrq)
512 {
513         static cms_archive_res res;
514
515         if (debug)
516                 fprintf(stderr, "cms_archive_5_svc called\n");
517
518         res.stat = CSA_E_NOT_SUPPORTED;
519         return (&res);
520 }
521
522 extern  CSA_return_code *
523 cms_restore_5_svc(cms_restore_args *args, struct svc_req *svcrq)
524 {
525         static CSA_return_code res;
526
527         if (debug)
528                 fprintf(stderr, "cms_restore_5_svc called\n");
529
530         res = CSA_E_NOT_SUPPORTED;
531         return (&res);
532 }
533
534 extern  cms_reminder_res *
535 cms_lookup_reminder_5_svc(cms_reminder_args *args, struct svc_req *svcrq)
536 {
537         static cms_reminder_res res;
538         _DtCmsCalendar          *cal;
539         char                    *user;
540         uint                    access;
541
542         if (debug)
543                 fprintf(stderr, "cms_lookup_reminder_5_svc called\n");
544
545         if (res.rems != NULL) {
546                 _DtCmsFreeReminderRef(res.rems);
547                 res.rems = NULL;
548         }
549
550         if ((res.stat = _DtCmsV5LoadAndCheckAccess(svcrq, args->cal, &user,
551             &access, &cal)) != CSA_SUCCESS)
552                 return (&res);
553
554         if ((access & CSA_OWNER_RIGHTS) == 0) {
555                 res.stat = CSA_E_NO_AUTHORITY;
556                 return (&res);
557         }
558
559         if (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION) {
560                 res.stat = _DtCmsLookupReminder(cal->remq, args->tick,
561                                 args->num_names, args->names, &res.rems);
562         } else {
563                 Reminder_4      *v4rem;
564                 _DtCmsEntryId   *ids;
565
566                 if ((res.stat = _DtCmsGetV4Reminders(cal, time(0), &v4rem,
567                     &ids)) == CSA_SUCCESS) {
568                         res.stat = _DtCmsV4ReminderToReminderRef(cal->calendar,
569                                         v4rem, ids, &res.rems);
570                         _DtCm_free_reminder4(v4rem);
571                         _DtCmsFreeEntryIds(ids);
572                 }
573         }
574
575         return (&res);
576 }
577
578 extern  cms_entries_res *
579 cms_lookup_entries_5_svc(cms_lookup_entries_args *args, struct svc_req *svcrq)
580 {
581         static cms_entries_res  res;
582         _DtCmsCalendar          *cal;
583         char                    *user;
584         uint                    access;
585         time_t                  start1, start2, end1, end2;
586         long                    id = 0;
587         boolean_t               no_match;
588         boolean_t               no_start_time_range, no_end_time_range;
589         CSA_uint32              hnum;
590         cms_attribute           *hattrs;
591         CSA_enum                *hops;
592
593         if (debug)
594                 fprintf(stderr, "cms_lookup_entries_5_svc called\n");
595
596         if (res.entries) {
597                 _DtCm_free_cms_entries(res.entries);
598                 res.entries = NULL;
599         }
600
601         if ((res.stat = _DtCmsV5LoadAndCheckAccess(svcrq, args->cal, &user,
602             &access, &cal)) != CSA_SUCCESS)
603                 return (&res);
604
605         if (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION &&
606             !_DTCMS_HAS_VIEW_ACCESS(access)) {
607                 res.stat = CSA_E_NO_AUTHORITY;
608                 return (&res);
609         }
610
611         /* check operator */
612         if ((res.stat = _DtCm_check_operator(args->num_attrs, NULL,
613             args->attrs, args->ops)) != CSA_SUCCESS)
614                 return (&res);
615
616         /* get time range */
617         if ((res.stat = _DtCmHashCriteria(
618             cal->fversion < _DtCM_FIRST_EXTENSIBLE_DATA_VERSION ?
619             _DtCm_entry_name_tbl : cal->entry_tbl, args->num_attrs,
620             NULL, args->attrs, args->ops, &no_match, &no_start_time_range,
621             &no_end_time_range, &start1, &start2, &end1, &end2, &id,
622             &hnum, &hattrs, &hops)) == CSA_E_INVALID_ATTRIBUTE) {
623
624                 /* attribute not defined in this calendar specified,
625                  * return NULL list
626                  */
627                 res.stat = CSA_SUCCESS;
628                 return (&res);
629
630         } else if (res.stat != CSA_SUCCESS || no_match == B_TRUE)
631                 return (&res);
632
633         /* do lookup */
634         if (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION) {
635                 if (id > 0)
636                         res.stat = _DtCmsLookupEntriesById(cal, user, access,
637                                         no_start_time_range, no_end_time_range,
638                                         start1, start2, end1, end2, id, hnum,
639                                         hattrs, hops, &res.entries);
640                 else
641                         res.stat = _DtCmsLookupEntries(cal, user, access,
642                                         start1, start2, no_end_time_range,
643                                         end1, end2, hnum, hattrs, hops,
644                                         &res.entries);
645         } else {
646                 Range_4 prange;
647                 Appt_4  *appt = NULL;;
648
649                 if (id > 0) {
650                         res.stat = _DtCmsLookupKeyrangeV4(cal, user, access,
651                                         no_start_time_range, no_end_time_range,
652                                         start1, start2, end1, end2, id,
653                                         _DtCm_match_one_appt, hnum, hattrs,
654                                         hops, &appt, NULL);
655                 } else {
656                         prange.key1 = start1;
657                         prange.key2 = start2;
658                         prange.next = NULL;
659                         res.stat = _DtCmsLookupRangeV4(cal, user, access,
660                                         &prange, no_end_time_range, end1, end2,
661                                         _DtCm_match_one_appt, hnum,
662                                         hattrs, hops, &appt, NULL);
663                 }
664
665                 if (res.stat == CSA_SUCCESS && appt) {
666                         res.stat = _DtCmsAppt4ToCmsentriesForClient(args->cal,
667                                         appt, &res.entries);
668                         _DtCm_free_appt4(appt);
669                 }
670         }
671
672         _DtCmFreeHashedArrays(hnum, hattrs, hops);
673
674         return (&res);
675 }
676
677 extern  cms_entries_res *
678 cms_enumerate_sequence_5_svc(cms_enumerate_args *args, struct svc_req *svcrq)
679 {
680         static cms_entries_res  res;
681         _DtCmsCalendar          *cal;
682         char                    *user;
683         uint                    access;
684
685         if (debug)
686                 fprintf(stderr, "cms_enumerate_sequence_5_svc called\n");
687
688         if (res.entries) {
689                 _DtCm_free_cms_entries(res.entries);
690                 res.entries = NULL;
691         }
692
693         if ((res.stat = _DtCmsV5LoadAndCheckAccess(svcrq, args->cal, &user,
694             &access, &cal)) != CSA_SUCCESS)
695                 return (&res);
696
697         if (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION &&
698             !_DTCMS_HAS_VIEW_ACCESS(access)) {
699                 res.stat = CSA_E_NO_AUTHORITY;
700                 return (&res);
701         }
702
703         /* do lookup */
704         if (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION) {
705                 res.stat = _DtCmsEnumerateSequenceById(cal, user, access,
706                                 B_FALSE, B_TRUE, args->start, args->end,
707                                 0, 0, args->id, 0, NULL, NULL, &res.entries);
708         } else {
709                 Range_4 prange;
710                 Appt_4 *appt = NULL;
711                 res.stat = _DtCmsLookupKeyrangeV4(cal, user, access,
712                                 B_FALSE, B_TRUE, args->start, args->end,
713                                 0, 0, args->id, NULL, 0, NULL, NULL, &appt,
714                                 NULL);
715
716                 if (res.stat == CSA_SUCCESS && appt) {
717                         res.stat = _DtCmsAppt4ToCmsentriesForClient(args->cal,
718                                         appt, &res.entries);
719                         _DtCm_free_appt4(appt);
720                 }
721         }
722
723         return (&res);
724 }
725
726 extern  cms_get_entry_attr_res *
727 cms_get_entry_attr_5_svc(cms_get_entry_attr_args *args, struct svc_req *svcrq)
728 {
729         static cms_get_entry_attr_res   res;
730         _DtCmsCalendar          *cal;
731         char                    *user;
732         uint                    access;
733
734         if (debug)
735                 fprintf(stderr, "cms_get_entry_attr_5_svc called\n");
736
737         if (res.entries) {
738                 _DtCmsFreeEntryAttrResItem(res.entries);
739                 res.entries = NULL;
740         }
741
742         if ((res.stat = _DtCmsV5LoadAndCheckAccess(svcrq, args->cal, &user,
743             &access, &cal)) != CSA_SUCCESS)
744                 return (&res);
745
746         if (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION &&
747             !_DTCMS_HAS_VIEW_ACCESS(access)) {
748                 res.stat = CSA_E_NO_AUTHORITY;
749                 return (&res);
750         }
751
752         /* check operator */
753         if (args->num_keys == 0) {
754                 res.stat = CSA_E_INVALID_PARAMETER;
755                 return (&res);
756         }
757
758         if (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION) {
759                 res.stat = _DtCmsLookupEntriesByKey(cal, user, access,
760                         args->num_keys, args->keys, args->num_names,
761                         args->names, &res.entries);
762         } else {
763                 res.stat = CSA_E_NOT_SUPPORTED;
764         }
765
766         return (&res);
767 }
768
769 extern  cms_entry_res *
770 cms_insert_entry_5_svc(cms_insert_args *args, struct svc_req *svcrq)
771 {
772         static cms_entry_res    res;
773         _DtCmsCalendar          *cal;
774         cms_entry               *entry;
775         cms_key                 key;
776         char                    *user;
777         uint                    access, needaccess;
778         Appt_4                  *appt;
779
780         if (debug)
781                 fprintf(stderr, "cms_insert_entry_5_svc called\n");
782
783         if (res.entry != NULL) {
784                 res.entry->num_attrs--;
785                 _DtCm_free_cms_entry(res.entry);
786                 res.entry = NULL;
787         }
788
789         if ((res.stat = _DtCmsV5LoadAndCheckAccess(svcrq, args->cal, &user,
790             &access, &cal)) != CSA_SUCCESS)
791                 return (&res);
792
793         if ((cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION &&
794             !_DTCMS_HAS_INSERT_ACCESS(access)) ||
795             (cal->fversion < _DtCM_FIRST_EXTENSIBLE_DATA_VERSION &&
796             !_DTCMS_HAS_V4_WRITE_ACCESS(access))) {
797                 res.stat = CSA_E_NO_AUTHORITY;
798                 return (&res);
799         }
800
801         /* check argument */
802         if (args->cal == NULL || args->num_attrs == 0) {
803                 res.stat = CSA_E_INVALID_PARAMETER;
804                 return (&res);
805         }
806
807         /* check validity of attribute values */
808         if ((res.stat = _DtCm_check_entry_cms_attributes(
809             (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION ?
810             cal->fversion : _DtCM_FIRST_EXTENSIBLE_DATA_VERSION - 1),
811             args->num_attrs, args->attrs, CSA_CB_ENTRY_ADDED, B_TRUE))
812             != CSA_SUCCESS)
813                 return (&res);
814
815         if (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION) {
816                 if ((res.stat = _DtCmsMakeHashedEntry(cal, args->num_attrs,
817                     args->attrs, &entry)) != CSA_SUCCESS)
818                         return (&res);
819
820                 if ((res.stat = _DtCmsCheckInitialAttributes(entry))
821                     != CSA_SUCCESS) {
822                         _DtCm_free_cms_entry(entry);
823                         return (&res);
824                 }
825
826                 /* check access rights */
827                 needaccess = _DtCmsClassToInsertAccess(entry);
828                 if ((access & (CSA_OWNER_RIGHTS | needaccess)) == 0) {
829                         _DtCm_free_cms_entry(entry);
830                         res.stat = CSA_E_NO_AUTHORITY;
831                         return (&res);
832                 }
833
834                 /* set organizer */
835                 if ((res.stat = _DtCm_set_string_attrval(user,
836                     &entry->attrs[CSA_ENTRY_ATTR_ORGANIZER_I].value,
837                     CSA_VALUE_CALENDAR_USER)) != CSA_SUCCESS) {
838                         _DtCm_free_cms_entry(entry);
839                         return (&res);
840                 }
841
842                 /* insert entry and log it */
843                 if ((res.stat = _DtCmsInsertEntryAndLog(cal, entry))
844                     != CSA_SUCCESS) {
845                         _DtCm_free_cms_entry(entry);
846                         return (&res);
847                 }
848
849                 key = entry->key;
850         } else {
851                 if ((appt = _DtCm_make_appt4(B_TRUE)) == NULL) {
852                         res.stat = CSA_E_INSUFFICIENT_MEMORY;
853                         return (&res);
854                 }
855  
856                 if ((res.stat = _DtCmsAttrsToAppt4(args->num_attrs,
857                     args->attrs, appt, B_TRUE)) != CSA_SUCCESS) {
858                         _DtCm_free_appt4(appt);
859                         return (&res);
860                 }
861
862                 if (appt->author) free(appt->author);
863                 if ((appt->author = strdup(user)) == NULL) {
864                         _DtCm_free_appt4(appt);
865                         return (&res);
866                 }
867
868                 /*
869                  * calculate the correct start day,
870                  */
871                 _DtCms_adjust_appt_startdate(appt);
872
873                 if ((res.stat = _DtCmsInsertApptAndLog(cal, appt))
874                     != CSA_SUCCESS) {
875                         _DtCm_free_appt4(appt);
876                         return (&res);
877                 }
878
879                 key.id = appt->appt_id.key;
880                 key.time = appt->appt_id.tick;
881         }
882
883         if (res.stat == CSA_SUCCESS)
884                 cal->modified = B_TRUE;
885         else
886                 return (&res);
887
888         /* do callback */
889         cal->rlist = _DtCmsDoV1CbForV4Data(cal->rlist, user, args->pid,
890                         &key, NULL);
891
892         cal->rlist = _DtCmsDoInsertEntryCallback(cal->rlist, cal->calendar,
893                         user, key.id, args->pid);
894
895         /* reply */
896         if (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION ||
897             (cal->fversion < _DtCM_FIRST_EXTENSIBLE_DATA_VERSION &&
898             (res.stat = _DtCmsAppt4ToCmsentriesForClient(cal->calendar, appt,
899             &entry)) == CSA_SUCCESS)) {
900
901                 res.stat = _DtCmsGetCmsEntryForClient(entry, &res.entry, B_FALSE);
902
903                 _DtCm_free_cms_entry(entry);
904         }
905
906         return (&res);
907 }
908
909 extern  cms_entry_res *
910 cms_update_entry_5_svc(cms_update_args *args, struct svc_req *svcrq)
911 {
912         static cms_entry_res    res;
913         _DtCmsCalendar          *cal;
914         cms_entry               *newentry;
915         caddr_t                 event;
916         char                    *user;
917         uint                    access, needaccess;
918         Appt_4                  *appt = NULL;
919
920         if (debug)
921                 fprintf(stderr, "cms_update_entry_5_svc called\n");
922
923         if (res.entry != NULL) {
924                 res.entry->num_attrs--;
925                 _DtCm_free_cms_entry(res.entry);
926                 res.entry = NULL;
927         }
928
929         if ((res.stat = _DtCmsV5LoadAndCheckAccess(svcrq, args->cal, &user,
930             &access, &cal)) != CSA_SUCCESS)
931                 return (&res);
932
933         if ((cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION &&
934             !_DTCMS_HAS_CHANGE_ACCESS(access)) ||
935             (cal->fversion < _DtCM_FIRST_EXTENSIBLE_DATA_VERSION &&
936             !_DTCMS_HAS_V4_WRITE_ACCESS(access))) {
937                 res.stat = CSA_E_NO_AUTHORITY;
938                 return (&res);
939         }
940
941         /* check argument */
942         if (args->cal == NULL || args->entry.id <= 0 || args->num_attrs == 0) {
943                 res.stat = CSA_E_INVALID_PARAMETER;
944                 return (&res);
945         }
946
947         if (args->scope < CSA_SCOPE_ALL || args->scope > CSA_SCOPE_FORWARD) {
948                 res.stat = CSA_E_INVALID_ENUM;
949                 return (&res);
950         }
951
952         /* check validity of attribute values */
953         if ((res.stat = _DtCm_check_entry_cms_attributes(
954             (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION ?
955             cal->fversion : _DtCM_FIRST_EXTENSIBLE_DATA_VERSION - 1),
956             args->num_attrs, args->attrs, CSA_CB_ENTRY_UPDATED, B_TRUE))
957             != CSA_SUCCESS)
958                 return (&res);
959
960         /* get event from one-time event tree */
961         event = (caddr_t)rb_lookup(cal->tree, (caddr_t)&args->entry);
962
963         /* update entry and log it */
964         if (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION) {
965                 if (event != NULL || args->scope == CSA_SCOPE_ALL) {
966                         res.stat = _DtCmsUpdateEntry(cal, user, access,
967                                         &args->entry, args->num_attrs,
968                                         args->attrs, NULL, &newentry);
969                 } else {
970                         res.stat = _DtCmsUpdateInstances(cal, user, access,
971                                         &args->entry, args->scope,
972                                         args->num_attrs, args->attrs, NULL,
973                                         &newentry);
974                 }
975         } else {
976                 Options_4       opt;
977
978                 if (event != NULL || args->scope == CSA_SCOPE_ALL)
979                         opt = do_all_4;
980                 else
981                         opt = (args->scope == CSA_SCOPE_ONE) ?
982                                 do_one_4 : do_forward_4;
983
984                 if (event == NULL && (event = hc_lookup(cal->list,
985                     (caddr_t)&args->entry)) == NULL) {
986
987                         res.stat = CSA_X_DT_E_ENTRY_NOT_FOUND;
988
989                 } else if ((appt = _DtCm_copy_one_appt4((Appt_4 *)event))
990                     == NULL) {
991
992                         res.stat = CSA_E_INSUFFICIENT_MEMORY; 
993
994                 } else {
995                         /* get rid of exceptions */
996                         _DtCm_free_excpt4(appt->exception);
997                         appt->exception = NULL;
998
999                         if ((res.stat = _DtCmsAttrsToAppt4(args->num_attrs,
1000                             args->attrs, appt, B_TRUE)) == CSA_SUCCESS) {
1001
1002                                 if (opt == do_all_4)
1003                                         res.stat = _DtCmsChangeAll(cal, user,
1004                                                 access, (Id_4 *)&args->entry,
1005                                                 appt, NULL);
1006                                 else
1007                                         res.stat = _DtCmsChangeSome(cal, user,
1008                                                 access, (Id_4 *)&args->entry,
1009                                                 appt, opt, NULL);
1010
1011                         }
1012                 }
1013         }
1014
1015         if (res.stat == CSA_SUCCESS)
1016                 cal->modified = B_TRUE;
1017         else
1018                 return (&res);
1019
1020         /* do callback */
1021         cal->rlist = _DtCmsDoV1CbForV4Data(cal->rlist, user, args->pid,
1022                         &args->entry,
1023                         (appt ? (cms_key *)&appt->appt_id : &newentry->key));
1024
1025         cal->rlist = _DtCmsDoUpdateEntryCallback(cal->rlist, cal->calendar,
1026                         user, (appt ? appt->appt_id.key : newentry->key.id),
1027                         args->entry.id, args->scope, args->entry.time,
1028                         args->pid);
1029
1030         /* reply */
1031         if (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION ||
1032             (cal->fversion < _DtCM_FIRST_EXTENSIBLE_DATA_VERSION &&
1033             (res.stat = _DtCmsAppt4ToCmsentriesForClient(cal->calendar, appt,
1034             &newentry)) == CSA_SUCCESS)) {
1035
1036                 res.stat = _DtCmsGetCmsEntryForClient(newentry, &res.entry, B_FALSE);
1037
1038                 _DtCm_free_cms_entry(newentry);
1039         }
1040
1041         return (&res);
1042 }
1043
1044 extern  CSA_return_code *
1045 cms_delete_entry_5_svc(cms_delete_args *args, struct svc_req *svcrq)
1046 {
1047         static CSA_return_code  res;
1048         _DtCmsCalendar          *cal;
1049         caddr_t                 event;
1050         char                    *user;
1051         uint                    access, needaccess;
1052
1053         if (debug)
1054                 fprintf(stderr, "cms_delete_entry_5_svc called\n");
1055
1056         if ((res = _DtCmsV5LoadAndCheckAccess(svcrq, args->cal, &user,
1057             &access, &cal)) != CSA_SUCCESS)
1058                 return (&res);
1059
1060         /* for v3 data, authority check is done in the routines doing the
1061          * deletion since we may need to check the organizer
1062          */
1063         if (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION &&
1064             !_DTCMS_HAS_CHANGE_ACCESS(access)) {
1065                 res = CSA_E_NO_AUTHORITY;
1066                 return (&res);
1067         }
1068
1069         /* check argument */
1070         if (args->cal == NULL || args->entry.id <= 0) {
1071                 res = CSA_E_INVALID_PARAMETER;
1072                 return (&res);
1073         }
1074
1075         if (args->scope < CSA_SCOPE_ALL || args->scope > CSA_SCOPE_FORWARD) {
1076                 res = CSA_E_INVALID_ENUM;
1077                 return (&res);
1078         }
1079
1080
1081         /* get event from one-time event tree */
1082         event = (caddr_t)rb_lookup(cal->tree, (caddr_t)&args->entry);
1083
1084         /* delete entry and log it */
1085         if (cal->fversion >= _DtCM_FIRST_EXTENSIBLE_DATA_VERSION) {
1086                 if (event != NULL || args->scope == CSA_SCOPE_ALL) {
1087                         res = _DtCmsDeleteEntryAndLog(cal, user, access,
1088                                 &args->entry, NULL);
1089                 } else {
1090                         res = _DtCmsDeleteInstancesAndLog(cal, user, access,
1091                                 &args->entry, args->scope, NULL, NULL);
1092                 }
1093         } else {
1094                 if (event != NULL || args->scope == CSA_SCOPE_ALL) {
1095                         res = _DtCmsDeleteApptAndLog(cal, user, access,
1096                                 (Id_4 *)&args->entry, NULL);
1097                 } else {
1098                         res = _DtCmsDeleteApptInstancesAndLog(cal, user, access,
1099                                 (Id_4 *)&args->entry,
1100                                 ((args->scope == CSA_SCOPE_ONE) ? do_one_4 :
1101                                 do_forward_4), NULL, NULL);
1102                 }
1103         }
1104
1105         if (res == CSA_SUCCESS)
1106                 cal->modified = B_TRUE;
1107         else
1108                 return (&res);
1109
1110         /* do callback */
1111         cal->rlist = _DtCmsDoV1CbForV4Data(cal->rlist, user, args->pid,
1112                         &args->entry, NULL);
1113
1114         cal->rlist = _DtCmsDoDeleteEntryCallback(cal->rlist, cal->calendar,
1115                         user, args->entry.id, args->scope, args->entry.time,
1116                         args->pid);
1117
1118
1119         return (&res);
1120 }
1121
1122 void initfunctable(program_handle ph)
1123 {
1124         ph->program_num = TABLEPROG;
1125         ph->prog[TABLEVERS].vers = &tableprog_5_table[0];
1126         ph->prog[TABLEVERS].nproc = sizeof(tableprog_5_table)/sizeof(tableprog_5_table[0]);
1127 }
1128
1129 /*****************************************************************************
1130  * static functions used within the file
1131  *****************************************************************************/
1132
1133 static CSA_return_code
1134 _DtCmsCreateCallog(char *user, cms_create_args *args, _DtCmsCalendar **newcal)
1135 {
1136         CSA_return_code stat;
1137         _DtCmsCalendar  *cal;
1138         int             i, index;
1139         char            datestr[80];
1140         char            *calname;
1141         char            *log;
1142         char            *username;
1143         cms_attribute_value val;
1144         cms_access_entry aentry;
1145         int             nidx = 0, oidx = 0;
1146         char            *name, *owner;
1147
1148         /*
1149          * if calendar name is a user name, make sure that
1150          * it's the same as the sender.
1151          */
1152         calname = _DtCmGetPrefix(args->cal, '@');
1153         username = _DtCmGetPrefix(user, '@');
1154
1155         if (_DtCmIsUserName(calname) && strcmp(calname, username)) {
1156                 free(calname);
1157                 free(username);
1158                 return (CSA_E_NO_AUTHORITY);
1159         }
1160         log = _DtCmsGetLogFN(calname);
1161         free(calname);
1162         free(username);
1163
1164         /* create internal calendar data structure */
1165         if ((cal = _DtCmsMakeCalendar(user, args->cal)) == NULL) {
1166                 free(log);
1167                 return (CSA_E_INSUFFICIENT_MEMORY);
1168         }
1169
1170         /* fill in information */
1171         _csa_tick_to_iso8601(time(0), datestr);
1172
1173         if ((stat = _DtCm_set_string_attrval(datestr,
1174             &cal->attrs[CSA_CAL_ATTR_DATE_CREATED_I].value,
1175             CSA_VALUE_DATE_TIME)) != CSA_SUCCESS) {
1176                 _DtCmsFreeCalendar(cal);
1177                 free(log);
1178                 return (stat);
1179         }
1180
1181         /* initialize access list to be "WORLD", VIEW_PUBLIC */
1182         aentry.user = WORLD;
1183         aentry.rights = CSA_VIEW_PUBLIC_ENTRIES;
1184         aentry.next = NULL;
1185         val.item.access_list_value = &aentry;
1186         val.type = CSA_VALUE_ACCESS_LIST;
1187
1188         if ((stat = _DtCmUpdateAccessListAttrVal(&val,
1189             &cal->attrs[CSA_CAL_ATTR_ACCESS_LIST_I].value)) != CSA_SUCCESS) {
1190                 _DtCmsFreeCalendar(cal);
1191                 free(log);
1192                 return (stat);
1193         }
1194
1195         /* set product identifier */
1196         if ((stat = _DtCm_set_string_attrval(_DtCM_PRODUCT_IDENTIFIER,
1197             &cal->attrs[CSA_CAL_ATTR_PRODUCT_IDENTIFIER_I].value,
1198             CSA_VALUE_STRING)) != CSA_SUCCESS) {
1199                 _DtCmsFreeCalendar(cal);
1200                 free(log);
1201                 return (stat);
1202         }
1203
1204         /* set CSA version */
1205         if ((stat = _DtCm_set_string_attrval(_DtCM_SPEC_VERSION_SUPPORTED,
1206             &cal->attrs[CSA_CAL_ATTR_VERSION_I].value, CSA_VALUE_STRING))
1207             != CSA_SUCCESS) {
1208                 _DtCmsFreeCalendar(cal);
1209                 free(log);
1210                 return (stat);
1211         }
1212
1213         /* we dont use the values specified by client */
1214         for (i = 0; i < args->num_attrs; i++) {
1215                 if (strcmp(args->attrs[i].name.name,
1216                     CSA_CAL_ATTR_CALENDAR_NAME) == 0) {
1217                         nidx = i;
1218                         name = args->attrs[i].name.name;
1219                         args->attrs[i].name.name = NULL;
1220                 } else if (strcmp(args->attrs[i].name.name,
1221                     CSA_CAL_ATTR_CALENDAR_OWNER) == 0) {
1222                         oidx = i;
1223                         owner = args->attrs[i].name.name;
1224                         args->attrs[i].name.name = NULL;
1225                 }
1226         }
1227
1228         /* initialize calendar attribute with info provided by caller */
1229         if ((stat = _DtCmUpdateAttributes(args->num_attrs, args->attrs,
1230             &cal->num_attrs, &cal->attrs, &cal->cal_tbl, B_TRUE,
1231             NULL, B_FALSE)) != CSA_SUCCESS) {
1232                 _DtCmsFreeCalendar(cal);
1233                 free(log);
1234                 return (stat);
1235         }
1236
1237         if (nidx) args->attrs[nidx].name.name = name;
1238         if (oidx) args->attrs[oidx].name.name = owner;
1239
1240         /* use passed in char set if client does not supply one */
1241         if (cal->attrs[CSA_CAL_ATTR_CHARACTER_SET_I].value == NULL &&
1242              args->char_set && *args->char_set != '\0') {
1243                 if ((stat = _DtCm_set_string_attrval(args->char_set,
1244                     &cal->attrs[CSA_CAL_ATTR_CHARACTER_SET_I].value,
1245                     CSA_VALUE_STRING)) != CSA_SUCCESS) {
1246                         _DtCmsFreeCalendar(cal);
1247                         free(log);
1248                         return (stat);
1249                 }
1250         }
1251
1252         /* create file */
1253         if ((stat = _DtCmsCreateLogV2(user, log)) != CSA_SUCCESS) {
1254                 _DtCmsFreeCalendar(cal);
1255                 free(log);
1256                 return (stat);
1257         }
1258
1259         /* dump file */
1260         if ((stat = _DtCmsAppendCalAttrsByFN(log, cal->num_attrs, cal->attrs))
1261             != CSA_SUCCESS) {
1262                 free(log);
1263                 unlink(log);
1264                 _DtCmsFreeCalendar(cal);
1265                 return (stat);
1266         }
1267         free(log);
1268
1269         _DtCmsPutInCalList(cal);
1270
1271         *newcal = cal;
1272
1273         return (stat);
1274 }
1275
1276 #define _NAME_INCREMENT 10
1277
1278 static CSA_return_code
1279 _ListCalendarNames(uint *num_names, char ***names)
1280 {
1281         uint    num = 0, count = 0;
1282         char    **names_r = NULL;
1283         DIR     *dirp;
1284         struct  dirent *dp;
1285
1286         if ((dirp = opendir(".")) == NULL)
1287                 return (CSA_E_FAILURE);
1288
1289 #ifdef SunOS
1290         if ((dp = (struct dirent *)malloc(sizeof(struct dirent) + FILENAME_MAX))
1291             == NULL) {
1292                 closedir(dirp);
1293                 return (CSA_E_INSUFFICIENT_MEMORY);
1294         }
1295
1296         while (dp = readdir_r(dirp, dp)) {
1297 #else
1298         while (dp = readdir(dirp)) {
1299 #endif
1300                 if (strncmp(dp->d_name, "callog.", strlen("callog.")) == 0) {
1301                         if (count == num) {
1302                                 count += _NAME_INCREMENT;
1303                                 if ((names_r = (char **)_grow_char_array(
1304                                     names_r, sizeof(char *) * count,
1305                                     sizeof(char *) * (count + _NAME_INCREMENT)))
1306                                     == NULL) {
1307                                         if (num > 0) {
1308                                                 _free_char_array(
1309                                                         num, names_r);
1310                                         }
1311                                         closedir(dirp);
1312                                         return (CSA_E_INSUFFICIENT_MEMORY);
1313                                 }
1314                         }
1315                         if ((names_r[num++] = strdup(dp->d_name)) == NULL) {
1316                                 _free_char_array(num, names_r);
1317                                 closedir(dirp);
1318                                 return (CSA_E_INSUFFICIENT_MEMORY);
1319                         }
1320                 }
1321         }
1322
1323         if (num > 0) {
1324                 *num_names = num;
1325                 *names = names_r;
1326         } else if (count > 0)
1327                 free(names_r);
1328
1329         return (CSA_SUCCESS);
1330 }
1331
1332 static void *
1333 _grow_char_array(void *ptr, uint oldcount, uint newcount)
1334 {
1335         void *nptr;
1336
1337         if (nptr = realloc(ptr, newcount)) {
1338                 memset((void *)((char *)nptr + oldcount), 0,
1339                         newcount - oldcount);
1340                 return (nptr);
1341         } else
1342                 return (NULL);
1343 }
1344
1345 static void
1346 _free_char_array(uint num_elem, char **ptr)
1347 {
1348         int i;
1349
1350         if (num_elem == 0)
1351                 return;
1352
1353         for (i = 0; i < num_elem; i++) {
1354                 if (ptr[i])
1355                         free(ptr[i]);
1356                 else
1357                         break;
1358         }
1359
1360         free(ptr);
1361 }
1362
1363 /*
1364  * This routine assumes that the attributes are hashed already
1365  */
1366 static CSA_return_code
1367 _DtCmsGetOldCalAttrNames(
1368         _DtCmsCalendar  *cal,
1369         uint            *num_names_r,
1370         cms_attr_name   **names_r)
1371 {
1372         CSA_return_code stat = CSA_SUCCESS;
1373         cms_attr_name   *names;
1374         uint            i, j;
1375
1376         if ((names = (cms_attr_name *)calloc(1,
1377             sizeof(cms_attr_name)*_DtCM_OLD_CAL_ATTR_SIZE)) == NULL)
1378                 return (CSA_E_INSUFFICIENT_MEMORY);
1379
1380         for (i = 1, j = 0; i <= _DtCM_DEFINED_CAL_ATTR_SIZE; i++) {
1381                 if ((_CSA_cal_attr_info[i].fst_vers > 0 &&
1382                     _CSA_cal_attr_info[i].fst_vers <= cal->fversion)
1383                     || i == CSA_CAL_ATTR_CALENDAR_SIZE_I) {
1384                         if ((names[j].name =
1385                             strdup(_CSA_calendar_attribute_names[i])) == NULL)
1386                         {
1387                                 _DtCmsFreeCmsAttrNames(j, names);
1388                                 return (CSA_E_INSUFFICIENT_MEMORY);
1389                         } else {
1390                                 names[j].num = i;
1391                                 j++;
1392                         }
1393                 }
1394         }
1395
1396         *num_names_r = j;
1397         *names_r = names;
1398
1399         return (CSA_SUCCESS);
1400 }
1401