dbck.c 35.5 KB
Newer Older
Linus Tolke's avatar
Linus Tolke committed
1
/*
2
 * $Id: dbck.c,v 0.32 1996/07/24 23:38:52 ceder Exp $
Per Cederqvist's avatar
Per Cederqvist committed
3
 * Copyright (C) 1991, 1992, 1993, 1994, 1995  Lysator Academic Computer Association.
Linus Tolke's avatar
Linus Tolke 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 char *rcsid = "$Id: dbck.c,v 0.32 1996/07/24 23:38:52 ceder Exp $";
32
33
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
34

Per Cederqvist's avatar
Per Cederqvist committed
35
36
37
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
38
#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
39
40
41
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
42
43
#include <time.h>
#include <sys/types.h>
44
45
46
47
48
49
#ifdef HAVE_CRYPT_H
#  include <crypt.h>
#endif
#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
50
#include "getopt.h"
Per Cederqvist's avatar
Per Cederqvist committed
51
#include "misc-types.h"
52
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
53
54
#include "kom-types.h"
#include "tmp-limits.h"
Per Cederqvist's avatar
Per Cederqvist committed
55
#include "lyskomd.h"
Per Cederqvist's avatar
Per Cederqvist committed
56
57
#include "log.h"
#include "server/smalloc.h"
Per Cederqvist's avatar
Per Cederqvist committed
58
#include "misc-parser.h"
Per Cederqvist's avatar
Per Cederqvist committed
59
60
61
#include "cache.h"
#include "config.h"
#include "debug.h"
Per Cederqvist's avatar
Per Cederqvist committed
62
#include "dbck-cache.h"
63
64
65
#include "param.h"
#include "server-config.h"
#include "manipulate.h"
Per Cederqvist's avatar
Per Cederqvist committed
66
67
#include "version-info.h"

68
69
70
71
72
73
74
75

#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
76
77
78
79
80
81
82
83
84
85
86
87
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' },
88
89
90
91
92
93
94
    {"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
95
96
97
98
99
    {"help", 0, 0, 'h' },
    { 0, 0, 0, 0 }
};

    
100

101
Info kom_info;
102
struct kom_par param;
Per Cederqvist's avatar
Per Cederqvist committed
103

104
105
106
107
/* 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
108
109
110
111
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. */
112
int sflag=0;			/* Statistic flag. */
Per Cederqvist's avatar
Per Cederqvist committed
113
114
static Pers_no reset_pwd=0;	/* Person whose password should be cleared. */
static Pers_no grant_all=0;	/* Person which should receive all bits. */
115
116
117

/* The following variable holds the output format */

118
long oformat=-1;		/* Output format */
119
120
long force_output=0;		/* Force sync (for conversions) */

121
122
123
124
125
126
127
128
129
/* 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;


130
131
132
133
/* 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;

134
135
136
137
/* 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
138
139
140
141
142
143
144
145
146
147
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
148
#ifndef NDEBUG
Per Cederqvist's avatar
Per Cederqvist committed
149
150
151
int	buglevel = 0;
#endif

Per Cederqvist's avatar
Per Cederqvist committed
152
#if defined(HAVE_VFPRINTF) && defined(HAVE_STDARG_H)
Per Cederqvist's avatar
Per Cederqvist committed
153
154
155
156
157
158
159
160
161
162
163
extern void
log (const char * format, ...)
{
    va_list AP;

    va_start(AP, format);

    vfprintf(stdout, format, AP);

    va_end(AP);
}
164
165
#else
extern void
166
167
168
log (format, a, b, c, d, e, f, g)
     const char * format;
     int a, b, c, d, e, f, g;
169
170
171
172
{
    fprintf(stdout, format, a, b, c, d, e, f, g);
}
#endif
Per Cederqvist's avatar
Per Cederqvist committed
173

Per Cederqvist's avatar
Per Cederqvist committed
174
#if defined(HAVE_VFPRINTF) && defined(HAVE_STDARG_H)
Per Cederqvist's avatar
Per Cederqvist committed
175
176
177
178
179
180
181
182
183
184
185
186
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
187
#else
188
extern void
189
190
191
restart_kom (format, a, b, c, d, e, f, g)
     const char * format;
     int a, b, c, d, e, f, g;
192
193
194
195
196
{
    fprintf(stdout, format, a, b, c, d, e, f, g);
    exit(1);
}
#endif
Per Cederqvist's avatar
Per Cederqvist committed
197

198
199
200
201
202
203
204
205
206
void 
register_jubel(Pers_no pno,
	       Text_no divis,
	       Text_no tno)
{
    /* Not needed in dbck.c, but called from conf-file.c. */
}


Per Cederqvist's avatar
Per Cederqvist committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
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;
}

