aux-items.c 59.9 KB
Newer Older
1
/*
2
 * $Id: aux-items.c,v 1.61 2003/08/25 17:24:23 ceder Exp $
3
 * Copyright (C) 1994-2003  Lysator Academic Computer Association.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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.
 *
Per Cederqvist's avatar
Per Cederqvist committed
23
 * Please report bugs at http://bugzilla.lysator.liu.se/. 
24 25
 */

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
#include "timewrap.h"
41

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

#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
67 68
/* Externally accessible variables (used by the parser) */

69
Aux_item_definition     *aux_item_definition_list = NULL;
David Byers's avatar
David Byers committed
70 71 72 73
unsigned long            num_aux_item_definitions = 0; 

/* Variables local to this file */

74 75 76
#if COMPILED_AUX_ITEMS
static Aux_item_definition compiled_aux_items[] = {};
#endif
77

78

David Byers's avatar
David Byers committed
79 80
/* Forward declaration of triggers */

81 82 83 84
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
85 86 87 88


/* Forward declarations of validators */

89
static Success aux_item_validate_existing_text(Aux_item_validation_data *data);
90 91 92 93

/* 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
94

95 96 97
static void prepare_aux_item(Aux_item *item, Pers_no creator,
			     const Aux_item_definition *def);

David Byers's avatar
David Byers committed
98 99 100


/* Symbol table for the triggers */
101

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

David Byers's avatar
David Byers committed
112 113 114 115 116 117 118 119 120 121 122 123 124

/* 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 */

125 126 127 128 129 130
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
131
    0,                      /* Disabled */
132 133
    0,                      /* Author only */    
    0,                      /* Supervisor only */
David Byers's avatar
David Byers committed
134
    0,                      /* System only */
135
    0,                      /* Unique */         
136
    0,                      /* Unique-data */
137 138
    0,                      /* Can't delete */
    0,                      /* Owner delete */
139 140
    0,                      /* Inherit limit */  
    FALSE,                  /* Texts */
141
    0,
142
    FALSE,                  /* Confs */
143
    0,
144 145
    FALSE,                  /* Letterbox */
    FALSE,                  /* System */
David Byers's avatar
David Byers committed
146 147
    0,                      /* Number of validators */
    NULL,                   /* Validator list */
148 149 150
    0, NULL,
    0, NULL,
    0, NULL,
151 152 153 154 155 156 157 158 159 160 161 162 163
    NULL                    /* Next */
};

    


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

static Aux_item_definition simple_aux_item =
{
164
    (char*)"simple",        /* Name */           
165 166 167
    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
168
    0,                      /* Disabled */
169 170
    0,                      /* Author only */    
    0,                      /* Supervisor only */
David Byers's avatar
David Byers committed
171
    0,                      /* System only */
172
    0,                      /* Unique */         
173
    0,                      /* Unique-data */
174 175
    0,                      /* Can't delete */
    0,                      /* Owner delete */
176 177
    0,                      /* Inherit limit */  
    TRUE,                   /* Texts */
178
    0,
179
    TRUE,                   /* Confs */
180
    0,
181 182
    TRUE,                   /* Letterbox */
    TRUE,                   /* System */
David Byers's avatar
David Byers committed
183
    0, NULL,                /* Validators */
184 185 186 187
    0, NULL,                /* Add triggers */
    0, NULL,                /* Delete triggers */
    0, NULL,                /* Undelete triggers */
    NULL
188 189 190 191 192 193
};


/* Forward declarations */

