dbck.c 36.6 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: dbck.c,v 0.41 1998/10/11 16:56:26 ceder Exp $
Per Cederqvist's avatar
Per Cederqvist committed
3
 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996  Lysator Academic Computer Association.
Linus Tolke Y's avatar
Linus Tolke Y committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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. 
 */
Per Cederqvist's avatar
Per Cederqvist committed
25 26 27 28 29 30
/*
 * dbck.c - A simple database checker and corrector.
 *
 * Author: Per Cederqvist.
 */

31
static const char *
32
rcsid = "$Id: dbck.c,v 0.41 1998/10/11 16:56:26 ceder Exp $";
33 34
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
35

Per Cederqvist's avatar
Per Cederqvist committed
36 37 38
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
39
#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
40 41 42
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
43 44
#include <time.h>
#include <sys/types.h>
45 46 47 48 49 50
#ifdef HAVE_CRYPT_H
#  include <crypt.h>
#endif
#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif
51 52 53
#ifdef HAVE_STRING_H
#  include <string.h>
#endif
54
#include <assert.h>
55

Per Cederqvist's avatar
Per Cederqvist committed
56
#include "getopt.h"
Per Cederqvist's avatar
Per Cederqvist committed
57
#include "misc-types.h"
58
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
59 60
#include "kom-types.h"
#include "tmp-limits.h"
Per Cederqvist's avatar
Per Cederqvist committed
61
#include "lyskomd.h"
Per Cederqvist's avatar
Per Cederqvist committed
62 63
#include "log.h"
#include "server/smalloc.h"
Per Cederqvist's avatar
Per Cederqvist committed
64
#include "misc-parser.h"
Per Cederqvist's avatar
Per Cederqvist committed
65 66 67
#include "cache.h"
#include "config.h"
#include "debug.h"
Per Cederqvist's avatar
Per Cederqvist committed
68
#include "dbck-cache.h"
69 70 71
#include "param.h"
#include "server-config.h"
#include "manipulate.h"
Per Cederqvist's avatar
Per Cederqvist committed
72
#include "version-info.h"
73
#include "ram-output.h"
74
#include "unused.h"
75
#include "local-to-global.h"
Per Cederqvist's avatar
Per Cederqvist committed
76

77 78 79 80 81 82 83 84

#define OPT_PERS_PRESENTATION_CONF      1
#define OPT_CONF_PRESENTATION_CONF      2
#define OPT_MOTD_CONF                   3
#define OPT_MOTD_OF_KOM                 4
#define OPT_KOM_NEWS_CONF               5


Per Cederqvist's avatar
Per Cederqvist committed
85 86 87 88 89 90 91 92 93 94 95 96
static struct option longopts[] = {
    {"compact-text-mass", 0, 0, 'g' },
    {"interactive", 0, 0, 'i' },
    {"auto-repair", 0, 0, 'r' },
    {"verbose", 0, 0, 'v' },
    {"print-statistics", 0, 0, 's' },
    {"list-text-no", 0, 0, 't' },
    {"set-change-name", 0, 0, 'c' },
    {"clear-password", required_argument, 0, 'P' },
    {"grant-all", required_argument, 0, 'G' },
    {"output-version", required_argument, 0, 'o' },
    {"force-output", 0, 0, 'F' },
97 98 99 100 101 102 103
    {"pers-pres-conf", required_argument, 0, 
     OPT_PERS_PRESENTATION_CONF },
    {"conf-pres-conf", required_argument, 0, 
     OPT_CONF_PRESENTATION_CONF },
    {"motd-conf", required_argument, 0, OPT_MOTD_CONF },
    {"motd-of-kom", required_argument, 0, OPT_MOTD_OF_KOM },
    {"kom-news-conf", required_argument, 0, OPT_KOM_NEWS_CONF },
Per Cederqvist's avatar
Per Cederqvist committed
104 105 106 107 108
    {"help", 0, 0, 'h' },
    { 0, 0, 0, 0 }
};

    
109

110
extern Info kom_info;
111
struct kom_par param;
Per Cederqvist's avatar
Per Cederqvist committed
112

113 114 115 116
/* This is set to TRUE if init_cache finds out that the last part of the
   database is missing. */
Bool truncated_texts = FALSE;

Per Cederqvist's avatar
Per Cederqvist committed
117 118 119 120
int vflag=0;			/* Verbose - list statistics also. */
int iflag=0;			/* Interactive - prompt user and repair. */
int rflag=0;			/* Repair simple error without confirmation. */
int gflag=0;			/* Garbage collect text-file. */
121
int sflag=0;			/* Statistic flag. */
Per Cederqvist's avatar
Per Cederqvist committed
122 123
static Pers_no reset_pwd=0;	/* Person whose password should be cleared. */
static Pers_no grant_all=0;	/* Person which should receive all bits. */
124 125 126

/* The following variable holds the output format */

127
long oformat=-1;		/* Output format */
128 129
long force_output=0;		/* Force sync (for conversions) */

130 131 132 133 134 135 136 137 138
/* The following variables hold kom_info modifications */

