aux-items.c 62.1 KB
Newer Older
1
/*
2
 * $Id: aux-items.c,v 1.37 2001/12/29 22:57:42 ceder Exp $
Per Cederqvist's avatar
Per Cederqvist committed
3
 * Copyright (C) 1994-2001  Lysator Academic Computer Association.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 1, or (at your option) 
 * any later version.
 * 
 * LysKOM is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */

David Byers's avatar
David Byers committed
26
27
28
29
30

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

31
32
33
34
35
36
37
38
#include <stdio.h>
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#include <setjmp.h>
39
#include <assert.h>
40
41

#include "kom-types.h"
David Byers's avatar
Server    
David Byers committed
42
43
44
#include "com.h"
#include "async.h"
#include "connections.h"
45
46
47
#include "manipulate.h"
#include "aux-items.h"
#include "kom-errno.h"
David Byers's avatar
David Byers committed
48
#include "kom-memory.h"
49
50
51
52
#include "server/smalloc.h"
#include "cache.h"
#include "s-string.h"
#include "lyskomd.h"
David Byers's avatar
David Byers committed
53
#include "kom-config.h"
54
55
#include "regex.h"
#include "log.h"
Per Cederqvist's avatar
Per Cederqvist committed
56
#include "services.h"
David Byers's avatar
David Byers committed
57
58
#include "admin.h"
#include "param.h"
59
#include "server-time.h"
David Byers's avatar
David Byers committed
60
#include "string-malloc.h"
61
62
63

#define AUX_ADJUST_FLAG(flg) item->flags.flg = (def->clear_flags.flg)?0:((def->set_flags.flg)?1:item->flags.flg)

David Byers's avatar
David Byers committed
64
65
/* Externally accessible variables (used by the parser) */

66
Aux_item_definition     *aux_item_definition_list = NULL;
David Byers's avatar
David Byers committed
67
68
69
70
unsigned long            num_aux_item_definitions = 0; 

/* Variables local to this file */

71
72
73
#if COMPILED_AUX_ITEMS
static Aux_item_definition compiled_aux_items[] = {};
#endif
74

75

David Byers's avatar
David Byers committed
76
77
/* Forward declaration of triggers */

78
79
80
81
static void aux_item_trigger_mark_text(Aux_item_trigger_data *);
static void aux_item_trigger_unmark_text(Aux_item_trigger_data *);
static void aux_item_trigger_mirror_faq(Aux_item_trigger_data *);
static void aux_item_trigger_link_item(Aux_item_trigger_data *data);
David Byers's avatar
David Byers committed
82
83
84
85


/* Forward declarations of validators */

86
87
88
89
90
static Bool aux_item_validate_existing_text(Aux_item_validation_data *data);

/* Other forward declarations */
static const Aux_item *
find_aux_item(const Aux_item_list *list, unsigned long aux_no);
David Byers's avatar
David Byers committed
91

92
93
94
static void prepare_aux_item(Aux_item *item, Pers_no creator,
			     const Aux_item_definition *def);

David Byers's avatar
David Byers committed
95
96
97


/* Symbol table for the triggers */
98

99
static Aux_item_trigger_mapping
100
101
102
103
aux_item_triggers [] =
{
    { "mark-text", aux_item_trigger_mark_text },
    { "unmark-text", aux_item_trigger_unmark_text },
David Byers's avatar
David Byers committed
104
    { "link-faq", aux_item_trigger_mirror_faq },
David Byers's avatar
David Byers committed
105
    { "link-item", aux_item_trigger_link_item },
106
107
108
    { NULL, NULL }
};

David Byers's avatar
David Byers committed
109
110
111
112
113
114
115
116
117
118
119
120
121

/* Symbol table for the validators */

static Aux_item_validator_mapping
aux_item_validators [] =
{
    { "existing-readable-text", aux_item_validate_existing_text },
    { NULL, NULL }
};


/* Use this to initialize an empty definition */

122
123
124
125
126
127
Aux_item_definition empty_aux_item_definition =
{
    NULL,                   /* Name */           
    0,                      /* Tag */            
    { 0,0,0,0,0,0,0,0 },    /* Clear flags */    
    { 0,0,0,0,0,0,0,0 },    /* Set flags */      
David Byers's avatar
David Byers committed
128
    0,                      /* Disabled */
129
130
    0,                      /* Author only */    
    0,                      /* Supervisor only */
David Byers's avatar
David Byers committed
131
    0,                      /* System only */
132
    0,                      /* Unique */         
133
134
    0,                      /* Can't delete */
    0,                      /* Owner delete */
135
136
    0,                      /* Inherit limit */  
    FALSE,                  /* Texts */
137
    0,
138
    FALSE,                  /* Confs */
139
    0,
140
141
    FALSE,                  /* Letterbox */
    FALSE,                  /* System */
David Byers's avatar
David Byers committed
142
143
    0,                      /* Number of validators */
    NULL,                   /* Validator list */
144
145
146
    0, NULL,
    0, NULL,
    0, NULL,
147
148
149
150
151
152
153
154
155
156
157
158
159
    NULL                    /* Next */
};

    


/*
 * simple_aux_item is the default definition for simple
 * items (non-reserved tags)
 */

static Aux_item_definition simple_aux_item =
{
160
    (char*)"simple",        /* Name */           
161
162
163
    0,                      /* Tag */            
    { 0,0,0,0,0,0,0,0 },    /* Clear flags */    
    { 0,0,0,0,0,0,0,0 },    /* Set flags */      
David Byers's avatar
David Byers committed
164
    0,                      /* Disabled */
165
166
    0,                      /* Author only */    
    0,                      /* Supervisor only */
David Byers's avatar
David Byers committed
167
    0,                      /* System only */
168
    0,                      /* Unique */         
169
170
    0,                      /* Can't delete */
    0,                      /* Owner delete */
171
172
    0,                      /* Inherit limit */  
    TRUE,                   /* Texts */
173
    0,
174
    TRUE,                   /* Confs */
175
    0,
176
177
    TRUE,                   /* Letterbox */
    TRUE,                   /* System */
David Byers's avatar
David Byers committed
178
    0, NULL,                /* Validators */
179
180
181
182
    0, NULL,                /* Add triggers */
    0, NULL,                /* Delete triggers */
    0, NULL,                /* Undelete triggers */
    NULL
183
184
185
186
187
188
};


/* Forward declarations */