static Bool
194
aux_item_check_unique(const Aux_item *item,
Per Cederqvist's avatar
Per Cederqvist committed
195
                      const Aux_item_definition *def,
196 197 198 199 200 201 202 203 204 205 206 207
                      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 *
208
aux_item_default_definition(const Aux_item *item)
209 210 211 212 213 214 215 216 217 218
{
    /* 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;

David Byers's avatar
David Byers committed
219
    return &simple_aux_item;
220 221 222
}


223 224 225 226 227
/*
 * aux_item_find_trigger
 */

Aux_item_trigger
228
aux_item_find_trigger(const char *trigger_name)
229 230 231 232 233 234 235 236 237 238 239 240
{
    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
241
Aux_item_validation_function
242
aux_item_find_validator(const char *validator_name)
David Byers's avatar
David Byers committed
243 244 245 246 247 248 249 250 251 252 253 254 255
{
    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;
}


256 257 258 259 260 261 262
/*
 * find_aux_item_definition
 *
 * Return the definition of ITEM or NULL if it does not
 * have a valid information.
 */

263
static const Aux_item_definition *
264
find_aux_item_definition(const Aux_item *item)
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
{
    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
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

/* 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
312
aux_item_call_add_triggers(const Aux_item_definition *def,
313
			   enum object_type object_type,
David Byers's avatar
David Byers committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
			   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
337
aux_item_call_delete_triggers(const Aux_item_definition *def,
338
                              enum object_type object_type,
David Byers's avatar
David Byers committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
                              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
361
aux_item_call_undelete_triggers(const Aux_item_definition *def,
362
				enum object_type object_type,
David Byers's avatar
David Byers committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
				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);
    }
}



386 387 388 389 390 391 392 393 394 395 396 397 398 399
/*
 * 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
400
        restart_kom("out of memory adding aux-item definition");
401 402 403 404 405 406 407 408
    }
    *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
409 410 411 412 413 414 415


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

Per Cederqvist's avatar
Per Cederqvist committed
416
static void
417
aux_item_definition_cache_regexp(const Aux_item_definition *def,
David Byers's avatar
David Byers committed
418
                                 unsigned long ix)
419 420 421 422 423 424
{
    struct re_pattern_buffer   *pat_buf;
    const char                 *errmsg;

    re_syntax_options = RE_SYNTAX_POSIX_EXTENDED;
            
David Byers's avatar
David Byers committed
425 426
    def->validators[ix].v.re.cached_re_buf = smalloc(sizeof(*pat_buf));
    if (def->validators[ix].v.re.cached_re_buf == NULL)
427
    {
David Byers's avatar
David Byers committed
428 429 430
        kom_log("Out of memory compiling aux-item regexp %lu (%s).\n",
                def->tag,
                def->name);
431 432
        return;
    }
David Byers's avatar
David Byers committed
433
    pat_buf = def->validators[ix].v.re.cached_re_buf;
434 435 436 437 438 439
    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
440 441
         re_compile_pattern(def->validators[ix].v.re.regexp,
                            strlen(def->validators[ix].v.re.regexp),
442 443 444
                            pat_buf))
        != NULL)
    {
David Byers's avatar
David Byers committed
445
        kom_log("%s in validate regexp of aux-item definition %lu (%s).\n",
446 447 448
                errmsg,
                def->tag,
                def->name);
David Byers's avatar
David Byers committed
449
        if (def->validators[ix].v.re.cached_re_buf)
450
        {
451 452
	    /* If re_compile_pattern fails, it can leave allocated
	       stuff in the buffer. */
453
            regfree(def->validators[ix].v.re.cached_re_buf);
David Byers's avatar
David Byers committed
454
            sfree(def->validators[ix].v.re.cached_re_buf);
455
        }
David Byers's avatar
David Byers committed
456 457
        if (def->validators[ix].v.re.regexp)
            string_free(def->validators[ix].v.re.regexp);
David Byers's avatar
David Byers committed
458
        def->validators[ix].v.re.cached_re_buf = NULL;
David Byers's avatar
David Byers committed
459
        def->validators[ix].v.re.regexp = NULL;
460 461 462 463 464 465 466
        return;
    }
}

void
initialize_aux_items(char *aux_def_file)
{
467
#if COMPILED_AUX_ITEMS
468 469 470 471 472 473 474 475 476
    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]);
    }
477
#endif
478 479 480 481

    /* Read definitions from configuration file */

    parse_aux_item_definitions(aux_def_file);
David Byers's avatar
David Byers committed
482
}
483

David Byers's avatar
David Byers committed
484 485 486 487 488 489
void
free_aux_item_definitions(void)
{
    unsigned long i;
    Aux_item_definition *def;
    Aux_item_definition *tmp;
David Byers's avatar
David Byers committed
490

David Byers's avatar
David Byers committed
491
    def = aux_item_definition_list;
492 493
    while (def != NULL)
    {
David Byers's avatar
David Byers committed
494 495 496 497 498 499 500 501
        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)
502 503
                {
                    regfree(def->validators[i].v.re.cached_re_buf);
David Byers's avatar
David Byers committed
504
                    sfree(def->validators[i].v.re.cached_re_buf);
505
                }
David Byers's avatar
David Byers committed
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
            }
        }
        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;
535
    }
David Byers's avatar
David Byers committed
536 537

    aux_item_definition_list = NULL;
538 539 540
}


541

542
static long
543
find_aux_item_index(const Aux_item_list *list, unsigned long aux_no)
544 545
{
    unsigned long i;
David Byers's avatar
David Byers committed
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563

    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;
564
    
David Byers's avatar
David Byers committed
565 566 567 568
    switch (item->linked_item.target_type)
    {
    case NO_OBJECT_TYPE:
        return NULL;
569

David Byers's avatar
David Byers committed
570 571 572 573 574 575 576 577 578 579 580 581 582
    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;

    default:
        /* No lists in these items */
583 584
        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
585 586 587 588 589 590
        return NULL;
    }
}

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

