dbck.c 40 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
David Byers's avatar
David Byers committed
2
 * $Id: dbck.c,v 0.47 1999/05/12 13:25:08 byers 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.
 */

David Byers's avatar
David Byers committed
31 32 33 34 35 36

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


37
static const char *
David Byers's avatar
David Byers committed
38
rcsid = "$Id: dbck.c,v 0.47 1999/05/12 13:25:08 byers Exp $";
39 40
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
41

Per Cederqvist's avatar
Per Cederqvist committed
42 43 44
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
45
#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
46 47 48
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
49 50
#include <time.h>
#include <sys/types.h>
51 52 53 54 55 56
#ifdef HAVE_CRYPT_H
#  include <crypt.h>
#endif
#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif
57 58 59
#ifdef HAVE_STRING_H
#  include <string.h>
#endif
60
#include <assert.h>
61

Per Cederqvist's avatar
Per Cederqvist committed
62
#include "getopt.h"
Per Cederqvist's avatar
Per Cederqvist committed
63
#include "misc-types.h"
64
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
65
#include "kom-types.h"
Per Cederqvist's avatar
Per Cederqvist committed
66
#include "lyskomd.h"
Per Cederqvist's avatar
Per Cederqvist committed
67 68
#include "log.h"
#include "server/smalloc.h"
Per Cederqvist's avatar
Per Cederqvist committed
69
#include "misc-parser.h"
Per Cederqvist's avatar
Per Cederqvist committed
70
#include "cache.h"
David Byers's avatar
David Byers committed
71
#include "kom-config.h"
Per Cederqvist's avatar
Per Cederqvist committed
72
#include "debug.h"
Per Cederqvist's avatar
Per Cederqvist committed
73
#include "dbck-cache.h"
74 75 76
#include "param.h"
#include "server-config.h"
#include "manipulate.h"
Per Cederqvist's avatar
Per Cederqvist committed
77
#include "version-info.h"
78
#include "ram-output.h"
79
#include "unused.h"
80
#include "local-to-global.h"
Per Cederqvist's avatar
Per Cederqvist committed
81

82 83 84 85 86 87 88 89

#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
90 91 92 93 94 95 96 97 98 99 100 101
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' },
102 103 104 105 106 107 108
    {"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
109 110 111 112 113
    {"help", 0, 0, 'h' },
    { 0, 0, 0, 0 }
};

    
114

115
extern Info kom_info;
Per Cederqvist's avatar
Per Cederqvist committed
116

117 118 119 120
/* 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
121 122 123 124
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. */
125
int sflag=0;			/* Statistic flag. */
Per Cederqvist's avatar
Per Cederqvist committed
126 127
static Pers_no reset_pwd=0;	/* Person whose password should be cleared. */
static Pers_no grant_all=0;	/* Person which should receive all bits. */
128 129 130

/* The following variable holds the output format */

131
long oformat=-1;		/* Output format */
132 133
long force_output=0;		/* Force sync (for conversions) */