static Bool
189
190
aux_item_check_unique(const Aux_item *item,
                      const Aux_item_definition *xdef,
191
192
193
194
195
196
197
198
199
200
201
202
                      Aux_item_list *add_to_list,
                      unsigned long start_looking_at);


/*
 * aux_item_default_definition
 *
 * Return the default definition of ITEM or NULL if ITEM cannot
 * be created without an explicit definition
 */

static Aux_item_definition *
203
aux_item_default_definition(const Aux_item *item)
204
205
206
207
208
209
210
211
212
213
214
{
    /* A predefined aux_item that we didn't find a definition for is
       illegal. */

    if (AUX_IS_ILLEGAL(item->tag))
        return NULL;

    if (AUX_IS_PREDEFINED(item->tag))
        return NULL;

    
215
216
217
218
219
    /* FIXME (bug 346): "extended item" is a concept that isn't
       implemented, and it conflicts with what the Protocol-A.texi
       documentation says.  This code is probably a rest from the
       design phase, and it should probably be removed. */  

220
221
222
223
224
225
226
    /* This might be an extended item. This code is very horrendous,
       execially if you take a closer look at the AUX_CHECK_BIT macro.
       I wrote it this way because I think it will work in a
       wrong-endian environment, and I was unsure of how well simple
       masking would. */
    /* MMMM XFUP SSSS CCCC NNNN NNNN NNNN NNNN */

David Byers's avatar
David Byers committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
  tmp = item->tag;

  extended                        = (tmp & 0x07000000) ? 1 : 0;
  def_s.author_only               = (tmp & 0x04000000) ? 1 : 0;
  def_s.one_per_person            = (tmp & 0x02000000) ? 1 : 0;
  def_s.may_not_delete            = (tmp & 0x01000000) ? 1 : 0;
  def_s.set_flags.deleted         = (tmp & 0x00700000) ? 1 : 0;
  def_s.set_flags.inherit         = (tmp & 0x00400000) ? 1 : 0;
  def_s.set_flags.secret          = (tmp & 0x00200000) ? 1 : 0;
  def_s.set_flags.hide_creator    = (tmp & 0x00100000) ? 1 : 0;
  def_s.clear_flags.deleted       = (tmp & 0x00070000) ? 1 : 0;
  def_s.clear_flags.inherit       = (tmp & 0x00040000) ? 1 : 0;
  def_s.clear_flags.secret        = (tmp & 0x00020000) ? 1 : 0;
  def_s.clear_flags.hide_creator  = (tmp & 0x00010000) ? 1 : 0;

  def_s.tag = item->tag;
  def_s.inherit_limit = 0;
  def_s.name = "extended-default";


  / * If it wasn't extended, then it was simple, because it sure
  wasn't predefined. * /

  if (!extended)
  {
  return &simple_aux_item;
  }

  return &def_s;
*/

    return &simple_aux_item;
260
261
262
}


263
264
265
266
267
/*
 * aux_item_find_trigger
 */

Aux_item_trigger
268
aux_item_find_trigger(const char *trigger_name)
269
270
271
272
273
274
275
276
277
278
279
280
{
    unsigned long i = 0;

    while (aux_item_triggers[i].name != NULL)
    {
        if (!strcmp(aux_item_triggers[i].name, trigger_name))
            return aux_item_triggers[i].function;
        i += 1;
    }
    return NULL;
}

David Byers's avatar
David Byers committed
281
Aux_item_validation_function
282
aux_item_find_validator(const char *validator_name)
David Byers's avatar
David Byers committed
283
284
285
286
287
288
289
290
291
292
293
294
295
{
    unsigned long i = 0;

    while (aux_item_validators[i].name != NULL)
    {
        if (!strcmp(aux_item_validators[i].name, validator_name))
            return aux_item_validators[i].function;
        i += 1;
    }
    return NULL;
}


296
297
298
299
300
301
302
/*
 * find_aux_item_definition
 *
 * Return the definition of ITEM or NULL if it does not
 * have a valid information.
 */

303
static const Aux_item_definition *
304
find_aux_item_definition(const Aux_item *item)
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
{
    Aux_item_definition *def;

    def = aux_item_definition_list;
    while (def != NULL)
    {
        if (def->tag == item->tag)
            return def;
        def = def->next;
    }

    return aux_item_default_definition(item);
}


/* 
 * ======================================================================
 * UTILITY FUNCTIONS
 * ======================================================================
 */
 
David Byers's avatar
David Byers committed
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351

/* TRIGGER THINGS */


static void
aux_item_fix_trigger_data(Aux_item_trigger_data *data)
{
    switch (data->object_type)
    {
    case TEXT_OBJECT_TYPE:
        data->item = &((Text_stat *)data->object)->aux_item_list.items[data->item_index];
        break;
    case CONF_OBJECT_TYPE:
        data->item = &((Conference *)data->object)->aux_item_list.items[data->item_index];
        break;
    case INFO_OBJECT_TYPE:
        data->item = &((Info *)data->object)->aux_item_list.items[data->item_index];
        break;

    default:
        break;
    }
}


static void
352
aux_item_call_add_triggers(const Aux_item_definition *def,
353
			   enum object_type object_type,
David Byers's avatar
David Byers committed
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
			   unsigned long item_index,
			   unsigned long integer_argument,
			   void     * pointer_argument,
                           Aux_item * item)
{
    unsigned long i;
    Aux_item_trigger_data data;
    
    data.action = AUX_ITEM_ADD_ACTION;
    data.object_type = object_type;
    data.item_index = item_index;
    data.object_no = integer_argument;
    data.object = pointer_argument;
    data.item = item;

    for (i = 0; i < def->num_add_triggers; i++)
    {
        (*(def->add_triggers[i]))(&data);
        aux_item_fix_trigger_data(&data);
    }
}

static void
377
aux_item_call_delete_triggers(const Aux_item_definition *def,
378
                              enum object_type object_type,
David Byers's avatar
David Byers committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
                              unsigned long item_index,
                              unsigned long integer_argument,
                              void     * pointer_argument,
                              Aux_item *item)
{
    unsigned long i;
    Aux_item_trigger_data data;
    
    data.action = AUX_ITEM_DELETE_ACTION;
    data.object_type = object_type;
    data.item_index = item_index;
    data.object_no = integer_argument;
    data.object = pointer_argument;
    data.item = item;

    for (i = 0; i < def->num_delete_triggers; i++)
    {
        (*(def->delete_triggers[i]))(&data);
    }
}