591
static const Aux_item *
David Byers's avatar
David Byers committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
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)
612
    {
David Byers's avatar
David Byers committed
613 614 615 616 617 618 619 620 621 622 623 624 625
    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:
    default:
        /* Need no commit for these objects */
626
	;
627 628 629
    }
}

David Byers's avatar
David Byers committed
630 631 632 633 634 635 636 637 638



/*
 * 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
639
 * pointers in aux-item trigger data.
David Byers's avatar
David Byers committed
640 641 642 643 644 645
 *
 * 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
646
static void
647 648 649 650 651 652 653 654 655
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)
656
{
David Byers's avatar
David Byers committed
657
    Aux_item_list       item_list;
658

David Byers's avatar
David Byers committed
659 660 661
    /* Get a pointer to the destination object */

    if (dst_ptr == NULL)
662
    {
David Byers's avatar
David Byers committed
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
        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;
694
    }
David Byers's avatar
David Byers committed
695 696 697 698 699 700 701 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


    /* 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
727
    prepare_aux_item_list(&item_list, src_item->creator);
David Byers's avatar
David Byers committed
728 729 730


    /*
731
     * Add the item and mark the destination as changed.
David Byers's avatar
David Byers committed
732 733 734 735 736 737 738 739 740
     *
     * 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
741 742
                                    (Text_no)dst_no, &item_list,
                                    src_item->creator);
David Byers's avatar
David Byers committed
743 744 745 746
        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
747 748
                                    (Conf_no)dst_no, &item_list,
                                    src_item->creator);
David Byers's avatar
David Byers committed
749 750 751 752
        mark_conference_as_changed((Conf_no)dst_no);
        break;

    case INFO_OBJECT_TYPE:
David Byers's avatar
Server:  
David Byers committed
753 754
        system_add_aux_item_list((Info *)dst_ptr, &item_list,
                                 src_item->creator);
David Byers's avatar
David Byers committed
755 756 757 758 759 760 761
        break;

    default:
        break;
    }

    /*
762
     * Mark the source object as changed.
David Byers's avatar
David Byers committed
763 764 765
     */

    mark_linked_object_as_changed(dst_item_data);
766 767
}

David Byers's avatar
David Byers committed
768 769 770



Per Cederqvist's avatar
Per Cederqvist committed
771
static void
David Byers's avatar
David Byers committed
772 773
aux_item_list_add_items(Aux_item_list *add_to_list,
                        Aux_item_list *items_to_add,
774
                        enum object_type object_type,
David Byers's avatar
David Byers committed
775 776 777 778
                        unsigned long  object_no,
                        void          *object_ptr,
                        unsigned long *highest_ptr,
                        Pers_no        item_creator)
779
{
David Byers's avatar
David Byers committed
780 781 782
    unsigned long       i;
    unsigned long       highest_local;
    unsigned long       start_index;
783

David Byers's avatar
David Byers committed
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
    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->length += items_to_add->length;
801 802
    add_to_list->items = srealloc(add_to_list->items,
                                  add_to_list->length * sizeof(Aux_item));
David Byers's avatar
David Byers committed
803 804 805
    *highest_ptr = *highest_ptr + items_to_add->length;

    for (i = 0; i < items_to_add->length; i++)
806
    {
David Byers's avatar
David Byers committed
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
        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]);
828 829
    }
}
830

David Byers's avatar
David Byers committed
831 832 833



834 835 836 837 838 839 840 841 842 843 844 845
/*
 * 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,
846
                      Pers_no creator)
847 848 849 850 851 852 853
{
    unsigned long i;

    if (list == NULL)
        return;
    
    for (i = 0; i < list->length; i++)
854
        prepare_aux_item(&list->items[i], creator, NULL);
855 856
}

857 858 859 860
static void
prepare_aux_item(Aux_item *item,
		 Pers_no creator,
		 const Aux_item_definition *def)
861 862 863 864 865 866 867 868 869 870
{
    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);
871
    AUX_ADJUST_FLAG(dont_garb);
872 873 874 875 876 877
    AUX_ADJUST_FLAG(reserved3);
    AUX_ADJUST_FLAG(reserved4);
    AUX_ADJUST_FLAG(reserved5);
    /* AUX_ADJUST_FLAG(deleted); */
    item->flags.deleted = 0;