134 135 136 137 138 139 140 141 142
/* 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;


143 144 145 146
/* 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;

147 148 149 150
/* 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
151 152 153 154 155 156 157 158
int modifications = 0;

typedef struct {
    int	created_confs;
} Person_scratchpad;

static const Person_scratchpad   EMPTY_PERSON_SCRATCHPAD = { 0 };

David Byers's avatar
David Byers committed
159
static Person_scratchpad **person_scratchpad = NULL;
Per Cederqvist's avatar
Per Cederqvist committed
160

David Byers's avatar
David Byers committed
161 162
int buglevel = 0;
BUGDECL;
Per Cederqvist's avatar
Per Cederqvist committed
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 193 194 195

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
196
#if defined(HAVE_VFPRINTF) && defined(HAVE_STDARG_H)
Per Cederqvist's avatar
Per Cederqvist committed
197
extern void
David Byers's avatar
David Byers committed
198
kom_log (const char * format, ...)
Per Cederqvist's avatar
Per Cederqvist committed
199 200 201 202 203 204 205 206 207
{
    va_list AP;

    va_start(AP, format);

    vfprintf(stdout, format, AP);

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

Per Cederqvist's avatar
Per Cederqvist committed
218
#if defined(HAVE_VFPRINTF) && defined(HAVE_STDARG_H)
Per Cederqvist's avatar
Per Cederqvist committed
219 220 221 222 223 224 225 226 227 228 229 230
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
231
#else
232
extern void
233 234 235
restart_kom (format, a, b, c, d, e, f, g)
     const char * format;
     int a, b, c, d, e, f, g;
236 237 238 239 240
{
    fprintf(stdout, format, a, b, c, d, e, f, g);
    exit(1);
}
#endif
Per Cederqvist's avatar
Per Cederqvist committed
241

242

Per Cederqvist's avatar
Per Cederqvist committed
243 244 245 246 247 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
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;
}

342
Member *
Per Cederqvist's avatar
Per Cederqvist committed
343 344 345
locate_member(Pers_no      pers_no,
	      Conference * conf_c)
{
346
    Member * mbr;
Per Cederqvist's avatar
Per Cederqvist committed
347 348
    int      i;

349 350
    for(mbr = conf_c->members.members, i = conf_c->members.no_of_members;
	i > 0; i--, mbr++)
Per Cederqvist's avatar
Per Cederqvist committed
351
    {
352
	if ( mbr->member == pers_no )
Per Cederqvist's avatar
Per Cederqvist committed
353
	{
354
	    return mbr;
Per Cederqvist's avatar
Per Cederqvist committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
	}
    }

    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. */
375
    Bool ready = FALSE;
Per Cederqvist's avatar
Per Cederqvist committed
376 377 378 379 380 381
    
    /* Check range of misc */

    if (misc < tstat->misc_items
	|| misc >= tstat->misc_items + tstat->no_of_misc )
    {
382
	restart_kom("delete_misc() - misc out of range\n");
Per Cederqvist's avatar
Per Cederqvist committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    }

    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:
399
        case bcc_recpt:
Per Cederqvist's avatar
Per Cederqvist committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
	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
426
confirm(const char *question)
Per Cederqvist's avatar
Per Cederqvist committed
427 428 429 430 431 432 433 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
{
    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;
460 461 462
    Local_text_no conf_min;
    Local_text_no conf_max;
    
463
    long errors=0;
Per Cederqvist's avatar
Per Cederqvist committed
464 465 466 467 468 469 470 471 472 473 474 475

    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 )
	    {
David Byers's avatar
David Byers committed
476
		kom_log ("Conference 0 is recipient to text %lu.\n",
Per Cederqvist's avatar
Per Cederqvist committed
477
		     (unsigned long)tno);
Per Cederqvist's avatar
Per Cederqvist committed
478 479 480 481 482
		if (rflag || confirm("Repair by deleting misc_item? "))
		{
		    delete_misc(tstat, previous);
		    mark_text_as_changed(tno);
		    modifications++;
David Byers's avatar
David Byers committed
483
		    kom_log("Repaired: Conference 0 is no longer a recipient.\n");
Per Cederqvist's avatar
Per Cederqvist committed
484 485 486
		    misc = previous;
		}
		else
487
		    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
488 489 490 491 492 493 494 495
		
		break;
	    }

	    if ( c == NULL )
		break;
	    
	    /* Check loc_no */
496 497 498 499
	    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
500
	    {
David Byers's avatar
David Byers committed
501
		kom_log("Text %lu: Recipient %lu<%lu> loc_no is less than %lu\n",
502 503
		    (unsigned long)tno, (unsigned long)group.recipient,
		    (unsigned long)group.local_no,
504
		    (unsigned long)conf_min);
505
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
506
	    }
507
	    else if (group.local_no >= conf_max)
Per Cederqvist's avatar
Per Cederqvist committed
508
	    {
David Byers's avatar
David Byers committed
509
		kom_log("Text %lu: Recipient %lu<%lu> loc_no >= %lu\n",
510 511
		    (unsigned long)tno, (unsigned long)group.recipient,
		    (unsigned long)group.local_no,
512
		    (unsigned long)conf_max);
513
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
514
	    }
515
	    else if (l2g_lookup(&c->texts, group.local_no) != tno)