306
Member *
Per Cederqvist's avatar
Per Cederqvist committed
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
347
348
349
350
351
352
353
354
355
356
357
358
359
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
387
388
389
390
391
392
393
394
395
396
397
398
399
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
426
427
428
429
430
431
432
433
434
435
436
437
438
locate_member(Pers_no      pers_no,
	      Conference * conf_c)
{
    Member * member;
    int      i;

    for(member = conf_c->members.members, i = conf_c->members.no_of_members;
	i > 0; i--, member++)
    {
	if ( member->member == pers_no )
	{
	    return member;
	}
    }

    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 )
    {
	restart_kom("delete_misc() - misc out of range");
    }

    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:
	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
confirm(char *question)
{
    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;

    long error=0;

    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
439
440
		log ("Conference 0 is recipient to text %lu.\n",
		     (unsigned long)tno);
Per Cederqvist's avatar
Per Cederqvist committed
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
		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
		    error++;
		
		break;
	    }

	    if ( c == NULL )
		break;
	    
	    /* Check loc_no */
	    if ( group.local_no < c->texts.first_local_no )
	    {
		log("Text %lu: Recipient %lu<%lu> loc_no is less than %lu\n",
462
463
464
		    (unsigned long)tno, (unsigned long)group.recipient,
		    (unsigned long)group.local_no,
		    (unsigned long)c->texts.first_local_no);
Per Cederqvist's avatar
Per Cederqvist committed
465
466
467
468
469
		error++;
	    }
	    else if ( c->texts.first_local_no + c->texts.no_of_texts - 1
		     < group.local_no )
	    {
Per Cederqvist's avatar
Per Cederqvist committed
470
		log("Text %lu: Recipient %lu<%lu> loc_no > %lu\n",
471
472
473
		    (unsigned long)tno, (unsigned long)group.recipient,
		    (unsigned long)group.local_no,
		    (unsigned long)(c->texts.first_local_no
Per Cederqvist's avatar
Per Cederqvist committed
474
475
476
477
478
479
			     + c->texts.no_of_texts - 1));
		error++;
	    }
	    else if ( c->texts.texts[group.local_no
				     - c->texts.first_local_no] != tno )
	    {
Per Cederqvist's avatar
Per Cederqvist committed
480
		log("Text %lu: Recipient %lu<%lu>: %s to %lu.\n",
481
482
		    (unsigned long)tno, (unsigned long)group.recipient,
		    (unsigned long)group.local_no,
Per Cederqvist's avatar
Per Cederqvist committed
483
		    "that local number is mapped",
484
		    (unsigned long)c->texts.texts[group.local_no
Per Cederqvist's avatar
Per Cederqvist committed
485
486
487
488
489
490
491
492
493
494
495
					   - c->texts.first_local_no]);
		error++;
	    }

	    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",
496
		     (unsigned long)tno);
Per Cederqvist's avatar
Per Cederqvist committed
497
498
499
500
501
		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
502
		    log("Repaired: Conf 0 is no longer a cc_recipient.\n");
Per Cederqvist's avatar
Per Cederqvist committed
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
		    misc = previous;
		}
		else
		    error++;
		
		break;
	    }

	    if ( c == NULL )
		break;
	    
	    /* Check loc_no */
	    if ( group.local_no < c->texts.first_local_no )
	    {
		log("Text %lu: CC_Recipient %lu<%lu> is less than %lu\n",
518
519
520
		    (unsigned long)tno, (unsigned long)group.cc_recipient,
		    (unsigned long)group.local_no,
		    (unsigned long)c->texts.first_local_no);
Per Cederqvist's avatar
Per Cederqvist committed
521
522
523
524
525
		error++;
	    }
	    else if ( c->texts.first_local_no + c->texts.no_of_texts - 1
		     < group.local_no )
	    {
Per Cederqvist's avatar
Per Cederqvist committed
526
		log("Text %lu: CC_Recipient %lu<%lu> loc_no > %lu\n",
527
528
529
		    (unsigned long)tno, (unsigned long)group.cc_recipient,
		    (unsigned long)group.local_no,
		    (unsigned long)(c->texts.first_local_no
Per Cederqvist's avatar
Per Cederqvist committed
530
531
532
533
534
535
			     + c->texts.no_of_texts - 1));
		error++;
	    }
	    else if ( c->texts.texts[group.local_no
				     - c->texts.first_local_no] != tno )
	    {
Per Cederqvist's avatar
Per Cederqvist committed
536
		log("Text %lu: CC_Recipient %lu<%lu>: %s to %lu.\n",
537
538
		    (unsigned long)tno, (unsigned long)group.cc_recipient,
		    (unsigned long)group.local_no,
Per Cederqvist's avatar
Per Cederqvist committed
539
		    "that local number is mapped",
540
		    (unsigned long)c->texts.texts[group.local_no
Per Cederqvist's avatar
Per Cederqvist committed
541
542
543
544
545
546
547
548
549
550
551
552
					   - c->texts.first_local_no]);
		error++;
	    }

	    break;

	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",
553
		    (unsigned long)tno, (unsigned long)group.comment_to);