878 879 880 881
    if (def->inherit_limit != 0
	&& (item->inherit_limit > def->inherit_limit
	    || item->inherit_limit == 0))
    {
882
        item->inherit_limit = def->inherit_limit;
883
    }
884 885

    item->creator = creator;
886
    item->sent_at = current_time.tv_sec;
David Byers's avatar
David Byers committed
887 888 889 890
}



891 892 893
/*
 * Sets kom_errno when it returns FAILURE.
 */
894 895
static Success
aux_item_validate(Aux_item_validation_data validation_data)
David Byers's avatar
David Byers committed
896 897
{
    unsigned long               i;
898
    const Aux_item_definition        *def;
David Byers's avatar
David Byers committed
899 900 901 902 903 904 905 906

    def = validation_data.def;
    for (i = 0; i < def->num_validators; i++)
    {
        switch (def->validators[i].type)
        {
        case AUX_VALIDATE_FUNCTION:
            /*
907 908 909
             * Call the validation function.
             * If it returns FAILURE, then abort validation (the
             * validation function must set kom_errno).
910
             * If it returns OK, then continue with the
911
             * next validator.
David Byers's avatar
David Byers committed
912 913
             */

914
            if ((*def->validators[i].v.fn.function)(&validation_data) != OK)
David Byers's avatar
David Byers committed
915
            {
916
                return FAILURE;
David Byers's avatar
David Byers committed
917 918 919 920 921 922
            }
            break;

        case AUX_VALIDATE_REGEXP:

            /*
923
             * Ensure that the regexp is cached.
David Byers's avatar
David Byers committed
924 925 926 927 928 929 930 931
             */
            
            if (def->validators[i].v.re.cached_re_buf == NULL)
            {
                aux_item_definition_cache_regexp(def, i);
            }

            /*
932 933 934
             * 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.
David Byers's avatar
David Byers committed
935 936 937 938
             */

            if (def->validators[i].v.re.cached_re_buf == NULL)
            {
939
                kom_log("Failed to cache regexp validator for \"%s\". "
940
			"Rejecting all such items.\n",
David Byers's avatar
David Byers committed
941
                        def->name);
David Byers's avatar
David Byers committed
942 943
                sfree(def->validators[i].v.re.regexp);
                def->validators[i].v.re.regexp = NULL;
David Byers's avatar
David Byers committed
944
                def->validators[i].type = AUX_VALIDATE_REJECT;
945
                kom_errno = KOM_ILL_AUX_DATA;
946
                return FAILURE;
David Byers's avatar
David Byers committed
947 948 949 950 951 952 953
            }

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

            switch (re_search(def->validators[i].v.re.cached_re_buf,
954
                              (char*)validation_data.item->data.string,
David Byers's avatar
David Byers committed
955 956 957 958 959 960 961
                              s_strlen(validation_data.item->data),
                              0,
                              s_strlen(validation_data.item->data),
                              NULL))
            {
            case -1:
                /* No match */
962
                kom_errno = KOM_ILL_AUX_DATA;
963
                return FAILURE;
David Byers's avatar
David Byers committed
964
            case -2:
Per Cederqvist's avatar
Per Cederqvist committed
965
                /* Internal error (may be temporary) */
966
                kom_log("Internal error in regex matching aux-item data.\n");
967
                kom_errno = KOM_ILL_AUX_DATA;
968
                return FAILURE;
David Byers's avatar
David Byers committed
969 970 971 972 973 974 975 976 977 978 979 980
                break;
            default:
                /* Matched */
                break;
            }
            

            break;

        case AUX_VALIDATE_REJECT:
            /*
             * Just reject the item. Used when we detect a problem
981
             * with a validator.
David Byers's avatar
David Byers committed
982 983
             */

984
            kom_errno = KOM_ILL_AUX_DATA;
985
            return FAILURE;
David Byers's avatar
David Byers committed
986 987 988
            break;

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

David Byers's avatar
David Byers committed
991 992 993 994
        }
    }

    /*
995
     * If we got all the way to here we passed all the validators.
David Byers's avatar
David Byers committed
996 997
     */

998
    return OK;
999 1000
}

David Byers's avatar
David Byers committed
1001