static void
401
aux_item_call_undelete_triggers(const Aux_item_definition *def,
402
				enum object_type object_type,
David Byers's avatar
David Byers committed
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
				unsigned long item_index,
				unsigned long integer_argument,
				void     * pointer_argument,
                                Aux_item * item)
{
    unsigned long i;
    Aux_item_trigger_data data;
    
    data.action = AUX_ITEM_UNDELETE_ACTION;
    data.object_type = object_type;
    data.item_index = item_index;
    data.object_no = integer_argument;
    data.object = pointer_argument;
    data.item = item;

    for (i = 0; i < def->num_undelete_triggers; i++)
    {
        (*(def->undelete_triggers[i]))(&data);
    }
}



426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
 * initialize_aux_items
 *
 * Initialize this code and its data structures
 */

void
aux_item_definition_add(Aux_item_definition *def)
{
    Aux_item_definition     *new_definition;

    new_definition = smalloc(sizeof(Aux_item_definition));
    if (new_definition == NULL)
    {
David Byers's avatar
David Byers committed
440
        restart_kom("out of memory adding aux-item definition");
441
442
443
444
445
446
447
448
    }
    *new_definition = *def;
    num_aux_item_definitions += 1;

    new_definition->next = aux_item_definition_list;
    aux_item_definition_list = new_definition;
}

David Byers's avatar
David Byers committed
449
450
451
452
453
454
455


/*
 * Compile the verification regexp for the definition in DEF
 * and put the result somewhere convenient.
 */

Per Cederqvist's avatar
Per Cederqvist committed
456
static void
457
aux_item_definition_cache_regexp(const Aux_item_definition *def,
David Byers's avatar
David Byers committed
458
                                 unsigned long ix)
459
460
461
462
463
464
{
    struct re_pattern_buffer   *pat_buf;
    const char                 *errmsg;

    re_syntax_options = RE_SYNTAX_POSIX_EXTENDED;
            
David Byers's avatar
David Byers committed
465
466
    def->validators[ix].v.re.cached_re_buf = smalloc(sizeof(*pat_buf));
    if (def->validators[ix].v.re.cached_re_buf == NULL)
467
    {
David Byers's avatar
David Byers committed
468
469
470
        kom_log("Out of memory compiling aux-item regexp %lu (%s).\n",
                def->tag,
                def->name);
471
472
        return;
    }
David Byers's avatar
David Byers committed
473
    pat_buf = def->validators[ix].v.re.cached_re_buf;
474
475
476
477
478
479
480
    pat_buf->translate = DEFAULT_COLLAT_TAB;
    pat_buf->translate = NULL;
    pat_buf->fastmap = 0;
    pat_buf->allocated = 0;
    pat_buf->buffer = 0;

    if ((errmsg =
David Byers's avatar
David Byers committed
481
482
         re_compile_pattern(def->validators[ix].v.re.regexp,
                            strlen(def->validators[ix].v.re.regexp),
483
484
485
                            pat_buf))
        != NULL)
    {
David Byers's avatar
David Byers committed
486
        kom_log("%s in validate regexp of aux-item definition %lu (%s).\n",
487
488
489
                errmsg,
                def->tag,
                def->name);
David Byers's avatar
David Byers committed
490
491
        if (def->validators[ix].v.re.cached_re_buf)
            sfree(def->validators[ix].v.re.cached_re_buf);
David Byers's avatar
David Byers committed
492
493
        if (def->validators[ix].v.re.regexp)
            string_free(def->validators[ix].v.re.regexp);
David Byers's avatar
David Byers committed
494
        def->validators[ix].v.re.cached_re_buf = NULL;
David Byers's avatar
David Byers committed
495
        def->validators[ix].v.re.regexp = NULL;
496
497
498
499
500
501
502
        return;
    }
}

void
initialize_aux_items(char *aux_def_file)
{
503
#if COMPILED_AUX_ITEMS
504
505
506
507
508
509
510
511
512
    unsigned long                i;

    /* Set up precompiled items */
    for (i = 0;
         i < (sizeof(compiled_aux_items)/sizeof(*compiled_aux_items));
         i++)
    {
        aux_item_definition_add(&compiled_aux_items[i]);
    }
513
#endif
514
515
516
517

    /* Read definitions from configuration file */

    parse_aux_item_definitions(aux_def_file);
David Byers's avatar
David Byers committed
518
}
519

David Byers's avatar
David Byers committed
520
521
522
523
524
525
void
free_aux_item_definitions(void)
{
    unsigned long i;
    Aux_item_definition *def;
    Aux_item_definition *tmp;
David Byers's avatar
David Byers committed
526

David Byers's avatar
David Byers committed
527
    def = aux_item_definition_list;
528
529
    while (def != NULL)
    {
David Byers's avatar
David Byers committed
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
        string_free(def->name);
        for (i = 0; i < def->num_validators; i++)
        {
            if (def->validators[i].type == AUX_VALIDATE_REGEXP)
            {
                if (def->validators[i].v.re.regexp != NULL)
                    string_free(def->validators[i].v.re.regexp);
                if (def->validators[i].v.re.cached_re_buf != NULL)
                    sfree(def->validators[i].v.re.cached_re_buf);
            }
        }
        if (def->validators != NULL)
        {
            sfree(def->validators);
            def->validators = NULL;
            def->num_validators = 0;
        }
        if (def->add_triggers != NULL)
        {
            sfree(def->add_triggers);
            def->add_triggers = NULL;
            def->num_add_triggers = 0;
        }
        if (def->delete_triggers != NULL)
        {
            sfree(def->delete_triggers);
            def->delete_triggers = NULL;
            def->num_delete_triggers = 0;
        }
        if (def->undelete_triggers != NULL)
        {
            sfree(def->undelete_triggers);
            def->undelete_triggers = NULL;
            def->num_undelete_triggers = 0;
        }
        tmp = def->next;
        sfree(def);
        def = tmp;
568
    }
David Byers's avatar
David Byers committed
569
570

    aux_item_definition_list = NULL;
571
572
573
}


574

575
static long
576
find_aux_item_index(const Aux_item_list *list, unsigned long aux_no)
577
578
{
    unsigned long i;
David Byers's avatar
David Byers committed
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596

    for (i = 0; i < list->length; i++)
    {
        if (list->items[i].aux_no == aux_no)
            return i;
    }

    return -1;
}


/* Find the aux item list containing the item that ITEM is linked to */