Per Cederqvist's avatar
Per Cederqvist committed
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570

		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
		    error++;
		    
		error++;
	    }
	    else if (!is_comment_to(tno, t))
	    {
		log("Text %lu is a comment to %lu, but not the reverse.\n",
571
		    (unsigned long)tno, (unsigned long)group.comment_to);
Per Cederqvist's avatar
Per Cederqvist committed
572
573
574
575
576
577
578
579
580
581
582
		error++;
	    }

	    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",
583
		    (unsigned long)tno, (unsigned long)group.commented_in);
Per Cederqvist's avatar
Per Cederqvist committed
584
585
586
587
588
589
590
591
592
593
594
595
596
597

		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
		    error++;
	    }
	    else if (!is_commented_in(tno, t))
	    {
598
		log("Text %lu is commented in %lu, but not the reverse.\n",
599
		    (unsigned long)tno, (unsigned long)group.commented_in);
Per Cederqvist's avatar
Per Cederqvist committed
600
601
602
603
604
605
606
607
608
609
610
		error++;
	    }

	    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",
611
		    (unsigned long)tno, (unsigned long)group.footnote_to);
Per Cederqvist's avatar
Per Cederqvist committed
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626

		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
		    error++;
	    }
	    else if (!is_footnote_to(tno, t))
	    {
		log("Text %lu is a footnote to %lu, but not the reverse.\n",
627
		    (unsigned long)tno, (unsigned long)group.footnote_to);
Per Cederqvist's avatar
Per Cederqvist committed
628
629
630
631
632
633
634
635
636
637
638
		error++;
	    }

	    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",
639
		    (unsigned long)tno, (unsigned long)group.footnoted_in);
Per Cederqvist's avatar
Per Cederqvist committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653

		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
		    error++;
	    }
	    else if (!is_footnoted_in(tno, t))
	    {
654
		log("Text %lu is footnoted in %lu, but not the reverse.\n",
655
		    (unsigned long)tno, (unsigned long)group.footnoted_in);
Per Cederqvist's avatar
Per Cederqvist committed
656
657
658
659
660
661
662
		error++;
	    }

	    break;

	default:
	    log("check_misc_infos(): parse_next_misc returned type %lu\n",
663
		(unsigned long)group.type);
Per Cederqvist's avatar
Per Cederqvist committed
664
665
666
667
668
669
	    break;
	}
    }

    if ( group.type == m_error )
    {
670
	log("Text %lu has a bad misc_info_list.\n", (unsigned long)tno);
Per Cederqvist's avatar
Per Cederqvist committed
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
	error++;
    }

    return error;
}

		    
		     
		 
static long
check_texts(void)
{
    Text_no    ct = 0;
    Text_stat *ctp=NULL;
    long	errors = 0;
    Text_no	number_of_texts = 0;
687
688
    unsigned long	bytes=0;
    unsigned long	max_bytes=0;
Per Cederqvist's avatar
Per Cederqvist committed
689
690
    Text_no	max_text=0;

691
    while ( (ct = traverse_text(ct)) != 0 )
Per Cederqvist's avatar
Per Cederqvist committed
692
693
694
695
696
697
698
699
700
701
702
    {
	number_of_texts++;
	
	ctp = cached_get_text_stat( ct );
	if ( ctp == NULL )
	{
	    log("Text %lu nonexistent.\n", ct);
	    errors++;
	}
	else
	{
703
704
705
	    if (dump_text_numbers)
		log("Checking text_no %ld\n", (unsigned long)ct);

Per Cederqvist's avatar
Per Cederqvist committed
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
	    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
724
	    log("Total of %lu texts (total %lu bytes, avg. %lu bytes/text).\n",
725
726
727
		(unsigned long)number_of_texts,
		(unsigned long)bytes,
		(unsigned long)(bytes/number_of_texts));
Per Cederqvist's avatar
Per Cederqvist committed
728
	    log("Longest text is %lu (%lu bytes).\n",
729
		(unsigned long)max_text, (unsigned long)max_bytes);
Per Cederqvist's avatar
Per Cederqvist committed
730
731
732
733
734
735
736
737
738
	}
    }
    
    return errors;
}

static Bool
adjust_text_list(Text_list *text_list)
{
739
740
    unsigned long zeroes;
    unsigned long i;
Per Cederqvist's avatar
Per Cederqvist committed
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759

    for (zeroes = 0;
	 zeroes < text_list->no_of_texts && text_list->texts[ zeroes ] == 0;
	 zeroes++)
	;

    if ( zeroes > 0 )
    {
	text_list->no_of_texts -= zeroes;
	text_list->first_local_no += zeroes;

	for ( i = 0; i < text_list->no_of_texts; i++)
	    text_list->texts[ i ] = text_list->texts[ i + zeroes ];

	text_list->texts = srealloc(text_list->texts,
				    (text_list->no_of_texts
				     * sizeof(Text_no)));
    }

760
    return (zeroes > 0) ? TRUE : FALSE;
Per Cederqvist's avatar
Per Cederqvist committed
761
762
763
764
765
766
}
    