1002 1003 1004 1005 1006 1007 1008
/*
 * 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.
1009 1010 1011 1012 1013 1014 1015 1016
 * CREATING_CONN is the connection that is causing this item to be
 *     added.  It is NULL if the item is created due to inheritance.
 * SUBORDINATE. If author-only or supervisor-only, the person logged
 *     in on CREATING_CONN must be a supervisor of the SUBORDINATE to
 *     be allowed to create the item.  For a text, the SUBORDINATE
 *     should be set to the author of the text.  For a conference or
 *     person, this is the conference or person itself.
 * ADD_TO_LIST is the item list the item should be added to, or NULL.
1017 1018
 * START_LOOKING_AT is the first index in add_to_list to start
 *     looking at for possible duplicates.
1019 1020
 * CREATING is true if we are checking while creating the object that
 *     the item will be added to.
1021
 * OBJECT_TYPE is the type of the object the item is added to.
1022 1023
 *
 * Sets kom_errno when it returns FALSE.
1024 1025
 */

1026
static Bool
1027 1028
aux_item_add_perm(const Aux_item *item,
                  const Aux_item_definition *def,
1029 1030
                  Connection *creating_conn,
                  Pers_no subordinate,
1031
                  Aux_item_list *add_to_list,
1032 1033
                  unsigned long start_looking_at,
                  Bool creating,
1034
                  enum object_type object_type)
1035
{
1036 1037
    short can_add_when = 0;
    
1038 1039 1040 1041
    /* Either there is a connection responsible for the creation, or
       we are inheriting.  If we are inheriting, creating must be true.  */
    assert(creating_conn != NULL || creating);

1042 1043
    if (def == NULL)
	def = find_aux_item_definition(item);
1044 1045 1046 1047

    /* Can't create an item with no definition */
      
    if (def == NULL)
David Byers's avatar
David Byers committed
1048
    {
1049
        kom_errno = KOM_ILL_AUX_TAG;
1050
        return FALSE;
David Byers's avatar
David Byers committed
1051
    }
1052

David Byers's avatar
David Byers committed
1053 1054
    if (def->disabled)
    {
1055
        kom_errno = KOM_ILL_AUX_TAG;
David Byers's avatar
David Byers committed
1056 1057 1058
        return FALSE;
    }

1059 1060 1061 1062 1063 1064 1065 1066 1067
    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)
    {
1068 1069 1070 1071 1072 1073 1074 1075 1076
	if (creating)
	{
	    if (!(can_add_when & AUX_ITEM_ADD_ON_CREATE))
	    {
		kom_errno = KOM_AUX_MODIFY_ONLY;
		return FALSE;
	    }
	}
	else
1077
        {
1078 1079 1080 1081 1082 1083
	    if (!(can_add_when & AUX_ITEM_ADD_ON_MODIFY))
	    {
		kom_errno = KOM_AUX_CREATE_ONLY;
		return FALSE;
	    }
	}
1084 1085
    }

1086
    if (def->system_only)
David Byers's avatar
David Byers committed
1087
    {
1088
        kom_errno = KOM_AUX_SERVER_ONLY;
David Byers's avatar
David Byers committed
1089 1090 1091
        return FALSE;
    }

1092 1093 1094 1095
    if ((def->author_only || def->supervisor_only)
	&& !creating
	&& creating_conn != NULL
	&& !ENA_C(creating_conn, wheel, 8))
1096
    {
1097 1098 1099 1100 1101 1102
	if (def->author_only
	    && !is_supervisor(subordinate, creating_conn))
	{
	    kom_errno = KOM_AUX_PERM;
	    return FALSE;
	}
1103

1104 1105 1106 1107 1108 1109 1110 1111 1112
	/* FIXME (bug 313): is_strictly_supervisor() or is_supervisor()? */ 
	if (def->supervisor_only
	    && !is_strictly_supervisor(subordinate,
				       creating_conn->pers_no,
				       creating_conn->person))
	{
	    kom_errno = KOM_AUX_PERM;
	    return FALSE;
	}
1113 1114 1115 1116 1117 1118 1119 1120 1121
    }

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

    if (!aux_item_check_unique(item, def, add_to_list, start_looking_at))
        return FALSE;

    /* Check the contents */

David Byers's avatar
David Byers committed
1122
    if (def->num_validators > 0)
1123
    {
David Byers's avatar
David Byers committed
1124 1125 1126 1127
        Aux_item_validation_data validation_data;

        validation_data.item = item;
        validation_data.def = def;
1128 1129 1130 1131 1132 1133 1134 1135
        validation_data.creating_conn = creating_conn;
	validation_data.creating = creating;
        validation_data.subordinate = subordinate;
#if 0
	/* Since no validator currently uses these fields, they
	   have been temporarily removed from the
	   Aux_item_validation_data type. */
	
David Byers's avatar
David Byers committed
1136 1137 1138
        validation_data.add_to_list = add_to_list;
        validation_data.start_looking_at = start_looking_at;
        validation_data.object_type = object_type;