static Aux_item_list *
find_linked_aux_item_list(Aux_item *item)
{
    Text_stat       *text_stat;
    Conference      *conf_stat;
597
    
David Byers's avatar
David Byers committed
598
599
600
601
    switch (item->linked_item.target_type)
    {
    case NO_OBJECT_TYPE:
        return NULL;
602

David Byers's avatar
David Byers committed
603
604
605
606
607
608
609
610
611
612
613
    case TEXT_OBJECT_TYPE:
        GET_T_STAT(text_stat, item->linked_item.target_object.text, NULL);
        return &text_stat->aux_item_list;

    case CONF_OBJECT_TYPE:
        GET_C_STAT(conf_stat, item->linked_item.target_object.conf, NULL);
        return &conf_stat->aux_item_list;

    case INFO_OBJECT_TYPE:
        return &kom_info.aux_item_list;

614
#if 0
David Byers's avatar
David Byers committed
615
    case PERS_OBJECT_TYPE:
616
#endif
David Byers's avatar
David Byers committed
617
618
    default:
        /* No lists in these items */
619
620
        kom_log("find_linked_aux_item_list: Bad aux_item somewhere: "
		"link to person or other kind.\n");
David Byers's avatar
David Byers committed
621
622
623
624
625
626
        return NULL;
    }
}

/* Find the aux item that ITEM is linked to */

627
static const Aux_item *
David Byers's avatar
David Byers committed
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
find_linked_aux_item(Aux_item *item)
{
    Aux_item_list       *target_list;

    target_list = find_linked_aux_item_list(item);
    if (target_list == NULL)
        return NULL;

    return find_aux_item(target_list, item->linked_item.target_item);
}

/* Mark the object linked to by an aux_item as changed */

static void
mark_linked_object_as_changed(Aux_item *item)
{
    Text_stat       *text_stat;
    Conference      *conf_stat;

    switch (item->linked_item.target_type)
648
    {
David Byers's avatar
David Byers committed
649
650
651
652
653
654
655
656
657
658
659
    case TEXT_OBJECT_TYPE:
        VOID_GET_T_STAT(text_stat, item->linked_item.target_object.text);
        mark_text_as_changed(item->linked_item.target_object.text);
        break;

    case CONF_OBJECT_TYPE:
        VOID_GET_C_STAT(conf_stat, item->linked_item.target_object.conf);
        mark_conference_as_changed(item->linked_item.target_object.conf);
        break;

    case INFO_OBJECT_TYPE:
660
#if 0
David Byers's avatar
David Byers committed
661
    case PERS_OBJECT_TYPE:
662
#endif
David Byers's avatar
David Byers committed
663
664
    default:
        /* Need no commit for these objects */
665
	;
666
667
668
    }
}

David Byers's avatar
David Byers committed
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684



/*
 * Utility function for linking aux items. This function will take
 * care of adding the appropriate item to the appropriate list.
 *
 * Note that this may cause aux-item lists to be realloced, so pointers
 * into such lists may become invalid. Take particular care with the
 * pointers in aux-item trigger data
 *
 * Call aux_item_fix_trigger_data if you need to fix the contents of
 * trigger data after calling this function.
 */


Per Cederqvist's avatar
Per Cederqvist committed
685
static void
686
687
688
689
690
691
692
693
694
aux_item_link_items(enum object_type src_type,  /* Source object type */
                    unsigned long    src_no,    /* Source object pointer */
                    void            *src_ptr,   /* Source pointer */
                    Aux_item        *src_item,  /* Source item */

                    enum object_type dst_type,  /* Destination obj type */
                    unsigned long    dst_no,    /* Destination obj pointer */
                    void            *dst_ptr,   /* Destination pointer */
                    Aux_item        *dst_item_data)
695
{
David Byers's avatar
David Byers committed
696
    Aux_item_list       item_list;
697

David Byers's avatar
David Byers committed
698
699
700
    /* Get a pointer to the destination object */

    if (dst_ptr == NULL)
701
    {
David Byers's avatar
David Byers committed
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
        switch (dst_type)
        {
        case TEXT_OBJECT_TYPE:
            dst_ptr = cached_get_text_stat(dst_no);
            break;
        case CONF_OBJECT_TYPE:
            dst_ptr = cached_get_conf_stat(dst_no);
            break;
        case INFO_OBJECT_TYPE:
            dst_ptr = &kom_info;
            break;
        default:
            break;
        }
    }

    if (dst_ptr == NULL || src_ptr == NULL)
        return;


    /* Set up the linking information in the destination item */

    dst_item_data->linked_item.target_type = src_type;
    dst_item_data->linked_item.target_item = src_item->aux_no;
    switch (src_type)
    {
    case TEXT_OBJECT_TYPE: dst_item_data->linked_item.target_object.text = src_no; break;
    case CONF_OBJECT_TYPE: dst_item_data->linked_item.target_object.conf = src_no; break;
    case INFO_OBJECT_TYPE:
    default:
        dst_item_data->linked_item.target_object.text = 0;
733
    }
David Byers's avatar
David Byers committed
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765


    /* Set up the linking information in the source item */

    src_item->linked_item.target_type = dst_type;
    src_item->linked_item.target_item = 0;
    switch (dst_type)
    {
    case TEXT_OBJECT_TYPE:
        src_item->linked_item.target_object.text = dst_no;
        src_item->linked_item.target_item = ((Text_stat *)dst_ptr)->highest_aux + 1;
        break;
    case CONF_OBJECT_TYPE:
        src_item->linked_item.target_object.conf = dst_no;
        src_item->linked_item.target_item = ((Conference *)dst_ptr)->highest_aux + 1;
        break;
    case INFO_OBJECT_TYPE:
        src_item->linked_item.target_item = ((Info *)dst_ptr)->highest_aux_no + 1;
        src_item->linked_item.target_object.text = 0;
        src_item->linked_item.target_object.conf = 0;
        break;
    default:
        src_item->linked_item.target_item = 0;
        src_item->linked_item.target_object.text = 0;
        src_item->linked_item.target_object.conf = 0;
    }


    /* Set up an item list to add */

    item_list.length = 1;
    item_list.items = dst_item_data;
David Byers's avatar
Server:    
David Byers committed
766
    prepare_aux_item_list(&item_list, src_item->creator);
David Byers's avatar
David Byers committed
767
768
769
770
771
772
773
774
775
776
777
778
779


    /*
     * Add the item and mark the destination as changed
     *
     * Note that these calls may cause the aux item list of the
     * destination to move in memory due to calls to realloc.
     */

    switch (dst_type)
    {
    case TEXT_OBJECT_TYPE:
        text_stat_add_aux_item_list((Text_stat *)dst_ptr,
David Byers's avatar
Server:    
David Byers committed
780
781
                                    (Text_no)dst_no, &item_list,
                                    src_item->creator);
782
	/* FIXME (bug 141): is this necessary?  The call to
783
784
	   mark_linked_object_as_changed below seems to fix this,
	   right?  --ceder 1999-07-10. */
David Byers's avatar
David Byers committed
785
786
787
788
        mark_text_as_changed((Text_no)dst_no);
        break;
    case CONF_OBJECT_TYPE:
        conf_stat_add_aux_item_list((Conference *)dst_ptr,
David Byers's avatar
Server:    
David Byers committed
789
790
                                    (Conf_no)dst_no, &item_list,
                                    src_item->creator);
791
	/* FIXME (bug 141): is this necessary?  The call to
792
793
	   mark_linked_object_as_changed below seems to fix this,
	   right?  --ceder 1999-07-10. */
David Byers's avatar
David Byers committed
794
795
796
797
        mark_conference_as_changed((Conf_no)dst_no);
        break;

    case INFO_OBJECT_TYPE:
David Byers's avatar
Server:    
David Byers committed
798
799
        system_add_aux_item_list((Info *)dst_ptr, &item_list,
                                 src_item->creator);
David Byers's avatar
David Byers committed
800
801
802
803
804
805
806
807
808
809
810
811
        break;

    default:
        break;
    }

    /*
     * Set src_item to NULL since there is a chance that it is
     * no longer valid (this happens when the source and destination
     * objects are the same and reallocing the aux item list causes
     *  it to move in memory
     */
812
    /* FIXME (bug 142): but src_item isn't used below this point!  What is the
813
       point of setting it to NULL?  --ceder 1999-07-10.  */
David Byers's avatar
David Byers committed
814
815
816
817
818
819
820
821

    src_item = NULL;

    /*
     * Mark the source object as changed
     */

    mark_linked_object_as_changed(dst_item_data);
822
823
}