Per Cederqvist's avatar
Per Cederqvist committed
516
	    {
David Byers's avatar
David Byers committed
517
		kom_log("Text %lu: Recipient %lu<%lu>: %s to %lu.\n",
518 519
		    (unsigned long)tno, (unsigned long)group.recipient,
		    (unsigned long)group.local_no,
Per Cederqvist's avatar
Per Cederqvist committed
520
		    "that local number is mapped",
521
		    (unsigned long)l2g_lookup(&c->texts, group.local_no));
522
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
523 524 525 526 527 528 529 530
	    }

	    break;

	case m_cc_recpt:
	    c = cached_get_conf_stat (group.cc_recipient);
	    if ( c == NULL && group.cc_recipient == 0 )
	    {
David Byers's avatar
David Byers committed
531
		kom_log ("Conference 0 is cc_recipient to text %lu.\n",
532
		     (unsigned long)tno);
Per Cederqvist's avatar
Per Cederqvist committed
533 534 535 536 537
		if (rflag || confirm("Repair by deleting misc_item? "))
		{
		    delete_misc(tstat, previous);
		    mark_text_as_changed(tno);
		    modifications++;
David Byers's avatar
David Byers committed
538
		    kom_log("Repaired: Conf 0 is no longer a cc_recipient.\n");
Per Cederqvist's avatar
Per Cederqvist committed
539 540 541
		    misc = previous;
		}
		else
542
		    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
543 544 545 546 547 548 549 550
		
		break;
	    }

	    if ( c == NULL )
		break;
	    
	    /* Check loc_no */
551 552 553 554
	    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
555
	    {
David Byers's avatar
David Byers committed
556
		kom_log("Text %lu: CC_Recipient %lu<%lu> is less than %lu\n",
557 558
		    (unsigned long)tno, (unsigned long)group.cc_recipient,
		    (unsigned long)group.local_no,
559
		    (unsigned long)conf_min);
560
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
561
	    }
562
	    else if (group.local_no >= conf_max)
Per Cederqvist's avatar
Per Cederqvist committed
563
	    {
David Byers's avatar
David Byers committed
564
		kom_log("Text %lu: CC_Recipient %lu<%lu> loc_no >= %lu\n",
565 566
		    (unsigned long)tno, (unsigned long)group.cc_recipient,
		    (unsigned long)group.local_no,
567
		    (unsigned long)conf_max);
568
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
569
	    }
570
	    else if (l2g_lookup(&c->texts, group.local_no) != tno)
Per Cederqvist's avatar
Per Cederqvist committed
571
	    {
David Byers's avatar
David Byers committed
572
		kom_log("Text %lu: CC_Recipient %lu<%lu>: %s to %lu.\n",
573 574
		    (unsigned long)tno, (unsigned long)group.cc_recipient,
		    (unsigned long)group.local_no,
Per Cederqvist's avatar
Per Cederqvist committed
575
		    "that local number is mapped",
576
		    (unsigned long)l2g_lookup(&c->texts, group.local_no));
577
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
578 579 580 581
	    }

	    break;

582 583 584 585
	case m_bcc_recpt:
	    c = cached_get_conf_stat (group.bcc_recipient);
	    if ( c == NULL && group.bcc_recipient == 0 )
	    {
David Byers's avatar
David Byers committed
586
		kom_log ("Conference 0 is bcc_recipient to text %lu.\n",
587 588 589 590 591 592
		     (unsigned long)tno);
		if (rflag || confirm("Repair by deleting misc_item? "))
		{
		    delete_misc(tstat, previous);
		    mark_text_as_changed(tno);
		    modifications++;
David Byers's avatar
David Byers committed
593
		    kom_log("Repaired: Conf 0 is no longer a bcc_recipient.\n");
594 595 596
		    misc = previous;
		}
		else
597
		    errors++;
598 599 600 601 602 603 604 605
		
		break;
	    }

	    if ( c == NULL )
		break;
	    
	    /* Check loc_no */
606 607 608 609
	    conf_min = l2g_lookup(&c->texts, 0);
	    conf_max = l2g_first_appendable_key(&c->texts);

	    if (group.local_no < conf_min)