int pers_pres_conf = -1;
int conf_pres_conf = -1;
int motd_conf = -1;
int motd_of_lyskom = -1;
int kom_news_conf = -1;


139 140 141 142
/* The following variable corresponds to the -c flag, and is
   present here due to a bug in lyskomd 1.6.1. */
int unset_change_name_is_error=0;

143 144 145 146
/* A list of all available text numbers was useful when developing
   new data structures in lyskomd in 1995.  Option: -t. */
int dump_text_numbers=0;

Per Cederqvist's avatar
Per Cederqvist committed
147 148 149 150 151 152 153 154 155 156
int modifications = 0;

typedef struct {
    int	created_confs;
} Person_scratchpad;

static const Person_scratchpad   EMPTY_PERSON_SCRATCHPAD = { 0 };

static Person_scratchpad *person_scratchpad[MAX_CONF];

Per Cederqvist's avatar
Per Cederqvist committed
157
#ifndef NDEBUG
Per Cederqvist's avatar
Per Cederqvist committed
158 159 160
int	buglevel = 0;
#endif

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

struct delete_list {
    struct delete_list *next;
    Local_text_no lno;
};

static void
delete_list_append(struct delete_list **head,
		   Local_text_no lno)
{
    struct delete_list *item = smalloc(sizeof(struct delete_list));
    item->next = *head;
    item->lno =  lno;
    *head = item;
}

static void
execute_deletions(Local_to_global *l2g,
		  struct delete_list **head)
{
    struct delete_list *item;

    while (*head != NULL)
    {
	item = *head;
	*head = item->next;
	l2g_delete(l2g, item->lno);
	sfree(item);
    }
}


Per Cederqvist's avatar
Per Cederqvist committed
193
#if defined(HAVE_VFPRINTF) && defined(HAVE_STDARG_H)
Per Cederqvist's avatar
Per Cederqvist committed
194 195 196 197 198 199 200 201 202 203 204
extern void
log (const char * format, ...)
{
    va_list AP;

    va_start(AP, format);

    vfprintf(stdout, format, AP);

    va_end(AP);
}
205 206
#else
extern void
207 208 209
log (format, a, b, c, d, e, f, g)
     const char * format;
     int a, b, c, d, e, f, g;
210 211 212 213
{
    fprintf(stdout, format, a, b, c, d, e, f, g);
}
#endif
Per Cederqvist's avatar
Per Cederqvist committed
214

Per Cederqvist's avatar
Per Cederqvist committed
215
#if defined(HAVE_VFPRINTF) && defined(HAVE_STDARG_H)
Per Cederqvist's avatar
Per Cederqvist committed
216 217 218 219 220 221 222 223 224 225 226 227
extern void
restart_kom (const char * format, ...)
{
    va_list AP;

    va_start(AP, format);

    vfprintf(stdout, format, AP);

    va_end(AP);
    exit(1);
}
Per Cederqvist's avatar
Per Cederqvist committed
228
#else
229
extern void
230 231 232
restart_kom (format, a, b, c, d, e, f, g)
     const char * format;
     int a, b, c, d, e, f, g;
233 234 235 236 237
{
    fprintf(stdout, format, a, b, c, d, e, f, g);
    exit(1);
}
#endif
Per Cederqvist's avatar
Per Cederqvist committed
238

239
void 
240 241 242
register_jubel(Pers_no UNUSED(pno),
	       Text_no UNUSED(divis),
	       Text_no UNUSED(tno))
243 244 245 246 247
{
    /* Not needed in dbck.c, but called from conf-file.c. */
}


Per Cederqvist's avatar
Per Cederqvist committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
static Person_scratchpad *
alloc_person_scratchpad(void)
{
    Person_scratchpad *p;

    p = smalloc(sizeof(Person_scratchpad));
    *p = EMPTY_PERSON_SCRATCHPAD;
    return p;
}


static Bool
is_comment_to(Text_no    comment,
	      Text_stat *parent)
{
    int i;

    for ( i = 0; i < parent->no_of_misc; i++ )
    {
	switch( parent->misc_items[ i ].type )
	{
	case comm_in:
	    if ( parent->misc_items[ i ].datum.commented_in == comment )
		return TRUE;
	    break;
	default:
	    break;
	}
    }

    return FALSE;
}

static Bool
is_commented_in(Text_no    parent,
		Text_stat *child)
{
    int i;

    for ( i = 0; i < child->no_of_misc; i++ )
    {
	switch( child->misc_items[ i ].type )
	{
	case comm_to:
	    if ( child->misc_items[ i ].datum.comment_to == parent )
		return TRUE;
	    break;
	default:
	    break;
	}
    }

    return FALSE;
}
	
static Bool
is_footnote_to(Text_no    footnote,
	       Text_stat *parent)
{
    int i;

    for ( i = 0; i < parent->no_of_misc; i++ )
    {
	switch( parent->misc_items[ i ].type )
	{
	case footn_in:
	    if ( parent->misc_items[ i ].datum.footnoted_in == footnote )
		return TRUE;
	    break;
	default:
	    break;
	}
    }

    return FALSE;
}