David Byers's avatar
David Byers committed
824
825
826



Per Cederqvist's avatar
Per Cederqvist committed
827
static void
David Byers's avatar
David Byers committed
828
829
aux_item_list_add_items(Aux_item_list *add_to_list,
                        Aux_item_list *items_to_add,
830
                        enum object_type object_type,
David Byers's avatar
David Byers committed
831
832
833
834
                        unsigned long  object_no,
                        void          *object_ptr,
                        unsigned long *highest_ptr,
                        Pers_no        item_creator)
835
{
David Byers's avatar
David Byers committed
836
837
838
    unsigned long       i;
    unsigned long       highest_local;
    unsigned long       start_index;
839

David Byers's avatar
David Byers committed
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
    if (items_to_add == NULL)
        return;

    /*
     * Make local copies of variables we need in their original state
     */

    start_index = add_to_list->length;
    highest_local = *highest_ptr;

    /*
     * Set up the target list so it looks like we've added
     * items to it already. That way it is semi-safe to call
     * this function recursively.
     */

    add_to_list->items = srealloc(add_to_list->items,
                                  (add_to_list->length + items_to_add->length) *
                                  sizeof(Aux_item));
    add_to_list->length += items_to_add->length;
    *highest_ptr = *highest_ptr + items_to_add->length;

    for (i = 0; i < items_to_add->length; i++)
863
    {
David Byers's avatar
David Byers committed
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
        init_aux_item(&add_to_list->items[start_index + i]);
    }

    /*
     * Now add the items and call the add triggers
     */

    for (i = 0; i < items_to_add->length; i++)
    {
        highest_local += 1;
        copy_aux_item(&add_to_list->items[i + start_index],
                      &items_to_add->items[i]);
        add_to_list->items[i + start_index].aux_no  = highest_local;
        add_to_list->items[i + start_index].creator = item_creator;
        aux_item_call_add_triggers(
            find_aux_item_definition(&items_to_add->items[i]),
            object_type,
            i + start_index,
            object_no,
            object_ptr,
            &add_to_list->items[i + start_index]);
885
886
    }
}
887

David Byers's avatar
David Byers committed
888
889
890



891
892
893
894
895
896
897
898
899
900
901
902
/*
 * prepare_aux_item_list
 *
 * Prepare items in LIST for checking and addition. You *have*
 * to call this before any of the addition or checking
 * function. CREATOR is the person who wants to create the
 * item list. This function will set various fields in the
 * items. UTSL.
 */

void
prepare_aux_item_list(Aux_item_list *list,
903
                      Pers_no creator)
904
905
906
907
908
909
910
911
{
    unsigned long i;

    if (list == NULL)
        return;
    
    for (i = 0; i < list->length; i++)
    {
912
        prepare_aux_item(&list->items[i], creator, NULL);
913
914
915
    }
}

916
917
918
919
static void
prepare_aux_item(Aux_item *item,
		 Pers_no creator,
		 const Aux_item_definition *def)
920
921
922
923
924
925
926
927
928
929
{
    if (item == NULL)
        return;
    def = def ? def : find_aux_item_definition(item);
    if (def == NULL)
        return;

    AUX_ADJUST_FLAG(inherit);
    AUX_ADJUST_FLAG(secret);
    AUX_ADJUST_FLAG(hide_creator);
930
    AUX_ADJUST_FLAG(dont_garb);
931
932
933
934
935
936
937
938
939
940
941
942
    AUX_ADJUST_FLAG(reserved3);
    AUX_ADJUST_FLAG(reserved4);
    AUX_ADJUST_FLAG(reserved5);
    /* AUX_ADJUST_FLAG(deleted); */
    item->flags.deleted = 0;

    if (def->inherit_limit != 0 &&
        (item->inherit_limit > def->inherit_limit ||
         item->inherit_limit == 0))
        item->inherit_limit = def->inherit_limit;

    item->creator = creator;
943
    item->sent_at = current_time;
David Byers's avatar
David Byers committed
944
945
946
947
948
949
950
}