610
	    {
David Byers's avatar
David Byers committed
611
		kom_log("Text %lu: BCC_Recipient %lu<%lu> is less than %lu\n",
612 613
		    (unsigned long)tno, (unsigned long)group.bcc_recipient,
		    (unsigned long)group.local_no,
614
		    (unsigned long)conf_min);
615
		errors++;
616
	    }
617
	    else if (group.local_no >= conf_max)
618
	    {
David Byers's avatar
David Byers committed
619
		kom_log("Text %lu: BCC_Recipient %lu<%lu> loc_no >= %lu\n",
620 621
		    (unsigned long)tno, (unsigned long)group.bcc_recipient,
		    (unsigned long)group.local_no,
622
		    (unsigned long)conf_max);
623
		errors++;
624
	    }
625
	    else if (l2g_lookup(&c->texts, group.local_no) != tno)
626
	    {
David Byers's avatar
David Byers committed
627
		kom_log("Text %lu: BCC_Recipient %lu<%lu>: %s to %lu.\n",
628 629 630
		    (unsigned long)tno, (unsigned long)group.bcc_recipient,
		    (unsigned long)group.local_no,
		    "that local number is mapped",
631 632
		    (unsigned long)l2g_lookup(&c->texts,
					      group.local_no));
633
		errors++;
634 635 636 637
	    }

	    break;

Per Cederqvist's avatar
Per Cederqvist committed
638 639 640 641 642
	case m_comm_to:
	    t = cached_get_text_stat(group.comment_to);

	    if ( t == NULL )
	    {
David Byers's avatar
David Byers committed
643
		kom_log("Text %lu is a comment to %lu, which doesn't exist.\n",
644
		    (unsigned long)tno, (unsigned long)group.comment_to);
Per Cederqvist's avatar
Per Cederqvist committed
645 646 647 648 649 650

		if (rflag || confirm("Repair by deleting misc_item? "))
		{
		    delete_misc(tstat, previous);
		    mark_text_as_changed(tno);
		    modifications++;
David Byers's avatar
David Byers committed
651
		    kom_log("Repaired: Comment-link deleted.\n");
Per Cederqvist's avatar
Per Cederqvist committed
652 653 654
		    misc = previous;
		}
		else
655
		    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
656
		    
657
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
658 659 660
	    }
	    else if (!is_comment_to(tno, t))
	    {
David Byers's avatar
David Byers committed
661
		kom_log("Text %lu is a comment to %lu, but not the reverse.\n",
662
		    (unsigned long)tno, (unsigned long)group.comment_to);
663
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
664 665 666 667 668 669 670 671 672
	    }

	    break;

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

	    if ( t == NULL )
	    {
David Byers's avatar
David Byers committed
673
		kom_log("Text %lu is commented in %lu, which doesn't exist.\n",
674
		    (unsigned long)tno, (unsigned long)group.commented_in);
Per Cederqvist's avatar
Per Cederqvist committed
675 676 677 678 679 680

		if (rflag || confirm("Repair by deleting misc_item? "))
		{
		    delete_misc(tstat, previous);
		    mark_text_as_changed(tno);
		    modifications++;
David Byers's avatar
David Byers committed
681
		    kom_log("Repaired: Comment-link deleted.\n");
Per Cederqvist's avatar
Per Cederqvist committed
682 683 684
		    misc = previous;
		}
		else
685
		    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
686 687 688
	    }
	    else if (!is_commented_in(tno, t))
	    {
David Byers's avatar
David Byers committed
689
		kom_log("Text %lu is commented in %lu, but not the reverse.\n",
690
		    (unsigned long)tno, (unsigned long)group.commented_in);
691
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
692 693 694 695 696 697 698 699 700
	    }

	    break;

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

	    if ( t == NULL )
	    {
David Byers's avatar
David Byers committed
701
		kom_log("Text %lu is a footnote to %lu, which doesn't exist.\n",
702
		    (unsigned long)tno, (unsigned long)group.footnote_to);
Per Cederqvist's avatar
Per Cederqvist committed
703 704 705 706 707 708

		if (rflag || confirm("Repair by deleting misc_item? "))
		{
		    delete_misc(tstat, previous);
		    mark_text_as_changed(tno);
		    modifications++;
David Byers's avatar
David Byers committed
709
		    kom_log("Repaired: Footnote-link deleted.\n");
Per Cederqvist's avatar
Per Cederqvist committed
710 711 712
		    misc = previous;
		}
		else
713
		    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
714 715 716
	    }
	    else if (!is_footnote_to(tno, t))
	    {
David Byers's avatar
David Byers committed
717
		kom_log("Text %lu is a footnote to %lu, but not the reverse.\n",
718
		    (unsigned long)tno, (unsigned long)group.footnote_to);
719
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
720 721 722 723 724 725 726 727 728
	    }

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

	    if ( t == NULL )
	    {
David Byers's avatar
David Byers committed
729
		kom_log("Text %lu is footnoted in %lu, which doesn't exist.\n",
730
		    (unsigned long)tno, (unsigned long)group.footnoted_in);
Per Cederqvist's avatar
Per Cederqvist committed
731 732 733 734 735 736

		if (rflag || confirm("Repair by deleting misc_item? "))
		{
		    delete_misc(tstat, previous);
		    mark_text_as_changed(tno);
		    modifications++;
David Byers's avatar
David Byers committed
737
		    kom_log("Repaired: Footnote-link deleted.\n");
Per Cederqvist's avatar
Per Cederqvist committed
738 739 740
		    misc = previous;
		}
		else
741
		    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
742 743 744
	    }
	    else if (!is_footnoted_in(tno, t))
	    {
David Byers's avatar
David Byers committed
745
		kom_log("Text %lu is footnoted in %lu, but not the reverse.\n",
746
		    (unsigned long)tno, (unsigned long)group.footnoted_in);
747
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
748 749 750 751 752
	    }

	    break;

	default:
David Byers's avatar
David Byers committed
753
	    kom_log("check_misc_infos(): parse_next_misc returned type %lu\n",
754
		(unsigned long)group.type);
Per Cederqvist's avatar
Per Cederqvist committed
755 756 757 758 759 760
	    break;
	}
    }

    if ( group.type == m_error )
    {
David Byers's avatar
David Byers committed
761
	kom_log("Text %lu has a bad misc_info_list.\n", (unsigned long)tno);
762
	errors++;
Per Cederqvist's avatar
Per Cederqvist committed
763 764
    }

765
    return errors;
Per Cederqvist's avatar
Per Cederqvist committed
766 767 768 769 770 771 772 773 774 775 776 777
}

		    
		     
		 
static long
check_texts(void)
{
    Text_no    ct = 0;
    Text_stat *ctp=NULL;
    long	errors = 0;
    Text_no	number_of_texts = 0;
778 779
    unsigned long	bytes=0;
    unsigned long	max_bytes=0;
Per Cederqvist's avatar
Per Cederqvist committed
780 781
    Text_no	max_text=0;

782
    while ( (ct = traverse_text(ct)) != 0 )
Per Cederqvist's avatar
Per Cederqvist committed
783 784 785 786 787 788
    {
	number_of_texts++;
	
	ctp = cached_get_text_stat( ct );
	if ( ctp == NULL )
	{
David Byers's avatar
David Byers committed
789
	    kom_log("Text %lu nonexistent.\n", ct);
Per Cederqvist's avatar
Per Cederqvist committed
790 791 792 793
	    errors++;
	}
	else
	{
794
	    if (dump_text_numbers)
David Byers's avatar
David Byers committed
795
		kom_log("Checking text_no %ld\n", (unsigned long)ct);
796

Per Cederqvist's avatar
Per Cederqvist committed
797
	    bytes += ctp->no_of_chars;
David Byers's avatar
David Byers committed
798
	    if ( (unsigned long)ctp->no_of_chars > max_bytes )
Per Cederqvist's avatar
Per Cederqvist committed
799 800 801 802 803 804 805 806 807 808 809 810 811
	    {
		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 )
David Byers's avatar
David Byers committed
812
	    kom_log("WARNING: No texts found.\n");
Per Cederqvist's avatar
Per Cederqvist committed
813 814
	else
	{
David Byers's avatar
David Byers committed
815
	    kom_log("Total of %lu texts (total %lu bytes, avg. %lu bytes/text).\n",
816 817 818
		(unsigned long)number_of_texts,
		(unsigned long)bytes,
		(unsigned long)(bytes/number_of_texts));
David Byers's avatar
David Byers committed
819
	    kom_log("Longest text is %lu (%lu bytes).\n",
820
		(unsigned long)max_text, (unsigned long)max_bytes);
Per Cederqvist's avatar
Per Cederqvist committed
821 822 823 824 825 826 827 828 829
	}
    }
    
    return errors;
}


static int
check_created_texts(Pers_no pno,
830
		    Local_to_global *created)
Per Cederqvist's avatar
Per Cederqvist committed
831 832
{
    Text_stat *t;
833
    int errors=0;
834 835
    L2g_iterator iter;
    struct delete_list *del_list = NULL;
Per Cederqvist's avatar
Per Cederqvist committed
836

837
    for (l2gi_searchall(&iter, created); !iter.search_ended; l2gi_next(&iter))
Per Cederqvist's avatar
Per Cederqvist committed
838
    {
839 840
	assert(iter.tno != 0);
	assert(iter.lno != 0);
Per Cederqvist's avatar
Per Cederqvist committed
841

842 843 844
	t = cached_get_text_stat(iter.tno);
	if ( t != NULL && t->author != pno)
	{
David Byers's avatar
David Byers committed
845
	    kom_log("Person %lu is author of text %lu whose author is %lu.\n",
846 847 848
		(unsigned long)pno, (unsigned long)iter.tno,
		(unsigned long)t->author);
	    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
849 850
	}

851
	if ( t == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
852
	{
David Byers's avatar
David Byers committed
853
	    kom_log("Person %lu is author of text %lu, which doesn't exist.\n",
854 855 856 857 858 859 860
		(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++;
David Byers's avatar
David Byers committed
861
		kom_log("Repaired: created_texts corrected.\n");
862 863 864
	    }
	    else
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
865
	}
866
	
Per Cederqvist's avatar
Per Cederqvist committed
867
    }
868
    execute_deletions(created, &del_list);
Per Cederqvist's avatar
Per Cederqvist committed
869
    
870
    return errors;
Per Cederqvist's avatar
Per Cederqvist committed
871 872 873 874
}

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

	for ( last = i = 0; i < mship->no_of_read; i++)
	{
	    if ( mship->read_texts[i] <= last )
	    {
David Byers's avatar
David Byers committed
908
		kom_log("Person %lu's membership in %lu %s %lu<%lu> <= %lu.\n",
909
		    (unsigned long)pno, (unsigned long)mship->conf_no,
Per Cederqvist's avatar
Per Cederqvist committed
910
		    "is corrupt: read text number",
Per Cederqvist's avatar
Per Cederqvist committed
911 912
		    (unsigned long)mship->read_texts[i], (unsigned long)i,
		    (unsigned long)last);
913
		errors++;
Per Cederqvist's avatar
Per Cederqvist committed
914 915 916 917 918 919
	    }

	    last = mship->read_texts[i];
	}

	/* Check that he is a member */
David Byers's avatar
David Byers committed
920
	if ( (mem = locate_member(pno, conf)) == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
921
	{
David Byers's avatar
David Byers committed
922
	    kom_log("Person %lu is a member in %lu in which he isn't a member.\n",
923
		(unsigned long)pno, (unsigned long)mship->conf_no);
924
	    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
925
	}
David Byers's avatar
David Byers committed
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
        else
        {
            /* Check duplicated information
               Short circuit and in each check sets foo_e
               if an error has been detected
            */

            if (mem->type.invitation != mship->type.invitation ||
                mem->type.passive != mship->type.passive ||
                mem->type.secret != mship->type.secret ||
                mem->type.reserved1 != mship->type.reserved1 ||
                mem->type.reserved2 != mship->type.reserved2 ||
                mem->type.reserved3 != mship->type.reserved3 ||
                mem->type.reserved4 != mship->type.reserved4 ||
                mem->type.reserved5 != mship->type.reserved5 ||
                mem->added_at != mship->added_at ||
                mem->added_by != mship->added_by)
            {
David Byers's avatar
David Byers committed
944
                kom_log("Person %lu membership in %lu does not match member record.\n",
David Byers's avatar
David Byers committed
945 946 947 948 949 950 951 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 991
                    (unsigned long)pno,
                    (unsigned long)mship->conf_no
                    );

                errors++;

                fputs("Membership:", stdout);
                printf("  type:      %s%s%s%s%s%s%s%s\n",
                       mship->type.invitation ? "invitation " : "",
                       mship->type.passive ? "passive " : "",
                       mship->type.secret ? "secret " : "",
                       mship->type.reserved1 ? "rsv1 "  : "",
                       mship->type.reserved2 ? "rsv2 "  : "",
                       mship->type.reserved3 ? "rsv3 "  : "",
                       mship->type.reserved4 ? "rsv4 "  : "",
                       mship->type.reserved5 ? "rsv5"  : "");
                printf("  added_by: %lu\n", (unsigned long)mship->added_by);
                printf("  added_at: %lu\n", (unsigned long)mship->added_at);
                fputs("Member:", stdout);
                printf("  type:      %s%s%s%s%s%s%s%s\n",
                       mem->type.invitation ? "invitation " : "",
                       mem->type.passive ? "passive " : "",
                       mem->type.secret ? "secret " : "",
                       mem->type.reserved1 ? "rsv1 "  : "",
                       mem->type.reserved2 ? "rsv2 "  : "",
                       mem->type.reserved3 ? "rsv3 "  : "",
                       mem->type.reserved4 ? "rsv4 "  : "",
                       mem->type.reserved5 ? "rsv5"  : "");
                printf("  added_by: %lu\n", (unsigned long)mem->added_by);
                printf("  added_at: %lu\n", (unsigned long)mem->added_at);

                if (confirm("Copy membership to member"))
                {
                    mem->added_at = mship->added_at;
                    mem->added_by = mship->added_by;
                    mem->type = mship->type;
                    modifications++;
                }
                else if (confirm("Copy member to membership"))
                {
                    mship->added_at = mem->added_at;
                    mship->added_by = mem->added_by;
                    mship->type = mem->type;
                    modifications++;
                }
            }
        }
Per Cederqvist's avatar
Per Cederqvist committed
992 993
    }

994
    return errors;
Per Cederqvist's avatar
Per Cederqvist committed
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
}

	    

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
1016 1017
    static char crypt_seed[] = 
	"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
Per Cederqvist's avatar
Per Cederqvist committed
1018 1019 1020 1021 1022 1023
    Pers_no     cp = 0;
    Person     *pstat=NULL;
    Conference *cstat=NULL;
    long	errors = 0;
    Pers_no	number_of_persons=0;

1024
    while ( (cp = traverse_person(cp)) != 0 )
Per Cederqvist's avatar
Per Cederqvist committed
1025 1026 1027 1028 1029 1030 1031
    {
	number_of_persons++;
	pstat = cached_get_person_stat (cp);
	cstat = cached_get_conf_stat (cp);
	
	if ( pstat == NULL )
	{
David Byers's avatar
David Byers committed
1032
	    kom_log("Person %lu nonexistent.\n", (unsigned long)cp);
Per Cederqvist's avatar
Per Cederqvist committed
1033 1034 1035 1036
	    errors++;
	}
	else if (cstat == NULL)
	{
David Byers's avatar
David Byers committed
1037
	    kom_log("Person %lu has no conference.\n", (unsigned long)cp);
Per Cederqvist's avatar
Per Cederqvist committed
1038 1039 1040 1041
	    errors++;
	}
	else if (!cstat->type.letter_box)
	{
David Byers's avatar
David Byers committed
1042
	    kom_log("Person %lu's conference is not a letter_box.\n",
1043
		(unsigned long)cp);
Per Cederqvist's avatar
Per Cederqvist committed
1044 1045 1046 1047 1048 1049 1050
	    errors++;
	}
	else
	{
	    errors += (check_created_texts(cp, &pstat->created_texts)
		       + check_membership_list(cp, &pstat->conferences));
	}
1051 1052 1053 1054

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

1058 1059 1060 1061 1062 1063 1064 1065 1066
	    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
1067 1068 1069