static Bool
is_footnoted_in(Text_no    parent,
		Text_stat *child)
{
    int i;

    for ( i = 0; i < child->no_of_misc; i++ )
    {
	switch( child->misc_items[ i ].type )
	{
	case footn_to:
	    if ( child->misc_items[ i ].datum.footnote_to == parent )
		return TRUE;
	    break;
	default:
	    break;
	}
    }

    return FALSE;
}

347
Member *
Per Cederqvist's avatar
Per Cederqvist committed
348 349 350
locate_member(Pers_no      pers_no,
	      Conference * conf_c)
{
351
    Member * mbr;
Per Cederqvist's avatar
Per Cederqvist committed
352 353
    int      i;

354 355
    for(mbr = conf_c->members.members, i = conf_c->members.no_of_members;
	i > 0; i--, mbr++)
Per Cederqvist's avatar
Per Cederqvist committed
356
    {
357
	if ( mbr->member == pers_no )
Per Cederqvist's avatar
Per Cederqvist committed
358
	{
359
	    return mbr;
Per Cederqvist's avatar
Per Cederqvist committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
	}
    }

    return NULL;
}

/*
 * Delete a misc_info.
 * If it is a recpt, cc_recpt, comm_to or footn_to delete any
 * loc_no, rec_time, sent_by or sent_at that might follow it.
 *
 * Note that the Misc_info is not reallocated.
 */

static void
delete_misc (Text_stat *tstat,
	     Misc_info *misc)	/* Pointer to first misc_item to delete. */
{
    int del = 1;		/* Number of items to delete. */
    				/* Always delete at least one item. */
    Bool ready;
    
    /* Check range of misc */

    if (misc < tstat->misc_items
	|| misc >= tstat->misc_items + tstat->no_of_misc )
    {
387
	restart_kom("delete_misc() - misc out of range\n");
Per Cederqvist's avatar
Per Cederqvist committed
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    }

    ready = FALSE;
    
    while (ready == FALSE
	   && misc + del < tstat->misc_items + tstat->no_of_misc )
    {
	switch ( misc[ del ].type )
	{
	case loc_no:
	case rec_time:
	case sent_by:
	case sent_at:
	    del++;
	    break;

	case recpt:
	case cc_recpt:
406
        case bcc_recpt:
Per Cederqvist's avatar
Per Cederqvist committed
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
	case footn_to:
	case footn_in:
	case comm_to:
	case comm_in:
	    ready = TRUE;
	    break;

#ifndef COMPILE_CHECKS
	default:
	    restart_kom("delete_misc() - illegal misc found.\n");
#endif
	}
    }

    tstat->no_of_misc -= del;

    /* Move items beyond the deleted ones. */

    while ( misc < tstat->misc_items + tstat->no_of_misc )
    {
	misc[ 0 ] = misc[ del ];
	misc++;
    }
}

