Merge branch 'linux1'
[oweals/cde.git] / cde / programs / dtcm / server / log.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: log.c /main/8 1996/11/21 19:45:13 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 #include <EUSCompat.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <pwd.h>
37 #include <unistd.h>
38 #include <sys/stat.h>
39 #include <sys/file.h>
40 #define XOS_USE_NO_LOCKING
41 #define X_INCLUDE_TIME_H
42 #if defined(linux)
43 #undef SVR4
44 #endif
45 #include <X11/Xos_r.h>
46 #include "cm.h"
47 #include "rtable4.h"
48 #include "log.h"
49 #include "iso8601.h"
50
51 extern uid_t    daemon_uid;
52 extern gid_t    daemon_gid;
53 extern char     *pgname;
54 extern int      debug;
55 static char     *spool_dir = _DtCMS_DEFAULT_DIR;
56
57 char *tag_string[] = {
58         "0:boolean",
59         "1:enum",
60         "2:flags",
61         "3:sint32",
62         "4:uint32",
63         "5:string",
64         "6:user",
65         "7:date_time",
66         "8:date_time_range",
67         "9:time_duration",
68         "10:access_list",
69         "11:attendees",
70         "12:date_time_list",
71         "13:reminder",
72         "14:opaque_data",
73         NULL
74 };
75
76 /*
77  * forward declaration of functions used within this file
78  */
79 static char * cr_to_str(char *s, int size);
80 static void periodtostr(Interval_4 i, char *q);
81 static void privacytostr(Privacy_Level_4 p, char *q);
82 static void apptstatustostr(Appt_Status_4 p, char *q);
83 static void apptstatustostr(Appt_Status_4 p, char *q);
84 static void tagstostr(Event_Type_4 p, char *q);
85 static char * get_fname (char *dir, char *fname, char *who);
86 static CSA_return_code append_log(int f, char *buf);
87 static CSA_return_code create_log(char *owner, char *file, int version);
88 static char *attrs_to_attrliststr(int len, cms_attribute *attrs,
89                                 boolean_t write_hash, boolean_t entryattrs);
90 static char *grow_n_concat(char *base, char *newstr, int newcount);
91 static char *get_access_list_string(cms_access_entry *list);
92 static char *get_date_time_list_string(CSA_date_time_list list);
93
94
95 /*
96  * extern functions
97  */
98
99 extern char *
100 _DtCmsGetLogFN(char *who)
101 {
102         return (get_fname (spool_dir, _DtCMS_DEFAULT_LOG, who));
103 }
104  
105 extern char *
106 _DtCmsGetBakFN(char *who)
107 {
108         return (get_fname (spool_dir, _DtCMS_DEFAULT_BAK, who));
109 }
110  
111 extern char *
112 _DtCmsGetTmpFN(char *who)
113 {
114         return (get_fname (spool_dir, _DtCMS_DEFAULT_TMP, who));
115 }
116
117 extern char *
118 _DtCmsGetDelFN(char *who)
119 {
120         return (get_fname (spool_dir, _DtCMS_DEFAULT_DEL, who));
121 }
122
123 extern CSA_return_code
124 _DtCmsCreateLogV1(char *owner, char *file)
125 {
126         return (create_log(owner, file, _DtCMS_VERSION1));
127 }
128
129
130 extern CSA_return_code
131 _DtCmsAppendHTableByFN(char *file, uint size, char **names, int *types)
132 {
133         int     f;
134         CSA_return_code stat;
135
136         if ((f = open(file, O_WRONLY | O_APPEND | O_SYNC)) < 0) {
137                 perror(pgname);
138                 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
139         }
140
141         stat = _DtCmsAppendHTableByFD(f, size, names, types);
142
143         close(f);
144         return (stat);
145 }
146
147 /*
148  * the first element of the arrays is not used
149  */
150 extern CSA_return_code
151 _DtCmsAppendHTableByFD(int fd, uint size, char **names, int *types)
152 {
153         CSA_return_code stat;
154         char    *tptr, *buf, tmpbuf[BUFSIZ/4];
155         int     i, tcount, count = 15;  /* strlen("(entrytable )\n") + 1 */
156
157         if ((buf = malloc(BUFSIZ)) == NULL) return (CSA_E_INSUFFICIENT_MEMORY);
158         tcount = BUFSIZ;
159         strcpy(buf, "(entrytable ");
160
161         for (i = 1; i <= size; i++) {
162                 sprintf(tmpbuf, "(%d \"%s\" \"%s\")\n", i, names[i],
163                         tag_string[types[i]]);
164
165                 count += strlen(tmpbuf);
166                 if (tcount < count) {
167                         if ((tptr = grow_n_concat(buf, tmpbuf, tcount + BUFSIZ))
168                             == NULL) {
169                                 free(buf);
170                                 return (CSA_E_INSUFFICIENT_MEMORY);
171                         }
172                         buf = tptr;
173                         tcount += BUFSIZ;
174                 } else
175                         strcat(buf, tmpbuf);
176         }
177
178         strcat(buf, ")\n");
179
180         stat = append_log(fd, buf);
181
182         free(buf);
183         return (stat);
184 }
185
186 extern CSA_return_code
187 _DtCmsAppendEntryByFN(char *file, cms_entry *entry, _DtCmsLogOps op)
188 {
189         int     f;
190         CSA_return_code stat;
191
192         if ((f = open(file, O_WRONLY | O_APPEND | O_SYNC)) < 0) {
193                 perror(pgname);
194                 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
195         }
196
197         stat = _DtCmsAppendEntryByFD(f, entry, op);
198
199         close(f);
200         return (stat);
201 }
202
203 extern CSA_return_code
204 _DtCmsAppendEntryByFD(int f, cms_entry *entry, _DtCmsLogOps op)
205 {
206         CSA_return_code stat;
207         int     count;
208         char    *cptr, *tptr, *buf;
209         char    isotime[25];
210
211         if ((buf = malloc(100)) == NULL) return (CSA_E_INSUFFICIENT_MEMORY);
212
213         _csa_tick_to_iso8601(entry->key.time, isotime);
214         sprintf(buf, "(%s \"%s\" key: %ld%s",
215                 (op == _DtCmsLogAdd ? "add" : "remove"),
216                 isotime, entry->key.id,
217                 (op == _DtCmsLogAdd ? "\nhashedattributes: (" : ""));
218         count = strlen(buf) + 4;
219
220         if (op == _DtCmsLogAdd && entry->num_attrs > 0) {
221                 if ((cptr = attrs_to_attrliststr(entry->num_attrs,
222                     entry->attrs, B_TRUE, B_TRUE)) == NULL) {
223                         free(buf);
224                         return (CSA_E_INSUFFICIENT_MEMORY);
225                 }
226                 count += strlen(cptr);
227                 if ((tptr = grow_n_concat(buf, cptr, count)) == NULL) {
228                         free(cptr);
229                         free(buf);
230                         return (CSA_E_INSUFFICIENT_MEMORY);
231                 }
232                 free(cptr);
233                 buf = tptr;
234         }
235
236         /* closing for attr list and closing for entry */
237         if (op == _DtCmsLogAdd)
238                 strcat(buf, "))\n");
239         else
240                 strcat(buf, ")\n");
241
242         stat = append_log(f, buf);
243
244         free(buf);
245         return (stat);
246 }
247
248 extern CSA_return_code
249 _DtCmsAppendCalAttrsByFN(char *file, int size, cms_attribute * attrs)
250 {
251         int     f;
252         CSA_return_code stat;
253
254         if (file == NULL || size <= 0)
255                 return (CSA_E_INVALID_PARAMETER);
256
257         if ((f = open(file, O_WRONLY | O_APPEND | O_SYNC)) < 0) {
258                 perror(pgname);
259                 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
260         }
261
262         stat = _DtCmsAppendCalAttrsByFD(f, size, attrs);
263
264         close(f);
265         return (stat);
266 }
267
268 extern CSA_return_code
269 _DtCmsAppendCalAttrsByFD(int f, int size, cms_attribute * attrs)
270 {
271         CSA_return_code stat;
272         char    *buf, *attrstr;
273         char    *prefix = "(calendarattributes ";
274         char    *subfix = ")\n";
275         int     count;
276
277         if ((attrstr = attrs_to_attrliststr(size, attrs, B_FALSE, B_FALSE))
278             == NULL)
279                 return (CSA_E_INSUFFICIENT_MEMORY);
280
281         count = strlen(prefix) + strlen(attrstr) + strlen(subfix) + 1;
282         if ((buf = malloc(count)) == NULL) {
283                 free(attrstr);
284                 return (CSA_E_INSUFFICIENT_MEMORY);
285         }
286         sprintf(buf, "%s%s%s", prefix, attrstr, subfix);
287
288         stat = append_log(f, buf);
289
290         free(attrstr);
291         free(buf);
292         return (stat);
293 }
294
295 extern CSA_return_code
296 _DtCmsAppendAppt4ByFN(char *file, Appt_4 *appt, _DtCmsLogOps op)
297 {
298         int     f;
299         CSA_return_code stat;
300
301         if ((f = open(file, O_WRONLY | O_APPEND | O_SYNC)) < 0) {
302                 perror(pgname);
303                 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
304         }
305
306         stat = _DtCmsAppendAppt4ByFD(f, appt, op);
307
308         close(f);
309         return (stat);
310 }
311
312 extern CSA_return_code
313 _DtCmsAppendAppt4ByFD(int f, Appt_4 *appt, _DtCmsLogOps op)
314 {
315         char *cptr;
316         char tmpbuf[BUFSIZ];
317         char buf[BUFSIZ*3], buf2[BUFSIZ*2]; /* 1 BUFSIZ for what fields, 
318                                                1 BUFSIZ for mailto field,
319                                                and 1 BUFSIZ for the rest */
320
321         cptr = ctime (&appt->appt_id.tick);
322         cptr[24] = NULL;                /* strip off CR */
323
324         buf[0] = NULL;
325         switch (op) {
326         case _DtCmsLogAdd:
327                 sprintf(buf, "(add \"%s\" key: %ld ", cptr, appt->appt_id.key);
328                 if (appt->what) {
329                         cptr = cr_to_str(appt->what, strlen(appt->what));
330                         if (cptr != NULL) {
331                                 sprintf(buf2, "what: \"%s\" ", cptr);
332                                 strcat(buf, buf2);
333                                 free(cptr);
334                         } else {
335                                 return (CSA_E_INSUFFICIENT_MEMORY);
336                         }
337                 }
338                 if (appt->client_data) {
339                         sprintf(buf2, "details: \"%s\" ", appt->client_data);
340                         strcat(buf, buf2);
341                 }
342                 if (appt->duration) {
343                         sprintf(buf2, "duration: %d ", appt->duration);
344                         strcat(buf, buf2);
345                 }
346
347                 periodtostr (appt->period.period, tmpbuf);
348                 sprintf(buf2, "period: %s ", tmpbuf);
349                 strcat(buf, buf2);
350
351                 if (appt->period.nth != 0) {
352                         sprintf (buf2, "nth: %d ", appt->period.nth);
353                         strcat(buf, buf2);
354                 }
355                 if (appt->period.enddate != 0) {
356                         cptr = ctime (&(appt->period.enddate));
357                         cptr[24] = NULL; /* strip off CR */
358                         sprintf(buf2, "enddate: \"%s\" ", cptr);
359                         strcat(buf, buf2);
360                 }
361
362                 sprintf(buf2, "ntimes: %d ", appt->ntimes);
363                 strcat(buf, buf2);
364
365                 if (appt->exception != NULL) {
366                         struct Except_4 *e = appt->exception;
367                         strcat(buf, "exceptions: (");
368                         while(e != NULL) {
369                                 sprintf(buf2, "%d ", e->ordinal);
370                                 strcat(buf, buf2);
371                                 e = e->next;
372                         }
373                         strcat(buf, ") ");
374                 }
375
376                 if (appt->author != NULL) {
377                         sprintf(buf2, "author: \"%s\" ", appt->author);
378                         strcat(buf, buf2);
379                 }
380                 if (appt->attr != NULL) {
381                         struct Attribute_4 *item = appt->attr;
382                         strcat(buf, "attributes: (");
383                         while(item != NULL) {
384                                 sprintf(buf2, "(\"%s\",\"%s\",\"%s\")",
385                                         item->attr, item->value,
386                                         item->clientdata);
387                                 strcat(buf, buf2);
388                                 item = item->next;
389                         }
390                         strcat(buf, ") ");
391                 }
392                 if (appt->tag != NULL) {
393                         struct Tag_4 *item = appt->tag;
394                         strcat(buf, "tags: (");
395                         while(item != NULL) {
396                                 tagstostr(item->tag, tmpbuf);
397                                 sprintf(buf2, "(%s , %d)", tmpbuf,
398                                         item->showtime);
399                                 strcat(buf, buf2);
400                                 item = item->next;
401                         }
402                         strcat(buf, ") ");
403                 }
404
405                 apptstatustostr(appt->appt_status, tmpbuf);
406                 sprintf(buf2, "apptstat: %s ", tmpbuf);
407                 strcat(buf, buf2);
408
409                 privacytostr(appt->privacy, tmpbuf);
410                 sprintf(buf2, "privacy: %s )\n", tmpbuf);
411                 strcat(buf, buf2);
412
413                 break;    
414
415         case _DtCmsLogRemove:
416                 sprintf(buf, "(remove \"%s\" key: %ld)\n", cptr,
417                         appt->appt_id.key);
418                 break;
419         }
420
421         return (append_log(f, buf));
422 }
423
424 extern CSA_return_code
425 _DtCmsAppendAccessByFN(char *file, int type, Access_Entry_4 *p)
426 {
427         int     f;
428         CSA_return_code stat;
429
430         if ((f = open(file, O_WRONLY | O_APPEND | O_SYNC)) < 0) {
431                 perror(pgname);
432                 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
433         }
434
435         stat = _DtCmsAppendAccessByFD(f, type, p);
436
437         close(f);
438         return (stat);
439 }
440
441 extern CSA_return_code
442 _DtCmsAppendAccessByFD(int f, int type, Access_Entry_4 *p)
443 {
444         CSA_return_code stat;
445         int     count = 12; /* (access  )\n */
446         char    *p_type;
447         char    *buf;
448
449         if (type == access_read_4)
450                 p_type = "read";
451         else if (type == access_write_4)
452                 p_type = "write";
453         else if (type == access_delete_4)
454                 p_type = "delete";
455         else if (type == access_exec_4)
456                 p_type = "exec";
457         else
458                 return (CSA_E_INVALID_PARAMETER);
459
460         count = count + strlen(p_type);
461         if ((buf = malloc(count)) == NULL)
462                 return (CSA_E_INSUFFICIENT_MEMORY);
463
464         sprintf(buf, "(access %s ", p_type);
465
466         while(p != NULL) {
467                 if (p->who != NULL) {
468                         count = count + strlen(p->who) + 3;
469                         if ((buf = realloc(buf, count)) == NULL)
470                                 return (CSA_E_INSUFFICIENT_MEMORY);
471                         else {
472                                 strcat(buf, "\"");
473                                 strcat(buf, p->who);
474                                 strcat(buf, "\" ");
475                         }
476                 }
477                 p = p->next;
478         }
479         strcat(buf, ")\n");
480
481         stat = append_log(f, buf);
482
483         free(buf);
484         return(stat);
485 }
486
487 extern boolean_t
488 _DtCmsPrintAppt4(caddr_t data)
489 {
490         Appt_4 *appt = (Appt_4 *)data;
491         char *tmstr;
492         char buf[BUFSIZ];
493
494         fprintf(stderr, "*** V4 appointement: ***\n\n");
495
496         tmstr = ctime (&appt->appt_id.tick);
497         tmstr[24] = NULL;               /* strip off CR */
498
499         if (fprintf(stderr, "(add \"%s\" ", tmstr)==EOF) {
500                 return (B_TRUE);
501         }
502         if (fprintf(stderr, "key: %ld ", appt->appt_id.key)==EOF) {
503                 return (B_TRUE);
504         }
505         if (appt->what) {
506                 tmstr = cr_to_str(appt->what, strlen(appt->what));
507                 if (fprintf(stderr, "what: \"%s\" ", tmstr) == EOF) {
508                         free(tmstr);
509                         return (1);
510                 } else
511                         free(tmstr);
512         }
513         if (appt->client_data) {
514                 if (fprintf(stderr, "details: \"%s\" ", appt->client_data)
515                     == EOF)
516                         return (B_TRUE);
517         }
518         if (appt->duration) {
519                 if (fprintf(stderr, "duration: %d ", appt->duration) == EOF)
520                         return (B_TRUE);
521         }
522
523         buf[0]=NULL;  
524         periodtostr (appt->period.period, buf);
525         if (fprintf(stderr, "period: %s ", buf) == EOF)
526                 return (B_TRUE);
527
528         if (appt->period.nth != 0) {
529                 if (fprintf (stderr, "nth: %d ", appt->period.nth) == EOF)
530                         return (B_TRUE);
531         }
532         if (appt->period.enddate != 0) {
533                 tmstr = ctime (&(appt->period.enddate));
534                 tmstr[24] = NULL; /* strip off CR */
535                 if (fprintf(stderr, "enddate: \"%s\" ", tmstr) == EOF)
536                         return (B_TRUE);
537         }
538
539         if (fprintf(stderr, "ntimes: %d ", appt->ntimes) == EOF)
540                 return (B_TRUE);
541
542         if (appt->exception != NULL) {
543                 struct Except_4 *e = appt->exception;
544                 if (fprintf(stderr, "exceptions: (") == EOF)
545                         return (B_TRUE);
546                 while(e != NULL) {
547                         if (fprintf(stderr, "%d ", e->ordinal) == EOF)
548                                 return (B_TRUE);
549                         e = e->next;
550                 }
551                 if (fprintf(stderr, ") ") == EOF)
552                         return (B_TRUE);
553         }
554         if (appt->author != NULL) {
555                 if (fprintf(stderr, "author: \"%s\" ", appt->author) == EOF)
556                         return (B_TRUE);
557         }
558         if (appt->attr != NULL) {
559                 struct Attribute_4 *item = appt->attr;
560                 if (fprintf(stderr, "attributes: (") == EOF)
561                         return (B_TRUE);
562                 while(item != NULL) {
563                         if (fprintf(stderr, "(\"%s\",\"%s\",\"%s\")",
564                             item->attr, item->value, item->clientdata) == EOF)
565                                 return (B_TRUE);
566                         item = item->next;
567                 }
568                 if (fprintf(stderr, ") ") == EOF)
569                         return (B_TRUE);
570         }
571         if (appt->tag != NULL) {
572                 struct Tag_4 *item = appt->tag;
573                 if (fprintf(stderr, "tags: (") == EOF)
574                         return (B_TRUE);
575                 while(item != NULL) {
576                         buf[0]=NULL;
577                         tagstostr(item->tag, buf);
578                         if (fprintf(stderr, "(%s , %d)", buf, item->showtime)
579                             == EOF)
580                                 return (B_TRUE);
581                         item = item->next;
582                 }
583                 if (fprintf(stderr, ") ") == EOF)
584                         return (B_TRUE);
585         }
586
587
588         buf[0]=NULL;
589         apptstatustostr(appt->appt_status, buf);
590         if (fprintf(stderr, "apptstat: %s ", buf) == EOF)
591                 return (B_TRUE);
592
593         buf[0]=NULL;
594         privacytostr(appt->privacy, buf);
595         if (fprintf(stderr, "privacy: %s )\n", buf) == EOF)
596                 return (B_TRUE);
597
598         return (B_FALSE);
599 }
600
601 extern void
602 _DtCmsPrintExceptions(int len, int *exceptions)
603 {
604         int i;
605
606         fprintf(stderr, "\nexception part:\n");
607         fprintf(stderr, "number of exception = %d\n", len);
608         fprintf(stderr, "exceptions:");
609         for (i = 0; i < len; i++) {
610                 fprintf(stderr, " %d", exceptions[i]);
611         }
612         fprintf(stderr, "\n");
613 }
614
615 extern CSA_return_code
616 _DtCmsCreateLogV2(char *owner, char *file)
617 {
618         return (create_log(owner, file, _DtCMS_VERSION4));
619 }
620
621 extern CSA_return_code
622 _DtCmsGetFileSize(char *calendar, int *size)
623 {
624         char            *log;
625         struct stat     info;
626
627         if ((log = _DtCmsGetLogFN(calendar)) == NULL)
628                 return (CSA_E_INSUFFICIENT_MEMORY);
629
630         if (stat(log, &info) != 0) {
631                 free(log);
632                 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
633         } else {
634                 *size = info.st_size;
635                 free(log);
636                 return (CSA_SUCCESS);
637         }
638 }
639
640 extern void
641 _DtCmsTruncateFile(char *calendar, int size)
642 {
643         char    *log;
644         int     f;
645
646         if ((log = _DtCmsGetLogFN(calendar)) == NULL)
647                 return;
648
649         /* truncate log file to specified size */
650         if ((f = open(log, O_RDWR | O_APPEND | O_SYNC)) >= 0)
651                 ftruncate(f, size);
652
653         free(log);
654 }
655
656 extern CSA_return_code
657 _DtCmsRemoveLog(char *calendar, char *user)
658 {
659         CSA_return_code stat = CSA_SUCCESS;
660         char            *log, *dlog;
661
662         if ((log = _DtCmsGetLogFN(calendar)) == NULL)
663                 return (CSA_E_INSUFFICIENT_MEMORY);
664
665         if ((dlog = _DtCmsGetDelFN(calendar)) == NULL) {
666                 free(log);
667                 return (CSA_E_INSUFFICIENT_MEMORY);
668         }
669
670         if (rename(log, dlog) < 0)
671                 stat = CSA_X_DT_E_BACKING_STORE_PROBLEM;
672
673         free(log);
674         free(dlog);
675
676         return (stat);
677 }
678
679 extern CSA_return_code
680 _DtCmsWriteVersionString(char *file, int version)
681 {
682         struct  tm *tm;
683         time_t  tmval;
684         char    *tmstr;
685         int     fd, len;
686         char    buf[BUFSIZ];
687         CSA_return_code stat = CSA_SUCCESS;
688         _Xltimeparams   localtime_buf;
689         _Xatimeparams   asctime_buf;
690
691         tmval = time((time_t *) 0);
692         tm = _XLocaltime(&tmval, localtime_buf);
693         tmstr  = _XAsctime(tm, asctime_buf);
694         tmstr[24] = NULL;               /* strip off CR */
695
696         if ((fd = open(file, O_WRONLY|O_TRUNC|O_SYNC)) < 0) {
697                 if (debug)
698                         fprintf(stderr, "%s: failed to open %s in %s\n",
699                                 pgname, file, _DtCmsWriteVersionString);
700                 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
701         }
702
703         sprintf(buf, "Version: %d\n**** start of log on %s ****\n\n",
704                 version, tmstr);
705
706         len = strlen(buf);
707         if (write(fd, buf, len) != len) {
708                 perror(pgname);
709                 stat = CSA_X_DT_E_BACKING_STORE_PROBLEM;
710         }
711         close(fd);
712
713         return (stat);
714 }
715
716 /*
717  * if everything works fine, return 0; otherwise, return -1
718  */
719 extern int
720 _DtCmsSetFileMode(
721         char *file,
722         uid_t uid,
723         gid_t gid,
724         mode_t mode,
725         boolean_t changeeuid,
726         boolean_t printerr)
727 {
728         int     error;
729         char    buff[BUFSIZ];
730
731         if (changeeuid == B_TRUE) {
732 #ifndef AIX
733 #ifdef HPUX
734                 setuid (0);
735 #else
736                 seteuid (0);
737 #endif
738 #endif
739         }
740
741         if (chmod (file, mode) < 0) {
742                 if (printerr == B_TRUE) {
743                         error = errno;
744                         sprintf (buff, "%s: chmod %s to %lo failed.\n%s: System error",
745                                 pgname, file, (long)mode, pgname);
746                         errno = error;
747                         perror (buff);
748                 }
749                 return(-1);
750         }
751
752         if (chown (file, uid, gid) < 0) {
753                 if (printerr == B_TRUE) {
754                         error = errno;
755                         sprintf (buff, "%s: chown %s to (uid=%ld,gid=%ld) failed.\n%s%s",
756                                 pgname, file, (long)uid, (long)gid, pgname, ": System error");
757                         errno = error;
758                         perror (buff);
759                 }
760                 return(-1);
761         }
762
763 #ifndef AIX
764 #ifdef HPUX
765         if (changeeuid == B_TRUE && setuid (daemon_uid) < 0)
766 #else
767         if (changeeuid == B_TRUE && seteuid (daemon_uid) < 0)
768 #endif
769         {
770                 if (printerr == B_TRUE) {
771                         error = errno;
772                         sprintf (buff, "%s: Can't switch process uid back to daemon.\n%s%s",
773                                 pgname, pgname, ": System error");
774                         errno = error;
775                         perror (buff);
776                 }
777         }
778 #endif /* AIX */
779
780         return(0);
781 }
782
783 static void
784 periodtostr(Interval_4 i, char *q)
785 {
786         if (q==NULL) return;
787         q[0]=NULL;
788         switch (i) {
789                 case single_4:
790                         strcpy (q, "single");
791                         break;
792                 case daily_4:
793                         strcpy (q, "daily");
794                         break;
795                 case weekly_4:
796                         strcpy (q, "weekly");
797                         break;
798                 case biweekly_4:
799                         strcpy (q, "biweekly");
800                         break;
801                 case monthly_4:
802                         strcpy (q, "monthly");
803                         break;
804                 case yearly_4:
805                         strcpy (q, "yearly");
806                         break;
807                 case nthWeekday_4:
808                         strcpy (q, "nthWeekday");
809                         break;
810                 case everyNthDay_4:
811                         strcpy (q, "everyNthDay");
812                         break;
813                 case everyNthWeek_4:
814                         strcpy (q, "everyNthWeek");
815                         break;
816                 case everyNthMonth_4:
817                         strcpy (q, "everyNthMonth");
818                         break;
819                 case monThruFri_4:
820                         strcpy (q, "monThruFri");
821                         break;
822                 case monWedFri_4:
823                         strcpy (q, "monWedFri");
824                         break;
825                 case tueThur_4:
826                         strcpy (q, "tueThur");
827                         break;
828                 case daysOfWeek_4:
829                         strcpy (q, "daysOfWeek");
830                         break;
831                 default:
832                         strcpy (q, "single");
833                         break;
834         }
835 }
836
837 static void
838 privacytostr(Privacy_Level_4 p, char *q)
839 {
840         if (q==NULL) return;
841         q[0]=NULL;
842         switch(p) {
843         case public_4:
844                 strcpy(q, "public");
845                 break;
846         case private_4:
847                 strcpy(q, "private");
848                 break;
849         case semiprivate_4:
850                 strcpy(q, "semiprivate");
851                 break;
852         default:
853                 strcpy(q, "public");
854                 break;
855         }
856 }
857
858 static void
859 apptstatustostr(Appt_Status_4 p, char *q)
860 {
861         if (q==NULL) return;
862         q[0]=NULL;
863         switch(p) {
864         case active_4:
865                 strcpy(q, "active");
866                 break;
867         case pendingAdd_4:
868                 strcpy(q, "pendingAdd");
869                 break;
870         case pendingDelete_4:
871                 strcpy(q, "pendingDelete");
872                 break;
873         case committed_4:
874                 strcpy(q, "committed");
875                 break;
876         case cancelled_4:
877                 strcpy(q, "cancelled");
878                 break;
879         case completed_4:
880                 strcpy(q, "completed");
881                 break;
882         default:
883                 strcpy(q, "active");
884                 break;
885         }
886 }
887
888 static void
889 tagstostr(Event_Type_4 p, char *q)
890 {
891         if (q==NULL) return;
892         q[0]=NULL;
893         switch(p) {
894         case appointment_4:
895                 strcpy(q, "appointment");
896                 break;
897         case reminder_4:
898                 strcpy(q, "reminder");
899                 break;
900         case otherTag_4:
901                 strcpy(q, "otherTag");
902                 break;
903         case holiday_4:
904                 strcpy(q, "holiday");
905                 break;
906         case toDo_4:
907                 strcpy(q, "toDo");
908                 break;
909         default:
910                 strcpy(q, "appointment");
911                 break;
912         }
913 }
914
915 static char *
916 get_fname (char *dir, char *fname, char *who)
917 {
918         char *buf;
919
920         buf = (char *)malloc(strlen(dir) + strlen(fname) + strlen(who) + 3);
921         if (buf != NULL)
922                 sprintf (buf, "%s/%s.%s", dir, fname, who);
923         return (buf);
924 }
925
926 static CSA_return_code
927 create_log(char *owner, char *file, int version)
928 {
929         uid_t   uid;
930         struct  passwd *pw;
931         int     fd;
932         char    *ptr;
933         CSA_return_code stat;
934
935         ptr = strchr(owner, '@');
936         if (ptr) *ptr = NULL;
937         pw = getpwnam (owner);
938         if (ptr) *ptr = '@';
939         if (pw == NULL)
940                 return(CSA_E_FAILURE);
941         else
942                 uid = pw->pw_uid;
943
944         /* Read by owner and Read/Write by group (gid must be daemon) */
945         fd = open(file, O_WRONLY|O_CREAT|O_EXCL, _DtCMS_DEFAULT_MODE);
946         if (fd < 0) {
947                 if (debug)
948                         fprintf(stderr, "%s: can't create %s\n", pgname, file);
949
950                 if (errno == EEXIST)
951                         return(CSA_E_CALENDAR_EXISTS);
952                 else
953                         return(CSA_X_DT_E_BACKING_STORE_PROBLEM);
954         }
955
956         if (_DtCmsSetFileMode(file, uid, daemon_gid, _DtCMS_DEFAULT_MODE,
957             B_TRUE, B_TRUE) < 0) {
958                 close(fd);
959                 unlink(file);
960                 return(CSA_X_DT_E_BACKING_STORE_PROBLEM);
961         }
962         close(fd);
963
964         if ((stat = _DtCmsWriteVersionString(file, version)) != CSA_SUCCESS) {
965                 unlink(file);
966         }
967
968         return (stat);
969 }
970
971 static CSA_return_code
972 append_log(int f, char *buf)
973 {
974         CSA_return_code status;
975         struct stat finfo;
976         int file_size = 0, nbytes_written = 0, nbytes_towrite = 0;
977
978         if (buf == NULL)
979                 return (CSA_E_INVALID_PARAMETER);
980
981         if (fstat(f, &finfo) < 0) {
982                 perror(pgname);
983                 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
984         } else
985                 file_size = finfo.st_size;
986
987         nbytes_towrite = strlen(buf);
988         nbytes_written = write(f, buf, nbytes_towrite);
989         if (nbytes_written != nbytes_towrite) {
990                 if (errno == ENOSPC)
991                         status = CSA_E_DISK_FULL;
992                 else
993                         status = CSA_X_DT_E_BACKING_STORE_PROBLEM;
994
995                 perror(pgname);
996                 ftruncate(f, file_size);
997                 return (status);        
998         }
999
1000         return(CSA_SUCCESS);
1001 }
1002
1003 static char *
1004 grow_n_concat(char *base, char *newstr, int newcount)
1005 {
1006         char *ptr;
1007
1008         if ((ptr = realloc(base, newcount)) == NULL)
1009                 return (NULL);
1010
1011         if (base)
1012                 strcat(ptr, newstr);
1013         else
1014                 strcpy(ptr, newstr);
1015
1016         return (ptr);
1017 }
1018
1019 /*
1020  * format of attributes:
1021  * CSA_VALUE_BOOLEAN:           ("name","type","number")
1022  * CSA_VALUE_ENUMERATED:        ("name","type","number")
1023  * CSA_VALUE_FLAGS:             ("name","type","number")
1024  * CSA_VALUE_SINT32:            ("name","type","number")
1025  * CSA_VALUE_UINT32:            ("name","type","number")
1026  * CSA_VALUE_STRING:            ("name","type","string")
1027  * CSA_VALUE_ACCESS_LIST:       ("name","type","user:rights [user:rights]")
1028  * CSA_VALUE_CALENDAR_USER:     ("name","type","string")
1029  * CSA_VALUE_DATE_TIME:         ("name","type","datetime")
1030  * CSA_VALUE_DATE_TIME_RANGE:   ("name","type","datetimerange")
1031  * CSA_VALUE_TIME_DURATION:     ("name","type","timeduration")
1032  * CSA_VALUE_DATE_TIME_LIST:    ("name","type","datetime [datetime]")
1033  * CSA_VALUE_REMINDER:          ("name","type","string:number:string") or
1034  *                              ("name","type","string:string:number:number:string")
1035  * CSA_VALUE_OPAQUE_DATA:       ("name","type","number:string")
1036  *
1037  * format of attributes (when only hashed number is written):
1038  * CSA_VALUE_BOOLEAN:           (hash_number "number")
1039  * CSA_VALUE_ENUMERATED:        (hash_number "number")
1040  * CSA_VALUE_FLAGS:             (hash_number "number")
1041  * CSA_VALUE_SINT32:            (hash_number "number")
1042  * CSA_VALUE_UINT32:            (hash_number "number")
1043  * CSA_VALUE_STRING:            (hash_number "string")
1044  * CSA_VALUE_ACCESS_LIST:       (hash_number "user:rights [user:rights]")
1045  * CSA_VALUE_CALENDAR_USER:     (hash_number "string")
1046  * CSA_VALUE_DATE_TIME:         (hash_number "datetime")
1047  * CSA_VALUE_DATE_TIME_RANGE:   (hash_number "datetimerange")
1048  * CSA_VALUE_TIME_DURATION:     (hash_number "timeduration")
1049  * CSA_VALUE_DATE_TIME_LIST:    (hash_number "datetime [datetime]")
1050  * CSA_VALUE_REMINDER:          (hash_number "string:number:string") or
1051  *                              (hash_number "string:string:number:number:string")
1052  * CSA_VALUE_OPAQUE_DATA:       (hash_number "number:string")
1053  *
1054  * Note: element 0 of the array is not used
1055  */
1056 static char *
1057 attrs_to_attrliststr(
1058         int             num_attr,
1059         cms_attribute   *attrs,
1060         boolean_t       write_hash,
1061         boolean_t       entryattrs)
1062 {
1063         int     i, count, tcount;
1064         char    *ptr, *buf = NULL;
1065         char    tmpbuf[BUFSIZ/2], tmpbuf2[BUFSIZ/2], *body;
1066         CSA_opaque_data *opq;
1067
1068         if ((buf = malloc(BUFSIZ+1)) == NULL) return (NULL);
1069         tcount = BUFSIZ;
1070
1071         for (i = 1, count = 0, *buf = NULL; i <= num_attr; i++) {
1072
1073                 if (attrs[i].value == NULL || (entryattrs &&
1074                     (i == CSA_ENTRY_ATTR_NUMBER_RECURRENCES_I ||
1075                     i == CSA_X_DT_ENTRY_ATTR_REPEAT_TYPE_I ||
1076                     i == CSA_X_DT_ENTRY_ATTR_REPEAT_TIMES_I ||
1077                     i == CSA_X_DT_ENTRY_ATTR_REPEAT_INTERVAL_I ||
1078                     i == CSA_X_DT_ENTRY_ATTR_REPEAT_OCCURRENCE_NUM_I ||
1079                     i == CSA_X_DT_ENTRY_ATTR_SEQUENCE_END_DATE_I)))
1080                         continue;
1081
1082                 if (write_hash)
1083                         /* open_bracket hash_number open quote for value */
1084                         sprintf(tmpbuf, "(%d \"", attrs[i].name.num);
1085                 else
1086                         /* open bracket name, type, open quote for value */
1087                         sprintf(tmpbuf, "(\"%s\",\"%s\",\"", attrs[i].name.name,
1088                                 tag_string[attrs[i].value->type]);
1089
1090                 /* value string */
1091                 body = NULL;
1092                 *tmpbuf2 = NULL;
1093                 switch (attrs[i].value->type) {
1094                 case CSA_VALUE_ENUMERATED:
1095                 case CSA_VALUE_SINT32:
1096                         sprintf(tmpbuf2, "%ld",
1097                                 attrs[i].value->item.sint32_value);
1098                         break;
1099
1100                 case CSA_VALUE_BOOLEAN:
1101                 case CSA_VALUE_FLAGS:
1102                 case CSA_VALUE_UINT32:
1103                         sprintf(tmpbuf2, "%lu",
1104                                 attrs[i].value->item.uint32_value);
1105                         break;
1106
1107                 case CSA_VALUE_STRING:
1108                 case CSA_VALUE_DATE_TIME:
1109                 case CSA_VALUE_DATE_TIME_RANGE:
1110                 case CSA_VALUE_TIME_DURATION:
1111                 case CSA_VALUE_CALENDAR_USER:
1112                         if (attrs[i].value->item.string_value) {
1113                                 if ((ptr = cr_to_str(attrs[i].value->\
1114                                     item.string_value,
1115                                     strlen(attrs[i].value->item.string_value)))
1116                                     == NULL) {
1117                                         free(buf);
1118                                         return (NULL);
1119                                 }
1120                         } else
1121                                 ptr = NULL;
1122
1123                         body = ptr;
1124                         break;
1125
1126                 case CSA_VALUE_REMINDER:
1127                         opq = &attrs[i].value->item.reminder_value->\
1128                                 reminder_data;
1129                         if (attrs[i].value->item.reminder_value->repeat_count
1130                             > 1)
1131                         {
1132                             sprintf(tmpbuf2, "%s:%s:%lu:%lu:",
1133                                 attrs[i].value->item.reminder_value->lead_time,
1134                                 (attrs[i].value->item.reminder_value->snooze_time ?
1135                                 attrs[i].value->item.reminder_value->snooze_time:""),
1136                                 attrs[i].value->item.reminder_value->repeat_count,
1137                                 opq->size);
1138                         } else {
1139                             sprintf(tmpbuf2, "%s:%lu:",
1140                                 attrs[i].value->item.reminder_value->lead_time,
1141                                 opq->size);
1142                         }
1143
1144                         if (opq->size > 0) {
1145                                 if ((ptr = cr_to_str((char *)opq->data,
1146                                     opq->size)) == NULL) {
1147                                         free(buf);
1148                                         return (NULL);
1149                                 }
1150                         } else
1151                                 ptr = NULL;
1152                         body = ptr;
1153
1154                         break;
1155
1156                 case CSA_VALUE_OPAQUE_DATA:
1157                         opq = attrs[i].value->item.opaque_data_value;
1158                         sprintf(tmpbuf2, "%lu:", opq->size);
1159
1160                         if (opq->size > 0) {
1161                                 if ((ptr = cr_to_str((char *)opq->data,
1162                                     opq->size)) == NULL) {
1163                                         free(buf);
1164                                         return (NULL);
1165                                 }
1166                         } else
1167                                 ptr = NULL;
1168                         body = ptr;
1169                         break;
1170
1171                 case CSA_VALUE_ACCESS_LIST:
1172                         if (attrs[i].value->item.access_list_value) {
1173                                 if ((ptr = get_access_list_string(
1174                                     attrs[i].value->item.access_list_value))
1175                                     == NULL) {
1176                                         free(buf);
1177                                         return (NULL);
1178                                 }
1179                         } else
1180                                 ptr = NULL;
1181                         body = ptr;
1182                         break;
1183
1184                 case CSA_VALUE_DATE_TIME_LIST:
1185                         if (attrs[i].value->item.date_time_list_value) {
1186                                 if ((ptr = get_date_time_list_string(
1187                                     attrs[i].value->item.date_time_list_value))
1188                                     == NULL) {
1189                                         free(buf);
1190                                         return (NULL);
1191                                 }
1192                         } else
1193                                 ptr = NULL;
1194                         body = ptr;
1195                         break;
1196                 }
1197
1198                 count += strlen(tmpbuf) + strlen(tmpbuf2) +
1199                                 (body ? strlen(body) : 0) + 3; /* closing */
1200                 if (tcount < count) {
1201                         if ((ptr = realloc(buf, tcount+BUFSIZ)) == NULL) {
1202                                 if (body) free(body);
1203                                 free(buf);
1204                                 return NULL;
1205                         }
1206                         buf = ptr;
1207                         tcount += BUFSIZ;
1208                 }
1209                 strcat(buf, tmpbuf);
1210                 if (*tmpbuf2 != NULL) strcat(buf, tmpbuf2);
1211                 if (body) strcat(buf, body);
1212                 strcat(buf, "\")\n");
1213                 if (body) free(body);
1214         }
1215
1216         return (buf);
1217 }
1218
1219 static char *
1220 get_access_list_string(cms_access_entry *list)
1221 {
1222         char *ptr = NULL, *tptr, buf[BUFSIZ/4];
1223         int count, tcount;
1224
1225         /* do the first one */
1226         if ((ptr = malloc(BUFSIZ+1)) == NULL) return (NULL);
1227         tcount = BUFSIZ;
1228         sprintf(ptr, "%s:%u", list->user, list->rights);
1229         count = strlen(ptr);
1230
1231         while (list->next) {
1232                 list = list->next;
1233                 sprintf(buf, " %s:%u", list->user, list->rights);
1234
1235                 count += strlen(buf);
1236                 if (tcount < count) {
1237                         if ((tptr = grow_n_concat(ptr, buf, tcount+BUFSIZ))
1238                             == NULL) {
1239                                 free(ptr);
1240                                 return (NULL);
1241                         }
1242                         ptr = tptr;
1243                         tcount += BUFSIZ;
1244                 } else
1245                         strcat(ptr, buf);
1246         }
1247         return (ptr);
1248 }
1249
1250 static char *
1251 cr_to_str(char *s, int size)
1252 {
1253         int j, k;
1254         char *newstr;
1255
1256         if (s==NULL)
1257                 return(NULL);
1258
1259         if ((newstr = (char *) calloc(1, (unsigned)((2 * size) + 2))) == NULL)
1260                 return (NULL);
1261         k = 0;
1262         for (j=0; j<size; j++) {
1263                 if (s[j]=='\n') {
1264                         newstr[k] = '\\';
1265                         newstr[k+1] = 'n';
1266                         k+=2;
1267                 }
1268                 else if (s[j]=='\\') {
1269                         newstr[k] = '\\';
1270                         newstr[k+1] = '\\';
1271                         k+=2;
1272                 }
1273                 else if (s[j]=='\"') {
1274                         newstr[k] = '\\';
1275                         newstr[k+1] = '\"';
1276                         k+=2;
1277                 }
1278                 else {
1279                         newstr[k] = s[j];
1280                         k++;
1281                 }
1282         }
1283         newstr[k] = NULL;
1284         return(newstr);
1285 }
1286
1287 static char *
1288 get_date_time_list_string(CSA_date_time_list list)
1289 {
1290         char *ptr = NULL, *tptr, buf[80];
1291         int count, datestrlen, tcount;
1292
1293         /* do the first one */
1294         sprintf(buf, "%s", list->date_time);
1295         if ((ptr = strdup(buf)) == NULL)
1296                 return (NULL);
1297
1298         if ((ptr = malloc(BUFSIZ+1)) == NULL) return (NULL);
1299         tcount = BUFSIZ;
1300         strcpy(ptr, list->date_time);
1301         count = strlen(ptr);
1302
1303         datestrlen = count + 1;
1304         while (list->next) {
1305                 list = list->next;
1306                 sprintf(buf, " %s", list->date_time);
1307
1308                 count += datestrlen;
1309                 if (tcount < count) {
1310                         if ((tptr = grow_n_concat(ptr, buf, tcount+BUFSIZ))
1311                             == NULL) {
1312                                 free(ptr);
1313                                 return (NULL);
1314                         }
1315                         ptr = tptr;
1316                         tcount += BUFSIZ;
1317                 } else
1318                         strcat(ptr, buf);
1319         }
1320         return (ptr);
1321 }
1322