static Bool aux_item_validate(Aux_item_validation_data validation_data)
{
    unsigned long               i;
951
    const Aux_item_definition        *def;
David Byers's avatar
David Byers committed
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990

    def = validation_data.def;
    for (i = 0; i < def->num_validators; i++)
    {
        switch (def->validators[i].type)
        {
        case AUX_VALIDATE_FUNCTION:
            /*
             * Call the validation function
             * If it returns false, then abort validation
             * If it returns true, then continue with the
             * next validator
             */

            if ((*def->validators[i].v.fn.function)(&validation_data) == FALSE)
            {
                return FALSE;
            }
            break;

        case AUX_VALIDATE_REGEXP:

            /*
             * Ensure that the regexp is cached
             */
            
            if (def->validators[i].v.re.cached_re_buf == NULL)
            {
                aux_item_definition_cache_regexp(def, i);
            }

            /*
             * If the RE is still not cached there is a problem
             * in this case to not validate the item at all and
             * log a problem
             */

            if (def->validators[i].v.re.cached_re_buf == NULL)
            {
991
992
                kom_log("Failed to cache regexp validator for \"%s\". "
			"Rejecting all such items\n",
David Byers's avatar
David Byers committed
993
                        def->name);
David Byers's avatar
David Byers committed
994
995
                sfree(def->validators[i].v.re.regexp);
                def->validators[i].v.re.regexp = NULL;
David Byers's avatar
David Byers committed
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
                def->validators[i].type = AUX_VALIDATE_REJECT;
                kom_errno = KOM_ILL_AUX;
                return FALSE;
            }

            /*
             * RE is compiled and can be used
             */

            switch (re_search(def->validators[i].v.re.cached_re_buf,
                              validation_data.item->data.string,
                              s_strlen(validation_data.item->data),
                              0,
                              s_strlen(validation_data.item->data),
                              NULL))
            {
            case -1:
                /* No match */
                kom_errno = KOM_ILL_AUX;
                return FALSE;
            case -2:
                /* Internal error may (be temporary) */
1018
                kom_log("Internal error in regex matching aux-item data.\n");
David Byers's avatar
David Byers committed
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
                kom_errno = KOM_ILL_AUX;
                return FALSE;
                break;
            default:
                /* Matched */
                break;
            }
            

            break;

        case AUX_VALIDATE_REJECT:
            /*
             * Just reject the item. Used when we detect a problem
             * with a validator
             */

            kom_errno = KOM_ILL_AUX;
            return FALSE;
            break;

        default:
1041
            kom_log("BUG: unknown aux_item validator type\n");
David Byers's avatar
David Byers committed
1042

David Byers's avatar
David Byers committed
1043
1044
1045
1046
1047
1048
1049
1050
1051
        }
    }

    /*
     * If we got all the way to here we passed all the validators
     */

    return TRUE;

1052
1053
}

David Byers's avatar
David Byers committed
1054

1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
/*
 * aux_item_add_perm
 *
 * Check if it is alright to add an aux_item.
 *
 * ITEM is the item that you want to add.
 * DEF is the item's definition or NULL.
 * ITEM_CREATOR is the person who wants to create the item.
 * OBJECT_CREATOR is the person who created the object to which
 *     the item might be added. For texts it is the author, for
 *     persons it is the person and for conferences it is the
 *     conference supervisor.
 * OWNER_CHECK is TRUE when this function is supposed to check author
Per Cederqvist's avatar
Per Cederqvist committed
1068
 *     and supervisor-related flags. The only case when this is to
1069
1070
1071
1072
1073
 *     be FALSE is when checking author or supervisor makes not sense,
 *     such as when creating a completely new conference.
 * ADD_TO_LIST is the item list to add to, or NULL.
 * START_LOOKING_AT is the first index in add_to_list to start
 *     looking at for possible duplicates.
1074
 * CREATING is true if we are checking while creating an object.
1075
1076
 */

1077
static Bool
1078
1079
aux_item_add_perm(const Aux_item *item,
                  const Aux_item_definition *def,
1080
1081
1082
1083
                  Pers_no item_creator,
                  Pers_no object_creator,
                  Bool owner_check,
                  Aux_item_list *add_to_list,
1084
1085
                  unsigned long start_looking_at,
                  Bool creating,
1086
                  enum object_type object_type
1087
                  )
1088
{
1089
1090
    short can_add_when = 0;
    
David Byers's avatar
David Byers committed
1091
1092
    kom_errno = KOM_NO_ERROR;

1093
1094
    if (def == NULL)
	def = find_aux_item_definition(item);
1095
1096
1097
1098

    /* Can't create an item with no definition */
      
    if (def == NULL)
David Byers's avatar
David Byers committed
1099
1100
    {
        kom_errno = KOM_ILL_AUX;
1101
        return FALSE;
David Byers's avatar
David Byers committed
1102
    }
1103

David Byers's avatar
David Byers committed
1104
1105
1106
1107
1108
1109
    if (def->disabled)
    {
        kom_errno = KOM_ILL_AUX;
        return FALSE;
    }

1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
    switch (object_type)
    {
    case TEXT_OBJECT_TYPE: can_add_when = def->text_a; break;
    case CONF_OBJECT_TYPE: can_add_when = def->conf_a; break;
    default: can_add_when = 0;
    }

    if (can_add_when)
    {
        if (!((creating  && (can_add_when & AUX_ITEM_ADD_ON_CREATE)) ||
              (!creating && (can_add_when & AUX_ITEM_ADD_ON_MODIFY))))
        {
David Byers's avatar
David Byers committed
1122
            kom_errno = KOM_AUX_PERM;
1123
1124
1125
1126
            return FALSE;
        }
    }

David Byers's avatar
David Byers committed
1127
1128
1129
1130
1131
1132
1133
    if (def->system_only &&
        owner_check)
    {
        kom_errno = KOM_AUX_PERM;
        return FALSE;
    }

1134
1135
1136
1137
    /* FIXME (bug 334): the is_strictly_supervisor() call checks if
       item_creator is supervisor of object_creator.  The intention is
       to check if item_creator is supervisor of conference X, where
       X's supervisor is object_creator. */
1138
1139
1140
    if (def->author_only &&
        owner_check &&
        object_creator != item_creator &&
1141
        !is_strictly_supervisor(object_creator, item_creator, NULL) &&
David Byers's avatar
Server    
David Byers committed
1142
        !(item_creator == ACTPERS && ENA(wheel, 8))) /* NOT OK! */
1143
    {
David Byers's avatar
David Byers committed
1144
1145
        kom_errno = KOM_AUX_PERM;
        return FALSE;
1146
1147
    }

1148
1149
1150
1151
    /* FIXME (bug 334): the is_strictly_supervisor() call checks if
       item_creator is supervisor of object_creator.  The intention is
       to check if item_creator is supervisor of conference X, where
       X's supervisor is object_creator. */
1152
1153
    if (def->supervisor_only &&
         owner_check &&
1154
        !is_strictly_supervisor(object_creator, item_creator, NULL) &&
David Byers's avatar
Server    
David Byers committed
1155
        !(item_creator == ACTPERS && ENA(wheel,8))) /* NOT OK! */
1156
    {
David Byers's avatar
David Byers committed
1157
        kom_errno = KOM_AUX_PERM;
1158
1159
1160
1161
1162
1163
        return FALSE;
    }

    /* Even the administrator can't override this */

    if (!aux_item_check_unique(item, def, add_to_list, start_looking_at))
David Byers's avatar
David Byers committed
1164
1165
    {
        kom_errno = KOM_AUX_PERM;
1166
        return FALSE;
David Byers's avatar
David Byers committed
1167
    }
1168
1169
1170

    /* Check the contents */

David Byers's avatar
David Byers committed
1171
    if (def->num_validators > 0)
1172
    {
David Byers's avatar
David Byers committed
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
        Aux_item_validation_data validation_data;

        validation_data.item = item;
        validation_data.def = def;
        validation_data.item_creator = item_creator;
        validation_data.object_creator = object_creator;
        validation_data.owner_check = owner_check;
        validation_data.add_to_list = add_to_list;
        validation_data.start_looking_at = start_looking_at;
        validation_data.creating = creating;
        validation_data.object_type = object_type;

        if (aux_item_validate(validation_data) != TRUE)
David Byers's avatar
David Byers committed
1186
1187
        {
            kom_errno = KOM_ILL_AUX;
1188
            return FALSE;
David Byers's avatar
David Byers committed
1189
        }
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
    }

    return TRUE;
}