static int
433
confirm(const char *question)
Per Cederqvist's avatar
Per Cederqvist committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
{
    if ( iflag )
    {
	fputs(question, stdout);
	fputs(" (y/n) ", stdout);
	while(1)
	    switch(getchar())
	    {
	    case 'y':
	    case 'Y':
		return 1;
	    case 'n':
	    case 'N':
	    case EOF:
		return 0;
	    default:
		break;
	    }
    }
    else
	return 0;
}
	
    
static long
check_misc_infos(Text_no    tno,
		 Text_stat *tstat)
{
    const Misc_info   * misc = tstat->misc_items;
    Misc_info   * previous;
    Misc_info_group group;
    Conference *c;
    Text_stat *t;
467 468 469
    Local_text_no conf_min;
    Local_text_no conf_max;
    
470
    long errors=0;
Per Cederqvist's avatar
Per Cederqvist committed
471 472 473 474 475 476 477 478 479 480 481 482

    while (previous = (Misc_info *)misc,
	   group = parse_next_misc(&misc,
				   tstat->misc_items + tstat->no_of_misc),
	   group.type != m_end_of_list && group.type != m_error )
    {
	switch ( group.type )
	{
	case m_recpt:
	    c = cached_get_conf_stat (group.recipient);
	    if ( c == NULL && group.recipient == 0 )
	    {
Per Cederqvist's avatar
Per Cederqvist committed
483 484
		log ("Conference 0 is recipient to text %lu.\n",
		     (unsigned long)tno);
Per Cederqvist's avatar
Per Cederqvist committed
485 486 487 488 489 490 491 492 493
		if (rflag || confirm("Repair by deleting misc_item? "))
		{
		    delete_misc(tstat, previous);
		    mark_text_as_changed(tno);
		    modifications++;
		    log("Repaired: Conference 0 is no longer a recipient.\n");
		    misc = previous;
		}
		else
494
		    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
495 496 497 498 499 500 501 502
		
		break;
	    }

	    if ( c == NULL )
		break;
	    
	    /* Check loc_no */
503 504 505 506
	    conf_min = l2g_lookup(&c->texts, 0);
	    conf_max = l2g_first_appendable_key(&c->texts);
	    
	    if (group.local_no < conf_min)
Per Cederqvist's avatar
Per Cederqvist committed
507 508
	    {
		log("Text %lu: Recipient %lu<%lu> loc_no is less than %lu\n",
509 510
		    (unsigned long)tno, (unsigned long)group.recipient,
		    (unsigned long)group.local_no,
511
		    (unsigned long)conf_min);
512
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
513
	    }
514
	    else if (group.local_no >= conf_max)
Per Cederqvist's avatar
Per Cederqvist committed
515
	    {
516
		log("Text %lu: Recipient %lu<%lu> loc_no >= %lu\n",
517 518
		    (unsigned long)tno, (unsigned long)group.recipient,
		    (unsigned long)group.local_no,
519
		    (unsigned long)conf_max);
520
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
521
	    }
522
	    else if (l2g_lookup(&c->texts, group.local_no) != tno)
Per Cederqvist's avatar
Per Cederqvist committed
523
	    {
Per Cederqvist's avatar
Per Cederqvist committed
524
		log("Text %lu: Recipient %lu<%lu>: %s to %lu.\n",
525 526
		    (unsigned long)tno, (unsigned long)group.recipient,
		    (unsigned long)group.local_no,
Per Cederqvist's avatar
Per Cederqvist committed
527
		    "that local number is mapped",
528
		    (unsigned long)l2g_lookup(&c->texts, group.local_no));
529
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
530 531 532 533 534 535 536 537 538
	    }

	    break;

	case m_cc_recpt:
	    c = cached_get_conf_stat (group.cc_recipient);
	    if ( c == NULL && group.cc_recipient == 0 )
	    {
		log ("Conference 0 is cc_recipient to text %lu.\n",
539
		     (unsigned long)tno);
Per Cederqvist's avatar
Per Cederqvist committed
540 541 542 543 544
		if (rflag || confirm("Repair by deleting misc_item? "))
		{
		    delete_misc(tstat, previous);
		    mark_text_as_changed(tno);
		    modifications++;
Per Cederqvist's avatar
Per Cederqvist committed
545
		    log("Repaired: Conf 0 is no longer a cc_recipient.\n");
Per Cederqvist's avatar
Per Cederqvist committed
546 547 548
		    misc = previous;
		}
		else
549
		    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
550 551 552 553 554 555 556 557
		
		break;
	    }

	    if ( c == NULL )
		break;
	    
	    /* Check loc_no */
558 559 560 561
	    conf_min = l2g_lookup(&c->texts, 0);
	    conf_max = l2g_first_appendable_key(&c->texts);

	    if (group.local_no < conf_min)
Per Cederqvist's avatar
Per Cederqvist committed
562 563
	    {
		log("Text %lu: CC_Recipient %lu<%lu> is less than %lu\n",
564 565
		    (unsigned long)tno, (unsigned long)group.cc_recipient,
		    (unsigned long)group.local_no,
566
		    (unsigned long)conf_min);
567
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
568
	    }
569
	    else if (group.local_no >= conf_max)
Per Cederqvist's avatar
Per Cederqvist committed
570
	    {
571
		log("Text %lu: CC_Recipient %lu<%lu> loc_no >= %lu\n",
572 573
		    (unsigned long)tno, (unsigned long)group.cc_recipient,
		    (unsigned long)group.local_no,
574
		    (unsigned long)conf_max);
575
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
576
	    }
577
	    else if (l2g_lookup(&c->texts, group.local_no) != tno)
Per Cederqvist's avatar
Per Cederqvist committed
578
	    {
Per Cederqvist's avatar
Per Cederqvist committed
579
		log("Text %lu: CC_Recipient %lu<%lu>: %s to %lu.\n",
580 581
		    (unsigned long)tno, (unsigned long)group.cc_recipient,
		    (unsigned long)group.local_no,
Per Cederqvist's avatar
Per Cederqvist committed
582
		    "that local number is mapped",
583
		    (unsigned long)l2g_lookup(&c->texts, group.local_no));
584
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
585 586 587 588
	    }

	    break;

589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	case m_bcc_recpt:
	    c = cached_get_conf_stat (group.bcc_recipient);
	    if ( c == NULL && group.bcc_recipient == 0 )
	    {
		log ("Conference 0 is bcc_recipient to text %lu.\n",
		     (unsigned long)tno);
		if (rflag || confirm("Repair by deleting misc_item? "))
		{
		    delete_misc(tstat, previous);
		    mark_text_as_changed(tno);
		    modifications++;
		    log("Repaired: Conf 0 is no longer a bcc_recipient.\n");
		    misc = previous;
		}
		else
604
		    errors++;
605 606 607 608 609 610 611 612
		
		break;
	    }

	    if ( c == NULL )
		break;
	    
	    /* Check loc_no */
613 614 615 616
	    conf_min = l2g_lookup(&c->texts, 0);
	    conf_max = l2g_first_appendable_key(&c->texts);

	    if (group.local_no < conf_min)
617 618 619 620
	    {
		log("Text %lu: BCC_Recipient %lu<%lu> is less than %lu\n",
		    (unsigned long)tno, (unsigned long)group.bcc_recipient,
		    (unsigned long)group.local_no,
621
		    (unsigned long)conf_min);
622
		errors++;
623
	    }
624
	    else if (group.local_no >= conf_max)
625
	    {
626
		log("Text %lu: BCC_Recipient %lu<%lu> loc_no >= %lu\n",
627 628
		    (unsigned long)tno, (unsigned long)group.bcc_recipient,
		    (unsigned long)group.local_no,
629
		    (unsigned long)conf_max);
630
		errors++;
631
	    }
632
	    else if (l2g_lookup(&c->texts, group.local_no) != tno)
633 634 635 636 637
	    {
		log("Text %lu: BCC_Recipient %lu<%lu>: %s to %lu.\n",
		    (unsigned long)tno, (unsigned long)group.bcc_recipient,
		    (unsigned long)group.local_no,
		    "that local number is mapped",
638 639
		    (unsigned long)l2g_lookup(&c->texts,
					      group.local_no));
640
		errors++;
641 642 643 644
	    }

	    break;

Per Cederqvist's avatar
Per Cederqvist committed
645 646 647 648 649 650
	case m_comm_to:
	    t = cached_get_text_stat(group.comment_to);

	    if ( t == NULL )
	    {
		log("Text %lu is a comment to %lu, which doesn't exist.\n",
651
		    (unsigned long)tno, (unsigned long)group.comment_to);
Per Cederqvist's avatar
Per Cederqvist committed
652 653 654 655 656 657 658 659 660 661

		if (rflag || confirm("Repair by deleting misc_item? "))
		{
		    delete_misc(tstat, previous);
		    mark_text_as_changed(tno);
		    modifications++;
		    log("Repaired: Comment-link deleted.\n");
		    misc = previous;
		}
		else
662
		    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
663
		    
664
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
665 666 667 668
	    }
	    else if (!is_comment_to(tno, t))
	    {
		log("Text %lu is a comment to %lu, but not the reverse.\n",
669
		    (unsigned long)tno, (unsigned long)group.comment_to);
670
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
671 672 673 674 675 676 677 678 679 680
	    }

	    break;

	case m_comm_in:
	    t = cached_get_text_stat(group.commented_in);

	    if ( t == NULL )
	    {
		log("Text %lu is commented in %lu, which doesn't exist.\n",
681
		    (unsigned long)tno, (unsigned long)group.commented_in);
Per Cederqvist's avatar
Per Cederqvist committed
682 683 684 685 686 687 688 689 690 691

		if (rflag || confirm("Repair by deleting misc_item? "))
		{
		    delete_misc(tstat, previous);
		    mark_text_as_changed(tno);
		    modifications++;
		    log("Repaired: Comment-link deleted.\n");
		    misc = previous;
		}
		else
692
		    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
693 694 695
	    }
	    else if (!is_commented_in(tno, t))
	    {
696
		log("Text %lu is commented in %lu, but not the reverse.\n",
697
		    (unsigned long)tno, (unsigned long)group.commented_in);
698
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
699 700 701 702 703 704 705 706 707 708
	    }

	    break;

	case m_footn_to:
	    t = cached_get_text_stat(group.footnote_to);

	    if ( t == NULL )
	    {
		log("Text %lu is a footnote to %lu, which doesn't exist.\n",
709
		    (unsigned long)tno, (unsigned long)group.footnote_to);
Per Cederqvist's avatar
Per Cederqvist committed
710 711 712 713 714 715 716 717 718 719

		if (rflag || confirm("Repair by deleting misc_item? "))
		{
		    delete_misc(tstat, previous);
		    mark_text_as_changed(tno);
		    modifications++;
		    log("Repaired: Footnote-link deleted.\n");
		    misc = previous;
		}
		else
720
		    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
721 722 723 724
	    }
	    else if (!is_footnote_to(tno, t))
	    {
		log("Text %lu is a footnote to %lu, but not the reverse.\n",
725
		    (unsigned long)tno, (unsigned long)group.footnote_to);
726
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
727 728 729 730 731 732 733 734 735 736
	    }

	    break;
	    
	case m_footn_in:
	    t = cached_get_text_stat(group.footnoted_in);

	    if ( t == NULL )
	    {
		log("Text %lu is footnoted in %lu, which doesn't exist.\n",
737
		    (unsigned long)tno, (unsigned long)group.footnoted_in);
Per Cederqvist's avatar
Per Cederqvist committed
738 739 740 741 742 743 744 745 746 747

		if (rflag || confirm("Repair by deleting misc_item? "))
		{
		    delete_misc(tstat, previous);
		    mark_text_as_changed(tno);
		    modifications++;
		    log("Repaired: Footnote-link deleted.\n");
		    misc = previous;
		}
		else
748
		    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
749 750 751
	    }
	    else if (!is_footnoted_in(tno, t))
	    {
752
		log("Text %lu is footnoted in %lu, but not the reverse.\n",
753
		    (unsigned long)tno, (unsigned long)group.footnoted_in);
754
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
755 756 757 758 759 760
	    }

	    break;

	default:
	    log("check_misc_infos(): parse_next_misc returned type %lu\n",
761
		(unsigned long)group.type);
Per Cederqvist's avatar
Per Cederqvist committed
762 763 764 765 766 767
	    break;
	}
    }

    if ( group.type == m_error )
    {
768
	log("Text %lu has a bad misc_info_list.\n", (unsigned long)tno);
769
	errors++;
Per Cederqvist's avatar
Per Cederqvist committed
770 771
    }

772
    return errors;
Per Cederqvist's avatar
Per Cederqvist committed
773 774 775 776 777 778 779 780 781 782 783 784
}

		    
		     
		 
static long
check_texts(void)
{
    Text_no    ct = 0;
    Text_stat *ctp=NULL;
    long	errors = 0;
    Text_no	number_of_texts = 0;
785 786
    unsigned long	bytes=0;
    unsigned long	max_bytes=0;
Per Cederqvist's avatar
Per Cederqvist committed
787 788
    Text_no	max_text=0;

789
    while ( (ct = traverse_text(ct)) != 0 )
Per Cederqvist's avatar
Per Cederqvist committed
790 791 792 793 794 795 796 797 798 799 800
    {
	number_of_texts++;
	
	ctp = cached_get_text_stat( ct );
	if ( ctp == NULL )
	{
	    log("Text %lu nonexistent.\n", ct);
	    errors++;
	}
	else
	{
801 802 803
	    if (dump_text_numbers)
		log("Checking text_no %ld\n", (unsigned long)ct);

Per Cederqvist's avatar
Per Cederqvist committed
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
	    bytes += ctp->no_of_chars;
	    if ( ctp->no_of_chars > max_bytes )
	    {
		max_bytes = ctp->no_of_chars;
		max_text = ct;
	    }
	    
	    /* no_of_marks is not yet checked. */
	    errors += check_misc_infos(ct, ctp);
	}
    }

    if (vflag)
    {
	if ( number_of_texts == 0 )
	    log("WARNING: No texts found.\n");
	else
	{
Per Cederqvist's avatar
Per Cederqvist committed
822
	    log("Total of %lu texts (total %lu bytes, avg. %lu bytes/text).\n",
823 824 825
		(unsigned long)number_of_texts,
		(unsigned long)bytes,
		(unsigned long)(bytes/number_of_texts));
Per Cederqvist's avatar
Per Cederqvist committed
826
	    log("Longest text is %lu (%lu bytes).\n",
827
		(unsigned long)max_text, (unsigned long)max_bytes);
Per Cederqvist's avatar
Per Cederqvist committed
828 829 830 831 832 833 834 835 836
	}
    }
    
    return errors;
}


static int
check_created_texts(Pers_no pno,
837
		    Local_to_global *created)
Per Cederqvist's avatar
Per Cederqvist committed
838 839
{
    Text_stat *t;
840
    int errors=0;
841 842
    L2g_iterator iter;
    struct delete_list *del_list = NULL;
Per Cederqvist's avatar
Per Cederqvist committed
843

844
    for (l2gi_searchall(&iter, created); !iter.search_ended; l2gi_next(&iter))
Per Cederqvist's avatar
Per Cederqvist committed
845
    {
846 847
	assert(iter.tno != 0);
	assert(iter.lno != 0);
Per Cederqvist's avatar
Per Cederqvist committed
848

849 850 851 852 853 854 855
	t = cached_get_text_stat(iter.tno);
	if ( t != NULL && t->author != pno)
	{
	    log("Person %lu is author of text %lu whose author is %lu.\n",
		(unsigned long)pno, (unsigned long)iter.tno,
		(unsigned long)t->author);
	    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
856 857
	}

858
	if ( t == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
859
	{
860 861 862 863 864 865 866 867 868 869 870 871
	    log("Person %lu is author of text %lu, which doesn't exist.\n",
		(unsigned long)pno, (unsigned long)iter.tno);
	    if ( rflag ||
		 confirm("Repair by setting to text_no to 0 in local map"))
	    {
		delete_list_append(&del_list, iter.lno);
		mark_person_as_changed(pno);
		modifications++;
		log("Repaired: created_texts corrected.\n");
	    }
	    else
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
872
	}
873
	
Per Cederqvist's avatar
Per Cederqvist committed
874
    }
875
    execute_deletions(created, &del_list);
Per Cederqvist's avatar
Per Cederqvist committed
876
    
877
    return errors;
Per Cederqvist's avatar
Per Cederqvist committed
878 879 880 881 882 883
}

static int
check_membership(Pers_no pno,
		 const Membership *mship)
{
884
    int errors=0;
Per Cederqvist's avatar
Per Cederqvist committed
885 886 887 888 889 890 891 892
    Conference *conf;
    int i;
    Local_text_no last=0;
    
    conf = cached_get_conf_stat(mship->conf_no);
    if ( conf == NULL )
    {
	log("Person %lu is a member in the non-existing conference %lu.\n",
893
	    (unsigned long)pno, (unsigned long)mship->conf_no);
894
	errors++;
Per Cederqvist's avatar
Per Cederqvist committed
895 896 897 898
    }
    else
    {
	/* Check read texts */
899
	if (mship->last_text_read >= l2g_first_appendable_key(&conf->texts))
Per Cederqvist's avatar
Per Cederqvist committed
900
	{
Per Cederqvist's avatar
Per Cederqvist committed
901
	    log("Person %lu %s %lu in conf %lu, which only has %lu texts.\n",
902
		(unsigned long)pno,
Per Cederqvist's avatar
Per Cederqvist committed
903
		"has read text",
904 905
		(unsigned long)mship->last_text_read,
		(unsigned long)mship->conf_no,
906
		(unsigned long)(l2g_first_appendable_key(&conf->texts) -1));
907
	    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
908 909 910 911 912 913
	}

	for ( last = i = 0; i < mship->no_of_read; i++)
	{
	    if ( mship->read_texts[i] <= last )
	    {
Per Cederqvist's avatar
Per Cederqvist committed
914
		log("Person %lu's membership in %lu %s %lu<%lu> <= %lu.\n",
915
		    (unsigned long)pno, (unsigned long)mship->conf_no,
Per Cederqvist's avatar
Per Cederqvist committed
916
		    "is corrupt: read text number",
Per Cederqvist's avatar
Per Cederqvist committed
917 918
		    (unsigned long)mship->read_texts[i], (unsigned long)i,
		    (unsigned long)last);
919
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
920 921 922 923 924 925 926 927 928
	    }

	    last = mship->read_texts[i];
	}

	/* Check that he is a member */
	if ( locate_member(pno, conf) == NULL )
	{
	    log("Person %lu is a member in %lu in which he isn't a member.\n",
929
		(unsigned long)pno, (unsigned long)mship->conf_no);
930
	    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
931 932 933
	}
    }

934
    return errors;
Per Cederqvist's avatar
Per Cederqvist committed
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
}

	    

static int
check_membership_list(Pers_no pno,
		      const Membership_list *mlist)
{
    int errors=0;
    int i;
    
    for (i = 0; i < mlist->no_of_confs; i++)
	errors += check_membership(pno, &mlist->confs[i]);

    return errors;
}


static int
check_persons(void)
{
Per Cederqvist's avatar
Per Cederqvist committed
956 957
    static char crypt_seed[] = 
	"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
Per Cederqvist's avatar
Per Cederqvist committed
958 959 960 961 962 963
    Pers_no     cp = 0;
    Person     *pstat=NULL;
    Conference *cstat=NULL;
    long	errors = 0;
    Pers_no	number_of_persons=0;

964
    while ( (cp = traverse_person(cp)) != 0 )
Per Cederqvist's avatar
Per Cederqvist committed
965 966 967 968 969 970 971
    {
	number_of_persons++;
	pstat = cached_get_person_stat (cp);
	cstat = cached_get_conf_stat (cp);
	
	if ( pstat == NULL )
	{
972
	    log("Person %lu nonexistent.\n", (unsigned long)cp);
Per Cederqvist's avatar
Per Cederqvist committed
973 974 975 976
	    errors++;
	}
	else if (cstat == NULL)
	{
977
	    log("Person %lu has no conference.\n", (unsigned long)cp);
Per Cederqvist's avatar
Per Cederqvist committed
978 979 980 981 982
	    errors++;
	}
	else if (!cstat->type.letter_box)
	{
	    log("Person %lu's conference is not a letter_box.\n",
983
		(unsigned long)cp);
Per Cederqvist's avatar
Per Cederqvist committed
984 985 986 987 988 989 990
	    errors++;
	}
	else
	{
	    errors += (check_created_texts(cp, &pstat->created_texts)
		       + check_membership_list(cp, &pstat->conferences));
	}
991 992 993 994 995 996

	if (unset_change_name_is_error == 1
	    && pstat->privileges.change_name == 0)
	{
	    log("Person %lu has no change_name capability.\n",
		(unsigned long)cp);
Per Cederqvist's avatar
Per Cederqvist committed
997

998 999 1000 1001 1002 1003 1004 1005 1006
	    if (rflag || confirm("Grant him the capability"))
	    {
		pstat->privileges.change_name = 1;
		mark_person_as_changed(cp);
		modifications++;
	    }
	    else
		errors++;
	}
Per Cederqvist's avatar
Per Cederqvist committed
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
	if (cp == reset_pwd)
	{
	    char salt[3];
	  
	    salt[0] = crypt_seed [rand() % (sizeof (crypt_seed) - 1)];
	    salt[1] = crypt_seed [rand() % (sizeof (crypt_seed) - 1)];
	    salt[2] = '\0';
	  
	    strcpy((char *)pstat->pwd, (const char *)crypt("", salt));
	    mark_person_as_changed(cp);
	    modifications++;
	}

	if (cp == grant_all)
	{
	    pstat->privileges.wheel = 1;
	    pstat->privileges.admin = 1;
	    pstat->privileges.statistic = 1;
	    pstat->privileges.create_pers = 1;
	    pstat->privileges.create_conf = 1;
	    pstat->privileges.change_name = 1;
	    pstat->privileges.extern_gw = 1;
	    pstat->privileges.flg8 = 1;
	    pstat->privileges.flg9 = 1;
	    pstat->privileges.flg10 = 1;
	    pstat->privileges.flg11 = 1;
	    pstat->privileges.flg12 = 1;
	    pstat->privileges.flg13 = 1;
	    pstat->privileges.flg14 = 1;
	    pstat->privileges.flg15 = 1;
	    pstat->privileges.flg16 = 1;
	    mark_person_as_changed(cp);
	    modifications++;
	}
Per Cederqvist's avatar
Per Cederqvist committed
1041 1042 1043
    }

    if (vflag)
1044
	log("Total of %lu persons.\n", (unsigned long)number_of_persons);
Per Cederqvist's avatar
Per Cederqvist committed
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
    
    return errors;
}

static Bool
is_recipient(Conf_no	 conf_no,
	     Text_stat * t_stat)
{
    int i;
    
    for ( i = 0; i < t_stat->no_of_misc; i++ )
    {
	switch( t_stat->misc_items[ i ].type )
	{
	case recpt:
	    if ( t_stat->misc_items[ i ].datum.recipient == conf_no )
	    {
		return TRUE;
	    }
	    break;
	    
	case cc_recpt:
	    if ( t_stat->misc_items[ i ].datum.cc_recipient == conf_no )
	    {
		return TRUE;
	    }
	    break;
	    
1073 1074 1075 1076 1077 1078 1079
	case bcc_recpt:
	    if ( t_stat->misc_items[ i ].datum.bcc_recipient == conf_no )
	    {
		return TRUE;
	    }
	    break;
	    
Per Cederqvist's avatar
Per Cederqvist committed
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
	case rec_time:
	case comm_to:
	case comm_in:
	case footn_to:
	case footn_in:
	case sent_by:
	case sent_at:
	case loc_no:
	    break;
	    
#ifndef COMPILE_CHECKS
	default:
	    restart_kom("is_recipient(): illegal misc_item\n");
#endif
	}
    }

    return FALSE;
}

static int
check_texts_in_conf(Conf_no cc,
1102
		    Local_to_global *tlist)
Per Cederqvist's avatar
Per Cederqvist committed
1103 1104
{
    Text_stat *t;
1105
    int errors=0;
1106 1107 1108 1109
    L2g_iterator iter;
    struct delete_list *del_list = NULL;
    
    for (l2gi_searchall(&iter, tlist); !iter.search_ended; l2gi_next(&iter))
Per Cederqvist's avatar
Per Cederqvist committed
1110
    {
1111 1112 1113 1114
	assert(iter.lno != 0);
	assert(iter.tno != 0);
	t = cached_get_text_stat(iter.tno);
	if (t == NULL)
Per Cederqvist's avatar
Per Cederqvist committed
1115
	{
1116 1117 1118 1119 1120 1121
	    log("Text %lu<%lu> in conference %lu is non-existent.\n",
		(unsigned long)iter.tno,
		(unsigned long)iter.lno,
		(unsigned long)cc);

	    if (rflag || confirm("Repair by deleting the Text_no in the map?"))
Per Cederqvist's avatar
Per Cederqvist committed
1122
	    {
1123 1124 1125 1126
		delete_list_append(&del_list, iter.lno);
		mark_conference_as_changed(cc);
		modifications++;
		log("Repaired: %lu is no longer a recipient.\n",
1127
		    (unsigned long)cc);
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
	    }
	    else
		errors++;
	}
	else
	{
	    if (!is_recipient(cc, t))
	    {
		log("Text %lu<%lu> in conference %lu %s.\n",
		    (unsigned long)iter.tno,
		    (unsigned long)iter.lno,
		    (unsigned long)cc,
		    "doesn't have the conference as recipient");
Per Cederqvist's avatar
Per Cederqvist committed
1141

1142
		if (confirm("Repair by deleting Text_no from the map?") )
Per Cederqvist's avatar
Per Cederqvist committed
1143
		{
1144
		    delete_list_append(&del_list, iter.lno);
Per Cederqvist's avatar
Per Cederqvist committed
1145 1146 1147
		    mark_conference_as_changed(cc);
		    modifications++;
		    log("Repaired: %lu is no longer a recipient.\n",
1148
			(unsigned long)cc);
Per Cederqvist's avatar
Per Cederqvist committed
1149 1150
		}
		else
1151
		    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
1152 1153 1154
	    }
	}
    }
1155
    execute_deletions(tlist, &del_list);
Per Cederqvist's avatar
Per Cederqvist committed
1156

1157
    return errors;
Per Cederqvist's avatar
Per Cederqvist committed
1158 1159
}

1160
Membership *
Per Cederqvist's avatar
Per Cederqvist committed
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
locate_membership(Conf_no     conf_no,
		  Person    * pers_p)
{
    Membership * confp;
    int    i;

    for(confp = pers_p->conferences.confs, i = pers_p->conferences.no_of_confs;
	i > 0; i--, confp++)
    {
	if ( confp->conf_no == conf_no )
	{
	    return confp;
	}
    }

    return NULL;
}

static int
check_member(Conf_no cc,
	     Member *memb)
{
    Person *pp;
Per Cederqvist's avatar