static int
check_created_texts(Pers_no pno,
		    Text_list *created)
{
767
    unsigned long i;
Per Cederqvist's avatar
Per Cederqvist committed
768
769
770
771
772
773
774
775
776
777
778
    Text_stat *t;
    int error=0;

    for ( i=0; i < created->no_of_texts; i++ )
    {
	if (created->texts[i] != 0)
	{
	    t = cached_get_text_stat(created->texts[i]);
	    if ( t != NULL && t->author != pno)
	    {
		log("Person %lu is author of text %lu whose author is %lu.\n",
779
780
		    (unsigned long)pno, (unsigned long)created->texts[i],
		    (unsigned long)t->author);
Per Cederqvist's avatar
Per Cederqvist committed
781
782
783
784
785
786
		error++;
	    }

	    if ( t == NULL )
	    {
		log("Person %lu is author of text %lu, which doesn't exist.\n",
787
		    (unsigned long)pno, (unsigned long)created->texts[i]);
Per Cederqvist's avatar
Per Cederqvist committed
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
		if ( rflag ||
		    confirm("Repair by setting to text_no to 0 in local map"))
		{
		    created->texts[i] = 0;
		    mark_person_as_changed(pno);
		    modifications++;
		    log("Repaired: created_texts corrected.\n");
		}
		else
		    error++;
	    }
	}
    }

    if ( created->no_of_texts > 0 && created->texts[0] == 0 )
    {
	log("Person %lu has a bad created_texts array. Starts with a 0.\n",
805
	    (unsigned long)pno);
Per Cederqvist's avatar
Per Cederqvist committed
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
	if ( rflag || confirm ("Repair by adjusting created_texts"))
	{
	    adjust_text_list(created);
	    mark_person_as_changed(pno);
	    modifications++;
	    log("Repaired: created_texts adjusted.\n");
	}
	else
	    error++;
    }
    
    return error;
}

static int
check_membership(Pers_no pno,
		 const Membership *mship)
{
    int error=0;
    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",
833
	    (unsigned long)pno, (unsigned long)mship->conf_no);
Per Cederqvist's avatar
Per Cederqvist committed
834
835
836
837
838
839
840
841
	error++;
    }
    else
    {
	/* Check read texts */
	if ( mship->last_text_read >
	    conf->texts.first_local_no + conf->texts.no_of_texts - 1)
	{
Per Cederqvist's avatar
Per Cederqvist committed
842
	    log("Person %lu %s %lu in conf %lu, which only has %lu texts.\n",
843
		(unsigned long)pno,
Per Cederqvist's avatar
Per Cederqvist committed
844
		"has read text",
845
846
847
		(unsigned long)mship->last_text_read,
		(unsigned long)mship->conf_no,
		(unsigned long)(conf->texts.first_local_no
Per Cederqvist's avatar
Per Cederqvist committed
848
849
850
851
852
853
854
855
			 + conf->texts.no_of_texts - 1));
	    error++;
	}

	for ( last = i = 0; i < mship->no_of_read; i++)
	{
	    if ( mship->read_texts[i] <= last )
	    {
Per Cederqvist's avatar
Per Cederqvist committed
856
		log("Person %lu's membership in %lu %s %lu<%lu> <= %lu.\n",
857
		    (unsigned long)pno, (unsigned long)mship->conf_no,
Per Cederqvist's avatar
Per Cederqvist committed
858
		    "is corrupt: read text number",
Per Cederqvist's avatar
Per Cederqvist committed
859
860
		    (unsigned long)mship->read_texts[i], (unsigned long)i,
		    (unsigned long)last);
Per Cederqvist's avatar
Per Cederqvist committed
861
862
863
864
865
866
867
868
869
870
		error++;
	    }

	    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",
871
		(unsigned long)pno, (unsigned long)mship->conf_no);
Per Cederqvist's avatar
Per Cederqvist committed
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
	    error++;
	}
    }

    return error;
}

	    

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
898
899
    static char crypt_seed[] = 
	"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
Per Cederqvist's avatar
Per Cederqvist committed
900
901
902
903
904
905
    Pers_no     cp = 0;
    Person     *pstat=NULL;
    Conference *cstat=NULL;
    long	errors = 0;
    Pers_no	number_of_persons=0;

906
    while ( (cp = traverse_person(cp)) != 0 )
Per Cederqvist's avatar
Per Cederqvist committed
907
908
909
910
911
912
913
    {
	number_of_persons++;
	pstat = cached_get_person_stat (cp);
	cstat = cached_get_conf_stat (cp);
	
	if ( pstat == NULL )
	{
914
	    log("Person %lu nonexistent.\n", (unsigned long)cp);
Per Cederqvist's avatar
Per Cederqvist committed
915
916
917
918
	    errors++;
	}
	else if (cstat == NULL)
	{
919
	    log("Person %lu has no conference.\n", (unsigned long)cp);
Per Cederqvist's avatar
Per Cederqvist committed
920
921
922
923
924
	    errors++;
	}
	else if (!cstat->type.letter_box)
	{
	    log("Person %lu's conference is not a letter_box.\n",
925
		(unsigned long)cp);
Per Cederqvist's avatar
Per Cederqvist committed
926
927
928
929
930
931
932
	    errors++;
	}
	else
	{
	    errors += (check_created_texts(cp, &pstat->created_texts)
		       + check_membership_list(cp, &pstat->conferences));
	}
933
934
935
936
937
938

	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
939

940
941
942
943
944
945
946
947
948
	    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
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
	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
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
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
    }

    if (vflag)
	log("Total of %lu persons.\n", number_of_persons);
    
    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;
	    
	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,
		    Text_list *tlist)
{
1039
    unsigned long i;
Per Cederqvist's avatar
Per Cederqvist committed
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
    Text_stat *t;
    int error=0;

    for ( i=0; i < tlist->no_of_texts; i++ )
    {
	if (tlist->texts[i] != 0)
	{
	    t = cached_get_text_stat(tlist->texts[i]);
	    if ( t == NULL )
	    {
		log("Text %lu<%lu> in conference %lu is non-existent.\n",
1051
1052
1053
		    (unsigned long)tlist->texts[i],
		    (unsigned long)i + tlist->first_local_no,
		    (unsigned long)cc);
Per Cederqvist's avatar
Per Cederqvist committed
1054
1055
1056
1057
1058
1059
1060
1061

		if (rflag
		    || confirm("Repair by setting Text_no to 0 in the map?") )
		{
		    tlist->texts[i]=0;
		    mark_conference_as_changed(cc);
		    modifications++;
		    log("Repaired: %lu is no longer a recipient.\n",
1062
			(unsigned long)cc);
Per Cederqvist's avatar
Per Cederqvist committed
1063
1064
1065
1066
1067
1068
1069
1070
		}
		else
		    error++;
	    }
	    else
	    {
		if ( !is_recipient(cc, t) )
		{
Per Cederqvist's avatar
Per Cederqvist committed
1071
		    log("Text %lu<%lu> in conference %lu %s.\n",
1072
1073
1074
			(unsigned long)tlist->texts[i],
			(unsigned long)i + tlist->first_local_no,
			(unsigned long)cc,
Per Cederqvist's avatar
Per Cederqvist committed
1075
			"doesn't have the conference as recipient");
Per Cederqvist's avatar
Per Cederqvist committed
1076
1077
1078
1079
1080
1081
1082

		    if (confirm("Repair by setting Text_no to 0 in the map?") )
		    {
			tlist->texts[i]=0;
			mark_conference_as_changed(cc);
			modifications++;
			log("Repaired: %lu is no longer a recipient.\n",
1083
			    (unsigned long)cc);
Per Cederqvist's avatar
Per Cederqvist committed
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
		    }
		    else
			error++;
		}
	    }
	}
    }

    if ( tlist->no_of_texts > 0 && tlist->texts[0] == 0 )
    {
	log("Conference %lu has a bad Text_list. Starts with a 0.\n",
1095
	    (unsigned long)cc);
Per Cederqvist's avatar
Per Cederqvist committed
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
	if ( rflag || confirm ("Repair by adjusting text_list"))
	{
	    adjust_text_list(tlist);
	    mark_conference_as_changed(cc);
	    modifications++;
	    log("Repaired: text_list adjusted.\n");
	}
	else
	    error++;
    }
    
    return error;
}

1110
Membership *
Per Cederqvist's avatar
Per Cederqvist committed
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
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;
    int error=0;

    pp = cached_get_person_stat(memb->member);
    if ( pp == NULL )
    {
Per Cederqvist's avatar
Per Cederqvist committed
1139
	log("Person %lu, supposedly a member in conf %lu, is nonexistent.\n",
1140
	    (unsigned long)memb->member, (unsigned long)cc);
Per Cederqvist's avatar
Per Cederqvist committed
1141
1142
1143
1144
1145
1146
1147
	error++;
    }
    else
    {
	if ( locate_membership(cc, pp) == NULL )
	{
	    log("Person %lu is not a member in conf %lu.\n",
1148
1149
		(unsigned long)memb->member,
		(unsigned long)cc);
Per Cederqvist's avatar
Per Cederqvist committed
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
	    error++;
	}
    }

    return error;
}

static int
check_member_list(Conf_no cc,
		  const Member_list *mlist)
{
    int errors=0;
    int i;
    
    for (i = 0; i < mlist->no_of_members; i++)
	errors += check_member(cc, &mlist->members[i]);

    return errors;
}


static int
check_confs(void)
{
    Conf_no     cc = 0;
    Person     *pstat=NULL;
    Conference *cstat=NULL;
    long	errors = 0;
    Conf_no	number_of_confs = 0;

    while ( (cc = traverse_conference(cc)) != 0 )
    {
	number_of_confs++;
	cstat = cached_get_conf_stat (cc);
	
	if ( cstat == NULL )
	{
1187
	    log("Conference %lu nonexistent.\n", (unsigned long)cc);
Per Cederqvist's avatar
Per Cederqvist committed
1188
1189
1190
1191
1192
1193
1194
1195
1196
	    errors++;
	}
	else
	{
	    if (cstat->type.letter_box)
	    {
		pstat = cached_get_person_stat(cc);
		if (pstat == NULL)
		{
1197
		    log("Mailbox %lu has no person.\n", (unsigned long)cc);
Per Cederqvist's avatar
Per Cederqvist committed
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
		    errors++;
		}
	    }
	    else		/* not letter_box */
	    {
		/* Remember that the creator might no longer exist. */
		if ( person_scratchpad[ cstat->creator ] != NULL )
		    ++person_scratchpad[ cstat->creator ]->created_confs;
	    }

	    errors += (check_texts_in_conf(cc, &cstat->texts)
		       + check_member_list(cc, &cstat->members));
	}
    }

    if ( vflag )
1214
	log("Total of %lu conferences.\n", (unsigned long)number_of_confs);
Per Cederqvist's avatar
Per Cederqvist committed
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236

    return errors;
}

static void
init_person_scratch(void)
{
    Pers_no pno = 0;
    
    while( (pno = traverse_person(pno)) != 0 )
    {
	person_scratchpad[pno] = alloc_person_scratchpad();
    }
}

static long
post_check_persons(void)
{
    long errors = 0;
    
    Pers_no pers_no = 0;
    Person *pstat;
Per Cederqvist's avatar
Per Cederqvist committed
1237
1238
  
    while ((pers_no = traverse_person(pers_no)) != 0)
Per Cederqvist's avatar
Per Cederqvist committed
1239
    {
Per Cederqvist's avatar
Per Cederqvist committed
1240
	if ((pstat = cached_get_person_stat(pers_no)) == NULL)
Per Cederqvist's avatar
Per Cederqvist committed
1241
	{
Per Cederqvist's avatar
Per Cederqvist committed
1242
1243
	    log("%s(): can't cached_get_person_stat(%d).\n", 
		"INTERNAL DBCK ERROR: post_check_persons", pers_no);
Per Cederqvist's avatar
Per Cederqvist committed
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
	}
    }

    return errors;
}

		
/*
 * Returns 0 if the database seems to be correct.
 */
static long
check_data_base(void)
{
    long errors;

    init_person_scratch();
    errors = check_texts() + check_persons() + check_confs();
    return errors + post_check_persons();
}

static void
1265
init_data_base(char *dbase_dir)
Per Cederqvist's avatar
Per Cederqvist committed
1266
1267
1268
{
    if ( vflag )
    {
1269
1270
1271
1272
	log("Database = %s\n", param.datafile_name);
	log("Backup   = %s\n", param.backupfile_name);
	log("Text     = %s\n", param.textfile_name);
	log("Textback = %s\n", param.textbackupfile_name);
Per Cederqvist's avatar
Per Cederqvist committed
1273
1274
1275
1276
1277
1278
    }
    
    if ( init_cache() == FAILURE )
	restart_kom("Can't find database.\n");
}

Per Cederqvist's avatar
Per Cederqvist committed
1279
static void
Per Cederqvist's avatar
Per Cederqvist committed
1280
1281
1282
1283
1284
garb_text_file(void)
{
    Text_no tno = 0;
    String text;

1285
1286
1287
    log("Renaming %s to %s\n", param.textfile_name, param.textbackupfile_name);
    rename(param.textfile_name, param.textbackupfile_name);
    log("Writing texts to (new) %s\n", param.textfile_name);
Per Cederqvist's avatar
Per Cederqvist committed
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
    fflush(stdout);
    fflush(stderr);
    cache_open_new_text_file();

    while ( (tno = traverse_text(tno)) != 0 )
    {
	text = cached_get_text(tno);
	cached_flush_text(tno, text);
	free_tmp();
    }
    log("Writing datafile with new indexes.\n");
    fflush(stdout);
    fflush(stderr);
    cache_sync();
    log("Ready.");    
}

	
	
Per Cederqvist's avatar
Per Cederqvist committed
1307
static void
1308
1309
1310
1311
1312
1313
1314
print_statistics(void)
{
    Text_stat *ts;
    Text_no    t;
    int  *hist;
    int i;

1315
    hist = calloc(param.text_len, sizeof(int));
1316

Per Cederqvist's avatar
Per Cederqvist committed
1317
1318
1319
1320
1321
1322
    if (hist == NULL)
    {
	perror("dbck: print_statistics(): can't calloc()");
	return;
    }
    
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
    for (t=0; (t=traverse_text(t)) != 0;)
    {
	ts = cached_get_text_stat(t);
	if (ts == NULL)
	{
	    log("print_statistics(): Can't get text_stat.\n");
	    return;
	}
		
	hist[ts->no_of_chars]++;
    }

    log("Length  Frequency\n");
1336
    for(i=0; i<param.text_len; i++)
1337
1338
1339
	if(hist[i] != 0)
	    log("%8d %d\n", i, hist[i]);
}
Per Cederqvist's avatar
Per Cederqvist committed
1340

Per Cederqvist's avatar
Per Cederqvist committed
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
static void
give_help(const char *default_config)
{
    printf("dbck for %s version %s\n",
           kom_version_info.server_name,
           kom_version_info.server_version);
    puts("Usage: dbck [options] [config_file]");
    puts("Available options:");
    puts("Short   Long option         Explanation");
    puts("  -g  --compact-text-mass   Reclaim unused space among texts.");
    puts("  -i  --interactive         Offer to repair some error types.");
    puts("  -r  --auto-repair         "
         "Repair simple errors without prompting.");
    puts("  -v  --verbose             Talk more.");
    puts("  -o  --output-version=N    File format 0 or 1.");
    puts("  -F  --force-output        Save db even if no changes.");
    puts("  -s  --print-statistics    Output a lot of statistics.");
    puts("  -t  --list-text-no        "
         "List the text number of all existing texts.");
    puts("  -c  --set-change-name     "
         "Consider unset change_name capabilities an error.");
    puts("  -P  --clear-password=pno  "
         "Set password to empty string for a user.");
    puts("  -G  --grant-all=pno       Give a user all available privileges.");
1365
1366
1367
1368
1369
1370
    puts("      --pers-pres-conf=N    Set conference for user presentations.");
    puts("      --conf-pres-conf=N    Set conference for conference "
         "presentations.");
    puts("      --kom-news-conf=N     Set conference for news about KOM.");
    puts("      --motd-conf=N         Set conference for lapps on the doors.");
    puts("      --motd-of-kom=N       Set KOM login message text.");
Per Cederqvist's avatar
Per Cederqvist committed
1371
1372
1373
1374
1375
1376
1377
1378
1379
    puts("  -h  --help                Display this help.");
    puts("Note: the -P and -G options "
         "also require a person number as argument");
    printf("The config_file argument defaults to %s\n", default_config);
    puts("WARNING: Don't allow lyskomd "
         "and dbck to modify the database simultaneously!");
    exit(0);
}

Per Cederqvist's avatar
Per Cederqvist committed
1380
1381
/* Stop "no previous prototype" warning from gcc 2.0 */
int main(int, char**);
Per Cederqvist's avatar
Per Cederqvist committed
1382
1383
1384
1385
1386

int
main (int    argc,
      char **argv)
{
Per Cederqvist's avatar
Per Cederqvist committed
1387
    int hflag = 0;
Per Cederqvist's avatar
Per Cederqvist committed
1388
    int errors;
1389
1390
    char *default_config_file;
    char *config_file;
Per Cederqvist's avatar
Per Cederqvist committed
1391
    char optc;
Per Cederqvist's avatar
Per Cederqvist committed
1392
1393
    BUGDECL;

Per Cederqvist's avatar
Per Cederqvist committed
1394
1395
    while ((optc = getopt_long(argc, argv, "girvstdcP:G:Fo:",
			       longopts, (int *) 0)) != EOF)
Per Cederqvist's avatar
Per Cederqvist committed
1396
    {
Per Cederqvist's avatar
Per Cederqvist committed
1397
	switch (optc)
Per Cederqvist's avatar
Per Cederqvist committed
1398
	{
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
        case OPT_PERS_PRESENTATION_CONF:
            pers_pres_conf = atoi(optarg);
            if (pers_pres_conf == 0)
                restart_kom("%s: bad conference number %s", argv[0], optarg);
            break;

        case OPT_CONF_PRESENTATION_CONF:
            conf_pres_conf = atoi(optarg);
            if (conf_pres_conf == 0)
                restart_kom("%s: bad conference number %s", argv[0], optarg);
            break;

        case OPT_MOTD_CONF:
            motd_conf = atoi(optarg);
            if (motd_conf == 0)
                restart_kom("%s: bad conference number %s", argv[0], optarg);
            break;

        case OPT_MOTD_OF_KOM:
            motd_of_lyskom = atoi(optarg);
            if (motd_of_lyskom == 0 && *optarg != '0')
                restart_kom("%s: bad text number %s", argv[0], optarg);
            break;

        case OPT_KOM_NEWS_CONF:
            kom_news_conf = atoi(optarg);
            if (kom_news_conf == 0)
                restart_kom("%s: bad conference number %s", argv[0], optarg);
            break;

Per Cederqvist's avatar
Per Cederqvist committed
1429
#ifndef NDEBUG
Per Cederqvist's avatar
Per Cederqvist committed
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
	case 'd':
	    buglevel++;
	    break;
#endif

	case 'i':		/* Running interactively. */
	    iflag++;		/* Will ask user and try to repair. */
	    break;

	case 'r':		/* Repair simple errors wihtout asking. */
	    rflag++;
	    break;

	case 'v':		/* Verbose: report more than errors. */
	    vflag++;
	    break;

	case 'g':		/* Garbage collect: compress text-file. */
	    gflag++;
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
1451
1452
1453
1454
	case 'h':		/* Help: give usage message. */
	    hflag++;
	    break;

1455
1456
1457
1458
	case 's':		/* Statistics: text length et c. */
	    sflag++;
	    break;

1459
1460
1461
1462
	case 'c':		/* Consider an unset change_name an error. */
	    unset_change_name_is_error = 1;
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
	case 't':
	    dump_text_numbers++;
	    break;

	case 'P':
	    reset_pwd = atol(optarg);
	    log("Will reset password of person %ld", (long)reset_pwd);
#ifdef ENCRYPT_PASSWORDS
            /* Seed the random number generator. */
            srand(time(NULL) + getpid());
#endif
	    break;

	case 'G':
	    grant_all = atol(optarg);
	    log("Will grant all bits to person %ld", (long)grant_all);

1480
1481
1482
1483
1484
	case 'F':
	    force_output = 1;
	    break;

	case 'o':		/* Select output format */
Per Cederqvist's avatar
Per Cederqvist committed
1485
1486
1487
	    oformat = atoi(optarg);
	    if (oformat == 0 && *optarg != '0')
		restart_kom("%s: bad output format %s", argv[0], optarg);
1488
1489
1490
1491
1492
1493
1494
1495
1496
	    /* Enumerate all supported output formats here. */
	    switch (oformat)
	    {
	    case 0:		/* Traditional. */
	    case 1:		/* Introduced spring 1996 by David Byers. */
		break;
	    default:
		restart_kom("%s: unknown output format %ld", argv[0], oformat);
	    }
1497
1498
	    break;
	    
Per Cederqvist's avatar
Per Cederqvist committed
1499
	default:
Per Cederqvist's avatar
Per Cederqvist committed
1500
	    restart_kom("Try --help for usage message.\n");
Per Cederqvist's avatar
Per Cederqvist committed
1501
1502
	}
    }
Per Cederqvist's avatar
Per Cederqvist committed
1503
1504

    /* Start reading in the configuration file. */
1505
1506
1507
1508

    default_config_file = smalloc(strlen(DEFAULT_DBASE_DIR) +
				  strlen(CONFIG_FILE) + 2);
    sprintf(default_config_file, "%s/%s", DEFAULT_DBASE_DIR, CONFIG_FILE);
Per Cederqvist's avatar
Per Cederqvist committed
1509
1510
1511
1512
1513
1514
1515
1516
1517

    /* Check if help was requested. */
    if (hflag > 0)
	give_help(default_config_file);

    /* Continue reading in the configuration file. */

    if (optind < argc)
	config_file = argv[optind++];
1518
1519
1520
1521
1522
1523
    else
	config_file = default_config_file;

    read_configuration(config_file);
    sfree(default_config_file);

1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
    /*
     *      Override configuration with command-line options
     */

    if (pers_pres_conf != -1)
        kom_info.pers_pres_conf = pers_pres_conf;
    if (conf_pres_conf != -1)
        kom_info.conf_pres_conf = conf_pres_conf;
    if (motd_conf != -1)
        kom_info.motd_conf = motd_conf;
    if (kom_news_conf != -1)
        kom_info.kom_news_conf = kom_news_conf;
    if (motd_of_lyskom != -1)
        kom_info.motd_of_lyskom = motd_of_lyskom;

Per Cederqvist's avatar
Per Cederqvist committed
1539
1540
1541
    if (optind != argc)
	restart_kom("%s: too many arguments.  %s --help for usage.\n",
		    argv[0], argv[0]);
1542

Per Cederqvist's avatar
Per Cederqvist committed
1543
1544
    s_set_storage_management(smalloc, srealloc, sfree);

1545
    init_data_base(param.dbase_dir);
Per Cederqvist's avatar
Per Cederqvist committed
1546
    errors = check_data_base();
1547
1548
1549
1550

    if (truncated_texts == TRUE)
	modifications++;

Per Cederqvist's avatar
Per Cederqvist committed
1551
1552
1553
1554
1555
    if ( iflag )
	log("Total of %d error%s remains.\n", errors, errors == 1 ? "" : "s");
    else if ( vflag && errors > 0 )
	log("%d error%s found.\n", errors, errors == 1 ? "" : "s");

1556
    if ( modifications > 0 || force_output)
Per Cederqvist's avatar
Per Cederqvist committed
1557
1558
1559
1560
1561
1562
1563
1564
1565
    {
	log("%d modification%s made. Syncing...\n",
	    modifications, modifications == 1 ? "" : "s");
	fflush(stdout);
	fflush(stderr);
	cache_sync();
	log("ready.\n");
    }

1566
1567
1568
    if ( sflag )
	print_statistics();
	
Per Cederqvist's avatar
Per Cederqvist committed
1569
    if ( gflag )
Per Cederqvist's avatar
Per Cederqvist committed
1570
    {
Per Cederqvist's avatar
Per Cederqvist committed
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
	if ( modifications == 0 && errors == 0 )
	{
	    log("No errors found. Compressing textfile.\n");
	    fflush(stdout);
	    fflush(stderr);
	    garb_text_file();
	    log("ready.\n");
	}
	else
	    log("Compression not done since errors was found.\n");
Per Cederqvist's avatar
Per Cederqvist committed
1581
1582
1583
1584
    }
    
    return errors != 0;
}