/*
 * aux_item_check_unique
 *
 * Check whether there are items that block creation of a new item.
 * ITEM is the item we want to add.
 * DEF is the item's definitioon
 * ADD_TO_LIST is the list we want to add to
 * START_LOOKING_AT is the first index in the list to look at.
 */

static Bool
1207
1208
aux_item_check_unique(const Aux_item *item,
                      const Aux_item_definition *def,
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
                      Aux_item_list *add_to_list,
                      unsigned long start_looking_at)
{
    unsigned long i;

    if (add_to_list == NULL ||
        item == NULL ||
        start_looking_at >= add_to_list->length)
        return TRUE;

1219
1220
    if (def != NULL)
	def = find_aux_item_definition(item);
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
    if (def == NULL || !def->one_per_person)
        return TRUE;

    for (i = start_looking_at; i < add_to_list->length; i++)
    {
        if (add_to_list->items[i].tag == item->tag &&
            add_to_list->items[i].creator == item->creator &&
            !add_to_list->items[i].flags.deleted)
            return FALSE;
    }

    return TRUE;
}


/*
 * aux_inherit_items
 *
 * Inherit aux items from PARENT to TARGET. COUNTER is a pointer
 * to the aux_no counter in the object being inherited to and CREATOR
 * is the author or equivalent of the target object.
 */

void
aux_inherit_items(Aux_item_list *target,
1246
                  const Aux_item_list *parent,
1247
                  unsigned long *counter,
1248
1249
                  Pers_no target_creator,
                  Bool creating,
1250
                  enum object_type object_type,
1251
1252
                  unsigned long object_no,
                  void *object)
1253
1254
1255
{
    int i;
    Aux_item item;
1256
    const Aux_item_definition *def;
1257

David Byers's avatar
David Byers committed
1258
    /* Inheriting linked items is strange, but works. */
David Byers's avatar
David Byers committed
1259

1260
1261
1262
1263
1264
1265
1266
1267
    if (!target || !parent)
        return;

    for (i = 0; i < parent->length; i++)
    {
        if (parent->items[i].flags.inherit &&
            !parent->items[i].flags.deleted &&
            parent->items[i].inherit_limit != 1)
1268
	{
1269
1270
1271
1272
1273
1274
1275
1276
            def = find_aux_item_definition(&parent->items[i]);
            if (def == NULL ||
                !aux_item_add_perm(&parent->items[i],
                                   def,
                                   parent->items[i].creator,
                                   target_creator,
                                   TRUE,
                                   target,
1277
1278
1279
                                   0,
                                   creating,
                                   object_type
1280
1281
1282
                                   ))
                continue;

David Byers's avatar
David Byers committed
1283
            copy_aux_item(&item, &parent->items[i]);
David Byers's avatar
David Byers committed
1284
            init_aux_item_link(&item.linked_item);
1285
            prepare_aux_item(&item, parent->items[i].creator, def);
1286
1287
1288
            if (item.inherit_limit != 0)
                item.inherit_limit -= 1;
            
David Byers's avatar
David Byers committed
1289
1290
1291
1292
1293
1294
            target->items = srealloc(target->items,
                                    (target->length + 1) * sizeof(Aux_item));
            target->items[target->length] = item;
            *counter += 1;
            target->items[target->length].aux_no = *counter;
            target->length += 1;
1295
1296
1297
1298
1299

            aux_item_call_add_triggers(def,
                                       object_type,
                                       target->length - 1,
                                       object_no,
David Byers's avatar
David Byers committed
1300
1301
                                       object,
                                       &target->items[target->length]);
1302
1303
1304
1305
        }
    }
}

1306
1307
1308
1309
1310
1311

/* Filter the items in ORIGINAL for sending to the person logged in on
 * connection CONN.  On entry, RESULT should be a pointer to an empty
 * aux_item_list.  It will be filled with copies of those items in
 * ORIGINAL that CONN is allowed to see.  All memory allocated in this
 * call is allocated with tmp_alloc. */
1312
1313

void
1314
1315
1316
filter_aux_item_list(const Aux_item_list *original,
                     Aux_item_list       *result,
                     const Connection    *viewer_conn)
1317
1318
1319
{
    Aux_item        *orig_aux;
    unsigned long    from, to;
David Byers's avatar
Server    
David Byers committed
1320
1321
    /*    Pers_no          viewer;
          Person        *viewer_p;*/
1322
1323
1324
1325
1326
1327
1328
1329
1330

    result->items = tmp_alloc(original->length * sizeof (Aux_item));
    result->length = 0;
    to = 0;

    for (from = 0; from < original->length; from++)
    {
        orig_aux = &original->items[from];
        if ( orig_aux->flags.secret &&
1331
             !is_supervisor(orig_aux->creator,
David Byers's avatar
Server    
David Byers committed
1332
1333
                            viewer_conn->pers_no, viewer_conn->person) &&
             !ENA_C(viewer_conn, admin, 4))
1334
1335
1336
1337
1338
            continue;

        result->items[to] = *orig_aux;

        if (orig_aux->flags.hide_creator &&
1339
             !is_supervisor(orig_aux->creator,
David Byers's avatar
Server    
David Byers committed
1340
1341
                            viewer_conn->pers_no, viewer_conn->person) &&
             !ENA_C(viewer_conn, admin, 4))
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
            result->items[to].creator = 0;

        result->length += 1;
        to += 1;
    }
}

/*
 * check_delete_aux
 *
 * Return TRUE if ACTPERS may delete ITEM. DEF is the item's definition
 * or NULL.
 */

Success
1357
check_delete_aux_item_list(const Number_list *items_to_delete,
1358
1359
                           const Aux_item_list *list_to_delete_from,
                           const Conf_no owner)
1360
{
David Byers's avatar
David Byers committed
1361
    long i;
1362
1363
    const Aux_item *item;
    const Aux_item_definition *def;
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375

    for (i = 0; i < items_to_delete->length; i++)
    {
        if (items_to_delete->data[i] == 0)
            continue;


        /* Get pointer to the item and to its definition */

        if ((item = find_aux_item(list_to_delete_from,
                                  items_to_delete->data[i])) == NULL)
        {
David Byers's avatar
David Byers committed
1376
            kom_errno = KOM_AUX_PERM;
1377
1378
1379
1380
1381
1382
1383
1384
            err_stat = i;
            return FAILURE;
        }


        /* Check all sorts of permissions */

        def = find_aux_item_definition(item);
1385
1386


1387
1388
        if (def == NULL)
	{
1389
1390
1391
            kom_errno = KOM_AUX_PERM;
            err_stat = i;
            return FAILURE;
1392
1393
1394
        }
	else if (def->may_not_delete)
	{
1395
1396
1397
            kom_errno = KOM_AUX_PERM;
            err_stat = i;
            return FAILURE;
1398
1399
1400
1401
1402
        }
	else if (item->creator == 0)
	{
            if (!ENA(wheel, 8))
	    {
1403
1404
1405
1406
                kom_errno = KOM_AUX_PERM;
                err_stat = i;
                return FAILURE;
            }
1407
1408
1409
        }
	else if (item->creator != ACTPERS)
	{
1410
1411
            if (!ENA(wheel, 8) &&
                !is_supervisor(item->creator, ACTPERS, NULL) &&
1412
1413
                (!def->owner_delete || !is_supervisor(owner, ACTPERS, NULL)))
	    {
1414
1415
1416
1417
                kom_errno = KOM_AUX_PERM;
                err_stat = i;
                return FAILURE;
            }
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
        }

        /* For already deleted items, clear them out of the list */

        if (item->flags.deleted)
            items_to_delete->data[i] = 0;
    }

    return OK;
}

/*
 * delete_aux_item_list
 *
 * Mark ITEM as deleted. LIST is the list we're deleting
 * the item from.
David Byers's avatar
David Byers committed
1434
1435
 *
 * Call commit_aux_item_list to make the changes permanent
1436
1437
1438
 */
 
void
1439
delete_aux_item_list(const Number_list *items_to_delete,
1440
                     Aux_item_list *list_to_delete_from,
1441
                     enum object_type object_type,
1442
1443
                     unsigned long object_no,
                     void *object)
1444
{
David Byers's avatar
David Byers committed
1445
    long i;
1446
    Aux_item *item;
1447
    const Aux_item *linked_item;
David Byers's avatar
David Byers committed
1448
    long item_index;
David Byers's avatar
David Byers committed
1449
1450
1451
1452
1453
    long            item_to_delete;
    unsigned long   linked_object_no;
    Number_list     linked_delete;
    Aux_item_list   *linked_item_list;
    void            *linked_object;
1454
1455
1456
1457
1458
1459

    for (i = 0; i < items_to_delete->length; i++)
    {
        if (items_to_delete->data[i] == 0)
            continue;
        
David Byers's avatar
David Byers committed
1460
1461
1462
1463
1464
        item_index = find_aux_item_index(list_to_delete_from,
                                         items_to_delete->data[i]);
        if (item_index == -1)
            restart_kom("Call to delete_aux_item_list without call to check_delete_aux_item_list");
        item = &list_to_delete_from->items[item_index];
1465
        item->flags.deleted = 1;
David Byers's avatar
David Byers committed
1466
1467
1468
1469
        aux_item_call_delete_triggers(find_aux_item_definition(item),
                                      object_type,
                                      item_index,
                                      object_no,
David Byers's avatar
David Byers committed
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
                                      object,
                                      item);
        /* If we have linked items, undelete them too.
           It is safe to do this with a recursive call
           since we have marked this item as undeleted.
           But just to avoid pointless recursion, only
           do the recursive call if the corresponding
           item is marked as deleted */

        linked_item = find_linked_aux_item(item);
        if (linked_item && !linked_item->flags.deleted)
        {
            /* Set up the undelete list for the target object */
            item_to_delete = item->linked_item.target_item;
            linked_delete.length = 1;
            linked_delete.data = &item_to_delete;

            /* Find the item list to undelete from */
            linked_item_list = find_linked_aux_item_list(item);

            /* Set up the object and object_no parameters for undelete */
            switch (item->linked_item.target_type)
            {
            case CONF_OBJECT_TYPE:
                linked_object_no = item->linked_item.target_object.conf;
David Byers's avatar
David Byers committed
1495
                linked_object = (void*)cached_get_conf_stat(linked_object_no);
David Byers's avatar
David Byers committed
1496
1497
1498
                break;
            case TEXT_OBJECT_TYPE:
                linked_object_no = item->linked_item.target_object.text;
David Byers's avatar
David Byers committed
1499
                linked_object = (void*)cached_get_text_stat(linked_object_no);
David Byers's avatar
David Byers committed
1500
1501
1502
1503
1504
1505
                break;
            case INFO_OBJECT_TYPE:
                linked_object_no = 0;
                linked_object = NULL;
                break;
            default: